Fossil SCM

Update the built-in SQLite to the latest 3.40.0 alpha version that includes all bug fixes and performance enhancements.

drh 2022-10-26 10:47 trunk
Commit c7dc18847856fe678c1189517155e3e20cbde85f03181a4693f14ada3b233725
3 files changed +421 -387 +2112 -697 +33 -12
+421 -387
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -100,10 +100,18 @@
100100
# ifndef _FILE_OFFSET_BITS
101101
# define _FILE_OFFSET_BITS 64
102102
# endif
103103
# define _LARGEFILE_SOURCE 1
104104
#endif
105
+
106
+#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
107
+/*
108
+** emcc requires _POSIX_SOURCE (or one of several similar defines)
109
+** to expose strdup().
110
+*/
111
+# define _POSIX_SOURCE
112
+#endif
105113
106114
#include <stdlib.h>
107115
#include <string.h>
108116
#include <stdio.h>
109117
#include <assert.h>
@@ -256,10 +264,22 @@
256264
# define setTextMode(X,Y)
257265
#endif
258266
259267
/* True if the timer is enabled */
260268
static int enableTimer = 0;
269
+
270
+/* A version of strcmp() that works with NULL values */
271
+static int cli_strcmp(const char *a, const char *b){
272
+ if( a==0 ) a = "";
273
+ if( b==0 ) b = "";
274
+ return strcmp(a,b);
275
+}
276
+static int cli_strncmp(const char *a, const char *b, size_t n){
277
+ if( a==0 ) a = "";
278
+ if( b==0 ) b = "";
279
+ return strncmp(a,b,n);
280
+}
261281
262282
/* Return the current wall-clock time */
263283
static sqlite3_int64 timeOfDay(void){
264284
static sqlite3_vfs *clockVfs = 0;
265285
sqlite3_int64 t;
@@ -688,11 +708,11 @@
688708
/* For interactive input on Windows systems, translate the
689709
** multi-byte characterset characters into UTF-8. */
690710
if( stdin_is_interactive && in==stdin ){
691711
char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
692712
if( zTrans ){
693
- int nTrans = strlen30(zTrans)+1;
713
+ i64 nTrans = strlen(zTrans)+1;
694714
if( nTrans>nLine ){
695715
zLine = realloc(zLine, nTrans);
696716
shell_check_oom(zLine);
697717
}
698718
memcpy(zLine, zTrans, nTrans);
@@ -824,13 +844,13 @@
824844
**
825845
** If the third argument, quote, is not '\0', then it is used as a
826846
** quote character for zAppend.
827847
*/
828848
static void appendText(ShellText *p, const char *zAppend, char quote){
829
- int len;
830
- int i;
831
- int nAppend = strlen30(zAppend);
849
+ i64 len;
850
+ i64 i;
851
+ i64 nAppend = strlen30(zAppend);
832852
833853
len = nAppend+p->n+1;
834854
if( quote ){
835855
len += 2;
836856
for(i=0; i<nAppend; i++){
@@ -985,14 +1005,14 @@
9851005
const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
9861006
const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
9871007
const char *zName = (const char*)sqlite3_value_text(apVal[2]);
9881008
sqlite3 *db = sqlite3_context_db_handle(pCtx);
9891009
UNUSED_PARAMETER(nVal);
990
- if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
1010
+ if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
9911011
for(i=0; i<ArraySize(aPrefix); i++){
9921012
int n = strlen30(aPrefix[i]);
993
- if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1013
+ if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
9941014
char *z = 0;
9951015
char *zFake = 0;
9961016
if( zSchema ){
9971017
char cQuote = quoteChar(zSchema);
9981018
if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
@@ -12361,10 +12381,11 @@
1236112381
ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
1236212382
#ifdef SQLITE_SHELL_FIDDLE
1236312383
struct {
1236412384
const char * zInput; /* Input string from wasm/JS proxy */
1236512385
const char * zPos; /* Cursor pos into zInput */
12386
+ const char * zDefaultDbName; /* Default name for db file */
1236612387
} wasm;
1236712388
#endif
1236812389
};
1236912390
1237012391
#ifdef SQLITE_SHELL_FIDDLE
@@ -12873,13 +12894,13 @@
1287312894
}
1287412895
1287512896
/*
1287612897
** Output the given string as a quoted according to JSON quoting rules.
1287712898
*/
12878
-static void output_json_string(FILE *out, const char *z, int n){
12899
+static void output_json_string(FILE *out, const char *z, i64 n){
1287912900
unsigned int c;
12880
- if( n<0 ) n = (int)strlen(z);
12901
+ if( n<0 ) n = strlen(z);
1288112902
fputc('"', out);
1288212903
while( n-- ){
1288312904
c = *(z++);
1288412905
if( c=='\\' || c=='"' ){
1288512906
fputc('\\', out);
@@ -13178,11 +13199,13 @@
1317813199
/*
1317913200
** Add a new entry to the EXPLAIN QUERY PLAN data
1318013201
*/
1318113202
static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1318213203
EQPGraphRow *pNew;
13183
- int nText = strlen30(zText);
13204
+ i64 nText;
13205
+ if( zText==0 ) return;
13206
+ nText = strlen(zText);
1318413207
if( p->autoEQPtest ){
1318513208
utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1318613209
}
1318713210
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1318813211
shell_check_oom(pNew);
@@ -13223,18 +13246,18 @@
1322313246
/* Render a single level of the graph that has iEqpId as its parent. Called
1322413247
** recursively to render sublevels.
1322513248
*/
1322613249
static void eqp_render_level(ShellState *p, int iEqpId){
1322713250
EQPGraphRow *pRow, *pNext;
13228
- int n = strlen30(p->sGraph.zPrefix);
13251
+ i64 n = strlen(p->sGraph.zPrefix);
1322913252
char *z;
1323013253
for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1323113254
pNext = eqp_next_row(p, iEqpId, pRow);
1323213255
z = pRow->zText;
1323313256
utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1323413257
pNext ? "|--" : "`--", z);
13235
- if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
13258
+ if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
1323613259
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
1323713260
eqp_render_level(p, pRow->iEqpId);
1323813261
p->sGraph.zPrefix[n] = 0;
1323913262
}
1324013263
}
@@ -13946,11 +13969,11 @@
1394613969
{ "cancelled_write_bytes: ", "Cancelled write bytes:" },
1394713970
};
1394813971
int i;
1394913972
for(i=0; i<ArraySize(aTrans); i++){
1395013973
int n = strlen30(aTrans[i].zPattern);
13951
- if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13974
+ if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
1395213975
utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
1395313976
break;
1395413977
}
1395513978
}
1395613979
}
@@ -14185,11 +14208,11 @@
1418514208
** Otherwise, return zero.
1418614209
*/
1418714210
static int str_in_array(const char *zStr, const char **azArray){
1418814211
int i;
1418914212
for(i=0; azArray[i]; i++){
14190
- if( 0==strcmp(zStr, azArray[i]) ) return 1;
14213
+ if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
1419114214
}
1419214215
return 0;
1419314216
}
1419414217
1419514218
/*
@@ -14260,11 +14283,11 @@
1426014283
** it is not */
1426114284
static const char *explainCols[] = {
1426214285
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
1426314286
int jj;
1426414287
for(jj=0; jj<ArraySize(explainCols); jj++){
14265
- if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14288
+ if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
1426614289
p->cMode = p->mode;
1426714290
sqlite3_reset(pSql);
1426814291
return;
1426914292
}
1427014293
}
@@ -14984,14 +15007,14 @@
1498415007
for(i=1; rc==SQLITE_OK && i<nArg; i++){
1498515008
char *z = azArg[i];
1498615009
int n;
1498715010
if( z[0]=='-' && z[1]=='-' ) z++;
1498815011
n = strlen30(z);
14989
- if( n>=2 && 0==strncmp(z, "-verbose", n) ){
15012
+ if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
1499015013
pState->expert.bVerbose = 1;
1499115014
}
14992
- else if( n>=2 && 0==strncmp(z, "-sample", n) ){
15015
+ else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
1499315016
if( i==(nArg-1) ){
1499415017
raw_printf(stderr, "option requires an argument: %s\n", z);
1499515018
rc = SQLITE_ERROR;
1499615019
}else{
1499715020
iSample = (int)integerValue(azArg[++i]);
@@ -15335,22 +15358,24 @@
1533515358
UNUSED_PARAMETER(azNotUsed);
1533615359
if( nArg!=3 || azArg==0 ) return 0;
1533715360
zTable = azArg[0];
1533815361
zType = azArg[1];
1533915362
zSql = azArg[2];
15363
+ if( zTable==0 ) return 0;
15364
+ if( zType==0 ) return 0;
1534015365
dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
1534115366
noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
1534215367
15343
- if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
15368
+ if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
1534415369
if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
1534515370
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
1534615371
if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
15347
- }else if( strncmp(zTable, "sqlite_", 7)==0 ){
15372
+ }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
1534815373
return 0;
1534915374
}else if( dataOnly ){
1535015375
/* no-op */
15351
- }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
15376
+ }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1535215377
char *zIns;
1535315378
if( !p->writableSchema ){
1535415379
raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
1535515380
p->writableSchema = 1;
1535615381
}
@@ -15364,11 +15389,11 @@
1536415389
return 0;
1536515390
}else{
1536615391
printSchemaLine(p->out, zSql, ";\n");
1536715392
}
1536815393
15369
- if( strcmp(zType, "table")==0 ){
15394
+ if( cli_strcmp(zType, "table")==0 ){
1537015395
ShellText sSelect;
1537115396
ShellText sTable;
1537215397
char **azCol;
1537315398
int i;
1537415399
char *savedDestTable;
@@ -15608,11 +15633,11 @@
1560815633
" insert SQL insert statements for TABLE",
1560915634
" json Results in a JSON array",
1561015635
" line One value per line",
1561115636
" list Values delimited by \"|\"",
1561215637
" markdown Markdown table format",
15613
- " qbox Shorthand for \"box --width 60 --quote\"",
15638
+ " qbox Shorthand for \"box --wrap 60 --quote\"",
1561415639
" quote Escape answers as for SQL",
1561515640
" table ASCII-art table",
1561615641
" tabs Tab-separated values",
1561715642
" tcl TCL list elements",
1561815643
" OPTIONS: (for columnar modes or insert mode):",
@@ -15783,13 +15808,13 @@
1578315808
int j = 0;
1578415809
int n = 0;
1578515810
char *zPat;
1578615811
if( zPattern==0
1578715812
|| zPattern[0]=='0'
15788
- || strcmp(zPattern,"-a")==0
15789
- || strcmp(zPattern,"-all")==0
15790
- || strcmp(zPattern,"--all")==0
15813
+ || cli_strcmp(zPattern,"-a")==0
15814
+ || cli_strcmp(zPattern,"-all")==0
15815
+ || cli_strcmp(zPattern,"--all")==0
1579115816
){
1579215817
/* Show all commands, but only one line per command */
1579315818
if( zPattern==0 ) zPattern = "";
1579415819
for(i=0; i<ArraySize(azHelp); i++){
1579515820
if( azHelp[i][0]=='.' || zPattern[0] ){
@@ -16022,11 +16047,11 @@
1602216047
rc = sscanf(zLine, "| page %d offset %d", &j, &k);
1602316048
if( rc==2 ){
1602416049
iOffset = k;
1602516050
continue;
1602616051
}
16027
- if( strncmp(zLine, "| end ", 6)==0 ){
16052
+ if( cli_strncmp(zLine, "| end ", 6)==0 ){
1602816053
break;
1602916054
}
1603016055
rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
1603116056
&j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
1603216057
&x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
@@ -16050,11 +16075,11 @@
1605016075
if( in!=p->in ){
1605116076
fclose(in);
1605216077
}else{
1605316078
while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
1605416079
nLine++;
16055
- if(strncmp(zLine, "| end ", 6)==0 ) break;
16080
+ if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
1605616081
}
1605716082
p->lineno = nLine;
1605816083
}
1605916084
sqlite3_free(a);
1606016085
utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
@@ -16142,32 +16167,32 @@
1614216167
sqlite3_value **argv
1614316168
){
1614416169
const char *zText = (const char*)sqlite3_value_text(argv[0]);
1614516170
UNUSED_PARAMETER(argc);
1614616171
if( zText && zText[0]=='\'' ){
16147
- int nText = sqlite3_value_bytes(argv[0]);
16148
- int i;
16172
+ i64 nText = sqlite3_value_bytes(argv[0]);
16173
+ i64 i;
1614916174
char zBuf1[20];
1615016175
char zBuf2[20];
1615116176
const char *zNL = 0;
1615216177
const char *zCR = 0;
16153
- int nCR = 0;
16154
- int nNL = 0;
16178
+ i64 nCR = 0;
16179
+ i64 nNL = 0;
1615516180
1615616181
for(i=0; zText[i]; i++){
1615716182
if( zNL==0 && zText[i]=='\n' ){
1615816183
zNL = unused_string(zText, "\\n", "\\012", zBuf1);
16159
- nNL = (int)strlen(zNL);
16184
+ nNL = strlen(zNL);
1616016185
}
1616116186
if( zCR==0 && zText[i]=='\r' ){
1616216187
zCR = unused_string(zText, "\\r", "\\015", zBuf2);
16163
- nCR = (int)strlen(zCR);
16188
+ nCR = strlen(zCR);
1616416189
}
1616516190
}
1616616191
1616716192
if( zNL || zCR ){
16168
- int iOut = 0;
16193
+ i64 iOut = 0;
1616916194
i64 nMax = (nNL > nCR) ? nNL : nCR;
1617016195
i64 nAlloc = nMax * nText + (nMax+64)*2;
1617116196
char *zOut = (char*)sqlite3_malloc64(nAlloc);
1617216197
if( zOut==0 ){
1617316198
sqlite3_result_error_nomem(context);
@@ -16404,12 +16429,12 @@
1640416429
#elif HAVE_LINENOISE
1640516430
/*
1640616431
** Linenoise completion callback
1640716432
*/
1640816433
static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
16409
- int nLine = strlen30(zLine);
16410
- int i, iStart;
16434
+ i64 nLine = strlen(zLine);
16435
+ i64 i, iStart;
1641116436
sqlite3_stmt *pStmt = 0;
1641216437
char *zSql;
1641316438
char zBuf[1000];
1641416439
1641516440
if( nLine>sizeof(zBuf)-30 ) return;
@@ -16543,15 +16568,15 @@
1654316568
** recognized and do the right thing. NULL is returned if the output
1654416569
** filename is "off".
1654516570
*/
1654616571
static FILE *output_file_open(const char *zFile, int bTextMode){
1654716572
FILE *f;
16548
- if( strcmp(zFile,"stdout")==0 ){
16573
+ if( cli_strcmp(zFile,"stdout")==0 ){
1654916574
f = stdout;
16550
- }else if( strcmp(zFile, "stderr")==0 ){
16575
+ }else if( cli_strcmp(zFile, "stderr")==0 ){
1655116576
f = stderr;
16552
- }else if( strcmp(zFile, "off")==0 ){
16577
+ }else if( cli_strcmp(zFile, "off")==0 ){
1655316578
f = 0;
1655416579
}else{
1655516580
f = fopen(zFile, bTextMode ? "w" : "wb");
1655616581
if( f==0 ){
1655716582
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
@@ -16571,11 +16596,11 @@
1657116596
void *pX /* Auxiliary output */
1657216597
){
1657316598
ShellState *p = (ShellState*)pArg;
1657416599
sqlite3_stmt *pStmt;
1657516600
const char *zSql;
16576
- int nSql;
16601
+ i64 nSql;
1657716602
if( p->traceOut==0 ) return 0;
1657816603
if( mType==SQLITE_TRACE_CLOSE ){
1657916604
utf8_printf(p->traceOut, "-- closing database connection\n");
1658016605
return 0;
1658116606
}
@@ -16599,21 +16624,22 @@
1659916624
break;
1660016625
}
1660116626
}
1660216627
}
1660316628
if( zSql==0 ) return 0;
16604
- nSql = strlen30(zSql);
16629
+ nSql = strlen(zSql);
16630
+ if( nSql>1000000000 ) nSql = 1000000000;
1660516631
while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
1660616632
switch( mType ){
1660716633
case SQLITE_TRACE_ROW:
1660816634
case SQLITE_TRACE_STMT: {
16609
- utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
16635
+ utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
1661016636
break;
1661116637
}
1661216638
case SQLITE_TRACE_PROFILE: {
1661316639
sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16614
- utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
16640
+ utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
1661516641
break;
1661616642
}
1661716643
}
1661816644
return 0;
1661916645
}
@@ -17158,11 +17184,11 @@
1715817184
}
1715917185
raw_printf(p->out, "\n");
1716017186
}
1716117187
if( zDb==0 ){
1716217188
zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
17163
- }else if( strcmp(zDb,"temp")==0 ){
17189
+ }else if( cli_strcmp(zDb,"temp")==0 ){
1716417190
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
1716517191
}else{
1716617192
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
1716717193
}
1716817194
for(i=0; i<ArraySize(aQuery); i++){
@@ -17292,11 +17318,11 @@
1729217318
*/
1729317319
static int optionMatch(const char *zStr, const char *zOpt){
1729417320
if( zStr[0]!='-' ) return 0;
1729517321
zStr++;
1729617322
if( zStr[0]=='-' ) zStr++;
17297
- return strcmp(zStr, zOpt)==0;
17323
+ return cli_strcmp(zStr, zOpt)==0;
1729817324
}
1729917325
1730017326
/*
1730117327
** Delete a file.
1730217328
*/
@@ -19398,11 +19424,11 @@
1939819424
n = strlen30(azArg[0]);
1939919425
c = azArg[0][0];
1940019426
clearTempFile(p);
1940119427
1940219428
#ifndef SQLITE_OMIT_AUTHORIZATION
19403
- if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
19429
+ if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
1940419430
if( nArg!=2 ){
1940519431
raw_printf(stderr, "Usage: .auth ON|OFF\n");
1940619432
rc = 1;
1940719433
goto meta_command_exit;
1940819434
}
@@ -19417,20 +19443,20 @@
1941719443
}else
1941819444
#endif
1941919445
1942019446
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
1942119447
&& !defined(SQLITE_SHELL_FIDDLE)
19422
- if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19448
+ if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
1942319449
open_db(p, 0);
1942419450
failIfSafeMode(p, "cannot run .archive in safe mode");
1942519451
rc = arDotCommand(p, 0, azArg, nArg);
1942619452
}else
1942719453
#endif
1942819454
1942919455
#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)
19456
+ if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
19457
+ || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
1943219458
){
1943319459
const char *zDestFile = 0;
1943419460
const char *zDb = 0;
1943519461
sqlite3 *pDest;
1943619462
sqlite3_backup *pBackup;
@@ -19440,14 +19466,14 @@
1944019466
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
1944119467
for(j=1; j<nArg; j++){
1944219468
const char *z = azArg[j];
1944319469
if( z[0]=='-' ){
1944419470
if( z[1]=='-' ) z++;
19445
- if( strcmp(z, "-append")==0 ){
19471
+ if( cli_strcmp(z, "-append")==0 ){
1944619472
zVfs = "apndvfs";
1944719473
}else
19448
- if( strcmp(z, "-async")==0 ){
19474
+ if( cli_strcmp(z, "-async")==0 ){
1944919475
bAsync = 1;
1945019476
}else
1945119477
{
1945219478
utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
1945319479
return 1;
@@ -19495,20 +19521,20 @@
1949519521
}
1949619522
close_db(pDest);
1949719523
}else
1949819524
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1949919525
19500
- if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19526
+ if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
1950119527
if( nArg==2 ){
1950219528
bail_on_error = booleanValue(azArg[1]);
1950319529
}else{
1950419530
raw_printf(stderr, "Usage: .bail on|off\n");
1950519531
rc = 1;
1950619532
}
1950719533
}else
1950819534
19509
- if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
19535
+ if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
1951019536
if( nArg==2 ){
1951119537
if( booleanValue(azArg[1]) ){
1951219538
setBinaryMode(p->out, 1);
1951319539
}else{
1951419540
setTextMode(p->out, 1);
@@ -19520,16 +19546,16 @@
1952019546
}else
1952119547
1952219548
/* The undocumented ".breakpoint" command causes a call to the no-op
1952319549
** routine named test_breakpoint().
1952419550
*/
19525
- if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19551
+ if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
1952619552
test_breakpoint();
1952719553
}else
1952819554
1952919555
#ifndef SQLITE_SHELL_FIDDLE
19530
- if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19556
+ if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
1953119557
failIfSafeMode(p, "cannot run .cd in safe mode");
1953219558
if( nArg==2 ){
1953319559
#if defined(_WIN32) || defined(WIN32)
1953419560
wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
1953519561
rc = !SetCurrentDirectoryW(z);
@@ -19546,11 +19572,11 @@
1954619572
rc = 1;
1954719573
}
1954819574
}else
1954919575
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1955019576
19551
- if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19577
+ if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
1955219578
if( nArg==2 ){
1955319579
setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
1955419580
}else{
1955519581
raw_printf(stderr, "Usage: .changes on|off\n");
1955619582
rc = 1;
@@ -19560,11 +19586,11 @@
1956019586
#ifndef SQLITE_SHELL_FIDDLE
1956119587
/* Cancel output redirection, if it is currently set (by .testcase)
1956219588
** Then read the content of the testcase-out.txt file and compare against
1956319589
** azArg[1]. If there are differences, report an error and exit.
1956419590
*/
19565
- if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
19591
+ if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
1956619592
char *zRes = 0;
1956719593
output_reset(p);
1956819594
if( nArg!=2 ){
1956919595
raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
1957019596
rc = 2;
@@ -19583,11 +19609,11 @@
1958319609
sqlite3_free(zRes);
1958419610
}else
1958519611
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1958619612
1958719613
#ifndef SQLITE_SHELL_FIDDLE
19588
- if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19614
+ if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
1958919615
failIfSafeMode(p, "cannot run .clone in safe mode");
1959019616
if( nArg==2 ){
1959119617
tryToClone(p, azArg[1]);
1959219618
}else{
1959319619
raw_printf(stderr, "Usage: .clone FILENAME\n");
@@ -19594,11 +19620,11 @@
1959419620
rc = 1;
1959519621
}
1959619622
}else
1959719623
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
1959819624
19599
- if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19625
+ if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
1960019626
if( nArg==1 ){
1960119627
/* List available connections */
1960219628
int i;
1960319629
for(i=0; i<ArraySize(p->aAuxDb); i++){
1960419630
const char *zFile = p->aAuxDb[i].zDbFilename;
@@ -19621,11 +19647,11 @@
1962119647
p->pAuxDb->db = p->db;
1962219648
p->pAuxDb = &p->aAuxDb[i];
1962319649
globalDb = p->db = p->pAuxDb->db;
1962419650
p->pAuxDb->db = 0;
1962519651
}
19626
- }else if( nArg==3 && strcmp(azArg[1], "close")==0
19652
+ }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
1962719653
&& IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
1962819654
int i = azArg[2][0] - '0';
1962919655
if( i<0 || i>=ArraySize(p->aAuxDb) ){
1963019656
/* No-op */
1963119657
}else if( p->pAuxDb == &p->aAuxDb[i] ){
@@ -19640,11 +19666,11 @@
1964019666
raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
1964119667
rc = 1;
1964219668
}
1964319669
}else
1964419670
19645
- if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
19671
+ if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
1964619672
char **azName = 0;
1964719673
int nName = 0;
1964819674
sqlite3_stmt *pStmt;
1964919675
int i;
1965019676
open_db(p, 0);
@@ -19679,11 +19705,11 @@
1967919705
free(azName[i*2+1]);
1968019706
}
1968119707
sqlite3_free(azName);
1968219708
}else
1968319709
19684
- if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
19710
+ if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
1968519711
static const struct DbConfigChoices {
1968619712
const char *zName;
1968719713
int op;
1968819714
} aDbConfig[] = {
1968919715
{ "defensive", SQLITE_DBCONFIG_DEFENSIVE },
@@ -19704,11 +19730,11 @@
1970419730
{ "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
1970519731
};
1970619732
int ii, v;
1970719733
open_db(p, 0);
1970819734
for(ii=0; ii<ArraySize(aDbConfig); ii++){
19709
- if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
19735
+ if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
1971019736
if( nArg>=3 ){
1971119737
sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
1971219738
}
1971319739
sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
1971419740
utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
@@ -19719,21 +19745,21 @@
1971919745
utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
1972019746
}
1972119747
}else
1972219748
1972319749
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19724
- if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
19750
+ if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
1972519751
rc = shell_dbinfo_command(p, nArg, azArg);
1972619752
}else
1972719753
19728
- if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
19754
+ if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
1972919755
open_db(p, 0);
1973019756
rc = recoverDatabaseCmd(p, nArg, azArg);
1973119757
}else
1973219758
#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
1973319759
19734
- if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
19760
+ if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
1973519761
char *zLike = 0;
1973619762
char *zSql;
1973719763
int i;
1973819764
int savedShowHeader = p->showHeader;
1973919765
int savedShellFlags = p->shellFlgs;
@@ -19742,11 +19768,11 @@
1974219768
|SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
1974319769
for(i=1; i<nArg; i++){
1974419770
if( azArg[i][0]=='-' ){
1974519771
const char *z = azArg[i]+1;
1974619772
if( z[0]=='-' ) z++;
19747
- if( strcmp(z,"preserve-rowids")==0 ){
19773
+ if( cli_strcmp(z,"preserve-rowids")==0 ){
1974819774
#ifdef SQLITE_OMIT_VIRTUALTABLE
1974919775
raw_printf(stderr, "The --preserve-rowids option is not compatible"
1975019776
" with SQLITE_OMIT_VIRTUALTABLE\n");
1975119777
rc = 1;
1975219778
sqlite3_free(zLike);
@@ -19753,17 +19779,17 @@
1975319779
goto meta_command_exit;
1975419780
#else
1975519781
ShellSetFlag(p, SHFLG_PreserveRowid);
1975619782
#endif
1975719783
}else
19758
- if( strcmp(z,"newlines")==0 ){
19784
+ if( cli_strcmp(z,"newlines")==0 ){
1975919785
ShellSetFlag(p, SHFLG_Newlines);
1976019786
}else
19761
- if( strcmp(z,"data-only")==0 ){
19787
+ if( cli_strcmp(z,"data-only")==0 ){
1976219788
ShellSetFlag(p, SHFLG_DumpDataOnly);
1976319789
}else
19764
- if( strcmp(z,"nosys")==0 ){
19790
+ if( cli_strcmp(z,"nosys")==0 ){
1976519791
ShellSetFlag(p, SHFLG_DumpNoSys);
1976619792
}else
1976719793
{
1976819794
raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
1976919795
rc = 1;
@@ -19841,35 +19867,35 @@
1984119867
}
1984219868
p->showHeader = savedShowHeader;
1984319869
p->shellFlgs = savedShellFlags;
1984419870
}else
1984519871
19846
- if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
19872
+ if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
1984719873
if( nArg==2 ){
1984819874
setOrClearFlag(p, SHFLG_Echo, azArg[1]);
1984919875
}else{
1985019876
raw_printf(stderr, "Usage: .echo on|off\n");
1985119877
rc = 1;
1985219878
}
1985319879
}else
1985419880
19855
- if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
19881
+ if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
1985619882
if( nArg==2 ){
1985719883
p->autoEQPtest = 0;
1985819884
if( p->autoEQPtrace ){
1985919885
if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
1986019886
p->autoEQPtrace = 0;
1986119887
}
19862
- if( strcmp(azArg[1],"full")==0 ){
19888
+ if( cli_strcmp(azArg[1],"full")==0 ){
1986319889
p->autoEQP = AUTOEQP_full;
19864
- }else if( strcmp(azArg[1],"trigger")==0 ){
19890
+ }else if( cli_strcmp(azArg[1],"trigger")==0 ){
1986519891
p->autoEQP = AUTOEQP_trigger;
1986619892
#ifdef SQLITE_DEBUG
19867
- }else if( strcmp(azArg[1],"test")==0 ){
19893
+ }else if( cli_strcmp(azArg[1],"test")==0 ){
1986819894
p->autoEQP = AUTOEQP_on;
1986919895
p->autoEQPtest = 1;
19870
- }else if( strcmp(azArg[1],"trace")==0 ){
19896
+ }else if( cli_strcmp(azArg[1],"trace")==0 ){
1987119897
p->autoEQP = AUTOEQP_full;
1987219898
p->autoEQPtrace = 1;
1987319899
open_db(p, 0);
1987419900
sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
1987519901
sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
@@ -19882,22 +19908,22 @@
1988219908
rc = 1;
1988319909
}
1988419910
}else
1988519911
1988619912
#ifndef SQLITE_SHELL_FIDDLE
19887
- if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19913
+ if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
1988819914
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
1988919915
rc = 2;
1989019916
}else
1989119917
#endif
1989219918
1989319919
/* The ".explain" command is automatic now. It is largely pointless. It
1989419920
** retained purely for backwards compatibility */
19895
- if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19921
+ if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
1989619922
int val = 1;
1989719923
if( nArg>=2 ){
19898
- if( strcmp(azArg[1],"auto")==0 ){
19924
+ if( cli_strcmp(azArg[1],"auto")==0 ){
1989919925
val = 99;
1990019926
}else{
1990119927
val = booleanValue(azArg[1]);
1990219928
}
1990319929
}
@@ -19913,11 +19939,11 @@
1991319939
p->autoExplain = 1;
1991419940
}
1991519941
}else
1991619942
1991719943
#ifndef SQLITE_OMIT_VIRTUALTABLE
19918
- if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19944
+ if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
1991919945
if( p->bSafeMode ){
1992019946
raw_printf(stderr,
1992119947
"Cannot run experimental commands such as \"%s\" in safe mode\n",
1992219948
azArg[0]);
1992319949
rc = 1;
@@ -19926,11 +19952,11 @@
1992619952
expertDotCommand(p, azArg, nArg);
1992719953
}
1992819954
}else
1992919955
#endif
1993019956
19931
- if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19957
+ if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
1993219958
static const struct {
1993319959
const char *zCtrlName; /* Name of a test-control option */
1993419960
int ctrlCode; /* Integer code for that option */
1993519961
const char *zUsage; /* Usage notes */
1993619962
} aCtrl[] = {
@@ -19956,11 +19982,11 @@
1995619982
1995719983
open_db(p, 0);
1995819984
zCmd = nArg>=2 ? azArg[1] : "help";
1995919985
1996019986
if( zCmd[0]=='-'
19961
- && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
19987
+ && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
1996219988
&& nArg>=4
1996319989
){
1996419990
zSchema = azArg[2];
1996519991
for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
1996619992
nArg -= 2;
@@ -19972,11 +19998,11 @@
1997219998
zCmd++;
1997319999
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
1997420000
}
1997520001
1997620002
/* --help lists all file-controls */
19977
- if( strcmp(zCmd,"help")==0 ){
20003
+ if( cli_strcmp(zCmd,"help")==0 ){
1997820004
utf8_printf(p->out, "Available file-controls:\n");
1997920005
for(i=0; i<ArraySize(aCtrl); i++){
1998020006
utf8_printf(p->out, " .filectrl %s %s\n",
1998120007
aCtrl[i].zCtrlName, aCtrl[i].zUsage);
1998220008
}
@@ -19986,11 +20012,11 @@
1998620012
1998720013
/* convert filectrl text option to value. allow any unique prefix
1998820014
** of the option name, or a numerical value. */
1998920015
n2 = strlen30(zCmd);
1999020016
for(i=0; i<ArraySize(aCtrl); i++){
19991
- if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20017
+ if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
1999220018
if( filectrl<0 ){
1999320019
filectrl = aCtrl[i].ctrlCode;
1999420020
iCtrl = i;
1999520021
}else{
1999620022
utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
@@ -20073,11 +20099,11 @@
2007320099
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
2007420100
raw_printf(p->out, "%s\n", zBuf);
2007520101
}
2007620102
}else
2007720103
20078
- if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
20104
+ if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
2007920105
ShellState data;
2008020106
int doStats = 0;
2008120107
memcpy(&data, p, sizeof(data));
2008220108
data.showHeader = 0;
2008320109
data.cMode = data.mode = MODE_Semi;
@@ -20120,21 +20146,21 @@
2012020146
shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
2012120147
raw_printf(p->out, "ANALYZE sqlite_schema;\n");
2012220148
}
2012320149
}else
2012420150
20125
- if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
20151
+ if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
2012620152
if( nArg==2 ){
2012720153
p->showHeader = booleanValue(azArg[1]);
2012820154
p->shellFlgs |= SHFLG_HeaderSet;
2012920155
}else{
2013020156
raw_printf(stderr, "Usage: .headers on|off\n");
2013120157
rc = 1;
2013220158
}
2013320159
}else
2013420160
20135
- if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
20161
+ if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
2013620162
if( nArg>=2 ){
2013720163
n = showHelp(p->out, azArg[1]);
2013820164
if( n==0 ){
2013920165
utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
2014020166
}
@@ -20142,11 +20168,11 @@
2014220168
showHelp(p->out, 0);
2014320169
}
2014420170
}else
2014520171
2014620172
#ifndef SQLITE_SHELL_FIDDLE
20147
- if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
20173
+ if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
2014820174
char *zTable = 0; /* Insert data into this table */
2014920175
char *zSchema = 0; /* within this schema (may default to "main") */
2015020176
char *zFile = 0; /* Name of file to extra content from */
2015120177
sqlite3_stmt *pStmt = NULL; /* A statement */
2015220178
int nCol; /* Number of columns in the table */
@@ -20182,22 +20208,22 @@
2018220208
}else{
2018320209
utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
2018420210
showHelp(p->out, "import");
2018520211
goto meta_command_exit;
2018620212
}
20187
- }else if( strcmp(z,"-v")==0 ){
20213
+ }else if( cli_strcmp(z,"-v")==0 ){
2018820214
eVerbose++;
20189
- }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
20215
+ }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
2019020216
zSchema = azArg[++i];
20191
- }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
20217
+ }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
2019220218
nSkip = integerValue(azArg[++i]);
20193
- }else if( strcmp(z,"-ascii")==0 ){
20219
+ }else if( cli_strcmp(z,"-ascii")==0 ){
2019420220
sCtx.cColSep = SEP_Unit[0];
2019520221
sCtx.cRowSep = SEP_Record[0];
2019620222
xRead = ascii_read_one_field;
2019720223
useOutputMode = 0;
20198
- }else if( strcmp(z,"-csv")==0 ){
20224
+ }else if( cli_strcmp(z,"-csv")==0 ){
2019920225
sCtx.cColSep = ',';
2020020226
sCtx.cRowSep = '\n';
2020120227
xRead = csv_read_one_field;
2020220228
useOutputMode = 0;
2020320229
}else{
@@ -20233,11 +20259,13 @@
2023320259
if( nSep==0 ){
2023420260
raw_printf(stderr,
2023520261
"Error: non-null row separator required for import\n");
2023620262
goto meta_command_exit;
2023720263
}
20238
- if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
20264
+ if( nSep==2 && p->mode==MODE_Csv
20265
+ && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
20266
+ ){
2023920267
/* When importing CSV (only), if the row separator is set to the
2024020268
** default output row separator, change it to the default input
2024120269
** row separator. This avoids having to maintain different input
2024220270
** and output row separators. */
2024320271
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -20435,11 +20463,11 @@
2043520463
}
2043620464
}else
2043720465
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2043820466
2043920467
#ifndef SQLITE_UNTESTABLE
20440
- if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20468
+ if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
2044120469
char *zSql;
2044220470
char *zCollist = 0;
2044320471
sqlite3_stmt *pStmt;
2044420472
int tnum = 0;
2044520473
int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
@@ -20536,17 +20564,17 @@
2053620564
sqlite3_free(zSql);
2053720565
}else
2053820566
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
2053920567
2054020568
#ifdef SQLITE_ENABLE_IOTRACE
20541
- if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
20569
+ if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
2054220570
SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
2054320571
if( iotrace && iotrace!=stdout ) fclose(iotrace);
2054420572
iotrace = 0;
2054520573
if( nArg<2 ){
2054620574
sqlite3IoTrace = 0;
20547
- }else if( strcmp(azArg[1], "-")==0 ){
20575
+ }else if( cli_strcmp(azArg[1], "-")==0 ){
2054820576
sqlite3IoTrace = iotracePrintf;
2054920577
iotrace = stdout;
2055020578
}else{
2055120579
iotrace = fopen(azArg[1], "w");
2055220580
if( iotrace==0 ){
@@ -20558,11 +20586,11 @@
2055820586
}
2055920587
}
2056020588
}else
2056120589
#endif
2056220590
20563
- if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
20591
+ if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
2056420592
static const struct {
2056520593
const char *zLimitName; /* Name of a limit */
2056620594
int limitCode; /* Integer code for that limit */
2056720595
} aLimit[] = {
2056820596
{ "length", SQLITE_LIMIT_LENGTH },
@@ -20617,17 +20645,17 @@
2061720645
printf("%20s %d\n", aLimit[iLimit].zLimitName,
2061820646
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
2061920647
}
2062020648
}else
2062120649
20622
- if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20650
+ if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
2062320651
open_db(p, 0);
2062420652
lintDotCommand(p, azArg, nArg);
2062520653
}else
2062620654
2062720655
#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
20628
- if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20656
+ if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
2062920657
const char *zFile, *zProc;
2063020658
char *zErrMsg = 0;
2063120659
failIfSafeMode(p, "cannot run .load in safe mode");
2063220660
if( nArg<2 ){
2063320661
raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
@@ -20645,11 +20673,11 @@
2064520673
}
2064620674
}else
2064720675
#endif
2064820676
2064920677
#ifndef SQLITE_SHELL_FIDDLE
20650
- if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20678
+ if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
2065120679
failIfSafeMode(p, "cannot run .log in safe mode");
2065220680
if( nArg!=2 ){
2065320681
raw_printf(stderr, "Usage: .log FILENAME\n");
2065420682
rc = 1;
2065520683
}else{
@@ -20658,11 +20686,11 @@
2065820686
p->pLog = output_file_open(zFile, 0);
2065920687
}
2066020688
}else
2066120689
#endif
2066220690
20663
- if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20691
+ if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
2066420692
const char *zMode = 0;
2066520693
const char *zTabname = 0;
2066620694
int i, n2;
2066720695
ColModeOpts cmOpts = ColModeOpts_default;
2066820696
for(i=1; i<nArg; i++){
@@ -20677,14 +20705,14 @@
2067720705
cmOpts.bQuote = 1;
2067820706
}else if( optionMatch(z,"noquote") ){
2067920707
cmOpts.bQuote = 0;
2068020708
}else if( zMode==0 ){
2068120709
zMode = z;
20682
- /* Apply defaults for qbox pseudo-mods. If that
20710
+ /* Apply defaults for qbox pseudo-mode. If that
2068320711
* overwrites already-set values, user was informed of this.
2068420712
*/
20685
- if( strcmp(z, "qbox")==0 ){
20713
+ if( cli_strcmp(z, "qbox")==0 ){
2068620714
ColModeOpts cmo = ColModeOpts_default_qbox;
2068720715
zMode = "box";
2068820716
cmOpts = cmo;
2068920717
}
2069020718
}else if( zTabname==0 ){
@@ -20719,62 +20747,62 @@
2071920747
raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
2072020748
}
2072120749
zMode = modeDescr[p->mode];
2072220750
}
2072320751
n2 = strlen30(zMode);
20724
- if( strncmp(zMode,"lines",n2)==0 ){
20752
+ if( cli_strncmp(zMode,"lines",n2)==0 ){
2072520753
p->mode = MODE_Line;
2072620754
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20727
- }else if( strncmp(zMode,"columns",n2)==0 ){
20755
+ }else if( cli_strncmp(zMode,"columns",n2)==0 ){
2072820756
p->mode = MODE_Column;
2072920757
if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
2073020758
p->showHeader = 1;
2073120759
}
2073220760
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
2073320761
p->cmOpts = cmOpts;
20734
- }else if( strncmp(zMode,"list",n2)==0 ){
20762
+ }else if( cli_strncmp(zMode,"list",n2)==0 ){
2073520763
p->mode = MODE_List;
2073620764
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
2073720765
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20738
- }else if( strncmp(zMode,"html",n2)==0 ){
20766
+ }else if( cli_strncmp(zMode,"html",n2)==0 ){
2073920767
p->mode = MODE_Html;
20740
- }else if( strncmp(zMode,"tcl",n2)==0 ){
20768
+ }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
2074120769
p->mode = MODE_Tcl;
2074220770
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
2074320771
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20744
- }else if( strncmp(zMode,"csv",n2)==0 ){
20772
+ }else if( cli_strncmp(zMode,"csv",n2)==0 ){
2074520773
p->mode = MODE_Csv;
2074620774
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
2074720775
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20748
- }else if( strncmp(zMode,"tabs",n2)==0 ){
20776
+ }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
2074920777
p->mode = MODE_List;
2075020778
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20751
- }else if( strncmp(zMode,"insert",n2)==0 ){
20779
+ }else if( cli_strncmp(zMode,"insert",n2)==0 ){
2075220780
p->mode = MODE_Insert;
2075320781
set_table_name(p, zTabname ? zTabname : "table");
20754
- }else if( strncmp(zMode,"quote",n2)==0 ){
20782
+ }else if( cli_strncmp(zMode,"quote",n2)==0 ){
2075520783
p->mode = MODE_Quote;
2075620784
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
2075720785
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20758
- }else if( strncmp(zMode,"ascii",n2)==0 ){
20786
+ }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
2075920787
p->mode = MODE_Ascii;
2076020788
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
2076120789
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20762
- }else if( strncmp(zMode,"markdown",n2)==0 ){
20790
+ }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
2076320791
p->mode = MODE_Markdown;
2076420792
p->cmOpts = cmOpts;
20765
- }else if( strncmp(zMode,"table",n2)==0 ){
20793
+ }else if( cli_strncmp(zMode,"table",n2)==0 ){
2076620794
p->mode = MODE_Table;
2076720795
p->cmOpts = cmOpts;
20768
- }else if( strncmp(zMode,"box",n2)==0 ){
20796
+ }else if( cli_strncmp(zMode,"box",n2)==0 ){
2076920797
p->mode = MODE_Box;
2077020798
p->cmOpts = cmOpts;
20771
- }else if( strncmp(zMode,"count",n2)==0 ){
20799
+ }else if( cli_strncmp(zMode,"count",n2)==0 ){
2077220800
p->mode = MODE_Count;
20773
- }else if( strncmp(zMode,"off",n2)==0 ){
20801
+ }else if( cli_strncmp(zMode,"off",n2)==0 ){
2077420802
p->mode = MODE_Off;
20775
- }else if( strncmp(zMode,"json",n2)==0 ){
20803
+ }else if( cli_strncmp(zMode,"json",n2)==0 ){
2077620804
p->mode = MODE_Json;
2077720805
}else{
2077820806
raw_printf(stderr, "Error: mode should be one of: "
2077920807
"ascii box column csv html insert json line list markdown "
2078020808
"qbox quote table tabs tcl\n");
@@ -20782,15 +20810,15 @@
2078220810
}
2078320811
p->cMode = p->mode;
2078420812
}else
2078520813
2078620814
#ifndef SQLITE_SHELL_FIDDLE
20787
- if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20815
+ if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
2078820816
if( nArg!=2 ){
2078920817
raw_printf(stderr, "Usage: .nonce NONCE\n");
2079020818
rc = 1;
20791
- }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
20819
+ }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
2079220820
raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
2079320821
p->lineno, azArg[1]);
2079420822
exit(1);
2079520823
}else{
2079620824
p->bSafeMode = 0;
@@ -20798,21 +20826,21 @@
2079820826
** at the end of this procedure */
2079920827
}
2080020828
}else
2080120829
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2080220830
20803
- if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20831
+ if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
2080420832
if( nArg==2 ){
2080520833
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
2080620834
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
2080720835
}else{
2080820836
raw_printf(stderr, "Usage: .nullvalue STRING\n");
2080920837
rc = 1;
2081020838
}
2081120839
}else
2081220840
20813
- if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
20841
+ if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
2081420842
const char *zFN = 0; /* Pointer to constant filename */
2081520843
char *zNewFilename = 0; /* Name of the database file to open */
2081620844
int iName = 1; /* Index in azArg[] of the filename */
2081720845
int newFlag = 0; /* True to delete file before opening */
2081820846
int openMode = SHELL_OPEN_UNSPEC;
@@ -20872,11 +20900,11 @@
2087220900
if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
2087320901
#ifndef SQLITE_SHELL_FIDDLE
2087420902
if( p->bSafeMode
2087520903
&& p->openMode!=SHELL_OPEN_HEXDB
2087620904
&& zFN
20877
- && strcmp(zFN,":memory:")!=0
20905
+ && cli_strcmp(zFN,":memory:")!=0
2087820906
){
2087920907
failIfSafeMode(p, "cannot open disk-based database files in safe mode");
2088020908
}
2088120909
#else
2088220910
/* WASM mode has its own sandboxed pseudo-filesystem. */
@@ -20903,12 +20931,13 @@
2090320931
}
2090420932
}else
2090520933
2090620934
#ifndef SQLITE_SHELL_FIDDLE
2090720935
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)
20936
+ && (cli_strncmp(azArg[0], "output", n)==0
20937
+ || cli_strncmp(azArg[0], "once", n)==0))
20938
+ || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
2091020939
){
2091120940
char *zFile = 0;
2091220941
int bTxtMode = 0;
2091320942
int i;
2091420943
int eMode = 0;
@@ -20918,25 +20947,25 @@
2091820947
zBOM[0] = 0;
2091920948
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
2092020949
if( c=='e' ){
2092120950
eMode = 'x';
2092220951
bOnce = 2;
20923
- }else if( strncmp(azArg[0],"once",n)==0 ){
20952
+ }else if( cli_strncmp(azArg[0],"once",n)==0 ){
2092420953
bOnce = 1;
2092520954
}
2092620955
for(i=1; i<nArg; i++){
2092720956
char *z = azArg[i];
2092820957
if( z[0]=='-' ){
2092920958
if( z[1]=='-' ) z++;
20930
- if( strcmp(z,"-bom")==0 ){
20959
+ if( cli_strcmp(z,"-bom")==0 ){
2093120960
zBOM[0] = 0xef;
2093220961
zBOM[1] = 0xbb;
2093320962
zBOM[2] = 0xbf;
2093420963
zBOM[3] = 0;
20935
- }else if( c!='e' && strcmp(z,"-x")==0 ){
20964
+ }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
2093620965
eMode = 'x'; /* spreadsheet */
20937
- }else if( c!='e' && strcmp(z,"-e")==0 ){
20966
+ }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
2093820967
eMode = 'e'; /* text editor */
2093920968
}else{
2094020969
utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
2094120970
azArg[i]);
2094220971
showHelp(p->out, azArg[0]);
@@ -21005,11 +21034,11 @@
2100521034
}
2100621035
#endif
2100721036
}else{
2100821037
p->out = output_file_open(zFile, bTxtMode);
2100921038
if( p->out==0 ){
21010
- if( strcmp(zFile,"off")!=0 ){
21039
+ if( cli_strcmp(zFile,"off")!=0 ){
2101121040
utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
2101221041
}
2101321042
p->out = stdout;
2101421043
rc = 1;
2101521044
} else {
@@ -21019,26 +21048,26 @@
2101921048
}
2102021049
sqlite3_free(zFile);
2102121050
}else
2102221051
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2102321052
21024
- if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
21053
+ if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
2102521054
open_db(p,0);
2102621055
if( nArg<=1 ) goto parameter_syntax_error;
2102721056
2102821057
/* .parameter clear
2102921058
** Clear all bind parameters by dropping the TEMP table that holds them.
2103021059
*/
21031
- if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
21060
+ if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
2103221061
sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
2103321062
0, 0, 0);
2103421063
}else
2103521064
2103621065
/* .parameter list
2103721066
** List all bind parameters.
2103821067
*/
21039
- if( nArg==2 && strcmp(azArg[1],"list")==0 ){
21068
+ if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
2104021069
sqlite3_stmt *pStmt = 0;
2104121070
int rx;
2104221071
int len = 0;
2104321072
rx = sqlite3_prepare_v2(p->db,
2104421073
"SELECT max(length(key)) "
@@ -21063,21 +21092,21 @@
2106321092
2106421093
/* .parameter init
2106521094
** Make sure the TEMP table used to hold bind parameters exists.
2106621095
** Create it if necessary.
2106721096
*/
21068
- if( nArg==2 && strcmp(azArg[1],"init")==0 ){
21097
+ if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
2106921098
bind_table_init(p);
2107021099
}else
2107121100
2107221101
/* .parameter set NAME VALUE
2107321102
** Set or reset a bind parameter. NAME should be the full parameter
2107421103
** name exactly as it appears in the query. (ex: $abc, @def). The
2107521104
** VALUE can be in either SQL literal notation, or if not it will be
2107621105
** understood to be a text string.
2107721106
*/
21078
- if( nArg==4 && strcmp(azArg[1],"set")==0 ){
21107
+ if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
2107921108
int rx;
2108021109
char *zSql;
2108121110
sqlite3_stmt *pStmt;
2108221111
const char *zKey = azArg[2];
2108321112
const char *zValue = azArg[3];
@@ -21111,11 +21140,11 @@
2111121140
2111221141
/* .parameter unset NAME
2111321142
** Remove the NAME binding from the parameter binding table, if it
2111421143
** exists.
2111521144
*/
21116
- if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
21145
+ if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
2111721146
char *zSql = sqlite3_mprintf(
2111821147
"DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
2111921148
shell_check_oom(zSql);
2112021149
sqlite3_exec(p->db, zSql, 0, 0, 0);
2112121150
sqlite3_free(zSql);
@@ -21123,21 +21152,21 @@
2112321152
/* If no command name matches, show a syntax error */
2112421153
parameter_syntax_error:
2112521154
showHelp(p->out, "parameter");
2112621155
}else
2112721156
21128
- if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
21157
+ if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
2112921158
int i;
2113021159
for(i=1; i<nArg; i++){
2113121160
if( i>1 ) raw_printf(p->out, " ");
2113221161
utf8_printf(p->out, "%s", azArg[i]);
2113321162
}
2113421163
raw_printf(p->out, "\n");
2113521164
}else
2113621165
2113721166
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
21138
- if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
21167
+ if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
2113921168
int i;
2114021169
int nn = 0;
2114121170
p->flgProgress = 0;
2114221171
p->mxProgress = 0;
2114321172
p->nProgress = 0;
@@ -21144,23 +21173,23 @@
2114421173
for(i=1; i<nArg; i++){
2114521174
const char *z = azArg[i];
2114621175
if( z[0]=='-' ){
2114721176
z++;
2114821177
if( z[0]=='-' ) z++;
21149
- if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
21178
+ if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
2115021179
p->flgProgress |= SHELL_PROGRESS_QUIET;
2115121180
continue;
2115221181
}
21153
- if( strcmp(z,"reset")==0 ){
21182
+ if( cli_strcmp(z,"reset")==0 ){
2115421183
p->flgProgress |= SHELL_PROGRESS_RESET;
2115521184
continue;
2115621185
}
21157
- if( strcmp(z,"once")==0 ){
21186
+ if( cli_strcmp(z,"once")==0 ){
2115821187
p->flgProgress |= SHELL_PROGRESS_ONCE;
2115921188
continue;
2116021189
}
21161
- if( strcmp(z,"limit")==0 ){
21190
+ if( cli_strcmp(z,"limit")==0 ){
2116221191
if( i+1>=nArg ){
2116321192
utf8_printf(stderr, "Error: missing argument on --limit\n");
2116421193
rc = 1;
2116521194
goto meta_command_exit;
2116621195
}else{
@@ -21178,27 +21207,27 @@
2117821207
open_db(p, 0);
2117921208
sqlite3_progress_handler(p->db, nn, progress_handler, p);
2118021209
}else
2118121210
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2118221211
21183
- if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
21212
+ if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
2118421213
if( nArg >= 2) {
2118521214
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
2118621215
}
2118721216
if( nArg >= 3) {
2118821217
strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
2118921218
}
2119021219
}else
2119121220
2119221221
#ifndef SQLITE_SHELL_FIDDLE
21193
- if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
21222
+ if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
2119421223
rc = 2;
2119521224
}else
2119621225
#endif
2119721226
2119821227
#ifndef SQLITE_SHELL_FIDDLE
21199
- if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
21228
+ if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
2120021229
FILE *inSaved = p->in;
2120121230
int savedLineno = p->lineno;
2120221231
failIfSafeMode(p, "cannot run .read in safe mode");
2120321232
if( nArg!=2 ){
2120421233
raw_printf(stderr, "Usage: .read FILE\n");
@@ -21231,11 +21260,11 @@
2123121260
p->lineno = savedLineno;
2123221261
}else
2123321262
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2123421263
2123521264
#ifndef SQLITE_SHELL_FIDDLE
21236
- if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
21265
+ if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
2123721266
const char *zSrcFile;
2123821267
const char *zDb;
2123921268
sqlite3 *pSrc;
2124021269
sqlite3_backup *pBackup;
2124121270
int nTimeout = 0;
@@ -21284,11 +21313,11 @@
2128421313
}
2128521314
close_db(pSrc);
2128621315
}else
2128721316
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2128821317
21289
- if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21318
+ if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
2129021319
if( nArg==2 ){
2129121320
p->scanstatsOn = (u8)booleanValue(azArg[1]);
2129221321
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2129321322
raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
2129421323
#endif
@@ -21296,11 +21325,11 @@
2129621325
raw_printf(stderr, "Usage: .scanstats on|off\n");
2129721326
rc = 1;
2129821327
}
2129921328
}else
2130021329
21301
- if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
21330
+ if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
2130221331
ShellText sSelect;
2130321332
ShellState data;
2130421333
char *zErrMsg = 0;
2130521334
const char *zDiv = "(";
2130621335
const char *zName = 0;
@@ -21439,19 +21468,19 @@
2143921468
}else{
2144021469
rc = 0;
2144121470
}
2144221471
}else
2144321472
21444
- if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0)
21445
- || (c=='t' && n==9 && strncmp(azArg[0], "treetrace", n)==0)
21473
+ if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
21474
+ || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
2144621475
){
2144721476
unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
2144821477
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
2144921478
}else
2145021479
2145121480
#if defined(SQLITE_ENABLE_SESSION)
21452
- if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
21481
+ if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
2145321482
struct AuxDb *pAuxDb = p->pAuxDb;
2145421483
OpenSession *pSession = &pAuxDb->aSession[0];
2145521484
char **azCmd = &azArg[1];
2145621485
int iSes = 0;
2145721486
int nCmd = nArg - 1;
@@ -21458,11 +21487,11 @@
2145821487
int i;
2145921488
if( nArg<=1 ) goto session_syntax_error;
2146021489
open_db(p, 0);
2146121490
if( nArg>=3 ){
2146221491
for(iSes=0; iSes<pAuxDb->nSession; iSes++){
21463
- if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
21492
+ if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
2146421493
}
2146521494
if( iSes<pAuxDb->nSession ){
2146621495
pSession = &pAuxDb->aSession[iSes];
2146721496
azCmd++;
2146821497
nCmd--;
@@ -21474,11 +21503,11 @@
2147421503
2147521504
/* .session attach TABLE
2147621505
** Invoke the sqlite3session_attach() interface to attach a particular
2147721506
** table so that it is never filtered.
2147821507
*/
21479
- if( strcmp(azCmd[0],"attach")==0 ){
21508
+ if( cli_strcmp(azCmd[0],"attach")==0 ){
2148021509
if( nCmd!=2 ) goto session_syntax_error;
2148121510
if( pSession->p==0 ){
2148221511
session_not_open:
2148321512
raw_printf(stderr, "ERROR: No sessions are open\n");
2148421513
}else{
@@ -21492,11 +21521,13 @@
2149221521
2149321522
/* .session changeset FILE
2149421523
** .session patchset FILE
2149521524
** Write a changeset or patchset into a file. The file is overwritten.
2149621525
*/
21497
- if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
21526
+ if( cli_strcmp(azCmd[0],"changeset")==0
21527
+ || cli_strcmp(azCmd[0],"patchset")==0
21528
+ ){
2149821529
FILE *out = 0;
2149921530
failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
2150021531
if( nCmd!=2 ) goto session_syntax_error;
2150121532
if( pSession->p==0 ) goto session_not_open;
2150221533
out = fopen(azCmd[1], "wb");
@@ -21526,11 +21557,11 @@
2152621557
}else
2152721558
2152821559
/* .session close
2152921560
** Close the identified session
2153021561
*/
21531
- if( strcmp(azCmd[0], "close")==0 ){
21562
+ if( cli_strcmp(azCmd[0], "close")==0 ){
2153221563
if( nCmd!=1 ) goto session_syntax_error;
2153321564
if( pAuxDb->nSession ){
2153421565
session_close(pSession);
2153521566
pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
2153621567
}
@@ -21537,11 +21568,11 @@
2153721568
}else
2153821569
2153921570
/* .session enable ?BOOLEAN?
2154021571
** Query or set the enable flag
2154121572
*/
21542
- if( strcmp(azCmd[0], "enable")==0 ){
21573
+ if( cli_strcmp(azCmd[0], "enable")==0 ){
2154321574
int ii;
2154421575
if( nCmd>2 ) goto session_syntax_error;
2154521576
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
2154621577
if( pAuxDb->nSession ){
2154721578
ii = sqlite3session_enable(pSession->p, ii);
@@ -21551,11 +21582,11 @@
2155121582
}else
2155221583
2155321584
/* .session filter GLOB ....
2155421585
** Set a list of GLOB patterns of table names to be excluded.
2155521586
*/
21556
- if( strcmp(azCmd[0], "filter")==0 ){
21587
+ if( cli_strcmp(azCmd[0], "filter")==0 ){
2155721588
int ii, nByte;
2155821589
if( nCmd<2 ) goto session_syntax_error;
2155921590
if( pAuxDb->nSession ){
2156021591
for(ii=0; ii<pSession->nFilter; ii++){
2156121592
sqlite3_free(pSession->azFilter[ii]);
@@ -21576,11 +21607,11 @@
2157621607
}else
2157721608
2157821609
/* .session indirect ?BOOLEAN?
2157921610
** Query or set the indirect flag
2158021611
*/
21581
- if( strcmp(azCmd[0], "indirect")==0 ){
21612
+ if( cli_strcmp(azCmd[0], "indirect")==0 ){
2158221613
int ii;
2158321614
if( nCmd>2 ) goto session_syntax_error;
2158421615
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
2158521616
if( pAuxDb->nSession ){
2158621617
ii = sqlite3session_indirect(pSession->p, ii);
@@ -21590,11 +21621,11 @@
2159021621
}else
2159121622
2159221623
/* .session isempty
2159321624
** Determine if the session is empty
2159421625
*/
21595
- if( strcmp(azCmd[0], "isempty")==0 ){
21626
+ if( cli_strcmp(azCmd[0], "isempty")==0 ){
2159621627
int ii;
2159721628
if( nCmd!=1 ) goto session_syntax_error;
2159821629
if( pAuxDb->nSession ){
2159921630
ii = sqlite3session_isempty(pSession->p);
2160021631
utf8_printf(p->out, "session %s isempty flag = %d\n",
@@ -21603,27 +21634,27 @@
2160321634
}else
2160421635
2160521636
/* .session list
2160621637
** List all currently open sessions
2160721638
*/
21608
- if( strcmp(azCmd[0],"list")==0 ){
21639
+ if( cli_strcmp(azCmd[0],"list")==0 ){
2160921640
for(i=0; i<pAuxDb->nSession; i++){
2161021641
utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
2161121642
}
2161221643
}else
2161321644
2161421645
/* .session open DB NAME
2161521646
** Open a new session called NAME on the attached database DB.
2161621647
** DB is normally "main".
2161721648
*/
21618
- if( strcmp(azCmd[0],"open")==0 ){
21649
+ if( cli_strcmp(azCmd[0],"open")==0 ){
2161921650
char *zName;
2162021651
if( nCmd!=3 ) goto session_syntax_error;
2162121652
zName = azCmd[2];
2162221653
if( zName[0]==0 ) goto session_syntax_error;
2162321654
for(i=0; i<pAuxDb->nSession; i++){
21624
- if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
21655
+ if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
2162521656
utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
2162621657
goto meta_command_exit;
2162721658
}
2162821659
}
2162921660
if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
@@ -21650,19 +21681,19 @@
2165021681
#endif
2165121682
2165221683
#ifdef SQLITE_DEBUG
2165321684
/* Undocumented commands for internal testing. Subject to change
2165421685
** without notice. */
21655
- if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
21656
- if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
21686
+ if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
21687
+ if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
2165721688
int i, v;
2165821689
for(i=1; i<nArg; i++){
2165921690
v = booleanValue(azArg[i]);
2166021691
utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
2166121692
}
2166221693
}
21663
- if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
21694
+ if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
2166421695
int i; sqlite3_int64 v;
2166521696
for(i=1; i<nArg; i++){
2166621697
char zBuf[200];
2166721698
v = integerValue(azArg[i]);
2166821699
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
@@ -21670,11 +21701,11 @@
2167021701
}
2167121702
}
2167221703
}else
2167321704
#endif
2167421705
21675
- if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
21706
+ if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
2167621707
int bIsInit = 0; /* True to initialize the SELFTEST table */
2167721708
int bVerbose = 0; /* Verbose output */
2167821709
int bSelftestExists; /* True if SELFTEST already exists */
2167921710
int i, k; /* Loop counters */
2168021711
int nTest = 0; /* Number of tests runs */
@@ -21684,14 +21715,14 @@
2168421715
2168521716
open_db(p,0);
2168621717
for(i=1; i<nArg; i++){
2168721718
const char *z = azArg[i];
2168821719
if( z[0]=='-' && z[1]=='-' ) z++;
21689
- if( strcmp(z,"-init")==0 ){
21720
+ if( cli_strcmp(z,"-init")==0 ){
2169021721
bIsInit = 1;
2169121722
}else
21692
- if( strcmp(z,"-v")==0 ){
21723
+ if( cli_strcmp(z,"-v")==0 ){
2169321724
bVerbose++;
2169421725
}else
2169521726
{
2169621727
utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
2169721728
azArg[i], azArg[0]);
@@ -21740,14 +21771,14 @@
2174021771
if( zAns==0 ) continue;
2174121772
k = 0;
2174221773
if( bVerbose>0 ){
2174321774
printf("%d: %s %s\n", tno, zOp, zSql);
2174421775
}
21745
- if( strcmp(zOp,"memo")==0 ){
21776
+ if( cli_strcmp(zOp,"memo")==0 ){
2174621777
utf8_printf(p->out, "%s\n", zSql);
2174721778
}else
21748
- if( strcmp(zOp,"run")==0 ){
21779
+ if( cli_strcmp(zOp,"run")==0 ){
2174921780
char *zErrMsg = 0;
2175021781
str.n = 0;
2175121782
str.z[0] = 0;
2175221783
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
2175321784
nTest++;
@@ -21757,11 +21788,11 @@
2175721788
if( rc || zErrMsg ){
2175821789
nErr++;
2175921790
rc = 1;
2176021791
utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
2176121792
sqlite3_free(zErrMsg);
21762
- }else if( strcmp(zAns,str.z)!=0 ){
21793
+ }else if( cli_strcmp(zAns,str.z)!=0 ){
2176321794
nErr++;
2176421795
rc = 1;
2176521796
utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
2176621797
utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
2176721798
}
@@ -21777,11 +21808,11 @@
2177721808
} /* End loop over k */
2177821809
freeText(&str);
2177921810
utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
2178021811
}else
2178121812
21782
- if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
21813
+ if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
2178321814
if( nArg<2 || nArg>3 ){
2178421815
raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
2178521816
rc = 1;
2178621817
}
2178721818
if( nArg>=2 ){
@@ -21792,11 +21823,11 @@
2179221823
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
2179321824
"%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
2179421825
}
2179521826
}else
2179621827
21797
- if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
21828
+ if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
2179821829
const char *zLike = 0; /* Which table to checksum. 0 means everything */
2179921830
int i; /* Loop counter */
2180021831
int bSchema = 0; /* Also hash the schema */
2180121832
int bSeparate = 0; /* Hash each table separately */
2180221833
int iSize = 224; /* Hash algorithm to use */
@@ -21810,19 +21841,19 @@
2181021841
for(i=1; i<nArg; i++){
2181121842
const char *z = azArg[i];
2181221843
if( z[0]=='-' ){
2181321844
z++;
2181421845
if( z[0]=='-' ) z++;
21815
- if( strcmp(z,"schema")==0 ){
21846
+ if( cli_strcmp(z,"schema")==0 ){
2181621847
bSchema = 1;
2181721848
}else
21818
- if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
21819
- || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
21849
+ if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
21850
+ || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
2182021851
){
2182121852
iSize = atoi(&z[5]);
2182221853
}else
21823
- if( strcmp(z,"debug")==0 ){
21854
+ if( cli_strcmp(z,"debug")==0 ){
2182421855
bDebug = 1;
2182521856
}else
2182621857
{
2182721858
utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
2182821859
azArg[i], azArg[0]);
@@ -21858,24 +21889,24 @@
2185821889
zSep = "VALUES(";
2185921890
while( SQLITE_ROW==sqlite3_step(pStmt) ){
2186021891
const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
2186121892
if( zTab==0 ) continue;
2186221893
if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21863
- if( strncmp(zTab, "sqlite_",7)!=0 ){
21894
+ if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
2186421895
appendText(&sQuery,"SELECT * FROM ", 0);
2186521896
appendText(&sQuery,zTab,'"');
2186621897
appendText(&sQuery," NOT INDEXED;", 0);
21867
- }else if( strcmp(zTab, "sqlite_schema")==0 ){
21898
+ }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
2186821899
appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
2186921900
" ORDER BY name;", 0);
21870
- }else if( strcmp(zTab, "sqlite_sequence")==0 ){
21901
+ }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
2187121902
appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
2187221903
" ORDER BY name;", 0);
21873
- }else if( strcmp(zTab, "sqlite_stat1")==0 ){
21904
+ }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
2187421905
appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
2187521906
" ORDER BY tbl,idx;", 0);
21876
- }else if( strcmp(zTab, "sqlite_stat4")==0 ){
21907
+ }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
2187721908
appendText(&sQuery, "SELECT * FROM ", 0);
2187821909
appendText(&sQuery, zTab, 0);
2187921910
appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
2188021911
}
2188121912
appendText(&sSql, zSep, 0);
@@ -21910,11 +21941,12 @@
2191021941
sqlite3_free(zSql);
2191121942
}else
2191221943
2191321944
#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
2191421945
if( c=='s'
21915
- && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21946
+ && (cli_strncmp(azArg[0], "shell", n)==0
21947
+ || cli_strncmp(azArg[0],"system",n)==0)
2191621948
){
2191721949
char *zCmd;
2191821950
int i, x;
2191921951
failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
2192021952
if( nArg<2 ){
@@ -21931,11 +21963,11 @@
2193121963
sqlite3_free(zCmd);
2193221964
if( x ) raw_printf(stderr, "System command returns %d\n", x);
2193321965
}else
2193421966
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
2193521967
21936
- if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21968
+ if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
2193721969
static const char *azBool[] = { "off", "on", "trigger", "full"};
2193821970
const char *zOut;
2193921971
int i;
2194021972
if( nArg!=1 ){
2194121973
raw_printf(stderr, "Usage: .show\n");
@@ -21984,15 +22016,15 @@
2198422016
raw_printf(p->out, "\n");
2198522017
utf8_printf(p->out, "%12.12s: %s\n", "filename",
2198622018
p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
2198722019
}else
2198822020
21989
- if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
22021
+ if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
2199022022
if( nArg==2 ){
21991
- if( strcmp(azArg[1],"stmt")==0 ){
22023
+ if( cli_strcmp(azArg[1],"stmt")==0 ){
2199222024
p->statsOn = 2;
21993
- }else if( strcmp(azArg[1],"vmstep")==0 ){
22025
+ }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
2199422026
p->statsOn = 3;
2199522027
}else{
2199622028
p->statsOn = (u8)booleanValue(azArg[1]);
2199722029
}
2199822030
}else if( nArg==1 ){
@@ -22001,13 +22033,13 @@
2200122033
raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
2200222034
rc = 1;
2200322035
}
2200422036
}else
2200522037
22006
- if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
22007
- || (c=='i' && (strncmp(azArg[0], "indices", n)==0
22008
- || strncmp(azArg[0], "indexes", n)==0) )
22038
+ if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
22039
+ || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
22040
+ || cli_strncmp(azArg[0], "indexes", n)==0) )
2200922041
){
2201022042
sqlite3_stmt *pStmt;
2201122043
char **azResult;
2201222044
int nRow, nAlloc;
2201322045
int ii;
@@ -22111,11 +22143,11 @@
2211122143
sqlite3_free(azResult);
2211222144
}else
2211322145
2211422146
#ifndef SQLITE_SHELL_FIDDLE
2211522147
/* Begin redirecting output to the file "testcase-out.txt" */
22116
- if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
22148
+ if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
2211722149
output_reset(p);
2211822150
p->out = output_file_open("testcase-out.txt", 0);
2211922151
if( p->out==0 ){
2212022152
raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
2212122153
}
@@ -22126,11 +22158,11 @@
2212622158
}
2212722159
}else
2212822160
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
2212922161
2213022162
#ifndef SQLITE_UNTESTABLE
22131
- if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
22163
+ if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
2213222164
static const struct {
2213322165
const char *zCtrlName; /* Name of a test-control option */
2213422166
int ctrlCode; /* Integer code for that option */
2213522167
int unSafe; /* Not valid for --safe mode */
2213622168
const char *zUsage; /* Usage notes */
@@ -22173,11 +22205,11 @@
2217322205
zCmd++;
2217422206
if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
2217522207
}
2217622208
2217722209
/* --help lists all test-controls */
22178
- if( strcmp(zCmd,"help")==0 ){
22210
+ if( cli_strcmp(zCmd,"help")==0 ){
2217922211
utf8_printf(p->out, "Available test-controls:\n");
2218022212
for(i=0; i<ArraySize(aCtrl); i++){
2218122213
utf8_printf(p->out, " .testctrl %s %s\n",
2218222214
aCtrl[i].zCtrlName, aCtrl[i].zUsage);
2218322215
}
@@ -22187,11 +22219,11 @@
2218722219
2218822220
/* convert testctrl text option to value. allow any unique prefix
2218922221
** of the option name, or a numerical value. */
2219022222
n2 = strlen30(zCmd);
2219122223
for(i=0; i<ArraySize(aCtrl); i++){
22192
- if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
22224
+ if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
2219322225
if( testctrl<0 ){
2219422226
testctrl = aCtrl[i].ctrlCode;
2219522227
iCtrl = i;
2219622228
}else{
2219722229
utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
@@ -22243,11 +22275,11 @@
2224322275
/* sqlite3_test_control(int, int, sqlite3*) */
2224422276
case SQLITE_TESTCTRL_PRNG_SEED:
2224522277
if( nArg==3 || nArg==4 ){
2224622278
int ii = (int)integerValue(azArg[2]);
2224722279
sqlite3 *db;
22248
- if( ii==0 && strcmp(azArg[2],"random")==0 ){
22280
+ if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
2224922281
sqlite3_randomness(sizeof(ii),&ii);
2225022282
printf("-- random seed: %d\n", ii);
2225122283
}
2225222284
if( nArg==3 ){
2225322285
db = 0;
@@ -22359,16 +22391,16 @@
2235922391
raw_printf(p->out, "0x%08x\n", rc2);
2236022392
}
2236122393
}else
2236222394
#endif /* !defined(SQLITE_UNTESTABLE) */
2236322395
22364
- if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
22396
+ if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
2236522397
open_db(p, 0);
2236622398
sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
2236722399
}else
2236822400
22369
- if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
22401
+ if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
2237022402
if( nArg==2 ){
2237122403
enableTimer = booleanValue(azArg[1]);
2237222404
if( enableTimer && !HAS_TIMER ){
2237322405
raw_printf(stderr, "Error: timer not available on this system.\n");
2237422406
enableTimer = 0;
@@ -22378,11 +22410,11 @@
2237822410
rc = 1;
2237922411
}
2238022412
}else
2238122413
2238222414
#ifndef SQLITE_OMIT_TRACE
22383
- if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
22415
+ if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
2238422416
int mType = 0;
2238522417
int jj;
2238622418
open_db(p, 0);
2238722419
for(jj=1; jj<nArg; jj++){
2238822420
const char *z = azArg[jj];
@@ -22428,11 +22460,11 @@
2242822460
}
2242922461
}else
2243022462
#endif /* !defined(SQLITE_OMIT_TRACE) */
2243122463
2243222464
#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22433
- if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
22465
+ if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
2243422466
int ii;
2243522467
int lenOpt;
2243622468
char *zOpt;
2243722469
if( nArg<2 ){
2243822470
raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
@@ -22441,11 +22473,11 @@
2244122473
}
2244222474
open_db(p, 0);
2244322475
zOpt = azArg[1];
2244422476
if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
2244522477
lenOpt = (int)strlen(zOpt);
22446
- if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
22478
+ if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
2244722479
assert( azArg[nArg]==0 );
2244822480
sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
2244922481
}else{
2245022482
for(ii=1; ii<nArg; ii++){
2245122483
sqlite3_create_module(p->db, azArg[ii], 0, 0);
@@ -22453,18 +22485,18 @@
2245322485
}
2245422486
}else
2245522487
#endif
2245622488
2245722489
#if SQLITE_USER_AUTHENTICATION
22458
- if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
22490
+ if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
2245922491
if( nArg<2 ){
2246022492
raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
2246122493
rc = 1;
2246222494
goto meta_command_exit;
2246322495
}
2246422496
open_db(p, 0);
22465
- if( strcmp(azArg[1],"login")==0 ){
22497
+ if( cli_strcmp(azArg[1],"login")==0 ){
2246622498
if( nArg!=4 ){
2246722499
raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
2246822500
rc = 1;
2246922501
goto meta_command_exit;
2247022502
}
@@ -22472,11 +22504,11 @@
2247222504
strlen30(azArg[3]));
2247322505
if( rc ){
2247422506
utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
2247522507
rc = 1;
2247622508
}
22477
- }else if( strcmp(azArg[1],"add")==0 ){
22509
+ }else if( cli_strcmp(azArg[1],"add")==0 ){
2247822510
if( nArg!=5 ){
2247922511
raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
2248022512
rc = 1;
2248122513
goto meta_command_exit;
2248222514
}
@@ -22484,11 +22516,11 @@
2248422516
booleanValue(azArg[4]));
2248522517
if( rc ){
2248622518
raw_printf(stderr, "User-Add failed: %d\n", rc);
2248722519
rc = 1;
2248822520
}
22489
- }else if( strcmp(azArg[1],"edit")==0 ){
22521
+ }else if( cli_strcmp(azArg[1],"edit")==0 ){
2249022522
if( nArg!=5 ){
2249122523
raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
2249222524
rc = 1;
2249322525
goto meta_command_exit;
2249422526
}
@@ -22496,11 +22528,11 @@
2249622528
booleanValue(azArg[4]));
2249722529
if( rc ){
2249822530
raw_printf(stderr, "User-Edit failed: %d\n", rc);
2249922531
rc = 1;
2250022532
}
22501
- }else if( strcmp(azArg[1],"delete")==0 ){
22533
+ }else if( cli_strcmp(azArg[1],"delete")==0 ){
2250222534
if( nArg!=3 ){
2250322535
raw_printf(stderr, "Usage: .user delete USER\n");
2250422536
rc = 1;
2250522537
goto meta_command_exit;
2250622538
}
@@ -22515,11 +22547,11 @@
2251522547
goto meta_command_exit;
2251622548
}
2251722549
}else
2251822550
#endif /* SQLITE_USER_AUTHENTICATION */
2251922551
22520
- if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
22552
+ if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
2252122553
utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
2252222554
sqlite3_libversion(), sqlite3_sourceid());
2252322555
#if SQLITE_HAVE_ZLIB
2252422556
utf8_printf(p->out, "zlib version %s\n", zlibVersion());
2252522557
#endif
@@ -22534,11 +22566,11 @@
2253422566
#elif defined(__GNUC__) && defined(__VERSION__)
2253522567
utf8_printf(p->out, "gcc-" __VERSION__ "\n");
2253622568
#endif
2253722569
}else
2253822570
22539
- if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
22571
+ if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
2254022572
const char *zDbName = nArg==2 ? azArg[1] : "main";
2254122573
sqlite3_vfs *pVfs = 0;
2254222574
if( p->db ){
2254322575
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
2254422576
if( pVfs ){
@@ -22548,11 +22580,11 @@
2254822580
raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
2254922581
}
2255022582
}
2255122583
}else
2255222584
22553
- if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
22585
+ if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
2255422586
sqlite3_vfs *pVfs;
2255522587
sqlite3_vfs *pCurrent = 0;
2255622588
if( p->db ){
2255722589
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
2255822590
}
@@ -22566,11 +22598,11 @@
2256622598
raw_printf(p->out, "-----------------------------------\n");
2256722599
}
2256822600
}
2256922601
}else
2257022602
22571
- if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
22603
+ if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
2257222604
const char *zDbName = nArg==2 ? azArg[1] : "main";
2257322605
char *zVfsName = 0;
2257422606
if( p->db ){
2257522607
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
2257622608
if( zVfsName ){
@@ -22578,16 +22610,16 @@
2257822610
sqlite3_free(zVfsName);
2257922611
}
2258022612
}
2258122613
}else
2258222614
22583
- if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
22615
+ if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
2258422616
unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
2258522617
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
2258622618
}else
2258722619
22588
- if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
22620
+ if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
2258922621
int j;
2259022622
assert( nArg<=ArraySize(azArg) );
2259122623
p->nWidth = nArg-1;
2259222624
p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
2259322625
if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
@@ -22761,14 +22793,14 @@
2276122793
const char *zErrorTail;
2276222794
const char *zErrorType;
2276322795
if( zErrMsg==0 ){
2276422796
zErrorType = "Error";
2276522797
zErrorTail = sqlite3_errmsg(p->db);
22766
- }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
22798
+ }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
2276722799
zErrorType = "Parse error";
2276822800
zErrorTail = &zErrMsg[12];
22769
- }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
22801
+ }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
2277022802
zErrorType = "Runtime error";
2277122803
zErrorTail = &zErrMsg[10];
2277222804
}else{
2277322805
zErrorType = "Error";
2277422806
zErrorTail = zErrMsg;
@@ -22806,11 +22838,11 @@
2280622838
static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
2280722839
/* Parse the next line from shellState.wasm.zInput. */
2280822840
const char *zBegin = shellState.wasm.zPos;
2280922841
const char *z = zBegin;
2281022842
char *zLine = 0;
22811
- int nZ = 0;
22843
+ i64 nZ = 0;
2281222844
2281322845
UNUSED_PARAMETER(in);
2281422846
UNUSED_PARAMETER(isContinuation);
2281522847
if(!z || !*z){
2281622848
return 0;
@@ -22822,11 +22854,11 @@
2282222854
--nZ;
2282322855
}
2282422856
shellState.wasm.zPos = z;
2282522857
zLine = realloc(zPrior, nZ+1);
2282622858
shell_check_oom(zLine);
22827
- memcpy(zLine, zBegin, (size_t)nZ);
22859
+ memcpy(zLine, zBegin, nZ);
2282822860
zLine[nZ] = 0;
2282922861
return zLine;
2283022862
}
2283122863
#endif /* SQLITE_SHELL_FIDDLE */
2283222864
@@ -22840,16 +22872,16 @@
2284022872
** Return the number of errors.
2284122873
*/
2284222874
static int process_input(ShellState *p){
2284322875
char *zLine = 0; /* A single input line */
2284422876
char *zSql = 0; /* Accumulated SQL text */
22845
- int nLine; /* Length of current line */
22846
- int nSql = 0; /* Bytes of zSql[] used */
22847
- int nAlloc = 0; /* Allocated zSql[] space */
22877
+ i64 nLine; /* Length of current line */
22878
+ i64 nSql = 0; /* Bytes of zSql[] used */
22879
+ i64 nAlloc = 0; /* Allocated zSql[] space */
2284822880
int rc; /* Error code */
2284922881
int errCnt = 0; /* Number of errors seen */
22850
- int startline = 0; /* Line number for start of current input */
22882
+ i64 startline = 0; /* Line number for start of current input */
2285122883
QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
2285222884
2285322885
if( p->inputNesting==MAX_INPUT_NESTING ){
2285422886
/* This will be more informative in a later version. */
2285522887
utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
@@ -22895,19 +22927,19 @@
2289522927
}
2289622928
qss = QSS_Start;
2289722929
continue;
2289822930
}
2289922931
/* No single-line dispositions remain; accumulate line(s). */
22900
- nLine = strlen30(zLine);
22932
+ nLine = strlen(zLine);
2290122933
if( nSql+nLine+2>=nAlloc ){
2290222934
/* Grow buffer by half-again increments when big. */
2290322935
nAlloc = nSql+(nSql>>1)+nLine+100;
2290422936
zSql = realloc(zSql, nAlloc);
2290522937
shell_check_oom(zSql);
2290622938
}
2290722939
if( nSql==0 ){
22908
- int i;
22940
+ i64 i;
2290922941
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2291022942
assert( nAlloc>0 && zSql!=0 );
2291122943
memcpy(zSql, zLine+i, nLine+1-i);
2291222944
startline = p->lineno;
2291322945
nSql = nLine-i;
@@ -23003,11 +23035,11 @@
2300323035
#endif
2300423036
2300523037
#endif /* !_WIN32_WCE */
2300623038
2300723039
if( home_dir ){
23008
- int n = strlen30(home_dir) + 1;
23040
+ i64 n = strlen(home_dir) + 1;
2300923041
char *z = malloc( n );
2301023042
if( z ) memcpy(z, home_dir, n);
2301123043
home_dir = z;
2301223044
}
2301323045
@@ -23246,10 +23278,11 @@
2324623278
setBinaryMode(stdin, 0);
2324723279
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
2324823280
#ifdef SQLITE_SHELL_FIDDLE
2324923281
stdin_is_interactive = 0;
2325023282
stdout_is_console = 1;
23283
+ data.wasm.zDefaultDbName = "/fiddle.sqlite3";
2325123284
#else
2325223285
stdin_is_interactive = isatty(0);
2325323286
stdout_is_console = isatty(1);
2325423287
#endif
2325523288
@@ -23273,11 +23306,11 @@
2327323306
}
2327423307
}
2327523308
#endif
2327623309
2327723310
#if USE_SYSTEM_SQLITE+0!=1
23278
- if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
23311
+ if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
2327923312
utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
2328023313
sqlite3_sourceid(), SQLITE_SOURCE_ID);
2328123314
exit(1);
2328223315
}
2328323316
#endif
@@ -23295,13 +23328,13 @@
2329523328
shell_check_oom(argvToFree);
2329623329
argcToFree = argc;
2329723330
argv = argvToFree + argc;
2329823331
for(i=0; i<argc; i++){
2329923332
char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
23300
- int n;
23333
+ i64 n;
2330123334
shell_check_oom(z);
23302
- n = (int)strlen(z);
23335
+ n = strlen(z);
2330323336
argv[i] = malloc( n+1 );
2330423337
shell_check_oom(argv[i]);
2330523338
memcpy(argv[i], z, n+1);
2330623339
argvToFree[i] = argv[i];
2330723340
sqlite3_free(z);
@@ -23354,25 +23387,25 @@
2335423387
shell_check_oom(azCmd);
2335523388
azCmd[nCmd-1] = z;
2335623389
}
2335723390
}
2335823391
if( z[1]=='-' ) z++;
23359
- if( strcmp(z,"-separator")==0
23360
- || strcmp(z,"-nullvalue")==0
23361
- || strcmp(z,"-newline")==0
23362
- || strcmp(z,"-cmd")==0
23392
+ if( cli_strcmp(z,"-separator")==0
23393
+ || cli_strcmp(z,"-nullvalue")==0
23394
+ || cli_strcmp(z,"-newline")==0
23395
+ || cli_strcmp(z,"-cmd")==0
2336323396
){
2336423397
(void)cmdline_option_value(argc, argv, ++i);
23365
- }else if( strcmp(z,"-init")==0 ){
23398
+ }else if( cli_strcmp(z,"-init")==0 ){
2336623399
zInitFile = cmdline_option_value(argc, argv, ++i);
23367
- }else if( strcmp(z,"-batch")==0 ){
23400
+ }else if( cli_strcmp(z,"-batch")==0 ){
2336823401
/* Need to check for batch mode here to so we can avoid printing
2336923402
** informational messages (like from process_sqliterc) before
2337023403
** we do the actual processing of arguments later in a second pass.
2337123404
*/
2337223405
stdin_is_interactive = 0;
23373
- }else if( strcmp(z,"-heap")==0 ){
23406
+ }else if( cli_strcmp(z,"-heap")==0 ){
2337423407
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2337523408
const char *zSize;
2337623409
sqlite3_int64 szHeap;
2337723410
2337823411
zSize = cmdline_option_value(argc, argv, ++i);
@@ -23380,11 +23413,11 @@
2338023413
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2338123414
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2338223415
#else
2338323416
(void)cmdline_option_value(argc, argv, ++i);
2338423417
#endif
23385
- }else if( strcmp(z,"-pagecache")==0 ){
23418
+ }else if( cli_strcmp(z,"-pagecache")==0 ){
2338623419
sqlite3_int64 n, sz;
2338723420
sz = integerValue(cmdline_option_value(argc,argv,++i));
2338823421
if( sz>70000 ) sz = 70000;
2338923422
if( sz<0 ) sz = 0;
2339023423
n = integerValue(cmdline_option_value(argc,argv,++i));
@@ -23392,28 +23425,28 @@
2339223425
n = 0xffffffffffffLL/sz;
2339323426
}
2339423427
sqlite3_config(SQLITE_CONFIG_PAGECACHE,
2339523428
(n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
2339623429
data.shellFlgs |= SHFLG_Pagecache;
23397
- }else if( strcmp(z,"-lookaside")==0 ){
23430
+ }else if( cli_strcmp(z,"-lookaside")==0 ){
2339823431
int n, sz;
2339923432
sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
2340023433
if( sz<0 ) sz = 0;
2340123434
n = (int)integerValue(cmdline_option_value(argc,argv,++i));
2340223435
if( n<0 ) n = 0;
2340323436
sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
2340423437
if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
23405
- }else if( strcmp(z,"-threadsafe")==0 ){
23438
+ }else if( cli_strcmp(z,"-threadsafe")==0 ){
2340623439
int n;
2340723440
n = (int)integerValue(cmdline_option_value(argc,argv,++i));
2340823441
switch( n ){
2340923442
case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
2341023443
case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
2341123444
default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
2341223445
}
2341323446
#ifdef SQLITE_ENABLE_VFSTRACE
23414
- }else if( strcmp(z,"-vfstrace")==0 ){
23447
+ }else if( cli_strcmp(z,"-vfstrace")==0 ){
2341523448
extern int vfstrace_register(
2341623449
const char *zTraceName,
2341723450
const char *zOldVfsName,
2341823451
int (*xOut)(const char*,void*),
2341923452
void *pOutArg,
@@ -23420,54 +23453,54 @@
2342023453
int makeDefault
2342123454
);
2342223455
vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
2342323456
#endif
2342423457
#ifdef SQLITE_ENABLE_MULTIPLEX
23425
- }else if( strcmp(z,"-multiplex")==0 ){
23458
+ }else if( cli_strcmp(z,"-multiplex")==0 ){
2342623459
extern int sqlite3_multiple_initialize(const char*,int);
2342723460
sqlite3_multiplex_initialize(0, 1);
2342823461
#endif
23429
- }else if( strcmp(z,"-mmap")==0 ){
23462
+ }else if( cli_strcmp(z,"-mmap")==0 ){
2343023463
sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
2343123464
sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
2343223465
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
23433
- }else if( strcmp(z,"-sorterref")==0 ){
23466
+ }else if( cli_strcmp(z,"-sorterref")==0 ){
2343423467
sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
2343523468
sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
2343623469
#endif
23437
- }else if( strcmp(z,"-vfs")==0 ){
23470
+ }else if( cli_strcmp(z,"-vfs")==0 ){
2343823471
zVfs = cmdline_option_value(argc, argv, ++i);
2343923472
#ifdef SQLITE_HAVE_ZLIB
23440
- }else if( strcmp(z,"-zip")==0 ){
23473
+ }else if( cli_strcmp(z,"-zip")==0 ){
2344123474
data.openMode = SHELL_OPEN_ZIPFILE;
2344223475
#endif
23443
- }else if( strcmp(z,"-append")==0 ){
23476
+ }else if( cli_strcmp(z,"-append")==0 ){
2344423477
data.openMode = SHELL_OPEN_APPENDVFS;
2344523478
#ifndef SQLITE_OMIT_DESERIALIZE
23446
- }else if( strcmp(z,"-deserialize")==0 ){
23479
+ }else if( cli_strcmp(z,"-deserialize")==0 ){
2344723480
data.openMode = SHELL_OPEN_DESERIALIZE;
23448
- }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23481
+ }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
2344923482
data.szMax = integerValue(argv[++i]);
2345023483
#endif
23451
- }else if( strcmp(z,"-readonly")==0 ){
23484
+ }else if( cli_strcmp(z,"-readonly")==0 ){
2345223485
data.openMode = SHELL_OPEN_READONLY;
23453
- }else if( strcmp(z,"-nofollow")==0 ){
23486
+ }else if( cli_strcmp(z,"-nofollow")==0 ){
2345423487
data.openFlags = SQLITE_OPEN_NOFOLLOW;
2345523488
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23456
- }else if( strncmp(z, "-A",2)==0 ){
23489
+ }else if( cli_strncmp(z, "-A",2)==0 ){
2345723490
/* All remaining command-line arguments are passed to the ".archive"
2345823491
** command, so ignore them */
2345923492
break;
2346023493
#endif
23461
- }else if( strcmp(z, "-memtrace")==0 ){
23494
+ }else if( cli_strcmp(z, "-memtrace")==0 ){
2346223495
sqlite3MemTraceActivate(stderr);
23463
- }else if( strcmp(z,"-bail")==0 ){
23496
+ }else if( cli_strcmp(z,"-bail")==0 ){
2346423497
bail_on_error = 1;
23465
- }else if( strcmp(z,"-nonce")==0 ){
23498
+ }else if( cli_strcmp(z,"-nonce")==0 ){
2346623499
free(data.zNonce);
2346723500
data.zNonce = strdup(argv[++i]);
23468
- }else if( strcmp(z,"-safe")==0 ){
23501
+ }else if( cli_strcmp(z,"-safe")==0 ){
2346923502
/* no-op - catch this on the second pass */
2347023503
}
2347123504
}
2347223505
verify_uninitialized();
2347323506
@@ -23533,131 +23566,131 @@
2353323566
*/
2353423567
for(i=1; i<argc; i++){
2353523568
char *z = argv[i];
2353623569
if( z[0]!='-' ) continue;
2353723570
if( z[1]=='-' ){ z++; }
23538
- if( strcmp(z,"-init")==0 ){
23571
+ if( cli_strcmp(z,"-init")==0 ){
2353923572
i++;
23540
- }else if( strcmp(z,"-html")==0 ){
23573
+ }else if( cli_strcmp(z,"-html")==0 ){
2354123574
data.mode = MODE_Html;
23542
- }else if( strcmp(z,"-list")==0 ){
23575
+ }else if( cli_strcmp(z,"-list")==0 ){
2354323576
data.mode = MODE_List;
23544
- }else if( strcmp(z,"-quote")==0 ){
23577
+ }else if( cli_strcmp(z,"-quote")==0 ){
2354523578
data.mode = MODE_Quote;
2354623579
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
2354723580
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23548
- }else if( strcmp(z,"-line")==0 ){
23581
+ }else if( cli_strcmp(z,"-line")==0 ){
2354923582
data.mode = MODE_Line;
23550
- }else if( strcmp(z,"-column")==0 ){
23583
+ }else if( cli_strcmp(z,"-column")==0 ){
2355123584
data.mode = MODE_Column;
23552
- }else if( strcmp(z,"-json")==0 ){
23585
+ }else if( cli_strcmp(z,"-json")==0 ){
2355323586
data.mode = MODE_Json;
23554
- }else if( strcmp(z,"-markdown")==0 ){
23587
+ }else if( cli_strcmp(z,"-markdown")==0 ){
2355523588
data.mode = MODE_Markdown;
23556
- }else if( strcmp(z,"-table")==0 ){
23589
+ }else if( cli_strcmp(z,"-table")==0 ){
2355723590
data.mode = MODE_Table;
23558
- }else if( strcmp(z,"-box")==0 ){
23591
+ }else if( cli_strcmp(z,"-box")==0 ){
2355923592
data.mode = MODE_Box;
23560
- }else if( strcmp(z,"-csv")==0 ){
23593
+ }else if( cli_strcmp(z,"-csv")==0 ){
2356123594
data.mode = MODE_Csv;
2356223595
memcpy(data.colSeparator,",",2);
2356323596
#ifdef SQLITE_HAVE_ZLIB
23564
- }else if( strcmp(z,"-zip")==0 ){
23597
+ }else if( cli_strcmp(z,"-zip")==0 ){
2356523598
data.openMode = SHELL_OPEN_ZIPFILE;
2356623599
#endif
23567
- }else if( strcmp(z,"-append")==0 ){
23600
+ }else if( cli_strcmp(z,"-append")==0 ){
2356823601
data.openMode = SHELL_OPEN_APPENDVFS;
2356923602
#ifndef SQLITE_OMIT_DESERIALIZE
23570
- }else if( strcmp(z,"-deserialize")==0 ){
23603
+ }else if( cli_strcmp(z,"-deserialize")==0 ){
2357123604
data.openMode = SHELL_OPEN_DESERIALIZE;
23572
- }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23605
+ }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
2357323606
data.szMax = integerValue(argv[++i]);
2357423607
#endif
23575
- }else if( strcmp(z,"-readonly")==0 ){
23608
+ }else if( cli_strcmp(z,"-readonly")==0 ){
2357623609
data.openMode = SHELL_OPEN_READONLY;
23577
- }else if( strcmp(z,"-nofollow")==0 ){
23610
+ }else if( cli_strcmp(z,"-nofollow")==0 ){
2357823611
data.openFlags |= SQLITE_OPEN_NOFOLLOW;
23579
- }else if( strcmp(z,"-ascii")==0 ){
23612
+ }else if( cli_strcmp(z,"-ascii")==0 ){
2358023613
data.mode = MODE_Ascii;
2358123614
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
2358223615
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
23583
- }else if( strcmp(z,"-tabs")==0 ){
23616
+ }else if( cli_strcmp(z,"-tabs")==0 ){
2358423617
data.mode = MODE_List;
2358523618
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
2358623619
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23587
- }else if( strcmp(z,"-separator")==0 ){
23620
+ }else if( cli_strcmp(z,"-separator")==0 ){
2358823621
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
2358923622
"%s",cmdline_option_value(argc,argv,++i));
23590
- }else if( strcmp(z,"-newline")==0 ){
23623
+ }else if( cli_strcmp(z,"-newline")==0 ){
2359123624
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
2359223625
"%s",cmdline_option_value(argc,argv,++i));
23593
- }else if( strcmp(z,"-nullvalue")==0 ){
23626
+ }else if( cli_strcmp(z,"-nullvalue")==0 ){
2359423627
sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
2359523628
"%s",cmdline_option_value(argc,argv,++i));
23596
- }else if( strcmp(z,"-header")==0 ){
23629
+ }else if( cli_strcmp(z,"-header")==0 ){
2359723630
data.showHeader = 1;
2359823631
ShellSetFlag(&data, SHFLG_HeaderSet);
23599
- }else if( strcmp(z,"-noheader")==0 ){
23632
+ }else if( cli_strcmp(z,"-noheader")==0 ){
2360023633
data.showHeader = 0;
2360123634
ShellSetFlag(&data, SHFLG_HeaderSet);
23602
- }else if( strcmp(z,"-echo")==0 ){
23635
+ }else if( cli_strcmp(z,"-echo")==0 ){
2360323636
ShellSetFlag(&data, SHFLG_Echo);
23604
- }else if( strcmp(z,"-eqp")==0 ){
23637
+ }else if( cli_strcmp(z,"-eqp")==0 ){
2360523638
data.autoEQP = AUTOEQP_on;
23606
- }else if( strcmp(z,"-eqpfull")==0 ){
23639
+ }else if( cli_strcmp(z,"-eqpfull")==0 ){
2360723640
data.autoEQP = AUTOEQP_full;
23608
- }else if( strcmp(z,"-stats")==0 ){
23641
+ }else if( cli_strcmp(z,"-stats")==0 ){
2360923642
data.statsOn = 1;
23610
- }else if( strcmp(z,"-scanstats")==0 ){
23643
+ }else if( cli_strcmp(z,"-scanstats")==0 ){
2361123644
data.scanstatsOn = 1;
23612
- }else if( strcmp(z,"-backslash")==0 ){
23645
+ }else if( cli_strcmp(z,"-backslash")==0 ){
2361323646
/* Undocumented command-line option: -backslash
2361423647
** Causes C-style backslash escapes to be evaluated in SQL statements
2361523648
** prior to sending the SQL into SQLite. Useful for injecting
2361623649
** crazy bytes in the middle of SQL statements for testing and debugging.
2361723650
*/
2361823651
ShellSetFlag(&data, SHFLG_Backslash);
23619
- }else if( strcmp(z,"-bail")==0 ){
23652
+ }else if( cli_strcmp(z,"-bail")==0 ){
2362023653
/* No-op. The bail_on_error flag should already be set. */
23621
- }else if( strcmp(z,"-version")==0 ){
23654
+ }else if( cli_strcmp(z,"-version")==0 ){
2362223655
printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
2362323656
return 0;
23624
- }else if( strcmp(z,"-interactive")==0 ){
23625
- stdin_is_interactive = 1;
23626
- }else if( strcmp(z,"-batch")==0 ){
23627
- stdin_is_interactive = 0;
23628
- }else if( strcmp(z,"-heap")==0 ){
23629
- i++;
23630
- }else if( strcmp(z,"-pagecache")==0 ){
23631
- i+=2;
23632
- }else if( strcmp(z,"-lookaside")==0 ){
23633
- i+=2;
23634
- }else if( strcmp(z,"-threadsafe")==0 ){
23635
- i+=2;
23636
- }else if( strcmp(z,"-nonce")==0 ){
23637
- i += 2;
23638
- }else if( strcmp(z,"-mmap")==0 ){
23639
- i++;
23640
- }else if( strcmp(z,"-memtrace")==0 ){
23657
+ }else if( cli_strcmp(z,"-interactive")==0 ){
23658
+ stdin_is_interactive = 1;
23659
+ }else if( cli_strcmp(z,"-batch")==0 ){
23660
+ stdin_is_interactive = 0;
23661
+ }else if( cli_strcmp(z,"-heap")==0 ){
23662
+ i++;
23663
+ }else if( cli_strcmp(z,"-pagecache")==0 ){
23664
+ i+=2;
23665
+ }else if( cli_strcmp(z,"-lookaside")==0 ){
23666
+ i+=2;
23667
+ }else if( cli_strcmp(z,"-threadsafe")==0 ){
23668
+ i+=2;
23669
+ }else if( cli_strcmp(z,"-nonce")==0 ){
23670
+ i += 2;
23671
+ }else if( cli_strcmp(z,"-mmap")==0 ){
23672
+ i++;
23673
+ }else if( cli_strcmp(z,"-memtrace")==0 ){
2364123674
i++;
2364223675
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
23643
- }else if( strcmp(z,"-sorterref")==0 ){
23676
+ }else if( cli_strcmp(z,"-sorterref")==0 ){
2364423677
i++;
2364523678
#endif
23646
- }else if( strcmp(z,"-vfs")==0 ){
23679
+ }else if( cli_strcmp(z,"-vfs")==0 ){
2364723680
i++;
2364823681
#ifdef SQLITE_ENABLE_VFSTRACE
23649
- }else if( strcmp(z,"-vfstrace")==0 ){
23682
+ }else if( cli_strcmp(z,"-vfstrace")==0 ){
2365023683
i++;
2365123684
#endif
2365223685
#ifdef SQLITE_ENABLE_MULTIPLEX
23653
- }else if( strcmp(z,"-multiplex")==0 ){
23686
+ }else if( cli_strcmp(z,"-multiplex")==0 ){
2365423687
i++;
2365523688
#endif
23656
- }else if( strcmp(z,"-help")==0 ){
23689
+ }else if( cli_strcmp(z,"-help")==0 ){
2365723690
usage(1);
23658
- }else if( strcmp(z,"-cmd")==0 ){
23691
+ }else if( cli_strcmp(z,"-cmd")==0 ){
2365923692
/* Run commands that follow -cmd first and separately from commands
2366023693
** that simply appear on the command-line. This seems goofy. It would
2366123694
** be better if all commands ran in the order that they appear. But
2366223695
** we retain the goofy behavior for historical compatibility. */
2366323696
if( i==argc-1 ) break;
@@ -23675,11 +23708,11 @@
2367523708
utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
2367623709
if( bail_on_error ) return rc;
2367723710
}
2367823711
}
2367923712
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23680
- }else if( strncmp(z, "-A", 2)==0 ){
23713
+ }else if( cli_strncmp(z, "-A", 2)==0 ){
2368123714
if( nCmd>0 ){
2368223715
utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
2368323716
" with \"%s\"\n", z);
2368423717
return 1;
2368523718
}
@@ -23691,11 +23724,11 @@
2369123724
arDotCommand(&data, 1, argv+i, argc-i);
2369223725
}
2369323726
readStdin = 0;
2369423727
break;
2369523728
#endif
23696
- }else if( strcmp(z,"-safe")==0 ){
23729
+ }else if( cli_strcmp(z,"-safe")==0 ){
2369723730
data.bSafeMode = data.bSafeModePersist = 1;
2369823731
}else{
2369923732
utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2370023733
raw_printf(stderr,"Use -help for a list of options.\n");
2370123734
return 1;
@@ -23816,34 +23849,34 @@
2381623849
2381723850
2381823851
#ifdef SQLITE_SHELL_FIDDLE
2381923852
/* Only for emcc experimentation purposes. */
2382023853
int fiddle_experiment(int a,int b){
23821
- return a + b;
23822
-}
23823
-
23824
-/* Only for emcc experimentation purposes.
23825
-
23826
- Define this function in JS using:
23827
-
23828
- emcc ... --js-library somefile.js
23829
-
23830
- containing:
23831
-
23832
-mergeInto(LibraryManager.library, {
23833
- my_foo: function(){
23834
- console.debug("my_foo()",arguments);
23835
- }
23836
-});
23854
+ return a + b;
23855
+}
23856
+
23857
+/*
23858
+** Returns a pointer to the current DB handle.
23859
+*/
23860
+sqlite3 * fiddle_db_handle(){
23861
+ return globalDb;
23862
+}
23863
+
23864
+/*
23865
+** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
23866
+** "main" is assumed. Returns 0 if no db with the given name is
23867
+** open.
2383723868
*/
23838
-/*extern void my_foo(sqlite3 *);*/
23839
-/* Only for emcc experimentation purposes. */
23840
-sqlite3 * fiddle_the_db(){
23841
- printf("fiddle_the_db(%p)\n", (const void*)globalDb);
23842
- /*my_foo(globalDb);*/
23843
- return globalDb;
23869
+sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
23870
+ sqlite3_vfs * pVfs = 0;
23871
+ if(globalDb){
23872
+ sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
23873
+ SQLITE_FCNTL_VFS_POINTER, &pVfs);
23874
+ }
23875
+ return pVfs;
2384423876
}
23877
+
2384523878
/* Only for emcc experimentation purposes. */
2384623879
sqlite3 * fiddle_db_arg(sqlite3 *arg){
2384723880
printf("fiddle_db_arg(%p)\n", (const void*)arg);
2384823881
return arg;
2384923882
}
@@ -23853,11 +23886,11 @@
2385323886
** SharedWorker() (which manages the wasm module) is performing work
2385423887
** which should be interrupted. Unfortunately, SharedWorker is not
2385523888
** portable enough to make real use of.
2385623889
*/
2385723890
void fiddle_interrupt(void){
23858
- if(globalDb) sqlite3_interrupt(globalDb);
23891
+ if( globalDb ) sqlite3_interrupt(globalDb);
2385923892
}
2386023893
2386123894
/*
2386223895
** Returns the filename of the given db name, assuming "main" if
2386323896
** zDbName is NULL. Returns NULL if globalDb is not opened.
@@ -23867,73 +23900,74 @@
2386723900
? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
2386823901
: NULL;
2386923902
}
2387023903
2387123904
/*
23872
-** Closes, unlinks, and reopens the db using its current filename (or
23873
-** the default if the db is currently closed). It is assumed, for
23874
-** purposes of the fiddle build, that the file is in a transient
23875
-** virtual filesystem within the browser.
23905
+** Completely wipes out the contents of the currently-opened database
23906
+** but leaves its storage intact for reuse.
2387623907
*/
2387723908
void fiddle_reset_db(void){
23878
- char *zFilename = 0;
23879
- if(0==globalDb){
23880
- shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
23881
- }else{
23882
- zFilename =
23883
- sqlite3_mprintf("%s", sqlite3_db_filename(globalDb, "main"));
23884
- shell_check_oom(zFilename);
23885
- close_db(globalDb);
23886
- shellDeleteFile(zFilename);
23887
- shellState.db = 0;
23888
- shellState.pAuxDb->zDbFilename = zFilename;
23889
- }
23890
- open_db(&shellState, 0);
23891
- sqlite3_free(zFilename);
23909
+ if( globalDb ){
23910
+ int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
23911
+ if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
23912
+ sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
23913
+ }
23914
+}
23915
+
23916
+/*
23917
+** Uses the current database's VFS xRead to stream the db file's
23918
+** contents out to the given callback. The callback gets a single
23919
+** chunk of size n (its 2nd argument) on each call and must return 0
23920
+** on success, non-0 on error. This function returns 0 on success,
23921
+** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
23922
+** code from the callback. Note that this is not thread-friendly: it
23923
+** expects that it will be the only thread reading the db file and
23924
+** takes no measures to ensure that is the case.
23925
+*/
23926
+int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
23927
+ sqlite3_int64 nSize = 0;
23928
+ sqlite3_int64 nPos = 0;
23929
+ sqlite3_file * pFile = 0;
23930
+ unsigned char buf[1024 * 8];
23931
+ int nBuf = (int)sizeof(buf);
23932
+ int rc = shellState.db
23933
+ ? sqlite3_file_control(shellState.db, "main",
23934
+ SQLITE_FCNTL_FILE_POINTER, &pFile)
23935
+ : SQLITE_NOTFOUND;
23936
+ if( rc ) return rc;
23937
+ rc = pFile->pMethods->xFileSize(pFile, &nSize);
23938
+ if( rc ) return rc;
23939
+ if(nSize % nBuf){
23940
+ /* DB size is not an even multiple of the buffer size. Reduce
23941
+ ** buffer size so that we do not unduly inflate the db size when
23942
+ ** exporting. */
23943
+ if(0 == nSize % 4096) nBuf = 4096;
23944
+ else if(0 == nSize % 2048) nBuf = 2048;
23945
+ else if(0 == nSize % 1024) nBuf = 1024;
23946
+ else nBuf = 512;
23947
+ }
23948
+ for( ; 0==rc && nPos<nSize; nPos += nBuf ){
23949
+ rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
23950
+ if(SQLITE_IOERR_SHORT_READ == rc){
23951
+ rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
23952
+ }
23953
+ if( 0==rc ) rc = xCallback(buf, nBuf);
23954
+ }
23955
+ return rc;
2389223956
}
2389323957
2389423958
/*
23895
-** Trivial exportable function for emscripten. Needs to be exported using:
23896
-**
23897
-** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
23898
-**
23899
-** (Note the underscore before the function name.) It processes zSql
23900
-** as if it were input to the sqlite3 shell and redirects all output
23901
-** to the wasm binding.
23959
+** Trivial exportable function for emscripten. It processes zSql as if
23960
+** it were input to the sqlite3 shell and redirects all output to the
23961
+** wasm binding. fiddle_main() must have been called before this
23962
+** is called, or results are undefined.
2390223963
*/
2390323964
void fiddle_exec(const char * zSql){
23904
- static int once = 0;
23905
- int rc = 0;
23906
- if(!once){
23907
- /* Simulate an argv array for main() */
23908
- static char * argv[] = {"fiddle",
23909
- "-bail",
23910
- "-safe"};
23911
- rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
23912
- once = rc ? -1 : 1;
23913
- memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23914
- printf(
23915
- "SQLite version %s %.19s\n" /*extra-version-info*/,
23916
- sqlite3_libversion(), sqlite3_sourceid()
23917
- );
23918
- puts("WASM shell");
23919
- puts("Enter \".help\" for usage hints.");
23920
- if(once>0){
23921
- fiddle_reset_db();
23922
- }
23923
- if(shellState.db){
23924
- printf("Connected to %s.\n", fiddle_db_filename(NULL));
23925
- }else{
23926
- fprintf(stderr,"ERROR initializing db!\n");
23927
- return;
23928
- }
23929
- }
23930
- if(once<0){
23931
- puts("DB init failed. Not executing SQL.");
23932
- }else if(zSql && *zSql){
23965
+ if(zSql && *zSql){
23966
+ if('.'==*zSql) puts(zSql);
2393323967
shellState.wasm.zInput = zSql;
2393423968
shellState.wasm.zPos = zSql;
2393523969
process_input(&shellState);
23936
- memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23970
+ shellState.wasm.zInput = shellState.wasm.zPos = 0;
2393723971
}
2393823972
}
2393923973
#endif /* SQLITE_SHELL_FIDDLE */
2394023974
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -100,10 +100,18 @@
100 # ifndef _FILE_OFFSET_BITS
101 # define _FILE_OFFSET_BITS 64
102 # endif
103 # define _LARGEFILE_SOURCE 1
104 #endif
 
 
 
 
 
 
 
 
105
106 #include <stdlib.h>
107 #include <string.h>
108 #include <stdio.h>
109 #include <assert.h>
@@ -256,10 +264,22 @@
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){
264 static sqlite3_vfs *clockVfs = 0;
265 sqlite3_int64 t;
@@ -688,11 +708,11 @@
688 /* For interactive input on Windows systems, translate the
689 ** multi-byte characterset characters into UTF-8. */
690 if( stdin_is_interactive && in==stdin ){
691 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
692 if( zTrans ){
693 int nTrans = strlen30(zTrans)+1;
694 if( nTrans>nLine ){
695 zLine = realloc(zLine, nTrans);
696 shell_check_oom(zLine);
697 }
698 memcpy(zLine, zTrans, nTrans);
@@ -824,13 +844,13 @@
824 **
825 ** If the third argument, quote, is not '\0', then it is used as a
826 ** quote character for zAppend.
827 */
828 static void appendText(ShellText *p, const char *zAppend, char quote){
829 int len;
830 int i;
831 int nAppend = strlen30(zAppend);
832
833 len = nAppend+p->n+1;
834 if( quote ){
835 len += 2;
836 for(i=0; i<nAppend; i++){
@@ -985,14 +1005,14 @@
985 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
986 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
987 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
988 sqlite3 *db = sqlite3_context_db_handle(pCtx);
989 UNUSED_PARAMETER(nVal);
990 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
991 for(i=0; i<ArraySize(aPrefix); i++){
992 int n = strlen30(aPrefix[i]);
993 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
994 char *z = 0;
995 char *zFake = 0;
996 if( zSchema ){
997 char cQuote = quoteChar(zSchema);
998 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
@@ -12361,10 +12381,11 @@
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
@@ -12873,13 +12894,13 @@
12873 }
12874
12875 /*
12876 ** Output the given string as a quoted according to JSON quoting rules.
12877 */
12878 static void output_json_string(FILE *out, const char *z, int n){
12879 unsigned int c;
12880 if( n<0 ) n = (int)strlen(z);
12881 fputc('"', out);
12882 while( n-- ){
12883 c = *(z++);
12884 if( c=='\\' || c=='"' ){
12885 fputc('\\', out);
@@ -13178,11 +13199,13 @@
13178 /*
13179 ** Add a new entry to the EXPLAIN QUERY PLAN data
13180 */
13181 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
13182 EQPGraphRow *pNew;
13183 int nText = strlen30(zText);
 
 
13184 if( p->autoEQPtest ){
13185 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13186 }
13187 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13188 shell_check_oom(pNew);
@@ -13223,18 +13246,18 @@
13223 /* Render a single level of the graph that has iEqpId as its parent. Called
13224 ** recursively to render sublevels.
13225 */
13226 static void eqp_render_level(ShellState *p, int iEqpId){
13227 EQPGraphRow *pRow, *pNext;
13228 int n = strlen30(p->sGraph.zPrefix);
13229 char *z;
13230 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
13231 pNext = eqp_next_row(p, iEqpId, pRow);
13232 z = pRow->zText;
13233 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
13234 pNext ? "|--" : "`--", z);
13235 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
13236 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
13237 eqp_render_level(p, pRow->iEqpId);
13238 p->sGraph.zPrefix[n] = 0;
13239 }
13240 }
@@ -13946,11 +13969,11 @@
13946 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
13947 };
13948 int i;
13949 for(i=0; i<ArraySize(aTrans); i++){
13950 int n = strlen30(aTrans[i].zPattern);
13951 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13952 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13953 break;
13954 }
13955 }
13956 }
@@ -14185,11 +14208,11 @@
14185 ** Otherwise, return zero.
14186 */
14187 static int str_in_array(const char *zStr, const char **azArray){
14188 int i;
14189 for(i=0; azArray[i]; i++){
14190 if( 0==strcmp(zStr, azArray[i]) ) return 1;
14191 }
14192 return 0;
14193 }
14194
14195 /*
@@ -14260,11 +14283,11 @@
14260 ** it is not */
14261 static const char *explainCols[] = {
14262 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
14263 int jj;
14264 for(jj=0; jj<ArraySize(explainCols); jj++){
14265 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14266 p->cMode = p->mode;
14267 sqlite3_reset(pSql);
14268 return;
14269 }
14270 }
@@ -14984,14 +15007,14 @@
14984 for(i=1; rc==SQLITE_OK && i<nArg; i++){
14985 char *z = azArg[i];
14986 int n;
14987 if( z[0]=='-' && z[1]=='-' ) z++;
14988 n = strlen30(z);
14989 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14990 pState->expert.bVerbose = 1;
14991 }
14992 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14993 if( i==(nArg-1) ){
14994 raw_printf(stderr, "option requires an argument: %s\n", z);
14995 rc = SQLITE_ERROR;
14996 }else{
14997 iSample = (int)integerValue(azArg[++i]);
@@ -15335,22 +15358,24 @@
15335 UNUSED_PARAMETER(azNotUsed);
15336 if( nArg!=3 || azArg==0 ) return 0;
15337 zTable = azArg[0];
15338 zType = azArg[1];
15339 zSql = azArg[2];
 
 
15340 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
15341 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
15342
15343 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
15344 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
15345 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
15346 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
15347 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
15348 return 0;
15349 }else if( dataOnly ){
15350 /* no-op */
15351 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
15352 char *zIns;
15353 if( !p->writableSchema ){
15354 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
15355 p->writableSchema = 1;
15356 }
@@ -15364,11 +15389,11 @@
15364 return 0;
15365 }else{
15366 printSchemaLine(p->out, zSql, ";\n");
15367 }
15368
15369 if( strcmp(zType, "table")==0 ){
15370 ShellText sSelect;
15371 ShellText sTable;
15372 char **azCol;
15373 int i;
15374 char *savedDestTable;
@@ -15608,11 +15633,11 @@
15608 " insert SQL insert statements for TABLE",
15609 " json Results in a JSON array",
15610 " line One value per line",
15611 " list Values delimited by \"|\"",
15612 " markdown Markdown table format",
15613 " qbox Shorthand for \"box --width 60 --quote\"",
15614 " quote Escape answers as for SQL",
15615 " table ASCII-art table",
15616 " tabs Tab-separated values",
15617 " tcl TCL list elements",
15618 " OPTIONS: (for columnar modes or insert mode):",
@@ -15783,13 +15808,13 @@
15783 int j = 0;
15784 int n = 0;
15785 char *zPat;
15786 if( zPattern==0
15787 || zPattern[0]=='0'
15788 || strcmp(zPattern,"-a")==0
15789 || strcmp(zPattern,"-all")==0
15790 || strcmp(zPattern,"--all")==0
15791 ){
15792 /* Show all commands, but only one line per command */
15793 if( zPattern==0 ) zPattern = "";
15794 for(i=0; i<ArraySize(azHelp); i++){
15795 if( azHelp[i][0]=='.' || zPattern[0] ){
@@ -16022,11 +16047,11 @@
16022 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
16023 if( rc==2 ){
16024 iOffset = k;
16025 continue;
16026 }
16027 if( strncmp(zLine, "| end ", 6)==0 ){
16028 break;
16029 }
16030 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
16031 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
16032 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
@@ -16050,11 +16075,11 @@
16050 if( in!=p->in ){
16051 fclose(in);
16052 }else{
16053 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
16054 nLine++;
16055 if(strncmp(zLine, "| end ", 6)==0 ) break;
16056 }
16057 p->lineno = nLine;
16058 }
16059 sqlite3_free(a);
16060 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
@@ -16142,32 +16167,32 @@
16142 sqlite3_value **argv
16143 ){
16144 const char *zText = (const char*)sqlite3_value_text(argv[0]);
16145 UNUSED_PARAMETER(argc);
16146 if( zText && zText[0]=='\'' ){
16147 int nText = sqlite3_value_bytes(argv[0]);
16148 int i;
16149 char zBuf1[20];
16150 char zBuf2[20];
16151 const char *zNL = 0;
16152 const char *zCR = 0;
16153 int nCR = 0;
16154 int nNL = 0;
16155
16156 for(i=0; zText[i]; i++){
16157 if( zNL==0 && zText[i]=='\n' ){
16158 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
16159 nNL = (int)strlen(zNL);
16160 }
16161 if( zCR==0 && zText[i]=='\r' ){
16162 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
16163 nCR = (int)strlen(zCR);
16164 }
16165 }
16166
16167 if( zNL || zCR ){
16168 int iOut = 0;
16169 i64 nMax = (nNL > nCR) ? nNL : nCR;
16170 i64 nAlloc = nMax * nText + (nMax+64)*2;
16171 char *zOut = (char*)sqlite3_malloc64(nAlloc);
16172 if( zOut==0 ){
16173 sqlite3_result_error_nomem(context);
@@ -16404,12 +16429,12 @@
16404 #elif HAVE_LINENOISE
16405 /*
16406 ** Linenoise completion callback
16407 */
16408 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
16409 int nLine = strlen30(zLine);
16410 int i, iStart;
16411 sqlite3_stmt *pStmt = 0;
16412 char *zSql;
16413 char zBuf[1000];
16414
16415 if( nLine>sizeof(zBuf)-30 ) return;
@@ -16543,15 +16568,15 @@
16543 ** recognized and do the right thing. NULL is returned if the output
16544 ** filename is "off".
16545 */
16546 static FILE *output_file_open(const char *zFile, int bTextMode){
16547 FILE *f;
16548 if( strcmp(zFile,"stdout")==0 ){
16549 f = stdout;
16550 }else if( strcmp(zFile, "stderr")==0 ){
16551 f = stderr;
16552 }else if( strcmp(zFile, "off")==0 ){
16553 f = 0;
16554 }else{
16555 f = fopen(zFile, bTextMode ? "w" : "wb");
16556 if( f==0 ){
16557 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
@@ -16571,11 +16596,11 @@
16571 void *pX /* Auxiliary output */
16572 ){
16573 ShellState *p = (ShellState*)pArg;
16574 sqlite3_stmt *pStmt;
16575 const char *zSql;
16576 int nSql;
16577 if( p->traceOut==0 ) return 0;
16578 if( mType==SQLITE_TRACE_CLOSE ){
16579 utf8_printf(p->traceOut, "-- closing database connection\n");
16580 return 0;
16581 }
@@ -16599,21 +16624,22 @@
16599 break;
16600 }
16601 }
16602 }
16603 if( zSql==0 ) return 0;
16604 nSql = strlen30(zSql);
 
16605 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
16606 switch( mType ){
16607 case SQLITE_TRACE_ROW:
16608 case SQLITE_TRACE_STMT: {
16609 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
16610 break;
16611 }
16612 case SQLITE_TRACE_PROFILE: {
16613 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16614 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
16615 break;
16616 }
16617 }
16618 return 0;
16619 }
@@ -17158,11 +17184,11 @@
17158 }
17159 raw_printf(p->out, "\n");
17160 }
17161 if( zDb==0 ){
17162 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
17163 }else if( strcmp(zDb,"temp")==0 ){
17164 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
17165 }else{
17166 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
17167 }
17168 for(i=0; i<ArraySize(aQuery); i++){
@@ -17292,11 +17318,11 @@
17292 */
17293 static int optionMatch(const char *zStr, const char *zOpt){
17294 if( zStr[0]!='-' ) return 0;
17295 zStr++;
17296 if( zStr[0]=='-' ) zStr++;
17297 return strcmp(zStr, zOpt)==0;
17298 }
17299
17300 /*
17301 ** Delete a file.
17302 */
@@ -19398,11 +19424,11 @@
19398 n = strlen30(azArg[0]);
19399 c = azArg[0][0];
19400 clearTempFile(p);
19401
19402 #ifndef SQLITE_OMIT_AUTHORIZATION
19403 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
19404 if( nArg!=2 ){
19405 raw_printf(stderr, "Usage: .auth ON|OFF\n");
19406 rc = 1;
19407 goto meta_command_exit;
19408 }
@@ -19417,20 +19443,20 @@
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;
19435 sqlite3 *pDest;
19436 sqlite3_backup *pBackup;
@@ -19440,14 +19466,14 @@
19440 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
19441 for(j=1; j<nArg; j++){
19442 const char *z = azArg[j];
19443 if( z[0]=='-' ){
19444 if( z[1]=='-' ) z++;
19445 if( strcmp(z, "-append")==0 ){
19446 zVfs = "apndvfs";
19447 }else
19448 if( strcmp(z, "-async")==0 ){
19449 bAsync = 1;
19450 }else
19451 {
19452 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
19453 return 1;
@@ -19495,20 +19521,20 @@
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{
19504 raw_printf(stderr, "Usage: .bail on|off\n");
19505 rc = 1;
19506 }
19507 }else
19508
19509 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
19510 if( nArg==2 ){
19511 if( booleanValue(azArg[1]) ){
19512 setBinaryMode(p->out, 1);
19513 }else{
19514 setTextMode(p->out, 1);
@@ -19520,16 +19546,16 @@
19520 }else
19521
19522 /* The undocumented ".breakpoint" command causes a call to the no-op
19523 ** routine named test_breakpoint().
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]);
19535 rc = !SetCurrentDirectoryW(z);
@@ -19546,11 +19572,11 @@
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{
19555 raw_printf(stderr, "Usage: .changes on|off\n");
19556 rc = 1;
@@ -19560,11 +19586,11 @@
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 ){
19566 char *zRes = 0;
19567 output_reset(p);
19568 if( nArg!=2 ){
19569 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
19570 rc = 2;
@@ -19583,11 +19609,11 @@
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,11 +19620,11 @@
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;
19603 for(i=0; i<ArraySize(p->aAuxDb); i++){
19604 const char *zFile = p->aAuxDb[i].zDbFilename;
@@ -19621,11 +19647,11 @@
19621 p->pAuxDb->db = p->db;
19622 p->pAuxDb = &p->aAuxDb[i];
19623 globalDb = p->db = p->pAuxDb->db;
19624 p->pAuxDb->db = 0;
19625 }
19626 }else if( nArg==3 && strcmp(azArg[1], "close")==0
19627 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
19628 int i = azArg[2][0] - '0';
19629 if( i<0 || i>=ArraySize(p->aAuxDb) ){
19630 /* No-op */
19631 }else if( p->pAuxDb == &p->aAuxDb[i] ){
@@ -19640,11 +19666,11 @@
19640 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
19641 rc = 1;
19642 }
19643 }else
19644
19645 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
19646 char **azName = 0;
19647 int nName = 0;
19648 sqlite3_stmt *pStmt;
19649 int i;
19650 open_db(p, 0);
@@ -19679,11 +19705,11 @@
19679 free(azName[i*2+1]);
19680 }
19681 sqlite3_free(azName);
19682 }else
19683
19684 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
19685 static const struct DbConfigChoices {
19686 const char *zName;
19687 int op;
19688 } aDbConfig[] = {
19689 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
@@ -19704,11 +19730,11 @@
19704 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
19705 };
19706 int ii, v;
19707 open_db(p, 0);
19708 for(ii=0; ii<ArraySize(aDbConfig); ii++){
19709 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
19710 if( nArg>=3 ){
19711 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
19712 }
19713 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
19714 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
@@ -19719,21 +19745,21 @@
19719 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
19720 }
19721 }else
19722
19723 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19724 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
19725 rc = shell_dbinfo_command(p, nArg, azArg);
19726 }else
19727
19728 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
19729 open_db(p, 0);
19730 rc = recoverDatabaseCmd(p, nArg, azArg);
19731 }else
19732 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
19733
19734 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
19735 char *zLike = 0;
19736 char *zSql;
19737 int i;
19738 int savedShowHeader = p->showHeader;
19739 int savedShellFlags = p->shellFlgs;
@@ -19742,11 +19768,11 @@
19742 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
19743 for(i=1; i<nArg; i++){
19744 if( azArg[i][0]=='-' ){
19745 const char *z = azArg[i]+1;
19746 if( z[0]=='-' ) z++;
19747 if( strcmp(z,"preserve-rowids")==0 ){
19748 #ifdef SQLITE_OMIT_VIRTUALTABLE
19749 raw_printf(stderr, "The --preserve-rowids option is not compatible"
19750 " with SQLITE_OMIT_VIRTUALTABLE\n");
19751 rc = 1;
19752 sqlite3_free(zLike);
@@ -19753,17 +19779,17 @@
19753 goto meta_command_exit;
19754 #else
19755 ShellSetFlag(p, SHFLG_PreserveRowid);
19756 #endif
19757 }else
19758 if( strcmp(z,"newlines")==0 ){
19759 ShellSetFlag(p, SHFLG_Newlines);
19760 }else
19761 if( strcmp(z,"data-only")==0 ){
19762 ShellSetFlag(p, SHFLG_DumpDataOnly);
19763 }else
19764 if( strcmp(z,"nosys")==0 ){
19765 ShellSetFlag(p, SHFLG_DumpNoSys);
19766 }else
19767 {
19768 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
19769 rc = 1;
@@ -19841,35 +19867,35 @@
19841 }
19842 p->showHeader = savedShowHeader;
19843 p->shellFlgs = savedShellFlags;
19844 }else
19845
19846 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
19847 if( nArg==2 ){
19848 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
19849 }else{
19850 raw_printf(stderr, "Usage: .echo on|off\n");
19851 rc = 1;
19852 }
19853 }else
19854
19855 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
19856 if( nArg==2 ){
19857 p->autoEQPtest = 0;
19858 if( p->autoEQPtrace ){
19859 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
19860 p->autoEQPtrace = 0;
19861 }
19862 if( strcmp(azArg[1],"full")==0 ){
19863 p->autoEQP = AUTOEQP_full;
19864 }else if( strcmp(azArg[1],"trigger")==0 ){
19865 p->autoEQP = AUTOEQP_trigger;
19866 #ifdef SQLITE_DEBUG
19867 }else if( strcmp(azArg[1],"test")==0 ){
19868 p->autoEQP = AUTOEQP_on;
19869 p->autoEQPtest = 1;
19870 }else if( strcmp(azArg[1],"trace")==0 ){
19871 p->autoEQP = AUTOEQP_full;
19872 p->autoEQPtrace = 1;
19873 open_db(p, 0);
19874 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
19875 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
@@ -19882,22 +19908,22 @@
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
19892
19893 /* The ".explain" command is automatic now. It is largely pointless. It
19894 ** retained purely for backwards compatibility */
19895 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19896 int val = 1;
19897 if( nArg>=2 ){
19898 if( strcmp(azArg[1],"auto")==0 ){
19899 val = 99;
19900 }else{
19901 val = booleanValue(azArg[1]);
19902 }
19903 }
@@ -19913,11 +19939,11 @@
19913 p->autoExplain = 1;
19914 }
19915 }else
19916
19917 #ifndef SQLITE_OMIT_VIRTUALTABLE
19918 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19919 if( p->bSafeMode ){
19920 raw_printf(stderr,
19921 "Cannot run experimental commands such as \"%s\" in safe mode\n",
19922 azArg[0]);
19923 rc = 1;
@@ -19926,11 +19952,11 @@
19926 expertDotCommand(p, azArg, nArg);
19927 }
19928 }else
19929 #endif
19930
19931 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19932 static const struct {
19933 const char *zCtrlName; /* Name of a test-control option */
19934 int ctrlCode; /* Integer code for that option */
19935 const char *zUsage; /* Usage notes */
19936 } aCtrl[] = {
@@ -19956,11 +19982,11 @@
19956
19957 open_db(p, 0);
19958 zCmd = nArg>=2 ? azArg[1] : "help";
19959
19960 if( zCmd[0]=='-'
19961 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
19962 && nArg>=4
19963 ){
19964 zSchema = azArg[2];
19965 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
19966 nArg -= 2;
@@ -19972,11 +19998,11 @@
19972 zCmd++;
19973 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19974 }
19975
19976 /* --help lists all file-controls */
19977 if( strcmp(zCmd,"help")==0 ){
19978 utf8_printf(p->out, "Available file-controls:\n");
19979 for(i=0; i<ArraySize(aCtrl); i++){
19980 utf8_printf(p->out, " .filectrl %s %s\n",
19981 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19982 }
@@ -19986,11 +20012,11 @@
19986
19987 /* convert filectrl text option to value. allow any unique prefix
19988 ** of the option name, or a numerical value. */
19989 n2 = strlen30(zCmd);
19990 for(i=0; i<ArraySize(aCtrl); i++){
19991 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19992 if( filectrl<0 ){
19993 filectrl = aCtrl[i].ctrlCode;
19994 iCtrl = i;
19995 }else{
19996 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
@@ -20073,11 +20099,11 @@
20073 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
20074 raw_printf(p->out, "%s\n", zBuf);
20075 }
20076 }else
20077
20078 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
20079 ShellState data;
20080 int doStats = 0;
20081 memcpy(&data, p, sizeof(data));
20082 data.showHeader = 0;
20083 data.cMode = data.mode = MODE_Semi;
@@ -20120,21 +20146,21 @@
20120 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
20121 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
20122 }
20123 }else
20124
20125 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
20126 if( nArg==2 ){
20127 p->showHeader = booleanValue(azArg[1]);
20128 p->shellFlgs |= SHFLG_HeaderSet;
20129 }else{
20130 raw_printf(stderr, "Usage: .headers on|off\n");
20131 rc = 1;
20132 }
20133 }else
20134
20135 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
20136 if( nArg>=2 ){
20137 n = showHelp(p->out, azArg[1]);
20138 if( n==0 ){
20139 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
20140 }
@@ -20142,11 +20168,11 @@
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 */
20152 int nCol; /* Number of columns in the table */
@@ -20182,22 +20208,22 @@
20182 }else{
20183 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
20184 showHelp(p->out, "import");
20185 goto meta_command_exit;
20186 }
20187 }else if( strcmp(z,"-v")==0 ){
20188 eVerbose++;
20189 }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
20190 zSchema = azArg[++i];
20191 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
20192 nSkip = integerValue(azArg[++i]);
20193 }else if( strcmp(z,"-ascii")==0 ){
20194 sCtx.cColSep = SEP_Unit[0];
20195 sCtx.cRowSep = SEP_Record[0];
20196 xRead = ascii_read_one_field;
20197 useOutputMode = 0;
20198 }else if( strcmp(z,"-csv")==0 ){
20199 sCtx.cColSep = ',';
20200 sCtx.cRowSep = '\n';
20201 xRead = csv_read_one_field;
20202 useOutputMode = 0;
20203 }else{
@@ -20233,11 +20259,13 @@
20233 if( nSep==0 ){
20234 raw_printf(stderr,
20235 "Error: non-null row separator required for import\n");
20236 goto meta_command_exit;
20237 }
20238 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
 
 
20239 /* When importing CSV (only), if the row separator is set to the
20240 ** default output row separator, change it to the default input
20241 ** row separator. This avoids having to maintain different input
20242 ** and output row separators. */
20243 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -20435,11 +20463,11 @@
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;
20443 sqlite3_stmt *pStmt;
20444 int tnum = 0;
20445 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
@@ -20536,17 +20564,17 @@
20536 sqlite3_free(zSql);
20537 }else
20538 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
20539
20540 #ifdef SQLITE_ENABLE_IOTRACE
20541 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
20542 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
20543 if( iotrace && iotrace!=stdout ) fclose(iotrace);
20544 iotrace = 0;
20545 if( nArg<2 ){
20546 sqlite3IoTrace = 0;
20547 }else if( strcmp(azArg[1], "-")==0 ){
20548 sqlite3IoTrace = iotracePrintf;
20549 iotrace = stdout;
20550 }else{
20551 iotrace = fopen(azArg[1], "w");
20552 if( iotrace==0 ){
@@ -20558,11 +20586,11 @@
20558 }
20559 }
20560 }else
20561 #endif
20562
20563 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
20564 static const struct {
20565 const char *zLimitName; /* Name of a limit */
20566 int limitCode; /* Integer code for that limit */
20567 } aLimit[] = {
20568 { "length", SQLITE_LIMIT_LENGTH },
@@ -20617,17 +20645,17 @@
20617 printf("%20s %d\n", aLimit[iLimit].zLimitName,
20618 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
20619 }
20620 }else
20621
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 ){
20633 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
@@ -20645,11 +20673,11 @@
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;
20655 }else{
@@ -20658,11 +20686,11 @@
20658 p->pLog = output_file_open(zFile, 0);
20659 }
20660 }else
20661 #endif
20662
20663 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20664 const char *zMode = 0;
20665 const char *zTabname = 0;
20666 int i, n2;
20667 ColModeOpts cmOpts = ColModeOpts_default;
20668 for(i=1; i<nArg; i++){
@@ -20677,14 +20705,14 @@
20677 cmOpts.bQuote = 1;
20678 }else if( optionMatch(z,"noquote") ){
20679 cmOpts.bQuote = 0;
20680 }else if( zMode==0 ){
20681 zMode = z;
20682 /* Apply defaults for qbox pseudo-mods. If that
20683 * overwrites already-set values, user was informed of this.
20684 */
20685 if( strcmp(z, "qbox")==0 ){
20686 ColModeOpts cmo = ColModeOpts_default_qbox;
20687 zMode = "box";
20688 cmOpts = cmo;
20689 }
20690 }else if( zTabname==0 ){
@@ -20719,62 +20747,62 @@
20719 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20720 }
20721 zMode = modeDescr[p->mode];
20722 }
20723 n2 = strlen30(zMode);
20724 if( strncmp(zMode,"lines",n2)==0 ){
20725 p->mode = MODE_Line;
20726 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20727 }else if( strncmp(zMode,"columns",n2)==0 ){
20728 p->mode = MODE_Column;
20729 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
20730 p->showHeader = 1;
20731 }
20732 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20733 p->cmOpts = cmOpts;
20734 }else if( strncmp(zMode,"list",n2)==0 ){
20735 p->mode = MODE_List;
20736 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
20737 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20738 }else if( strncmp(zMode,"html",n2)==0 ){
20739 p->mode = MODE_Html;
20740 }else if( strncmp(zMode,"tcl",n2)==0 ){
20741 p->mode = MODE_Tcl;
20742 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
20743 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20744 }else if( strncmp(zMode,"csv",n2)==0 ){
20745 p->mode = MODE_Csv;
20746 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20747 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20748 }else if( strncmp(zMode,"tabs",n2)==0 ){
20749 p->mode = MODE_List;
20750 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20751 }else if( strncmp(zMode,"insert",n2)==0 ){
20752 p->mode = MODE_Insert;
20753 set_table_name(p, zTabname ? zTabname : "table");
20754 }else if( strncmp(zMode,"quote",n2)==0 ){
20755 p->mode = MODE_Quote;
20756 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20757 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20758 }else if( strncmp(zMode,"ascii",n2)==0 ){
20759 p->mode = MODE_Ascii;
20760 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
20761 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20762 }else if( strncmp(zMode,"markdown",n2)==0 ){
20763 p->mode = MODE_Markdown;
20764 p->cmOpts = cmOpts;
20765 }else if( strncmp(zMode,"table",n2)==0 ){
20766 p->mode = MODE_Table;
20767 p->cmOpts = cmOpts;
20768 }else if( strncmp(zMode,"box",n2)==0 ){
20769 p->mode = MODE_Box;
20770 p->cmOpts = cmOpts;
20771 }else if( strncmp(zMode,"count",n2)==0 ){
20772 p->mode = MODE_Count;
20773 }else if( strncmp(zMode,"off",n2)==0 ){
20774 p->mode = MODE_Off;
20775 }else if( strncmp(zMode,"json",n2)==0 ){
20776 p->mode = MODE_Json;
20777 }else{
20778 raw_printf(stderr, "Error: mode should be one of: "
20779 "ascii box column csv html insert json line list markdown "
20780 "qbox quote table tabs tcl\n");
@@ -20782,15 +20810,15 @@
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 ){
20792 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
20793 p->lineno, azArg[1]);
20794 exit(1);
20795 }else{
20796 p->bSafeMode = 0;
@@ -20798,21 +20826,21 @@
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]);
20807 }else{
20808 raw_printf(stderr, "Usage: .nullvalue STRING\n");
20809 rc = 1;
20810 }
20811 }else
20812
20813 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
20814 const char *zFN = 0; /* Pointer to constant filename */
20815 char *zNewFilename = 0; /* Name of the database file to open */
20816 int iName = 1; /* Index in azArg[] of the filename */
20817 int newFlag = 0; /* True to delete file before opening */
20818 int openMode = SHELL_OPEN_UNSPEC;
@@ -20872,11 +20900,11 @@
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 ){
20879 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20880 }
20881 #else
20882 /* WASM mode has its own sandboxed pseudo-filesystem. */
@@ -20903,12 +20931,13 @@
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;
20912 int bTxtMode = 0;
20913 int i;
20914 int eMode = 0;
@@ -20918,25 +20947,25 @@
20918 zBOM[0] = 0;
20919 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
20920 if( c=='e' ){
20921 eMode = 'x';
20922 bOnce = 2;
20923 }else if( strncmp(azArg[0],"once",n)==0 ){
20924 bOnce = 1;
20925 }
20926 for(i=1; i<nArg; i++){
20927 char *z = azArg[i];
20928 if( z[0]=='-' ){
20929 if( z[1]=='-' ) z++;
20930 if( strcmp(z,"-bom")==0 ){
20931 zBOM[0] = 0xef;
20932 zBOM[1] = 0xbb;
20933 zBOM[2] = 0xbf;
20934 zBOM[3] = 0;
20935 }else if( c!='e' && strcmp(z,"-x")==0 ){
20936 eMode = 'x'; /* spreadsheet */
20937 }else if( c!='e' && strcmp(z,"-e")==0 ){
20938 eMode = 'e'; /* text editor */
20939 }else{
20940 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
20941 azArg[i]);
20942 showHelp(p->out, azArg[0]);
@@ -21005,11 +21034,11 @@
21005 }
21006 #endif
21007 }else{
21008 p->out = output_file_open(zFile, bTxtMode);
21009 if( p->out==0 ){
21010 if( strcmp(zFile,"off")!=0 ){
21011 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
21012 }
21013 p->out = stdout;
21014 rc = 1;
21015 } else {
@@ -21019,26 +21048,26 @@
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
21028 /* .parameter clear
21029 ** Clear all bind parameters by dropping the TEMP table that holds them.
21030 */
21031 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
21032 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
21033 0, 0, 0);
21034 }else
21035
21036 /* .parameter list
21037 ** List all bind parameters.
21038 */
21039 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
21040 sqlite3_stmt *pStmt = 0;
21041 int rx;
21042 int len = 0;
21043 rx = sqlite3_prepare_v2(p->db,
21044 "SELECT max(length(key)) "
@@ -21063,21 +21092,21 @@
21063
21064 /* .parameter init
21065 ** Make sure the TEMP table used to hold bind parameters exists.
21066 ** Create it if necessary.
21067 */
21068 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
21069 bind_table_init(p);
21070 }else
21071
21072 /* .parameter set NAME VALUE
21073 ** Set or reset a bind parameter. NAME should be the full parameter
21074 ** name exactly as it appears in the query. (ex: $abc, @def). The
21075 ** VALUE can be in either SQL literal notation, or if not it will be
21076 ** understood to be a text string.
21077 */
21078 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
21079 int rx;
21080 char *zSql;
21081 sqlite3_stmt *pStmt;
21082 const char *zKey = azArg[2];
21083 const char *zValue = azArg[3];
@@ -21111,11 +21140,11 @@
21111
21112 /* .parameter unset NAME
21113 ** Remove the NAME binding from the parameter binding table, if it
21114 ** exists.
21115 */
21116 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
21117 char *zSql = sqlite3_mprintf(
21118 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
21119 shell_check_oom(zSql);
21120 sqlite3_exec(p->db, zSql, 0, 0, 0);
21121 sqlite3_free(zSql);
@@ -21123,21 +21152,21 @@
21123 /* If no command name matches, show a syntax error */
21124 parameter_syntax_error:
21125 showHelp(p->out, "parameter");
21126 }else
21127
21128 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
21129 int i;
21130 for(i=1; i<nArg; i++){
21131 if( i>1 ) raw_printf(p->out, " ");
21132 utf8_printf(p->out, "%s", azArg[i]);
21133 }
21134 raw_printf(p->out, "\n");
21135 }else
21136
21137 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
21138 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
21139 int i;
21140 int nn = 0;
21141 p->flgProgress = 0;
21142 p->mxProgress = 0;
21143 p->nProgress = 0;
@@ -21144,23 +21173,23 @@
21144 for(i=1; i<nArg; i++){
21145 const char *z = azArg[i];
21146 if( z[0]=='-' ){
21147 z++;
21148 if( z[0]=='-' ) z++;
21149 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
21150 p->flgProgress |= SHELL_PROGRESS_QUIET;
21151 continue;
21152 }
21153 if( strcmp(z,"reset")==0 ){
21154 p->flgProgress |= SHELL_PROGRESS_RESET;
21155 continue;
21156 }
21157 if( strcmp(z,"once")==0 ){
21158 p->flgProgress |= SHELL_PROGRESS_ONCE;
21159 continue;
21160 }
21161 if( strcmp(z,"limit")==0 ){
21162 if( i+1>=nArg ){
21163 utf8_printf(stderr, "Error: missing argument on --limit\n");
21164 rc = 1;
21165 goto meta_command_exit;
21166 }else{
@@ -21178,27 +21207,27 @@
21178 open_db(p, 0);
21179 sqlite3_progress_handler(p->db, nn, progress_handler, p);
21180 }else
21181 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
21182
21183 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
21184 if( nArg >= 2) {
21185 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
21186 }
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 ){
21204 raw_printf(stderr, "Usage: .read FILE\n");
@@ -21231,11 +21260,11 @@
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;
21241 int nTimeout = 0;
@@ -21284,11 +21313,11 @@
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
21293 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
21294 #endif
@@ -21296,11 +21325,11 @@
21296 raw_printf(stderr, "Usage: .scanstats on|off\n");
21297 rc = 1;
21298 }
21299 }else
21300
21301 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
21302 ShellText sSelect;
21303 ShellState data;
21304 char *zErrMsg = 0;
21305 const char *zDiv = "(";
21306 const char *zName = 0;
@@ -21439,19 +21468,19 @@
21439 }else{
21440 rc = 0;
21441 }
21442 }else
21443
21444 if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0)
21445 || (c=='t' && n==9 && strncmp(azArg[0], "treetrace", n)==0)
21446 ){
21447 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21448 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
21449 }else
21450
21451 #if defined(SQLITE_ENABLE_SESSION)
21452 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
21453 struct AuxDb *pAuxDb = p->pAuxDb;
21454 OpenSession *pSession = &pAuxDb->aSession[0];
21455 char **azCmd = &azArg[1];
21456 int iSes = 0;
21457 int nCmd = nArg - 1;
@@ -21458,11 +21487,11 @@
21458 int i;
21459 if( nArg<=1 ) goto session_syntax_error;
21460 open_db(p, 0);
21461 if( nArg>=3 ){
21462 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
21463 if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
21464 }
21465 if( iSes<pAuxDb->nSession ){
21466 pSession = &pAuxDb->aSession[iSes];
21467 azCmd++;
21468 nCmd--;
@@ -21474,11 +21503,11 @@
21474
21475 /* .session attach TABLE
21476 ** Invoke the sqlite3session_attach() interface to attach a particular
21477 ** table so that it is never filtered.
21478 */
21479 if( strcmp(azCmd[0],"attach")==0 ){
21480 if( nCmd!=2 ) goto session_syntax_error;
21481 if( pSession->p==0 ){
21482 session_not_open:
21483 raw_printf(stderr, "ERROR: No sessions are open\n");
21484 }else{
@@ -21492,11 +21521,13 @@
21492
21493 /* .session changeset FILE
21494 ** .session patchset FILE
21495 ** Write a changeset or patchset into a file. The file is overwritten.
21496 */
21497 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
 
 
21498 FILE *out = 0;
21499 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
21500 if( nCmd!=2 ) goto session_syntax_error;
21501 if( pSession->p==0 ) goto session_not_open;
21502 out = fopen(azCmd[1], "wb");
@@ -21526,11 +21557,11 @@
21526 }else
21527
21528 /* .session close
21529 ** Close the identified session
21530 */
21531 if( strcmp(azCmd[0], "close")==0 ){
21532 if( nCmd!=1 ) goto session_syntax_error;
21533 if( pAuxDb->nSession ){
21534 session_close(pSession);
21535 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
21536 }
@@ -21537,11 +21568,11 @@
21537 }else
21538
21539 /* .session enable ?BOOLEAN?
21540 ** Query or set the enable flag
21541 */
21542 if( strcmp(azCmd[0], "enable")==0 ){
21543 int ii;
21544 if( nCmd>2 ) goto session_syntax_error;
21545 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21546 if( pAuxDb->nSession ){
21547 ii = sqlite3session_enable(pSession->p, ii);
@@ -21551,11 +21582,11 @@
21551 }else
21552
21553 /* .session filter GLOB ....
21554 ** Set a list of GLOB patterns of table names to be excluded.
21555 */
21556 if( strcmp(azCmd[0], "filter")==0 ){
21557 int ii, nByte;
21558 if( nCmd<2 ) goto session_syntax_error;
21559 if( pAuxDb->nSession ){
21560 for(ii=0; ii<pSession->nFilter; ii++){
21561 sqlite3_free(pSession->azFilter[ii]);
@@ -21576,11 +21607,11 @@
21576 }else
21577
21578 /* .session indirect ?BOOLEAN?
21579 ** Query or set the indirect flag
21580 */
21581 if( strcmp(azCmd[0], "indirect")==0 ){
21582 int ii;
21583 if( nCmd>2 ) goto session_syntax_error;
21584 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21585 if( pAuxDb->nSession ){
21586 ii = sqlite3session_indirect(pSession->p, ii);
@@ -21590,11 +21621,11 @@
21590 }else
21591
21592 /* .session isempty
21593 ** Determine if the session is empty
21594 */
21595 if( strcmp(azCmd[0], "isempty")==0 ){
21596 int ii;
21597 if( nCmd!=1 ) goto session_syntax_error;
21598 if( pAuxDb->nSession ){
21599 ii = sqlite3session_isempty(pSession->p);
21600 utf8_printf(p->out, "session %s isempty flag = %d\n",
@@ -21603,27 +21634,27 @@
21603 }else
21604
21605 /* .session list
21606 ** List all currently open sessions
21607 */
21608 if( strcmp(azCmd[0],"list")==0 ){
21609 for(i=0; i<pAuxDb->nSession; i++){
21610 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
21611 }
21612 }else
21613
21614 /* .session open DB NAME
21615 ** Open a new session called NAME on the attached database DB.
21616 ** DB is normally "main".
21617 */
21618 if( strcmp(azCmd[0],"open")==0 ){
21619 char *zName;
21620 if( nCmd!=3 ) goto session_syntax_error;
21621 zName = azCmd[2];
21622 if( zName[0]==0 ) goto session_syntax_error;
21623 for(i=0; i<pAuxDb->nSession; i++){
21624 if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
21625 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
21626 goto meta_command_exit;
21627 }
21628 }
21629 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
@@ -21650,19 +21681,19 @@
21650 #endif
21651
21652 #ifdef SQLITE_DEBUG
21653 /* Undocumented commands for internal testing. Subject to change
21654 ** without notice. */
21655 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
21656 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
21657 int i, v;
21658 for(i=1; i<nArg; i++){
21659 v = booleanValue(azArg[i]);
21660 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
21661 }
21662 }
21663 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
21664 int i; sqlite3_int64 v;
21665 for(i=1; i<nArg; i++){
21666 char zBuf[200];
21667 v = integerValue(azArg[i]);
21668 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
@@ -21670,11 +21701,11 @@
21670 }
21671 }
21672 }else
21673 #endif
21674
21675 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
21676 int bIsInit = 0; /* True to initialize the SELFTEST table */
21677 int bVerbose = 0; /* Verbose output */
21678 int bSelftestExists; /* True if SELFTEST already exists */
21679 int i, k; /* Loop counters */
21680 int nTest = 0; /* Number of tests runs */
@@ -21684,14 +21715,14 @@
21684
21685 open_db(p,0);
21686 for(i=1; i<nArg; i++){
21687 const char *z = azArg[i];
21688 if( z[0]=='-' && z[1]=='-' ) z++;
21689 if( strcmp(z,"-init")==0 ){
21690 bIsInit = 1;
21691 }else
21692 if( strcmp(z,"-v")==0 ){
21693 bVerbose++;
21694 }else
21695 {
21696 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21697 azArg[i], azArg[0]);
@@ -21740,14 +21771,14 @@
21740 if( zAns==0 ) continue;
21741 k = 0;
21742 if( bVerbose>0 ){
21743 printf("%d: %s %s\n", tno, zOp, zSql);
21744 }
21745 if( strcmp(zOp,"memo")==0 ){
21746 utf8_printf(p->out, "%s\n", zSql);
21747 }else
21748 if( strcmp(zOp,"run")==0 ){
21749 char *zErrMsg = 0;
21750 str.n = 0;
21751 str.z[0] = 0;
21752 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
21753 nTest++;
@@ -21757,11 +21788,11 @@
21757 if( rc || zErrMsg ){
21758 nErr++;
21759 rc = 1;
21760 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
21761 sqlite3_free(zErrMsg);
21762 }else if( strcmp(zAns,str.z)!=0 ){
21763 nErr++;
21764 rc = 1;
21765 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
21766 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
21767 }
@@ -21777,11 +21808,11 @@
21777 } /* End loop over k */
21778 freeText(&str);
21779 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
21780 }else
21781
21782 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
21783 if( nArg<2 || nArg>3 ){
21784 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
21785 rc = 1;
21786 }
21787 if( nArg>=2 ){
@@ -21792,11 +21823,11 @@
21792 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
21793 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
21794 }
21795 }else
21796
21797 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
21798 const char *zLike = 0; /* Which table to checksum. 0 means everything */
21799 int i; /* Loop counter */
21800 int bSchema = 0; /* Also hash the schema */
21801 int bSeparate = 0; /* Hash each table separately */
21802 int iSize = 224; /* Hash algorithm to use */
@@ -21810,19 +21841,19 @@
21810 for(i=1; i<nArg; i++){
21811 const char *z = azArg[i];
21812 if( z[0]=='-' ){
21813 z++;
21814 if( z[0]=='-' ) z++;
21815 if( strcmp(z,"schema")==0 ){
21816 bSchema = 1;
21817 }else
21818 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
21819 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
21820 ){
21821 iSize = atoi(&z[5]);
21822 }else
21823 if( strcmp(z,"debug")==0 ){
21824 bDebug = 1;
21825 }else
21826 {
21827 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21828 azArg[i], azArg[0]);
@@ -21858,24 +21889,24 @@
21858 zSep = "VALUES(";
21859 while( SQLITE_ROW==sqlite3_step(pStmt) ){
21860 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21861 if( zTab==0 ) continue;
21862 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21863 if( strncmp(zTab, "sqlite_",7)!=0 ){
21864 appendText(&sQuery,"SELECT * FROM ", 0);
21865 appendText(&sQuery,zTab,'"');
21866 appendText(&sQuery," NOT INDEXED;", 0);
21867 }else if( strcmp(zTab, "sqlite_schema")==0 ){
21868 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
21869 " ORDER BY name;", 0);
21870 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
21871 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
21872 " ORDER BY name;", 0);
21873 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
21874 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
21875 " ORDER BY tbl,idx;", 0);
21876 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
21877 appendText(&sQuery, "SELECT * FROM ", 0);
21878 appendText(&sQuery, zTab, 0);
21879 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
21880 }
21881 appendText(&sSql, zSep, 0);
@@ -21910,11 +21941,12 @@
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;
21919 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
21920 if( nArg<2 ){
@@ -21931,11 +21963,11 @@
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;
21940 if( nArg!=1 ){
21941 raw_printf(stderr, "Usage: .show\n");
@@ -21984,15 +22016,15 @@
21984 raw_printf(p->out, "\n");
21985 utf8_printf(p->out, "%12.12s: %s\n", "filename",
21986 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
21987 }else
21988
21989 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
21990 if( nArg==2 ){
21991 if( strcmp(azArg[1],"stmt")==0 ){
21992 p->statsOn = 2;
21993 }else if( strcmp(azArg[1],"vmstep")==0 ){
21994 p->statsOn = 3;
21995 }else{
21996 p->statsOn = (u8)booleanValue(azArg[1]);
21997 }
21998 }else if( nArg==1 ){
@@ -22001,13 +22033,13 @@
22001 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
22002 rc = 1;
22003 }
22004 }else
22005
22006 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
22007 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
22008 || strncmp(azArg[0], "indexes", n)==0) )
22009 ){
22010 sqlite3_stmt *pStmt;
22011 char **azResult;
22012 int nRow, nAlloc;
22013 int ii;
@@ -22111,11 +22143,11 @@
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 ){
22120 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
22121 }
@@ -22126,11 +22158,11 @@
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 */
22134 int ctrlCode; /* Integer code for that option */
22135 int unSafe; /* Not valid for --safe mode */
22136 const char *zUsage; /* Usage notes */
@@ -22173,11 +22205,11 @@
22173 zCmd++;
22174 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
22175 }
22176
22177 /* --help lists all test-controls */
22178 if( strcmp(zCmd,"help")==0 ){
22179 utf8_printf(p->out, "Available test-controls:\n");
22180 for(i=0; i<ArraySize(aCtrl); i++){
22181 utf8_printf(p->out, " .testctrl %s %s\n",
22182 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
22183 }
@@ -22187,11 +22219,11 @@
22187
22188 /* convert testctrl text option to value. allow any unique prefix
22189 ** of the option name, or a numerical value. */
22190 n2 = strlen30(zCmd);
22191 for(i=0; i<ArraySize(aCtrl); i++){
22192 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
22193 if( testctrl<0 ){
22194 testctrl = aCtrl[i].ctrlCode;
22195 iCtrl = i;
22196 }else{
22197 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
@@ -22243,11 +22275,11 @@
22243 /* sqlite3_test_control(int, int, sqlite3*) */
22244 case SQLITE_TESTCTRL_PRNG_SEED:
22245 if( nArg==3 || nArg==4 ){
22246 int ii = (int)integerValue(azArg[2]);
22247 sqlite3 *db;
22248 if( ii==0 && strcmp(azArg[2],"random")==0 ){
22249 sqlite3_randomness(sizeof(ii),&ii);
22250 printf("-- random seed: %d\n", ii);
22251 }
22252 if( nArg==3 ){
22253 db = 0;
@@ -22359,16 +22391,16 @@
22359 raw_printf(p->out, "0x%08x\n", rc2);
22360 }
22361 }else
22362 #endif /* !defined(SQLITE_UNTESTABLE) */
22363
22364 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
22365 open_db(p, 0);
22366 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
22367 }else
22368
22369 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
22370 if( nArg==2 ){
22371 enableTimer = booleanValue(azArg[1]);
22372 if( enableTimer && !HAS_TIMER ){
22373 raw_printf(stderr, "Error: timer not available on this system.\n");
22374 enableTimer = 0;
@@ -22378,11 +22410,11 @@
22378 rc = 1;
22379 }
22380 }else
22381
22382 #ifndef SQLITE_OMIT_TRACE
22383 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
22384 int mType = 0;
22385 int jj;
22386 open_db(p, 0);
22387 for(jj=1; jj<nArg; jj++){
22388 const char *z = azArg[jj];
@@ -22428,11 +22460,11 @@
22428 }
22429 }else
22430 #endif /* !defined(SQLITE_OMIT_TRACE) */
22431
22432 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22433 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
22434 int ii;
22435 int lenOpt;
22436 char *zOpt;
22437 if( nArg<2 ){
22438 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
@@ -22441,11 +22473,11 @@
22441 }
22442 open_db(p, 0);
22443 zOpt = azArg[1];
22444 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
22445 lenOpt = (int)strlen(zOpt);
22446 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
22447 assert( azArg[nArg]==0 );
22448 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
22449 }else{
22450 for(ii=1; ii<nArg; ii++){
22451 sqlite3_create_module(p->db, azArg[ii], 0, 0);
@@ -22453,18 +22485,18 @@
22453 }
22454 }else
22455 #endif
22456
22457 #if SQLITE_USER_AUTHENTICATION
22458 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
22459 if( nArg<2 ){
22460 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
22461 rc = 1;
22462 goto meta_command_exit;
22463 }
22464 open_db(p, 0);
22465 if( strcmp(azArg[1],"login")==0 ){
22466 if( nArg!=4 ){
22467 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
22468 rc = 1;
22469 goto meta_command_exit;
22470 }
@@ -22472,11 +22504,11 @@
22472 strlen30(azArg[3]));
22473 if( rc ){
22474 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
22475 rc = 1;
22476 }
22477 }else if( strcmp(azArg[1],"add")==0 ){
22478 if( nArg!=5 ){
22479 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
22480 rc = 1;
22481 goto meta_command_exit;
22482 }
@@ -22484,11 +22516,11 @@
22484 booleanValue(azArg[4]));
22485 if( rc ){
22486 raw_printf(stderr, "User-Add failed: %d\n", rc);
22487 rc = 1;
22488 }
22489 }else if( strcmp(azArg[1],"edit")==0 ){
22490 if( nArg!=5 ){
22491 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
22492 rc = 1;
22493 goto meta_command_exit;
22494 }
@@ -22496,11 +22528,11 @@
22496 booleanValue(azArg[4]));
22497 if( rc ){
22498 raw_printf(stderr, "User-Edit failed: %d\n", rc);
22499 rc = 1;
22500 }
22501 }else if( strcmp(azArg[1],"delete")==0 ){
22502 if( nArg!=3 ){
22503 raw_printf(stderr, "Usage: .user delete USER\n");
22504 rc = 1;
22505 goto meta_command_exit;
22506 }
@@ -22515,11 +22547,11 @@
22515 goto meta_command_exit;
22516 }
22517 }else
22518 #endif /* SQLITE_USER_AUTHENTICATION */
22519
22520 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
22521 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
22522 sqlite3_libversion(), sqlite3_sourceid());
22523 #if SQLITE_HAVE_ZLIB
22524 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
22525 #endif
@@ -22534,11 +22566,11 @@
22534 #elif defined(__GNUC__) && defined(__VERSION__)
22535 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
22536 #endif
22537 }else
22538
22539 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
22540 const char *zDbName = nArg==2 ? azArg[1] : "main";
22541 sqlite3_vfs *pVfs = 0;
22542 if( p->db ){
22543 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
22544 if( pVfs ){
@@ -22548,11 +22580,11 @@
22548 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
22549 }
22550 }
22551 }else
22552
22553 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
22554 sqlite3_vfs *pVfs;
22555 sqlite3_vfs *pCurrent = 0;
22556 if( p->db ){
22557 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
22558 }
@@ -22566,11 +22598,11 @@
22566 raw_printf(p->out, "-----------------------------------\n");
22567 }
22568 }
22569 }else
22570
22571 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
22572 const char *zDbName = nArg==2 ? azArg[1] : "main";
22573 char *zVfsName = 0;
22574 if( p->db ){
22575 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
22576 if( zVfsName ){
@@ -22578,16 +22610,16 @@
22578 sqlite3_free(zVfsName);
22579 }
22580 }
22581 }else
22582
22583 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
22584 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
22585 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
22586 }else
22587
22588 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
22589 int j;
22590 assert( nArg<=ArraySize(azArg) );
22591 p->nWidth = nArg-1;
22592 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
22593 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
@@ -22761,14 +22793,14 @@
22761 const char *zErrorTail;
22762 const char *zErrorType;
22763 if( zErrMsg==0 ){
22764 zErrorType = "Error";
22765 zErrorTail = sqlite3_errmsg(p->db);
22766 }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
22767 zErrorType = "Parse error";
22768 zErrorTail = &zErrMsg[12];
22769 }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
22770 zErrorType = "Runtime error";
22771 zErrorTail = &zErrMsg[10];
22772 }else{
22773 zErrorType = "Error";
22774 zErrorTail = zErrMsg;
@@ -22806,11 +22838,11 @@
22806 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
22807 /* Parse the next line from shellState.wasm.zInput. */
22808 const char *zBegin = shellState.wasm.zPos;
22809 const char *z = zBegin;
22810 char *zLine = 0;
22811 int nZ = 0;
22812
22813 UNUSED_PARAMETER(in);
22814 UNUSED_PARAMETER(isContinuation);
22815 if(!z || !*z){
22816 return 0;
@@ -22822,11 +22854,11 @@
22822 --nZ;
22823 }
22824 shellState.wasm.zPos = z;
22825 zLine = realloc(zPrior, nZ+1);
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
@@ -22840,16 +22872,16 @@
22840 ** Return the number of errors.
22841 */
22842 static int process_input(ShellState *p){
22843 char *zLine = 0; /* A single input line */
22844 char *zSql = 0; /* Accumulated SQL text */
22845 int nLine; /* Length of current line */
22846 int nSql = 0; /* Bytes of zSql[] used */
22847 int nAlloc = 0; /* Allocated zSql[] space */
22848 int rc; /* Error code */
22849 int errCnt = 0; /* Number of errors seen */
22850 int startline = 0; /* Line number for start of current input */
22851 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
22852
22853 if( p->inputNesting==MAX_INPUT_NESTING ){
22854 /* This will be more informative in a later version. */
22855 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
@@ -22895,19 +22927,19 @@
22895 }
22896 qss = QSS_Start;
22897 continue;
22898 }
22899 /* No single-line dispositions remain; accumulate line(s). */
22900 nLine = strlen30(zLine);
22901 if( nSql+nLine+2>=nAlloc ){
22902 /* Grow buffer by half-again increments when big. */
22903 nAlloc = nSql+(nSql>>1)+nLine+100;
22904 zSql = realloc(zSql, nAlloc);
22905 shell_check_oom(zSql);
22906 }
22907 if( nSql==0 ){
22908 int i;
22909 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
22910 assert( nAlloc>0 && zSql!=0 );
22911 memcpy(zSql, zLine+i, nLine+1-i);
22912 startline = p->lineno;
22913 nSql = nLine-i;
@@ -23003,11 +23035,11 @@
23003 #endif
23004
23005 #endif /* !_WIN32_WCE */
23006
23007 if( home_dir ){
23008 int n = strlen30(home_dir) + 1;
23009 char *z = malloc( n );
23010 if( z ) memcpy(z, home_dir, n);
23011 home_dir = z;
23012 }
23013
@@ -23246,10 +23278,11 @@
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);
23254 #endif
23255
@@ -23273,11 +23306,11 @@
23273 }
23274 }
23275 #endif
23276
23277 #if USE_SYSTEM_SQLITE+0!=1
23278 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
23279 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
23280 sqlite3_sourceid(), SQLITE_SOURCE_ID);
23281 exit(1);
23282 }
23283 #endif
@@ -23295,13 +23328,13 @@
23295 shell_check_oom(argvToFree);
23296 argcToFree = argc;
23297 argv = argvToFree + argc;
23298 for(i=0; i<argc; i++){
23299 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
23300 int n;
23301 shell_check_oom(z);
23302 n = (int)strlen(z);
23303 argv[i] = malloc( n+1 );
23304 shell_check_oom(argv[i]);
23305 memcpy(argv[i], z, n+1);
23306 argvToFree[i] = argv[i];
23307 sqlite3_free(z);
@@ -23354,25 +23387,25 @@
23354 shell_check_oom(azCmd);
23355 azCmd[nCmd-1] = z;
23356 }
23357 }
23358 if( z[1]=='-' ) z++;
23359 if( strcmp(z,"-separator")==0
23360 || strcmp(z,"-nullvalue")==0
23361 || strcmp(z,"-newline")==0
23362 || strcmp(z,"-cmd")==0
23363 ){
23364 (void)cmdline_option_value(argc, argv, ++i);
23365 }else if( strcmp(z,"-init")==0 ){
23366 zInitFile = cmdline_option_value(argc, argv, ++i);
23367 }else if( strcmp(z,"-batch")==0 ){
23368 /* Need to check for batch mode here to so we can avoid printing
23369 ** informational messages (like from process_sqliterc) before
23370 ** we do the actual processing of arguments later in a second pass.
23371 */
23372 stdin_is_interactive = 0;
23373 }else if( strcmp(z,"-heap")==0 ){
23374 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
23375 const char *zSize;
23376 sqlite3_int64 szHeap;
23377
23378 zSize = cmdline_option_value(argc, argv, ++i);
@@ -23380,11 +23413,11 @@
23380 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
23381 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
23382 #else
23383 (void)cmdline_option_value(argc, argv, ++i);
23384 #endif
23385 }else if( strcmp(z,"-pagecache")==0 ){
23386 sqlite3_int64 n, sz;
23387 sz = integerValue(cmdline_option_value(argc,argv,++i));
23388 if( sz>70000 ) sz = 70000;
23389 if( sz<0 ) sz = 0;
23390 n = integerValue(cmdline_option_value(argc,argv,++i));
@@ -23392,28 +23425,28 @@
23392 n = 0xffffffffffffLL/sz;
23393 }
23394 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
23395 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
23396 data.shellFlgs |= SHFLG_Pagecache;
23397 }else if( strcmp(z,"-lookaside")==0 ){
23398 int n, sz;
23399 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
23400 if( sz<0 ) sz = 0;
23401 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23402 if( n<0 ) n = 0;
23403 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
23404 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
23405 }else if( strcmp(z,"-threadsafe")==0 ){
23406 int n;
23407 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23408 switch( n ){
23409 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
23410 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
23411 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
23412 }
23413 #ifdef SQLITE_ENABLE_VFSTRACE
23414 }else if( strcmp(z,"-vfstrace")==0 ){
23415 extern int vfstrace_register(
23416 const char *zTraceName,
23417 const char *zOldVfsName,
23418 int (*xOut)(const char*,void*),
23419 void *pOutArg,
@@ -23420,54 +23453,54 @@
23420 int makeDefault
23421 );
23422 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
23423 #endif
23424 #ifdef SQLITE_ENABLE_MULTIPLEX
23425 }else if( strcmp(z,"-multiplex")==0 ){
23426 extern int sqlite3_multiple_initialize(const char*,int);
23427 sqlite3_multiplex_initialize(0, 1);
23428 #endif
23429 }else if( strcmp(z,"-mmap")==0 ){
23430 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23431 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
23432 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23433 }else if( strcmp(z,"-sorterref")==0 ){
23434 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23435 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
23436 #endif
23437 }else if( strcmp(z,"-vfs")==0 ){
23438 zVfs = cmdline_option_value(argc, argv, ++i);
23439 #ifdef SQLITE_HAVE_ZLIB
23440 }else if( strcmp(z,"-zip")==0 ){
23441 data.openMode = SHELL_OPEN_ZIPFILE;
23442 #endif
23443 }else if( strcmp(z,"-append")==0 ){
23444 data.openMode = SHELL_OPEN_APPENDVFS;
23445 #ifndef SQLITE_OMIT_DESERIALIZE
23446 }else if( strcmp(z,"-deserialize")==0 ){
23447 data.openMode = SHELL_OPEN_DESERIALIZE;
23448 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23449 data.szMax = integerValue(argv[++i]);
23450 #endif
23451 }else if( strcmp(z,"-readonly")==0 ){
23452 data.openMode = SHELL_OPEN_READONLY;
23453 }else if( strcmp(z,"-nofollow")==0 ){
23454 data.openFlags = SQLITE_OPEN_NOFOLLOW;
23455 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23456 }else if( strncmp(z, "-A",2)==0 ){
23457 /* All remaining command-line arguments are passed to the ".archive"
23458 ** command, so ignore them */
23459 break;
23460 #endif
23461 }else if( strcmp(z, "-memtrace")==0 ){
23462 sqlite3MemTraceActivate(stderr);
23463 }else if( strcmp(z,"-bail")==0 ){
23464 bail_on_error = 1;
23465 }else if( strcmp(z,"-nonce")==0 ){
23466 free(data.zNonce);
23467 data.zNonce = strdup(argv[++i]);
23468 }else if( strcmp(z,"-safe")==0 ){
23469 /* no-op - catch this on the second pass */
23470 }
23471 }
23472 verify_uninitialized();
23473
@@ -23533,131 +23566,131 @@
23533 */
23534 for(i=1; i<argc; i++){
23535 char *z = argv[i];
23536 if( z[0]!='-' ) continue;
23537 if( z[1]=='-' ){ z++; }
23538 if( strcmp(z,"-init")==0 ){
23539 i++;
23540 }else if( strcmp(z,"-html")==0 ){
23541 data.mode = MODE_Html;
23542 }else if( strcmp(z,"-list")==0 ){
23543 data.mode = MODE_List;
23544 }else if( strcmp(z,"-quote")==0 ){
23545 data.mode = MODE_Quote;
23546 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
23547 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23548 }else if( strcmp(z,"-line")==0 ){
23549 data.mode = MODE_Line;
23550 }else if( strcmp(z,"-column")==0 ){
23551 data.mode = MODE_Column;
23552 }else if( strcmp(z,"-json")==0 ){
23553 data.mode = MODE_Json;
23554 }else if( strcmp(z,"-markdown")==0 ){
23555 data.mode = MODE_Markdown;
23556 }else if( strcmp(z,"-table")==0 ){
23557 data.mode = MODE_Table;
23558 }else if( strcmp(z,"-box")==0 ){
23559 data.mode = MODE_Box;
23560 }else if( strcmp(z,"-csv")==0 ){
23561 data.mode = MODE_Csv;
23562 memcpy(data.colSeparator,",",2);
23563 #ifdef SQLITE_HAVE_ZLIB
23564 }else if( strcmp(z,"-zip")==0 ){
23565 data.openMode = SHELL_OPEN_ZIPFILE;
23566 #endif
23567 }else if( strcmp(z,"-append")==0 ){
23568 data.openMode = SHELL_OPEN_APPENDVFS;
23569 #ifndef SQLITE_OMIT_DESERIALIZE
23570 }else if( strcmp(z,"-deserialize")==0 ){
23571 data.openMode = SHELL_OPEN_DESERIALIZE;
23572 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
23573 data.szMax = integerValue(argv[++i]);
23574 #endif
23575 }else if( strcmp(z,"-readonly")==0 ){
23576 data.openMode = SHELL_OPEN_READONLY;
23577 }else if( strcmp(z,"-nofollow")==0 ){
23578 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
23579 }else if( strcmp(z,"-ascii")==0 ){
23580 data.mode = MODE_Ascii;
23581 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
23582 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
23583 }else if( strcmp(z,"-tabs")==0 ){
23584 data.mode = MODE_List;
23585 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
23586 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23587 }else if( strcmp(z,"-separator")==0 ){
23588 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
23589 "%s",cmdline_option_value(argc,argv,++i));
23590 }else if( strcmp(z,"-newline")==0 ){
23591 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
23592 "%s",cmdline_option_value(argc,argv,++i));
23593 }else if( strcmp(z,"-nullvalue")==0 ){
23594 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
23595 "%s",cmdline_option_value(argc,argv,++i));
23596 }else if( strcmp(z,"-header")==0 ){
23597 data.showHeader = 1;
23598 ShellSetFlag(&data, SHFLG_HeaderSet);
23599 }else if( strcmp(z,"-noheader")==0 ){
23600 data.showHeader = 0;
23601 ShellSetFlag(&data, SHFLG_HeaderSet);
23602 }else if( strcmp(z,"-echo")==0 ){
23603 ShellSetFlag(&data, SHFLG_Echo);
23604 }else if( strcmp(z,"-eqp")==0 ){
23605 data.autoEQP = AUTOEQP_on;
23606 }else if( strcmp(z,"-eqpfull")==0 ){
23607 data.autoEQP = AUTOEQP_full;
23608 }else if( strcmp(z,"-stats")==0 ){
23609 data.statsOn = 1;
23610 }else if( strcmp(z,"-scanstats")==0 ){
23611 data.scanstatsOn = 1;
23612 }else if( strcmp(z,"-backslash")==0 ){
23613 /* Undocumented command-line option: -backslash
23614 ** Causes C-style backslash escapes to be evaluated in SQL statements
23615 ** prior to sending the SQL into SQLite. Useful for injecting
23616 ** crazy bytes in the middle of SQL statements for testing and debugging.
23617 */
23618 ShellSetFlag(&data, SHFLG_Backslash);
23619 }else if( strcmp(z,"-bail")==0 ){
23620 /* No-op. The bail_on_error flag should already be set. */
23621 }else if( strcmp(z,"-version")==0 ){
23622 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
23623 return 0;
23624 }else if( strcmp(z,"-interactive")==0 ){
23625 stdin_is_interactive = 1;
23626 }else if( strcmp(z,"-batch")==0 ){
23627 stdin_is_interactive = 0;
23628 }else if( strcmp(z,"-heap")==0 ){
23629 i++;
23630 }else if( strcmp(z,"-pagecache")==0 ){
23631 i+=2;
23632 }else if( strcmp(z,"-lookaside")==0 ){
23633 i+=2;
23634 }else if( strcmp(z,"-threadsafe")==0 ){
23635 i+=2;
23636 }else if( strcmp(z,"-nonce")==0 ){
23637 i += 2;
23638 }else if( strcmp(z,"-mmap")==0 ){
23639 i++;
23640 }else if( strcmp(z,"-memtrace")==0 ){
23641 i++;
23642 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23643 }else if( strcmp(z,"-sorterref")==0 ){
23644 i++;
23645 #endif
23646 }else if( strcmp(z,"-vfs")==0 ){
23647 i++;
23648 #ifdef SQLITE_ENABLE_VFSTRACE
23649 }else if( strcmp(z,"-vfstrace")==0 ){
23650 i++;
23651 #endif
23652 #ifdef SQLITE_ENABLE_MULTIPLEX
23653 }else if( strcmp(z,"-multiplex")==0 ){
23654 i++;
23655 #endif
23656 }else if( strcmp(z,"-help")==0 ){
23657 usage(1);
23658 }else if( strcmp(z,"-cmd")==0 ){
23659 /* Run commands that follow -cmd first and separately from commands
23660 ** that simply appear on the command-line. This seems goofy. It would
23661 ** be better if all commands ran in the order that they appear. But
23662 ** we retain the goofy behavior for historical compatibility. */
23663 if( i==argc-1 ) break;
@@ -23675,11 +23708,11 @@
23675 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
23676 if( bail_on_error ) return rc;
23677 }
23678 }
23679 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23680 }else if( strncmp(z, "-A", 2)==0 ){
23681 if( nCmd>0 ){
23682 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
23683 " with \"%s\"\n", z);
23684 return 1;
23685 }
@@ -23691,11 +23724,11 @@
23691 arDotCommand(&data, 1, argv+i, argc-i);
23692 }
23693 readStdin = 0;
23694 break;
23695 #endif
23696 }else if( strcmp(z,"-safe")==0 ){
23697 data.bSafeMode = data.bSafeModePersist = 1;
23698 }else{
23699 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
23700 raw_printf(stderr,"Use -help for a list of options.\n");
23701 return 1;
@@ -23816,34 +23849,34 @@
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
23824 /* Only for emcc experimentation purposes.
23825
23826 Define this function in JS using:
23827
23828 emcc ... --js-library somefile.js
23829
23830 containing:
23831
23832 mergeInto(LibraryManager.library, {
23833 my_foo: function(){
23834 console.debug("my_foo()",arguments);
23835 }
23836 });
23837 */
23838 /*extern void my_foo(sqlite3 *);*/
23839 /* Only for emcc experimentation purposes. */
23840 sqlite3 * fiddle_the_db(){
23841 printf("fiddle_the_db(%p)\n", (const void*)globalDb);
23842 /*my_foo(globalDb);*/
23843 return globalDb;
 
23844 }
 
23845 /* Only for emcc experimentation purposes. */
23846 sqlite3 * fiddle_db_arg(sqlite3 *arg){
23847 printf("fiddle_db_arg(%p)\n", (const void*)arg);
23848 return arg;
23849 }
@@ -23853,11 +23886,11 @@
23853 ** SharedWorker() (which manages the wasm module) is performing work
23854 ** which should be interrupted. Unfortunately, SharedWorker is not
23855 ** portable enough to make real use of.
23856 */
23857 void fiddle_interrupt(void){
23858 if(globalDb) sqlite3_interrupt(globalDb);
23859 }
23860
23861 /*
23862 ** Returns the filename of the given db name, assuming "main" if
23863 ** zDbName is NULL. Returns NULL if globalDb is not opened.
@@ -23867,73 +23900,74 @@
23867 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
23868 : NULL;
23869 }
23870
23871 /*
23872 ** Closes, unlinks, and reopens the db using its current filename (or
23873 ** the default if the db is currently closed). It is assumed, for
23874 ** purposes of the fiddle build, that the file is in a transient
23875 ** virtual filesystem within the browser.
23876 */
23877 void fiddle_reset_db(void){
23878 char *zFilename = 0;
23879 if(0==globalDb){
23880 shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
23881 }else{
23882 zFilename =
23883 sqlite3_mprintf("%s", sqlite3_db_filename(globalDb, "main"));
23884 shell_check_oom(zFilename);
23885 close_db(globalDb);
23886 shellDeleteFile(zFilename);
23887 shellState.db = 0;
23888 shellState.pAuxDb->zDbFilename = zFilename;
23889 }
23890 open_db(&shellState, 0);
23891 sqlite3_free(zFilename);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23892 }
23893
23894 /*
23895 ** Trivial exportable function for emscripten. Needs to be exported using:
23896 **
23897 ** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
23898 **
23899 ** (Note the underscore before the function name.) It processes zSql
23900 ** as if it were input to the sqlite3 shell and redirects all output
23901 ** to the wasm binding.
23902 */
23903 void fiddle_exec(const char * zSql){
23904 static int once = 0;
23905 int rc = 0;
23906 if(!once){
23907 /* Simulate an argv array for main() */
23908 static char * argv[] = {"fiddle",
23909 "-bail",
23910 "-safe"};
23911 rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
23912 once = rc ? -1 : 1;
23913 memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23914 printf(
23915 "SQLite version %s %.19s\n" /*extra-version-info*/,
23916 sqlite3_libversion(), sqlite3_sourceid()
23917 );
23918 puts("WASM shell");
23919 puts("Enter \".help\" for usage hints.");
23920 if(once>0){
23921 fiddle_reset_db();
23922 }
23923 if(shellState.db){
23924 printf("Connected to %s.\n", fiddle_db_filename(NULL));
23925 }else{
23926 fprintf(stderr,"ERROR initializing db!\n");
23927 return;
23928 }
23929 }
23930 if(once<0){
23931 puts("DB init failed. Not executing SQL.");
23932 }else if(zSql && *zSql){
23933 shellState.wasm.zInput = zSql;
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
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -100,10 +100,18 @@
100 # ifndef _FILE_OFFSET_BITS
101 # define _FILE_OFFSET_BITS 64
102 # endif
103 # define _LARGEFILE_SOURCE 1
104 #endif
105
106 #if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
107 /*
108 ** emcc requires _POSIX_SOURCE (or one of several similar defines)
109 ** to expose strdup().
110 */
111 # define _POSIX_SOURCE
112 #endif
113
114 #include <stdlib.h>
115 #include <string.h>
116 #include <stdio.h>
117 #include <assert.h>
@@ -256,10 +264,22 @@
264 # define setTextMode(X,Y)
265 #endif
266
267 /* True if the timer is enabled */
268 static int enableTimer = 0;
269
270 /* A version of strcmp() that works with NULL values */
271 static int cli_strcmp(const char *a, const char *b){
272 if( a==0 ) a = "";
273 if( b==0 ) b = "";
274 return strcmp(a,b);
275 }
276 static int cli_strncmp(const char *a, const char *b, size_t n){
277 if( a==0 ) a = "";
278 if( b==0 ) b = "";
279 return strncmp(a,b,n);
280 }
281
282 /* Return the current wall-clock time */
283 static sqlite3_int64 timeOfDay(void){
284 static sqlite3_vfs *clockVfs = 0;
285 sqlite3_int64 t;
@@ -688,11 +708,11 @@
708 /* For interactive input on Windows systems, translate the
709 ** multi-byte characterset characters into UTF-8. */
710 if( stdin_is_interactive && in==stdin ){
711 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
712 if( zTrans ){
713 i64 nTrans = strlen(zTrans)+1;
714 if( nTrans>nLine ){
715 zLine = realloc(zLine, nTrans);
716 shell_check_oom(zLine);
717 }
718 memcpy(zLine, zTrans, nTrans);
@@ -824,13 +844,13 @@
844 **
845 ** If the third argument, quote, is not '\0', then it is used as a
846 ** quote character for zAppend.
847 */
848 static void appendText(ShellText *p, const char *zAppend, char quote){
849 i64 len;
850 i64 i;
851 i64 nAppend = strlen30(zAppend);
852
853 len = nAppend+p->n+1;
854 if( quote ){
855 len += 2;
856 for(i=0; i<nAppend; i++){
@@ -985,14 +1005,14 @@
1005 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1006 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1007 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1008 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1009 UNUSED_PARAMETER(nVal);
1010 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
1011 for(i=0; i<ArraySize(aPrefix); i++){
1012 int n = strlen30(aPrefix[i]);
1013 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1014 char *z = 0;
1015 char *zFake = 0;
1016 if( zSchema ){
1017 char cQuote = quoteChar(zSchema);
1018 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
@@ -12361,10 +12381,11 @@
12381 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12382 #ifdef SQLITE_SHELL_FIDDLE
12383 struct {
12384 const char * zInput; /* Input string from wasm/JS proxy */
12385 const char * zPos; /* Cursor pos into zInput */
12386 const char * zDefaultDbName; /* Default name for db file */
12387 } wasm;
12388 #endif
12389 };
12390
12391 #ifdef SQLITE_SHELL_FIDDLE
@@ -12873,13 +12894,13 @@
12894 }
12895
12896 /*
12897 ** Output the given string as a quoted according to JSON quoting rules.
12898 */
12899 static void output_json_string(FILE *out, const char *z, i64 n){
12900 unsigned int c;
12901 if( n<0 ) n = strlen(z);
12902 fputc('"', out);
12903 while( n-- ){
12904 c = *(z++);
12905 if( c=='\\' || c=='"' ){
12906 fputc('\\', out);
@@ -13178,11 +13199,13 @@
13199 /*
13200 ** Add a new entry to the EXPLAIN QUERY PLAN data
13201 */
13202 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
13203 EQPGraphRow *pNew;
13204 i64 nText;
13205 if( zText==0 ) return;
13206 nText = strlen(zText);
13207 if( p->autoEQPtest ){
13208 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13209 }
13210 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13211 shell_check_oom(pNew);
@@ -13223,18 +13246,18 @@
13246 /* Render a single level of the graph that has iEqpId as its parent. Called
13247 ** recursively to render sublevels.
13248 */
13249 static void eqp_render_level(ShellState *p, int iEqpId){
13250 EQPGraphRow *pRow, *pNext;
13251 i64 n = strlen(p->sGraph.zPrefix);
13252 char *z;
13253 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
13254 pNext = eqp_next_row(p, iEqpId, pRow);
13255 z = pRow->zText;
13256 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
13257 pNext ? "|--" : "`--", z);
13258 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
13259 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
13260 eqp_render_level(p, pRow->iEqpId);
13261 p->sGraph.zPrefix[n] = 0;
13262 }
13263 }
@@ -13946,11 +13969,11 @@
13969 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
13970 };
13971 int i;
13972 for(i=0; i<ArraySize(aTrans); i++){
13973 int n = strlen30(aTrans[i].zPattern);
13974 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
13975 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13976 break;
13977 }
13978 }
13979 }
@@ -14185,11 +14208,11 @@
14208 ** Otherwise, return zero.
14209 */
14210 static int str_in_array(const char *zStr, const char **azArray){
14211 int i;
14212 for(i=0; azArray[i]; i++){
14213 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
14214 }
14215 return 0;
14216 }
14217
14218 /*
@@ -14260,11 +14283,11 @@
14283 ** it is not */
14284 static const char *explainCols[] = {
14285 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
14286 int jj;
14287 for(jj=0; jj<ArraySize(explainCols); jj++){
14288 if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14289 p->cMode = p->mode;
14290 sqlite3_reset(pSql);
14291 return;
14292 }
14293 }
@@ -14984,14 +15007,14 @@
15007 for(i=1; rc==SQLITE_OK && i<nArg; i++){
15008 char *z = azArg[i];
15009 int n;
15010 if( z[0]=='-' && z[1]=='-' ) z++;
15011 n = strlen30(z);
15012 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
15013 pState->expert.bVerbose = 1;
15014 }
15015 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
15016 if( i==(nArg-1) ){
15017 raw_printf(stderr, "option requires an argument: %s\n", z);
15018 rc = SQLITE_ERROR;
15019 }else{
15020 iSample = (int)integerValue(azArg[++i]);
@@ -15335,22 +15358,24 @@
15358 UNUSED_PARAMETER(azNotUsed);
15359 if( nArg!=3 || azArg==0 ) return 0;
15360 zTable = azArg[0];
15361 zType = azArg[1];
15362 zSql = azArg[2];
15363 if( zTable==0 ) return 0;
15364 if( zType==0 ) return 0;
15365 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
15366 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
15367
15368 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
15369 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
15370 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
15371 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
15372 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
15373 return 0;
15374 }else if( dataOnly ){
15375 /* no-op */
15376 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
15377 char *zIns;
15378 if( !p->writableSchema ){
15379 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
15380 p->writableSchema = 1;
15381 }
@@ -15364,11 +15389,11 @@
15389 return 0;
15390 }else{
15391 printSchemaLine(p->out, zSql, ";\n");
15392 }
15393
15394 if( cli_strcmp(zType, "table")==0 ){
15395 ShellText sSelect;
15396 ShellText sTable;
15397 char **azCol;
15398 int i;
15399 char *savedDestTable;
@@ -15608,11 +15633,11 @@
15633 " insert SQL insert statements for TABLE",
15634 " json Results in a JSON array",
15635 " line One value per line",
15636 " list Values delimited by \"|\"",
15637 " markdown Markdown table format",
15638 " qbox Shorthand for \"box --wrap 60 --quote\"",
15639 " quote Escape answers as for SQL",
15640 " table ASCII-art table",
15641 " tabs Tab-separated values",
15642 " tcl TCL list elements",
15643 " OPTIONS: (for columnar modes or insert mode):",
@@ -15783,13 +15808,13 @@
15808 int j = 0;
15809 int n = 0;
15810 char *zPat;
15811 if( zPattern==0
15812 || zPattern[0]=='0'
15813 || cli_strcmp(zPattern,"-a")==0
15814 || cli_strcmp(zPattern,"-all")==0
15815 || cli_strcmp(zPattern,"--all")==0
15816 ){
15817 /* Show all commands, but only one line per command */
15818 if( zPattern==0 ) zPattern = "";
15819 for(i=0; i<ArraySize(azHelp); i++){
15820 if( azHelp[i][0]=='.' || zPattern[0] ){
@@ -16022,11 +16047,11 @@
16047 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
16048 if( rc==2 ){
16049 iOffset = k;
16050 continue;
16051 }
16052 if( cli_strncmp(zLine, "| end ", 6)==0 ){
16053 break;
16054 }
16055 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
16056 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
16057 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
@@ -16050,11 +16075,11 @@
16075 if( in!=p->in ){
16076 fclose(in);
16077 }else{
16078 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
16079 nLine++;
16080 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
16081 }
16082 p->lineno = nLine;
16083 }
16084 sqlite3_free(a);
16085 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
@@ -16142,32 +16167,32 @@
16167 sqlite3_value **argv
16168 ){
16169 const char *zText = (const char*)sqlite3_value_text(argv[0]);
16170 UNUSED_PARAMETER(argc);
16171 if( zText && zText[0]=='\'' ){
16172 i64 nText = sqlite3_value_bytes(argv[0]);
16173 i64 i;
16174 char zBuf1[20];
16175 char zBuf2[20];
16176 const char *zNL = 0;
16177 const char *zCR = 0;
16178 i64 nCR = 0;
16179 i64 nNL = 0;
16180
16181 for(i=0; zText[i]; i++){
16182 if( zNL==0 && zText[i]=='\n' ){
16183 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
16184 nNL = strlen(zNL);
16185 }
16186 if( zCR==0 && zText[i]=='\r' ){
16187 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
16188 nCR = strlen(zCR);
16189 }
16190 }
16191
16192 if( zNL || zCR ){
16193 i64 iOut = 0;
16194 i64 nMax = (nNL > nCR) ? nNL : nCR;
16195 i64 nAlloc = nMax * nText + (nMax+64)*2;
16196 char *zOut = (char*)sqlite3_malloc64(nAlloc);
16197 if( zOut==0 ){
16198 sqlite3_result_error_nomem(context);
@@ -16404,12 +16429,12 @@
16429 #elif HAVE_LINENOISE
16430 /*
16431 ** Linenoise completion callback
16432 */
16433 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
16434 i64 nLine = strlen(zLine);
16435 i64 i, iStart;
16436 sqlite3_stmt *pStmt = 0;
16437 char *zSql;
16438 char zBuf[1000];
16439
16440 if( nLine>sizeof(zBuf)-30 ) return;
@@ -16543,15 +16568,15 @@
16568 ** recognized and do the right thing. NULL is returned if the output
16569 ** filename is "off".
16570 */
16571 static FILE *output_file_open(const char *zFile, int bTextMode){
16572 FILE *f;
16573 if( cli_strcmp(zFile,"stdout")==0 ){
16574 f = stdout;
16575 }else if( cli_strcmp(zFile, "stderr")==0 ){
16576 f = stderr;
16577 }else if( cli_strcmp(zFile, "off")==0 ){
16578 f = 0;
16579 }else{
16580 f = fopen(zFile, bTextMode ? "w" : "wb");
16581 if( f==0 ){
16582 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
@@ -16571,11 +16596,11 @@
16596 void *pX /* Auxiliary output */
16597 ){
16598 ShellState *p = (ShellState*)pArg;
16599 sqlite3_stmt *pStmt;
16600 const char *zSql;
16601 i64 nSql;
16602 if( p->traceOut==0 ) return 0;
16603 if( mType==SQLITE_TRACE_CLOSE ){
16604 utf8_printf(p->traceOut, "-- closing database connection\n");
16605 return 0;
16606 }
@@ -16599,21 +16624,22 @@
16624 break;
16625 }
16626 }
16627 }
16628 if( zSql==0 ) return 0;
16629 nSql = strlen(zSql);
16630 if( nSql>1000000000 ) nSql = 1000000000;
16631 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
16632 switch( mType ){
16633 case SQLITE_TRACE_ROW:
16634 case SQLITE_TRACE_STMT: {
16635 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
16636 break;
16637 }
16638 case SQLITE_TRACE_PROFILE: {
16639 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16640 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
16641 break;
16642 }
16643 }
16644 return 0;
16645 }
@@ -17158,11 +17184,11 @@
17184 }
17185 raw_printf(p->out, "\n");
17186 }
17187 if( zDb==0 ){
17188 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
17189 }else if( cli_strcmp(zDb,"temp")==0 ){
17190 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
17191 }else{
17192 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
17193 }
17194 for(i=0; i<ArraySize(aQuery); i++){
@@ -17292,11 +17318,11 @@
17318 */
17319 static int optionMatch(const char *zStr, const char *zOpt){
17320 if( zStr[0]!='-' ) return 0;
17321 zStr++;
17322 if( zStr[0]=='-' ) zStr++;
17323 return cli_strcmp(zStr, zOpt)==0;
17324 }
17325
17326 /*
17327 ** Delete a file.
17328 */
@@ -19398,11 +19424,11 @@
19424 n = strlen30(azArg[0]);
19425 c = azArg[0][0];
19426 clearTempFile(p);
19427
19428 #ifndef SQLITE_OMIT_AUTHORIZATION
19429 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
19430 if( nArg!=2 ){
19431 raw_printf(stderr, "Usage: .auth ON|OFF\n");
19432 rc = 1;
19433 goto meta_command_exit;
19434 }
@@ -19417,20 +19443,20 @@
19443 }else
19444 #endif
19445
19446 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19447 && !defined(SQLITE_SHELL_FIDDLE)
19448 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
19449 open_db(p, 0);
19450 failIfSafeMode(p, "cannot run .archive in safe mode");
19451 rc = arDotCommand(p, 0, azArg, nArg);
19452 }else
19453 #endif
19454
19455 #ifndef SQLITE_SHELL_FIDDLE
19456 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
19457 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
19458 ){
19459 const char *zDestFile = 0;
19460 const char *zDb = 0;
19461 sqlite3 *pDest;
19462 sqlite3_backup *pBackup;
@@ -19440,14 +19466,14 @@
19466 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
19467 for(j=1; j<nArg; j++){
19468 const char *z = azArg[j];
19469 if( z[0]=='-' ){
19470 if( z[1]=='-' ) z++;
19471 if( cli_strcmp(z, "-append")==0 ){
19472 zVfs = "apndvfs";
19473 }else
19474 if( cli_strcmp(z, "-async")==0 ){
19475 bAsync = 1;
19476 }else
19477 {
19478 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
19479 return 1;
@@ -19495,20 +19521,20 @@
19521 }
19522 close_db(pDest);
19523 }else
19524 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19525
19526 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
19527 if( nArg==2 ){
19528 bail_on_error = booleanValue(azArg[1]);
19529 }else{
19530 raw_printf(stderr, "Usage: .bail on|off\n");
19531 rc = 1;
19532 }
19533 }else
19534
19535 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
19536 if( nArg==2 ){
19537 if( booleanValue(azArg[1]) ){
19538 setBinaryMode(p->out, 1);
19539 }else{
19540 setTextMode(p->out, 1);
@@ -19520,16 +19546,16 @@
19546 }else
19547
19548 /* The undocumented ".breakpoint" command causes a call to the no-op
19549 ** routine named test_breakpoint().
19550 */
19551 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
19552 test_breakpoint();
19553 }else
19554
19555 #ifndef SQLITE_SHELL_FIDDLE
19556 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
19557 failIfSafeMode(p, "cannot run .cd in safe mode");
19558 if( nArg==2 ){
19559 #if defined(_WIN32) || defined(WIN32)
19560 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
19561 rc = !SetCurrentDirectoryW(z);
@@ -19546,11 +19572,11 @@
19572 rc = 1;
19573 }
19574 }else
19575 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19576
19577 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
19578 if( nArg==2 ){
19579 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19580 }else{
19581 raw_printf(stderr, "Usage: .changes on|off\n");
19582 rc = 1;
@@ -19560,11 +19586,11 @@
19586 #ifndef SQLITE_SHELL_FIDDLE
19587 /* Cancel output redirection, if it is currently set (by .testcase)
19588 ** Then read the content of the testcase-out.txt file and compare against
19589 ** azArg[1]. If there are differences, report an error and exit.
19590 */
19591 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
19592 char *zRes = 0;
19593 output_reset(p);
19594 if( nArg!=2 ){
19595 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
19596 rc = 2;
@@ -19583,11 +19609,11 @@
19609 sqlite3_free(zRes);
19610 }else
19611 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19612
19613 #ifndef SQLITE_SHELL_FIDDLE
19614 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
19615 failIfSafeMode(p, "cannot run .clone in safe mode");
19616 if( nArg==2 ){
19617 tryToClone(p, azArg[1]);
19618 }else{
19619 raw_printf(stderr, "Usage: .clone FILENAME\n");
@@ -19594,11 +19620,11 @@
19620 rc = 1;
19621 }
19622 }else
19623 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
19624
19625 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
19626 if( nArg==1 ){
19627 /* List available connections */
19628 int i;
19629 for(i=0; i<ArraySize(p->aAuxDb); i++){
19630 const char *zFile = p->aAuxDb[i].zDbFilename;
@@ -19621,11 +19647,11 @@
19647 p->pAuxDb->db = p->db;
19648 p->pAuxDb = &p->aAuxDb[i];
19649 globalDb = p->db = p->pAuxDb->db;
19650 p->pAuxDb->db = 0;
19651 }
19652 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
19653 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
19654 int i = azArg[2][0] - '0';
19655 if( i<0 || i>=ArraySize(p->aAuxDb) ){
19656 /* No-op */
19657 }else if( p->pAuxDb == &p->aAuxDb[i] ){
@@ -19640,11 +19666,11 @@
19666 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
19667 rc = 1;
19668 }
19669 }else
19670
19671 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
19672 char **azName = 0;
19673 int nName = 0;
19674 sqlite3_stmt *pStmt;
19675 int i;
19676 open_db(p, 0);
@@ -19679,11 +19705,11 @@
19705 free(azName[i*2+1]);
19706 }
19707 sqlite3_free(azName);
19708 }else
19709
19710 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
19711 static const struct DbConfigChoices {
19712 const char *zName;
19713 int op;
19714 } aDbConfig[] = {
19715 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
@@ -19704,11 +19730,11 @@
19730 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
19731 };
19732 int ii, v;
19733 open_db(p, 0);
19734 for(ii=0; ii<ArraySize(aDbConfig); ii++){
19735 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
19736 if( nArg>=3 ){
19737 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
19738 }
19739 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
19740 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
@@ -19719,21 +19745,21 @@
19745 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
19746 }
19747 }else
19748
19749 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
19750 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
19751 rc = shell_dbinfo_command(p, nArg, azArg);
19752 }else
19753
19754 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
19755 open_db(p, 0);
19756 rc = recoverDatabaseCmd(p, nArg, azArg);
19757 }else
19758 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
19759
19760 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
19761 char *zLike = 0;
19762 char *zSql;
19763 int i;
19764 int savedShowHeader = p->showHeader;
19765 int savedShellFlags = p->shellFlgs;
@@ -19742,11 +19768,11 @@
19768 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
19769 for(i=1; i<nArg; i++){
19770 if( azArg[i][0]=='-' ){
19771 const char *z = azArg[i]+1;
19772 if( z[0]=='-' ) z++;
19773 if( cli_strcmp(z,"preserve-rowids")==0 ){
19774 #ifdef SQLITE_OMIT_VIRTUALTABLE
19775 raw_printf(stderr, "The --preserve-rowids option is not compatible"
19776 " with SQLITE_OMIT_VIRTUALTABLE\n");
19777 rc = 1;
19778 sqlite3_free(zLike);
@@ -19753,17 +19779,17 @@
19779 goto meta_command_exit;
19780 #else
19781 ShellSetFlag(p, SHFLG_PreserveRowid);
19782 #endif
19783 }else
19784 if( cli_strcmp(z,"newlines")==0 ){
19785 ShellSetFlag(p, SHFLG_Newlines);
19786 }else
19787 if( cli_strcmp(z,"data-only")==0 ){
19788 ShellSetFlag(p, SHFLG_DumpDataOnly);
19789 }else
19790 if( cli_strcmp(z,"nosys")==0 ){
19791 ShellSetFlag(p, SHFLG_DumpNoSys);
19792 }else
19793 {
19794 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
19795 rc = 1;
@@ -19841,35 +19867,35 @@
19867 }
19868 p->showHeader = savedShowHeader;
19869 p->shellFlgs = savedShellFlags;
19870 }else
19871
19872 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
19873 if( nArg==2 ){
19874 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
19875 }else{
19876 raw_printf(stderr, "Usage: .echo on|off\n");
19877 rc = 1;
19878 }
19879 }else
19880
19881 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
19882 if( nArg==2 ){
19883 p->autoEQPtest = 0;
19884 if( p->autoEQPtrace ){
19885 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
19886 p->autoEQPtrace = 0;
19887 }
19888 if( cli_strcmp(azArg[1],"full")==0 ){
19889 p->autoEQP = AUTOEQP_full;
19890 }else if( cli_strcmp(azArg[1],"trigger")==0 ){
19891 p->autoEQP = AUTOEQP_trigger;
19892 #ifdef SQLITE_DEBUG
19893 }else if( cli_strcmp(azArg[1],"test")==0 ){
19894 p->autoEQP = AUTOEQP_on;
19895 p->autoEQPtest = 1;
19896 }else if( cli_strcmp(azArg[1],"trace")==0 ){
19897 p->autoEQP = AUTOEQP_full;
19898 p->autoEQPtrace = 1;
19899 open_db(p, 0);
19900 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
19901 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
@@ -19882,22 +19908,22 @@
19908 rc = 1;
19909 }
19910 }else
19911
19912 #ifndef SQLITE_SHELL_FIDDLE
19913 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
19914 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19915 rc = 2;
19916 }else
19917 #endif
19918
19919 /* The ".explain" command is automatic now. It is largely pointless. It
19920 ** retained purely for backwards compatibility */
19921 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
19922 int val = 1;
19923 if( nArg>=2 ){
19924 if( cli_strcmp(azArg[1],"auto")==0 ){
19925 val = 99;
19926 }else{
19927 val = booleanValue(azArg[1]);
19928 }
19929 }
@@ -19913,11 +19939,11 @@
19939 p->autoExplain = 1;
19940 }
19941 }else
19942
19943 #ifndef SQLITE_OMIT_VIRTUALTABLE
19944 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
19945 if( p->bSafeMode ){
19946 raw_printf(stderr,
19947 "Cannot run experimental commands such as \"%s\" in safe mode\n",
19948 azArg[0]);
19949 rc = 1;
@@ -19926,11 +19952,11 @@
19952 expertDotCommand(p, azArg, nArg);
19953 }
19954 }else
19955 #endif
19956
19957 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
19958 static const struct {
19959 const char *zCtrlName; /* Name of a test-control option */
19960 int ctrlCode; /* Integer code for that option */
19961 const char *zUsage; /* Usage notes */
19962 } aCtrl[] = {
@@ -19956,11 +19982,11 @@
19982
19983 open_db(p, 0);
19984 zCmd = nArg>=2 ? azArg[1] : "help";
19985
19986 if( zCmd[0]=='-'
19987 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
19988 && nArg>=4
19989 ){
19990 zSchema = azArg[2];
19991 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
19992 nArg -= 2;
@@ -19972,11 +19998,11 @@
19998 zCmd++;
19999 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
20000 }
20001
20002 /* --help lists all file-controls */
20003 if( cli_strcmp(zCmd,"help")==0 ){
20004 utf8_printf(p->out, "Available file-controls:\n");
20005 for(i=0; i<ArraySize(aCtrl); i++){
20006 utf8_printf(p->out, " .filectrl %s %s\n",
20007 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
20008 }
@@ -19986,11 +20012,11 @@
20012
20013 /* convert filectrl text option to value. allow any unique prefix
20014 ** of the option name, or a numerical value. */
20015 n2 = strlen30(zCmd);
20016 for(i=0; i<ArraySize(aCtrl); i++){
20017 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20018 if( filectrl<0 ){
20019 filectrl = aCtrl[i].ctrlCode;
20020 iCtrl = i;
20021 }else{
20022 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
@@ -20073,11 +20099,11 @@
20099 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
20100 raw_printf(p->out, "%s\n", zBuf);
20101 }
20102 }else
20103
20104 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
20105 ShellState data;
20106 int doStats = 0;
20107 memcpy(&data, p, sizeof(data));
20108 data.showHeader = 0;
20109 data.cMode = data.mode = MODE_Semi;
@@ -20120,21 +20146,21 @@
20146 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
20147 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
20148 }
20149 }else
20150
20151 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
20152 if( nArg==2 ){
20153 p->showHeader = booleanValue(azArg[1]);
20154 p->shellFlgs |= SHFLG_HeaderSet;
20155 }else{
20156 raw_printf(stderr, "Usage: .headers on|off\n");
20157 rc = 1;
20158 }
20159 }else
20160
20161 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
20162 if( nArg>=2 ){
20163 n = showHelp(p->out, azArg[1]);
20164 if( n==0 ){
20165 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
20166 }
@@ -20142,11 +20168,11 @@
20168 showHelp(p->out, 0);
20169 }
20170 }else
20171
20172 #ifndef SQLITE_SHELL_FIDDLE
20173 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
20174 char *zTable = 0; /* Insert data into this table */
20175 char *zSchema = 0; /* within this schema (may default to "main") */
20176 char *zFile = 0; /* Name of file to extra content from */
20177 sqlite3_stmt *pStmt = NULL; /* A statement */
20178 int nCol; /* Number of columns in the table */
@@ -20182,22 +20208,22 @@
20208 }else{
20209 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
20210 showHelp(p->out, "import");
20211 goto meta_command_exit;
20212 }
20213 }else if( cli_strcmp(z,"-v")==0 ){
20214 eVerbose++;
20215 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
20216 zSchema = azArg[++i];
20217 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
20218 nSkip = integerValue(azArg[++i]);
20219 }else if( cli_strcmp(z,"-ascii")==0 ){
20220 sCtx.cColSep = SEP_Unit[0];
20221 sCtx.cRowSep = SEP_Record[0];
20222 xRead = ascii_read_one_field;
20223 useOutputMode = 0;
20224 }else if( cli_strcmp(z,"-csv")==0 ){
20225 sCtx.cColSep = ',';
20226 sCtx.cRowSep = '\n';
20227 xRead = csv_read_one_field;
20228 useOutputMode = 0;
20229 }else{
@@ -20233,11 +20259,13 @@
20259 if( nSep==0 ){
20260 raw_printf(stderr,
20261 "Error: non-null row separator required for import\n");
20262 goto meta_command_exit;
20263 }
20264 if( nSep==2 && p->mode==MODE_Csv
20265 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
20266 ){
20267 /* When importing CSV (only), if the row separator is set to the
20268 ** default output row separator, change it to the default input
20269 ** row separator. This avoids having to maintain different input
20270 ** and output row separators. */
20271 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -20435,11 +20463,11 @@
20463 }
20464 }else
20465 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
20466
20467 #ifndef SQLITE_UNTESTABLE
20468 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
20469 char *zSql;
20470 char *zCollist = 0;
20471 sqlite3_stmt *pStmt;
20472 int tnum = 0;
20473 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
@@ -20536,17 +20564,17 @@
20564 sqlite3_free(zSql);
20565 }else
20566 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
20567
20568 #ifdef SQLITE_ENABLE_IOTRACE
20569 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
20570 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
20571 if( iotrace && iotrace!=stdout ) fclose(iotrace);
20572 iotrace = 0;
20573 if( nArg<2 ){
20574 sqlite3IoTrace = 0;
20575 }else if( cli_strcmp(azArg[1], "-")==0 ){
20576 sqlite3IoTrace = iotracePrintf;
20577 iotrace = stdout;
20578 }else{
20579 iotrace = fopen(azArg[1], "w");
20580 if( iotrace==0 ){
@@ -20558,11 +20586,11 @@
20586 }
20587 }
20588 }else
20589 #endif
20590
20591 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
20592 static const struct {
20593 const char *zLimitName; /* Name of a limit */
20594 int limitCode; /* Integer code for that limit */
20595 } aLimit[] = {
20596 { "length", SQLITE_LIMIT_LENGTH },
@@ -20617,17 +20645,17 @@
20645 printf("%20s %d\n", aLimit[iLimit].zLimitName,
20646 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
20647 }
20648 }else
20649
20650 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
20651 open_db(p, 0);
20652 lintDotCommand(p, azArg, nArg);
20653 }else
20654
20655 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
20656 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
20657 const char *zFile, *zProc;
20658 char *zErrMsg = 0;
20659 failIfSafeMode(p, "cannot run .load in safe mode");
20660 if( nArg<2 ){
20661 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
@@ -20645,11 +20673,11 @@
20673 }
20674 }else
20675 #endif
20676
20677 #ifndef SQLITE_SHELL_FIDDLE
20678 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
20679 failIfSafeMode(p, "cannot run .log in safe mode");
20680 if( nArg!=2 ){
20681 raw_printf(stderr, "Usage: .log FILENAME\n");
20682 rc = 1;
20683 }else{
@@ -20658,11 +20686,11 @@
20686 p->pLog = output_file_open(zFile, 0);
20687 }
20688 }else
20689 #endif
20690
20691 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
20692 const char *zMode = 0;
20693 const char *zTabname = 0;
20694 int i, n2;
20695 ColModeOpts cmOpts = ColModeOpts_default;
20696 for(i=1; i<nArg; i++){
@@ -20677,14 +20705,14 @@
20705 cmOpts.bQuote = 1;
20706 }else if( optionMatch(z,"noquote") ){
20707 cmOpts.bQuote = 0;
20708 }else if( zMode==0 ){
20709 zMode = z;
20710 /* Apply defaults for qbox pseudo-mode. If that
20711 * overwrites already-set values, user was informed of this.
20712 */
20713 if( cli_strcmp(z, "qbox")==0 ){
20714 ColModeOpts cmo = ColModeOpts_default_qbox;
20715 zMode = "box";
20716 cmOpts = cmo;
20717 }
20718 }else if( zTabname==0 ){
@@ -20719,62 +20747,62 @@
20747 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20748 }
20749 zMode = modeDescr[p->mode];
20750 }
20751 n2 = strlen30(zMode);
20752 if( cli_strncmp(zMode,"lines",n2)==0 ){
20753 p->mode = MODE_Line;
20754 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20755 }else if( cli_strncmp(zMode,"columns",n2)==0 ){
20756 p->mode = MODE_Column;
20757 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
20758 p->showHeader = 1;
20759 }
20760 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20761 p->cmOpts = cmOpts;
20762 }else if( cli_strncmp(zMode,"list",n2)==0 ){
20763 p->mode = MODE_List;
20764 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
20765 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20766 }else if( cli_strncmp(zMode,"html",n2)==0 ){
20767 p->mode = MODE_Html;
20768 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
20769 p->mode = MODE_Tcl;
20770 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
20771 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20772 }else if( cli_strncmp(zMode,"csv",n2)==0 ){
20773 p->mode = MODE_Csv;
20774 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20775 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20776 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
20777 p->mode = MODE_List;
20778 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20779 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
20780 p->mode = MODE_Insert;
20781 set_table_name(p, zTabname ? zTabname : "table");
20782 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
20783 p->mode = MODE_Quote;
20784 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20785 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20786 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
20787 p->mode = MODE_Ascii;
20788 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
20789 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20790 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
20791 p->mode = MODE_Markdown;
20792 p->cmOpts = cmOpts;
20793 }else if( cli_strncmp(zMode,"table",n2)==0 ){
20794 p->mode = MODE_Table;
20795 p->cmOpts = cmOpts;
20796 }else if( cli_strncmp(zMode,"box",n2)==0 ){
20797 p->mode = MODE_Box;
20798 p->cmOpts = cmOpts;
20799 }else if( cli_strncmp(zMode,"count",n2)==0 ){
20800 p->mode = MODE_Count;
20801 }else if( cli_strncmp(zMode,"off",n2)==0 ){
20802 p->mode = MODE_Off;
20803 }else if( cli_strncmp(zMode,"json",n2)==0 ){
20804 p->mode = MODE_Json;
20805 }else{
20806 raw_printf(stderr, "Error: mode should be one of: "
20807 "ascii box column csv html insert json line list markdown "
20808 "qbox quote table tabs tcl\n");
@@ -20782,15 +20810,15 @@
20810 }
20811 p->cMode = p->mode;
20812 }else
20813
20814 #ifndef SQLITE_SHELL_FIDDLE
20815 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
20816 if( nArg!=2 ){
20817 raw_printf(stderr, "Usage: .nonce NONCE\n");
20818 rc = 1;
20819 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
20820 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
20821 p->lineno, azArg[1]);
20822 exit(1);
20823 }else{
20824 p->bSafeMode = 0;
@@ -20798,21 +20826,21 @@
20826 ** at the end of this procedure */
20827 }
20828 }else
20829 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
20830
20831 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
20832 if( nArg==2 ){
20833 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20834 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
20835 }else{
20836 raw_printf(stderr, "Usage: .nullvalue STRING\n");
20837 rc = 1;
20838 }
20839 }else
20840
20841 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
20842 const char *zFN = 0; /* Pointer to constant filename */
20843 char *zNewFilename = 0; /* Name of the database file to open */
20844 int iName = 1; /* Index in azArg[] of the filename */
20845 int newFlag = 0; /* True to delete file before opening */
20846 int openMode = SHELL_OPEN_UNSPEC;
@@ -20872,11 +20900,11 @@
20900 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20901 #ifndef SQLITE_SHELL_FIDDLE
20902 if( p->bSafeMode
20903 && p->openMode!=SHELL_OPEN_HEXDB
20904 && zFN
20905 && cli_strcmp(zFN,":memory:")!=0
20906 ){
20907 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20908 }
20909 #else
20910 /* WASM mode has its own sandboxed pseudo-filesystem. */
@@ -20903,12 +20931,13 @@
20931 }
20932 }else
20933
20934 #ifndef SQLITE_SHELL_FIDDLE
20935 if( (c=='o'
20936 && (cli_strncmp(azArg[0], "output", n)==0
20937 || cli_strncmp(azArg[0], "once", n)==0))
20938 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
20939 ){
20940 char *zFile = 0;
20941 int bTxtMode = 0;
20942 int i;
20943 int eMode = 0;
@@ -20918,25 +20947,25 @@
20947 zBOM[0] = 0;
20948 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
20949 if( c=='e' ){
20950 eMode = 'x';
20951 bOnce = 2;
20952 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
20953 bOnce = 1;
20954 }
20955 for(i=1; i<nArg; i++){
20956 char *z = azArg[i];
20957 if( z[0]=='-' ){
20958 if( z[1]=='-' ) z++;
20959 if( cli_strcmp(z,"-bom")==0 ){
20960 zBOM[0] = 0xef;
20961 zBOM[1] = 0xbb;
20962 zBOM[2] = 0xbf;
20963 zBOM[3] = 0;
20964 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
20965 eMode = 'x'; /* spreadsheet */
20966 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
20967 eMode = 'e'; /* text editor */
20968 }else{
20969 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
20970 azArg[i]);
20971 showHelp(p->out, azArg[0]);
@@ -21005,11 +21034,11 @@
21034 }
21035 #endif
21036 }else{
21037 p->out = output_file_open(zFile, bTxtMode);
21038 if( p->out==0 ){
21039 if( cli_strcmp(zFile,"off")!=0 ){
21040 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
21041 }
21042 p->out = stdout;
21043 rc = 1;
21044 } else {
@@ -21019,26 +21048,26 @@
21048 }
21049 sqlite3_free(zFile);
21050 }else
21051 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21052
21053 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
21054 open_db(p,0);
21055 if( nArg<=1 ) goto parameter_syntax_error;
21056
21057 /* .parameter clear
21058 ** Clear all bind parameters by dropping the TEMP table that holds them.
21059 */
21060 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
21061 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
21062 0, 0, 0);
21063 }else
21064
21065 /* .parameter list
21066 ** List all bind parameters.
21067 */
21068 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
21069 sqlite3_stmt *pStmt = 0;
21070 int rx;
21071 int len = 0;
21072 rx = sqlite3_prepare_v2(p->db,
21073 "SELECT max(length(key)) "
@@ -21063,21 +21092,21 @@
21092
21093 /* .parameter init
21094 ** Make sure the TEMP table used to hold bind parameters exists.
21095 ** Create it if necessary.
21096 */
21097 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
21098 bind_table_init(p);
21099 }else
21100
21101 /* .parameter set NAME VALUE
21102 ** Set or reset a bind parameter. NAME should be the full parameter
21103 ** name exactly as it appears in the query. (ex: $abc, @def). The
21104 ** VALUE can be in either SQL literal notation, or if not it will be
21105 ** understood to be a text string.
21106 */
21107 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
21108 int rx;
21109 char *zSql;
21110 sqlite3_stmt *pStmt;
21111 const char *zKey = azArg[2];
21112 const char *zValue = azArg[3];
@@ -21111,11 +21140,11 @@
21140
21141 /* .parameter unset NAME
21142 ** Remove the NAME binding from the parameter binding table, if it
21143 ** exists.
21144 */
21145 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
21146 char *zSql = sqlite3_mprintf(
21147 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
21148 shell_check_oom(zSql);
21149 sqlite3_exec(p->db, zSql, 0, 0, 0);
21150 sqlite3_free(zSql);
@@ -21123,21 +21152,21 @@
21152 /* If no command name matches, show a syntax error */
21153 parameter_syntax_error:
21154 showHelp(p->out, "parameter");
21155 }else
21156
21157 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
21158 int i;
21159 for(i=1; i<nArg; i++){
21160 if( i>1 ) raw_printf(p->out, " ");
21161 utf8_printf(p->out, "%s", azArg[i]);
21162 }
21163 raw_printf(p->out, "\n");
21164 }else
21165
21166 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
21167 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
21168 int i;
21169 int nn = 0;
21170 p->flgProgress = 0;
21171 p->mxProgress = 0;
21172 p->nProgress = 0;
@@ -21144,23 +21173,23 @@
21173 for(i=1; i<nArg; i++){
21174 const char *z = azArg[i];
21175 if( z[0]=='-' ){
21176 z++;
21177 if( z[0]=='-' ) z++;
21178 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
21179 p->flgProgress |= SHELL_PROGRESS_QUIET;
21180 continue;
21181 }
21182 if( cli_strcmp(z,"reset")==0 ){
21183 p->flgProgress |= SHELL_PROGRESS_RESET;
21184 continue;
21185 }
21186 if( cli_strcmp(z,"once")==0 ){
21187 p->flgProgress |= SHELL_PROGRESS_ONCE;
21188 continue;
21189 }
21190 if( cli_strcmp(z,"limit")==0 ){
21191 if( i+1>=nArg ){
21192 utf8_printf(stderr, "Error: missing argument on --limit\n");
21193 rc = 1;
21194 goto meta_command_exit;
21195 }else{
@@ -21178,27 +21207,27 @@
21207 open_db(p, 0);
21208 sqlite3_progress_handler(p->db, nn, progress_handler, p);
21209 }else
21210 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
21211
21212 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
21213 if( nArg >= 2) {
21214 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
21215 }
21216 if( nArg >= 3) {
21217 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
21218 }
21219 }else
21220
21221 #ifndef SQLITE_SHELL_FIDDLE
21222 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
21223 rc = 2;
21224 }else
21225 #endif
21226
21227 #ifndef SQLITE_SHELL_FIDDLE
21228 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
21229 FILE *inSaved = p->in;
21230 int savedLineno = p->lineno;
21231 failIfSafeMode(p, "cannot run .read in safe mode");
21232 if( nArg!=2 ){
21233 raw_printf(stderr, "Usage: .read FILE\n");
@@ -21231,11 +21260,11 @@
21260 p->lineno = savedLineno;
21261 }else
21262 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21263
21264 #ifndef SQLITE_SHELL_FIDDLE
21265 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
21266 const char *zSrcFile;
21267 const char *zDb;
21268 sqlite3 *pSrc;
21269 sqlite3_backup *pBackup;
21270 int nTimeout = 0;
@@ -21284,11 +21313,11 @@
21313 }
21314 close_db(pSrc);
21315 }else
21316 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
21317
21318 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
21319 if( nArg==2 ){
21320 p->scanstatsOn = (u8)booleanValue(azArg[1]);
21321 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
21322 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
21323 #endif
@@ -21296,11 +21325,11 @@
21325 raw_printf(stderr, "Usage: .scanstats on|off\n");
21326 rc = 1;
21327 }
21328 }else
21329
21330 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
21331 ShellText sSelect;
21332 ShellState data;
21333 char *zErrMsg = 0;
21334 const char *zDiv = "(";
21335 const char *zName = 0;
@@ -21439,19 +21468,19 @@
21468 }else{
21469 rc = 0;
21470 }
21471 }else
21472
21473 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
21474 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
21475 ){
21476 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21477 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
21478 }else
21479
21480 #if defined(SQLITE_ENABLE_SESSION)
21481 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
21482 struct AuxDb *pAuxDb = p->pAuxDb;
21483 OpenSession *pSession = &pAuxDb->aSession[0];
21484 char **azCmd = &azArg[1];
21485 int iSes = 0;
21486 int nCmd = nArg - 1;
@@ -21458,11 +21487,11 @@
21487 int i;
21488 if( nArg<=1 ) goto session_syntax_error;
21489 open_db(p, 0);
21490 if( nArg>=3 ){
21491 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
21492 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
21493 }
21494 if( iSes<pAuxDb->nSession ){
21495 pSession = &pAuxDb->aSession[iSes];
21496 azCmd++;
21497 nCmd--;
@@ -21474,11 +21503,11 @@
21503
21504 /* .session attach TABLE
21505 ** Invoke the sqlite3session_attach() interface to attach a particular
21506 ** table so that it is never filtered.
21507 */
21508 if( cli_strcmp(azCmd[0],"attach")==0 ){
21509 if( nCmd!=2 ) goto session_syntax_error;
21510 if( pSession->p==0 ){
21511 session_not_open:
21512 raw_printf(stderr, "ERROR: No sessions are open\n");
21513 }else{
@@ -21492,11 +21521,13 @@
21521
21522 /* .session changeset FILE
21523 ** .session patchset FILE
21524 ** Write a changeset or patchset into a file. The file is overwritten.
21525 */
21526 if( cli_strcmp(azCmd[0],"changeset")==0
21527 || cli_strcmp(azCmd[0],"patchset")==0
21528 ){
21529 FILE *out = 0;
21530 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
21531 if( nCmd!=2 ) goto session_syntax_error;
21532 if( pSession->p==0 ) goto session_not_open;
21533 out = fopen(azCmd[1], "wb");
@@ -21526,11 +21557,11 @@
21557 }else
21558
21559 /* .session close
21560 ** Close the identified session
21561 */
21562 if( cli_strcmp(azCmd[0], "close")==0 ){
21563 if( nCmd!=1 ) goto session_syntax_error;
21564 if( pAuxDb->nSession ){
21565 session_close(pSession);
21566 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
21567 }
@@ -21537,11 +21568,11 @@
21568 }else
21569
21570 /* .session enable ?BOOLEAN?
21571 ** Query or set the enable flag
21572 */
21573 if( cli_strcmp(azCmd[0], "enable")==0 ){
21574 int ii;
21575 if( nCmd>2 ) goto session_syntax_error;
21576 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21577 if( pAuxDb->nSession ){
21578 ii = sqlite3session_enable(pSession->p, ii);
@@ -21551,11 +21582,11 @@
21582 }else
21583
21584 /* .session filter GLOB ....
21585 ** Set a list of GLOB patterns of table names to be excluded.
21586 */
21587 if( cli_strcmp(azCmd[0], "filter")==0 ){
21588 int ii, nByte;
21589 if( nCmd<2 ) goto session_syntax_error;
21590 if( pAuxDb->nSession ){
21591 for(ii=0; ii<pSession->nFilter; ii++){
21592 sqlite3_free(pSession->azFilter[ii]);
@@ -21576,11 +21607,11 @@
21607 }else
21608
21609 /* .session indirect ?BOOLEAN?
21610 ** Query or set the indirect flag
21611 */
21612 if( cli_strcmp(azCmd[0], "indirect")==0 ){
21613 int ii;
21614 if( nCmd>2 ) goto session_syntax_error;
21615 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
21616 if( pAuxDb->nSession ){
21617 ii = sqlite3session_indirect(pSession->p, ii);
@@ -21590,11 +21621,11 @@
21621 }else
21622
21623 /* .session isempty
21624 ** Determine if the session is empty
21625 */
21626 if( cli_strcmp(azCmd[0], "isempty")==0 ){
21627 int ii;
21628 if( nCmd!=1 ) goto session_syntax_error;
21629 if( pAuxDb->nSession ){
21630 ii = sqlite3session_isempty(pSession->p);
21631 utf8_printf(p->out, "session %s isempty flag = %d\n",
@@ -21603,27 +21634,27 @@
21634 }else
21635
21636 /* .session list
21637 ** List all currently open sessions
21638 */
21639 if( cli_strcmp(azCmd[0],"list")==0 ){
21640 for(i=0; i<pAuxDb->nSession; i++){
21641 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
21642 }
21643 }else
21644
21645 /* .session open DB NAME
21646 ** Open a new session called NAME on the attached database DB.
21647 ** DB is normally "main".
21648 */
21649 if( cli_strcmp(azCmd[0],"open")==0 ){
21650 char *zName;
21651 if( nCmd!=3 ) goto session_syntax_error;
21652 zName = azCmd[2];
21653 if( zName[0]==0 ) goto session_syntax_error;
21654 for(i=0; i<pAuxDb->nSession; i++){
21655 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
21656 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
21657 goto meta_command_exit;
21658 }
21659 }
21660 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
@@ -21650,19 +21681,19 @@
21681 #endif
21682
21683 #ifdef SQLITE_DEBUG
21684 /* Undocumented commands for internal testing. Subject to change
21685 ** without notice. */
21686 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
21687 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
21688 int i, v;
21689 for(i=1; i<nArg; i++){
21690 v = booleanValue(azArg[i]);
21691 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
21692 }
21693 }
21694 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
21695 int i; sqlite3_int64 v;
21696 for(i=1; i<nArg; i++){
21697 char zBuf[200];
21698 v = integerValue(azArg[i]);
21699 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
@@ -21670,11 +21701,11 @@
21701 }
21702 }
21703 }else
21704 #endif
21705
21706 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
21707 int bIsInit = 0; /* True to initialize the SELFTEST table */
21708 int bVerbose = 0; /* Verbose output */
21709 int bSelftestExists; /* True if SELFTEST already exists */
21710 int i, k; /* Loop counters */
21711 int nTest = 0; /* Number of tests runs */
@@ -21684,14 +21715,14 @@
21715
21716 open_db(p,0);
21717 for(i=1; i<nArg; i++){
21718 const char *z = azArg[i];
21719 if( z[0]=='-' && z[1]=='-' ) z++;
21720 if( cli_strcmp(z,"-init")==0 ){
21721 bIsInit = 1;
21722 }else
21723 if( cli_strcmp(z,"-v")==0 ){
21724 bVerbose++;
21725 }else
21726 {
21727 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21728 azArg[i], azArg[0]);
@@ -21740,14 +21771,14 @@
21771 if( zAns==0 ) continue;
21772 k = 0;
21773 if( bVerbose>0 ){
21774 printf("%d: %s %s\n", tno, zOp, zSql);
21775 }
21776 if( cli_strcmp(zOp,"memo")==0 ){
21777 utf8_printf(p->out, "%s\n", zSql);
21778 }else
21779 if( cli_strcmp(zOp,"run")==0 ){
21780 char *zErrMsg = 0;
21781 str.n = 0;
21782 str.z[0] = 0;
21783 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
21784 nTest++;
@@ -21757,11 +21788,11 @@
21788 if( rc || zErrMsg ){
21789 nErr++;
21790 rc = 1;
21791 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
21792 sqlite3_free(zErrMsg);
21793 }else if( cli_strcmp(zAns,str.z)!=0 ){
21794 nErr++;
21795 rc = 1;
21796 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
21797 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
21798 }
@@ -21777,11 +21808,11 @@
21808 } /* End loop over k */
21809 freeText(&str);
21810 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
21811 }else
21812
21813 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
21814 if( nArg<2 || nArg>3 ){
21815 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
21816 rc = 1;
21817 }
21818 if( nArg>=2 ){
@@ -21792,11 +21823,11 @@
21823 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
21824 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
21825 }
21826 }else
21827
21828 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
21829 const char *zLike = 0; /* Which table to checksum. 0 means everything */
21830 int i; /* Loop counter */
21831 int bSchema = 0; /* Also hash the schema */
21832 int bSeparate = 0; /* Hash each table separately */
21833 int iSize = 224; /* Hash algorithm to use */
@@ -21810,19 +21841,19 @@
21841 for(i=1; i<nArg; i++){
21842 const char *z = azArg[i];
21843 if( z[0]=='-' ){
21844 z++;
21845 if( z[0]=='-' ) z++;
21846 if( cli_strcmp(z,"schema")==0 ){
21847 bSchema = 1;
21848 }else
21849 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
21850 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
21851 ){
21852 iSize = atoi(&z[5]);
21853 }else
21854 if( cli_strcmp(z,"debug")==0 ){
21855 bDebug = 1;
21856 }else
21857 {
21858 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
21859 azArg[i], azArg[0]);
@@ -21858,24 +21889,24 @@
21889 zSep = "VALUES(";
21890 while( SQLITE_ROW==sqlite3_step(pStmt) ){
21891 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21892 if( zTab==0 ) continue;
21893 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21894 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
21895 appendText(&sQuery,"SELECT * FROM ", 0);
21896 appendText(&sQuery,zTab,'"');
21897 appendText(&sQuery," NOT INDEXED;", 0);
21898 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
21899 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
21900 " ORDER BY name;", 0);
21901 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
21902 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
21903 " ORDER BY name;", 0);
21904 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
21905 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
21906 " ORDER BY tbl,idx;", 0);
21907 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
21908 appendText(&sQuery, "SELECT * FROM ", 0);
21909 appendText(&sQuery, zTab, 0);
21910 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
21911 }
21912 appendText(&sSql, zSep, 0);
@@ -21910,11 +21941,12 @@
21941 sqlite3_free(zSql);
21942 }else
21943
21944 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
21945 if( c=='s'
21946 && (cli_strncmp(azArg[0], "shell", n)==0
21947 || cli_strncmp(azArg[0],"system",n)==0)
21948 ){
21949 char *zCmd;
21950 int i, x;
21951 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
21952 if( nArg<2 ){
@@ -21931,11 +21963,11 @@
21963 sqlite3_free(zCmd);
21964 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21965 }else
21966 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
21967
21968 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
21969 static const char *azBool[] = { "off", "on", "trigger", "full"};
21970 const char *zOut;
21971 int i;
21972 if( nArg!=1 ){
21973 raw_printf(stderr, "Usage: .show\n");
@@ -21984,15 +22016,15 @@
22016 raw_printf(p->out, "\n");
22017 utf8_printf(p->out, "%12.12s: %s\n", "filename",
22018 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
22019 }else
22020
22021 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
22022 if( nArg==2 ){
22023 if( cli_strcmp(azArg[1],"stmt")==0 ){
22024 p->statsOn = 2;
22025 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
22026 p->statsOn = 3;
22027 }else{
22028 p->statsOn = (u8)booleanValue(azArg[1]);
22029 }
22030 }else if( nArg==1 ){
@@ -22001,13 +22033,13 @@
22033 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
22034 rc = 1;
22035 }
22036 }else
22037
22038 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
22039 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
22040 || cli_strncmp(azArg[0], "indexes", n)==0) )
22041 ){
22042 sqlite3_stmt *pStmt;
22043 char **azResult;
22044 int nRow, nAlloc;
22045 int ii;
@@ -22111,11 +22143,11 @@
22143 sqlite3_free(azResult);
22144 }else
22145
22146 #ifndef SQLITE_SHELL_FIDDLE
22147 /* Begin redirecting output to the file "testcase-out.txt" */
22148 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
22149 output_reset(p);
22150 p->out = output_file_open("testcase-out.txt", 0);
22151 if( p->out==0 ){
22152 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
22153 }
@@ -22126,11 +22158,11 @@
22158 }
22159 }else
22160 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
22161
22162 #ifndef SQLITE_UNTESTABLE
22163 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
22164 static const struct {
22165 const char *zCtrlName; /* Name of a test-control option */
22166 int ctrlCode; /* Integer code for that option */
22167 int unSafe; /* Not valid for --safe mode */
22168 const char *zUsage; /* Usage notes */
@@ -22173,11 +22205,11 @@
22205 zCmd++;
22206 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
22207 }
22208
22209 /* --help lists all test-controls */
22210 if( cli_strcmp(zCmd,"help")==0 ){
22211 utf8_printf(p->out, "Available test-controls:\n");
22212 for(i=0; i<ArraySize(aCtrl); i++){
22213 utf8_printf(p->out, " .testctrl %s %s\n",
22214 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
22215 }
@@ -22187,11 +22219,11 @@
22219
22220 /* convert testctrl text option to value. allow any unique prefix
22221 ** of the option name, or a numerical value. */
22222 n2 = strlen30(zCmd);
22223 for(i=0; i<ArraySize(aCtrl); i++){
22224 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
22225 if( testctrl<0 ){
22226 testctrl = aCtrl[i].ctrlCode;
22227 iCtrl = i;
22228 }else{
22229 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
@@ -22243,11 +22275,11 @@
22275 /* sqlite3_test_control(int, int, sqlite3*) */
22276 case SQLITE_TESTCTRL_PRNG_SEED:
22277 if( nArg==3 || nArg==4 ){
22278 int ii = (int)integerValue(azArg[2]);
22279 sqlite3 *db;
22280 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
22281 sqlite3_randomness(sizeof(ii),&ii);
22282 printf("-- random seed: %d\n", ii);
22283 }
22284 if( nArg==3 ){
22285 db = 0;
@@ -22359,16 +22391,16 @@
22391 raw_printf(p->out, "0x%08x\n", rc2);
22392 }
22393 }else
22394 #endif /* !defined(SQLITE_UNTESTABLE) */
22395
22396 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
22397 open_db(p, 0);
22398 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
22399 }else
22400
22401 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
22402 if( nArg==2 ){
22403 enableTimer = booleanValue(azArg[1]);
22404 if( enableTimer && !HAS_TIMER ){
22405 raw_printf(stderr, "Error: timer not available on this system.\n");
22406 enableTimer = 0;
@@ -22378,11 +22410,11 @@
22410 rc = 1;
22411 }
22412 }else
22413
22414 #ifndef SQLITE_OMIT_TRACE
22415 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
22416 int mType = 0;
22417 int jj;
22418 open_db(p, 0);
22419 for(jj=1; jj<nArg; jj++){
22420 const char *z = azArg[jj];
@@ -22428,11 +22460,11 @@
22460 }
22461 }else
22462 #endif /* !defined(SQLITE_OMIT_TRACE) */
22463
22464 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22465 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
22466 int ii;
22467 int lenOpt;
22468 char *zOpt;
22469 if( nArg<2 ){
22470 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
@@ -22441,11 +22473,11 @@
22473 }
22474 open_db(p, 0);
22475 zOpt = azArg[1];
22476 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
22477 lenOpt = (int)strlen(zOpt);
22478 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
22479 assert( azArg[nArg]==0 );
22480 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
22481 }else{
22482 for(ii=1; ii<nArg; ii++){
22483 sqlite3_create_module(p->db, azArg[ii], 0, 0);
@@ -22453,18 +22485,18 @@
22485 }
22486 }else
22487 #endif
22488
22489 #if SQLITE_USER_AUTHENTICATION
22490 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
22491 if( nArg<2 ){
22492 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
22493 rc = 1;
22494 goto meta_command_exit;
22495 }
22496 open_db(p, 0);
22497 if( cli_strcmp(azArg[1],"login")==0 ){
22498 if( nArg!=4 ){
22499 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
22500 rc = 1;
22501 goto meta_command_exit;
22502 }
@@ -22472,11 +22504,11 @@
22504 strlen30(azArg[3]));
22505 if( rc ){
22506 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
22507 rc = 1;
22508 }
22509 }else if( cli_strcmp(azArg[1],"add")==0 ){
22510 if( nArg!=5 ){
22511 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
22512 rc = 1;
22513 goto meta_command_exit;
22514 }
@@ -22484,11 +22516,11 @@
22516 booleanValue(azArg[4]));
22517 if( rc ){
22518 raw_printf(stderr, "User-Add failed: %d\n", rc);
22519 rc = 1;
22520 }
22521 }else if( cli_strcmp(azArg[1],"edit")==0 ){
22522 if( nArg!=5 ){
22523 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
22524 rc = 1;
22525 goto meta_command_exit;
22526 }
@@ -22496,11 +22528,11 @@
22528 booleanValue(azArg[4]));
22529 if( rc ){
22530 raw_printf(stderr, "User-Edit failed: %d\n", rc);
22531 rc = 1;
22532 }
22533 }else if( cli_strcmp(azArg[1],"delete")==0 ){
22534 if( nArg!=3 ){
22535 raw_printf(stderr, "Usage: .user delete USER\n");
22536 rc = 1;
22537 goto meta_command_exit;
22538 }
@@ -22515,11 +22547,11 @@
22547 goto meta_command_exit;
22548 }
22549 }else
22550 #endif /* SQLITE_USER_AUTHENTICATION */
22551
22552 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
22553 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
22554 sqlite3_libversion(), sqlite3_sourceid());
22555 #if SQLITE_HAVE_ZLIB
22556 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
22557 #endif
@@ -22534,11 +22566,11 @@
22566 #elif defined(__GNUC__) && defined(__VERSION__)
22567 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
22568 #endif
22569 }else
22570
22571 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
22572 const char *zDbName = nArg==2 ? azArg[1] : "main";
22573 sqlite3_vfs *pVfs = 0;
22574 if( p->db ){
22575 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
22576 if( pVfs ){
@@ -22548,11 +22580,11 @@
22580 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
22581 }
22582 }
22583 }else
22584
22585 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
22586 sqlite3_vfs *pVfs;
22587 sqlite3_vfs *pCurrent = 0;
22588 if( p->db ){
22589 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
22590 }
@@ -22566,11 +22598,11 @@
22598 raw_printf(p->out, "-----------------------------------\n");
22599 }
22600 }
22601 }else
22602
22603 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
22604 const char *zDbName = nArg==2 ? azArg[1] : "main";
22605 char *zVfsName = 0;
22606 if( p->db ){
22607 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
22608 if( zVfsName ){
@@ -22578,16 +22610,16 @@
22610 sqlite3_free(zVfsName);
22611 }
22612 }
22613 }else
22614
22615 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
22616 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
22617 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
22618 }else
22619
22620 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
22621 int j;
22622 assert( nArg<=ArraySize(azArg) );
22623 p->nWidth = nArg-1;
22624 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
22625 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
@@ -22761,14 +22793,14 @@
22793 const char *zErrorTail;
22794 const char *zErrorType;
22795 if( zErrMsg==0 ){
22796 zErrorType = "Error";
22797 zErrorTail = sqlite3_errmsg(p->db);
22798 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
22799 zErrorType = "Parse error";
22800 zErrorTail = &zErrMsg[12];
22801 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
22802 zErrorType = "Runtime error";
22803 zErrorTail = &zErrMsg[10];
22804 }else{
22805 zErrorType = "Error";
22806 zErrorTail = zErrMsg;
@@ -22806,11 +22838,11 @@
22838 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
22839 /* Parse the next line from shellState.wasm.zInput. */
22840 const char *zBegin = shellState.wasm.zPos;
22841 const char *z = zBegin;
22842 char *zLine = 0;
22843 i64 nZ = 0;
22844
22845 UNUSED_PARAMETER(in);
22846 UNUSED_PARAMETER(isContinuation);
22847 if(!z || !*z){
22848 return 0;
@@ -22822,11 +22854,11 @@
22854 --nZ;
22855 }
22856 shellState.wasm.zPos = z;
22857 zLine = realloc(zPrior, nZ+1);
22858 shell_check_oom(zLine);
22859 memcpy(zLine, zBegin, nZ);
22860 zLine[nZ] = 0;
22861 return zLine;
22862 }
22863 #endif /* SQLITE_SHELL_FIDDLE */
22864
@@ -22840,16 +22872,16 @@
22872 ** Return the number of errors.
22873 */
22874 static int process_input(ShellState *p){
22875 char *zLine = 0; /* A single input line */
22876 char *zSql = 0; /* Accumulated SQL text */
22877 i64 nLine; /* Length of current line */
22878 i64 nSql = 0; /* Bytes of zSql[] used */
22879 i64 nAlloc = 0; /* Allocated zSql[] space */
22880 int rc; /* Error code */
22881 int errCnt = 0; /* Number of errors seen */
22882 i64 startline = 0; /* Line number for start of current input */
22883 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
22884
22885 if( p->inputNesting==MAX_INPUT_NESTING ){
22886 /* This will be more informative in a later version. */
22887 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
@@ -22895,19 +22927,19 @@
22927 }
22928 qss = QSS_Start;
22929 continue;
22930 }
22931 /* No single-line dispositions remain; accumulate line(s). */
22932 nLine = strlen(zLine);
22933 if( nSql+nLine+2>=nAlloc ){
22934 /* Grow buffer by half-again increments when big. */
22935 nAlloc = nSql+(nSql>>1)+nLine+100;
22936 zSql = realloc(zSql, nAlloc);
22937 shell_check_oom(zSql);
22938 }
22939 if( nSql==0 ){
22940 i64 i;
22941 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
22942 assert( nAlloc>0 && zSql!=0 );
22943 memcpy(zSql, zLine+i, nLine+1-i);
22944 startline = p->lineno;
22945 nSql = nLine-i;
@@ -23003,11 +23035,11 @@
23035 #endif
23036
23037 #endif /* !_WIN32_WCE */
23038
23039 if( home_dir ){
23040 i64 n = strlen(home_dir) + 1;
23041 char *z = malloc( n );
23042 if( z ) memcpy(z, home_dir, n);
23043 home_dir = z;
23044 }
23045
@@ -23246,10 +23278,11 @@
23278 setBinaryMode(stdin, 0);
23279 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23280 #ifdef SQLITE_SHELL_FIDDLE
23281 stdin_is_interactive = 0;
23282 stdout_is_console = 1;
23283 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
23284 #else
23285 stdin_is_interactive = isatty(0);
23286 stdout_is_console = isatty(1);
23287 #endif
23288
@@ -23273,11 +23306,11 @@
23306 }
23307 }
23308 #endif
23309
23310 #if USE_SYSTEM_SQLITE+0!=1
23311 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
23312 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
23313 sqlite3_sourceid(), SQLITE_SOURCE_ID);
23314 exit(1);
23315 }
23316 #endif
@@ -23295,13 +23328,13 @@
23328 shell_check_oom(argvToFree);
23329 argcToFree = argc;
23330 argv = argvToFree + argc;
23331 for(i=0; i<argc; i++){
23332 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
23333 i64 n;
23334 shell_check_oom(z);
23335 n = strlen(z);
23336 argv[i] = malloc( n+1 );
23337 shell_check_oom(argv[i]);
23338 memcpy(argv[i], z, n+1);
23339 argvToFree[i] = argv[i];
23340 sqlite3_free(z);
@@ -23354,25 +23387,25 @@
23387 shell_check_oom(azCmd);
23388 azCmd[nCmd-1] = z;
23389 }
23390 }
23391 if( z[1]=='-' ) z++;
23392 if( cli_strcmp(z,"-separator")==0
23393 || cli_strcmp(z,"-nullvalue")==0
23394 || cli_strcmp(z,"-newline")==0
23395 || cli_strcmp(z,"-cmd")==0
23396 ){
23397 (void)cmdline_option_value(argc, argv, ++i);
23398 }else if( cli_strcmp(z,"-init")==0 ){
23399 zInitFile = cmdline_option_value(argc, argv, ++i);
23400 }else if( cli_strcmp(z,"-batch")==0 ){
23401 /* Need to check for batch mode here to so we can avoid printing
23402 ** informational messages (like from process_sqliterc) before
23403 ** we do the actual processing of arguments later in a second pass.
23404 */
23405 stdin_is_interactive = 0;
23406 }else if( cli_strcmp(z,"-heap")==0 ){
23407 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
23408 const char *zSize;
23409 sqlite3_int64 szHeap;
23410
23411 zSize = cmdline_option_value(argc, argv, ++i);
@@ -23380,11 +23413,11 @@
23413 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
23414 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
23415 #else
23416 (void)cmdline_option_value(argc, argv, ++i);
23417 #endif
23418 }else if( cli_strcmp(z,"-pagecache")==0 ){
23419 sqlite3_int64 n, sz;
23420 sz = integerValue(cmdline_option_value(argc,argv,++i));
23421 if( sz>70000 ) sz = 70000;
23422 if( sz<0 ) sz = 0;
23423 n = integerValue(cmdline_option_value(argc,argv,++i));
@@ -23392,28 +23425,28 @@
23425 n = 0xffffffffffffLL/sz;
23426 }
23427 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
23428 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
23429 data.shellFlgs |= SHFLG_Pagecache;
23430 }else if( cli_strcmp(z,"-lookaside")==0 ){
23431 int n, sz;
23432 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
23433 if( sz<0 ) sz = 0;
23434 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23435 if( n<0 ) n = 0;
23436 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
23437 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
23438 }else if( cli_strcmp(z,"-threadsafe")==0 ){
23439 int n;
23440 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
23441 switch( n ){
23442 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
23443 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
23444 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
23445 }
23446 #ifdef SQLITE_ENABLE_VFSTRACE
23447 }else if( cli_strcmp(z,"-vfstrace")==0 ){
23448 extern int vfstrace_register(
23449 const char *zTraceName,
23450 const char *zOldVfsName,
23451 int (*xOut)(const char*,void*),
23452 void *pOutArg,
@@ -23420,54 +23453,54 @@
23453 int makeDefault
23454 );
23455 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
23456 #endif
23457 #ifdef SQLITE_ENABLE_MULTIPLEX
23458 }else if( cli_strcmp(z,"-multiplex")==0 ){
23459 extern int sqlite3_multiple_initialize(const char*,int);
23460 sqlite3_multiplex_initialize(0, 1);
23461 #endif
23462 }else if( cli_strcmp(z,"-mmap")==0 ){
23463 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23464 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
23465 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23466 }else if( cli_strcmp(z,"-sorterref")==0 ){
23467 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
23468 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
23469 #endif
23470 }else if( cli_strcmp(z,"-vfs")==0 ){
23471 zVfs = cmdline_option_value(argc, argv, ++i);
23472 #ifdef SQLITE_HAVE_ZLIB
23473 }else if( cli_strcmp(z,"-zip")==0 ){
23474 data.openMode = SHELL_OPEN_ZIPFILE;
23475 #endif
23476 }else if( cli_strcmp(z,"-append")==0 ){
23477 data.openMode = SHELL_OPEN_APPENDVFS;
23478 #ifndef SQLITE_OMIT_DESERIALIZE
23479 }else if( cli_strcmp(z,"-deserialize")==0 ){
23480 data.openMode = SHELL_OPEN_DESERIALIZE;
23481 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
23482 data.szMax = integerValue(argv[++i]);
23483 #endif
23484 }else if( cli_strcmp(z,"-readonly")==0 ){
23485 data.openMode = SHELL_OPEN_READONLY;
23486 }else if( cli_strcmp(z,"-nofollow")==0 ){
23487 data.openFlags = SQLITE_OPEN_NOFOLLOW;
23488 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23489 }else if( cli_strncmp(z, "-A",2)==0 ){
23490 /* All remaining command-line arguments are passed to the ".archive"
23491 ** command, so ignore them */
23492 break;
23493 #endif
23494 }else if( cli_strcmp(z, "-memtrace")==0 ){
23495 sqlite3MemTraceActivate(stderr);
23496 }else if( cli_strcmp(z,"-bail")==0 ){
23497 bail_on_error = 1;
23498 }else if( cli_strcmp(z,"-nonce")==0 ){
23499 free(data.zNonce);
23500 data.zNonce = strdup(argv[++i]);
23501 }else if( cli_strcmp(z,"-safe")==0 ){
23502 /* no-op - catch this on the second pass */
23503 }
23504 }
23505 verify_uninitialized();
23506
@@ -23533,131 +23566,131 @@
23566 */
23567 for(i=1; i<argc; i++){
23568 char *z = argv[i];
23569 if( z[0]!='-' ) continue;
23570 if( z[1]=='-' ){ z++; }
23571 if( cli_strcmp(z,"-init")==0 ){
23572 i++;
23573 }else if( cli_strcmp(z,"-html")==0 ){
23574 data.mode = MODE_Html;
23575 }else if( cli_strcmp(z,"-list")==0 ){
23576 data.mode = MODE_List;
23577 }else if( cli_strcmp(z,"-quote")==0 ){
23578 data.mode = MODE_Quote;
23579 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
23580 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23581 }else if( cli_strcmp(z,"-line")==0 ){
23582 data.mode = MODE_Line;
23583 }else if( cli_strcmp(z,"-column")==0 ){
23584 data.mode = MODE_Column;
23585 }else if( cli_strcmp(z,"-json")==0 ){
23586 data.mode = MODE_Json;
23587 }else if( cli_strcmp(z,"-markdown")==0 ){
23588 data.mode = MODE_Markdown;
23589 }else if( cli_strcmp(z,"-table")==0 ){
23590 data.mode = MODE_Table;
23591 }else if( cli_strcmp(z,"-box")==0 ){
23592 data.mode = MODE_Box;
23593 }else if( cli_strcmp(z,"-csv")==0 ){
23594 data.mode = MODE_Csv;
23595 memcpy(data.colSeparator,",",2);
23596 #ifdef SQLITE_HAVE_ZLIB
23597 }else if( cli_strcmp(z,"-zip")==0 ){
23598 data.openMode = SHELL_OPEN_ZIPFILE;
23599 #endif
23600 }else if( cli_strcmp(z,"-append")==0 ){
23601 data.openMode = SHELL_OPEN_APPENDVFS;
23602 #ifndef SQLITE_OMIT_DESERIALIZE
23603 }else if( cli_strcmp(z,"-deserialize")==0 ){
23604 data.openMode = SHELL_OPEN_DESERIALIZE;
23605 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
23606 data.szMax = integerValue(argv[++i]);
23607 #endif
23608 }else if( cli_strcmp(z,"-readonly")==0 ){
23609 data.openMode = SHELL_OPEN_READONLY;
23610 }else if( cli_strcmp(z,"-nofollow")==0 ){
23611 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
23612 }else if( cli_strcmp(z,"-ascii")==0 ){
23613 data.mode = MODE_Ascii;
23614 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
23615 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
23616 }else if( cli_strcmp(z,"-tabs")==0 ){
23617 data.mode = MODE_List;
23618 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
23619 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
23620 }else if( cli_strcmp(z,"-separator")==0 ){
23621 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
23622 "%s",cmdline_option_value(argc,argv,++i));
23623 }else if( cli_strcmp(z,"-newline")==0 ){
23624 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
23625 "%s",cmdline_option_value(argc,argv,++i));
23626 }else if( cli_strcmp(z,"-nullvalue")==0 ){
23627 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
23628 "%s",cmdline_option_value(argc,argv,++i));
23629 }else if( cli_strcmp(z,"-header")==0 ){
23630 data.showHeader = 1;
23631 ShellSetFlag(&data, SHFLG_HeaderSet);
23632 }else if( cli_strcmp(z,"-noheader")==0 ){
23633 data.showHeader = 0;
23634 ShellSetFlag(&data, SHFLG_HeaderSet);
23635 }else if( cli_strcmp(z,"-echo")==0 ){
23636 ShellSetFlag(&data, SHFLG_Echo);
23637 }else if( cli_strcmp(z,"-eqp")==0 ){
23638 data.autoEQP = AUTOEQP_on;
23639 }else if( cli_strcmp(z,"-eqpfull")==0 ){
23640 data.autoEQP = AUTOEQP_full;
23641 }else if( cli_strcmp(z,"-stats")==0 ){
23642 data.statsOn = 1;
23643 }else if( cli_strcmp(z,"-scanstats")==0 ){
23644 data.scanstatsOn = 1;
23645 }else if( cli_strcmp(z,"-backslash")==0 ){
23646 /* Undocumented command-line option: -backslash
23647 ** Causes C-style backslash escapes to be evaluated in SQL statements
23648 ** prior to sending the SQL into SQLite. Useful for injecting
23649 ** crazy bytes in the middle of SQL statements for testing and debugging.
23650 */
23651 ShellSetFlag(&data, SHFLG_Backslash);
23652 }else if( cli_strcmp(z,"-bail")==0 ){
23653 /* No-op. The bail_on_error flag should already be set. */
23654 }else if( cli_strcmp(z,"-version")==0 ){
23655 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
23656 return 0;
23657 }else if( cli_strcmp(z,"-interactive")==0 ){
23658 stdin_is_interactive = 1;
23659 }else if( cli_strcmp(z,"-batch")==0 ){
23660 stdin_is_interactive = 0;
23661 }else if( cli_strcmp(z,"-heap")==0 ){
23662 i++;
23663 }else if( cli_strcmp(z,"-pagecache")==0 ){
23664 i+=2;
23665 }else if( cli_strcmp(z,"-lookaside")==0 ){
23666 i+=2;
23667 }else if( cli_strcmp(z,"-threadsafe")==0 ){
23668 i+=2;
23669 }else if( cli_strcmp(z,"-nonce")==0 ){
23670 i += 2;
23671 }else if( cli_strcmp(z,"-mmap")==0 ){
23672 i++;
23673 }else if( cli_strcmp(z,"-memtrace")==0 ){
23674 i++;
23675 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
23676 }else if( cli_strcmp(z,"-sorterref")==0 ){
23677 i++;
23678 #endif
23679 }else if( cli_strcmp(z,"-vfs")==0 ){
23680 i++;
23681 #ifdef SQLITE_ENABLE_VFSTRACE
23682 }else if( cli_strcmp(z,"-vfstrace")==0 ){
23683 i++;
23684 #endif
23685 #ifdef SQLITE_ENABLE_MULTIPLEX
23686 }else if( cli_strcmp(z,"-multiplex")==0 ){
23687 i++;
23688 #endif
23689 }else if( cli_strcmp(z,"-help")==0 ){
23690 usage(1);
23691 }else if( cli_strcmp(z,"-cmd")==0 ){
23692 /* Run commands that follow -cmd first and separately from commands
23693 ** that simply appear on the command-line. This seems goofy. It would
23694 ** be better if all commands ran in the order that they appear. But
23695 ** we retain the goofy behavior for historical compatibility. */
23696 if( i==argc-1 ) break;
@@ -23675,11 +23708,11 @@
23708 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
23709 if( bail_on_error ) return rc;
23710 }
23711 }
23712 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
23713 }else if( cli_strncmp(z, "-A", 2)==0 ){
23714 if( nCmd>0 ){
23715 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
23716 " with \"%s\"\n", z);
23717 return 1;
23718 }
@@ -23691,11 +23724,11 @@
23724 arDotCommand(&data, 1, argv+i, argc-i);
23725 }
23726 readStdin = 0;
23727 break;
23728 #endif
23729 }else if( cli_strcmp(z,"-safe")==0 ){
23730 data.bSafeMode = data.bSafeModePersist = 1;
23731 }else{
23732 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
23733 raw_printf(stderr,"Use -help for a list of options.\n");
23734 return 1;
@@ -23816,34 +23849,34 @@
23849
23850
23851 #ifdef SQLITE_SHELL_FIDDLE
23852 /* Only for emcc experimentation purposes. */
23853 int fiddle_experiment(int a,int b){
23854 return a + b;
23855 }
23856
23857 /*
23858 ** Returns a pointer to the current DB handle.
23859 */
23860 sqlite3 * fiddle_db_handle(){
23861 return globalDb;
23862 }
23863
23864 /*
23865 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
23866 ** "main" is assumed. Returns 0 if no db with the given name is
23867 ** open.
 
 
23868 */
23869 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
23870 sqlite3_vfs * pVfs = 0;
23871 if(globalDb){
23872 sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
23873 SQLITE_FCNTL_VFS_POINTER, &pVfs);
23874 }
23875 return pVfs;
23876 }
23877
23878 /* Only for emcc experimentation purposes. */
23879 sqlite3 * fiddle_db_arg(sqlite3 *arg){
23880 printf("fiddle_db_arg(%p)\n", (const void*)arg);
23881 return arg;
23882 }
@@ -23853,11 +23886,11 @@
23886 ** SharedWorker() (which manages the wasm module) is performing work
23887 ** which should be interrupted. Unfortunately, SharedWorker is not
23888 ** portable enough to make real use of.
23889 */
23890 void fiddle_interrupt(void){
23891 if( globalDb ) sqlite3_interrupt(globalDb);
23892 }
23893
23894 /*
23895 ** Returns the filename of the given db name, assuming "main" if
23896 ** zDbName is NULL. Returns NULL if globalDb is not opened.
@@ -23867,73 +23900,74 @@
23900 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
23901 : NULL;
23902 }
23903
23904 /*
23905 ** Completely wipes out the contents of the currently-opened database
23906 ** but leaves its storage intact for reuse.
 
 
23907 */
23908 void fiddle_reset_db(void){
23909 if( globalDb ){
23910 int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
23911 if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
23912 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
23913 }
23914 }
23915
23916 /*
23917 ** Uses the current database's VFS xRead to stream the db file's
23918 ** contents out to the given callback. The callback gets a single
23919 ** chunk of size n (its 2nd argument) on each call and must return 0
23920 ** on success, non-0 on error. This function returns 0 on success,
23921 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
23922 ** code from the callback. Note that this is not thread-friendly: it
23923 ** expects that it will be the only thread reading the db file and
23924 ** takes no measures to ensure that is the case.
23925 */
23926 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
23927 sqlite3_int64 nSize = 0;
23928 sqlite3_int64 nPos = 0;
23929 sqlite3_file * pFile = 0;
23930 unsigned char buf[1024 * 8];
23931 int nBuf = (int)sizeof(buf);
23932 int rc = shellState.db
23933 ? sqlite3_file_control(shellState.db, "main",
23934 SQLITE_FCNTL_FILE_POINTER, &pFile)
23935 : SQLITE_NOTFOUND;
23936 if( rc ) return rc;
23937 rc = pFile->pMethods->xFileSize(pFile, &nSize);
23938 if( rc ) return rc;
23939 if(nSize % nBuf){
23940 /* DB size is not an even multiple of the buffer size. Reduce
23941 ** buffer size so that we do not unduly inflate the db size when
23942 ** exporting. */
23943 if(0 == nSize % 4096) nBuf = 4096;
23944 else if(0 == nSize % 2048) nBuf = 2048;
23945 else if(0 == nSize % 1024) nBuf = 1024;
23946 else nBuf = 512;
23947 }
23948 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
23949 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
23950 if(SQLITE_IOERR_SHORT_READ == rc){
23951 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
23952 }
23953 if( 0==rc ) rc = xCallback(buf, nBuf);
23954 }
23955 return rc;
23956 }
23957
23958 /*
23959 ** Trivial exportable function for emscripten. It processes zSql as if
23960 ** it were input to the sqlite3 shell and redirects all output to the
23961 ** wasm binding. fiddle_main() must have been called before this
23962 ** is called, or results are undefined.
 
 
 
23963 */
23964 void fiddle_exec(const char * zSql){
23965 if(zSql && *zSql){
23966 if('.'==*zSql) puts(zSql);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23967 shellState.wasm.zInput = zSql;
23968 shellState.wasm.zPos = zSql;
23969 process_input(&shellState);
23970 shellState.wasm.zInput = shellState.wasm.zPos = 0;
23971 }
23972 }
23973 #endif /* SQLITE_SHELL_FIDDLE */
23974
+2112 -697
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452452
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453
** [sqlite_version()] and [sqlite_source_id()].
454454
*/
455455
#define SQLITE_VERSION "3.40.0"
456456
#define SQLITE_VERSION_NUMBER 3040000
457
-#define SQLITE_SOURCE_ID "2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646"
457
+#define SQLITE_SOURCE_ID "2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -974,17 +974,21 @@
974974
/*
975975
** CAPI3REF: File Locking Levels
976976
**
977977
** SQLite uses one of these integer values as the second
978978
** argument to calls it makes to the xLock() and xUnlock() methods
979
-** of an [sqlite3_io_methods] object.
979
+** of an [sqlite3_io_methods] object. These values are ordered from
980
+** lest restrictive to most restrictive.
981
+**
982
+** The argument to xLock() is always SHARED or higher. The argument to
983
+** xUnlock is either SHARED or NONE.
980984
*/
981
-#define SQLITE_LOCK_NONE 0
982
-#define SQLITE_LOCK_SHARED 1
983
-#define SQLITE_LOCK_RESERVED 2
984
-#define SQLITE_LOCK_PENDING 3
985
-#define SQLITE_LOCK_EXCLUSIVE 4
985
+#define SQLITE_LOCK_NONE 0 /* xUnlock() only */
986
+#define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */
987
+#define SQLITE_LOCK_RESERVED 2 /* xLock() only */
988
+#define SQLITE_LOCK_PENDING 3 /* xLock() only */
989
+#define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */
986990
987991
/*
988992
** CAPI3REF: Synchronization Type Flags
989993
**
990994
** When SQLite invokes the xSync() method of an
@@ -1058,11 +1062,18 @@
10581062
** <li> [SQLITE_LOCK_SHARED],
10591063
** <li> [SQLITE_LOCK_RESERVED],
10601064
** <li> [SQLITE_LOCK_PENDING], or
10611065
** <li> [SQLITE_LOCK_EXCLUSIVE].
10621066
** </ul>
1063
-** xLock() increases the lock. xUnlock() decreases the lock.
1067
+** xLock() upgrades the database file lock. In other words, xLock() moves the
1068
+** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1069
+** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1070
+** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1071
+** requested lock, then the call to xLock() is a no-op.
1072
+** xUnlock() downgrades the database file lock to either SHARED or NONE.
1073
+* If the lock is already at or below the requested lock state, then the call
1074
+** to xUnlock() is a no-op.
10641075
** The xCheckReservedLock() method checks whether any database connection,
10651076
** either in this process or in some other process, is holding a RESERVED,
10661077
** PENDING, or EXCLUSIVE lock on the file. It returns true
10671078
** if such a lock exists and false otherwise.
10681079
**
@@ -1163,13 +1174,12 @@
11631174
** <li>[[SQLITE_FCNTL_LOCKSTATE]]
11641175
** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
11651176
** opcode causes the xFileControl method to write the current state of
11661177
** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
11671178
** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1168
-** into an integer that the pArg argument points to. This capability
1169
-** is used during testing and is only available when the SQLITE_TEST
1170
-** compile-time option is used.
1179
+** into an integer that the pArg argument points to.
1180
+** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
11711181
**
11721182
** <li>[[SQLITE_FCNTL_SIZE_HINT]]
11731183
** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
11741184
** layer a hint of how large the database file will grow to be during the
11751185
** current transaction. This hint is not guaranteed to be accurate but it
@@ -5815,10 +5825,20 @@
58155825
** such a conversion is possible without loss of information (in other
58165826
** words, if the value is a string that looks like a number)
58175827
** then the conversion is performed. Otherwise no conversion occurs.
58185828
** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
58195829
**
5830
+** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5831
+** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5832
+** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5833
+** returns something other than SQLITE_TEXT, then the return value from
5834
+** sqlite3_value_encoding(X) is meaningless. ^Calls to
5835
+** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5836
+** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5837
+** sqlite3_value_bytes16(X) might change the encoding of the value X and
5838
+** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5839
+**
58205840
** ^Within the [xUpdate] method of a [virtual table], the
58215841
** sqlite3_value_nochange(X) interface returns true if and only if
58225842
** the column corresponding to X is unchanged by the UPDATE operation
58235843
** that the xUpdate method call was invoked to implement and if
58245844
** and the prior [xColumn] method call that was invoked to extracted
@@ -5879,10 +5899,11 @@
58795899
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
58805900
SQLITE_API int sqlite3_value_type(sqlite3_value*);
58815901
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
58825902
SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
58835903
SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5904
+SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
58845905
58855906
/*
58865907
** CAPI3REF: Finding The Subtype Of SQL Values
58875908
** METHOD: sqlite3_value
58885909
**
@@ -5932,11 +5953,11 @@
59325953
** In those cases, sqlite3_aggregate_context() might be called for the
59335954
** first time from within xFinal().)^
59345955
**
59355956
** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
59365957
** when first called if N is less than or equal to zero or if a memory
5937
-** allocate error occurs.
5958
+** allocation error occurs.
59385959
**
59395960
** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
59405961
** determined by the N parameter on first successful call. Changing the
59415962
** value of N in any subsequent call to sqlite3_aggregate_context() within
59425963
** the same aggregate function instance will not resize the memory
@@ -13160,11 +13181,11 @@
1316013181
/*
1316113182
** Include the configuration header output by 'configure' if we're using the
1316213183
** autoconf-based build
1316313184
*/
1316413185
#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13165
-#include "config.h"
13186
+#include "sqlite_cfg.h"
1316613187
#define SQLITECONFIG_H 1
1316713188
#endif
1316813189
1316913190
/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
1317013191
/************** Begin file sqliteLimit.h *************************************/
@@ -14643,10 +14664,11 @@
1464314664
typedef struct FuncDestructor FuncDestructor;
1464414665
typedef struct FuncDef FuncDef;
1464514666
typedef struct FuncDefHash FuncDefHash;
1464614667
typedef struct IdList IdList;
1464714668
typedef struct Index Index;
14669
+typedef struct IndexedExpr IndexedExpr;
1464814670
typedef struct IndexSample IndexSample;
1464914671
typedef struct KeyClass KeyClass;
1465014672
typedef struct KeyInfo KeyInfo;
1465114673
typedef struct Lookaside Lookaside;
1465214674
typedef struct LookasideSlot LookasideSlot;
@@ -14708,10 +14730,11 @@
1470814730
#define MASKBIT(n) (((Bitmask)1)<<(n))
1470914731
#define MASKBIT64(n) (((u64)1)<<(n))
1471014732
#define MASKBIT32(n) (((unsigned int)1)<<(n))
1471114733
#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
1471214734
#define ALLBITS ((Bitmask)-1)
14735
+#define TOPBIT (((Bitmask)1)<<(BMS-1))
1471314736
1471414737
/* A VList object records a mapping between parameters/variables/wildcards
1471514738
** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
1471614739
** variable number associated with that parameter. See the format description
1471714740
** on the sqlite3VListAdd() routine for more information. A VList is really
@@ -14722,10 +14745,335 @@
1472214745
/*
1472314746
** Defer sourcing vdbe.h and btree.h until after the "u8" and
1472414747
** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
1472514748
** pointer types (i.e. FuncDef) defined above.
1472614749
*/
14750
+/************** Include os.h in the middle of sqliteInt.h ********************/
14751
+/************** Begin file os.h **********************************************/
14752
+/*
14753
+** 2001 September 16
14754
+**
14755
+** The author disclaims copyright to this source code. In place of
14756
+** a legal notice, here is a blessing:
14757
+**
14758
+** May you do good and not evil.
14759
+** May you find forgiveness for yourself and forgive others.
14760
+** May you share freely, never taking more than you give.
14761
+**
14762
+******************************************************************************
14763
+**
14764
+** This header file (together with is companion C source-code file
14765
+** "os.c") attempt to abstract the underlying operating system so that
14766
+** the SQLite library will work on both POSIX and windows systems.
14767
+**
14768
+** This header file is #include-ed by sqliteInt.h and thus ends up
14769
+** being included by every source file.
14770
+*/
14771
+#ifndef _SQLITE_OS_H_
14772
+#define _SQLITE_OS_H_
14773
+
14774
+/*
14775
+** Attempt to automatically detect the operating system and setup the
14776
+** necessary pre-processor macros for it.
14777
+*/
14778
+/************** Include os_setup.h in the middle of os.h *********************/
14779
+/************** Begin file os_setup.h ****************************************/
14780
+/*
14781
+** 2013 November 25
14782
+**
14783
+** The author disclaims copyright to this source code. In place of
14784
+** a legal notice, here is a blessing:
14785
+**
14786
+** May you do good and not evil.
14787
+** May you find forgiveness for yourself and forgive others.
14788
+** May you share freely, never taking more than you give.
14789
+**
14790
+******************************************************************************
14791
+**
14792
+** This file contains pre-processor directives related to operating system
14793
+** detection and/or setup.
14794
+*/
14795
+#ifndef SQLITE_OS_SETUP_H
14796
+#define SQLITE_OS_SETUP_H
14797
+
14798
+/*
14799
+** Figure out if we are dealing with Unix, Windows, or some other operating
14800
+** system.
14801
+**
14802
+** After the following block of preprocess macros, all of
14803
+**
14804
+** SQLITE_OS_KV
14805
+** SQLITE_OS_OTHER
14806
+** SQLITE_OS_UNIX
14807
+** SQLITE_OS_WIN
14808
+**
14809
+** will defined to either 1 or 0. One of them will be 1. The others will be 0.
14810
+** If none of the macros are initially defined, then select either
14811
+** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform.
14812
+**
14813
+** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
14814
+** must provide its own VFS implementation together with sqlite3_os_init()
14815
+** and sqlite3_os_end() routines.
14816
+*/
14817
+#if !defined(SQLITE_OS_KV) && !defined(SQLITE_OS_OTHER) && \
14818
+ !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_WIN)
14819
+# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
14820
+ defined(__MINGW32__) || defined(__BORLANDC__)
14821
+# define SQLITE_OS_WIN 1
14822
+# define SQLITE_OS_UNIX 0
14823
+# else
14824
+# define SQLITE_OS_WIN 0
14825
+# define SQLITE_OS_UNIX 1
14826
+# endif
14827
+#endif
14828
+#if SQLITE_OS_OTHER+1>1
14829
+# undef SQLITE_OS_KV
14830
+# define SQLITE_OS_KV 0
14831
+# undef SQLITE_OS_UNIX
14832
+# define SQLITE_OS_UNIX 0
14833
+# undef SQLITE_OS_WIN
14834
+# define SQLITE_OS_WIN 0
14835
+#endif
14836
+#if SQLITE_OS_KV+1>1
14837
+# undef SQLITE_OS_OTHER
14838
+# define SQLITE_OS_OTHER 0
14839
+# undef SQLITE_OS_UNIX
14840
+# define SQLITE_OS_UNIX 0
14841
+# undef SQLITE_OS_WIN
14842
+# define SQLITE_OS_WIN 0
14843
+# define SQLITE_OMIT_LOAD_EXTENSION 1
14844
+# define SQLITE_OMIT_WAL 1
14845
+# define SQLITE_OMIT_DEPRECATED 1
14846
+# undef SQLITE_TEMP_STORE
14847
+# define SQLITE_TEMP_STORE 3 /* Always use memory for temporary storage */
14848
+# define SQLITE_DQS 0
14849
+# define SQLITE_OMIT_SHARED_CACHE 1
14850
+# define SQLITE_OMIT_AUTOINIT 1
14851
+#endif
14852
+#if SQLITE_OS_UNIX+1>1
14853
+# undef SQLITE_OS_KV
14854
+# define SQLITE_OS_KV 0
14855
+# undef SQLITE_OS_OTHER
14856
+# define SQLITE_OS_OTHER 0
14857
+# undef SQLITE_OS_WIN
14858
+# define SQLITE_OS_WIN 0
14859
+#endif
14860
+#if SQLITE_OS_WIN+1>1
14861
+# undef SQLITE_OS_KV
14862
+# define SQLITE_OS_KV 0
14863
+# undef SQLITE_OS_OTHER
14864
+# define SQLITE_OS_OTHER 0
14865
+# undef SQLITE_OS_UNIX
14866
+# define SQLITE_OS_UNIX 0
14867
+#endif
14868
+
14869
+
14870
+#endif /* SQLITE_OS_SETUP_H */
14871
+
14872
+/************** End of os_setup.h ********************************************/
14873
+/************** Continuing where we left off in os.h *************************/
14874
+
14875
+/* If the SET_FULLSYNC macro is not defined above, then make it
14876
+** a no-op
14877
+*/
14878
+#ifndef SET_FULLSYNC
14879
+# define SET_FULLSYNC(x,y)
14880
+#endif
14881
+
14882
+/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
14883
+*/
14884
+#ifndef SQLITE_MAX_PATHLEN
14885
+# define SQLITE_MAX_PATHLEN FILENAME_MAX
14886
+#endif
14887
+
14888
+/* Maximum number of symlinks that will be resolved while trying to
14889
+** expand a filename in xFullPathname() in the VFS.
14890
+*/
14891
+#ifndef SQLITE_MAX_SYMLINK
14892
+# define SQLITE_MAX_SYMLINK 200
14893
+#endif
14894
+
14895
+/*
14896
+** The default size of a disk sector
14897
+*/
14898
+#ifndef SQLITE_DEFAULT_SECTOR_SIZE
14899
+# define SQLITE_DEFAULT_SECTOR_SIZE 4096
14900
+#endif
14901
+
14902
+/*
14903
+** Temporary files are named starting with this prefix followed by 16 random
14904
+** alphanumeric characters, and no file extension. They are stored in the
14905
+** OS's standard temporary file directory, and are deleted prior to exit.
14906
+** If sqlite is being embedded in another program, you may wish to change the
14907
+** prefix to reflect your program's name, so that if your program exits
14908
+** prematurely, old temporary files can be easily identified. This can be done
14909
+** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
14910
+**
14911
+** 2006-10-31: The default prefix used to be "sqlite_". But then
14912
+** Mcafee started using SQLite in their anti-virus product and it
14913
+** started putting files with the "sqlite" name in the c:/temp folder.
14914
+** This annoyed many windows users. Those users would then do a
14915
+** Google search for "sqlite", find the telephone numbers of the
14916
+** developers and call to wake them up at night and complain.
14917
+** For this reason, the default name prefix is changed to be "sqlite"
14918
+** spelled backwards. So the temp files are still identified, but
14919
+** anybody smart enough to figure out the code is also likely smart
14920
+** enough to know that calling the developer will not help get rid
14921
+** of the file.
14922
+*/
14923
+#ifndef SQLITE_TEMP_FILE_PREFIX
14924
+# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
14925
+#endif
14926
+
14927
+/*
14928
+** The following values may be passed as the second argument to
14929
+** sqlite3OsLock(). The various locks exhibit the following semantics:
14930
+**
14931
+** SHARED: Any number of processes may hold a SHARED lock simultaneously.
14932
+** RESERVED: A single process may hold a RESERVED lock on a file at
14933
+** any time. Other processes may hold and obtain new SHARED locks.
14934
+** PENDING: A single process may hold a PENDING lock on a file at
14935
+** any one time. Existing SHARED locks may persist, but no new
14936
+** SHARED locks may be obtained by other processes.
14937
+** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
14938
+**
14939
+** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
14940
+** process that requests an EXCLUSIVE lock may actually obtain a PENDING
14941
+** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
14942
+** sqlite3OsLock().
14943
+*/
14944
+#define NO_LOCK 0
14945
+#define SHARED_LOCK 1
14946
+#define RESERVED_LOCK 2
14947
+#define PENDING_LOCK 3
14948
+#define EXCLUSIVE_LOCK 4
14949
+
14950
+/*
14951
+** File Locking Notes: (Mostly about windows but also some info for Unix)
14952
+**
14953
+** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
14954
+** those functions are not available. So we use only LockFile() and
14955
+** UnlockFile().
14956
+**
14957
+** LockFile() prevents not just writing but also reading by other processes.
14958
+** A SHARED_LOCK is obtained by locking a single randomly-chosen
14959
+** byte out of a specific range of bytes. The lock byte is obtained at
14960
+** random so two separate readers can probably access the file at the
14961
+** same time, unless they are unlucky and choose the same lock byte.
14962
+** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
14963
+** There can only be one writer. A RESERVED_LOCK is obtained by locking
14964
+** a single byte of the file that is designated as the reserved lock byte.
14965
+** A PENDING_LOCK is obtained by locking a designated byte different from
14966
+** the RESERVED_LOCK byte.
14967
+**
14968
+** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
14969
+** which means we can use reader/writer locks. When reader/writer locks
14970
+** are used, the lock is placed on the same range of bytes that is used
14971
+** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
14972
+** will support two or more Win95 readers or two or more WinNT readers.
14973
+** But a single Win95 reader will lock out all WinNT readers and a single
14974
+** WinNT reader will lock out all other Win95 readers.
14975
+**
14976
+** The following #defines specify the range of bytes used for locking.
14977
+** SHARED_SIZE is the number of bytes available in the pool from which
14978
+** a random byte is selected for a shared lock. The pool of bytes for
14979
+** shared locks begins at SHARED_FIRST.
14980
+**
14981
+** The same locking strategy and
14982
+** byte ranges are used for Unix. This leaves open the possibility of having
14983
+** clients on win95, winNT, and unix all talking to the same shared file
14984
+** and all locking correctly. To do so would require that samba (or whatever
14985
+** tool is being used for file sharing) implements locks correctly between
14986
+** windows and unix. I'm guessing that isn't likely to happen, but by
14987
+** using the same locking range we are at least open to the possibility.
14988
+**
14989
+** Locking in windows is manditory. For this reason, we cannot store
14990
+** actual data in the bytes used for locking. The pager never allocates
14991
+** the pages involved in locking therefore. SHARED_SIZE is selected so
14992
+** that all locks will fit on a single page even at the minimum page size.
14993
+** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
14994
+** is set high so that we don't have to allocate an unused page except
14995
+** for very large databases. But one should test the page skipping logic
14996
+** by setting PENDING_BYTE low and running the entire regression suite.
14997
+**
14998
+** Changing the value of PENDING_BYTE results in a subtly incompatible
14999
+** file format. Depending on how it is changed, you might not notice
15000
+** the incompatibility right away, even running a full regression test.
15001
+** The default location of PENDING_BYTE is the first byte past the
15002
+** 1GB boundary.
15003
+**
15004
+*/
15005
+#ifdef SQLITE_OMIT_WSD
15006
+# define PENDING_BYTE (0x40000000)
15007
+#else
15008
+# define PENDING_BYTE sqlite3PendingByte
15009
+#endif
15010
+#define RESERVED_BYTE (PENDING_BYTE+1)
15011
+#define SHARED_FIRST (PENDING_BYTE+2)
15012
+#define SHARED_SIZE 510
15013
+
15014
+/*
15015
+** Wrapper around OS specific sqlite3_os_init() function.
15016
+*/
15017
+SQLITE_PRIVATE int sqlite3OsInit(void);
15018
+
15019
+/*
15020
+** Functions for accessing sqlite3_file methods
15021
+*/
15022
+SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
15023
+SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15024
+SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15025
+SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15026
+SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15027
+SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15028
+SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15029
+SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15030
+SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15031
+SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15032
+SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15033
+#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
15034
+SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15035
+SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15036
+#ifndef SQLITE_OMIT_WAL
15037
+SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15038
+SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15039
+SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
15040
+SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15041
+#endif /* SQLITE_OMIT_WAL */
15042
+SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15043
+SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15044
+
15045
+
15046
+/*
15047
+** Functions for accessing sqlite3_vfs methods
15048
+*/
15049
+SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15050
+SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15051
+SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15052
+SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15053
+#ifndef SQLITE_OMIT_LOAD_EXTENSION
15054
+SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15055
+SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15056
+SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15057
+SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
15058
+#endif /* SQLITE_OMIT_LOAD_EXTENSION */
15059
+SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15060
+SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15061
+SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15062
+SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15063
+
15064
+/*
15065
+** Convenience functions for opening and closing files using
15066
+** sqlite3_malloc() to obtain space for the file-handle structure.
15067
+*/
15068
+SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15069
+SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
15070
+
15071
+#endif /* _SQLITE_OS_H_ */
15072
+
15073
+/************** End of os.h **************************************************/
15074
+/************** Continuing where we left off in sqliteInt.h ******************/
1472715075
/************** Include pager.h in the middle of sqliteInt.h *****************/
1472815076
/************** Begin file pager.h *******************************************/
1472915077
/*
1473015078
** 2001 September 15
1473115079
**
@@ -15576,11 +15924,11 @@
1557615924
#define OP_MustBeInt 13 /* jump */
1557715925
#define OP_Jump 14 /* jump */
1557815926
#define OP_Once 15 /* jump */
1557915927
#define OP_If 16 /* jump */
1558015928
#define OP_IfNot 17 /* jump */
15581
-#define OP_IsNullOrType 18 /* jump, synopsis: if typeof(r[P1]) IN (P3,5) goto P2 */
15929
+#define OP_IsType 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
1558215930
#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
1558315931
#define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
1558415932
#define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
1558515933
#define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
1558615934
#define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
@@ -15759,11 +16107,11 @@
1575916107
#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
1576016108
#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
1576116109
#define OPFLG_INITIALIZER {\
1576216110
/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
1576316111
/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
15764
-/* 16 */ 0x03, 0x03, 0x03, 0x12, 0x01, 0x09, 0x09, 0x09,\
16112
+/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x09, 0x09, 0x09,\
1576516113
/* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
1576616114
/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
1576716115
/* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\
1576816116
/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
1576916117
/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\
@@ -15856,10 +16204,11 @@
1585616204
SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
1585716205
SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
1585816206
SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
1585916207
SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
1586016208
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
16209
+SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe*, int);
1586116210
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
1586216211
SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
1586316212
SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
1586416213
SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
1586516214
#ifdef SQLITE_DEBUG
@@ -16218,301 +16567,10 @@
1621816567
#endif
1621916568
1622016569
#endif /* _PCACHE_H_ */
1622116570
1622216571
/************** End of pcache.h **********************************************/
16223
-/************** Continuing where we left off in sqliteInt.h ******************/
16224
-/************** Include os.h in the middle of sqliteInt.h ********************/
16225
-/************** Begin file os.h **********************************************/
16226
-/*
16227
-** 2001 September 16
16228
-**
16229
-** The author disclaims copyright to this source code. In place of
16230
-** a legal notice, here is a blessing:
16231
-**
16232
-** May you do good and not evil.
16233
-** May you find forgiveness for yourself and forgive others.
16234
-** May you share freely, never taking more than you give.
16235
-**
16236
-******************************************************************************
16237
-**
16238
-** This header file (together with is companion C source-code file
16239
-** "os.c") attempt to abstract the underlying operating system so that
16240
-** the SQLite library will work on both POSIX and windows systems.
16241
-**
16242
-** This header file is #include-ed by sqliteInt.h and thus ends up
16243
-** being included by every source file.
16244
-*/
16245
-#ifndef _SQLITE_OS_H_
16246
-#define _SQLITE_OS_H_
16247
-
16248
-/*
16249
-** Attempt to automatically detect the operating system and setup the
16250
-** necessary pre-processor macros for it.
16251
-*/
16252
-/************** Include os_setup.h in the middle of os.h *********************/
16253
-/************** Begin file os_setup.h ****************************************/
16254
-/*
16255
-** 2013 November 25
16256
-**
16257
-** The author disclaims copyright to this source code. In place of
16258
-** a legal notice, here is a blessing:
16259
-**
16260
-** May you do good and not evil.
16261
-** May you find forgiveness for yourself and forgive others.
16262
-** May you share freely, never taking more than you give.
16263
-**
16264
-******************************************************************************
16265
-**
16266
-** This file contains pre-processor directives related to operating system
16267
-** detection and/or setup.
16268
-*/
16269
-#ifndef SQLITE_OS_SETUP_H
16270
-#define SQLITE_OS_SETUP_H
16271
-
16272
-/*
16273
-** Figure out if we are dealing with Unix, Windows, or some other operating
16274
-** system.
16275
-**
16276
-** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
16277
-** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
16278
-** the three will be 1. The other two will be 0.
16279
-*/
16280
-#if defined(SQLITE_OS_OTHER)
16281
-# if SQLITE_OS_OTHER==1
16282
-# undef SQLITE_OS_UNIX
16283
-# define SQLITE_OS_UNIX 0
16284
-# undef SQLITE_OS_WIN
16285
-# define SQLITE_OS_WIN 0
16286
-# else
16287
-# undef SQLITE_OS_OTHER
16288
-# endif
16289
-#endif
16290
-#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
16291
-# define SQLITE_OS_OTHER 0
16292
-# ifndef SQLITE_OS_WIN
16293
-# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
16294
- defined(__MINGW32__) || defined(__BORLANDC__)
16295
-# define SQLITE_OS_WIN 1
16296
-# define SQLITE_OS_UNIX 0
16297
-# else
16298
-# define SQLITE_OS_WIN 0
16299
-# define SQLITE_OS_UNIX 1
16300
-# endif
16301
-# else
16302
-# define SQLITE_OS_UNIX 0
16303
-# endif
16304
-#else
16305
-# ifndef SQLITE_OS_WIN
16306
-# define SQLITE_OS_WIN 0
16307
-# endif
16308
-#endif
16309
-
16310
-#endif /* SQLITE_OS_SETUP_H */
16311
-
16312
-/************** End of os_setup.h ********************************************/
16313
-/************** Continuing where we left off in os.h *************************/
16314
-
16315
-/* If the SET_FULLSYNC macro is not defined above, then make it
16316
-** a no-op
16317
-*/
16318
-#ifndef SET_FULLSYNC
16319
-# define SET_FULLSYNC(x,y)
16320
-#endif
16321
-
16322
-/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
16323
-*/
16324
-#ifndef SQLITE_MAX_PATHLEN
16325
-# define SQLITE_MAX_PATHLEN FILENAME_MAX
16326
-#endif
16327
-
16328
-/* Maximum number of symlinks that will be resolved while trying to
16329
-** expand a filename in xFullPathname() in the VFS.
16330
-*/
16331
-#ifndef SQLITE_MAX_SYMLINK
16332
-# define SQLITE_MAX_SYMLINK 200
16333
-#endif
16334
-
16335
-/*
16336
-** The default size of a disk sector
16337
-*/
16338
-#ifndef SQLITE_DEFAULT_SECTOR_SIZE
16339
-# define SQLITE_DEFAULT_SECTOR_SIZE 4096
16340
-#endif
16341
-
16342
-/*
16343
-** Temporary files are named starting with this prefix followed by 16 random
16344
-** alphanumeric characters, and no file extension. They are stored in the
16345
-** OS's standard temporary file directory, and are deleted prior to exit.
16346
-** If sqlite is being embedded in another program, you may wish to change the
16347
-** prefix to reflect your program's name, so that if your program exits
16348
-** prematurely, old temporary files can be easily identified. This can be done
16349
-** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16350
-**
16351
-** 2006-10-31: The default prefix used to be "sqlite_". But then
16352
-** Mcafee started using SQLite in their anti-virus product and it
16353
-** started putting files with the "sqlite" name in the c:/temp folder.
16354
-** This annoyed many windows users. Those users would then do a
16355
-** Google search for "sqlite", find the telephone numbers of the
16356
-** developers and call to wake them up at night and complain.
16357
-** For this reason, the default name prefix is changed to be "sqlite"
16358
-** spelled backwards. So the temp files are still identified, but
16359
-** anybody smart enough to figure out the code is also likely smart
16360
-** enough to know that calling the developer will not help get rid
16361
-** of the file.
16362
-*/
16363
-#ifndef SQLITE_TEMP_FILE_PREFIX
16364
-# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
16365
-#endif
16366
-
16367
-/*
16368
-** The following values may be passed as the second argument to
16369
-** sqlite3OsLock(). The various locks exhibit the following semantics:
16370
-**
16371
-** SHARED: Any number of processes may hold a SHARED lock simultaneously.
16372
-** RESERVED: A single process may hold a RESERVED lock on a file at
16373
-** any time. Other processes may hold and obtain new SHARED locks.
16374
-** PENDING: A single process may hold a PENDING lock on a file at
16375
-** any one time. Existing SHARED locks may persist, but no new
16376
-** SHARED locks may be obtained by other processes.
16377
-** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16378
-**
16379
-** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16380
-** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16381
-** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16382
-** sqlite3OsLock().
16383
-*/
16384
-#define NO_LOCK 0
16385
-#define SHARED_LOCK 1
16386
-#define RESERVED_LOCK 2
16387
-#define PENDING_LOCK 3
16388
-#define EXCLUSIVE_LOCK 4
16389
-
16390
-/*
16391
-** File Locking Notes: (Mostly about windows but also some info for Unix)
16392
-**
16393
-** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16394
-** those functions are not available. So we use only LockFile() and
16395
-** UnlockFile().
16396
-**
16397
-** LockFile() prevents not just writing but also reading by other processes.
16398
-** A SHARED_LOCK is obtained by locking a single randomly-chosen
16399
-** byte out of a specific range of bytes. The lock byte is obtained at
16400
-** random so two separate readers can probably access the file at the
16401
-** same time, unless they are unlucky and choose the same lock byte.
16402
-** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16403
-** There can only be one writer. A RESERVED_LOCK is obtained by locking
16404
-** a single byte of the file that is designated as the reserved lock byte.
16405
-** A PENDING_LOCK is obtained by locking a designated byte different from
16406
-** the RESERVED_LOCK byte.
16407
-**
16408
-** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16409
-** which means we can use reader/writer locks. When reader/writer locks
16410
-** are used, the lock is placed on the same range of bytes that is used
16411
-** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16412
-** will support two or more Win95 readers or two or more WinNT readers.
16413
-** But a single Win95 reader will lock out all WinNT readers and a single
16414
-** WinNT reader will lock out all other Win95 readers.
16415
-**
16416
-** The following #defines specify the range of bytes used for locking.
16417
-** SHARED_SIZE is the number of bytes available in the pool from which
16418
-** a random byte is selected for a shared lock. The pool of bytes for
16419
-** shared locks begins at SHARED_FIRST.
16420
-**
16421
-** The same locking strategy and
16422
-** byte ranges are used for Unix. This leaves open the possibility of having
16423
-** clients on win95, winNT, and unix all talking to the same shared file
16424
-** and all locking correctly. To do so would require that samba (or whatever
16425
-** tool is being used for file sharing) implements locks correctly between
16426
-** windows and unix. I'm guessing that isn't likely to happen, but by
16427
-** using the same locking range we are at least open to the possibility.
16428
-**
16429
-** Locking in windows is manditory. For this reason, we cannot store
16430
-** actual data in the bytes used for locking. The pager never allocates
16431
-** the pages involved in locking therefore. SHARED_SIZE is selected so
16432
-** that all locks will fit on a single page even at the minimum page size.
16433
-** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16434
-** is set high so that we don't have to allocate an unused page except
16435
-** for very large databases. But one should test the page skipping logic
16436
-** by setting PENDING_BYTE low and running the entire regression suite.
16437
-**
16438
-** Changing the value of PENDING_BYTE results in a subtly incompatible
16439
-** file format. Depending on how it is changed, you might not notice
16440
-** the incompatibility right away, even running a full regression test.
16441
-** The default location of PENDING_BYTE is the first byte past the
16442
-** 1GB boundary.
16443
-**
16444
-*/
16445
-#ifdef SQLITE_OMIT_WSD
16446
-# define PENDING_BYTE (0x40000000)
16447
-#else
16448
-# define PENDING_BYTE sqlite3PendingByte
16449
-#endif
16450
-#define RESERVED_BYTE (PENDING_BYTE+1)
16451
-#define SHARED_FIRST (PENDING_BYTE+2)
16452
-#define SHARED_SIZE 510
16453
-
16454
-/*
16455
-** Wrapper around OS specific sqlite3_os_init() function.
16456
-*/
16457
-SQLITE_PRIVATE int sqlite3OsInit(void);
16458
-
16459
-/*
16460
-** Functions for accessing sqlite3_file methods
16461
-*/
16462
-SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
16463
-SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16464
-SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16465
-SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
16466
-SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
16467
-SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16468
-SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
16469
-SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
16470
-SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16471
-SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
16472
-SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16473
-#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
16474
-SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
16475
-SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16476
-#ifndef SQLITE_OMIT_WAL
16477
-SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16478
-SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16479
-SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
16480
-SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
16481
-#endif /* SQLITE_OMIT_WAL */
16482
-SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16483
-SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16484
-
16485
-
16486
-/*
16487
-** Functions for accessing sqlite3_vfs methods
16488
-*/
16489
-SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16490
-SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16491
-SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16492
-SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16493
-#ifndef SQLITE_OMIT_LOAD_EXTENSION
16494
-SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16495
-SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16496
-SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16497
-SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
16498
-#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16499
-SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16500
-SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
16501
-SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
16502
-SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16503
-
16504
-/*
16505
-** Convenience functions for opening and closing files using
16506
-** sqlite3_malloc() to obtain space for the file-handle structure.
16507
-*/
16508
-SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16509
-SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
16510
-
16511
-#endif /* _SQLITE_OS_H_ */
16512
-
16513
-/************** End of os.h **************************************************/
1651416572
/************** Continuing where we left off in sqliteInt.h ******************/
1651516573
/************** Include mutex.h in the middle of sqliteInt.h *****************/
1651616574
/************** Begin file mutex.h *******************************************/
1651716575
/*
1651816576
** 2007 August 28
@@ -17100,10 +17158,11 @@
1710017158
#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
1710117159
#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
1710217160
#define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */
1710317161
#define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */
1710417162
/* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
17163
+#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */
1710517164
#define SQLITE_AllOpts 0xffffffff /* All optimizations */
1710617165
1710717166
/*
1710817167
** Macros for testing whether or not optimizations are enabled or disabled.
1710917168
*/
@@ -17672,11 +17731,11 @@
1767217731
** table support is omitted from the build.
1767317732
*/
1767417733
#ifndef SQLITE_OMIT_VIRTUALTABLE
1767517734
# define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
1767617735
# define ExprIsVtab(X) \
17677
- ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB)
17736
+ ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
1767817737
#else
1767917738
# define IsVirtual(X) 0
1768017739
# define ExprIsVtab(X) 0
1768117740
#endif
1768217741
@@ -17889,13 +17948,25 @@
1788917948
** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
1789017949
**
1789117950
** The Index.onError field determines whether or not the indexed columns
1789217951
** must be unique and what to do if they are not. When Index.onError=OE_None,
1789317952
** it means this is not a unique index. Otherwise it is a unique index
17894
-** and the value of Index.onError indicate the which conflict resolution
17895
-** algorithm to employ whenever an attempt is made to insert a non-unique
17953
+** and the value of Index.onError indicates which conflict resolution
17954
+** algorithm to employ when an attempt is made to insert a non-unique
1789617955
** element.
17956
+**
17957
+** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
17958
+** for a fast test to see if an index can serve as a covering index.
17959
+** colNotIdxed has a 1 bit for every column of the original table that
17960
+** is *not* available in the index. Thus the expression
17961
+** "colUsed & colNotIdxed" will be non-zero if the index is not a
17962
+** covering index. The most significant bit of of colNotIdxed will always
17963
+** be true (note-20221022-a). If a column beyond the 63rd column of the
17964
+** table is used, the "colUsed & colNotIdxed" test will always be non-zero
17965
+** and we have to assume either that the index is not covering, or use
17966
+** an alternative (slower) algorithm to determine whether or not
17967
+** the index is covering.
1789717968
**
1789817969
** While parsing a CREATE TABLE or CREATE INDEX statement in order to
1789917970
** generate VDBE code (as opposed to parsing one read from an sqlite_schema
1790017971
** table as part of parsing an existing database schema), transient instances
1790117972
** of this structure may be created. In this case the Index.tnum variable is
@@ -17928,19 +17999,21 @@
1792817999
unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
1792918000
unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
1793018001
unsigned bNoQuery:1; /* Do not use this index to optimize queries */
1793118002
unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
1793218003
unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18004
+ unsigned bHasExpr:1; /* Index contains an expression, either a literal
18005
+ ** expression, or a reference to a VIRTUAL column */
1793318006
#ifdef SQLITE_ENABLE_STAT4
1793418007
int nSample; /* Number of elements in aSample[] */
1793518008
int nSampleCol; /* Size of IndexSample.anEq[] and so on */
1793618009
tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
1793718010
IndexSample *aSample; /* Samples of the left-most key */
1793818011
tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
1793918012
tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
1794018013
#endif
17941
- Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
18014
+ Bitmask colNotIdxed; /* Unindexed columns in pTab */
1794218015
};
1794318016
1794418017
/*
1794518018
** Allowed values for Index.idxType
1794618019
*/
@@ -18389,10 +18462,18 @@
1838918462
1839018463
/*
1839118464
** The SrcItem object represents a single term in the FROM clause of a query.
1839218465
** The SrcList object is mostly an array of SrcItems.
1839318466
**
18467
+** The jointype starts out showing the join type between the current table
18468
+** and the next table on the list. The parser builds the list this way.
18469
+** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18470
+** jointype expresses the join between the table and the previous table.
18471
+**
18472
+** In the colUsed field, the high-order bit (bit 63) is set if the table
18473
+** contains more than 63 columns and the 64-th or later column is used.
18474
+**
1839418475
** Union member validity:
1839518476
**
1839618477
** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
1839718478
** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
1839818479
** u2.pIBIndex fg.isIndexedBy && !fg.isCte
@@ -18428,18 +18509,18 @@
1842818509
int iCursor; /* The VDBE cursor number used to access this table */
1842918510
union {
1843018511
Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
1843118512
IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
1843218513
} u3;
18433
- Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
18514
+ Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
1843418515
union {
1843518516
char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
1843618517
ExprList *pFuncArg; /* Arguments to table-valued-function */
1843718518
} u1;
1843818519
union {
1843918520
Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18440
- CteUse *pCteUse; /* CTE Usage info info fg.isCte is true */
18521
+ CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
1844118522
} u2;
1844218523
};
1844318524
1844418525
/*
1844518526
** The OnOrUsing object represents either an ON clause or a USING clause.
@@ -18449,27 +18530,15 @@
1844918530
Expr *pOn; /* The ON clause of a join */
1845018531
IdList *pUsing; /* The USING clause of a join */
1845118532
};
1845218533
1845318534
/*
18454
-** The following structure describes the FROM clause of a SELECT statement.
18455
-** Each table or subquery in the FROM clause is a separate element of
18456
-** the SrcList.a[] array.
18457
-**
18458
-** With the addition of multiple database support, the following structure
18459
-** can also be used to describe a particular table such as the table that
18460
-** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
18461
-** such a table must be a simple name: ID. But in SQLite, the table can
18462
-** now be identified by a database name, a dot, then the table name: ID.ID.
18463
-**
18464
-** The jointype starts out showing the join type between the current table
18465
-** and the next table on the list. The parser builds the list this way.
18466
-** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18467
-** jointype expresses the join between the table and the previous table.
18468
-**
18469
-** In the colUsed field, the high-order bit (bit 63) is set if the table
18470
-** contains more than 63 columns and the 64-th or later column is used.
18535
+** This object represents one or more tables that are the source of
18536
+** content for an SQL statement. For example, a single SrcList object
18537
+** is used to hold the FROM clause of a SELECT statement. SrcList also
18538
+** represents the target tables for DELETE, INSERT, and UPDATE statements.
18539
+**
1847118540
*/
1847218541
struct SrcList {
1847318542
int nSrc; /* Number of tables or subqueries in the FROM clause */
1847418543
u32 nAlloc; /* Number of entries allocated in a[] below */
1847518544
SrcItem a[1]; /* One entry for each identifier on the list */
@@ -18875,10 +18944,32 @@
1887518944
# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
1887618945
# define DbMaskAllZero(M) (M)==0
1887718946
# define DbMaskNonZero(M) (M)!=0
1887818947
#endif
1887918948
18949
+/*
18950
+** For each index X that has as one of its arguments either an expression
18951
+** or the name of a virtual generated column, and if X is in scope such that
18952
+** the value of the expression can simply be read from the index, then
18953
+** there is an instance of this object on the Parse.pIdxExpr list.
18954
+**
18955
+** During code generation, while generating code to evaluate expressions,
18956
+** this list is consulted and if a matching expression is found, the value
18957
+** is read from the index rather than being recomputed.
18958
+*/
18959
+struct IndexedExpr {
18960
+ Expr *pExpr; /* The expression contained in the index */
18961
+ int iDataCur; /* The data cursor associated with the index */
18962
+ int iIdxCur; /* The index cursor */
18963
+ int iIdxCol; /* The index column that contains value of pExpr */
18964
+ u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
18965
+ IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
18966
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
18967
+ const char *zIdxName; /* Name of index, used only for bytecode comments */
18968
+#endif
18969
+};
18970
+
1888018971
/*
1888118972
** An instance of the ParseCleanup object specifies an operation that
1888218973
** should be performed after parsing to deallocation resources obtained
1888318974
** during the parse and which are no longer needed.
1888418975
*/
@@ -18916,11 +19007,11 @@
1891619007
u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
1891719008
u8 mayAbort; /* True if statement may throw an ABORT exception */
1891819009
u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
1891919010
u8 okConstFactor; /* OK to factor out constants */
1892019011
u8 disableLookaside; /* Number of times lookaside has been disabled */
18921
- u8 disableVtab; /* Disable all virtual tables for this parse */
19012
+ u8 prepFlags; /* SQLITE_PREPARE_* flags */
1892219013
u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
1892319014
#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
1892419015
u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
1892519016
#endif
1892619017
int nRangeReg; /* Size of the temporary register block */
@@ -18933,10 +19024,11 @@
1893319024
** of the base register during check-constraint eval */
1893419025
int nLabel; /* The *negative* of the number of labels used */
1893519026
int nLabelAlloc; /* Number of slots in aLabel */
1893619027
int *aLabel; /* Space to hold the labels */
1893719028
ExprList *pConstExpr;/* Constant expressions */
19029
+ IndexedExpr *pIdxExpr;/* List of expressions used by active indexes */
1893819030
Token constraintName;/* Name of the constraint currently being parsed */
1893919031
yDbMask writeMask; /* Start a write transaction on these databases */
1894019032
yDbMask cookieMask; /* Bitmask of schema verified databases */
1894119033
int regRowid; /* Register holding rowid of CREATE TABLE entry */
1894219034
int regRoot; /* Register holding root page number for new objects */
@@ -19368,19 +19460,19 @@
1936819460
SrcList *pSrcList; /* FROM clause */
1936919461
struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
1937019462
struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
1937119463
int *aiCol; /* array of column indexes */
1937219464
struct IdxCover *pIdxCover; /* Check for index coverage */
19373
- struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
1937419465
ExprList *pGroupBy; /* GROUP BY clause */
1937519466
Select *pSelect; /* HAVING to WHERE clause ctx */
1937619467
struct WindowRewrite *pRewrite; /* Window rewrite context */
1937719468
struct WhereConst *pConst; /* WHERE clause constants */
1937819469
struct RenameCtx *pRename; /* RENAME COLUMN context */
1937919470
struct Table *pTab; /* Table of generated column */
19471
+ struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
1938019472
SrcItem *pSrcItem; /* A single FROM clause item */
19381
- DbFixer *pFix;
19473
+ DbFixer *pFix; /* See sqlite3FixSelect() */
1938219474
} u;
1938319475
};
1938419476
1938519477
/*
1938619478
** The following structure contains information used by the sqliteFix...
@@ -19703,16 +19795,20 @@
1970319795
** The alloca() routine never returns NULL. This will cause code paths
1970419796
** that deal with sqlite3StackAlloc() failures to be unreachable.
1970519797
*/
1970619798
#ifdef SQLITE_USE_ALLOCA
1970719799
# define sqlite3StackAllocRaw(D,N) alloca(N)
19800
+# define sqlite3StackAllocRawNN(D,N) alloca(N)
1970819801
# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
1970919802
# define sqlite3StackFree(D,P)
19803
+# define sqlite3StackFreeNN(D,P)
1971019804
#else
1971119805
# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
19806
+# define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
1971219807
# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
1971319808
# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
19809
+# define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P)
1971419810
#endif
1971519811
1971619812
/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
1971719813
** are, disable MEMSYS3
1971819814
*/
@@ -20767,10 +20863,14 @@
2076720863
SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
2076820864
2076920865
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2077020866
SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
2077120867
#endif
20868
+
20869
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20870
+SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20871
+#endif
2077220872
2077320873
#endif /* SQLITEINT_H */
2077420874
2077520875
/************** End of sqliteInt.h *******************************************/
2077620876
/************** Begin file os_common.h ***************************************/
@@ -20999,11 +21099,11 @@
2099921099
/*
2100021100
** Include the configuration header output by 'configure' if we're using the
2100121101
** autoconf-based build
2100221102
*/
2100321103
#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21004
-/* #include "config.h" */
21104
+/* #include "sqlite_cfg.h" */
2100521105
#define SQLITECONFIG_H 1
2100621106
#endif
2100721107
2100821108
/* These macros are provided to "stringify" the value of the define
2100921109
** for those options in which the value is meaningful. */
@@ -21164,10 +21264,13 @@
2116421264
"DISABLE_PAGECACHE_OVERFLOW_STATS",
2116521265
#endif
2116621266
#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
2116721267
"DISABLE_SKIPAHEAD_DISTINCT",
2116821268
#endif
21269
+#ifdef SQLITE_DQS
21270
+ "DQS=" CTIMEOPT_VAL(SQLITE_DQS),
21271
+#endif
2116921272
#ifdef SQLITE_ENABLE_8_3_NAMES
2117021273
"ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
2117121274
#endif
2117221275
#ifdef SQLITE_ENABLE_API_ARMOR
2117321276
"ENABLE_API_ARMOR",
@@ -24740,13 +24843,15 @@
2474024843
DO_OS_MALLOC_TEST(id);
2474124844
return id->pMethods->xFileSize(id, pSize);
2474224845
}
2474324846
SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
2474424847
DO_OS_MALLOC_TEST(id);
24848
+ assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE );
2474524849
return id->pMethods->xLock(id, lockType);
2474624850
}
2474724851
SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24852
+ assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED );
2474824853
return id->pMethods->xUnlock(id, lockType);
2474924854
}
2475024855
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
2475124856
DO_OS_MALLOC_TEST(id);
2475224857
return id->pMethods->xCheckReservedLock(id, pResOut);
@@ -35382,11 +35487,11 @@
3538235487
/* 13 */ "MustBeInt" OpHelp(""),
3538335488
/* 14 */ "Jump" OpHelp(""),
3538435489
/* 15 */ "Once" OpHelp(""),
3538535490
/* 16 */ "If" OpHelp(""),
3538635491
/* 17 */ "IfNot" OpHelp(""),
35387
- /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35492
+ /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
3538835493
/* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
3538935494
/* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
3539035495
/* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
3539135496
/* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
3539235497
/* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
@@ -35557,10 +35662,982 @@
3555735662
return azName[i];
3555835663
}
3555935664
#endif
3556035665
3556135666
/************** End of opcodes.c *********************************************/
35667
+/************** Begin file os_kv.c *******************************************/
35668
+/*
35669
+** 2022-09-06
35670
+**
35671
+** The author disclaims copyright to this source code. In place of
35672
+** a legal notice, here is a blessing:
35673
+**
35674
+** May you do good and not evil.
35675
+** May you find forgiveness for yourself and forgive others.
35676
+** May you share freely, never taking more than you give.
35677
+**
35678
+******************************************************************************
35679
+**
35680
+** This file contains an experimental VFS layer that operates on a
35681
+** Key/Value storage engine where both keys and values must be pure
35682
+** text.
35683
+*/
35684
+/* #include <sqliteInt.h> */
35685
+#if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL))
35686
+
35687
+/*****************************************************************************
35688
+** Debugging logic
35689
+*/
35690
+
35691
+/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
35692
+#if 0
35693
+#define SQLITE_KV_TRACE(X) printf X
35694
+#else
35695
+#define SQLITE_KV_TRACE(X)
35696
+#endif
35697
+
35698
+/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
35699
+#if 0
35700
+#define SQLITE_KV_LOG(X) printf X
35701
+#else
35702
+#define SQLITE_KV_LOG(X)
35703
+#endif
35704
+
35705
+
35706
+/*
35707
+** Forward declaration of objects used by this VFS implementation
35708
+*/
35709
+typedef struct KVVfsFile KVVfsFile;
35710
+
35711
+/* A single open file. There are only two files represented by this
35712
+** VFS - the database and the rollback journal.
35713
+*/
35714
+struct KVVfsFile {
35715
+ sqlite3_file base; /* IO methods */
35716
+ const char *zClass; /* Storage class */
35717
+ int isJournal; /* True if this is a journal file */
35718
+ unsigned int nJrnl; /* Space allocated for aJrnl[] */
35719
+ char *aJrnl; /* Journal content */
35720
+ int szPage; /* Last known page size */
35721
+ sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35722
+};
35723
+
35724
+/*
35725
+** Methods for KVVfsFile
35726
+*/
35727
+static int kvvfsClose(sqlite3_file*);
35728
+static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35729
+static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35730
+static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35731
+static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35732
+static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
35733
+static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
35734
+static int kvvfsSyncDb(sqlite3_file*, int flags);
35735
+static int kvvfsSyncJrnl(sqlite3_file*, int flags);
35736
+static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
35737
+static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
35738
+static int kvvfsLock(sqlite3_file*, int);
35739
+static int kvvfsUnlock(sqlite3_file*, int);
35740
+static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
35741
+static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
35742
+static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
35743
+static int kvvfsSectorSize(sqlite3_file*);
35744
+static int kvvfsDeviceCharacteristics(sqlite3_file*);
35745
+
35746
+/*
35747
+** Methods for sqlite3_vfs
35748
+*/
35749
+static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
35750
+static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
35751
+static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
35752
+static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
35753
+static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
35754
+static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
35755
+static int kvvfsSleep(sqlite3_vfs*, int microseconds);
35756
+static int kvvfsCurrentTime(sqlite3_vfs*, double*);
35757
+static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
35758
+
35759
+static sqlite3_vfs sqlite3OsKvvfsObject = {
35760
+ 1, /* iVersion */
35761
+ sizeof(KVVfsFile), /* szOsFile */
35762
+ 1024, /* mxPathname */
35763
+ 0, /* pNext */
35764
+ "kvvfs", /* zName */
35765
+ 0, /* pAppData */
35766
+ kvvfsOpen, /* xOpen */
35767
+ kvvfsDelete, /* xDelete */
35768
+ kvvfsAccess, /* xAccess */
35769
+ kvvfsFullPathname, /* xFullPathname */
35770
+ kvvfsDlOpen, /* xDlOpen */
35771
+ 0, /* xDlError */
35772
+ 0, /* xDlSym */
35773
+ 0, /* xDlClose */
35774
+ kvvfsRandomness, /* xRandomness */
35775
+ kvvfsSleep, /* xSleep */
35776
+ kvvfsCurrentTime, /* xCurrentTime */
35777
+ 0, /* xGetLastError */
35778
+ kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
35779
+};
35780
+
35781
+/* Methods for sqlite3_file objects referencing a database file
35782
+*/
35783
+static sqlite3_io_methods kvvfs_db_io_methods = {
35784
+ 1, /* iVersion */
35785
+ kvvfsClose, /* xClose */
35786
+ kvvfsReadDb, /* xRead */
35787
+ kvvfsWriteDb, /* xWrite */
35788
+ kvvfsTruncateDb, /* xTruncate */
35789
+ kvvfsSyncDb, /* xSync */
35790
+ kvvfsFileSizeDb, /* xFileSize */
35791
+ kvvfsLock, /* xLock */
35792
+ kvvfsUnlock, /* xUnlock */
35793
+ kvvfsCheckReservedLock, /* xCheckReservedLock */
35794
+ kvvfsFileControlDb, /* xFileControl */
35795
+ kvvfsSectorSize, /* xSectorSize */
35796
+ kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35797
+ 0, /* xShmMap */
35798
+ 0, /* xShmLock */
35799
+ 0, /* xShmBarrier */
35800
+ 0, /* xShmUnmap */
35801
+ 0, /* xFetch */
35802
+ 0 /* xUnfetch */
35803
+};
35804
+
35805
+/* Methods for sqlite3_file objects referencing a rollback journal
35806
+*/
35807
+static sqlite3_io_methods kvvfs_jrnl_io_methods = {
35808
+ 1, /* iVersion */
35809
+ kvvfsClose, /* xClose */
35810
+ kvvfsReadJrnl, /* xRead */
35811
+ kvvfsWriteJrnl, /* xWrite */
35812
+ kvvfsTruncateJrnl, /* xTruncate */
35813
+ kvvfsSyncJrnl, /* xSync */
35814
+ kvvfsFileSizeJrnl, /* xFileSize */
35815
+ kvvfsLock, /* xLock */
35816
+ kvvfsUnlock, /* xUnlock */
35817
+ kvvfsCheckReservedLock, /* xCheckReservedLock */
35818
+ kvvfsFileControlJrnl, /* xFileControl */
35819
+ kvvfsSectorSize, /* xSectorSize */
35820
+ kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35821
+ 0, /* xShmMap */
35822
+ 0, /* xShmLock */
35823
+ 0, /* xShmBarrier */
35824
+ 0, /* xShmUnmap */
35825
+ 0, /* xFetch */
35826
+ 0 /* xUnfetch */
35827
+};
35828
+
35829
+/****** Storage subsystem **************************************************/
35830
+#include <sys/types.h>
35831
+#include <sys/stat.h>
35832
+#include <unistd.h>
35833
+
35834
+/* Forward declarations for the low-level storage engine
35835
+*/
35836
+static int kvstorageWrite(const char*, const char *zKey, const char *zData);
35837
+static int kvstorageDelete(const char*, const char *zKey);
35838
+static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
35839
+#define KVSTORAGE_KEY_SZ 32
35840
+
35841
+/* Expand the key name with an appropriate prefix and put the result
35842
+** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
35843
+** KVSTORAGE_KEY_SZ bytes.
35844
+*/
35845
+static void kvstorageMakeKey(
35846
+ const char *zClass,
35847
+ const char *zKeyIn,
35848
+ char *zKeyOut
35849
+){
35850
+ sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
35851
+}
35852
+
35853
+/* Write content into a key. zClass is the particular namespace of the
35854
+** underlying key/value store to use - either "local" or "session".
35855
+**
35856
+** Both zKey and zData are zero-terminated pure text strings.
35857
+**
35858
+** Return the number of errors.
35859
+*/
35860
+static int kvstorageWrite(
35861
+ const char *zClass,
35862
+ const char *zKey,
35863
+ const char *zData
35864
+){
35865
+ FILE *fd;
35866
+ char zXKey[KVSTORAGE_KEY_SZ];
35867
+ kvstorageMakeKey(zClass, zKey, zXKey);
35868
+ fd = fopen(zXKey, "wb");
35869
+ if( fd ){
35870
+ SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
35871
+ (int)strlen(zData), zData,
35872
+ strlen(zData)>50 ? "..." : ""));
35873
+ fputs(zData, fd);
35874
+ fclose(fd);
35875
+ return 0;
35876
+ }else{
35877
+ return 1;
35878
+ }
35879
+}
35880
+
35881
+/* Delete a key (with its corresponding data) from the key/value
35882
+** namespace given by zClass. If the key does not previously exist,
35883
+** this routine is a no-op.
35884
+*/
35885
+static int kvstorageDelete(const char *zClass, const char *zKey){
35886
+ char zXKey[KVSTORAGE_KEY_SZ];
35887
+ kvstorageMakeKey(zClass, zKey, zXKey);
35888
+ unlink(zXKey);
35889
+ SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
35890
+ return 0;
35891
+}
35892
+
35893
+/* Read the value associated with a zKey from the key/value namespace given
35894
+** by zClass and put the text data associated with that key in the first
35895
+** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
35896
+** enough to hold it all. The value put into zBuf must always be zero
35897
+** terminated, even if it gets truncated because nBuf is not large enough.
35898
+**
35899
+** Return the total number of bytes in the data, without truncation, and
35900
+** not counting the final zero terminator. Return -1 if the key does
35901
+** not exist.
35902
+**
35903
+** If nBuf<=0 then this routine simply returns the size of the data without
35904
+** actually reading it.
35905
+*/
35906
+static int kvstorageRead(
35907
+ const char *zClass,
35908
+ const char *zKey,
35909
+ char *zBuf,
35910
+ int nBuf
35911
+){
35912
+ FILE *fd;
35913
+ struct stat buf;
35914
+ char zXKey[KVSTORAGE_KEY_SZ];
35915
+ kvstorageMakeKey(zClass, zKey, zXKey);
35916
+ if( access(zXKey, R_OK)!=0
35917
+ || stat(zXKey, &buf)!=0
35918
+ || !S_ISREG(buf.st_mode)
35919
+ ){
35920
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35921
+ return -1;
35922
+ }
35923
+ if( nBuf<=0 ){
35924
+ return (int)buf.st_size;
35925
+ }else if( nBuf==1 ){
35926
+ zBuf[0] = 0;
35927
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
35928
+ (int)buf.st_size));
35929
+ return (int)buf.st_size;
35930
+ }
35931
+ if( nBuf > buf.st_size + 1 ){
35932
+ nBuf = buf.st_size + 1;
35933
+ }
35934
+ fd = fopen(zXKey, "rb");
35935
+ if( fd==0 ){
35936
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35937
+ return -1;
35938
+ }else{
35939
+ sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
35940
+ fclose(fd);
35941
+ zBuf[n] = 0;
35942
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
35943
+ n, zBuf, n>50 ? "..." : ""));
35944
+ return (int)n;
35945
+ }
35946
+}
35947
+
35948
+/*
35949
+** An internal level of indirection which enables us to replace the
35950
+** kvvfs i/o methods with JavaScript implementations in WASM builds.
35951
+** Maintenance reminder: if this struct changes in any way, the JSON
35952
+** rendering of its structure must be updated in
35953
+** sqlite3_wasm_enum_json(). There are no binary compatibility
35954
+** concerns, so it does not need an iVersion member. This file is
35955
+** necessarily always compiled together with sqlite3_wasm_enum_json(),
35956
+** and JS code dynamically creates the mapping of members based on
35957
+** that JSON description.
35958
+*/
35959
+typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
35960
+struct sqlite3_kvvfs_methods {
35961
+ int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
35962
+ int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
35963
+ int (*xDelete)(const char *zClass, const char *zKey);
35964
+ const int nKeySize;
35965
+};
35966
+
35967
+/*
35968
+** This object holds the kvvfs I/O methods which may be swapped out
35969
+** for JavaScript-side implementations in WASM builds. In such builds
35970
+** it cannot be const, but in native builds it should be so that
35971
+** the compiler can hopefully optimize this level of indirection out.
35972
+** That said, kvvfs is intended primarily for use in WASM builds.
35973
+**
35974
+** Note that this is not explicitly flagged as static because the
35975
+** amalgamation build will tag it with SQLITE_PRIVATE.
35976
+*/
35977
+#ifndef SQLITE_WASM
35978
+const
35979
+#endif
35980
+SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
35981
+kvstorageRead,
35982
+kvstorageWrite,
35983
+kvstorageDelete,
35984
+KVSTORAGE_KEY_SZ
35985
+};
35986
+
35987
+/****** Utility subroutines ************************************************/
35988
+
35989
+/*
35990
+** Encode binary into the text encoded used to persist on disk.
35991
+** The output text is stored in aOut[], which must be at least
35992
+** nData+1 bytes in length.
35993
+**
35994
+** Return the actual length of the encoded text, not counting the
35995
+** zero terminator at the end.
35996
+**
35997
+** Encoding format
35998
+** ---------------
35999
+**
36000
+** * Non-zero bytes are encoded as upper-case hexadecimal
36001
+**
36002
+** * A sequence of one or more zero-bytes that are not at the
36003
+** beginning of the buffer are encoded as a little-endian
36004
+** base-26 number using a..z. "a" means 0. "b" means 1,
36005
+** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
36006
+**
36007
+** * Because there is no overlap between the encoding characters
36008
+** of hexadecimal and base-26 numbers, it is always clear where
36009
+** one stops and the next begins.
36010
+*/
36011
+static int kvvfsEncode(const char *aData, int nData, char *aOut){
36012
+ int i, j;
36013
+ const unsigned char *a = (const unsigned char*)aData;
36014
+ for(i=j=0; i<nData; i++){
36015
+ unsigned char c = a[i];
36016
+ if( c!=0 ){
36017
+ aOut[j++] = "0123456789ABCDEF"[c>>4];
36018
+ aOut[j++] = "0123456789ABCDEF"[c&0xf];
36019
+ }else{
36020
+ /* A sequence of 1 or more zeros is stored as a little-endian
36021
+ ** base-26 number using a..z as the digits. So one zero is "b".
36022
+ ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
36023
+ ** and so forth.
36024
+ */
36025
+ int k;
36026
+ for(k=1; i+k<nData && a[i+k]==0; k++){}
36027
+ i += k-1;
36028
+ while( k>0 ){
36029
+ aOut[j++] = 'a'+(k%26);
36030
+ k /= 26;
36031
+ }
36032
+ }
36033
+ }
36034
+ aOut[j] = 0;
36035
+ return j;
36036
+}
36037
+
36038
+static const signed char kvvfsHexValue[256] = {
36039
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36040
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36041
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36042
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36043
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36044
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36045
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36046
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36047
+
36048
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36049
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36050
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36051
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36052
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36053
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36054
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36055
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36056
+};
36057
+
36058
+/*
36059
+** Decode the text encoding back to binary. The binary content is
36060
+** written into pOut, which must be at least nOut bytes in length.
36061
+**
36062
+** The return value is the number of bytes actually written into aOut[].
36063
+*/
36064
+static int kvvfsDecode(const char *a, char *aOut, int nOut){
36065
+ int i, j;
36066
+ int c;
36067
+ const unsigned char *aIn = (const unsigned char*)a;
36068
+ i = 0;
36069
+ j = 0;
36070
+ while( 1 ){
36071
+ c = kvvfsHexValue[aIn[i]];
36072
+ if( c<0 ){
36073
+ int n = 0;
36074
+ int mult = 1;
36075
+ c = aIn[i];
36076
+ if( c==0 ) break;
36077
+ while( c>='a' && c<='z' ){
36078
+ n += (c - 'a')*mult;
36079
+ mult *= 26;
36080
+ c = aIn[++i];
36081
+ }
36082
+ if( j+n>nOut ) return -1;
36083
+ memset(&aOut[j], 0, n);
36084
+ j += n;
36085
+ c = aIn[i];
36086
+ if( c==0 ) break;
36087
+ }else{
36088
+ aOut[j] = c<<4;
36089
+ c = kvvfsHexValue[aIn[++i]];
36090
+ if( c<0 ) break;
36091
+ aOut[j++] += c;
36092
+ i++;
36093
+ }
36094
+ }
36095
+ return j;
36096
+}
36097
+
36098
+/*
36099
+** Decode a complete journal file. Allocate space in pFile->aJrnl
36100
+** and store the decoding there. Or leave pFile->aJrnl set to NULL
36101
+** if an error is encountered.
36102
+**
36103
+** The first few characters of the text encoding will be a little-endian
36104
+** base-26 number (digits a..z) that is the total number of bytes
36105
+** in the decoded journal file image. This base-26 number is followed
36106
+** by a single space, then the encoding of the journal. The space
36107
+** separator is required to act as a terminator for the base-26 number.
36108
+*/
36109
+static void kvvfsDecodeJournal(
36110
+ KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36111
+ const char *zTxt, /* Text encoding. Zero-terminated */
36112
+ int nTxt /* Bytes in zTxt, excluding zero terminator */
36113
+){
36114
+ unsigned int n = 0;
36115
+ int c, i, mult;
36116
+ i = 0;
36117
+ mult = 1;
36118
+ while( (c = zTxt[i++])>='a' && c<='z' ){
36119
+ n += (zTxt[i] - 'a')*mult;
36120
+ mult *= 26;
36121
+ }
36122
+ sqlite3_free(pFile->aJrnl);
36123
+ pFile->aJrnl = sqlite3_malloc64( n );
36124
+ if( pFile->aJrnl==0 ){
36125
+ pFile->nJrnl = 0;
36126
+ return;
36127
+ }
36128
+ pFile->nJrnl = n;
36129
+ n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36130
+ if( n<pFile->nJrnl ){
36131
+ sqlite3_free(pFile->aJrnl);
36132
+ pFile->aJrnl = 0;
36133
+ pFile->nJrnl = 0;
36134
+ }
36135
+}
36136
+
36137
+/*
36138
+** Read or write the "sz" element, containing the database file size.
36139
+*/
36140
+static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
36141
+ char zData[50];
36142
+ zData[0] = 0;
36143
+ sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36144
+ return strtoll(zData, 0, 0);
36145
+}
36146
+static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
36147
+ char zData[50];
36148
+ sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
36149
+ return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36150
+}
36151
+
36152
+/****** sqlite3_io_methods methods ******************************************/
36153
+
36154
+/*
36155
+** Close an kvvfs-file.
36156
+*/
36157
+static int kvvfsClose(sqlite3_file *pProtoFile){
36158
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36159
+
36160
+ SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36161
+ pFile->isJournal ? "journal" : "db"));
36162
+ sqlite3_free(pFile->aJrnl);
36163
+ return SQLITE_OK;
36164
+}
36165
+
36166
+/*
36167
+** Read from the -journal file.
36168
+*/
36169
+static int kvvfsReadJrnl(
36170
+ sqlite3_file *pProtoFile,
36171
+ void *zBuf,
36172
+ int iAmt,
36173
+ sqlite_int64 iOfst
36174
+){
36175
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36176
+ assert( pFile->isJournal );
36177
+ SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36178
+ if( pFile->aJrnl==0 ){
36179
+ int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36180
+ char *aTxt;
36181
+ if( szTxt<=4 ){
36182
+ return SQLITE_IOERR;
36183
+ }
36184
+ aTxt = sqlite3_malloc64( szTxt+1 );
36185
+ if( aTxt==0 ) return SQLITE_NOMEM;
36186
+ kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36187
+ kvvfsDecodeJournal(pFile, aTxt, szTxt);
36188
+ sqlite3_free(aTxt);
36189
+ if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36190
+ }
36191
+ if( iOfst+iAmt>pFile->nJrnl ){
36192
+ return SQLITE_IOERR_SHORT_READ;
36193
+ }
36194
+ memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36195
+ return SQLITE_OK;
36196
+}
36197
+
36198
+/*
36199
+** Read from the database file.
36200
+*/
36201
+static int kvvfsReadDb(
36202
+ sqlite3_file *pProtoFile,
36203
+ void *zBuf,
36204
+ int iAmt,
36205
+ sqlite_int64 iOfst
36206
+){
36207
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36208
+ unsigned int pgno;
36209
+ int got, n;
36210
+ char zKey[30];
36211
+ char aData[133073];
36212
+ assert( iOfst>=0 );
36213
+ assert( iAmt>=0 );
36214
+ SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36215
+ if( iOfst+iAmt>=512 ){
36216
+ if( (iOfst % iAmt)!=0 ){
36217
+ return SQLITE_IOERR_READ;
36218
+ }
36219
+ if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36220
+ return SQLITE_IOERR_READ;
36221
+ }
36222
+ pFile->szPage = iAmt;
36223
+ pgno = 1 + iOfst/iAmt;
36224
+ }else{
36225
+ pgno = 1;
36226
+ }
36227
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36228
+ got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36229
+ if( got<0 ){
36230
+ n = 0;
36231
+ }else{
36232
+ aData[got] = 0;
36233
+ if( iOfst+iAmt<512 ){
36234
+ int k = iOfst+iAmt;
36235
+ aData[k*2] = 0;
36236
+ n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36237
+ if( n>=iOfst+iAmt ){
36238
+ memcpy(zBuf, &aData[2000+iOfst], iAmt);
36239
+ n = iAmt;
36240
+ }else{
36241
+ n = 0;
36242
+ }
36243
+ }else{
36244
+ n = kvvfsDecode(aData, zBuf, iAmt);
36245
+ }
36246
+ }
36247
+ if( n<iAmt ){
36248
+ memset(zBuf+n, 0, iAmt-n);
36249
+ return SQLITE_IOERR_SHORT_READ;
36250
+ }
36251
+ return SQLITE_OK;
36252
+}
36253
+
36254
+
36255
+/*
36256
+** Write into the -journal file.
36257
+*/
36258
+static int kvvfsWriteJrnl(
36259
+ sqlite3_file *pProtoFile,
36260
+ const void *zBuf,
36261
+ int iAmt,
36262
+ sqlite_int64 iOfst
36263
+){
36264
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36265
+ sqlite3_int64 iEnd = iOfst+iAmt;
36266
+ SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36267
+ if( iEnd>=0x10000000 ) return SQLITE_FULL;
36268
+ if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36269
+ char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36270
+ if( aNew==0 ){
36271
+ return SQLITE_IOERR_NOMEM;
36272
+ }
36273
+ pFile->aJrnl = aNew;
36274
+ if( pFile->nJrnl<iOfst ){
36275
+ memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36276
+ }
36277
+ pFile->nJrnl = iEnd;
36278
+ }
36279
+ memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36280
+ return SQLITE_OK;
36281
+}
36282
+
36283
+/*
36284
+** Write into the database file.
36285
+*/
36286
+static int kvvfsWriteDb(
36287
+ sqlite3_file *pProtoFile,
36288
+ const void *zBuf,
36289
+ int iAmt,
36290
+ sqlite_int64 iOfst
36291
+){
36292
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36293
+ unsigned int pgno;
36294
+ char zKey[30];
36295
+ char aData[131073];
36296
+ SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36297
+ assert( iAmt>=512 && iAmt<=65536 );
36298
+ assert( (iAmt & (iAmt-1))==0 );
36299
+ pgno = 1 + iOfst/iAmt;
36300
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36301
+ kvvfsEncode(zBuf, iAmt, aData);
36302
+ if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36303
+ return SQLITE_IOERR;
36304
+ }
36305
+ if( iOfst+iAmt > pFile->szDb ){
36306
+ pFile->szDb = iOfst + iAmt;
36307
+ }
36308
+ return SQLITE_OK;
36309
+}
36310
+
36311
+/*
36312
+** Truncate an kvvfs-file.
36313
+*/
36314
+static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
36315
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36316
+ SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36317
+ assert( size==0 );
36318
+ sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36319
+ sqlite3_free(pFile->aJrnl);
36320
+ pFile->aJrnl = 0;
36321
+ pFile->nJrnl = 0;
36322
+ return SQLITE_OK;
36323
+}
36324
+static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
36325
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36326
+ if( pFile->szDb>size
36327
+ && pFile->szPage>0
36328
+ && (size % pFile->szPage)==0
36329
+ ){
36330
+ char zKey[50];
36331
+ unsigned int pgno, pgnoMax;
36332
+ SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36333
+ pgno = 1 + size/pFile->szPage;
36334
+ pgnoMax = 2 + pFile->szDb/pFile->szPage;
36335
+ while( pgno<=pgnoMax ){
36336
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36337
+ sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36338
+ pgno++;
36339
+ }
36340
+ pFile->szDb = size;
36341
+ return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK;
36342
+ }
36343
+ return SQLITE_IOERR;
36344
+}
36345
+
36346
+/*
36347
+** Sync an kvvfs-file.
36348
+*/
36349
+static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
36350
+ int i, n;
36351
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36352
+ char *zOut;
36353
+ SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36354
+ if( pFile->nJrnl<=0 ){
36355
+ return kvvfsTruncateJrnl(pProtoFile, 0);
36356
+ }
36357
+ zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36358
+ if( zOut==0 ){
36359
+ return SQLITE_IOERR_NOMEM;
36360
+ }
36361
+ n = pFile->nJrnl;
36362
+ i = 0;
36363
+ do{
36364
+ zOut[i++] = 'a' + (n%26);
36365
+ n /= 26;
36366
+ }while( n>0 );
36367
+ zOut[i++] = ' ';
36368
+ kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36369
+ i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36370
+ sqlite3_free(zOut);
36371
+ return i ? SQLITE_IOERR : SQLITE_OK;
36372
+}
36373
+static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
36374
+ return SQLITE_OK;
36375
+}
36376
+
36377
+/*
36378
+** Return the current file-size of an kvvfs-file.
36379
+*/
36380
+static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36381
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36382
+ SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36383
+ *pSize = pFile->nJrnl;
36384
+ return SQLITE_OK;
36385
+}
36386
+static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36387
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36388
+ SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36389
+ if( pFile->szDb>=0 ){
36390
+ *pSize = pFile->szDb;
36391
+ }else{
36392
+ *pSize = kvvfsReadFileSize(pFile);
36393
+ }
36394
+ return SQLITE_OK;
36395
+}
36396
+
36397
+/*
36398
+** Lock an kvvfs-file.
36399
+*/
36400
+static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
36401
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36402
+ assert( !pFile->isJournal );
36403
+ SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36404
+
36405
+ if( eLock!=SQLITE_LOCK_NONE ){
36406
+ pFile->szDb = kvvfsReadFileSize(pFile);
36407
+ }
36408
+ return SQLITE_OK;
36409
+}
36410
+
36411
+/*
36412
+** Unlock an kvvfs-file.
36413
+*/
36414
+static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
36415
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36416
+ assert( !pFile->isJournal );
36417
+ SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36418
+ if( eLock==SQLITE_LOCK_NONE ){
36419
+ pFile->szDb = -1;
36420
+ }
36421
+ return SQLITE_OK;
36422
+}
36423
+
36424
+/*
36425
+** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36426
+*/
36427
+static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
36428
+ SQLITE_KV_LOG(("xCheckReservedLock\n"));
36429
+ *pResOut = 0;
36430
+ return SQLITE_OK;
36431
+}
36432
+
36433
+/*
36434
+** File control method. For custom operations on an kvvfs-file.
36435
+*/
36436
+static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
36437
+ SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
36438
+ return SQLITE_NOTFOUND;
36439
+}
36440
+static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
36441
+ SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
36442
+ if( op==SQLITE_FCNTL_SYNC ){
36443
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36444
+ int rc = SQLITE_OK;
36445
+ SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36446
+ if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36447
+ rc = SQLITE_IOERR;
36448
+ }
36449
+ return rc;
36450
+ }
36451
+ return SQLITE_NOTFOUND;
36452
+}
36453
+
36454
+/*
36455
+** Return the sector-size in bytes for an kvvfs-file.
36456
+*/
36457
+static int kvvfsSectorSize(sqlite3_file *pFile){
36458
+ return 512;
36459
+}
36460
+
36461
+/*
36462
+** Return the device characteristic flags supported by an kvvfs-file.
36463
+*/
36464
+static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
36465
+ return 0;
36466
+}
36467
+
36468
+/****** sqlite3_vfs methods *************************************************/
36469
+
36470
+/*
36471
+** Open an kvvfs file handle.
36472
+*/
36473
+static int kvvfsOpen(
36474
+ sqlite3_vfs *pProtoVfs,
36475
+ const char *zName,
36476
+ sqlite3_file *pProtoFile,
36477
+ int flags,
36478
+ int *pOutFlags
36479
+){
36480
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36481
+ SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36482
+ if( strcmp(zName, "local")==0
36483
+ || strcmp(zName, "session")==0
36484
+ ){
36485
+ pFile->isJournal = 0;
36486
+ pFile->base.pMethods = &kvvfs_db_io_methods;
36487
+ }else
36488
+ if( strcmp(zName, "local-journal")==0
36489
+ || strcmp(zName, "session-journal")==0
36490
+ ){
36491
+ pFile->isJournal = 1;
36492
+ pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36493
+ }else{
36494
+ return SQLITE_CANTOPEN;
36495
+ }
36496
+ if( zName[0]=='s' ){
36497
+ pFile->zClass = "session";
36498
+ }else{
36499
+ pFile->zClass = "local";
36500
+ }
36501
+ pFile->aJrnl = 0;
36502
+ pFile->nJrnl = 0;
36503
+ pFile->szPage = -1;
36504
+ pFile->szDb = -1;
36505
+ return SQLITE_OK;
36506
+}
36507
+
36508
+/*
36509
+** Delete the file located at zPath. If the dirSync argument is true,
36510
+** ensure the file-system modifications are synced to disk before
36511
+** returning.
36512
+*/
36513
+static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
36514
+ if( strcmp(zPath, "local-journal")==0 ){
36515
+ sqlite3KvvfsMethods.xDelete("local", "jrnl");
36516
+ }else
36517
+ if( strcmp(zPath, "session-journal")==0 ){
36518
+ sqlite3KvvfsMethods.xDelete("session", "jrnl");
36519
+ }
36520
+ return SQLITE_OK;
36521
+}
36522
+
36523
+/*
36524
+** Test for access permissions. Return true if the requested permission
36525
+** is available, or false otherwise.
36526
+*/
36527
+static int kvvfsAccess(
36528
+ sqlite3_vfs *pProtoVfs,
36529
+ const char *zPath,
36530
+ int flags,
36531
+ int *pResOut
36532
+){
36533
+ SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
36534
+ if( strcmp(zPath, "local-journal")==0 ){
36535
+ *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
36536
+ }else
36537
+ if( strcmp(zPath, "session-journal")==0 ){
36538
+ *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
36539
+ }else
36540
+ if( strcmp(zPath, "local")==0 ){
36541
+ *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
36542
+ }else
36543
+ if( strcmp(zPath, "session")==0 ){
36544
+ *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
36545
+ }else
36546
+ {
36547
+ *pResOut = 0;
36548
+ }
36549
+ SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
36550
+ return SQLITE_OK;
36551
+}
36552
+
36553
+/*
36554
+** Populate buffer zOut with the full canonical pathname corresponding
36555
+** to the pathname in zPath. zOut is guaranteed to point to a buffer
36556
+** of at least (INST_MAX_PATHNAME+1) bytes.
36557
+*/
36558
+static int kvvfsFullPathname(
36559
+ sqlite3_vfs *pVfs,
36560
+ const char *zPath,
36561
+ int nOut,
36562
+ char *zOut
36563
+){
36564
+ size_t nPath;
36565
+#ifdef SQLITE_OS_KV_ALWAYS_LOCAL
36566
+ zPath = "local";
36567
+#endif
36568
+ nPath = strlen(zPath);
36569
+ SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
36570
+ if( nOut<nPath+1 ) nPath = nOut - 1;
36571
+ memcpy(zOut, zPath, nPath);
36572
+ zOut[nPath] = 0;
36573
+ return SQLITE_OK;
36574
+}
36575
+
36576
+/*
36577
+** Open the dynamic library located at zPath and return a handle.
36578
+*/
36579
+static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
36580
+ return 0;
36581
+}
36582
+
36583
+/*
36584
+** Populate the buffer pointed to by zBufOut with nByte bytes of
36585
+** random data.
36586
+*/
36587
+static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
36588
+ memset(zBufOut, 0, nByte);
36589
+ return nByte;
36590
+}
36591
+
36592
+/*
36593
+** Sleep for nMicro microseconds. Return the number of microseconds
36594
+** actually slept.
36595
+*/
36596
+static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
36597
+ return SQLITE_OK;
36598
+}
36599
+
36600
+/*
36601
+** Return the current time as a Julian Day number in *pTimeOut.
36602
+*/
36603
+static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
36604
+ sqlite3_int64 i = 0;
36605
+ int rc;
36606
+ rc = kvvfsCurrentTimeInt64(0, &i);
36607
+ *pTimeOut = i/86400000.0;
36608
+ return rc;
36609
+}
36610
+#include <sys/time.h>
36611
+static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
36612
+ static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36613
+ struct timeval sNow;
36614
+ (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
36615
+ *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
36616
+ return SQLITE_OK;
36617
+}
36618
+#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
36619
+
36620
+#if SQLITE_OS_KV
36621
+/*
36622
+** This routine is called initialize the KV-vfs as the default VFS.
36623
+*/
36624
+SQLITE_API int sqlite3_os_init(void){
36625
+ return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
36626
+}
36627
+SQLITE_API int sqlite3_os_end(void){
36628
+ return SQLITE_OK;
36629
+}
36630
+#endif /* SQLITE_OS_KV */
36631
+
36632
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
36633
+SQLITE_PRIVATE int sqlite3KvvfsInit(void){
36634
+ return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
36635
+}
36636
+#endif
36637
+
36638
+/************** End of os_kv.c ***********************************************/
3556236639
/************** Begin file os_unix.c *****************************************/
3556336640
/*
3556436641
** 2004 May 22
3556536642
**
3556636643
** The author disclaims copyright to this source code. In place of
@@ -35647,17 +36724,17 @@
3564736724
#endif
3564836725
3564936726
/*
3565036727
** standard include files.
3565136728
*/
35652
-#include <sys/types.h>
35653
-#include <sys/stat.h>
36729
+#include <sys/types.h> /* amalgamator: keep */
36730
+#include <sys/stat.h> /* amalgamator: keep */
3565436731
#include <fcntl.h>
3565536732
#include <sys/ioctl.h>
35656
-#include <unistd.h>
36733
+#include <unistd.h> /* amalgamator: keep */
3565736734
/* #include <time.h> */
35658
-#include <sys/time.h>
36735
+#include <sys/time.h> /* amalgamator: keep */
3565936736
#include <errno.h>
3566036737
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3566136738
# include <sys/mman.h>
3566236739
#endif
3566336740
@@ -43625,10 +44702,13 @@
4362544702
0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
4362644703
#else
4362744704
sqlite3_vfs_register(&aVfs[i], i==0);
4362844705
#endif
4362944706
}
44707
+#ifdef SQLITE_OS_KV_OPTIONAL
44708
+ sqlite3KvvfsInit();
44709
+#endif
4363044710
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
4363144711
4363244712
#ifndef SQLITE_OMIT_WAL
4363344713
/* Validate lock assumptions */
4363444714
assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
@@ -70722,10 +71802,13 @@
7072271802
put4byte(pCell+info.nSize-4, iTo);
7072371803
break;
7072471804
}
7072571805
}
7072671806
}else{
71807
+ if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
71808
+ return SQLITE_CORRUPT_PAGE(pPage);
71809
+ }
7072771810
if( get4byte(pCell)==iFrom ){
7072871811
put4byte(pCell, iTo);
7072971812
break;
7073071813
}
7073171814
}
@@ -73056,18 +74139,11 @@
7305674139
}
7305774140
}
7305874141
7305974142
pPage = pCur->pPage;
7306074143
idx = ++pCur->ix;
73061
- if( !pPage->isInit || sqlite3FaultSim(412) ){
73062
- /* The only known way for this to happen is for there to be a
73063
- ** recursive SQL function that does a DELETE operation as part of a
73064
- ** SELECT which deletes content out from under an active cursor
73065
- ** in a corrupt database file where the table being DELETE-ed from
73066
- ** has pages in common with the table being queried. See TH3
73067
- ** module cov1/btree78.test testcase 220 (2018-06-08) for an
73068
- ** example. */
74144
+ if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
7306974145
return SQLITE_CORRUPT_BKPT;
7307074146
}
7307174147
7307274148
if( idx>=pPage->nCell ){
7307374149
if( !pPage->leaf ){
@@ -75753,10 +76829,15 @@
7575376829
assert( pCur->pPage->nOverflow );
7575476830
}
7575576831
}else{
7575676832
break;
7575776833
}
76834
+ }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
76835
+ /* The page being written is not a root page, and there is currently
76836
+ ** more than one reference to it. This only happens if the page is one
76837
+ ** of its own ancestor pages. Corruption. */
76838
+ rc = SQLITE_CORRUPT_BKPT;
7575876839
}else{
7575976840
MemPage * const pParent = pCur->apPage[iPage-1];
7576076841
int const iIdx = pCur->aiIdx[iPage-1];
7576176842
7576276843
rc = sqlite3PagerWrite(pParent->pDbPage);
@@ -79679,10 +80760,11 @@
7967980760
assert( MEM_Str==(MEM_Blob>>3) );
7968080761
pMem->flags |= (pMem->flags&MEM_Blob)>>3;
7968180762
sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
7968280763
assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
7968380764
pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
80765
+ if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
7968480766
return sqlite3VdbeChangeEncoding(pMem, encoding);
7968580767
}
7968680768
}
7968780769
return SQLITE_OK;
7968880770
}
@@ -80813,10 +81895,13 @@
8081381895
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
8081481896
Mem *p = (Mem*)pVal;
8081581897
assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
8081681898
if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
8081781899
return p->n;
81900
+ }
81901
+ if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
81902
+ return p->n;
8081881903
}
8081981904
if( (p->flags & MEM_Blob)!=0 ){
8082081905
if( p->flags & MEM_Zero ){
8082181906
return p->n + p->u.nZero;
8082281907
}else{
@@ -81983,10 +83068,22 @@
8198383068
}
8198483069
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
8198583070
assert( p->nOp>0 || p->db->mallocFailed );
8198683071
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
8198783072
}
83073
+
83074
+/*
83075
+** If the previous opcode is an OP_Column that delivers results
83076
+** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
83077
+** opcode.
83078
+*/
83079
+SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
83080
+ VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
83081
+ if( pOp->p3==iDest && pOp->opcode==OP_Column ){
83082
+ pOp->p5 |= OPFLAG_TYPEOFARG;
83083
+ }
83084
+}
8198883085
8198983086
/*
8199083087
** Change the P2 operand of instruction addr so that it points to
8199183088
** the address of the next instruction to be coded.
8199283089
*/
@@ -85402,11 +86499,11 @@
8540286499
testcase( pRhs->flags & MEM_Int );
8540386500
testcase( pRhs->flags & MEM_IntReal );
8540486501
serial_type = aKey1[idx1];
8540586502
testcase( serial_type==12 );
8540686503
if( serial_type>=10 ){
85407
- rc = +1;
86504
+ rc = serial_type==10 ? -1 : +1;
8540886505
}else if( serial_type==0 ){
8540986506
rc = -1;
8541086507
}else if( serial_type==7 ){
8541186508
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
8541286509
rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
@@ -85427,11 +86524,11 @@
8542786524
if( serial_type>=10 ){
8542886525
/* Serial types 12 or greater are strings and blobs (greater than
8542986526
** numbers). Types 10 and 11 are currently "reserved for future
8543086527
** use", so it doesn't really matter what the results of comparing
8543186528
** them to numberic values are. */
85432
- rc = +1;
86529
+ rc = serial_type==10 ? -1 : +1;
8543386530
}else if( serial_type==0 ){
8543486531
rc = -1;
8543586532
}else{
8543686533
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
8543786534
if( serial_type==7 ){
@@ -85508,11 +86605,11 @@
8550886605
}
8550986606
8551086607
/* RHS is null */
8551186608
else{
8551286609
serial_type = aKey1[idx1];
85513
- rc = (serial_type!=0);
86610
+ rc = (serial_type!=0 && serial_type!=10);
8551486611
}
8551586612
8551686613
if( rc!=0 ){
8551786614
int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
8551886615
if( sortFlags ){
@@ -86457,10 +87554,13 @@
8645787554
}
8645887555
assert( eType == aType[pVal->flags&MEM_AffMask] );
8645987556
}
8646087557
#endif
8646187558
return aType[pVal->flags&MEM_AffMask];
87559
+}
87560
+SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
87561
+ return pVal->enc;
8646287562
}
8646387563
8646487564
/* Return true if a parameter to xUpdate represents an unchanged column */
8646587565
SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
8646687566
return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
@@ -91094,23 +92194,94 @@
9109492194
goto jump_to_p2;
9109592195
}
9109692196
break;
9109792197
}
9109892198
91099
-/* Opcode: IsNullOrType P1 P2 P3 * *
91100
-** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
92199
+/* Opcode: IsType P1 P2 P3 P4 P5
92200
+** Synopsis: if typeof(P1.P3) in P5 goto P2
9110192201
**
91102
-** Jump to P2 if the value in register P1 is NULL or has a datatype P3.
91103
-** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT,
91104
-** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT.
92202
+** Jump to P2 if the type of a column in a btree is one of the types specified
92203
+** by the P5 bitmask.
92204
+**
92205
+** P1 is normally a cursor on a btree for which the row decode cache is
92206
+** valid through at least column P3. In other words, there should have been
92207
+** a prior OP_Column for column P3 or greater. If the cursor is not valid,
92208
+** then this opcode might give spurious results.
92209
+** The the btree row has fewer than P3 columns, then use P4 as the
92210
+** datatype.
92211
+**
92212
+** If P1 is -1, then P3 is a register number and the datatype is taken
92213
+** from the value in that register.
92214
+**
92215
+** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
92216
+** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
92217
+** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
92218
+**
92219
+** Take the jump to address P2 if and only if the datatype of the
92220
+** value determined by P1 and P3 corresponds to one of the bits in the
92221
+** P5 bitmask.
92222
+**
9110592223
*/
91106
-case OP_IsNullOrType: { /* jump, in1 */
91107
- int doTheJump;
91108
- pIn1 = &aMem[pOp->p1];
91109
- doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
91110
- VdbeBranchTaken( doTheJump, 2);
91111
- if( doTheJump ) goto jump_to_p2;
92224
+case OP_IsType: { /* jump */
92225
+ VdbeCursor *pC;
92226
+ u16 typeMask;
92227
+ u32 serialType;
92228
+
92229
+ assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
92230
+ assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
92231
+ if( pOp->p1>=0 ){
92232
+ pC = p->apCsr[pOp->p1];
92233
+ assert( pC!=0 );
92234
+ assert( pOp->p3>=0 );
92235
+ if( pOp->p3<pC->nHdrParsed ){
92236
+ serialType = pC->aType[pOp->p3];
92237
+ if( serialType>=12 ){
92238
+ if( serialType&1 ){
92239
+ typeMask = 0x04; /* SQLITE_TEXT */
92240
+ }else{
92241
+ typeMask = 0x08; /* SQLITE_BLOB */
92242
+ }
92243
+ }else{
92244
+ static const unsigned char aMask[] = {
92245
+ 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
92246
+ 0x01, 0x01, 0x10, 0x10
92247
+ };
92248
+ testcase( serialType==0 );
92249
+ testcase( serialType==1 );
92250
+ testcase( serialType==2 );
92251
+ testcase( serialType==3 );
92252
+ testcase( serialType==4 );
92253
+ testcase( serialType==5 );
92254
+ testcase( serialType==6 );
92255
+ testcase( serialType==7 );
92256
+ testcase( serialType==8 );
92257
+ testcase( serialType==9 );
92258
+ testcase( serialType==10 );
92259
+ testcase( serialType==11 );
92260
+ typeMask = aMask[serialType];
92261
+ }
92262
+ }else{
92263
+ typeMask = 1 << (pOp->p4.i - 1);
92264
+ testcase( typeMask==0x01 );
92265
+ testcase( typeMask==0x02 );
92266
+ testcase( typeMask==0x04 );
92267
+ testcase( typeMask==0x08 );
92268
+ testcase( typeMask==0x10 );
92269
+ }
92270
+ }else{
92271
+ assert( memIsValid(&aMem[pOp->p3]) );
92272
+ typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
92273
+ testcase( typeMask==0x01 );
92274
+ testcase( typeMask==0x02 );
92275
+ testcase( typeMask==0x04 );
92276
+ testcase( typeMask==0x08 );
92277
+ testcase( typeMask==0x10 );
92278
+ }
92279
+ VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
92280
+ if( typeMask & pOp->p5 ){
92281
+ goto jump_to_p2;
92282
+ }
9111292283
break;
9111392284
}
9111492285
9111592286
/* Opcode: ZeroOrNull P1 P2 P3 * *
9111692287
** Synopsis: r[P2] = 0 OR NULL
@@ -91207,23 +92378,25 @@
9120792378
** Synopsis: r[P3]=PX cursor P1 column P2
9120892379
**
9120992380
** Interpret the data that cursor P1 points to as a structure built using
9121092381
** the MakeRecord instruction. (See the MakeRecord opcode for additional
9121192382
** information about the format of the data.) Extract the P2-th column
91212
-** from this record. If there are less that (P2+1)
92383
+** from this record. If there are less than (P2+1)
9121392384
** values in the record, extract a NULL.
9121492385
**
9121592386
** The value extracted is stored in register P3.
9121692387
**
9121792388
** If the record contains fewer than P2 fields, then extract a NULL. Or,
9121892389
** if the P4 argument is a P4_MEM use the value of the P4 argument as
9121992390
** the result.
9122092391
**
91221
-** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
91222
-** the result is guaranteed to only be used as the argument of a length()
91223
-** or typeof() function, respectively. The loading of large blobs can be
91224
-** skipped for length() and all content loading can be skipped for typeof().
92392
+** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
92393
+** to only be used by the length() function or the equivalent. The content
92394
+** of large blobs is not loaded, thus saving CPU cycles. If the
92395
+** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
92396
+** typeof() function or the IS NULL or IS NOT NULL operators or the
92397
+** equivalent. In this case, all content loading can be omitted.
9122592398
*/
9122692399
case OP_Column: {
9122792400
u32 p2; /* column number to retrieve */
9122892401
VdbeCursor *pC; /* The VDBE cursor */
9122992402
BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
@@ -93158,11 +94331,17 @@
9315894331
assert( oc!=OP_SeekGE || r.default_rc==+1 );
9315994332
assert( oc!=OP_SeekLT || r.default_rc==+1 );
9316094333
9316194334
r.aMem = &aMem[pOp->p3];
9316294335
#ifdef SQLITE_DEBUG
93163
- { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
94336
+ {
94337
+ int i;
94338
+ for(i=0; i<r.nField; i++){
94339
+ assert( memIsValid(&r.aMem[i]) );
94340
+ if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
94341
+ }
94342
+ }
9316494343
#endif
9316594344
r.eqSeen = 0;
9316694345
rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
9316794346
if( rc!=SQLITE_OK ){
9316894347
goto abort_due_to_error;
@@ -93221,54 +94400,76 @@
9322194400
}
9322294401
break;
9322394402
}
9322494403
9322594404
93226
-/* Opcode: SeekScan P1 P2 * * *
94405
+/* Opcode: SeekScan P1 P2 * * P5
9322794406
** Synopsis: Scan-ahead up to P1 rows
9322894407
**
9322994408
** This opcode is a prefix opcode to OP_SeekGE. In other words, this
9323094409
** opcode must be immediately followed by OP_SeekGE. This constraint is
9323194410
** checked by assert() statements.
9323294411
**
9323394412
** This opcode uses the P1 through P4 operands of the subsequent
9323494413
** OP_SeekGE. In the text that follows, the operands of the subsequent
9323594414
** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
93236
-** the P1 and P2 operands of this opcode are also used, and are called
93237
-** This.P1 and This.P2.
94415
+** the P1, P2 and P5 operands of this opcode are also used, and are called
94416
+** This.P1, This.P2 and This.P5.
9323894417
**
9323994418
** This opcode helps to optimize IN operators on a multi-column index
9324094419
** where the IN operator is on the later terms of the index by avoiding
9324194420
** unnecessary seeks on the btree, substituting steps to the next row
9324294421
** of the b-tree instead. A correct answer is obtained if this opcode
9324394422
** is omitted or is a no-op.
9324494423
**
9324594424
** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
9324694425
** is the desired entry that we want the cursor SeekGE.P1 to be pointing
93247
-** to. Call this SeekGE.P4/P5 row the "target".
94426
+** to. Call this SeekGE.P3/P4 row the "target".
9324894427
**
9324994428
** If the SeekGE.P1 cursor is not currently pointing to a valid row,
9325094429
** then this opcode is a no-op and control passes through into the OP_SeekGE.
9325194430
**
9325294431
** If the SeekGE.P1 cursor is pointing to a valid row, then that row
9325394432
** might be the target row, or it might be near and slightly before the
93254
-** target row. This opcode attempts to position the cursor on the target
93255
-** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
93256
-** between 0 and This.P1 times.
93257
-**
93258
-** There are three possible outcomes from this opcode:<ol>
93259
-**
93260
-** <li> If after This.P1 steps, the cursor is still pointing to a place that
93261
-** is earlier in the btree than the target row, then fall through
93262
-** into the subsquence OP_SeekGE opcode.
93263
-**
93264
-** <li> If the cursor is successfully moved to the target row by 0 or more
93265
-** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
93266
-** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
93267
-**
93268
-** <li> If the cursor ends up past the target row (indicating that the target
93269
-** row does not exist in the btree) then jump to SeekOP.P2.
94433
+** target row, or it might be after the target row. If the cursor is
94434
+** currently before the target row, then this opcode attempts to position
94435
+** the cursor on or after the target row by invoking sqlite3BtreeStep()
94436
+** on the cursor between 1 and This.P1 times.
94437
+**
94438
+** The This.P5 parameter is a flag that indicates what to do if the
94439
+** cursor ends up pointing at a valid row that is past the target
94440
+** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
94441
+** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
94442
+** case occurs when there are no inequality constraints to the right of
94443
+** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
94444
+** occurs when there are inequality constraints to the right of the IN
94445
+** operator. In that case, the This.P2 will point either directly to or
94446
+** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
94447
+** loop terminate.
94448
+**
94449
+** Possible outcomes from this opcode:<ol>
94450
+**
94451
+** <li> If the cursor is initally not pointed to any valid row, then
94452
+** fall through into the subsequent OP_SeekGE opcode.
94453
+**
94454
+** <li> If the cursor is left pointing to a row that is before the target
94455
+** row, even after making as many as This.P1 calls to
94456
+** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
94457
+**
94458
+** <li> If the cursor is left pointing at the target row, either because it
94459
+** was at the target row to begin with or because one or more
94460
+** sqlite3BtreeNext() calls moved the cursor to the target row,
94461
+** then jump to This.P2..,
94462
+**
94463
+** <li> If the cursor started out before the target row and a call to
94464
+** to sqlite3BtreeNext() moved the cursor off the end of the index
94465
+** (indicating that the target row definitely does not exist in the
94466
+** btree) then jump to SeekGE.P2, ending the loop.
94467
+**
94468
+** <li> If the cursor ends up on a valid row that is past the target row
94469
+** (indicating that the target row does not exist in the btree) then
94470
+** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
9327094471
** </ol>
9327194472
*/
9327294473
case OP_SeekScan: {
9327394474
VdbeCursor *pC;
9327494475
int res;
@@ -93275,18 +94476,29 @@
9327594476
int nStep;
9327694477
UnpackedRecord r;
9327794478
9327894479
assert( pOp[1].opcode==OP_SeekGE );
9327994480
93280
- /* pOp->p2 points to the first instruction past the OP_IdxGT that
93281
- ** follows the OP_SeekGE. */
94481
+ /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
94482
+ ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
94483
+ ** opcode past the OP_SeekGE itself. */
9328294484
assert( pOp->p2>=(int)(pOp-aOp)+2 );
93283
- assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
93284
- testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
93285
- assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
93286
- assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
93287
- assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
94485
+#ifdef SQLITE_DEBUG
94486
+ if( pOp->p5==0 ){
94487
+ /* There are no inequality constraints following the IN constraint. */
94488
+ assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
94489
+ assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
94490
+ assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
94491
+ assert( aOp[pOp->p2-1].opcode==OP_IdxGT
94492
+ || aOp[pOp->p2-1].opcode==OP_IdxGE );
94493
+ testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
94494
+ }else{
94495
+ /* There are inequality constraints. */
94496
+ assert( pOp->p2==(int)(pOp-aOp)+2 );
94497
+ assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
94498
+ }
94499
+#endif
9328894500
9328994501
assert( pOp->p1>0 );
9329094502
pC = p->apCsr[pOp[1].p1];
9329194503
assert( pC!=0 );
9329294504
assert( pC->eCurType==CURTYPE_BTREE );
@@ -93316,22 +94528,24 @@
9331694528
#endif
9331794529
res = 0; /* Not needed. Only used to silence a warning. */
9331894530
while(1){
9331994531
rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
9332094532
if( rc ) goto abort_due_to_error;
93321
- if( res>0 ){
94533
+ if( res>0 && pOp->p5==0 ){
9332294534
seekscan_search_fail:
94535
+ /* Jump to SeekGE.P2, ending the loop */
9332394536
#ifdef SQLITE_DEBUG
9332494537
if( db->flags&SQLITE_VdbeTrace ){
9332594538
printf("... %d steps and then skip\n", pOp->p1 - nStep);
9332694539
}
9332794540
#endif
9332894541
VdbeBranchTaken(1,3);
9332994542
pOp++;
9333094543
goto jump_to_p2;
9333194544
}
93332
- if( res==0 ){
94545
+ if( res>=0 ){
94546
+ /* Jump to This.P2, bypassing the OP_SeekGE opcode */
9333394547
#ifdef SQLITE_DEBUG
9333494548
if( db->flags&SQLITE_VdbeTrace ){
9333594549
printf("... %d steps and then success\n", pOp->p1 - nStep);
9333694550
}
9333794551
#endif
@@ -103867,13 +105081,12 @@
103867105081
}
103868105082
op = pExpr->op;
103869105083
if( op==TK_REGISTER ) op = pExpr->op2;
103870105084
if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
103871105085
assert( ExprUseYTab(pExpr) );
103872
- if( pExpr->y.pTab ){
103873
- return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103874
- }
105086
+ assert( pExpr->y.pTab!=0 );
105087
+ return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103875105088
}
103876105089
if( op==TK_SELECT ){
103877105090
assert( ExprUseXSelect(pExpr) );
103878105091
assert( pExpr->x.pSelect!=0 );
103879105092
assert( pExpr->x.pSelect->pEList!=0 );
@@ -103987,21 +105200,18 @@
103987105200
const Expr *p = pExpr;
103988105201
while( p ){
103989105202
int op = p->op;
103990105203
if( op==TK_REGISTER ) op = p->op2;
103991105204
if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
105205
+ int j;
103992105206
assert( ExprUseYTab(p) );
103993
- if( p->y.pTab!=0 ){
103994
- /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
103995
- ** a TK_COLUMN but was previously evaluated and cached in a register */
103996
- int j = p->iColumn;
103997
- if( j>=0 ){
103998
- const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
103999
- pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
104000
- }
104001
- break;
104002
- }
105207
+ assert( p->y.pTab!=0 );
105208
+ if( (j = p->iColumn)>=0 ){
105209
+ const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
105210
+ pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
105211
+ }
105212
+ break;
104003105213
}
104004105214
if( op==TK_CAST || op==TK_UPLUS ){
104005105215
p = p->pLeft;
104006105216
continue;
104007105217
}
@@ -107602,14 +108812,11 @@
107602108812
int iCol, /* Index of the column to extract */
107603108813
int regOut /* Extract the value into this register */
107604108814
){
107605108815
Column *pCol;
107606108816
assert( v!=0 );
107607
- if( pTab==0 ){
107608
- sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
107609
- return;
107610
- }
108817
+ assert( pTab!=0 );
107611108818
if( iCol<0 || iCol==pTab->iPKey ){
107612108819
sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
107613108820
VdbeComment((v, "%s.rowid", pTab->zName));
107614108821
}else{
107615108822
int op;
@@ -107855,10 +109062,57 @@
107855109062
#endif /* !defined(SQLITE_UNTESTABLE) */
107856109063
}
107857109064
return target;
107858109065
}
107859109066
109067
+/*
109068
+** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109069
+** If it is, then resolve the expression by reading from the index and
109070
+** return the register into which the value has been read. If pExpr is
109071
+** not an indexed expression, then return negative.
109072
+*/
109073
+static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
109074
+ Parse *pParse, /* The parsing context */
109075
+ Expr *pExpr, /* The expression to potentially bypass */
109076
+ int target /* Where to store the result of the expression */
109077
+){
109078
+ IndexedExpr *p;
109079
+ Vdbe *v;
109080
+ for(p=pParse->pIdxExpr; p; p=p->pIENext){
109081
+ int iDataCur = p->iDataCur;
109082
+ if( iDataCur<0 ) continue;
109083
+ if( pParse->iSelfTab ){
109084
+ if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109085
+ iDataCur = -1;
109086
+ }
109087
+ if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
109088
+ v = pParse->pVdbe;
109089
+ assert( v!=0 );
109090
+ if( p->bMaybeNullRow ){
109091
+ /* If the index is on a NULL row due to an outer join, then we
109092
+ ** cannot extract the value from the index. The value must be
109093
+ ** computed using the original expression. */
109094
+ int addr = sqlite3VdbeCurrentAddr(v);
109095
+ sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109096
+ VdbeCoverage(v);
109097
+ sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109098
+ VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109099
+ sqlite3VdbeGoto(v, 0);
109100
+ p = pParse->pIdxExpr;
109101
+ pParse->pIdxExpr = 0;
109102
+ sqlite3ExprCode(pParse, pExpr, target);
109103
+ pParse->pIdxExpr = p;
109104
+ sqlite3VdbeJumpHere(v, addr+2);
109105
+ }else{
109106
+ sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109107
+ VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109108
+ }
109109
+ return target;
109110
+ }
109111
+ return -1; /* Not found */
109112
+}
109113
+
107860109114
107861109115
/*
107862109116
** Generate code into the current Vdbe to evaluate the given
107863109117
** expression. Attempt to store the results in register "target".
107864109118
** Return the register where results are stored.
@@ -107883,10 +109137,15 @@
107883109137
assert( v!=0 );
107884109138
107885109139
expr_code_doover:
107886109140
if( pExpr==0 ){
107887109141
op = TK_NULL;
109142
+ }else if( pParse->pIdxExpr!=0
109143
+ && !ExprHasProperty(pExpr, EP_Leaf)
109144
+ && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109145
+ ){
109146
+ return r1;
107888109147
}else{
107889109148
assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
107890109149
op = pExpr->op;
107891109150
}
107892109151
switch( op ){
@@ -107928,15 +109187,12 @@
107928109187
** constant.
107929109188
*/
107930109189
int aff;
107931109190
iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107932109191
assert( ExprUseYTab(pExpr) );
107933
- if( pExpr->y.pTab ){
107934
- aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107935
- }else{
107936
- aff = pExpr->affExpr;
107937
- }
109192
+ assert( pExpr->y.pTab!=0 );
109193
+ aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107938109194
if( aff>SQLITE_AFF_BLOB ){
107939109195
static const char zAff[] = "B\000C\000D\000E";
107940109196
assert( SQLITE_AFF_BLOB=='A' );
107941109197
assert( SQLITE_AFF_TEXT=='B' );
107942109198
sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
@@ -107994,16 +109250,14 @@
107994109250
** in the index refer to the table to which the index belongs */
107995109251
iTab = pParse->iSelfTab - 1;
107996109252
}
107997109253
}
107998109254
assert( ExprUseYTab(pExpr) );
109255
+ assert( pExpr->y.pTab!=0 );
107999109256
iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
108000109257
pExpr->iColumn, iTab, target,
108001109258
pExpr->op2);
108002
- if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
108003
- sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
108004
- }
108005109259
return iReg;
108006109260
}
108007109261
case TK_INTEGER: {
108008109262
codeInteger(pParse, pExpr, 0, target);
108009109263
return target;
@@ -109053,10 +110307,11 @@
109053110307
case TK_ISNULL:
109054110308
case TK_NOTNULL: {
109055110309
assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
109056110310
assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
109057110311
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110312
+ sqlite3VdbeTypeofColumn(v, r1);
109058110313
sqlite3VdbeAddOp2(v, op, r1, dest);
109059110314
VdbeCoverageIf(v, op==TK_ISNULL);
109060110315
VdbeCoverageIf(v, op==TK_NOTNULL);
109061110316
testcase( regFree1==0 );
109062110317
break;
@@ -109227,10 +110482,11 @@
109227110482
break;
109228110483
}
109229110484
case TK_ISNULL:
109230110485
case TK_NOTNULL: {
109231110486
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110487
+ sqlite3VdbeTypeofColumn(v, r1);
109232110488
sqlite3VdbeAddOp2(v, op, r1, dest);
109233110489
testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
109234110490
testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
109235110491
testcase( regFree1==0 );
109236110492
break;
@@ -109380,11 +110636,17 @@
109380110636
return 1;
109381110637
}
109382110638
if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109383110639
return 1;
109384110640
}
109385
- return 2;
110641
+ if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
110642
+ && pB->iTable<0 && pA->iTable==iTab
110643
+ ){
110644
+ /* fall through */
110645
+ }else{
110646
+ return 2;
110647
+ }
109386110648
}
109387110649
assert( !ExprHasProperty(pA, EP_IntValue) );
109388110650
assert( !ExprHasProperty(pB, EP_IntValue) );
109389110651
if( pA->u.zToken ){
109390110652
if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
@@ -109682,14 +110944,14 @@
109682110944
/* The y.pTab=0 assignment in wherecode.c always happens after the
109683110945
** impliesNotNullRow() test */
109684110946
assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
109685110947
assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
109686110948
if( (pLeft->op==TK_COLUMN
109687
- && pLeft->y.pTab!=0
110949
+ && ALWAYS(pLeft->y.pTab!=0)
109688110950
&& IsVirtual(pLeft->y.pTab))
109689110951
|| (pRight->op==TK_COLUMN
109690
- && pRight->y.pTab!=0
110952
+ && ALWAYS(pRight->y.pTab!=0)
109691110953
&& IsVirtual(pRight->y.pTab))
109692110954
){
109693110955
return WRC_Prune;
109694110956
}
109695110957
/* no break */ deliberate_fall_through
@@ -113510,10 +114772,11 @@
113510114772
assert( k>=0 && k<pIdx->nColumn );
113511114773
i = pIdx->aiColumn[k];
113512114774
if( NEVER(i==XN_ROWID) ){
113513114775
VdbeComment((v,"%s.rowid",pIdx->zName));
113514114776
}else if( i==XN_EXPR ){
114777
+ assert( pIdx->bHasExpr );
113515114778
VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113516114779
}else{
113517114780
VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113518114781
}
113519114782
}
@@ -115825,11 +117088,11 @@
115825117088
if( p==0 ){
115826117089
#ifndef SQLITE_OMIT_VIRTUALTABLE
115827117090
/* If zName is the not the name of a table in the schema created using
115828117091
** CREATE, then check to see if it is the name of an virtual table that
115829117092
** can be an eponymous virtual table. */
115830
- if( pParse->disableVtab==0 && db->init.busy==0 ){
117093
+ if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
115831117094
Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115832117095
if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
115833117096
pMod = sqlite3PragmaVtabRegister(db, zName);
115834117097
}
115835117098
if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
@@ -115838,11 +117101,11 @@
115838117101
}
115839117102
}
115840117103
#endif
115841117104
if( flags & LOCATE_NOERR ) return 0;
115842117105
pParse->checkSchema = 1;
115843
- }else if( IsVirtual(p) && pParse->disableVtab ){
117106
+ }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
115844117107
p = 0;
115845117108
}
115846117109
115847117110
if( p==0 ){
115848117111
const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
@@ -117648,11 +118911,12 @@
117648118911
}
117649118912
117650118913
/* Recompute the colNotIdxed field of the Index.
117651118914
**
117652118915
** colNotIdxed is a bitmask that has a 0 bit representing each indexed
117653
-** columns that are within the first 63 columns of the table. The
118916
+** columns that are within the first 63 columns of the table and a 1 for
118917
+** all other bits (all columns that are not in the index). The
117654118918
** high-order bit of colNotIdxed is always 1. All unindexed columns
117655118919
** of the table have a 1.
117656118920
**
117657118921
** 2019-10-24: For the purpose of this computation, virtual columns are
117658118922
** not considered to be covered by the index, even if they are in the
@@ -117676,11 +118940,11 @@
117676118940
testcase( x==BMS-2 );
117677118941
if( x<BMS-1 ) m |= MASKBIT(x);
117678118942
}
117679118943
}
117680118944
pIdx->colNotIdxed = ~m;
117681
- assert( (pIdx->colNotIdxed>>63)==1 );
118945
+ assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
117682118946
}
117683118947
117684118948
/*
117685118949
** This routine runs at the end of parsing a CREATE TABLE statement that
117686118950
** has a WITHOUT ROWID clause. The job of this routine is to convert both
@@ -119564,10 +120828,11 @@
119564120828
pList = 0;
119565120829
}
119566120830
j = XN_EXPR;
119567120831
pIndex->aiColumn[i] = XN_EXPR;
119568120832
pIndex->uniqNotNull = 0;
120833
+ pIndex->bHasExpr = 1;
119569120834
}else{
119570120835
j = pCExpr->iColumn;
119571120836
assert( j<=0x7fff );
119572120837
if( j<0 ){
119573120838
j = pTab->iPKey;
@@ -119575,10 +120840,11 @@
119575120840
if( pTab->aCol[j].notNull==0 ){
119576120841
pIndex->uniqNotNull = 0;
119577120842
}
119578120843
if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
119579120844
pIndex->bHasVCol = 1;
120845
+ pIndex->bHasExpr = 1;
119580120846
}
119581120847
}
119582120848
pIndex->aiColumn[i] = (i16)j;
119583120849
}
119584120850
zColl = 0;
@@ -123352,11 +124618,11 @@
123352124618
**
123353124619
** For a case-insensitive search, set variable cx to be the same as
123354124620
** c but in the other case and search the input string for either
123355124621
** c or cx.
123356124622
*/
123357
- if( c<=0x80 ){
124623
+ if( c<0x80 ){
123358124624
char zStop[3];
123359124625
int bMatch;
123360124626
if( noCase ){
123361124627
zStop[0] = sqlite3Toupper(c);
123362124628
zStop[1] = sqlite3Tolower(c);
@@ -123435,19 +124701,31 @@
123435124701
/*
123436124702
** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
123437124703
** non-zero if there is no match.
123438124704
*/
123439124705
SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
123440
- return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124706
+ if( zString==0 ){
124707
+ return zGlobPattern!=0;
124708
+ }else if( zGlobPattern==0 ){
124709
+ return 1;
124710
+ }else {
124711
+ return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124712
+ }
123441124713
}
123442124714
123443124715
/*
123444124716
** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123445124717
** a miss - like strcmp().
123446124718
*/
123447124719
SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
123448
- return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124720
+ if( zStr==0 ){
124721
+ return zPattern!=0;
124722
+ }else if( zPattern==0 ){
124723
+ return 1;
124724
+ }else{
124725
+ return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124726
+ }
123449124727
}
123450124728
123451124729
/*
123452124730
** Count the number of times that the LIKE operator (or GLOB which is
123453124731
** just a variation of LIKE) gets called. This is used for testing
@@ -126572,10 +127850,11 @@
126572127850
aff = pTab->aCol[x].affinity;
126573127851
}else if( x==XN_ROWID ){
126574127852
aff = SQLITE_AFF_INTEGER;
126575127853
}else{
126576127854
assert( x==XN_EXPR );
127855
+ assert( pIdx->bHasExpr );
126577127856
assert( pIdx->aColExpr!=0 );
126578127857
aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126579127858
}
126580127859
if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
126581127860
if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
@@ -130146,10 +131425,12 @@
130146131425
int (*deserialize)(sqlite3*,const char*,unsigned char*,
130147131426
sqlite3_int64,sqlite3_int64,unsigned);
130148131427
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
130149131428
unsigned int);
130150131429
const char *(*db_name)(sqlite3*,int);
131430
+ /* Version 3.40.0 and later */
131431
+ int (*value_encoding)(sqlite3_value*);
130151131432
};
130152131433
130153131434
/*
130154131435
** This is the function signature used for all extension entry points. It
130155131436
** is also defined in the file "loadext.c".
@@ -130470,10 +131751,12 @@
130470131751
#ifndef SQLITE_OMIT_DESERIALIZE
130471131752
#define sqlite3_deserialize sqlite3_api->deserialize
130472131753
#define sqlite3_serialize sqlite3_api->serialize
130473131754
#endif
130474131755
#define sqlite3_db_name sqlite3_api->db_name
131756
+/* Version 3.40.0 and later */
131757
+#define sqlite3_value_encoding sqlite3_api->value_encoding
130475131758
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130476131759
130477131760
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130478131761
/* This case when the file really is being compiled as a loadable
130479131762
** extension */
@@ -130982,11 +132265,13 @@
130982132265
sqlite3_serialize,
130983132266
#else
130984132267
0,
130985132268
0,
130986132269
#endif
130987
- sqlite3_db_name
132270
+ sqlite3_db_name,
132271
+ /* Version 3.40.0 and later */
132272
+ sqlite3_value_type
130988132273
};
130989132274
130990132275
/* True if x is the directory separator character
130991132276
*/
130992132277
#if SQLITE_OS_WIN
@@ -133786,12 +135071,13 @@
133786135071
Index *pIdx, *pPk;
133787135072
Index *pPrior = 0; /* Previous index */
133788135073
int loopTop;
133789135074
int iDataCur, iIdxCur;
133790135075
int r1 = -1;
133791
- int bStrict;
135076
+ int bStrict; /* True for a STRICT table */
133792135077
int r2; /* Previous key for WITHOUT ROWID tables */
135078
+ int mxCol; /* Maximum non-virtual column number */
133793135079
133794135080
if( !IsOrdinaryTable(pTab) ) continue;
133795135081
if( pObjTab && pObjTab!=pTab ) continue;
133796135082
if( isQuick || HasRowid(pTab) ){
133797135083
pPk = 0;
@@ -133812,15 +135098,26 @@
133812135098
}
133813135099
assert( pParse->nMem>=8+j );
133814135100
assert( sqlite3NoTempsInRange(pParse,1,7+j) );
133815135101
sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
133816135102
loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
135103
+
135104
+ /* Fetch the right-most column from the table. This will cause
135105
+ ** the entire record header to be parsed and sanity checked. It
135106
+ ** will also prepopulate the cursor column cache that is used
135107
+ ** by the OP_IsType code, so it is a required step.
135108
+ */
135109
+ mxCol = pTab->nCol-1;
135110
+ while( mxCol>=0
135111
+ && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
135112
+ || pTab->iPKey==mxCol) ) mxCol--;
135113
+ if( mxCol>=0 ){
135114
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
135115
+ sqlite3VdbeTypeofColumn(v, 3);
135116
+ }
135117
+
133817135118
if( !isQuick ){
133818
- /* Sanity check on record header decoding */
133819
- sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133820
- sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133821
- VdbeComment((v, "(right-most column)"));
133822135119
if( pPk ){
133823135120
/* Verify WITHOUT ROWID keys are in ascending order */
133824135121
int a1;
133825135122
char *zErr;
133826135123
a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
@@ -133836,48 +135133,126 @@
133836135133
for(j=0; j<pPk->nKeyCol; j++){
133837135134
sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
133838135135
}
133839135136
}
133840135137
}
133841
- /* Verify that all NOT NULL columns really are NOT NULL. At the
133842
- ** same time verify the type of the content of STRICT tables */
135138
+ /* Verify datatypes for all columns:
135139
+ **
135140
+ ** (1) NOT NULL columns may not contain a NULL
135141
+ ** (2) Datatype must be exact for non-ANY columns in STRICT tables
135142
+ ** (3) Datatype for TEXT columns in non-STRICT tables must be
135143
+ ** NULL, TEXT, or BLOB.
135144
+ ** (4) Datatype for numeric columns in non-STRICT tables must not
135145
+ ** be a TEXT value that can be losslessly converted to numeric.
135146
+ */
133843135147
bStrict = (pTab->tabFlags & TF_Strict)!=0;
133844135148
for(j=0; j<pTab->nCol; j++){
133845135149
char *zErr;
133846
- Column *pCol = pTab->aCol + j;
133847
- int doError, jmp2;
135150
+ Column *pCol = pTab->aCol + j; /* The column to be checked */
135151
+ int labelError; /* Jump here to report an error */
135152
+ int labelOk; /* Jump here if all looks ok */
135153
+ int p1, p3, p4; /* Operands to the OP_IsType opcode */
135154
+ int doTypeCheck; /* Check datatypes (besides NOT NULL) */
135155
+
133848135156
if( j==pTab->iPKey ) continue;
133849
- if( pCol->notNull==0 && !bStrict ) continue;
133850
- doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133851
- sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133852
- if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){
133853
- sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
135157
+ if( bStrict ){
135158
+ doTypeCheck = pCol->eCType>COLTYPE_ANY;
135159
+ }else{
135160
+ doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
133854135161
}
135162
+ if( pCol->notNull==0 && !doTypeCheck ) continue;
135163
+
135164
+ /* Compute the operands that will be needed for OP_IsType */
135165
+ p4 = SQLITE_NULL;
135166
+ if( pCol->colFlags & COLFLAG_VIRTUAL ){
135167
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135168
+ p1 = -1;
135169
+ p3 = 3;
135170
+ }else{
135171
+ if( pCol->iDflt ){
135172
+ sqlite3_value *pDfltValue = 0;
135173
+ sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
135174
+ pCol->affinity, &pDfltValue);
135175
+ if( pDfltValue ){
135176
+ p4 = sqlite3_value_type(pDfltValue);
135177
+ sqlite3ValueFree(pDfltValue);
135178
+ }
135179
+ }
135180
+ p1 = iDataCur;
135181
+ if( !HasRowid(pTab) ){
135182
+ testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
135183
+ p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
135184
+ }else{
135185
+ p3 = sqlite3TableColumnToStorage(pTab,j);
135186
+ testcase( p3!=j);
135187
+ }
135188
+ }
135189
+
135190
+ labelError = sqlite3VdbeMakeLabel(pParse);
135191
+ labelOk = sqlite3VdbeMakeLabel(pParse);
133855135192
if( pCol->notNull ){
133856
- jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
135193
+ /* (1) NOT NULL columns may not contain a NULL */
135194
+ int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135195
+ sqlite3VdbeChangeP5(v, 0x0f);
135196
+ VdbeCoverage(v);
133857135197
zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133858135198
pCol->zCnName);
133859135199
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133860
- if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133861
- sqlite3VdbeGoto(v, doError);
135200
+ if( doTypeCheck ){
135201
+ sqlite3VdbeGoto(v, labelError);
135202
+ sqlite3VdbeJumpHere(v, jmp2);
133862135203
}else{
133863
- integrityCheckResultRow(v);
135204
+ /* VDBE byte code will fall thru */
133864135205
}
133865
- sqlite3VdbeJumpHere(v, jmp2);
133866135206
}
133867
- if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133868
- jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133869
- sqlite3StdTypeMap[pCol->eCType-1]);
135207
+ if( bStrict && doTypeCheck ){
135208
+ /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
135209
+ static unsigned char aStdTypeMask[] = {
135210
+ 0x1f, /* ANY */
135211
+ 0x18, /* BLOB */
135212
+ 0x11, /* INT */
135213
+ 0x11, /* INTEGER */
135214
+ 0x13, /* REAL */
135215
+ 0x14 /* TEXT */
135216
+ };
135217
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135218
+ assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
135219
+ sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
133870135220
VdbeCoverage(v);
133871135221
zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133872135222
sqlite3StdType[pCol->eCType-1],
133873135223
pTab->zName, pTab->aCol[j].zCnName);
133874135224
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133875
- sqlite3VdbeResolveLabel(v, doError);
133876
- integrityCheckResultRow(v);
133877
- sqlite3VdbeJumpHere(v, jmp2);
135225
+ }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
135226
+ /* (3) Datatype for TEXT columns in non-STRICT tables must be
135227
+ ** NULL, TEXT, or BLOB. */
135228
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135229
+ sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135230
+ VdbeCoverage(v);
135231
+ zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
135232
+ pTab->zName, pTab->aCol[j].zCnName);
135233
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135234
+ }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
135235
+ /* (4) Datatype for numeric columns in non-STRICT tables must not
135236
+ ** be a TEXT value that can be converted to numeric. */
135237
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135238
+ sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
135239
+ VdbeCoverage(v);
135240
+ if( p1>=0 ){
135241
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135242
+ }
135243
+ sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
135244
+ sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
135245
+ sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135246
+ VdbeCoverage(v);
135247
+ zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
135248
+ pTab->zName, pTab->aCol[j].zCnName);
135249
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133878135250
}
135251
+ sqlite3VdbeResolveLabel(v, labelError);
135252
+ integrityCheckResultRow(v);
135253
+ sqlite3VdbeResolveLabel(v, labelOk);
133879135254
}
133880135255
/* Verify CHECK constraints */
133881135256
if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133882135257
ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133883135258
if( db->mallocFailed==0 ){
@@ -135493,11 +136868,11 @@
135493136868
*/
135494136869
if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
135495136870
sParse.disableLookaside++;
135496136871
DisableLookaside;
135497136872
}
135498
- sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
136873
+ sParse.prepFlags = prepFlags & 0xff;
135499136874
135500136875
/* Check to verify that it is possible to get a read lock on all
135501136876
** database schemas. The inability to get a read lock indicates that
135502136877
** some other database connection is holding a write-lock, which in
135503136878
** turn means that the other connection has made uncommitted changes
@@ -135534,11 +136909,13 @@
135534136909
}
135535136910
}
135536136911
}
135537136912
}
135538136913
135539
- sqlite3VtabUnlockList(db);
136914
+#ifndef SQLITE_OMIT_VIRTUALTABLE
136915
+ if( db->pDisconnect ) sqlite3VtabUnlockList(db);
136916
+#endif
135540136917
135541136918
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135542136919
char *zSqlCopy;
135543136920
int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
135544136921
testcase( nBytes==mxLen );
@@ -139603,10 +140980,11 @@
139603140980
Parse *pParse; /* The parsing context */
139604140981
int iTable; /* Replace references to this table */
139605140982
int iNewTable; /* New table number */
139606140983
int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
139607140984
ExprList *pEList; /* Replacement expressions */
140985
+ ExprList *pCList; /* Collation sequences for replacement expr */
139608140986
} SubstContext;
139609140987
139610140988
/* Forward Declarations */
139611140989
static void substExprList(SubstContext*, ExprList*);
139612140990
static void substSelect(SubstContext*, Select*, int);
@@ -139644,13 +141022,14 @@
139644141022
pExpr->op = TK_NULL;
139645141023
}else
139646141024
#endif
139647141025
{
139648141026
Expr *pNew;
139649
- Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
141027
+ int iColumn = pExpr->iColumn;
141028
+ Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
139650141029
Expr ifNullRow;
139651
- assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
141030
+ assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
139652141031
assert( pExpr->pRight==0 );
139653141032
if( sqlite3ExprIsVector(pCopy) ){
139654141033
sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139655141034
}else{
139656141035
sqlite3 *db = pSubst->pParse->db;
@@ -139684,15 +141063,20 @@
139684141063
ExprSetProperty(pExpr, EP_IntValue);
139685141064
}
139686141065
139687141066
/* Ensure that the expression now has an implicit collation sequence,
139688141067
** just as it did when it was a column of a view or sub-query. */
139689
- if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
139690
- CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
139691
- pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
139692
- (pColl ? pColl->zName : "BINARY")
141068
+ {
141069
+ CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
141070
+ CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
141071
+ pSubst->pCList->a[iColumn].pExpr
139693141072
);
141073
+ if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
141074
+ pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
141075
+ (pColl ? pColl->zName : "BINARY")
141076
+ );
141077
+ }
139694141078
}
139695141079
ExprClearProperty(pExpr, EP_Collate);
139696141080
}
139697141081
}
139698141082
}else{
@@ -139880,10 +141264,22 @@
139880141264
w.xExprCallback = renumberCursorsCb;
139881141265
w.xSelectCallback = sqlite3SelectWalkNoop;
139882141266
sqlite3WalkSelect(&w, p);
139883141267
}
139884141268
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
141269
+
141270
+/*
141271
+** If pSel is not part of a compound SELECT, return a pointer to its
141272
+** expression list. Otherwise, return a pointer to the expression list
141273
+** of the leftmost SELECT in the compound.
141274
+*/
141275
+static ExprList *findLeftmostExprlist(Select *pSel){
141276
+ while( pSel->pPrior ){
141277
+ pSel = pSel->pPrior;
141278
+ }
141279
+ return pSel->pEList;
141280
+}
139885141281
139886141282
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139887141283
/*
139888141284
** This routine attempts to flatten subqueries as a performance optimization.
139889141285
** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -140433,10 +141829,11 @@
140433141829
x.pParse = pParse;
140434141830
x.iTable = iParent;
140435141831
x.iNewTable = iNewParent;
140436141832
x.isOuterJoin = isOuterJoin;
140437141833
x.pEList = pSub->pEList;
141834
+ x.pCList = findLeftmostExprlist(pSub);
140438141835
substSelect(&x, pParent, 0);
140439141836
}
140440141837
140441141838
/* The flattened query is a compound if either the inner or the
140442141839
** outer query is a compound. */
@@ -140452,11 +141849,11 @@
140452141849
if( pSub->pLimit ){
140453141850
pParent->pLimit = pSub->pLimit;
140454141851
pSub->pLimit = 0;
140455141852
}
140456141853
140457
- /* Recompute the SrcList_item.colUsed masks for the flattened
141854
+ /* Recompute the SrcItem.colUsed masks for the flattened
140458141855
** tables. */
140459141856
for(i=0; i<nSubSrc; i++){
140460141857
recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140461141858
}
140462141859
}
@@ -140915,10 +142312,11 @@
140915142312
x.pParse = pParse;
140916142313
x.iTable = pSrc->iCursor;
140917142314
x.iNewTable = pSrc->iCursor;
140918142315
x.isOuterJoin = 0;
140919142316
x.pEList = pSubq->pEList;
142317
+ x.pCList = findLeftmostExprlist(pSubq);
140920142318
pNew = substExpr(&x, pNew);
140921142319
#ifndef SQLITE_OMIT_WINDOWFUNC
140922142320
if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140923142321
/* Restriction 6c has prevented push-down in this case */
140924142322
sqlite3ExprDelete(pParse->db, pNew);
@@ -141439,13 +142837,13 @@
141439142837
}
141440142838
}
141441142839
#endif
141442142840
141443142841
/*
141444
-** The SrcList_item structure passed as the second argument represents a
142842
+** The SrcItem structure passed as the second argument represents a
141445142843
** sub-query in the FROM clause of a SELECT statement. This function
141446
-** allocates and populates the SrcList_item.pTab object. If successful,
142844
+** allocates and populates the SrcItem.pTab object. If successful,
141447142845
** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
141448142846
** SQLITE_NOMEM.
141449142847
*/
141450142848
SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
141451142849
Select *pSel = pFrom->pSelect;
@@ -142274,11 +143672,11 @@
142274143672
#endif
142275143673
}
142276143674
142277143675
/*
142278143676
** Check to see if the pThis entry of pTabList is a self-join of a prior view.
142279
-** If it is, then return the SrcList_item for the prior view. If it is not,
143677
+** If it is, then return the SrcItem for the prior view. If it is not,
142280143678
** then return 0.
142281143679
*/
142282143680
static SrcItem *isSelfJoinView(
142283143681
SrcList *pTabList, /* Search for self-joins in this FROM clause */
142284143682
SrcItem *pThis /* Search for prior reference to this subquery */
@@ -143318,11 +144716,11 @@
143318144716
** in the right order to begin with.
143319144717
*/
143320144718
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
143321144719
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143322144720
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
143323
- 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144721
+ p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
143324144722
| (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
143325144723
);
143326144724
if( pWInfo==0 ){
143327144725
sqlite3ExprListDelete(db, pDistinct);
143328144726
goto select_end;
@@ -143617,11 +145015,11 @@
143617145015
assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
143618145016
assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
143619145017
143620145018
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143621145019
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
143622
- pDistinct, 0, minMaxFlag|distFlag, 0);
145020
+ pDistinct, p, minMaxFlag|distFlag, 0);
143623145021
if( pWInfo==0 ){
143624145022
goto select_end;
143625145023
}
143626145024
SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
143627145025
eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -145101,11 +146499,11 @@
145101146499
sSubParse.pTriggerTab = pTab;
145102146500
sSubParse.pToplevel = pTop;
145103146501
sSubParse.zAuthContext = pTrigger->zName;
145104146502
sSubParse.eTriggerOp = pTrigger->op;
145105146503
sSubParse.nQueryLoop = pParse->nQueryLoop;
145106
- sSubParse.disableVtab = pParse->disableVtab;
146504
+ sSubParse.prepFlags = pParse->prepFlags;
145107146505
145108146506
v = sqlite3GetVdbe(&sSubParse);
145109146507
if( v ){
145110146508
VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
145111146509
pTrigger->zName, onErrorText(orconf),
@@ -145447,15 +146845,18 @@
145447146845
** (not a virtual table) then the value might have been stored as an
145448146846
** integer. In that case, add an OP_RealAffinity opcode to make sure
145449146847
** it has been converted into REAL.
145450146848
*/
145451146849
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
146850
+ Column *pCol;
145452146851
assert( pTab!=0 );
145453
- if( !IsView(pTab) ){
146852
+ assert( pTab->nCol>i );
146853
+ pCol = &pTab->aCol[i];
146854
+ if( pCol->iDflt ){
145454146855
sqlite3_value *pValue = 0;
145455146856
u8 enc = ENC(sqlite3VdbeDb(v));
145456
- Column *pCol = &pTab->aCol[i];
146857
+ assert( !IsView(pTab) );
145457146858
VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145458146859
assert( i<pTab->nCol );
145459146860
sqlite3ValueFromExpr(sqlite3VdbeDb(v),
145460146861
sqlite3ColumnExpr(pTab,pCol), enc,
145461146862
pCol->affinity, &pValue);
@@ -145462,11 +146863,11 @@
145462146863
if( pValue ){
145463146864
sqlite3VdbeAppendP4(v, pValue, P4_MEM);
145464146865
}
145465146866
}
145466146867
#ifndef SQLITE_OMIT_FLOATING_POINT
145467
- if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
146868
+ if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
145468146869
sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
145469146870
}
145470146871
#endif
145471146872
}
145472146873
@@ -146902,10 +148303,11 @@
146902148303
Expr *pExpr;
146903148304
sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146904148305
if( pIdx->aiColumn[ii]==XN_EXPR ){
146905148306
assert( pIdx->aColExpr!=0 );
146906148307
assert( pIdx->aColExpr->nExpr>ii );
148308
+ assert( pIdx->bHasExpr );
146907148309
pExpr = pIdx->aColExpr->a[ii].pExpr;
146908148310
if( pExpr->op!=TK_COLLATE ){
146909148311
sCol[0].pLeft = pExpr;
146910148312
pExpr = &sCol[0];
146911148313
}
@@ -147215,10 +148617,11 @@
147215148617
int isMemDb; /* True if vacuuming a :memory: database */
147216148618
int nRes; /* Bytes of reserved space at the end of each page */
147217148619
int nDb; /* Number of attached databases */
147218148620
const char *zDbMain; /* Schema name of database to vacuum */
147219148621
const char *zOut; /* Name of output file */
148622
+ u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
147220148623
147221148624
if( !db->autoCommit ){
147222148625
sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
147223148626
return SQLITE_ERROR; /* IMP: R-12218-18073 */
147224148627
}
@@ -147286,16 +148689,21 @@
147286148689
rc = SQLITE_ERROR;
147287148690
sqlite3SetString(pzErrMsg, db, "output file already exists");
147288148691
goto end_of_vacuum;
147289148692
}
147290148693
db->mDbFlags |= DBFLAG_VacuumInto;
148694
+
148695
+ /* For a VACUUM INTO, the pager-flags are set to the same values as
148696
+ ** they are for the database being vacuumed, except that PAGER_CACHESPILL
148697
+ ** is always set. */
148698
+ pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
147291148699
}
147292148700
nRes = sqlite3BtreeGetRequestedReserve(pMain);
147293148701
147294148702
sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
147295148703
sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
147296
- sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
148704
+ sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL);
147297148705
147298148706
/* Begin a transaction and take an exclusive lock on the main database
147299148707
** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
147300148708
** to ensure that we do not try to change the page-size on a WAL database.
147301148709
*/
@@ -148605,11 +150013,11 @@
148605150013
/* Check to see the left operand is a column in a virtual table */
148606150014
if( NEVER(pExpr==0) ) return pDef;
148607150015
if( pExpr->op!=TK_COLUMN ) return pDef;
148608150016
assert( ExprUseYTab(pExpr) );
148609150017
pTab = pExpr->y.pTab;
148610
- if( pTab==0 ) return pDef;
150018
+ if( NEVER(pTab==0) ) return pDef;
148611150019
if( !IsVirtual(pTab) ) return pDef;
148612150020
pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148613150021
assert( pVtab!=0 );
148614150022
assert( pVtab->pModule!=0 );
148615150023
pMod = (sqlite3_module *)pVtab->pModule;
@@ -149212,11 +150620,11 @@
149212150620
/*
149213150621
** An instance of the following structure keeps track of a mapping
149214150622
** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
149215150623
**
149216150624
** The VDBE cursor numbers are small integers contained in
149217
-** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
150625
+** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
149218150626
** clause, the cursor numbers might not begin with 0 and they might
149219150627
** contain gaps in the numbering sequence. But we want to make maximum
149220150628
** use of the bits in our bitmasks. This structure provides a mapping
149221150629
** from the sparse cursor numbers into consecutive integers beginning
149222150630
** with 0.
@@ -149283,24 +150691,10 @@
149283150691
#endif
149284150692
#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
149285150693
# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
149286150694
#endif
149287150695
149288
-/*
149289
-** Each instance of this object records a change to a single node
149290
-** in an expression tree to cause that node to point to a column
149291
-** of an index rather than an expression or a virtual column. All
149292
-** such transformations need to be undone at the end of WHERE clause
149293
-** processing.
149294
-*/
149295
-typedef struct WhereExprMod WhereExprMod;
149296
-struct WhereExprMod {
149297
- WhereExprMod *pNext; /* Next translation on a list of them all */
149298
- Expr *pExpr; /* The Expr node that was transformed */
149299
- Expr orig; /* Original value of the Expr node */
149300
-};
149301
-
149302150696
/*
149303150697
** The WHERE clause processing routine has two halves. The
149304150698
** first part does the start of the WHERE loop and the second
149305150699
** half does the tail of the WHERE loop. An instance of
149306150700
** this structure is returned by the first half and passed
@@ -149312,14 +150706,14 @@
149312150706
struct WhereInfo {
149313150707
Parse *pParse; /* Parsing and code generating context */
149314150708
SrcList *pTabList; /* List of tables in the join */
149315150709
ExprList *pOrderBy; /* The ORDER BY clause or NULL */
149316150710
ExprList *pResultSet; /* Result set of the query */
150711
+#if WHERETRACE_ENABLED
149317150712
Expr *pWhere; /* The complete WHERE clause */
149318
-#ifndef SQLITE_OMIT_VIRTUALTABLE
149319
- Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
149320150713
#endif
150714
+ Select *pSelect; /* The entire SELECT statement containing WHERE */
149321150715
int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
149322150716
int iContinue; /* Jump here to continue with next record */
149323150717
int iBreak; /* Jump here to break out of the loop */
149324150718
int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149325150719
u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -149334,11 +150728,10 @@
149334150728
unsigned sorted :1; /* True if really sorted (not just grouped) */
149335150729
LogEst nRowOut; /* Estimated number of output rows */
149336150730
int iTop; /* The very beginning of the WHERE loop */
149337150731
int iEndWhere; /* End of the WHERE clause itself */
149338150732
WhereLoop *pLoops; /* List of all WhereLoop objects */
149339
- WhereExprMod *pExprMods; /* Expression modifications */
149340150733
WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
149341150734
Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
149342150735
WhereClause sWC; /* Decomposition of the WHERE clause */
149343150736
WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
149344150737
WhereLevel a[1]; /* Information about each nest loop in WHERE */
@@ -150686,147 +152079,10 @@
150686152079
assert( nReg==1 || pParse->nErr );
150687152080
sqlite3ExprCode(pParse, p, iReg);
150688152081
}
150689152082
}
150690152083
150691
-/* An instance of the IdxExprTrans object carries information about a
150692
-** mapping from an expression on table columns into a column in an index
150693
-** down through the Walker.
150694
-*/
150695
-typedef struct IdxExprTrans {
150696
- Expr *pIdxExpr; /* The index expression */
150697
- int iTabCur; /* The cursor of the corresponding table */
150698
- int iIdxCur; /* The cursor for the index */
150699
- int iIdxCol; /* The column for the index */
150700
- int iTabCol; /* The column for the table */
150701
- WhereInfo *pWInfo; /* Complete WHERE clause information */
150702
- sqlite3 *db; /* Database connection (for malloc()) */
150703
-} IdxExprTrans;
150704
-
150705
-/*
150706
-** Preserve pExpr on the WhereETrans list of the WhereInfo.
150707
-*/
150708
-static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
150709
- WhereExprMod *pNew;
150710
- pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
150711
- if( pNew==0 ) return;
150712
- pNew->pNext = pTrans->pWInfo->pExprMods;
150713
- pTrans->pWInfo->pExprMods = pNew;
150714
- pNew->pExpr = pExpr;
150715
- memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
150716
-}
150717
-
150718
-/* The walker node callback used to transform matching expressions into
150719
-** a reference to an index column for an index on an expression.
150720
-**
150721
-** If pExpr matches, then transform it into a reference to the index column
150722
-** that contains the value of pExpr.
150723
-*/
150724
-static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
150725
- IdxExprTrans *pX = p->u.pIdxTrans;
150726
- if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
150727
- pExpr = sqlite3ExprSkipCollate(pExpr);
150728
- preserveExpr(pX, pExpr);
150729
- pExpr->affExpr = sqlite3ExprAffinity(pExpr);
150730
- pExpr->op = TK_COLUMN;
150731
- pExpr->iTable = pX->iIdxCur;
150732
- pExpr->iColumn = pX->iIdxCol;
150733
- testcase( ExprHasProperty(pExpr, EP_Unlikely) );
150734
- ExprClearProperty(pExpr, EP_Skip|EP_Unlikely|EP_WinFunc|EP_Subrtn);
150735
- pExpr->y.pTab = 0;
150736
- return WRC_Prune;
150737
- }else{
150738
- return WRC_Continue;
150739
- }
150740
-}
150741
-
150742
-#ifndef SQLITE_OMIT_GENERATED_COLUMNS
150743
-/* A walker node callback that translates a column reference to a table
150744
-** into a corresponding column reference of an index.
150745
-*/
150746
-static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
150747
- if( pExpr->op==TK_COLUMN ){
150748
- IdxExprTrans *pX = p->u.pIdxTrans;
150749
- if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
150750
- assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 );
150751
- preserveExpr(pX, pExpr);
150752
- pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
150753
- pExpr->iTable = pX->iIdxCur;
150754
- pExpr->iColumn = pX->iIdxCol;
150755
- pExpr->y.pTab = 0;
150756
- }
150757
- }
150758
- return WRC_Continue;
150759
-}
150760
-#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150761
-
150762
-/*
150763
-** For an indexes on expression X, locate every instance of expression X
150764
-** in pExpr and change that subexpression into a reference to the appropriate
150765
-** column of the index.
150766
-**
150767
-** 2019-10-24: Updated to also translate references to a VIRTUAL column in
150768
-** the table into references to the corresponding (stored) column of the
150769
-** index.
150770
-*/
150771
-static void whereIndexExprTrans(
150772
- Index *pIdx, /* The Index */
150773
- int iTabCur, /* Cursor of the table that is being indexed */
150774
- int iIdxCur, /* Cursor of the index itself */
150775
- WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
150776
-){
150777
- int iIdxCol; /* Column number of the index */
150778
- ExprList *aColExpr; /* Expressions that are indexed */
150779
- Table *pTab;
150780
- Walker w;
150781
- IdxExprTrans x;
150782
- aColExpr = pIdx->aColExpr;
150783
- if( aColExpr==0 && !pIdx->bHasVCol ){
150784
- /* The index does not reference any expressions or virtual columns
150785
- ** so no translations are needed. */
150786
- return;
150787
- }
150788
- pTab = pIdx->pTable;
150789
- memset(&w, 0, sizeof(w));
150790
- w.u.pIdxTrans = &x;
150791
- x.iTabCur = iTabCur;
150792
- x.iIdxCur = iIdxCur;
150793
- x.pWInfo = pWInfo;
150794
- x.db = pWInfo->pParse->db;
150795
- for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
150796
- i16 iRef = pIdx->aiColumn[iIdxCol];
150797
- if( iRef==XN_EXPR ){
150798
- assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 );
150799
- x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
150800
- if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue;
150801
- w.xExprCallback = whereIndexExprTransNode;
150802
-#ifndef SQLITE_OMIT_GENERATED_COLUMNS
150803
- }else if( iRef>=0
150804
- && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
150805
- && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0
150806
- || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]),
150807
- sqlite3StrBINARY)==0)
150808
- ){
150809
- /* Check to see if there are direct references to generated columns
150810
- ** that are contained in the index. Pulling the generated column
150811
- ** out of the index is an optimization only - the main table is always
150812
- ** available if the index cannot be used. To avoid unnecessary
150813
- ** complication, omit this optimization if the collating sequence for
150814
- ** the column is non-standard */
150815
- x.iTabCol = iRef;
150816
- w.xExprCallback = whereIndexExprTransColumn;
150817
-#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150818
- }else{
150819
- continue;
150820
- }
150821
- x.iIdxCol = iIdxCol;
150822
- sqlite3WalkExpr(&w, pWInfo->pWhere);
150823
- sqlite3WalkExprList(&w, pWInfo->pOrderBy);
150824
- sqlite3WalkExprList(&w, pWInfo->pResultSet);
150825
- }
150826
-}
150827
-
150828152084
/*
150829152085
** The pTruth expression is always true because it is the WHERE clause
150830152086
** a partial index that is driving a query loop. Look through all of the
150831152087
** WHERE clause terms on the query, and if any of those terms must be
150832152088
** true because pTruth is true, then mark those WHERE clause terms as
@@ -150891,10 +152147,12 @@
150891152147
assert( pTerm!=0 );
150892152148
assert( pTerm->pExpr!=0 );
150893152149
testcase( pTerm->wtFlags & TERM_VIRTUAL );
150894152150
regRowid = sqlite3GetTempReg(pParse);
150895152151
regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
152152
+ sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
152153
+ VdbeCoverage(pParse->pVdbe);
150896152154
sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150897152155
addrNxt, regRowid, 1);
150898152156
VdbeCoverage(pParse->pVdbe);
150899152157
}else{
150900152158
u16 nEq = pLoop->u.btree.nEq;
@@ -151042,13 +152300,13 @@
151042152300
codeExprOrVector(pParse, pRight, iTarget, 1);
151043152301
if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
151044152302
&& pLoop->u.vtab.bOmitOffset
151045152303
){
151046152304
assert( pTerm->eOperator==WO_AUX );
151047
- assert( pWInfo->pLimit!=0 );
151048
- assert( pWInfo->pLimit->iOffset>0 );
151049
- sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
152305
+ assert( pWInfo->pSelect!=0 );
152306
+ assert( pWInfo->pSelect->iOffset>0 );
152307
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
151050152308
VdbeComment((v,"Zero OFFSET counter"));
151051152309
}
151052152310
}
151053152311
}
151054152312
sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
@@ -151152,10 +152410,12 @@
151152152410
iReleaseReg = ++pParse->nMem;
151153152411
iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
151154152412
if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
151155152413
addrNxt = pLevel->addrNxt;
151156152414
if( pLevel->regFilter ){
152415
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
152416
+ VdbeCoverage(v);
151157152417
sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
151158152418
iRowidReg, 1);
151159152419
VdbeCoverage(v);
151160152420
filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
151161152421
}
@@ -151503,10 +152763,15 @@
151503152763
** of entries in the tree, so basing the number of steps to try
151504152764
** on the estimated number of rows in the btree seems like a good
151505152765
** guess. */
151506152766
addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
151507152767
(pIdx->aiRowLogEst[0]+9)/10);
152768
+ if( pRangeStart ){
152769
+ sqlite3VdbeChangeP5(v, 1);
152770
+ sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
152771
+ addrSeekScan = 0;
152772
+ }
151508152773
VdbeCoverage(v);
151509152774
}
151510152775
sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
151511152776
VdbeCoverage(v);
151512152777
VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
@@ -151641,31 +152906,10 @@
151641152906
sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
151642152907
iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151643152908
}
151644152909
151645152910
if( pLevel->iLeftJoin==0 ){
151646
- /* If pIdx is an index on one or more expressions, then look through
151647
- ** all the expressions in pWInfo and try to transform matching expressions
151648
- ** into reference to index columns. Also attempt to translate references
151649
- ** to virtual columns in the table into references to (stored) columns
151650
- ** of the index.
151651
- **
151652
- ** Do not do this for the RHS of a LEFT JOIN. This is because the
151653
- ** expression may be evaluated after OP_NullRow has been executed on
151654
- ** the cursor. In this case it is important to do the full evaluation,
151655
- ** as the result of the expression may not be NULL, even if all table
151656
- ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
151657
- **
151658
- ** Also, do not do this when processing one index an a multi-index
151659
- ** OR clause, since the transformation will become invalid once we
151660
- ** move forward to the next index.
151661
- ** https://sqlite.org/src/info/4e8e4857d32d401f
151662
- */
151663
- if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ){
151664
- whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
151665
- }
151666
-
151667152911
/* If a partial index is driving the loop, try to eliminate WHERE clause
151668152912
** terms from the query that must be true due to the WHERE clause of
151669152913
** the partial index.
151670152914
**
151671152915
** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
@@ -151774,11 +153018,11 @@
151774153018
*/
151775153019
if( pWInfo->nLevel>1 ){
151776153020
int nNotReady; /* The number of notReady tables */
151777153021
SrcItem *origSrc; /* Original list of tables */
151778153022
nNotReady = pWInfo->nLevel - iLevel - 1;
151779
- pOrTab = sqlite3StackAllocRaw(db,
153023
+ pOrTab = sqlite3DbMallocRawNN(db,
151780153024
sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
151781153025
if( pOrTab==0 ) return notReady;
151782153026
pOrTab->nAlloc = (u8)(nNotReady + 1);
151783153027
pOrTab->nSrc = pOrTab->nAlloc;
151784153028
memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
@@ -152027,11 +153271,11 @@
152027153271
** indent everything in between the this point and the final OP_Return.
152028153272
** See tag-20220407a in vdbe.c and shell.c */
152029153273
assert( pLevel->op==OP_Return );
152030153274
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
152031153275
152032
- if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
153276
+ if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
152033153277
if( !untestedTerms ) disableTerm(pLevel, pTerm);
152034153278
}else
152035153279
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
152036153280
152037153281
{
@@ -152655,11 +153899,11 @@
152655153899
** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152656153900
*/
152657153901
if( pLeft->op!=TK_COLUMN
152658153902
|| sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
152659153903
|| (ALWAYS( ExprUseYTab(pLeft) )
152660
- && pLeft->y.pTab
153904
+ && ALWAYS(pLeft->y.pTab)
152661153905
&& IsVirtual(pLeft->y.pTab)) /* Might be numeric */
152662153906
){
152663153907
int isNum;
152664153908
double rDummy;
152665153909
isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
@@ -152772,12 +154016,11 @@
152772154016
**
152773154017
** vtab_column MATCH expression
152774154018
** MATCH(expression,vtab_column)
152775154019
*/
152776154020
pCol = pList->a[1].pExpr;
152777
- assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152778
- testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
154021
+ assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
152779154022
if( ExprIsVtab(pCol) ){
152780154023
for(i=0; i<ArraySize(aOp); i++){
152781154024
assert( !ExprHasProperty(pExpr, EP_IntValue) );
152782154025
if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152783154026
*peOp2 = aOp[i].eOp2;
@@ -152798,11 +154041,11 @@
152798154041
** names. But for this use case, xFindFunction is expected to deal
152799154042
** with function names in an arbitrary case.
152800154043
*/
152801154044
pCol = pList->a[0].pExpr;
152802154045
assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152803
- testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
154046
+ assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
152804154047
if( ExprIsVtab(pCol) ){
152805154048
sqlite3_vtab *pVtab;
152806154049
sqlite3_module *pMod;
152807154050
void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
152808154051
void *pNotUsed;
@@ -152823,17 +154066,16 @@
152823154066
}
152824154067
}else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
152825154068
int res = 0;
152826154069
Expr *pLeft = pExpr->pLeft;
152827154070
Expr *pRight = pExpr->pRight;
152828
- assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
152829
- testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
154071
+ assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
152830154072
if( ExprIsVtab(pLeft) ){
152831154073
res++;
152832154074
}
152833
- assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
152834
- testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
154075
+ assert( pRight==0 || pRight->op!=TK_COLUMN
154076
+ || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
152835154077
if( pRight && ExprIsVtab(pRight) ){
152836154078
res++;
152837154079
SWAP(Expr*, pLeft, pRight);
152838154080
}
152839154081
*ppLeft = pLeft;
@@ -153378,10 +154620,11 @@
153378154620
iCur = pFrom->a[i].iCursor;
153379154621
for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153380154622
if( pIdx->aColExpr==0 ) continue;
153381154623
for(i=0; i<pIdx->nKeyCol; i++){
153382154624
if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
154625
+ assert( pIdx->bHasExpr );
153383154626
if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153384154627
aiCurCol[0] = iCur;
153385154628
aiCurCol[1] = XN_EXPR;
153386154629
return 1;
153387154630
}
@@ -153991,13 +155234,13 @@
153991155234
**
153992155235
** LIMIT and OFFSET terms are ignored by most of the planner code. They
153993155236
** exist only so that they may be passed to the xBestIndex method of the
153994155237
** single virtual table in the FROM clause of the SELECT.
153995155238
*/
153996
-SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
153997
- assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
153998
- if( (p && p->pLimit) /* 1 */
155239
+SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
155240
+ assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
155241
+ if( p->pGroupBy==0
153999155242
&& (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
154000155243
&& (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
154001155244
){
154002155245
ExprList *pOrderBy = p->pOrderBy;
154003155246
int iCsr = p->pSrc->a[0].iCursor;
@@ -156530,30 +157773,18 @@
156530157773
while( pWInfo->pLoops ){
156531157774
WhereLoop *p = pWInfo->pLoops;
156532157775
pWInfo->pLoops = p->pNextLoop;
156533157776
whereLoopDelete(db, p);
156534157777
}
156535
- assert( pWInfo->pExprMods==0 );
156536157778
while( pWInfo->pMemToFree ){
156537157779
WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156538157780
sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
156539157781
pWInfo->pMemToFree = pNext;
156540157782
}
156541157783
sqlite3DbNNFreeNN(db, pWInfo);
156542157784
}
156543157785
156544
-/* Undo all Expr node modifications
156545
-*/
156546
-static void whereUndoExprMods(WhereInfo *pWInfo){
156547
- while( pWInfo->pExprMods ){
156548
- WhereExprMod *p = pWInfo->pExprMods;
156549
- pWInfo->pExprMods = p->pNext;
156550
- memcpy(p->pExpr, &p->orig, sizeof(p->orig));
156551
- sqlite3DbFree(pWInfo->pParse->db, p);
156552
- }
156553
-}
156554
-
156555157786
/*
156556157787
** Return TRUE if all of the following are true:
156557157788
**
156558157789
** (1) X has the same or lower cost, or returns the same or fewer rows,
156559157790
** than Y.
@@ -157501,10 +158732,98 @@
157501158732
return 1;
157502158733
}
157503158734
}
157504158735
return 0;
157505158736
}
158737
+
158738
+/*
158739
+** Structure passed to the whereIsCoveringIndex Walker callback.
158740
+*/
158741
+struct CoveringIndexCheck {
158742
+ Index *pIdx; /* The index */
158743
+ int iTabCur; /* Cursor number for the corresponding table */
158744
+};
158745
+
158746
+/*
158747
+** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
158748
+**
158749
+** If the Expr node references the table with cursor pCk->iTabCur, then
158750
+** make sure that column is covered by the index pCk->pIdx. We know that
158751
+** all columns less than 63 (really BMS-1) are covered, so we don't need
158752
+** to check them. But we do need to check any column at 63 or greater.
158753
+**
158754
+** If the index does not cover the column, then set pWalk->eCode to
158755
+** non-zero and return WRC_Abort to stop the search.
158756
+**
158757
+** If this node does not disprove that the index can be a covering index,
158758
+** then just return WRC_Continue, to continue the search.
158759
+*/
158760
+static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158761
+ int i; /* Loop counter */
158762
+ const Index *pIdx; /* The index of interest */
158763
+ const i16 *aiColumn; /* Columns contained in the index */
158764
+ u16 nColumn; /* Number of columns in the index */
158765
+ if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158766
+ if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158767
+ if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158768
+ pIdx = pWalk->u.pCovIdxCk->pIdx;
158769
+ aiColumn = pIdx->aiColumn;
158770
+ nColumn = pIdx->nColumn;
158771
+ for(i=0; i<nColumn; i++){
158772
+ if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158773
+ }
158774
+ pWalk->eCode = 1;
158775
+ return WRC_Abort;
158776
+}
158777
+
158778
+
158779
+/*
158780
+** pIdx is an index that covers all of the low-number columns used by
158781
+** pWInfo->pSelect (columns from 0 through 62). But there are columns
158782
+** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158783
+** of whether pIdx covers *all* columns in the query.
158784
+**
158785
+** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158786
+** not a covering index or if we are unable to determine if pIdx is a
158787
+** covering index.
158788
+**
158789
+** This routine is an optimization. It is always safe to return non-zero.
158790
+** But returning zero when non-zero should have been returned can lead to
158791
+** incorrect bytecode and assertion faults.
158792
+*/
158793
+static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158794
+ WhereInfo *pWInfo, /* The WHERE clause context */
158795
+ Index *pIdx, /* Index that is being tested */
158796
+ int iTabCur /* Cursor for the table being indexed */
158797
+){
158798
+ int i;
158799
+ struct CoveringIndexCheck ck;
158800
+ Walker w;
158801
+ if( pWInfo->pSelect==0 ){
158802
+ /* We don't have access to the full query, so we cannot check to see
158803
+ ** if pIdx is covering. Assume it is not. */
158804
+ return 1;
158805
+ }
158806
+ for(i=0; i<pIdx->nColumn; i++){
158807
+ if( pIdx->aiColumn[i]>=BMS-1 ) break;
158808
+ }
158809
+ if( i>=pIdx->nColumn ){
158810
+ /* pIdx does not index any columns greater than 62, but we know from
158811
+ ** colMask that columns greater than 62 are used, so this is not a
158812
+ ** covering index */
158813
+ return 1;
158814
+ }
158815
+ ck.pIdx = pIdx;
158816
+ ck.iTabCur = iTabCur;
158817
+ memset(&w, 0, sizeof(w));
158818
+ w.xExprCallback = whereIsCoveringIndexWalkCallback;
158819
+ w.xSelectCallback = sqlite3SelectWalkNoop;
158820
+ w.u.pCovIdxCk = &ck;
158821
+ w.eCode = 0;
158822
+ sqlite3WalkSelect(&w, pWInfo->pSelect);
158823
+ return w.eCode;
158824
+}
157506158825
157507158826
/*
157508158827
** Add all WhereLoop objects for a single table of the join where the table
157509158828
** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157510158829
** a b-tree table, not a virtual table.
@@ -157719,10 +159038,13 @@
157719159038
if( pProbe->isCovering ){
157720159039
pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
157721159040
m = 0;
157722159041
}else{
157723159042
m = pSrc->colUsed & pProbe->colNotIdxed;
159043
+ if( m==TOPBIT ){
159044
+ m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159045
+ }
157724159046
pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
157725159047
}
157726159048
157727159049
/* Full scan via index */
157728159050
if( b
@@ -158944,11 +160266,10 @@
158944160266
*/
158945160267
static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
158946160268
int mxChoice; /* Maximum number of simultaneous paths tracked */
158947160269
int nLoop; /* Number of terms in the join */
158948160270
Parse *pParse; /* Parsing context */
158949
- sqlite3 *db; /* The database connection */
158950160271
int iLoop; /* Loop counter over the terms of the join */
158951160272
int ii, jj; /* Loop counters */
158952160273
int mxI = 0; /* Index of next entry to replace */
158953160274
int nOrderBy; /* Number of ORDER BY clause terms */
158954160275
LogEst mxCost = 0; /* Maximum cost of a set of paths */
@@ -158963,11 +160284,10 @@
158963160284
LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
158964160285
char *pSpace; /* Temporary memory used by this routine */
158965160286
int nSpace; /* Bytes of space allocated at pSpace */
158966160287
158967160288
pParse = pWInfo->pParse;
158968
- db = pParse->db;
158969160289
nLoop = pWInfo->nLevel;
158970160290
/* TUNING: For simple queries, only the best path is tracked.
158971160291
** For 2-way joins, the 5 best paths are followed.
158972160292
** For joins of 3 or more tables, track the 10 best paths */
158973160293
mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
@@ -158986,11 +160306,11 @@
158986160306
}
158987160307
158988160308
/* Allocate and initialize space for aTo, aFrom and aSortCost[] */
158989160309
nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
158990160310
nSpace += sizeof(LogEst) * nOrderBy;
158991
- pSpace = sqlite3DbMallocRawNN(db, nSpace);
160311
+ pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
158992160312
if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
158993160313
aTo = (WherePath*)pSpace;
158994160314
aFrom = aTo+mxChoice;
158995160315
memset(aFrom, 0, sizeof(aFrom[0]));
158996160316
pX = (WhereLoop**)(aFrom+mxChoice);
@@ -159244,11 +160564,11 @@
159244160564
nFrom = nTo;
159245160565
}
159246160566
159247160567
if( nFrom==0 ){
159248160568
sqlite3ErrorMsg(pParse, "no query solution");
159249
- sqlite3DbFreeNN(db, pSpace);
160569
+ sqlite3StackFreeNN(pParse->db, pSpace);
159250160570
return SQLITE_ERROR;
159251160571
}
159252160572
159253160573
/* Find the lowest cost path. pFrom will be left pointing to that path */
159254160574
pFrom = aFrom;
@@ -159326,12 +160646,11 @@
159326160646
159327160647
159328160648
pWInfo->nRowOut = pFrom->nRow;
159329160649
159330160650
/* Free temporary memory and return success */
159331
- assert( db!=0 );
159332
- sqlite3DbNNFreeNN(db, pSpace);
160651
+ sqlite3StackFreeNN(pParse->db, pSpace);
159333160652
return SQLITE_OK;
159334160653
}
159335160654
159336160655
/*
159337160656
** Most queries use only a single table (they are not joins) and have
@@ -159625,10 +160944,81 @@
159625160944
}
159626160945
}
159627160946
nSearch += pLoop->nOut;
159628160947
}
159629160948
}
160949
+
160950
+/*
160951
+** This is an sqlite3ParserAddCleanup() callback that is invoked to
160952
+** free the Parse->pIdxExpr list when the Parse object is destroyed.
160953
+*/
160954
+static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
160955
+ Parse *pParse = (Parse*)pObject;
160956
+ while( pParse->pIdxExpr!=0 ){
160957
+ IndexedExpr *p = pParse->pIdxExpr;
160958
+ pParse->pIdxExpr = p->pIENext;
160959
+ sqlite3ExprDelete(db, p->pExpr);
160960
+ sqlite3DbFreeNN(db, p);
160961
+ }
160962
+}
160963
+
160964
+/*
160965
+** The index pIdx is used by a query and contains one or more expressions.
160966
+** In other words pIdx is an index on an expression. iIdxCur is the cursor
160967
+** number for the index and iDataCur is the cursor number for the corresponding
160968
+** table.
160969
+**
160970
+** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
160971
+** each of the expressions in the index so that the expression code generator
160972
+** will know to replace occurrences of the indexed expression with
160973
+** references to the corresponding column of the index.
160974
+*/
160975
+static SQLITE_NOINLINE void whereAddIndexedExpr(
160976
+ Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
160977
+ Index *pIdx, /* The index-on-expression that contains the expressions */
160978
+ int iIdxCur, /* Cursor number for pIdx */
160979
+ SrcItem *pTabItem /* The FROM clause entry for the table */
160980
+){
160981
+ int i;
160982
+ IndexedExpr *p;
160983
+ Table *pTab;
160984
+ assert( pIdx->bHasExpr );
160985
+ pTab = pIdx->pTable;
160986
+ for(i=0; i<pIdx->nColumn; i++){
160987
+ Expr *pExpr;
160988
+ int j = pIdx->aiColumn[i];
160989
+ int bMaybeNullRow;
160990
+ if( j==XN_EXPR ){
160991
+ pExpr = pIdx->aColExpr->a[i].pExpr;
160992
+ testcase( pTabItem->fg.jointype & JT_LEFT );
160993
+ testcase( pTabItem->fg.jointype & JT_RIGHT );
160994
+ testcase( pTabItem->fg.jointype & JT_LTORJ );
160995
+ bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
160996
+ }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
160997
+ pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
160998
+ bMaybeNullRow = 0;
160999
+ }else{
161000
+ continue;
161001
+ }
161002
+ if( sqlite3ExprIsConstant(pExpr) ) continue;
161003
+ p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
161004
+ if( p==0 ) break;
161005
+ p->pIENext = pParse->pIdxExpr;
161006
+ p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
161007
+ p->iDataCur = pTabItem->iCursor;
161008
+ p->iIdxCur = iIdxCur;
161009
+ p->iIdxCol = i;
161010
+ p->bMaybeNullRow = bMaybeNullRow;
161011
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
161012
+ p->zIdxName = pIdx->zName;
161013
+#endif
161014
+ pParse->pIdxExpr = p;
161015
+ if( p->pIENext==0 ){
161016
+ sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161017
+ }
161018
+ }
161019
+}
159630161020
159631161021
/*
159632161022
** Generate the beginning of the loop used for WHERE clause processing.
159633161023
** The return value is a pointer to an opaque structure that contains
159634161024
** information needed to terminate the loop. Later, the calling routine
@@ -159720,11 +161110,11 @@
159720161110
Parse *pParse, /* The parser context */
159721161111
SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
159722161112
Expr *pWhere, /* The WHERE clause */
159723161113
ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
159724161114
ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
159725
- Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
161115
+ Select *pSelect, /* The entire SELECT statement */
159726161116
u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
159727161117
int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
159728161118
** If WHERE_USE_LIMIT, then the limit amount */
159729161119
){
159730161120
int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -159789,21 +161179,21 @@
159789161179
goto whereBeginError;
159790161180
}
159791161181
pWInfo->pParse = pParse;
159792161182
pWInfo->pTabList = pTabList;
159793161183
pWInfo->pOrderBy = pOrderBy;
161184
+#if WHERETRACE_ENABLED
159794161185
pWInfo->pWhere = pWhere;
161186
+#endif
159795161187
pWInfo->pResultSet = pResultSet;
159796161188
pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159797161189
pWInfo->nLevel = nTabList;
159798161190
pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159799161191
pWInfo->wctrlFlags = wctrlFlags;
159800161192
pWInfo->iLimit = iAuxArg;
159801161193
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159802
-#ifndef SQLITE_OMIT_VIRTUALTABLE
159803
- pWInfo->pLimit = pLimit;
159804
-#endif
161194
+ pWInfo->pSelect = pSelect;
159805161195
memset(&pWInfo->nOBSat, 0,
159806161196
offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
159807161197
memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159808161198
assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
159809161199
pMaskSet = &pWInfo->sMaskSet;
@@ -159868,11 +161258,13 @@
159868161258
#endif
159869161259
}
159870161260
159871161261
/* Analyze all of the subexpressions. */
159872161262
sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159873
- sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
161263
+ if( pSelect && pSelect->pLimit ){
161264
+ sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
161265
+ }
159874161266
if( pParse->nErr ) goto whereBeginError;
159875161267
159876161268
/* Special case: WHERE terms that do not refer to any tables in the join
159877161269
** (constant expressions). Evaluate each such term, and jump over all the
159878161270
** generated code if the result is not true.
@@ -160171,10 +161563,13 @@
160171161563
}else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
160172161564
iIndexCur = iAuxArg;
160173161565
op = OP_ReopenIdx;
160174161566
}else{
160175161567
iIndexCur = pParse->nTab++;
161568
+ if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
161569
+ whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
161570
+ }
160176161571
}
160177161572
pLevel->iIdxCur = iIndexCur;
160178161573
assert( pIx!=0 );
160179161574
assert( pIx->pSchema==pTab->pSchema );
160180161575
assert( iIndexCur>=0 );
@@ -160293,12 +161688,10 @@
160293161688
return pWInfo;
160294161689
160295161690
/* Jump here if malloc fails */
160296161691
whereBeginError:
160297161692
if( pWInfo ){
160298
- testcase( pWInfo->pExprMods!=0 );
160299
- whereUndoExprMods(pWInfo);
160300161693
pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160301161694
whereInfoFree(db, pWInfo);
160302161695
}
160303161696
return 0;
160304161697
}
@@ -160513,11 +161906,10 @@
160513161906
VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160514161907
pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160515161908
}
160516161909
160517161910
assert( pWInfo->nLevel<=pTabList->nSrc );
160518
- if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160519161911
for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160520161912
int k, last;
160521161913
VdbeOp *pOp, *pLastOp;
160522161914
Index *pIdx = 0;
160523161915
SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
@@ -160566,10 +161958,20 @@
160566161958
){
160567161959
if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
160568161960
last = iEnd;
160569161961
}else{
160570161962
last = pWInfo->iEndWhere;
161963
+ }
161964
+ if( pIdx->bHasExpr ){
161965
+ IndexedExpr *p = pParse->pIdxExpr;
161966
+ while( p ){
161967
+ if( p->iIdxCur==pLevel->iIdxCur ){
161968
+ p->iDataCur = -1;
161969
+ p->iIdxCur = -1;
161970
+ }
161971
+ p = p->pIENext;
161972
+ }
160571161973
}
160572161974
k = pLevel->addrBody + 1;
160573161975
#ifdef SQLITE_DEBUG
160574161976
if( db->flags & SQLITE_VdbeAddopTrace ){
160575161977
printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
@@ -174293,10 +175695,23 @@
174293175695
createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
174294175696
if( db->mallocFailed ){
174295175697
goto opendb_out;
174296175698
}
174297175699
175700
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
175701
+ /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
175702
+ if( zFilename && zFilename[0]==':' ){
175703
+ if( strcmp(zFilename, ":localStorage:")==0 ){
175704
+ zFilename = "file:local?vfs=kvvfs";
175705
+ flags |= SQLITE_OPEN_URI;
175706
+ }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
175707
+ zFilename = "file:session?vfs=kvvfs";
175708
+ flags |= SQLITE_OPEN_URI;
175709
+ }
175710
+ }
175711
+#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
175712
+
174298175713
/* Parse the filename/URI argument
174299175714
**
174300175715
** Only allow sensible combinations of bits in the flags argument.
174301175716
** Throw an error if any non-sense combination is used. If we
174302175717
** do not block illegal combinations here, it could trigger
@@ -175722,12 +177137,12 @@
175722177137
** Recover as many snapshots as possible from the wal file associated with
175723177138
** schema zDb of database db.
175724177139
*/
175725177140
SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
175726177141
int rc = SQLITE_ERROR;
175727
- int iDb;
175728177142
#ifndef SQLITE_OMIT_WAL
177143
+ int iDb;
175729177144
175730177145
#ifdef SQLITE_ENABLE_API_ARMOR
175731177146
if( !sqlite3SafetyCheckOk(db) ){
175732177147
return SQLITE_MISUSE_BKPT;
175733177148
}
@@ -201626,11 +203041,11 @@
201626203041
return SQLITE_LOCKED_VTAB;
201627203042
}
201628203043
rtreeReference(pRtree);
201629203044
assert(nData>=1);
201630203045
201631
- cell.iRowid = 0; /* Used only to suppress a compiler warning */
203046
+ memset(&cell, 0, sizeof(cell));
201632203047
201633203048
/* Constraint handling. A write operation on an r-tree table may return
201634203049
** SQLITE_CONSTRAINT for two reasons:
201635203050
**
201636203051
** 1. A duplicate rowid value, or
@@ -236984,11 +238399,11 @@
236984238399
int nArg, /* Number of args */
236985238400
sqlite3_value **apUnused /* Function arguments */
236986238401
){
236987238402
assert( nArg==0 );
236988238403
UNUSED_PARAM2(nArg, apUnused);
236989
- sqlite3_result_text(pCtx, "fts5: 2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646", -1, SQLITE_TRANSIENT);
238404
+ sqlite3_result_text(pCtx, "fts5: 2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910", -1, SQLITE_TRANSIENT);
236990238405
}
236991238406
236992238407
/*
236993238408
** Return true if zName is the extension on one of the shadow tables used
236994238409
** by this module.
236995238410
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
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-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -974,17 +974,21 @@
974 /*
975 ** CAPI3REF: File Locking Levels
976 **
977 ** SQLite uses one of these integer values as the second
978 ** argument to calls it makes to the xLock() and xUnlock() methods
979 ** of an [sqlite3_io_methods] object.
 
 
 
 
980 */
981 #define SQLITE_LOCK_NONE 0
982 #define SQLITE_LOCK_SHARED 1
983 #define SQLITE_LOCK_RESERVED 2
984 #define SQLITE_LOCK_PENDING 3
985 #define SQLITE_LOCK_EXCLUSIVE 4
986
987 /*
988 ** CAPI3REF: Synchronization Type Flags
989 **
990 ** When SQLite invokes the xSync() method of an
@@ -1058,11 +1062,18 @@
1058 ** <li> [SQLITE_LOCK_SHARED],
1059 ** <li> [SQLITE_LOCK_RESERVED],
1060 ** <li> [SQLITE_LOCK_PENDING], or
1061 ** <li> [SQLITE_LOCK_EXCLUSIVE].
1062 ** </ul>
1063 ** xLock() increases the lock. xUnlock() decreases the lock.
 
 
 
 
 
 
 
1064 ** The xCheckReservedLock() method checks whether any database connection,
1065 ** either in this process or in some other process, is holding a RESERVED,
1066 ** PENDING, or EXCLUSIVE lock on the file. It returns true
1067 ** if such a lock exists and false otherwise.
1068 **
@@ -1163,13 +1174,12 @@
1163 ** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1164 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1165 ** opcode causes the xFileControl method to write the current state of
1166 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1167 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1168 ** into an integer that the pArg argument points to. This capability
1169 ** is used during testing and is only available when the SQLITE_TEST
1170 ** compile-time option is used.
1171 **
1172 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1173 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1174 ** layer a hint of how large the database file will grow to be during the
1175 ** current transaction. This hint is not guaranteed to be accurate but it
@@ -5815,10 +5825,20 @@
5815 ** such a conversion is possible without loss of information (in other
5816 ** words, if the value is a string that looks like a number)
5817 ** then the conversion is performed. Otherwise no conversion occurs.
5818 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5819 **
 
 
 
 
 
 
 
 
 
 
5820 ** ^Within the [xUpdate] method of a [virtual table], the
5821 ** sqlite3_value_nochange(X) interface returns true if and only if
5822 ** the column corresponding to X is unchanged by the UPDATE operation
5823 ** that the xUpdate method call was invoked to implement and if
5824 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5879,10 +5899,11 @@
5879 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5880 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5881 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5882 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5883 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
 
5884
5885 /*
5886 ** CAPI3REF: Finding The Subtype Of SQL Values
5887 ** METHOD: sqlite3_value
5888 **
@@ -5932,11 +5953,11 @@
5932 ** In those cases, sqlite3_aggregate_context() might be called for the
5933 ** first time from within xFinal().)^
5934 **
5935 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5936 ** when first called if N is less than or equal to zero or if a memory
5937 ** allocate error occurs.
5938 **
5939 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5940 ** determined by the N parameter on first successful call. Changing the
5941 ** value of N in any subsequent call to sqlite3_aggregate_context() within
5942 ** the same aggregate function instance will not resize the memory
@@ -13160,11 +13181,11 @@
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)
13165 #include "config.h"
13166 #define SQLITECONFIG_H 1
13167 #endif
13168
13169 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13170 /************** Begin file sqliteLimit.h *************************************/
@@ -14643,10 +14664,11 @@
14643 typedef struct FuncDestructor FuncDestructor;
14644 typedef struct FuncDef FuncDef;
14645 typedef struct FuncDefHash FuncDefHash;
14646 typedef struct IdList IdList;
14647 typedef struct Index Index;
 
14648 typedef struct IndexSample IndexSample;
14649 typedef struct KeyClass KeyClass;
14650 typedef struct KeyInfo KeyInfo;
14651 typedef struct Lookaside Lookaside;
14652 typedef struct LookasideSlot LookasideSlot;
@@ -14708,10 +14730,11 @@
14708 #define MASKBIT(n) (((Bitmask)1)<<(n))
14709 #define MASKBIT64(n) (((u64)1)<<(n))
14710 #define MASKBIT32(n) (((unsigned int)1)<<(n))
14711 #define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
14712 #define ALLBITS ((Bitmask)-1)
 
14713
14714 /* A VList object records a mapping between parameters/variables/wildcards
14715 ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14716 ** variable number associated with that parameter. See the format description
14717 ** on the sqlite3VListAdd() routine for more information. A VList is really
@@ -14722,10 +14745,335 @@
14722 /*
14723 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
14724 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14725 ** pointer types (i.e. FuncDef) defined above.
14726 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14727 /************** Include pager.h in the middle of sqliteInt.h *****************/
14728 /************** Begin file pager.h *******************************************/
14729 /*
14730 ** 2001 September 15
14731 **
@@ -15576,11 +15924,11 @@
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] */
@@ -15759,11 +16107,11 @@
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,\
@@ -15856,10 +16204,11 @@
15856 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
15857 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
15858 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
15859 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
15860 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
 
15861 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
15862 SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
15863 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15864 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15865 #ifdef SQLITE_DEBUG
@@ -16218,301 +16567,10 @@
16218 #endif
16219
16220 #endif /* _PCACHE_H_ */
16221
16222 /************** End of pcache.h **********************************************/
16223 /************** Continuing where we left off in sqliteInt.h ******************/
16224 /************** Include os.h in the middle of sqliteInt.h ********************/
16225 /************** Begin file os.h **********************************************/
16226 /*
16227 ** 2001 September 16
16228 **
16229 ** The author disclaims copyright to this source code. In place of
16230 ** a legal notice, here is a blessing:
16231 **
16232 ** May you do good and not evil.
16233 ** May you find forgiveness for yourself and forgive others.
16234 ** May you share freely, never taking more than you give.
16235 **
16236 ******************************************************************************
16237 **
16238 ** This header file (together with is companion C source-code file
16239 ** "os.c") attempt to abstract the underlying operating system so that
16240 ** the SQLite library will work on both POSIX and windows systems.
16241 **
16242 ** This header file is #include-ed by sqliteInt.h and thus ends up
16243 ** being included by every source file.
16244 */
16245 #ifndef _SQLITE_OS_H_
16246 #define _SQLITE_OS_H_
16247
16248 /*
16249 ** Attempt to automatically detect the operating system and setup the
16250 ** necessary pre-processor macros for it.
16251 */
16252 /************** Include os_setup.h in the middle of os.h *********************/
16253 /************** Begin file os_setup.h ****************************************/
16254 /*
16255 ** 2013 November 25
16256 **
16257 ** The author disclaims copyright to this source code. In place of
16258 ** a legal notice, here is a blessing:
16259 **
16260 ** May you do good and not evil.
16261 ** May you find forgiveness for yourself and forgive others.
16262 ** May you share freely, never taking more than you give.
16263 **
16264 ******************************************************************************
16265 **
16266 ** This file contains pre-processor directives related to operating system
16267 ** detection and/or setup.
16268 */
16269 #ifndef SQLITE_OS_SETUP_H
16270 #define SQLITE_OS_SETUP_H
16271
16272 /*
16273 ** Figure out if we are dealing with Unix, Windows, or some other operating
16274 ** system.
16275 **
16276 ** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
16277 ** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
16278 ** the three will be 1. The other two will be 0.
16279 */
16280 #if defined(SQLITE_OS_OTHER)
16281 # if SQLITE_OS_OTHER==1
16282 # undef SQLITE_OS_UNIX
16283 # define SQLITE_OS_UNIX 0
16284 # undef SQLITE_OS_WIN
16285 # define SQLITE_OS_WIN 0
16286 # else
16287 # undef SQLITE_OS_OTHER
16288 # endif
16289 #endif
16290 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
16291 # define SQLITE_OS_OTHER 0
16292 # ifndef SQLITE_OS_WIN
16293 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
16294 defined(__MINGW32__) || defined(__BORLANDC__)
16295 # define SQLITE_OS_WIN 1
16296 # define SQLITE_OS_UNIX 0
16297 # else
16298 # define SQLITE_OS_WIN 0
16299 # define SQLITE_OS_UNIX 1
16300 # endif
16301 # else
16302 # define SQLITE_OS_UNIX 0
16303 # endif
16304 #else
16305 # ifndef SQLITE_OS_WIN
16306 # define SQLITE_OS_WIN 0
16307 # endif
16308 #endif
16309
16310 #endif /* SQLITE_OS_SETUP_H */
16311
16312 /************** End of os_setup.h ********************************************/
16313 /************** Continuing where we left off in os.h *************************/
16314
16315 /* If the SET_FULLSYNC macro is not defined above, then make it
16316 ** a no-op
16317 */
16318 #ifndef SET_FULLSYNC
16319 # define SET_FULLSYNC(x,y)
16320 #endif
16321
16322 /* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
16323 */
16324 #ifndef SQLITE_MAX_PATHLEN
16325 # define SQLITE_MAX_PATHLEN FILENAME_MAX
16326 #endif
16327
16328 /* Maximum number of symlinks that will be resolved while trying to
16329 ** expand a filename in xFullPathname() in the VFS.
16330 */
16331 #ifndef SQLITE_MAX_SYMLINK
16332 # define SQLITE_MAX_SYMLINK 200
16333 #endif
16334
16335 /*
16336 ** The default size of a disk sector
16337 */
16338 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
16339 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
16340 #endif
16341
16342 /*
16343 ** Temporary files are named starting with this prefix followed by 16 random
16344 ** alphanumeric characters, and no file extension. They are stored in the
16345 ** OS's standard temporary file directory, and are deleted prior to exit.
16346 ** If sqlite is being embedded in another program, you may wish to change the
16347 ** prefix to reflect your program's name, so that if your program exits
16348 ** prematurely, old temporary files can be easily identified. This can be done
16349 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16350 **
16351 ** 2006-10-31: The default prefix used to be "sqlite_". But then
16352 ** Mcafee started using SQLite in their anti-virus product and it
16353 ** started putting files with the "sqlite" name in the c:/temp folder.
16354 ** This annoyed many windows users. Those users would then do a
16355 ** Google search for "sqlite", find the telephone numbers of the
16356 ** developers and call to wake them up at night and complain.
16357 ** For this reason, the default name prefix is changed to be "sqlite"
16358 ** spelled backwards. So the temp files are still identified, but
16359 ** anybody smart enough to figure out the code is also likely smart
16360 ** enough to know that calling the developer will not help get rid
16361 ** of the file.
16362 */
16363 #ifndef SQLITE_TEMP_FILE_PREFIX
16364 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
16365 #endif
16366
16367 /*
16368 ** The following values may be passed as the second argument to
16369 ** sqlite3OsLock(). The various locks exhibit the following semantics:
16370 **
16371 ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
16372 ** RESERVED: A single process may hold a RESERVED lock on a file at
16373 ** any time. Other processes may hold and obtain new SHARED locks.
16374 ** PENDING: A single process may hold a PENDING lock on a file at
16375 ** any one time. Existing SHARED locks may persist, but no new
16376 ** SHARED locks may be obtained by other processes.
16377 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
16378 **
16379 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
16380 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
16381 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
16382 ** sqlite3OsLock().
16383 */
16384 #define NO_LOCK 0
16385 #define SHARED_LOCK 1
16386 #define RESERVED_LOCK 2
16387 #define PENDING_LOCK 3
16388 #define EXCLUSIVE_LOCK 4
16389
16390 /*
16391 ** File Locking Notes: (Mostly about windows but also some info for Unix)
16392 **
16393 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
16394 ** those functions are not available. So we use only LockFile() and
16395 ** UnlockFile().
16396 **
16397 ** LockFile() prevents not just writing but also reading by other processes.
16398 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
16399 ** byte out of a specific range of bytes. The lock byte is obtained at
16400 ** random so two separate readers can probably access the file at the
16401 ** same time, unless they are unlucky and choose the same lock byte.
16402 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
16403 ** There can only be one writer. A RESERVED_LOCK is obtained by locking
16404 ** a single byte of the file that is designated as the reserved lock byte.
16405 ** A PENDING_LOCK is obtained by locking a designated byte different from
16406 ** the RESERVED_LOCK byte.
16407 **
16408 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
16409 ** which means we can use reader/writer locks. When reader/writer locks
16410 ** are used, the lock is placed on the same range of bytes that is used
16411 ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
16412 ** will support two or more Win95 readers or two or more WinNT readers.
16413 ** But a single Win95 reader will lock out all WinNT readers and a single
16414 ** WinNT reader will lock out all other Win95 readers.
16415 **
16416 ** The following #defines specify the range of bytes used for locking.
16417 ** SHARED_SIZE is the number of bytes available in the pool from which
16418 ** a random byte is selected for a shared lock. The pool of bytes for
16419 ** shared locks begins at SHARED_FIRST.
16420 **
16421 ** The same locking strategy and
16422 ** byte ranges are used for Unix. This leaves open the possibility of having
16423 ** clients on win95, winNT, and unix all talking to the same shared file
16424 ** and all locking correctly. To do so would require that samba (or whatever
16425 ** tool is being used for file sharing) implements locks correctly between
16426 ** windows and unix. I'm guessing that isn't likely to happen, but by
16427 ** using the same locking range we are at least open to the possibility.
16428 **
16429 ** Locking in windows is manditory. For this reason, we cannot store
16430 ** actual data in the bytes used for locking. The pager never allocates
16431 ** the pages involved in locking therefore. SHARED_SIZE is selected so
16432 ** that all locks will fit on a single page even at the minimum page size.
16433 ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
16434 ** is set high so that we don't have to allocate an unused page except
16435 ** for very large databases. But one should test the page skipping logic
16436 ** by setting PENDING_BYTE low and running the entire regression suite.
16437 **
16438 ** Changing the value of PENDING_BYTE results in a subtly incompatible
16439 ** file format. Depending on how it is changed, you might not notice
16440 ** the incompatibility right away, even running a full regression test.
16441 ** The default location of PENDING_BYTE is the first byte past the
16442 ** 1GB boundary.
16443 **
16444 */
16445 #ifdef SQLITE_OMIT_WSD
16446 # define PENDING_BYTE (0x40000000)
16447 #else
16448 # define PENDING_BYTE sqlite3PendingByte
16449 #endif
16450 #define RESERVED_BYTE (PENDING_BYTE+1)
16451 #define SHARED_FIRST (PENDING_BYTE+2)
16452 #define SHARED_SIZE 510
16453
16454 /*
16455 ** Wrapper around OS specific sqlite3_os_init() function.
16456 */
16457 SQLITE_PRIVATE int sqlite3OsInit(void);
16458
16459 /*
16460 ** Functions for accessing sqlite3_file methods
16461 */
16462 SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
16463 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16464 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16465 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
16466 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
16467 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16468 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
16469 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
16470 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16471 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
16472 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16473 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
16474 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
16475 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16476 #ifndef SQLITE_OMIT_WAL
16477 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16478 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16479 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
16480 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
16481 #endif /* SQLITE_OMIT_WAL */
16482 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16483 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16484
16485
16486 /*
16487 ** Functions for accessing sqlite3_vfs methods
16488 */
16489 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16490 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16491 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16492 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16493 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16494 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16495 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16496 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16497 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
16498 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
16499 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16500 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
16501 SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
16502 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16503
16504 /*
16505 ** Convenience functions for opening and closing files using
16506 ** sqlite3_malloc() to obtain space for the file-handle structure.
16507 */
16508 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16509 SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
16510
16511 #endif /* _SQLITE_OS_H_ */
16512
16513 /************** End of os.h **************************************************/
16514 /************** Continuing where we left off in sqliteInt.h ******************/
16515 /************** Include mutex.h in the middle of sqliteInt.h *****************/
16516 /************** Begin file mutex.h *******************************************/
16517 /*
16518 ** 2007 August 28
@@ -17100,10 +17158,11 @@
17100 #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
17101 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17102 #define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */
17103 #define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */
17104 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
 
17105 #define SQLITE_AllOpts 0xffffffff /* All optimizations */
17106
17107 /*
17108 ** Macros for testing whether or not optimizations are enabled or disabled.
17109 */
@@ -17672,11 +17731,11 @@
17672 ** table support is omitted from the build.
17673 */
17674 #ifndef SQLITE_OMIT_VIRTUALTABLE
17675 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17676 # define ExprIsVtab(X) \
17677 ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB)
17678 #else
17679 # define IsVirtual(X) 0
17680 # define ExprIsVtab(X) 0
17681 #endif
17682
@@ -17889,13 +17948,25 @@
17889 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17890 **
17891 ** The Index.onError field determines whether or not the indexed columns
17892 ** must be unique and what to do if they are not. When Index.onError=OE_None,
17893 ** it means this is not a unique index. Otherwise it is a unique index
17894 ** and the value of Index.onError indicate the which conflict resolution
17895 ** algorithm to employ whenever an attempt is made to insert a non-unique
17896 ** element.
 
 
 
 
 
 
 
 
 
 
 
 
17897 **
17898 ** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17899 ** generate VDBE code (as opposed to parsing one read from an sqlite_schema
17900 ** table as part of parsing an existing database schema), transient instances
17901 ** of this structure may be created. In this case the Index.tnum variable is
@@ -17928,19 +17999,21 @@
17928 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17929 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17930 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17931 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
17932 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
 
 
17933 #ifdef SQLITE_ENABLE_STAT4
17934 int nSample; /* Number of elements in aSample[] */
17935 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17936 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17937 IndexSample *aSample; /* Samples of the left-most key */
17938 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17939 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17940 #endif
17941 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17942 };
17943
17944 /*
17945 ** Allowed values for Index.idxType
17946 */
@@ -18389,10 +18462,18 @@
18389
18390 /*
18391 ** The SrcItem object represents a single term in the FROM clause of a query.
18392 ** The SrcList object is mostly an array of SrcItems.
18393 **
 
 
 
 
 
 
 
 
18394 ** Union member validity:
18395 **
18396 ** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
18397 ** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
18398 ** u2.pIBIndex fg.isIndexedBy && !fg.isCte
@@ -18428,18 +18509,18 @@
18428 int iCursor; /* The VDBE cursor number used to access this table */
18429 union {
18430 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
18431 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
18432 } u3;
18433 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
18434 union {
18435 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18436 ExprList *pFuncArg; /* Arguments to table-valued-function */
18437 } u1;
18438 union {
18439 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18440 CteUse *pCteUse; /* CTE Usage info info fg.isCte is true */
18441 } u2;
18442 };
18443
18444 /*
18445 ** The OnOrUsing object represents either an ON clause or a USING clause.
@@ -18449,27 +18530,15 @@
18449 Expr *pOn; /* The ON clause of a join */
18450 IdList *pUsing; /* The USING clause of a join */
18451 };
18452
18453 /*
18454 ** The following structure describes the FROM clause of a SELECT statement.
18455 ** Each table or subquery in the FROM clause is a separate element of
18456 ** the SrcList.a[] array.
18457 **
18458 ** With the addition of multiple database support, the following structure
18459 ** can also be used to describe a particular table such as the table that
18460 ** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
18461 ** such a table must be a simple name: ID. But in SQLite, the table can
18462 ** now be identified by a database name, a dot, then the table name: ID.ID.
18463 **
18464 ** The jointype starts out showing the join type between the current table
18465 ** and the next table on the list. The parser builds the list this way.
18466 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18467 ** jointype expresses the join between the table and the previous table.
18468 **
18469 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18470 ** contains more than 63 columns and the 64-th or later column is used.
18471 */
18472 struct SrcList {
18473 int nSrc; /* Number of tables or subqueries in the FROM clause */
18474 u32 nAlloc; /* Number of entries allocated in a[] below */
18475 SrcItem a[1]; /* One entry for each identifier on the list */
@@ -18875,10 +18944,32 @@
18875 # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18876 # define DbMaskAllZero(M) (M)==0
18877 # define DbMaskNonZero(M) (M)!=0
18878 #endif
18879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18880 /*
18881 ** An instance of the ParseCleanup object specifies an operation that
18882 ** should be performed after parsing to deallocation resources obtained
18883 ** during the parse and which are no longer needed.
18884 */
@@ -18916,11 +19007,11 @@
18916 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
18917 u8 mayAbort; /* True if statement may throw an ABORT exception */
18918 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18919 u8 okConstFactor; /* OK to factor out constants */
18920 u8 disableLookaside; /* Number of times lookaside has been disabled */
18921 u8 disableVtab; /* Disable all virtual tables for this parse */
18922 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
18923 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
18924 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
18925 #endif
18926 int nRangeReg; /* Size of the temporary register block */
@@ -18933,10 +19024,11 @@
18933 ** of the base register during check-constraint eval */
18934 int nLabel; /* The *negative* of the number of labels used */
18935 int nLabelAlloc; /* Number of slots in aLabel */
18936 int *aLabel; /* Space to hold the labels */
18937 ExprList *pConstExpr;/* Constant expressions */
 
18938 Token constraintName;/* Name of the constraint currently being parsed */
18939 yDbMask writeMask; /* Start a write transaction on these databases */
18940 yDbMask cookieMask; /* Bitmask of schema verified databases */
18941 int regRowid; /* Register holding rowid of CREATE TABLE entry */
18942 int regRoot; /* Register holding root page number for new objects */
@@ -19368,19 +19460,19 @@
19368 SrcList *pSrcList; /* FROM clause */
19369 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
19370 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
19371 int *aiCol; /* array of column indexes */
19372 struct IdxCover *pIdxCover; /* Check for index coverage */
19373 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
19374 ExprList *pGroupBy; /* GROUP BY clause */
19375 Select *pSelect; /* HAVING to WHERE clause ctx */
19376 struct WindowRewrite *pRewrite; /* Window rewrite context */
19377 struct WhereConst *pConst; /* WHERE clause constants */
19378 struct RenameCtx *pRename; /* RENAME COLUMN context */
19379 struct Table *pTab; /* Table of generated column */
 
19380 SrcItem *pSrcItem; /* A single FROM clause item */
19381 DbFixer *pFix;
19382 } u;
19383 };
19384
19385 /*
19386 ** The following structure contains information used by the sqliteFix...
@@ -19703,16 +19795,20 @@
19703 ** The alloca() routine never returns NULL. This will cause code paths
19704 ** that deal with sqlite3StackAlloc() failures to be unreachable.
19705 */
19706 #ifdef SQLITE_USE_ALLOCA
19707 # define sqlite3StackAllocRaw(D,N) alloca(N)
 
19708 # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
19709 # define sqlite3StackFree(D,P)
 
19710 #else
19711 # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
 
19712 # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
19713 # define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
 
19714 #endif
19715
19716 /* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
19717 ** are, disable MEMSYS3
19718 */
@@ -20767,10 +20863,14 @@
20767 SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20768
20769 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20770 SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20771 #endif
 
 
 
 
20772
20773 #endif /* SQLITEINT_H */
20774
20775 /************** End of sqliteInt.h *******************************************/
20776 /************** Begin file os_common.h ***************************************/
@@ -20999,11 +21099,11 @@
20999 /*
21000 ** Include the configuration header output by 'configure' if we're using the
21001 ** autoconf-based build
21002 */
21003 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21004 /* #include "config.h" */
21005 #define SQLITECONFIG_H 1
21006 #endif
21007
21008 /* These macros are provided to "stringify" the value of the define
21009 ** for those options in which the value is meaningful. */
@@ -21164,10 +21264,13 @@
21164 "DISABLE_PAGECACHE_OVERFLOW_STATS",
21165 #endif
21166 #ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
21167 "DISABLE_SKIPAHEAD_DISTINCT",
21168 #endif
 
 
 
21169 #ifdef SQLITE_ENABLE_8_3_NAMES
21170 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
21171 #endif
21172 #ifdef SQLITE_ENABLE_API_ARMOR
21173 "ENABLE_API_ARMOR",
@@ -24740,13 +24843,15 @@
24740 DO_OS_MALLOC_TEST(id);
24741 return id->pMethods->xFileSize(id, pSize);
24742 }
24743 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
24744 DO_OS_MALLOC_TEST(id);
 
24745 return id->pMethods->xLock(id, lockType);
24746 }
24747 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
 
24748 return id->pMethods->xUnlock(id, lockType);
24749 }
24750 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
24751 DO_OS_MALLOC_TEST(id);
24752 return id->pMethods->xCheckReservedLock(id, pResOut);
@@ -35382,11 +35487,11 @@
35382 /* 13 */ "MustBeInt" OpHelp(""),
35383 /* 14 */ "Jump" OpHelp(""),
35384 /* 15 */ "Once" OpHelp(""),
35385 /* 16 */ "If" OpHelp(""),
35386 /* 17 */ "IfNot" OpHelp(""),
35387 /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35388 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35389 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35390 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35391 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35392 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
@@ -35557,10 +35662,982 @@
35557 return azName[i];
35558 }
35559 #endif
35560
35561 /************** End of opcodes.c *********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35562 /************** Begin file os_unix.c *****************************************/
35563 /*
35564 ** 2004 May 22
35565 **
35566 ** The author disclaims copyright to this source code. In place of
@@ -35647,17 +36724,17 @@
35647 #endif
35648
35649 /*
35650 ** standard include files.
35651 */
35652 #include <sys/types.h>
35653 #include <sys/stat.h>
35654 #include <fcntl.h>
35655 #include <sys/ioctl.h>
35656 #include <unistd.h>
35657 /* #include <time.h> */
35658 #include <sys/time.h>
35659 #include <errno.h>
35660 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
35661 # include <sys/mman.h>
35662 #endif
35663
@@ -43625,10 +44702,13 @@
43625 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
43626 #else
43627 sqlite3_vfs_register(&aVfs[i], i==0);
43628 #endif
43629 }
 
 
 
43630 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
43631
43632 #ifndef SQLITE_OMIT_WAL
43633 /* Validate lock assumptions */
43634 assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
@@ -70722,10 +71802,13 @@
70722 put4byte(pCell+info.nSize-4, iTo);
70723 break;
70724 }
70725 }
70726 }else{
 
 
 
70727 if( get4byte(pCell)==iFrom ){
70728 put4byte(pCell, iTo);
70729 break;
70730 }
70731 }
@@ -73056,18 +74139,11 @@
73056 }
73057 }
73058
73059 pPage = pCur->pPage;
73060 idx = ++pCur->ix;
73061 if( !pPage->isInit || sqlite3FaultSim(412) ){
73062 /* The only known way for this to happen is for there to be a
73063 ** recursive SQL function that does a DELETE operation as part of a
73064 ** SELECT which deletes content out from under an active cursor
73065 ** in a corrupt database file where the table being DELETE-ed from
73066 ** has pages in common with the table being queried. See TH3
73067 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
73068 ** example. */
73069 return SQLITE_CORRUPT_BKPT;
73070 }
73071
73072 if( idx>=pPage->nCell ){
73073 if( !pPage->leaf ){
@@ -75753,10 +76829,15 @@
75753 assert( pCur->pPage->nOverflow );
75754 }
75755 }else{
75756 break;
75757 }
 
 
 
 
 
75758 }else{
75759 MemPage * const pParent = pCur->apPage[iPage-1];
75760 int const iIdx = pCur->aiIdx[iPage-1];
75761
75762 rc = sqlite3PagerWrite(pParent->pDbPage);
@@ -79679,10 +80760,11 @@
79679 assert( MEM_Str==(MEM_Blob>>3) );
79680 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
79681 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
79682 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
79683 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
 
79684 return sqlite3VdbeChangeEncoding(pMem, encoding);
79685 }
79686 }
79687 return SQLITE_OK;
79688 }
@@ -80813,10 +81895,13 @@
80813 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
80814 Mem *p = (Mem*)pVal;
80815 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
80816 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
80817 return p->n;
 
 
 
80818 }
80819 if( (p->flags & MEM_Blob)!=0 ){
80820 if( p->flags & MEM_Zero ){
80821 return p->n + p->u.nZero;
80822 }else{
@@ -81983,10 +83068,22 @@
81983 }
81984 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
81985 assert( p->nOp>0 || p->db->mallocFailed );
81986 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
81987 }
 
 
 
 
 
 
 
 
 
 
 
 
81988
81989 /*
81990 ** Change the P2 operand of instruction addr so that it points to
81991 ** the address of the next instruction to be coded.
81992 */
@@ -85402,11 +86499,11 @@
85402 testcase( pRhs->flags & MEM_Int );
85403 testcase( pRhs->flags & MEM_IntReal );
85404 serial_type = aKey1[idx1];
85405 testcase( serial_type==12 );
85406 if( serial_type>=10 ){
85407 rc = +1;
85408 }else if( serial_type==0 ){
85409 rc = -1;
85410 }else if( serial_type==7 ){
85411 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
85412 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
@@ -85427,11 +86524,11 @@
85427 if( serial_type>=10 ){
85428 /* Serial types 12 or greater are strings and blobs (greater than
85429 ** numbers). Types 10 and 11 are currently "reserved for future
85430 ** use", so it doesn't really matter what the results of comparing
85431 ** them to numberic values are. */
85432 rc = +1;
85433 }else if( serial_type==0 ){
85434 rc = -1;
85435 }else{
85436 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
85437 if( serial_type==7 ){
@@ -85508,11 +86605,11 @@
85508 }
85509
85510 /* RHS is null */
85511 else{
85512 serial_type = aKey1[idx1];
85513 rc = (serial_type!=0);
85514 }
85515
85516 if( rc!=0 ){
85517 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
85518 if( sortFlags ){
@@ -86457,10 +87554,13 @@
86457 }
86458 assert( eType == aType[pVal->flags&MEM_AffMask] );
86459 }
86460 #endif
86461 return aType[pVal->flags&MEM_AffMask];
 
 
 
86462 }
86463
86464 /* Return true if a parameter to xUpdate represents an unchanged column */
86465 SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
86466 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
@@ -91094,23 +92194,94 @@
91094 goto jump_to_p2;
91095 }
91096 break;
91097 }
91098
91099 /* Opcode: IsNullOrType P1 P2 P3 * *
91100 ** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
91101 **
91102 ** Jump to P2 if the value in register P1 is NULL or has a datatype P3.
91103 ** P3 is an integer which should be one of SQLITE_INTEGER, SQLITE_FLOAT,
91104 ** SQLITE_BLOB, SQLITE_NULL, or SQLITE_TEXT.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91105 */
91106 case OP_IsNullOrType: { /* jump, in1 */
91107 int doTheJump;
91108 pIn1 = &aMem[pOp->p1];
91109 doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
91110 VdbeBranchTaken( doTheJump, 2);
91111 if( doTheJump ) goto jump_to_p2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91112 break;
91113 }
91114
91115 /* Opcode: ZeroOrNull P1 P2 P3 * *
91116 ** Synopsis: r[P2] = 0 OR NULL
@@ -91207,23 +92378,25 @@
91207 ** Synopsis: r[P3]=PX cursor P1 column P2
91208 **
91209 ** Interpret the data that cursor P1 points to as a structure built using
91210 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
91211 ** information about the format of the data.) Extract the P2-th column
91212 ** from this record. If there are less that (P2+1)
91213 ** values in the record, extract a NULL.
91214 **
91215 ** The value extracted is stored in register P3.
91216 **
91217 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
91218 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
91219 ** the result.
91220 **
91221 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
91222 ** the result is guaranteed to only be used as the argument of a length()
91223 ** or typeof() function, respectively. The loading of large blobs can be
91224 ** skipped for length() and all content loading can be skipped for typeof().
 
 
91225 */
91226 case OP_Column: {
91227 u32 p2; /* column number to retrieve */
91228 VdbeCursor *pC; /* The VDBE cursor */
91229 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
@@ -93158,11 +94331,17 @@
93158 assert( oc!=OP_SeekGE || r.default_rc==+1 );
93159 assert( oc!=OP_SeekLT || r.default_rc==+1 );
93160
93161 r.aMem = &aMem[pOp->p3];
93162 #ifdef SQLITE_DEBUG
93163 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
 
 
 
 
 
 
93164 #endif
93165 r.eqSeen = 0;
93166 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
93167 if( rc!=SQLITE_OK ){
93168 goto abort_due_to_error;
@@ -93221,54 +94400,76 @@
93221 }
93222 break;
93223 }
93224
93225
93226 /* Opcode: SeekScan P1 P2 * * *
93227 ** Synopsis: Scan-ahead up to P1 rows
93228 **
93229 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
93230 ** opcode must be immediately followed by OP_SeekGE. This constraint is
93231 ** checked by assert() statements.
93232 **
93233 ** This opcode uses the P1 through P4 operands of the subsequent
93234 ** OP_SeekGE. In the text that follows, the operands of the subsequent
93235 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
93236 ** the P1 and P2 operands of this opcode are also used, and are called
93237 ** This.P1 and This.P2.
93238 **
93239 ** This opcode helps to optimize IN operators on a multi-column index
93240 ** where the IN operator is on the later terms of the index by avoiding
93241 ** unnecessary seeks on the btree, substituting steps to the next row
93242 ** of the b-tree instead. A correct answer is obtained if this opcode
93243 ** is omitted or is a no-op.
93244 **
93245 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
93246 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
93247 ** to. Call this SeekGE.P4/P5 row the "target".
93248 **
93249 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
93250 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
93251 **
93252 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
93253 ** might be the target row, or it might be near and slightly before the
93254 ** target row. This opcode attempts to position the cursor on the target
93255 ** row by, perhaps by invoking sqlite3BtreeStep() on the cursor
93256 ** between 0 and This.P1 times.
93257 **
93258 ** There are three possible outcomes from this opcode:<ol>
93259 **
93260 ** <li> If after This.P1 steps, the cursor is still pointing to a place that
93261 ** is earlier in the btree than the target row, then fall through
93262 ** into the subsquence OP_SeekGE opcode.
93263 **
93264 ** <li> If the cursor is successfully moved to the target row by 0 or more
93265 ** sqlite3BtreeNext() calls, then jump to This.P2, which will land just
93266 ** past the OP_IdxGT or OP_IdxGE opcode that follows the OP_SeekGE.
93267 **
93268 ** <li> If the cursor ends up past the target row (indicating that the target
93269 ** row does not exist in the btree) then jump to SeekOP.P2.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93270 ** </ol>
93271 */
93272 case OP_SeekScan: {
93273 VdbeCursor *pC;
93274 int res;
@@ -93275,18 +94476,29 @@
93275 int nStep;
93276 UnpackedRecord r;
93277
93278 assert( pOp[1].opcode==OP_SeekGE );
93279
93280 /* pOp->p2 points to the first instruction past the OP_IdxGT that
93281 ** follows the OP_SeekGE. */
 
93282 assert( pOp->p2>=(int)(pOp-aOp)+2 );
93283 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
93284 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
93285 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
93286 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
93287 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
 
 
 
 
 
 
 
 
 
 
93288
93289 assert( pOp->p1>0 );
93290 pC = p->apCsr[pOp[1].p1];
93291 assert( pC!=0 );
93292 assert( pC->eCurType==CURTYPE_BTREE );
@@ -93316,22 +94528,24 @@
93316 #endif
93317 res = 0; /* Not needed. Only used to silence a warning. */
93318 while(1){
93319 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
93320 if( rc ) goto abort_due_to_error;
93321 if( res>0 ){
93322 seekscan_search_fail:
 
93323 #ifdef SQLITE_DEBUG
93324 if( db->flags&SQLITE_VdbeTrace ){
93325 printf("... %d steps and then skip\n", pOp->p1 - nStep);
93326 }
93327 #endif
93328 VdbeBranchTaken(1,3);
93329 pOp++;
93330 goto jump_to_p2;
93331 }
93332 if( res==0 ){
 
93333 #ifdef SQLITE_DEBUG
93334 if( db->flags&SQLITE_VdbeTrace ){
93335 printf("... %d steps and then success\n", pOp->p1 - nStep);
93336 }
93337 #endif
@@ -103867,13 +105081,12 @@
103867 }
103868 op = pExpr->op;
103869 if( op==TK_REGISTER ) op = pExpr->op2;
103870 if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
103871 assert( ExprUseYTab(pExpr) );
103872 if( pExpr->y.pTab ){
103873 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103874 }
103875 }
103876 if( op==TK_SELECT ){
103877 assert( ExprUseXSelect(pExpr) );
103878 assert( pExpr->x.pSelect!=0 );
103879 assert( pExpr->x.pSelect->pEList!=0 );
@@ -103987,21 +105200,18 @@
103987 const Expr *p = pExpr;
103988 while( p ){
103989 int op = p->op;
103990 if( op==TK_REGISTER ) op = p->op2;
103991 if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
 
103992 assert( ExprUseYTab(p) );
103993 if( p->y.pTab!=0 ){
103994 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
103995 ** a TK_COLUMN but was previously evaluated and cached in a register */
103996 int j = p->iColumn;
103997 if( j>=0 ){
103998 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
103999 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
104000 }
104001 break;
104002 }
104003 }
104004 if( op==TK_CAST || op==TK_UPLUS ){
104005 p = p->pLeft;
104006 continue;
104007 }
@@ -107602,14 +108812,11 @@
107602 int iCol, /* Index of the column to extract */
107603 int regOut /* Extract the value into this register */
107604 ){
107605 Column *pCol;
107606 assert( v!=0 );
107607 if( pTab==0 ){
107608 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
107609 return;
107610 }
107611 if( iCol<0 || iCol==pTab->iPKey ){
107612 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
107613 VdbeComment((v, "%s.rowid", pTab->zName));
107614 }else{
107615 int op;
@@ -107855,10 +109062,57 @@
107855 #endif /* !defined(SQLITE_UNTESTABLE) */
107856 }
107857 return target;
107858 }
107859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107860
107861 /*
107862 ** Generate code into the current Vdbe to evaluate the given
107863 ** expression. Attempt to store the results in register "target".
107864 ** Return the register where results are stored.
@@ -107883,10 +109137,15 @@
107883 assert( v!=0 );
107884
107885 expr_code_doover:
107886 if( pExpr==0 ){
107887 op = TK_NULL;
 
 
 
 
 
107888 }else{
107889 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
107890 op = pExpr->op;
107891 }
107892 switch( op ){
@@ -107928,15 +109187,12 @@
107928 ** constant.
107929 */
107930 int aff;
107931 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107932 assert( ExprUseYTab(pExpr) );
107933 if( pExpr->y.pTab ){
107934 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107935 }else{
107936 aff = pExpr->affExpr;
107937 }
107938 if( aff>SQLITE_AFF_BLOB ){
107939 static const char zAff[] = "B\000C\000D\000E";
107940 assert( SQLITE_AFF_BLOB=='A' );
107941 assert( SQLITE_AFF_TEXT=='B' );
107942 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
@@ -107994,16 +109250,14 @@
107994 ** in the index refer to the table to which the index belongs */
107995 iTab = pParse->iSelfTab - 1;
107996 }
107997 }
107998 assert( ExprUseYTab(pExpr) );
 
107999 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
108000 pExpr->iColumn, iTab, target,
108001 pExpr->op2);
108002 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
108003 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
108004 }
108005 return iReg;
108006 }
108007 case TK_INTEGER: {
108008 codeInteger(pParse, pExpr, 0, target);
108009 return target;
@@ -109053,10 +110307,11 @@
109053 case TK_ISNULL:
109054 case TK_NOTNULL: {
109055 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
109056 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
109057 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 
109058 sqlite3VdbeAddOp2(v, op, r1, dest);
109059 VdbeCoverageIf(v, op==TK_ISNULL);
109060 VdbeCoverageIf(v, op==TK_NOTNULL);
109061 testcase( regFree1==0 );
109062 break;
@@ -109227,10 +110482,11 @@
109227 break;
109228 }
109229 case TK_ISNULL:
109230 case TK_NOTNULL: {
109231 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
 
109232 sqlite3VdbeAddOp2(v, op, r1, dest);
109233 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
109234 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
109235 testcase( regFree1==0 );
109236 break;
@@ -109380,11 +110636,17 @@
109380 return 1;
109381 }
109382 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109383 return 1;
109384 }
109385 return 2;
 
 
 
 
 
 
109386 }
109387 assert( !ExprHasProperty(pA, EP_IntValue) );
109388 assert( !ExprHasProperty(pB, EP_IntValue) );
109389 if( pA->u.zToken ){
109390 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
@@ -109682,14 +110944,14 @@
109682 /* The y.pTab=0 assignment in wherecode.c always happens after the
109683 ** impliesNotNullRow() test */
109684 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
109685 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
109686 if( (pLeft->op==TK_COLUMN
109687 && pLeft->y.pTab!=0
109688 && IsVirtual(pLeft->y.pTab))
109689 || (pRight->op==TK_COLUMN
109690 && pRight->y.pTab!=0
109691 && IsVirtual(pRight->y.pTab))
109692 ){
109693 return WRC_Prune;
109694 }
109695 /* no break */ deliberate_fall_through
@@ -113510,10 +114772,11 @@
113510 assert( k>=0 && k<pIdx->nColumn );
113511 i = pIdx->aiColumn[k];
113512 if( NEVER(i==XN_ROWID) ){
113513 VdbeComment((v,"%s.rowid",pIdx->zName));
113514 }else if( i==XN_EXPR ){
 
113515 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113516 }else{
113517 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113518 }
113519 }
@@ -115825,11 +117088,11 @@
115825 if( p==0 ){
115826 #ifndef SQLITE_OMIT_VIRTUALTABLE
115827 /* If zName is the not the name of a table in the schema created using
115828 ** CREATE, then check to see if it is the name of an virtual table that
115829 ** can be an eponymous virtual table. */
115830 if( pParse->disableVtab==0 && db->init.busy==0 ){
115831 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115832 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
115833 pMod = sqlite3PragmaVtabRegister(db, zName);
115834 }
115835 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
@@ -115838,11 +117101,11 @@
115838 }
115839 }
115840 #endif
115841 if( flags & LOCATE_NOERR ) return 0;
115842 pParse->checkSchema = 1;
115843 }else if( IsVirtual(p) && pParse->disableVtab ){
115844 p = 0;
115845 }
115846
115847 if( p==0 ){
115848 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
@@ -117648,11 +118911,12 @@
117648 }
117649
117650 /* Recompute the colNotIdxed field of the Index.
117651 **
117652 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
117653 ** columns that are within the first 63 columns of the table. The
 
117654 ** high-order bit of colNotIdxed is always 1. All unindexed columns
117655 ** of the table have a 1.
117656 **
117657 ** 2019-10-24: For the purpose of this computation, virtual columns are
117658 ** not considered to be covered by the index, even if they are in the
@@ -117676,11 +118940,11 @@
117676 testcase( x==BMS-2 );
117677 if( x<BMS-1 ) m |= MASKBIT(x);
117678 }
117679 }
117680 pIdx->colNotIdxed = ~m;
117681 assert( (pIdx->colNotIdxed>>63)==1 );
117682 }
117683
117684 /*
117685 ** This routine runs at the end of parsing a CREATE TABLE statement that
117686 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
@@ -119564,10 +120828,11 @@
119564 pList = 0;
119565 }
119566 j = XN_EXPR;
119567 pIndex->aiColumn[i] = XN_EXPR;
119568 pIndex->uniqNotNull = 0;
 
119569 }else{
119570 j = pCExpr->iColumn;
119571 assert( j<=0x7fff );
119572 if( j<0 ){
119573 j = pTab->iPKey;
@@ -119575,10 +120840,11 @@
119575 if( pTab->aCol[j].notNull==0 ){
119576 pIndex->uniqNotNull = 0;
119577 }
119578 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
119579 pIndex->bHasVCol = 1;
 
119580 }
119581 }
119582 pIndex->aiColumn[i] = (i16)j;
119583 }
119584 zColl = 0;
@@ -123352,11 +124618,11 @@
123352 **
123353 ** For a case-insensitive search, set variable cx to be the same as
123354 ** c but in the other case and search the input string for either
123355 ** c or cx.
123356 */
123357 if( c<=0x80 ){
123358 char zStop[3];
123359 int bMatch;
123360 if( noCase ){
123361 zStop[0] = sqlite3Toupper(c);
123362 zStop[1] = sqlite3Tolower(c);
@@ -123435,19 +124701,31 @@
123435 /*
123436 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
123437 ** non-zero if there is no match.
123438 */
123439 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
123440 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
 
 
 
 
 
 
123441 }
123442
123443 /*
123444 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123445 ** a miss - like strcmp().
123446 */
123447 SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
123448 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
 
 
 
 
 
 
123449 }
123450
123451 /*
123452 ** Count the number of times that the LIKE operator (or GLOB which is
123453 ** just a variation of LIKE) gets called. This is used for testing
@@ -126572,10 +127850,11 @@
126572 aff = pTab->aCol[x].affinity;
126573 }else if( x==XN_ROWID ){
126574 aff = SQLITE_AFF_INTEGER;
126575 }else{
126576 assert( x==XN_EXPR );
 
126577 assert( pIdx->aColExpr!=0 );
126578 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126579 }
126580 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
126581 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
@@ -130146,10 +131425,12 @@
130146 int (*deserialize)(sqlite3*,const char*,unsigned char*,
130147 sqlite3_int64,sqlite3_int64,unsigned);
130148 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
130149 unsigned int);
130150 const char *(*db_name)(sqlite3*,int);
 
 
130151 };
130152
130153 /*
130154 ** This is the function signature used for all extension entry points. It
130155 ** is also defined in the file "loadext.c".
@@ -130470,10 +131751,12 @@
130470 #ifndef SQLITE_OMIT_DESERIALIZE
130471 #define sqlite3_deserialize sqlite3_api->deserialize
130472 #define sqlite3_serialize sqlite3_api->serialize
130473 #endif
130474 #define sqlite3_db_name sqlite3_api->db_name
 
 
130475 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130476
130477 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130478 /* This case when the file really is being compiled as a loadable
130479 ** extension */
@@ -130982,11 +132265,13 @@
130982 sqlite3_serialize,
130983 #else
130984 0,
130985 0,
130986 #endif
130987 sqlite3_db_name
 
 
130988 };
130989
130990 /* True if x is the directory separator character
130991 */
130992 #if SQLITE_OS_WIN
@@ -133786,12 +135071,13 @@
133786 Index *pIdx, *pPk;
133787 Index *pPrior = 0; /* Previous index */
133788 int loopTop;
133789 int iDataCur, iIdxCur;
133790 int r1 = -1;
133791 int bStrict;
133792 int r2; /* Previous key for WITHOUT ROWID tables */
 
133793
133794 if( !IsOrdinaryTable(pTab) ) continue;
133795 if( pObjTab && pObjTab!=pTab ) continue;
133796 if( isQuick || HasRowid(pTab) ){
133797 pPk = 0;
@@ -133812,15 +135098,26 @@
133812 }
133813 assert( pParse->nMem>=8+j );
133814 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
133815 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
133816 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133817 if( !isQuick ){
133818 /* Sanity check on record header decoding */
133819 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133820 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133821 VdbeComment((v, "(right-most column)"));
133822 if( pPk ){
133823 /* Verify WITHOUT ROWID keys are in ascending order */
133824 int a1;
133825 char *zErr;
133826 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
@@ -133836,48 +135133,126 @@
133836 for(j=0; j<pPk->nKeyCol; j++){
133837 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
133838 }
133839 }
133840 }
133841 /* Verify that all NOT NULL columns really are NOT NULL. At the
133842 ** same time verify the type of the content of STRICT tables */
 
 
 
 
 
 
 
133843 bStrict = (pTab->tabFlags & TF_Strict)!=0;
133844 for(j=0; j<pTab->nCol; j++){
133845 char *zErr;
133846 Column *pCol = pTab->aCol + j;
133847 int doError, jmp2;
 
 
 
 
133848 if( j==pTab->iPKey ) continue;
133849 if( pCol->notNull==0 && !bStrict ) continue;
133850 doError = bStrict ? sqlite3VdbeMakeLabel(pParse) : 0;
133851 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
133852 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Column ){
133853 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
133854 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133855 if( pCol->notNull ){
133856 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
 
 
 
133857 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133858 pCol->zCnName);
133859 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133860 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133861 sqlite3VdbeGoto(v, doError);
 
133862 }else{
133863 integrityCheckResultRow(v);
133864 }
133865 sqlite3VdbeJumpHere(v, jmp2);
133866 }
133867 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133868 jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133869 sqlite3StdTypeMap[pCol->eCType-1]);
 
 
 
 
 
 
 
 
 
 
133870 VdbeCoverage(v);
133871 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133872 sqlite3StdType[pCol->eCType-1],
133873 pTab->zName, pTab->aCol[j].zCnName);
133874 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133875 sqlite3VdbeResolveLabel(v, doError);
133876 integrityCheckResultRow(v);
133877 sqlite3VdbeJumpHere(v, jmp2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133878 }
 
 
 
133879 }
133880 /* Verify CHECK constraints */
133881 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133882 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133883 if( db->mallocFailed==0 ){
@@ -135493,11 +136868,11 @@
135493 */
135494 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
135495 sParse.disableLookaside++;
135496 DisableLookaside;
135497 }
135498 sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
135499
135500 /* Check to verify that it is possible to get a read lock on all
135501 ** database schemas. The inability to get a read lock indicates that
135502 ** some other database connection is holding a write-lock, which in
135503 ** turn means that the other connection has made uncommitted changes
@@ -135534,11 +136909,13 @@
135534 }
135535 }
135536 }
135537 }
135538
135539 sqlite3VtabUnlockList(db);
 
 
135540
135541 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135542 char *zSqlCopy;
135543 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
135544 testcase( nBytes==mxLen );
@@ -139603,10 +140980,11 @@
139603 Parse *pParse; /* The parsing context */
139604 int iTable; /* Replace references to this table */
139605 int iNewTable; /* New table number */
139606 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
139607 ExprList *pEList; /* Replacement expressions */
 
139608 } SubstContext;
139609
139610 /* Forward Declarations */
139611 static void substExprList(SubstContext*, ExprList*);
139612 static void substSelect(SubstContext*, Select*, int);
@@ -139644,13 +141022,14 @@
139644 pExpr->op = TK_NULL;
139645 }else
139646 #endif
139647 {
139648 Expr *pNew;
139649 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
 
139650 Expr ifNullRow;
139651 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
139652 assert( pExpr->pRight==0 );
139653 if( sqlite3ExprIsVector(pCopy) ){
139654 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139655 }else{
139656 sqlite3 *db = pSubst->pParse->db;
@@ -139684,15 +141063,20 @@
139684 ExprSetProperty(pExpr, EP_IntValue);
139685 }
139686
139687 /* Ensure that the expression now has an implicit collation sequence,
139688 ** just as it did when it was a column of a view or sub-query. */
139689 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
139690 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
139691 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
139692 (pColl ? pColl->zName : "BINARY")
139693 );
 
 
 
 
 
139694 }
139695 ExprClearProperty(pExpr, EP_Collate);
139696 }
139697 }
139698 }else{
@@ -139880,10 +141264,22 @@
139880 w.xExprCallback = renumberCursorsCb;
139881 w.xSelectCallback = sqlite3SelectWalkNoop;
139882 sqlite3WalkSelect(&w, p);
139883 }
139884 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
 
 
 
 
 
 
 
 
 
 
 
 
139885
139886 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139887 /*
139888 ** This routine attempts to flatten subqueries as a performance optimization.
139889 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -140433,10 +141829,11 @@
140433 x.pParse = pParse;
140434 x.iTable = iParent;
140435 x.iNewTable = iNewParent;
140436 x.isOuterJoin = isOuterJoin;
140437 x.pEList = pSub->pEList;
 
140438 substSelect(&x, pParent, 0);
140439 }
140440
140441 /* The flattened query is a compound if either the inner or the
140442 ** outer query is a compound. */
@@ -140452,11 +141849,11 @@
140452 if( pSub->pLimit ){
140453 pParent->pLimit = pSub->pLimit;
140454 pSub->pLimit = 0;
140455 }
140456
140457 /* Recompute the SrcList_item.colUsed masks for the flattened
140458 ** tables. */
140459 for(i=0; i<nSubSrc; i++){
140460 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140461 }
140462 }
@@ -140915,10 +142312,11 @@
140915 x.pParse = pParse;
140916 x.iTable = pSrc->iCursor;
140917 x.iNewTable = pSrc->iCursor;
140918 x.isOuterJoin = 0;
140919 x.pEList = pSubq->pEList;
 
140920 pNew = substExpr(&x, pNew);
140921 #ifndef SQLITE_OMIT_WINDOWFUNC
140922 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140923 /* Restriction 6c has prevented push-down in this case */
140924 sqlite3ExprDelete(pParse->db, pNew);
@@ -141439,13 +142837,13 @@
141439 }
141440 }
141441 #endif
141442
141443 /*
141444 ** The SrcList_item structure passed as the second argument represents a
141445 ** sub-query in the FROM clause of a SELECT statement. This function
141446 ** allocates and populates the SrcList_item.pTab object. If successful,
141447 ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
141448 ** SQLITE_NOMEM.
141449 */
141450 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
141451 Select *pSel = pFrom->pSelect;
@@ -142274,11 +143672,11 @@
142274 #endif
142275 }
142276
142277 /*
142278 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
142279 ** If it is, then return the SrcList_item for the prior view. If it is not,
142280 ** then return 0.
142281 */
142282 static SrcItem *isSelfJoinView(
142283 SrcList *pTabList, /* Search for self-joins in this FROM clause */
142284 SrcItem *pThis /* Search for prior reference to this subquery */
@@ -143318,11 +144716,11 @@
143318 ** in the right order to begin with.
143319 */
143320 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
143321 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143322 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
143323 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
143324 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
143325 );
143326 if( pWInfo==0 ){
143327 sqlite3ExprListDelete(db, pDistinct);
143328 goto select_end;
@@ -143617,11 +145015,11 @@
143617 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
143618 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
143619
143620 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143621 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
143622 pDistinct, 0, minMaxFlag|distFlag, 0);
143623 if( pWInfo==0 ){
143624 goto select_end;
143625 }
143626 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
143627 eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -145101,11 +146499,11 @@
145101 sSubParse.pTriggerTab = pTab;
145102 sSubParse.pToplevel = pTop;
145103 sSubParse.zAuthContext = pTrigger->zName;
145104 sSubParse.eTriggerOp = pTrigger->op;
145105 sSubParse.nQueryLoop = pParse->nQueryLoop;
145106 sSubParse.disableVtab = pParse->disableVtab;
145107
145108 v = sqlite3GetVdbe(&sSubParse);
145109 if( v ){
145110 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
145111 pTrigger->zName, onErrorText(orconf),
@@ -145447,15 +146845,18 @@
145447 ** (not a virtual table) then the value might have been stored as an
145448 ** integer. In that case, add an OP_RealAffinity opcode to make sure
145449 ** it has been converted into REAL.
145450 */
145451 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
 
145452 assert( pTab!=0 );
145453 if( !IsView(pTab) ){
 
 
145454 sqlite3_value *pValue = 0;
145455 u8 enc = ENC(sqlite3VdbeDb(v));
145456 Column *pCol = &pTab->aCol[i];
145457 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145458 assert( i<pTab->nCol );
145459 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
145460 sqlite3ColumnExpr(pTab,pCol), enc,
145461 pCol->affinity, &pValue);
@@ -145462,11 +146863,11 @@
145462 if( pValue ){
145463 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
145464 }
145465 }
145466 #ifndef SQLITE_OMIT_FLOATING_POINT
145467 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
145468 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
145469 }
145470 #endif
145471 }
145472
@@ -146902,10 +148303,11 @@
146902 Expr *pExpr;
146903 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146904 if( pIdx->aiColumn[ii]==XN_EXPR ){
146905 assert( pIdx->aColExpr!=0 );
146906 assert( pIdx->aColExpr->nExpr>ii );
 
146907 pExpr = pIdx->aColExpr->a[ii].pExpr;
146908 if( pExpr->op!=TK_COLLATE ){
146909 sCol[0].pLeft = pExpr;
146910 pExpr = &sCol[0];
146911 }
@@ -147215,10 +148617,11 @@
147215 int isMemDb; /* True if vacuuming a :memory: database */
147216 int nRes; /* Bytes of reserved space at the end of each page */
147217 int nDb; /* Number of attached databases */
147218 const char *zDbMain; /* Schema name of database to vacuum */
147219 const char *zOut; /* Name of output file */
 
147220
147221 if( !db->autoCommit ){
147222 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
147223 return SQLITE_ERROR; /* IMP: R-12218-18073 */
147224 }
@@ -147286,16 +148689,21 @@
147286 rc = SQLITE_ERROR;
147287 sqlite3SetString(pzErrMsg, db, "output file already exists");
147288 goto end_of_vacuum;
147289 }
147290 db->mDbFlags |= DBFLAG_VacuumInto;
 
 
 
 
 
147291 }
147292 nRes = sqlite3BtreeGetRequestedReserve(pMain);
147293
147294 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
147295 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
147296 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
147297
147298 /* Begin a transaction and take an exclusive lock on the main database
147299 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
147300 ** to ensure that we do not try to change the page-size on a WAL database.
147301 */
@@ -148605,11 +150013,11 @@
148605 /* Check to see the left operand is a column in a virtual table */
148606 if( NEVER(pExpr==0) ) return pDef;
148607 if( pExpr->op!=TK_COLUMN ) return pDef;
148608 assert( ExprUseYTab(pExpr) );
148609 pTab = pExpr->y.pTab;
148610 if( pTab==0 ) return pDef;
148611 if( !IsVirtual(pTab) ) return pDef;
148612 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148613 assert( pVtab!=0 );
148614 assert( pVtab->pModule!=0 );
148615 pMod = (sqlite3_module *)pVtab->pModule;
@@ -149212,11 +150620,11 @@
149212 /*
149213 ** An instance of the following structure keeps track of a mapping
149214 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
149215 **
149216 ** The VDBE cursor numbers are small integers contained in
149217 ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
149218 ** clause, the cursor numbers might not begin with 0 and they might
149219 ** contain gaps in the numbering sequence. But we want to make maximum
149220 ** use of the bits in our bitmasks. This structure provides a mapping
149221 ** from the sparse cursor numbers into consecutive integers beginning
149222 ** with 0.
@@ -149283,24 +150691,10 @@
149283 #endif
149284 #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
149285 # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
149286 #endif
149287
149288 /*
149289 ** Each instance of this object records a change to a single node
149290 ** in an expression tree to cause that node to point to a column
149291 ** of an index rather than an expression or a virtual column. All
149292 ** such transformations need to be undone at the end of WHERE clause
149293 ** processing.
149294 */
149295 typedef struct WhereExprMod WhereExprMod;
149296 struct WhereExprMod {
149297 WhereExprMod *pNext; /* Next translation on a list of them all */
149298 Expr *pExpr; /* The Expr node that was transformed */
149299 Expr orig; /* Original value of the Expr node */
149300 };
149301
149302 /*
149303 ** The WHERE clause processing routine has two halves. The
149304 ** first part does the start of the WHERE loop and the second
149305 ** half does the tail of the WHERE loop. An instance of
149306 ** this structure is returned by the first half and passed
@@ -149312,14 +150706,14 @@
149312 struct WhereInfo {
149313 Parse *pParse; /* Parsing and code generating context */
149314 SrcList *pTabList; /* List of tables in the join */
149315 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
149316 ExprList *pResultSet; /* Result set of the query */
 
149317 Expr *pWhere; /* The complete WHERE clause */
149318 #ifndef SQLITE_OMIT_VIRTUALTABLE
149319 Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
149320 #endif
 
149321 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
149322 int iContinue; /* Jump here to continue with next record */
149323 int iBreak; /* Jump here to break out of the loop */
149324 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149325 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -149334,11 +150728,10 @@
149334 unsigned sorted :1; /* True if really sorted (not just grouped) */
149335 LogEst nRowOut; /* Estimated number of output rows */
149336 int iTop; /* The very beginning of the WHERE loop */
149337 int iEndWhere; /* End of the WHERE clause itself */
149338 WhereLoop *pLoops; /* List of all WhereLoop objects */
149339 WhereExprMod *pExprMods; /* Expression modifications */
149340 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
149341 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
149342 WhereClause sWC; /* Decomposition of the WHERE clause */
149343 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
149344 WhereLevel a[1]; /* Information about each nest loop in WHERE */
@@ -150686,147 +152079,10 @@
150686 assert( nReg==1 || pParse->nErr );
150687 sqlite3ExprCode(pParse, p, iReg);
150688 }
150689 }
150690
150691 /* An instance of the IdxExprTrans object carries information about a
150692 ** mapping from an expression on table columns into a column in an index
150693 ** down through the Walker.
150694 */
150695 typedef struct IdxExprTrans {
150696 Expr *pIdxExpr; /* The index expression */
150697 int iTabCur; /* The cursor of the corresponding table */
150698 int iIdxCur; /* The cursor for the index */
150699 int iIdxCol; /* The column for the index */
150700 int iTabCol; /* The column for the table */
150701 WhereInfo *pWInfo; /* Complete WHERE clause information */
150702 sqlite3 *db; /* Database connection (for malloc()) */
150703 } IdxExprTrans;
150704
150705 /*
150706 ** Preserve pExpr on the WhereETrans list of the WhereInfo.
150707 */
150708 static void preserveExpr(IdxExprTrans *pTrans, Expr *pExpr){
150709 WhereExprMod *pNew;
150710 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
150711 if( pNew==0 ) return;
150712 pNew->pNext = pTrans->pWInfo->pExprMods;
150713 pTrans->pWInfo->pExprMods = pNew;
150714 pNew->pExpr = pExpr;
150715 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
150716 }
150717
150718 /* The walker node callback used to transform matching expressions into
150719 ** a reference to an index column for an index on an expression.
150720 **
150721 ** If pExpr matches, then transform it into a reference to the index column
150722 ** that contains the value of pExpr.
150723 */
150724 static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
150725 IdxExprTrans *pX = p->u.pIdxTrans;
150726 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
150727 pExpr = sqlite3ExprSkipCollate(pExpr);
150728 preserveExpr(pX, pExpr);
150729 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
150730 pExpr->op = TK_COLUMN;
150731 pExpr->iTable = pX->iIdxCur;
150732 pExpr->iColumn = pX->iIdxCol;
150733 testcase( ExprHasProperty(pExpr, EP_Unlikely) );
150734 ExprClearProperty(pExpr, EP_Skip|EP_Unlikely|EP_WinFunc|EP_Subrtn);
150735 pExpr->y.pTab = 0;
150736 return WRC_Prune;
150737 }else{
150738 return WRC_Continue;
150739 }
150740 }
150741
150742 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
150743 /* A walker node callback that translates a column reference to a table
150744 ** into a corresponding column reference of an index.
150745 */
150746 static int whereIndexExprTransColumn(Walker *p, Expr *pExpr){
150747 if( pExpr->op==TK_COLUMN ){
150748 IdxExprTrans *pX = p->u.pIdxTrans;
150749 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
150750 assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 );
150751 preserveExpr(pX, pExpr);
150752 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
150753 pExpr->iTable = pX->iIdxCur;
150754 pExpr->iColumn = pX->iIdxCol;
150755 pExpr->y.pTab = 0;
150756 }
150757 }
150758 return WRC_Continue;
150759 }
150760 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150761
150762 /*
150763 ** For an indexes on expression X, locate every instance of expression X
150764 ** in pExpr and change that subexpression into a reference to the appropriate
150765 ** column of the index.
150766 **
150767 ** 2019-10-24: Updated to also translate references to a VIRTUAL column in
150768 ** the table into references to the corresponding (stored) column of the
150769 ** index.
150770 */
150771 static void whereIndexExprTrans(
150772 Index *pIdx, /* The Index */
150773 int iTabCur, /* Cursor of the table that is being indexed */
150774 int iIdxCur, /* Cursor of the index itself */
150775 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
150776 ){
150777 int iIdxCol; /* Column number of the index */
150778 ExprList *aColExpr; /* Expressions that are indexed */
150779 Table *pTab;
150780 Walker w;
150781 IdxExprTrans x;
150782 aColExpr = pIdx->aColExpr;
150783 if( aColExpr==0 && !pIdx->bHasVCol ){
150784 /* The index does not reference any expressions or virtual columns
150785 ** so no translations are needed. */
150786 return;
150787 }
150788 pTab = pIdx->pTable;
150789 memset(&w, 0, sizeof(w));
150790 w.u.pIdxTrans = &x;
150791 x.iTabCur = iTabCur;
150792 x.iIdxCur = iIdxCur;
150793 x.pWInfo = pWInfo;
150794 x.db = pWInfo->pParse->db;
150795 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
150796 i16 iRef = pIdx->aiColumn[iIdxCol];
150797 if( iRef==XN_EXPR ){
150798 assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 );
150799 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
150800 if( sqlite3ExprIsConstant(x.pIdxExpr) ) continue;
150801 w.xExprCallback = whereIndexExprTransNode;
150802 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
150803 }else if( iRef>=0
150804 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
150805 && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0
150806 || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]),
150807 sqlite3StrBINARY)==0)
150808 ){
150809 /* Check to see if there are direct references to generated columns
150810 ** that are contained in the index. Pulling the generated column
150811 ** out of the index is an optimization only - the main table is always
150812 ** available if the index cannot be used. To avoid unnecessary
150813 ** complication, omit this optimization if the collating sequence for
150814 ** the column is non-standard */
150815 x.iTabCol = iRef;
150816 w.xExprCallback = whereIndexExprTransColumn;
150817 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */
150818 }else{
150819 continue;
150820 }
150821 x.iIdxCol = iIdxCol;
150822 sqlite3WalkExpr(&w, pWInfo->pWhere);
150823 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
150824 sqlite3WalkExprList(&w, pWInfo->pResultSet);
150825 }
150826 }
150827
150828 /*
150829 ** The pTruth expression is always true because it is the WHERE clause
150830 ** a partial index that is driving a query loop. Look through all of the
150831 ** WHERE clause terms on the query, and if any of those terms must be
150832 ** true because pTruth is true, then mark those WHERE clause terms as
@@ -150891,10 +152147,12 @@
150891 assert( pTerm!=0 );
150892 assert( pTerm->pExpr!=0 );
150893 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150894 regRowid = sqlite3GetTempReg(pParse);
150895 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
 
 
150896 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150897 addrNxt, regRowid, 1);
150898 VdbeCoverage(pParse->pVdbe);
150899 }else{
150900 u16 nEq = pLoop->u.btree.nEq;
@@ -151042,13 +152300,13 @@
151042 codeExprOrVector(pParse, pRight, iTarget, 1);
151043 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
151044 && pLoop->u.vtab.bOmitOffset
151045 ){
151046 assert( pTerm->eOperator==WO_AUX );
151047 assert( pWInfo->pLimit!=0 );
151048 assert( pWInfo->pLimit->iOffset>0 );
151049 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
151050 VdbeComment((v,"Zero OFFSET counter"));
151051 }
151052 }
151053 }
151054 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
@@ -151152,10 +152410,12 @@
151152 iReleaseReg = ++pParse->nMem;
151153 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
151154 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
151155 addrNxt = pLevel->addrNxt;
151156 if( pLevel->regFilter ){
 
 
151157 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
151158 iRowidReg, 1);
151159 VdbeCoverage(v);
151160 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
151161 }
@@ -151503,10 +152763,15 @@
151503 ** of entries in the tree, so basing the number of steps to try
151504 ** on the estimated number of rows in the btree seems like a good
151505 ** guess. */
151506 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
151507 (pIdx->aiRowLogEst[0]+9)/10);
 
 
 
 
 
151508 VdbeCoverage(v);
151509 }
151510 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
151511 VdbeCoverage(v);
151512 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
@@ -151641,31 +152906,10 @@
151641 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
151642 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151643 }
151644
151645 if( pLevel->iLeftJoin==0 ){
151646 /* If pIdx is an index on one or more expressions, then look through
151647 ** all the expressions in pWInfo and try to transform matching expressions
151648 ** into reference to index columns. Also attempt to translate references
151649 ** to virtual columns in the table into references to (stored) columns
151650 ** of the index.
151651 **
151652 ** Do not do this for the RHS of a LEFT JOIN. This is because the
151653 ** expression may be evaluated after OP_NullRow has been executed on
151654 ** the cursor. In this case it is important to do the full evaluation,
151655 ** as the result of the expression may not be NULL, even if all table
151656 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
151657 **
151658 ** Also, do not do this when processing one index an a multi-index
151659 ** OR clause, since the transformation will become invalid once we
151660 ** move forward to the next index.
151661 ** https://sqlite.org/src/info/4e8e4857d32d401f
151662 */
151663 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ){
151664 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
151665 }
151666
151667 /* If a partial index is driving the loop, try to eliminate WHERE clause
151668 ** terms from the query that must be true due to the WHERE clause of
151669 ** the partial index.
151670 **
151671 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
@@ -151774,11 +153018,11 @@
151774 */
151775 if( pWInfo->nLevel>1 ){
151776 int nNotReady; /* The number of notReady tables */
151777 SrcItem *origSrc; /* Original list of tables */
151778 nNotReady = pWInfo->nLevel - iLevel - 1;
151779 pOrTab = sqlite3StackAllocRaw(db,
151780 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
151781 if( pOrTab==0 ) return notReady;
151782 pOrTab->nAlloc = (u8)(nNotReady + 1);
151783 pOrTab->nSrc = pOrTab->nAlloc;
151784 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
@@ -152027,11 +153271,11 @@
152027 ** indent everything in between the this point and the final OP_Return.
152028 ** See tag-20220407a in vdbe.c and shell.c */
152029 assert( pLevel->op==OP_Return );
152030 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
152031
152032 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
152033 if( !untestedTerms ) disableTerm(pLevel, pTerm);
152034 }else
152035 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
152036
152037 {
@@ -152655,11 +153899,11 @@
152655 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152656 */
152657 if( pLeft->op!=TK_COLUMN
152658 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
152659 || (ALWAYS( ExprUseYTab(pLeft) )
152660 && pLeft->y.pTab
152661 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
152662 ){
152663 int isNum;
152664 double rDummy;
152665 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
@@ -152772,12 +154016,11 @@
152772 **
152773 ** vtab_column MATCH expression
152774 ** MATCH(expression,vtab_column)
152775 */
152776 pCol = pList->a[1].pExpr;
152777 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152778 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152779 if( ExprIsVtab(pCol) ){
152780 for(i=0; i<ArraySize(aOp); i++){
152781 assert( !ExprHasProperty(pExpr, EP_IntValue) );
152782 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152783 *peOp2 = aOp[i].eOp2;
@@ -152798,11 +154041,11 @@
152798 ** names. But for this use case, xFindFunction is expected to deal
152799 ** with function names in an arbitrary case.
152800 */
152801 pCol = pList->a[0].pExpr;
152802 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152803 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152804 if( ExprIsVtab(pCol) ){
152805 sqlite3_vtab *pVtab;
152806 sqlite3_module *pMod;
152807 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
152808 void *pNotUsed;
@@ -152823,17 +154066,16 @@
152823 }
152824 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
152825 int res = 0;
152826 Expr *pLeft = pExpr->pLeft;
152827 Expr *pRight = pExpr->pRight;
152828 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
152829 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
152830 if( ExprIsVtab(pLeft) ){
152831 res++;
152832 }
152833 assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
152834 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
152835 if( pRight && ExprIsVtab(pRight) ){
152836 res++;
152837 SWAP(Expr*, pLeft, pRight);
152838 }
152839 *ppLeft = pLeft;
@@ -153378,10 +154620,11 @@
153378 iCur = pFrom->a[i].iCursor;
153379 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153380 if( pIdx->aColExpr==0 ) continue;
153381 for(i=0; i<pIdx->nKeyCol; i++){
153382 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
 
153383 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153384 aiCurCol[0] = iCur;
153385 aiCurCol[1] = XN_EXPR;
153386 return 1;
153387 }
@@ -153991,13 +155234,13 @@
153991 **
153992 ** LIMIT and OFFSET terms are ignored by most of the planner code. They
153993 ** exist only so that they may be passed to the xBestIndex method of the
153994 ** single virtual table in the FROM clause of the SELECT.
153995 */
153996 SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
153997 assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
153998 if( (p && p->pLimit) /* 1 */
153999 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
154000 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
154001 ){
154002 ExprList *pOrderBy = p->pOrderBy;
154003 int iCsr = p->pSrc->a[0].iCursor;
@@ -156530,30 +157773,18 @@
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){
156547 while( pWInfo->pExprMods ){
156548 WhereExprMod *p = pWInfo->pExprMods;
156549 pWInfo->pExprMods = p->pNext;
156550 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
156551 sqlite3DbFree(pWInfo->pParse->db, p);
156552 }
156553 }
156554
156555 /*
156556 ** Return TRUE if all of the following are true:
156557 **
156558 ** (1) X has the same or lower cost, or returns the same or fewer rows,
156559 ** than Y.
@@ -157501,10 +158732,98 @@
157501 return 1;
157502 }
157503 }
157504 return 0;
157505 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157506
157507 /*
157508 ** Add all WhereLoop objects for a single table of the join where the table
157509 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157510 ** a b-tree table, not a virtual table.
@@ -157719,10 +159038,13 @@
157719 if( pProbe->isCovering ){
157720 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
157721 m = 0;
157722 }else{
157723 m = pSrc->colUsed & pProbe->colNotIdxed;
 
 
 
157724 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
157725 }
157726
157727 /* Full scan via index */
157728 if( b
@@ -158944,11 +160266,10 @@
158944 */
158945 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
158946 int mxChoice; /* Maximum number of simultaneous paths tracked */
158947 int nLoop; /* Number of terms in the join */
158948 Parse *pParse; /* Parsing context */
158949 sqlite3 *db; /* The database connection */
158950 int iLoop; /* Loop counter over the terms of the join */
158951 int ii, jj; /* Loop counters */
158952 int mxI = 0; /* Index of next entry to replace */
158953 int nOrderBy; /* Number of ORDER BY clause terms */
158954 LogEst mxCost = 0; /* Maximum cost of a set of paths */
@@ -158963,11 +160284,10 @@
158963 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
158964 char *pSpace; /* Temporary memory used by this routine */
158965 int nSpace; /* Bytes of space allocated at pSpace */
158966
158967 pParse = pWInfo->pParse;
158968 db = pParse->db;
158969 nLoop = pWInfo->nLevel;
158970 /* TUNING: For simple queries, only the best path is tracked.
158971 ** For 2-way joins, the 5 best paths are followed.
158972 ** For joins of 3 or more tables, track the 10 best paths */
158973 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
@@ -158986,11 +160306,11 @@
158986 }
158987
158988 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
158989 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
158990 nSpace += sizeof(LogEst) * nOrderBy;
158991 pSpace = sqlite3DbMallocRawNN(db, nSpace);
158992 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
158993 aTo = (WherePath*)pSpace;
158994 aFrom = aTo+mxChoice;
158995 memset(aFrom, 0, sizeof(aFrom[0]));
158996 pX = (WhereLoop**)(aFrom+mxChoice);
@@ -159244,11 +160564,11 @@
159244 nFrom = nTo;
159245 }
159246
159247 if( nFrom==0 ){
159248 sqlite3ErrorMsg(pParse, "no query solution");
159249 sqlite3DbFreeNN(db, pSpace);
159250 return SQLITE_ERROR;
159251 }
159252
159253 /* Find the lowest cost path. pFrom will be left pointing to that path */
159254 pFrom = aFrom;
@@ -159326,12 +160646,11 @@
159326
159327
159328 pWInfo->nRowOut = pFrom->nRow;
159329
159330 /* Free temporary memory and return success */
159331 assert( db!=0 );
159332 sqlite3DbNNFreeNN(db, pSpace);
159333 return SQLITE_OK;
159334 }
159335
159336 /*
159337 ** Most queries use only a single table (they are not joins) and have
@@ -159625,10 +160944,81 @@
159625 }
159626 }
159627 nSearch += pLoop->nOut;
159628 }
159629 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159630
159631 /*
159632 ** Generate the beginning of the loop used for WHERE clause processing.
159633 ** The return value is a pointer to an opaque structure that contains
159634 ** information needed to terminate the loop. Later, the calling routine
@@ -159720,11 +161110,11 @@
159720 Parse *pParse, /* The parser context */
159721 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
159722 Expr *pWhere, /* The WHERE clause */
159723 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
159724 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
159725 Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
159726 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
159727 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
159728 ** If WHERE_USE_LIMIT, then the limit amount */
159729 ){
159730 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -159789,21 +161179,21 @@
159789 goto whereBeginError;
159790 }
159791 pWInfo->pParse = pParse;
159792 pWInfo->pTabList = pTabList;
159793 pWInfo->pOrderBy = pOrderBy;
 
159794 pWInfo->pWhere = pWhere;
 
159795 pWInfo->pResultSet = pResultSet;
159796 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159797 pWInfo->nLevel = nTabList;
159798 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159799 pWInfo->wctrlFlags = wctrlFlags;
159800 pWInfo->iLimit = iAuxArg;
159801 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159802 #ifndef SQLITE_OMIT_VIRTUALTABLE
159803 pWInfo->pLimit = pLimit;
159804 #endif
159805 memset(&pWInfo->nOBSat, 0,
159806 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
159807 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159808 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
159809 pMaskSet = &pWInfo->sMaskSet;
@@ -159868,11 +161258,13 @@
159868 #endif
159869 }
159870
159871 /* Analyze all of the subexpressions. */
159872 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159873 sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
 
 
159874 if( pParse->nErr ) goto whereBeginError;
159875
159876 /* Special case: WHERE terms that do not refer to any tables in the join
159877 ** (constant expressions). Evaluate each such term, and jump over all the
159878 ** generated code if the result is not true.
@@ -160171,10 +161563,13 @@
160171 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
160172 iIndexCur = iAuxArg;
160173 op = OP_ReopenIdx;
160174 }else{
160175 iIndexCur = pParse->nTab++;
 
 
 
160176 }
160177 pLevel->iIdxCur = iIndexCur;
160178 assert( pIx!=0 );
160179 assert( pIx->pSchema==pTab->pSchema );
160180 assert( iIndexCur>=0 );
@@ -160293,12 +161688,10 @@
160293 return pWInfo;
160294
160295 /* Jump here if malloc fails */
160296 whereBeginError:
160297 if( pWInfo ){
160298 testcase( pWInfo->pExprMods!=0 );
160299 whereUndoExprMods(pWInfo);
160300 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160301 whereInfoFree(db, pWInfo);
160302 }
160303 return 0;
160304 }
@@ -160513,11 +161906,10 @@
160513 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160514 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160515 }
160516
160517 assert( pWInfo->nLevel<=pTabList->nSrc );
160518 if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160519 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160520 int k, last;
160521 VdbeOp *pOp, *pLastOp;
160522 Index *pIdx = 0;
160523 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
@@ -160566,10 +161958,20 @@
160566 ){
160567 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
160568 last = iEnd;
160569 }else{
160570 last = pWInfo->iEndWhere;
 
 
 
 
 
 
 
 
 
 
160571 }
160572 k = pLevel->addrBody + 1;
160573 #ifdef SQLITE_DEBUG
160574 if( db->flags & SQLITE_VdbeAddopTrace ){
160575 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
@@ -174293,10 +175695,23 @@
174293 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
174294 if( db->mallocFailed ){
174295 goto opendb_out;
174296 }
174297
 
 
 
 
 
 
 
 
 
 
 
 
 
174298 /* Parse the filename/URI argument
174299 **
174300 ** Only allow sensible combinations of bits in the flags argument.
174301 ** Throw an error if any non-sense combination is used. If we
174302 ** do not block illegal combinations here, it could trigger
@@ -175722,12 +177137,12 @@
175722 ** Recover as many snapshots as possible from the wal file associated with
175723 ** schema zDb of database db.
175724 */
175725 SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
175726 int rc = SQLITE_ERROR;
175727 int iDb;
175728 #ifndef SQLITE_OMIT_WAL
 
175729
175730 #ifdef SQLITE_ENABLE_API_ARMOR
175731 if( !sqlite3SafetyCheckOk(db) ){
175732 return SQLITE_MISUSE_BKPT;
175733 }
@@ -201626,11 +203041,11 @@
201626 return SQLITE_LOCKED_VTAB;
201627 }
201628 rtreeReference(pRtree);
201629 assert(nData>=1);
201630
201631 cell.iRowid = 0; /* Used only to suppress a compiler warning */
201632
201633 /* Constraint handling. A write operation on an r-tree table may return
201634 ** SQLITE_CONSTRAINT for two reasons:
201635 **
201636 ** 1. A duplicate rowid value, or
@@ -236984,11 +238399,11 @@
236984 int nArg, /* Number of args */
236985 sqlite3_value **apUnused /* Function arguments */
236986 ){
236987 assert( nArg==0 );
236988 UNUSED_PARAM2(nArg, apUnused);
236989 sqlite3_result_text(pCtx, "fts5: 2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646", -1, SQLITE_TRANSIENT);
236990 }
236991
236992 /*
236993 ** Return true if zName is the extension on one of the shadow tables used
236994 ** by this module.
236995
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
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-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -974,17 +974,21 @@
974 /*
975 ** CAPI3REF: File Locking Levels
976 **
977 ** SQLite uses one of these integer values as the second
978 ** argument to calls it makes to the xLock() and xUnlock() methods
979 ** of an [sqlite3_io_methods] object. These values are ordered from
980 ** lest restrictive to most restrictive.
981 **
982 ** The argument to xLock() is always SHARED or higher. The argument to
983 ** xUnlock is either SHARED or NONE.
984 */
985 #define SQLITE_LOCK_NONE 0 /* xUnlock() only */
986 #define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */
987 #define SQLITE_LOCK_RESERVED 2 /* xLock() only */
988 #define SQLITE_LOCK_PENDING 3 /* xLock() only */
989 #define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */
990
991 /*
992 ** CAPI3REF: Synchronization Type Flags
993 **
994 ** When SQLite invokes the xSync() method of an
@@ -1058,11 +1062,18 @@
1062 ** <li> [SQLITE_LOCK_SHARED],
1063 ** <li> [SQLITE_LOCK_RESERVED],
1064 ** <li> [SQLITE_LOCK_PENDING], or
1065 ** <li> [SQLITE_LOCK_EXCLUSIVE].
1066 ** </ul>
1067 ** xLock() upgrades the database file lock. In other words, xLock() moves the
1068 ** database file lock in the direction NONE toward EXCLUSIVE. The argument to
1069 ** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
1070 ** SQLITE_LOCK_NONE. If the database file lock is already at or above the
1071 ** requested lock, then the call to xLock() is a no-op.
1072 ** xUnlock() downgrades the database file lock to either SHARED or NONE.
1073 * If the lock is already at or below the requested lock state, then the call
1074 ** to xUnlock() is a no-op.
1075 ** The xCheckReservedLock() method checks whether any database connection,
1076 ** either in this process or in some other process, is holding a RESERVED,
1077 ** PENDING, or EXCLUSIVE lock on the file. It returns true
1078 ** if such a lock exists and false otherwise.
1079 **
@@ -1163,13 +1174,12 @@
1174 ** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1175 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1176 ** opcode causes the xFileControl method to write the current state of
1177 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1178 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1179 ** into an integer that the pArg argument points to.
1180 ** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
 
1181 **
1182 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1183 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1184 ** layer a hint of how large the database file will grow to be during the
1185 ** current transaction. This hint is not guaranteed to be accurate but it
@@ -5815,10 +5825,20 @@
5825 ** such a conversion is possible without loss of information (in other
5826 ** words, if the value is a string that looks like a number)
5827 ** then the conversion is performed. Otherwise no conversion occurs.
5828 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5829 **
5830 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5831 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5832 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5833 ** returns something other than SQLITE_TEXT, then the return value from
5834 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5835 ** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5836 ** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5837 ** sqlite3_value_bytes16(X) might change the encoding of the value X and
5838 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5839 **
5840 ** ^Within the [xUpdate] method of a [virtual table], the
5841 ** sqlite3_value_nochange(X) interface returns true if and only if
5842 ** the column corresponding to X is unchanged by the UPDATE operation
5843 ** that the xUpdate method call was invoked to implement and if
5844 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5879,10 +5899,11 @@
5899 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5900 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5901 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5902 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5903 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5904 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5905
5906 /*
5907 ** CAPI3REF: Finding The Subtype Of SQL Values
5908 ** METHOD: sqlite3_value
5909 **
@@ -5932,11 +5953,11 @@
5953 ** In those cases, sqlite3_aggregate_context() might be called for the
5954 ** first time from within xFinal().)^
5955 **
5956 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5957 ** when first called if N is less than or equal to zero or if a memory
5958 ** allocation error occurs.
5959 **
5960 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5961 ** determined by the N parameter on first successful call. Changing the
5962 ** value of N in any subsequent call to sqlite3_aggregate_context() within
5963 ** the same aggregate function instance will not resize the memory
@@ -13160,11 +13181,11 @@
13181 /*
13182 ** Include the configuration header output by 'configure' if we're using the
13183 ** autoconf-based build
13184 */
13185 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
13186 #include "sqlite_cfg.h"
13187 #define SQLITECONFIG_H 1
13188 #endif
13189
13190 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
13191 /************** Begin file sqliteLimit.h *************************************/
@@ -14643,10 +14664,11 @@
14664 typedef struct FuncDestructor FuncDestructor;
14665 typedef struct FuncDef FuncDef;
14666 typedef struct FuncDefHash FuncDefHash;
14667 typedef struct IdList IdList;
14668 typedef struct Index Index;
14669 typedef struct IndexedExpr IndexedExpr;
14670 typedef struct IndexSample IndexSample;
14671 typedef struct KeyClass KeyClass;
14672 typedef struct KeyInfo KeyInfo;
14673 typedef struct Lookaside Lookaside;
14674 typedef struct LookasideSlot LookasideSlot;
@@ -14708,10 +14730,11 @@
14730 #define MASKBIT(n) (((Bitmask)1)<<(n))
14731 #define MASKBIT64(n) (((u64)1)<<(n))
14732 #define MASKBIT32(n) (((unsigned int)1)<<(n))
14733 #define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0)
14734 #define ALLBITS ((Bitmask)-1)
14735 #define TOPBIT (((Bitmask)1)<<(BMS-1))
14736
14737 /* A VList object records a mapping between parameters/variables/wildcards
14738 ** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14739 ** variable number associated with that parameter. See the format description
14740 ** on the sqlite3VListAdd() routine for more information. A VList is really
@@ -14722,10 +14745,335 @@
14745 /*
14746 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
14747 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14748 ** pointer types (i.e. FuncDef) defined above.
14749 */
14750 /************** Include os.h in the middle of sqliteInt.h ********************/
14751 /************** Begin file os.h **********************************************/
14752 /*
14753 ** 2001 September 16
14754 **
14755 ** The author disclaims copyright to this source code. In place of
14756 ** a legal notice, here is a blessing:
14757 **
14758 ** May you do good and not evil.
14759 ** May you find forgiveness for yourself and forgive others.
14760 ** May you share freely, never taking more than you give.
14761 **
14762 ******************************************************************************
14763 **
14764 ** This header file (together with is companion C source-code file
14765 ** "os.c") attempt to abstract the underlying operating system so that
14766 ** the SQLite library will work on both POSIX and windows systems.
14767 **
14768 ** This header file is #include-ed by sqliteInt.h and thus ends up
14769 ** being included by every source file.
14770 */
14771 #ifndef _SQLITE_OS_H_
14772 #define _SQLITE_OS_H_
14773
14774 /*
14775 ** Attempt to automatically detect the operating system and setup the
14776 ** necessary pre-processor macros for it.
14777 */
14778 /************** Include os_setup.h in the middle of os.h *********************/
14779 /************** Begin file os_setup.h ****************************************/
14780 /*
14781 ** 2013 November 25
14782 **
14783 ** The author disclaims copyright to this source code. In place of
14784 ** a legal notice, here is a blessing:
14785 **
14786 ** May you do good and not evil.
14787 ** May you find forgiveness for yourself and forgive others.
14788 ** May you share freely, never taking more than you give.
14789 **
14790 ******************************************************************************
14791 **
14792 ** This file contains pre-processor directives related to operating system
14793 ** detection and/or setup.
14794 */
14795 #ifndef SQLITE_OS_SETUP_H
14796 #define SQLITE_OS_SETUP_H
14797
14798 /*
14799 ** Figure out if we are dealing with Unix, Windows, or some other operating
14800 ** system.
14801 **
14802 ** After the following block of preprocess macros, all of
14803 **
14804 ** SQLITE_OS_KV
14805 ** SQLITE_OS_OTHER
14806 ** SQLITE_OS_UNIX
14807 ** SQLITE_OS_WIN
14808 **
14809 ** will defined to either 1 or 0. One of them will be 1. The others will be 0.
14810 ** If none of the macros are initially defined, then select either
14811 ** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform.
14812 **
14813 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
14814 ** must provide its own VFS implementation together with sqlite3_os_init()
14815 ** and sqlite3_os_end() routines.
14816 */
14817 #if !defined(SQLITE_OS_KV) && !defined(SQLITE_OS_OTHER) && \
14818 !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_WIN)
14819 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
14820 defined(__MINGW32__) || defined(__BORLANDC__)
14821 # define SQLITE_OS_WIN 1
14822 # define SQLITE_OS_UNIX 0
14823 # else
14824 # define SQLITE_OS_WIN 0
14825 # define SQLITE_OS_UNIX 1
14826 # endif
14827 #endif
14828 #if SQLITE_OS_OTHER+1>1
14829 # undef SQLITE_OS_KV
14830 # define SQLITE_OS_KV 0
14831 # undef SQLITE_OS_UNIX
14832 # define SQLITE_OS_UNIX 0
14833 # undef SQLITE_OS_WIN
14834 # define SQLITE_OS_WIN 0
14835 #endif
14836 #if SQLITE_OS_KV+1>1
14837 # undef SQLITE_OS_OTHER
14838 # define SQLITE_OS_OTHER 0
14839 # undef SQLITE_OS_UNIX
14840 # define SQLITE_OS_UNIX 0
14841 # undef SQLITE_OS_WIN
14842 # define SQLITE_OS_WIN 0
14843 # define SQLITE_OMIT_LOAD_EXTENSION 1
14844 # define SQLITE_OMIT_WAL 1
14845 # define SQLITE_OMIT_DEPRECATED 1
14846 # undef SQLITE_TEMP_STORE
14847 # define SQLITE_TEMP_STORE 3 /* Always use memory for temporary storage */
14848 # define SQLITE_DQS 0
14849 # define SQLITE_OMIT_SHARED_CACHE 1
14850 # define SQLITE_OMIT_AUTOINIT 1
14851 #endif
14852 #if SQLITE_OS_UNIX+1>1
14853 # undef SQLITE_OS_KV
14854 # define SQLITE_OS_KV 0
14855 # undef SQLITE_OS_OTHER
14856 # define SQLITE_OS_OTHER 0
14857 # undef SQLITE_OS_WIN
14858 # define SQLITE_OS_WIN 0
14859 #endif
14860 #if SQLITE_OS_WIN+1>1
14861 # undef SQLITE_OS_KV
14862 # define SQLITE_OS_KV 0
14863 # undef SQLITE_OS_OTHER
14864 # define SQLITE_OS_OTHER 0
14865 # undef SQLITE_OS_UNIX
14866 # define SQLITE_OS_UNIX 0
14867 #endif
14868
14869
14870 #endif /* SQLITE_OS_SETUP_H */
14871
14872 /************** End of os_setup.h ********************************************/
14873 /************** Continuing where we left off in os.h *************************/
14874
14875 /* If the SET_FULLSYNC macro is not defined above, then make it
14876 ** a no-op
14877 */
14878 #ifndef SET_FULLSYNC
14879 # define SET_FULLSYNC(x,y)
14880 #endif
14881
14882 /* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
14883 */
14884 #ifndef SQLITE_MAX_PATHLEN
14885 # define SQLITE_MAX_PATHLEN FILENAME_MAX
14886 #endif
14887
14888 /* Maximum number of symlinks that will be resolved while trying to
14889 ** expand a filename in xFullPathname() in the VFS.
14890 */
14891 #ifndef SQLITE_MAX_SYMLINK
14892 # define SQLITE_MAX_SYMLINK 200
14893 #endif
14894
14895 /*
14896 ** The default size of a disk sector
14897 */
14898 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
14899 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
14900 #endif
14901
14902 /*
14903 ** Temporary files are named starting with this prefix followed by 16 random
14904 ** alphanumeric characters, and no file extension. They are stored in the
14905 ** OS's standard temporary file directory, and are deleted prior to exit.
14906 ** If sqlite is being embedded in another program, you may wish to change the
14907 ** prefix to reflect your program's name, so that if your program exits
14908 ** prematurely, old temporary files can be easily identified. This can be done
14909 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
14910 **
14911 ** 2006-10-31: The default prefix used to be "sqlite_". But then
14912 ** Mcafee started using SQLite in their anti-virus product and it
14913 ** started putting files with the "sqlite" name in the c:/temp folder.
14914 ** This annoyed many windows users. Those users would then do a
14915 ** Google search for "sqlite", find the telephone numbers of the
14916 ** developers and call to wake them up at night and complain.
14917 ** For this reason, the default name prefix is changed to be "sqlite"
14918 ** spelled backwards. So the temp files are still identified, but
14919 ** anybody smart enough to figure out the code is also likely smart
14920 ** enough to know that calling the developer will not help get rid
14921 ** of the file.
14922 */
14923 #ifndef SQLITE_TEMP_FILE_PREFIX
14924 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
14925 #endif
14926
14927 /*
14928 ** The following values may be passed as the second argument to
14929 ** sqlite3OsLock(). The various locks exhibit the following semantics:
14930 **
14931 ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
14932 ** RESERVED: A single process may hold a RESERVED lock on a file at
14933 ** any time. Other processes may hold and obtain new SHARED locks.
14934 ** PENDING: A single process may hold a PENDING lock on a file at
14935 ** any one time. Existing SHARED locks may persist, but no new
14936 ** SHARED locks may be obtained by other processes.
14937 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
14938 **
14939 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
14940 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
14941 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
14942 ** sqlite3OsLock().
14943 */
14944 #define NO_LOCK 0
14945 #define SHARED_LOCK 1
14946 #define RESERVED_LOCK 2
14947 #define PENDING_LOCK 3
14948 #define EXCLUSIVE_LOCK 4
14949
14950 /*
14951 ** File Locking Notes: (Mostly about windows but also some info for Unix)
14952 **
14953 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
14954 ** those functions are not available. So we use only LockFile() and
14955 ** UnlockFile().
14956 **
14957 ** LockFile() prevents not just writing but also reading by other processes.
14958 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
14959 ** byte out of a specific range of bytes. The lock byte is obtained at
14960 ** random so two separate readers can probably access the file at the
14961 ** same time, unless they are unlucky and choose the same lock byte.
14962 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
14963 ** There can only be one writer. A RESERVED_LOCK is obtained by locking
14964 ** a single byte of the file that is designated as the reserved lock byte.
14965 ** A PENDING_LOCK is obtained by locking a designated byte different from
14966 ** the RESERVED_LOCK byte.
14967 **
14968 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
14969 ** which means we can use reader/writer locks. When reader/writer locks
14970 ** are used, the lock is placed on the same range of bytes that is used
14971 ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
14972 ** will support two or more Win95 readers or two or more WinNT readers.
14973 ** But a single Win95 reader will lock out all WinNT readers and a single
14974 ** WinNT reader will lock out all other Win95 readers.
14975 **
14976 ** The following #defines specify the range of bytes used for locking.
14977 ** SHARED_SIZE is the number of bytes available in the pool from which
14978 ** a random byte is selected for a shared lock. The pool of bytes for
14979 ** shared locks begins at SHARED_FIRST.
14980 **
14981 ** The same locking strategy and
14982 ** byte ranges are used for Unix. This leaves open the possibility of having
14983 ** clients on win95, winNT, and unix all talking to the same shared file
14984 ** and all locking correctly. To do so would require that samba (or whatever
14985 ** tool is being used for file sharing) implements locks correctly between
14986 ** windows and unix. I'm guessing that isn't likely to happen, but by
14987 ** using the same locking range we are at least open to the possibility.
14988 **
14989 ** Locking in windows is manditory. For this reason, we cannot store
14990 ** actual data in the bytes used for locking. The pager never allocates
14991 ** the pages involved in locking therefore. SHARED_SIZE is selected so
14992 ** that all locks will fit on a single page even at the minimum page size.
14993 ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
14994 ** is set high so that we don't have to allocate an unused page except
14995 ** for very large databases. But one should test the page skipping logic
14996 ** by setting PENDING_BYTE low and running the entire regression suite.
14997 **
14998 ** Changing the value of PENDING_BYTE results in a subtly incompatible
14999 ** file format. Depending on how it is changed, you might not notice
15000 ** the incompatibility right away, even running a full regression test.
15001 ** The default location of PENDING_BYTE is the first byte past the
15002 ** 1GB boundary.
15003 **
15004 */
15005 #ifdef SQLITE_OMIT_WSD
15006 # define PENDING_BYTE (0x40000000)
15007 #else
15008 # define PENDING_BYTE sqlite3PendingByte
15009 #endif
15010 #define RESERVED_BYTE (PENDING_BYTE+1)
15011 #define SHARED_FIRST (PENDING_BYTE+2)
15012 #define SHARED_SIZE 510
15013
15014 /*
15015 ** Wrapper around OS specific sqlite3_os_init() function.
15016 */
15017 SQLITE_PRIVATE int sqlite3OsInit(void);
15018
15019 /*
15020 ** Functions for accessing sqlite3_file methods
15021 */
15022 SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
15023 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15024 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15025 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15026 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15027 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15028 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15029 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15030 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15031 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15032 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15033 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
15034 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15035 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15036 #ifndef SQLITE_OMIT_WAL
15037 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15038 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15039 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
15040 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15041 #endif /* SQLITE_OMIT_WAL */
15042 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15043 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15044
15045
15046 /*
15047 ** Functions for accessing sqlite3_vfs methods
15048 */
15049 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15050 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15051 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15052 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15053 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15054 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15055 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15056 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15057 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
15058 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
15059 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15060 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15061 SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15062 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15063
15064 /*
15065 ** Convenience functions for opening and closing files using
15066 ** sqlite3_malloc() to obtain space for the file-handle structure.
15067 */
15068 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15069 SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
15070
15071 #endif /* _SQLITE_OS_H_ */
15072
15073 /************** End of os.h **************************************************/
15074 /************** Continuing where we left off in sqliteInt.h ******************/
15075 /************** Include pager.h in the middle of sqliteInt.h *****************/
15076 /************** Begin file pager.h *******************************************/
15077 /*
15078 ** 2001 September 15
15079 **
@@ -15576,11 +15924,11 @@
15924 #define OP_MustBeInt 13 /* jump */
15925 #define OP_Jump 14 /* jump */
15926 #define OP_Once 15 /* jump */
15927 #define OP_If 16 /* jump */
15928 #define OP_IfNot 17 /* jump */
15929 #define OP_IsType 18 /* jump, synopsis: if typeof(P1.P3) in P5 goto P2 */
15930 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
15931 #define OP_IfNullRow 20 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
15932 #define OP_SeekLT 21 /* jump, synopsis: key=r[P3@P4] */
15933 #define OP_SeekLE 22 /* jump, synopsis: key=r[P3@P4] */
15934 #define OP_SeekGE 23 /* jump, synopsis: key=r[P3@P4] */
@@ -15759,11 +16107,11 @@
16107 #define OPFLG_OUT2 0x10 /* out2: P2 is an output */
16108 #define OPFLG_OUT3 0x20 /* out3: P3 is an output */
16109 #define OPFLG_INITIALIZER {\
16110 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
16111 /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16112 /* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x09, 0x09, 0x09,\
16113 /* 24 */ 0x09, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
16114 /* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
16115 /* 40 */ 0x01, 0x01, 0x01, 0x26, 0x26, 0x01, 0x23, 0x0b,\
16116 /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16117 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x01,\
@@ -15856,10 +16204,11 @@
16204 SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
16205 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
16206 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
16207 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
16208 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
16209 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe*, int);
16210 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
16211 SQLITE_PRIVATE void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
16212 SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
16213 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
16214 #ifdef SQLITE_DEBUG
@@ -16218,301 +16567,10 @@
16567 #endif
16568
16569 #endif /* _PCACHE_H_ */
16570
16571 /************** End of pcache.h **********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16572 /************** Continuing where we left off in sqliteInt.h ******************/
16573 /************** Include mutex.h in the middle of sqliteInt.h *****************/
16574 /************** Begin file mutex.h *******************************************/
16575 /*
16576 ** 2007 August 28
@@ -17100,10 +17158,11 @@
17158 #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
17159 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17160 #define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */
17161 #define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */
17162 /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */
17163 #define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */
17164 #define SQLITE_AllOpts 0xffffffff /* All optimizations */
17165
17166 /*
17167 ** Macros for testing whether or not optimizations are enabled or disabled.
17168 */
@@ -17672,11 +17731,11 @@
17731 ** table support is omitted from the build.
17732 */
17733 #ifndef SQLITE_OMIT_VIRTUALTABLE
17734 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17735 # define ExprIsVtab(X) \
17736 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
17737 #else
17738 # define IsVirtual(X) 0
17739 # define ExprIsVtab(X) 0
17740 #endif
17741
@@ -17889,13 +17948,25 @@
17948 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17949 **
17950 ** The Index.onError field determines whether or not the indexed columns
17951 ** must be unique and what to do if they are not. When Index.onError=OE_None,
17952 ** it means this is not a unique index. Otherwise it is a unique index
17953 ** and the value of Index.onError indicates which conflict resolution
17954 ** algorithm to employ when an attempt is made to insert a non-unique
17955 ** element.
17956 **
17957 ** The colNotIdxed bitmask is used in combination with SrcItem.colUsed
17958 ** for a fast test to see if an index can serve as a covering index.
17959 ** colNotIdxed has a 1 bit for every column of the original table that
17960 ** is *not* available in the index. Thus the expression
17961 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
17962 ** covering index. The most significant bit of of colNotIdxed will always
17963 ** be true (note-20221022-a). If a column beyond the 63rd column of the
17964 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
17965 ** and we have to assume either that the index is not covering, or use
17966 ** an alternative (slower) algorithm to determine whether or not
17967 ** the index is covering.
17968 **
17969 ** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17970 ** generate VDBE code (as opposed to parsing one read from an sqlite_schema
17971 ** table as part of parsing an existing database schema), transient instances
17972 ** of this structure may be created. In this case the Index.tnum variable is
@@ -17928,19 +17999,21 @@
17999 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18000 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18001 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18002 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18003 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
18004 unsigned bHasExpr:1; /* Index contains an expression, either a literal
18005 ** expression, or a reference to a VIRTUAL column */
18006 #ifdef SQLITE_ENABLE_STAT4
18007 int nSample; /* Number of elements in aSample[] */
18008 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
18009 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
18010 IndexSample *aSample; /* Samples of the left-most key */
18011 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18012 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18013 #endif
18014 Bitmask colNotIdxed; /* Unindexed columns in pTab */
18015 };
18016
18017 /*
18018 ** Allowed values for Index.idxType
18019 */
@@ -18389,10 +18462,18 @@
18462
18463 /*
18464 ** The SrcItem object represents a single term in the FROM clause of a query.
18465 ** The SrcList object is mostly an array of SrcItems.
18466 **
18467 ** The jointype starts out showing the join type between the current table
18468 ** and the next table on the list. The parser builds the list this way.
18469 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
18470 ** jointype expresses the join between the table and the previous table.
18471 **
18472 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18473 ** contains more than 63 columns and the 64-th or later column is used.
18474 **
18475 ** Union member validity:
18476 **
18477 ** u1.zIndexedBy fg.isIndexedBy && !fg.isTabFunc
18478 ** u1.pFuncArg fg.isTabFunc && !fg.isIndexedBy
18479 ** u2.pIBIndex fg.isIndexedBy && !fg.isCte
@@ -18428,18 +18509,18 @@
18509 int iCursor; /* The VDBE cursor number used to access this table */
18510 union {
18511 Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */
18512 IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */
18513 } u3;
18514 Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
18515 union {
18516 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
18517 ExprList *pFuncArg; /* Arguments to table-valued-function */
18518 } u1;
18519 union {
18520 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
18521 CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */
18522 } u2;
18523 };
18524
18525 /*
18526 ** The OnOrUsing object represents either an ON clause or a USING clause.
@@ -18449,27 +18530,15 @@
18530 Expr *pOn; /* The ON clause of a join */
18531 IdList *pUsing; /* The USING clause of a join */
18532 };
18533
18534 /*
18535 ** This object represents one or more tables that are the source of
18536 ** content for an SQL statement. For example, a single SrcList object
18537 ** is used to hold the FROM clause of a SELECT statement. SrcList also
18538 ** represents the target tables for DELETE, INSERT, and UPDATE statements.
18539 **
 
 
 
 
 
 
 
 
 
 
 
 
18540 */
18541 struct SrcList {
18542 int nSrc; /* Number of tables or subqueries in the FROM clause */
18543 u32 nAlloc; /* Number of entries allocated in a[] below */
18544 SrcItem a[1]; /* One entry for each identifier on the list */
@@ -18875,10 +18944,32 @@
18944 # define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
18945 # define DbMaskAllZero(M) (M)==0
18946 # define DbMaskNonZero(M) (M)!=0
18947 #endif
18948
18949 /*
18950 ** For each index X that has as one of its arguments either an expression
18951 ** or the name of a virtual generated column, and if X is in scope such that
18952 ** the value of the expression can simply be read from the index, then
18953 ** there is an instance of this object on the Parse.pIdxExpr list.
18954 **
18955 ** During code generation, while generating code to evaluate expressions,
18956 ** this list is consulted and if a matching expression is found, the value
18957 ** is read from the index rather than being recomputed.
18958 */
18959 struct IndexedExpr {
18960 Expr *pExpr; /* The expression contained in the index */
18961 int iDataCur; /* The data cursor associated with the index */
18962 int iIdxCur; /* The index cursor */
18963 int iIdxCol; /* The index column that contains value of pExpr */
18964 u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */
18965 IndexedExpr *pIENext; /* Next in a list of all indexed expressions */
18966 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
18967 const char *zIdxName; /* Name of index, used only for bytecode comments */
18968 #endif
18969 };
18970
18971 /*
18972 ** An instance of the ParseCleanup object specifies an operation that
18973 ** should be performed after parsing to deallocation resources obtained
18974 ** during the parse and which are no longer needed.
18975 */
@@ -18916,11 +19007,11 @@
19007 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
19008 u8 mayAbort; /* True if statement may throw an ABORT exception */
19009 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
19010 u8 okConstFactor; /* OK to factor out constants */
19011 u8 disableLookaside; /* Number of times lookaside has been disabled */
19012 u8 prepFlags; /* SQLITE_PREPARE_* flags */
19013 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
19014 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
19015 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
19016 #endif
19017 int nRangeReg; /* Size of the temporary register block */
@@ -18933,10 +19024,11 @@
19024 ** of the base register during check-constraint eval */
19025 int nLabel; /* The *negative* of the number of labels used */
19026 int nLabelAlloc; /* Number of slots in aLabel */
19027 int *aLabel; /* Space to hold the labels */
19028 ExprList *pConstExpr;/* Constant expressions */
19029 IndexedExpr *pIdxExpr;/* List of expressions used by active indexes */
19030 Token constraintName;/* Name of the constraint currently being parsed */
19031 yDbMask writeMask; /* Start a write transaction on these databases */
19032 yDbMask cookieMask; /* Bitmask of schema verified databases */
19033 int regRowid; /* Register holding rowid of CREATE TABLE entry */
19034 int regRoot; /* Register holding root page number for new objects */
@@ -19368,19 +19460,19 @@
19460 SrcList *pSrcList; /* FROM clause */
19461 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
19462 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
19463 int *aiCol; /* array of column indexes */
19464 struct IdxCover *pIdxCover; /* Check for index coverage */
 
19465 ExprList *pGroupBy; /* GROUP BY clause */
19466 Select *pSelect; /* HAVING to WHERE clause ctx */
19467 struct WindowRewrite *pRewrite; /* Window rewrite context */
19468 struct WhereConst *pConst; /* WHERE clause constants */
19469 struct RenameCtx *pRename; /* RENAME COLUMN context */
19470 struct Table *pTab; /* Table of generated column */
19471 struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
19472 SrcItem *pSrcItem; /* A single FROM clause item */
19473 DbFixer *pFix; /* See sqlite3FixSelect() */
19474 } u;
19475 };
19476
19477 /*
19478 ** The following structure contains information used by the sqliteFix...
@@ -19703,16 +19795,20 @@
19795 ** The alloca() routine never returns NULL. This will cause code paths
19796 ** that deal with sqlite3StackAlloc() failures to be unreachable.
19797 */
19798 #ifdef SQLITE_USE_ALLOCA
19799 # define sqlite3StackAllocRaw(D,N) alloca(N)
19800 # define sqlite3StackAllocRawNN(D,N) alloca(N)
19801 # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
19802 # define sqlite3StackFree(D,P)
19803 # define sqlite3StackFreeNN(D,P)
19804 #else
19805 # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
19806 # define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N)
19807 # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
19808 # define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
19809 # define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P)
19810 #endif
19811
19812 /* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
19813 ** are, disable MEMSYS3
19814 */
@@ -20767,10 +20863,14 @@
20863 SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20864
20865 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20866 SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20867 #endif
20868
20869 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20870 SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20871 #endif
20872
20873 #endif /* SQLITEINT_H */
20874
20875 /************** End of sqliteInt.h *******************************************/
20876 /************** Begin file os_common.h ***************************************/
@@ -20999,11 +21099,11 @@
21099 /*
21100 ** Include the configuration header output by 'configure' if we're using the
21101 ** autoconf-based build
21102 */
21103 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21104 /* #include "sqlite_cfg.h" */
21105 #define SQLITECONFIG_H 1
21106 #endif
21107
21108 /* These macros are provided to "stringify" the value of the define
21109 ** for those options in which the value is meaningful. */
@@ -21164,10 +21264,13 @@
21264 "DISABLE_PAGECACHE_OVERFLOW_STATS",
21265 #endif
21266 #ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
21267 "DISABLE_SKIPAHEAD_DISTINCT",
21268 #endif
21269 #ifdef SQLITE_DQS
21270 "DQS=" CTIMEOPT_VAL(SQLITE_DQS),
21271 #endif
21272 #ifdef SQLITE_ENABLE_8_3_NAMES
21273 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
21274 #endif
21275 #ifdef SQLITE_ENABLE_API_ARMOR
21276 "ENABLE_API_ARMOR",
@@ -24740,13 +24843,15 @@
24843 DO_OS_MALLOC_TEST(id);
24844 return id->pMethods->xFileSize(id, pSize);
24845 }
24846 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
24847 DO_OS_MALLOC_TEST(id);
24848 assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE );
24849 return id->pMethods->xLock(id, lockType);
24850 }
24851 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24852 assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED );
24853 return id->pMethods->xUnlock(id, lockType);
24854 }
24855 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
24856 DO_OS_MALLOC_TEST(id);
24857 return id->pMethods->xCheckReservedLock(id, pResOut);
@@ -35382,11 +35487,11 @@
35487 /* 13 */ "MustBeInt" OpHelp(""),
35488 /* 14 */ "Jump" OpHelp(""),
35489 /* 15 */ "Once" OpHelp(""),
35490 /* 16 */ "If" OpHelp(""),
35491 /* 17 */ "IfNot" OpHelp(""),
35492 /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
35493 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35494 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35495 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35496 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35497 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
@@ -35557,10 +35662,982 @@
35662 return azName[i];
35663 }
35664 #endif
35665
35666 /************** End of opcodes.c *********************************************/
35667 /************** Begin file os_kv.c *******************************************/
35668 /*
35669 ** 2022-09-06
35670 **
35671 ** The author disclaims copyright to this source code. In place of
35672 ** a legal notice, here is a blessing:
35673 **
35674 ** May you do good and not evil.
35675 ** May you find forgiveness for yourself and forgive others.
35676 ** May you share freely, never taking more than you give.
35677 **
35678 ******************************************************************************
35679 **
35680 ** This file contains an experimental VFS layer that operates on a
35681 ** Key/Value storage engine where both keys and values must be pure
35682 ** text.
35683 */
35684 /* #include <sqliteInt.h> */
35685 #if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL))
35686
35687 /*****************************************************************************
35688 ** Debugging logic
35689 */
35690
35691 /* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
35692 #if 0
35693 #define SQLITE_KV_TRACE(X) printf X
35694 #else
35695 #define SQLITE_KV_TRACE(X)
35696 #endif
35697
35698 /* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
35699 #if 0
35700 #define SQLITE_KV_LOG(X) printf X
35701 #else
35702 #define SQLITE_KV_LOG(X)
35703 #endif
35704
35705
35706 /*
35707 ** Forward declaration of objects used by this VFS implementation
35708 */
35709 typedef struct KVVfsFile KVVfsFile;
35710
35711 /* A single open file. There are only two files represented by this
35712 ** VFS - the database and the rollback journal.
35713 */
35714 struct KVVfsFile {
35715 sqlite3_file base; /* IO methods */
35716 const char *zClass; /* Storage class */
35717 int isJournal; /* True if this is a journal file */
35718 unsigned int nJrnl; /* Space allocated for aJrnl[] */
35719 char *aJrnl; /* Journal content */
35720 int szPage; /* Last known page size */
35721 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35722 };
35723
35724 /*
35725 ** Methods for KVVfsFile
35726 */
35727 static int kvvfsClose(sqlite3_file*);
35728 static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35729 static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35730 static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35731 static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35732 static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
35733 static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
35734 static int kvvfsSyncDb(sqlite3_file*, int flags);
35735 static int kvvfsSyncJrnl(sqlite3_file*, int flags);
35736 static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
35737 static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
35738 static int kvvfsLock(sqlite3_file*, int);
35739 static int kvvfsUnlock(sqlite3_file*, int);
35740 static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
35741 static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
35742 static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
35743 static int kvvfsSectorSize(sqlite3_file*);
35744 static int kvvfsDeviceCharacteristics(sqlite3_file*);
35745
35746 /*
35747 ** Methods for sqlite3_vfs
35748 */
35749 static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
35750 static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
35751 static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
35752 static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
35753 static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
35754 static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
35755 static int kvvfsSleep(sqlite3_vfs*, int microseconds);
35756 static int kvvfsCurrentTime(sqlite3_vfs*, double*);
35757 static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
35758
35759 static sqlite3_vfs sqlite3OsKvvfsObject = {
35760 1, /* iVersion */
35761 sizeof(KVVfsFile), /* szOsFile */
35762 1024, /* mxPathname */
35763 0, /* pNext */
35764 "kvvfs", /* zName */
35765 0, /* pAppData */
35766 kvvfsOpen, /* xOpen */
35767 kvvfsDelete, /* xDelete */
35768 kvvfsAccess, /* xAccess */
35769 kvvfsFullPathname, /* xFullPathname */
35770 kvvfsDlOpen, /* xDlOpen */
35771 0, /* xDlError */
35772 0, /* xDlSym */
35773 0, /* xDlClose */
35774 kvvfsRandomness, /* xRandomness */
35775 kvvfsSleep, /* xSleep */
35776 kvvfsCurrentTime, /* xCurrentTime */
35777 0, /* xGetLastError */
35778 kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
35779 };
35780
35781 /* Methods for sqlite3_file objects referencing a database file
35782 */
35783 static sqlite3_io_methods kvvfs_db_io_methods = {
35784 1, /* iVersion */
35785 kvvfsClose, /* xClose */
35786 kvvfsReadDb, /* xRead */
35787 kvvfsWriteDb, /* xWrite */
35788 kvvfsTruncateDb, /* xTruncate */
35789 kvvfsSyncDb, /* xSync */
35790 kvvfsFileSizeDb, /* xFileSize */
35791 kvvfsLock, /* xLock */
35792 kvvfsUnlock, /* xUnlock */
35793 kvvfsCheckReservedLock, /* xCheckReservedLock */
35794 kvvfsFileControlDb, /* xFileControl */
35795 kvvfsSectorSize, /* xSectorSize */
35796 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35797 0, /* xShmMap */
35798 0, /* xShmLock */
35799 0, /* xShmBarrier */
35800 0, /* xShmUnmap */
35801 0, /* xFetch */
35802 0 /* xUnfetch */
35803 };
35804
35805 /* Methods for sqlite3_file objects referencing a rollback journal
35806 */
35807 static sqlite3_io_methods kvvfs_jrnl_io_methods = {
35808 1, /* iVersion */
35809 kvvfsClose, /* xClose */
35810 kvvfsReadJrnl, /* xRead */
35811 kvvfsWriteJrnl, /* xWrite */
35812 kvvfsTruncateJrnl, /* xTruncate */
35813 kvvfsSyncJrnl, /* xSync */
35814 kvvfsFileSizeJrnl, /* xFileSize */
35815 kvvfsLock, /* xLock */
35816 kvvfsUnlock, /* xUnlock */
35817 kvvfsCheckReservedLock, /* xCheckReservedLock */
35818 kvvfsFileControlJrnl, /* xFileControl */
35819 kvvfsSectorSize, /* xSectorSize */
35820 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35821 0, /* xShmMap */
35822 0, /* xShmLock */
35823 0, /* xShmBarrier */
35824 0, /* xShmUnmap */
35825 0, /* xFetch */
35826 0 /* xUnfetch */
35827 };
35828
35829 /****** Storage subsystem **************************************************/
35830 #include <sys/types.h>
35831 #include <sys/stat.h>
35832 #include <unistd.h>
35833
35834 /* Forward declarations for the low-level storage engine
35835 */
35836 static int kvstorageWrite(const char*, const char *zKey, const char *zData);
35837 static int kvstorageDelete(const char*, const char *zKey);
35838 static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
35839 #define KVSTORAGE_KEY_SZ 32
35840
35841 /* Expand the key name with an appropriate prefix and put the result
35842 ** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
35843 ** KVSTORAGE_KEY_SZ bytes.
35844 */
35845 static void kvstorageMakeKey(
35846 const char *zClass,
35847 const char *zKeyIn,
35848 char *zKeyOut
35849 ){
35850 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
35851 }
35852
35853 /* Write content into a key. zClass is the particular namespace of the
35854 ** underlying key/value store to use - either "local" or "session".
35855 **
35856 ** Both zKey and zData are zero-terminated pure text strings.
35857 **
35858 ** Return the number of errors.
35859 */
35860 static int kvstorageWrite(
35861 const char *zClass,
35862 const char *zKey,
35863 const char *zData
35864 ){
35865 FILE *fd;
35866 char zXKey[KVSTORAGE_KEY_SZ];
35867 kvstorageMakeKey(zClass, zKey, zXKey);
35868 fd = fopen(zXKey, "wb");
35869 if( fd ){
35870 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
35871 (int)strlen(zData), zData,
35872 strlen(zData)>50 ? "..." : ""));
35873 fputs(zData, fd);
35874 fclose(fd);
35875 return 0;
35876 }else{
35877 return 1;
35878 }
35879 }
35880
35881 /* Delete a key (with its corresponding data) from the key/value
35882 ** namespace given by zClass. If the key does not previously exist,
35883 ** this routine is a no-op.
35884 */
35885 static int kvstorageDelete(const char *zClass, const char *zKey){
35886 char zXKey[KVSTORAGE_KEY_SZ];
35887 kvstorageMakeKey(zClass, zKey, zXKey);
35888 unlink(zXKey);
35889 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
35890 return 0;
35891 }
35892
35893 /* Read the value associated with a zKey from the key/value namespace given
35894 ** by zClass and put the text data associated with that key in the first
35895 ** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
35896 ** enough to hold it all. The value put into zBuf must always be zero
35897 ** terminated, even if it gets truncated because nBuf is not large enough.
35898 **
35899 ** Return the total number of bytes in the data, without truncation, and
35900 ** not counting the final zero terminator. Return -1 if the key does
35901 ** not exist.
35902 **
35903 ** If nBuf<=0 then this routine simply returns the size of the data without
35904 ** actually reading it.
35905 */
35906 static int kvstorageRead(
35907 const char *zClass,
35908 const char *zKey,
35909 char *zBuf,
35910 int nBuf
35911 ){
35912 FILE *fd;
35913 struct stat buf;
35914 char zXKey[KVSTORAGE_KEY_SZ];
35915 kvstorageMakeKey(zClass, zKey, zXKey);
35916 if( access(zXKey, R_OK)!=0
35917 || stat(zXKey, &buf)!=0
35918 || !S_ISREG(buf.st_mode)
35919 ){
35920 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35921 return -1;
35922 }
35923 if( nBuf<=0 ){
35924 return (int)buf.st_size;
35925 }else if( nBuf==1 ){
35926 zBuf[0] = 0;
35927 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
35928 (int)buf.st_size));
35929 return (int)buf.st_size;
35930 }
35931 if( nBuf > buf.st_size + 1 ){
35932 nBuf = buf.st_size + 1;
35933 }
35934 fd = fopen(zXKey, "rb");
35935 if( fd==0 ){
35936 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35937 return -1;
35938 }else{
35939 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
35940 fclose(fd);
35941 zBuf[n] = 0;
35942 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
35943 n, zBuf, n>50 ? "..." : ""));
35944 return (int)n;
35945 }
35946 }
35947
35948 /*
35949 ** An internal level of indirection which enables us to replace the
35950 ** kvvfs i/o methods with JavaScript implementations in WASM builds.
35951 ** Maintenance reminder: if this struct changes in any way, the JSON
35952 ** rendering of its structure must be updated in
35953 ** sqlite3_wasm_enum_json(). There are no binary compatibility
35954 ** concerns, so it does not need an iVersion member. This file is
35955 ** necessarily always compiled together with sqlite3_wasm_enum_json(),
35956 ** and JS code dynamically creates the mapping of members based on
35957 ** that JSON description.
35958 */
35959 typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
35960 struct sqlite3_kvvfs_methods {
35961 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
35962 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
35963 int (*xDelete)(const char *zClass, const char *zKey);
35964 const int nKeySize;
35965 };
35966
35967 /*
35968 ** This object holds the kvvfs I/O methods which may be swapped out
35969 ** for JavaScript-side implementations in WASM builds. In such builds
35970 ** it cannot be const, but in native builds it should be so that
35971 ** the compiler can hopefully optimize this level of indirection out.
35972 ** That said, kvvfs is intended primarily for use in WASM builds.
35973 **
35974 ** Note that this is not explicitly flagged as static because the
35975 ** amalgamation build will tag it with SQLITE_PRIVATE.
35976 */
35977 #ifndef SQLITE_WASM
35978 const
35979 #endif
35980 SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
35981 kvstorageRead,
35982 kvstorageWrite,
35983 kvstorageDelete,
35984 KVSTORAGE_KEY_SZ
35985 };
35986
35987 /****** Utility subroutines ************************************************/
35988
35989 /*
35990 ** Encode binary into the text encoded used to persist on disk.
35991 ** The output text is stored in aOut[], which must be at least
35992 ** nData+1 bytes in length.
35993 **
35994 ** Return the actual length of the encoded text, not counting the
35995 ** zero terminator at the end.
35996 **
35997 ** Encoding format
35998 ** ---------------
35999 **
36000 ** * Non-zero bytes are encoded as upper-case hexadecimal
36001 **
36002 ** * A sequence of one or more zero-bytes that are not at the
36003 ** beginning of the buffer are encoded as a little-endian
36004 ** base-26 number using a..z. "a" means 0. "b" means 1,
36005 ** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
36006 **
36007 ** * Because there is no overlap between the encoding characters
36008 ** of hexadecimal and base-26 numbers, it is always clear where
36009 ** one stops and the next begins.
36010 */
36011 static int kvvfsEncode(const char *aData, int nData, char *aOut){
36012 int i, j;
36013 const unsigned char *a = (const unsigned char*)aData;
36014 for(i=j=0; i<nData; i++){
36015 unsigned char c = a[i];
36016 if( c!=0 ){
36017 aOut[j++] = "0123456789ABCDEF"[c>>4];
36018 aOut[j++] = "0123456789ABCDEF"[c&0xf];
36019 }else{
36020 /* A sequence of 1 or more zeros is stored as a little-endian
36021 ** base-26 number using a..z as the digits. So one zero is "b".
36022 ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
36023 ** and so forth.
36024 */
36025 int k;
36026 for(k=1; i+k<nData && a[i+k]==0; k++){}
36027 i += k-1;
36028 while( k>0 ){
36029 aOut[j++] = 'a'+(k%26);
36030 k /= 26;
36031 }
36032 }
36033 }
36034 aOut[j] = 0;
36035 return j;
36036 }
36037
36038 static const signed char kvvfsHexValue[256] = {
36039 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36040 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36041 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36042 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36043 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36044 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36045 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36046 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36047
36048 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36049 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36050 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36051 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36052 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36053 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36054 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36055 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36056 };
36057
36058 /*
36059 ** Decode the text encoding back to binary. The binary content is
36060 ** written into pOut, which must be at least nOut bytes in length.
36061 **
36062 ** The return value is the number of bytes actually written into aOut[].
36063 */
36064 static int kvvfsDecode(const char *a, char *aOut, int nOut){
36065 int i, j;
36066 int c;
36067 const unsigned char *aIn = (const unsigned char*)a;
36068 i = 0;
36069 j = 0;
36070 while( 1 ){
36071 c = kvvfsHexValue[aIn[i]];
36072 if( c<0 ){
36073 int n = 0;
36074 int mult = 1;
36075 c = aIn[i];
36076 if( c==0 ) break;
36077 while( c>='a' && c<='z' ){
36078 n += (c - 'a')*mult;
36079 mult *= 26;
36080 c = aIn[++i];
36081 }
36082 if( j+n>nOut ) return -1;
36083 memset(&aOut[j], 0, n);
36084 j += n;
36085 c = aIn[i];
36086 if( c==0 ) break;
36087 }else{
36088 aOut[j] = c<<4;
36089 c = kvvfsHexValue[aIn[++i]];
36090 if( c<0 ) break;
36091 aOut[j++] += c;
36092 i++;
36093 }
36094 }
36095 return j;
36096 }
36097
36098 /*
36099 ** Decode a complete journal file. Allocate space in pFile->aJrnl
36100 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
36101 ** if an error is encountered.
36102 **
36103 ** The first few characters of the text encoding will be a little-endian
36104 ** base-26 number (digits a..z) that is the total number of bytes
36105 ** in the decoded journal file image. This base-26 number is followed
36106 ** by a single space, then the encoding of the journal. The space
36107 ** separator is required to act as a terminator for the base-26 number.
36108 */
36109 static void kvvfsDecodeJournal(
36110 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36111 const char *zTxt, /* Text encoding. Zero-terminated */
36112 int nTxt /* Bytes in zTxt, excluding zero terminator */
36113 ){
36114 unsigned int n = 0;
36115 int c, i, mult;
36116 i = 0;
36117 mult = 1;
36118 while( (c = zTxt[i++])>='a' && c<='z' ){
36119 n += (zTxt[i] - 'a')*mult;
36120 mult *= 26;
36121 }
36122 sqlite3_free(pFile->aJrnl);
36123 pFile->aJrnl = sqlite3_malloc64( n );
36124 if( pFile->aJrnl==0 ){
36125 pFile->nJrnl = 0;
36126 return;
36127 }
36128 pFile->nJrnl = n;
36129 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36130 if( n<pFile->nJrnl ){
36131 sqlite3_free(pFile->aJrnl);
36132 pFile->aJrnl = 0;
36133 pFile->nJrnl = 0;
36134 }
36135 }
36136
36137 /*
36138 ** Read or write the "sz" element, containing the database file size.
36139 */
36140 static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
36141 char zData[50];
36142 zData[0] = 0;
36143 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36144 return strtoll(zData, 0, 0);
36145 }
36146 static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
36147 char zData[50];
36148 sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
36149 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36150 }
36151
36152 /****** sqlite3_io_methods methods ******************************************/
36153
36154 /*
36155 ** Close an kvvfs-file.
36156 */
36157 static int kvvfsClose(sqlite3_file *pProtoFile){
36158 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36159
36160 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36161 pFile->isJournal ? "journal" : "db"));
36162 sqlite3_free(pFile->aJrnl);
36163 return SQLITE_OK;
36164 }
36165
36166 /*
36167 ** Read from the -journal file.
36168 */
36169 static int kvvfsReadJrnl(
36170 sqlite3_file *pProtoFile,
36171 void *zBuf,
36172 int iAmt,
36173 sqlite_int64 iOfst
36174 ){
36175 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36176 assert( pFile->isJournal );
36177 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36178 if( pFile->aJrnl==0 ){
36179 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36180 char *aTxt;
36181 if( szTxt<=4 ){
36182 return SQLITE_IOERR;
36183 }
36184 aTxt = sqlite3_malloc64( szTxt+1 );
36185 if( aTxt==0 ) return SQLITE_NOMEM;
36186 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36187 kvvfsDecodeJournal(pFile, aTxt, szTxt);
36188 sqlite3_free(aTxt);
36189 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36190 }
36191 if( iOfst+iAmt>pFile->nJrnl ){
36192 return SQLITE_IOERR_SHORT_READ;
36193 }
36194 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36195 return SQLITE_OK;
36196 }
36197
36198 /*
36199 ** Read from the database file.
36200 */
36201 static int kvvfsReadDb(
36202 sqlite3_file *pProtoFile,
36203 void *zBuf,
36204 int iAmt,
36205 sqlite_int64 iOfst
36206 ){
36207 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36208 unsigned int pgno;
36209 int got, n;
36210 char zKey[30];
36211 char aData[133073];
36212 assert( iOfst>=0 );
36213 assert( iAmt>=0 );
36214 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36215 if( iOfst+iAmt>=512 ){
36216 if( (iOfst % iAmt)!=0 ){
36217 return SQLITE_IOERR_READ;
36218 }
36219 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36220 return SQLITE_IOERR_READ;
36221 }
36222 pFile->szPage = iAmt;
36223 pgno = 1 + iOfst/iAmt;
36224 }else{
36225 pgno = 1;
36226 }
36227 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36228 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36229 if( got<0 ){
36230 n = 0;
36231 }else{
36232 aData[got] = 0;
36233 if( iOfst+iAmt<512 ){
36234 int k = iOfst+iAmt;
36235 aData[k*2] = 0;
36236 n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36237 if( n>=iOfst+iAmt ){
36238 memcpy(zBuf, &aData[2000+iOfst], iAmt);
36239 n = iAmt;
36240 }else{
36241 n = 0;
36242 }
36243 }else{
36244 n = kvvfsDecode(aData, zBuf, iAmt);
36245 }
36246 }
36247 if( n<iAmt ){
36248 memset(zBuf+n, 0, iAmt-n);
36249 return SQLITE_IOERR_SHORT_READ;
36250 }
36251 return SQLITE_OK;
36252 }
36253
36254
36255 /*
36256 ** Write into the -journal file.
36257 */
36258 static int kvvfsWriteJrnl(
36259 sqlite3_file *pProtoFile,
36260 const void *zBuf,
36261 int iAmt,
36262 sqlite_int64 iOfst
36263 ){
36264 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36265 sqlite3_int64 iEnd = iOfst+iAmt;
36266 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36267 if( iEnd>=0x10000000 ) return SQLITE_FULL;
36268 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36269 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36270 if( aNew==0 ){
36271 return SQLITE_IOERR_NOMEM;
36272 }
36273 pFile->aJrnl = aNew;
36274 if( pFile->nJrnl<iOfst ){
36275 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36276 }
36277 pFile->nJrnl = iEnd;
36278 }
36279 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36280 return SQLITE_OK;
36281 }
36282
36283 /*
36284 ** Write into the database file.
36285 */
36286 static int kvvfsWriteDb(
36287 sqlite3_file *pProtoFile,
36288 const void *zBuf,
36289 int iAmt,
36290 sqlite_int64 iOfst
36291 ){
36292 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36293 unsigned int pgno;
36294 char zKey[30];
36295 char aData[131073];
36296 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36297 assert( iAmt>=512 && iAmt<=65536 );
36298 assert( (iAmt & (iAmt-1))==0 );
36299 pgno = 1 + iOfst/iAmt;
36300 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36301 kvvfsEncode(zBuf, iAmt, aData);
36302 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36303 return SQLITE_IOERR;
36304 }
36305 if( iOfst+iAmt > pFile->szDb ){
36306 pFile->szDb = iOfst + iAmt;
36307 }
36308 return SQLITE_OK;
36309 }
36310
36311 /*
36312 ** Truncate an kvvfs-file.
36313 */
36314 static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
36315 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36316 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36317 assert( size==0 );
36318 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36319 sqlite3_free(pFile->aJrnl);
36320 pFile->aJrnl = 0;
36321 pFile->nJrnl = 0;
36322 return SQLITE_OK;
36323 }
36324 static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
36325 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36326 if( pFile->szDb>size
36327 && pFile->szPage>0
36328 && (size % pFile->szPage)==0
36329 ){
36330 char zKey[50];
36331 unsigned int pgno, pgnoMax;
36332 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36333 pgno = 1 + size/pFile->szPage;
36334 pgnoMax = 2 + pFile->szDb/pFile->szPage;
36335 while( pgno<=pgnoMax ){
36336 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36337 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36338 pgno++;
36339 }
36340 pFile->szDb = size;
36341 return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK;
36342 }
36343 return SQLITE_IOERR;
36344 }
36345
36346 /*
36347 ** Sync an kvvfs-file.
36348 */
36349 static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
36350 int i, n;
36351 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36352 char *zOut;
36353 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36354 if( pFile->nJrnl<=0 ){
36355 return kvvfsTruncateJrnl(pProtoFile, 0);
36356 }
36357 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36358 if( zOut==0 ){
36359 return SQLITE_IOERR_NOMEM;
36360 }
36361 n = pFile->nJrnl;
36362 i = 0;
36363 do{
36364 zOut[i++] = 'a' + (n%26);
36365 n /= 26;
36366 }while( n>0 );
36367 zOut[i++] = ' ';
36368 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36369 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36370 sqlite3_free(zOut);
36371 return i ? SQLITE_IOERR : SQLITE_OK;
36372 }
36373 static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
36374 return SQLITE_OK;
36375 }
36376
36377 /*
36378 ** Return the current file-size of an kvvfs-file.
36379 */
36380 static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36381 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36382 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36383 *pSize = pFile->nJrnl;
36384 return SQLITE_OK;
36385 }
36386 static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36387 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36388 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36389 if( pFile->szDb>=0 ){
36390 *pSize = pFile->szDb;
36391 }else{
36392 *pSize = kvvfsReadFileSize(pFile);
36393 }
36394 return SQLITE_OK;
36395 }
36396
36397 /*
36398 ** Lock an kvvfs-file.
36399 */
36400 static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
36401 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36402 assert( !pFile->isJournal );
36403 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36404
36405 if( eLock!=SQLITE_LOCK_NONE ){
36406 pFile->szDb = kvvfsReadFileSize(pFile);
36407 }
36408 return SQLITE_OK;
36409 }
36410
36411 /*
36412 ** Unlock an kvvfs-file.
36413 */
36414 static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
36415 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36416 assert( !pFile->isJournal );
36417 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36418 if( eLock==SQLITE_LOCK_NONE ){
36419 pFile->szDb = -1;
36420 }
36421 return SQLITE_OK;
36422 }
36423
36424 /*
36425 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36426 */
36427 static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
36428 SQLITE_KV_LOG(("xCheckReservedLock\n"));
36429 *pResOut = 0;
36430 return SQLITE_OK;
36431 }
36432
36433 /*
36434 ** File control method. For custom operations on an kvvfs-file.
36435 */
36436 static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
36437 SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
36438 return SQLITE_NOTFOUND;
36439 }
36440 static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
36441 SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
36442 if( op==SQLITE_FCNTL_SYNC ){
36443 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36444 int rc = SQLITE_OK;
36445 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36446 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36447 rc = SQLITE_IOERR;
36448 }
36449 return rc;
36450 }
36451 return SQLITE_NOTFOUND;
36452 }
36453
36454 /*
36455 ** Return the sector-size in bytes for an kvvfs-file.
36456 */
36457 static int kvvfsSectorSize(sqlite3_file *pFile){
36458 return 512;
36459 }
36460
36461 /*
36462 ** Return the device characteristic flags supported by an kvvfs-file.
36463 */
36464 static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
36465 return 0;
36466 }
36467
36468 /****** sqlite3_vfs methods *************************************************/
36469
36470 /*
36471 ** Open an kvvfs file handle.
36472 */
36473 static int kvvfsOpen(
36474 sqlite3_vfs *pProtoVfs,
36475 const char *zName,
36476 sqlite3_file *pProtoFile,
36477 int flags,
36478 int *pOutFlags
36479 ){
36480 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36481 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36482 if( strcmp(zName, "local")==0
36483 || strcmp(zName, "session")==0
36484 ){
36485 pFile->isJournal = 0;
36486 pFile->base.pMethods = &kvvfs_db_io_methods;
36487 }else
36488 if( strcmp(zName, "local-journal")==0
36489 || strcmp(zName, "session-journal")==0
36490 ){
36491 pFile->isJournal = 1;
36492 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36493 }else{
36494 return SQLITE_CANTOPEN;
36495 }
36496 if( zName[0]=='s' ){
36497 pFile->zClass = "session";
36498 }else{
36499 pFile->zClass = "local";
36500 }
36501 pFile->aJrnl = 0;
36502 pFile->nJrnl = 0;
36503 pFile->szPage = -1;
36504 pFile->szDb = -1;
36505 return SQLITE_OK;
36506 }
36507
36508 /*
36509 ** Delete the file located at zPath. If the dirSync argument is true,
36510 ** ensure the file-system modifications are synced to disk before
36511 ** returning.
36512 */
36513 static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
36514 if( strcmp(zPath, "local-journal")==0 ){
36515 sqlite3KvvfsMethods.xDelete("local", "jrnl");
36516 }else
36517 if( strcmp(zPath, "session-journal")==0 ){
36518 sqlite3KvvfsMethods.xDelete("session", "jrnl");
36519 }
36520 return SQLITE_OK;
36521 }
36522
36523 /*
36524 ** Test for access permissions. Return true if the requested permission
36525 ** is available, or false otherwise.
36526 */
36527 static int kvvfsAccess(
36528 sqlite3_vfs *pProtoVfs,
36529 const char *zPath,
36530 int flags,
36531 int *pResOut
36532 ){
36533 SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
36534 if( strcmp(zPath, "local-journal")==0 ){
36535 *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
36536 }else
36537 if( strcmp(zPath, "session-journal")==0 ){
36538 *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
36539 }else
36540 if( strcmp(zPath, "local")==0 ){
36541 *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
36542 }else
36543 if( strcmp(zPath, "session")==0 ){
36544 *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
36545 }else
36546 {
36547 *pResOut = 0;
36548 }
36549 SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
36550 return SQLITE_OK;
36551 }
36552
36553 /*
36554 ** Populate buffer zOut with the full canonical pathname corresponding
36555 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
36556 ** of at least (INST_MAX_PATHNAME+1) bytes.
36557 */
36558 static int kvvfsFullPathname(
36559 sqlite3_vfs *pVfs,
36560 const char *zPath,
36561 int nOut,
36562 char *zOut
36563 ){
36564 size_t nPath;
36565 #ifdef SQLITE_OS_KV_ALWAYS_LOCAL
36566 zPath = "local";
36567 #endif
36568 nPath = strlen(zPath);
36569 SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
36570 if( nOut<nPath+1 ) nPath = nOut - 1;
36571 memcpy(zOut, zPath, nPath);
36572 zOut[nPath] = 0;
36573 return SQLITE_OK;
36574 }
36575
36576 /*
36577 ** Open the dynamic library located at zPath and return a handle.
36578 */
36579 static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
36580 return 0;
36581 }
36582
36583 /*
36584 ** Populate the buffer pointed to by zBufOut with nByte bytes of
36585 ** random data.
36586 */
36587 static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
36588 memset(zBufOut, 0, nByte);
36589 return nByte;
36590 }
36591
36592 /*
36593 ** Sleep for nMicro microseconds. Return the number of microseconds
36594 ** actually slept.
36595 */
36596 static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
36597 return SQLITE_OK;
36598 }
36599
36600 /*
36601 ** Return the current time as a Julian Day number in *pTimeOut.
36602 */
36603 static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
36604 sqlite3_int64 i = 0;
36605 int rc;
36606 rc = kvvfsCurrentTimeInt64(0, &i);
36607 *pTimeOut = i/86400000.0;
36608 return rc;
36609 }
36610 #include <sys/time.h>
36611 static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
36612 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36613 struct timeval sNow;
36614 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
36615 *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
36616 return SQLITE_OK;
36617 }
36618 #endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
36619
36620 #if SQLITE_OS_KV
36621 /*
36622 ** This routine is called initialize the KV-vfs as the default VFS.
36623 */
36624 SQLITE_API int sqlite3_os_init(void){
36625 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
36626 }
36627 SQLITE_API int sqlite3_os_end(void){
36628 return SQLITE_OK;
36629 }
36630 #endif /* SQLITE_OS_KV */
36631
36632 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
36633 SQLITE_PRIVATE int sqlite3KvvfsInit(void){
36634 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
36635 }
36636 #endif
36637
36638 /************** End of os_kv.c ***********************************************/
36639 /************** Begin file os_unix.c *****************************************/
36640 /*
36641 ** 2004 May 22
36642 **
36643 ** The author disclaims copyright to this source code. In place of
@@ -35647,17 +36724,17 @@
36724 #endif
36725
36726 /*
36727 ** standard include files.
36728 */
36729 #include <sys/types.h> /* amalgamator: keep */
36730 #include <sys/stat.h> /* amalgamator: keep */
36731 #include <fcntl.h>
36732 #include <sys/ioctl.h>
36733 #include <unistd.h> /* amalgamator: keep */
36734 /* #include <time.h> */
36735 #include <sys/time.h> /* amalgamator: keep */
36736 #include <errno.h>
36737 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
36738 # include <sys/mman.h>
36739 #endif
36740
@@ -43625,10 +44702,13 @@
44702 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
44703 #else
44704 sqlite3_vfs_register(&aVfs[i], i==0);
44705 #endif
44706 }
44707 #ifdef SQLITE_OS_KV_OPTIONAL
44708 sqlite3KvvfsInit();
44709 #endif
44710 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
44711
44712 #ifndef SQLITE_OMIT_WAL
44713 /* Validate lock assumptions */
44714 assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
@@ -70722,10 +71802,13 @@
71802 put4byte(pCell+info.nSize-4, iTo);
71803 break;
71804 }
71805 }
71806 }else{
71807 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
71808 return SQLITE_CORRUPT_PAGE(pPage);
71809 }
71810 if( get4byte(pCell)==iFrom ){
71811 put4byte(pCell, iTo);
71812 break;
71813 }
71814 }
@@ -73056,18 +74139,11 @@
74139 }
74140 }
74141
74142 pPage = pCur->pPage;
74143 idx = ++pCur->ix;
74144 if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
 
 
 
 
 
 
 
74145 return SQLITE_CORRUPT_BKPT;
74146 }
74147
74148 if( idx>=pPage->nCell ){
74149 if( !pPage->leaf ){
@@ -75753,10 +76829,15 @@
76829 assert( pCur->pPage->nOverflow );
76830 }
76831 }else{
76832 break;
76833 }
76834 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
76835 /* The page being written is not a root page, and there is currently
76836 ** more than one reference to it. This only happens if the page is one
76837 ** of its own ancestor pages. Corruption. */
76838 rc = SQLITE_CORRUPT_BKPT;
76839 }else{
76840 MemPage * const pParent = pCur->apPage[iPage-1];
76841 int const iIdx = pCur->aiIdx[iPage-1];
76842
76843 rc = sqlite3PagerWrite(pParent->pDbPage);
@@ -79679,10 +80760,11 @@
80760 assert( MEM_Str==(MEM_Blob>>3) );
80761 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
80762 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
80763 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
80764 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
80765 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
80766 return sqlite3VdbeChangeEncoding(pMem, encoding);
80767 }
80768 }
80769 return SQLITE_OK;
80770 }
@@ -80813,10 +81895,13 @@
81895 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
81896 Mem *p = (Mem*)pVal;
81897 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
81898 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
81899 return p->n;
81900 }
81901 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
81902 return p->n;
81903 }
81904 if( (p->flags & MEM_Blob)!=0 ){
81905 if( p->flags & MEM_Zero ){
81906 return p->n + p->u.nZero;
81907 }else{
@@ -81983,10 +83068,22 @@
83068 }
83069 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
83070 assert( p->nOp>0 || p->db->mallocFailed );
83071 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
83072 }
83073
83074 /*
83075 ** If the previous opcode is an OP_Column that delivers results
83076 ** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
83077 ** opcode.
83078 */
83079 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
83080 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
83081 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
83082 pOp->p5 |= OPFLAG_TYPEOFARG;
83083 }
83084 }
83085
83086 /*
83087 ** Change the P2 operand of instruction addr so that it points to
83088 ** the address of the next instruction to be coded.
83089 */
@@ -85402,11 +86499,11 @@
86499 testcase( pRhs->flags & MEM_Int );
86500 testcase( pRhs->flags & MEM_IntReal );
86501 serial_type = aKey1[idx1];
86502 testcase( serial_type==12 );
86503 if( serial_type>=10 ){
86504 rc = serial_type==10 ? -1 : +1;
86505 }else if( serial_type==0 ){
86506 rc = -1;
86507 }else if( serial_type==7 ){
86508 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
86509 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
@@ -85427,11 +86524,11 @@
86524 if( serial_type>=10 ){
86525 /* Serial types 12 or greater are strings and blobs (greater than
86526 ** numbers). Types 10 and 11 are currently "reserved for future
86527 ** use", so it doesn't really matter what the results of comparing
86528 ** them to numberic values are. */
86529 rc = serial_type==10 ? -1 : +1;
86530 }else if( serial_type==0 ){
86531 rc = -1;
86532 }else{
86533 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
86534 if( serial_type==7 ){
@@ -85508,11 +86605,11 @@
86605 }
86606
86607 /* RHS is null */
86608 else{
86609 serial_type = aKey1[idx1];
86610 rc = (serial_type!=0 && serial_type!=10);
86611 }
86612
86613 if( rc!=0 ){
86614 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
86615 if( sortFlags ){
@@ -86457,10 +87554,13 @@
87554 }
87555 assert( eType == aType[pVal->flags&MEM_AffMask] );
87556 }
87557 #endif
87558 return aType[pVal->flags&MEM_AffMask];
87559 }
87560 SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
87561 return pVal->enc;
87562 }
87563
87564 /* Return true if a parameter to xUpdate represents an unchanged column */
87565 SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
87566 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
@@ -91094,23 +92194,94 @@
92194 goto jump_to_p2;
92195 }
92196 break;
92197 }
92198
92199 /* Opcode: IsType P1 P2 P3 P4 P5
92200 ** Synopsis: if typeof(P1.P3) in P5 goto P2
92201 **
92202 ** Jump to P2 if the type of a column in a btree is one of the types specified
92203 ** by the P5 bitmask.
92204 **
92205 ** P1 is normally a cursor on a btree for which the row decode cache is
92206 ** valid through at least column P3. In other words, there should have been
92207 ** a prior OP_Column for column P3 or greater. If the cursor is not valid,
92208 ** then this opcode might give spurious results.
92209 ** The the btree row has fewer than P3 columns, then use P4 as the
92210 ** datatype.
92211 **
92212 ** If P1 is -1, then P3 is a register number and the datatype is taken
92213 ** from the value in that register.
92214 **
92215 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
92216 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
92217 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
92218 **
92219 ** Take the jump to address P2 if and only if the datatype of the
92220 ** value determined by P1 and P3 corresponds to one of the bits in the
92221 ** P5 bitmask.
92222 **
92223 */
92224 case OP_IsType: { /* jump */
92225 VdbeCursor *pC;
92226 u16 typeMask;
92227 u32 serialType;
92228
92229 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
92230 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
92231 if( pOp->p1>=0 ){
92232 pC = p->apCsr[pOp->p1];
92233 assert( pC!=0 );
92234 assert( pOp->p3>=0 );
92235 if( pOp->p3<pC->nHdrParsed ){
92236 serialType = pC->aType[pOp->p3];
92237 if( serialType>=12 ){
92238 if( serialType&1 ){
92239 typeMask = 0x04; /* SQLITE_TEXT */
92240 }else{
92241 typeMask = 0x08; /* SQLITE_BLOB */
92242 }
92243 }else{
92244 static const unsigned char aMask[] = {
92245 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
92246 0x01, 0x01, 0x10, 0x10
92247 };
92248 testcase( serialType==0 );
92249 testcase( serialType==1 );
92250 testcase( serialType==2 );
92251 testcase( serialType==3 );
92252 testcase( serialType==4 );
92253 testcase( serialType==5 );
92254 testcase( serialType==6 );
92255 testcase( serialType==7 );
92256 testcase( serialType==8 );
92257 testcase( serialType==9 );
92258 testcase( serialType==10 );
92259 testcase( serialType==11 );
92260 typeMask = aMask[serialType];
92261 }
92262 }else{
92263 typeMask = 1 << (pOp->p4.i - 1);
92264 testcase( typeMask==0x01 );
92265 testcase( typeMask==0x02 );
92266 testcase( typeMask==0x04 );
92267 testcase( typeMask==0x08 );
92268 testcase( typeMask==0x10 );
92269 }
92270 }else{
92271 assert( memIsValid(&aMem[pOp->p3]) );
92272 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
92273 testcase( typeMask==0x01 );
92274 testcase( typeMask==0x02 );
92275 testcase( typeMask==0x04 );
92276 testcase( typeMask==0x08 );
92277 testcase( typeMask==0x10 );
92278 }
92279 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
92280 if( typeMask & pOp->p5 ){
92281 goto jump_to_p2;
92282 }
92283 break;
92284 }
92285
92286 /* Opcode: ZeroOrNull P1 P2 P3 * *
92287 ** Synopsis: r[P2] = 0 OR NULL
@@ -91207,23 +92378,25 @@
92378 ** Synopsis: r[P3]=PX cursor P1 column P2
92379 **
92380 ** Interpret the data that cursor P1 points to as a structure built using
92381 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
92382 ** information about the format of the data.) Extract the P2-th column
92383 ** from this record. If there are less than (P2+1)
92384 ** values in the record, extract a NULL.
92385 **
92386 ** The value extracted is stored in register P3.
92387 **
92388 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
92389 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
92390 ** the result.
92391 **
92392 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
92393 ** to only be used by the length() function or the equivalent. The content
92394 ** of large blobs is not loaded, thus saving CPU cycles. If the
92395 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
92396 ** typeof() function or the IS NULL or IS NOT NULL operators or the
92397 ** equivalent. In this case, all content loading can be omitted.
92398 */
92399 case OP_Column: {
92400 u32 p2; /* column number to retrieve */
92401 VdbeCursor *pC; /* The VDBE cursor */
92402 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
@@ -93158,11 +94331,17 @@
94331 assert( oc!=OP_SeekGE || r.default_rc==+1 );
94332 assert( oc!=OP_SeekLT || r.default_rc==+1 );
94333
94334 r.aMem = &aMem[pOp->p3];
94335 #ifdef SQLITE_DEBUG
94336 {
94337 int i;
94338 for(i=0; i<r.nField; i++){
94339 assert( memIsValid(&r.aMem[i]) );
94340 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
94341 }
94342 }
94343 #endif
94344 r.eqSeen = 0;
94345 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
94346 if( rc!=SQLITE_OK ){
94347 goto abort_due_to_error;
@@ -93221,54 +94400,76 @@
94400 }
94401 break;
94402 }
94403
94404
94405 /* Opcode: SeekScan P1 P2 * * P5
94406 ** Synopsis: Scan-ahead up to P1 rows
94407 **
94408 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
94409 ** opcode must be immediately followed by OP_SeekGE. This constraint is
94410 ** checked by assert() statements.
94411 **
94412 ** This opcode uses the P1 through P4 operands of the subsequent
94413 ** OP_SeekGE. In the text that follows, the operands of the subsequent
94414 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
94415 ** the P1, P2 and P5 operands of this opcode are also used, and are called
94416 ** This.P1, This.P2 and This.P5.
94417 **
94418 ** This opcode helps to optimize IN operators on a multi-column index
94419 ** where the IN operator is on the later terms of the index by avoiding
94420 ** unnecessary seeks on the btree, substituting steps to the next row
94421 ** of the b-tree instead. A correct answer is obtained if this opcode
94422 ** is omitted or is a no-op.
94423 **
94424 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
94425 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
94426 ** to. Call this SeekGE.P3/P4 row the "target".
94427 **
94428 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
94429 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
94430 **
94431 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
94432 ** might be the target row, or it might be near and slightly before the
94433 ** target row, or it might be after the target row. If the cursor is
94434 ** currently before the target row, then this opcode attempts to position
94435 ** the cursor on or after the target row by invoking sqlite3BtreeStep()
94436 ** on the cursor between 1 and This.P1 times.
94437 **
94438 ** The This.P5 parameter is a flag that indicates what to do if the
94439 ** cursor ends up pointing at a valid row that is past the target
94440 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
94441 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
94442 ** case occurs when there are no inequality constraints to the right of
94443 ** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
94444 ** occurs when there are inequality constraints to the right of the IN
94445 ** operator. In that case, the This.P2 will point either directly to or
94446 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
94447 ** loop terminate.
94448 **
94449 ** Possible outcomes from this opcode:<ol>
94450 **
94451 ** <li> If the cursor is initally not pointed to any valid row, then
94452 ** fall through into the subsequent OP_SeekGE opcode.
94453 **
94454 ** <li> If the cursor is left pointing to a row that is before the target
94455 ** row, even after making as many as This.P1 calls to
94456 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
94457 **
94458 ** <li> If the cursor is left pointing at the target row, either because it
94459 ** was at the target row to begin with or because one or more
94460 ** sqlite3BtreeNext() calls moved the cursor to the target row,
94461 ** then jump to This.P2..,
94462 **
94463 ** <li> If the cursor started out before the target row and a call to
94464 ** to sqlite3BtreeNext() moved the cursor off the end of the index
94465 ** (indicating that the target row definitely does not exist in the
94466 ** btree) then jump to SeekGE.P2, ending the loop.
94467 **
94468 ** <li> If the cursor ends up on a valid row that is past the target row
94469 ** (indicating that the target row does not exist in the btree) then
94470 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
94471 ** </ol>
94472 */
94473 case OP_SeekScan: {
94474 VdbeCursor *pC;
94475 int res;
@@ -93275,18 +94476,29 @@
94476 int nStep;
94477 UnpackedRecord r;
94478
94479 assert( pOp[1].opcode==OP_SeekGE );
94480
94481 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
94482 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
94483 ** opcode past the OP_SeekGE itself. */
94484 assert( pOp->p2>=(int)(pOp-aOp)+2 );
94485 #ifdef SQLITE_DEBUG
94486 if( pOp->p5==0 ){
94487 /* There are no inequality constraints following the IN constraint. */
94488 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
94489 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
94490 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
94491 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
94492 || aOp[pOp->p2-1].opcode==OP_IdxGE );
94493 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
94494 }else{
94495 /* There are inequality constraints. */
94496 assert( pOp->p2==(int)(pOp-aOp)+2 );
94497 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
94498 }
94499 #endif
94500
94501 assert( pOp->p1>0 );
94502 pC = p->apCsr[pOp[1].p1];
94503 assert( pC!=0 );
94504 assert( pC->eCurType==CURTYPE_BTREE );
@@ -93316,22 +94528,24 @@
94528 #endif
94529 res = 0; /* Not needed. Only used to silence a warning. */
94530 while(1){
94531 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
94532 if( rc ) goto abort_due_to_error;
94533 if( res>0 && pOp->p5==0 ){
94534 seekscan_search_fail:
94535 /* Jump to SeekGE.P2, ending the loop */
94536 #ifdef SQLITE_DEBUG
94537 if( db->flags&SQLITE_VdbeTrace ){
94538 printf("... %d steps and then skip\n", pOp->p1 - nStep);
94539 }
94540 #endif
94541 VdbeBranchTaken(1,3);
94542 pOp++;
94543 goto jump_to_p2;
94544 }
94545 if( res>=0 ){
94546 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
94547 #ifdef SQLITE_DEBUG
94548 if( db->flags&SQLITE_VdbeTrace ){
94549 printf("... %d steps and then success\n", pOp->p1 - nStep);
94550 }
94551 #endif
@@ -103867,13 +105081,12 @@
105081 }
105082 op = pExpr->op;
105083 if( op==TK_REGISTER ) op = pExpr->op2;
105084 if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
105085 assert( ExprUseYTab(pExpr) );
105086 assert( pExpr->y.pTab!=0 );
105087 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 
105088 }
105089 if( op==TK_SELECT ){
105090 assert( ExprUseXSelect(pExpr) );
105091 assert( pExpr->x.pSelect!=0 );
105092 assert( pExpr->x.pSelect->pEList!=0 );
@@ -103987,21 +105200,18 @@
105200 const Expr *p = pExpr;
105201 while( p ){
105202 int op = p->op;
105203 if( op==TK_REGISTER ) op = p->op2;
105204 if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
105205 int j;
105206 assert( ExprUseYTab(p) );
105207 assert( p->y.pTab!=0 );
105208 if( (j = p->iColumn)>=0 ){
105209 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
105210 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
105211 }
105212 break;
 
 
 
 
105213 }
105214 if( op==TK_CAST || op==TK_UPLUS ){
105215 p = p->pLeft;
105216 continue;
105217 }
@@ -107602,14 +108812,11 @@
108812 int iCol, /* Index of the column to extract */
108813 int regOut /* Extract the value into this register */
108814 ){
108815 Column *pCol;
108816 assert( v!=0 );
108817 assert( pTab!=0 );
 
 
 
108818 if( iCol<0 || iCol==pTab->iPKey ){
108819 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
108820 VdbeComment((v, "%s.rowid", pTab->zName));
108821 }else{
108822 int op;
@@ -107855,10 +109062,57 @@
109062 #endif /* !defined(SQLITE_UNTESTABLE) */
109063 }
109064 return target;
109065 }
109066
109067 /*
109068 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109069 ** If it is, then resolve the expression by reading from the index and
109070 ** return the register into which the value has been read. If pExpr is
109071 ** not an indexed expression, then return negative.
109072 */
109073 static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
109074 Parse *pParse, /* The parsing context */
109075 Expr *pExpr, /* The expression to potentially bypass */
109076 int target /* Where to store the result of the expression */
109077 ){
109078 IndexedExpr *p;
109079 Vdbe *v;
109080 for(p=pParse->pIdxExpr; p; p=p->pIENext){
109081 int iDataCur = p->iDataCur;
109082 if( iDataCur<0 ) continue;
109083 if( pParse->iSelfTab ){
109084 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109085 iDataCur = -1;
109086 }
109087 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
109088 v = pParse->pVdbe;
109089 assert( v!=0 );
109090 if( p->bMaybeNullRow ){
109091 /* If the index is on a NULL row due to an outer join, then we
109092 ** cannot extract the value from the index. The value must be
109093 ** computed using the original expression. */
109094 int addr = sqlite3VdbeCurrentAddr(v);
109095 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109096 VdbeCoverage(v);
109097 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109098 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109099 sqlite3VdbeGoto(v, 0);
109100 p = pParse->pIdxExpr;
109101 pParse->pIdxExpr = 0;
109102 sqlite3ExprCode(pParse, pExpr, target);
109103 pParse->pIdxExpr = p;
109104 sqlite3VdbeJumpHere(v, addr+2);
109105 }else{
109106 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109107 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109108 }
109109 return target;
109110 }
109111 return -1; /* Not found */
109112 }
109113
109114
109115 /*
109116 ** Generate code into the current Vdbe to evaluate the given
109117 ** expression. Attempt to store the results in register "target".
109118 ** Return the register where results are stored.
@@ -107883,10 +109137,15 @@
109137 assert( v!=0 );
109138
109139 expr_code_doover:
109140 if( pExpr==0 ){
109141 op = TK_NULL;
109142 }else if( pParse->pIdxExpr!=0
109143 && !ExprHasProperty(pExpr, EP_Leaf)
109144 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109145 ){
109146 return r1;
109147 }else{
109148 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
109149 op = pExpr->op;
109150 }
109151 switch( op ){
@@ -107928,15 +109187,12 @@
109187 ** constant.
109188 */
109189 int aff;
109190 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
109191 assert( ExprUseYTab(pExpr) );
109192 assert( pExpr->y.pTab!=0 );
109193 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 
 
 
109194 if( aff>SQLITE_AFF_BLOB ){
109195 static const char zAff[] = "B\000C\000D\000E";
109196 assert( SQLITE_AFF_BLOB=='A' );
109197 assert( SQLITE_AFF_TEXT=='B' );
109198 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
@@ -107994,16 +109250,14 @@
109250 ** in the index refer to the table to which the index belongs */
109251 iTab = pParse->iSelfTab - 1;
109252 }
109253 }
109254 assert( ExprUseYTab(pExpr) );
109255 assert( pExpr->y.pTab!=0 );
109256 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
109257 pExpr->iColumn, iTab, target,
109258 pExpr->op2);
 
 
 
109259 return iReg;
109260 }
109261 case TK_INTEGER: {
109262 codeInteger(pParse, pExpr, 0, target);
109263 return target;
@@ -109053,10 +110307,11 @@
110307 case TK_ISNULL:
110308 case TK_NOTNULL: {
110309 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
110310 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
110311 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110312 sqlite3VdbeTypeofColumn(v, r1);
110313 sqlite3VdbeAddOp2(v, op, r1, dest);
110314 VdbeCoverageIf(v, op==TK_ISNULL);
110315 VdbeCoverageIf(v, op==TK_NOTNULL);
110316 testcase( regFree1==0 );
110317 break;
@@ -109227,10 +110482,11 @@
110482 break;
110483 }
110484 case TK_ISNULL:
110485 case TK_NOTNULL: {
110486 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110487 sqlite3VdbeTypeofColumn(v, r1);
110488 sqlite3VdbeAddOp2(v, op, r1, dest);
110489 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
110490 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
110491 testcase( regFree1==0 );
110492 break;
@@ -109380,11 +110636,17 @@
110636 return 1;
110637 }
110638 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
110639 return 1;
110640 }
110641 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
110642 && pB->iTable<0 && pA->iTable==iTab
110643 ){
110644 /* fall through */
110645 }else{
110646 return 2;
110647 }
110648 }
110649 assert( !ExprHasProperty(pA, EP_IntValue) );
110650 assert( !ExprHasProperty(pB, EP_IntValue) );
110651 if( pA->u.zToken ){
110652 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
@@ -109682,14 +110944,14 @@
110944 /* The y.pTab=0 assignment in wherecode.c always happens after the
110945 ** impliesNotNullRow() test */
110946 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
110947 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
110948 if( (pLeft->op==TK_COLUMN
110949 && ALWAYS(pLeft->y.pTab!=0)
110950 && IsVirtual(pLeft->y.pTab))
110951 || (pRight->op==TK_COLUMN
110952 && ALWAYS(pRight->y.pTab!=0)
110953 && IsVirtual(pRight->y.pTab))
110954 ){
110955 return WRC_Prune;
110956 }
110957 /* no break */ deliberate_fall_through
@@ -113510,10 +114772,11 @@
114772 assert( k>=0 && k<pIdx->nColumn );
114773 i = pIdx->aiColumn[k];
114774 if( NEVER(i==XN_ROWID) ){
114775 VdbeComment((v,"%s.rowid",pIdx->zName));
114776 }else if( i==XN_EXPR ){
114777 assert( pIdx->bHasExpr );
114778 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
114779 }else{
114780 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
114781 }
114782 }
@@ -115825,11 +117088,11 @@
117088 if( p==0 ){
117089 #ifndef SQLITE_OMIT_VIRTUALTABLE
117090 /* If zName is the not the name of a table in the schema created using
117091 ** CREATE, then check to see if it is the name of an virtual table that
117092 ** can be an eponymous virtual table. */
117093 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
117094 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
117095 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
117096 pMod = sqlite3PragmaVtabRegister(db, zName);
117097 }
117098 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
@@ -115838,11 +117101,11 @@
117101 }
117102 }
117103 #endif
117104 if( flags & LOCATE_NOERR ) return 0;
117105 pParse->checkSchema = 1;
117106 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
117107 p = 0;
117108 }
117109
117110 if( p==0 ){
117111 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
@@ -117648,11 +118911,12 @@
118911 }
118912
118913 /* Recompute the colNotIdxed field of the Index.
118914 **
118915 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
118916 ** columns that are within the first 63 columns of the table and a 1 for
118917 ** all other bits (all columns that are not in the index). The
118918 ** high-order bit of colNotIdxed is always 1. All unindexed columns
118919 ** of the table have a 1.
118920 **
118921 ** 2019-10-24: For the purpose of this computation, virtual columns are
118922 ** not considered to be covered by the index, even if they are in the
@@ -117676,11 +118940,11 @@
118940 testcase( x==BMS-2 );
118941 if( x<BMS-1 ) m |= MASKBIT(x);
118942 }
118943 }
118944 pIdx->colNotIdxed = ~m;
118945 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
118946 }
118947
118948 /*
118949 ** This routine runs at the end of parsing a CREATE TABLE statement that
118950 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
@@ -119564,10 +120828,11 @@
120828 pList = 0;
120829 }
120830 j = XN_EXPR;
120831 pIndex->aiColumn[i] = XN_EXPR;
120832 pIndex->uniqNotNull = 0;
120833 pIndex->bHasExpr = 1;
120834 }else{
120835 j = pCExpr->iColumn;
120836 assert( j<=0x7fff );
120837 if( j<0 ){
120838 j = pTab->iPKey;
@@ -119575,10 +120840,11 @@
120840 if( pTab->aCol[j].notNull==0 ){
120841 pIndex->uniqNotNull = 0;
120842 }
120843 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
120844 pIndex->bHasVCol = 1;
120845 pIndex->bHasExpr = 1;
120846 }
120847 }
120848 pIndex->aiColumn[i] = (i16)j;
120849 }
120850 zColl = 0;
@@ -123352,11 +124618,11 @@
124618 **
124619 ** For a case-insensitive search, set variable cx to be the same as
124620 ** c but in the other case and search the input string for either
124621 ** c or cx.
124622 */
124623 if( c<0x80 ){
124624 char zStop[3];
124625 int bMatch;
124626 if( noCase ){
124627 zStop[0] = sqlite3Toupper(c);
124628 zStop[1] = sqlite3Tolower(c);
@@ -123435,19 +124701,31 @@
124701 /*
124702 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
124703 ** non-zero if there is no match.
124704 */
124705 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
124706 if( zString==0 ){
124707 return zGlobPattern!=0;
124708 }else if( zGlobPattern==0 ){
124709 return 1;
124710 }else {
124711 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124712 }
124713 }
124714
124715 /*
124716 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
124717 ** a miss - like strcmp().
124718 */
124719 SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
124720 if( zStr==0 ){
124721 return zPattern!=0;
124722 }else if( zPattern==0 ){
124723 return 1;
124724 }else{
124725 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124726 }
124727 }
124728
124729 /*
124730 ** Count the number of times that the LIKE operator (or GLOB which is
124731 ** just a variation of LIKE) gets called. This is used for testing
@@ -126572,10 +127850,11 @@
127850 aff = pTab->aCol[x].affinity;
127851 }else if( x==XN_ROWID ){
127852 aff = SQLITE_AFF_INTEGER;
127853 }else{
127854 assert( x==XN_EXPR );
127855 assert( pIdx->bHasExpr );
127856 assert( pIdx->aColExpr!=0 );
127857 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
127858 }
127859 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
127860 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
@@ -130146,10 +131425,12 @@
131425 int (*deserialize)(sqlite3*,const char*,unsigned char*,
131426 sqlite3_int64,sqlite3_int64,unsigned);
131427 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
131428 unsigned int);
131429 const char *(*db_name)(sqlite3*,int);
131430 /* Version 3.40.0 and later */
131431 int (*value_encoding)(sqlite3_value*);
131432 };
131433
131434 /*
131435 ** This is the function signature used for all extension entry points. It
131436 ** is also defined in the file "loadext.c".
@@ -130470,10 +131751,12 @@
131751 #ifndef SQLITE_OMIT_DESERIALIZE
131752 #define sqlite3_deserialize sqlite3_api->deserialize
131753 #define sqlite3_serialize sqlite3_api->serialize
131754 #endif
131755 #define sqlite3_db_name sqlite3_api->db_name
131756 /* Version 3.40.0 and later */
131757 #define sqlite3_value_encoding sqlite3_api->value_encoding
131758 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
131759
131760 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
131761 /* This case when the file really is being compiled as a loadable
131762 ** extension */
@@ -130982,11 +132265,13 @@
132265 sqlite3_serialize,
132266 #else
132267 0,
132268 0,
132269 #endif
132270 sqlite3_db_name,
132271 /* Version 3.40.0 and later */
132272 sqlite3_value_type
132273 };
132274
132275 /* True if x is the directory separator character
132276 */
132277 #if SQLITE_OS_WIN
@@ -133786,12 +135071,13 @@
135071 Index *pIdx, *pPk;
135072 Index *pPrior = 0; /* Previous index */
135073 int loopTop;
135074 int iDataCur, iIdxCur;
135075 int r1 = -1;
135076 int bStrict; /* True for a STRICT table */
135077 int r2; /* Previous key for WITHOUT ROWID tables */
135078 int mxCol; /* Maximum non-virtual column number */
135079
135080 if( !IsOrdinaryTable(pTab) ) continue;
135081 if( pObjTab && pObjTab!=pTab ) continue;
135082 if( isQuick || HasRowid(pTab) ){
135083 pPk = 0;
@@ -133812,15 +135098,26 @@
135098 }
135099 assert( pParse->nMem>=8+j );
135100 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
135101 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
135102 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
135103
135104 /* Fetch the right-most column from the table. This will cause
135105 ** the entire record header to be parsed and sanity checked. It
135106 ** will also prepopulate the cursor column cache that is used
135107 ** by the OP_IsType code, so it is a required step.
135108 */
135109 mxCol = pTab->nCol-1;
135110 while( mxCol>=0
135111 && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
135112 || pTab->iPKey==mxCol) ) mxCol--;
135113 if( mxCol>=0 ){
135114 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
135115 sqlite3VdbeTypeofColumn(v, 3);
135116 }
135117
135118 if( !isQuick ){
 
 
 
 
135119 if( pPk ){
135120 /* Verify WITHOUT ROWID keys are in ascending order */
135121 int a1;
135122 char *zErr;
135123 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
@@ -133836,48 +135133,126 @@
135133 for(j=0; j<pPk->nKeyCol; j++){
135134 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
135135 }
135136 }
135137 }
135138 /* Verify datatypes for all columns:
135139 **
135140 ** (1) NOT NULL columns may not contain a NULL
135141 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
135142 ** (3) Datatype for TEXT columns in non-STRICT tables must be
135143 ** NULL, TEXT, or BLOB.
135144 ** (4) Datatype for numeric columns in non-STRICT tables must not
135145 ** be a TEXT value that can be losslessly converted to numeric.
135146 */
135147 bStrict = (pTab->tabFlags & TF_Strict)!=0;
135148 for(j=0; j<pTab->nCol; j++){
135149 char *zErr;
135150 Column *pCol = pTab->aCol + j; /* The column to be checked */
135151 int labelError; /* Jump here to report an error */
135152 int labelOk; /* Jump here if all looks ok */
135153 int p1, p3, p4; /* Operands to the OP_IsType opcode */
135154 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
135155
135156 if( j==pTab->iPKey ) continue;
135157 if( bStrict ){
135158 doTypeCheck = pCol->eCType>COLTYPE_ANY;
135159 }else{
135160 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
 
135161 }
135162 if( pCol->notNull==0 && !doTypeCheck ) continue;
135163
135164 /* Compute the operands that will be needed for OP_IsType */
135165 p4 = SQLITE_NULL;
135166 if( pCol->colFlags & COLFLAG_VIRTUAL ){
135167 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135168 p1 = -1;
135169 p3 = 3;
135170 }else{
135171 if( pCol->iDflt ){
135172 sqlite3_value *pDfltValue = 0;
135173 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
135174 pCol->affinity, &pDfltValue);
135175 if( pDfltValue ){
135176 p4 = sqlite3_value_type(pDfltValue);
135177 sqlite3ValueFree(pDfltValue);
135178 }
135179 }
135180 p1 = iDataCur;
135181 if( !HasRowid(pTab) ){
135182 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
135183 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
135184 }else{
135185 p3 = sqlite3TableColumnToStorage(pTab,j);
135186 testcase( p3!=j);
135187 }
135188 }
135189
135190 labelError = sqlite3VdbeMakeLabel(pParse);
135191 labelOk = sqlite3VdbeMakeLabel(pParse);
135192 if( pCol->notNull ){
135193 /* (1) NOT NULL columns may not contain a NULL */
135194 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135195 sqlite3VdbeChangeP5(v, 0x0f);
135196 VdbeCoverage(v);
135197 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
135198 pCol->zCnName);
135199 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135200 if( doTypeCheck ){
135201 sqlite3VdbeGoto(v, labelError);
135202 sqlite3VdbeJumpHere(v, jmp2);
135203 }else{
135204 /* VDBE byte code will fall thru */
135205 }
 
135206 }
135207 if( bStrict && doTypeCheck ){
135208 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
135209 static unsigned char aStdTypeMask[] = {
135210 0x1f, /* ANY */
135211 0x18, /* BLOB */
135212 0x11, /* INT */
135213 0x11, /* INTEGER */
135214 0x13, /* REAL */
135215 0x14 /* TEXT */
135216 };
135217 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135218 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
135219 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
135220 VdbeCoverage(v);
135221 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
135222 sqlite3StdType[pCol->eCType-1],
135223 pTab->zName, pTab->aCol[j].zCnName);
135224 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135225 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
135226 /* (3) Datatype for TEXT columns in non-STRICT tables must be
135227 ** NULL, TEXT, or BLOB. */
135228 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135229 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135230 VdbeCoverage(v);
135231 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
135232 pTab->zName, pTab->aCol[j].zCnName);
135233 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135234 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
135235 /* (4) Datatype for numeric columns in non-STRICT tables must not
135236 ** be a TEXT value that can be converted to numeric. */
135237 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135238 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
135239 VdbeCoverage(v);
135240 if( p1>=0 ){
135241 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135242 }
135243 sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
135244 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
135245 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135246 VdbeCoverage(v);
135247 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
135248 pTab->zName, pTab->aCol[j].zCnName);
135249 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135250 }
135251 sqlite3VdbeResolveLabel(v, labelError);
135252 integrityCheckResultRow(v);
135253 sqlite3VdbeResolveLabel(v, labelOk);
135254 }
135255 /* Verify CHECK constraints */
135256 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
135257 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
135258 if( db->mallocFailed==0 ){
@@ -135493,11 +136868,11 @@
136868 */
136869 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
136870 sParse.disableLookaside++;
136871 DisableLookaside;
136872 }
136873 sParse.prepFlags = prepFlags & 0xff;
136874
136875 /* Check to verify that it is possible to get a read lock on all
136876 ** database schemas. The inability to get a read lock indicates that
136877 ** some other database connection is holding a write-lock, which in
136878 ** turn means that the other connection has made uncommitted changes
@@ -135534,11 +136909,13 @@
136909 }
136910 }
136911 }
136912 }
136913
136914 #ifndef SQLITE_OMIT_VIRTUALTABLE
136915 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
136916 #endif
136917
136918 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
136919 char *zSqlCopy;
136920 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
136921 testcase( nBytes==mxLen );
@@ -139603,10 +140980,11 @@
140980 Parse *pParse; /* The parsing context */
140981 int iTable; /* Replace references to this table */
140982 int iNewTable; /* New table number */
140983 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
140984 ExprList *pEList; /* Replacement expressions */
140985 ExprList *pCList; /* Collation sequences for replacement expr */
140986 } SubstContext;
140987
140988 /* Forward Declarations */
140989 static void substExprList(SubstContext*, ExprList*);
140990 static void substSelect(SubstContext*, Select*, int);
@@ -139644,13 +141022,14 @@
141022 pExpr->op = TK_NULL;
141023 }else
141024 #endif
141025 {
141026 Expr *pNew;
141027 int iColumn = pExpr->iColumn;
141028 Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
141029 Expr ifNullRow;
141030 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
141031 assert( pExpr->pRight==0 );
141032 if( sqlite3ExprIsVector(pCopy) ){
141033 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
141034 }else{
141035 sqlite3 *db = pSubst->pParse->db;
@@ -139684,15 +141063,20 @@
141063 ExprSetProperty(pExpr, EP_IntValue);
141064 }
141065
141066 /* Ensure that the expression now has an implicit collation sequence,
141067 ** just as it did when it was a column of a view or sub-query. */
141068 {
141069 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
141070 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
141071 pSubst->pCList->a[iColumn].pExpr
141072 );
141073 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
141074 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
141075 (pColl ? pColl->zName : "BINARY")
141076 );
141077 }
141078 }
141079 ExprClearProperty(pExpr, EP_Collate);
141080 }
141081 }
141082 }else{
@@ -139880,10 +141264,22 @@
141264 w.xExprCallback = renumberCursorsCb;
141265 w.xSelectCallback = sqlite3SelectWalkNoop;
141266 sqlite3WalkSelect(&w, p);
141267 }
141268 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
141269
141270 /*
141271 ** If pSel is not part of a compound SELECT, return a pointer to its
141272 ** expression list. Otherwise, return a pointer to the expression list
141273 ** of the leftmost SELECT in the compound.
141274 */
141275 static ExprList *findLeftmostExprlist(Select *pSel){
141276 while( pSel->pPrior ){
141277 pSel = pSel->pPrior;
141278 }
141279 return pSel->pEList;
141280 }
141281
141282 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
141283 /*
141284 ** This routine attempts to flatten subqueries as a performance optimization.
141285 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -140433,10 +141829,11 @@
141829 x.pParse = pParse;
141830 x.iTable = iParent;
141831 x.iNewTable = iNewParent;
141832 x.isOuterJoin = isOuterJoin;
141833 x.pEList = pSub->pEList;
141834 x.pCList = findLeftmostExprlist(pSub);
141835 substSelect(&x, pParent, 0);
141836 }
141837
141838 /* The flattened query is a compound if either the inner or the
141839 ** outer query is a compound. */
@@ -140452,11 +141849,11 @@
141849 if( pSub->pLimit ){
141850 pParent->pLimit = pSub->pLimit;
141851 pSub->pLimit = 0;
141852 }
141853
141854 /* Recompute the SrcItem.colUsed masks for the flattened
141855 ** tables. */
141856 for(i=0; i<nSubSrc; i++){
141857 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
141858 }
141859 }
@@ -140915,10 +142312,11 @@
142312 x.pParse = pParse;
142313 x.iTable = pSrc->iCursor;
142314 x.iNewTable = pSrc->iCursor;
142315 x.isOuterJoin = 0;
142316 x.pEList = pSubq->pEList;
142317 x.pCList = findLeftmostExprlist(pSubq);
142318 pNew = substExpr(&x, pNew);
142319 #ifndef SQLITE_OMIT_WINDOWFUNC
142320 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
142321 /* Restriction 6c has prevented push-down in this case */
142322 sqlite3ExprDelete(pParse->db, pNew);
@@ -141439,13 +142837,13 @@
142837 }
142838 }
142839 #endif
142840
142841 /*
142842 ** The SrcItem structure passed as the second argument represents a
142843 ** sub-query in the FROM clause of a SELECT statement. This function
142844 ** allocates and populates the SrcItem.pTab object. If successful,
142845 ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
142846 ** SQLITE_NOMEM.
142847 */
142848 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
142849 Select *pSel = pFrom->pSelect;
@@ -142274,11 +143672,11 @@
143672 #endif
143673 }
143674
143675 /*
143676 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
143677 ** If it is, then return the SrcItem for the prior view. If it is not,
143678 ** then return 0.
143679 */
143680 static SrcItem *isSelfJoinView(
143681 SrcList *pTabList, /* Search for self-joins in this FROM clause */
143682 SrcItem *pThis /* Search for prior reference to this subquery */
@@ -143318,11 +144716,11 @@
144716 ** in the right order to begin with.
144717 */
144718 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
144719 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
144720 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
144721 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144722 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
144723 );
144724 if( pWInfo==0 ){
144725 sqlite3ExprListDelete(db, pDistinct);
144726 goto select_end;
@@ -143617,11 +145015,11 @@
145015 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
145016 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
145017
145018 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
145019 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
145020 pDistinct, p, minMaxFlag|distFlag, 0);
145021 if( pWInfo==0 ){
145022 goto select_end;
145023 }
145024 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
145025 eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -145101,11 +146499,11 @@
146499 sSubParse.pTriggerTab = pTab;
146500 sSubParse.pToplevel = pTop;
146501 sSubParse.zAuthContext = pTrigger->zName;
146502 sSubParse.eTriggerOp = pTrigger->op;
146503 sSubParse.nQueryLoop = pParse->nQueryLoop;
146504 sSubParse.prepFlags = pParse->prepFlags;
146505
146506 v = sqlite3GetVdbe(&sSubParse);
146507 if( v ){
146508 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
146509 pTrigger->zName, onErrorText(orconf),
@@ -145447,15 +146845,18 @@
146845 ** (not a virtual table) then the value might have been stored as an
146846 ** integer. In that case, add an OP_RealAffinity opcode to make sure
146847 ** it has been converted into REAL.
146848 */
146849 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
146850 Column *pCol;
146851 assert( pTab!=0 );
146852 assert( pTab->nCol>i );
146853 pCol = &pTab->aCol[i];
146854 if( pCol->iDflt ){
146855 sqlite3_value *pValue = 0;
146856 u8 enc = ENC(sqlite3VdbeDb(v));
146857 assert( !IsView(pTab) );
146858 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
146859 assert( i<pTab->nCol );
146860 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
146861 sqlite3ColumnExpr(pTab,pCol), enc,
146862 pCol->affinity, &pValue);
@@ -145462,11 +146863,11 @@
146863 if( pValue ){
146864 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
146865 }
146866 }
146867 #ifndef SQLITE_OMIT_FLOATING_POINT
146868 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
146869 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
146870 }
146871 #endif
146872 }
146873
@@ -146902,10 +148303,11 @@
148303 Expr *pExpr;
148304 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
148305 if( pIdx->aiColumn[ii]==XN_EXPR ){
148306 assert( pIdx->aColExpr!=0 );
148307 assert( pIdx->aColExpr->nExpr>ii );
148308 assert( pIdx->bHasExpr );
148309 pExpr = pIdx->aColExpr->a[ii].pExpr;
148310 if( pExpr->op!=TK_COLLATE ){
148311 sCol[0].pLeft = pExpr;
148312 pExpr = &sCol[0];
148313 }
@@ -147215,10 +148617,11 @@
148617 int isMemDb; /* True if vacuuming a :memory: database */
148618 int nRes; /* Bytes of reserved space at the end of each page */
148619 int nDb; /* Number of attached databases */
148620 const char *zDbMain; /* Schema name of database to vacuum */
148621 const char *zOut; /* Name of output file */
148622 u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
148623
148624 if( !db->autoCommit ){
148625 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
148626 return SQLITE_ERROR; /* IMP: R-12218-18073 */
148627 }
@@ -147286,16 +148689,21 @@
148689 rc = SQLITE_ERROR;
148690 sqlite3SetString(pzErrMsg, db, "output file already exists");
148691 goto end_of_vacuum;
148692 }
148693 db->mDbFlags |= DBFLAG_VacuumInto;
148694
148695 /* For a VACUUM INTO, the pager-flags are set to the same values as
148696 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
148697 ** is always set. */
148698 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
148699 }
148700 nRes = sqlite3BtreeGetRequestedReserve(pMain);
148701
148702 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
148703 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
148704 sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL);
148705
148706 /* Begin a transaction and take an exclusive lock on the main database
148707 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
148708 ** to ensure that we do not try to change the page-size on a WAL database.
148709 */
@@ -148605,11 +150013,11 @@
150013 /* Check to see the left operand is a column in a virtual table */
150014 if( NEVER(pExpr==0) ) return pDef;
150015 if( pExpr->op!=TK_COLUMN ) return pDef;
150016 assert( ExprUseYTab(pExpr) );
150017 pTab = pExpr->y.pTab;
150018 if( NEVER(pTab==0) ) return pDef;
150019 if( !IsVirtual(pTab) ) return pDef;
150020 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
150021 assert( pVtab!=0 );
150022 assert( pVtab->pModule!=0 );
150023 pMod = (sqlite3_module *)pVtab->pModule;
@@ -149212,11 +150620,11 @@
150620 /*
150621 ** An instance of the following structure keeps track of a mapping
150622 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
150623 **
150624 ** The VDBE cursor numbers are small integers contained in
150625 ** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
150626 ** clause, the cursor numbers might not begin with 0 and they might
150627 ** contain gaps in the numbering sequence. But we want to make maximum
150628 ** use of the bits in our bitmasks. This structure provides a mapping
150629 ** from the sparse cursor numbers into consecutive integers beginning
150630 ** with 0.
@@ -149283,24 +150691,10 @@
150691 #endif
150692 #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
150693 # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
150694 #endif
150695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150696 /*
150697 ** The WHERE clause processing routine has two halves. The
150698 ** first part does the start of the WHERE loop and the second
150699 ** half does the tail of the WHERE loop. An instance of
150700 ** this structure is returned by the first half and passed
@@ -149312,14 +150706,14 @@
150706 struct WhereInfo {
150707 Parse *pParse; /* Parsing and code generating context */
150708 SrcList *pTabList; /* List of tables in the join */
150709 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
150710 ExprList *pResultSet; /* Result set of the query */
150711 #if WHERETRACE_ENABLED
150712 Expr *pWhere; /* The complete WHERE clause */
 
 
150713 #endif
150714 Select *pSelect; /* The entire SELECT statement containing WHERE */
150715 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
150716 int iContinue; /* Jump here to continue with next record */
150717 int iBreak; /* Jump here to break out of the loop */
150718 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
150719 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -149334,11 +150728,10 @@
150728 unsigned sorted :1; /* True if really sorted (not just grouped) */
150729 LogEst nRowOut; /* Estimated number of output rows */
150730 int iTop; /* The very beginning of the WHERE loop */
150731 int iEndWhere; /* End of the WHERE clause itself */
150732 WhereLoop *pLoops; /* List of all WhereLoop objects */
 
150733 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
150734 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
150735 WhereClause sWC; /* Decomposition of the WHERE clause */
150736 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
150737 WhereLevel a[1]; /* Information about each nest loop in WHERE */
@@ -150686,147 +152079,10 @@
152079 assert( nReg==1 || pParse->nErr );
152080 sqlite3ExprCode(pParse, p, iReg);
152081 }
152082 }
152083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152084 /*
152085 ** The pTruth expression is always true because it is the WHERE clause
152086 ** a partial index that is driving a query loop. Look through all of the
152087 ** WHERE clause terms on the query, and if any of those terms must be
152088 ** true because pTruth is true, then mark those WHERE clause terms as
@@ -150891,10 +152147,12 @@
152147 assert( pTerm!=0 );
152148 assert( pTerm->pExpr!=0 );
152149 testcase( pTerm->wtFlags & TERM_VIRTUAL );
152150 regRowid = sqlite3GetTempReg(pParse);
152151 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
152152 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
152153 VdbeCoverage(pParse->pVdbe);
152154 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
152155 addrNxt, regRowid, 1);
152156 VdbeCoverage(pParse->pVdbe);
152157 }else{
152158 u16 nEq = pLoop->u.btree.nEq;
@@ -151042,13 +152300,13 @@
152300 codeExprOrVector(pParse, pRight, iTarget, 1);
152301 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
152302 && pLoop->u.vtab.bOmitOffset
152303 ){
152304 assert( pTerm->eOperator==WO_AUX );
152305 assert( pWInfo->pSelect!=0 );
152306 assert( pWInfo->pSelect->iOffset>0 );
152307 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
152308 VdbeComment((v,"Zero OFFSET counter"));
152309 }
152310 }
152311 }
152312 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
@@ -151152,10 +152410,12 @@
152410 iReleaseReg = ++pParse->nMem;
152411 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
152412 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
152413 addrNxt = pLevel->addrNxt;
152414 if( pLevel->regFilter ){
152415 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
152416 VdbeCoverage(v);
152417 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
152418 iRowidReg, 1);
152419 VdbeCoverage(v);
152420 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
152421 }
@@ -151503,10 +152763,15 @@
152763 ** of entries in the tree, so basing the number of steps to try
152764 ** on the estimated number of rows in the btree seems like a good
152765 ** guess. */
152766 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
152767 (pIdx->aiRowLogEst[0]+9)/10);
152768 if( pRangeStart ){
152769 sqlite3VdbeChangeP5(v, 1);
152770 sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
152771 addrSeekScan = 0;
152772 }
152773 VdbeCoverage(v);
152774 }
152775 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
152776 VdbeCoverage(v);
152777 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
@@ -151641,31 +152906,10 @@
152906 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
152907 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
152908 }
152909
152910 if( pLevel->iLeftJoin==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152911 /* If a partial index is driving the loop, try to eliminate WHERE clause
152912 ** terms from the query that must be true due to the WHERE clause of
152913 ** the partial index.
152914 **
152915 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
@@ -151774,11 +153018,11 @@
153018 */
153019 if( pWInfo->nLevel>1 ){
153020 int nNotReady; /* The number of notReady tables */
153021 SrcItem *origSrc; /* Original list of tables */
153022 nNotReady = pWInfo->nLevel - iLevel - 1;
153023 pOrTab = sqlite3DbMallocRawNN(db,
153024 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
153025 if( pOrTab==0 ) return notReady;
153026 pOrTab->nAlloc = (u8)(nNotReady + 1);
153027 pOrTab->nSrc = pOrTab->nAlloc;
153028 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
@@ -152027,11 +153271,11 @@
153271 ** indent everything in between the this point and the final OP_Return.
153272 ** See tag-20220407a in vdbe.c and shell.c */
153273 assert( pLevel->op==OP_Return );
153274 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153275
153276 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
153277 if( !untestedTerms ) disableTerm(pLevel, pTerm);
153278 }else
153279 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
153280
153281 {
@@ -152655,11 +153899,11 @@
153899 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
153900 */
153901 if( pLeft->op!=TK_COLUMN
153902 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
153903 || (ALWAYS( ExprUseYTab(pLeft) )
153904 && ALWAYS(pLeft->y.pTab)
153905 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
153906 ){
153907 int isNum;
153908 double rDummy;
153909 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
@@ -152772,12 +154016,11 @@
154016 **
154017 ** vtab_column MATCH expression
154018 ** MATCH(expression,vtab_column)
154019 */
154020 pCol = pList->a[1].pExpr;
154021 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
 
154022 if( ExprIsVtab(pCol) ){
154023 for(i=0; i<ArraySize(aOp); i++){
154024 assert( !ExprHasProperty(pExpr, EP_IntValue) );
154025 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
154026 *peOp2 = aOp[i].eOp2;
@@ -152798,11 +154041,11 @@
154041 ** names. But for this use case, xFindFunction is expected to deal
154042 ** with function names in an arbitrary case.
154043 */
154044 pCol = pList->a[0].pExpr;
154045 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
154046 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
154047 if( ExprIsVtab(pCol) ){
154048 sqlite3_vtab *pVtab;
154049 sqlite3_module *pMod;
154050 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
154051 void *pNotUsed;
@@ -152823,17 +154066,16 @@
154066 }
154067 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
154068 int res = 0;
154069 Expr *pLeft = pExpr->pLeft;
154070 Expr *pRight = pExpr->pRight;
154071 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
 
154072 if( ExprIsVtab(pLeft) ){
154073 res++;
154074 }
154075 assert( pRight==0 || pRight->op!=TK_COLUMN
154076 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
154077 if( pRight && ExprIsVtab(pRight) ){
154078 res++;
154079 SWAP(Expr*, pLeft, pRight);
154080 }
154081 *ppLeft = pLeft;
@@ -153378,10 +154620,11 @@
154620 iCur = pFrom->a[i].iCursor;
154621 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154622 if( pIdx->aColExpr==0 ) continue;
154623 for(i=0; i<pIdx->nKeyCol; i++){
154624 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
154625 assert( pIdx->bHasExpr );
154626 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
154627 aiCurCol[0] = iCur;
154628 aiCurCol[1] = XN_EXPR;
154629 return 1;
154630 }
@@ -153991,13 +155234,13 @@
155234 **
155235 ** LIMIT and OFFSET terms are ignored by most of the planner code. They
155236 ** exist only so that they may be passed to the xBestIndex method of the
155237 ** single virtual table in the FROM clause of the SELECT.
155238 */
155239 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
155240 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
155241 if( p->pGroupBy==0
155242 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
155243 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
155244 ){
155245 ExprList *pOrderBy = p->pOrderBy;
155246 int iCsr = p->pSrc->a[0].iCursor;
@@ -156530,30 +157773,18 @@
157773 while( pWInfo->pLoops ){
157774 WhereLoop *p = pWInfo->pLoops;
157775 pWInfo->pLoops = p->pNextLoop;
157776 whereLoopDelete(db, p);
157777 }
 
157778 while( pWInfo->pMemToFree ){
157779 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
157780 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
157781 pWInfo->pMemToFree = pNext;
157782 }
157783 sqlite3DbNNFreeNN(db, pWInfo);
157784 }
157785
 
 
 
 
 
 
 
 
 
 
 
157786 /*
157787 ** Return TRUE if all of the following are true:
157788 **
157789 ** (1) X has the same or lower cost, or returns the same or fewer rows,
157790 ** than Y.
@@ -157501,10 +158732,98 @@
158732 return 1;
158733 }
158734 }
158735 return 0;
158736 }
158737
158738 /*
158739 ** Structure passed to the whereIsCoveringIndex Walker callback.
158740 */
158741 struct CoveringIndexCheck {
158742 Index *pIdx; /* The index */
158743 int iTabCur; /* Cursor number for the corresponding table */
158744 };
158745
158746 /*
158747 ** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
158748 **
158749 ** If the Expr node references the table with cursor pCk->iTabCur, then
158750 ** make sure that column is covered by the index pCk->pIdx. We know that
158751 ** all columns less than 63 (really BMS-1) are covered, so we don't need
158752 ** to check them. But we do need to check any column at 63 or greater.
158753 **
158754 ** If the index does not cover the column, then set pWalk->eCode to
158755 ** non-zero and return WRC_Abort to stop the search.
158756 **
158757 ** If this node does not disprove that the index can be a covering index,
158758 ** then just return WRC_Continue, to continue the search.
158759 */
158760 static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158761 int i; /* Loop counter */
158762 const Index *pIdx; /* The index of interest */
158763 const i16 *aiColumn; /* Columns contained in the index */
158764 u16 nColumn; /* Number of columns in the index */
158765 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158766 if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158767 if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158768 pIdx = pWalk->u.pCovIdxCk->pIdx;
158769 aiColumn = pIdx->aiColumn;
158770 nColumn = pIdx->nColumn;
158771 for(i=0; i<nColumn; i++){
158772 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158773 }
158774 pWalk->eCode = 1;
158775 return WRC_Abort;
158776 }
158777
158778
158779 /*
158780 ** pIdx is an index that covers all of the low-number columns used by
158781 ** pWInfo->pSelect (columns from 0 through 62). But there are columns
158782 ** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158783 ** of whether pIdx covers *all* columns in the query.
158784 **
158785 ** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158786 ** not a covering index or if we are unable to determine if pIdx is a
158787 ** covering index.
158788 **
158789 ** This routine is an optimization. It is always safe to return non-zero.
158790 ** But returning zero when non-zero should have been returned can lead to
158791 ** incorrect bytecode and assertion faults.
158792 */
158793 static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158794 WhereInfo *pWInfo, /* The WHERE clause context */
158795 Index *pIdx, /* Index that is being tested */
158796 int iTabCur /* Cursor for the table being indexed */
158797 ){
158798 int i;
158799 struct CoveringIndexCheck ck;
158800 Walker w;
158801 if( pWInfo->pSelect==0 ){
158802 /* We don't have access to the full query, so we cannot check to see
158803 ** if pIdx is covering. Assume it is not. */
158804 return 1;
158805 }
158806 for(i=0; i<pIdx->nColumn; i++){
158807 if( pIdx->aiColumn[i]>=BMS-1 ) break;
158808 }
158809 if( i>=pIdx->nColumn ){
158810 /* pIdx does not index any columns greater than 62, but we know from
158811 ** colMask that columns greater than 62 are used, so this is not a
158812 ** covering index */
158813 return 1;
158814 }
158815 ck.pIdx = pIdx;
158816 ck.iTabCur = iTabCur;
158817 memset(&w, 0, sizeof(w));
158818 w.xExprCallback = whereIsCoveringIndexWalkCallback;
158819 w.xSelectCallback = sqlite3SelectWalkNoop;
158820 w.u.pCovIdxCk = &ck;
158821 w.eCode = 0;
158822 sqlite3WalkSelect(&w, pWInfo->pSelect);
158823 return w.eCode;
158824 }
158825
158826 /*
158827 ** Add all WhereLoop objects for a single table of the join where the table
158828 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
158829 ** a b-tree table, not a virtual table.
@@ -157719,10 +159038,13 @@
159038 if( pProbe->isCovering ){
159039 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159040 m = 0;
159041 }else{
159042 m = pSrc->colUsed & pProbe->colNotIdxed;
159043 if( m==TOPBIT ){
159044 m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159045 }
159046 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
159047 }
159048
159049 /* Full scan via index */
159050 if( b
@@ -158944,11 +160266,10 @@
160266 */
160267 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
160268 int mxChoice; /* Maximum number of simultaneous paths tracked */
160269 int nLoop; /* Number of terms in the join */
160270 Parse *pParse; /* Parsing context */
 
160271 int iLoop; /* Loop counter over the terms of the join */
160272 int ii, jj; /* Loop counters */
160273 int mxI = 0; /* Index of next entry to replace */
160274 int nOrderBy; /* Number of ORDER BY clause terms */
160275 LogEst mxCost = 0; /* Maximum cost of a set of paths */
@@ -158963,11 +160284,10 @@
160284 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
160285 char *pSpace; /* Temporary memory used by this routine */
160286 int nSpace; /* Bytes of space allocated at pSpace */
160287
160288 pParse = pWInfo->pParse;
 
160289 nLoop = pWInfo->nLevel;
160290 /* TUNING: For simple queries, only the best path is tracked.
160291 ** For 2-way joins, the 5 best paths are followed.
160292 ** For joins of 3 or more tables, track the 10 best paths */
160293 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
@@ -158986,11 +160306,11 @@
160306 }
160307
160308 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
160309 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
160310 nSpace += sizeof(LogEst) * nOrderBy;
160311 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
160312 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
160313 aTo = (WherePath*)pSpace;
160314 aFrom = aTo+mxChoice;
160315 memset(aFrom, 0, sizeof(aFrom[0]));
160316 pX = (WhereLoop**)(aFrom+mxChoice);
@@ -159244,11 +160564,11 @@
160564 nFrom = nTo;
160565 }
160566
160567 if( nFrom==0 ){
160568 sqlite3ErrorMsg(pParse, "no query solution");
160569 sqlite3StackFreeNN(pParse->db, pSpace);
160570 return SQLITE_ERROR;
160571 }
160572
160573 /* Find the lowest cost path. pFrom will be left pointing to that path */
160574 pFrom = aFrom;
@@ -159326,12 +160646,11 @@
160646
160647
160648 pWInfo->nRowOut = pFrom->nRow;
160649
160650 /* Free temporary memory and return success */
160651 sqlite3StackFreeNN(pParse->db, pSpace);
 
160652 return SQLITE_OK;
160653 }
160654
160655 /*
160656 ** Most queries use only a single table (they are not joins) and have
@@ -159625,10 +160944,81 @@
160944 }
160945 }
160946 nSearch += pLoop->nOut;
160947 }
160948 }
160949
160950 /*
160951 ** This is an sqlite3ParserAddCleanup() callback that is invoked to
160952 ** free the Parse->pIdxExpr list when the Parse object is destroyed.
160953 */
160954 static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
160955 Parse *pParse = (Parse*)pObject;
160956 while( pParse->pIdxExpr!=0 ){
160957 IndexedExpr *p = pParse->pIdxExpr;
160958 pParse->pIdxExpr = p->pIENext;
160959 sqlite3ExprDelete(db, p->pExpr);
160960 sqlite3DbFreeNN(db, p);
160961 }
160962 }
160963
160964 /*
160965 ** The index pIdx is used by a query and contains one or more expressions.
160966 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
160967 ** number for the index and iDataCur is the cursor number for the corresponding
160968 ** table.
160969 **
160970 ** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
160971 ** each of the expressions in the index so that the expression code generator
160972 ** will know to replace occurrences of the indexed expression with
160973 ** references to the corresponding column of the index.
160974 */
160975 static SQLITE_NOINLINE void whereAddIndexedExpr(
160976 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
160977 Index *pIdx, /* The index-on-expression that contains the expressions */
160978 int iIdxCur, /* Cursor number for pIdx */
160979 SrcItem *pTabItem /* The FROM clause entry for the table */
160980 ){
160981 int i;
160982 IndexedExpr *p;
160983 Table *pTab;
160984 assert( pIdx->bHasExpr );
160985 pTab = pIdx->pTable;
160986 for(i=0; i<pIdx->nColumn; i++){
160987 Expr *pExpr;
160988 int j = pIdx->aiColumn[i];
160989 int bMaybeNullRow;
160990 if( j==XN_EXPR ){
160991 pExpr = pIdx->aColExpr->a[i].pExpr;
160992 testcase( pTabItem->fg.jointype & JT_LEFT );
160993 testcase( pTabItem->fg.jointype & JT_RIGHT );
160994 testcase( pTabItem->fg.jointype & JT_LTORJ );
160995 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
160996 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
160997 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
160998 bMaybeNullRow = 0;
160999 }else{
161000 continue;
161001 }
161002 if( sqlite3ExprIsConstant(pExpr) ) continue;
161003 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
161004 if( p==0 ) break;
161005 p->pIENext = pParse->pIdxExpr;
161006 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
161007 p->iDataCur = pTabItem->iCursor;
161008 p->iIdxCur = iIdxCur;
161009 p->iIdxCol = i;
161010 p->bMaybeNullRow = bMaybeNullRow;
161011 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
161012 p->zIdxName = pIdx->zName;
161013 #endif
161014 pParse->pIdxExpr = p;
161015 if( p->pIENext==0 ){
161016 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161017 }
161018 }
161019 }
161020
161021 /*
161022 ** Generate the beginning of the loop used for WHERE clause processing.
161023 ** The return value is a pointer to an opaque structure that contains
161024 ** information needed to terminate the loop. Later, the calling routine
@@ -159720,11 +161110,11 @@
161110 Parse *pParse, /* The parser context */
161111 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
161112 Expr *pWhere, /* The WHERE clause */
161113 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
161114 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
161115 Select *pSelect, /* The entire SELECT statement */
161116 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
161117 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
161118 ** If WHERE_USE_LIMIT, then the limit amount */
161119 ){
161120 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -159789,21 +161179,21 @@
161179 goto whereBeginError;
161180 }
161181 pWInfo->pParse = pParse;
161182 pWInfo->pTabList = pTabList;
161183 pWInfo->pOrderBy = pOrderBy;
161184 #if WHERETRACE_ENABLED
161185 pWInfo->pWhere = pWhere;
161186 #endif
161187 pWInfo->pResultSet = pResultSet;
161188 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
161189 pWInfo->nLevel = nTabList;
161190 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
161191 pWInfo->wctrlFlags = wctrlFlags;
161192 pWInfo->iLimit = iAuxArg;
161193 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
161194 pWInfo->pSelect = pSelect;
 
 
161195 memset(&pWInfo->nOBSat, 0,
161196 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
161197 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
161198 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
161199 pMaskSet = &pWInfo->sMaskSet;
@@ -159868,11 +161258,13 @@
161258 #endif
161259 }
161260
161261 /* Analyze all of the subexpressions. */
161262 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
161263 if( pSelect && pSelect->pLimit ){
161264 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
161265 }
161266 if( pParse->nErr ) goto whereBeginError;
161267
161268 /* Special case: WHERE terms that do not refer to any tables in the join
161269 ** (constant expressions). Evaluate each such term, and jump over all the
161270 ** generated code if the result is not true.
@@ -160171,10 +161563,13 @@
161563 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
161564 iIndexCur = iAuxArg;
161565 op = OP_ReopenIdx;
161566 }else{
161567 iIndexCur = pParse->nTab++;
161568 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
161569 whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
161570 }
161571 }
161572 pLevel->iIdxCur = iIndexCur;
161573 assert( pIx!=0 );
161574 assert( pIx->pSchema==pTab->pSchema );
161575 assert( iIndexCur>=0 );
@@ -160293,12 +161688,10 @@
161688 return pWInfo;
161689
161690 /* Jump here if malloc fails */
161691 whereBeginError:
161692 if( pWInfo ){
 
 
161693 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
161694 whereInfoFree(db, pWInfo);
161695 }
161696 return 0;
161697 }
@@ -160513,11 +161906,10 @@
161906 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
161907 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
161908 }
161909
161910 assert( pWInfo->nLevel<=pTabList->nSrc );
 
161911 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
161912 int k, last;
161913 VdbeOp *pOp, *pLastOp;
161914 Index *pIdx = 0;
161915 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
@@ -160566,10 +161958,20 @@
161958 ){
161959 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
161960 last = iEnd;
161961 }else{
161962 last = pWInfo->iEndWhere;
161963 }
161964 if( pIdx->bHasExpr ){
161965 IndexedExpr *p = pParse->pIdxExpr;
161966 while( p ){
161967 if( p->iIdxCur==pLevel->iIdxCur ){
161968 p->iDataCur = -1;
161969 p->iIdxCur = -1;
161970 }
161971 p = p->pIENext;
161972 }
161973 }
161974 k = pLevel->addrBody + 1;
161975 #ifdef SQLITE_DEBUG
161976 if( db->flags & SQLITE_VdbeAddopTrace ){
161977 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
@@ -174293,10 +175695,23 @@
175695 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
175696 if( db->mallocFailed ){
175697 goto opendb_out;
175698 }
175699
175700 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
175701 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
175702 if( zFilename && zFilename[0]==':' ){
175703 if( strcmp(zFilename, ":localStorage:")==0 ){
175704 zFilename = "file:local?vfs=kvvfs";
175705 flags |= SQLITE_OPEN_URI;
175706 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
175707 zFilename = "file:session?vfs=kvvfs";
175708 flags |= SQLITE_OPEN_URI;
175709 }
175710 }
175711 #endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
175712
175713 /* Parse the filename/URI argument
175714 **
175715 ** Only allow sensible combinations of bits in the flags argument.
175716 ** Throw an error if any non-sense combination is used. If we
175717 ** do not block illegal combinations here, it could trigger
@@ -175722,12 +177137,12 @@
177137 ** Recover as many snapshots as possible from the wal file associated with
177138 ** schema zDb of database db.
177139 */
177140 SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
177141 int rc = SQLITE_ERROR;
 
177142 #ifndef SQLITE_OMIT_WAL
177143 int iDb;
177144
177145 #ifdef SQLITE_ENABLE_API_ARMOR
177146 if( !sqlite3SafetyCheckOk(db) ){
177147 return SQLITE_MISUSE_BKPT;
177148 }
@@ -201626,11 +203041,11 @@
203041 return SQLITE_LOCKED_VTAB;
203042 }
203043 rtreeReference(pRtree);
203044 assert(nData>=1);
203045
203046 memset(&cell, 0, sizeof(cell));
203047
203048 /* Constraint handling. A write operation on an r-tree table may return
203049 ** SQLITE_CONSTRAINT for two reasons:
203050 **
203051 ** 1. A duplicate rowid value, or
@@ -236984,11 +238399,11 @@
238399 int nArg, /* Number of args */
238400 sqlite3_value **apUnused /* Function arguments */
238401 ){
238402 assert( nArg==0 );
238403 UNUSED_PARAM2(nArg, apUnused);
238404 sqlite3_result_text(pCtx, "fts5: 2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910", -1, SQLITE_TRANSIENT);
238405 }
238406
238407 /*
238408 ** Return true if zName is the extension on one of the shadow tables used
238409 ** by this module.
238410
+33 -12
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.40.0"
150150
#define SQLITE_VERSION_NUMBER 3040000
151
-#define SQLITE_SOURCE_ID "2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646"
151
+#define SQLITE_SOURCE_ID "2022-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -668,17 +668,21 @@
668668
/*
669669
** CAPI3REF: File Locking Levels
670670
**
671671
** SQLite uses one of these integer values as the second
672672
** argument to calls it makes to the xLock() and xUnlock() methods
673
-** of an [sqlite3_io_methods] object.
673
+** of an [sqlite3_io_methods] object. These values are ordered from
674
+** lest restrictive to most restrictive.
675
+**
676
+** The argument to xLock() is always SHARED or higher. The argument to
677
+** xUnlock is either SHARED or NONE.
674678
*/
675
-#define SQLITE_LOCK_NONE 0
676
-#define SQLITE_LOCK_SHARED 1
677
-#define SQLITE_LOCK_RESERVED 2
678
-#define SQLITE_LOCK_PENDING 3
679
-#define SQLITE_LOCK_EXCLUSIVE 4
679
+#define SQLITE_LOCK_NONE 0 /* xUnlock() only */
680
+#define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */
681
+#define SQLITE_LOCK_RESERVED 2 /* xLock() only */
682
+#define SQLITE_LOCK_PENDING 3 /* xLock() only */
683
+#define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */
680684
681685
/*
682686
** CAPI3REF: Synchronization Type Flags
683687
**
684688
** When SQLite invokes the xSync() method of an
@@ -752,11 +756,18 @@
752756
** <li> [SQLITE_LOCK_SHARED],
753757
** <li> [SQLITE_LOCK_RESERVED],
754758
** <li> [SQLITE_LOCK_PENDING], or
755759
** <li> [SQLITE_LOCK_EXCLUSIVE].
756760
** </ul>
757
-** xLock() increases the lock. xUnlock() decreases the lock.
761
+** xLock() upgrades the database file lock. In other words, xLock() moves the
762
+** database file lock in the direction NONE toward EXCLUSIVE. The argument to
763
+** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
764
+** SQLITE_LOCK_NONE. If the database file lock is already at or above the
765
+** requested lock, then the call to xLock() is a no-op.
766
+** xUnlock() downgrades the database file lock to either SHARED or NONE.
767
+* If the lock is already at or below the requested lock state, then the call
768
+** to xUnlock() is a no-op.
758769
** The xCheckReservedLock() method checks whether any database connection,
759770
** either in this process or in some other process, is holding a RESERVED,
760771
** PENDING, or EXCLUSIVE lock on the file. It returns true
761772
** if such a lock exists and false otherwise.
762773
**
@@ -857,13 +868,12 @@
857868
** <li>[[SQLITE_FCNTL_LOCKSTATE]]
858869
** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
859870
** opcode causes the xFileControl method to write the current state of
860871
** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
861872
** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
862
-** into an integer that the pArg argument points to. This capability
863
-** is used during testing and is only available when the SQLITE_TEST
864
-** compile-time option is used.
873
+** into an integer that the pArg argument points to.
874
+** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
865875
**
866876
** <li>[[SQLITE_FCNTL_SIZE_HINT]]
867877
** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
868878
** layer a hint of how large the database file will grow to be during the
869879
** current transaction. This hint is not guaranteed to be accurate but it
@@ -5509,10 +5519,20 @@
55095519
** such a conversion is possible without loss of information (in other
55105520
** words, if the value is a string that looks like a number)
55115521
** then the conversion is performed. Otherwise no conversion occurs.
55125522
** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
55135523
**
5524
+** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5525
+** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5526
+** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5527
+** returns something other than SQLITE_TEXT, then the return value from
5528
+** sqlite3_value_encoding(X) is meaningless. ^Calls to
5529
+** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5530
+** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5531
+** sqlite3_value_bytes16(X) might change the encoding of the value X and
5532
+** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5533
+**
55145534
** ^Within the [xUpdate] method of a [virtual table], the
55155535
** sqlite3_value_nochange(X) interface returns true if and only if
55165536
** the column corresponding to X is unchanged by the UPDATE operation
55175537
** that the xUpdate method call was invoked to implement and if
55185538
** and the prior [xColumn] method call that was invoked to extracted
@@ -5573,10 +5593,11 @@
55735593
SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
55745594
SQLITE_API int sqlite3_value_type(sqlite3_value*);
55755595
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
55765596
SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
55775597
SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5598
+SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
55785599
55795600
/*
55805601
** CAPI3REF: Finding The Subtype Of SQL Values
55815602
** METHOD: sqlite3_value
55825603
**
@@ -5626,11 +5647,11 @@
56265647
** In those cases, sqlite3_aggregate_context() might be called for the
56275648
** first time from within xFinal().)^
56285649
**
56295650
** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
56305651
** when first called if N is less than or equal to zero or if a memory
5631
-** allocate error occurs.
5652
+** allocation error occurs.
56325653
**
56335654
** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
56345655
** determined by the N parameter on first successful call. Changing the
56355656
** value of N in any subsequent call to sqlite3_aggregate_context() within
56365657
** the same aggregate function instance will not resize the memory
56375658
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
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-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -668,17 +668,21 @@
668 /*
669 ** CAPI3REF: File Locking Levels
670 **
671 ** SQLite uses one of these integer values as the second
672 ** argument to calls it makes to the xLock() and xUnlock() methods
673 ** of an [sqlite3_io_methods] object.
 
 
 
 
674 */
675 #define SQLITE_LOCK_NONE 0
676 #define SQLITE_LOCK_SHARED 1
677 #define SQLITE_LOCK_RESERVED 2
678 #define SQLITE_LOCK_PENDING 3
679 #define SQLITE_LOCK_EXCLUSIVE 4
680
681 /*
682 ** CAPI3REF: Synchronization Type Flags
683 **
684 ** When SQLite invokes the xSync() method of an
@@ -752,11 +756,18 @@
752 ** <li> [SQLITE_LOCK_SHARED],
753 ** <li> [SQLITE_LOCK_RESERVED],
754 ** <li> [SQLITE_LOCK_PENDING], or
755 ** <li> [SQLITE_LOCK_EXCLUSIVE].
756 ** </ul>
757 ** xLock() increases the lock. xUnlock() decreases the lock.
 
 
 
 
 
 
 
758 ** The xCheckReservedLock() method checks whether any database connection,
759 ** either in this process or in some other process, is holding a RESERVED,
760 ** PENDING, or EXCLUSIVE lock on the file. It returns true
761 ** if such a lock exists and false otherwise.
762 **
@@ -857,13 +868,12 @@
857 ** <li>[[SQLITE_FCNTL_LOCKSTATE]]
858 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
859 ** opcode causes the xFileControl method to write the current state of
860 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
861 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
862 ** into an integer that the pArg argument points to. This capability
863 ** is used during testing and is only available when the SQLITE_TEST
864 ** compile-time option is used.
865 **
866 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
867 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
868 ** layer a hint of how large the database file will grow to be during the
869 ** current transaction. This hint is not guaranteed to be accurate but it
@@ -5509,10 +5519,20 @@
5509 ** such a conversion is possible without loss of information (in other
5510 ** words, if the value is a string that looks like a number)
5511 ** then the conversion is performed. Otherwise no conversion occurs.
5512 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5513 **
 
 
 
 
 
 
 
 
 
 
5514 ** ^Within the [xUpdate] method of a [virtual table], the
5515 ** sqlite3_value_nochange(X) interface returns true if and only if
5516 ** the column corresponding to X is unchanged by the UPDATE operation
5517 ** that the xUpdate method call was invoked to implement and if
5518 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5573,10 +5593,11 @@
5573 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5574 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5575 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5576 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5577 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
 
5578
5579 /*
5580 ** CAPI3REF: Finding The Subtype Of SQL Values
5581 ** METHOD: sqlite3_value
5582 **
@@ -5626,11 +5647,11 @@
5626 ** In those cases, sqlite3_aggregate_context() might be called for the
5627 ** first time from within xFinal().)^
5628 **
5629 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5630 ** when first called if N is less than or equal to zero or if a memory
5631 ** allocate error occurs.
5632 **
5633 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5634 ** determined by the N parameter on first successful call. Changing the
5635 ** value of N in any subsequent call to sqlite3_aggregate_context() within
5636 ** the same aggregate function instance will not resize the memory
5637
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
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-10-26 06:05:44 6f2a40d06dd9a56491d27928ed3785e08308c7dcea2b3f768097fc98ba91a910"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -668,17 +668,21 @@
668 /*
669 ** CAPI3REF: File Locking Levels
670 **
671 ** SQLite uses one of these integer values as the second
672 ** argument to calls it makes to the xLock() and xUnlock() methods
673 ** of an [sqlite3_io_methods] object. These values are ordered from
674 ** lest restrictive to most restrictive.
675 **
676 ** The argument to xLock() is always SHARED or higher. The argument to
677 ** xUnlock is either SHARED or NONE.
678 */
679 #define SQLITE_LOCK_NONE 0 /* xUnlock() only */
680 #define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */
681 #define SQLITE_LOCK_RESERVED 2 /* xLock() only */
682 #define SQLITE_LOCK_PENDING 3 /* xLock() only */
683 #define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */
684
685 /*
686 ** CAPI3REF: Synchronization Type Flags
687 **
688 ** When SQLite invokes the xSync() method of an
@@ -752,11 +756,18 @@
756 ** <li> [SQLITE_LOCK_SHARED],
757 ** <li> [SQLITE_LOCK_RESERVED],
758 ** <li> [SQLITE_LOCK_PENDING], or
759 ** <li> [SQLITE_LOCK_EXCLUSIVE].
760 ** </ul>
761 ** xLock() upgrades the database file lock. In other words, xLock() moves the
762 ** database file lock in the direction NONE toward EXCLUSIVE. The argument to
763 ** xLock() is always on of SHARED, RESERVED, PENDING, or EXCLUSIVE, never
764 ** SQLITE_LOCK_NONE. If the database file lock is already at or above the
765 ** requested lock, then the call to xLock() is a no-op.
766 ** xUnlock() downgrades the database file lock to either SHARED or NONE.
767 * If the lock is already at or below the requested lock state, then the call
768 ** to xUnlock() is a no-op.
769 ** The xCheckReservedLock() method checks whether any database connection,
770 ** either in this process or in some other process, is holding a RESERVED,
771 ** PENDING, or EXCLUSIVE lock on the file. It returns true
772 ** if such a lock exists and false otherwise.
773 **
@@ -857,13 +868,12 @@
868 ** <li>[[SQLITE_FCNTL_LOCKSTATE]]
869 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
870 ** opcode causes the xFileControl method to write the current state of
871 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
872 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
873 ** into an integer that the pArg argument points to.
874 ** This capability is only available if SQLite is compiled with [SQLITE_DEBUG].
 
875 **
876 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
877 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
878 ** layer a hint of how large the database file will grow to be during the
879 ** current transaction. This hint is not guaranteed to be accurate but it
@@ -5509,10 +5519,20 @@
5519 ** such a conversion is possible without loss of information (in other
5520 ** words, if the value is a string that looks like a number)
5521 ** then the conversion is performed. Otherwise no conversion occurs.
5522 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5523 **
5524 ** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8],
5525 ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current encoding
5526 ** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X)
5527 ** returns something other than SQLITE_TEXT, then the return value from
5528 ** sqlite3_value_encoding(X) is meaningless. ^Calls to
5529 ** sqlite3_value_text(X), sqlite3_value_text16(X), sqlite3_value_text16be(X),
5530 ** sqlite3_value_text16le(X), sqlite3_value_bytes(X), or
5531 ** sqlite3_value_bytes16(X) might change the encoding of the value X and
5532 ** thus change the return from subsequent calls to sqlite3_value_encoding(X).
5533 **
5534 ** ^Within the [xUpdate] method of a [virtual table], the
5535 ** sqlite3_value_nochange(X) interface returns true if and only if
5536 ** the column corresponding to X is unchanged by the UPDATE operation
5537 ** that the xUpdate method call was invoked to implement and if
5538 ** and the prior [xColumn] method call that was invoked to extracted
@@ -5573,10 +5593,11 @@
5593 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5594 SQLITE_API int sqlite3_value_type(sqlite3_value*);
5595 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5596 SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5597 SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5598 SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5599
5600 /*
5601 ** CAPI3REF: Finding The Subtype Of SQL Values
5602 ** METHOD: sqlite3_value
5603 **
@@ -5626,11 +5647,11 @@
5647 ** In those cases, sqlite3_aggregate_context() might be called for the
5648 ** first time from within xFinal().)^
5649 **
5650 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5651 ** when first called if N is less than or equal to zero or if a memory
5652 ** allocation error occurs.
5653 **
5654 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5655 ** determined by the N parameter on first successful call. Changing the
5656 ** value of N in any subsequent call to sqlite3_aggregate_context() within
5657 ** the same aggregate function instance will not resize the memory
5658

Keyboard Shortcuts

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