Fossil SCM

Update the built-in SQLite to the latest 3.38.0 alpha that include the new ".mode qbox" command and other enhancements in the shell.

drh 2022-01-31 14:19 trunk
Commit 100118cb91f1239affd3ca2446bcc1cf190f41e99b2d94968b42ee6ceefff9e9
3 files changed +276 -54 +284 -64 +86 -37
+276 -54
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -12161,10 +12161,12 @@
1216112161
u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
1216212162
u8 nEqpLevel; /* Depth of the EQP output graph */
1216312163
u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
1216412164
u8 bSafeMode; /* True to prohibit unsafe operations */
1216512165
u8 bSafeModePersist; /* The long-term value of bSafeMode */
12166
+ u8 bQuote; /* Quote results for .mode box and table */
12167
+ int iWrap; /* Wrap lines this long or longer in some output modes */
1216612168
unsigned statsOn; /* True to display memory stats before each finalize */
1216712169
unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
1216812170
int inputNesting; /* Track nesting level of .read and other redirects */
1216912171
int outCount; /* Revert to stdout when reaching zero */
1217012172
int cnt; /* Number of records displayed so far */
@@ -14241,11 +14243,116 @@
1424114243
utf8_printf(p->out, "%s", zSep3);
1424214244
}
1424314245
fputs("\n", p->out);
1424414246
}
1424514247
14248
+/*
14249
+** z[] is a line of text that is to be displayed the .mode box or table or
14250
+** similar tabular formats. z[] might contain control characters such
14251
+** as \n, \t, \f, or \r.
14252
+**
14253
+** Compute characters to display on the first line of z[]. Stop at the
14254
+** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
14255
+** from malloc()) of that first line. Write anything to display
14256
+** on the next line into *pzTail. If this is the last line, write a NULL
14257
+** into *pzTail.
14258
+*/
14259
+static char *translateForDisplayAndDup(
14260
+ const unsigned char *z,
14261
+ const unsigned char **pzTail,
14262
+ int mxWidth
14263
+){
14264
+ int i, j, n;
14265
+ unsigned char *zOut;
14266
+ if( z==0 ){
14267
+ *pzTail = 0;
14268
+ return 0;
14269
+ }
14270
+ if( mxWidth<0 ) mxWidth = -mxWidth;
14271
+ if( mxWidth==0 ) mxWidth = 1000000;
14272
+ i = j= n = 0;
14273
+ while( n<mxWidth ){
14274
+ if( z[i]>=' ' ){
14275
+ n++;
14276
+ do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
14277
+ continue;
14278
+ }
14279
+ if( z[i]=='\t' ){
14280
+ do{
14281
+ n++;
14282
+ j++;
14283
+ }while( (n&7)!=0 && n<mxWidth );
14284
+ n += 8;
14285
+ n &= ~7;
14286
+ i++;
14287
+ continue;
14288
+ }
14289
+ break;
14290
+ }
14291
+ if( n>=mxWidth && z[i]>=' ' ){
14292
+ *pzTail = &z[i];
14293
+ }else if( z[i]=='\r' && z[i+1]=='\n' ){
14294
+ *pzTail = z[i+2] ? &z[i+2] : 0;
14295
+ }else if( z[i]==0 || z[i+1]==0 ){
14296
+ *pzTail = 0;
14297
+ }else{
14298
+ *pzTail = &z[i+1];
14299
+ }
14300
+ zOut = malloc( j+1 );
14301
+ shell_check_oom(zOut);
14302
+ i = j = n = 0;
14303
+ while( n<mxWidth ){
14304
+ if( z[i]>=' ' ){
14305
+ n++;
14306
+ do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
14307
+ continue;
14308
+ }
14309
+ if( z[i]=='\t' ){
14310
+ do{
14311
+ n++;
14312
+ zOut[j++] = ' ';
14313
+ }while( (n&7)!=0 && n<mxWidth );
14314
+ i++;
14315
+ continue;
14316
+ }
14317
+ break;
14318
+ }
14319
+ zOut[j] = 0;
14320
+ return (char*)zOut;
14321
+}
1424614322
14323
+/* Extract the value of the i-th current column for pStmt as an SQL literal
14324
+** value. Memory is obtained from sqlite3_malloc64() and must be freed by
14325
+** the caller.
14326
+*/
14327
+static char *quoted_column(sqlite3_stmt *pStmt, int i){
14328
+ switch( sqlite3_column_type(pStmt, i) ){
14329
+ case SQLITE_NULL: {
14330
+ return sqlite3_mprintf("NULL");
14331
+ }
14332
+ case SQLITE_INTEGER:
14333
+ case SQLITE_FLOAT: {
14334
+ return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
14335
+ }
14336
+ case SQLITE_TEXT: {
14337
+ return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
14338
+ }
14339
+ case SQLITE_BLOB: {
14340
+ int j;
14341
+ sqlite3_str *pStr = sqlite3_str_new(0);
14342
+ const unsigned char *a = sqlite3_column_blob(pStmt,i);
14343
+ int n = sqlite3_column_bytes(pStmt,i);
14344
+ sqlite3_str_append(pStr, "x'", 2);
14345
+ for(j=0; j<n; j++){
14346
+ sqlite3_str_appendf(pStr, "%02x", a[j]);
14347
+ }
14348
+ sqlite3_str_append(pStr, "'", 1);
14349
+ return sqlite3_str_finish(pStr);
14350
+ }
14351
+ }
14352
+ return 0; /* Not reached */
14353
+}
1424714354
1424814355
/*
1424914356
** Run a prepared statement and output the result in one of the
1425014357
** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
1425114358
** or MODE_Box.
@@ -14261,39 +14368,40 @@
1426114368
){
1426214369
sqlite3_int64 nRow = 0;
1426314370
int nColumn = 0;
1426414371
char **azData = 0;
1426514372
sqlite3_int64 nAlloc = 0;
14373
+ char *abRowDiv = 0;
14374
+ const unsigned char *uz;
1426614375
const char *z;
14376
+ char **azQuoted = 0;
1426714377
int rc;
1426814378
sqlite3_int64 i, nData;
1426914379
int j, nTotal, w, n;
1427014380
const char *colSep = 0;
1427114381
const char *rowSep = 0;
14382
+ const unsigned char **azNextLine = 0;
14383
+ int bNextLine = 0;
14384
+ int bMultiLineRowExists = 0;
1427214385
1427314386
rc = sqlite3_step(pStmt);
1427414387
if( rc!=SQLITE_ROW ) return;
1427514388
nColumn = sqlite3_column_count(pStmt);
1427614389
nAlloc = nColumn*4;
1427714390
if( nAlloc<=0 ) nAlloc = 1;
1427814391
azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
1427914392
shell_check_oom(azData);
14280
- for(i=0; i<nColumn; i++){
14281
- azData[i] = strdup(sqlite3_column_name(pStmt,i));
14282
- }
14283
- do{
14284
- if( (nRow+2)*nColumn >= nAlloc ){
14285
- nAlloc *= 2;
14286
- azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14287
- shell_check_oom(azData);
14288
- }
14289
- nRow++;
14290
- for(i=0; i<nColumn; i++){
14291
- z = (const char*)sqlite3_column_text(pStmt,i);
14292
- azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14293
- }
14294
- }while( sqlite3_step(pStmt)==SQLITE_ROW );
14393
+ azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
14394
+ shell_check_oom(azNextLine);
14395
+ memset(azNextLine, 0, nColumn*sizeof(char*) );
14396
+ if( p->bQuote ){
14397
+ azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
14398
+ shell_check_oom(azQuoted);
14399
+ memset(azQuoted, 0, nColumn*sizeof(char*) );
14400
+ }
14401
+ abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
14402
+ shell_check_oom(abRowDiv);
1429514403
if( nColumn>p->nWidth ){
1429614404
p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
1429714405
shell_check_oom(p->colWidth);
1429814406
for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
1429914407
p->nWidth = nColumn;
@@ -14303,10 +14411,45 @@
1430314411
for(i=0; i<nColumn; i++){
1430414412
w = p->colWidth[i];
1430514413
if( w<0 ) w = -w;
1430614414
p->actualWidth[i] = w;
1430714415
}
14416
+ for(i=0; i<nColumn; i++){
14417
+ azData[i] = strdup(sqlite3_column_name(pStmt,i));
14418
+ }
14419
+ do{
14420
+ int useNextLine = bNextLine;
14421
+ bNextLine = 0;
14422
+ if( (nRow+2)*nColumn >= nAlloc ){
14423
+ nAlloc *= 2;
14424
+ azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14425
+ shell_check_oom(azData);
14426
+ abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
14427
+ shell_check_oom(abRowDiv);
14428
+ }
14429
+ abRowDiv[nRow] = 1;
14430
+ nRow++;
14431
+ for(i=0; i<nColumn; i++){
14432
+ int w = p->colWidth[i];
14433
+ if( w==0 ) w = p->iWrap;
14434
+ if( useNextLine ){
14435
+ uz = azNextLine[i];
14436
+ }else if( p->bQuote ){
14437
+ sqlite3_free(azQuoted[i]);
14438
+ azQuoted[i] = quoted_column(pStmt,i);
14439
+ uz = (const unsigned char*)azQuoted[i];
14440
+ }else{
14441
+ uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
14442
+ }
14443
+ azData[nRow*nColumn + i] = translateForDisplayAndDup(uz, &azNextLine[i], w);
14444
+ if( azNextLine[i] ){
14445
+ bNextLine = 1;
14446
+ abRowDiv[nRow-1] = 0;
14447
+ bMultiLineRowExists = 1;
14448
+ }
14449
+ }
14450
+ }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
1430814451
nTotal = nColumn*(nRow+1);
1430914452
for(i=0; i<nTotal; i++){
1431014453
z = azData[i];
1431114454
if( z==0 ) z = p->nullValue;
1431214455
n = strlenChar(z);
@@ -14385,10 +14528,17 @@
1438514528
w = p->actualWidth[j];
1438614529
if( p->colWidth[j]<0 ) w = -w;
1438714530
utf8_width_print(p->out, w, z);
1438814531
if( j==nColumn-1 ){
1438914532
utf8_printf(p->out, "%s", rowSep);
14533
+ if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
14534
+ if( p->cMode==MODE_Table ){
14535
+ print_row_separator(p, nColumn, "+");
14536
+ }else if( p->cMode==MODE_Box ){
14537
+ print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14538
+ }
14539
+ }
1439014540
j = -1;
1439114541
if( seenInterrupt ) goto columnar_end;
1439214542
}else{
1439314543
utf8_printf(p->out, "%s", colSep);
1439414544
}
@@ -14403,10 +14553,16 @@
1440314553
utf8_printf(p->out, "Interrupt\n");
1440414554
}
1440514555
nData = (nRow+1)*nColumn;
1440614556
for(i=0; i<nData; i++) free(azData[i]);
1440714557
sqlite3_free(azData);
14558
+ sqlite3_free(azNextLine);
14559
+ sqlite3_free(abRowDiv);
14560
+ if( azQuoted ){
14561
+ for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
14562
+ sqlite3_free(azQuoted);
14563
+ }
1440814564
}
1440914565
1441014566
/*
1441114567
** Run a prepared statement
1441214568
*/
@@ -15180,26 +15336,31 @@
1518015336
" fkey-indexes Find missing foreign key indexes",
1518115337
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1518215338
".load FILE ?ENTRY? Load an extension library",
1518315339
#endif
1518415340
".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15185
- ".mode MODE ?TABLE? Set output mode",
15341
+ ".mode MODE ?TABLE? ?OPTIONS? Set output mode",
1518615342
" MODE is one of:",
15187
- " ascii Columns/rows delimited by 0x1F and 0x1E",
15188
- " box Tables using unicode box-drawing characters",
15189
- " csv Comma-separated values",
15190
- " column Output in columns. (See .width)",
15191
- " html HTML <table> code",
15192
- " insert SQL insert statements for TABLE",
15193
- " json Results in a JSON array",
15194
- " line One value per line",
15195
- " list Values delimited by \"|\"",
15196
- " markdown Markdown table format",
15197
- " quote Escape answers as for SQL",
15198
- " table ASCII-art table",
15199
- " tabs Tab-separated values",
15200
- " tcl TCL list elements",
15343
+ " ascii Columns/rows delimited by 0x1F and 0x1E",
15344
+ " box Tables using unicode box-drawing characters",
15345
+ " csv Comma-separated values",
15346
+ " column Output in columns. (See .width)",
15347
+ " html HTML <table> code",
15348
+ " insert SQL insert statements for TABLE",
15349
+ " json Results in a JSON array",
15350
+ " line One value per line",
15351
+ " list Values delimited by \"|\"",
15352
+ " markdown Markdown table format",
15353
+ " qbox Shorthand for \"box --width 60 --quote\"",
15354
+ " quote Escape answers as for SQL",
15355
+ " table ASCII-art table",
15356
+ " tabs Tab-separated values",
15357
+ " tcl TCL list elements",
15358
+ " OPTIONS: (value for columnar modes only):",
15359
+ " --wrap N Wrap output lines longer than N character",
15360
+ " --quote Quote output text as SQL literals",
15361
+ " --noquote Do not quote output text",
1520115362
".nonce STRING Disable safe mode for one command if the nonce matches",
1520215363
".nullvalue STRING Use STRING in place of NULL values",
1520315364
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
1520415365
" If FILE begins with '|' then open as a pipe",
1520515366
" --bom Put a UTF8 byte-order mark at the beginning",
@@ -19976,68 +20137,122 @@
1997620137
p->pLog = output_file_open(zFile, 0);
1997720138
}
1997820139
}else
1997920140
1998020141
if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19981
- const char *zMode = nArg>=2 ? azArg[1] : "";
19982
- int n2 = strlen30(zMode);
19983
- int c2 = zMode[0];
19984
- if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
20142
+ const char *zMode = 0;
20143
+ const char *zTabname = 0;
20144
+ int i, n2;
20145
+ int bQuoteChng = 0;
20146
+ int bWrapChng = 0;
20147
+ for(i=1; i<nArg; i++){
20148
+ const char *z = azArg[i];
20149
+ if( optionMatch(z,"wrap") && i+1<nArg ){
20150
+ p->iWrap = integerValue(azArg[++i]);
20151
+ bWrapChng = 1;
20152
+ }else if( optionMatch(z,"quote") ){
20153
+ p->bQuote = 1;
20154
+ bQuoteChng = 1;
20155
+ }else if( optionMatch(z,"noquote") ){
20156
+ p->bQuote = 0;
20157
+ bQuoteChng = 1;
20158
+ }else if( zMode==0 ){
20159
+ zMode = z;
20160
+ }else if( zTabname==0 ){
20161
+ zTabname = z;
20162
+ }else if( z[0]=='-' ){
20163
+ utf8_printf(stderr, "unknown option: %s\n", z);
20164
+ utf8_printf(stderr, "options:\n"
20165
+ " --noquote\n"
20166
+ " --quote\n"
20167
+ " --wrap N\n");
20168
+ rc = 1;
20169
+ goto meta_command_exit;
20170
+ }else{
20171
+ utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20172
+ rc = 1;
20173
+ goto meta_command_exit;
20174
+ }
20175
+ }
20176
+ if( zMode==0 ){
20177
+ if( p->mode==MODE_Column
20178
+ || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
20179
+ ){
20180
+ raw_printf(p->out, "current output mode: %s --wrap %d --%squote\n",
20181
+ modeDescr[p->mode], p->iWrap, p->bQuote ? "" : "no");
20182
+ }else{
20183
+ raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20184
+ }
20185
+ bWrapChng = bQuoteChng = 1;
20186
+ zMode = modeDescr[p->mode];
20187
+ }
20188
+ n2 = strlen30(zMode);
20189
+ if( strncmp(zMode,"lines",n2)==0 ){
1998520190
p->mode = MODE_Line;
1998620191
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19987
- }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
20192
+ }else if( strncmp(zMode,"columns",n2)==0 ){
1998820193
p->mode = MODE_Column;
1998920194
if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
1999020195
p->showHeader = 1;
1999120196
}
1999220197
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19993
- }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
20198
+ if( !bWrapChng ) p->iWrap = 0;
20199
+ if( !bQuoteChng ) p->bQuote = 0;
20200
+ }else if( strncmp(zMode,"list",n2)==0 ){
1999420201
p->mode = MODE_List;
1999520202
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
1999620203
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19997
- }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
20204
+ }else if( strncmp(zMode,"html",n2)==0 ){
1999820205
p->mode = MODE_Html;
19999
- }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
20206
+ }else if( strncmp(zMode,"tcl",n2)==0 ){
2000020207
p->mode = MODE_Tcl;
2000120208
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
2000220209
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20003
- }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
20210
+ }else if( strncmp(zMode,"csv",n2)==0 ){
2000420211
p->mode = MODE_Csv;
2000520212
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
2000620213
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20007
- }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
20214
+ }else if( strncmp(zMode,"tabs",n2)==0 ){
2000820215
p->mode = MODE_List;
2000920216
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20010
- }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
20217
+ }else if( strncmp(zMode,"insert",n2)==0 ){
2001120218
p->mode = MODE_Insert;
20012
- set_table_name(p, nArg>=3 ? azArg[2] : "table");
20013
- }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
20219
+ set_table_name(p, zTabname ? zTabname : "table");
20220
+ }else if( strncmp(zMode,"quote",n2)==0 ){
2001420221
p->mode = MODE_Quote;
2001520222
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
2001620223
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20017
- }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
20224
+ }else if( strncmp(zMode,"ascii",n2)==0 ){
2001820225
p->mode = MODE_Ascii;
2001920226
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
2002020227
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20021
- }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
20228
+ }else if( strncmp(zMode,"markdown",n2)==0 ){
2002220229
p->mode = MODE_Markdown;
20023
- }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
20230
+ if( !bWrapChng ) p->iWrap = 0;
20231
+ if( !bQuoteChng ) p->bQuote = 0;
20232
+ }else if( strncmp(zMode,"table",n2)==0 ){
2002420233
p->mode = MODE_Table;
20025
- }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
20234
+ if( !bWrapChng ) p->iWrap = 0;
20235
+ if( !bQuoteChng ) p->bQuote = 0;
20236
+ }else if( strncmp(zMode,"box",n2)==0 ){
20237
+ p->mode = MODE_Box;
20238
+ if( !bWrapChng ) p->iWrap = 0;
20239
+ if( !bQuoteChng ) p->bQuote = 0;
20240
+ }else if( strcmp(zMode,"qbox")==0 ){
2002620241
p->mode = MODE_Box;
20027
- }else if( c2=='c' && strncmp(azArg[1],"count",n2)==0 ){
20242
+ if( !bWrapChng ) p->iWrap = 60;
20243
+ if( !bQuoteChng ) p->bQuote = 1;
20244
+ }else if( strncmp(zMode,"count",n2)==0 ){
2002820245
p->mode = MODE_Count;
20029
- }else if( c2=='o' && strncmp(azArg[1],"off",n2)==0 ){
20246
+ }else if( strncmp(zMode,"off",n2)==0 ){
2003020247
p->mode = MODE_Off;
20031
- }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
20248
+ }else if( strncmp(zMode,"json",n2)==0 ){
2003220249
p->mode = MODE_Json;
20033
- }else if( nArg==1 ){
20034
- raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
2003520250
}else{
2003620251
raw_printf(stderr, "Error: mode should be one of: "
2003720252
"ascii box column csv html insert json line list markdown "
20038
- "quote table tabs tcl\n");
20253
+ "qbox quote table tabs tcl\n");
2003920254
rc = 1;
2004020255
}
2004120256
p->cMode = p->mode;
2004220257
}else
2004320258
@@ -21181,11 +21396,18 @@
2118121396
azBool[ShellHasFlag(p, SHFLG_Echo)]);
2118221397
utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
2118321398
utf8_printf(p->out, "%12.12s: %s\n","explain",
2118421399
p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
2118521400
utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21186
- utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21401
+ if( p->mode==MODE_Column
21402
+ || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
21403
+ ){
21404
+ utf8_printf(p->out, "%12.12s: %s --wrap %d --%squote\n", "mode",
21405
+ modeDescr[p->mode], p->iWrap, p->bQuote ? "" : "no");
21406
+ }else{
21407
+ utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21408
+ }
2118721409
utf8_printf(p->out, "%12.12s: ", "nullvalue");
2118821410
output_c_string(p->out, p->nullValue);
2118921411
raw_printf(p->out, "\n");
2119021412
utf8_printf(p->out,"%12.12s: %s\n","output",
2119121413
strlen30(p->outfile) ? p->outfile : "stdout");
2119221414
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -12161,10 +12161,12 @@
12161 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
12162 u8 nEqpLevel; /* Depth of the EQP output graph */
12163 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
12164 u8 bSafeMode; /* True to prohibit unsafe operations */
12165 u8 bSafeModePersist; /* The long-term value of bSafeMode */
 
 
12166 unsigned statsOn; /* True to display memory stats before each finalize */
12167 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
12168 int inputNesting; /* Track nesting level of .read and other redirects */
12169 int outCount; /* Revert to stdout when reaching zero */
12170 int cnt; /* Number of records displayed so far */
@@ -14241,11 +14243,116 @@
14241 utf8_printf(p->out, "%s", zSep3);
14242 }
14243 fputs("\n", p->out);
14244 }
14245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14247
14248 /*
14249 ** Run a prepared statement and output the result in one of the
14250 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14251 ** or MODE_Box.
@@ -14261,39 +14368,40 @@
14261 ){
14262 sqlite3_int64 nRow = 0;
14263 int nColumn = 0;
14264 char **azData = 0;
14265 sqlite3_int64 nAlloc = 0;
 
 
14266 const char *z;
 
14267 int rc;
14268 sqlite3_int64 i, nData;
14269 int j, nTotal, w, n;
14270 const char *colSep = 0;
14271 const char *rowSep = 0;
 
 
 
14272
14273 rc = sqlite3_step(pStmt);
14274 if( rc!=SQLITE_ROW ) return;
14275 nColumn = sqlite3_column_count(pStmt);
14276 nAlloc = nColumn*4;
14277 if( nAlloc<=0 ) nAlloc = 1;
14278 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14279 shell_check_oom(azData);
14280 for(i=0; i<nColumn; i++){
14281 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14282 }
14283 do{
14284 if( (nRow+2)*nColumn >= nAlloc ){
14285 nAlloc *= 2;
14286 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14287 shell_check_oom(azData);
14288 }
14289 nRow++;
14290 for(i=0; i<nColumn; i++){
14291 z = (const char*)sqlite3_column_text(pStmt,i);
14292 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14293 }
14294 }while( sqlite3_step(pStmt)==SQLITE_ROW );
14295 if( nColumn>p->nWidth ){
14296 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14297 shell_check_oom(p->colWidth);
14298 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14299 p->nWidth = nColumn;
@@ -14303,10 +14411,45 @@
14303 for(i=0; i<nColumn; i++){
14304 w = p->colWidth[i];
14305 if( w<0 ) w = -w;
14306 p->actualWidth[i] = w;
14307 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14308 nTotal = nColumn*(nRow+1);
14309 for(i=0; i<nTotal; i++){
14310 z = azData[i];
14311 if( z==0 ) z = p->nullValue;
14312 n = strlenChar(z);
@@ -14385,10 +14528,17 @@
14385 w = p->actualWidth[j];
14386 if( p->colWidth[j]<0 ) w = -w;
14387 utf8_width_print(p->out, w, z);
14388 if( j==nColumn-1 ){
14389 utf8_printf(p->out, "%s", rowSep);
 
 
 
 
 
 
 
14390 j = -1;
14391 if( seenInterrupt ) goto columnar_end;
14392 }else{
14393 utf8_printf(p->out, "%s", colSep);
14394 }
@@ -14403,10 +14553,16 @@
14403 utf8_printf(p->out, "Interrupt\n");
14404 }
14405 nData = (nRow+1)*nColumn;
14406 for(i=0; i<nData; i++) free(azData[i]);
14407 sqlite3_free(azData);
 
 
 
 
 
 
14408 }
14409
14410 /*
14411 ** Run a prepared statement
14412 */
@@ -15180,26 +15336,31 @@
15180 " fkey-indexes Find missing foreign key indexes",
15181 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15182 ".load FILE ?ENTRY? Load an extension library",
15183 #endif
15184 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15185 ".mode MODE ?TABLE? Set output mode",
15186 " MODE is one of:",
15187 " ascii Columns/rows delimited by 0x1F and 0x1E",
15188 " box Tables using unicode box-drawing characters",
15189 " csv Comma-separated values",
15190 " column Output in columns. (See .width)",
15191 " html HTML <table> code",
15192 " insert SQL insert statements for TABLE",
15193 " json Results in a JSON array",
15194 " line One value per line",
15195 " list Values delimited by \"|\"",
15196 " markdown Markdown table format",
15197 " quote Escape answers as for SQL",
15198 " table ASCII-art table",
15199 " tabs Tab-separated values",
15200 " tcl TCL list elements",
 
 
 
 
 
15201 ".nonce STRING Disable safe mode for one command if the nonce matches",
15202 ".nullvalue STRING Use STRING in place of NULL values",
15203 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15204 " If FILE begins with '|' then open as a pipe",
15205 " --bom Put a UTF8 byte-order mark at the beginning",
@@ -19976,68 +20137,122 @@
19976 p->pLog = output_file_open(zFile, 0);
19977 }
19978 }else
19979
19980 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19981 const char *zMode = nArg>=2 ? azArg[1] : "";
19982 int n2 = strlen30(zMode);
19983 int c2 = zMode[0];
19984 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19985 p->mode = MODE_Line;
19986 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19987 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
19988 p->mode = MODE_Column;
19989 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
19990 p->showHeader = 1;
19991 }
19992 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19993 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
 
 
19994 p->mode = MODE_List;
19995 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
19996 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19997 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
19998 p->mode = MODE_Html;
19999 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
20000 p->mode = MODE_Tcl;
20001 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
20002 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20003 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
20004 p->mode = MODE_Csv;
20005 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20006 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20007 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
20008 p->mode = MODE_List;
20009 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20010 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
20011 p->mode = MODE_Insert;
20012 set_table_name(p, nArg>=3 ? azArg[2] : "table");
20013 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
20014 p->mode = MODE_Quote;
20015 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20016 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20017 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
20018 p->mode = MODE_Ascii;
20019 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
20020 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20021 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
20022 p->mode = MODE_Markdown;
20023 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
 
 
20024 p->mode = MODE_Table;
20025 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
 
 
 
 
 
 
20026 p->mode = MODE_Box;
20027 }else if( c2=='c' && strncmp(azArg[1],"count",n2)==0 ){
 
 
20028 p->mode = MODE_Count;
20029 }else if( c2=='o' && strncmp(azArg[1],"off",n2)==0 ){
20030 p->mode = MODE_Off;
20031 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
20032 p->mode = MODE_Json;
20033 }else if( nArg==1 ){
20034 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20035 }else{
20036 raw_printf(stderr, "Error: mode should be one of: "
20037 "ascii box column csv html insert json line list markdown "
20038 "quote table tabs tcl\n");
20039 rc = 1;
20040 }
20041 p->cMode = p->mode;
20042 }else
20043
@@ -21181,11 +21396,18 @@
21181 azBool[ShellHasFlag(p, SHFLG_Echo)]);
21182 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
21183 utf8_printf(p->out, "%12.12s: %s\n","explain",
21184 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
21185 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21186 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
 
 
 
 
 
 
 
21187 utf8_printf(p->out, "%12.12s: ", "nullvalue");
21188 output_c_string(p->out, p->nullValue);
21189 raw_printf(p->out, "\n");
21190 utf8_printf(p->out,"%12.12s: %s\n","output",
21191 strlen30(p->outfile) ? p->outfile : "stdout");
21192
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -12161,10 +12161,12 @@
12161 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
12162 u8 nEqpLevel; /* Depth of the EQP output graph */
12163 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
12164 u8 bSafeMode; /* True to prohibit unsafe operations */
12165 u8 bSafeModePersist; /* The long-term value of bSafeMode */
12166 u8 bQuote; /* Quote results for .mode box and table */
12167 int iWrap; /* Wrap lines this long or longer in some output modes */
12168 unsigned statsOn; /* True to display memory stats before each finalize */
12169 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
12170 int inputNesting; /* Track nesting level of .read and other redirects */
12171 int outCount; /* Revert to stdout when reaching zero */
12172 int cnt; /* Number of records displayed so far */
@@ -14241,11 +14243,116 @@
14243 utf8_printf(p->out, "%s", zSep3);
14244 }
14245 fputs("\n", p->out);
14246 }
14247
14248 /*
14249 ** z[] is a line of text that is to be displayed the .mode box or table or
14250 ** similar tabular formats. z[] might contain control characters such
14251 ** as \n, \t, \f, or \r.
14252 **
14253 ** Compute characters to display on the first line of z[]. Stop at the
14254 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
14255 ** from malloc()) of that first line. Write anything to display
14256 ** on the next line into *pzTail. If this is the last line, write a NULL
14257 ** into *pzTail.
14258 */
14259 static char *translateForDisplayAndDup(
14260 const unsigned char *z,
14261 const unsigned char **pzTail,
14262 int mxWidth
14263 ){
14264 int i, j, n;
14265 unsigned char *zOut;
14266 if( z==0 ){
14267 *pzTail = 0;
14268 return 0;
14269 }
14270 if( mxWidth<0 ) mxWidth = -mxWidth;
14271 if( mxWidth==0 ) mxWidth = 1000000;
14272 i = j= n = 0;
14273 while( n<mxWidth ){
14274 if( z[i]>=' ' ){
14275 n++;
14276 do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
14277 continue;
14278 }
14279 if( z[i]=='\t' ){
14280 do{
14281 n++;
14282 j++;
14283 }while( (n&7)!=0 && n<mxWidth );
14284 n += 8;
14285 n &= ~7;
14286 i++;
14287 continue;
14288 }
14289 break;
14290 }
14291 if( n>=mxWidth && z[i]>=' ' ){
14292 *pzTail = &z[i];
14293 }else if( z[i]=='\r' && z[i+1]=='\n' ){
14294 *pzTail = z[i+2] ? &z[i+2] : 0;
14295 }else if( z[i]==0 || z[i+1]==0 ){
14296 *pzTail = 0;
14297 }else{
14298 *pzTail = &z[i+1];
14299 }
14300 zOut = malloc( j+1 );
14301 shell_check_oom(zOut);
14302 i = j = n = 0;
14303 while( n<mxWidth ){
14304 if( z[i]>=' ' ){
14305 n++;
14306 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
14307 continue;
14308 }
14309 if( z[i]=='\t' ){
14310 do{
14311 n++;
14312 zOut[j++] = ' ';
14313 }while( (n&7)!=0 && n<mxWidth );
14314 i++;
14315 continue;
14316 }
14317 break;
14318 }
14319 zOut[j] = 0;
14320 return (char*)zOut;
14321 }
14322
14323 /* Extract the value of the i-th current column for pStmt as an SQL literal
14324 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
14325 ** the caller.
14326 */
14327 static char *quoted_column(sqlite3_stmt *pStmt, int i){
14328 switch( sqlite3_column_type(pStmt, i) ){
14329 case SQLITE_NULL: {
14330 return sqlite3_mprintf("NULL");
14331 }
14332 case SQLITE_INTEGER:
14333 case SQLITE_FLOAT: {
14334 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
14335 }
14336 case SQLITE_TEXT: {
14337 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
14338 }
14339 case SQLITE_BLOB: {
14340 int j;
14341 sqlite3_str *pStr = sqlite3_str_new(0);
14342 const unsigned char *a = sqlite3_column_blob(pStmt,i);
14343 int n = sqlite3_column_bytes(pStmt,i);
14344 sqlite3_str_append(pStr, "x'", 2);
14345 for(j=0; j<n; j++){
14346 sqlite3_str_appendf(pStr, "%02x", a[j]);
14347 }
14348 sqlite3_str_append(pStr, "'", 1);
14349 return sqlite3_str_finish(pStr);
14350 }
14351 }
14352 return 0; /* Not reached */
14353 }
14354
14355 /*
14356 ** Run a prepared statement and output the result in one of the
14357 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14358 ** or MODE_Box.
@@ -14261,39 +14368,40 @@
14368 ){
14369 sqlite3_int64 nRow = 0;
14370 int nColumn = 0;
14371 char **azData = 0;
14372 sqlite3_int64 nAlloc = 0;
14373 char *abRowDiv = 0;
14374 const unsigned char *uz;
14375 const char *z;
14376 char **azQuoted = 0;
14377 int rc;
14378 sqlite3_int64 i, nData;
14379 int j, nTotal, w, n;
14380 const char *colSep = 0;
14381 const char *rowSep = 0;
14382 const unsigned char **azNextLine = 0;
14383 int bNextLine = 0;
14384 int bMultiLineRowExists = 0;
14385
14386 rc = sqlite3_step(pStmt);
14387 if( rc!=SQLITE_ROW ) return;
14388 nColumn = sqlite3_column_count(pStmt);
14389 nAlloc = nColumn*4;
14390 if( nAlloc<=0 ) nAlloc = 1;
14391 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14392 shell_check_oom(azData);
14393 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
14394 shell_check_oom(azNextLine);
14395 memset(azNextLine, 0, nColumn*sizeof(char*) );
14396 if( p->bQuote ){
14397 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
14398 shell_check_oom(azQuoted);
14399 memset(azQuoted, 0, nColumn*sizeof(char*) );
14400 }
14401 abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
14402 shell_check_oom(abRowDiv);
 
 
 
 
 
14403 if( nColumn>p->nWidth ){
14404 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14405 shell_check_oom(p->colWidth);
14406 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14407 p->nWidth = nColumn;
@@ -14303,10 +14411,45 @@
14411 for(i=0; i<nColumn; i++){
14412 w = p->colWidth[i];
14413 if( w<0 ) w = -w;
14414 p->actualWidth[i] = w;
14415 }
14416 for(i=0; i<nColumn; i++){
14417 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14418 }
14419 do{
14420 int useNextLine = bNextLine;
14421 bNextLine = 0;
14422 if( (nRow+2)*nColumn >= nAlloc ){
14423 nAlloc *= 2;
14424 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14425 shell_check_oom(azData);
14426 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
14427 shell_check_oom(abRowDiv);
14428 }
14429 abRowDiv[nRow] = 1;
14430 nRow++;
14431 for(i=0; i<nColumn; i++){
14432 int w = p->colWidth[i];
14433 if( w==0 ) w = p->iWrap;
14434 if( useNextLine ){
14435 uz = azNextLine[i];
14436 }else if( p->bQuote ){
14437 sqlite3_free(azQuoted[i]);
14438 azQuoted[i] = quoted_column(pStmt,i);
14439 uz = (const unsigned char*)azQuoted[i];
14440 }else{
14441 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
14442 }
14443 azData[nRow*nColumn + i] = translateForDisplayAndDup(uz, &azNextLine[i], w);
14444 if( azNextLine[i] ){
14445 bNextLine = 1;
14446 abRowDiv[nRow-1] = 0;
14447 bMultiLineRowExists = 1;
14448 }
14449 }
14450 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
14451 nTotal = nColumn*(nRow+1);
14452 for(i=0; i<nTotal; i++){
14453 z = azData[i];
14454 if( z==0 ) z = p->nullValue;
14455 n = strlenChar(z);
@@ -14385,10 +14528,17 @@
14528 w = p->actualWidth[j];
14529 if( p->colWidth[j]<0 ) w = -w;
14530 utf8_width_print(p->out, w, z);
14531 if( j==nColumn-1 ){
14532 utf8_printf(p->out, "%s", rowSep);
14533 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
14534 if( p->cMode==MODE_Table ){
14535 print_row_separator(p, nColumn, "+");
14536 }else if( p->cMode==MODE_Box ){
14537 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14538 }
14539 }
14540 j = -1;
14541 if( seenInterrupt ) goto columnar_end;
14542 }else{
14543 utf8_printf(p->out, "%s", colSep);
14544 }
@@ -14403,10 +14553,16 @@
14553 utf8_printf(p->out, "Interrupt\n");
14554 }
14555 nData = (nRow+1)*nColumn;
14556 for(i=0; i<nData; i++) free(azData[i]);
14557 sqlite3_free(azData);
14558 sqlite3_free(azNextLine);
14559 sqlite3_free(abRowDiv);
14560 if( azQuoted ){
14561 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
14562 sqlite3_free(azQuoted);
14563 }
14564 }
14565
14566 /*
14567 ** Run a prepared statement
14568 */
@@ -15180,26 +15336,31 @@
15336 " fkey-indexes Find missing foreign key indexes",
15337 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15338 ".load FILE ?ENTRY? Load an extension library",
15339 #endif
15340 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15341 ".mode MODE ?TABLE? ?OPTIONS? Set output mode",
15342 " MODE is one of:",
15343 " ascii Columns/rows delimited by 0x1F and 0x1E",
15344 " box Tables using unicode box-drawing characters",
15345 " csv Comma-separated values",
15346 " column Output in columns. (See .width)",
15347 " html HTML <table> code",
15348 " insert SQL insert statements for TABLE",
15349 " json Results in a JSON array",
15350 " line One value per line",
15351 " list Values delimited by \"|\"",
15352 " markdown Markdown table format",
15353 " qbox Shorthand for \"box --width 60 --quote\"",
15354 " quote Escape answers as for SQL",
15355 " table ASCII-art table",
15356 " tabs Tab-separated values",
15357 " tcl TCL list elements",
15358 " OPTIONS: (value for columnar modes only):",
15359 " --wrap N Wrap output lines longer than N character",
15360 " --quote Quote output text as SQL literals",
15361 " --noquote Do not quote output text",
15362 ".nonce STRING Disable safe mode for one command if the nonce matches",
15363 ".nullvalue STRING Use STRING in place of NULL values",
15364 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15365 " If FILE begins with '|' then open as a pipe",
15366 " --bom Put a UTF8 byte-order mark at the beginning",
@@ -19976,68 +20137,122 @@
20137 p->pLog = output_file_open(zFile, 0);
20138 }
20139 }else
20140
20141 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20142 const char *zMode = 0;
20143 const char *zTabname = 0;
20144 int i, n2;
20145 int bQuoteChng = 0;
20146 int bWrapChng = 0;
20147 for(i=1; i<nArg; i++){
20148 const char *z = azArg[i];
20149 if( optionMatch(z,"wrap") && i+1<nArg ){
20150 p->iWrap = integerValue(azArg[++i]);
20151 bWrapChng = 1;
20152 }else if( optionMatch(z,"quote") ){
20153 p->bQuote = 1;
20154 bQuoteChng = 1;
20155 }else if( optionMatch(z,"noquote") ){
20156 p->bQuote = 0;
20157 bQuoteChng = 1;
20158 }else if( zMode==0 ){
20159 zMode = z;
20160 }else if( zTabname==0 ){
20161 zTabname = z;
20162 }else if( z[0]=='-' ){
20163 utf8_printf(stderr, "unknown option: %s\n", z);
20164 utf8_printf(stderr, "options:\n"
20165 " --noquote\n"
20166 " --quote\n"
20167 " --wrap N\n");
20168 rc = 1;
20169 goto meta_command_exit;
20170 }else{
20171 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20172 rc = 1;
20173 goto meta_command_exit;
20174 }
20175 }
20176 if( zMode==0 ){
20177 if( p->mode==MODE_Column
20178 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
20179 ){
20180 raw_printf(p->out, "current output mode: %s --wrap %d --%squote\n",
20181 modeDescr[p->mode], p->iWrap, p->bQuote ? "" : "no");
20182 }else{
20183 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
20184 }
20185 bWrapChng = bQuoteChng = 1;
20186 zMode = modeDescr[p->mode];
20187 }
20188 n2 = strlen30(zMode);
20189 if( strncmp(zMode,"lines",n2)==0 ){
20190 p->mode = MODE_Line;
20191 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20192 }else if( strncmp(zMode,"columns",n2)==0 ){
20193 p->mode = MODE_Column;
20194 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
20195 p->showHeader = 1;
20196 }
20197 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20198 if( !bWrapChng ) p->iWrap = 0;
20199 if( !bQuoteChng ) p->bQuote = 0;
20200 }else if( strncmp(zMode,"list",n2)==0 ){
20201 p->mode = MODE_List;
20202 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
20203 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20204 }else if( strncmp(zMode,"html",n2)==0 ){
20205 p->mode = MODE_Html;
20206 }else if( strncmp(zMode,"tcl",n2)==0 ){
20207 p->mode = MODE_Tcl;
20208 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
20209 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20210 }else if( strncmp(zMode,"csv",n2)==0 ){
20211 p->mode = MODE_Csv;
20212 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20213 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20214 }else if( strncmp(zMode,"tabs",n2)==0 ){
20215 p->mode = MODE_List;
20216 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
20217 }else if( strncmp(zMode,"insert",n2)==0 ){
20218 p->mode = MODE_Insert;
20219 set_table_name(p, zTabname ? zTabname : "table");
20220 }else if( strncmp(zMode,"quote",n2)==0 ){
20221 p->mode = MODE_Quote;
20222 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20223 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
20224 }else if( strncmp(zMode,"ascii",n2)==0 ){
20225 p->mode = MODE_Ascii;
20226 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
20227 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
20228 }else if( strncmp(zMode,"markdown",n2)==0 ){
20229 p->mode = MODE_Markdown;
20230 if( !bWrapChng ) p->iWrap = 0;
20231 if( !bQuoteChng ) p->bQuote = 0;
20232 }else if( strncmp(zMode,"table",n2)==0 ){
20233 p->mode = MODE_Table;
20234 if( !bWrapChng ) p->iWrap = 0;
20235 if( !bQuoteChng ) p->bQuote = 0;
20236 }else if( strncmp(zMode,"box",n2)==0 ){
20237 p->mode = MODE_Box;
20238 if( !bWrapChng ) p->iWrap = 0;
20239 if( !bQuoteChng ) p->bQuote = 0;
20240 }else if( strcmp(zMode,"qbox")==0 ){
20241 p->mode = MODE_Box;
20242 if( !bWrapChng ) p->iWrap = 60;
20243 if( !bQuoteChng ) p->bQuote = 1;
20244 }else if( strncmp(zMode,"count",n2)==0 ){
20245 p->mode = MODE_Count;
20246 }else if( strncmp(zMode,"off",n2)==0 ){
20247 p->mode = MODE_Off;
20248 }else if( strncmp(zMode,"json",n2)==0 ){
20249 p->mode = MODE_Json;
 
 
20250 }else{
20251 raw_printf(stderr, "Error: mode should be one of: "
20252 "ascii box column csv html insert json line list markdown "
20253 "qbox quote table tabs tcl\n");
20254 rc = 1;
20255 }
20256 p->cMode = p->mode;
20257 }else
20258
@@ -21181,11 +21396,18 @@
21396 azBool[ShellHasFlag(p, SHFLG_Echo)]);
21397 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
21398 utf8_printf(p->out, "%12.12s: %s\n","explain",
21399 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
21400 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21401 if( p->mode==MODE_Column
21402 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
21403 ){
21404 utf8_printf(p->out, "%12.12s: %s --wrap %d --%squote\n", "mode",
21405 modeDescr[p->mode], p->iWrap, p->bQuote ? "" : "no");
21406 }else{
21407 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21408 }
21409 utf8_printf(p->out, "%12.12s: ", "nullvalue");
21410 output_c_string(p->out, p->nullValue);
21411 raw_printf(p->out, "\n");
21412 utf8_printf(p->out,"%12.12s: %s\n","output",
21413 strlen30(p->outfile) ? p->outfile : "stdout");
21414
+284 -64
--- 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.38.0"
456456
#define SQLITE_VERSION_NUMBER 3038000
457
-#define SQLITE_SOURCE_ID "2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17"
457
+#define SQLITE_SOURCE_ID "2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -4656,10 +4656,12 @@
46564656
** still make the distinction between protected and unprotected
46574657
** sqlite3_value objects even when not strictly required.
46584658
**
46594659
** ^The sqlite3_value objects that are passed as parameters into the
46604660
** implementation of [application-defined SQL functions] are protected.
4661
+** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4662
+** are protected.
46614663
** ^The sqlite3_value object returned by
46624664
** [sqlite3_column_value()] is unprotected.
46634665
** Unprotected sqlite3_value objects may only be used as arguments
46644666
** to [sqlite3_result_value()], [sqlite3_bind_value()], and
46654667
** [sqlite3_value_dup()].
@@ -7435,28 +7437,60 @@
74357437
/*
74367438
** CAPI3REF: Virtual Table Constraint Operator Codes
74377439
**
74387440
** These macros define the allowed values for the
74397441
** [sqlite3_index_info].aConstraint[].op field. Each value represents
7440
-** an operator that is part of a constraint term in the wHERE clause of
7442
+** an operator that is part of a constraint term in the WHERE clause of
74417443
** a query that uses a [virtual table].
7444
+**
7445
+** ^The left-hand operand of the operator is given by the corresponding
7446
+** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7447
+** operand is the rowid.
7448
+** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7449
+** operators have no left-hand operand, and so for those operators the
7450
+** corresponding aConstraint[].iColumn is meaningless and should not be
7451
+** used.
7452
+**
7453
+** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7454
+** value 255 are reserved to represent functions that are overloaded
7455
+** by the [xFindFunction|xFindFunction method] of the virtual table
7456
+** implementation.
7457
+**
7458
+** The right-hand operands for each constraint might be accessible using
7459
+** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7460
+** operand is only available if it appears as a single constant literal
7461
+** in the input SQL. If the right-hand operand is another column or an
7462
+** expression (even a constant expression) or a parameter, then the
7463
+** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7464
+** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7465
+** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7466
+** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7467
+** always return SQLITE_NOTFOUND.
7468
+**
7469
+** The collating sequence to be used for comparison can be found using
7470
+** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7471
+** tables, the collating sequence of constraints does not matter (for example
7472
+** because the constraints are numeric) and so the sqlite3_vtab_collation()
7473
+** interface is no commonly needed.
74427474
*/
7443
-#define SQLITE_INDEX_CONSTRAINT_EQ 2
7444
-#define SQLITE_INDEX_CONSTRAINT_GT 4
7445
-#define SQLITE_INDEX_CONSTRAINT_LE 8
7446
-#define SQLITE_INDEX_CONSTRAINT_LT 16
7447
-#define SQLITE_INDEX_CONSTRAINT_GE 32
7448
-#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7449
-#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7450
-#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7451
-#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7452
-#define SQLITE_INDEX_CONSTRAINT_NE 68
7453
-#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7454
-#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7455
-#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7456
-#define SQLITE_INDEX_CONSTRAINT_IS 72
7457
-#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7475
+#define SQLITE_INDEX_CONSTRAINT_EQ 2
7476
+#define SQLITE_INDEX_CONSTRAINT_GT 4
7477
+#define SQLITE_INDEX_CONSTRAINT_LE 8
7478
+#define SQLITE_INDEX_CONSTRAINT_LT 16
7479
+#define SQLITE_INDEX_CONSTRAINT_GE 32
7480
+#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7481
+#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7482
+#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7483
+#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7484
+#define SQLITE_INDEX_CONSTRAINT_NE 68
7485
+#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7486
+#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7487
+#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7488
+#define SQLITE_INDEX_CONSTRAINT_IS 72
7489
+#define SQLITE_INDEX_CONSTRAINT_LIMIT 73
7490
+#define SQLITE_INDEX_CONSTRAINT_OFFSET 74
7491
+#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
74587492
74597493
/*
74607494
** CAPI3REF: Register A Virtual Table Implementation
74617495
** METHOD: sqlite3
74627496
**
@@ -9812,28 +9846,29 @@
98129846
98139847
/*
98149848
** CAPI3REF: Determine if a virtual table query is DISTINCT
98159849
** METHOD: sqlite3_index_info
98169850
**
9817
-** This API may only be used from within an xBestIndex() callback. The
9818
-** results of calling it from outside of an xBestIndex() callback are
9819
-** undefined and probably harmful.
9851
+** This API may only be used from within an [xBestIndex|xBestIndex method]
9852
+** of a [virtual table] implementation. The result of calling this
9853
+** interface from outside of xBestIndex() is undefined and probably harmful.
98209854
**
9821
-** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or
9822
-** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table
9823
-** additional information about how the query planner wants the output to be
9824
-** ordered. As long as the virtual table can meet the ordering requirements
9825
-** of the query planner, it may set the "orderByConsumed" flag.
9855
+** ^The sqlite3_vtab_distinct() interface returns an integer that is
9856
+** either 0, 1, or 2. The integer returned by sqlite3_vtab_distinct()
9857
+** gives the virtual table additional information about how the query
9858
+** planner wants the output to be ordered. As long as the virtual table
9859
+** can meet the ordering requirements of the query planner, it may set
9860
+** the "orderByConsumed" flag.
98269861
**
98279862
** <ol><li value="0"><p>
98289863
** ^If the sqlite3_vtab_distinct() interface returns 0, that means
98299864
** that the query planner needs the virtual table to return all rows in the
98309865
** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
98319866
** [sqlite3_index_info] object. This is the default expectation. If the
98329867
** virtual table outputs all rows in sorted order, then it is always safe for
98339868
** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9834
-** what the return value from sqlite3_vtab_distinct().
9869
+** the return value from sqlite3_vtab_distinct().
98359870
** <li value="1"><p>
98369871
** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
98379872
** that the query planner does not need the rows to be returned in sorted order
98389873
** as long as all rows with the same values in all columns identified by the
98399874
** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9842,11 +9877,11 @@
98429877
** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
98439878
** that the query planner does not need the rows returned in any particular
98449879
** order, as long as rows with the same values in all "aOrderBy" columns
98459880
** are adjacent.)^ ^(Furthermore, only a single row for each particular
98469881
** combination of values in the columns identified by the "aOrderBy" field
9847
-** needs to be returned.)^ ^It is ok always for two or more rows with the same
9882
+** needs to be returned.)^ ^It is always ok for two or more rows with the same
98489883
** values in all "aOrderBy" columns to be returned, as long as all such rows
98499884
** are adjacent. ^The virtual table may, if it chooses, omit extra rows
98509885
** that have the same value for all columns identified by "aOrderBy".
98519886
** ^However omitting the extra rows is optional.
98529887
** This mode is used for a DISTINCT query.
@@ -9877,31 +9912,45 @@
98779912
98789913
/*
98799914
** CAPI3REF: Constraint values in xBestIndex()
98809915
** METHOD: sqlite3_index_info
98819916
**
9882
-** This API may only be used from within an xBestIndex() callback. The
9883
-** results of calling it from outside of an xBestIndex() callback are
9884
-** undefined and probably harmful.
9917
+** This API may only be used from within the [xBestIndex|xBestIndex method]
9918
+** of a [virtual table] implementation. The result of calling this interface
9919
+** from outside of an xBestIndex method are undefined and probably harmful.
98859920
**
98869921
** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
98879922
** the [xBestIndex] method of a [virtual table] implementation, with P being
98889923
** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
98899924
** J being a 0-based index into P->aConstraint[], then this routine
9890
-** attempts to set *V to be the value on the right-hand side of
9891
-** that constraint if the right-hand side is a known constant. ^If the
9892
-** right-hand side of the constraint is not known, then *V is set to a NULL
9893
-** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9925
+** attempts to set *V to the value of the right-hand operand of
9926
+** that constraint if the right-hand operand is known. ^If the
9927
+** right-hand operand is not known, then *V is set to a NULL pointer.
9928
+** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
98949929
** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
98959930
** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
98969931
** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
98979932
** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
98989933
** something goes wrong.
98999934
**
9900
-** ^The sqlite3_value object returned in *V remains valid for the duration of
9901
-** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value
9902
-** object returned by sqlite3_vtab_rhs_value() is automatically deallocated.
9935
+** The sqlite3_vtab_rhs_value() interface is usually only successful if
9936
+** the right-hand operand of a constraint is a literal value in the original
9937
+** SQL statement. If the right-hand operand is an expression or a reference
9938
+** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
9939
+** will probably return [SQLITE_NOTFOUND].
9940
+**
9941
+** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
9942
+** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
9943
+** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
9944
+**
9945
+** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
9946
+** and remains valid for the duration of the xBestIndex method call.
9947
+** ^When xBestIndex returns, the sqlite3_value object returned by
9948
+** sqlite3_vtab_rhs_value() is automatically deallocated.
9949
+**
9950
+** The "_rhs_" in the name of this routine is an appreviation for
9951
+** "Right-Hand Side".
99039952
*/
99049953
SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
99059954
99069955
/*
99079956
** CAPI3REF: Conflict resolution modes
@@ -19662,11 +19711,12 @@
1966219711
#endif
1966319712
SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*);
1966419713
SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
1966519714
SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
1966619715
Upsert*);
19667
-SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
19716
+SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
19717
+ ExprList*,Select*,u16,int);
1966819718
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
1966919719
SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
1967019720
SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
1967119721
SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
1967219722
SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
@@ -23545,11 +23595,11 @@
2354523595
** is not the first modifier, or if the prior argument is not a numeric
2354623596
** value in the allowed range of julian day numbers understood by
2354723597
** SQLite (0..5373484.5) then the result will be NULL.
2354823598
*/
2354923599
if( sqlite3_stricmp(z, "julianday")==0 ){
23550
- if( idx>1 ) return 1;
23600
+ if( idx>1 ) return 1; /* IMP: R-31176-64601 */
2355123601
if( p->validJD && p->rawS ){
2355223602
rc = 0;
2355323603
p->rawS = 0;
2355423604
}
2355523605
}
@@ -23576,10 +23626,11 @@
2357623626
**
2357723627
** Treat the current value of p->s as the number of
2357823628
** seconds since 1970. Convert to a real julian day number.
2357923629
*/
2358023630
if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
23631
+ if( idx>1 ) return 1; /* IMP: R-49255-55373 */
2358123632
r = p->s*1000.0 + 210866760000000.0;
2358223633
if( r>=0.0 && r<464269060800000.0 ){
2358323634
clearYMD_HMS_TZ(p);
2358423635
p->iJD = (sqlite3_int64)(r + 0.5);
2358523636
p->validJD = 1;
@@ -76814,11 +76865,11 @@
7681476865
int i = sqlite3FindDbName(pDb, zDb);
7681576866
7681676867
if( i==1 ){
7681776868
Parse sParse;
7681876869
int rc = 0;
76819
- sqlite3ParseObjectInit(&sParse,pErrorDb);
76870
+ sqlite3ParseObjectInit(&sParse,pDb);
7682076871
if( sqlite3OpenTempDatabase(&sParse) ){
7682176872
sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
7682276873
rc = SQLITE_ERROR;
7682376874
}
7682476875
sqlite3DbFree(pErrorDb, sParse.zErrMsg);
@@ -79032,15 +79083,11 @@
7903279083
const char *zNeg = "";
7903379084
int rc = SQLITE_OK;
7903479085
7903579086
assert( pExpr!=0 );
7903679087
while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
79037
-#if defined(SQLITE_ENABLE_STAT4)
7903879088
if( op==TK_REGISTER ) op = pExpr->op2;
79039
-#else
79040
- if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
79041
-#endif
7904279089
7904379090
/* Compressed expressions only appear when parsing the DEFAULT clause
7904479091
** on a table column definition, and hence only when pCtx==0. This
7904579092
** check ensures that an EP_TokenOnly expression is never passed down
7904679093
** into valueFromFunction(). */
@@ -120069,11 +120116,11 @@
120069120116
**
120070120117
** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
120071120118
** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
120072120119
** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
120073120120
*/
120074
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
120121
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
120075120122
if( pWInfo==0 ) goto delete_from_cleanup;
120076120123
eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
120077120124
assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
120078120125
assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
120079120126
if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
@@ -123612,11 +123659,11 @@
123612123659
123613123660
/* Create VDBE to loop through the entries in pSrc that match the WHERE
123614123661
** clause. For each row found, increment either the deferred or immediate
123615123662
** foreign key constraint counter. */
123616123663
if( pParse->nErr==0 ){
123617
- pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
123664
+ pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
123618123665
sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
123619123666
if( pWInfo ){
123620123667
sqlite3WhereEnd(pWInfo);
123621123668
}
123622123669
}
@@ -133324,10 +133371,11 @@
133324133371
memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
133325133372
assert( db->pParse!=pParse );
133326133373
pParse->pOuterParse = db->pParse;
133327133374
db->pParse = pParse;
133328133375
pParse->db = db;
133376
+ if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
133329133377
}
133330133378
133331133379
/*
133332133380
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
133333133381
*/
@@ -133350,11 +133398,11 @@
133350133398
sParse.pOuterParse = db->pParse;
133351133399
db->pParse = &sParse;
133352133400
sParse.db = db;
133353133401
sParse.pReprepare = pReprepare;
133354133402
assert( ppStmt && *ppStmt==0 );
133355
- /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
133403
+ if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
133356133404
assert( sqlite3_mutex_held(db->mutex) );
133357133405
133358133406
/* For a long-term use prepared statement avoid the use of
133359133407
** lookaside memory.
133360133408
*/
@@ -140618,11 +140666,11 @@
140618140666
140619140667
140620140668
/* Begin the database scan. */
140621140669
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140622140670
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
140623
- p->pEList, wctrlFlags, p->nSelectRow);
140671
+ p->pEList, p, wctrlFlags, p->nSelectRow);
140624140672
if( pWInfo==0 ) goto select_end;
140625140673
if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
140626140674
p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
140627140675
}
140628140676
if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
@@ -140882,11 +140930,11 @@
140882140930
** in the right order to begin with.
140883140931
*/
140884140932
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
140885140933
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140886140934
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
140887
- WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
140935
+ 0, (WHERE_GROUPBY|(orderByGrp ? WHERE_SORTBYGROUP : 0)|distFlag), 0
140888140936
);
140889140937
if( pWInfo==0 ){
140890140938
sqlite3ExprListDelete(db, pDistinct);
140891140939
goto select_end;
140892140940
}
@@ -141180,11 +141228,11 @@
141180141228
assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
141181141229
assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
141182141230
141183141231
SELECTTRACE(1,pParse,p,("WhereBegin\n"));
141184141232
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
141185
- pDistinct, minMaxFlag|distFlag, 0);
141233
+ pDistinct, 0, minMaxFlag|distFlag, 0);
141186141234
if( pWInfo==0 ){
141187141235
goto select_end;
141188141236
}
141189141237
SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
141190141238
eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -143623,11 +143671,11 @@
143623143671
** or index, causing a single-pass approach to malfunction. */
143624143672
flags = WHERE_ONEPASS_DESIRED;
143625143673
if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
143626143674
flags |= WHERE_ONEPASS_MULTIROW;
143627143675
}
143628
- pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur);
143676
+ pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
143629143677
if( pWInfo==0 ) goto update_cleanup;
143630143678
143631143679
/* A one-pass strategy that might update more than one row may not
143632143680
** be used if any column of the index used for the scan is being
143633143681
** updated. Otherwise, if there is an index on "b", statements like
@@ -144145,11 +144193,13 @@
144145144193
}else{
144146144194
regRec = ++pParse->nMem;
144147144195
regRowid = ++pParse->nMem;
144148144196
144149144197
/* Start scanning the virtual table */
144150
- pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0);
144198
+ pWInfo = sqlite3WhereBegin(
144199
+ pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0
144200
+ );
144151144201
if( pWInfo==0 ) return;
144152144202
144153144203
/* Populate the argument registers. */
144154144204
for(i=0; i<pTab->nCol; i++){
144155144205
assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
@@ -146456,11 +146506,12 @@
146456146506
u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
146457146507
Index *pIndex; /* Index used, or NULL */
146458146508
} btree;
146459146509
struct { /* Information for virtual tables */
146460146510
int idxNum; /* Index number */
146461
- u8 needFree; /* True if sqlite3_free(idxStr) is needed */
146511
+ u8 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
146512
+ u8 bOmitOffset : 1; /* True to let virtual table handle offset */
146462146513
i8 isOrdered; /* True if satisfies ORDER BY */
146463146514
u16 omitMask; /* Terms that may be omitted */
146464146515
char *idxStr; /* Index identifier string */
146465146516
} vtab;
146466146517
} u;
@@ -146786,10 +146837,13 @@
146786146837
Parse *pParse; /* Parsing and code generating context */
146787146838
SrcList *pTabList; /* List of tables in the join */
146788146839
ExprList *pOrderBy; /* The ORDER BY clause or NULL */
146789146840
ExprList *pResultSet; /* Result set of the query */
146790146841
Expr *pWhere; /* The complete WHERE clause */
146842
+#ifndef SQLITE_OMIT_VIRTUALTABLE
146843
+ Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
146844
+#endif
146791146845
int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
146792146846
int iContinue; /* Jump here to continue with next record */
146793146847
int iBreak; /* Jump here to break out of the loop */
146794146848
int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
146795146849
u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -146871,10 +146925,11 @@
146871146925
146872146926
/* whereexpr.c: */
146873146927
SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
146874146928
SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
146875146929
SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
146930
+SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*);
146876146931
SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
146877146932
SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
146878146933
SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
146879146934
SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
146880146935
SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
@@ -146941,10 +146996,11 @@
146941146996
#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
146942146997
#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
146943146998
#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
146944146999
#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
146945147000
#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
147001
+#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
146946147002
146947147003
#endif /* !defined(SQLITE_WHEREINT_H) */
146948147004
146949147005
/************** End of whereInt.h ********************************************/
146950147006
/************** Continuing where we left off in wherecode.c ******************/
@@ -148464,10 +148520,19 @@
148464148520
codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
148465148521
addrNotFound = pLevel->addrNxt;
148466148522
}else{
148467148523
Expr *pRight = pTerm->pExpr->pRight;
148468148524
codeExprOrVector(pParse, pRight, iTarget, 1);
148525
+ if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
148526
+ && pLoop->u.vtab.bOmitOffset
148527
+ ){
148528
+ assert( pTerm->eOperator==WO_AUX );
148529
+ assert( pWInfo->pLimit!=0 );
148530
+ assert( pWInfo->pLimit->iOffset>0 );
148531
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
148532
+ VdbeComment((v,"Zero OFFSET counter"));
148533
+ }
148469148534
}
148470148535
}
148471148536
sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
148472148537
sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
148473148538
sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
@@ -149285,11 +149350,11 @@
149285149350
pOrExpr = pAndExpr;
149286149351
}
149287149352
/* Loop through table entries that match term pOrTerm. */
149288149353
ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
149289149354
WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
149290
- pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
149355
+ pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
149291149356
WHERE_OR_SUBCLAUSE, iCovCur);
149292149357
assert( pSubWInfo || pParse->nErr );
149293149358
if( pSubWInfo ){
149294149359
WhereLoop *pSubLoop;
149295149360
int addrExplain = sqlite3WhereExplainOneScan(
@@ -150982,11 +151047,14 @@
150982151047
** new terms for each component comparison - "a = ?" and "b = ?". The
150983151048
** new terms completely replace the original vector comparison, which is
150984151049
** no longer used.
150985151050
**
150986151051
** This is only required if at least one side of the comparison operation
150987
- ** is not a sub-select. */
151052
+ ** is not a sub-select.
151053
+ **
151054
+ ** tag-20220128a
151055
+ */
150988151056
if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
150989151057
&& (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
150990151058
&& sqlite3ExprVectorSize(pExpr->pRight)==nLeft
150991151059
&& ( (pExpr->pLeft->flags & EP_xIsSelect)==0
150992151060
|| (pExpr->pRight->flags & EP_xIsSelect)==0)
@@ -151124,10 +151192,117 @@
151124151192
}else{
151125151193
sqlite3WhereSplit(pWC, pE2->pLeft, op);
151126151194
sqlite3WhereSplit(pWC, pE2->pRight, op);
151127151195
}
151128151196
}
151197
+
151198
+/*
151199
+** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
151200
+** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
151201
+** where-clause passed as the first argument. The value for the term
151202
+** is found in register iReg.
151203
+**
151204
+** In the common case where the value is a simple integer
151205
+** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
151206
+** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
151207
+** If not, then it codes as a TK_REGISTER expression.
151208
+*/
151209
+void whereAddLimitExpr(
151210
+ WhereClause *pWC, /* Add the constraint to this WHERE clause */
151211
+ int iReg, /* Register that will hold value of the limit/offset */
151212
+ Expr *pExpr, /* Expression that defines the limit/offset */
151213
+ int iCsr, /* Cursor to which the constraint applies */
151214
+ int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
151215
+){
151216
+ Parse *pParse = pWC->pWInfo->pParse;
151217
+ sqlite3 *db = pParse->db;
151218
+ Expr *pNew;
151219
+ int iVal = 0;
151220
+
151221
+ if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){
151222
+ Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0);
151223
+ if( pVal==0 ) return;
151224
+ ExprSetProperty(pVal, EP_IntValue);
151225
+ pVal->u.iValue = iVal;
151226
+ pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
151227
+ }else{
151228
+ Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0);
151229
+ if( pVal==0 ) return;
151230
+ pVal->iTable = iReg;
151231
+ pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
151232
+ }
151233
+ if( pNew ){
151234
+ WhereTerm *pTerm;
151235
+ int idx;
151236
+ idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL);
151237
+ pTerm = &pWC->a[idx];
151238
+ pTerm->leftCursor = iCsr;
151239
+ pTerm->eOperator = WO_AUX;
151240
+ pTerm->eMatchOp = eMatchOp;
151241
+ }
151242
+}
151243
+
151244
+/*
151245
+** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
151246
+** SELECT statement passed as the second argument. These terms are only
151247
+** added if:
151248
+**
151249
+** 1. The SELECT statement has a LIMIT clause, and
151250
+** 2. The SELECT statement is not an aggregate or DISTINCT query, and
151251
+** 3. The SELECT statement has exactly one object in its from clause, and
151252
+** that object is a virtual table, and
151253
+** 4. There are no terms in the WHERE clause that will not be passed
151254
+** to the virtual table xBestIndex method.
151255
+** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
151256
+** method.
151257
+**
151258
+** LIMIT and OFFSET terms are ignored by most of the planner code. They
151259
+** exist only so that they may be passed to the xBestIndex method of the
151260
+** single virtual table in the FROM clause of the SELECT.
151261
+*/
151262
+SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
151263
+ assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
151264
+ if( (p && p->pLimit) /* 1 */
151265
+ && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
151266
+ && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
151267
+ ){
151268
+ ExprList *pOrderBy = p->pOrderBy;
151269
+ int iCsr = p->pSrc->a[0].iCursor;
151270
+ int ii;
151271
+
151272
+ /* Check condition (4). Return early if it is not met. */
151273
+ for(ii=0; ii<pWC->nTerm; ii++){
151274
+ if( pWC->a[ii].wtFlags & TERM_CODED ){
151275
+ /* This term is a vector operation that has been decomposed into
151276
+ ** other, subsequent terms. It can be ignored. See tag-20220128a */
151277
+ assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
151278
+ assert( pWC->a[ii].eOperator==0 );
151279
+ continue;
151280
+ }
151281
+ if( pWC->a[ii].leftCursor!=iCsr ) return;
151282
+ }
151283
+
151284
+ /* Check condition (5). Return early if it is not met. */
151285
+ if( pOrderBy ){
151286
+ for(ii=0; ii<pOrderBy->nExpr; ii++){
151287
+ Expr *pExpr = pOrderBy->a[ii].pExpr;
151288
+ if( pExpr->op!=TK_COLUMN ) return;
151289
+ if( pExpr->iTable!=iCsr ) return;
151290
+ if( pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_BIGNULL ) return;
151291
+ }
151292
+ }
151293
+
151294
+ /* All conditions are met. Add the terms to the where-clause object. */
151295
+ assert( p->pLimit->op==TK_LIMIT );
151296
+ whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
151297
+ iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
151298
+ if( p->iOffset>0 ){
151299
+ whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
151300
+ iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
151301
+ }
151302
+ }
151303
+}
151129151304
151130151305
/*
151131151306
** Initialize a preallocated WhereClause structure.
151132151307
*/
151133151308
SQLITE_PRIVATE void sqlite3WhereClauseInit(
@@ -151160,10 +151335,11 @@
151160151335
for(i=pWC->nBase; i<pWC->nTerm; i++){
151161151336
assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
151162151337
}
151163151338
#endif
151164151339
while(1){
151340
+ assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
151165151341
if( a->wtFlags & TERM_DYNAMIC ){
151166151342
sqlite3ExprDelete(db, a->pExpr);
151167151343
}
151168151344
if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
151169151345
if( a->wtFlags & TERM_ORINFO ){
@@ -151317,10 +151493,11 @@
151317151493
if( pColRef==0 ) return;
151318151494
pColRef->iTable = pItem->iCursor;
151319151495
pColRef->iColumn = k++;
151320151496
assert( ExprUseYTab(pColRef) );
151321151497
pColRef->y.pTab = pTab;
151498
+ pItem->colUsed |= sqlite3ExprColUsed(pColRef);
151322151499
pRhs = sqlite3PExpr(pParse, TK_UPLUS,
151323151500
sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
151324151501
pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
151325151502
if( pItem->fg.jointype & JT_LEFT ){
151326151503
sqlite3SetJoinExpr(pTerm, pItem->iCursor);
@@ -154789,10 +154966,19 @@
154789154966
}
154790154967
return rc;
154791154968
}
154792154969
154793154970
#ifndef SQLITE_OMIT_VIRTUALTABLE
154971
+
154972
+/*
154973
+** Return true if pTerm is a virtual table LIMIT or OFFSET term.
154974
+*/
154975
+static int isLimitTerm(WhereTerm *pTerm){
154976
+ assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
154977
+ return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
154978
+ && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
154979
+}
154794154980
154795154981
/*
154796154982
** Argument pIdxInfo is already populated with all constraints that may
154797154983
** be used by the virtual table identified by pBuilder->pNew->iTab. This
154798154984
** function marks a subset of those constraints usable, invokes the
@@ -154817,11 +155003,12 @@
154817155003
Bitmask mPrereq, /* Mask of tables that must be used. */
154818155004
Bitmask mUsable, /* Mask of usable tables */
154819155005
u16 mExclude, /* Exclude terms using these operators */
154820155006
sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
154821155007
u16 mNoOmit, /* Do not omit these constraints */
154822
- int *pbIn /* OUT: True if plan uses an IN(...) op */
155008
+ int *pbIn, /* OUT: True if plan uses an IN(...) op */
155009
+ int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
154823155010
){
154824155011
WhereClause *pWC = pBuilder->pWC;
154825155012
struct sqlite3_index_constraint *pIdxCons;
154826155013
struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
154827155014
int i;
@@ -154842,10 +155029,11 @@
154842155029
for(i=0; i<nConstraint; i++, pIdxCons++){
154843155030
WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
154844155031
pIdxCons->usable = 0;
154845155032
if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
154846155033
&& (pTerm->eOperator & mExclude)==0
155034
+ && (pbRetryLimit || !isLimitTerm(pTerm))
154847155035
){
154848155036
pIdxCons->usable = 1;
154849155037
}
154850155038
}
154851155039
@@ -154874,12 +155062,12 @@
154874155062
return rc;
154875155063
}
154876155064
154877155065
mxTerm = -1;
154878155066
assert( pNew->nLSlot>=nConstraint );
154879
- for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
154880
- pNew->u.vtab.omitMask = 0;
155067
+ memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
155068
+ memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
154881155069
pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
154882155070
for(i=0; i<nConstraint; i++, pIdxCons++){
154883155071
int iTerm;
154884155072
if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
154885155073
WhereTerm *pTerm;
@@ -154909,10 +155097,13 @@
154909155097
testcase( i!=iTerm );
154910155098
pNew->u.vtab.omitMask |= 1<<iTerm;
154911155099
}else{
154912155100
testcase( i!=iTerm );
154913155101
}
155102
+ if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
155103
+ pNew->u.vtab.bOmitOffset = 1;
155104
+ }
154914155105
}
154915155106
if( (pTerm->eOperator & WO_IN)!=0 ){
154916155107
/* A virtual table that is constrained by an IN clause may not
154917155108
** consume the ORDER BY clause because (1) the order of IN terms
154918155109
** is not necessarily related to the order of output terms and
@@ -154920,10 +155111,25 @@
154920155111
** together. */
154921155112
pIdxInfo->orderByConsumed = 0;
154922155113
pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
154923155114
*pbIn = 1; assert( (mExclude & WO_IN)==0 );
154924155115
}
155116
+
155117
+ if( isLimitTerm(pTerm) && *pbIn ){
155118
+ /* If there is an IN(...) term handled as an == (separate call to
155119
+ ** xFilter for each value on the RHS of the IN) and a LIMIT or
155120
+ ** OFFSET term handled as well, the plan is unusable. Set output
155121
+ ** variable *pbRetryLimit to true to tell the caller to retry with
155122
+ ** LIMIT and OFFSET disabled. */
155123
+ if( pIdxInfo->needToFreeIdxStr ){
155124
+ sqlite3_free(pIdxInfo->idxStr);
155125
+ pIdxInfo->idxStr = 0;
155126
+ pIdxInfo->needToFreeIdxStr = 0;
155127
+ }
155128
+ *pbRetryLimit = 1;
155129
+ return SQLITE_OK;
155130
+ }
154925155131
}
154926155132
}
154927155133
154928155134
pNew->nLTerm = mxTerm+1;
154929155135
for(i=0; i<=mxTerm; i++){
@@ -155082,10 +155288,11 @@
155082155288
int nConstraint; /* Number of constraints in p */
155083155289
int bIn; /* True if plan uses IN(...) operator */
155084155290
WhereLoop *pNew;
155085155291
Bitmask mBest; /* Tables used by best possible plan */
155086155292
u16 mNoOmit;
155293
+ int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
155087155294
155088155295
assert( (mPrereq & mUnusable)==0 );
155089155296
pWInfo = pBuilder->pWInfo;
155090155297
pParse = pWInfo->pParse;
155091155298
pWC = pBuilder->pWC;
@@ -155105,11 +155312,19 @@
155105155312
}
155106155313
155107155314
/* First call xBestIndex() with all constraints usable. */
155108155315
WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
155109155316
WHERETRACE(0x40, (" VirtualOne: all usable\n"));
155110
- rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
155317
+ rc = whereLoopAddVirtualOne(
155318
+ pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry
155319
+ );
155320
+ if( bRetry ){
155321
+ assert( rc==SQLITE_OK );
155322
+ rc = whereLoopAddVirtualOne(
155323
+ pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0
155324
+ );
155325
+ }
155111155326
155112155327
/* If the call to xBestIndex() with all terms enabled produced a plan
155113155328
** that does not require any source tables (IOW: a plan with mBest==0)
155114155329
** and does not use an IN(...) operator, then there is no point in making
155115155330
** any further calls to xBestIndex() since they will all return the same
@@ -155123,11 +155338,11 @@
155123155338
/* If the plan produced by the earlier call uses an IN(...) term, call
155124155339
** xBestIndex again, this time with IN(...) terms disabled. */
155125155340
if( bIn ){
155126155341
WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
155127155342
rc = whereLoopAddVirtualOne(
155128
- pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
155343
+ pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0);
155129155344
assert( bIn==0 );
155130155345
mBestNoIn = pNew->prereq & ~mPrereq;
155131155346
if( mBestNoIn==0 ){
155132155347
seenZero = 1;
155133155348
seenZeroNoIN = 1;
@@ -155150,11 +155365,11 @@
155150155365
if( mNext==ALLBITS ) break;
155151155366
if( mNext==mBest || mNext==mBestNoIn ) continue;
155152155367
WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
155153155368
(sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
155154155369
rc = whereLoopAddVirtualOne(
155155
- pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
155370
+ pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
155156155371
if( pNew->prereq==mPrereq ){
155157155372
seenZero = 1;
155158155373
if( bIn==0 ) seenZeroNoIN = 1;
155159155374
}
155160155375
}
@@ -155163,21 +155378,21 @@
155163155378
** that requires no source tables at all (i.e. one guaranteed to be
155164155379
** usable), make a call here with all source tables disabled */
155165155380
if( rc==SQLITE_OK && seenZero==0 ){
155166155381
WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
155167155382
rc = whereLoopAddVirtualOne(
155168
- pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
155383
+ pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
155169155384
if( bIn==0 ) seenZeroNoIN = 1;
155170155385
}
155171155386
155172155387
/* If the calls to xBestIndex() have so far failed to find a plan
155173155388
** that requires no source tables at all and does not use an IN(...)
155174155389
** operator, make a final call to obtain one here. */
155175155390
if( rc==SQLITE_OK && seenZeroNoIN==0 ){
155176155391
WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
155177155392
rc = whereLoopAddVirtualOne(
155178
- pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
155393
+ pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0);
155179155394
}
155180155395
}
155181155396
155182155397
if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
155183155398
freeIndexInfo(pParse->db, p);
@@ -156579,10 +156794,11 @@
156579156794
Parse *pParse, /* The parser context */
156580156795
SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
156581156796
Expr *pWhere, /* The WHERE clause */
156582156797
ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
156583156798
ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
156799
+ Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
156584156800
u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
156585156801
int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
156586156802
** If WHERE_USE_LIMIT, then the limit amount */
156587156803
){
156588156804
int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -156655,10 +156871,13 @@
156655156871
pWInfo->nLevel = nTabList;
156656156872
pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
156657156873
pWInfo->wctrlFlags = wctrlFlags;
156658156874
pWInfo->iLimit = iAuxArg;
156659156875
pWInfo->savedNQueryLoop = pParse->nQueryLoop;
156876
+#ifndef SQLITE_OMIT_VIRTUALTABLE
156877
+ pWInfo->pLimit = pLimit;
156878
+#endif
156660156879
memset(&pWInfo->nOBSat, 0,
156661156880
offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
156662156881
memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
156663156882
assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
156664156883
pMaskSet = &pWInfo->sMaskSet;
@@ -156723,10 +156942,11 @@
156723156942
#endif
156724156943
}
156725156944
156726156945
/* Analyze all of the subexpressions. */
156727156946
sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
156947
+ sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
156728156948
if( db->mallocFailed ) goto whereBeginError;
156729156949
156730156950
/* Special case: WHERE terms that do not refer to any tables in the join
156731156951
** (constant expressions). Evaluate each such term, and jump over all the
156732156952
** generated code if the result is not true.
@@ -233598,11 +233818,11 @@
233598233818
int nArg, /* Number of args */
233599233819
sqlite3_value **apUnused /* Function arguments */
233600233820
){
233601233821
assert( nArg==0 );
233602233822
UNUSED_PARAM2(nArg, apUnused);
233603
- sqlite3_result_text(pCtx, "fts5: 2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17", -1, SQLITE_TRANSIENT);
233823
+ sqlite3_result_text(pCtx, "fts5: 2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808", -1, SQLITE_TRANSIENT);
233604233824
}
233605233825
233606233826
/*
233607233827
** Return true if zName is the extension on one of the shadow tables used
233608233828
** by this module.
233609233829
--- 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.38.0"
456 #define SQLITE_VERSION_NUMBER 3038000
457 #define SQLITE_SOURCE_ID "2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -4656,10 +4656,12 @@
4656 ** still make the distinction between protected and unprotected
4657 ** sqlite3_value objects even when not strictly required.
4658 **
4659 ** ^The sqlite3_value objects that are passed as parameters into the
4660 ** implementation of [application-defined SQL functions] are protected.
 
 
4661 ** ^The sqlite3_value object returned by
4662 ** [sqlite3_column_value()] is unprotected.
4663 ** Unprotected sqlite3_value objects may only be used as arguments
4664 ** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4665 ** [sqlite3_value_dup()].
@@ -7435,28 +7437,60 @@
7435 /*
7436 ** CAPI3REF: Virtual Table Constraint Operator Codes
7437 **
7438 ** These macros define the allowed values for the
7439 ** [sqlite3_index_info].aConstraint[].op field. Each value represents
7440 ** an operator that is part of a constraint term in the wHERE clause of
7441 ** a query that uses a [virtual table].
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7442 */
7443 #define SQLITE_INDEX_CONSTRAINT_EQ 2
7444 #define SQLITE_INDEX_CONSTRAINT_GT 4
7445 #define SQLITE_INDEX_CONSTRAINT_LE 8
7446 #define SQLITE_INDEX_CONSTRAINT_LT 16
7447 #define SQLITE_INDEX_CONSTRAINT_GE 32
7448 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
7449 #define SQLITE_INDEX_CONSTRAINT_LIKE 65
7450 #define SQLITE_INDEX_CONSTRAINT_GLOB 66
7451 #define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7452 #define SQLITE_INDEX_CONSTRAINT_NE 68
7453 #define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7454 #define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7455 #define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7456 #define SQLITE_INDEX_CONSTRAINT_IS 72
7457 #define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
 
 
7458
7459 /*
7460 ** CAPI3REF: Register A Virtual Table Implementation
7461 ** METHOD: sqlite3
7462 **
@@ -9812,28 +9846,29 @@
9812
9813 /*
9814 ** CAPI3REF: Determine if a virtual table query is DISTINCT
9815 ** METHOD: sqlite3_index_info
9816 **
9817 ** This API may only be used from within an xBestIndex() callback. The
9818 ** results of calling it from outside of an xBestIndex() callback are
9819 ** undefined and probably harmful.
9820 **
9821 ** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or
9822 ** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table
9823 ** additional information about how the query planner wants the output to be
9824 ** ordered. As long as the virtual table can meet the ordering requirements
9825 ** of the query planner, it may set the "orderByConsumed" flag.
 
9826 **
9827 ** <ol><li value="0"><p>
9828 ** ^If the sqlite3_vtab_distinct() interface returns 0, that means
9829 ** that the query planner needs the virtual table to return all rows in the
9830 ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
9831 ** [sqlite3_index_info] object. This is the default expectation. If the
9832 ** virtual table outputs all rows in sorted order, then it is always safe for
9833 ** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9834 ** what the return value from sqlite3_vtab_distinct().
9835 ** <li value="1"><p>
9836 ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
9837 ** that the query planner does not need the rows to be returned in sorted order
9838 ** as long as all rows with the same values in all columns identified by the
9839 ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9842,11 +9877,11 @@
9842 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9843 ** that the query planner does not need the rows returned in any particular
9844 ** order, as long as rows with the same values in all "aOrderBy" columns
9845 ** are adjacent.)^ ^(Furthermore, only a single row for each particular
9846 ** combination of values in the columns identified by the "aOrderBy" field
9847 ** needs to be returned.)^ ^It is ok always for two or more rows with the same
9848 ** values in all "aOrderBy" columns to be returned, as long as all such rows
9849 ** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9850 ** that have the same value for all columns identified by "aOrderBy".
9851 ** ^However omitting the extra rows is optional.
9852 ** This mode is used for a DISTINCT query.
@@ -9877,31 +9912,45 @@
9877
9878 /*
9879 ** CAPI3REF: Constraint values in xBestIndex()
9880 ** METHOD: sqlite3_index_info
9881 **
9882 ** This API may only be used from within an xBestIndex() callback. The
9883 ** results of calling it from outside of an xBestIndex() callback are
9884 ** undefined and probably harmful.
9885 **
9886 ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
9887 ** the [xBestIndex] method of a [virtual table] implementation, with P being
9888 ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
9889 ** J being a 0-based index into P->aConstraint[], then this routine
9890 ** attempts to set *V to be the value on the right-hand side of
9891 ** that constraint if the right-hand side is a known constant. ^If the
9892 ** right-hand side of the constraint is not known, then *V is set to a NULL
9893 ** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9894 ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
9895 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
9896 ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
9897 ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
9898 ** something goes wrong.
9899 **
9900 ** ^The sqlite3_value object returned in *V remains valid for the duration of
9901 ** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value
9902 ** object returned by sqlite3_vtab_rhs_value() is automatically deallocated.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9903 */
9904 SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
9905
9906 /*
9907 ** CAPI3REF: Conflict resolution modes
@@ -19662,11 +19711,12 @@
19662 #endif
19663 SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*);
19664 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19665 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19666 Upsert*);
19667 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 
19668 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19669 SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19670 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19671 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19672 SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
@@ -23545,11 +23595,11 @@
23545 ** is not the first modifier, or if the prior argument is not a numeric
23546 ** value in the allowed range of julian day numbers understood by
23547 ** SQLite (0..5373484.5) then the result will be NULL.
23548 */
23549 if( sqlite3_stricmp(z, "julianday")==0 ){
23550 if( idx>1 ) return 1;
23551 if( p->validJD && p->rawS ){
23552 rc = 0;
23553 p->rawS = 0;
23554 }
23555 }
@@ -23576,10 +23626,11 @@
23576 **
23577 ** Treat the current value of p->s as the number of
23578 ** seconds since 1970. Convert to a real julian day number.
23579 */
23580 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
 
23581 r = p->s*1000.0 + 210866760000000.0;
23582 if( r>=0.0 && r<464269060800000.0 ){
23583 clearYMD_HMS_TZ(p);
23584 p->iJD = (sqlite3_int64)(r + 0.5);
23585 p->validJD = 1;
@@ -76814,11 +76865,11 @@
76814 int i = sqlite3FindDbName(pDb, zDb);
76815
76816 if( i==1 ){
76817 Parse sParse;
76818 int rc = 0;
76819 sqlite3ParseObjectInit(&sParse,pErrorDb);
76820 if( sqlite3OpenTempDatabase(&sParse) ){
76821 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
76822 rc = SQLITE_ERROR;
76823 }
76824 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
@@ -79032,15 +79083,11 @@
79032 const char *zNeg = "";
79033 int rc = SQLITE_OK;
79034
79035 assert( pExpr!=0 );
79036 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
79037 #if defined(SQLITE_ENABLE_STAT4)
79038 if( op==TK_REGISTER ) op = pExpr->op2;
79039 #else
79040 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
79041 #endif
79042
79043 /* Compressed expressions only appear when parsing the DEFAULT clause
79044 ** on a table column definition, and hence only when pCtx==0. This
79045 ** check ensures that an EP_TokenOnly expression is never passed down
79046 ** into valueFromFunction(). */
@@ -120069,11 +120116,11 @@
120069 **
120070 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
120071 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
120072 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
120073 */
120074 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
120075 if( pWInfo==0 ) goto delete_from_cleanup;
120076 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
120077 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
120078 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
120079 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
@@ -123612,11 +123659,11 @@
123612
123613 /* Create VDBE to loop through the entries in pSrc that match the WHERE
123614 ** clause. For each row found, increment either the deferred or immediate
123615 ** foreign key constraint counter. */
123616 if( pParse->nErr==0 ){
123617 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
123618 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
123619 if( pWInfo ){
123620 sqlite3WhereEnd(pWInfo);
123621 }
123622 }
@@ -133324,10 +133371,11 @@
133324 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
133325 assert( db->pParse!=pParse );
133326 pParse->pOuterParse = db->pParse;
133327 db->pParse = pParse;
133328 pParse->db = db;
 
133329 }
133330
133331 /*
133332 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
133333 */
@@ -133350,11 +133398,11 @@
133350 sParse.pOuterParse = db->pParse;
133351 db->pParse = &sParse;
133352 sParse.db = db;
133353 sParse.pReprepare = pReprepare;
133354 assert( ppStmt && *ppStmt==0 );
133355 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
133356 assert( sqlite3_mutex_held(db->mutex) );
133357
133358 /* For a long-term use prepared statement avoid the use of
133359 ** lookaside memory.
133360 */
@@ -140618,11 +140666,11 @@
140618
140619
140620 /* Begin the database scan. */
140621 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140622 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
140623 p->pEList, wctrlFlags, p->nSelectRow);
140624 if( pWInfo==0 ) goto select_end;
140625 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
140626 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
140627 }
140628 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
@@ -140882,11 +140930,11 @@
140882 ** in the right order to begin with.
140883 */
140884 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
140885 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140886 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
140887 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0
140888 );
140889 if( pWInfo==0 ){
140890 sqlite3ExprListDelete(db, pDistinct);
140891 goto select_end;
140892 }
@@ -141180,11 +141228,11 @@
141180 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
141181 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
141182
141183 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
141184 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
141185 pDistinct, minMaxFlag|distFlag, 0);
141186 if( pWInfo==0 ){
141187 goto select_end;
141188 }
141189 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
141190 eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -143623,11 +143671,11 @@
143623 ** or index, causing a single-pass approach to malfunction. */
143624 flags = WHERE_ONEPASS_DESIRED;
143625 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
143626 flags |= WHERE_ONEPASS_MULTIROW;
143627 }
143628 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags,iIdxCur);
143629 if( pWInfo==0 ) goto update_cleanup;
143630
143631 /* A one-pass strategy that might update more than one row may not
143632 ** be used if any column of the index used for the scan is being
143633 ** updated. Otherwise, if there is an index on "b", statements like
@@ -144145,11 +144193,13 @@
144145 }else{
144146 regRec = ++pParse->nMem;
144147 regRowid = ++pParse->nMem;
144148
144149 /* Start scanning the virtual table */
144150 pWInfo = sqlite3WhereBegin(pParse, pSrc,pWhere,0,0,WHERE_ONEPASS_DESIRED,0);
 
 
144151 if( pWInfo==0 ) return;
144152
144153 /* Populate the argument registers. */
144154 for(i=0; i<pTab->nCol; i++){
144155 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
@@ -146456,11 +146506,12 @@
146456 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
146457 Index *pIndex; /* Index used, or NULL */
146458 } btree;
146459 struct { /* Information for virtual tables */
146460 int idxNum; /* Index number */
146461 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
 
146462 i8 isOrdered; /* True if satisfies ORDER BY */
146463 u16 omitMask; /* Terms that may be omitted */
146464 char *idxStr; /* Index identifier string */
146465 } vtab;
146466 } u;
@@ -146786,10 +146837,13 @@
146786 Parse *pParse; /* Parsing and code generating context */
146787 SrcList *pTabList; /* List of tables in the join */
146788 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
146789 ExprList *pResultSet; /* Result set of the query */
146790 Expr *pWhere; /* The complete WHERE clause */
 
 
 
146791 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
146792 int iContinue; /* Jump here to continue with next record */
146793 int iBreak; /* Jump here to break out of the loop */
146794 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
146795 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -146871,10 +146925,11 @@
146871
146872 /* whereexpr.c: */
146873 SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
146874 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
146875 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
 
146876 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
146877 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
146878 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
146879 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
146880 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
@@ -146941,10 +146996,11 @@
146941 #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
146942 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
146943 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
146944 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
146945 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
 
146946
146947 #endif /* !defined(SQLITE_WHEREINT_H) */
146948
146949 /************** End of whereInt.h ********************************************/
146950 /************** Continuing where we left off in wherecode.c ******************/
@@ -148464,10 +148520,19 @@
148464 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
148465 addrNotFound = pLevel->addrNxt;
148466 }else{
148467 Expr *pRight = pTerm->pExpr->pRight;
148468 codeExprOrVector(pParse, pRight, iTarget, 1);
 
 
 
 
 
 
 
 
 
148469 }
148470 }
148471 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
148472 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
148473 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
@@ -149285,11 +149350,11 @@
149285 pOrExpr = pAndExpr;
149286 }
149287 /* Loop through table entries that match term pOrTerm. */
149288 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
149289 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
149290 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
149291 WHERE_OR_SUBCLAUSE, iCovCur);
149292 assert( pSubWInfo || pParse->nErr );
149293 if( pSubWInfo ){
149294 WhereLoop *pSubLoop;
149295 int addrExplain = sqlite3WhereExplainOneScan(
@@ -150982,11 +151047,14 @@
150982 ** new terms for each component comparison - "a = ?" and "b = ?". The
150983 ** new terms completely replace the original vector comparison, which is
150984 ** no longer used.
150985 **
150986 ** This is only required if at least one side of the comparison operation
150987 ** is not a sub-select. */
 
 
 
150988 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
150989 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
150990 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
150991 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
150992 || (pExpr->pRight->flags & EP_xIsSelect)==0)
@@ -151124,10 +151192,117 @@
151124 }else{
151125 sqlite3WhereSplit(pWC, pE2->pLeft, op);
151126 sqlite3WhereSplit(pWC, pE2->pRight, op);
151127 }
151128 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151129
151130 /*
151131 ** Initialize a preallocated WhereClause structure.
151132 */
151133 SQLITE_PRIVATE void sqlite3WhereClauseInit(
@@ -151160,10 +151335,11 @@
151160 for(i=pWC->nBase; i<pWC->nTerm; i++){
151161 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
151162 }
151163 #endif
151164 while(1){
 
151165 if( a->wtFlags & TERM_DYNAMIC ){
151166 sqlite3ExprDelete(db, a->pExpr);
151167 }
151168 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
151169 if( a->wtFlags & TERM_ORINFO ){
@@ -151317,10 +151493,11 @@
151317 if( pColRef==0 ) return;
151318 pColRef->iTable = pItem->iCursor;
151319 pColRef->iColumn = k++;
151320 assert( ExprUseYTab(pColRef) );
151321 pColRef->y.pTab = pTab;
 
151322 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
151323 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
151324 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
151325 if( pItem->fg.jointype & JT_LEFT ){
151326 sqlite3SetJoinExpr(pTerm, pItem->iCursor);
@@ -154789,10 +154966,19 @@
154789 }
154790 return rc;
154791 }
154792
154793 #ifndef SQLITE_OMIT_VIRTUALTABLE
 
 
 
 
 
 
 
 
 
154794
154795 /*
154796 ** Argument pIdxInfo is already populated with all constraints that may
154797 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
154798 ** function marks a subset of those constraints usable, invokes the
@@ -154817,11 +155003,12 @@
154817 Bitmask mPrereq, /* Mask of tables that must be used. */
154818 Bitmask mUsable, /* Mask of usable tables */
154819 u16 mExclude, /* Exclude terms using these operators */
154820 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
154821 u16 mNoOmit, /* Do not omit these constraints */
154822 int *pbIn /* OUT: True if plan uses an IN(...) op */
 
154823 ){
154824 WhereClause *pWC = pBuilder->pWC;
154825 struct sqlite3_index_constraint *pIdxCons;
154826 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
154827 int i;
@@ -154842,10 +155029,11 @@
154842 for(i=0; i<nConstraint; i++, pIdxCons++){
154843 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
154844 pIdxCons->usable = 0;
154845 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
154846 && (pTerm->eOperator & mExclude)==0
 
154847 ){
154848 pIdxCons->usable = 1;
154849 }
154850 }
154851
@@ -154874,12 +155062,12 @@
154874 return rc;
154875 }
154876
154877 mxTerm = -1;
154878 assert( pNew->nLSlot>=nConstraint );
154879 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
154880 pNew->u.vtab.omitMask = 0;
154881 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
154882 for(i=0; i<nConstraint; i++, pIdxCons++){
154883 int iTerm;
154884 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
154885 WhereTerm *pTerm;
@@ -154909,10 +155097,13 @@
154909 testcase( i!=iTerm );
154910 pNew->u.vtab.omitMask |= 1<<iTerm;
154911 }else{
154912 testcase( i!=iTerm );
154913 }
 
 
 
154914 }
154915 if( (pTerm->eOperator & WO_IN)!=0 ){
154916 /* A virtual table that is constrained by an IN clause may not
154917 ** consume the ORDER BY clause because (1) the order of IN terms
154918 ** is not necessarily related to the order of output terms and
@@ -154920,10 +155111,25 @@
154920 ** together. */
154921 pIdxInfo->orderByConsumed = 0;
154922 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
154923 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
154924 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154925 }
154926 }
154927
154928 pNew->nLTerm = mxTerm+1;
154929 for(i=0; i<=mxTerm; i++){
@@ -155082,10 +155288,11 @@
155082 int nConstraint; /* Number of constraints in p */
155083 int bIn; /* True if plan uses IN(...) operator */
155084 WhereLoop *pNew;
155085 Bitmask mBest; /* Tables used by best possible plan */
155086 u16 mNoOmit;
 
155087
155088 assert( (mPrereq & mUnusable)==0 );
155089 pWInfo = pBuilder->pWInfo;
155090 pParse = pWInfo->pParse;
155091 pWC = pBuilder->pWC;
@@ -155105,11 +155312,19 @@
155105 }
155106
155107 /* First call xBestIndex() with all constraints usable. */
155108 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
155109 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
155110 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
 
 
 
 
 
 
 
 
155111
155112 /* If the call to xBestIndex() with all terms enabled produced a plan
155113 ** that does not require any source tables (IOW: a plan with mBest==0)
155114 ** and does not use an IN(...) operator, then there is no point in making
155115 ** any further calls to xBestIndex() since they will all return the same
@@ -155123,11 +155338,11 @@
155123 /* If the plan produced by the earlier call uses an IN(...) term, call
155124 ** xBestIndex again, this time with IN(...) terms disabled. */
155125 if( bIn ){
155126 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
155127 rc = whereLoopAddVirtualOne(
155128 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
155129 assert( bIn==0 );
155130 mBestNoIn = pNew->prereq & ~mPrereq;
155131 if( mBestNoIn==0 ){
155132 seenZero = 1;
155133 seenZeroNoIN = 1;
@@ -155150,11 +155365,11 @@
155150 if( mNext==ALLBITS ) break;
155151 if( mNext==mBest || mNext==mBestNoIn ) continue;
155152 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
155153 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
155154 rc = whereLoopAddVirtualOne(
155155 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
155156 if( pNew->prereq==mPrereq ){
155157 seenZero = 1;
155158 if( bIn==0 ) seenZeroNoIN = 1;
155159 }
155160 }
@@ -155163,21 +155378,21 @@
155163 ** that requires no source tables at all (i.e. one guaranteed to be
155164 ** usable), make a call here with all source tables disabled */
155165 if( rc==SQLITE_OK && seenZero==0 ){
155166 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
155167 rc = whereLoopAddVirtualOne(
155168 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
155169 if( bIn==0 ) seenZeroNoIN = 1;
155170 }
155171
155172 /* If the calls to xBestIndex() have so far failed to find a plan
155173 ** that requires no source tables at all and does not use an IN(...)
155174 ** operator, make a final call to obtain one here. */
155175 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
155176 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
155177 rc = whereLoopAddVirtualOne(
155178 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
155179 }
155180 }
155181
155182 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
155183 freeIndexInfo(pParse->db, p);
@@ -156579,10 +156794,11 @@
156579 Parse *pParse, /* The parser context */
156580 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
156581 Expr *pWhere, /* The WHERE clause */
156582 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
156583 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
 
156584 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
156585 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
156586 ** If WHERE_USE_LIMIT, then the limit amount */
156587 ){
156588 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -156655,10 +156871,13 @@
156655 pWInfo->nLevel = nTabList;
156656 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
156657 pWInfo->wctrlFlags = wctrlFlags;
156658 pWInfo->iLimit = iAuxArg;
156659 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
 
 
 
156660 memset(&pWInfo->nOBSat, 0,
156661 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
156662 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
156663 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
156664 pMaskSet = &pWInfo->sMaskSet;
@@ -156723,10 +156942,11 @@
156723 #endif
156724 }
156725
156726 /* Analyze all of the subexpressions. */
156727 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
 
156728 if( db->mallocFailed ) goto whereBeginError;
156729
156730 /* Special case: WHERE terms that do not refer to any tables in the join
156731 ** (constant expressions). Evaluate each such term, and jump over all the
156732 ** generated code if the result is not true.
@@ -233598,11 +233818,11 @@
233598 int nArg, /* Number of args */
233599 sqlite3_value **apUnused /* Function arguments */
233600 ){
233601 assert( nArg==0 );
233602 UNUSED_PARAM2(nArg, apUnused);
233603 sqlite3_result_text(pCtx, "fts5: 2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17", -1, SQLITE_TRANSIENT);
233604 }
233605
233606 /*
233607 ** Return true if zName is the extension on one of the shadow tables used
233608 ** by this module.
233609
--- 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.38.0"
456 #define SQLITE_VERSION_NUMBER 3038000
457 #define SQLITE_SOURCE_ID "2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -4656,10 +4656,12 @@
4656 ** still make the distinction between protected and unprotected
4657 ** sqlite3_value objects even when not strictly required.
4658 **
4659 ** ^The sqlite3_value objects that are passed as parameters into the
4660 ** implementation of [application-defined SQL functions] are protected.
4661 ** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4662 ** are protected.
4663 ** ^The sqlite3_value object returned by
4664 ** [sqlite3_column_value()] is unprotected.
4665 ** Unprotected sqlite3_value objects may only be used as arguments
4666 ** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4667 ** [sqlite3_value_dup()].
@@ -7435,28 +7437,60 @@
7437 /*
7438 ** CAPI3REF: Virtual Table Constraint Operator Codes
7439 **
7440 ** These macros define the allowed values for the
7441 ** [sqlite3_index_info].aConstraint[].op field. Each value represents
7442 ** an operator that is part of a constraint term in the WHERE clause of
7443 ** a query that uses a [virtual table].
7444 **
7445 ** ^The left-hand operand of the operator is given by the corresponding
7446 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7447 ** operand is the rowid.
7448 ** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7449 ** operators have no left-hand operand, and so for those operators the
7450 ** corresponding aConstraint[].iColumn is meaningless and should not be
7451 ** used.
7452 **
7453 ** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7454 ** value 255 are reserved to represent functions that are overloaded
7455 ** by the [xFindFunction|xFindFunction method] of the virtual table
7456 ** implementation.
7457 **
7458 ** The right-hand operands for each constraint might be accessible using
7459 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7460 ** operand is only available if it appears as a single constant literal
7461 ** in the input SQL. If the right-hand operand is another column or an
7462 ** expression (even a constant expression) or a parameter, then the
7463 ** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7464 ** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7465 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7466 ** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7467 ** always return SQLITE_NOTFOUND.
7468 **
7469 ** The collating sequence to be used for comparison can be found using
7470 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7471 ** tables, the collating sequence of constraints does not matter (for example
7472 ** because the constraints are numeric) and so the sqlite3_vtab_collation()
7473 ** interface is no commonly needed.
7474 */
7475 #define SQLITE_INDEX_CONSTRAINT_EQ 2
7476 #define SQLITE_INDEX_CONSTRAINT_GT 4
7477 #define SQLITE_INDEX_CONSTRAINT_LE 8
7478 #define SQLITE_INDEX_CONSTRAINT_LT 16
7479 #define SQLITE_INDEX_CONSTRAINT_GE 32
7480 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
7481 #define SQLITE_INDEX_CONSTRAINT_LIKE 65
7482 #define SQLITE_INDEX_CONSTRAINT_GLOB 66
7483 #define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7484 #define SQLITE_INDEX_CONSTRAINT_NE 68
7485 #define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7486 #define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7487 #define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7488 #define SQLITE_INDEX_CONSTRAINT_IS 72
7489 #define SQLITE_INDEX_CONSTRAINT_LIMIT 73
7490 #define SQLITE_INDEX_CONSTRAINT_OFFSET 74
7491 #define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7492
7493 /*
7494 ** CAPI3REF: Register A Virtual Table Implementation
7495 ** METHOD: sqlite3
7496 **
@@ -9812,28 +9846,29 @@
9846
9847 /*
9848 ** CAPI3REF: Determine if a virtual table query is DISTINCT
9849 ** METHOD: sqlite3_index_info
9850 **
9851 ** This API may only be used from within an [xBestIndex|xBestIndex method]
9852 ** of a [virtual table] implementation. The result of calling this
9853 ** interface from outside of xBestIndex() is undefined and probably harmful.
9854 **
9855 ** ^The sqlite3_vtab_distinct() interface returns an integer that is
9856 ** either 0, 1, or 2. The integer returned by sqlite3_vtab_distinct()
9857 ** gives the virtual table additional information about how the query
9858 ** planner wants the output to be ordered. As long as the virtual table
9859 ** can meet the ordering requirements of the query planner, it may set
9860 ** the "orderByConsumed" flag.
9861 **
9862 ** <ol><li value="0"><p>
9863 ** ^If the sqlite3_vtab_distinct() interface returns 0, that means
9864 ** that the query planner needs the virtual table to return all rows in the
9865 ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
9866 ** [sqlite3_index_info] object. This is the default expectation. If the
9867 ** virtual table outputs all rows in sorted order, then it is always safe for
9868 ** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9869 ** the return value from sqlite3_vtab_distinct().
9870 ** <li value="1"><p>
9871 ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
9872 ** that the query planner does not need the rows to be returned in sorted order
9873 ** as long as all rows with the same values in all columns identified by the
9874 ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9842,11 +9877,11 @@
9877 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9878 ** that the query planner does not need the rows returned in any particular
9879 ** order, as long as rows with the same values in all "aOrderBy" columns
9880 ** are adjacent.)^ ^(Furthermore, only a single row for each particular
9881 ** combination of values in the columns identified by the "aOrderBy" field
9882 ** needs to be returned.)^ ^It is always ok for two or more rows with the same
9883 ** values in all "aOrderBy" columns to be returned, as long as all such rows
9884 ** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9885 ** that have the same value for all columns identified by "aOrderBy".
9886 ** ^However omitting the extra rows is optional.
9887 ** This mode is used for a DISTINCT query.
@@ -9877,31 +9912,45 @@
9912
9913 /*
9914 ** CAPI3REF: Constraint values in xBestIndex()
9915 ** METHOD: sqlite3_index_info
9916 **
9917 ** This API may only be used from within the [xBestIndex|xBestIndex method]
9918 ** of a [virtual table] implementation. The result of calling this interface
9919 ** from outside of an xBestIndex method are undefined and probably harmful.
9920 **
9921 ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
9922 ** the [xBestIndex] method of a [virtual table] implementation, with P being
9923 ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
9924 ** J being a 0-based index into P->aConstraint[], then this routine
9925 ** attempts to set *V to the value of the right-hand operand of
9926 ** that constraint if the right-hand operand is known. ^If the
9927 ** right-hand operand is not known, then *V is set to a NULL pointer.
9928 ** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9929 ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
9930 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
9931 ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
9932 ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
9933 ** something goes wrong.
9934 **
9935 ** The sqlite3_vtab_rhs_value() interface is usually only successful if
9936 ** the right-hand operand of a constraint is a literal value in the original
9937 ** SQL statement. If the right-hand operand is an expression or a reference
9938 ** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
9939 ** will probably return [SQLITE_NOTFOUND].
9940 **
9941 ** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
9942 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
9943 ** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
9944 **
9945 ** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
9946 ** and remains valid for the duration of the xBestIndex method call.
9947 ** ^When xBestIndex returns, the sqlite3_value object returned by
9948 ** sqlite3_vtab_rhs_value() is automatically deallocated.
9949 **
9950 ** The "_rhs_" in the name of this routine is an appreviation for
9951 ** "Right-Hand Side".
9952 */
9953 SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
9954
9955 /*
9956 ** CAPI3REF: Conflict resolution modes
@@ -19662,11 +19711,12 @@
19711 #endif
19712 SQLITE_PRIVATE void sqlite3CodeChangeCount(Vdbe*,int,const char*);
19713 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
19714 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
19715 Upsert*);
19716 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,
19717 ExprList*,Select*,u16,int);
19718 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
19719 SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
19720 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
19721 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
19722 SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
@@ -23545,11 +23595,11 @@
23595 ** is not the first modifier, or if the prior argument is not a numeric
23596 ** value in the allowed range of julian day numbers understood by
23597 ** SQLite (0..5373484.5) then the result will be NULL.
23598 */
23599 if( sqlite3_stricmp(z, "julianday")==0 ){
23600 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
23601 if( p->validJD && p->rawS ){
23602 rc = 0;
23603 p->rawS = 0;
23604 }
23605 }
@@ -23576,10 +23626,11 @@
23626 **
23627 ** Treat the current value of p->s as the number of
23628 ** seconds since 1970. Convert to a real julian day number.
23629 */
23630 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
23631 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
23632 r = p->s*1000.0 + 210866760000000.0;
23633 if( r>=0.0 && r<464269060800000.0 ){
23634 clearYMD_HMS_TZ(p);
23635 p->iJD = (sqlite3_int64)(r + 0.5);
23636 p->validJD = 1;
@@ -76814,11 +76865,11 @@
76865 int i = sqlite3FindDbName(pDb, zDb);
76866
76867 if( i==1 ){
76868 Parse sParse;
76869 int rc = 0;
76870 sqlite3ParseObjectInit(&sParse,pDb);
76871 if( sqlite3OpenTempDatabase(&sParse) ){
76872 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
76873 rc = SQLITE_ERROR;
76874 }
76875 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
@@ -79032,15 +79083,11 @@
79083 const char *zNeg = "";
79084 int rc = SQLITE_OK;
79085
79086 assert( pExpr!=0 );
79087 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
 
79088 if( op==TK_REGISTER ) op = pExpr->op2;
 
 
 
79089
79090 /* Compressed expressions only appear when parsing the DEFAULT clause
79091 ** on a table column definition, and hence only when pCtx==0. This
79092 ** check ensures that an EP_TokenOnly expression is never passed down
79093 ** into valueFromFunction(). */
@@ -120069,11 +120116,11 @@
120116 **
120117 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
120118 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
120119 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
120120 */
120121 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1);
120122 if( pWInfo==0 ) goto delete_from_cleanup;
120123 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
120124 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
120125 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
120126 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
@@ -123612,11 +123659,11 @@
123659
123660 /* Create VDBE to loop through the entries in pSrc that match the WHERE
123661 ** clause. For each row found, increment either the deferred or immediate
123662 ** foreign key constraint counter. */
123663 if( pParse->nErr==0 ){
123664 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0);
123665 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
123666 if( pWInfo ){
123667 sqlite3WhereEnd(pWInfo);
123668 }
123669 }
@@ -133324,10 +133371,11 @@
133371 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
133372 assert( db->pParse!=pParse );
133373 pParse->pOuterParse = db->pParse;
133374 db->pParse = pParse;
133375 pParse->db = db;
133376 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
133377 }
133378
133379 /*
133380 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
133381 */
@@ -133350,11 +133398,11 @@
133398 sParse.pOuterParse = db->pParse;
133399 db->pParse = &sParse;
133400 sParse.db = db;
133401 sParse.pReprepare = pReprepare;
133402 assert( ppStmt && *ppStmt==0 );
133403 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
133404 assert( sqlite3_mutex_held(db->mutex) );
133405
133406 /* For a long-term use prepared statement avoid the use of
133407 ** lookaside memory.
133408 */
@@ -140618,11 +140666,11 @@
140666
140667
140668 /* Begin the database scan. */
140669 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140670 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
140671 p->pEList, p, wctrlFlags, p->nSelectRow);
140672 if( pWInfo==0 ) goto select_end;
140673 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
140674 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
140675 }
140676 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
@@ -140882,11 +140930,11 @@
140930 ** in the right order to begin with.
140931 */
140932 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
140933 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
140934 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct,
140935 0, (WHERE_GROUPBY|(orderByGrp ? WHERE_SORTBYGROUP : 0)|distFlag), 0
140936 );
140937 if( pWInfo==0 ){
140938 sqlite3ExprListDelete(db, pDistinct);
140939 goto select_end;
140940 }
@@ -141180,11 +141228,11 @@
141228 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
141229 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
141230
141231 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
141232 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
141233 pDistinct, 0, minMaxFlag|distFlag, 0);
141234 if( pWInfo==0 ){
141235 goto select_end;
141236 }
141237 SELECTTRACE(1,pParse,p,("WhereBegin returns\n"));
141238 eDist = sqlite3WhereIsDistinct(pWInfo);
@@ -143623,11 +143671,11 @@
143671 ** or index, causing a single-pass approach to malfunction. */
143672 flags = WHERE_ONEPASS_DESIRED;
143673 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
143674 flags |= WHERE_ONEPASS_MULTIROW;
143675 }
143676 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur);
143677 if( pWInfo==0 ) goto update_cleanup;
143678
143679 /* A one-pass strategy that might update more than one row may not
143680 ** be used if any column of the index used for the scan is being
143681 ** updated. Otherwise, if there is an index on "b", statements like
@@ -144145,11 +144193,13 @@
144193 }else{
144194 regRec = ++pParse->nMem;
144195 regRowid = ++pParse->nMem;
144196
144197 /* Start scanning the virtual table */
144198 pWInfo = sqlite3WhereBegin(
144199 pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0
144200 );
144201 if( pWInfo==0 ) return;
144202
144203 /* Populate the argument registers. */
144204 for(i=0; i<pTab->nCol; i++){
144205 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
@@ -146456,11 +146506,12 @@
146506 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
146507 Index *pIndex; /* Index used, or NULL */
146508 } btree;
146509 struct { /* Information for virtual tables */
146510 int idxNum; /* Index number */
146511 u8 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
146512 u8 bOmitOffset : 1; /* True to let virtual table handle offset */
146513 i8 isOrdered; /* True if satisfies ORDER BY */
146514 u16 omitMask; /* Terms that may be omitted */
146515 char *idxStr; /* Index identifier string */
146516 } vtab;
146517 } u;
@@ -146786,10 +146837,13 @@
146837 Parse *pParse; /* Parsing and code generating context */
146838 SrcList *pTabList; /* List of tables in the join */
146839 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
146840 ExprList *pResultSet; /* Result set of the query */
146841 Expr *pWhere; /* The complete WHERE clause */
146842 #ifndef SQLITE_OMIT_VIRTUALTABLE
146843 Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
146844 #endif
146845 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
146846 int iContinue; /* Jump here to continue with next record */
146847 int iBreak; /* Jump here to break out of the loop */
146848 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
146849 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
@@ -146871,10 +146925,11 @@
146925
146926 /* whereexpr.c: */
146927 SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
146928 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
146929 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
146930 SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause*, Select*);
146931 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
146932 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
146933 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
146934 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
146935 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
@@ -146941,10 +146996,11 @@
146996 #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
146997 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
146998 #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
146999 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
147000 #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
147001 #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
147002
147003 #endif /* !defined(SQLITE_WHEREINT_H) */
147004
147005 /************** End of whereInt.h ********************************************/
147006 /************** Continuing where we left off in wherecode.c ******************/
@@ -148464,10 +148520,19 @@
148520 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
148521 addrNotFound = pLevel->addrNxt;
148522 }else{
148523 Expr *pRight = pTerm->pExpr->pRight;
148524 codeExprOrVector(pParse, pRight, iTarget, 1);
148525 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
148526 && pLoop->u.vtab.bOmitOffset
148527 ){
148528 assert( pTerm->eOperator==WO_AUX );
148529 assert( pWInfo->pLimit!=0 );
148530 assert( pWInfo->pLimit->iOffset>0 );
148531 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
148532 VdbeComment((v,"Zero OFFSET counter"));
148533 }
148534 }
148535 }
148536 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
148537 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
148538 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
@@ -149285,11 +149350,11 @@
149350 pOrExpr = pAndExpr;
149351 }
149352 /* Loop through table entries that match term pOrTerm. */
149353 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
149354 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
149355 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
149356 WHERE_OR_SUBCLAUSE, iCovCur);
149357 assert( pSubWInfo || pParse->nErr );
149358 if( pSubWInfo ){
149359 WhereLoop *pSubLoop;
149360 int addrExplain = sqlite3WhereExplainOneScan(
@@ -150982,11 +151047,14 @@
151047 ** new terms for each component comparison - "a = ?" and "b = ?". The
151048 ** new terms completely replace the original vector comparison, which is
151049 ** no longer used.
151050 **
151051 ** This is only required if at least one side of the comparison operation
151052 ** is not a sub-select.
151053 **
151054 ** tag-20220128a
151055 */
151056 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
151057 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
151058 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
151059 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
151060 || (pExpr->pRight->flags & EP_xIsSelect)==0)
@@ -151124,10 +151192,117 @@
151192 }else{
151193 sqlite3WhereSplit(pWC, pE2->pLeft, op);
151194 sqlite3WhereSplit(pWC, pE2->pRight, op);
151195 }
151196 }
151197
151198 /*
151199 ** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
151200 ** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
151201 ** where-clause passed as the first argument. The value for the term
151202 ** is found in register iReg.
151203 **
151204 ** In the common case where the value is a simple integer
151205 ** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
151206 ** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
151207 ** If not, then it codes as a TK_REGISTER expression.
151208 */
151209 void whereAddLimitExpr(
151210 WhereClause *pWC, /* Add the constraint to this WHERE clause */
151211 int iReg, /* Register that will hold value of the limit/offset */
151212 Expr *pExpr, /* Expression that defines the limit/offset */
151213 int iCsr, /* Cursor to which the constraint applies */
151214 int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */
151215 ){
151216 Parse *pParse = pWC->pWInfo->pParse;
151217 sqlite3 *db = pParse->db;
151218 Expr *pNew;
151219 int iVal = 0;
151220
151221 if( sqlite3ExprIsInteger(pExpr, &iVal) && iVal>=0 ){
151222 Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0);
151223 if( pVal==0 ) return;
151224 ExprSetProperty(pVal, EP_IntValue);
151225 pVal->u.iValue = iVal;
151226 pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
151227 }else{
151228 Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0);
151229 if( pVal==0 ) return;
151230 pVal->iTable = iReg;
151231 pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal);
151232 }
151233 if( pNew ){
151234 WhereTerm *pTerm;
151235 int idx;
151236 idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL);
151237 pTerm = &pWC->a[idx];
151238 pTerm->leftCursor = iCsr;
151239 pTerm->eOperator = WO_AUX;
151240 pTerm->eMatchOp = eMatchOp;
151241 }
151242 }
151243
151244 /*
151245 ** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
151246 ** SELECT statement passed as the second argument. These terms are only
151247 ** added if:
151248 **
151249 ** 1. The SELECT statement has a LIMIT clause, and
151250 ** 2. The SELECT statement is not an aggregate or DISTINCT query, and
151251 ** 3. The SELECT statement has exactly one object in its from clause, and
151252 ** that object is a virtual table, and
151253 ** 4. There are no terms in the WHERE clause that will not be passed
151254 ** to the virtual table xBestIndex method.
151255 ** 5. The ORDER BY clause, if any, will be made available to the xBestIndex
151256 ** method.
151257 **
151258 ** LIMIT and OFFSET terms are ignored by most of the planner code. They
151259 ** exist only so that they may be passed to the xBestIndex method of the
151260 ** single virtual table in the FROM clause of the SELECT.
151261 */
151262 SQLITE_PRIVATE void sqlite3WhereAddLimit(WhereClause *pWC, Select *p){
151263 assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
151264 if( (p && p->pLimit) /* 1 */
151265 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
151266 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
151267 ){
151268 ExprList *pOrderBy = p->pOrderBy;
151269 int iCsr = p->pSrc->a[0].iCursor;
151270 int ii;
151271
151272 /* Check condition (4). Return early if it is not met. */
151273 for(ii=0; ii<pWC->nTerm; ii++){
151274 if( pWC->a[ii].wtFlags & TERM_CODED ){
151275 /* This term is a vector operation that has been decomposed into
151276 ** other, subsequent terms. It can be ignored. See tag-20220128a */
151277 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
151278 assert( pWC->a[ii].eOperator==0 );
151279 continue;
151280 }
151281 if( pWC->a[ii].leftCursor!=iCsr ) return;
151282 }
151283
151284 /* Check condition (5). Return early if it is not met. */
151285 if( pOrderBy ){
151286 for(ii=0; ii<pOrderBy->nExpr; ii++){
151287 Expr *pExpr = pOrderBy->a[ii].pExpr;
151288 if( pExpr->op!=TK_COLUMN ) return;
151289 if( pExpr->iTable!=iCsr ) return;
151290 if( pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_BIGNULL ) return;
151291 }
151292 }
151293
151294 /* All conditions are met. Add the terms to the where-clause object. */
151295 assert( p->pLimit->op==TK_LIMIT );
151296 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
151297 iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT);
151298 if( p->iOffset>0 ){
151299 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
151300 iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET);
151301 }
151302 }
151303 }
151304
151305 /*
151306 ** Initialize a preallocated WhereClause structure.
151307 */
151308 SQLITE_PRIVATE void sqlite3WhereClauseInit(
@@ -151160,10 +151335,11 @@
151335 for(i=pWC->nBase; i<pWC->nTerm; i++){
151336 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
151337 }
151338 #endif
151339 while(1){
151340 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
151341 if( a->wtFlags & TERM_DYNAMIC ){
151342 sqlite3ExprDelete(db, a->pExpr);
151343 }
151344 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
151345 if( a->wtFlags & TERM_ORINFO ){
@@ -151317,10 +151493,11 @@
151493 if( pColRef==0 ) return;
151494 pColRef->iTable = pItem->iCursor;
151495 pColRef->iColumn = k++;
151496 assert( ExprUseYTab(pColRef) );
151497 pColRef->y.pTab = pTab;
151498 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
151499 pRhs = sqlite3PExpr(pParse, TK_UPLUS,
151500 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
151501 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
151502 if( pItem->fg.jointype & JT_LEFT ){
151503 sqlite3SetJoinExpr(pTerm, pItem->iCursor);
@@ -154789,10 +154966,19 @@
154966 }
154967 return rc;
154968 }
154969
154970 #ifndef SQLITE_OMIT_VIRTUALTABLE
154971
154972 /*
154973 ** Return true if pTerm is a virtual table LIMIT or OFFSET term.
154974 */
154975 static int isLimitTerm(WhereTerm *pTerm){
154976 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
154977 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
154978 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
154979 }
154980
154981 /*
154982 ** Argument pIdxInfo is already populated with all constraints that may
154983 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
154984 ** function marks a subset of those constraints usable, invokes the
@@ -154817,11 +155003,12 @@
155003 Bitmask mPrereq, /* Mask of tables that must be used. */
155004 Bitmask mUsable, /* Mask of usable tables */
155005 u16 mExclude, /* Exclude terms using these operators */
155006 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
155007 u16 mNoOmit, /* Do not omit these constraints */
155008 int *pbIn, /* OUT: True if plan uses an IN(...) op */
155009 int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */
155010 ){
155011 WhereClause *pWC = pBuilder->pWC;
155012 struct sqlite3_index_constraint *pIdxCons;
155013 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
155014 int i;
@@ -154842,10 +155029,11 @@
155029 for(i=0; i<nConstraint; i++, pIdxCons++){
155030 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
155031 pIdxCons->usable = 0;
155032 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
155033 && (pTerm->eOperator & mExclude)==0
155034 && (pbRetryLimit || !isLimitTerm(pTerm))
155035 ){
155036 pIdxCons->usable = 1;
155037 }
155038 }
155039
@@ -154874,12 +155062,12 @@
155062 return rc;
155063 }
155064
155065 mxTerm = -1;
155066 assert( pNew->nLSlot>=nConstraint );
155067 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
155068 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
155069 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
155070 for(i=0; i<nConstraint; i++, pIdxCons++){
155071 int iTerm;
155072 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
155073 WhereTerm *pTerm;
@@ -154909,10 +155097,13 @@
155097 testcase( i!=iTerm );
155098 pNew->u.vtab.omitMask |= 1<<iTerm;
155099 }else{
155100 testcase( i!=iTerm );
155101 }
155102 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
155103 pNew->u.vtab.bOmitOffset = 1;
155104 }
155105 }
155106 if( (pTerm->eOperator & WO_IN)!=0 ){
155107 /* A virtual table that is constrained by an IN clause may not
155108 ** consume the ORDER BY clause because (1) the order of IN terms
155109 ** is not necessarily related to the order of output terms and
@@ -154920,10 +155111,25 @@
155111 ** together. */
155112 pIdxInfo->orderByConsumed = 0;
155113 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
155114 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
155115 }
155116
155117 if( isLimitTerm(pTerm) && *pbIn ){
155118 /* If there is an IN(...) term handled as an == (separate call to
155119 ** xFilter for each value on the RHS of the IN) and a LIMIT or
155120 ** OFFSET term handled as well, the plan is unusable. Set output
155121 ** variable *pbRetryLimit to true to tell the caller to retry with
155122 ** LIMIT and OFFSET disabled. */
155123 if( pIdxInfo->needToFreeIdxStr ){
155124 sqlite3_free(pIdxInfo->idxStr);
155125 pIdxInfo->idxStr = 0;
155126 pIdxInfo->needToFreeIdxStr = 0;
155127 }
155128 *pbRetryLimit = 1;
155129 return SQLITE_OK;
155130 }
155131 }
155132 }
155133
155134 pNew->nLTerm = mxTerm+1;
155135 for(i=0; i<=mxTerm; i++){
@@ -155082,10 +155288,11 @@
155288 int nConstraint; /* Number of constraints in p */
155289 int bIn; /* True if plan uses IN(...) operator */
155290 WhereLoop *pNew;
155291 Bitmask mBest; /* Tables used by best possible plan */
155292 u16 mNoOmit;
155293 int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */
155294
155295 assert( (mPrereq & mUnusable)==0 );
155296 pWInfo = pBuilder->pWInfo;
155297 pParse = pWInfo->pParse;
155298 pWC = pBuilder->pWC;
@@ -155105,11 +155312,19 @@
155312 }
155313
155314 /* First call xBestIndex() with all constraints usable. */
155315 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
155316 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
155317 rc = whereLoopAddVirtualOne(
155318 pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry
155319 );
155320 if( bRetry ){
155321 assert( rc==SQLITE_OK );
155322 rc = whereLoopAddVirtualOne(
155323 pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0
155324 );
155325 }
155326
155327 /* If the call to xBestIndex() with all terms enabled produced a plan
155328 ** that does not require any source tables (IOW: a plan with mBest==0)
155329 ** and does not use an IN(...) operator, then there is no point in making
155330 ** any further calls to xBestIndex() since they will all return the same
@@ -155123,11 +155338,11 @@
155338 /* If the plan produced by the earlier call uses an IN(...) term, call
155339 ** xBestIndex again, this time with IN(...) terms disabled. */
155340 if( bIn ){
155341 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
155342 rc = whereLoopAddVirtualOne(
155343 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0);
155344 assert( bIn==0 );
155345 mBestNoIn = pNew->prereq & ~mPrereq;
155346 if( mBestNoIn==0 ){
155347 seenZero = 1;
155348 seenZeroNoIN = 1;
@@ -155150,11 +155365,11 @@
155365 if( mNext==ALLBITS ) break;
155366 if( mNext==mBest || mNext==mBestNoIn ) continue;
155367 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
155368 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
155369 rc = whereLoopAddVirtualOne(
155370 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0);
155371 if( pNew->prereq==mPrereq ){
155372 seenZero = 1;
155373 if( bIn==0 ) seenZeroNoIN = 1;
155374 }
155375 }
@@ -155163,21 +155378,21 @@
155378 ** that requires no source tables at all (i.e. one guaranteed to be
155379 ** usable), make a call here with all source tables disabled */
155380 if( rc==SQLITE_OK && seenZero==0 ){
155381 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
155382 rc = whereLoopAddVirtualOne(
155383 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0);
155384 if( bIn==0 ) seenZeroNoIN = 1;
155385 }
155386
155387 /* If the calls to xBestIndex() have so far failed to find a plan
155388 ** that requires no source tables at all and does not use an IN(...)
155389 ** operator, make a final call to obtain one here. */
155390 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
155391 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
155392 rc = whereLoopAddVirtualOne(
155393 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0);
155394 }
155395 }
155396
155397 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
155398 freeIndexInfo(pParse->db, p);
@@ -156579,10 +156794,11 @@
156794 Parse *pParse, /* The parser context */
156795 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
156796 Expr *pWhere, /* The WHERE clause */
156797 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
156798 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
156799 Select *pLimit, /* Use this LIMIT/OFFSET clause, if any */
156800 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
156801 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
156802 ** If WHERE_USE_LIMIT, then the limit amount */
156803 ){
156804 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
@@ -156655,10 +156871,13 @@
156871 pWInfo->nLevel = nTabList;
156872 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
156873 pWInfo->wctrlFlags = wctrlFlags;
156874 pWInfo->iLimit = iAuxArg;
156875 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
156876 #ifndef SQLITE_OMIT_VIRTUALTABLE
156877 pWInfo->pLimit = pLimit;
156878 #endif
156879 memset(&pWInfo->nOBSat, 0,
156880 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
156881 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
156882 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
156883 pMaskSet = &pWInfo->sMaskSet;
@@ -156723,10 +156942,11 @@
156942 #endif
156943 }
156944
156945 /* Analyze all of the subexpressions. */
156946 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
156947 sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
156948 if( db->mallocFailed ) goto whereBeginError;
156949
156950 /* Special case: WHERE terms that do not refer to any tables in the join
156951 ** (constant expressions). Evaluate each such term, and jump over all the
156952 ** generated code if the result is not true.
@@ -233598,11 +233818,11 @@
233818 int nArg, /* Number of args */
233819 sqlite3_value **apUnused /* Function arguments */
233820 ){
233821 assert( nArg==0 );
233822 UNUSED_PARAM2(nArg, apUnused);
233823 sqlite3_result_text(pCtx, "fts5: 2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808", -1, SQLITE_TRANSIENT);
233824 }
233825
233826 /*
233827 ** Return true if zName is the extension on one of the shadow tables used
233828 ** by this module.
233829
+86 -37
--- 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.38.0"
150150
#define SQLITE_VERSION_NUMBER 3038000
151
-#define SQLITE_SOURCE_ID "2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17"
151
+#define SQLITE_SOURCE_ID "2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -4350,10 +4350,12 @@
43504350
** still make the distinction between protected and unprotected
43514351
** sqlite3_value objects even when not strictly required.
43524352
**
43534353
** ^The sqlite3_value objects that are passed as parameters into the
43544354
** implementation of [application-defined SQL functions] are protected.
4355
+** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4356
+** are protected.
43554357
** ^The sqlite3_value object returned by
43564358
** [sqlite3_column_value()] is unprotected.
43574359
** Unprotected sqlite3_value objects may only be used as arguments
43584360
** to [sqlite3_result_value()], [sqlite3_bind_value()], and
43594361
** [sqlite3_value_dup()].
@@ -7129,28 +7131,60 @@
71297131
/*
71307132
** CAPI3REF: Virtual Table Constraint Operator Codes
71317133
**
71327134
** These macros define the allowed values for the
71337135
** [sqlite3_index_info].aConstraint[].op field. Each value represents
7134
-** an operator that is part of a constraint term in the wHERE clause of
7136
+** an operator that is part of a constraint term in the WHERE clause of
71357137
** a query that uses a [virtual table].
7138
+**
7139
+** ^The left-hand operand of the operator is given by the corresponding
7140
+** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7141
+** operand is the rowid.
7142
+** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7143
+** operators have no left-hand operand, and so for those operators the
7144
+** corresponding aConstraint[].iColumn is meaningless and should not be
7145
+** used.
7146
+**
7147
+** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7148
+** value 255 are reserved to represent functions that are overloaded
7149
+** by the [xFindFunction|xFindFunction method] of the virtual table
7150
+** implementation.
7151
+**
7152
+** The right-hand operands for each constraint might be accessible using
7153
+** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7154
+** operand is only available if it appears as a single constant literal
7155
+** in the input SQL. If the right-hand operand is another column or an
7156
+** expression (even a constant expression) or a parameter, then the
7157
+** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7158
+** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7159
+** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7160
+** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7161
+** always return SQLITE_NOTFOUND.
7162
+**
7163
+** The collating sequence to be used for comparison can be found using
7164
+** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7165
+** tables, the collating sequence of constraints does not matter (for example
7166
+** because the constraints are numeric) and so the sqlite3_vtab_collation()
7167
+** interface is no commonly needed.
71367168
*/
7137
-#define SQLITE_INDEX_CONSTRAINT_EQ 2
7138
-#define SQLITE_INDEX_CONSTRAINT_GT 4
7139
-#define SQLITE_INDEX_CONSTRAINT_LE 8
7140
-#define SQLITE_INDEX_CONSTRAINT_LT 16
7141
-#define SQLITE_INDEX_CONSTRAINT_GE 32
7142
-#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7143
-#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7144
-#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7145
-#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7146
-#define SQLITE_INDEX_CONSTRAINT_NE 68
7147
-#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7148
-#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7149
-#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7150
-#define SQLITE_INDEX_CONSTRAINT_IS 72
7151
-#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7169
+#define SQLITE_INDEX_CONSTRAINT_EQ 2
7170
+#define SQLITE_INDEX_CONSTRAINT_GT 4
7171
+#define SQLITE_INDEX_CONSTRAINT_LE 8
7172
+#define SQLITE_INDEX_CONSTRAINT_LT 16
7173
+#define SQLITE_INDEX_CONSTRAINT_GE 32
7174
+#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7175
+#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7176
+#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7177
+#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7178
+#define SQLITE_INDEX_CONSTRAINT_NE 68
7179
+#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7180
+#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7181
+#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7182
+#define SQLITE_INDEX_CONSTRAINT_IS 72
7183
+#define SQLITE_INDEX_CONSTRAINT_LIMIT 73
7184
+#define SQLITE_INDEX_CONSTRAINT_OFFSET 74
7185
+#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
71527186
71537187
/*
71547188
** CAPI3REF: Register A Virtual Table Implementation
71557189
** METHOD: sqlite3
71567190
**
@@ -9506,28 +9540,29 @@
95069540
95079541
/*
95089542
** CAPI3REF: Determine if a virtual table query is DISTINCT
95099543
** METHOD: sqlite3_index_info
95109544
**
9511
-** This API may only be used from within an xBestIndex() callback. The
9512
-** results of calling it from outside of an xBestIndex() callback are
9513
-** undefined and probably harmful.
9545
+** This API may only be used from within an [xBestIndex|xBestIndex method]
9546
+** of a [virtual table] implementation. The result of calling this
9547
+** interface from outside of xBestIndex() is undefined and probably harmful.
95149548
**
9515
-** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or
9516
-** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table
9517
-** additional information about how the query planner wants the output to be
9518
-** ordered. As long as the virtual table can meet the ordering requirements
9519
-** of the query planner, it may set the "orderByConsumed" flag.
9549
+** ^The sqlite3_vtab_distinct() interface returns an integer that is
9550
+** either 0, 1, or 2. The integer returned by sqlite3_vtab_distinct()
9551
+** gives the virtual table additional information about how the query
9552
+** planner wants the output to be ordered. As long as the virtual table
9553
+** can meet the ordering requirements of the query planner, it may set
9554
+** the "orderByConsumed" flag.
95209555
**
95219556
** <ol><li value="0"><p>
95229557
** ^If the sqlite3_vtab_distinct() interface returns 0, that means
95239558
** that the query planner needs the virtual table to return all rows in the
95249559
** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
95259560
** [sqlite3_index_info] object. This is the default expectation. If the
95269561
** virtual table outputs all rows in sorted order, then it is always safe for
95279562
** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9528
-** what the return value from sqlite3_vtab_distinct().
9563
+** the return value from sqlite3_vtab_distinct().
95299564
** <li value="1"><p>
95309565
** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
95319566
** that the query planner does not need the rows to be returned in sorted order
95329567
** as long as all rows with the same values in all columns identified by the
95339568
** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9536,11 +9571,11 @@
95369571
** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
95379572
** that the query planner does not need the rows returned in any particular
95389573
** order, as long as rows with the same values in all "aOrderBy" columns
95399574
** are adjacent.)^ ^(Furthermore, only a single row for each particular
95409575
** combination of values in the columns identified by the "aOrderBy" field
9541
-** needs to be returned.)^ ^It is ok always for two or more rows with the same
9576
+** needs to be returned.)^ ^It is always ok for two or more rows with the same
95429577
** values in all "aOrderBy" columns to be returned, as long as all such rows
95439578
** are adjacent. ^The virtual table may, if it chooses, omit extra rows
95449579
** that have the same value for all columns identified by "aOrderBy".
95459580
** ^However omitting the extra rows is optional.
95469581
** This mode is used for a DISTINCT query.
@@ -9571,31 +9606,45 @@
95719606
95729607
/*
95739608
** CAPI3REF: Constraint values in xBestIndex()
95749609
** METHOD: sqlite3_index_info
95759610
**
9576
-** This API may only be used from within an xBestIndex() callback. The
9577
-** results of calling it from outside of an xBestIndex() callback are
9578
-** undefined and probably harmful.
9611
+** This API may only be used from within the [xBestIndex|xBestIndex method]
9612
+** of a [virtual table] implementation. The result of calling this interface
9613
+** from outside of an xBestIndex method are undefined and probably harmful.
95799614
**
95809615
** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
95819616
** the [xBestIndex] method of a [virtual table] implementation, with P being
95829617
** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
95839618
** J being a 0-based index into P->aConstraint[], then this routine
9584
-** attempts to set *V to be the value on the right-hand side of
9585
-** that constraint if the right-hand side is a known constant. ^If the
9586
-** right-hand side of the constraint is not known, then *V is set to a NULL
9587
-** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9619
+** attempts to set *V to the value of the right-hand operand of
9620
+** that constraint if the right-hand operand is known. ^If the
9621
+** right-hand operand is not known, then *V is set to a NULL pointer.
9622
+** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
95889623
** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
95899624
** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
95909625
** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
95919626
** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
95929627
** something goes wrong.
95939628
**
9594
-** ^The sqlite3_value object returned in *V remains valid for the duration of
9595
-** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value
9596
-** object returned by sqlite3_vtab_rhs_value() is automatically deallocated.
9629
+** The sqlite3_vtab_rhs_value() interface is usually only successful if
9630
+** the right-hand operand of a constraint is a literal value in the original
9631
+** SQL statement. If the right-hand operand is an expression or a reference
9632
+** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
9633
+** will probably return [SQLITE_NOTFOUND].
9634
+**
9635
+** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
9636
+** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
9637
+** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
9638
+**
9639
+** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
9640
+** and remains valid for the duration of the xBestIndex method call.
9641
+** ^When xBestIndex returns, the sqlite3_value object returned by
9642
+** sqlite3_vtab_rhs_value() is automatically deallocated.
9643
+**
9644
+** The "_rhs_" in the name of this routine is an appreviation for
9645
+** "Right-Hand Side".
95979646
*/
95989647
SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
95999648
96009649
/*
96019650
** CAPI3REF: Conflict resolution modes
96029651
--- 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.38.0"
150 #define SQLITE_VERSION_NUMBER 3038000
151 #define SQLITE_SOURCE_ID "2022-01-25 00:03:25 a8db69411b0d1275909adeb21027784ada17af24efe3a59ae0ae2a897659ff17"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -4350,10 +4350,12 @@
4350 ** still make the distinction between protected and unprotected
4351 ** sqlite3_value objects even when not strictly required.
4352 **
4353 ** ^The sqlite3_value objects that are passed as parameters into the
4354 ** implementation of [application-defined SQL functions] are protected.
 
 
4355 ** ^The sqlite3_value object returned by
4356 ** [sqlite3_column_value()] is unprotected.
4357 ** Unprotected sqlite3_value objects may only be used as arguments
4358 ** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4359 ** [sqlite3_value_dup()].
@@ -7129,28 +7131,60 @@
7129 /*
7130 ** CAPI3REF: Virtual Table Constraint Operator Codes
7131 **
7132 ** These macros define the allowed values for the
7133 ** [sqlite3_index_info].aConstraint[].op field. Each value represents
7134 ** an operator that is part of a constraint term in the wHERE clause of
7135 ** a query that uses a [virtual table].
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7136 */
7137 #define SQLITE_INDEX_CONSTRAINT_EQ 2
7138 #define SQLITE_INDEX_CONSTRAINT_GT 4
7139 #define SQLITE_INDEX_CONSTRAINT_LE 8
7140 #define SQLITE_INDEX_CONSTRAINT_LT 16
7141 #define SQLITE_INDEX_CONSTRAINT_GE 32
7142 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
7143 #define SQLITE_INDEX_CONSTRAINT_LIKE 65
7144 #define SQLITE_INDEX_CONSTRAINT_GLOB 66
7145 #define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7146 #define SQLITE_INDEX_CONSTRAINT_NE 68
7147 #define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7148 #define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7149 #define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7150 #define SQLITE_INDEX_CONSTRAINT_IS 72
7151 #define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
 
 
7152
7153 /*
7154 ** CAPI3REF: Register A Virtual Table Implementation
7155 ** METHOD: sqlite3
7156 **
@@ -9506,28 +9540,29 @@
9506
9507 /*
9508 ** CAPI3REF: Determine if a virtual table query is DISTINCT
9509 ** METHOD: sqlite3_index_info
9510 **
9511 ** This API may only be used from within an xBestIndex() callback. The
9512 ** results of calling it from outside of an xBestIndex() callback are
9513 ** undefined and probably harmful.
9514 **
9515 ** ^The sqlite3_vtab_distinct() returns an integer that is either 0, 1, or
9516 ** 2. The integer returned by sqlite3_vtab_distinct() gives the virtual table
9517 ** additional information about how the query planner wants the output to be
9518 ** ordered. As long as the virtual table can meet the ordering requirements
9519 ** of the query planner, it may set the "orderByConsumed" flag.
 
9520 **
9521 ** <ol><li value="0"><p>
9522 ** ^If the sqlite3_vtab_distinct() interface returns 0, that means
9523 ** that the query planner needs the virtual table to return all rows in the
9524 ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
9525 ** [sqlite3_index_info] object. This is the default expectation. If the
9526 ** virtual table outputs all rows in sorted order, then it is always safe for
9527 ** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9528 ** what the return value from sqlite3_vtab_distinct().
9529 ** <li value="1"><p>
9530 ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
9531 ** that the query planner does not need the rows to be returned in sorted order
9532 ** as long as all rows with the same values in all columns identified by the
9533 ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9536,11 +9571,11 @@
9536 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9537 ** that the query planner does not need the rows returned in any particular
9538 ** order, as long as rows with the same values in all "aOrderBy" columns
9539 ** are adjacent.)^ ^(Furthermore, only a single row for each particular
9540 ** combination of values in the columns identified by the "aOrderBy" field
9541 ** needs to be returned.)^ ^It is ok always for two or more rows with the same
9542 ** values in all "aOrderBy" columns to be returned, as long as all such rows
9543 ** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9544 ** that have the same value for all columns identified by "aOrderBy".
9545 ** ^However omitting the extra rows is optional.
9546 ** This mode is used for a DISTINCT query.
@@ -9571,31 +9606,45 @@
9571
9572 /*
9573 ** CAPI3REF: Constraint values in xBestIndex()
9574 ** METHOD: sqlite3_index_info
9575 **
9576 ** This API may only be used from within an xBestIndex() callback. The
9577 ** results of calling it from outside of an xBestIndex() callback are
9578 ** undefined and probably harmful.
9579 **
9580 ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
9581 ** the [xBestIndex] method of a [virtual table] implementation, with P being
9582 ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
9583 ** J being a 0-based index into P->aConstraint[], then this routine
9584 ** attempts to set *V to be the value on the right-hand side of
9585 ** that constraint if the right-hand side is a known constant. ^If the
9586 ** right-hand side of the constraint is not known, then *V is set to a NULL
9587 ** pointer. ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9588 ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
9589 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
9590 ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
9591 ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
9592 ** something goes wrong.
9593 **
9594 ** ^The sqlite3_value object returned in *V remains valid for the duration of
9595 ** the xBestIndex method code. ^When xBestIndex returns, the sqlite3_value
9596 ** object returned by sqlite3_vtab_rhs_value() is automatically deallocated.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9597 */
9598 SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
9599
9600 /*
9601 ** CAPI3REF: Conflict resolution modes
9602
--- 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.38.0"
150 #define SQLITE_VERSION_NUMBER 3038000
151 #define SQLITE_SOURCE_ID "2022-01-31 14:14:29 539cef5214446a7181614793e9cf323e95ba00ba0f888585b14b598dd2ff0808"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -4350,10 +4350,12 @@
4350 ** still make the distinction between protected and unprotected
4351 ** sqlite3_value objects even when not strictly required.
4352 **
4353 ** ^The sqlite3_value objects that are passed as parameters into the
4354 ** implementation of [application-defined SQL functions] are protected.
4355 ** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()]
4356 ** are protected.
4357 ** ^The sqlite3_value object returned by
4358 ** [sqlite3_column_value()] is unprotected.
4359 ** Unprotected sqlite3_value objects may only be used as arguments
4360 ** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4361 ** [sqlite3_value_dup()].
@@ -7129,28 +7131,60 @@
7131 /*
7132 ** CAPI3REF: Virtual Table Constraint Operator Codes
7133 **
7134 ** These macros define the allowed values for the
7135 ** [sqlite3_index_info].aConstraint[].op field. Each value represents
7136 ** an operator that is part of a constraint term in the WHERE clause of
7137 ** a query that uses a [virtual table].
7138 **
7139 ** ^The left-hand operand of the operator is given by the corresponding
7140 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7141 ** operand is the rowid.
7142 ** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
7143 ** operators have no left-hand operand, and so for those operators the
7144 ** corresponding aConstraint[].iColumn is meaningless and should not be
7145 ** used.
7146 **
7147 ** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through
7148 ** value 255 are reserved to represent functions that are overloaded
7149 ** by the [xFindFunction|xFindFunction method] of the virtual table
7150 ** implementation.
7151 **
7152 ** The right-hand operands for each constraint might be accessible using
7153 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7154 ** operand is only available if it appears as a single constant literal
7155 ** in the input SQL. If the right-hand operand is another column or an
7156 ** expression (even a constant expression) or a parameter, then the
7157 ** sqlite3_vtab_rhs_value() probably will not be able to extract it.
7158 ** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and
7159 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7160 ** and hence calls to sqlite3_vtab_rhs_value() for those operators will
7161 ** always return SQLITE_NOTFOUND.
7162 **
7163 ** The collating sequence to be used for comparison can be found using
7164 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7165 ** tables, the collating sequence of constraints does not matter (for example
7166 ** because the constraints are numeric) and so the sqlite3_vtab_collation()
7167 ** interface is no commonly needed.
7168 */
7169 #define SQLITE_INDEX_CONSTRAINT_EQ 2
7170 #define SQLITE_INDEX_CONSTRAINT_GT 4
7171 #define SQLITE_INDEX_CONSTRAINT_LE 8
7172 #define SQLITE_INDEX_CONSTRAINT_LT 16
7173 #define SQLITE_INDEX_CONSTRAINT_GE 32
7174 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
7175 #define SQLITE_INDEX_CONSTRAINT_LIKE 65
7176 #define SQLITE_INDEX_CONSTRAINT_GLOB 66
7177 #define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7178 #define SQLITE_INDEX_CONSTRAINT_NE 68
7179 #define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7180 #define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7181 #define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7182 #define SQLITE_INDEX_CONSTRAINT_IS 72
7183 #define SQLITE_INDEX_CONSTRAINT_LIMIT 73
7184 #define SQLITE_INDEX_CONSTRAINT_OFFSET 74
7185 #define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7186
7187 /*
7188 ** CAPI3REF: Register A Virtual Table Implementation
7189 ** METHOD: sqlite3
7190 **
@@ -9506,28 +9540,29 @@
9540
9541 /*
9542 ** CAPI3REF: Determine if a virtual table query is DISTINCT
9543 ** METHOD: sqlite3_index_info
9544 **
9545 ** This API may only be used from within an [xBestIndex|xBestIndex method]
9546 ** of a [virtual table] implementation. The result of calling this
9547 ** interface from outside of xBestIndex() is undefined and probably harmful.
9548 **
9549 ** ^The sqlite3_vtab_distinct() interface returns an integer that is
9550 ** either 0, 1, or 2. The integer returned by sqlite3_vtab_distinct()
9551 ** gives the virtual table additional information about how the query
9552 ** planner wants the output to be ordered. As long as the virtual table
9553 ** can meet the ordering requirements of the query planner, it may set
9554 ** the "orderByConsumed" flag.
9555 **
9556 ** <ol><li value="0"><p>
9557 ** ^If the sqlite3_vtab_distinct() interface returns 0, that means
9558 ** that the query planner needs the virtual table to return all rows in the
9559 ** sort order defined by the "nOrderBy" and "aOrderBy" fields of the
9560 ** [sqlite3_index_info] object. This is the default expectation. If the
9561 ** virtual table outputs all rows in sorted order, then it is always safe for
9562 ** the xBestIndex method to set the "orderByConsumed" flag, regardless of
9563 ** the return value from sqlite3_vtab_distinct().
9564 ** <li value="1"><p>
9565 ** ^(If the sqlite3_vtab_distinct() interface returns 1, that means
9566 ** that the query planner does not need the rows to be returned in sorted order
9567 ** as long as all rows with the same values in all columns identified by the
9568 ** "aOrderBy" field are adjacent.)^ This mode is used when the query planner
@@ -9536,11 +9571,11 @@
9571 ** ^(If the sqlite3_vtab_distinct() interface returns 2, that means
9572 ** that the query planner does not need the rows returned in any particular
9573 ** order, as long as rows with the same values in all "aOrderBy" columns
9574 ** are adjacent.)^ ^(Furthermore, only a single row for each particular
9575 ** combination of values in the columns identified by the "aOrderBy" field
9576 ** needs to be returned.)^ ^It is always ok for two or more rows with the same
9577 ** values in all "aOrderBy" columns to be returned, as long as all such rows
9578 ** are adjacent. ^The virtual table may, if it chooses, omit extra rows
9579 ** that have the same value for all columns identified by "aOrderBy".
9580 ** ^However omitting the extra rows is optional.
9581 ** This mode is used for a DISTINCT query.
@@ -9571,31 +9606,45 @@
9606
9607 /*
9608 ** CAPI3REF: Constraint values in xBestIndex()
9609 ** METHOD: sqlite3_index_info
9610 **
9611 ** This API may only be used from within the [xBestIndex|xBestIndex method]
9612 ** of a [virtual table] implementation. The result of calling this interface
9613 ** from outside of an xBestIndex method are undefined and probably harmful.
9614 **
9615 ** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within
9616 ** the [xBestIndex] method of a [virtual table] implementation, with P being
9617 ** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and
9618 ** J being a 0-based index into P->aConstraint[], then this routine
9619 ** attempts to set *V to the value of the right-hand operand of
9620 ** that constraint if the right-hand operand is known. ^If the
9621 ** right-hand operand is not known, then *V is set to a NULL pointer.
9622 ** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if
9623 ** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V)
9624 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
9625 ** constraint is not available. ^The sqlite3_vtab_rhs_value() interface
9626 ** can return an result code other than SQLITE_OK or SQLITE_NOTFOUND if
9627 ** something goes wrong.
9628 **
9629 ** The sqlite3_vtab_rhs_value() interface is usually only successful if
9630 ** the right-hand operand of a constraint is a literal value in the original
9631 ** SQL statement. If the right-hand operand is an expression or a reference
9632 ** to some other column or a [host parameter], then sqlite3_vtab_rhs_value()
9633 ** will probably return [SQLITE_NOTFOUND].
9634 **
9635 ** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and
9636 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
9637 ** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^
9638 **
9639 ** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value
9640 ** and remains valid for the duration of the xBestIndex method call.
9641 ** ^When xBestIndex returns, the sqlite3_value object returned by
9642 ** sqlite3_vtab_rhs_value() is automatically deallocated.
9643 **
9644 ** The "_rhs_" in the name of this routine is an appreviation for
9645 ** "Right-Hand Side".
9646 */
9647 SQLITE_API int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal);
9648
9649 /*
9650 ** CAPI3REF: Conflict resolution modes
9651

Keyboard Shortcuts

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