Fossil SCM

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

drh 2022-10-26 11:14 trunk merge
Commit 4aa78375055222d156e98949e7c4ddbf2eca6c81a460c2c7e09b100f8a1c8a1b
+1 -1
--- Makefile.in
+++ Makefile.in
@@ -46,11 +46,11 @@
4646
4747
CFLAGS = @CFLAGS@
4848
CFLAGS_INCLUDE = @CFLAGS_INCLUDE@
4949
LIB = @LDFLAGS@ @EXTRA_LDFLAGS@ @LIBS@
5050
BCCFLAGS = @CPPFLAGS@ $(CFLAGS)
51
-TCCFLAGS = @EXTRA_CFLAGS@ @CPPFLAGS@ $(CFLAGS) -DHAVE_AUTOCONFIG_H -D_HAVE_SQLITE_CONFIG_H
51
+TCCFLAGS = @EXTRA_CFLAGS@ @CPPFLAGS@ $(CFLAGS) -DHAVE_AUTOCONFIG_H
5252
#
5353
# Fuzzing may be enable by appending -fsanitize=fuzzer -DFOSSIL_FUZZ
5454
# to the TCCFLAGS variable.
5555
# For more thorouth (but also slower) investigation
5656
# -fsanitize=fuzzer,undefined,address
5757
--- Makefile.in
+++ Makefile.in
@@ -46,11 +46,11 @@
46
47 CFLAGS = @CFLAGS@
48 CFLAGS_INCLUDE = @CFLAGS_INCLUDE@
49 LIB = @LDFLAGS@ @EXTRA_LDFLAGS@ @LIBS@
50 BCCFLAGS = @CPPFLAGS@ $(CFLAGS)
51 TCCFLAGS = @EXTRA_CFLAGS@ @CPPFLAGS@ $(CFLAGS) -DHAVE_AUTOCONFIG_H -D_HAVE_SQLITE_CONFIG_H
52 #
53 # Fuzzing may be enable by appending -fsanitize=fuzzer -DFOSSIL_FUZZ
54 # to the TCCFLAGS variable.
55 # For more thorouth (but also slower) investigation
56 # -fsanitize=fuzzer,undefined,address
57
--- Makefile.in
+++ Makefile.in
@@ -46,11 +46,11 @@
46
47 CFLAGS = @CFLAGS@
48 CFLAGS_INCLUDE = @CFLAGS_INCLUDE@
49 LIB = @LDFLAGS@ @EXTRA_LDFLAGS@ @LIBS@
50 BCCFLAGS = @CPPFLAGS@ $(CFLAGS)
51 TCCFLAGS = @EXTRA_CFLAGS@ @CPPFLAGS@ $(CFLAGS) -DHAVE_AUTOCONFIG_H
52 #
53 # Fuzzing may be enable by appending -fsanitize=fuzzer -DFOSSIL_FUZZ
54 # to the TCCFLAGS variable.
55 # For more thorouth (but also slower) investigation
56 # -fsanitize=fuzzer,undefined,address
57
+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 -710
--- 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 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
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
*/
@@ -20324,11 +20420,10 @@
2032420420
#ifndef SQLITE_AMALGAMATION
2032520421
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
2032620422
SQLITE_PRIVATE const char sqlite3StrBINARY[];
2032720423
SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[];
2032820424
SQLITE_PRIVATE const char sqlite3StdTypeAffinity[];
20329
-SQLITE_PRIVATE const char sqlite3StdTypeMap[];
2033020425
SQLITE_PRIVATE const char *sqlite3StdType[];
2033120426
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
2033220427
SQLITE_PRIVATE const unsigned char *sqlite3aLTb;
2033320428
SQLITE_PRIVATE const unsigned char *sqlite3aEQb;
2033420429
SQLITE_PRIVATE const unsigned char *sqlite3aGTb;
@@ -20767,10 +20862,14 @@
2076720862
SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
2076820863
2076920864
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2077020865
SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
2077120866
#endif
20867
+
20868
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20869
+SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20870
+#endif
2077220871
2077320872
#endif /* SQLITEINT_H */
2077420873
2077520874
/************** End of sqliteInt.h *******************************************/
2077620875
/************** Begin file os_common.h ***************************************/
@@ -20999,11 +21098,11 @@
2099921098
/*
2100021099
** Include the configuration header output by 'configure' if we're using the
2100121100
** autoconf-based build
2100221101
*/
2100321102
#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21004
-/* #include "config.h" */
21103
+/* #include "sqlite_cfg.h" */
2100521104
#define SQLITECONFIG_H 1
2100621105
#endif
2100721106
2100821107
/* These macros are provided to "stringify" the value of the define
2100921108
** for those options in which the value is meaningful. */
@@ -21164,10 +21263,13 @@
2116421263
"DISABLE_PAGECACHE_OVERFLOW_STATS",
2116521264
#endif
2116621265
#ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
2116721266
"DISABLE_SKIPAHEAD_DISTINCT",
2116821267
#endif
21268
+#ifdef SQLITE_DQS
21269
+ "DQS=" CTIMEOPT_VAL(SQLITE_DQS),
21270
+#endif
2116921271
#ifdef SQLITE_ENABLE_8_3_NAMES
2117021272
"ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
2117121273
#endif
2117221274
#ifdef SQLITE_ENABLE_API_ARMOR
2117321275
"ENABLE_API_ARMOR",
@@ -22133,14 +22235,10 @@
2213322235
** sqlite3StdTypeLen[] The length (in bytes) of each entry
2213422236
** in sqlite3StdType[].
2213522237
**
2213622238
** sqlite3StdTypeAffinity[] The affinity associated with each entry
2213722239
** in sqlite3StdType[].
22138
-**
22139
-** sqlite3StdTypeMap[] The type value (as returned from
22140
-** sqlite3_column_type() or sqlite3_value_type())
22141
-** for each entry in sqlite3StdType[].
2214222240
*/
2214322241
SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
2214422242
SQLITE_PRIVATE const char sqlite3StdTypeAffinity[] = {
2214522243
SQLITE_AFF_NUMERIC,
2214622244
SQLITE_AFF_BLOB,
@@ -22147,18 +22245,10 @@
2214722245
SQLITE_AFF_INTEGER,
2214822246
SQLITE_AFF_INTEGER,
2214922247
SQLITE_AFF_REAL,
2215022248
SQLITE_AFF_TEXT
2215122249
};
22152
-SQLITE_PRIVATE const char sqlite3StdTypeMap[] = {
22153
- 0,
22154
- SQLITE_BLOB,
22155
- SQLITE_INTEGER,
22156
- SQLITE_INTEGER,
22157
- SQLITE_FLOAT,
22158
- SQLITE_TEXT
22159
-};
2216022250
SQLITE_PRIVATE const char *sqlite3StdType[] = {
2216122251
"ANY",
2216222252
"BLOB",
2216322253
"INT",
2216422254
"INTEGER",
@@ -24740,13 +24830,15 @@
2474024830
DO_OS_MALLOC_TEST(id);
2474124831
return id->pMethods->xFileSize(id, pSize);
2474224832
}
2474324833
SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
2474424834
DO_OS_MALLOC_TEST(id);
24835
+ assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE );
2474524836
return id->pMethods->xLock(id, lockType);
2474624837
}
2474724838
SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24839
+ assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED );
2474824840
return id->pMethods->xUnlock(id, lockType);
2474924841
}
2475024842
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
2475124843
DO_OS_MALLOC_TEST(id);
2475224844
return id->pMethods->xCheckReservedLock(id, pResOut);
@@ -35382,11 +35474,11 @@
3538235474
/* 13 */ "MustBeInt" OpHelp(""),
3538335475
/* 14 */ "Jump" OpHelp(""),
3538435476
/* 15 */ "Once" OpHelp(""),
3538535477
/* 16 */ "If" OpHelp(""),
3538635478
/* 17 */ "IfNot" OpHelp(""),
35387
- /* 18 */ "IsNullOrType" OpHelp("if typeof(r[P1]) IN (P3,5) goto P2"),
35479
+ /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
3538835480
/* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
3538935481
/* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
3539035482
/* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
3539135483
/* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
3539235484
/* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
@@ -35557,10 +35649,982 @@
3555735649
return azName[i];
3555835650
}
3555935651
#endif
3556035652
3556135653
/************** End of opcodes.c *********************************************/
35654
+/************** Begin file os_kv.c *******************************************/
35655
+/*
35656
+** 2022-09-06
35657
+**
35658
+** The author disclaims copyright to this source code. In place of
35659
+** a legal notice, here is a blessing:
35660
+**
35661
+** May you do good and not evil.
35662
+** May you find forgiveness for yourself and forgive others.
35663
+** May you share freely, never taking more than you give.
35664
+**
35665
+******************************************************************************
35666
+**
35667
+** This file contains an experimental VFS layer that operates on a
35668
+** Key/Value storage engine where both keys and values must be pure
35669
+** text.
35670
+*/
35671
+/* #include <sqliteInt.h> */
35672
+#if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL))
35673
+
35674
+/*****************************************************************************
35675
+** Debugging logic
35676
+*/
35677
+
35678
+/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
35679
+#if 0
35680
+#define SQLITE_KV_TRACE(X) printf X
35681
+#else
35682
+#define SQLITE_KV_TRACE(X)
35683
+#endif
35684
+
35685
+/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
35686
+#if 0
35687
+#define SQLITE_KV_LOG(X) printf X
35688
+#else
35689
+#define SQLITE_KV_LOG(X)
35690
+#endif
35691
+
35692
+
35693
+/*
35694
+** Forward declaration of objects used by this VFS implementation
35695
+*/
35696
+typedef struct KVVfsFile KVVfsFile;
35697
+
35698
+/* A single open file. There are only two files represented by this
35699
+** VFS - the database and the rollback journal.
35700
+*/
35701
+struct KVVfsFile {
35702
+ sqlite3_file base; /* IO methods */
35703
+ const char *zClass; /* Storage class */
35704
+ int isJournal; /* True if this is a journal file */
35705
+ unsigned int nJrnl; /* Space allocated for aJrnl[] */
35706
+ char *aJrnl; /* Journal content */
35707
+ int szPage; /* Last known page size */
35708
+ sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35709
+};
35710
+
35711
+/*
35712
+** Methods for KVVfsFile
35713
+*/
35714
+static int kvvfsClose(sqlite3_file*);
35715
+static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35716
+static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35717
+static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35718
+static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35719
+static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
35720
+static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
35721
+static int kvvfsSyncDb(sqlite3_file*, int flags);
35722
+static int kvvfsSyncJrnl(sqlite3_file*, int flags);
35723
+static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
35724
+static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
35725
+static int kvvfsLock(sqlite3_file*, int);
35726
+static int kvvfsUnlock(sqlite3_file*, int);
35727
+static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
35728
+static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
35729
+static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
35730
+static int kvvfsSectorSize(sqlite3_file*);
35731
+static int kvvfsDeviceCharacteristics(sqlite3_file*);
35732
+
35733
+/*
35734
+** Methods for sqlite3_vfs
35735
+*/
35736
+static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
35737
+static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
35738
+static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
35739
+static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
35740
+static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
35741
+static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
35742
+static int kvvfsSleep(sqlite3_vfs*, int microseconds);
35743
+static int kvvfsCurrentTime(sqlite3_vfs*, double*);
35744
+static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
35745
+
35746
+static sqlite3_vfs sqlite3OsKvvfsObject = {
35747
+ 1, /* iVersion */
35748
+ sizeof(KVVfsFile), /* szOsFile */
35749
+ 1024, /* mxPathname */
35750
+ 0, /* pNext */
35751
+ "kvvfs", /* zName */
35752
+ 0, /* pAppData */
35753
+ kvvfsOpen, /* xOpen */
35754
+ kvvfsDelete, /* xDelete */
35755
+ kvvfsAccess, /* xAccess */
35756
+ kvvfsFullPathname, /* xFullPathname */
35757
+ kvvfsDlOpen, /* xDlOpen */
35758
+ 0, /* xDlError */
35759
+ 0, /* xDlSym */
35760
+ 0, /* xDlClose */
35761
+ kvvfsRandomness, /* xRandomness */
35762
+ kvvfsSleep, /* xSleep */
35763
+ kvvfsCurrentTime, /* xCurrentTime */
35764
+ 0, /* xGetLastError */
35765
+ kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
35766
+};
35767
+
35768
+/* Methods for sqlite3_file objects referencing a database file
35769
+*/
35770
+static sqlite3_io_methods kvvfs_db_io_methods = {
35771
+ 1, /* iVersion */
35772
+ kvvfsClose, /* xClose */
35773
+ kvvfsReadDb, /* xRead */
35774
+ kvvfsWriteDb, /* xWrite */
35775
+ kvvfsTruncateDb, /* xTruncate */
35776
+ kvvfsSyncDb, /* xSync */
35777
+ kvvfsFileSizeDb, /* xFileSize */
35778
+ kvvfsLock, /* xLock */
35779
+ kvvfsUnlock, /* xUnlock */
35780
+ kvvfsCheckReservedLock, /* xCheckReservedLock */
35781
+ kvvfsFileControlDb, /* xFileControl */
35782
+ kvvfsSectorSize, /* xSectorSize */
35783
+ kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35784
+ 0, /* xShmMap */
35785
+ 0, /* xShmLock */
35786
+ 0, /* xShmBarrier */
35787
+ 0, /* xShmUnmap */
35788
+ 0, /* xFetch */
35789
+ 0 /* xUnfetch */
35790
+};
35791
+
35792
+/* Methods for sqlite3_file objects referencing a rollback journal
35793
+*/
35794
+static sqlite3_io_methods kvvfs_jrnl_io_methods = {
35795
+ 1, /* iVersion */
35796
+ kvvfsClose, /* xClose */
35797
+ kvvfsReadJrnl, /* xRead */
35798
+ kvvfsWriteJrnl, /* xWrite */
35799
+ kvvfsTruncateJrnl, /* xTruncate */
35800
+ kvvfsSyncJrnl, /* xSync */
35801
+ kvvfsFileSizeJrnl, /* xFileSize */
35802
+ kvvfsLock, /* xLock */
35803
+ kvvfsUnlock, /* xUnlock */
35804
+ kvvfsCheckReservedLock, /* xCheckReservedLock */
35805
+ kvvfsFileControlJrnl, /* xFileControl */
35806
+ kvvfsSectorSize, /* xSectorSize */
35807
+ kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35808
+ 0, /* xShmMap */
35809
+ 0, /* xShmLock */
35810
+ 0, /* xShmBarrier */
35811
+ 0, /* xShmUnmap */
35812
+ 0, /* xFetch */
35813
+ 0 /* xUnfetch */
35814
+};
35815
+
35816
+/****** Storage subsystem **************************************************/
35817
+#include <sys/types.h>
35818
+#include <sys/stat.h>
35819
+#include <unistd.h>
35820
+
35821
+/* Forward declarations for the low-level storage engine
35822
+*/
35823
+static int kvstorageWrite(const char*, const char *zKey, const char *zData);
35824
+static int kvstorageDelete(const char*, const char *zKey);
35825
+static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
35826
+#define KVSTORAGE_KEY_SZ 32
35827
+
35828
+/* Expand the key name with an appropriate prefix and put the result
35829
+** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
35830
+** KVSTORAGE_KEY_SZ bytes.
35831
+*/
35832
+static void kvstorageMakeKey(
35833
+ const char *zClass,
35834
+ const char *zKeyIn,
35835
+ char *zKeyOut
35836
+){
35837
+ sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
35838
+}
35839
+
35840
+/* Write content into a key. zClass is the particular namespace of the
35841
+** underlying key/value store to use - either "local" or "session".
35842
+**
35843
+** Both zKey and zData are zero-terminated pure text strings.
35844
+**
35845
+** Return the number of errors.
35846
+*/
35847
+static int kvstorageWrite(
35848
+ const char *zClass,
35849
+ const char *zKey,
35850
+ const char *zData
35851
+){
35852
+ FILE *fd;
35853
+ char zXKey[KVSTORAGE_KEY_SZ];
35854
+ kvstorageMakeKey(zClass, zKey, zXKey);
35855
+ fd = fopen(zXKey, "wb");
35856
+ if( fd ){
35857
+ SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
35858
+ (int)strlen(zData), zData,
35859
+ strlen(zData)>50 ? "..." : ""));
35860
+ fputs(zData, fd);
35861
+ fclose(fd);
35862
+ return 0;
35863
+ }else{
35864
+ return 1;
35865
+ }
35866
+}
35867
+
35868
+/* Delete a key (with its corresponding data) from the key/value
35869
+** namespace given by zClass. If the key does not previously exist,
35870
+** this routine is a no-op.
35871
+*/
35872
+static int kvstorageDelete(const char *zClass, const char *zKey){
35873
+ char zXKey[KVSTORAGE_KEY_SZ];
35874
+ kvstorageMakeKey(zClass, zKey, zXKey);
35875
+ unlink(zXKey);
35876
+ SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
35877
+ return 0;
35878
+}
35879
+
35880
+/* Read the value associated with a zKey from the key/value namespace given
35881
+** by zClass and put the text data associated with that key in the first
35882
+** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
35883
+** enough to hold it all. The value put into zBuf must always be zero
35884
+** terminated, even if it gets truncated because nBuf is not large enough.
35885
+**
35886
+** Return the total number of bytes in the data, without truncation, and
35887
+** not counting the final zero terminator. Return -1 if the key does
35888
+** not exist.
35889
+**
35890
+** If nBuf<=0 then this routine simply returns the size of the data without
35891
+** actually reading it.
35892
+*/
35893
+static int kvstorageRead(
35894
+ const char *zClass,
35895
+ const char *zKey,
35896
+ char *zBuf,
35897
+ int nBuf
35898
+){
35899
+ FILE *fd;
35900
+ struct stat buf;
35901
+ char zXKey[KVSTORAGE_KEY_SZ];
35902
+ kvstorageMakeKey(zClass, zKey, zXKey);
35903
+ if( access(zXKey, R_OK)!=0
35904
+ || stat(zXKey, &buf)!=0
35905
+ || !S_ISREG(buf.st_mode)
35906
+ ){
35907
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35908
+ return -1;
35909
+ }
35910
+ if( nBuf<=0 ){
35911
+ return (int)buf.st_size;
35912
+ }else if( nBuf==1 ){
35913
+ zBuf[0] = 0;
35914
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
35915
+ (int)buf.st_size));
35916
+ return (int)buf.st_size;
35917
+ }
35918
+ if( nBuf > buf.st_size + 1 ){
35919
+ nBuf = buf.st_size + 1;
35920
+ }
35921
+ fd = fopen(zXKey, "rb");
35922
+ if( fd==0 ){
35923
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35924
+ return -1;
35925
+ }else{
35926
+ sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
35927
+ fclose(fd);
35928
+ zBuf[n] = 0;
35929
+ SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
35930
+ n, zBuf, n>50 ? "..." : ""));
35931
+ return (int)n;
35932
+ }
35933
+}
35934
+
35935
+/*
35936
+** An internal level of indirection which enables us to replace the
35937
+** kvvfs i/o methods with JavaScript implementations in WASM builds.
35938
+** Maintenance reminder: if this struct changes in any way, the JSON
35939
+** rendering of its structure must be updated in
35940
+** sqlite3_wasm_enum_json(). There are no binary compatibility
35941
+** concerns, so it does not need an iVersion member. This file is
35942
+** necessarily always compiled together with sqlite3_wasm_enum_json(),
35943
+** and JS code dynamically creates the mapping of members based on
35944
+** that JSON description.
35945
+*/
35946
+typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
35947
+struct sqlite3_kvvfs_methods {
35948
+ int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
35949
+ int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
35950
+ int (*xDelete)(const char *zClass, const char *zKey);
35951
+ const int nKeySize;
35952
+};
35953
+
35954
+/*
35955
+** This object holds the kvvfs I/O methods which may be swapped out
35956
+** for JavaScript-side implementations in WASM builds. In such builds
35957
+** it cannot be const, but in native builds it should be so that
35958
+** the compiler can hopefully optimize this level of indirection out.
35959
+** That said, kvvfs is intended primarily for use in WASM builds.
35960
+**
35961
+** Note that this is not explicitly flagged as static because the
35962
+** amalgamation build will tag it with SQLITE_PRIVATE.
35963
+*/
35964
+#ifndef SQLITE_WASM
35965
+const
35966
+#endif
35967
+SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
35968
+kvstorageRead,
35969
+kvstorageWrite,
35970
+kvstorageDelete,
35971
+KVSTORAGE_KEY_SZ
35972
+};
35973
+
35974
+/****** Utility subroutines ************************************************/
35975
+
35976
+/*
35977
+** Encode binary into the text encoded used to persist on disk.
35978
+** The output text is stored in aOut[], which must be at least
35979
+** nData+1 bytes in length.
35980
+**
35981
+** Return the actual length of the encoded text, not counting the
35982
+** zero terminator at the end.
35983
+**
35984
+** Encoding format
35985
+** ---------------
35986
+**
35987
+** * Non-zero bytes are encoded as upper-case hexadecimal
35988
+**
35989
+** * A sequence of one or more zero-bytes that are not at the
35990
+** beginning of the buffer are encoded as a little-endian
35991
+** base-26 number using a..z. "a" means 0. "b" means 1,
35992
+** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
35993
+**
35994
+** * Because there is no overlap between the encoding characters
35995
+** of hexadecimal and base-26 numbers, it is always clear where
35996
+** one stops and the next begins.
35997
+*/
35998
+static int kvvfsEncode(const char *aData, int nData, char *aOut){
35999
+ int i, j;
36000
+ const unsigned char *a = (const unsigned char*)aData;
36001
+ for(i=j=0; i<nData; i++){
36002
+ unsigned char c = a[i];
36003
+ if( c!=0 ){
36004
+ aOut[j++] = "0123456789ABCDEF"[c>>4];
36005
+ aOut[j++] = "0123456789ABCDEF"[c&0xf];
36006
+ }else{
36007
+ /* A sequence of 1 or more zeros is stored as a little-endian
36008
+ ** base-26 number using a..z as the digits. So one zero is "b".
36009
+ ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
36010
+ ** and so forth.
36011
+ */
36012
+ int k;
36013
+ for(k=1; i+k<nData && a[i+k]==0; k++){}
36014
+ i += k-1;
36015
+ while( k>0 ){
36016
+ aOut[j++] = 'a'+(k%26);
36017
+ k /= 26;
36018
+ }
36019
+ }
36020
+ }
36021
+ aOut[j] = 0;
36022
+ return j;
36023
+}
36024
+
36025
+static const signed char kvvfsHexValue[256] = {
36026
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36027
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36028
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36029
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36030
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36031
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36032
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36033
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36034
+
36035
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36036
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36037
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36038
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36043
+};
36044
+
36045
+/*
36046
+** Decode the text encoding back to binary. The binary content is
36047
+** written into pOut, which must be at least nOut bytes in length.
36048
+**
36049
+** The return value is the number of bytes actually written into aOut[].
36050
+*/
36051
+static int kvvfsDecode(const char *a, char *aOut, int nOut){
36052
+ int i, j;
36053
+ int c;
36054
+ const unsigned char *aIn = (const unsigned char*)a;
36055
+ i = 0;
36056
+ j = 0;
36057
+ while( 1 ){
36058
+ c = kvvfsHexValue[aIn[i]];
36059
+ if( c<0 ){
36060
+ int n = 0;
36061
+ int mult = 1;
36062
+ c = aIn[i];
36063
+ if( c==0 ) break;
36064
+ while( c>='a' && c<='z' ){
36065
+ n += (c - 'a')*mult;
36066
+ mult *= 26;
36067
+ c = aIn[++i];
36068
+ }
36069
+ if( j+n>nOut ) return -1;
36070
+ memset(&aOut[j], 0, n);
36071
+ j += n;
36072
+ c = aIn[i];
36073
+ if( c==0 ) break;
36074
+ }else{
36075
+ aOut[j] = c<<4;
36076
+ c = kvvfsHexValue[aIn[++i]];
36077
+ if( c<0 ) break;
36078
+ aOut[j++] += c;
36079
+ i++;
36080
+ }
36081
+ }
36082
+ return j;
36083
+}
36084
+
36085
+/*
36086
+** Decode a complete journal file. Allocate space in pFile->aJrnl
36087
+** and store the decoding there. Or leave pFile->aJrnl set to NULL
36088
+** if an error is encountered.
36089
+**
36090
+** The first few characters of the text encoding will be a little-endian
36091
+** base-26 number (digits a..z) that is the total number of bytes
36092
+** in the decoded journal file image. This base-26 number is followed
36093
+** by a single space, then the encoding of the journal. The space
36094
+** separator is required to act as a terminator for the base-26 number.
36095
+*/
36096
+static void kvvfsDecodeJournal(
36097
+ KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36098
+ const char *zTxt, /* Text encoding. Zero-terminated */
36099
+ int nTxt /* Bytes in zTxt, excluding zero terminator */
36100
+){
36101
+ unsigned int n = 0;
36102
+ int c, i, mult;
36103
+ i = 0;
36104
+ mult = 1;
36105
+ while( (c = zTxt[i++])>='a' && c<='z' ){
36106
+ n += (zTxt[i] - 'a')*mult;
36107
+ mult *= 26;
36108
+ }
36109
+ sqlite3_free(pFile->aJrnl);
36110
+ pFile->aJrnl = sqlite3_malloc64( n );
36111
+ if( pFile->aJrnl==0 ){
36112
+ pFile->nJrnl = 0;
36113
+ return;
36114
+ }
36115
+ pFile->nJrnl = n;
36116
+ n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36117
+ if( n<pFile->nJrnl ){
36118
+ sqlite3_free(pFile->aJrnl);
36119
+ pFile->aJrnl = 0;
36120
+ pFile->nJrnl = 0;
36121
+ }
36122
+}
36123
+
36124
+/*
36125
+** Read or write the "sz" element, containing the database file size.
36126
+*/
36127
+static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
36128
+ char zData[50];
36129
+ zData[0] = 0;
36130
+ sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36131
+ return strtoll(zData, 0, 0);
36132
+}
36133
+static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
36134
+ char zData[50];
36135
+ sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
36136
+ return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36137
+}
36138
+
36139
+/****** sqlite3_io_methods methods ******************************************/
36140
+
36141
+/*
36142
+** Close an kvvfs-file.
36143
+*/
36144
+static int kvvfsClose(sqlite3_file *pProtoFile){
36145
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36146
+
36147
+ SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36148
+ pFile->isJournal ? "journal" : "db"));
36149
+ sqlite3_free(pFile->aJrnl);
36150
+ return SQLITE_OK;
36151
+}
36152
+
36153
+/*
36154
+** Read from the -journal file.
36155
+*/
36156
+static int kvvfsReadJrnl(
36157
+ sqlite3_file *pProtoFile,
36158
+ void *zBuf,
36159
+ int iAmt,
36160
+ sqlite_int64 iOfst
36161
+){
36162
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36163
+ assert( pFile->isJournal );
36164
+ SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36165
+ if( pFile->aJrnl==0 ){
36166
+ int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36167
+ char *aTxt;
36168
+ if( szTxt<=4 ){
36169
+ return SQLITE_IOERR;
36170
+ }
36171
+ aTxt = sqlite3_malloc64( szTxt+1 );
36172
+ if( aTxt==0 ) return SQLITE_NOMEM;
36173
+ kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36174
+ kvvfsDecodeJournal(pFile, aTxt, szTxt);
36175
+ sqlite3_free(aTxt);
36176
+ if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36177
+ }
36178
+ if( iOfst+iAmt>pFile->nJrnl ){
36179
+ return SQLITE_IOERR_SHORT_READ;
36180
+ }
36181
+ memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36182
+ return SQLITE_OK;
36183
+}
36184
+
36185
+/*
36186
+** Read from the database file.
36187
+*/
36188
+static int kvvfsReadDb(
36189
+ sqlite3_file *pProtoFile,
36190
+ void *zBuf,
36191
+ int iAmt,
36192
+ sqlite_int64 iOfst
36193
+){
36194
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36195
+ unsigned int pgno;
36196
+ int got, n;
36197
+ char zKey[30];
36198
+ char aData[133073];
36199
+ assert( iOfst>=0 );
36200
+ assert( iAmt>=0 );
36201
+ SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36202
+ if( iOfst+iAmt>=512 ){
36203
+ if( (iOfst % iAmt)!=0 ){
36204
+ return SQLITE_IOERR_READ;
36205
+ }
36206
+ if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36207
+ return SQLITE_IOERR_READ;
36208
+ }
36209
+ pFile->szPage = iAmt;
36210
+ pgno = 1 + iOfst/iAmt;
36211
+ }else{
36212
+ pgno = 1;
36213
+ }
36214
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36215
+ got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36216
+ if( got<0 ){
36217
+ n = 0;
36218
+ }else{
36219
+ aData[got] = 0;
36220
+ if( iOfst+iAmt<512 ){
36221
+ int k = iOfst+iAmt;
36222
+ aData[k*2] = 0;
36223
+ n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36224
+ if( n>=iOfst+iAmt ){
36225
+ memcpy(zBuf, &aData[2000+iOfst], iAmt);
36226
+ n = iAmt;
36227
+ }else{
36228
+ n = 0;
36229
+ }
36230
+ }else{
36231
+ n = kvvfsDecode(aData, zBuf, iAmt);
36232
+ }
36233
+ }
36234
+ if( n<iAmt ){
36235
+ memset(zBuf+n, 0, iAmt-n);
36236
+ return SQLITE_IOERR_SHORT_READ;
36237
+ }
36238
+ return SQLITE_OK;
36239
+}
36240
+
36241
+
36242
+/*
36243
+** Write into the -journal file.
36244
+*/
36245
+static int kvvfsWriteJrnl(
36246
+ sqlite3_file *pProtoFile,
36247
+ const void *zBuf,
36248
+ int iAmt,
36249
+ sqlite_int64 iOfst
36250
+){
36251
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36252
+ sqlite3_int64 iEnd = iOfst+iAmt;
36253
+ SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36254
+ if( iEnd>=0x10000000 ) return SQLITE_FULL;
36255
+ if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36256
+ char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36257
+ if( aNew==0 ){
36258
+ return SQLITE_IOERR_NOMEM;
36259
+ }
36260
+ pFile->aJrnl = aNew;
36261
+ if( pFile->nJrnl<iOfst ){
36262
+ memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36263
+ }
36264
+ pFile->nJrnl = iEnd;
36265
+ }
36266
+ memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36267
+ return SQLITE_OK;
36268
+}
36269
+
36270
+/*
36271
+** Write into the database file.
36272
+*/
36273
+static int kvvfsWriteDb(
36274
+ sqlite3_file *pProtoFile,
36275
+ const void *zBuf,
36276
+ int iAmt,
36277
+ sqlite_int64 iOfst
36278
+){
36279
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36280
+ unsigned int pgno;
36281
+ char zKey[30];
36282
+ char aData[131073];
36283
+ SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36284
+ assert( iAmt>=512 && iAmt<=65536 );
36285
+ assert( (iAmt & (iAmt-1))==0 );
36286
+ pgno = 1 + iOfst/iAmt;
36287
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36288
+ kvvfsEncode(zBuf, iAmt, aData);
36289
+ if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36290
+ return SQLITE_IOERR;
36291
+ }
36292
+ if( iOfst+iAmt > pFile->szDb ){
36293
+ pFile->szDb = iOfst + iAmt;
36294
+ }
36295
+ return SQLITE_OK;
36296
+}
36297
+
36298
+/*
36299
+** Truncate an kvvfs-file.
36300
+*/
36301
+static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
36302
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36303
+ SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36304
+ assert( size==0 );
36305
+ sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36306
+ sqlite3_free(pFile->aJrnl);
36307
+ pFile->aJrnl = 0;
36308
+ pFile->nJrnl = 0;
36309
+ return SQLITE_OK;
36310
+}
36311
+static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
36312
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36313
+ if( pFile->szDb>size
36314
+ && pFile->szPage>0
36315
+ && (size % pFile->szPage)==0
36316
+ ){
36317
+ char zKey[50];
36318
+ unsigned int pgno, pgnoMax;
36319
+ SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36320
+ pgno = 1 + size/pFile->szPage;
36321
+ pgnoMax = 2 + pFile->szDb/pFile->szPage;
36322
+ while( pgno<=pgnoMax ){
36323
+ sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36324
+ sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36325
+ pgno++;
36326
+ }
36327
+ pFile->szDb = size;
36328
+ return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK;
36329
+ }
36330
+ return SQLITE_IOERR;
36331
+}
36332
+
36333
+/*
36334
+** Sync an kvvfs-file.
36335
+*/
36336
+static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
36337
+ int i, n;
36338
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36339
+ char *zOut;
36340
+ SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36341
+ if( pFile->nJrnl<=0 ){
36342
+ return kvvfsTruncateJrnl(pProtoFile, 0);
36343
+ }
36344
+ zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36345
+ if( zOut==0 ){
36346
+ return SQLITE_IOERR_NOMEM;
36347
+ }
36348
+ n = pFile->nJrnl;
36349
+ i = 0;
36350
+ do{
36351
+ zOut[i++] = 'a' + (n%26);
36352
+ n /= 26;
36353
+ }while( n>0 );
36354
+ zOut[i++] = ' ';
36355
+ kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36356
+ i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36357
+ sqlite3_free(zOut);
36358
+ return i ? SQLITE_IOERR : SQLITE_OK;
36359
+}
36360
+static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
36361
+ return SQLITE_OK;
36362
+}
36363
+
36364
+/*
36365
+** Return the current file-size of an kvvfs-file.
36366
+*/
36367
+static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36368
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36369
+ SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36370
+ *pSize = pFile->nJrnl;
36371
+ return SQLITE_OK;
36372
+}
36373
+static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36374
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36375
+ SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36376
+ if( pFile->szDb>=0 ){
36377
+ *pSize = pFile->szDb;
36378
+ }else{
36379
+ *pSize = kvvfsReadFileSize(pFile);
36380
+ }
36381
+ return SQLITE_OK;
36382
+}
36383
+
36384
+/*
36385
+** Lock an kvvfs-file.
36386
+*/
36387
+static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
36388
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36389
+ assert( !pFile->isJournal );
36390
+ SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36391
+
36392
+ if( eLock!=SQLITE_LOCK_NONE ){
36393
+ pFile->szDb = kvvfsReadFileSize(pFile);
36394
+ }
36395
+ return SQLITE_OK;
36396
+}
36397
+
36398
+/*
36399
+** Unlock an kvvfs-file.
36400
+*/
36401
+static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
36402
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36403
+ assert( !pFile->isJournal );
36404
+ SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36405
+ if( eLock==SQLITE_LOCK_NONE ){
36406
+ pFile->szDb = -1;
36407
+ }
36408
+ return SQLITE_OK;
36409
+}
36410
+
36411
+/*
36412
+** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36413
+*/
36414
+static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
36415
+ SQLITE_KV_LOG(("xCheckReservedLock\n"));
36416
+ *pResOut = 0;
36417
+ return SQLITE_OK;
36418
+}
36419
+
36420
+/*
36421
+** File control method. For custom operations on an kvvfs-file.
36422
+*/
36423
+static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
36424
+ SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
36425
+ return SQLITE_NOTFOUND;
36426
+}
36427
+static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
36428
+ SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
36429
+ if( op==SQLITE_FCNTL_SYNC ){
36430
+ KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36431
+ int rc = SQLITE_OK;
36432
+ SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36433
+ if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36434
+ rc = SQLITE_IOERR;
36435
+ }
36436
+ return rc;
36437
+ }
36438
+ return SQLITE_NOTFOUND;
36439
+}
36440
+
36441
+/*
36442
+** Return the sector-size in bytes for an kvvfs-file.
36443
+*/
36444
+static int kvvfsSectorSize(sqlite3_file *pFile){
36445
+ return 512;
36446
+}
36447
+
36448
+/*
36449
+** Return the device characteristic flags supported by an kvvfs-file.
36450
+*/
36451
+static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
36452
+ return 0;
36453
+}
36454
+
36455
+/****** sqlite3_vfs methods *************************************************/
36456
+
36457
+/*
36458
+** Open an kvvfs file handle.
36459
+*/
36460
+static int kvvfsOpen(
36461
+ sqlite3_vfs *pProtoVfs,
36462
+ const char *zName,
36463
+ sqlite3_file *pProtoFile,
36464
+ int flags,
36465
+ int *pOutFlags
36466
+){
36467
+ KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36468
+ SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36469
+ if( strcmp(zName, "local")==0
36470
+ || strcmp(zName, "session")==0
36471
+ ){
36472
+ pFile->isJournal = 0;
36473
+ pFile->base.pMethods = &kvvfs_db_io_methods;
36474
+ }else
36475
+ if( strcmp(zName, "local-journal")==0
36476
+ || strcmp(zName, "session-journal")==0
36477
+ ){
36478
+ pFile->isJournal = 1;
36479
+ pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36480
+ }else{
36481
+ return SQLITE_CANTOPEN;
36482
+ }
36483
+ if( zName[0]=='s' ){
36484
+ pFile->zClass = "session";
36485
+ }else{
36486
+ pFile->zClass = "local";
36487
+ }
36488
+ pFile->aJrnl = 0;
36489
+ pFile->nJrnl = 0;
36490
+ pFile->szPage = -1;
36491
+ pFile->szDb = -1;
36492
+ return SQLITE_OK;
36493
+}
36494
+
36495
+/*
36496
+** Delete the file located at zPath. If the dirSync argument is true,
36497
+** ensure the file-system modifications are synced to disk before
36498
+** returning.
36499
+*/
36500
+static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
36501
+ if( strcmp(zPath, "local-journal")==0 ){
36502
+ sqlite3KvvfsMethods.xDelete("local", "jrnl");
36503
+ }else
36504
+ if( strcmp(zPath, "session-journal")==0 ){
36505
+ sqlite3KvvfsMethods.xDelete("session", "jrnl");
36506
+ }
36507
+ return SQLITE_OK;
36508
+}
36509
+
36510
+/*
36511
+** Test for access permissions. Return true if the requested permission
36512
+** is available, or false otherwise.
36513
+*/
36514
+static int kvvfsAccess(
36515
+ sqlite3_vfs *pProtoVfs,
36516
+ const char *zPath,
36517
+ int flags,
36518
+ int *pResOut
36519
+){
36520
+ SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
36521
+ if( strcmp(zPath, "local-journal")==0 ){
36522
+ *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
36523
+ }else
36524
+ if( strcmp(zPath, "session-journal")==0 ){
36525
+ *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
36526
+ }else
36527
+ if( strcmp(zPath, "local")==0 ){
36528
+ *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
36529
+ }else
36530
+ if( strcmp(zPath, "session")==0 ){
36531
+ *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
36532
+ }else
36533
+ {
36534
+ *pResOut = 0;
36535
+ }
36536
+ SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
36537
+ return SQLITE_OK;
36538
+}
36539
+
36540
+/*
36541
+** Populate buffer zOut with the full canonical pathname corresponding
36542
+** to the pathname in zPath. zOut is guaranteed to point to a buffer
36543
+** of at least (INST_MAX_PATHNAME+1) bytes.
36544
+*/
36545
+static int kvvfsFullPathname(
36546
+ sqlite3_vfs *pVfs,
36547
+ const char *zPath,
36548
+ int nOut,
36549
+ char *zOut
36550
+){
36551
+ size_t nPath;
36552
+#ifdef SQLITE_OS_KV_ALWAYS_LOCAL
36553
+ zPath = "local";
36554
+#endif
36555
+ nPath = strlen(zPath);
36556
+ SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
36557
+ if( nOut<nPath+1 ) nPath = nOut - 1;
36558
+ memcpy(zOut, zPath, nPath);
36559
+ zOut[nPath] = 0;
36560
+ return SQLITE_OK;
36561
+}
36562
+
36563
+/*
36564
+** Open the dynamic library located at zPath and return a handle.
36565
+*/
36566
+static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
36567
+ return 0;
36568
+}
36569
+
36570
+/*
36571
+** Populate the buffer pointed to by zBufOut with nByte bytes of
36572
+** random data.
36573
+*/
36574
+static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
36575
+ memset(zBufOut, 0, nByte);
36576
+ return nByte;
36577
+}
36578
+
36579
+/*
36580
+** Sleep for nMicro microseconds. Return the number of microseconds
36581
+** actually slept.
36582
+*/
36583
+static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
36584
+ return SQLITE_OK;
36585
+}
36586
+
36587
+/*
36588
+** Return the current time as a Julian Day number in *pTimeOut.
36589
+*/
36590
+static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
36591
+ sqlite3_int64 i = 0;
36592
+ int rc;
36593
+ rc = kvvfsCurrentTimeInt64(0, &i);
36594
+ *pTimeOut = i/86400000.0;
36595
+ return rc;
36596
+}
36597
+#include <sys/time.h>
36598
+static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
36599
+ static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36600
+ struct timeval sNow;
36601
+ (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
36602
+ *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
36603
+ return SQLITE_OK;
36604
+}
36605
+#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
36606
+
36607
+#if SQLITE_OS_KV
36608
+/*
36609
+** This routine is called initialize the KV-vfs as the default VFS.
36610
+*/
36611
+SQLITE_API int sqlite3_os_init(void){
36612
+ return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
36613
+}
36614
+SQLITE_API int sqlite3_os_end(void){
36615
+ return SQLITE_OK;
36616
+}
36617
+#endif /* SQLITE_OS_KV */
36618
+
36619
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
36620
+SQLITE_PRIVATE int sqlite3KvvfsInit(void){
36621
+ return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
36622
+}
36623
+#endif
36624
+
36625
+/************** End of os_kv.c ***********************************************/
3556236626
/************** Begin file os_unix.c *****************************************/
3556336627
/*
3556436628
** 2004 May 22
3556536629
**
3556636630
** The author disclaims copyright to this source code. In place of
@@ -35647,17 +36711,17 @@
3564736711
#endif
3564836712
3564936713
/*
3565036714
** standard include files.
3565136715
*/
35652
-#include <sys/types.h>
35653
-#include <sys/stat.h>
36716
+#include <sys/types.h> /* amalgamator: keep */
36717
+#include <sys/stat.h> /* amalgamator: keep */
3565436718
#include <fcntl.h>
3565536719
#include <sys/ioctl.h>
35656
-#include <unistd.h>
36720
+#include <unistd.h> /* amalgamator: keep */
3565736721
/* #include <time.h> */
35658
-#include <sys/time.h>
36722
+#include <sys/time.h> /* amalgamator: keep */
3565936723
#include <errno.h>
3566036724
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3566136725
# include <sys/mman.h>
3566236726
#endif
3566336727
@@ -43625,10 +44689,13 @@
4362544689
0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
4362644690
#else
4362744691
sqlite3_vfs_register(&aVfs[i], i==0);
4362844692
#endif
4362944693
}
44694
+#ifdef SQLITE_OS_KV_OPTIONAL
44695
+ sqlite3KvvfsInit();
44696
+#endif
4363044697
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
4363144698
4363244699
#ifndef SQLITE_OMIT_WAL
4363344700
/* Validate lock assumptions */
4363444701
assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
@@ -70722,10 +71789,13 @@
7072271789
put4byte(pCell+info.nSize-4, iTo);
7072371790
break;
7072471791
}
7072571792
}
7072671793
}else{
71794
+ if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
71795
+ return SQLITE_CORRUPT_PAGE(pPage);
71796
+ }
7072771797
if( get4byte(pCell)==iFrom ){
7072871798
put4byte(pCell, iTo);
7072971799
break;
7073071800
}
7073171801
}
@@ -73056,18 +74126,11 @@
7305674126
}
7305774127
}
7305874128
7305974129
pPage = pCur->pPage;
7306074130
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. */
74131
+ if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
7306974132
return SQLITE_CORRUPT_BKPT;
7307074133
}
7307174134
7307274135
if( idx>=pPage->nCell ){
7307374136
if( !pPage->leaf ){
@@ -75753,10 +76816,15 @@
7575376816
assert( pCur->pPage->nOverflow );
7575476817
}
7575576818
}else{
7575676819
break;
7575776820
}
76821
+ }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
76822
+ /* The page being written is not a root page, and there is currently
76823
+ ** more than one reference to it. This only happens if the page is one
76824
+ ** of its own ancestor pages. Corruption. */
76825
+ rc = SQLITE_CORRUPT_BKPT;
7575876826
}else{
7575976827
MemPage * const pParent = pCur->apPage[iPage-1];
7576076828
int const iIdx = pCur->aiIdx[iPage-1];
7576176829
7576276830
rc = sqlite3PagerWrite(pParent->pDbPage);
@@ -79679,10 +80747,11 @@
7967980747
assert( MEM_Str==(MEM_Blob>>3) );
7968080748
pMem->flags |= (pMem->flags&MEM_Blob)>>3;
7968180749
sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
7968280750
assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
7968380751
pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
80752
+ if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
7968480753
return sqlite3VdbeChangeEncoding(pMem, encoding);
7968580754
}
7968680755
}
7968780756
return SQLITE_OK;
7968880757
}
@@ -80813,10 +81882,13 @@
8081381882
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
8081481883
Mem *p = (Mem*)pVal;
8081581884
assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
8081681885
if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
8081781886
return p->n;
81887
+ }
81888
+ if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
81889
+ return p->n;
8081881890
}
8081981891
if( (p->flags & MEM_Blob)!=0 ){
8082081892
if( p->flags & MEM_Zero ){
8082181893
return p->n + p->u.nZero;
8082281894
}else{
@@ -81983,10 +83055,22 @@
8198383055
}
8198483056
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
8198583057
assert( p->nOp>0 || p->db->mallocFailed );
8198683058
if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
8198783059
}
83060
+
83061
+/*
83062
+** If the previous opcode is an OP_Column that delivers results
83063
+** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
83064
+** opcode.
83065
+*/
83066
+SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
83067
+ VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
83068
+ if( pOp->p3==iDest && pOp->opcode==OP_Column ){
83069
+ pOp->p5 |= OPFLAG_TYPEOFARG;
83070
+ }
83071
+}
8198883072
8198983073
/*
8199083074
** Change the P2 operand of instruction addr so that it points to
8199183075
** the address of the next instruction to be coded.
8199283076
*/
@@ -85402,11 +86486,11 @@
8540286486
testcase( pRhs->flags & MEM_Int );
8540386487
testcase( pRhs->flags & MEM_IntReal );
8540486488
serial_type = aKey1[idx1];
8540586489
testcase( serial_type==12 );
8540686490
if( serial_type>=10 ){
85407
- rc = +1;
86491
+ rc = serial_type==10 ? -1 : +1;
8540886492
}else if( serial_type==0 ){
8540986493
rc = -1;
8541086494
}else if( serial_type==7 ){
8541186495
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
8541286496
rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
@@ -85427,11 +86511,11 @@
8542786511
if( serial_type>=10 ){
8542886512
/* Serial types 12 or greater are strings and blobs (greater than
8542986513
** numbers). Types 10 and 11 are currently "reserved for future
8543086514
** use", so it doesn't really matter what the results of comparing
8543186515
** them to numberic values are. */
85432
- rc = +1;
86516
+ rc = serial_type==10 ? -1 : +1;
8543386517
}else if( serial_type==0 ){
8543486518
rc = -1;
8543586519
}else{
8543686520
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
8543786521
if( serial_type==7 ){
@@ -85508,11 +86592,11 @@
8550886592
}
8550986593
8551086594
/* RHS is null */
8551186595
else{
8551286596
serial_type = aKey1[idx1];
85513
- rc = (serial_type!=0);
86597
+ rc = (serial_type!=0 && serial_type!=10);
8551486598
}
8551586599
8551686600
if( rc!=0 ){
8551786601
int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
8551886602
if( sortFlags ){
@@ -86457,10 +87541,13 @@
8645787541
}
8645887542
assert( eType == aType[pVal->flags&MEM_AffMask] );
8645987543
}
8646087544
#endif
8646187545
return aType[pVal->flags&MEM_AffMask];
87546
+}
87547
+SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
87548
+ return pVal->enc;
8646287549
}
8646387550
8646487551
/* Return true if a parameter to xUpdate represents an unchanged column */
8646587552
SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
8646687553
return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
@@ -91094,23 +92181,94 @@
9109492181
goto jump_to_p2;
9109592182
}
9109692183
break;
9109792184
}
9109892185
91099
-/* Opcode: IsNullOrType P1 P2 P3 * *
91100
-** Synopsis: if typeof(r[P1]) IN (P3,5) goto P2
92186
+/* Opcode: IsType P1 P2 P3 P4 P5
92187
+** Synopsis: if typeof(P1.P3) in P5 goto P2
9110192188
**
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.
92189
+** Jump to P2 if the type of a column in a btree is one of the types specified
92190
+** by the P5 bitmask.
92191
+**
92192
+** P1 is normally a cursor on a btree for which the row decode cache is
92193
+** valid through at least column P3. In other words, there should have been
92194
+** a prior OP_Column for column P3 or greater. If the cursor is not valid,
92195
+** then this opcode might give spurious results.
92196
+** The the btree row has fewer than P3 columns, then use P4 as the
92197
+** datatype.
92198
+**
92199
+** If P1 is -1, then P3 is a register number and the datatype is taken
92200
+** from the value in that register.
92201
+**
92202
+** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
92203
+** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
92204
+** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
92205
+**
92206
+** Take the jump to address P2 if and only if the datatype of the
92207
+** value determined by P1 and P3 corresponds to one of the bits in the
92208
+** P5 bitmask.
92209
+**
9110592210
*/
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;
92211
+case OP_IsType: { /* jump */
92212
+ VdbeCursor *pC;
92213
+ u16 typeMask;
92214
+ u32 serialType;
92215
+
92216
+ assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
92217
+ assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
92218
+ if( pOp->p1>=0 ){
92219
+ pC = p->apCsr[pOp->p1];
92220
+ assert( pC!=0 );
92221
+ assert( pOp->p3>=0 );
92222
+ if( pOp->p3<pC->nHdrParsed ){
92223
+ serialType = pC->aType[pOp->p3];
92224
+ if( serialType>=12 ){
92225
+ if( serialType&1 ){
92226
+ typeMask = 0x04; /* SQLITE_TEXT */
92227
+ }else{
92228
+ typeMask = 0x08; /* SQLITE_BLOB */
92229
+ }
92230
+ }else{
92231
+ static const unsigned char aMask[] = {
92232
+ 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
92233
+ 0x01, 0x01, 0x10, 0x10
92234
+ };
92235
+ testcase( serialType==0 );
92236
+ testcase( serialType==1 );
92237
+ testcase( serialType==2 );
92238
+ testcase( serialType==3 );
92239
+ testcase( serialType==4 );
92240
+ testcase( serialType==5 );
92241
+ testcase( serialType==6 );
92242
+ testcase( serialType==7 );
92243
+ testcase( serialType==8 );
92244
+ testcase( serialType==9 );
92245
+ testcase( serialType==10 );
92246
+ testcase( serialType==11 );
92247
+ typeMask = aMask[serialType];
92248
+ }
92249
+ }else{
92250
+ typeMask = 1 << (pOp->p4.i - 1);
92251
+ testcase( typeMask==0x01 );
92252
+ testcase( typeMask==0x02 );
92253
+ testcase( typeMask==0x04 );
92254
+ testcase( typeMask==0x08 );
92255
+ testcase( typeMask==0x10 );
92256
+ }
92257
+ }else{
92258
+ assert( memIsValid(&aMem[pOp->p3]) );
92259
+ typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
92260
+ testcase( typeMask==0x01 );
92261
+ testcase( typeMask==0x02 );
92262
+ testcase( typeMask==0x04 );
92263
+ testcase( typeMask==0x08 );
92264
+ testcase( typeMask==0x10 );
92265
+ }
92266
+ VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
92267
+ if( typeMask & pOp->p5 ){
92268
+ goto jump_to_p2;
92269
+ }
9111292270
break;
9111392271
}
9111492272
9111592273
/* Opcode: ZeroOrNull P1 P2 P3 * *
9111692274
** Synopsis: r[P2] = 0 OR NULL
@@ -91207,23 +92365,25 @@
9120792365
** Synopsis: r[P3]=PX cursor P1 column P2
9120892366
**
9120992367
** Interpret the data that cursor P1 points to as a structure built using
9121092368
** the MakeRecord instruction. (See the MakeRecord opcode for additional
9121192369
** information about the format of the data.) Extract the P2-th column
91212
-** from this record. If there are less that (P2+1)
92370
+** from this record. If there are less than (P2+1)
9121392371
** values in the record, extract a NULL.
9121492372
**
9121592373
** The value extracted is stored in register P3.
9121692374
**
9121792375
** If the record contains fewer than P2 fields, then extract a NULL. Or,
9121892376
** if the P4 argument is a P4_MEM use the value of the P4 argument as
9121992377
** the result.
9122092378
**
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().
92379
+** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
92380
+** to only be used by the length() function or the equivalent. The content
92381
+** of large blobs is not loaded, thus saving CPU cycles. If the
92382
+** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
92383
+** typeof() function or the IS NULL or IS NOT NULL operators or the
92384
+** equivalent. In this case, all content loading can be omitted.
9122592385
*/
9122692386
case OP_Column: {
9122792387
u32 p2; /* column number to retrieve */
9122892388
VdbeCursor *pC; /* The VDBE cursor */
9122992389
BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
@@ -93158,11 +94318,17 @@
9315894318
assert( oc!=OP_SeekGE || r.default_rc==+1 );
9315994319
assert( oc!=OP_SeekLT || r.default_rc==+1 );
9316094320
9316194321
r.aMem = &aMem[pOp->p3];
9316294322
#ifdef SQLITE_DEBUG
93163
- { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
94323
+ {
94324
+ int i;
94325
+ for(i=0; i<r.nField; i++){
94326
+ assert( memIsValid(&r.aMem[i]) );
94327
+ if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
94328
+ }
94329
+ }
9316494330
#endif
9316594331
r.eqSeen = 0;
9316694332
rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
9316794333
if( rc!=SQLITE_OK ){
9316894334
goto abort_due_to_error;
@@ -93221,54 +94387,76 @@
9322194387
}
9322294388
break;
9322394389
}
9322494390
9322594391
93226
-/* Opcode: SeekScan P1 P2 * * *
94392
+/* Opcode: SeekScan P1 P2 * * P5
9322794393
** Synopsis: Scan-ahead up to P1 rows
9322894394
**
9322994395
** This opcode is a prefix opcode to OP_SeekGE. In other words, this
9323094396
** opcode must be immediately followed by OP_SeekGE. This constraint is
9323194397
** checked by assert() statements.
9323294398
**
9323394399
** This opcode uses the P1 through P4 operands of the subsequent
9323494400
** OP_SeekGE. In the text that follows, the operands of the subsequent
9323594401
** 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.
94402
+** the P1, P2 and P5 operands of this opcode are also used, and are called
94403
+** This.P1, This.P2 and This.P5.
9323894404
**
9323994405
** This opcode helps to optimize IN operators on a multi-column index
9324094406
** where the IN operator is on the later terms of the index by avoiding
9324194407
** unnecessary seeks on the btree, substituting steps to the next row
9324294408
** of the b-tree instead. A correct answer is obtained if this opcode
9324394409
** is omitted or is a no-op.
9324494410
**
9324594411
** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
9324694412
** is the desired entry that we want the cursor SeekGE.P1 to be pointing
93247
-** to. Call this SeekGE.P4/P5 row the "target".
94413
+** to. Call this SeekGE.P3/P4 row the "target".
9324894414
**
9324994415
** If the SeekGE.P1 cursor is not currently pointing to a valid row,
9325094416
** then this opcode is a no-op and control passes through into the OP_SeekGE.
9325194417
**
9325294418
** If the SeekGE.P1 cursor is pointing to a valid row, then that row
9325394419
** 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.
94420
+** target row, or it might be after the target row. If the cursor is
94421
+** currently before the target row, then this opcode attempts to position
94422
+** the cursor on or after the target row by invoking sqlite3BtreeStep()
94423
+** on the cursor between 1 and This.P1 times.
94424
+**
94425
+** The This.P5 parameter is a flag that indicates what to do if the
94426
+** cursor ends up pointing at a valid row that is past the target
94427
+** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
94428
+** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
94429
+** case occurs when there are no inequality constraints to the right of
94430
+** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
94431
+** occurs when there are inequality constraints to the right of the IN
94432
+** operator. In that case, the This.P2 will point either directly to or
94433
+** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
94434
+** loop terminate.
94435
+**
94436
+** Possible outcomes from this opcode:<ol>
94437
+**
94438
+** <li> If the cursor is initally not pointed to any valid row, then
94439
+** fall through into the subsequent OP_SeekGE opcode.
94440
+**
94441
+** <li> If the cursor is left pointing to a row that is before the target
94442
+** row, even after making as many as This.P1 calls to
94443
+** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
94444
+**
94445
+** <li> If the cursor is left pointing at the target row, either because it
94446
+** was at the target row to begin with or because one or more
94447
+** sqlite3BtreeNext() calls moved the cursor to the target row,
94448
+** then jump to This.P2..,
94449
+**
94450
+** <li> If the cursor started out before the target row and a call to
94451
+** to sqlite3BtreeNext() moved the cursor off the end of the index
94452
+** (indicating that the target row definitely does not exist in the
94453
+** btree) then jump to SeekGE.P2, ending the loop.
94454
+**
94455
+** <li> If the cursor ends up on a valid row that is past the target row
94456
+** (indicating that the target row does not exist in the btree) then
94457
+** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
9327094458
** </ol>
9327194459
*/
9327294460
case OP_SeekScan: {
9327394461
VdbeCursor *pC;
9327494462
int res;
@@ -93275,18 +94463,29 @@
9327594463
int nStep;
9327694464
UnpackedRecord r;
9327794465
9327894466
assert( pOp[1].opcode==OP_SeekGE );
9327994467
93280
- /* pOp->p2 points to the first instruction past the OP_IdxGT that
93281
- ** follows the OP_SeekGE. */
94468
+ /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
94469
+ ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
94470
+ ** opcode past the OP_SeekGE itself. */
9328294471
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 );
94472
+#ifdef SQLITE_DEBUG
94473
+ if( pOp->p5==0 ){
94474
+ /* There are no inequality constraints following the IN constraint. */
94475
+ assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
94476
+ assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
94477
+ assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
94478
+ assert( aOp[pOp->p2-1].opcode==OP_IdxGT
94479
+ || aOp[pOp->p2-1].opcode==OP_IdxGE );
94480
+ testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
94481
+ }else{
94482
+ /* There are inequality constraints. */
94483
+ assert( pOp->p2==(int)(pOp-aOp)+2 );
94484
+ assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
94485
+ }
94486
+#endif
9328894487
9328994488
assert( pOp->p1>0 );
9329094489
pC = p->apCsr[pOp[1].p1];
9329194490
assert( pC!=0 );
9329294491
assert( pC->eCurType==CURTYPE_BTREE );
@@ -93316,22 +94515,24 @@
9331694515
#endif
9331794516
res = 0; /* Not needed. Only used to silence a warning. */
9331894517
while(1){
9331994518
rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
9332094519
if( rc ) goto abort_due_to_error;
93321
- if( res>0 ){
94520
+ if( res>0 && pOp->p5==0 ){
9332294521
seekscan_search_fail:
94522
+ /* Jump to SeekGE.P2, ending the loop */
9332394523
#ifdef SQLITE_DEBUG
9332494524
if( db->flags&SQLITE_VdbeTrace ){
9332594525
printf("... %d steps and then skip\n", pOp->p1 - nStep);
9332694526
}
9332794527
#endif
9332894528
VdbeBranchTaken(1,3);
9332994529
pOp++;
9333094530
goto jump_to_p2;
9333194531
}
93332
- if( res==0 ){
94532
+ if( res>=0 ){
94533
+ /* Jump to This.P2, bypassing the OP_SeekGE opcode */
9333394534
#ifdef SQLITE_DEBUG
9333494535
if( db->flags&SQLITE_VdbeTrace ){
9333594536
printf("... %d steps and then success\n", pOp->p1 - nStep);
9333694537
}
9333794538
#endif
@@ -103867,13 +105068,12 @@
103867105068
}
103868105069
op = pExpr->op;
103869105070
if( op==TK_REGISTER ) op = pExpr->op2;
103870105071
if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
103871105072
assert( ExprUseYTab(pExpr) );
103872
- if( pExpr->y.pTab ){
103873
- return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103874
- }
105073
+ assert( pExpr->y.pTab!=0 );
105074
+ return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103875105075
}
103876105076
if( op==TK_SELECT ){
103877105077
assert( ExprUseXSelect(pExpr) );
103878105078
assert( pExpr->x.pSelect!=0 );
103879105079
assert( pExpr->x.pSelect->pEList!=0 );
@@ -103987,21 +105187,18 @@
103987105187
const Expr *p = pExpr;
103988105188
while( p ){
103989105189
int op = p->op;
103990105190
if( op==TK_REGISTER ) op = p->op2;
103991105191
if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
105192
+ int j;
103992105193
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
- }
105194
+ assert( p->y.pTab!=0 );
105195
+ if( (j = p->iColumn)>=0 ){
105196
+ const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
105197
+ pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
105198
+ }
105199
+ break;
104003105200
}
104004105201
if( op==TK_CAST || op==TK_UPLUS ){
104005105202
p = p->pLeft;
104006105203
continue;
104007105204
}
@@ -107602,14 +108799,11 @@
107602108799
int iCol, /* Index of the column to extract */
107603108800
int regOut /* Extract the value into this register */
107604108801
){
107605108802
Column *pCol;
107606108803
assert( v!=0 );
107607
- if( pTab==0 ){
107608
- sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
107609
- return;
107610
- }
108804
+ assert( pTab!=0 );
107611108805
if( iCol<0 || iCol==pTab->iPKey ){
107612108806
sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
107613108807
VdbeComment((v, "%s.rowid", pTab->zName));
107614108808
}else{
107615108809
int op;
@@ -107855,10 +109049,57 @@
107855109049
#endif /* !defined(SQLITE_UNTESTABLE) */
107856109050
}
107857109051
return target;
107858109052
}
107859109053
109054
+/*
109055
+** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109056
+** If it is, then resolve the expression by reading from the index and
109057
+** return the register into which the value has been read. If pExpr is
109058
+** not an indexed expression, then return negative.
109059
+*/
109060
+static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
109061
+ Parse *pParse, /* The parsing context */
109062
+ Expr *pExpr, /* The expression to potentially bypass */
109063
+ int target /* Where to store the result of the expression */
109064
+){
109065
+ IndexedExpr *p;
109066
+ Vdbe *v;
109067
+ for(p=pParse->pIdxExpr; p; p=p->pIENext){
109068
+ int iDataCur = p->iDataCur;
109069
+ if( iDataCur<0 ) continue;
109070
+ if( pParse->iSelfTab ){
109071
+ if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109072
+ iDataCur = -1;
109073
+ }
109074
+ if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
109075
+ v = pParse->pVdbe;
109076
+ assert( v!=0 );
109077
+ if( p->bMaybeNullRow ){
109078
+ /* If the index is on a NULL row due to an outer join, then we
109079
+ ** cannot extract the value from the index. The value must be
109080
+ ** computed using the original expression. */
109081
+ int addr = sqlite3VdbeCurrentAddr(v);
109082
+ sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109083
+ VdbeCoverage(v);
109084
+ sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109085
+ VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109086
+ sqlite3VdbeGoto(v, 0);
109087
+ p = pParse->pIdxExpr;
109088
+ pParse->pIdxExpr = 0;
109089
+ sqlite3ExprCode(pParse, pExpr, target);
109090
+ pParse->pIdxExpr = p;
109091
+ sqlite3VdbeJumpHere(v, addr+2);
109092
+ }else{
109093
+ sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109094
+ VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109095
+ }
109096
+ return target;
109097
+ }
109098
+ return -1; /* Not found */
109099
+}
109100
+
107860109101
107861109102
/*
107862109103
** Generate code into the current Vdbe to evaluate the given
107863109104
** expression. Attempt to store the results in register "target".
107864109105
** Return the register where results are stored.
@@ -107883,10 +109124,15 @@
107883109124
assert( v!=0 );
107884109125
107885109126
expr_code_doover:
107886109127
if( pExpr==0 ){
107887109128
op = TK_NULL;
109129
+ }else if( pParse->pIdxExpr!=0
109130
+ && !ExprHasProperty(pExpr, EP_Leaf)
109131
+ && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109132
+ ){
109133
+ return r1;
107888109134
}else{
107889109135
assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
107890109136
op = pExpr->op;
107891109137
}
107892109138
switch( op ){
@@ -107928,15 +109174,12 @@
107928109174
** constant.
107929109175
*/
107930109176
int aff;
107931109177
iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107932109178
assert( ExprUseYTab(pExpr) );
107933
- if( pExpr->y.pTab ){
107934
- aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107935
- }else{
107936
- aff = pExpr->affExpr;
107937
- }
109179
+ assert( pExpr->y.pTab!=0 );
109180
+ aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107938109181
if( aff>SQLITE_AFF_BLOB ){
107939109182
static const char zAff[] = "B\000C\000D\000E";
107940109183
assert( SQLITE_AFF_BLOB=='A' );
107941109184
assert( SQLITE_AFF_TEXT=='B' );
107942109185
sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
@@ -107994,16 +109237,14 @@
107994109237
** in the index refer to the table to which the index belongs */
107995109238
iTab = pParse->iSelfTab - 1;
107996109239
}
107997109240
}
107998109241
assert( ExprUseYTab(pExpr) );
109242
+ assert( pExpr->y.pTab!=0 );
107999109243
iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
108000109244
pExpr->iColumn, iTab, target,
108001109245
pExpr->op2);
108002
- if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
108003
- sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
108004
- }
108005109246
return iReg;
108006109247
}
108007109248
case TK_INTEGER: {
108008109249
codeInteger(pParse, pExpr, 0, target);
108009109250
return target;
@@ -109053,10 +110294,11 @@
109053110294
case TK_ISNULL:
109054110295
case TK_NOTNULL: {
109055110296
assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
109056110297
assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
109057110298
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110299
+ sqlite3VdbeTypeofColumn(v, r1);
109058110300
sqlite3VdbeAddOp2(v, op, r1, dest);
109059110301
VdbeCoverageIf(v, op==TK_ISNULL);
109060110302
VdbeCoverageIf(v, op==TK_NOTNULL);
109061110303
testcase( regFree1==0 );
109062110304
break;
@@ -109227,10 +110469,11 @@
109227110469
break;
109228110470
}
109229110471
case TK_ISNULL:
109230110472
case TK_NOTNULL: {
109231110473
r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110474
+ sqlite3VdbeTypeofColumn(v, r1);
109232110475
sqlite3VdbeAddOp2(v, op, r1, dest);
109233110476
testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
109234110477
testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
109235110478
testcase( regFree1==0 );
109236110479
break;
@@ -109380,11 +110623,17 @@
109380110623
return 1;
109381110624
}
109382110625
if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109383110626
return 1;
109384110627
}
109385
- return 2;
110628
+ if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
110629
+ && pB->iTable<0 && pA->iTable==iTab
110630
+ ){
110631
+ /* fall through */
110632
+ }else{
110633
+ return 2;
110634
+ }
109386110635
}
109387110636
assert( !ExprHasProperty(pA, EP_IntValue) );
109388110637
assert( !ExprHasProperty(pB, EP_IntValue) );
109389110638
if( pA->u.zToken ){
109390110639
if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
@@ -109682,14 +110931,14 @@
109682110931
/* The y.pTab=0 assignment in wherecode.c always happens after the
109683110932
** impliesNotNullRow() test */
109684110933
assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
109685110934
assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
109686110935
if( (pLeft->op==TK_COLUMN
109687
- && pLeft->y.pTab!=0
110936
+ && ALWAYS(pLeft->y.pTab!=0)
109688110937
&& IsVirtual(pLeft->y.pTab))
109689110938
|| (pRight->op==TK_COLUMN
109690
- && pRight->y.pTab!=0
110939
+ && ALWAYS(pRight->y.pTab!=0)
109691110940
&& IsVirtual(pRight->y.pTab))
109692110941
){
109693110942
return WRC_Prune;
109694110943
}
109695110944
/* no break */ deliberate_fall_through
@@ -113510,10 +114759,11 @@
113510114759
assert( k>=0 && k<pIdx->nColumn );
113511114760
i = pIdx->aiColumn[k];
113512114761
if( NEVER(i==XN_ROWID) ){
113513114762
VdbeComment((v,"%s.rowid",pIdx->zName));
113514114763
}else if( i==XN_EXPR ){
114764
+ assert( pIdx->bHasExpr );
113515114765
VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113516114766
}else{
113517114767
VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113518114768
}
113519114769
}
@@ -115825,11 +117075,11 @@
115825117075
if( p==0 ){
115826117076
#ifndef SQLITE_OMIT_VIRTUALTABLE
115827117077
/* If zName is the not the name of a table in the schema created using
115828117078
** CREATE, then check to see if it is the name of an virtual table that
115829117079
** can be an eponymous virtual table. */
115830
- if( pParse->disableVtab==0 && db->init.busy==0 ){
117080
+ if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
115831117081
Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115832117082
if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
115833117083
pMod = sqlite3PragmaVtabRegister(db, zName);
115834117084
}
115835117085
if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
@@ -115838,11 +117088,11 @@
115838117088
}
115839117089
}
115840117090
#endif
115841117091
if( flags & LOCATE_NOERR ) return 0;
115842117092
pParse->checkSchema = 1;
115843
- }else if( IsVirtual(p) && pParse->disableVtab ){
117093
+ }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
115844117094
p = 0;
115845117095
}
115846117096
115847117097
if( p==0 ){
115848117098
const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
@@ -117648,11 +118898,12 @@
117648118898
}
117649118899
117650118900
/* Recompute the colNotIdxed field of the Index.
117651118901
**
117652118902
** 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
118903
+** columns that are within the first 63 columns of the table and a 1 for
118904
+** all other bits (all columns that are not in the index). The
117654118905
** high-order bit of colNotIdxed is always 1. All unindexed columns
117655118906
** of the table have a 1.
117656118907
**
117657118908
** 2019-10-24: For the purpose of this computation, virtual columns are
117658118909
** not considered to be covered by the index, even if they are in the
@@ -117676,11 +118927,11 @@
117676118927
testcase( x==BMS-2 );
117677118928
if( x<BMS-1 ) m |= MASKBIT(x);
117678118929
}
117679118930
}
117680118931
pIdx->colNotIdxed = ~m;
117681
- assert( (pIdx->colNotIdxed>>63)==1 );
118932
+ assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
117682118933
}
117683118934
117684118935
/*
117685118936
** This routine runs at the end of parsing a CREATE TABLE statement that
117686118937
** has a WITHOUT ROWID clause. The job of this routine is to convert both
@@ -119564,10 +120815,11 @@
119564120815
pList = 0;
119565120816
}
119566120817
j = XN_EXPR;
119567120818
pIndex->aiColumn[i] = XN_EXPR;
119568120819
pIndex->uniqNotNull = 0;
120820
+ pIndex->bHasExpr = 1;
119569120821
}else{
119570120822
j = pCExpr->iColumn;
119571120823
assert( j<=0x7fff );
119572120824
if( j<0 ){
119573120825
j = pTab->iPKey;
@@ -119575,10 +120827,11 @@
119575120827
if( pTab->aCol[j].notNull==0 ){
119576120828
pIndex->uniqNotNull = 0;
119577120829
}
119578120830
if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
119579120831
pIndex->bHasVCol = 1;
120832
+ pIndex->bHasExpr = 1;
119580120833
}
119581120834
}
119582120835
pIndex->aiColumn[i] = (i16)j;
119583120836
}
119584120837
zColl = 0;
@@ -123352,11 +124605,11 @@
123352124605
**
123353124606
** For a case-insensitive search, set variable cx to be the same as
123354124607
** c but in the other case and search the input string for either
123355124608
** c or cx.
123356124609
*/
123357
- if( c<=0x80 ){
124610
+ if( c<0x80 ){
123358124611
char zStop[3];
123359124612
int bMatch;
123360124613
if( noCase ){
123361124614
zStop[0] = sqlite3Toupper(c);
123362124615
zStop[1] = sqlite3Tolower(c);
@@ -123435,19 +124688,31 @@
123435124688
/*
123436124689
** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
123437124690
** non-zero if there is no match.
123438124691
*/
123439124692
SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
123440
- return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124693
+ if( zString==0 ){
124694
+ return zGlobPattern!=0;
124695
+ }else if( zGlobPattern==0 ){
124696
+ return 1;
124697
+ }else {
124698
+ return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124699
+ }
123441124700
}
123442124701
123443124702
/*
123444124703
** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123445124704
** a miss - like strcmp().
123446124705
*/
123447124706
SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
123448
- return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124707
+ if( zStr==0 ){
124708
+ return zPattern!=0;
124709
+ }else if( zPattern==0 ){
124710
+ return 1;
124711
+ }else{
124712
+ return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124713
+ }
123449124714
}
123450124715
123451124716
/*
123452124717
** Count the number of times that the LIKE operator (or GLOB which is
123453124718
** just a variation of LIKE) gets called. This is used for testing
@@ -126572,10 +127837,11 @@
126572127837
aff = pTab->aCol[x].affinity;
126573127838
}else if( x==XN_ROWID ){
126574127839
aff = SQLITE_AFF_INTEGER;
126575127840
}else{
126576127841
assert( x==XN_EXPR );
127842
+ assert( pIdx->bHasExpr );
126577127843
assert( pIdx->aColExpr!=0 );
126578127844
aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126579127845
}
126580127846
if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
126581127847
if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
@@ -130146,10 +131412,12 @@
130146131412
int (*deserialize)(sqlite3*,const char*,unsigned char*,
130147131413
sqlite3_int64,sqlite3_int64,unsigned);
130148131414
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
130149131415
unsigned int);
130150131416
const char *(*db_name)(sqlite3*,int);
131417
+ /* Version 3.40.0 and later */
131418
+ int (*value_encoding)(sqlite3_value*);
130151131419
};
130152131420
130153131421
/*
130154131422
** This is the function signature used for all extension entry points. It
130155131423
** is also defined in the file "loadext.c".
@@ -130470,10 +131738,12 @@
130470131738
#ifndef SQLITE_OMIT_DESERIALIZE
130471131739
#define sqlite3_deserialize sqlite3_api->deserialize
130472131740
#define sqlite3_serialize sqlite3_api->serialize
130473131741
#endif
130474131742
#define sqlite3_db_name sqlite3_api->db_name
131743
+/* Version 3.40.0 and later */
131744
+#define sqlite3_value_encoding sqlite3_api->value_encoding
130475131745
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130476131746
130477131747
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130478131748
/* This case when the file really is being compiled as a loadable
130479131749
** extension */
@@ -130982,11 +132252,13 @@
130982132252
sqlite3_serialize,
130983132253
#else
130984132254
0,
130985132255
0,
130986132256
#endif
130987
- sqlite3_db_name
132257
+ sqlite3_db_name,
132258
+ /* Version 3.40.0 and later */
132259
+ sqlite3_value_type
130988132260
};
130989132261
130990132262
/* True if x is the directory separator character
130991132263
*/
130992132264
#if SQLITE_OS_WIN
@@ -133786,12 +135058,13 @@
133786135058
Index *pIdx, *pPk;
133787135059
Index *pPrior = 0; /* Previous index */
133788135060
int loopTop;
133789135061
int iDataCur, iIdxCur;
133790135062
int r1 = -1;
133791
- int bStrict;
135063
+ int bStrict; /* True for a STRICT table */
133792135064
int r2; /* Previous key for WITHOUT ROWID tables */
135065
+ int mxCol; /* Maximum non-virtual column number */
133793135066
133794135067
if( !IsOrdinaryTable(pTab) ) continue;
133795135068
if( pObjTab && pObjTab!=pTab ) continue;
133796135069
if( isQuick || HasRowid(pTab) ){
133797135070
pPk = 0;
@@ -133812,15 +135085,26 @@
133812135085
}
133813135086
assert( pParse->nMem>=8+j );
133814135087
assert( sqlite3NoTempsInRange(pParse,1,7+j) );
133815135088
sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
133816135089
loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
135090
+
135091
+ /* Fetch the right-most column from the table. This will cause
135092
+ ** the entire record header to be parsed and sanity checked. It
135093
+ ** will also prepopulate the cursor column cache that is used
135094
+ ** by the OP_IsType code, so it is a required step.
135095
+ */
135096
+ mxCol = pTab->nCol-1;
135097
+ while( mxCol>=0
135098
+ && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
135099
+ || pTab->iPKey==mxCol) ) mxCol--;
135100
+ if( mxCol>=0 ){
135101
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
135102
+ sqlite3VdbeTypeofColumn(v, 3);
135103
+ }
135104
+
133817135105
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)"));
133822135106
if( pPk ){
133823135107
/* Verify WITHOUT ROWID keys are in ascending order */
133824135108
int a1;
133825135109
char *zErr;
133826135110
a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
@@ -133836,48 +135120,126 @@
133836135120
for(j=0; j<pPk->nKeyCol; j++){
133837135121
sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
133838135122
}
133839135123
}
133840135124
}
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 */
135125
+ /* Verify datatypes for all columns:
135126
+ **
135127
+ ** (1) NOT NULL columns may not contain a NULL
135128
+ ** (2) Datatype must be exact for non-ANY columns in STRICT tables
135129
+ ** (3) Datatype for TEXT columns in non-STRICT tables must be
135130
+ ** NULL, TEXT, or BLOB.
135131
+ ** (4) Datatype for numeric columns in non-STRICT tables must not
135132
+ ** be a TEXT value that can be losslessly converted to numeric.
135133
+ */
133843135134
bStrict = (pTab->tabFlags & TF_Strict)!=0;
133844135135
for(j=0; j<pTab->nCol; j++){
133845135136
char *zErr;
133846
- Column *pCol = pTab->aCol + j;
133847
- int doError, jmp2;
135137
+ Column *pCol = pTab->aCol + j; /* The column to be checked */
135138
+ int labelError; /* Jump here to report an error */
135139
+ int labelOk; /* Jump here if all looks ok */
135140
+ int p1, p3, p4; /* Operands to the OP_IsType opcode */
135141
+ int doTypeCheck; /* Check datatypes (besides NOT NULL) */
135142
+
133848135143
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);
135144
+ if( bStrict ){
135145
+ doTypeCheck = pCol->eCType>COLTYPE_ANY;
135146
+ }else{
135147
+ doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
133854135148
}
135149
+ if( pCol->notNull==0 && !doTypeCheck ) continue;
135150
+
135151
+ /* Compute the operands that will be needed for OP_IsType */
135152
+ p4 = SQLITE_NULL;
135153
+ if( pCol->colFlags & COLFLAG_VIRTUAL ){
135154
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135155
+ p1 = -1;
135156
+ p3 = 3;
135157
+ }else{
135158
+ if( pCol->iDflt ){
135159
+ sqlite3_value *pDfltValue = 0;
135160
+ sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
135161
+ pCol->affinity, &pDfltValue);
135162
+ if( pDfltValue ){
135163
+ p4 = sqlite3_value_type(pDfltValue);
135164
+ sqlite3ValueFree(pDfltValue);
135165
+ }
135166
+ }
135167
+ p1 = iDataCur;
135168
+ if( !HasRowid(pTab) ){
135169
+ testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
135170
+ p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
135171
+ }else{
135172
+ p3 = sqlite3TableColumnToStorage(pTab,j);
135173
+ testcase( p3!=j);
135174
+ }
135175
+ }
135176
+
135177
+ labelError = sqlite3VdbeMakeLabel(pParse);
135178
+ labelOk = sqlite3VdbeMakeLabel(pParse);
133855135179
if( pCol->notNull ){
133856
- jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
135180
+ /* (1) NOT NULL columns may not contain a NULL */
135181
+ int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135182
+ sqlite3VdbeChangeP5(v, 0x0f);
135183
+ VdbeCoverage(v);
133857135184
zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133858135185
pCol->zCnName);
133859135186
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133860
- if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133861
- sqlite3VdbeGoto(v, doError);
135187
+ if( doTypeCheck ){
135188
+ sqlite3VdbeGoto(v, labelError);
135189
+ sqlite3VdbeJumpHere(v, jmp2);
133862135190
}else{
133863
- integrityCheckResultRow(v);
135191
+ /* VDBE byte code will fall thru */
133864135192
}
133865
- sqlite3VdbeJumpHere(v, jmp2);
133866135193
}
133867
- if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133868
- jmp2 = sqlite3VdbeAddOp3(v, OP_IsNullOrType, 3, 0,
133869
- sqlite3StdTypeMap[pCol->eCType-1]);
135194
+ if( bStrict && doTypeCheck ){
135195
+ /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
135196
+ static unsigned char aStdTypeMask[] = {
135197
+ 0x1f, /* ANY */
135198
+ 0x18, /* BLOB */
135199
+ 0x11, /* INT */
135200
+ 0x11, /* INTEGER */
135201
+ 0x13, /* REAL */
135202
+ 0x14 /* TEXT */
135203
+ };
135204
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135205
+ assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
135206
+ sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
133870135207
VdbeCoverage(v);
133871135208
zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133872135209
sqlite3StdType[pCol->eCType-1],
133873135210
pTab->zName, pTab->aCol[j].zCnName);
133874135211
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133875
- sqlite3VdbeResolveLabel(v, doError);
133876
- integrityCheckResultRow(v);
133877
- sqlite3VdbeJumpHere(v, jmp2);
135212
+ }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
135213
+ /* (3) Datatype for TEXT columns in non-STRICT tables must be
135214
+ ** NULL, TEXT, or BLOB. */
135215
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135216
+ sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135217
+ VdbeCoverage(v);
135218
+ zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
135219
+ pTab->zName, pTab->aCol[j].zCnName);
135220
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135221
+ }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
135222
+ /* (4) Datatype for numeric columns in non-STRICT tables must not
135223
+ ** be a TEXT value that can be converted to numeric. */
135224
+ sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135225
+ sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
135226
+ VdbeCoverage(v);
135227
+ if( p1>=0 ){
135228
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135229
+ }
135230
+ sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
135231
+ sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
135232
+ sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135233
+ VdbeCoverage(v);
135234
+ zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
135235
+ pTab->zName, pTab->aCol[j].zCnName);
135236
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
133878135237
}
135238
+ sqlite3VdbeResolveLabel(v, labelError);
135239
+ integrityCheckResultRow(v);
135240
+ sqlite3VdbeResolveLabel(v, labelOk);
133879135241
}
133880135242
/* Verify CHECK constraints */
133881135243
if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133882135244
ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133883135245
if( db->mallocFailed==0 ){
@@ -135493,11 +136855,11 @@
135493136855
*/
135494136856
if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
135495136857
sParse.disableLookaside++;
135496136858
DisableLookaside;
135497136859
}
135498
- sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
136860
+ sParse.prepFlags = prepFlags & 0xff;
135499136861
135500136862
/* Check to verify that it is possible to get a read lock on all
135501136863
** database schemas. The inability to get a read lock indicates that
135502136864
** some other database connection is holding a write-lock, which in
135503136865
** turn means that the other connection has made uncommitted changes
@@ -135534,11 +136896,13 @@
135534136896
}
135535136897
}
135536136898
}
135537136899
}
135538136900
135539
- sqlite3VtabUnlockList(db);
136901
+#ifndef SQLITE_OMIT_VIRTUALTABLE
136902
+ if( db->pDisconnect ) sqlite3VtabUnlockList(db);
136903
+#endif
135540136904
135541136905
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135542136906
char *zSqlCopy;
135543136907
int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
135544136908
testcase( nBytes==mxLen );
@@ -139603,10 +140967,11 @@
139603140967
Parse *pParse; /* The parsing context */
139604140968
int iTable; /* Replace references to this table */
139605140969
int iNewTable; /* New table number */
139606140970
int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
139607140971
ExprList *pEList; /* Replacement expressions */
140972
+ ExprList *pCList; /* Collation sequences for replacement expr */
139608140973
} SubstContext;
139609140974
139610140975
/* Forward Declarations */
139611140976
static void substExprList(SubstContext*, ExprList*);
139612140977
static void substSelect(SubstContext*, Select*, int);
@@ -139644,13 +141009,14 @@
139644141009
pExpr->op = TK_NULL;
139645141010
}else
139646141011
#endif
139647141012
{
139648141013
Expr *pNew;
139649
- Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
141014
+ int iColumn = pExpr->iColumn;
141015
+ Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
139650141016
Expr ifNullRow;
139651
- assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
141017
+ assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
139652141018
assert( pExpr->pRight==0 );
139653141019
if( sqlite3ExprIsVector(pCopy) ){
139654141020
sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139655141021
}else{
139656141022
sqlite3 *db = pSubst->pParse->db;
@@ -139684,15 +141050,20 @@
139684141050
ExprSetProperty(pExpr, EP_IntValue);
139685141051
}
139686141052
139687141053
/* Ensure that the expression now has an implicit collation sequence,
139688141054
** 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")
141055
+ {
141056
+ CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
141057
+ CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
141058
+ pSubst->pCList->a[iColumn].pExpr
139693141059
);
141060
+ if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
141061
+ pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
141062
+ (pColl ? pColl->zName : "BINARY")
141063
+ );
141064
+ }
139694141065
}
139695141066
ExprClearProperty(pExpr, EP_Collate);
139696141067
}
139697141068
}
139698141069
}else{
@@ -139880,10 +141251,22 @@
139880141251
w.xExprCallback = renumberCursorsCb;
139881141252
w.xSelectCallback = sqlite3SelectWalkNoop;
139882141253
sqlite3WalkSelect(&w, p);
139883141254
}
139884141255
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
141256
+
141257
+/*
141258
+** If pSel is not part of a compound SELECT, return a pointer to its
141259
+** expression list. Otherwise, return a pointer to the expression list
141260
+** of the leftmost SELECT in the compound.
141261
+*/
141262
+static ExprList *findLeftmostExprlist(Select *pSel){
141263
+ while( pSel->pPrior ){
141264
+ pSel = pSel->pPrior;
141265
+ }
141266
+ return pSel->pEList;
141267
+}
139885141268
139886141269
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
139887141270
/*
139888141271
** This routine attempts to flatten subqueries as a performance optimization.
139889141272
** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -140433,10 +141816,11 @@
140433141816
x.pParse = pParse;
140434141817
x.iTable = iParent;
140435141818
x.iNewTable = iNewParent;
140436141819
x.isOuterJoin = isOuterJoin;
140437141820
x.pEList = pSub->pEList;
141821
+ x.pCList = findLeftmostExprlist(pSub);
140438141822
substSelect(&x, pParent, 0);
140439141823
}
140440141824
140441141825
/* The flattened query is a compound if either the inner or the
140442141826
** outer query is a compound. */
@@ -140452,11 +141836,11 @@
140452141836
if( pSub->pLimit ){
140453141837
pParent->pLimit = pSub->pLimit;
140454141838
pSub->pLimit = 0;
140455141839
}
140456141840
140457
- /* Recompute the SrcList_item.colUsed masks for the flattened
141841
+ /* Recompute the SrcItem.colUsed masks for the flattened
140458141842
** tables. */
140459141843
for(i=0; i<nSubSrc; i++){
140460141844
recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140461141845
}
140462141846
}
@@ -140915,10 +142299,11 @@
140915142299
x.pParse = pParse;
140916142300
x.iTable = pSrc->iCursor;
140917142301
x.iNewTable = pSrc->iCursor;
140918142302
x.isOuterJoin = 0;
140919142303
x.pEList = pSubq->pEList;
142304
+ x.pCList = findLeftmostExprlist(pSubq);
140920142305
pNew = substExpr(&x, pNew);
140921142306
#ifndef SQLITE_OMIT_WINDOWFUNC
140922142307
if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140923142308
/* Restriction 6c has prevented push-down in this case */
140924142309
sqlite3ExprDelete(pParse->db, pNew);
@@ -141439,13 +142824,13 @@
141439142824
}
141440142825
}
141441142826
#endif
141442142827
141443142828
/*
141444
-** The SrcList_item structure passed as the second argument represents a
142829
+** The SrcItem structure passed as the second argument represents a
141445142830
** sub-query in the FROM clause of a SELECT statement. This function
141446
-** allocates and populates the SrcList_item.pTab object. If successful,
142831
+** allocates and populates the SrcItem.pTab object. If successful,
141447142832
** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
141448142833
** SQLITE_NOMEM.
141449142834
*/
141450142835
SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
141451142836
Select *pSel = pFrom->pSelect;
@@ -142274,11 +143659,11 @@
142274143659
#endif
142275143660
}
142276143661
142277143662
/*
142278143663
** 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,
143664
+** If it is, then return the SrcItem for the prior view. If it is not,
142280143665
** then return 0.
142281143666
*/
142282143667
static SrcItem *isSelfJoinView(
142283143668
SrcList *pTabList, /* Search for self-joins in this FROM clause */
142284143669
SrcItem *pThis /* Search for prior reference to this subquery */
@@ -143318,11 +144703,11 @@
143318144703
** in the right order to begin with.
143319144704
*/
143320144705
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
143321144706
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143322144707
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
143323
- 0, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144708
+ p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
143324144709
| (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
143325144710
);
143326144711
if( pWInfo==0 ){
143327144712
sqlite3ExprListDelete(db, pDistinct);
143328144713
goto select_end;
@@ -143617,11 +145002,11 @@
143617145002
assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
143618145003
assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
143619145004
143620145005
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
143621145006
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
143622
- pDistinct, 0, minMaxFlag|distFlag, 0);
145007
+ pDistinct, p, minMaxFlag|distFlag, 0);
143623145008
if( pWInfo==0 ){
143624145009
goto select_end;
143625145010
}
143626145011
SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
143627145012
eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -145101,11 +146486,11 @@
145101146486
sSubParse.pTriggerTab = pTab;
145102146487
sSubParse.pToplevel = pTop;
145103146488
sSubParse.zAuthContext = pTrigger->zName;
145104146489
sSubParse.eTriggerOp = pTrigger->op;
145105146490
sSubParse.nQueryLoop = pParse->nQueryLoop;
145106
- sSubParse.disableVtab = pParse->disableVtab;
146491
+ sSubParse.prepFlags = pParse->prepFlags;
145107146492
145108146493
v = sqlite3GetVdbe(&sSubParse);
145109146494
if( v ){
145110146495
VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
145111146496
pTrigger->zName, onErrorText(orconf),
@@ -145447,15 +146832,18 @@
145447146832
** (not a virtual table) then the value might have been stored as an
145448146833
** integer. In that case, add an OP_RealAffinity opcode to make sure
145449146834
** it has been converted into REAL.
145450146835
*/
145451146836
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
146837
+ Column *pCol;
145452146838
assert( pTab!=0 );
145453
- if( !IsView(pTab) ){
146839
+ assert( pTab->nCol>i );
146840
+ pCol = &pTab->aCol[i];
146841
+ if( pCol->iDflt ){
145454146842
sqlite3_value *pValue = 0;
145455146843
u8 enc = ENC(sqlite3VdbeDb(v));
145456
- Column *pCol = &pTab->aCol[i];
146844
+ assert( !IsView(pTab) );
145457146845
VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145458146846
assert( i<pTab->nCol );
145459146847
sqlite3ValueFromExpr(sqlite3VdbeDb(v),
145460146848
sqlite3ColumnExpr(pTab,pCol), enc,
145461146849
pCol->affinity, &pValue);
@@ -145462,11 +146850,11 @@
145462146850
if( pValue ){
145463146851
sqlite3VdbeAppendP4(v, pValue, P4_MEM);
145464146852
}
145465146853
}
145466146854
#ifndef SQLITE_OMIT_FLOATING_POINT
145467
- if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
146855
+ if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
145468146856
sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
145469146857
}
145470146858
#endif
145471146859
}
145472146860
@@ -146902,10 +148290,11 @@
146902148290
Expr *pExpr;
146903148291
sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146904148292
if( pIdx->aiColumn[ii]==XN_EXPR ){
146905148293
assert( pIdx->aColExpr!=0 );
146906148294
assert( pIdx->aColExpr->nExpr>ii );
148295
+ assert( pIdx->bHasExpr );
146907148296
pExpr = pIdx->aColExpr->a[ii].pExpr;
146908148297
if( pExpr->op!=TK_COLLATE ){
146909148298
sCol[0].pLeft = pExpr;
146910148299
pExpr = &sCol[0];
146911148300
}
@@ -147215,10 +148604,11 @@
147215148604
int isMemDb; /* True if vacuuming a :memory: database */
147216148605
int nRes; /* Bytes of reserved space at the end of each page */
147217148606
int nDb; /* Number of attached databases */
147218148607
const char *zDbMain; /* Schema name of database to vacuum */
147219148608
const char *zOut; /* Name of output file */
148609
+ u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
147220148610
147221148611
if( !db->autoCommit ){
147222148612
sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
147223148613
return SQLITE_ERROR; /* IMP: R-12218-18073 */
147224148614
}
@@ -147286,16 +148676,21 @@
147286148676
rc = SQLITE_ERROR;
147287148677
sqlite3SetString(pzErrMsg, db, "output file already exists");
147288148678
goto end_of_vacuum;
147289148679
}
147290148680
db->mDbFlags |= DBFLAG_VacuumInto;
148681
+
148682
+ /* For a VACUUM INTO, the pager-flags are set to the same values as
148683
+ ** they are for the database being vacuumed, except that PAGER_CACHESPILL
148684
+ ** is always set. */
148685
+ pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
147291148686
}
147292148687
nRes = sqlite3BtreeGetRequestedReserve(pMain);
147293148688
147294148689
sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
147295148690
sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
147296
- sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
148691
+ sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL);
147297148692
147298148693
/* Begin a transaction and take an exclusive lock on the main database
147299148694
** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
147300148695
** to ensure that we do not try to change the page-size on a WAL database.
147301148696
*/
@@ -148605,11 +150000,11 @@
148605150000
/* Check to see the left operand is a column in a virtual table */
148606150001
if( NEVER(pExpr==0) ) return pDef;
148607150002
if( pExpr->op!=TK_COLUMN ) return pDef;
148608150003
assert( ExprUseYTab(pExpr) );
148609150004
pTab = pExpr->y.pTab;
148610
- if( pTab==0 ) return pDef;
150005
+ if( NEVER(pTab==0) ) return pDef;
148611150006
if( !IsVirtual(pTab) ) return pDef;
148612150007
pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148613150008
assert( pVtab!=0 );
148614150009
assert( pVtab->pModule!=0 );
148615150010
pMod = (sqlite3_module *)pVtab->pModule;
@@ -149212,11 +150607,11 @@
149212150607
/*
149213150608
** An instance of the following structure keeps track of a mapping
149214150609
** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
149215150610
**
149216150611
** The VDBE cursor numbers are small integers contained in
149217
-** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
150612
+** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
149218150613
** clause, the cursor numbers might not begin with 0 and they might
149219150614
** contain gaps in the numbering sequence. But we want to make maximum
149220150615
** use of the bits in our bitmasks. This structure provides a mapping
149221150616
** from the sparse cursor numbers into consecutive integers beginning
149222150617
** with 0.
@@ -149283,24 +150678,10 @@
149283150678
#endif
149284150679
#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
149285150680
# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
149286150681
#endif
149287150682
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
-
149302150683
/*
149303150684
** The WHERE clause processing routine has two halves. The
149304150685
** first part does the start of the WHERE loop and the second
149305150686
** half does the tail of the WHERE loop. An instance of
149306150687
** this structure is returned by the first half and passed
@@ -149312,14 +150693,14 @@
149312150693
struct WhereInfo {
149313150694
Parse *pParse; /* Parsing and code generating context */
149314150695
SrcList *pTabList; /* List of tables in the join */
149315150696
ExprList *pOrderBy; /* The ORDER BY clause or NULL */
149316150697
ExprList *pResultSet; /* Result set of the query */
150698
+#if WHERETRACE_ENABLED
149317150699
Expr *pWhere; /* The complete WHERE clause */
149318
-#ifndef SQLITE_OMIT_VIRTUALTABLE
149319
- Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
149320150700
#endif
150701
+ Select *pSelect; /* The entire SELECT statement containing WHERE */
149321150702
int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
149322150703
int iContinue; /* Jump here to continue with next record */
149323150704
int iBreak; /* Jump here to break out of the loop */
149324150705
int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149325150706
u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -149334,11 +150715,10 @@
149334150715
unsigned sorted :1; /* True if really sorted (not just grouped) */
149335150716
LogEst nRowOut; /* Estimated number of output rows */
149336150717
int iTop; /* The very beginning of the WHERE loop */
149337150718
int iEndWhere; /* End of the WHERE clause itself */
149338150719
WhereLoop *pLoops; /* List of all WhereLoop objects */
149339
- WhereExprMod *pExprMods; /* Expression modifications */
149340150720
WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
149341150721
Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
149342150722
WhereClause sWC; /* Decomposition of the WHERE clause */
149343150723
WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
149344150724
WhereLevel a[1]; /* Information about each nest loop in WHERE */
@@ -150686,147 +152066,10 @@
150686152066
assert( nReg==1 || pParse->nErr );
150687152067
sqlite3ExprCode(pParse, p, iReg);
150688152068
}
150689152069
}
150690152070
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
-
150828152071
/*
150829152072
** The pTruth expression is always true because it is the WHERE clause
150830152073
** a partial index that is driving a query loop. Look through all of the
150831152074
** WHERE clause terms on the query, and if any of those terms must be
150832152075
** true because pTruth is true, then mark those WHERE clause terms as
@@ -150891,10 +152134,12 @@
150891152134
assert( pTerm!=0 );
150892152135
assert( pTerm->pExpr!=0 );
150893152136
testcase( pTerm->wtFlags & TERM_VIRTUAL );
150894152137
regRowid = sqlite3GetTempReg(pParse);
150895152138
regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
152139
+ sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
152140
+ VdbeCoverage(pParse->pVdbe);
150896152141
sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150897152142
addrNxt, regRowid, 1);
150898152143
VdbeCoverage(pParse->pVdbe);
150899152144
}else{
150900152145
u16 nEq = pLoop->u.btree.nEq;
@@ -151042,13 +152287,13 @@
151042152287
codeExprOrVector(pParse, pRight, iTarget, 1);
151043152288
if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
151044152289
&& pLoop->u.vtab.bOmitOffset
151045152290
){
151046152291
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);
152292
+ assert( pWInfo->pSelect!=0 );
152293
+ assert( pWInfo->pSelect->iOffset>0 );
152294
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
151050152295
VdbeComment((v,"Zero OFFSET counter"));
151051152296
}
151052152297
}
151053152298
}
151054152299
sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
@@ -151152,10 +152397,12 @@
151152152397
iReleaseReg = ++pParse->nMem;
151153152398
iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
151154152399
if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
151155152400
addrNxt = pLevel->addrNxt;
151156152401
if( pLevel->regFilter ){
152402
+ sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
152403
+ VdbeCoverage(v);
151157152404
sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
151158152405
iRowidReg, 1);
151159152406
VdbeCoverage(v);
151160152407
filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
151161152408
}
@@ -151503,10 +152750,15 @@
151503152750
** of entries in the tree, so basing the number of steps to try
151504152751
** on the estimated number of rows in the btree seems like a good
151505152752
** guess. */
151506152753
addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
151507152754
(pIdx->aiRowLogEst[0]+9)/10);
152755
+ if( pRangeStart ){
152756
+ sqlite3VdbeChangeP5(v, 1);
152757
+ sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
152758
+ addrSeekScan = 0;
152759
+ }
151508152760
VdbeCoverage(v);
151509152761
}
151510152762
sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
151511152763
VdbeCoverage(v);
151512152764
VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
@@ -151641,31 +152893,10 @@
151641152893
sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
151642152894
iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151643152895
}
151644152896
151645152897
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
-
151667152898
/* If a partial index is driving the loop, try to eliminate WHERE clause
151668152899
** terms from the query that must be true due to the WHERE clause of
151669152900
** the partial index.
151670152901
**
151671152902
** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
@@ -151774,11 +153005,11 @@
151774153005
*/
151775153006
if( pWInfo->nLevel>1 ){
151776153007
int nNotReady; /* The number of notReady tables */
151777153008
SrcItem *origSrc; /* Original list of tables */
151778153009
nNotReady = pWInfo->nLevel - iLevel - 1;
151779
- pOrTab = sqlite3StackAllocRaw(db,
153010
+ pOrTab = sqlite3DbMallocRawNN(db,
151780153011
sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
151781153012
if( pOrTab==0 ) return notReady;
151782153013
pOrTab->nAlloc = (u8)(nNotReady + 1);
151783153014
pOrTab->nSrc = pOrTab->nAlloc;
151784153015
memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
@@ -152027,11 +153258,11 @@
152027153258
** indent everything in between the this point and the final OP_Return.
152028153259
** See tag-20220407a in vdbe.c and shell.c */
152029153260
assert( pLevel->op==OP_Return );
152030153261
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
152031153262
152032
- if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
153263
+ if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
152033153264
if( !untestedTerms ) disableTerm(pLevel, pTerm);
152034153265
}else
152035153266
#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
152036153267
152037153268
{
@@ -152655,11 +153886,11 @@
152655153886
** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152656153887
*/
152657153888
if( pLeft->op!=TK_COLUMN
152658153889
|| sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
152659153890
|| (ALWAYS( ExprUseYTab(pLeft) )
152660
- && pLeft->y.pTab
153891
+ && ALWAYS(pLeft->y.pTab)
152661153892
&& IsVirtual(pLeft->y.pTab)) /* Might be numeric */
152662153893
){
152663153894
int isNum;
152664153895
double rDummy;
152665153896
isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
@@ -152772,12 +154003,11 @@
152772154003
**
152773154004
** vtab_column MATCH expression
152774154005
** MATCH(expression,vtab_column)
152775154006
*/
152776154007
pCol = pList->a[1].pExpr;
152777
- assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152778
- testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
154008
+ assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
152779154009
if( ExprIsVtab(pCol) ){
152780154010
for(i=0; i<ArraySize(aOp); i++){
152781154011
assert( !ExprHasProperty(pExpr, EP_IntValue) );
152782154012
if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152783154013
*peOp2 = aOp[i].eOp2;
@@ -152798,11 +154028,11 @@
152798154028
** names. But for this use case, xFindFunction is expected to deal
152799154029
** with function names in an arbitrary case.
152800154030
*/
152801154031
pCol = pList->a[0].pExpr;
152802154032
assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152803
- testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
154033
+ assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
152804154034
if( ExprIsVtab(pCol) ){
152805154035
sqlite3_vtab *pVtab;
152806154036
sqlite3_module *pMod;
152807154037
void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
152808154038
void *pNotUsed;
@@ -152823,17 +154053,16 @@
152823154053
}
152824154054
}else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
152825154055
int res = 0;
152826154056
Expr *pLeft = pExpr->pLeft;
152827154057
Expr *pRight = pExpr->pRight;
152828
- assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
152829
- testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
154058
+ assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
152830154059
if( ExprIsVtab(pLeft) ){
152831154060
res++;
152832154061
}
152833
- assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
152834
- testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
154062
+ assert( pRight==0 || pRight->op!=TK_COLUMN
154063
+ || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
152835154064
if( pRight && ExprIsVtab(pRight) ){
152836154065
res++;
152837154066
SWAP(Expr*, pLeft, pRight);
152838154067
}
152839154068
*ppLeft = pLeft;
@@ -153378,10 +154607,11 @@
153378154607
iCur = pFrom->a[i].iCursor;
153379154608
for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153380154609
if( pIdx->aColExpr==0 ) continue;
153381154610
for(i=0; i<pIdx->nKeyCol; i++){
153382154611
if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
154612
+ assert( pIdx->bHasExpr );
153383154613
if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153384154614
aiCurCol[0] = iCur;
153385154615
aiCurCol[1] = XN_EXPR;
153386154616
return 1;
153387154617
}
@@ -153991,13 +155221,13 @@
153991155221
**
153992155222
** LIMIT and OFFSET terms are ignored by most of the planner code. They
153993155223
** exist only so that they may be passed to the xBestIndex method of the
153994155224
** single virtual table in the FROM clause of the SELECT.
153995155225
*/
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 */
155226
+SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
155227
+ assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
155228
+ if( p->pGroupBy==0
153999155229
&& (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
154000155230
&& (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
154001155231
){
154002155232
ExprList *pOrderBy = p->pOrderBy;
154003155233
int iCsr = p->pSrc->a[0].iCursor;
@@ -156530,30 +157760,18 @@
156530157760
while( pWInfo->pLoops ){
156531157761
WhereLoop *p = pWInfo->pLoops;
156532157762
pWInfo->pLoops = p->pNextLoop;
156533157763
whereLoopDelete(db, p);
156534157764
}
156535
- assert( pWInfo->pExprMods==0 );
156536157765
while( pWInfo->pMemToFree ){
156537157766
WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156538157767
sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
156539157768
pWInfo->pMemToFree = pNext;
156540157769
}
156541157770
sqlite3DbNNFreeNN(db, pWInfo);
156542157771
}
156543157772
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
-
156555157773
/*
156556157774
** Return TRUE if all of the following are true:
156557157775
**
156558157776
** (1) X has the same or lower cost, or returns the same or fewer rows,
156559157777
** than Y.
@@ -157501,10 +158719,98 @@
157501158719
return 1;
157502158720
}
157503158721
}
157504158722
return 0;
157505158723
}
158724
+
158725
+/*
158726
+** Structure passed to the whereIsCoveringIndex Walker callback.
158727
+*/
158728
+struct CoveringIndexCheck {
158729
+ Index *pIdx; /* The index */
158730
+ int iTabCur; /* Cursor number for the corresponding table */
158731
+};
158732
+
158733
+/*
158734
+** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
158735
+**
158736
+** If the Expr node references the table with cursor pCk->iTabCur, then
158737
+** make sure that column is covered by the index pCk->pIdx. We know that
158738
+** all columns less than 63 (really BMS-1) are covered, so we don't need
158739
+** to check them. But we do need to check any column at 63 or greater.
158740
+**
158741
+** If the index does not cover the column, then set pWalk->eCode to
158742
+** non-zero and return WRC_Abort to stop the search.
158743
+**
158744
+** If this node does not disprove that the index can be a covering index,
158745
+** then just return WRC_Continue, to continue the search.
158746
+*/
158747
+static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158748
+ int i; /* Loop counter */
158749
+ const Index *pIdx; /* The index of interest */
158750
+ const i16 *aiColumn; /* Columns contained in the index */
158751
+ u16 nColumn; /* Number of columns in the index */
158752
+ if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158753
+ if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158754
+ if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158755
+ pIdx = pWalk->u.pCovIdxCk->pIdx;
158756
+ aiColumn = pIdx->aiColumn;
158757
+ nColumn = pIdx->nColumn;
158758
+ for(i=0; i<nColumn; i++){
158759
+ if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158760
+ }
158761
+ pWalk->eCode = 1;
158762
+ return WRC_Abort;
158763
+}
158764
+
158765
+
158766
+/*
158767
+** pIdx is an index that covers all of the low-number columns used by
158768
+** pWInfo->pSelect (columns from 0 through 62). But there are columns
158769
+** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158770
+** of whether pIdx covers *all* columns in the query.
158771
+**
158772
+** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158773
+** not a covering index or if we are unable to determine if pIdx is a
158774
+** covering index.
158775
+**
158776
+** This routine is an optimization. It is always safe to return non-zero.
158777
+** But returning zero when non-zero should have been returned can lead to
158778
+** incorrect bytecode and assertion faults.
158779
+*/
158780
+static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158781
+ WhereInfo *pWInfo, /* The WHERE clause context */
158782
+ Index *pIdx, /* Index that is being tested */
158783
+ int iTabCur /* Cursor for the table being indexed */
158784
+){
158785
+ int i;
158786
+ struct CoveringIndexCheck ck;
158787
+ Walker w;
158788
+ if( pWInfo->pSelect==0 ){
158789
+ /* We don't have access to the full query, so we cannot check to see
158790
+ ** if pIdx is covering. Assume it is not. */
158791
+ return 1;
158792
+ }
158793
+ for(i=0; i<pIdx->nColumn; i++){
158794
+ if( pIdx->aiColumn[i]>=BMS-1 ) break;
158795
+ }
158796
+ if( i>=pIdx->nColumn ){
158797
+ /* pIdx does not index any columns greater than 62, but we know from
158798
+ ** colMask that columns greater than 62 are used, so this is not a
158799
+ ** covering index */
158800
+ return 1;
158801
+ }
158802
+ ck.pIdx = pIdx;
158803
+ ck.iTabCur = iTabCur;
158804
+ memset(&w, 0, sizeof(w));
158805
+ w.xExprCallback = whereIsCoveringIndexWalkCallback;
158806
+ w.xSelectCallback = sqlite3SelectWalkNoop;
158807
+ w.u.pCovIdxCk = &ck;
158808
+ w.eCode = 0;
158809
+ sqlite3WalkSelect(&w, pWInfo->pSelect);
158810
+ return w.eCode;
158811
+}
157506158812
157507158813
/*
157508158814
** Add all WhereLoop objects for a single table of the join where the table
157509158815
** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157510158816
** a b-tree table, not a virtual table.
@@ -157719,10 +159025,13 @@
157719159025
if( pProbe->isCovering ){
157720159026
pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
157721159027
m = 0;
157722159028
}else{
157723159029
m = pSrc->colUsed & pProbe->colNotIdxed;
159030
+ if( m==TOPBIT ){
159031
+ m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159032
+ }
157724159033
pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
157725159034
}
157726159035
157727159036
/* Full scan via index */
157728159037
if( b
@@ -158944,11 +160253,10 @@
158944160253
*/
158945160254
static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
158946160255
int mxChoice; /* Maximum number of simultaneous paths tracked */
158947160256
int nLoop; /* Number of terms in the join */
158948160257
Parse *pParse; /* Parsing context */
158949
- sqlite3 *db; /* The database connection */
158950160258
int iLoop; /* Loop counter over the terms of the join */
158951160259
int ii, jj; /* Loop counters */
158952160260
int mxI = 0; /* Index of next entry to replace */
158953160261
int nOrderBy; /* Number of ORDER BY clause terms */
158954160262
LogEst mxCost = 0; /* Maximum cost of a set of paths */
@@ -158963,11 +160271,10 @@
158963160271
LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
158964160272
char *pSpace; /* Temporary memory used by this routine */
158965160273
int nSpace; /* Bytes of space allocated at pSpace */
158966160274
158967160275
pParse = pWInfo->pParse;
158968
- db = pParse->db;
158969160276
nLoop = pWInfo->nLevel;
158970160277
/* TUNING: For simple queries, only the best path is tracked.
158971160278
** For 2-way joins, the 5 best paths are followed.
158972160279
** For joins of 3 or more tables, track the 10 best paths */
158973160280
mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
@@ -158986,11 +160293,11 @@
158986160293
}
158987160294
158988160295
/* Allocate and initialize space for aTo, aFrom and aSortCost[] */
158989160296
nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
158990160297
nSpace += sizeof(LogEst) * nOrderBy;
158991
- pSpace = sqlite3DbMallocRawNN(db, nSpace);
160298
+ pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
158992160299
if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
158993160300
aTo = (WherePath*)pSpace;
158994160301
aFrom = aTo+mxChoice;
158995160302
memset(aFrom, 0, sizeof(aFrom[0]));
158996160303
pX = (WhereLoop**)(aFrom+mxChoice);
@@ -159244,11 +160551,11 @@
159244160551
nFrom = nTo;
159245160552
}
159246160553
159247160554
if( nFrom==0 ){
159248160555
sqlite3ErrorMsg(pParse, "no query solution");
159249
- sqlite3DbFreeNN(db, pSpace);
160556
+ sqlite3StackFreeNN(pParse->db, pSpace);
159250160557
return SQLITE_ERROR;
159251160558
}
159252160559
159253160560
/* Find the lowest cost path. pFrom will be left pointing to that path */
159254160561
pFrom = aFrom;
@@ -159326,12 +160633,11 @@
159326160633
159327160634
159328160635
pWInfo->nRowOut = pFrom->nRow;
159329160636
159330160637
/* Free temporary memory and return success */
159331
- assert( db!=0 );
159332
- sqlite3DbNNFreeNN(db, pSpace);
160638
+ sqlite3StackFreeNN(pParse->db, pSpace);
159333160639
return SQLITE_OK;
159334160640
}
159335160641
159336160642
/*
159337160643
** Most queries use only a single table (they are not joins) and have
@@ -159625,10 +160931,81 @@
159625160931
}
159626160932
}
159627160933
nSearch += pLoop->nOut;
159628160934
}
159629160935
}
160936
+
160937
+/*
160938
+** This is an sqlite3ParserAddCleanup() callback that is invoked to
160939
+** free the Parse->pIdxExpr list when the Parse object is destroyed.
160940
+*/
160941
+static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
160942
+ Parse *pParse = (Parse*)pObject;
160943
+ while( pParse->pIdxExpr!=0 ){
160944
+ IndexedExpr *p = pParse->pIdxExpr;
160945
+ pParse->pIdxExpr = p->pIENext;
160946
+ sqlite3ExprDelete(db, p->pExpr);
160947
+ sqlite3DbFreeNN(db, p);
160948
+ }
160949
+}
160950
+
160951
+/*
160952
+** The index pIdx is used by a query and contains one or more expressions.
160953
+** In other words pIdx is an index on an expression. iIdxCur is the cursor
160954
+** number for the index and iDataCur is the cursor number for the corresponding
160955
+** table.
160956
+**
160957
+** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
160958
+** each of the expressions in the index so that the expression code generator
160959
+** will know to replace occurrences of the indexed expression with
160960
+** references to the corresponding column of the index.
160961
+*/
160962
+static SQLITE_NOINLINE void whereAddIndexedExpr(
160963
+ Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
160964
+ Index *pIdx, /* The index-on-expression that contains the expressions */
160965
+ int iIdxCur, /* Cursor number for pIdx */
160966
+ SrcItem *pTabItem /* The FROM clause entry for the table */
160967
+){
160968
+ int i;
160969
+ IndexedExpr *p;
160970
+ Table *pTab;
160971
+ assert( pIdx->bHasExpr );
160972
+ pTab = pIdx->pTable;
160973
+ for(i=0; i<pIdx->nColumn; i++){
160974
+ Expr *pExpr;
160975
+ int j = pIdx->aiColumn[i];
160976
+ int bMaybeNullRow;
160977
+ if( j==XN_EXPR ){
160978
+ pExpr = pIdx->aColExpr->a[i].pExpr;
160979
+ testcase( pTabItem->fg.jointype & JT_LEFT );
160980
+ testcase( pTabItem->fg.jointype & JT_RIGHT );
160981
+ testcase( pTabItem->fg.jointype & JT_LTORJ );
160982
+ bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
160983
+ }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
160984
+ pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
160985
+ bMaybeNullRow = 0;
160986
+ }else{
160987
+ continue;
160988
+ }
160989
+ if( sqlite3ExprIsConstant(pExpr) ) continue;
160990
+ p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
160991
+ if( p==0 ) break;
160992
+ p->pIENext = pParse->pIdxExpr;
160993
+ p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
160994
+ p->iDataCur = pTabItem->iCursor;
160995
+ p->iIdxCur = iIdxCur;
160996
+ p->iIdxCol = i;
160997
+ p->bMaybeNullRow = bMaybeNullRow;
160998
+#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
160999
+ p->zIdxName = pIdx->zName;
161000
+#endif
161001
+ pParse->pIdxExpr = p;
161002
+ if( p->pIENext==0 ){
161003
+ sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161004
+ }
161005
+ }
161006
+}
159630161007
159631161008
/*
159632161009
** Generate the beginning of the loop used for WHERE clause processing.
159633161010
** The return value is a pointer to an opaque structure that contains
159634161011
** information needed to terminate the loop. Later, the calling routine
@@ -159720,11 +161097,11 @@
159720161097
Parse *pParse, /* The parser context */
159721161098
SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
159722161099
Expr *pWhere, /* The WHERE clause */
159723161100
ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
159724161101
ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
159725
- Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
161102
+ Select *pSelect, /* The entire SELECT statement */
159726161103
u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
159727161104
int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
159728161105
** If WHERE_USE_LIMIT, then the limit amount */
159729161106
){
159730161107
int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -159789,21 +161166,21 @@
159789161166
goto whereBeginError;
159790161167
}
159791161168
pWInfo->pParse = pParse;
159792161169
pWInfo->pTabList = pTabList;
159793161170
pWInfo->pOrderBy = pOrderBy;
161171
+#if WHERETRACE_ENABLED
159794161172
pWInfo->pWhere = pWhere;
161173
+#endif
159795161174
pWInfo->pResultSet = pResultSet;
159796161175
pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159797161176
pWInfo->nLevel = nTabList;
159798161177
pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159799161178
pWInfo->wctrlFlags = wctrlFlags;
159800161179
pWInfo->iLimit = iAuxArg;
159801161180
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159802
-#ifndef SQLITE_OMIT_VIRTUALTABLE
159803
- pWInfo->pLimit = pLimit;
159804
-#endif
161181
+ pWInfo->pSelect = pSelect;
159805161182
memset(&pWInfo->nOBSat, 0,
159806161183
offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
159807161184
memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159808161185
assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
159809161186
pMaskSet = &pWInfo->sMaskSet;
@@ -159868,11 +161245,13 @@
159868161245
#endif
159869161246
}
159870161247
159871161248
/* Analyze all of the subexpressions. */
159872161249
sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159873
- sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
161250
+ if( pSelect && pSelect->pLimit ){
161251
+ sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
161252
+ }
159874161253
if( pParse->nErr ) goto whereBeginError;
159875161254
159876161255
/* Special case: WHERE terms that do not refer to any tables in the join
159877161256
** (constant expressions). Evaluate each such term, and jump over all the
159878161257
** generated code if the result is not true.
@@ -160171,10 +161550,13 @@
160171161550
}else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
160172161551
iIndexCur = iAuxArg;
160173161552
op = OP_ReopenIdx;
160174161553
}else{
160175161554
iIndexCur = pParse->nTab++;
161555
+ if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
161556
+ whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
161557
+ }
160176161558
}
160177161559
pLevel->iIdxCur = iIndexCur;
160178161560
assert( pIx!=0 );
160179161561
assert( pIx->pSchema==pTab->pSchema );
160180161562
assert( iIndexCur>=0 );
@@ -160293,12 +161675,10 @@
160293161675
return pWInfo;
160294161676
160295161677
/* Jump here if malloc fails */
160296161678
whereBeginError:
160297161679
if( pWInfo ){
160298
- testcase( pWInfo->pExprMods!=0 );
160299
- whereUndoExprMods(pWInfo);
160300161680
pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160301161681
whereInfoFree(db, pWInfo);
160302161682
}
160303161683
return 0;
160304161684
}
@@ -160513,11 +161893,10 @@
160513161893
VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160514161894
pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160515161895
}
160516161896
160517161897
assert( pWInfo->nLevel<=pTabList->nSrc );
160518
- if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160519161898
for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160520161899
int k, last;
160521161900
VdbeOp *pOp, *pLastOp;
160522161901
Index *pIdx = 0;
160523161902
SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
@@ -160566,10 +161945,20 @@
160566161945
){
160567161946
if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
160568161947
last = iEnd;
160569161948
}else{
160570161949
last = pWInfo->iEndWhere;
161950
+ }
161951
+ if( pIdx->bHasExpr ){
161952
+ IndexedExpr *p = pParse->pIdxExpr;
161953
+ while( p ){
161954
+ if( p->iIdxCur==pLevel->iIdxCur ){
161955
+ p->iDataCur = -1;
161956
+ p->iIdxCur = -1;
161957
+ }
161958
+ p = p->pIENext;
161959
+ }
160571161960
}
160572161961
k = pLevel->addrBody + 1;
160573161962
#ifdef SQLITE_DEBUG
160574161963
if( db->flags & SQLITE_VdbeAddopTrace ){
160575161964
printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
@@ -174293,10 +175682,23 @@
174293175682
createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
174294175683
if( db->mallocFailed ){
174295175684
goto opendb_out;
174296175685
}
174297175686
175687
+#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
175688
+ /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
175689
+ if( zFilename && zFilename[0]==':' ){
175690
+ if( strcmp(zFilename, ":localStorage:")==0 ){
175691
+ zFilename = "file:local?vfs=kvvfs";
175692
+ flags |= SQLITE_OPEN_URI;
175693
+ }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
175694
+ zFilename = "file:session?vfs=kvvfs";
175695
+ flags |= SQLITE_OPEN_URI;
175696
+ }
175697
+ }
175698
+#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
175699
+
174298175700
/* Parse the filename/URI argument
174299175701
**
174300175702
** Only allow sensible combinations of bits in the flags argument.
174301175703
** Throw an error if any non-sense combination is used. If we
174302175704
** do not block illegal combinations here, it could trigger
@@ -175722,12 +177124,12 @@
175722177124
** Recover as many snapshots as possible from the wal file associated with
175723177125
** schema zDb of database db.
175724177126
*/
175725177127
SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
175726177128
int rc = SQLITE_ERROR;
175727
- int iDb;
175728177129
#ifndef SQLITE_OMIT_WAL
177130
+ int iDb;
175729177131
175730177132
#ifdef SQLITE_ENABLE_API_ARMOR
175731177133
if( !sqlite3SafetyCheckOk(db) ){
175732177134
return SQLITE_MISUSE_BKPT;
175733177135
}
@@ -201626,11 +203028,11 @@
201626203028
return SQLITE_LOCKED_VTAB;
201627203029
}
201628203030
rtreeReference(pRtree);
201629203031
assert(nData>=1);
201630203032
201631
- cell.iRowid = 0; /* Used only to suppress a compiler warning */
203033
+ memset(&cell, 0, sizeof(cell));
201632203034
201633203035
/* Constraint handling. A write operation on an r-tree table may return
201634203036
** SQLITE_CONSTRAINT for two reasons:
201635203037
**
201636203038
** 1. A duplicate rowid value, or
@@ -236984,11 +238386,11 @@
236984238386
int nArg, /* Number of args */
236985238387
sqlite3_value **apUnused /* Function arguments */
236986238388
){
236987238389
assert( nArg==0 );
236988238390
UNUSED_PARAM2(nArg, apUnused);
236989
- sqlite3_result_text(pCtx, "fts5: 2022-09-28 19:14:01 f25cf63471cbed1edb27591e57fead62550d4046dbdcb61312288f0f6f24c646", -1, SQLITE_TRANSIENT);
238391
+ sqlite3_result_text(pCtx, "fts5: 2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd", -1, SQLITE_TRANSIENT);
236990238392
}
236991238393
236992238394
/*
236993238395
** Return true if zName is the extension on one of the shadow tables used
236994238396
** by this module.
236995238397
--- 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 */
@@ -20324,11 +20420,10 @@
20324 #ifndef SQLITE_AMALGAMATION
20325 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
20326 SQLITE_PRIVATE const char sqlite3StrBINARY[];
20327 SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[];
20328 SQLITE_PRIVATE const char sqlite3StdTypeAffinity[];
20329 SQLITE_PRIVATE const char sqlite3StdTypeMap[];
20330 SQLITE_PRIVATE const char *sqlite3StdType[];
20331 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
20332 SQLITE_PRIVATE const unsigned char *sqlite3aLTb;
20333 SQLITE_PRIVATE const unsigned char *sqlite3aEQb;
20334 SQLITE_PRIVATE const unsigned char *sqlite3aGTb;
@@ -20767,10 +20862,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 +21098,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 +21263,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",
@@ -22133,14 +22235,10 @@
22133 ** sqlite3StdTypeLen[] The length (in bytes) of each entry
22134 ** in sqlite3StdType[].
22135 **
22136 ** sqlite3StdTypeAffinity[] The affinity associated with each entry
22137 ** in sqlite3StdType[].
22138 **
22139 ** sqlite3StdTypeMap[] The type value (as returned from
22140 ** sqlite3_column_type() or sqlite3_value_type())
22141 ** for each entry in sqlite3StdType[].
22142 */
22143 SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
22144 SQLITE_PRIVATE const char sqlite3StdTypeAffinity[] = {
22145 SQLITE_AFF_NUMERIC,
22146 SQLITE_AFF_BLOB,
@@ -22147,18 +22245,10 @@
22147 SQLITE_AFF_INTEGER,
22148 SQLITE_AFF_INTEGER,
22149 SQLITE_AFF_REAL,
22150 SQLITE_AFF_TEXT
22151 };
22152 SQLITE_PRIVATE const char sqlite3StdTypeMap[] = {
22153 0,
22154 SQLITE_BLOB,
22155 SQLITE_INTEGER,
22156 SQLITE_INTEGER,
22157 SQLITE_FLOAT,
22158 SQLITE_TEXT
22159 };
22160 SQLITE_PRIVATE const char *sqlite3StdType[] = {
22161 "ANY",
22162 "BLOB",
22163 "INT",
22164 "INTEGER",
@@ -24740,13 +24830,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 +35474,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 +35649,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 +36711,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 +44689,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 +71789,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 +74126,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 +76816,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 +80747,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 +81882,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 +83055,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 +86486,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 +86511,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 +86592,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 +87541,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 +92181,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 +92365,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 +94318,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 +94387,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 +94463,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 +94515,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 +105068,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 +105187,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 +108799,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 +109049,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 +109124,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 +109174,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 +109237,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 +110294,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 +110469,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 +110623,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 +110931,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 +114759,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 +117075,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 +117088,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 +118898,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 +118927,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 +120815,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 +120827,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 +124605,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 +124688,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 +127837,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 +131412,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 +131738,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 +132252,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 +135058,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 +135085,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 +135120,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 +136855,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 +136896,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 +140967,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 +141009,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 +141050,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 +141251,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 +141816,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 +141836,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 +142299,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 +142824,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 +143659,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 +144703,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 +145002,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 +146486,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 +146832,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 +146850,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 +148290,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 +148604,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 +148676,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 +150000,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 +150607,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 +150678,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 +150693,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 +150715,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 +152066,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 +152134,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 +152287,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 +152397,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 +152750,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 +152893,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 +153005,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 +153258,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 +153886,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 +154003,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 +154028,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 +154053,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 +154607,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 +155221,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 +157760,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 +158719,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 +159025,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 +160253,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 +160271,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 +160293,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 +160551,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 +160633,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 +160931,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 +161097,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 +161166,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 +161245,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 +161550,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 +161675,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 +161893,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 +161945,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 +175682,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 +177124,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 +203028,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 +238386,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 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
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 */
@@ -20324,11 +20420,10 @@
20420 #ifndef SQLITE_AMALGAMATION
20421 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
20422 SQLITE_PRIVATE const char sqlite3StrBINARY[];
20423 SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[];
20424 SQLITE_PRIVATE const char sqlite3StdTypeAffinity[];
 
20425 SQLITE_PRIVATE const char *sqlite3StdType[];
20426 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
20427 SQLITE_PRIVATE const unsigned char *sqlite3aLTb;
20428 SQLITE_PRIVATE const unsigned char *sqlite3aEQb;
20429 SQLITE_PRIVATE const unsigned char *sqlite3aGTb;
@@ -20767,10 +20862,14 @@
20862 SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
20863
20864 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
20865 SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
20866 #endif
20867
20868 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
20869 SQLITE_PRIVATE int sqlite3KvvfsInit(void);
20870 #endif
20871
20872 #endif /* SQLITEINT_H */
20873
20874 /************** End of sqliteInt.h *******************************************/
20875 /************** Begin file os_common.h ***************************************/
@@ -20999,11 +21098,11 @@
21098 /*
21099 ** Include the configuration header output by 'configure' if we're using the
21100 ** autoconf-based build
21101 */
21102 #if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
21103 /* #include "sqlite_cfg.h" */
21104 #define SQLITECONFIG_H 1
21105 #endif
21106
21107 /* These macros are provided to "stringify" the value of the define
21108 ** for those options in which the value is meaningful. */
@@ -21164,10 +21263,13 @@
21263 "DISABLE_PAGECACHE_OVERFLOW_STATS",
21264 #endif
21265 #ifdef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
21266 "DISABLE_SKIPAHEAD_DISTINCT",
21267 #endif
21268 #ifdef SQLITE_DQS
21269 "DQS=" CTIMEOPT_VAL(SQLITE_DQS),
21270 #endif
21271 #ifdef SQLITE_ENABLE_8_3_NAMES
21272 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
21273 #endif
21274 #ifdef SQLITE_ENABLE_API_ARMOR
21275 "ENABLE_API_ARMOR",
@@ -22133,14 +22235,10 @@
22235 ** sqlite3StdTypeLen[] The length (in bytes) of each entry
22236 ** in sqlite3StdType[].
22237 **
22238 ** sqlite3StdTypeAffinity[] The affinity associated with each entry
22239 ** in sqlite3StdType[].
 
 
 
 
22240 */
22241 SQLITE_PRIVATE const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 };
22242 SQLITE_PRIVATE const char sqlite3StdTypeAffinity[] = {
22243 SQLITE_AFF_NUMERIC,
22244 SQLITE_AFF_BLOB,
@@ -22147,18 +22245,10 @@
22245 SQLITE_AFF_INTEGER,
22246 SQLITE_AFF_INTEGER,
22247 SQLITE_AFF_REAL,
22248 SQLITE_AFF_TEXT
22249 };
 
 
 
 
 
 
 
 
22250 SQLITE_PRIVATE const char *sqlite3StdType[] = {
22251 "ANY",
22252 "BLOB",
22253 "INT",
22254 "INTEGER",
@@ -24740,13 +24830,15 @@
24830 DO_OS_MALLOC_TEST(id);
24831 return id->pMethods->xFileSize(id, pSize);
24832 }
24833 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
24834 DO_OS_MALLOC_TEST(id);
24835 assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE );
24836 return id->pMethods->xLock(id, lockType);
24837 }
24838 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
24839 assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED );
24840 return id->pMethods->xUnlock(id, lockType);
24841 }
24842 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
24843 DO_OS_MALLOC_TEST(id);
24844 return id->pMethods->xCheckReservedLock(id, pResOut);
@@ -35382,11 +35474,11 @@
35474 /* 13 */ "MustBeInt" OpHelp(""),
35475 /* 14 */ "Jump" OpHelp(""),
35476 /* 15 */ "Once" OpHelp(""),
35477 /* 16 */ "If" OpHelp(""),
35478 /* 17 */ "IfNot" OpHelp(""),
35479 /* 18 */ "IsType" OpHelp("if typeof(P1.P3) in P5 goto P2"),
35480 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
35481 /* 20 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
35482 /* 21 */ "SeekLT" OpHelp("key=r[P3@P4]"),
35483 /* 22 */ "SeekLE" OpHelp("key=r[P3@P4]"),
35484 /* 23 */ "SeekGE" OpHelp("key=r[P3@P4]"),
@@ -35557,10 +35649,982 @@
35649 return azName[i];
35650 }
35651 #endif
35652
35653 /************** End of opcodes.c *********************************************/
35654 /************** Begin file os_kv.c *******************************************/
35655 /*
35656 ** 2022-09-06
35657 **
35658 ** The author disclaims copyright to this source code. In place of
35659 ** a legal notice, here is a blessing:
35660 **
35661 ** May you do good and not evil.
35662 ** May you find forgiveness for yourself and forgive others.
35663 ** May you share freely, never taking more than you give.
35664 **
35665 ******************************************************************************
35666 **
35667 ** This file contains an experimental VFS layer that operates on a
35668 ** Key/Value storage engine where both keys and values must be pure
35669 ** text.
35670 */
35671 /* #include <sqliteInt.h> */
35672 #if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL))
35673
35674 /*****************************************************************************
35675 ** Debugging logic
35676 */
35677
35678 /* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */
35679 #if 0
35680 #define SQLITE_KV_TRACE(X) printf X
35681 #else
35682 #define SQLITE_KV_TRACE(X)
35683 #endif
35684
35685 /* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */
35686 #if 0
35687 #define SQLITE_KV_LOG(X) printf X
35688 #else
35689 #define SQLITE_KV_LOG(X)
35690 #endif
35691
35692
35693 /*
35694 ** Forward declaration of objects used by this VFS implementation
35695 */
35696 typedef struct KVVfsFile KVVfsFile;
35697
35698 /* A single open file. There are only two files represented by this
35699 ** VFS - the database and the rollback journal.
35700 */
35701 struct KVVfsFile {
35702 sqlite3_file base; /* IO methods */
35703 const char *zClass; /* Storage class */
35704 int isJournal; /* True if this is a journal file */
35705 unsigned int nJrnl; /* Space allocated for aJrnl[] */
35706 char *aJrnl; /* Journal content */
35707 int szPage; /* Last known page size */
35708 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35709 };
35710
35711 /*
35712 ** Methods for KVVfsFile
35713 */
35714 static int kvvfsClose(sqlite3_file*);
35715 static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35716 static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
35717 static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35718 static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64);
35719 static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size);
35720 static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size);
35721 static int kvvfsSyncDb(sqlite3_file*, int flags);
35722 static int kvvfsSyncJrnl(sqlite3_file*, int flags);
35723 static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize);
35724 static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize);
35725 static int kvvfsLock(sqlite3_file*, int);
35726 static int kvvfsUnlock(sqlite3_file*, int);
35727 static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut);
35728 static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg);
35729 static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg);
35730 static int kvvfsSectorSize(sqlite3_file*);
35731 static int kvvfsDeviceCharacteristics(sqlite3_file*);
35732
35733 /*
35734 ** Methods for sqlite3_vfs
35735 */
35736 static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
35737 static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir);
35738 static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *);
35739 static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
35740 static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename);
35741 static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut);
35742 static int kvvfsSleep(sqlite3_vfs*, int microseconds);
35743 static int kvvfsCurrentTime(sqlite3_vfs*, double*);
35744 static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
35745
35746 static sqlite3_vfs sqlite3OsKvvfsObject = {
35747 1, /* iVersion */
35748 sizeof(KVVfsFile), /* szOsFile */
35749 1024, /* mxPathname */
35750 0, /* pNext */
35751 "kvvfs", /* zName */
35752 0, /* pAppData */
35753 kvvfsOpen, /* xOpen */
35754 kvvfsDelete, /* xDelete */
35755 kvvfsAccess, /* xAccess */
35756 kvvfsFullPathname, /* xFullPathname */
35757 kvvfsDlOpen, /* xDlOpen */
35758 0, /* xDlError */
35759 0, /* xDlSym */
35760 0, /* xDlClose */
35761 kvvfsRandomness, /* xRandomness */
35762 kvvfsSleep, /* xSleep */
35763 kvvfsCurrentTime, /* xCurrentTime */
35764 0, /* xGetLastError */
35765 kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */
35766 };
35767
35768 /* Methods for sqlite3_file objects referencing a database file
35769 */
35770 static sqlite3_io_methods kvvfs_db_io_methods = {
35771 1, /* iVersion */
35772 kvvfsClose, /* xClose */
35773 kvvfsReadDb, /* xRead */
35774 kvvfsWriteDb, /* xWrite */
35775 kvvfsTruncateDb, /* xTruncate */
35776 kvvfsSyncDb, /* xSync */
35777 kvvfsFileSizeDb, /* xFileSize */
35778 kvvfsLock, /* xLock */
35779 kvvfsUnlock, /* xUnlock */
35780 kvvfsCheckReservedLock, /* xCheckReservedLock */
35781 kvvfsFileControlDb, /* xFileControl */
35782 kvvfsSectorSize, /* xSectorSize */
35783 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35784 0, /* xShmMap */
35785 0, /* xShmLock */
35786 0, /* xShmBarrier */
35787 0, /* xShmUnmap */
35788 0, /* xFetch */
35789 0 /* xUnfetch */
35790 };
35791
35792 /* Methods for sqlite3_file objects referencing a rollback journal
35793 */
35794 static sqlite3_io_methods kvvfs_jrnl_io_methods = {
35795 1, /* iVersion */
35796 kvvfsClose, /* xClose */
35797 kvvfsReadJrnl, /* xRead */
35798 kvvfsWriteJrnl, /* xWrite */
35799 kvvfsTruncateJrnl, /* xTruncate */
35800 kvvfsSyncJrnl, /* xSync */
35801 kvvfsFileSizeJrnl, /* xFileSize */
35802 kvvfsLock, /* xLock */
35803 kvvfsUnlock, /* xUnlock */
35804 kvvfsCheckReservedLock, /* xCheckReservedLock */
35805 kvvfsFileControlJrnl, /* xFileControl */
35806 kvvfsSectorSize, /* xSectorSize */
35807 kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */
35808 0, /* xShmMap */
35809 0, /* xShmLock */
35810 0, /* xShmBarrier */
35811 0, /* xShmUnmap */
35812 0, /* xFetch */
35813 0 /* xUnfetch */
35814 };
35815
35816 /****** Storage subsystem **************************************************/
35817 #include <sys/types.h>
35818 #include <sys/stat.h>
35819 #include <unistd.h>
35820
35821 /* Forward declarations for the low-level storage engine
35822 */
35823 static int kvstorageWrite(const char*, const char *zKey, const char *zData);
35824 static int kvstorageDelete(const char*, const char *zKey);
35825 static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
35826 #define KVSTORAGE_KEY_SZ 32
35827
35828 /* Expand the key name with an appropriate prefix and put the result
35829 ** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
35830 ** KVSTORAGE_KEY_SZ bytes.
35831 */
35832 static void kvstorageMakeKey(
35833 const char *zClass,
35834 const char *zKeyIn,
35835 char *zKeyOut
35836 ){
35837 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
35838 }
35839
35840 /* Write content into a key. zClass is the particular namespace of the
35841 ** underlying key/value store to use - either "local" or "session".
35842 **
35843 ** Both zKey and zData are zero-terminated pure text strings.
35844 **
35845 ** Return the number of errors.
35846 */
35847 static int kvstorageWrite(
35848 const char *zClass,
35849 const char *zKey,
35850 const char *zData
35851 ){
35852 FILE *fd;
35853 char zXKey[KVSTORAGE_KEY_SZ];
35854 kvstorageMakeKey(zClass, zKey, zXKey);
35855 fd = fopen(zXKey, "wb");
35856 if( fd ){
35857 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
35858 (int)strlen(zData), zData,
35859 strlen(zData)>50 ? "..." : ""));
35860 fputs(zData, fd);
35861 fclose(fd);
35862 return 0;
35863 }else{
35864 return 1;
35865 }
35866 }
35867
35868 /* Delete a key (with its corresponding data) from the key/value
35869 ** namespace given by zClass. If the key does not previously exist,
35870 ** this routine is a no-op.
35871 */
35872 static int kvstorageDelete(const char *zClass, const char *zKey){
35873 char zXKey[KVSTORAGE_KEY_SZ];
35874 kvstorageMakeKey(zClass, zKey, zXKey);
35875 unlink(zXKey);
35876 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
35877 return 0;
35878 }
35879
35880 /* Read the value associated with a zKey from the key/value namespace given
35881 ** by zClass and put the text data associated with that key in the first
35882 ** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large
35883 ** enough to hold it all. The value put into zBuf must always be zero
35884 ** terminated, even if it gets truncated because nBuf is not large enough.
35885 **
35886 ** Return the total number of bytes in the data, without truncation, and
35887 ** not counting the final zero terminator. Return -1 if the key does
35888 ** not exist.
35889 **
35890 ** If nBuf<=0 then this routine simply returns the size of the data without
35891 ** actually reading it.
35892 */
35893 static int kvstorageRead(
35894 const char *zClass,
35895 const char *zKey,
35896 char *zBuf,
35897 int nBuf
35898 ){
35899 FILE *fd;
35900 struct stat buf;
35901 char zXKey[KVSTORAGE_KEY_SZ];
35902 kvstorageMakeKey(zClass, zKey, zXKey);
35903 if( access(zXKey, R_OK)!=0
35904 || stat(zXKey, &buf)!=0
35905 || !S_ISREG(buf.st_mode)
35906 ){
35907 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35908 return -1;
35909 }
35910 if( nBuf<=0 ){
35911 return (int)buf.st_size;
35912 }else if( nBuf==1 ){
35913 zBuf[0] = 0;
35914 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
35915 (int)buf.st_size));
35916 return (int)buf.st_size;
35917 }
35918 if( nBuf > buf.st_size + 1 ){
35919 nBuf = buf.st_size + 1;
35920 }
35921 fd = fopen(zXKey, "rb");
35922 if( fd==0 ){
35923 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
35924 return -1;
35925 }else{
35926 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
35927 fclose(fd);
35928 zBuf[n] = 0;
35929 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
35930 n, zBuf, n>50 ? "..." : ""));
35931 return (int)n;
35932 }
35933 }
35934
35935 /*
35936 ** An internal level of indirection which enables us to replace the
35937 ** kvvfs i/o methods with JavaScript implementations in WASM builds.
35938 ** Maintenance reminder: if this struct changes in any way, the JSON
35939 ** rendering of its structure must be updated in
35940 ** sqlite3_wasm_enum_json(). There are no binary compatibility
35941 ** concerns, so it does not need an iVersion member. This file is
35942 ** necessarily always compiled together with sqlite3_wasm_enum_json(),
35943 ** and JS code dynamically creates the mapping of members based on
35944 ** that JSON description.
35945 */
35946 typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
35947 struct sqlite3_kvvfs_methods {
35948 int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
35949 int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
35950 int (*xDelete)(const char *zClass, const char *zKey);
35951 const int nKeySize;
35952 };
35953
35954 /*
35955 ** This object holds the kvvfs I/O methods which may be swapped out
35956 ** for JavaScript-side implementations in WASM builds. In such builds
35957 ** it cannot be const, but in native builds it should be so that
35958 ** the compiler can hopefully optimize this level of indirection out.
35959 ** That said, kvvfs is intended primarily for use in WASM builds.
35960 **
35961 ** Note that this is not explicitly flagged as static because the
35962 ** amalgamation build will tag it with SQLITE_PRIVATE.
35963 */
35964 #ifndef SQLITE_WASM
35965 const
35966 #endif
35967 SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
35968 kvstorageRead,
35969 kvstorageWrite,
35970 kvstorageDelete,
35971 KVSTORAGE_KEY_SZ
35972 };
35973
35974 /****** Utility subroutines ************************************************/
35975
35976 /*
35977 ** Encode binary into the text encoded used to persist on disk.
35978 ** The output text is stored in aOut[], which must be at least
35979 ** nData+1 bytes in length.
35980 **
35981 ** Return the actual length of the encoded text, not counting the
35982 ** zero terminator at the end.
35983 **
35984 ** Encoding format
35985 ** ---------------
35986 **
35987 ** * Non-zero bytes are encoded as upper-case hexadecimal
35988 **
35989 ** * A sequence of one or more zero-bytes that are not at the
35990 ** beginning of the buffer are encoded as a little-endian
35991 ** base-26 number using a..z. "a" means 0. "b" means 1,
35992 ** "z" means 25. "ab" means 26. "ac" means 52. And so forth.
35993 **
35994 ** * Because there is no overlap between the encoding characters
35995 ** of hexadecimal and base-26 numbers, it is always clear where
35996 ** one stops and the next begins.
35997 */
35998 static int kvvfsEncode(const char *aData, int nData, char *aOut){
35999 int i, j;
36000 const unsigned char *a = (const unsigned char*)aData;
36001 for(i=j=0; i<nData; i++){
36002 unsigned char c = a[i];
36003 if( c!=0 ){
36004 aOut[j++] = "0123456789ABCDEF"[c>>4];
36005 aOut[j++] = "0123456789ABCDEF"[c&0xf];
36006 }else{
36007 /* A sequence of 1 or more zeros is stored as a little-endian
36008 ** base-26 number using a..z as the digits. So one zero is "b".
36009 ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb",
36010 ** and so forth.
36011 */
36012 int k;
36013 for(k=1; i+k<nData && a[i+k]==0; k++){}
36014 i += k-1;
36015 while( k>0 ){
36016 aOut[j++] = 'a'+(k%26);
36017 k /= 26;
36018 }
36019 }
36020 }
36021 aOut[j] = 0;
36022 return j;
36023 }
36024
36025 static const signed char kvvfsHexValue[256] = {
36026 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36027 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36028 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36029 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36030 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36031 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36032 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36033 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36034
36035 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36036 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36037 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36038 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36043 };
36044
36045 /*
36046 ** Decode the text encoding back to binary. The binary content is
36047 ** written into pOut, which must be at least nOut bytes in length.
36048 **
36049 ** The return value is the number of bytes actually written into aOut[].
36050 */
36051 static int kvvfsDecode(const char *a, char *aOut, int nOut){
36052 int i, j;
36053 int c;
36054 const unsigned char *aIn = (const unsigned char*)a;
36055 i = 0;
36056 j = 0;
36057 while( 1 ){
36058 c = kvvfsHexValue[aIn[i]];
36059 if( c<0 ){
36060 int n = 0;
36061 int mult = 1;
36062 c = aIn[i];
36063 if( c==0 ) break;
36064 while( c>='a' && c<='z' ){
36065 n += (c - 'a')*mult;
36066 mult *= 26;
36067 c = aIn[++i];
36068 }
36069 if( j+n>nOut ) return -1;
36070 memset(&aOut[j], 0, n);
36071 j += n;
36072 c = aIn[i];
36073 if( c==0 ) break;
36074 }else{
36075 aOut[j] = c<<4;
36076 c = kvvfsHexValue[aIn[++i]];
36077 if( c<0 ) break;
36078 aOut[j++] += c;
36079 i++;
36080 }
36081 }
36082 return j;
36083 }
36084
36085 /*
36086 ** Decode a complete journal file. Allocate space in pFile->aJrnl
36087 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
36088 ** if an error is encountered.
36089 **
36090 ** The first few characters of the text encoding will be a little-endian
36091 ** base-26 number (digits a..z) that is the total number of bytes
36092 ** in the decoded journal file image. This base-26 number is followed
36093 ** by a single space, then the encoding of the journal. The space
36094 ** separator is required to act as a terminator for the base-26 number.
36095 */
36096 static void kvvfsDecodeJournal(
36097 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36098 const char *zTxt, /* Text encoding. Zero-terminated */
36099 int nTxt /* Bytes in zTxt, excluding zero terminator */
36100 ){
36101 unsigned int n = 0;
36102 int c, i, mult;
36103 i = 0;
36104 mult = 1;
36105 while( (c = zTxt[i++])>='a' && c<='z' ){
36106 n += (zTxt[i] - 'a')*mult;
36107 mult *= 26;
36108 }
36109 sqlite3_free(pFile->aJrnl);
36110 pFile->aJrnl = sqlite3_malloc64( n );
36111 if( pFile->aJrnl==0 ){
36112 pFile->nJrnl = 0;
36113 return;
36114 }
36115 pFile->nJrnl = n;
36116 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36117 if( n<pFile->nJrnl ){
36118 sqlite3_free(pFile->aJrnl);
36119 pFile->aJrnl = 0;
36120 pFile->nJrnl = 0;
36121 }
36122 }
36123
36124 /*
36125 ** Read or write the "sz" element, containing the database file size.
36126 */
36127 static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){
36128 char zData[50];
36129 zData[0] = 0;
36130 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36131 return strtoll(zData, 0, 0);
36132 }
36133 static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){
36134 char zData[50];
36135 sqlite3_snprintf(sizeof(zData), zData, "%lld", sz);
36136 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36137 }
36138
36139 /****** sqlite3_io_methods methods ******************************************/
36140
36141 /*
36142 ** Close an kvvfs-file.
36143 */
36144 static int kvvfsClose(sqlite3_file *pProtoFile){
36145 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36146
36147 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36148 pFile->isJournal ? "journal" : "db"));
36149 sqlite3_free(pFile->aJrnl);
36150 return SQLITE_OK;
36151 }
36152
36153 /*
36154 ** Read from the -journal file.
36155 */
36156 static int kvvfsReadJrnl(
36157 sqlite3_file *pProtoFile,
36158 void *zBuf,
36159 int iAmt,
36160 sqlite_int64 iOfst
36161 ){
36162 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36163 assert( pFile->isJournal );
36164 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36165 if( pFile->aJrnl==0 ){
36166 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36167 char *aTxt;
36168 if( szTxt<=4 ){
36169 return SQLITE_IOERR;
36170 }
36171 aTxt = sqlite3_malloc64( szTxt+1 );
36172 if( aTxt==0 ) return SQLITE_NOMEM;
36173 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36174 kvvfsDecodeJournal(pFile, aTxt, szTxt);
36175 sqlite3_free(aTxt);
36176 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36177 }
36178 if( iOfst+iAmt>pFile->nJrnl ){
36179 return SQLITE_IOERR_SHORT_READ;
36180 }
36181 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36182 return SQLITE_OK;
36183 }
36184
36185 /*
36186 ** Read from the database file.
36187 */
36188 static int kvvfsReadDb(
36189 sqlite3_file *pProtoFile,
36190 void *zBuf,
36191 int iAmt,
36192 sqlite_int64 iOfst
36193 ){
36194 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36195 unsigned int pgno;
36196 int got, n;
36197 char zKey[30];
36198 char aData[133073];
36199 assert( iOfst>=0 );
36200 assert( iAmt>=0 );
36201 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36202 if( iOfst+iAmt>=512 ){
36203 if( (iOfst % iAmt)!=0 ){
36204 return SQLITE_IOERR_READ;
36205 }
36206 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36207 return SQLITE_IOERR_READ;
36208 }
36209 pFile->szPage = iAmt;
36210 pgno = 1 + iOfst/iAmt;
36211 }else{
36212 pgno = 1;
36213 }
36214 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36215 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36216 if( got<0 ){
36217 n = 0;
36218 }else{
36219 aData[got] = 0;
36220 if( iOfst+iAmt<512 ){
36221 int k = iOfst+iAmt;
36222 aData[k*2] = 0;
36223 n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36224 if( n>=iOfst+iAmt ){
36225 memcpy(zBuf, &aData[2000+iOfst], iAmt);
36226 n = iAmt;
36227 }else{
36228 n = 0;
36229 }
36230 }else{
36231 n = kvvfsDecode(aData, zBuf, iAmt);
36232 }
36233 }
36234 if( n<iAmt ){
36235 memset(zBuf+n, 0, iAmt-n);
36236 return SQLITE_IOERR_SHORT_READ;
36237 }
36238 return SQLITE_OK;
36239 }
36240
36241
36242 /*
36243 ** Write into the -journal file.
36244 */
36245 static int kvvfsWriteJrnl(
36246 sqlite3_file *pProtoFile,
36247 const void *zBuf,
36248 int iAmt,
36249 sqlite_int64 iOfst
36250 ){
36251 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36252 sqlite3_int64 iEnd = iOfst+iAmt;
36253 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36254 if( iEnd>=0x10000000 ) return SQLITE_FULL;
36255 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36256 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36257 if( aNew==0 ){
36258 return SQLITE_IOERR_NOMEM;
36259 }
36260 pFile->aJrnl = aNew;
36261 if( pFile->nJrnl<iOfst ){
36262 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36263 }
36264 pFile->nJrnl = iEnd;
36265 }
36266 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36267 return SQLITE_OK;
36268 }
36269
36270 /*
36271 ** Write into the database file.
36272 */
36273 static int kvvfsWriteDb(
36274 sqlite3_file *pProtoFile,
36275 const void *zBuf,
36276 int iAmt,
36277 sqlite_int64 iOfst
36278 ){
36279 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36280 unsigned int pgno;
36281 char zKey[30];
36282 char aData[131073];
36283 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36284 assert( iAmt>=512 && iAmt<=65536 );
36285 assert( (iAmt & (iAmt-1))==0 );
36286 pgno = 1 + iOfst/iAmt;
36287 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36288 kvvfsEncode(zBuf, iAmt, aData);
36289 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36290 return SQLITE_IOERR;
36291 }
36292 if( iOfst+iAmt > pFile->szDb ){
36293 pFile->szDb = iOfst + iAmt;
36294 }
36295 return SQLITE_OK;
36296 }
36297
36298 /*
36299 ** Truncate an kvvfs-file.
36300 */
36301 static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){
36302 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36303 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36304 assert( size==0 );
36305 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36306 sqlite3_free(pFile->aJrnl);
36307 pFile->aJrnl = 0;
36308 pFile->nJrnl = 0;
36309 return SQLITE_OK;
36310 }
36311 static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){
36312 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36313 if( pFile->szDb>size
36314 && pFile->szPage>0
36315 && (size % pFile->szPage)==0
36316 ){
36317 char zKey[50];
36318 unsigned int pgno, pgnoMax;
36319 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36320 pgno = 1 + size/pFile->szPage;
36321 pgnoMax = 2 + pFile->szDb/pFile->szPage;
36322 while( pgno<=pgnoMax ){
36323 sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno);
36324 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36325 pgno++;
36326 }
36327 pFile->szDb = size;
36328 return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK;
36329 }
36330 return SQLITE_IOERR;
36331 }
36332
36333 /*
36334 ** Sync an kvvfs-file.
36335 */
36336 static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){
36337 int i, n;
36338 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36339 char *zOut;
36340 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36341 if( pFile->nJrnl<=0 ){
36342 return kvvfsTruncateJrnl(pProtoFile, 0);
36343 }
36344 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36345 if( zOut==0 ){
36346 return SQLITE_IOERR_NOMEM;
36347 }
36348 n = pFile->nJrnl;
36349 i = 0;
36350 do{
36351 zOut[i++] = 'a' + (n%26);
36352 n /= 26;
36353 }while( n>0 );
36354 zOut[i++] = ' ';
36355 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36356 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36357 sqlite3_free(zOut);
36358 return i ? SQLITE_IOERR : SQLITE_OK;
36359 }
36360 static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){
36361 return SQLITE_OK;
36362 }
36363
36364 /*
36365 ** Return the current file-size of an kvvfs-file.
36366 */
36367 static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36368 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36369 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36370 *pSize = pFile->nJrnl;
36371 return SQLITE_OK;
36372 }
36373 static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
36374 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36375 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36376 if( pFile->szDb>=0 ){
36377 *pSize = pFile->szDb;
36378 }else{
36379 *pSize = kvvfsReadFileSize(pFile);
36380 }
36381 return SQLITE_OK;
36382 }
36383
36384 /*
36385 ** Lock an kvvfs-file.
36386 */
36387 static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
36388 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36389 assert( !pFile->isJournal );
36390 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36391
36392 if( eLock!=SQLITE_LOCK_NONE ){
36393 pFile->szDb = kvvfsReadFileSize(pFile);
36394 }
36395 return SQLITE_OK;
36396 }
36397
36398 /*
36399 ** Unlock an kvvfs-file.
36400 */
36401 static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
36402 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36403 assert( !pFile->isJournal );
36404 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36405 if( eLock==SQLITE_LOCK_NONE ){
36406 pFile->szDb = -1;
36407 }
36408 return SQLITE_OK;
36409 }
36410
36411 /*
36412 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36413 */
36414 static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
36415 SQLITE_KV_LOG(("xCheckReservedLock\n"));
36416 *pResOut = 0;
36417 return SQLITE_OK;
36418 }
36419
36420 /*
36421 ** File control method. For custom operations on an kvvfs-file.
36422 */
36423 static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){
36424 SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op));
36425 return SQLITE_NOTFOUND;
36426 }
36427 static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){
36428 SQLITE_KV_LOG(("xFileControl(%d) on database\n", op));
36429 if( op==SQLITE_FCNTL_SYNC ){
36430 KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
36431 int rc = SQLITE_OK;
36432 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36433 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36434 rc = SQLITE_IOERR;
36435 }
36436 return rc;
36437 }
36438 return SQLITE_NOTFOUND;
36439 }
36440
36441 /*
36442 ** Return the sector-size in bytes for an kvvfs-file.
36443 */
36444 static int kvvfsSectorSize(sqlite3_file *pFile){
36445 return 512;
36446 }
36447
36448 /*
36449 ** Return the device characteristic flags supported by an kvvfs-file.
36450 */
36451 static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){
36452 return 0;
36453 }
36454
36455 /****** sqlite3_vfs methods *************************************************/
36456
36457 /*
36458 ** Open an kvvfs file handle.
36459 */
36460 static int kvvfsOpen(
36461 sqlite3_vfs *pProtoVfs,
36462 const char *zName,
36463 sqlite3_file *pProtoFile,
36464 int flags,
36465 int *pOutFlags
36466 ){
36467 KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
36468 SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName));
36469 if( strcmp(zName, "local")==0
36470 || strcmp(zName, "session")==0
36471 ){
36472 pFile->isJournal = 0;
36473 pFile->base.pMethods = &kvvfs_db_io_methods;
36474 }else
36475 if( strcmp(zName, "local-journal")==0
36476 || strcmp(zName, "session-journal")==0
36477 ){
36478 pFile->isJournal = 1;
36479 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36480 }else{
36481 return SQLITE_CANTOPEN;
36482 }
36483 if( zName[0]=='s' ){
36484 pFile->zClass = "session";
36485 }else{
36486 pFile->zClass = "local";
36487 }
36488 pFile->aJrnl = 0;
36489 pFile->nJrnl = 0;
36490 pFile->szPage = -1;
36491 pFile->szDb = -1;
36492 return SQLITE_OK;
36493 }
36494
36495 /*
36496 ** Delete the file located at zPath. If the dirSync argument is true,
36497 ** ensure the file-system modifications are synced to disk before
36498 ** returning.
36499 */
36500 static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
36501 if( strcmp(zPath, "local-journal")==0 ){
36502 sqlite3KvvfsMethods.xDelete("local", "jrnl");
36503 }else
36504 if( strcmp(zPath, "session-journal")==0 ){
36505 sqlite3KvvfsMethods.xDelete("session", "jrnl");
36506 }
36507 return SQLITE_OK;
36508 }
36509
36510 /*
36511 ** Test for access permissions. Return true if the requested permission
36512 ** is available, or false otherwise.
36513 */
36514 static int kvvfsAccess(
36515 sqlite3_vfs *pProtoVfs,
36516 const char *zPath,
36517 int flags,
36518 int *pResOut
36519 ){
36520 SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath));
36521 if( strcmp(zPath, "local-journal")==0 ){
36522 *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0;
36523 }else
36524 if( strcmp(zPath, "session-journal")==0 ){
36525 *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0;
36526 }else
36527 if( strcmp(zPath, "local")==0 ){
36528 *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0;
36529 }else
36530 if( strcmp(zPath, "session")==0 ){
36531 *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0;
36532 }else
36533 {
36534 *pResOut = 0;
36535 }
36536 SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut));
36537 return SQLITE_OK;
36538 }
36539
36540 /*
36541 ** Populate buffer zOut with the full canonical pathname corresponding
36542 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
36543 ** of at least (INST_MAX_PATHNAME+1) bytes.
36544 */
36545 static int kvvfsFullPathname(
36546 sqlite3_vfs *pVfs,
36547 const char *zPath,
36548 int nOut,
36549 char *zOut
36550 ){
36551 size_t nPath;
36552 #ifdef SQLITE_OS_KV_ALWAYS_LOCAL
36553 zPath = "local";
36554 #endif
36555 nPath = strlen(zPath);
36556 SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath));
36557 if( nOut<nPath+1 ) nPath = nOut - 1;
36558 memcpy(zOut, zPath, nPath);
36559 zOut[nPath] = 0;
36560 return SQLITE_OK;
36561 }
36562
36563 /*
36564 ** Open the dynamic library located at zPath and return a handle.
36565 */
36566 static void *kvvfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
36567 return 0;
36568 }
36569
36570 /*
36571 ** Populate the buffer pointed to by zBufOut with nByte bytes of
36572 ** random data.
36573 */
36574 static int kvvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
36575 memset(zBufOut, 0, nByte);
36576 return nByte;
36577 }
36578
36579 /*
36580 ** Sleep for nMicro microseconds. Return the number of microseconds
36581 ** actually slept.
36582 */
36583 static int kvvfsSleep(sqlite3_vfs *pVfs, int nMicro){
36584 return SQLITE_OK;
36585 }
36586
36587 /*
36588 ** Return the current time as a Julian Day number in *pTimeOut.
36589 */
36590 static int kvvfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
36591 sqlite3_int64 i = 0;
36592 int rc;
36593 rc = kvvfsCurrentTimeInt64(0, &i);
36594 *pTimeOut = i/86400000.0;
36595 return rc;
36596 }
36597 #include <sys/time.h>
36598 static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
36599 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36600 struct timeval sNow;
36601 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
36602 *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
36603 return SQLITE_OK;
36604 }
36605 #endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */
36606
36607 #if SQLITE_OS_KV
36608 /*
36609 ** This routine is called initialize the KV-vfs as the default VFS.
36610 */
36611 SQLITE_API int sqlite3_os_init(void){
36612 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1);
36613 }
36614 SQLITE_API int sqlite3_os_end(void){
36615 return SQLITE_OK;
36616 }
36617 #endif /* SQLITE_OS_KV */
36618
36619 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
36620 SQLITE_PRIVATE int sqlite3KvvfsInit(void){
36621 return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0);
36622 }
36623 #endif
36624
36625 /************** End of os_kv.c ***********************************************/
36626 /************** Begin file os_unix.c *****************************************/
36627 /*
36628 ** 2004 May 22
36629 **
36630 ** The author disclaims copyright to this source code. In place of
@@ -35647,17 +36711,17 @@
36711 #endif
36712
36713 /*
36714 ** standard include files.
36715 */
36716 #include <sys/types.h> /* amalgamator: keep */
36717 #include <sys/stat.h> /* amalgamator: keep */
36718 #include <fcntl.h>
36719 #include <sys/ioctl.h>
36720 #include <unistd.h> /* amalgamator: keep */
36721 /* #include <time.h> */
36722 #include <sys/time.h> /* amalgamator: keep */
36723 #include <errno.h>
36724 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
36725 # include <sys/mman.h>
36726 #endif
36727
@@ -43625,10 +44689,13 @@
44689 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS));
44690 #else
44691 sqlite3_vfs_register(&aVfs[i], i==0);
44692 #endif
44693 }
44694 #ifdef SQLITE_OS_KV_OPTIONAL
44695 sqlite3KvvfsInit();
44696 #endif
44697 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
44698
44699 #ifndef SQLITE_OMIT_WAL
44700 /* Validate lock assumptions */
44701 assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
@@ -70722,10 +71789,13 @@
71789 put4byte(pCell+info.nSize-4, iTo);
71790 break;
71791 }
71792 }
71793 }else{
71794 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
71795 return SQLITE_CORRUPT_PAGE(pPage);
71796 }
71797 if( get4byte(pCell)==iFrom ){
71798 put4byte(pCell, iTo);
71799 break;
71800 }
71801 }
@@ -73056,18 +74126,11 @@
74126 }
74127 }
74128
74129 pPage = pCur->pPage;
74130 idx = ++pCur->ix;
74131 if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
 
 
 
 
 
 
 
74132 return SQLITE_CORRUPT_BKPT;
74133 }
74134
74135 if( idx>=pPage->nCell ){
74136 if( !pPage->leaf ){
@@ -75753,10 +76816,15 @@
76816 assert( pCur->pPage->nOverflow );
76817 }
76818 }else{
76819 break;
76820 }
76821 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
76822 /* The page being written is not a root page, and there is currently
76823 ** more than one reference to it. This only happens if the page is one
76824 ** of its own ancestor pages. Corruption. */
76825 rc = SQLITE_CORRUPT_BKPT;
76826 }else{
76827 MemPage * const pParent = pCur->apPage[iPage-1];
76828 int const iIdx = pCur->aiIdx[iPage-1];
76829
76830 rc = sqlite3PagerWrite(pParent->pDbPage);
@@ -79679,10 +80747,11 @@
80747 assert( MEM_Str==(MEM_Blob>>3) );
80748 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
80749 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
80750 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
80751 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
80752 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
80753 return sqlite3VdbeChangeEncoding(pMem, encoding);
80754 }
80755 }
80756 return SQLITE_OK;
80757 }
@@ -80813,10 +81882,13 @@
81882 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
81883 Mem *p = (Mem*)pVal;
81884 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
81885 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
81886 return p->n;
81887 }
81888 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
81889 return p->n;
81890 }
81891 if( (p->flags & MEM_Blob)!=0 ){
81892 if( p->flags & MEM_Zero ){
81893 return p->n + p->u.nZero;
81894 }else{
@@ -81983,10 +83055,22 @@
83055 }
83056 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
83057 assert( p->nOp>0 || p->db->mallocFailed );
83058 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
83059 }
83060
83061 /*
83062 ** If the previous opcode is an OP_Column that delivers results
83063 ** into register iDest, then add the OPFLAG_TYPEOFARG flag to that
83064 ** opcode.
83065 */
83066 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
83067 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
83068 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
83069 pOp->p5 |= OPFLAG_TYPEOFARG;
83070 }
83071 }
83072
83073 /*
83074 ** Change the P2 operand of instruction addr so that it points to
83075 ** the address of the next instruction to be coded.
83076 */
@@ -85402,11 +86486,11 @@
86486 testcase( pRhs->flags & MEM_Int );
86487 testcase( pRhs->flags & MEM_IntReal );
86488 serial_type = aKey1[idx1];
86489 testcase( serial_type==12 );
86490 if( serial_type>=10 ){
86491 rc = serial_type==10 ? -1 : +1;
86492 }else if( serial_type==0 ){
86493 rc = -1;
86494 }else if( serial_type==7 ){
86495 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
86496 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
@@ -85427,11 +86511,11 @@
86511 if( serial_type>=10 ){
86512 /* Serial types 12 or greater are strings and blobs (greater than
86513 ** numbers). Types 10 and 11 are currently "reserved for future
86514 ** use", so it doesn't really matter what the results of comparing
86515 ** them to numberic values are. */
86516 rc = serial_type==10 ? -1 : +1;
86517 }else if( serial_type==0 ){
86518 rc = -1;
86519 }else{
86520 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
86521 if( serial_type==7 ){
@@ -85508,11 +86592,11 @@
86592 }
86593
86594 /* RHS is null */
86595 else{
86596 serial_type = aKey1[idx1];
86597 rc = (serial_type!=0 && serial_type!=10);
86598 }
86599
86600 if( rc!=0 ){
86601 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
86602 if( sortFlags ){
@@ -86457,10 +87541,13 @@
87541 }
87542 assert( eType == aType[pVal->flags&MEM_AffMask] );
87543 }
87544 #endif
87545 return aType[pVal->flags&MEM_AffMask];
87546 }
87547 SQLITE_API int sqlite3_value_encoding(sqlite3_value *pVal){
87548 return pVal->enc;
87549 }
87550
87551 /* Return true if a parameter to xUpdate represents an unchanged column */
87552 SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
87553 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
@@ -91094,23 +92181,94 @@
92181 goto jump_to_p2;
92182 }
92183 break;
92184 }
92185
92186 /* Opcode: IsType P1 P2 P3 P4 P5
92187 ** Synopsis: if typeof(P1.P3) in P5 goto P2
92188 **
92189 ** Jump to P2 if the type of a column in a btree is one of the types specified
92190 ** by the P5 bitmask.
92191 **
92192 ** P1 is normally a cursor on a btree for which the row decode cache is
92193 ** valid through at least column P3. In other words, there should have been
92194 ** a prior OP_Column for column P3 or greater. If the cursor is not valid,
92195 ** then this opcode might give spurious results.
92196 ** The the btree row has fewer than P3 columns, then use P4 as the
92197 ** datatype.
92198 **
92199 ** If P1 is -1, then P3 is a register number and the datatype is taken
92200 ** from the value in that register.
92201 **
92202 ** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant
92203 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
92204 ** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10.
92205 **
92206 ** Take the jump to address P2 if and only if the datatype of the
92207 ** value determined by P1 and P3 corresponds to one of the bits in the
92208 ** P5 bitmask.
92209 **
92210 */
92211 case OP_IsType: { /* jump */
92212 VdbeCursor *pC;
92213 u16 typeMask;
92214 u32 serialType;
92215
92216 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
92217 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
92218 if( pOp->p1>=0 ){
92219 pC = p->apCsr[pOp->p1];
92220 assert( pC!=0 );
92221 assert( pOp->p3>=0 );
92222 if( pOp->p3<pC->nHdrParsed ){
92223 serialType = pC->aType[pOp->p3];
92224 if( serialType>=12 ){
92225 if( serialType&1 ){
92226 typeMask = 0x04; /* SQLITE_TEXT */
92227 }else{
92228 typeMask = 0x08; /* SQLITE_BLOB */
92229 }
92230 }else{
92231 static const unsigned char aMask[] = {
92232 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2,
92233 0x01, 0x01, 0x10, 0x10
92234 };
92235 testcase( serialType==0 );
92236 testcase( serialType==1 );
92237 testcase( serialType==2 );
92238 testcase( serialType==3 );
92239 testcase( serialType==4 );
92240 testcase( serialType==5 );
92241 testcase( serialType==6 );
92242 testcase( serialType==7 );
92243 testcase( serialType==8 );
92244 testcase( serialType==9 );
92245 testcase( serialType==10 );
92246 testcase( serialType==11 );
92247 typeMask = aMask[serialType];
92248 }
92249 }else{
92250 typeMask = 1 << (pOp->p4.i - 1);
92251 testcase( typeMask==0x01 );
92252 testcase( typeMask==0x02 );
92253 testcase( typeMask==0x04 );
92254 testcase( typeMask==0x08 );
92255 testcase( typeMask==0x10 );
92256 }
92257 }else{
92258 assert( memIsValid(&aMem[pOp->p3]) );
92259 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
92260 testcase( typeMask==0x01 );
92261 testcase( typeMask==0x02 );
92262 testcase( typeMask==0x04 );
92263 testcase( typeMask==0x08 );
92264 testcase( typeMask==0x10 );
92265 }
92266 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
92267 if( typeMask & pOp->p5 ){
92268 goto jump_to_p2;
92269 }
92270 break;
92271 }
92272
92273 /* Opcode: ZeroOrNull P1 P2 P3 * *
92274 ** Synopsis: r[P2] = 0 OR NULL
@@ -91207,23 +92365,25 @@
92365 ** Synopsis: r[P3]=PX cursor P1 column P2
92366 **
92367 ** Interpret the data that cursor P1 points to as a structure built using
92368 ** the MakeRecord instruction. (See the MakeRecord opcode for additional
92369 ** information about the format of the data.) Extract the P2-th column
92370 ** from this record. If there are less than (P2+1)
92371 ** values in the record, extract a NULL.
92372 **
92373 ** The value extracted is stored in register P3.
92374 **
92375 ** If the record contains fewer than P2 fields, then extract a NULL. Or,
92376 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
92377 ** the result.
92378 **
92379 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
92380 ** to only be used by the length() function or the equivalent. The content
92381 ** of large blobs is not loaded, thus saving CPU cycles. If the
92382 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
92383 ** typeof() function or the IS NULL or IS NOT NULL operators or the
92384 ** equivalent. In this case, all content loading can be omitted.
92385 */
92386 case OP_Column: {
92387 u32 p2; /* column number to retrieve */
92388 VdbeCursor *pC; /* The VDBE cursor */
92389 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
@@ -93158,11 +94318,17 @@
94318 assert( oc!=OP_SeekGE || r.default_rc==+1 );
94319 assert( oc!=OP_SeekLT || r.default_rc==+1 );
94320
94321 r.aMem = &aMem[pOp->p3];
94322 #ifdef SQLITE_DEBUG
94323 {
94324 int i;
94325 for(i=0; i<r.nField; i++){
94326 assert( memIsValid(&r.aMem[i]) );
94327 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
94328 }
94329 }
94330 #endif
94331 r.eqSeen = 0;
94332 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
94333 if( rc!=SQLITE_OK ){
94334 goto abort_due_to_error;
@@ -93221,54 +94387,76 @@
94387 }
94388 break;
94389 }
94390
94391
94392 /* Opcode: SeekScan P1 P2 * * P5
94393 ** Synopsis: Scan-ahead up to P1 rows
94394 **
94395 ** This opcode is a prefix opcode to OP_SeekGE. In other words, this
94396 ** opcode must be immediately followed by OP_SeekGE. This constraint is
94397 ** checked by assert() statements.
94398 **
94399 ** This opcode uses the P1 through P4 operands of the subsequent
94400 ** OP_SeekGE. In the text that follows, the operands of the subsequent
94401 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only
94402 ** the P1, P2 and P5 operands of this opcode are also used, and are called
94403 ** This.P1, This.P2 and This.P5.
94404 **
94405 ** This opcode helps to optimize IN operators on a multi-column index
94406 ** where the IN operator is on the later terms of the index by avoiding
94407 ** unnecessary seeks on the btree, substituting steps to the next row
94408 ** of the b-tree instead. A correct answer is obtained if this opcode
94409 ** is omitted or is a no-op.
94410 **
94411 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
94412 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
94413 ** to. Call this SeekGE.P3/P4 row the "target".
94414 **
94415 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
94416 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
94417 **
94418 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
94419 ** might be the target row, or it might be near and slightly before the
94420 ** target row, or it might be after the target row. If the cursor is
94421 ** currently before the target row, then this opcode attempts to position
94422 ** the cursor on or after the target row by invoking sqlite3BtreeStep()
94423 ** on the cursor between 1 and This.P1 times.
94424 **
94425 ** The This.P5 parameter is a flag that indicates what to do if the
94426 ** cursor ends up pointing at a valid row that is past the target
94427 ** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If
94428 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
94429 ** case occurs when there are no inequality constraints to the right of
94430 ** the IN constraing. The jump to SeekGE.P2 ends the loop. The P5!=0 case
94431 ** occurs when there are inequality constraints to the right of the IN
94432 ** operator. In that case, the This.P2 will point either directly to or
94433 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
94434 ** loop terminate.
94435 **
94436 ** Possible outcomes from this opcode:<ol>
94437 **
94438 ** <li> If the cursor is initally not pointed to any valid row, then
94439 ** fall through into the subsequent OP_SeekGE opcode.
94440 **
94441 ** <li> If the cursor is left pointing to a row that is before the target
94442 ** row, even after making as many as This.P1 calls to
94443 ** sqlite3BtreeNext(), then also fall through into OP_SeekGE.
94444 **
94445 ** <li> If the cursor is left pointing at the target row, either because it
94446 ** was at the target row to begin with or because one or more
94447 ** sqlite3BtreeNext() calls moved the cursor to the target row,
94448 ** then jump to This.P2..,
94449 **
94450 ** <li> If the cursor started out before the target row and a call to
94451 ** to sqlite3BtreeNext() moved the cursor off the end of the index
94452 ** (indicating that the target row definitely does not exist in the
94453 ** btree) then jump to SeekGE.P2, ending the loop.
94454 **
94455 ** <li> If the cursor ends up on a valid row that is past the target row
94456 ** (indicating that the target row does not exist in the btree) then
94457 ** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
94458 ** </ol>
94459 */
94460 case OP_SeekScan: {
94461 VdbeCursor *pC;
94462 int res;
@@ -93275,18 +94463,29 @@
94463 int nStep;
94464 UnpackedRecord r;
94465
94466 assert( pOp[1].opcode==OP_SeekGE );
94467
94468 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
94469 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
94470 ** opcode past the OP_SeekGE itself. */
94471 assert( pOp->p2>=(int)(pOp-aOp)+2 );
94472 #ifdef SQLITE_DEBUG
94473 if( pOp->p5==0 ){
94474 /* There are no inequality constraints following the IN constraint. */
94475 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
94476 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
94477 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
94478 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
94479 || aOp[pOp->p2-1].opcode==OP_IdxGE );
94480 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
94481 }else{
94482 /* There are inequality constraints. */
94483 assert( pOp->p2==(int)(pOp-aOp)+2 );
94484 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
94485 }
94486 #endif
94487
94488 assert( pOp->p1>0 );
94489 pC = p->apCsr[pOp[1].p1];
94490 assert( pC!=0 );
94491 assert( pC->eCurType==CURTYPE_BTREE );
@@ -93316,22 +94515,24 @@
94515 #endif
94516 res = 0; /* Not needed. Only used to silence a warning. */
94517 while(1){
94518 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
94519 if( rc ) goto abort_due_to_error;
94520 if( res>0 && pOp->p5==0 ){
94521 seekscan_search_fail:
94522 /* Jump to SeekGE.P2, ending the loop */
94523 #ifdef SQLITE_DEBUG
94524 if( db->flags&SQLITE_VdbeTrace ){
94525 printf("... %d steps and then skip\n", pOp->p1 - nStep);
94526 }
94527 #endif
94528 VdbeBranchTaken(1,3);
94529 pOp++;
94530 goto jump_to_p2;
94531 }
94532 if( res>=0 ){
94533 /* Jump to This.P2, bypassing the OP_SeekGE opcode */
94534 #ifdef SQLITE_DEBUG
94535 if( db->flags&SQLITE_VdbeTrace ){
94536 printf("... %d steps and then success\n", pOp->p1 - nStep);
94537 }
94538 #endif
@@ -103867,13 +105068,12 @@
105068 }
105069 op = pExpr->op;
105070 if( op==TK_REGISTER ) op = pExpr->op2;
105071 if( op==TK_COLUMN || op==TK_AGG_COLUMN ){
105072 assert( ExprUseYTab(pExpr) );
105073 assert( pExpr->y.pTab!=0 );
105074 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 
105075 }
105076 if( op==TK_SELECT ){
105077 assert( ExprUseXSelect(pExpr) );
105078 assert( pExpr->x.pSelect!=0 );
105079 assert( pExpr->x.pSelect->pEList!=0 );
@@ -103987,21 +105187,18 @@
105187 const Expr *p = pExpr;
105188 while( p ){
105189 int op = p->op;
105190 if( op==TK_REGISTER ) op = p->op2;
105191 if( op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER ){
105192 int j;
105193 assert( ExprUseYTab(p) );
105194 assert( p->y.pTab!=0 );
105195 if( (j = p->iColumn)>=0 ){
105196 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
105197 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
105198 }
105199 break;
 
 
 
 
105200 }
105201 if( op==TK_CAST || op==TK_UPLUS ){
105202 p = p->pLeft;
105203 continue;
105204 }
@@ -107602,14 +108799,11 @@
108799 int iCol, /* Index of the column to extract */
108800 int regOut /* Extract the value into this register */
108801 ){
108802 Column *pCol;
108803 assert( v!=0 );
108804 assert( pTab!=0 );
 
 
 
108805 if( iCol<0 || iCol==pTab->iPKey ){
108806 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
108807 VdbeComment((v, "%s.rowid", pTab->zName));
108808 }else{
108809 int op;
@@ -107855,10 +109049,57 @@
109049 #endif /* !defined(SQLITE_UNTESTABLE) */
109050 }
109051 return target;
109052 }
109053
109054 /*
109055 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109056 ** If it is, then resolve the expression by reading from the index and
109057 ** return the register into which the value has been read. If pExpr is
109058 ** not an indexed expression, then return negative.
109059 */
109060 static SQLITE_NOINLINE int sqlite3IndexedExprLookup(
109061 Parse *pParse, /* The parsing context */
109062 Expr *pExpr, /* The expression to potentially bypass */
109063 int target /* Where to store the result of the expression */
109064 ){
109065 IndexedExpr *p;
109066 Vdbe *v;
109067 for(p=pParse->pIdxExpr; p; p=p->pIENext){
109068 int iDataCur = p->iDataCur;
109069 if( iDataCur<0 ) continue;
109070 if( pParse->iSelfTab ){
109071 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109072 iDataCur = -1;
109073 }
109074 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
109075 v = pParse->pVdbe;
109076 assert( v!=0 );
109077 if( p->bMaybeNullRow ){
109078 /* If the index is on a NULL row due to an outer join, then we
109079 ** cannot extract the value from the index. The value must be
109080 ** computed using the original expression. */
109081 int addr = sqlite3VdbeCurrentAddr(v);
109082 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109083 VdbeCoverage(v);
109084 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109085 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109086 sqlite3VdbeGoto(v, 0);
109087 p = pParse->pIdxExpr;
109088 pParse->pIdxExpr = 0;
109089 sqlite3ExprCode(pParse, pExpr, target);
109090 pParse->pIdxExpr = p;
109091 sqlite3VdbeJumpHere(v, addr+2);
109092 }else{
109093 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109094 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109095 }
109096 return target;
109097 }
109098 return -1; /* Not found */
109099 }
109100
109101
109102 /*
109103 ** Generate code into the current Vdbe to evaluate the given
109104 ** expression. Attempt to store the results in register "target".
109105 ** Return the register where results are stored.
@@ -107883,10 +109124,15 @@
109124 assert( v!=0 );
109125
109126 expr_code_doover:
109127 if( pExpr==0 ){
109128 op = TK_NULL;
109129 }else if( pParse->pIdxExpr!=0
109130 && !ExprHasProperty(pExpr, EP_Leaf)
109131 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0
109132 ){
109133 return r1;
109134 }else{
109135 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
109136 op = pExpr->op;
109137 }
109138 switch( op ){
@@ -107928,15 +109174,12 @@
109174 ** constant.
109175 */
109176 int aff;
109177 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
109178 assert( ExprUseYTab(pExpr) );
109179 assert( pExpr->y.pTab!=0 );
109180 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
 
 
 
109181 if( aff>SQLITE_AFF_BLOB ){
109182 static const char zAff[] = "B\000C\000D\000E";
109183 assert( SQLITE_AFF_BLOB=='A' );
109184 assert( SQLITE_AFF_TEXT=='B' );
109185 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
@@ -107994,16 +109237,14 @@
109237 ** in the index refer to the table to which the index belongs */
109238 iTab = pParse->iSelfTab - 1;
109239 }
109240 }
109241 assert( ExprUseYTab(pExpr) );
109242 assert( pExpr->y.pTab!=0 );
109243 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
109244 pExpr->iColumn, iTab, target,
109245 pExpr->op2);
 
 
 
109246 return iReg;
109247 }
109248 case TK_INTEGER: {
109249 codeInteger(pParse, pExpr, 0, target);
109250 return target;
@@ -109053,10 +110294,11 @@
110294 case TK_ISNULL:
110295 case TK_NOTNULL: {
110296 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
110297 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
110298 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110299 sqlite3VdbeTypeofColumn(v, r1);
110300 sqlite3VdbeAddOp2(v, op, r1, dest);
110301 VdbeCoverageIf(v, op==TK_ISNULL);
110302 VdbeCoverageIf(v, op==TK_NOTNULL);
110303 testcase( regFree1==0 );
110304 break;
@@ -109227,10 +110469,11 @@
110469 break;
110470 }
110471 case TK_ISNULL:
110472 case TK_NOTNULL: {
110473 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110474 sqlite3VdbeTypeofColumn(v, r1);
110475 sqlite3VdbeAddOp2(v, op, r1, dest);
110476 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
110477 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
110478 testcase( regFree1==0 );
110479 break;
@@ -109380,11 +110623,17 @@
110623 return 1;
110624 }
110625 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
110626 return 1;
110627 }
110628 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
110629 && pB->iTable<0 && pA->iTable==iTab
110630 ){
110631 /* fall through */
110632 }else{
110633 return 2;
110634 }
110635 }
110636 assert( !ExprHasProperty(pA, EP_IntValue) );
110637 assert( !ExprHasProperty(pB, EP_IntValue) );
110638 if( pA->u.zToken ){
110639 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
@@ -109682,14 +110931,14 @@
110931 /* The y.pTab=0 assignment in wherecode.c always happens after the
110932 ** impliesNotNullRow() test */
110933 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
110934 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
110935 if( (pLeft->op==TK_COLUMN
110936 && ALWAYS(pLeft->y.pTab!=0)
110937 && IsVirtual(pLeft->y.pTab))
110938 || (pRight->op==TK_COLUMN
110939 && ALWAYS(pRight->y.pTab!=0)
110940 && IsVirtual(pRight->y.pTab))
110941 ){
110942 return WRC_Prune;
110943 }
110944 /* no break */ deliberate_fall_through
@@ -113510,10 +114759,11 @@
114759 assert( k>=0 && k<pIdx->nColumn );
114760 i = pIdx->aiColumn[k];
114761 if( NEVER(i==XN_ROWID) ){
114762 VdbeComment((v,"%s.rowid",pIdx->zName));
114763 }else if( i==XN_EXPR ){
114764 assert( pIdx->bHasExpr );
114765 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
114766 }else{
114767 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
114768 }
114769 }
@@ -115825,11 +117075,11 @@
117075 if( p==0 ){
117076 #ifndef SQLITE_OMIT_VIRTUALTABLE
117077 /* If zName is the not the name of a table in the schema created using
117078 ** CREATE, then check to see if it is the name of an virtual table that
117079 ** can be an eponymous virtual table. */
117080 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
117081 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
117082 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
117083 pMod = sqlite3PragmaVtabRegister(db, zName);
117084 }
117085 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
@@ -115838,11 +117088,11 @@
117088 }
117089 }
117090 #endif
117091 if( flags & LOCATE_NOERR ) return 0;
117092 pParse->checkSchema = 1;
117093 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
117094 p = 0;
117095 }
117096
117097 if( p==0 ){
117098 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
@@ -117648,11 +118898,12 @@
118898 }
118899
118900 /* Recompute the colNotIdxed field of the Index.
118901 **
118902 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
118903 ** columns that are within the first 63 columns of the table and a 1 for
118904 ** all other bits (all columns that are not in the index). The
118905 ** high-order bit of colNotIdxed is always 1. All unindexed columns
118906 ** of the table have a 1.
118907 **
118908 ** 2019-10-24: For the purpose of this computation, virtual columns are
118909 ** not considered to be covered by the index, even if they are in the
@@ -117676,11 +118927,11 @@
118927 testcase( x==BMS-2 );
118928 if( x<BMS-1 ) m |= MASKBIT(x);
118929 }
118930 }
118931 pIdx->colNotIdxed = ~m;
118932 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
118933 }
118934
118935 /*
118936 ** This routine runs at the end of parsing a CREATE TABLE statement that
118937 ** has a WITHOUT ROWID clause. The job of this routine is to convert both
@@ -119564,10 +120815,11 @@
120815 pList = 0;
120816 }
120817 j = XN_EXPR;
120818 pIndex->aiColumn[i] = XN_EXPR;
120819 pIndex->uniqNotNull = 0;
120820 pIndex->bHasExpr = 1;
120821 }else{
120822 j = pCExpr->iColumn;
120823 assert( j<=0x7fff );
120824 if( j<0 ){
120825 j = pTab->iPKey;
@@ -119575,10 +120827,11 @@
120827 if( pTab->aCol[j].notNull==0 ){
120828 pIndex->uniqNotNull = 0;
120829 }
120830 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
120831 pIndex->bHasVCol = 1;
120832 pIndex->bHasExpr = 1;
120833 }
120834 }
120835 pIndex->aiColumn[i] = (i16)j;
120836 }
120837 zColl = 0;
@@ -123352,11 +124605,11 @@
124605 **
124606 ** For a case-insensitive search, set variable cx to be the same as
124607 ** c but in the other case and search the input string for either
124608 ** c or cx.
124609 */
124610 if( c<0x80 ){
124611 char zStop[3];
124612 int bMatch;
124613 if( noCase ){
124614 zStop[0] = sqlite3Toupper(c);
124615 zStop[1] = sqlite3Tolower(c);
@@ -123435,19 +124688,31 @@
124688 /*
124689 ** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
124690 ** non-zero if there is no match.
124691 */
124692 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
124693 if( zString==0 ){
124694 return zGlobPattern!=0;
124695 }else if( zGlobPattern==0 ){
124696 return 1;
124697 }else {
124698 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
124699 }
124700 }
124701
124702 /*
124703 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
124704 ** a miss - like strcmp().
124705 */
124706 SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
124707 if( zStr==0 ){
124708 return zPattern!=0;
124709 }else if( zPattern==0 ){
124710 return 1;
124711 }else{
124712 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
124713 }
124714 }
124715
124716 /*
124717 ** Count the number of times that the LIKE operator (or GLOB which is
124718 ** just a variation of LIKE) gets called. This is used for testing
@@ -126572,10 +127837,11 @@
127837 aff = pTab->aCol[x].affinity;
127838 }else if( x==XN_ROWID ){
127839 aff = SQLITE_AFF_INTEGER;
127840 }else{
127841 assert( x==XN_EXPR );
127842 assert( pIdx->bHasExpr );
127843 assert( pIdx->aColExpr!=0 );
127844 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
127845 }
127846 if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
127847 if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
@@ -130146,10 +131412,12 @@
131412 int (*deserialize)(sqlite3*,const char*,unsigned char*,
131413 sqlite3_int64,sqlite3_int64,unsigned);
131414 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
131415 unsigned int);
131416 const char *(*db_name)(sqlite3*,int);
131417 /* Version 3.40.0 and later */
131418 int (*value_encoding)(sqlite3_value*);
131419 };
131420
131421 /*
131422 ** This is the function signature used for all extension entry points. It
131423 ** is also defined in the file "loadext.c".
@@ -130470,10 +131738,12 @@
131738 #ifndef SQLITE_OMIT_DESERIALIZE
131739 #define sqlite3_deserialize sqlite3_api->deserialize
131740 #define sqlite3_serialize sqlite3_api->serialize
131741 #endif
131742 #define sqlite3_db_name sqlite3_api->db_name
131743 /* Version 3.40.0 and later */
131744 #define sqlite3_value_encoding sqlite3_api->value_encoding
131745 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
131746
131747 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
131748 /* This case when the file really is being compiled as a loadable
131749 ** extension */
@@ -130982,11 +132252,13 @@
132252 sqlite3_serialize,
132253 #else
132254 0,
132255 0,
132256 #endif
132257 sqlite3_db_name,
132258 /* Version 3.40.0 and later */
132259 sqlite3_value_type
132260 };
132261
132262 /* True if x is the directory separator character
132263 */
132264 #if SQLITE_OS_WIN
@@ -133786,12 +135058,13 @@
135058 Index *pIdx, *pPk;
135059 Index *pPrior = 0; /* Previous index */
135060 int loopTop;
135061 int iDataCur, iIdxCur;
135062 int r1 = -1;
135063 int bStrict; /* True for a STRICT table */
135064 int r2; /* Previous key for WITHOUT ROWID tables */
135065 int mxCol; /* Maximum non-virtual column number */
135066
135067 if( !IsOrdinaryTable(pTab) ) continue;
135068 if( pObjTab && pObjTab!=pTab ) continue;
135069 if( isQuick || HasRowid(pTab) ){
135070 pPk = 0;
@@ -133812,15 +135085,26 @@
135085 }
135086 assert( pParse->nMem>=8+j );
135087 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
135088 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
135089 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
135090
135091 /* Fetch the right-most column from the table. This will cause
135092 ** the entire record header to be parsed and sanity checked. It
135093 ** will also prepopulate the cursor column cache that is used
135094 ** by the OP_IsType code, so it is a required step.
135095 */
135096 mxCol = pTab->nCol-1;
135097 while( mxCol>=0
135098 && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
135099 || pTab->iPKey==mxCol) ) mxCol--;
135100 if( mxCol>=0 ){
135101 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
135102 sqlite3VdbeTypeofColumn(v, 3);
135103 }
135104
135105 if( !isQuick ){
 
 
 
 
135106 if( pPk ){
135107 /* Verify WITHOUT ROWID keys are in ascending order */
135108 int a1;
135109 char *zErr;
135110 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
@@ -133836,48 +135120,126 @@
135120 for(j=0; j<pPk->nKeyCol; j++){
135121 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
135122 }
135123 }
135124 }
135125 /* Verify datatypes for all columns:
135126 **
135127 ** (1) NOT NULL columns may not contain a NULL
135128 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
135129 ** (3) Datatype for TEXT columns in non-STRICT tables must be
135130 ** NULL, TEXT, or BLOB.
135131 ** (4) Datatype for numeric columns in non-STRICT tables must not
135132 ** be a TEXT value that can be losslessly converted to numeric.
135133 */
135134 bStrict = (pTab->tabFlags & TF_Strict)!=0;
135135 for(j=0; j<pTab->nCol; j++){
135136 char *zErr;
135137 Column *pCol = pTab->aCol + j; /* The column to be checked */
135138 int labelError; /* Jump here to report an error */
135139 int labelOk; /* Jump here if all looks ok */
135140 int p1, p3, p4; /* Operands to the OP_IsType opcode */
135141 int doTypeCheck; /* Check datatypes (besides NOT NULL) */
135142
135143 if( j==pTab->iPKey ) continue;
135144 if( bStrict ){
135145 doTypeCheck = pCol->eCType>COLTYPE_ANY;
135146 }else{
135147 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
 
135148 }
135149 if( pCol->notNull==0 && !doTypeCheck ) continue;
135150
135151 /* Compute the operands that will be needed for OP_IsType */
135152 p4 = SQLITE_NULL;
135153 if( pCol->colFlags & COLFLAG_VIRTUAL ){
135154 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135155 p1 = -1;
135156 p3 = 3;
135157 }else{
135158 if( pCol->iDflt ){
135159 sqlite3_value *pDfltValue = 0;
135160 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
135161 pCol->affinity, &pDfltValue);
135162 if( pDfltValue ){
135163 p4 = sqlite3_value_type(pDfltValue);
135164 sqlite3ValueFree(pDfltValue);
135165 }
135166 }
135167 p1 = iDataCur;
135168 if( !HasRowid(pTab) ){
135169 testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
135170 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
135171 }else{
135172 p3 = sqlite3TableColumnToStorage(pTab,j);
135173 testcase( p3!=j);
135174 }
135175 }
135176
135177 labelError = sqlite3VdbeMakeLabel(pParse);
135178 labelOk = sqlite3VdbeMakeLabel(pParse);
135179 if( pCol->notNull ){
135180 /* (1) NOT NULL columns may not contain a NULL */
135181 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135182 sqlite3VdbeChangeP5(v, 0x0f);
135183 VdbeCoverage(v);
135184 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
135185 pCol->zCnName);
135186 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135187 if( doTypeCheck ){
135188 sqlite3VdbeGoto(v, labelError);
135189 sqlite3VdbeJumpHere(v, jmp2);
135190 }else{
135191 /* VDBE byte code will fall thru */
135192 }
 
135193 }
135194 if( bStrict && doTypeCheck ){
135195 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
135196 static unsigned char aStdTypeMask[] = {
135197 0x1f, /* ANY */
135198 0x18, /* BLOB */
135199 0x11, /* INT */
135200 0x11, /* INTEGER */
135201 0x13, /* REAL */
135202 0x14 /* TEXT */
135203 };
135204 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135205 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
135206 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
135207 VdbeCoverage(v);
135208 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
135209 sqlite3StdType[pCol->eCType-1],
135210 pTab->zName, pTab->aCol[j].zCnName);
135211 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135212 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
135213 /* (3) Datatype for TEXT columns in non-STRICT tables must be
135214 ** NULL, TEXT, or BLOB. */
135215 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135216 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135217 VdbeCoverage(v);
135218 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
135219 pTab->zName, pTab->aCol[j].zCnName);
135220 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135221 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
135222 /* (4) Datatype for numeric columns in non-STRICT tables must not
135223 ** be a TEXT value that can be converted to numeric. */
135224 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
135225 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
135226 VdbeCoverage(v);
135227 if( p1>=0 ){
135228 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
135229 }
135230 sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
135231 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
135232 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
135233 VdbeCoverage(v);
135234 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
135235 pTab->zName, pTab->aCol[j].zCnName);
135236 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
135237 }
135238 sqlite3VdbeResolveLabel(v, labelError);
135239 integrityCheckResultRow(v);
135240 sqlite3VdbeResolveLabel(v, labelOk);
135241 }
135242 /* Verify CHECK constraints */
135243 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
135244 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
135245 if( db->mallocFailed==0 ){
@@ -135493,11 +136855,11 @@
136855 */
136856 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
136857 sParse.disableLookaside++;
136858 DisableLookaside;
136859 }
136860 sParse.prepFlags = prepFlags & 0xff;
136861
136862 /* Check to verify that it is possible to get a read lock on all
136863 ** database schemas. The inability to get a read lock indicates that
136864 ** some other database connection is holding a write-lock, which in
136865 ** turn means that the other connection has made uncommitted changes
@@ -135534,11 +136896,13 @@
136896 }
136897 }
136898 }
136899 }
136900
136901 #ifndef SQLITE_OMIT_VIRTUALTABLE
136902 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
136903 #endif
136904
136905 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
136906 char *zSqlCopy;
136907 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
136908 testcase( nBytes==mxLen );
@@ -139603,10 +140967,11 @@
140967 Parse *pParse; /* The parsing context */
140968 int iTable; /* Replace references to this table */
140969 int iNewTable; /* New table number */
140970 int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
140971 ExprList *pEList; /* Replacement expressions */
140972 ExprList *pCList; /* Collation sequences for replacement expr */
140973 } SubstContext;
140974
140975 /* Forward Declarations */
140976 static void substExprList(SubstContext*, ExprList*);
140977 static void substSelect(SubstContext*, Select*, int);
@@ -139644,13 +141009,14 @@
141009 pExpr->op = TK_NULL;
141010 }else
141011 #endif
141012 {
141013 Expr *pNew;
141014 int iColumn = pExpr->iColumn;
141015 Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
141016 Expr ifNullRow;
141017 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
141018 assert( pExpr->pRight==0 );
141019 if( sqlite3ExprIsVector(pCopy) ){
141020 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
141021 }else{
141022 sqlite3 *db = pSubst->pParse->db;
@@ -139684,15 +141050,20 @@
141050 ExprSetProperty(pExpr, EP_IntValue);
141051 }
141052
141053 /* Ensure that the expression now has an implicit collation sequence,
141054 ** just as it did when it was a column of a view or sub-query. */
141055 {
141056 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
141057 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
141058 pSubst->pCList->a[iColumn].pExpr
141059 );
141060 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
141061 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
141062 (pColl ? pColl->zName : "BINARY")
141063 );
141064 }
141065 }
141066 ExprClearProperty(pExpr, EP_Collate);
141067 }
141068 }
141069 }else{
@@ -139880,10 +141251,22 @@
141251 w.xExprCallback = renumberCursorsCb;
141252 w.xSelectCallback = sqlite3SelectWalkNoop;
141253 sqlite3WalkSelect(&w, p);
141254 }
141255 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
141256
141257 /*
141258 ** If pSel is not part of a compound SELECT, return a pointer to its
141259 ** expression list. Otherwise, return a pointer to the expression list
141260 ** of the leftmost SELECT in the compound.
141261 */
141262 static ExprList *findLeftmostExprlist(Select *pSel){
141263 while( pSel->pPrior ){
141264 pSel = pSel->pPrior;
141265 }
141266 return pSel->pEList;
141267 }
141268
141269 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
141270 /*
141271 ** This routine attempts to flatten subqueries as a performance optimization.
141272 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
@@ -140433,10 +141816,11 @@
141816 x.pParse = pParse;
141817 x.iTable = iParent;
141818 x.iNewTable = iNewParent;
141819 x.isOuterJoin = isOuterJoin;
141820 x.pEList = pSub->pEList;
141821 x.pCList = findLeftmostExprlist(pSub);
141822 substSelect(&x, pParent, 0);
141823 }
141824
141825 /* The flattened query is a compound if either the inner or the
141826 ** outer query is a compound. */
@@ -140452,11 +141836,11 @@
141836 if( pSub->pLimit ){
141837 pParent->pLimit = pSub->pLimit;
141838 pSub->pLimit = 0;
141839 }
141840
141841 /* Recompute the SrcItem.colUsed masks for the flattened
141842 ** tables. */
141843 for(i=0; i<nSubSrc; i++){
141844 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
141845 }
141846 }
@@ -140915,10 +142299,11 @@
142299 x.pParse = pParse;
142300 x.iTable = pSrc->iCursor;
142301 x.iNewTable = pSrc->iCursor;
142302 x.isOuterJoin = 0;
142303 x.pEList = pSubq->pEList;
142304 x.pCList = findLeftmostExprlist(pSubq);
142305 pNew = substExpr(&x, pNew);
142306 #ifndef SQLITE_OMIT_WINDOWFUNC
142307 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
142308 /* Restriction 6c has prevented push-down in this case */
142309 sqlite3ExprDelete(pParse->db, pNew);
@@ -141439,13 +142824,13 @@
142824 }
142825 }
142826 #endif
142827
142828 /*
142829 ** The SrcItem structure passed as the second argument represents a
142830 ** sub-query in the FROM clause of a SELECT statement. This function
142831 ** allocates and populates the SrcItem.pTab object. If successful,
142832 ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
142833 ** SQLITE_NOMEM.
142834 */
142835 SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){
142836 Select *pSel = pFrom->pSelect;
@@ -142274,11 +143659,11 @@
143659 #endif
143660 }
143661
143662 /*
143663 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
143664 ** If it is, then return the SrcItem for the prior view. If it is not,
143665 ** then return 0.
143666 */
143667 static SrcItem *isSelfJoinView(
143668 SrcList *pTabList, /* Search for self-joins in this FROM clause */
143669 SrcItem *pThis /* Search for prior reference to this subquery */
@@ -143318,11 +144703,11 @@
144703 ** in the right order to begin with.
144704 */
144705 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
144706 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
144707 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
144708 p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY)
144709 | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
144710 );
144711 if( pWInfo==0 ){
144712 sqlite3ExprListDelete(db, pDistinct);
144713 goto select_end;
@@ -143617,11 +145002,11 @@
145002 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
145003 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
145004
145005 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
145006 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
145007 pDistinct, p, minMaxFlag|distFlag, 0);
145008 if( pWInfo==0 ){
145009 goto select_end;
145010 }
145011 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
145012 eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -145101,11 +146486,11 @@
146486 sSubParse.pTriggerTab = pTab;
146487 sSubParse.pToplevel = pTop;
146488 sSubParse.zAuthContext = pTrigger->zName;
146489 sSubParse.eTriggerOp = pTrigger->op;
146490 sSubParse.nQueryLoop = pParse->nQueryLoop;
146491 sSubParse.prepFlags = pParse->prepFlags;
146492
146493 v = sqlite3GetVdbe(&sSubParse);
146494 if( v ){
146495 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
146496 pTrigger->zName, onErrorText(orconf),
@@ -145447,15 +146832,18 @@
146832 ** (not a virtual table) then the value might have been stored as an
146833 ** integer. In that case, add an OP_RealAffinity opcode to make sure
146834 ** it has been converted into REAL.
146835 */
146836 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
146837 Column *pCol;
146838 assert( pTab!=0 );
146839 assert( pTab->nCol>i );
146840 pCol = &pTab->aCol[i];
146841 if( pCol->iDflt ){
146842 sqlite3_value *pValue = 0;
146843 u8 enc = ENC(sqlite3VdbeDb(v));
146844 assert( !IsView(pTab) );
146845 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
146846 assert( i<pTab->nCol );
146847 sqlite3ValueFromExpr(sqlite3VdbeDb(v),
146848 sqlite3ColumnExpr(pTab,pCol), enc,
146849 pCol->affinity, &pValue);
@@ -145462,11 +146850,11 @@
146850 if( pValue ){
146851 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
146852 }
146853 }
146854 #ifndef SQLITE_OMIT_FLOATING_POINT
146855 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
146856 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
146857 }
146858 #endif
146859 }
146860
@@ -146902,10 +148290,11 @@
148290 Expr *pExpr;
148291 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
148292 if( pIdx->aiColumn[ii]==XN_EXPR ){
148293 assert( pIdx->aColExpr!=0 );
148294 assert( pIdx->aColExpr->nExpr>ii );
148295 assert( pIdx->bHasExpr );
148296 pExpr = pIdx->aColExpr->a[ii].pExpr;
148297 if( pExpr->op!=TK_COLLATE ){
148298 sCol[0].pLeft = pExpr;
148299 pExpr = &sCol[0];
148300 }
@@ -147215,10 +148604,11 @@
148604 int isMemDb; /* True if vacuuming a :memory: database */
148605 int nRes; /* Bytes of reserved space at the end of each page */
148606 int nDb; /* Number of attached databases */
148607 const char *zDbMain; /* Schema name of database to vacuum */
148608 const char *zOut; /* Name of output file */
148609 u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
148610
148611 if( !db->autoCommit ){
148612 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
148613 return SQLITE_ERROR; /* IMP: R-12218-18073 */
148614 }
@@ -147286,16 +148676,21 @@
148676 rc = SQLITE_ERROR;
148677 sqlite3SetString(pzErrMsg, db, "output file already exists");
148678 goto end_of_vacuum;
148679 }
148680 db->mDbFlags |= DBFLAG_VacuumInto;
148681
148682 /* For a VACUUM INTO, the pager-flags are set to the same values as
148683 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
148684 ** is always set. */
148685 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
148686 }
148687 nRes = sqlite3BtreeGetRequestedReserve(pMain);
148688
148689 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
148690 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
148691 sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL);
148692
148693 /* Begin a transaction and take an exclusive lock on the main database
148694 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
148695 ** to ensure that we do not try to change the page-size on a WAL database.
148696 */
@@ -148605,11 +150000,11 @@
150000 /* Check to see the left operand is a column in a virtual table */
150001 if( NEVER(pExpr==0) ) return pDef;
150002 if( pExpr->op!=TK_COLUMN ) return pDef;
150003 assert( ExprUseYTab(pExpr) );
150004 pTab = pExpr->y.pTab;
150005 if( NEVER(pTab==0) ) return pDef;
150006 if( !IsVirtual(pTab) ) return pDef;
150007 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
150008 assert( pVtab!=0 );
150009 assert( pVtab->pModule!=0 );
150010 pMod = (sqlite3_module *)pVtab->pModule;
@@ -149212,11 +150607,11 @@
150607 /*
150608 ** An instance of the following structure keeps track of a mapping
150609 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
150610 **
150611 ** The VDBE cursor numbers are small integers contained in
150612 ** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
150613 ** clause, the cursor numbers might not begin with 0 and they might
150614 ** contain gaps in the numbering sequence. But we want to make maximum
150615 ** use of the bits in our bitmasks. This structure provides a mapping
150616 ** from the sparse cursor numbers into consecutive integers beginning
150617 ** with 0.
@@ -149283,24 +150678,10 @@
150678 #endif
150679 #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
150680 # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
150681 #endif
150682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150683 /*
150684 ** The WHERE clause processing routine has two halves. The
150685 ** first part does the start of the WHERE loop and the second
150686 ** half does the tail of the WHERE loop. An instance of
150687 ** this structure is returned by the first half and passed
@@ -149312,14 +150693,14 @@
150693 struct WhereInfo {
150694 Parse *pParse; /* Parsing and code generating context */
150695 SrcList *pTabList; /* List of tables in the join */
150696 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
150697 ExprList *pResultSet; /* Result set of the query */
150698 #if WHERETRACE_ENABLED
150699 Expr *pWhere; /* The complete WHERE clause */
 
 
150700 #endif
150701 Select *pSelect; /* The entire SELECT statement containing WHERE */
150702 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
150703 int iContinue; /* Jump here to continue with next record */
150704 int iBreak; /* Jump here to break out of the loop */
150705 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
150706 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -149334,11 +150715,10 @@
150715 unsigned sorted :1; /* True if really sorted (not just grouped) */
150716 LogEst nRowOut; /* Estimated number of output rows */
150717 int iTop; /* The very beginning of the WHERE loop */
150718 int iEndWhere; /* End of the WHERE clause itself */
150719 WhereLoop *pLoops; /* List of all WhereLoop objects */
 
150720 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
150721 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
150722 WhereClause sWC; /* Decomposition of the WHERE clause */
150723 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
150724 WhereLevel a[1]; /* Information about each nest loop in WHERE */
@@ -150686,147 +152066,10 @@
152066 assert( nReg==1 || pParse->nErr );
152067 sqlite3ExprCode(pParse, p, iReg);
152068 }
152069 }
152070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152071 /*
152072 ** The pTruth expression is always true because it is the WHERE clause
152073 ** a partial index that is driving a query loop. Look through all of the
152074 ** WHERE clause terms on the query, and if any of those terms must be
152075 ** true because pTruth is true, then mark those WHERE clause terms as
@@ -150891,10 +152134,12 @@
152134 assert( pTerm!=0 );
152135 assert( pTerm->pExpr!=0 );
152136 testcase( pTerm->wtFlags & TERM_VIRTUAL );
152137 regRowid = sqlite3GetTempReg(pParse);
152138 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
152139 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
152140 VdbeCoverage(pParse->pVdbe);
152141 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
152142 addrNxt, regRowid, 1);
152143 VdbeCoverage(pParse->pVdbe);
152144 }else{
152145 u16 nEq = pLoop->u.btree.nEq;
@@ -151042,13 +152287,13 @@
152287 codeExprOrVector(pParse, pRight, iTarget, 1);
152288 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
152289 && pLoop->u.vtab.bOmitOffset
152290 ){
152291 assert( pTerm->eOperator==WO_AUX );
152292 assert( pWInfo->pSelect!=0 );
152293 assert( pWInfo->pSelect->iOffset>0 );
152294 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
152295 VdbeComment((v,"Zero OFFSET counter"));
152296 }
152297 }
152298 }
152299 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
@@ -151152,10 +152397,12 @@
152397 iReleaseReg = ++pParse->nMem;
152398 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
152399 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
152400 addrNxt = pLevel->addrNxt;
152401 if( pLevel->regFilter ){
152402 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
152403 VdbeCoverage(v);
152404 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
152405 iRowidReg, 1);
152406 VdbeCoverage(v);
152407 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
152408 }
@@ -151503,10 +152750,15 @@
152750 ** of entries in the tree, so basing the number of steps to try
152751 ** on the estimated number of rows in the btree seems like a good
152752 ** guess. */
152753 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
152754 (pIdx->aiRowLogEst[0]+9)/10);
152755 if( pRangeStart ){
152756 sqlite3VdbeChangeP5(v, 1);
152757 sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
152758 addrSeekScan = 0;
152759 }
152760 VdbeCoverage(v);
152761 }
152762 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
152763 VdbeCoverage(v);
152764 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
@@ -151641,31 +152893,10 @@
152893 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
152894 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
152895 }
152896
152897 if( pLevel->iLeftJoin==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152898 /* If a partial index is driving the loop, try to eliminate WHERE clause
152899 ** terms from the query that must be true due to the WHERE clause of
152900 ** the partial index.
152901 **
152902 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
@@ -151774,11 +153005,11 @@
153005 */
153006 if( pWInfo->nLevel>1 ){
153007 int nNotReady; /* The number of notReady tables */
153008 SrcItem *origSrc; /* Original list of tables */
153009 nNotReady = pWInfo->nLevel - iLevel - 1;
153010 pOrTab = sqlite3DbMallocRawNN(db,
153011 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
153012 if( pOrTab==0 ) return notReady;
153013 pOrTab->nAlloc = (u8)(nNotReady + 1);
153014 pOrTab->nSrc = pOrTab->nAlloc;
153015 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
@@ -152027,11 +153258,11 @@
153258 ** indent everything in between the this point and the final OP_Return.
153259 ** See tag-20220407a in vdbe.c and shell.c */
153260 assert( pLevel->op==OP_Return );
153261 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153262
153263 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
153264 if( !untestedTerms ) disableTerm(pLevel, pTerm);
153265 }else
153266 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
153267
153268 {
@@ -152655,11 +153886,11 @@
153886 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
153887 */
153888 if( pLeft->op!=TK_COLUMN
153889 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
153890 || (ALWAYS( ExprUseYTab(pLeft) )
153891 && ALWAYS(pLeft->y.pTab)
153892 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
153893 ){
153894 int isNum;
153895 double rDummy;
153896 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
@@ -152772,12 +154003,11 @@
154003 **
154004 ** vtab_column MATCH expression
154005 ** MATCH(expression,vtab_column)
154006 */
154007 pCol = pList->a[1].pExpr;
154008 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
 
154009 if( ExprIsVtab(pCol) ){
154010 for(i=0; i<ArraySize(aOp); i++){
154011 assert( !ExprHasProperty(pExpr, EP_IntValue) );
154012 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
154013 *peOp2 = aOp[i].eOp2;
@@ -152798,11 +154028,11 @@
154028 ** names. But for this use case, xFindFunction is expected to deal
154029 ** with function names in an arbitrary case.
154030 */
154031 pCol = pList->a[0].pExpr;
154032 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
154033 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
154034 if( ExprIsVtab(pCol) ){
154035 sqlite3_vtab *pVtab;
154036 sqlite3_module *pMod;
154037 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
154038 void *pNotUsed;
@@ -152823,17 +154053,16 @@
154053 }
154054 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
154055 int res = 0;
154056 Expr *pLeft = pExpr->pLeft;
154057 Expr *pRight = pExpr->pRight;
154058 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
 
154059 if( ExprIsVtab(pLeft) ){
154060 res++;
154061 }
154062 assert( pRight==0 || pRight->op!=TK_COLUMN
154063 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
154064 if( pRight && ExprIsVtab(pRight) ){
154065 res++;
154066 SWAP(Expr*, pLeft, pRight);
154067 }
154068 *ppLeft = pLeft;
@@ -153378,10 +154607,11 @@
154607 iCur = pFrom->a[i].iCursor;
154608 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154609 if( pIdx->aColExpr==0 ) continue;
154610 for(i=0; i<pIdx->nKeyCol; i++){
154611 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
154612 assert( pIdx->bHasExpr );
154613 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
154614 aiCurCol[0] = iCur;
154615 aiCurCol[1] = XN_EXPR;
154616 return 1;
154617 }
@@ -153991,13 +155221,13 @@
155221 **
155222 ** LIMIT and OFFSET terms are ignored by most of the planner code. They
155223 ** exist only so that they may be passed to the xBestIndex method of the
155224 ** single virtual table in the FROM clause of the SELECT.
155225 */
155226 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
155227 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
155228 if( p->pGroupBy==0
155229 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
155230 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
155231 ){
155232 ExprList *pOrderBy = p->pOrderBy;
155233 int iCsr = p->pSrc->a[0].iCursor;
@@ -156530,30 +157760,18 @@
157760 while( pWInfo->pLoops ){
157761 WhereLoop *p = pWInfo->pLoops;
157762 pWInfo->pLoops = p->pNextLoop;
157763 whereLoopDelete(db, p);
157764 }
 
157765 while( pWInfo->pMemToFree ){
157766 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
157767 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
157768 pWInfo->pMemToFree = pNext;
157769 }
157770 sqlite3DbNNFreeNN(db, pWInfo);
157771 }
157772
 
 
 
 
 
 
 
 
 
 
 
157773 /*
157774 ** Return TRUE if all of the following are true:
157775 **
157776 ** (1) X has the same or lower cost, or returns the same or fewer rows,
157777 ** than Y.
@@ -157501,10 +158719,98 @@
158719 return 1;
158720 }
158721 }
158722 return 0;
158723 }
158724
158725 /*
158726 ** Structure passed to the whereIsCoveringIndex Walker callback.
158727 */
158728 struct CoveringIndexCheck {
158729 Index *pIdx; /* The index */
158730 int iTabCur; /* Cursor number for the corresponding table */
158731 };
158732
158733 /*
158734 ** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
158735 **
158736 ** If the Expr node references the table with cursor pCk->iTabCur, then
158737 ** make sure that column is covered by the index pCk->pIdx. We know that
158738 ** all columns less than 63 (really BMS-1) are covered, so we don't need
158739 ** to check them. But we do need to check any column at 63 or greater.
158740 **
158741 ** If the index does not cover the column, then set pWalk->eCode to
158742 ** non-zero and return WRC_Abort to stop the search.
158743 **
158744 ** If this node does not disprove that the index can be a covering index,
158745 ** then just return WRC_Continue, to continue the search.
158746 */
158747 static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){
158748 int i; /* Loop counter */
158749 const Index *pIdx; /* The index of interest */
158750 const i16 *aiColumn; /* Columns contained in the index */
158751 u16 nColumn; /* Number of columns in the index */
158752 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
158753 if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
158754 if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
158755 pIdx = pWalk->u.pCovIdxCk->pIdx;
158756 aiColumn = pIdx->aiColumn;
158757 nColumn = pIdx->nColumn;
158758 for(i=0; i<nColumn; i++){
158759 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
158760 }
158761 pWalk->eCode = 1;
158762 return WRC_Abort;
158763 }
158764
158765
158766 /*
158767 ** pIdx is an index that covers all of the low-number columns used by
158768 ** pWInfo->pSelect (columns from 0 through 62). But there are columns
158769 ** in pWInfo->pSelect beyond 62. This routine tries to answer the question
158770 ** of whether pIdx covers *all* columns in the query.
158771 **
158772 ** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
158773 ** not a covering index or if we are unable to determine if pIdx is a
158774 ** covering index.
158775 **
158776 ** This routine is an optimization. It is always safe to return non-zero.
158777 ** But returning zero when non-zero should have been returned can lead to
158778 ** incorrect bytecode and assertion faults.
158779 */
158780 static SQLITE_NOINLINE u32 whereIsCoveringIndex(
158781 WhereInfo *pWInfo, /* The WHERE clause context */
158782 Index *pIdx, /* Index that is being tested */
158783 int iTabCur /* Cursor for the table being indexed */
158784 ){
158785 int i;
158786 struct CoveringIndexCheck ck;
158787 Walker w;
158788 if( pWInfo->pSelect==0 ){
158789 /* We don't have access to the full query, so we cannot check to see
158790 ** if pIdx is covering. Assume it is not. */
158791 return 1;
158792 }
158793 for(i=0; i<pIdx->nColumn; i++){
158794 if( pIdx->aiColumn[i]>=BMS-1 ) break;
158795 }
158796 if( i>=pIdx->nColumn ){
158797 /* pIdx does not index any columns greater than 62, but we know from
158798 ** colMask that columns greater than 62 are used, so this is not a
158799 ** covering index */
158800 return 1;
158801 }
158802 ck.pIdx = pIdx;
158803 ck.iTabCur = iTabCur;
158804 memset(&w, 0, sizeof(w));
158805 w.xExprCallback = whereIsCoveringIndexWalkCallback;
158806 w.xSelectCallback = sqlite3SelectWalkNoop;
158807 w.u.pCovIdxCk = &ck;
158808 w.eCode = 0;
158809 sqlite3WalkSelect(&w, pWInfo->pSelect);
158810 return w.eCode;
158811 }
158812
158813 /*
158814 ** Add all WhereLoop objects for a single table of the join where the table
158815 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
158816 ** a b-tree table, not a virtual table.
@@ -157719,10 +159025,13 @@
159025 if( pProbe->isCovering ){
159026 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
159027 m = 0;
159028 }else{
159029 m = pSrc->colUsed & pProbe->colNotIdxed;
159030 if( m==TOPBIT ){
159031 m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
159032 }
159033 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
159034 }
159035
159036 /* Full scan via index */
159037 if( b
@@ -158944,11 +160253,10 @@
160253 */
160254 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
160255 int mxChoice; /* Maximum number of simultaneous paths tracked */
160256 int nLoop; /* Number of terms in the join */
160257 Parse *pParse; /* Parsing context */
 
160258 int iLoop; /* Loop counter over the terms of the join */
160259 int ii, jj; /* Loop counters */
160260 int mxI = 0; /* Index of next entry to replace */
160261 int nOrderBy; /* Number of ORDER BY clause terms */
160262 LogEst mxCost = 0; /* Maximum cost of a set of paths */
@@ -158963,11 +160271,10 @@
160271 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
160272 char *pSpace; /* Temporary memory used by this routine */
160273 int nSpace; /* Bytes of space allocated at pSpace */
160274
160275 pParse = pWInfo->pParse;
 
160276 nLoop = pWInfo->nLevel;
160277 /* TUNING: For simple queries, only the best path is tracked.
160278 ** For 2-way joins, the 5 best paths are followed.
160279 ** For joins of 3 or more tables, track the 10 best paths */
160280 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
@@ -158986,11 +160293,11 @@
160293 }
160294
160295 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
160296 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
160297 nSpace += sizeof(LogEst) * nOrderBy;
160298 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
160299 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
160300 aTo = (WherePath*)pSpace;
160301 aFrom = aTo+mxChoice;
160302 memset(aFrom, 0, sizeof(aFrom[0]));
160303 pX = (WhereLoop**)(aFrom+mxChoice);
@@ -159244,11 +160551,11 @@
160551 nFrom = nTo;
160552 }
160553
160554 if( nFrom==0 ){
160555 sqlite3ErrorMsg(pParse, "no query solution");
160556 sqlite3StackFreeNN(pParse->db, pSpace);
160557 return SQLITE_ERROR;
160558 }
160559
160560 /* Find the lowest cost path. pFrom will be left pointing to that path */
160561 pFrom = aFrom;
@@ -159326,12 +160633,11 @@
160633
160634
160635 pWInfo->nRowOut = pFrom->nRow;
160636
160637 /* Free temporary memory and return success */
160638 sqlite3StackFreeNN(pParse->db, pSpace);
 
160639 return SQLITE_OK;
160640 }
160641
160642 /*
160643 ** Most queries use only a single table (they are not joins) and have
@@ -159625,10 +160931,81 @@
160931 }
160932 }
160933 nSearch += pLoop->nOut;
160934 }
160935 }
160936
160937 /*
160938 ** This is an sqlite3ParserAddCleanup() callback that is invoked to
160939 ** free the Parse->pIdxExpr list when the Parse object is destroyed.
160940 */
160941 static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){
160942 Parse *pParse = (Parse*)pObject;
160943 while( pParse->pIdxExpr!=0 ){
160944 IndexedExpr *p = pParse->pIdxExpr;
160945 pParse->pIdxExpr = p->pIENext;
160946 sqlite3ExprDelete(db, p->pExpr);
160947 sqlite3DbFreeNN(db, p);
160948 }
160949 }
160950
160951 /*
160952 ** The index pIdx is used by a query and contains one or more expressions.
160953 ** In other words pIdx is an index on an expression. iIdxCur is the cursor
160954 ** number for the index and iDataCur is the cursor number for the corresponding
160955 ** table.
160956 **
160957 ** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
160958 ** each of the expressions in the index so that the expression code generator
160959 ** will know to replace occurrences of the indexed expression with
160960 ** references to the corresponding column of the index.
160961 */
160962 static SQLITE_NOINLINE void whereAddIndexedExpr(
160963 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
160964 Index *pIdx, /* The index-on-expression that contains the expressions */
160965 int iIdxCur, /* Cursor number for pIdx */
160966 SrcItem *pTabItem /* The FROM clause entry for the table */
160967 ){
160968 int i;
160969 IndexedExpr *p;
160970 Table *pTab;
160971 assert( pIdx->bHasExpr );
160972 pTab = pIdx->pTable;
160973 for(i=0; i<pIdx->nColumn; i++){
160974 Expr *pExpr;
160975 int j = pIdx->aiColumn[i];
160976 int bMaybeNullRow;
160977 if( j==XN_EXPR ){
160978 pExpr = pIdx->aColExpr->a[i].pExpr;
160979 testcase( pTabItem->fg.jointype & JT_LEFT );
160980 testcase( pTabItem->fg.jointype & JT_RIGHT );
160981 testcase( pTabItem->fg.jointype & JT_LTORJ );
160982 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
160983 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
160984 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
160985 bMaybeNullRow = 0;
160986 }else{
160987 continue;
160988 }
160989 if( sqlite3ExprIsConstant(pExpr) ) continue;
160990 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
160991 if( p==0 ) break;
160992 p->pIENext = pParse->pIdxExpr;
160993 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
160994 p->iDataCur = pTabItem->iCursor;
160995 p->iIdxCur = iIdxCur;
160996 p->iIdxCol = i;
160997 p->bMaybeNullRow = bMaybeNullRow;
160998 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
160999 p->zIdxName = pIdx->zName;
161000 #endif
161001 pParse->pIdxExpr = p;
161002 if( p->pIENext==0 ){
161003 sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pParse);
161004 }
161005 }
161006 }
161007
161008 /*
161009 ** Generate the beginning of the loop used for WHERE clause processing.
161010 ** The return value is a pointer to an opaque structure that contains
161011 ** information needed to terminate the loop. Later, the calling routine
@@ -159720,11 +161097,11 @@
161097 Parse *pParse, /* The parser context */
161098 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
161099 Expr *pWhere, /* The WHERE clause */
161100 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
161101 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
161102 Select *pSelect, /* The entire SELECT statement */
161103 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
161104 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
161105 ** If WHERE_USE_LIMIT, then the limit amount */
161106 ){
161107 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -159789,21 +161166,21 @@
161166 goto whereBeginError;
161167 }
161168 pWInfo->pParse = pParse;
161169 pWInfo->pTabList = pTabList;
161170 pWInfo->pOrderBy = pOrderBy;
161171 #if WHERETRACE_ENABLED
161172 pWInfo->pWhere = pWhere;
161173 #endif
161174 pWInfo->pResultSet = pResultSet;
161175 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
161176 pWInfo->nLevel = nTabList;
161177 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
161178 pWInfo->wctrlFlags = wctrlFlags;
161179 pWInfo->iLimit = iAuxArg;
161180 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
161181 pWInfo->pSelect = pSelect;
 
 
161182 memset(&pWInfo->nOBSat, 0,
161183 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
161184 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
161185 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
161186 pMaskSet = &pWInfo->sMaskSet;
@@ -159868,11 +161245,13 @@
161245 #endif
161246 }
161247
161248 /* Analyze all of the subexpressions. */
161249 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
161250 if( pSelect && pSelect->pLimit ){
161251 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
161252 }
161253 if( pParse->nErr ) goto whereBeginError;
161254
161255 /* Special case: WHERE terms that do not refer to any tables in the join
161256 ** (constant expressions). Evaluate each such term, and jump over all the
161257 ** generated code if the result is not true.
@@ -160171,10 +161550,13 @@
161550 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
161551 iIndexCur = iAuxArg;
161552 op = OP_ReopenIdx;
161553 }else{
161554 iIndexCur = pParse->nTab++;
161555 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
161556 whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem);
161557 }
161558 }
161559 pLevel->iIdxCur = iIndexCur;
161560 assert( pIx!=0 );
161561 assert( pIx->pSchema==pTab->pSchema );
161562 assert( iIndexCur>=0 );
@@ -160293,12 +161675,10 @@
161675 return pWInfo;
161676
161677 /* Jump here if malloc fails */
161678 whereBeginError:
161679 if( pWInfo ){
 
 
161680 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
161681 whereInfoFree(db, pWInfo);
161682 }
161683 return 0;
161684 }
@@ -160513,11 +161893,10 @@
161893 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
161894 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
161895 }
161896
161897 assert( pWInfo->nLevel<=pTabList->nSrc );
 
161898 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
161899 int k, last;
161900 VdbeOp *pOp, *pLastOp;
161901 Index *pIdx = 0;
161902 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
@@ -160566,10 +161945,20 @@
161945 ){
161946 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
161947 last = iEnd;
161948 }else{
161949 last = pWInfo->iEndWhere;
161950 }
161951 if( pIdx->bHasExpr ){
161952 IndexedExpr *p = pParse->pIdxExpr;
161953 while( p ){
161954 if( p->iIdxCur==pLevel->iIdxCur ){
161955 p->iDataCur = -1;
161956 p->iIdxCur = -1;
161957 }
161958 p = p->pIENext;
161959 }
161960 }
161961 k = pLevel->addrBody + 1;
161962 #ifdef SQLITE_DEBUG
161963 if( db->flags & SQLITE_VdbeAddopTrace ){
161964 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
@@ -174293,10 +175682,23 @@
175682 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
175683 if( db->mallocFailed ){
175684 goto opendb_out;
175685 }
175686
175687 #if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
175688 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
175689 if( zFilename && zFilename[0]==':' ){
175690 if( strcmp(zFilename, ":localStorage:")==0 ){
175691 zFilename = "file:local?vfs=kvvfs";
175692 flags |= SQLITE_OPEN_URI;
175693 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
175694 zFilename = "file:session?vfs=kvvfs";
175695 flags |= SQLITE_OPEN_URI;
175696 }
175697 }
175698 #endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
175699
175700 /* Parse the filename/URI argument
175701 **
175702 ** Only allow sensible combinations of bits in the flags argument.
175703 ** Throw an error if any non-sense combination is used. If we
175704 ** do not block illegal combinations here, it could trigger
@@ -175722,12 +177124,12 @@
177124 ** Recover as many snapshots as possible from the wal file associated with
177125 ** schema zDb of database db.
177126 */
177127 SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
177128 int rc = SQLITE_ERROR;
 
177129 #ifndef SQLITE_OMIT_WAL
177130 int iDb;
177131
177132 #ifdef SQLITE_ENABLE_API_ARMOR
177133 if( !sqlite3SafetyCheckOk(db) ){
177134 return SQLITE_MISUSE_BKPT;
177135 }
@@ -201626,11 +203028,11 @@
203028 return SQLITE_LOCKED_VTAB;
203029 }
203030 rtreeReference(pRtree);
203031 assert(nData>=1);
203032
203033 memset(&cell, 0, sizeof(cell));
203034
203035 /* Constraint handling. A write operation on an r-tree table may return
203036 ** SQLITE_CONSTRAINT for two reasons:
203037 **
203038 ** 1. A duplicate rowid value, or
@@ -236984,11 +238386,11 @@
238386 int nArg, /* Number of args */
238387 sqlite3_value **apUnused /* Function arguments */
238388 ){
238389 assert( nArg==0 );
238390 UNUSED_PARAM2(nArg, apUnused);
238391 sqlite3_result_text(pCtx, "fts5: 2022-10-26 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd", -1, SQLITE_TRANSIENT);
238392 }
238393
238394 /*
238395 ** Return true if zName is the extension on one of the shadow tables used
238396 ** by this module.
238397
+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 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
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 11:11:31 3dfdfb3f12edb3f4267942598efd05d573e13b7c5d6cdbc3404373f41b8993dd"
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