Fossil SCM

Update the built-in SQLite to the latest 3.38.0 alpha, for testing.

drh 2022-01-01 00:41 trunk
Commit 3e74ae503f2113a440a70b886dcb3baeb1b0f9a6d027fabfba8a804707b93283
3 files changed +217 -171 +419 -260 +37 -9
+217 -171
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -443,19 +443,10 @@
443443
/*
444444
** True if an interrupt (Control-C) has been received.
445445
*/
446446
static volatile int seenInterrupt = 0;
447447
448
-#ifdef SQLITE_DEBUG
449
-/*
450
-** Out-of-memory simulator variables
451
-*/
452
-static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
453
-static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
454
-static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
455
-#endif /* SQLITE_DEBUG */
456
-
457448
/*
458449
** This is the name of our program. It is set in main(), used
459450
** in a number of other places, mostly for error messages.
460451
*/
461452
static char *Argv0;
@@ -503,52 +494,16 @@
503494
static void shell_out_of_memory(void){
504495
raw_printf(stderr,"Error: out of memory\n");
505496
exit(1);
506497
}
507498
508
-#ifdef SQLITE_DEBUG
509
-/* This routine is called when a simulated OOM occurs. It is broken
510
-** out as a separate routine to make it easy to set a breakpoint on
511
-** the OOM
512
-*/
513
-void shellOomFault(void){
514
- if( oomRepeat>0 ){
515
- oomRepeat--;
516
- }else{
517
- oomCounter--;
518
- }
519
-}
520
-#endif /* SQLITE_DEBUG */
521
-
522
-#ifdef SQLITE_DEBUG
523
-/* This routine is a replacement malloc() that is used to simulate
524
-** Out-Of-Memory (OOM) errors for testing purposes.
525
-*/
526
-static void *oomMalloc(int nByte){
527
- if( oomCounter ){
528
- if( oomCounter==1 ){
529
- shellOomFault();
530
- return 0;
531
- }else{
532
- oomCounter--;
533
- }
534
- }
535
- return defaultMalloc(nByte);
536
-}
537
-#endif /* SQLITE_DEBUG */
538
-
539
-#ifdef SQLITE_DEBUG
540
-/* Register the OOM simulator. This must occur before any memory
541
-** allocations */
542
-static void registerOomSimulator(void){
543
- sqlite3_mem_methods mem;
544
- sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
545
- defaultMalloc = mem.xMalloc;
546
- mem.xMalloc = oomMalloc;
547
- sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
548
-}
549
-#endif
499
+/* Check a pointer to see if it is NULL. If it is NULL, exit with an
500
+** out-of-memory error.
501
+*/
502
+static void shell_check_oom(void *p){
503
+ if( p==0 ) shell_out_of_memory();
504
+}
550505
551506
/*
552507
** Write I/O traces to the following stream.
553508
*/
554509
#ifdef SQLITE_ENABLE_IOTRACE
@@ -701,11 +656,11 @@
701656
702657
while( 1 ){
703658
if( n+100>nLine ){
704659
nLine = nLine*2 + 100;
705660
zLine = realloc(zLine, nLine);
706
- if( zLine==0 ) shell_out_of_memory();
661
+ shell_check_oom(zLine);
707662
}
708663
if( fgets(&zLine[n], nLine - n, in)==0 ){
709664
if( n==0 ){
710665
free(zLine);
711666
return 0;
@@ -728,11 +683,11 @@
728683
char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
729684
if( zTrans ){
730685
int nTrans = strlen30(zTrans)+1;
731686
if( nTrans>nLine ){
732687
zLine = realloc(zLine, nTrans);
733
- if( zLine==0 ) shell_out_of_memory();
688
+ shell_check_oom(zLine);
734689
}
735690
memcpy(zLine, zTrans, nTrans);
736691
sqlite3_free(zTrans);
737692
}
738693
}
@@ -875,11 +830,11 @@
875830
}
876831
877832
if( p->z==0 || p->n+len>=p->nAlloc ){
878833
p->nAlloc = p->nAlloc*2 + len + 20;
879834
p->z = realloc(p->z, p->nAlloc);
880
- if( p->z==0 ) shell_out_of_memory();
835
+ shell_check_oom(p->z);
881836
}
882837
883838
if( quote ){
884839
char *zCsr = p->z+p->n;
885840
*zCsr++ = quote;
@@ -930,10 +885,11 @@
930885
char *zDiv = "(";
931886
int nRow = 0;
932887
933888
zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
934889
zSchema ? zSchema : "main", zName);
890
+ shell_check_oom(zSql);
935891
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
936892
sqlite3_free(zSql);
937893
initText(&s);
938894
if( zSchema ){
939895
cQuote = quoteChar(zSchema);
@@ -946,10 +902,11 @@
946902
while( sqlite3_step(pStmt)==SQLITE_ROW ){
947903
const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
948904
nRow++;
949905
appendText(&s, zDiv, 0);
950906
zDiv = ",";
907
+ if( zCol==0 ) zCol = "";
951908
cQuote = quoteChar(zCol);
952909
appendText(&s, zCol, cQuote);
953910
}
954911
appendText(&s, ")", 0);
955912
sqlite3_finalize(pStmt);
@@ -969,13 +926,15 @@
969926
static void shellModuleSchema(
970927
sqlite3_context *pCtx,
971928
int nVal,
972929
sqlite3_value **apVal
973930
){
974
- const char *zName = (const char*)sqlite3_value_text(apVal[0]);
975
- char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
931
+ const char *zName;
932
+ char *zFake;
976933
UNUSED_PARAMETER(nVal);
934
+ zName = (const char*)sqlite3_value_text(apVal[0]);
935
+ zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
977936
if( zFake ){
978937
sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
979938
-1, sqlite3_free);
980939
free(zFake);
981940
}
@@ -1859,10 +1818,11 @@
18591818
SHA3Context *p,
18601819
const unsigned char *aData,
18611820
unsigned int nData
18621821
){
18631822
unsigned int i = 0;
1823
+ if( aData==0 ) return;
18641824
#if SHA3_BYTEORDER==1234
18651825
if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
18661826
for(; i+7<nData; i+=8){
18671827
p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
18681828
p->nLoaded += 8;
@@ -2517,14 +2477,15 @@
25172477
const char *zFile, /* File to write */
25182478
sqlite3_value *pData, /* Data to write */
25192479
mode_t mode, /* MODE parameter passed to writefile() */
25202480
sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
25212481
){
2482
+ if( zFile==0 ) return 1;
25222483
#if !defined(_WIN32) && !defined(WIN32)
25232484
if( S_ISLNK(mode) ){
25242485
const char *zTo = (const char*)sqlite3_value_text(pData);
2525
- if( symlink(zTo, zFile)<0 ) return 1;
2486
+ if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
25262487
}else
25272488
#endif
25282489
{
25292490
if( S_ISDIR(mode) ){
25302491
if( mkdir(zFile, mode) ){
@@ -5886,11 +5847,11 @@
58865847
if( (idxNum & 3)==3 ){
58875848
/* Both start= and stop= boundaries are available. This is the
58885849
** the preferred case */
58895850
pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
58905851
pIdxInfo->estimatedRows = 1000;
5891
- if( pIdxInfo->nOrderBy==1 ){
5852
+ if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
58925853
if( pIdxInfo->aOrderBy[0].desc ){
58935854
idxNum |= 8;
58945855
}else{
58955856
idxNum |= 16;
58965857
}
@@ -6721,17 +6682,19 @@
67216682
const sqlite3_api_routines *pApi
67226683
){
67236684
int rc = SQLITE_OK;
67246685
SQLITE_EXTENSION_INIT2(pApi);
67256686
(void)pzErrMsg; /* Unused */
6726
- rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6727
- 0, re_sql_func, 0, 0);
6687
+ rc = sqlite3_create_function(db, "regexp", 2,
6688
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6689
+ 0, re_sql_func, 0, 0);
67286690
if( rc==SQLITE_OK ){
67296691
/* The regexpi(PATTERN,STRING) function is a case-insensitive version
67306692
** of regexp(PATTERN,STRING). */
6731
- rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6732
- (void*)db, re_sql_func, 0, 0);
6693
+ rc = sqlite3_create_function(db, "regexpi", 2,
6694
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6695
+ (void*)db, re_sql_func, 0, 0);
67336696
}
67346697
return rc;
67356698
}
67366699
67376700
/************************* End ../ext/misc/regexp.c ********************/
@@ -9939,21 +9902,29 @@
99399902
IdxTable **ppOut, /* OUT: New object (if successful) */
99409903
char **pzErrmsg /* OUT: Error message (if not) */
99419904
){
99429905
sqlite3_stmt *p1 = 0;
99439906
int nCol = 0;
9944
- int nTab = STRLEN(zTab);
9945
- int nByte = sizeof(IdxTable) + nTab + 1;
9907
+ int nTab;
9908
+ int nByte;
99469909
IdxTable *pNew = 0;
99479910
int rc, rc2;
99489911
char *pCsr = 0;
99499912
int nPk = 0;
99509913
9914
+ *ppOut = 0;
9915
+ if( zTab==0 ) return SQLITE_ERROR;
9916
+ nTab = STRLEN(zTab);
9917
+ nByte = sizeof(IdxTable) + nTab + 1;
99519918
rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
99529919
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
99539920
const char *zCol = (const char*)sqlite3_column_text(p1, 1);
99549921
const char *zColSeq = 0;
9922
+ if( zCol==0 ){
9923
+ rc = SQLITE_ERROR;
9924
+ break;
9925
+ }
99559926
nByte += 1 + STRLEN(zCol);
99569927
rc = sqlite3_table_column_metadata(
99579928
db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
99589929
);
99599930
if( zColSeq==0 ) zColSeq = "binary";
@@ -9976,11 +9947,13 @@
99769947
99779948
nCol = 0;
99789949
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
99799950
const char *zCol = (const char*)sqlite3_column_text(p1, 1);
99809951
const char *zColSeq = 0;
9981
- int nCopy = STRLEN(zCol) + 1;
9952
+ int nCopy;
9953
+ if( zCol==0 ) continue;
9954
+ nCopy = STRLEN(zCol) + 1;
99829955
pNew->aCol[nCol].zName = pCsr;
99839956
pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
99849957
memcpy(pCsr, zCol, nCopy);
99859958
pCsr += nCopy;
99869959
@@ -10128,10 +10101,11 @@
1012810101
while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
1012910102
int bMatch = 1;
1013010103
IdxConstraint *pT = pTail;
1013110104
sqlite3_stmt *pInfo = 0;
1013210105
const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10106
+ if( zIdx==0 ) continue;
1013310107
1013410108
/* Zero the IdxConstraint.bFlag values in the pEq list */
1013510109
for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
1013610110
1013710111
rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
@@ -10539,10 +10513,11 @@
1053910513
1054010514
/* Create the table and its triggers in the temp schema */
1054110515
rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
1054210516
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
1054310517
const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10518
+ if( zCreate==0 ) continue;
1054410519
rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
1054510520
}
1054610521
idxFinalize(&rc, pSelect);
1054710522
1054810523
/* Rename the table in the temp schema to zInt */
@@ -10641,12 +10616,13 @@
1064110616
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
1064210617
const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
1064310618
const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
1064410619
const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
1064510620
10621
+ if( zType==0 || zName==0 ) continue;
1064610622
if( zType[0]=='v' || zType[1]=='r' ){
10647
- rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10623
+ if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
1064810624
}else{
1064910625
IdxTable *pTab;
1065010626
rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
1065110627
if( rc==SQLITE_OK ){
1065210628
int i;
@@ -10779,10 +10755,11 @@
1077910755
break;
1078010756
1078110757
case SQLITE_BLOB:
1078210758
case SQLITE_TEXT: {
1078310759
int nByte = sqlite3_value_bytes(argv[1]);
10760
+ const void *pData = 0;
1078410761
if( nByte>pSlot->nByte ){
1078510762
char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
1078610763
if( zNew==0 ){
1078710764
sqlite3_result_error_nomem(pCtx);
1078810765
return;
@@ -10790,13 +10767,15 @@
1079010767
pSlot->nByte = nByte*2;
1079110768
pSlot->z = zNew;
1079210769
}
1079310770
pSlot->n = nByte;
1079410771
if( pSlot->eType==SQLITE_BLOB ){
10795
- memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
10772
+ pData = sqlite3_value_blob(argv[1]);
10773
+ if( pData ) memcpy(pSlot->z, pData, nByte);
1079610774
}else{
10797
- memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
10775
+ pData = sqlite3_value_text(argv[1]);
10776
+ memcpy(pSlot->z, pData, nByte);
1079810777
}
1079910778
break;
1080010779
}
1080110780
}
1080210781
}
@@ -11003,10 +10982,11 @@
1100310982
1100410983
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
1100510984
i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
1100610985
const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
1100710986
const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10987
+ if( zTab==0 || zIdx==0 ) continue;
1100810988
if( p->iSample<100 && iPrev!=iRowid ){
1100910989
samplectx.target = (double)p->iSample / 100.0;
1101010990
samplectx.iTarget = p->iSample;
1101110991
samplectx.nRow = 0.0;
1101210992
samplectx.nRet = 0.0;
@@ -11069,18 +11049,18 @@
1106911049
}
1107011050
1107111051
1107211052
/* Copy the entire schema of database [db] into [dbm]. */
1107311053
if( rc==SQLITE_OK ){
11074
- sqlite3_stmt *pSql;
11054
+ sqlite3_stmt *pSql = 0;
1107511055
rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
1107611056
"SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
1107711057
" AND sql NOT LIKE 'CREATE VIRTUAL %%'"
1107811058
);
1107911059
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
1108011060
const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11081
- rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11061
+ if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
1108211062
}
1108311063
idxFinalize(&rc, pSql);
1108411064
}
1108511065
1108611066
/* Create the vtab schema */
@@ -12831,10 +12811,11 @@
1283112811
break;
1283212812
}
1283312813
}
1283412814
if( i==0 || strstr(z, p->colSeparator)!=0 ){
1283512815
char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12816
+ shell_check_oom(zQuoted);
1283612817
utf8_printf(out, "%s", zQuoted);
1283712818
sqlite3_free(zQuoted);
1283812819
}else{
1283912820
utf8_printf(out, "%s", z);
1284012821
}
@@ -13005,11 +12986,11 @@
1300512986
int nText = strlen30(zText);
1300612987
if( p->autoEQPtest ){
1300712988
utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1300812989
}
1300912990
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13010
- if( pNew==0 ) shell_out_of_memory();
12991
+ shell_check_oom(pNew);
1301112992
pNew->iEqpId = iEqpId;
1301212993
pNew->iParentId = p2;
1301312994
memcpy(pNew->zText, zText, nText+1);
1301412995
pNew->pNext = 0;
1301512996
if( p->sGraph.pLast ){
@@ -13226,10 +13207,11 @@
1322613207
){
1322713208
utf8_printf(p->out, "%s;\n", azArg[0]);
1322813209
break;
1322913210
}
1323013211
z = sqlite3_mprintf("%s", azArg[0]);
13212
+ shell_check_oom(z);
1323113213
j = 0;
1323213214
for(i=0; IsSpace(z[i]); i++){}
1323313215
for(; (c = z[i])!=0; i++){
1323413216
if( IsSpace(c) ){
1323513217
if( z[j-1]=='\r' ) z[j-1] = '\n';
@@ -13357,10 +13339,11 @@
1335713339
raw_printf(p->out,"(");
1335813340
for(i=0; i<nArg; i++){
1335913341
if( i>0 ) raw_printf(p->out, ",");
1336013342
if( quoteChar(azCol[i]) ){
1336113343
char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13344
+ shell_check_oom(z);
1336213345
utf8_printf(p->out, "%s", z);
1336313346
sqlite3_free(z);
1336413347
}else{
1336513348
raw_printf(p->out, "%s", azCol[i]);
1336613349
}
@@ -13602,20 +13585,61 @@
1360213585
if( zName==0 ) return;
1360313586
cQuote = quoteChar(zName);
1360413587
n = strlen30(zName);
1360513588
if( cQuote ) n += n+2;
1360613589
z = p->zDestTable = malloc( n+1 );
13607
- if( z==0 ) shell_out_of_memory();
13590
+ shell_check_oom(z);
1360813591
n = 0;
1360913592
if( cQuote ) z[n++] = cQuote;
1361013593
for(i=0; zName[i]; i++){
1361113594
z[n++] = zName[i];
1361213595
if( zName[i]==cQuote ) z[n++] = cQuote;
1361313596
}
1361413597
if( cQuote ) z[n++] = cQuote;
1361513598
z[n] = 0;
1361613599
}
13600
+
13601
+/*
13602
+** Maybe construct two lines of text that point out the position of a
13603
+** syntax error. Return a pointer to the text, in memory obtained from
13604
+** sqlite3_malloc(). Or, if the most recent error does not involve a
13605
+** specific token that we can point to, return an empty string.
13606
+**
13607
+** In all cases, the memory returned is obtained from sqlite3_malloc64()
13608
+** and should be released by the caller invoking sqlite3_free().
13609
+*/
13610
+static char *shell_error_context(const char *zSql, sqlite3 *db){
13611
+ int iOffset;
13612
+ size_t len;
13613
+ char *zCode;
13614
+ char *zMsg;
13615
+ int i;
13616
+ if( db==0
13617
+ || zSql==0
13618
+ || (iOffset = sqlite3_error_offset(db))<0
13619
+ ){
13620
+ return sqlite3_mprintf("");
13621
+ }
13622
+ while( iOffset>50 ){
13623
+ iOffset--;
13624
+ zSql++;
13625
+ while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
13626
+ }
13627
+ len = strlen(zSql);
13628
+ if( len>78 ){
13629
+ len = 78;
13630
+ while( (zSql[len]&0xc0)==0x80 ) len--;
13631
+ }
13632
+ zCode = sqlite3_mprintf("%.*s", len, zSql);
13633
+ for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
13634
+ if( iOffset<25 ){
13635
+ zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
13636
+ }else{
13637
+ zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
13638
+ }
13639
+ return zMsg;
13640
+}
1361713641
1361813642
1361913643
/*
1362013644
** Execute a query statement that will generate SQL output. Print
1362113645
** the result columns, comma-separated, on a line and then add a
@@ -13635,12 +13659,14 @@
1363513659
int nResult;
1363613660
int i;
1363713661
const char *z;
1363813662
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1363913663
if( rc!=SQLITE_OK || !pSelect ){
13640
- utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13641
- sqlite3_errmsg(p->db));
13664
+ char *zContext = shell_error_context(zSelect, p->db);
13665
+ utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
13666
+ sqlite3_errmsg(p->db), zContext);
13667
+ sqlite3_free(zContext);
1364213668
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1364313669
return rc;
1364413670
}
1364513671
rc = sqlite3_step(pSelect);
1364613672
nResult = sqlite3_column_count(pSelect);
@@ -13672,15 +13698,21 @@
1367213698
** Allocate space and save off string indicating current error.
1367313699
*/
1367413700
static char *save_err_msg(
1367513701
sqlite3 *db, /* Database to query */
1367613702
const char *zWhen, /* Qualifier (format) wrapper */
13677
- int rc /* Error code returned from API */
13703
+ int rc, /* Error code returned from API */
13704
+ const char *zSql /* SQL string, or NULL */
1367813705
){
13679
- if( zWhen==0 )
13680
- zWhen = "%s (%d)";
13681
- return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
13706
+ char *zErr;
13707
+ char *zContext;
13708
+ if( zWhen==0 ) zWhen = "%s (%d)%s";
13709
+ zContext = shell_error_context(zSql, db);
13710
+ zErr = sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc, zContext);
13711
+ shell_check_oom(zErr);
13712
+ sqlite3_free(zContext);
13713
+ return zErr;
1368213714
}
1368313715
1368413716
#ifdef __linux__
1368513717
/*
1368613718
** Attempt to display I/O stats on Linux using /proc/PID/io
@@ -14024,13 +14056,13 @@
1402414056
}
1402514057
}
1402614058
}
1402714059
nAlloc += 100;
1402814060
p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14029
- if( p->aiIndent==0 ) shell_out_of_memory();
14061
+ shell_check_oom(p->aiIndent);
1403014062
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14031
- if( abYield==0 ) shell_out_of_memory();
14063
+ shell_check_oom(abYield);
1403214064
}
1403314065
abYield[iOp] = str_in_array(zOp, azYield);
1403414066
p->aiIndent[iOp] = 0;
1403514067
p->nIndent = iOp+1;
1403614068
@@ -14235,29 +14267,29 @@
1423514267
if( rc!=SQLITE_ROW ) return;
1423614268
nColumn = sqlite3_column_count(pStmt);
1423714269
nAlloc = nColumn*4;
1423814270
if( nAlloc<=0 ) nAlloc = 1;
1423914271
azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14240
- if( azData==0 ) shell_out_of_memory();
14272
+ shell_check_oom(azData);
1424114273
for(i=0; i<nColumn; i++){
1424214274
azData[i] = strdup(sqlite3_column_name(pStmt,i));
1424314275
}
1424414276
do{
1424514277
if( (nRow+2)*nColumn >= nAlloc ){
1424614278
nAlloc *= 2;
1424714279
azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14248
- if( azData==0 ) shell_out_of_memory();
14280
+ shell_check_oom(azData);
1424914281
}
1425014282
nRow++;
1425114283
for(i=0; i<nColumn; i++){
1425214284
z = (const char*)sqlite3_column_text(pStmt,i);
1425314285
azData[nRow*nColumn + i] = z ? strdup(z) : 0;
1425414286
}
1425514287
}while( sqlite3_step(pStmt)==SQLITE_ROW );
1425614288
if( nColumn>p->nWidth ){
1425714289
p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14258
- if( p->colWidth==0 ) shell_out_of_memory();
14290
+ shell_check_oom(p->colWidth);
1425914291
for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
1426014292
p->nWidth = nColumn;
1426114293
p->actualWidth = &p->colWidth[nColumn];
1426214294
}
1426314295
memset(p->actualWidth, 0, nColumn*sizeof(int));
@@ -14436,11 +14468,14 @@
1443614468
} while( SQLITE_ROW == rc );
1443714469
sqlite3_free(pData);
1443814470
if( pArg->cMode==MODE_Json ){
1443914471
fputs("]\n", pArg->out);
1444014472
}else if( pArg->cMode==MODE_Count ){
14441
- printf("%llu row%s\n", nRow, nRow!=1 ? "s" : "");
14473
+ char zBuf[200];
14474
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
14475
+ nRow, nRow!=1 ? "s" : "");
14476
+ printf("%s", zBuf);
1444214477
}
1444314478
}
1444414479
}
1444514480
}
1444614481
@@ -14560,18 +14595,19 @@
1456014595
}
1456114596
1456214597
if( rc==SQLITE_OK ){
1456314598
pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
1456414599
if( pState->expert.pExpert==0 ){
14565
- raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14600
+ raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
1456614601
rc = SQLITE_ERROR;
1456714602
}else{
1456814603
sqlite3_expert_config(
1456914604
pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
1457014605
);
1457114606
}
1457214607
}
14608
+ sqlite3_free(zErr);
1457314609
1457414610
return rc;
1457514611
}
1457614612
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1457714613
@@ -14609,11 +14645,11 @@
1460914645
while( zSql[0] && (SQLITE_OK == rc) ){
1461014646
static const char *zStmtSql;
1461114647
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
1461214648
if( SQLITE_OK != rc ){
1461314649
if( pzErrMsg ){
14614
- *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
14650
+ *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)%s", rc, zSql);
1461514651
}
1461614652
}else{
1461714653
if( !pStmt ){
1461814654
/* this happens for a comment or white-space */
1461914655
zSql = zLeftover;
@@ -14644,10 +14680,11 @@
1464414680
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
1464514681
if( pArg->autoEQP>=AUTOEQP_trigger ){
1464614682
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
1464714683
}
1464814684
zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14685
+ shell_check_oom(zEQP);
1464914686
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1465014687
if( rc==SQLITE_OK ){
1465114688
while( sqlite3_step(pExplain)==SQLITE_ROW ){
1465214689
const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
1465314690
int iEqpId = sqlite3_column_int(pExplain, 0);
@@ -14661,10 +14698,11 @@
1466114698
sqlite3_finalize(pExplain);
1466214699
sqlite3_free(zEQP);
1466314700
if( pArg->autoEQP>=AUTOEQP_full ){
1466414701
/* Also do an EXPLAIN for ".eqp full" mode */
1466514702
zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14703
+ shell_check_oom(zEQP);
1466614704
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1466714705
if( rc==SQLITE_OK ){
1466814706
pArg->cMode = MODE_Explain;
1466914707
explain_data_prepare(pArg, pExplain);
1467014708
exec_prepared_stmt(pArg, pExplain);
@@ -14723,11 +14761,11 @@
1472314761
if( rc!=SQLITE_NOMEM ) rc = rc2;
1472414762
if( rc==SQLITE_OK ){
1472514763
zSql = zLeftover;
1472614764
while( IsSpace(zSql[0]) ) zSql++;
1472714765
}else if( pzErrMsg ){
14728
- *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
14766
+ *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc, 0);
1472914767
}
1473014768
1473114769
/* clear saved stmt handle */
1473214770
if( pArg ){
1473314771
pArg->pStmt = NULL;
@@ -14773,20 +14811,22 @@
1477314811
int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
1477414812
int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
1477514813
int rc;
1477614814
1477714815
zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14816
+ shell_check_oom(zSql);
1477814817
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1477914818
sqlite3_free(zSql);
1478014819
if( rc ) return 0;
1478114820
while( sqlite3_step(pStmt)==SQLITE_ROW ){
1478214821
if( nCol>=nAlloc-2 ){
1478314822
nAlloc = nAlloc*2 + nCol + 10;
1478414823
azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14785
- if( azCol==0 ) shell_out_of_memory();
14824
+ shell_check_oom(azCol);
1478614825
}
1478714826
azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14827
+ shell_check_oom(azCol[nCol]);
1478814828
if( sqlite3_column_int(pStmt, 5) ){
1478914829
nPK++;
1479014830
if( nPK==1
1479114831
&& sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
1479214832
"INTEGER")==0
@@ -14816,10 +14856,11 @@
1481614856
** there is a "pk" entry in "PRAGMA index_list". There will be
1481714857
** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
1481814858
*/
1481914859
zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
1482014860
" WHERE origin='pk'", zTab);
14861
+ shell_check_oom(zSql);
1482114862
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
1482214863
sqlite3_free(zSql);
1482314864
if( rc ){
1482414865
freeColumnList(azCol);
1482514866
return 0;
@@ -14907,10 +14948,11 @@
1490714948
}
1490814949
zIns = sqlite3_mprintf(
1490914950
"INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
1491014951
"VALUES('table','%q','%q',0,'%q');",
1491114952
zTable, zTable, zSql);
14953
+ shell_check_oom(zIns);
1491214954
utf8_printf(p->out, "%s\n", zIns);
1491314955
sqlite3_free(zIns);
1491414956
return 0;
1491514957
}else{
1491614958
printSchemaLine(p->out, zSql, ";\n");
@@ -15150,13 +15192,10 @@
1515015192
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
1515115193
" If FILE begins with '|' then open as a pipe",
1515215194
" --bom Put a UTF8 byte-order mark at the beginning",
1515315195
" -e Send output to the system text editor",
1515415196
" -x Send output as CSV to a spreadsheet (same as \".excel\")",
15155
-#ifdef SQLITE_DEBUG
15156
- ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
15157
-#endif
1515815197
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
1515915198
" Options:",
1516015199
" --append Use appendvfs to append database to the end of FILE",
1516115200
#ifndef SQLITE_OMIT_DESERIALIZE
1516215201
" --deserialize Load into memory using sqlite3_deserialize()",
@@ -15188,11 +15227,12 @@
1518815227
" --quiet|-q No output except at interrupts",
1518915228
" --reset Reset the count for each input and interrupt",
1519015229
#endif
1519115230
".prompt MAIN CONTINUE Replace the standard prompts",
1519215231
".quit Exit this program",
15193
- ".read FILE Read input from FILE",
15232
+ ".read FILE Read input from FILE or command output",
15233
+ " If FILE begins with \"|\", it is a command that generates the input.",
1519415234
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1519515235
".recover Recover as much data as possible from corrupt db.",
1519615236
" --freelist-corrupt Assume the freelist is corrupt",
1519715237
" --recovery-db NAME Store recovery metadata in database file NAME",
1519815238
" --lost-and-found TABLE Alternative name for the lost-and-found table",
@@ -15308,10 +15348,11 @@
1530815348
}
1530915349
}
1531015350
}else{
1531115351
/* Look for commands that for which zPattern is an exact prefix */
1531215352
zPat = sqlite3_mprintf(".%s*", zPattern);
15353
+ shell_check_oom(zPat);
1531315354
for(i=0; i<ArraySize(azHelp); i++){
1531415355
if( sqlite3_strglob(zPat, azHelp[i])==0 ){
1531515356
utf8_printf(out, "%s\n", azHelp[i]);
1531615357
j = i+1;
1531715358
n++;
@@ -15330,10 +15371,11 @@
1533015371
return n;
1533115372
}
1533215373
/* Look for commands that contain zPattern anywhere. Show the complete
1533315374
** text of all commands that match. */
1533415375
zPat = sqlite3_mprintf("%%%s%%", zPattern);
15376
+ shell_check_oom(zPat);
1533515377
for(i=0; i<ArraySize(azHelp); i++){
1533615378
if( azHelp[i][0]=='.' ) j = i;
1533715379
if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
1533815380
utf8_printf(out, "%s\n", azHelp[j]);
1533915381
while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
@@ -15518,14 +15560,11 @@
1551815560
if( rc!=2 ) goto readHexDb_error;
1551915561
if( n<0 ) goto readHexDb_error;
1552015562
if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
1552115563
n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
1552215564
a = sqlite3_malloc( n ? n : 1 );
15523
- if( a==0 ){
15524
- utf8_printf(stderr, "Out of memory!\n");
15525
- goto readHexDb_error;
15526
- }
15565
+ shell_check_oom(a);
1552715566
memset(a, 0, n);
1552815567
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
1552915568
utf8_printf(stderr, "invalid pagesize\n");
1553015569
goto readHexDb_error;
1553115570
}
@@ -15652,11 +15691,11 @@
1565215691
int argc,
1565315692
sqlite3_value **argv
1565415693
){
1565515694
const char *zText = (const char*)sqlite3_value_text(argv[0]);
1565615695
UNUSED_PARAMETER(argc);
15657
- if( zText[0]=='\'' ){
15696
+ if( zText && zText[0]=='\'' ){
1565815697
int nText = sqlite3_value_bytes(argv[0]);
1565915698
int i;
1566015699
char zBuf1[20];
1566115700
char zBuf2[20];
1566215701
const char *zNL = 0;
@@ -15829,10 +15868,11 @@
1582915868
editFunc, 0, 0);
1583015869
#endif
1583115870
if( p->openMode==SHELL_OPEN_ZIPFILE ){
1583215871
char *zSql = sqlite3_mprintf(
1583315872
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
15873
+ shell_check_oom(zSql);
1583415874
sqlite3_exec(p->db, zSql, 0, 0, 0);
1583515875
sqlite3_free(zSql);
1583615876
}
1583715877
#ifndef SQLITE_OMIT_DESERIALIZE
1583815878
else
@@ -15886,15 +15926,17 @@
1588615926
if( state==0 ){
1588715927
char *zSql;
1588815928
sqlite3_finalize(pStmt);
1588915929
zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
1589015930
" FROM completion(%Q) ORDER BY 1", text);
15931
+ shell_check_oom(zSql);
1589115932
sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
1589215933
sqlite3_free(zSql);
1589315934
}
1589415935
if( sqlite3_step(pStmt)==SQLITE_ROW ){
15895
- zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
15936
+ const char *z = (const char*)sqlite3_column_text(pStmt,0);
15937
+ zRet = z ? strdup(z) : 0;
1589615938
}else{
1589715939
sqlite3_finalize(pStmt);
1589815940
pStmt = 0;
1589915941
zRet = 0;
1590015942
}
@@ -15923,17 +15965,18 @@
1592315965
iStart = i+1;
1592415966
memcpy(zBuf, zLine, iStart);
1592515967
zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
1592615968
" FROM completion(%Q,%Q) ORDER BY 1",
1592715969
&zLine[iStart], zLine);
15970
+ shell_check_oom(zSql);
1592815971
sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
1592915972
sqlite3_free(zSql);
1593015973
sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
1593115974
while( sqlite3_step(pStmt)==SQLITE_ROW ){
1593215975
const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
1593315976
int nCompletion = sqlite3_column_bytes(pStmt, 0);
15934
- if( iStart+nCompletion < sizeof(zBuf)-1 ){
15977
+ if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
1593515978
memcpy(zBuf+iStart, zCompletion, nCompletion+1);
1593615979
linenoiseAddCompletion(lc, zBuf);
1593715980
}
1593815981
}
1593915982
sqlite3_finalize(pStmt);
@@ -16164,11 +16207,11 @@
1616416207
/* Append a single byte to z[] */
1616516208
static void import_append_char(ImportCtx *p, int c){
1616616209
if( p->n+1>=p->nAlloc ){
1616716210
p->nAlloc += p->nAlloc + 100;
1616816211
p->z = sqlite3_realloc64(p->z, p->nAlloc);
16169
- if( p->z==0 ) shell_out_of_memory();
16212
+ shell_check_oom(p->z);
1617016213
}
1617116214
p->z[p->n++] = (char)c;
1617216215
}
1617316216
1617416217
/* Read a single field of CSV text. Compatible with rfc4180 and extended
@@ -16316,20 +16359,21 @@
1631616359
int k = 0;
1631716360
int cnt = 0;
1631816361
const int spinRate = 10000;
1631916362
1632016363
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16364
+ shell_check_oom(zQuery);
1632116365
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
1632216366
if( rc ){
1632316367
utf8_printf(stderr, "Error %d: %s on [%s]\n",
1632416368
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
1632516369
zQuery);
1632616370
goto end_data_xfer;
1632716371
}
1632816372
n = sqlite3_column_count(pQuery);
1632916373
zInsert = sqlite3_malloc64(200 + nTable + n*3);
16330
- if( zInsert==0 ) shell_out_of_memory();
16374
+ shell_check_oom(zInsert);
1633116375
sqlite3_snprintf(200+nTable,zInsert,
1633216376
"INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
1633316377
i = strlen30(zInsert);
1633416378
for(j=1; j<n; j++){
1633516379
memcpy(zInsert+i, ",?", 2);
@@ -16388,10 +16432,11 @@
1638816432
if( rc==SQLITE_DONE ) break;
1638916433
sqlite3_finalize(pQuery);
1639016434
sqlite3_free(zQuery);
1639116435
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
1639216436
zTable);
16437
+ shell_check_oom(zQuery);
1639316438
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
1639416439
if( rc ){
1639516440
utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
1639616441
break;
1639716442
}
@@ -16424,10 +16469,11 @@
1642416469
const unsigned char *zSql;
1642516470
char *zErrMsg = 0;
1642616471
1642716472
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
1642816473
" WHERE %s", zWhere);
16474
+ shell_check_oom(zQuery);
1642916475
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
1643016476
if( rc ){
1643116477
utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
1643216478
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
1643316479
zQuery);
@@ -16434,10 +16480,11 @@
1643416480
goto end_schema_xfer;
1643516481
}
1643616482
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
1643716483
zName = sqlite3_column_text(pQuery, 0);
1643816484
zSql = sqlite3_column_text(pQuery, 1);
16485
+ if( zName==0 || zSql==0 ) continue;
1643916486
printf("%s... ", zName); fflush(stdout);
1644016487
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
1644116488
if( zErrMsg ){
1644216489
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
1644316490
sqlite3_free(zErrMsg);
@@ -16451,10 +16498,11 @@
1645116498
if( rc!=SQLITE_DONE ){
1645216499
sqlite3_finalize(pQuery);
1645316500
sqlite3_free(zQuery);
1645416501
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
1645516502
" WHERE %s ORDER BY rowid DESC", zWhere);
16503
+ shell_check_oom(zQuery);
1645616504
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
1645716505
if( rc ){
1645816506
utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
1645916507
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
1646016508
zQuery);
@@ -16461,10 +16509,11 @@
1646116509
goto end_schema_xfer;
1646216510
}
1646316511
while( sqlite3_step(pQuery)==SQLITE_ROW ){
1646416512
zName = sqlite3_column_text(pQuery, 0);
1646516513
zSql = sqlite3_column_text(pQuery, 1);
16514
+ if( zName==0 || zSql==0 ) continue;
1646616515
printf("%s... ", zName); fflush(stdout);
1646716516
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
1646816517
if( zErrMsg ){
1646916518
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
1647016519
sqlite3_free(zErrMsg);
@@ -16845,13 +16894,11 @@
1684516894
}
1684616895
p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
1684716896
}else{
1684816897
p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
1684916898
}
16850
- if( p->zTempFile==0 ){
16851
- shell_out_of_memory();
16852
- }
16899
+ shell_check_oom(p->zTempFile);
1685316900
}
1685416901
1685516902
1685616903
/*
1685716904
** The implementation of SQL scalar function fkey_collate_clause(), used
@@ -17028,18 +17075,18 @@
1702817075
const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
1702917076
const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
1703017077
const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
1703117078
const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
1703217079
17080
+ if( zEQP==0 ) continue;
17081
+ if( zGlob==0 ) continue;
1703317082
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1703417083
if( rc!=SQLITE_OK ) break;
1703517084
if( SQLITE_ROW==sqlite3_step(pExplain) ){
1703617085
const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17037
- res = (
17038
- 0==sqlite3_strglob(zGlob, zPlan)
17039
- || 0==sqlite3_strglob(zGlobIPK, zPlan)
17040
- );
17086
+ res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
17087
+ || 0==sqlite3_strglob(zGlobIPK, zPlan));
1704117088
}
1704217089
rc = sqlite3_finalize(pExplain);
1704317090
if( rc!=SQLITE_OK ) break;
1704417091
1704517092
if( res<0 ){
@@ -18140,10 +18187,11 @@
1814018187
, zName, zName
1814118188
);
1814218189
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
1814318190
pTab->iPk = sqlite3_column_int(pPkFinder, 0);
1814418191
zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18192
+ if( zPk==0 ){ zPk = "_"; /* Defensive. Should never happen */ }
1814518193
}
1814618194
}
1814718195
1814818196
pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
1814918197
pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
@@ -18224,12 +18272,14 @@
1822418272
break;
1822518273
}
1822618274
if( sqlite3_stricmp(zType, "table")==0 ){
1822718275
zName = (const char*)sqlite3_column_text(pStmt, 1);
1822818276
zSql = (const char*)sqlite3_column_text(pStmt, 2);
18229
- pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18230
- break;
18277
+ if( zName!=0 && zSql!=0 ){
18278
+ pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18279
+ break;
18280
+ }
1823118281
}
1823218282
}
1823318283
1823418284
shellFinalize(pRc, pStmt);
1823518285
*pbNoop = bNoop;
@@ -18919,12 +18969,13 @@
1891918969
rc = 1;
1892018970
}else{
1892118971
while( sqlite3_step(pStmt)==SQLITE_ROW ){
1892218972
const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
1892318973
const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18974
+ if( zSchema==0 || zFile==0 ) continue;
1892418975
azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18925
- if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
18976
+ shell_check_oom(azName);
1892618977
azName[nName*2] = strdup(zSchema);
1892718978
azName[nName*2+1] = strdup(zFile);
1892818979
nName++;
1892918980
}
1893018981
}
@@ -19176,12 +19227,19 @@
1917619227
}
1917719228
}else
1917819229
1917919230
#ifndef SQLITE_OMIT_VIRTUALTABLE
1918019231
if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19181
- open_db(p, 0);
19182
- expertDotCommand(p, azArg, nArg);
19232
+ if( p->bSafeMode ){
19233
+ raw_printf(stderr,
19234
+ "Cannot run experimental commands such as \"%s\" in safe mode\n",
19235
+ azArg[0]);
19236
+ rc = 1;
19237
+ }else{
19238
+ open_db(p, 0);
19239
+ expertDotCommand(p, azArg, nArg);
19240
+ }
1918319241
}else
1918419242
#endif
1918519243
1918619244
if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
1918719245
static const struct {
@@ -19969,11 +20027,12 @@
1996920027
if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
1997020028
if( nArg!=2 ){
1997120029
raw_printf(stderr, "Usage: .nonce NONCE\n");
1997220030
rc = 1;
1997320031
}else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
19974
- raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
20032
+ raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
20033
+ p->lineno, azArg[1]);
1997520034
exit(1);
1997620035
}else{
1997720036
p->bSafeMode = 0;
1997820037
return 0; /* Return immediately to bypass the safe mode reset
1997920038
** at the end of this procedure */
@@ -19988,39 +20047,12 @@
1998820047
raw_printf(stderr, "Usage: .nullvalue STRING\n");
1998920048
rc = 1;
1999020049
}
1999120050
}else
1999220051
19993
-#ifdef SQLITE_DEBUG
19994
- if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19995
- int i;
19996
- for(i=1; i<nArg; i++){
19997
- const char *z = azArg[i];
19998
- if( z[0]=='-' && z[1]=='-' ) z++;
19999
- if( strcmp(z,"-repeat")==0 ){
20000
- if( i==nArg-1 ){
20001
- raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
20002
- rc = 1;
20003
- }else{
20004
- oomRepeat = (int)integerValue(azArg[++i]);
20005
- }
20006
- }else if( IsDigit(z[0]) ){
20007
- oomCounter = (int)integerValue(azArg[i]);
20008
- }else{
20009
- raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
20010
- raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
20011
- rc = 1;
20012
- }
20013
- }
20014
- if( rc==0 ){
20015
- raw_printf(p->out, "oomCounter = %d\n", oomCounter);
20016
- raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
20017
- }
20018
- }else
20019
-#endif /* SQLITE_DEBUG */
20020
-
2002120052
if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
20053
+ const char *zFN = 0; /* Pointer to constant filename */
2002220054
char *zNewFilename = 0; /* Name of the database file to open */
2002320055
int iName = 1; /* Index in azArg[] of the filename */
2002420056
int newFlag = 0; /* True to delete file before opening */
2002520057
int openMode = SHELL_OPEN_UNSPEC;
2002620058
@@ -20049,16 +20081,16 @@
2004920081
#endif /* SQLITE_OMIT_DESERIALIZE */
2005020082
}else if( z[0]=='-' ){
2005120083
utf8_printf(stderr, "unknown option: %s\n", z);
2005220084
rc = 1;
2005320085
goto meta_command_exit;
20054
- }else if( zNewFilename ){
20086
+ }else if( zFN ){
2005520087
utf8_printf(stderr, "extra argument: \"%s\"\n", z);
2005620088
rc = 1;
2005720089
goto meta_command_exit;
2005820090
}else{
20059
- zNewFilename = sqlite3_mprintf("%s", z);
20091
+ zFN = z;
2006020092
}
2006120093
}
2006220094
2006320095
/* Close the existing database */
2006420096
session_close_all(p, -1);
@@ -20070,19 +20102,25 @@
2007020102
p->openMode = openMode;
2007120103
p->openFlags = 0;
2007220104
p->szMax = 0;
2007320105
2007420106
/* If a filename is specified, try to open it first */
20075
- if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
20076
- if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
20107
+ if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20108
+ if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
2007720109
if( p->bSafeMode
2007820110
&& p->openMode!=SHELL_OPEN_HEXDB
20079
- && zNewFilename
20080
- && strcmp(zNewFilename,":memory:")!=0
20111
+ && zFN
20112
+ && strcmp(zFN,":memory:")!=0
2008120113
){
2008220114
failIfSafeMode(p, "cannot open disk-based database files in safe mode");
2008320115
}
20116
+ if( zFN ){
20117
+ zNewFilename = sqlite3_mprintf("%s", zFN);
20118
+ shell_check_oom(zNewFilename);
20119
+ }else{
20120
+ zNewFilename = 0;
20121
+ }
2008420122
p->pAuxDb->zDbFilename = zNewFilename;
2008520123
open_db(p, OPEN_DB_KEEPALIVE);
2008620124
if( p->db==0 ){
2008720125
utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
2008820126
sqlite3_free(zNewFilename);
@@ -20132,11 +20170,11 @@
2013220170
rc = 1;
2013320171
goto meta_command_exit;
2013420172
}
2013520173
}else if( zFile==0 && eMode!='e' && eMode!='x' ){
2013620174
zFile = sqlite3_mprintf("%s", z);
20137
- if( zFile[0]=='|' ){
20175
+ if( zFile && zFile[0]=='|' ){
2013820176
while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
2013920177
break;
2014020178
}
2014120179
}else{
2014220180
utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
@@ -20145,11 +20183,13 @@
2014520183
rc = 1;
2014620184
sqlite3_free(zFile);
2014720185
goto meta_command_exit;
2014820186
}
2014920187
}
20150
- if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
20188
+ if( zFile==0 ){
20189
+ zFile = sqlite3_mprintf("stdout");
20190
+ }
2015120191
if( bOnce ){
2015220192
p->outCount = 2;
2015320193
}else{
2015420194
p->outCount = 0;
2015520195
}
@@ -20172,10 +20212,11 @@
2017220212
}
2017320213
sqlite3_free(zFile);
2017420214
zFile = sqlite3_mprintf("%s", p->zTempFile);
2017520215
}
2017620216
#endif /* SQLITE_NOHAVE_SYSTEM */
20217
+ shell_check_oom(zFile);
2017720218
if( zFile[0]=='|' ){
2017820219
#ifdef SQLITE_OMIT_POPEN
2017920220
raw_printf(stderr, "Error: pipes are not supported in this OS\n");
2018020221
rc = 1;
2018120222
p->out = stdout;
@@ -20268,21 +20309,21 @@
2026820309
const char *zValue = azArg[3];
2026920310
bind_table_init(p);
2027020311
zSql = sqlite3_mprintf(
2027120312
"REPLACE INTO temp.sqlite_parameters(key,value)"
2027220313
"VALUES(%Q,%s);", zKey, zValue);
20273
- if( zSql==0 ) shell_out_of_memory();
20314
+ shell_check_oom(zSql);
2027420315
pStmt = 0;
2027520316
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2027620317
sqlite3_free(zSql);
2027720318
if( rx!=SQLITE_OK ){
2027820319
sqlite3_finalize(pStmt);
2027920320
pStmt = 0;
2028020321
zSql = sqlite3_mprintf(
2028120322
"REPLACE INTO temp.sqlite_parameters(key,value)"
2028220323
"VALUES(%Q,%Q);", zKey, zValue);
20283
- if( zSql==0 ) shell_out_of_memory();
20324
+ shell_check_oom(zSql);
2028420325
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2028520326
sqlite3_free(zSql);
2028620327
if( rx!=SQLITE_OK ){
2028720328
utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
2028820329
sqlite3_finalize(pStmt);
@@ -20299,11 +20340,11 @@
2029920340
** exists.
2030020341
*/
2030120342
if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
2030220343
char *zSql = sqlite3_mprintf(
2030320344
"DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20304
- if( zSql==0 ) shell_out_of_memory();
20345
+ shell_check_oom(zSql);
2030520346
sqlite3_exec(p->db, zSql, 0, 0, 0);
2030620347
sqlite3_free(zSql);
2030720348
}else
2030820349
/* If no command name matches, show a syntax error */
2030920350
parameter_syntax_error:
@@ -20525,10 +20566,11 @@
2052520566
" name text,\n"
2052620567
" tbl_name text,\n"
2052720568
" rootpage integer,\n"
2052820569
" sql text\n"
2052920570
")", zName);
20571
+ shell_check_oom(new_argv[0]);
2053020572
new_argv[1] = 0;
2053120573
new_colv[0] = "sql";
2053220574
new_colv[1] = 0;
2053320575
callback(&data, 1, new_argv, new_colv);
2053420576
sqlite3_free(new_argv[0]);
@@ -20576,12 +20618,14 @@
2057620618
}
2057720619
#endif
2057820620
appendText(&sSelect, ") WHERE ", 0);
2057920621
if( zName ){
2058020622
char *zQarg = sqlite3_mprintf("%Q", zName);
20581
- int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20582
- strchr(zName, '[') != 0;
20623
+ int bGlob;
20624
+ shell_check_oom(zQarg);
20625
+ bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20626
+ strchr(zName, '[') != 0;
2058320627
if( strchr(zName, '.') ){
2058420628
appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
2058520629
}else{
2058620630
appendText(&sSelect, "lower(tbl_name)", 0);
2058720631
}
@@ -20740,11 +20784,12 @@
2074020784
if( pSession->azFilter==0 ){
2074120785
raw_printf(stderr, "Error: out or memory\n");
2074220786
exit(1);
2074320787
}
2074420788
for(ii=1; ii<nCmd; ii++){
20745
- pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20789
+ char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20790
+ shell_check_oom(x);
2074620791
}
2074720792
pSession->nFilter = ii-1;
2074820793
}
2074920794
}else
2075020795
@@ -20812,10 +20857,11 @@
2081220857
}
2081320858
pSession->nFilter = 0;
2081420859
sqlite3session_table_filter(pSession->p, session_filter, pSession);
2081520860
pAuxDb->nSession++;
2081620861
pSession->zName = sqlite3_mprintf("%s", zName);
20862
+ shell_check_oom(pSession->zName);
2081720863
}else
2081820864
/* If no command name matches, show a syntax error */
2081920865
session_syntax_error:
2082020866
showHelp(p->out, "session");
2082120867
}else
@@ -20905,15 +20951,16 @@
2090520951
int tno = sqlite3_column_int(pStmt, 0);
2090620952
const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
2090720953
const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
2090820954
const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
2090920955
20956
+ if( zOp==0 ) continue;
20957
+ if( zSql==0 ) continue;
20958
+ if( zAns==0 ) continue;
2091020959
k = 0;
2091120960
if( bVerbose>0 ){
20912
- char *zQuote = sqlite3_mprintf("%q", zSql);
2091320961
printf("%d: %s %s\n", tno, zOp, zSql);
20914
- sqlite3_free(zQuote);
2091520962
}
2091620963
if( strcmp(zOp,"memo")==0 ){
2091720964
utf8_printf(p->out, "%s\n", zSql);
2091820965
}else
2091920966
if( strcmp(zOp,"run")==0 ){
@@ -21027,10 +21074,11 @@
2102721074
initText(&sSql);
2102821075
appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
2102921076
zSep = "VALUES(";
2103021077
while( SQLITE_ROW==sqlite3_step(pStmt) ){
2103121078
const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21079
+ if( zTab==0 ) continue;
2103221080
if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
2103321081
if( strncmp(zTab, "sqlite_",7)!=0 ){
2103421082
appendText(&sQuery,"SELECT * FROM ", 0);
2103521083
appendText(&sQuery,zTab,'"');
2103621084
appendText(&sQuery," NOT INDEXED;", 0);
@@ -21067,10 +21115,11 @@
2106721115
"%s))"
2106821116
" SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
2106921117
" FROM [sha3sum$query]",
2107021118
sSql.z, iSize);
2107121119
}
21120
+ shell_check_oom(zSql);
2107221121
freeText(&sQuery);
2107321122
freeText(&sSql);
2107421123
if( bDebug ){
2107521124
utf8_printf(p->out, "%s\n", zSql);
2107621125
}else{
@@ -21090,15 +21139,15 @@
2109021139
raw_printf(stderr, "Usage: .system COMMAND\n");
2109121140
rc = 1;
2109221141
goto meta_command_exit;
2109321142
}
2109421143
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21095
- for(i=2; i<nArg; i++){
21144
+ for(i=2; i<nArg && zCmd!=0; i++){
2109621145
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
2109721146
zCmd, azArg[i]);
2109821147
}
21099
- x = system(zCmd);
21148
+ x = zCmd!=0 ? system(zCmd) : 1;
2110021149
sqlite3_free(zCmd);
2110121150
if( x ) raw_printf(stderr, "System command returns %d\n", x);
2110221151
}else
2110321152
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
2110421153
@@ -21230,16 +21279,16 @@
2123021279
while( sqlite3_step(pStmt)==SQLITE_ROW ){
2123121280
if( nRow>=nAlloc ){
2123221281
char **azNew;
2123321282
int n2 = nAlloc*2 + 10;
2123421283
azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21235
- if( azNew==0 ) shell_out_of_memory();
21284
+ shell_check_oom(azNew);
2123621285
nAlloc = n2;
2123721286
azResult = azNew;
2123821287
}
2123921288
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21240
- if( 0==azResult[nRow] ) shell_out_of_memory();
21289
+ shell_check_oom(azResult[nRow]);
2124121290
nRow++;
2124221291
}
2124321292
if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
2124421293
rc = shellDatabaseError(p->db);
2124521294
}
@@ -22004,11 +22053,11 @@
2200422053
nLine = strlen30(zLine);
2200522054
if( nSql+nLine+2>=nAlloc ){
2200622055
/* Grow buffer by half-again increments when big. */
2200722056
nAlloc = nSql+(nSql>>1)+nLine+100;
2200822057
zSql = realloc(zSql, nAlloc);
22009
- if( zSql==0 ) shell_out_of_memory();
22058
+ shell_check_oom(zSql);
2201022059
}
2201122060
if( nSql==0 ){
2201222061
int i;
2201322062
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2201422063
assert( nAlloc>0 && zSql!=0 );
@@ -22136,10 +22185,11 @@
2213622185
raw_printf(stderr, "-- warning: cannot find home directory;"
2213722186
" cannot read ~/.sqliterc\n");
2213822187
return;
2213922188
}
2214022189
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
22190
+ shell_check_oom(zBuf);
2214122191
sqliterc = zBuf;
2214222192
}
2214322193
p->in = fopen(sqliterc,"rb");
2214422194
if( p->in ){
2214522195
if( stdin_is_interactive ){
@@ -22334,14 +22384,10 @@
2233422384
setBinaryMode(stdin, 0);
2233522385
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
2233622386
stdin_is_interactive = isatty(0);
2233722387
stdout_is_console = isatty(1);
2233822388
22339
-#ifdef SQLITE_DEBUG
22340
- registerOomSimulator();
22341
-#endif
22342
-
2234322389
#if !defined(_WIN32_WCE)
2234422390
if( getenv("SQLITE_DEBUG_BREAK") ){
2234522391
if( isatty(0) && isatty(2) ){
2234622392
fprintf(stderr,
2234722393
"attach debugger to process %d and press any key to continue.\n",
@@ -22377,20 +22423,20 @@
2237722423
** memory that does not come from the SQLite memory allocator.
2237822424
*/
2237922425
#if !SQLITE_SHELL_IS_UTF8
2238022426
sqlite3_initialize();
2238122427
argvToFree = malloc(sizeof(argv[0])*argc*2);
22428
+ shell_check_oom(argvToFree);
2238222429
argcToFree = argc;
2238322430
argv = argvToFree + argc;
22384
- if( argv==0 ) shell_out_of_memory();
2238522431
for(i=0; i<argc; i++){
2238622432
char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
2238722433
int n;
22388
- if( z==0 ) shell_out_of_memory();
22434
+ shell_check_oom(z);
2238922435
n = (int)strlen(z);
2239022436
argv[i] = malloc( n+1 );
22391
- if( argv[i]==0 ) shell_out_of_memory();
22437
+ shell_check_oom(argv[i]);
2239222438
memcpy(argv[i], z, n+1);
2239322439
argvToFree[i] = argv[i];
2239422440
sqlite3_free(z);
2239522441
}
2239622442
sqlite3_shutdown();
@@ -22436,11 +22482,11 @@
2243622482
/* Excesss arguments are interpreted as SQL (or dot-commands) and
2243722483
** mean that nothing is read from stdin */
2243822484
readStdin = 0;
2243922485
nCmd++;
2244022486
azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
22441
- if( azCmd==0 ) shell_out_of_memory();
22487
+ shell_check_oom(azCmd);
2244222488
azCmd[nCmd-1] = z;
2244322489
}
2244422490
}
2244522491
if( z[1]=='-' ) z++;
2244622492
if( strcmp(z,"-separator")==0
2244722493
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -443,19 +443,10 @@
443 /*
444 ** True if an interrupt (Control-C) has been received.
445 */
446 static volatile int seenInterrupt = 0;
447
448 #ifdef SQLITE_DEBUG
449 /*
450 ** Out-of-memory simulator variables
451 */
452 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
453 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
454 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
455 #endif /* SQLITE_DEBUG */
456
457 /*
458 ** This is the name of our program. It is set in main(), used
459 ** in a number of other places, mostly for error messages.
460 */
461 static char *Argv0;
@@ -503,52 +494,16 @@
503 static void shell_out_of_memory(void){
504 raw_printf(stderr,"Error: out of memory\n");
505 exit(1);
506 }
507
508 #ifdef SQLITE_DEBUG
509 /* This routine is called when a simulated OOM occurs. It is broken
510 ** out as a separate routine to make it easy to set a breakpoint on
511 ** the OOM
512 */
513 void shellOomFault(void){
514 if( oomRepeat>0 ){
515 oomRepeat--;
516 }else{
517 oomCounter--;
518 }
519 }
520 #endif /* SQLITE_DEBUG */
521
522 #ifdef SQLITE_DEBUG
523 /* This routine is a replacement malloc() that is used to simulate
524 ** Out-Of-Memory (OOM) errors for testing purposes.
525 */
526 static void *oomMalloc(int nByte){
527 if( oomCounter ){
528 if( oomCounter==1 ){
529 shellOomFault();
530 return 0;
531 }else{
532 oomCounter--;
533 }
534 }
535 return defaultMalloc(nByte);
536 }
537 #endif /* SQLITE_DEBUG */
538
539 #ifdef SQLITE_DEBUG
540 /* Register the OOM simulator. This must occur before any memory
541 ** allocations */
542 static void registerOomSimulator(void){
543 sqlite3_mem_methods mem;
544 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
545 defaultMalloc = mem.xMalloc;
546 mem.xMalloc = oomMalloc;
547 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
548 }
549 #endif
550
551 /*
552 ** Write I/O traces to the following stream.
553 */
554 #ifdef SQLITE_ENABLE_IOTRACE
@@ -701,11 +656,11 @@
701
702 while( 1 ){
703 if( n+100>nLine ){
704 nLine = nLine*2 + 100;
705 zLine = realloc(zLine, nLine);
706 if( zLine==0 ) shell_out_of_memory();
707 }
708 if( fgets(&zLine[n], nLine - n, in)==0 ){
709 if( n==0 ){
710 free(zLine);
711 return 0;
@@ -728,11 +683,11 @@
728 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
729 if( zTrans ){
730 int nTrans = strlen30(zTrans)+1;
731 if( nTrans>nLine ){
732 zLine = realloc(zLine, nTrans);
733 if( zLine==0 ) shell_out_of_memory();
734 }
735 memcpy(zLine, zTrans, nTrans);
736 sqlite3_free(zTrans);
737 }
738 }
@@ -875,11 +830,11 @@
875 }
876
877 if( p->z==0 || p->n+len>=p->nAlloc ){
878 p->nAlloc = p->nAlloc*2 + len + 20;
879 p->z = realloc(p->z, p->nAlloc);
880 if( p->z==0 ) shell_out_of_memory();
881 }
882
883 if( quote ){
884 char *zCsr = p->z+p->n;
885 *zCsr++ = quote;
@@ -930,10 +885,11 @@
930 char *zDiv = "(";
931 int nRow = 0;
932
933 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
934 zSchema ? zSchema : "main", zName);
 
935 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
936 sqlite3_free(zSql);
937 initText(&s);
938 if( zSchema ){
939 cQuote = quoteChar(zSchema);
@@ -946,10 +902,11 @@
946 while( sqlite3_step(pStmt)==SQLITE_ROW ){
947 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
948 nRow++;
949 appendText(&s, zDiv, 0);
950 zDiv = ",";
 
951 cQuote = quoteChar(zCol);
952 appendText(&s, zCol, cQuote);
953 }
954 appendText(&s, ")", 0);
955 sqlite3_finalize(pStmt);
@@ -969,13 +926,15 @@
969 static void shellModuleSchema(
970 sqlite3_context *pCtx,
971 int nVal,
972 sqlite3_value **apVal
973 ){
974 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
975 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
976 UNUSED_PARAMETER(nVal);
 
 
977 if( zFake ){
978 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
979 -1, sqlite3_free);
980 free(zFake);
981 }
@@ -1859,10 +1818,11 @@
1859 SHA3Context *p,
1860 const unsigned char *aData,
1861 unsigned int nData
1862 ){
1863 unsigned int i = 0;
 
1864 #if SHA3_BYTEORDER==1234
1865 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1866 for(; i+7<nData; i+=8){
1867 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1868 p->nLoaded += 8;
@@ -2517,14 +2477,15 @@
2517 const char *zFile, /* File to write */
2518 sqlite3_value *pData, /* Data to write */
2519 mode_t mode, /* MODE parameter passed to writefile() */
2520 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2521 ){
 
2522 #if !defined(_WIN32) && !defined(WIN32)
2523 if( S_ISLNK(mode) ){
2524 const char *zTo = (const char*)sqlite3_value_text(pData);
2525 if( symlink(zTo, zFile)<0 ) return 1;
2526 }else
2527 #endif
2528 {
2529 if( S_ISDIR(mode) ){
2530 if( mkdir(zFile, mode) ){
@@ -5886,11 +5847,11 @@
5886 if( (idxNum & 3)==3 ){
5887 /* Both start= and stop= boundaries are available. This is the
5888 ** the preferred case */
5889 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5890 pIdxInfo->estimatedRows = 1000;
5891 if( pIdxInfo->nOrderBy==1 ){
5892 if( pIdxInfo->aOrderBy[0].desc ){
5893 idxNum |= 8;
5894 }else{
5895 idxNum |= 16;
5896 }
@@ -6721,17 +6682,19 @@
6721 const sqlite3_api_routines *pApi
6722 ){
6723 int rc = SQLITE_OK;
6724 SQLITE_EXTENSION_INIT2(pApi);
6725 (void)pzErrMsg; /* Unused */
6726 rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6727 0, re_sql_func, 0, 0);
 
6728 if( rc==SQLITE_OK ){
6729 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6730 ** of regexp(PATTERN,STRING). */
6731 rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6732 (void*)db, re_sql_func, 0, 0);
 
6733 }
6734 return rc;
6735 }
6736
6737 /************************* End ../ext/misc/regexp.c ********************/
@@ -9939,21 +9902,29 @@
9939 IdxTable **ppOut, /* OUT: New object (if successful) */
9940 char **pzErrmsg /* OUT: Error message (if not) */
9941 ){
9942 sqlite3_stmt *p1 = 0;
9943 int nCol = 0;
9944 int nTab = STRLEN(zTab);
9945 int nByte = sizeof(IdxTable) + nTab + 1;
9946 IdxTable *pNew = 0;
9947 int rc, rc2;
9948 char *pCsr = 0;
9949 int nPk = 0;
9950
 
 
 
 
9951 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9952 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9953 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9954 const char *zColSeq = 0;
 
 
 
 
9955 nByte += 1 + STRLEN(zCol);
9956 rc = sqlite3_table_column_metadata(
9957 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9958 );
9959 if( zColSeq==0 ) zColSeq = "binary";
@@ -9976,11 +9947,13 @@
9976
9977 nCol = 0;
9978 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9979 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9980 const char *zColSeq = 0;
9981 int nCopy = STRLEN(zCol) + 1;
 
 
9982 pNew->aCol[nCol].zName = pCsr;
9983 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9984 memcpy(pCsr, zCol, nCopy);
9985 pCsr += nCopy;
9986
@@ -10128,10 +10101,11 @@
10128 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10129 int bMatch = 1;
10130 IdxConstraint *pT = pTail;
10131 sqlite3_stmt *pInfo = 0;
10132 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
 
10133
10134 /* Zero the IdxConstraint.bFlag values in the pEq list */
10135 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10136
10137 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
@@ -10539,10 +10513,11 @@
10539
10540 /* Create the table and its triggers in the temp schema */
10541 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10542 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10543 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
 
10544 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10545 }
10546 idxFinalize(&rc, pSelect);
10547
10548 /* Rename the table in the temp schema to zInt */
@@ -10641,12 +10616,13 @@
10641 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10642 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10643 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10644 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10645
 
10646 if( zType[0]=='v' || zType[1]=='r' ){
10647 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10648 }else{
10649 IdxTable *pTab;
10650 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10651 if( rc==SQLITE_OK ){
10652 int i;
@@ -10779,10 +10755,11 @@
10779 break;
10780
10781 case SQLITE_BLOB:
10782 case SQLITE_TEXT: {
10783 int nByte = sqlite3_value_bytes(argv[1]);
 
10784 if( nByte>pSlot->nByte ){
10785 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10786 if( zNew==0 ){
10787 sqlite3_result_error_nomem(pCtx);
10788 return;
@@ -10790,13 +10767,15 @@
10790 pSlot->nByte = nByte*2;
10791 pSlot->z = zNew;
10792 }
10793 pSlot->n = nByte;
10794 if( pSlot->eType==SQLITE_BLOB ){
10795 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
 
10796 }else{
10797 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
 
10798 }
10799 break;
10800 }
10801 }
10802 }
@@ -11003,10 +10982,11 @@
11003
11004 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
11005 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
11006 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
11007 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
 
11008 if( p->iSample<100 && iPrev!=iRowid ){
11009 samplectx.target = (double)p->iSample / 100.0;
11010 samplectx.iTarget = p->iSample;
11011 samplectx.nRow = 0.0;
11012 samplectx.nRet = 0.0;
@@ -11069,18 +11049,18 @@
11069 }
11070
11071
11072 /* Copy the entire schema of database [db] into [dbm]. */
11073 if( rc==SQLITE_OK ){
11074 sqlite3_stmt *pSql;
11075 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
11076 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
11077 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
11078 );
11079 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11080 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11081 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11082 }
11083 idxFinalize(&rc, pSql);
11084 }
11085
11086 /* Create the vtab schema */
@@ -12831,10 +12811,11 @@
12831 break;
12832 }
12833 }
12834 if( i==0 || strstr(z, p->colSeparator)!=0 ){
12835 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
 
12836 utf8_printf(out, "%s", zQuoted);
12837 sqlite3_free(zQuoted);
12838 }else{
12839 utf8_printf(out, "%s", z);
12840 }
@@ -13005,11 +12986,11 @@
13005 int nText = strlen30(zText);
13006 if( p->autoEQPtest ){
13007 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13008 }
13009 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13010 if( pNew==0 ) shell_out_of_memory();
13011 pNew->iEqpId = iEqpId;
13012 pNew->iParentId = p2;
13013 memcpy(pNew->zText, zText, nText+1);
13014 pNew->pNext = 0;
13015 if( p->sGraph.pLast ){
@@ -13226,10 +13207,11 @@
13226 ){
13227 utf8_printf(p->out, "%s;\n", azArg[0]);
13228 break;
13229 }
13230 z = sqlite3_mprintf("%s", azArg[0]);
 
13231 j = 0;
13232 for(i=0; IsSpace(z[i]); i++){}
13233 for(; (c = z[i])!=0; i++){
13234 if( IsSpace(c) ){
13235 if( z[j-1]=='\r' ) z[j-1] = '\n';
@@ -13357,10 +13339,11 @@
13357 raw_printf(p->out,"(");
13358 for(i=0; i<nArg; i++){
13359 if( i>0 ) raw_printf(p->out, ",");
13360 if( quoteChar(azCol[i]) ){
13361 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
 
13362 utf8_printf(p->out, "%s", z);
13363 sqlite3_free(z);
13364 }else{
13365 raw_printf(p->out, "%s", azCol[i]);
13366 }
@@ -13602,20 +13585,61 @@
13602 if( zName==0 ) return;
13603 cQuote = quoteChar(zName);
13604 n = strlen30(zName);
13605 if( cQuote ) n += n+2;
13606 z = p->zDestTable = malloc( n+1 );
13607 if( z==0 ) shell_out_of_memory();
13608 n = 0;
13609 if( cQuote ) z[n++] = cQuote;
13610 for(i=0; zName[i]; i++){
13611 z[n++] = zName[i];
13612 if( zName[i]==cQuote ) z[n++] = cQuote;
13613 }
13614 if( cQuote ) z[n++] = cQuote;
13615 z[n] = 0;
13616 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13617
13618
13619 /*
13620 ** Execute a query statement that will generate SQL output. Print
13621 ** the result columns, comma-separated, on a line and then add a
@@ -13635,12 +13659,14 @@
13635 int nResult;
13636 int i;
13637 const char *z;
13638 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13639 if( rc!=SQLITE_OK || !pSelect ){
13640 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13641 sqlite3_errmsg(p->db));
 
 
13642 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13643 return rc;
13644 }
13645 rc = sqlite3_step(pSelect);
13646 nResult = sqlite3_column_count(pSelect);
@@ -13672,15 +13698,21 @@
13672 ** Allocate space and save off string indicating current error.
13673 */
13674 static char *save_err_msg(
13675 sqlite3 *db, /* Database to query */
13676 const char *zWhen, /* Qualifier (format) wrapper */
13677 int rc /* Error code returned from API */
 
13678 ){
13679 if( zWhen==0 )
13680 zWhen = "%s (%d)";
13681 return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
 
 
 
 
 
13682 }
13683
13684 #ifdef __linux__
13685 /*
13686 ** Attempt to display I/O stats on Linux using /proc/PID/io
@@ -14024,13 +14056,13 @@
14024 }
14025 }
14026 }
14027 nAlloc += 100;
14028 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14029 if( p->aiIndent==0 ) shell_out_of_memory();
14030 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14031 if( abYield==0 ) shell_out_of_memory();
14032 }
14033 abYield[iOp] = str_in_array(zOp, azYield);
14034 p->aiIndent[iOp] = 0;
14035 p->nIndent = iOp+1;
14036
@@ -14235,29 +14267,29 @@
14235 if( rc!=SQLITE_ROW ) return;
14236 nColumn = sqlite3_column_count(pStmt);
14237 nAlloc = nColumn*4;
14238 if( nAlloc<=0 ) nAlloc = 1;
14239 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14240 if( azData==0 ) shell_out_of_memory();
14241 for(i=0; i<nColumn; i++){
14242 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14243 }
14244 do{
14245 if( (nRow+2)*nColumn >= nAlloc ){
14246 nAlloc *= 2;
14247 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14248 if( azData==0 ) shell_out_of_memory();
14249 }
14250 nRow++;
14251 for(i=0; i<nColumn; i++){
14252 z = (const char*)sqlite3_column_text(pStmt,i);
14253 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14254 }
14255 }while( sqlite3_step(pStmt)==SQLITE_ROW );
14256 if( nColumn>p->nWidth ){
14257 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14258 if( p->colWidth==0 ) shell_out_of_memory();
14259 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14260 p->nWidth = nColumn;
14261 p->actualWidth = &p->colWidth[nColumn];
14262 }
14263 memset(p->actualWidth, 0, nColumn*sizeof(int));
@@ -14436,11 +14468,14 @@
14436 } while( SQLITE_ROW == rc );
14437 sqlite3_free(pData);
14438 if( pArg->cMode==MODE_Json ){
14439 fputs("]\n", pArg->out);
14440 }else if( pArg->cMode==MODE_Count ){
14441 printf("%llu row%s\n", nRow, nRow!=1 ? "s" : "");
 
 
 
14442 }
14443 }
14444 }
14445 }
14446
@@ -14560,18 +14595,19 @@
14560 }
14561
14562 if( rc==SQLITE_OK ){
14563 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14564 if( pState->expert.pExpert==0 ){
14565 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14566 rc = SQLITE_ERROR;
14567 }else{
14568 sqlite3_expert_config(
14569 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14570 );
14571 }
14572 }
 
14573
14574 return rc;
14575 }
14576 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14577
@@ -14609,11 +14645,11 @@
14609 while( zSql[0] && (SQLITE_OK == rc) ){
14610 static const char *zStmtSql;
14611 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14612 if( SQLITE_OK != rc ){
14613 if( pzErrMsg ){
14614 *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
14615 }
14616 }else{
14617 if( !pStmt ){
14618 /* this happens for a comment or white-space */
14619 zSql = zLeftover;
@@ -14644,10 +14680,11 @@
14644 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14645 if( pArg->autoEQP>=AUTOEQP_trigger ){
14646 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14647 }
14648 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
 
14649 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14650 if( rc==SQLITE_OK ){
14651 while( sqlite3_step(pExplain)==SQLITE_ROW ){
14652 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14653 int iEqpId = sqlite3_column_int(pExplain, 0);
@@ -14661,10 +14698,11 @@
14661 sqlite3_finalize(pExplain);
14662 sqlite3_free(zEQP);
14663 if( pArg->autoEQP>=AUTOEQP_full ){
14664 /* Also do an EXPLAIN for ".eqp full" mode */
14665 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
 
14666 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14667 if( rc==SQLITE_OK ){
14668 pArg->cMode = MODE_Explain;
14669 explain_data_prepare(pArg, pExplain);
14670 exec_prepared_stmt(pArg, pExplain);
@@ -14723,11 +14761,11 @@
14723 if( rc!=SQLITE_NOMEM ) rc = rc2;
14724 if( rc==SQLITE_OK ){
14725 zSql = zLeftover;
14726 while( IsSpace(zSql[0]) ) zSql++;
14727 }else if( pzErrMsg ){
14728 *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
14729 }
14730
14731 /* clear saved stmt handle */
14732 if( pArg ){
14733 pArg->pStmt = NULL;
@@ -14773,20 +14811,22 @@
14773 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
14774 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14775 int rc;
14776
14777 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
 
14778 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14779 sqlite3_free(zSql);
14780 if( rc ) return 0;
14781 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14782 if( nCol>=nAlloc-2 ){
14783 nAlloc = nAlloc*2 + nCol + 10;
14784 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14785 if( azCol==0 ) shell_out_of_memory();
14786 }
14787 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
 
14788 if( sqlite3_column_int(pStmt, 5) ){
14789 nPK++;
14790 if( nPK==1
14791 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14792 "INTEGER")==0
@@ -14816,10 +14856,11 @@
14816 ** there is a "pk" entry in "PRAGMA index_list". There will be
14817 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14818 */
14819 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14820 " WHERE origin='pk'", zTab);
 
14821 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14822 sqlite3_free(zSql);
14823 if( rc ){
14824 freeColumnList(azCol);
14825 return 0;
@@ -14907,10 +14948,11 @@
14907 }
14908 zIns = sqlite3_mprintf(
14909 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14910 "VALUES('table','%q','%q',0,'%q');",
14911 zTable, zTable, zSql);
 
14912 utf8_printf(p->out, "%s\n", zIns);
14913 sqlite3_free(zIns);
14914 return 0;
14915 }else{
14916 printSchemaLine(p->out, zSql, ";\n");
@@ -15150,13 +15192,10 @@
15150 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15151 " If FILE begins with '|' then open as a pipe",
15152 " --bom Put a UTF8 byte-order mark at the beginning",
15153 " -e Send output to the system text editor",
15154 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
15155 #ifdef SQLITE_DEBUG
15156 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
15157 #endif
15158 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
15159 " Options:",
15160 " --append Use appendvfs to append database to the end of FILE",
15161 #ifndef SQLITE_OMIT_DESERIALIZE
15162 " --deserialize Load into memory using sqlite3_deserialize()",
@@ -15188,11 +15227,12 @@
15188 " --quiet|-q No output except at interrupts",
15189 " --reset Reset the count for each input and interrupt",
15190 #endif
15191 ".prompt MAIN CONTINUE Replace the standard prompts",
15192 ".quit Exit this program",
15193 ".read FILE Read input from FILE",
 
15194 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15195 ".recover Recover as much data as possible from corrupt db.",
15196 " --freelist-corrupt Assume the freelist is corrupt",
15197 " --recovery-db NAME Store recovery metadata in database file NAME",
15198 " --lost-and-found TABLE Alternative name for the lost-and-found table",
@@ -15308,10 +15348,11 @@
15308 }
15309 }
15310 }else{
15311 /* Look for commands that for which zPattern is an exact prefix */
15312 zPat = sqlite3_mprintf(".%s*", zPattern);
 
15313 for(i=0; i<ArraySize(azHelp); i++){
15314 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15315 utf8_printf(out, "%s\n", azHelp[i]);
15316 j = i+1;
15317 n++;
@@ -15330,10 +15371,11 @@
15330 return n;
15331 }
15332 /* Look for commands that contain zPattern anywhere. Show the complete
15333 ** text of all commands that match. */
15334 zPat = sqlite3_mprintf("%%%s%%", zPattern);
 
15335 for(i=0; i<ArraySize(azHelp); i++){
15336 if( azHelp[i][0]=='.' ) j = i;
15337 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15338 utf8_printf(out, "%s\n", azHelp[j]);
15339 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
@@ -15518,14 +15560,11 @@
15518 if( rc!=2 ) goto readHexDb_error;
15519 if( n<0 ) goto readHexDb_error;
15520 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15521 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
15522 a = sqlite3_malloc( n ? n : 1 );
15523 if( a==0 ){
15524 utf8_printf(stderr, "Out of memory!\n");
15525 goto readHexDb_error;
15526 }
15527 memset(a, 0, n);
15528 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15529 utf8_printf(stderr, "invalid pagesize\n");
15530 goto readHexDb_error;
15531 }
@@ -15652,11 +15691,11 @@
15652 int argc,
15653 sqlite3_value **argv
15654 ){
15655 const char *zText = (const char*)sqlite3_value_text(argv[0]);
15656 UNUSED_PARAMETER(argc);
15657 if( zText[0]=='\'' ){
15658 int nText = sqlite3_value_bytes(argv[0]);
15659 int i;
15660 char zBuf1[20];
15661 char zBuf2[20];
15662 const char *zNL = 0;
@@ -15829,10 +15868,11 @@
15829 editFunc, 0, 0);
15830 #endif
15831 if( p->openMode==SHELL_OPEN_ZIPFILE ){
15832 char *zSql = sqlite3_mprintf(
15833 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
 
15834 sqlite3_exec(p->db, zSql, 0, 0, 0);
15835 sqlite3_free(zSql);
15836 }
15837 #ifndef SQLITE_OMIT_DESERIALIZE
15838 else
@@ -15886,15 +15926,17 @@
15886 if( state==0 ){
15887 char *zSql;
15888 sqlite3_finalize(pStmt);
15889 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15890 " FROM completion(%Q) ORDER BY 1", text);
 
15891 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15892 sqlite3_free(zSql);
15893 }
15894 if( sqlite3_step(pStmt)==SQLITE_ROW ){
15895 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
 
15896 }else{
15897 sqlite3_finalize(pStmt);
15898 pStmt = 0;
15899 zRet = 0;
15900 }
@@ -15923,17 +15965,18 @@
15923 iStart = i+1;
15924 memcpy(zBuf, zLine, iStart);
15925 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15926 " FROM completion(%Q,%Q) ORDER BY 1",
15927 &zLine[iStart], zLine);
 
15928 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15929 sqlite3_free(zSql);
15930 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15931 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15932 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15933 int nCompletion = sqlite3_column_bytes(pStmt, 0);
15934 if( iStart+nCompletion < sizeof(zBuf)-1 ){
15935 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15936 linenoiseAddCompletion(lc, zBuf);
15937 }
15938 }
15939 sqlite3_finalize(pStmt);
@@ -16164,11 +16207,11 @@
16164 /* Append a single byte to z[] */
16165 static void import_append_char(ImportCtx *p, int c){
16166 if( p->n+1>=p->nAlloc ){
16167 p->nAlloc += p->nAlloc + 100;
16168 p->z = sqlite3_realloc64(p->z, p->nAlloc);
16169 if( p->z==0 ) shell_out_of_memory();
16170 }
16171 p->z[p->n++] = (char)c;
16172 }
16173
16174 /* Read a single field of CSV text. Compatible with rfc4180 and extended
@@ -16316,20 +16359,21 @@
16316 int k = 0;
16317 int cnt = 0;
16318 const int spinRate = 10000;
16319
16320 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
 
16321 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16322 if( rc ){
16323 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16324 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16325 zQuery);
16326 goto end_data_xfer;
16327 }
16328 n = sqlite3_column_count(pQuery);
16329 zInsert = sqlite3_malloc64(200 + nTable + n*3);
16330 if( zInsert==0 ) shell_out_of_memory();
16331 sqlite3_snprintf(200+nTable,zInsert,
16332 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16333 i = strlen30(zInsert);
16334 for(j=1; j<n; j++){
16335 memcpy(zInsert+i, ",?", 2);
@@ -16388,10 +16432,11 @@
16388 if( rc==SQLITE_DONE ) break;
16389 sqlite3_finalize(pQuery);
16390 sqlite3_free(zQuery);
16391 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16392 zTable);
 
16393 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16394 if( rc ){
16395 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16396 break;
16397 }
@@ -16424,10 +16469,11 @@
16424 const unsigned char *zSql;
16425 char *zErrMsg = 0;
16426
16427 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16428 " WHERE %s", zWhere);
 
16429 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16430 if( rc ){
16431 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16432 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16433 zQuery);
@@ -16434,10 +16480,11 @@
16434 goto end_schema_xfer;
16435 }
16436 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16437 zName = sqlite3_column_text(pQuery, 0);
16438 zSql = sqlite3_column_text(pQuery, 1);
 
16439 printf("%s... ", zName); fflush(stdout);
16440 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16441 if( zErrMsg ){
16442 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16443 sqlite3_free(zErrMsg);
@@ -16451,10 +16498,11 @@
16451 if( rc!=SQLITE_DONE ){
16452 sqlite3_finalize(pQuery);
16453 sqlite3_free(zQuery);
16454 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16455 " WHERE %s ORDER BY rowid DESC", zWhere);
 
16456 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16457 if( rc ){
16458 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16459 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16460 zQuery);
@@ -16461,10 +16509,11 @@
16461 goto end_schema_xfer;
16462 }
16463 while( sqlite3_step(pQuery)==SQLITE_ROW ){
16464 zName = sqlite3_column_text(pQuery, 0);
16465 zSql = sqlite3_column_text(pQuery, 1);
 
16466 printf("%s... ", zName); fflush(stdout);
16467 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16468 if( zErrMsg ){
16469 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16470 sqlite3_free(zErrMsg);
@@ -16845,13 +16894,11 @@
16845 }
16846 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16847 }else{
16848 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16849 }
16850 if( p->zTempFile==0 ){
16851 shell_out_of_memory();
16852 }
16853 }
16854
16855
16856 /*
16857 ** The implementation of SQL scalar function fkey_collate_clause(), used
@@ -17028,18 +17075,18 @@
17028 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
17029 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
17030 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
17031 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
17032
 
 
17033 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
17034 if( rc!=SQLITE_OK ) break;
17035 if( SQLITE_ROW==sqlite3_step(pExplain) ){
17036 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17037 res = (
17038 0==sqlite3_strglob(zGlob, zPlan)
17039 || 0==sqlite3_strglob(zGlobIPK, zPlan)
17040 );
17041 }
17042 rc = sqlite3_finalize(pExplain);
17043 if( rc!=SQLITE_OK ) break;
17044
17045 if( res<0 ){
@@ -18140,10 +18187,11 @@
18140 , zName, zName
18141 );
18142 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18143 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18144 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
 
18145 }
18146 }
18147
18148 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18149 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
@@ -18224,12 +18272,14 @@
18224 break;
18225 }
18226 if( sqlite3_stricmp(zType, "table")==0 ){
18227 zName = (const char*)sqlite3_column_text(pStmt, 1);
18228 zSql = (const char*)sqlite3_column_text(pStmt, 2);
18229 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18230 break;
 
 
18231 }
18232 }
18233
18234 shellFinalize(pRc, pStmt);
18235 *pbNoop = bNoop;
@@ -18919,12 +18969,13 @@
18919 rc = 1;
18920 }else{
18921 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18922 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18923 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
 
18924 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18925 if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
18926 azName[nName*2] = strdup(zSchema);
18927 azName[nName*2+1] = strdup(zFile);
18928 nName++;
18929 }
18930 }
@@ -19176,12 +19227,19 @@
19176 }
19177 }else
19178
19179 #ifndef SQLITE_OMIT_VIRTUALTABLE
19180 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19181 open_db(p, 0);
19182 expertDotCommand(p, azArg, nArg);
 
 
 
 
 
 
 
19183 }else
19184 #endif
19185
19186 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19187 static const struct {
@@ -19969,11 +20027,12 @@
19969 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
19970 if( nArg!=2 ){
19971 raw_printf(stderr, "Usage: .nonce NONCE\n");
19972 rc = 1;
19973 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
19974 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
 
19975 exit(1);
19976 }else{
19977 p->bSafeMode = 0;
19978 return 0; /* Return immediately to bypass the safe mode reset
19979 ** at the end of this procedure */
@@ -19988,39 +20047,12 @@
19988 raw_printf(stderr, "Usage: .nullvalue STRING\n");
19989 rc = 1;
19990 }
19991 }else
19992
19993 #ifdef SQLITE_DEBUG
19994 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19995 int i;
19996 for(i=1; i<nArg; i++){
19997 const char *z = azArg[i];
19998 if( z[0]=='-' && z[1]=='-' ) z++;
19999 if( strcmp(z,"-repeat")==0 ){
20000 if( i==nArg-1 ){
20001 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
20002 rc = 1;
20003 }else{
20004 oomRepeat = (int)integerValue(azArg[++i]);
20005 }
20006 }else if( IsDigit(z[0]) ){
20007 oomCounter = (int)integerValue(azArg[i]);
20008 }else{
20009 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
20010 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
20011 rc = 1;
20012 }
20013 }
20014 if( rc==0 ){
20015 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
20016 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
20017 }
20018 }else
20019 #endif /* SQLITE_DEBUG */
20020
20021 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
 
20022 char *zNewFilename = 0; /* Name of the database file to open */
20023 int iName = 1; /* Index in azArg[] of the filename */
20024 int newFlag = 0; /* True to delete file before opening */
20025 int openMode = SHELL_OPEN_UNSPEC;
20026
@@ -20049,16 +20081,16 @@
20049 #endif /* SQLITE_OMIT_DESERIALIZE */
20050 }else if( z[0]=='-' ){
20051 utf8_printf(stderr, "unknown option: %s\n", z);
20052 rc = 1;
20053 goto meta_command_exit;
20054 }else if( zNewFilename ){
20055 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20056 rc = 1;
20057 goto meta_command_exit;
20058 }else{
20059 zNewFilename = sqlite3_mprintf("%s", z);
20060 }
20061 }
20062
20063 /* Close the existing database */
20064 session_close_all(p, -1);
@@ -20070,19 +20102,25 @@
20070 p->openMode = openMode;
20071 p->openFlags = 0;
20072 p->szMax = 0;
20073
20074 /* If a filename is specified, try to open it first */
20075 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
20076 if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
20077 if( p->bSafeMode
20078 && p->openMode!=SHELL_OPEN_HEXDB
20079 && zNewFilename
20080 && strcmp(zNewFilename,":memory:")!=0
20081 ){
20082 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20083 }
 
 
 
 
 
 
20084 p->pAuxDb->zDbFilename = zNewFilename;
20085 open_db(p, OPEN_DB_KEEPALIVE);
20086 if( p->db==0 ){
20087 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
20088 sqlite3_free(zNewFilename);
@@ -20132,11 +20170,11 @@
20132 rc = 1;
20133 goto meta_command_exit;
20134 }
20135 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
20136 zFile = sqlite3_mprintf("%s", z);
20137 if( zFile[0]=='|' ){
20138 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
20139 break;
20140 }
20141 }else{
20142 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
@@ -20145,11 +20183,13 @@
20145 rc = 1;
20146 sqlite3_free(zFile);
20147 goto meta_command_exit;
20148 }
20149 }
20150 if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
 
 
20151 if( bOnce ){
20152 p->outCount = 2;
20153 }else{
20154 p->outCount = 0;
20155 }
@@ -20172,10 +20212,11 @@
20172 }
20173 sqlite3_free(zFile);
20174 zFile = sqlite3_mprintf("%s", p->zTempFile);
20175 }
20176 #endif /* SQLITE_NOHAVE_SYSTEM */
 
20177 if( zFile[0]=='|' ){
20178 #ifdef SQLITE_OMIT_POPEN
20179 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20180 rc = 1;
20181 p->out = stdout;
@@ -20268,21 +20309,21 @@
20268 const char *zValue = azArg[3];
20269 bind_table_init(p);
20270 zSql = sqlite3_mprintf(
20271 "REPLACE INTO temp.sqlite_parameters(key,value)"
20272 "VALUES(%Q,%s);", zKey, zValue);
20273 if( zSql==0 ) shell_out_of_memory();
20274 pStmt = 0;
20275 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20276 sqlite3_free(zSql);
20277 if( rx!=SQLITE_OK ){
20278 sqlite3_finalize(pStmt);
20279 pStmt = 0;
20280 zSql = sqlite3_mprintf(
20281 "REPLACE INTO temp.sqlite_parameters(key,value)"
20282 "VALUES(%Q,%Q);", zKey, zValue);
20283 if( zSql==0 ) shell_out_of_memory();
20284 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20285 sqlite3_free(zSql);
20286 if( rx!=SQLITE_OK ){
20287 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
20288 sqlite3_finalize(pStmt);
@@ -20299,11 +20340,11 @@
20299 ** exists.
20300 */
20301 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
20302 char *zSql = sqlite3_mprintf(
20303 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20304 if( zSql==0 ) shell_out_of_memory();
20305 sqlite3_exec(p->db, zSql, 0, 0, 0);
20306 sqlite3_free(zSql);
20307 }else
20308 /* If no command name matches, show a syntax error */
20309 parameter_syntax_error:
@@ -20525,10 +20566,11 @@
20525 " name text,\n"
20526 " tbl_name text,\n"
20527 " rootpage integer,\n"
20528 " sql text\n"
20529 ")", zName);
 
20530 new_argv[1] = 0;
20531 new_colv[0] = "sql";
20532 new_colv[1] = 0;
20533 callback(&data, 1, new_argv, new_colv);
20534 sqlite3_free(new_argv[0]);
@@ -20576,12 +20618,14 @@
20576 }
20577 #endif
20578 appendText(&sSelect, ") WHERE ", 0);
20579 if( zName ){
20580 char *zQarg = sqlite3_mprintf("%Q", zName);
20581 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20582 strchr(zName, '[') != 0;
 
 
20583 if( strchr(zName, '.') ){
20584 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20585 }else{
20586 appendText(&sSelect, "lower(tbl_name)", 0);
20587 }
@@ -20740,11 +20784,12 @@
20740 if( pSession->azFilter==0 ){
20741 raw_printf(stderr, "Error: out or memory\n");
20742 exit(1);
20743 }
20744 for(ii=1; ii<nCmd; ii++){
20745 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
 
20746 }
20747 pSession->nFilter = ii-1;
20748 }
20749 }else
20750
@@ -20812,10 +20857,11 @@
20812 }
20813 pSession->nFilter = 0;
20814 sqlite3session_table_filter(pSession->p, session_filter, pSession);
20815 pAuxDb->nSession++;
20816 pSession->zName = sqlite3_mprintf("%s", zName);
 
20817 }else
20818 /* If no command name matches, show a syntax error */
20819 session_syntax_error:
20820 showHelp(p->out, "session");
20821 }else
@@ -20905,15 +20951,16 @@
20905 int tno = sqlite3_column_int(pStmt, 0);
20906 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20907 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20908 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20909
 
 
 
20910 k = 0;
20911 if( bVerbose>0 ){
20912 char *zQuote = sqlite3_mprintf("%q", zSql);
20913 printf("%d: %s %s\n", tno, zOp, zSql);
20914 sqlite3_free(zQuote);
20915 }
20916 if( strcmp(zOp,"memo")==0 ){
20917 utf8_printf(p->out, "%s\n", zSql);
20918 }else
20919 if( strcmp(zOp,"run")==0 ){
@@ -21027,10 +21074,11 @@
21027 initText(&sSql);
21028 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
21029 zSep = "VALUES(";
21030 while( SQLITE_ROW==sqlite3_step(pStmt) ){
21031 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
 
21032 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21033 if( strncmp(zTab, "sqlite_",7)!=0 ){
21034 appendText(&sQuery,"SELECT * FROM ", 0);
21035 appendText(&sQuery,zTab,'"');
21036 appendText(&sQuery," NOT INDEXED;", 0);
@@ -21067,10 +21115,11 @@
21067 "%s))"
21068 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
21069 " FROM [sha3sum$query]",
21070 sSql.z, iSize);
21071 }
 
21072 freeText(&sQuery);
21073 freeText(&sSql);
21074 if( bDebug ){
21075 utf8_printf(p->out, "%s\n", zSql);
21076 }else{
@@ -21090,15 +21139,15 @@
21090 raw_printf(stderr, "Usage: .system COMMAND\n");
21091 rc = 1;
21092 goto meta_command_exit;
21093 }
21094 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21095 for(i=2; i<nArg; i++){
21096 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
21097 zCmd, azArg[i]);
21098 }
21099 x = system(zCmd);
21100 sqlite3_free(zCmd);
21101 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21102 }else
21103 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21104
@@ -21230,16 +21279,16 @@
21230 while( sqlite3_step(pStmt)==SQLITE_ROW ){
21231 if( nRow>=nAlloc ){
21232 char **azNew;
21233 int n2 = nAlloc*2 + 10;
21234 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21235 if( azNew==0 ) shell_out_of_memory();
21236 nAlloc = n2;
21237 azResult = azNew;
21238 }
21239 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21240 if( 0==azResult[nRow] ) shell_out_of_memory();
21241 nRow++;
21242 }
21243 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
21244 rc = shellDatabaseError(p->db);
21245 }
@@ -22004,11 +22053,11 @@
22004 nLine = strlen30(zLine);
22005 if( nSql+nLine+2>=nAlloc ){
22006 /* Grow buffer by half-again increments when big. */
22007 nAlloc = nSql+(nSql>>1)+nLine+100;
22008 zSql = realloc(zSql, nAlloc);
22009 if( zSql==0 ) shell_out_of_memory();
22010 }
22011 if( nSql==0 ){
22012 int i;
22013 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
22014 assert( nAlloc>0 && zSql!=0 );
@@ -22136,10 +22185,11 @@
22136 raw_printf(stderr, "-- warning: cannot find home directory;"
22137 " cannot read ~/.sqliterc\n");
22138 return;
22139 }
22140 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
 
22141 sqliterc = zBuf;
22142 }
22143 p->in = fopen(sqliterc,"rb");
22144 if( p->in ){
22145 if( stdin_is_interactive ){
@@ -22334,14 +22384,10 @@
22334 setBinaryMode(stdin, 0);
22335 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
22336 stdin_is_interactive = isatty(0);
22337 stdout_is_console = isatty(1);
22338
22339 #ifdef SQLITE_DEBUG
22340 registerOomSimulator();
22341 #endif
22342
22343 #if !defined(_WIN32_WCE)
22344 if( getenv("SQLITE_DEBUG_BREAK") ){
22345 if( isatty(0) && isatty(2) ){
22346 fprintf(stderr,
22347 "attach debugger to process %d and press any key to continue.\n",
@@ -22377,20 +22423,20 @@
22377 ** memory that does not come from the SQLite memory allocator.
22378 */
22379 #if !SQLITE_SHELL_IS_UTF8
22380 sqlite3_initialize();
22381 argvToFree = malloc(sizeof(argv[0])*argc*2);
 
22382 argcToFree = argc;
22383 argv = argvToFree + argc;
22384 if( argv==0 ) shell_out_of_memory();
22385 for(i=0; i<argc; i++){
22386 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
22387 int n;
22388 if( z==0 ) shell_out_of_memory();
22389 n = (int)strlen(z);
22390 argv[i] = malloc( n+1 );
22391 if( argv[i]==0 ) shell_out_of_memory();
22392 memcpy(argv[i], z, n+1);
22393 argvToFree[i] = argv[i];
22394 sqlite3_free(z);
22395 }
22396 sqlite3_shutdown();
@@ -22436,11 +22482,11 @@
22436 /* Excesss arguments are interpreted as SQL (or dot-commands) and
22437 ** mean that nothing is read from stdin */
22438 readStdin = 0;
22439 nCmd++;
22440 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
22441 if( azCmd==0 ) shell_out_of_memory();
22442 azCmd[nCmd-1] = z;
22443 }
22444 }
22445 if( z[1]=='-' ) z++;
22446 if( strcmp(z,"-separator")==0
22447
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -443,19 +443,10 @@
443 /*
444 ** True if an interrupt (Control-C) has been received.
445 */
446 static volatile int seenInterrupt = 0;
447
 
 
 
 
 
 
 
 
 
448 /*
449 ** This is the name of our program. It is set in main(), used
450 ** in a number of other places, mostly for error messages.
451 */
452 static char *Argv0;
@@ -503,52 +494,16 @@
494 static void shell_out_of_memory(void){
495 raw_printf(stderr,"Error: out of memory\n");
496 exit(1);
497 }
498
499 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
500 ** out-of-memory error.
501 */
502 static void shell_check_oom(void *p){
503 if( p==0 ) shell_out_of_memory();
504 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505
506 /*
507 ** Write I/O traces to the following stream.
508 */
509 #ifdef SQLITE_ENABLE_IOTRACE
@@ -701,11 +656,11 @@
656
657 while( 1 ){
658 if( n+100>nLine ){
659 nLine = nLine*2 + 100;
660 zLine = realloc(zLine, nLine);
661 shell_check_oom(zLine);
662 }
663 if( fgets(&zLine[n], nLine - n, in)==0 ){
664 if( n==0 ){
665 free(zLine);
666 return 0;
@@ -728,11 +683,11 @@
683 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
684 if( zTrans ){
685 int nTrans = strlen30(zTrans)+1;
686 if( nTrans>nLine ){
687 zLine = realloc(zLine, nTrans);
688 shell_check_oom(zLine);
689 }
690 memcpy(zLine, zTrans, nTrans);
691 sqlite3_free(zTrans);
692 }
693 }
@@ -875,11 +830,11 @@
830 }
831
832 if( p->z==0 || p->n+len>=p->nAlloc ){
833 p->nAlloc = p->nAlloc*2 + len + 20;
834 p->z = realloc(p->z, p->nAlloc);
835 shell_check_oom(p->z);
836 }
837
838 if( quote ){
839 char *zCsr = p->z+p->n;
840 *zCsr++ = quote;
@@ -930,10 +885,11 @@
885 char *zDiv = "(";
886 int nRow = 0;
887
888 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
889 zSchema ? zSchema : "main", zName);
890 shell_check_oom(zSql);
891 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
892 sqlite3_free(zSql);
893 initText(&s);
894 if( zSchema ){
895 cQuote = quoteChar(zSchema);
@@ -946,10 +902,11 @@
902 while( sqlite3_step(pStmt)==SQLITE_ROW ){
903 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
904 nRow++;
905 appendText(&s, zDiv, 0);
906 zDiv = ",";
907 if( zCol==0 ) zCol = "";
908 cQuote = quoteChar(zCol);
909 appendText(&s, zCol, cQuote);
910 }
911 appendText(&s, ")", 0);
912 sqlite3_finalize(pStmt);
@@ -969,13 +926,15 @@
926 static void shellModuleSchema(
927 sqlite3_context *pCtx,
928 int nVal,
929 sqlite3_value **apVal
930 ){
931 const char *zName;
932 char *zFake;
933 UNUSED_PARAMETER(nVal);
934 zName = (const char*)sqlite3_value_text(apVal[0]);
935 zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
936 if( zFake ){
937 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
938 -1, sqlite3_free);
939 free(zFake);
940 }
@@ -1859,10 +1818,11 @@
1818 SHA3Context *p,
1819 const unsigned char *aData,
1820 unsigned int nData
1821 ){
1822 unsigned int i = 0;
1823 if( aData==0 ) return;
1824 #if SHA3_BYTEORDER==1234
1825 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1826 for(; i+7<nData; i+=8){
1827 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1828 p->nLoaded += 8;
@@ -2517,14 +2477,15 @@
2477 const char *zFile, /* File to write */
2478 sqlite3_value *pData, /* Data to write */
2479 mode_t mode, /* MODE parameter passed to writefile() */
2480 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2481 ){
2482 if( zFile==0 ) return 1;
2483 #if !defined(_WIN32) && !defined(WIN32)
2484 if( S_ISLNK(mode) ){
2485 const char *zTo = (const char*)sqlite3_value_text(pData);
2486 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
2487 }else
2488 #endif
2489 {
2490 if( S_ISDIR(mode) ){
2491 if( mkdir(zFile, mode) ){
@@ -5886,11 +5847,11 @@
5847 if( (idxNum & 3)==3 ){
5848 /* Both start= and stop= boundaries are available. This is the
5849 ** the preferred case */
5850 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5851 pIdxInfo->estimatedRows = 1000;
5852 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){
5853 if( pIdxInfo->aOrderBy[0].desc ){
5854 idxNum |= 8;
5855 }else{
5856 idxNum |= 16;
5857 }
@@ -6721,17 +6682,19 @@
6682 const sqlite3_api_routines *pApi
6683 ){
6684 int rc = SQLITE_OK;
6685 SQLITE_EXTENSION_INIT2(pApi);
6686 (void)pzErrMsg; /* Unused */
6687 rc = sqlite3_create_function(db, "regexp", 2,
6688 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6689 0, re_sql_func, 0, 0);
6690 if( rc==SQLITE_OK ){
6691 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6692 ** of regexp(PATTERN,STRING). */
6693 rc = sqlite3_create_function(db, "regexpi", 2,
6694 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
6695 (void*)db, re_sql_func, 0, 0);
6696 }
6697 return rc;
6698 }
6699
6700 /************************* End ../ext/misc/regexp.c ********************/
@@ -9939,21 +9902,29 @@
9902 IdxTable **ppOut, /* OUT: New object (if successful) */
9903 char **pzErrmsg /* OUT: Error message (if not) */
9904 ){
9905 sqlite3_stmt *p1 = 0;
9906 int nCol = 0;
9907 int nTab;
9908 int nByte;
9909 IdxTable *pNew = 0;
9910 int rc, rc2;
9911 char *pCsr = 0;
9912 int nPk = 0;
9913
9914 *ppOut = 0;
9915 if( zTab==0 ) return SQLITE_ERROR;
9916 nTab = STRLEN(zTab);
9917 nByte = sizeof(IdxTable) + nTab + 1;
9918 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9919 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9920 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9921 const char *zColSeq = 0;
9922 if( zCol==0 ){
9923 rc = SQLITE_ERROR;
9924 break;
9925 }
9926 nByte += 1 + STRLEN(zCol);
9927 rc = sqlite3_table_column_metadata(
9928 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9929 );
9930 if( zColSeq==0 ) zColSeq = "binary";
@@ -9976,11 +9947,13 @@
9947
9948 nCol = 0;
9949 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9950 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9951 const char *zColSeq = 0;
9952 int nCopy;
9953 if( zCol==0 ) continue;
9954 nCopy = STRLEN(zCol) + 1;
9955 pNew->aCol[nCol].zName = pCsr;
9956 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9957 memcpy(pCsr, zCol, nCopy);
9958 pCsr += nCopy;
9959
@@ -10128,10 +10101,11 @@
10101 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10102 int bMatch = 1;
10103 IdxConstraint *pT = pTail;
10104 sqlite3_stmt *pInfo = 0;
10105 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10106 if( zIdx==0 ) continue;
10107
10108 /* Zero the IdxConstraint.bFlag values in the pEq list */
10109 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10110
10111 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
@@ -10539,10 +10513,11 @@
10513
10514 /* Create the table and its triggers in the temp schema */
10515 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10516 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10517 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10518 if( zCreate==0 ) continue;
10519 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10520 }
10521 idxFinalize(&rc, pSelect);
10522
10523 /* Rename the table in the temp schema to zInt */
@@ -10641,12 +10616,13 @@
10616 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10617 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10618 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10619 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10620
10621 if( zType==0 || zName==0 ) continue;
10622 if( zType[0]=='v' || zType[1]=='r' ){
10623 if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10624 }else{
10625 IdxTable *pTab;
10626 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10627 if( rc==SQLITE_OK ){
10628 int i;
@@ -10779,10 +10755,11 @@
10755 break;
10756
10757 case SQLITE_BLOB:
10758 case SQLITE_TEXT: {
10759 int nByte = sqlite3_value_bytes(argv[1]);
10760 const void *pData = 0;
10761 if( nByte>pSlot->nByte ){
10762 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10763 if( zNew==0 ){
10764 sqlite3_result_error_nomem(pCtx);
10765 return;
@@ -10790,13 +10767,15 @@
10767 pSlot->nByte = nByte*2;
10768 pSlot->z = zNew;
10769 }
10770 pSlot->n = nByte;
10771 if( pSlot->eType==SQLITE_BLOB ){
10772 pData = sqlite3_value_blob(argv[1]);
10773 if( pData ) memcpy(pSlot->z, pData, nByte);
10774 }else{
10775 pData = sqlite3_value_text(argv[1]);
10776 memcpy(pSlot->z, pData, nByte);
10777 }
10778 break;
10779 }
10780 }
10781 }
@@ -11003,10 +10982,11 @@
10982
10983 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10984 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10985 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10986 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10987 if( zTab==0 || zIdx==0 ) continue;
10988 if( p->iSample<100 && iPrev!=iRowid ){
10989 samplectx.target = (double)p->iSample / 100.0;
10990 samplectx.iTarget = p->iSample;
10991 samplectx.nRow = 0.0;
10992 samplectx.nRet = 0.0;
@@ -11069,18 +11049,18 @@
11049 }
11050
11051
11052 /* Copy the entire schema of database [db] into [dbm]. */
11053 if( rc==SQLITE_OK ){
11054 sqlite3_stmt *pSql = 0;
11055 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
11056 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
11057 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
11058 );
11059 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11060 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11061 if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11062 }
11063 idxFinalize(&rc, pSql);
11064 }
11065
11066 /* Create the vtab schema */
@@ -12831,10 +12811,11 @@
12811 break;
12812 }
12813 }
12814 if( i==0 || strstr(z, p->colSeparator)!=0 ){
12815 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12816 shell_check_oom(zQuoted);
12817 utf8_printf(out, "%s", zQuoted);
12818 sqlite3_free(zQuoted);
12819 }else{
12820 utf8_printf(out, "%s", z);
12821 }
@@ -13005,11 +12986,11 @@
12986 int nText = strlen30(zText);
12987 if( p->autoEQPtest ){
12988 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
12989 }
12990 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
12991 shell_check_oom(pNew);
12992 pNew->iEqpId = iEqpId;
12993 pNew->iParentId = p2;
12994 memcpy(pNew->zText, zText, nText+1);
12995 pNew->pNext = 0;
12996 if( p->sGraph.pLast ){
@@ -13226,10 +13207,11 @@
13207 ){
13208 utf8_printf(p->out, "%s;\n", azArg[0]);
13209 break;
13210 }
13211 z = sqlite3_mprintf("%s", azArg[0]);
13212 shell_check_oom(z);
13213 j = 0;
13214 for(i=0; IsSpace(z[i]); i++){}
13215 for(; (c = z[i])!=0; i++){
13216 if( IsSpace(c) ){
13217 if( z[j-1]=='\r' ) z[j-1] = '\n';
@@ -13357,10 +13339,11 @@
13339 raw_printf(p->out,"(");
13340 for(i=0; i<nArg; i++){
13341 if( i>0 ) raw_printf(p->out, ",");
13342 if( quoteChar(azCol[i]) ){
13343 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13344 shell_check_oom(z);
13345 utf8_printf(p->out, "%s", z);
13346 sqlite3_free(z);
13347 }else{
13348 raw_printf(p->out, "%s", azCol[i]);
13349 }
@@ -13602,20 +13585,61 @@
13585 if( zName==0 ) return;
13586 cQuote = quoteChar(zName);
13587 n = strlen30(zName);
13588 if( cQuote ) n += n+2;
13589 z = p->zDestTable = malloc( n+1 );
13590 shell_check_oom(z);
13591 n = 0;
13592 if( cQuote ) z[n++] = cQuote;
13593 for(i=0; zName[i]; i++){
13594 z[n++] = zName[i];
13595 if( zName[i]==cQuote ) z[n++] = cQuote;
13596 }
13597 if( cQuote ) z[n++] = cQuote;
13598 z[n] = 0;
13599 }
13600
13601 /*
13602 ** Maybe construct two lines of text that point out the position of a
13603 ** syntax error. Return a pointer to the text, in memory obtained from
13604 ** sqlite3_malloc(). Or, if the most recent error does not involve a
13605 ** specific token that we can point to, return an empty string.
13606 **
13607 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
13608 ** and should be released by the caller invoking sqlite3_free().
13609 */
13610 static char *shell_error_context(const char *zSql, sqlite3 *db){
13611 int iOffset;
13612 size_t len;
13613 char *zCode;
13614 char *zMsg;
13615 int i;
13616 if( db==0
13617 || zSql==0
13618 || (iOffset = sqlite3_error_offset(db))<0
13619 ){
13620 return sqlite3_mprintf("");
13621 }
13622 while( iOffset>50 ){
13623 iOffset--;
13624 zSql++;
13625 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
13626 }
13627 len = strlen(zSql);
13628 if( len>78 ){
13629 len = 78;
13630 while( (zSql[len]&0xc0)==0x80 ) len--;
13631 }
13632 zCode = sqlite3_mprintf("%.*s", len, zSql);
13633 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
13634 if( iOffset<25 ){
13635 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, "");
13636 }else{
13637 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, "");
13638 }
13639 return zMsg;
13640 }
13641
13642
13643 /*
13644 ** Execute a query statement that will generate SQL output. Print
13645 ** the result columns, comma-separated, on a line and then add a
@@ -13635,12 +13659,14 @@
13659 int nResult;
13660 int i;
13661 const char *z;
13662 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13663 if( rc!=SQLITE_OK || !pSelect ){
13664 char *zContext = shell_error_context(zSelect, p->db);
13665 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
13666 sqlite3_errmsg(p->db), zContext);
13667 sqlite3_free(zContext);
13668 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13669 return rc;
13670 }
13671 rc = sqlite3_step(pSelect);
13672 nResult = sqlite3_column_count(pSelect);
@@ -13672,15 +13698,21 @@
13698 ** Allocate space and save off string indicating current error.
13699 */
13700 static char *save_err_msg(
13701 sqlite3 *db, /* Database to query */
13702 const char *zWhen, /* Qualifier (format) wrapper */
13703 int rc, /* Error code returned from API */
13704 const char *zSql /* SQL string, or NULL */
13705 ){
13706 char *zErr;
13707 char *zContext;
13708 if( zWhen==0 ) zWhen = "%s (%d)%s";
13709 zContext = shell_error_context(zSql, db);
13710 zErr = sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc, zContext);
13711 shell_check_oom(zErr);
13712 sqlite3_free(zContext);
13713 return zErr;
13714 }
13715
13716 #ifdef __linux__
13717 /*
13718 ** Attempt to display I/O stats on Linux using /proc/PID/io
@@ -14024,13 +14056,13 @@
14056 }
14057 }
14058 }
14059 nAlloc += 100;
14060 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14061 shell_check_oom(p->aiIndent);
14062 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14063 shell_check_oom(abYield);
14064 }
14065 abYield[iOp] = str_in_array(zOp, azYield);
14066 p->aiIndent[iOp] = 0;
14067 p->nIndent = iOp+1;
14068
@@ -14235,29 +14267,29 @@
14267 if( rc!=SQLITE_ROW ) return;
14268 nColumn = sqlite3_column_count(pStmt);
14269 nAlloc = nColumn*4;
14270 if( nAlloc<=0 ) nAlloc = 1;
14271 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14272 shell_check_oom(azData);
14273 for(i=0; i<nColumn; i++){
14274 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14275 }
14276 do{
14277 if( (nRow+2)*nColumn >= nAlloc ){
14278 nAlloc *= 2;
14279 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14280 shell_check_oom(azData);
14281 }
14282 nRow++;
14283 for(i=0; i<nColumn; i++){
14284 z = (const char*)sqlite3_column_text(pStmt,i);
14285 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14286 }
14287 }while( sqlite3_step(pStmt)==SQLITE_ROW );
14288 if( nColumn>p->nWidth ){
14289 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14290 shell_check_oom(p->colWidth);
14291 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14292 p->nWidth = nColumn;
14293 p->actualWidth = &p->colWidth[nColumn];
14294 }
14295 memset(p->actualWidth, 0, nColumn*sizeof(int));
@@ -14436,11 +14468,14 @@
14468 } while( SQLITE_ROW == rc );
14469 sqlite3_free(pData);
14470 if( pArg->cMode==MODE_Json ){
14471 fputs("]\n", pArg->out);
14472 }else if( pArg->cMode==MODE_Count ){
14473 char zBuf[200];
14474 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
14475 nRow, nRow!=1 ? "s" : "");
14476 printf("%s", zBuf);
14477 }
14478 }
14479 }
14480 }
14481
@@ -14560,18 +14595,19 @@
14595 }
14596
14597 if( rc==SQLITE_OK ){
14598 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14599 if( pState->expert.pExpert==0 ){
14600 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
14601 rc = SQLITE_ERROR;
14602 }else{
14603 sqlite3_expert_config(
14604 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14605 );
14606 }
14607 }
14608 sqlite3_free(zErr);
14609
14610 return rc;
14611 }
14612 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14613
@@ -14609,11 +14645,11 @@
14645 while( zSql[0] && (SQLITE_OK == rc) ){
14646 static const char *zStmtSql;
14647 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14648 if( SQLITE_OK != rc ){
14649 if( pzErrMsg ){
14650 *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)%s", rc, zSql);
14651 }
14652 }else{
14653 if( !pStmt ){
14654 /* this happens for a comment or white-space */
14655 zSql = zLeftover;
@@ -14644,10 +14680,11 @@
14680 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14681 if( pArg->autoEQP>=AUTOEQP_trigger ){
14682 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14683 }
14684 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14685 shell_check_oom(zEQP);
14686 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14687 if( rc==SQLITE_OK ){
14688 while( sqlite3_step(pExplain)==SQLITE_ROW ){
14689 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14690 int iEqpId = sqlite3_column_int(pExplain, 0);
@@ -14661,10 +14698,11 @@
14698 sqlite3_finalize(pExplain);
14699 sqlite3_free(zEQP);
14700 if( pArg->autoEQP>=AUTOEQP_full ){
14701 /* Also do an EXPLAIN for ".eqp full" mode */
14702 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14703 shell_check_oom(zEQP);
14704 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14705 if( rc==SQLITE_OK ){
14706 pArg->cMode = MODE_Explain;
14707 explain_data_prepare(pArg, pExplain);
14708 exec_prepared_stmt(pArg, pExplain);
@@ -14723,11 +14761,11 @@
14761 if( rc!=SQLITE_NOMEM ) rc = rc2;
14762 if( rc==SQLITE_OK ){
14763 zSql = zLeftover;
14764 while( IsSpace(zSql[0]) ) zSql++;
14765 }else if( pzErrMsg ){
14766 *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc, 0);
14767 }
14768
14769 /* clear saved stmt handle */
14770 if( pArg ){
14771 pArg->pStmt = NULL;
@@ -14773,20 +14811,22 @@
14811 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
14812 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14813 int rc;
14814
14815 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14816 shell_check_oom(zSql);
14817 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14818 sqlite3_free(zSql);
14819 if( rc ) return 0;
14820 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14821 if( nCol>=nAlloc-2 ){
14822 nAlloc = nAlloc*2 + nCol + 10;
14823 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14824 shell_check_oom(azCol);
14825 }
14826 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14827 shell_check_oom(azCol[nCol]);
14828 if( sqlite3_column_int(pStmt, 5) ){
14829 nPK++;
14830 if( nPK==1
14831 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14832 "INTEGER")==0
@@ -14816,10 +14856,11 @@
14856 ** there is a "pk" entry in "PRAGMA index_list". There will be
14857 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14858 */
14859 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14860 " WHERE origin='pk'", zTab);
14861 shell_check_oom(zSql);
14862 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14863 sqlite3_free(zSql);
14864 if( rc ){
14865 freeColumnList(azCol);
14866 return 0;
@@ -14907,10 +14948,11 @@
14948 }
14949 zIns = sqlite3_mprintf(
14950 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14951 "VALUES('table','%q','%q',0,'%q');",
14952 zTable, zTable, zSql);
14953 shell_check_oom(zIns);
14954 utf8_printf(p->out, "%s\n", zIns);
14955 sqlite3_free(zIns);
14956 return 0;
14957 }else{
14958 printSchemaLine(p->out, zSql, ";\n");
@@ -15150,13 +15192,10 @@
15192 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15193 " If FILE begins with '|' then open as a pipe",
15194 " --bom Put a UTF8 byte-order mark at the beginning",
15195 " -e Send output to the system text editor",
15196 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
 
 
 
15197 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
15198 " Options:",
15199 " --append Use appendvfs to append database to the end of FILE",
15200 #ifndef SQLITE_OMIT_DESERIALIZE
15201 " --deserialize Load into memory using sqlite3_deserialize()",
@@ -15188,11 +15227,12 @@
15227 " --quiet|-q No output except at interrupts",
15228 " --reset Reset the count for each input and interrupt",
15229 #endif
15230 ".prompt MAIN CONTINUE Replace the standard prompts",
15231 ".quit Exit this program",
15232 ".read FILE Read input from FILE or command output",
15233 " If FILE begins with \"|\", it is a command that generates the input.",
15234 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15235 ".recover Recover as much data as possible from corrupt db.",
15236 " --freelist-corrupt Assume the freelist is corrupt",
15237 " --recovery-db NAME Store recovery metadata in database file NAME",
15238 " --lost-and-found TABLE Alternative name for the lost-and-found table",
@@ -15308,10 +15348,11 @@
15348 }
15349 }
15350 }else{
15351 /* Look for commands that for which zPattern is an exact prefix */
15352 zPat = sqlite3_mprintf(".%s*", zPattern);
15353 shell_check_oom(zPat);
15354 for(i=0; i<ArraySize(azHelp); i++){
15355 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15356 utf8_printf(out, "%s\n", azHelp[i]);
15357 j = i+1;
15358 n++;
@@ -15330,10 +15371,11 @@
15371 return n;
15372 }
15373 /* Look for commands that contain zPattern anywhere. Show the complete
15374 ** text of all commands that match. */
15375 zPat = sqlite3_mprintf("%%%s%%", zPattern);
15376 shell_check_oom(zPat);
15377 for(i=0; i<ArraySize(azHelp); i++){
15378 if( azHelp[i][0]=='.' ) j = i;
15379 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15380 utf8_printf(out, "%s\n", azHelp[j]);
15381 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
@@ -15518,14 +15560,11 @@
15560 if( rc!=2 ) goto readHexDb_error;
15561 if( n<0 ) goto readHexDb_error;
15562 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15563 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
15564 a = sqlite3_malloc( n ? n : 1 );
15565 shell_check_oom(a);
 
 
 
15566 memset(a, 0, n);
15567 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15568 utf8_printf(stderr, "invalid pagesize\n");
15569 goto readHexDb_error;
15570 }
@@ -15652,11 +15691,11 @@
15691 int argc,
15692 sqlite3_value **argv
15693 ){
15694 const char *zText = (const char*)sqlite3_value_text(argv[0]);
15695 UNUSED_PARAMETER(argc);
15696 if( zText && zText[0]=='\'' ){
15697 int nText = sqlite3_value_bytes(argv[0]);
15698 int i;
15699 char zBuf1[20];
15700 char zBuf2[20];
15701 const char *zNL = 0;
@@ -15829,10 +15868,11 @@
15868 editFunc, 0, 0);
15869 #endif
15870 if( p->openMode==SHELL_OPEN_ZIPFILE ){
15871 char *zSql = sqlite3_mprintf(
15872 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
15873 shell_check_oom(zSql);
15874 sqlite3_exec(p->db, zSql, 0, 0, 0);
15875 sqlite3_free(zSql);
15876 }
15877 #ifndef SQLITE_OMIT_DESERIALIZE
15878 else
@@ -15886,15 +15926,17 @@
15926 if( state==0 ){
15927 char *zSql;
15928 sqlite3_finalize(pStmt);
15929 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15930 " FROM completion(%Q) ORDER BY 1", text);
15931 shell_check_oom(zSql);
15932 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15933 sqlite3_free(zSql);
15934 }
15935 if( sqlite3_step(pStmt)==SQLITE_ROW ){
15936 const char *z = (const char*)sqlite3_column_text(pStmt,0);
15937 zRet = z ? strdup(z) : 0;
15938 }else{
15939 sqlite3_finalize(pStmt);
15940 pStmt = 0;
15941 zRet = 0;
15942 }
@@ -15923,17 +15965,18 @@
15965 iStart = i+1;
15966 memcpy(zBuf, zLine, iStart);
15967 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15968 " FROM completion(%Q,%Q) ORDER BY 1",
15969 &zLine[iStart], zLine);
15970 shell_check_oom(zSql);
15971 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15972 sqlite3_free(zSql);
15973 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15974 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15975 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15976 int nCompletion = sqlite3_column_bytes(pStmt, 0);
15977 if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
15978 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15979 linenoiseAddCompletion(lc, zBuf);
15980 }
15981 }
15982 sqlite3_finalize(pStmt);
@@ -16164,11 +16207,11 @@
16207 /* Append a single byte to z[] */
16208 static void import_append_char(ImportCtx *p, int c){
16209 if( p->n+1>=p->nAlloc ){
16210 p->nAlloc += p->nAlloc + 100;
16211 p->z = sqlite3_realloc64(p->z, p->nAlloc);
16212 shell_check_oom(p->z);
16213 }
16214 p->z[p->n++] = (char)c;
16215 }
16216
16217 /* Read a single field of CSV text. Compatible with rfc4180 and extended
@@ -16316,20 +16359,21 @@
16359 int k = 0;
16360 int cnt = 0;
16361 const int spinRate = 10000;
16362
16363 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16364 shell_check_oom(zQuery);
16365 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16366 if( rc ){
16367 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16368 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16369 zQuery);
16370 goto end_data_xfer;
16371 }
16372 n = sqlite3_column_count(pQuery);
16373 zInsert = sqlite3_malloc64(200 + nTable + n*3);
16374 shell_check_oom(zInsert);
16375 sqlite3_snprintf(200+nTable,zInsert,
16376 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16377 i = strlen30(zInsert);
16378 for(j=1; j<n; j++){
16379 memcpy(zInsert+i, ",?", 2);
@@ -16388,10 +16432,11 @@
16432 if( rc==SQLITE_DONE ) break;
16433 sqlite3_finalize(pQuery);
16434 sqlite3_free(zQuery);
16435 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16436 zTable);
16437 shell_check_oom(zQuery);
16438 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16439 if( rc ){
16440 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16441 break;
16442 }
@@ -16424,10 +16469,11 @@
16469 const unsigned char *zSql;
16470 char *zErrMsg = 0;
16471
16472 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16473 " WHERE %s", zWhere);
16474 shell_check_oom(zQuery);
16475 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16476 if( rc ){
16477 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16478 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16479 zQuery);
@@ -16434,10 +16480,11 @@
16480 goto end_schema_xfer;
16481 }
16482 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16483 zName = sqlite3_column_text(pQuery, 0);
16484 zSql = sqlite3_column_text(pQuery, 1);
16485 if( zName==0 || zSql==0 ) continue;
16486 printf("%s... ", zName); fflush(stdout);
16487 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16488 if( zErrMsg ){
16489 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16490 sqlite3_free(zErrMsg);
@@ -16451,10 +16498,11 @@
16498 if( rc!=SQLITE_DONE ){
16499 sqlite3_finalize(pQuery);
16500 sqlite3_free(zQuery);
16501 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16502 " WHERE %s ORDER BY rowid DESC", zWhere);
16503 shell_check_oom(zQuery);
16504 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16505 if( rc ){
16506 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16507 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16508 zQuery);
@@ -16461,10 +16509,11 @@
16509 goto end_schema_xfer;
16510 }
16511 while( sqlite3_step(pQuery)==SQLITE_ROW ){
16512 zName = sqlite3_column_text(pQuery, 0);
16513 zSql = sqlite3_column_text(pQuery, 1);
16514 if( zName==0 || zSql==0 ) continue;
16515 printf("%s... ", zName); fflush(stdout);
16516 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16517 if( zErrMsg ){
16518 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16519 sqlite3_free(zErrMsg);
@@ -16845,13 +16894,11 @@
16894 }
16895 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16896 }else{
16897 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16898 }
16899 shell_check_oom(p->zTempFile);
 
 
16900 }
16901
16902
16903 /*
16904 ** The implementation of SQL scalar function fkey_collate_clause(), used
@@ -17028,18 +17075,18 @@
17075 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
17076 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
17077 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
17078 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
17079
17080 if( zEQP==0 ) continue;
17081 if( zGlob==0 ) continue;
17082 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
17083 if( rc!=SQLITE_OK ) break;
17084 if( SQLITE_ROW==sqlite3_step(pExplain) ){
17085 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17086 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
17087 || 0==sqlite3_strglob(zGlobIPK, zPlan));
 
 
17088 }
17089 rc = sqlite3_finalize(pExplain);
17090 if( rc!=SQLITE_OK ) break;
17091
17092 if( res<0 ){
@@ -18140,10 +18187,11 @@
18187 , zName, zName
18188 );
18189 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18190 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18191 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18192 if( zPk==0 ){ zPk = "_"; /* Defensive. Should never happen */ }
18193 }
18194 }
18195
18196 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18197 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
@@ -18224,12 +18272,14 @@
18272 break;
18273 }
18274 if( sqlite3_stricmp(zType, "table")==0 ){
18275 zName = (const char*)sqlite3_column_text(pStmt, 1);
18276 zSql = (const char*)sqlite3_column_text(pStmt, 2);
18277 if( zName!=0 && zSql!=0 ){
18278 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18279 break;
18280 }
18281 }
18282 }
18283
18284 shellFinalize(pRc, pStmt);
18285 *pbNoop = bNoop;
@@ -18919,12 +18969,13 @@
18969 rc = 1;
18970 }else{
18971 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18972 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18973 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18974 if( zSchema==0 || zFile==0 ) continue;
18975 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18976 shell_check_oom(azName);
18977 azName[nName*2] = strdup(zSchema);
18978 azName[nName*2+1] = strdup(zFile);
18979 nName++;
18980 }
18981 }
@@ -19176,12 +19227,19 @@
19227 }
19228 }else
19229
19230 #ifndef SQLITE_OMIT_VIRTUALTABLE
19231 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19232 if( p->bSafeMode ){
19233 raw_printf(stderr,
19234 "Cannot run experimental commands such as \"%s\" in safe mode\n",
19235 azArg[0]);
19236 rc = 1;
19237 }else{
19238 open_db(p, 0);
19239 expertDotCommand(p, azArg, nArg);
19240 }
19241 }else
19242 #endif
19243
19244 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19245 static const struct {
@@ -19969,11 +20027,12 @@
20027 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20028 if( nArg!=2 ){
20029 raw_printf(stderr, "Usage: .nonce NONCE\n");
20030 rc = 1;
20031 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
20032 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
20033 p->lineno, azArg[1]);
20034 exit(1);
20035 }else{
20036 p->bSafeMode = 0;
20037 return 0; /* Return immediately to bypass the safe mode reset
20038 ** at the end of this procedure */
@@ -19988,39 +20047,12 @@
20047 raw_printf(stderr, "Usage: .nullvalue STRING\n");
20048 rc = 1;
20049 }
20050 }else
20051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20052 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
20053 const char *zFN = 0; /* Pointer to constant filename */
20054 char *zNewFilename = 0; /* Name of the database file to open */
20055 int iName = 1; /* Index in azArg[] of the filename */
20056 int newFlag = 0; /* True to delete file before opening */
20057 int openMode = SHELL_OPEN_UNSPEC;
20058
@@ -20049,16 +20081,16 @@
20081 #endif /* SQLITE_OMIT_DESERIALIZE */
20082 }else if( z[0]=='-' ){
20083 utf8_printf(stderr, "unknown option: %s\n", z);
20084 rc = 1;
20085 goto meta_command_exit;
20086 }else if( zFN ){
20087 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20088 rc = 1;
20089 goto meta_command_exit;
20090 }else{
20091 zFN = z;
20092 }
20093 }
20094
20095 /* Close the existing database */
20096 session_close_all(p, -1);
@@ -20070,19 +20102,25 @@
20102 p->openMode = openMode;
20103 p->openFlags = 0;
20104 p->szMax = 0;
20105
20106 /* If a filename is specified, try to open it first */
20107 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20108 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20109 if( p->bSafeMode
20110 && p->openMode!=SHELL_OPEN_HEXDB
20111 && zFN
20112 && strcmp(zFN,":memory:")!=0
20113 ){
20114 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20115 }
20116 if( zFN ){
20117 zNewFilename = sqlite3_mprintf("%s", zFN);
20118 shell_check_oom(zNewFilename);
20119 }else{
20120 zNewFilename = 0;
20121 }
20122 p->pAuxDb->zDbFilename = zNewFilename;
20123 open_db(p, OPEN_DB_KEEPALIVE);
20124 if( p->db==0 ){
20125 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
20126 sqlite3_free(zNewFilename);
@@ -20132,11 +20170,11 @@
20170 rc = 1;
20171 goto meta_command_exit;
20172 }
20173 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
20174 zFile = sqlite3_mprintf("%s", z);
20175 if( zFile && zFile[0]=='|' ){
20176 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
20177 break;
20178 }
20179 }else{
20180 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
@@ -20145,11 +20183,13 @@
20183 rc = 1;
20184 sqlite3_free(zFile);
20185 goto meta_command_exit;
20186 }
20187 }
20188 if( zFile==0 ){
20189 zFile = sqlite3_mprintf("stdout");
20190 }
20191 if( bOnce ){
20192 p->outCount = 2;
20193 }else{
20194 p->outCount = 0;
20195 }
@@ -20172,10 +20212,11 @@
20212 }
20213 sqlite3_free(zFile);
20214 zFile = sqlite3_mprintf("%s", p->zTempFile);
20215 }
20216 #endif /* SQLITE_NOHAVE_SYSTEM */
20217 shell_check_oom(zFile);
20218 if( zFile[0]=='|' ){
20219 #ifdef SQLITE_OMIT_POPEN
20220 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20221 rc = 1;
20222 p->out = stdout;
@@ -20268,21 +20309,21 @@
20309 const char *zValue = azArg[3];
20310 bind_table_init(p);
20311 zSql = sqlite3_mprintf(
20312 "REPLACE INTO temp.sqlite_parameters(key,value)"
20313 "VALUES(%Q,%s);", zKey, zValue);
20314 shell_check_oom(zSql);
20315 pStmt = 0;
20316 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20317 sqlite3_free(zSql);
20318 if( rx!=SQLITE_OK ){
20319 sqlite3_finalize(pStmt);
20320 pStmt = 0;
20321 zSql = sqlite3_mprintf(
20322 "REPLACE INTO temp.sqlite_parameters(key,value)"
20323 "VALUES(%Q,%Q);", zKey, zValue);
20324 shell_check_oom(zSql);
20325 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20326 sqlite3_free(zSql);
20327 if( rx!=SQLITE_OK ){
20328 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
20329 sqlite3_finalize(pStmt);
@@ -20299,11 +20340,11 @@
20340 ** exists.
20341 */
20342 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
20343 char *zSql = sqlite3_mprintf(
20344 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20345 shell_check_oom(zSql);
20346 sqlite3_exec(p->db, zSql, 0, 0, 0);
20347 sqlite3_free(zSql);
20348 }else
20349 /* If no command name matches, show a syntax error */
20350 parameter_syntax_error:
@@ -20525,10 +20566,11 @@
20566 " name text,\n"
20567 " tbl_name text,\n"
20568 " rootpage integer,\n"
20569 " sql text\n"
20570 ")", zName);
20571 shell_check_oom(new_argv[0]);
20572 new_argv[1] = 0;
20573 new_colv[0] = "sql";
20574 new_colv[1] = 0;
20575 callback(&data, 1, new_argv, new_colv);
20576 sqlite3_free(new_argv[0]);
@@ -20576,12 +20618,14 @@
20618 }
20619 #endif
20620 appendText(&sSelect, ") WHERE ", 0);
20621 if( zName ){
20622 char *zQarg = sqlite3_mprintf("%Q", zName);
20623 int bGlob;
20624 shell_check_oom(zQarg);
20625 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20626 strchr(zName, '[') != 0;
20627 if( strchr(zName, '.') ){
20628 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20629 }else{
20630 appendText(&sSelect, "lower(tbl_name)", 0);
20631 }
@@ -20740,11 +20784,12 @@
20784 if( pSession->azFilter==0 ){
20785 raw_printf(stderr, "Error: out or memory\n");
20786 exit(1);
20787 }
20788 for(ii=1; ii<nCmd; ii++){
20789 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20790 shell_check_oom(x);
20791 }
20792 pSession->nFilter = ii-1;
20793 }
20794 }else
20795
@@ -20812,10 +20857,11 @@
20857 }
20858 pSession->nFilter = 0;
20859 sqlite3session_table_filter(pSession->p, session_filter, pSession);
20860 pAuxDb->nSession++;
20861 pSession->zName = sqlite3_mprintf("%s", zName);
20862 shell_check_oom(pSession->zName);
20863 }else
20864 /* If no command name matches, show a syntax error */
20865 session_syntax_error:
20866 showHelp(p->out, "session");
20867 }else
@@ -20905,15 +20951,16 @@
20951 int tno = sqlite3_column_int(pStmt, 0);
20952 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20953 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20954 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20955
20956 if( zOp==0 ) continue;
20957 if( zSql==0 ) continue;
20958 if( zAns==0 ) continue;
20959 k = 0;
20960 if( bVerbose>0 ){
 
20961 printf("%d: %s %s\n", tno, zOp, zSql);
 
20962 }
20963 if( strcmp(zOp,"memo")==0 ){
20964 utf8_printf(p->out, "%s\n", zSql);
20965 }else
20966 if( strcmp(zOp,"run")==0 ){
@@ -21027,10 +21074,11 @@
21074 initText(&sSql);
21075 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
21076 zSep = "VALUES(";
21077 while( SQLITE_ROW==sqlite3_step(pStmt) ){
21078 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21079 if( zTab==0 ) continue;
21080 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21081 if( strncmp(zTab, "sqlite_",7)!=0 ){
21082 appendText(&sQuery,"SELECT * FROM ", 0);
21083 appendText(&sQuery,zTab,'"');
21084 appendText(&sQuery," NOT INDEXED;", 0);
@@ -21067,10 +21115,11 @@
21115 "%s))"
21116 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
21117 " FROM [sha3sum$query]",
21118 sSql.z, iSize);
21119 }
21120 shell_check_oom(zSql);
21121 freeText(&sQuery);
21122 freeText(&sSql);
21123 if( bDebug ){
21124 utf8_printf(p->out, "%s\n", zSql);
21125 }else{
@@ -21090,15 +21139,15 @@
21139 raw_printf(stderr, "Usage: .system COMMAND\n");
21140 rc = 1;
21141 goto meta_command_exit;
21142 }
21143 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21144 for(i=2; i<nArg && zCmd!=0; i++){
21145 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
21146 zCmd, azArg[i]);
21147 }
21148 x = zCmd!=0 ? system(zCmd) : 1;
21149 sqlite3_free(zCmd);
21150 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21151 }else
21152 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21153
@@ -21230,16 +21279,16 @@
21279 while( sqlite3_step(pStmt)==SQLITE_ROW ){
21280 if( nRow>=nAlloc ){
21281 char **azNew;
21282 int n2 = nAlloc*2 + 10;
21283 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21284 shell_check_oom(azNew);
21285 nAlloc = n2;
21286 azResult = azNew;
21287 }
21288 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21289 shell_check_oom(azResult[nRow]);
21290 nRow++;
21291 }
21292 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
21293 rc = shellDatabaseError(p->db);
21294 }
@@ -22004,11 +22053,11 @@
22053 nLine = strlen30(zLine);
22054 if( nSql+nLine+2>=nAlloc ){
22055 /* Grow buffer by half-again increments when big. */
22056 nAlloc = nSql+(nSql>>1)+nLine+100;
22057 zSql = realloc(zSql, nAlloc);
22058 shell_check_oom(zSql);
22059 }
22060 if( nSql==0 ){
22061 int i;
22062 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
22063 assert( nAlloc>0 && zSql!=0 );
@@ -22136,10 +22185,11 @@
22185 raw_printf(stderr, "-- warning: cannot find home directory;"
22186 " cannot read ~/.sqliterc\n");
22187 return;
22188 }
22189 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
22190 shell_check_oom(zBuf);
22191 sqliterc = zBuf;
22192 }
22193 p->in = fopen(sqliterc,"rb");
22194 if( p->in ){
22195 if( stdin_is_interactive ){
@@ -22334,14 +22384,10 @@
22384 setBinaryMode(stdin, 0);
22385 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
22386 stdin_is_interactive = isatty(0);
22387 stdout_is_console = isatty(1);
22388
 
 
 
 
22389 #if !defined(_WIN32_WCE)
22390 if( getenv("SQLITE_DEBUG_BREAK") ){
22391 if( isatty(0) && isatty(2) ){
22392 fprintf(stderr,
22393 "attach debugger to process %d and press any key to continue.\n",
@@ -22377,20 +22423,20 @@
22423 ** memory that does not come from the SQLite memory allocator.
22424 */
22425 #if !SQLITE_SHELL_IS_UTF8
22426 sqlite3_initialize();
22427 argvToFree = malloc(sizeof(argv[0])*argc*2);
22428 shell_check_oom(argvToFree);
22429 argcToFree = argc;
22430 argv = argvToFree + argc;
 
22431 for(i=0; i<argc; i++){
22432 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
22433 int n;
22434 shell_check_oom(z);
22435 n = (int)strlen(z);
22436 argv[i] = malloc( n+1 );
22437 shell_check_oom(argv[i]);
22438 memcpy(argv[i], z, n+1);
22439 argvToFree[i] = argv[i];
22440 sqlite3_free(z);
22441 }
22442 sqlite3_shutdown();
@@ -22436,11 +22482,11 @@
22482 /* Excesss arguments are interpreted as SQL (or dot-commands) and
22483 ** mean that nothing is read from stdin */
22484 readStdin = 0;
22485 nCmd++;
22486 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
22487 shell_check_oom(azCmd);
22488 azCmd[nCmd-1] = z;
22489 }
22490 }
22491 if( z[1]=='-' ) z++;
22492 if( strcmp(z,"-separator")==0
22493
+419 -260
--- 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 "2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882"
457
+#define SQLITE_SOURCE_ID "2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -4128,17 +4128,18 @@
41284128
**
41294129
** The values returned by sqlite3_errcode() and/or
41304130
** sqlite3_extended_errcode() might change with each API call.
41314131
** Except, there are some interfaces that are guaranteed to never
41324132
** change the value of the error code. The error-code preserving
4133
-** interfaces are:
4133
+** interfaces include the following:
41344134
**
41354135
** <ul>
41364136
** <li> sqlite3_errcode()
41374137
** <li> sqlite3_extended_errcode()
41384138
** <li> sqlite3_errmsg()
41394139
** <li> sqlite3_errmsg16()
4140
+** <li> sqlite3_error_offset()
41404141
** </ul>
41414142
**
41424143
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
41434144
** text that describes the error, as either UTF-8 or UTF-16 respectively.
41444145
** ^(Memory to hold the error message string is managed internally.
@@ -4148,10 +4149,17 @@
41484149
**
41494150
** ^The sqlite3_errstr() interface returns the English-language text
41504151
** that describes the [result code], as UTF-8.
41514152
** ^(Memory to hold the error message string is managed internally
41524153
** and must not be freed by the application)^.
4154
+**
4155
+** ^If the most recent error references a specific token in the input
4156
+** SQL, the sqlite3_error_offset() interface returns the byte offset
4157
+** of the start of that token. ^The byte offset returned by
4158
+** sqlite3_error_offset() assumes that the input SQL is UTF8.
4159
+** ^If the most error does not reference a specific token in the input
4160
+** SQL, then the sqlite3_error_offset() function returns -1.
41534161
**
41544162
** When the serialized [threading mode] is in use, it might be the
41554163
** case that a second error occurs on a separate thread in between
41564164
** the time of the first error and the call to these interfaces.
41574165
** When that happens, the second error will be reported since these
@@ -4168,10 +4176,11 @@
41684176
SQLITE_API int sqlite3_errcode(sqlite3 *db);
41694177
SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
41704178
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
41714179
SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
41724180
SQLITE_API const char *sqlite3_errstr(int);
4181
+SQLITE_API int sqlite3_error_offset(sqlite3 *db);
41734182
41744183
/*
41754184
** CAPI3REF: Prepared Statement Object
41764185
** KEYWORDS: {prepared statement} {prepared statements}
41774186
**
@@ -9768,18 +9777,37 @@
97689777
97699778
/*
97709779
** CAPI3REF: Determine The Collation For a Virtual Table Constraint
97719780
**
97729781
** This function may only be called from within a call to the [xBestIndex]
9773
-** method of a [virtual table].
9782
+** method of a [virtual table]. This function returns a pointer to a string
9783
+** that is the name of the appropriate collation sequence to use for text
9784
+** comparisons on the constraint identified by its arguments.
97749785
**
9775
-** The first argument must be the sqlite3_index_info object that is the
9776
-** first parameter to the xBestIndex() method. The second argument must be
9777
-** an index into the aConstraint[] array belonging to the sqlite3_index_info
9778
-** structure passed to xBestIndex. This function returns a pointer to a buffer
9779
-** containing the name of the collation sequence for the corresponding
9780
-** constraint.
9786
+** The first argument must be the pointer to the sqlite3_index_info object
9787
+** that is the first parameter to the xBestIndex() method. The second argument
9788
+** must be an index into the aConstraint[] array belonging to the
9789
+** sqlite3_index_info structure passed to xBestIndex.
9790
+**
9791
+** Important:
9792
+** The first parameter must be the same pointer that is passed into the
9793
+** xBestMethod() method. The first parameter may not be a pointer to a
9794
+** different sqlite3_index_info object, even an exact copy.
9795
+**
9796
+** The return value is computed as follows:
9797
+**
9798
+** <ol>
9799
+** <li><p> If the constraint comes from a WHERE clause expression that contains
9800
+** a [COLLATE operator], then the name of the collation specified by
9801
+** that COLLATE operator is returned.
9802
+** <li><p> If there is no COLLATE operator, but the column that is the subject
9803
+** of the constraint specifies an alternative collating sequence via
9804
+** a [COLLATE clause] on the column definition within the CREATE TABLE
9805
+** statement that was passed into [sqlite3_declare_vtab()], then the
9806
+** name of that alternative collating sequence is returned.
9807
+** <li><p> Otherwise, "BINARY" is returned.
9808
+** </ol>
97819809
*/
97829810
SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
97839811
97849812
/*
97859813
** CAPI3REF: Conflict resolution modes
@@ -16489,10 +16517,11 @@
1648916517
i64 lastRowid; /* ROWID of most recent insert (see above) */
1649016518
i64 szMmap; /* Default mmap_size setting */
1649116519
u32 nSchemaLock; /* Do not reset the schema when non-zero */
1649216520
unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
1649316521
int errCode; /* Most recent error code (SQLITE_*) */
16522
+ int errByteOffset; /* Byte offset of error in SQL statement */
1649416523
int errMask; /* & result codes with this before returning */
1649516524
int iSysErrno; /* Errno value from last system error */
1649616525
u32 dbOptFlags; /* Flags to enable/disable optimizations */
1649716526
u8 enc; /* Text encoding */
1649816527
u8 autoCommit; /* The auto-commit flag. */
@@ -16725,10 +16754,11 @@
1672516754
#define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
1672616755
#define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
1672716756
/* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
1672816757
#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */
1672916758
#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
16759
+#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
1673016760
#define SQLITE_AllOpts 0xffffffff /* All optimizations */
1673116761
1673216762
/*
1673316763
** Macros for testing whether or not optimizations are enabled or disabled.
1673416764
*/
@@ -18519,10 +18549,12 @@
1851918549
TableLock *aTableLock; /* Required table locks for shared-cache mode */
1852018550
#endif
1852118551
AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
1852218552
Parse *pToplevel; /* Parse structure for main program (or NULL) */
1852318553
Table *pTriggerTab; /* Table triggers are being coded for */
18554
+ TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18555
+ ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
1852418556
union {
1852518557
int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
1852618558
Returning *pReturning; /* The RETURNING clause */
1852718559
} u1;
1852818560
u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
@@ -18573,13 +18605,11 @@
1857318605
const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
1857418606
#ifndef SQLITE_OMIT_VIRTUALTABLE
1857518607
Token sArg; /* Complete text of a module argument */
1857618608
Table **apVtabLock; /* Pointer to virtual tables needing locking */
1857718609
#endif
18578
- TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
1857918610
With *pWith; /* Current WITH clause, or NULL */
18580
- ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
1858118611
#ifndef SQLITE_OMIT_ALTERTABLE
1858218612
RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
1858318613
#endif
1858418614
};
1858518615
@@ -19366,11 +19396,11 @@
1936619396
SQLITE_PRIVATE void sqlite3Dequote(char*);
1936719397
SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
1936819398
SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
1936919399
SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
1937019400
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19371
-SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
19401
+SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*);
1937219402
SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
1937319403
SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
1937419404
SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
1937519405
SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
1937619406
SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
@@ -19646,10 +19676,11 @@
1964619676
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
1964719677
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
1964819678
SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
1964919679
SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
1965019680
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19681
+SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
1965119682
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
1965219683
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
1965319684
SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
1965419685
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
1965519686
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -19932,15 +19963,17 @@
1993219963
SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
1993319964
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
1993419965
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
1993519966
1993619967
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19968
+SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, int);
1993719969
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
1993819970
SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8);
1993919971
SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
1994019972
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
1994119973
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19974
+SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
1994219975
1994319976
SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
1994419977
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
1994519978
1994619979
#ifndef SQLITE_OMIT_SUBQUERY
@@ -22181,11 +22214,11 @@
2218122214
SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*);
2218222215
SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
2218322216
SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
2218422217
SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
2218522218
SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
22186
-SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
22219
+SQLITE_PRIVATE void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
2218722220
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
2218822221
2218922222
int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
2219022223
SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
2219122224
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
@@ -23319,22 +23352,21 @@
2331923352
**
2332023353
** Where NNN is an arbitrary floating-point number and "days" can be one
2332123354
** of several units of time.
2332223355
*/
2332323356
static const struct {
23324
- u8 eType; /* Transformation type code */
23325
- u8 nName; /* Length of th name */
23326
- char *zName; /* Name of the transformation */
23327
- double rLimit; /* Maximum NNN value for this transform */
23328
- double rXform; /* Constant used for this transform */
23357
+ u8 nName; /* Length of the name */
23358
+ char zName[7]; /* Name of the transformation */
23359
+ float rLimit; /* Maximum NNN value for this transform */
23360
+ float rXform; /* Constant used for this transform */
2332923361
} aXformType[] = {
23330
- { 0, 6, "second", 464269060800.0, 1000.0 },
23331
- { 0, 6, "minute", 7737817680.0, 60000.0 },
23332
- { 0, 4, "hour", 128963628.0, 3600000.0 },
23333
- { 0, 3, "day", 5373485.0, 86400000.0 },
23334
- { 1, 5, "month", 176546.0, 2592000000.0 },
23335
- { 2, 4, "year", 14713.0, 31536000000.0 },
23362
+ { 6, "second", 4.6427e+14, 1.0 },
23363
+ { 6, "minute", 7.7379e+12, 60.0 },
23364
+ { 4, "hour", 1.2897e+11, 3600.0 },
23365
+ { 3, "day", 5373485.0, 86400.0 },
23366
+ { 5, "month", 176546.0, 2592000.0 },
23367
+ { 4, "year", 14713.0, 31536000.0 },
2333623368
};
2333723369
2333823370
/*
2333923371
** Process a modifier to a date-time stamp. The modifiers are
2334023372
** as follows:
@@ -23567,33 +23599,35 @@
2356723599
for(i=0; i<ArraySize(aXformType); i++){
2356823600
if( aXformType[i].nName==n
2356923601
&& sqlite3_strnicmp(aXformType[i].zName, z, n)==0
2357023602
&& r>-aXformType[i].rLimit && r<aXformType[i].rLimit
2357123603
){
23572
- switch( aXformType[i].eType ){
23573
- case 1: { /* Special processing to add months */
23604
+ switch( i ){
23605
+ case 4: { /* Special processing to add months */
2357423606
int x;
23607
+ assert( strcmp(aXformType[i].zName,"month")==0 );
2357523608
computeYMD_HMS(p);
2357623609
p->M += (int)r;
2357723610
x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
2357823611
p->Y += x;
2357923612
p->M -= x*12;
2358023613
p->validJD = 0;
2358123614
r -= (int)r;
2358223615
break;
2358323616
}
23584
- case 2: { /* Special processing to add years */
23617
+ case 5: { /* Special processing to add years */
2358523618
int y = (int)r;
23619
+ assert( strcmp(aXformType[i].zName,"year")==0 );
2358623620
computeYMD_HMS(p);
2358723621
p->Y += y;
2358823622
p->validJD = 0;
2358923623
r -= (int)r;
2359023624
break;
2359123625
}
2359223626
}
2359323627
computeJD(p);
23594
- p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
23628
+ p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
2359523629
rc = 0;
2359623630
break;
2359723631
}
2359823632
}
2359923633
clearYMD_HMS_TZ(p);
@@ -29835,10 +29869,11 @@
2983529869
if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
2983629870
pToken = va_arg(ap, Token*);
2983729871
assert( bArgList==0 );
2983829872
if( pToken && pToken->n ){
2983929873
sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
29874
+ sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
2984029875
}
2984129876
length = width = 0;
2984229877
break;
2984329878
}
2984429879
case etSRCITEM: {
@@ -29889,18 +29924,42 @@
2988929924
zExtra = 0;
2989029925
}
2989129926
}/* End for loop over the format string */
2989229927
} /* End of function */
2989329928
29929
+
29930
+/*
29931
+** The z string points to the first character of a token that is
29932
+** associated with an error. If db does not already have an error
29933
+** byte offset recorded, try to compute the error byte offset for
29934
+** z and set the error byte offset in db.
29935
+*/
29936
+SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
29937
+ const Parse *pParse;
29938
+ const char *zText;
29939
+ const char *zEnd;
29940
+ assert( z!=0 );
29941
+ if( NEVER(db==0) ) return;
29942
+ if( db->errByteOffset!=(-2) ) return;
29943
+ pParse = db->pParse;
29944
+ if( NEVER(pParse==0) ) return;
29945
+ zText =pParse->zTail;
29946
+ if( NEVER(zText==0) ) return;
29947
+ zEnd = &zText[strlen(zText)];
29948
+ if( SQLITE_WITHIN(z,zText,zEnd) ){
29949
+ db->errByteOffset = (int)(z-zText);
29950
+ }
29951
+}
29952
+
2989429953
/*
2989529954
** Enlarge the memory allocation on a StrAccum object so that it is
2989629955
** able to accept at least N more bytes of text.
2989729956
**
2989829957
** Return the number of bytes of text that StrAccum is able to accept
2989929958
** after the attempted enlargement. The value returned might be zero.
2990029959
*/
29901
-static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29960
+SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum *p, int N){
2990229961
char *zNew;
2990329962
assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
2990429963
if( p->accError ){
2990529964
testcase(p->accError==SQLITE_TOOBIG);
2990629965
testcase(p->accError==SQLITE_NOMEM);
@@ -32212,20 +32271,25 @@
3221232271
** that would be appropriate.
3221332272
*/
3221432273
SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
3221532274
assert( db!=0 );
3221632275
db->errCode = err_code;
32217
- if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
32276
+ if( err_code || db->pErr ){
32277
+ sqlite3ErrorFinish(db, err_code);
32278
+ }else{
32279
+ db->errByteOffset = -1;
32280
+ }
3221832281
}
3221932282
3222032283
/*
3222132284
** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
3222232285
** and error message.
3222332286
*/
3222432287
SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
3222532288
assert( db!=0 );
3222632289
db->errCode = SQLITE_OK;
32290
+ db->errByteOffset = -1;
3222732291
if( db->pErr ) sqlite3ValueSetNull(db->pErr);
3222832292
}
3222932293
3223032294
/*
3223132295
** Load the sqlite3.iSysErrno field if that is an appropriate thing
@@ -32242,21 +32306,12 @@
3224232306
/*
3224332307
** Set the most recent error code and error string for the sqlite
3224432308
** handle "db". The error code is set to "err_code".
3224532309
**
3224632310
** If it is not NULL, string zFormat specifies the format of the
32247
-** error string in the style of the printf functions: The following
32248
-** format characters are allowed:
32249
-**
32250
-** %s Insert a string
32251
-** %z A string that should be freed after use
32252
-** %d Insert an integer
32253
-** %T Insert a token
32254
-** %S Insert the first element of a SrcList
32255
-**
32256
-** zFormat and any string tokens that follow it are assumed to be
32257
-** encoded in UTF-8.
32311
+** error string. zFormat and any string tokens that follow it are
32312
+** assumed to be encoded in UTF-8.
3225832313
**
3225932314
** To clear the most recent error for sqlite handle "db", sqlite3Error
3226032315
** should be called with err_code set to SQLITE_OK and zFormat set
3226132316
** to NULL.
3226232317
*/
@@ -32276,17 +32331,10 @@
3227632331
}
3227732332
}
3227832333
3227932334
/*
3228032335
** Add an error message to pParse->zErrMsg and increment pParse->nErr.
32281
-** The following formatting characters are allowed:
32282
-**
32283
-** %s Insert a string
32284
-** %z A string that should be freed after use
32285
-** %d Insert an integer
32286
-** %T Insert a token
32287
-** %S Insert the first element of a SrcList
3228832336
**
3228932337
** This function should be used to report any error that occurs while
3229032338
** compiling an SQL statement (i.e. within sqlite3_prepare()). The
3229132339
** last thing the sqlite3_prepare() function does is copy the error
3229232340
** stored by this function into the database handle using sqlite3Error().
@@ -32295,13 +32343,15 @@
3229532343
*/
3229632344
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
3229732345
char *zMsg;
3229832346
va_list ap;
3229932347
sqlite3 *db = pParse->db;
32348
+ db->errByteOffset = -2;
3230032349
va_start(ap, zFormat);
3230132350
zMsg = sqlite3VMPrintf(db, zFormat, ap);
3230232351
va_end(ap);
32352
+ if( db->errByteOffset<-1 ) db->errByteOffset = -1;
3230332353
if( db->suppressErr ){
3230432354
sqlite3DbFree(db, zMsg);
3230532355
}else{
3230632356
pParse->nErr++;
3230732357
sqlite3DbFree(db, pParse->zErrMsg);
@@ -66875,11 +66925,11 @@
6687566925
6687666926
pInfo->nKey = *(i64*)&iKey;
6687766927
pInfo->nPayload = nPayload;
6687866928
pInfo->pPayload = pIter;
6687966929
testcase( nPayload==pPage->maxLocal );
66880
- testcase( nPayload==pPage->maxLocal+1 );
66930
+ testcase( nPayload==(u32)pPage->maxLocal+1 );
6688166931
if( nPayload<=pPage->maxLocal ){
6688266932
/* This is the (easy) common case where the entire payload fits
6688366933
** on the local page. No overflow is required.
6688466934
*/
6688566935
pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66912,11 +66962,11 @@
6691266962
pIter++;
6691366963
pInfo->nKey = nPayload;
6691466964
pInfo->nPayload = nPayload;
6691566965
pInfo->pPayload = pIter;
6691666966
testcase( nPayload==pPage->maxLocal );
66917
- testcase( nPayload==pPage->maxLocal+1 );
66967
+ testcase( nPayload==(u32)pPage->maxLocal+1 );
6691866968
if( nPayload<=pPage->maxLocal ){
6691966969
/* This is the (easy) common case where the entire payload fits
6692066970
** on the local page. No overflow is required.
6692166971
*/
6692266972
pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66975,19 +67025,19 @@
6697567025
** past the end of the key value. */
6697667026
pEnd = &pIter[9];
6697767027
while( (*pIter++)&0x80 && pIter<pEnd );
6697867028
}
6697967029
testcase( nSize==pPage->maxLocal );
66980
- testcase( nSize==pPage->maxLocal+1 );
67030
+ testcase( nSize==(u32)pPage->maxLocal+1 );
6698167031
if( nSize<=pPage->maxLocal ){
6698267032
nSize += (u32)(pIter - pCell);
6698367033
if( nSize<4 ) nSize = 4;
6698467034
}else{
6698567035
int minLocal = pPage->minLocal;
6698667036
nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
6698767037
testcase( nSize==pPage->maxLocal );
66988
- testcase( nSize==pPage->maxLocal+1 );
67038
+ testcase( nSize==(u32)pPage->maxLocal+1 );
6698967039
if( nSize>pPage->maxLocal ){
6699067040
nSize = minLocal;
6699167041
}
6699267042
nSize += 4 + (u16)(pIter - pCell);
6699367043
}
@@ -69882,11 +69932,11 @@
6988269932
*/
6988369933
static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
6988469934
int nPage = get4byte(&pPage1->aData[28]);
6988569935
testcase( nPage==0 );
6988669936
if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
69887
- testcase( pBt->nPage!=nPage );
69937
+ testcase( pBt->nPage!=(u32)nPage );
6988869938
pBt->nPage = nPage;
6988969939
}
6989069940
6989169941
/*
6989269942
** Rollback the transaction in progress.
@@ -70903,11 +70953,11 @@
7090370953
if( pCur->iPage ){
7090470954
releasePageNotNull(pCur->pPage);
7090570955
while( --pCur->iPage ){
7090670956
releasePageNotNull(pCur->apPage[pCur->iPage]);
7090770957
}
70908
- pCur->pPage = pCur->apPage[0];
70958
+ pRoot = pCur->pPage = pCur->apPage[0];
7090970959
goto skip_init;
7091070960
}
7091170961
}else if( pCur->pgnoRoot==0 ){
7091270962
pCur->eState = CURSOR_INVALID;
7091370963
return SQLITE_EMPTY;
@@ -70950,11 +71000,10 @@
7095071000
skip_init:
7095171001
pCur->ix = 0;
7095271002
pCur->info.nSize = 0;
7095371003
pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
7095471004
70955
- pRoot = pCur->pPage;
7095671005
if( pRoot->nCell>0 ){
7095771006
pCur->eState = CURSOR_VALID;
7095871007
}else if( !pRoot->leaf ){
7095971008
Pgno subpage;
7096071009
if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
@@ -72458,11 +72507,11 @@
7245872507
assert( pPage->nFree>=0 );
7245972508
data = pPage->aData;
7246072509
ptr = &pPage->aCellIdx[2*idx];
7246172510
pc = get2byte(ptr);
7246272511
hdr = pPage->hdrOffset;
72463
- testcase( pc==get2byte(&data[hdr+5]) );
72512
+ testcase( pc==(u32)get2byte(&data[hdr+5]) );
7246472513
testcase( pc+sz==pPage->pBt->usableSize );
7246572514
if( pc+sz > pPage->pBt->usableSize ){
7246672515
*pRC = SQLITE_CORRUPT_BKPT;
7246772516
return;
7246872517
}
@@ -83085,18 +83134,18 @@
8308583134
#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
8308683135
#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
8308783136
8308883137
/*
8308983138
** Deserialize the data blob pointed to by buf as serial type serial_type
83090
-** and store the result in pMem. Return the number of bytes read.
83139
+** and store the result in pMem.
8309183140
**
8309283141
** This function is implemented as two separate routines for performance.
8309383142
** The few cases that require local variables are broken out into a separate
8309483143
** routine so that in most cases the overhead of moving the stack pointer
8309583144
** is avoided.
8309683145
*/
83097
-static u32 serialGet(
83146
+static void serialGet(
8309883147
const unsigned char *buf, /* Buffer to deserialize from */
8309983148
u32 serial_type, /* Serial type to deserialize */
8310083149
Mem *pMem /* Memory cell to write value into */
8310183150
){
8310283151
u64 x = FOUR_BYTE_UINT(buf);
@@ -83126,13 +83175,12 @@
8312683175
assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
8312783176
swapMixedEndianFloat(x);
8312883177
memcpy(&pMem->u.r, &x, sizeof(x));
8312983178
pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
8313083179
}
83131
- return 8;
8313283180
}
83133
-SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
83181
+SQLITE_PRIVATE void sqlite3VdbeSerialGet(
8313483182
const unsigned char *buf, /* Buffer to deserialize from */
8313583183
u32 serial_type, /* Serial type to deserialize */
8313683184
Mem *pMem /* Memory cell to write value into */
8313783185
){
8313883186
switch( serial_type ){
@@ -83139,41 +83187,41 @@
8313983187
case 10: { /* Internal use only: NULL with virtual table
8314083188
** UPDATE no-change flag set */
8314183189
pMem->flags = MEM_Null|MEM_Zero;
8314283190
pMem->n = 0;
8314383191
pMem->u.nZero = 0;
83144
- break;
83192
+ return;
8314583193
}
8314683194
case 11: /* Reserved for future use */
8314783195
case 0: { /* Null */
8314883196
/* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
8314983197
pMem->flags = MEM_Null;
83150
- break;
83198
+ return;
8315183199
}
8315283200
case 1: {
8315383201
/* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
8315483202
** integer. */
8315583203
pMem->u.i = ONE_BYTE_INT(buf);
8315683204
pMem->flags = MEM_Int;
8315783205
testcase( pMem->u.i<0 );
83158
- return 1;
83206
+ return;
8315983207
}
8316083208
case 2: { /* 2-byte signed integer */
8316183209
/* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
8316283210
** twos-complement integer. */
8316383211
pMem->u.i = TWO_BYTE_INT(buf);
8316483212
pMem->flags = MEM_Int;
8316583213
testcase( pMem->u.i<0 );
83166
- return 2;
83214
+ return;
8316783215
}
8316883216
case 3: { /* 3-byte signed integer */
8316983217
/* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
8317083218
** twos-complement integer. */
8317183219
pMem->u.i = THREE_BYTE_INT(buf);
8317283220
pMem->flags = MEM_Int;
8317383221
testcase( pMem->u.i<0 );
83174
- return 3;
83222
+ return;
8317583223
}
8317683224
case 4: { /* 4-byte signed integer */
8317783225
/* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
8317883226
** twos-complement integer. */
8317983227
pMem->u.i = FOUR_BYTE_INT(buf);
@@ -83181,33 +83229,34 @@
8318183229
/* Work around a sign-extension bug in the HP compiler for HP/UX */
8318283230
if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
8318383231
#endif
8318483232
pMem->flags = MEM_Int;
8318583233
testcase( pMem->u.i<0 );
83186
- return 4;
83234
+ return;
8318783235
}
8318883236
case 5: { /* 6-byte signed integer */
8318983237
/* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
8319083238
** twos-complement integer. */
8319183239
pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
8319283240
pMem->flags = MEM_Int;
8319383241
testcase( pMem->u.i<0 );
83194
- return 6;
83242
+ return;
8319583243
}
8319683244
case 6: /* 8-byte signed integer */
8319783245
case 7: { /* IEEE floating point */
8319883246
/* These use local variables, so do them in a separate routine
8319983247
** to avoid having to move the frame pointer in the common case */
83200
- return serialGet(buf,serial_type,pMem);
83248
+ serialGet(buf,serial_type,pMem);
83249
+ return;
8320183250
}
8320283251
case 8: /* Integer 0 */
8320383252
case 9: { /* Integer 1 */
8320483253
/* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
8320583254
/* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
8320683255
pMem->u.i = serial_type-8;
8320783256
pMem->flags = MEM_Int;
83208
- return 0;
83257
+ return;
8320983258
}
8321083259
default: {
8321183260
/* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
8321283261
** length.
8321383262
** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
@@ -83214,14 +83263,14 @@
8321483263
** (N-13)/2 bytes in length. */
8321583264
static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
8321683265
pMem->z = (char *)buf;
8321783266
pMem->n = (serial_type-12)/2;
8321883267
pMem->flags = aFlag[serial_type&1];
83219
- return pMem->n;
83268
+ return;
8322083269
}
8322183270
}
83222
- return 0;
83271
+ return;
8322383272
}
8322483273
/*
8322583274
** This routine is used to allocate sufficient space for an UnpackedRecord
8322683275
** structure large enough to be used with sqlite3VdbeRecordUnpack() if
8322783276
** the first argument is a pointer to KeyInfo structure pKeyInfo.
@@ -83280,11 +83329,12 @@
8328083329
pMem->enc = pKeyInfo->enc;
8328183330
pMem->db = pKeyInfo->db;
8328283331
/* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
8328383332
pMem->szMalloc = 0;
8328483333
pMem->z = 0;
83285
- d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
83334
+ sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
83335
+ d += sqlite3VdbeSerialTypeLen(serial_type);
8328683336
pMem++;
8328783337
if( (++u)>=p->nField ) break;
8328883338
}
8328983339
if( d>(u32)nKey && u ){
8329083340
assert( CORRUPT_DB );
@@ -83364,11 +83414,12 @@
8336483414
break;
8336583415
}
8336683416
8336783417
/* Extract the values to be compared.
8336883418
*/
83369
- d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
83419
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
83420
+ d1 += sqlite3VdbeSerialTypeLen(serial_type1);
8337083421
8337183422
/* Do the comparison
8337283423
*/
8337383424
rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
8337483425
pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
@@ -84168,11 +84219,11 @@
8416884219
}
8416984220
8417084221
/* The index entry must begin with a header size */
8417184222
getVarint32NR((u8*)m.z, szHdr);
8417284223
testcase( szHdr==3 );
84173
- testcase( szHdr==m.n );
84224
+ testcase( szHdr==(u32)m.n );
8417484225
testcase( szHdr>0x7fffffff );
8417584226
assert( m.n>=0 );
8417684227
if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
8417784228
goto idx_rowid_corruption;
8417884229
}
@@ -87446,11 +87497,10 @@
8744687497
int i, mx;
8744787498
u64 h = 0;
8744887499
8744987500
i = pOp->p3;
8745087501
assert( pOp->p4type==P4_INT32 );
87451
- mx = i + pOp->p4.i;
8745287502
for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
8745387503
const Mem *p = &aMem[i];
8745487504
if( p->flags & (MEM_Int|MEM_IntReal) ){
8745587505
h += p->u.i;
8745687506
}else if( p->flags & MEM_Real ){
@@ -100064,12 +100114,13 @@
100064100114
hit = 1;
100065100115
}
100066100116
}
100067100117
if( hit || zTab==0 ) continue;
100068100118
}
100069
- if( zDb && pTab->pSchema!=pSchema ){
100070
- continue;
100119
+ if( zDb ){
100120
+ if( pTab->pSchema!=pSchema ) continue;
100121
+ if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
100071100122
}
100072100123
if( zTab ){
100073100124
const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
100074100125
assert( zTabName!=0 );
100075100126
if( sqlite3StrICmp(zTabName, zTab)!=0 ){
@@ -100196,10 +100247,11 @@
100196100247
{
100197100248
assert( ExprUseYTab(pExpr) );
100198100249
pExpr->y.pTab = pTab;
100199100250
if( pParse->bReturning ){
100200100251
eNewExprOp = TK_REGISTER;
100252
+ pExpr->op2 = TK_COLUMN;
100201100253
pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
100202100254
sqlite3TableColumnToStorage(pTab, iCol) + 1;
100203100255
}else{
100204100256
pExpr->iColumn = (i16)iCol;
100205100257
eNewExprOp = TK_TRIGGER;
@@ -109275,11 +109327,10 @@
109275109327
sqlite3 *db, /* Database handle */
109276109328
const char *zSql, /* SQL to parse */
109277109329
int bTemp /* True if SQL is from temp schema */
109278109330
){
109279109331
int rc;
109280
- char *zErr = 0;
109281109332
109282109333
db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109283109334
109284109335
/* Parse the SQL statement passed as the first argument. If no error
109285109336
** occurs and the parse does not result in a new table, index or
@@ -109286,14 +109337,11 @@
109286109337
** trigger object, the database must be corrupt. */
109287109338
memset(p, 0, sizeof(Parse));
109288109339
p->eParseMode = PARSE_MODE_RENAME;
109289109340
p->db = db;
109290109341
p->nQueryLoop = 1;
109291
- rc = zSql ? sqlite3RunParser(p, zSql, &zErr) : SQLITE_NOMEM;
109292
- assert( p->zErrMsg==0 );
109293
- assert( rc!=SQLITE_OK || zErr==0 );
109294
- p->zErrMsg = zErr;
109342
+ rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM;
109295109343
if( db->mallocFailed ) rc = SQLITE_NOMEM;
109296109344
if( rc==SQLITE_OK
109297109345
&& p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
109298109346
){
109299109347
rc = SQLITE_CORRUPT_BKPT;
@@ -113496,11 +113544,10 @@
113496113544
** built-in function.
113497113545
*/
113498113546
SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
113499113547
va_list ap;
113500113548
char *zSql;
113501
- char *zErrMsg = 0;
113502113549
sqlite3 *db = pParse->db;
113503113550
u32 savedDbFlags = db->mDbFlags;
113504113551
char saveBuf[PARSE_TAIL_SZ];
113505113552
113506113553
if( pParse->nErr ) return;
@@ -113518,13 +113565,12 @@
113518113565
}
113519113566
pParse->nested++;
113520113567
memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
113521113568
memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
113522113569
db->mDbFlags |= DBFLAG_PreferBuiltin;
113523
- sqlite3RunParser(pParse, zSql, &zErrMsg);
113570
+ sqlite3RunParser(pParse, zSql);
113524113571
db->mDbFlags = savedDbFlags;
113525
- sqlite3DbFree(db, zErrMsg);
113526113572
sqlite3DbFree(db, zSql);
113527113573
memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
113528113574
pParse->nested--;
113529113575
}
113530113576
@@ -121372,87 +121418,95 @@
121372121418
'0', '1', '2', '3', '4', '5', '6', '7',
121373121419
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
121374121420
};
121375121421
121376121422
/*
121377
-** Implementation of the QUOTE() function. This function takes a single
121378
-** argument. If the argument is numeric, the return value is the same as
121379
-** the argument. If the argument is NULL, the return value is the string
121380
-** "NULL". Otherwise, the argument is enclosed in single quotes with
121381
-** single-quote escapes.
121423
+** Append to pStr text that is the SQL literal representation of the
121424
+** value contained in pValue.
121382121425
*/
121383
-static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
121384
- assert( argc==1 );
121385
- UNUSED_PARAMETER(argc);
121386
- switch( sqlite3_value_type(argv[0]) ){
121426
+SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
121427
+ /* As currently implemented, the string must be initially empty.
121428
+ ** we might relax this requirement in the future, but that will
121429
+ ** require enhancements to the implementation. */
121430
+ assert( pStr!=0 && pStr->nChar==0 );
121431
+
121432
+ switch( sqlite3_value_type(pValue) ){
121387121433
case SQLITE_FLOAT: {
121388121434
double r1, r2;
121389
- char zBuf[50];
121390
- r1 = sqlite3_value_double(argv[0]);
121391
- sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
121392
- sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
121393
- if( r1!=r2 ){
121394
- sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
121395
- }
121396
- sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
121435
+ const char *zVal;
121436
+ r1 = sqlite3_value_double(pValue);
121437
+ sqlite3_str_appendf(pStr, "%!.15g", r1);
121438
+ zVal = sqlite3_str_value(pStr);
121439
+ if( zVal ){
121440
+ sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
121441
+ if( r1!=r2 ){
121442
+ sqlite3_str_reset(pStr);
121443
+ sqlite3_str_appendf(pStr, "%!.20e", r1);
121444
+ }
121445
+ }
121397121446
break;
121398121447
}
121399121448
case SQLITE_INTEGER: {
121400
- sqlite3_result_value(context, argv[0]);
121449
+ sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
121401121450
break;
121402121451
}
121403121452
case SQLITE_BLOB: {
121404
- char *zText = 0;
121405
- char const *zBlob = sqlite3_value_blob(argv[0]);
121406
- int nBlob = sqlite3_value_bytes(argv[0]);
121407
- assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
121408
- zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
121409
- if( zText ){
121453
+ char const *zBlob = sqlite3_value_blob(pValue);
121454
+ int nBlob = sqlite3_value_bytes(pValue);
121455
+ assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */
121456
+ sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
121457
+ if( pStr->accError==0 ){
121458
+ char *zText = pStr->zText;
121410121459
int i;
121411121460
for(i=0; i<nBlob; i++){
121412121461
zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
121413121462
zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
121414121463
}
121415121464
zText[(nBlob*2)+2] = '\'';
121416121465
zText[(nBlob*2)+3] = '\0';
121417121466
zText[0] = 'X';
121418121467
zText[1] = '\'';
121419
- sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
121420
- sqlite3_free(zText);
121468
+ pStr->nChar = nBlob*2 + 3;
121421121469
}
121422121470
break;
121423121471
}
121424121472
case SQLITE_TEXT: {
121425
- int i,j;
121426
- u64 n;
121427
- const unsigned char *zArg = sqlite3_value_text(argv[0]);
121428
- char *z;
121429
-
121430
- if( zArg==0 ) return;
121431
- for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
121432
- z = contextMalloc(context, ((i64)i)+((i64)n)+3);
121433
- if( z ){
121434
- z[0] = '\'';
121435
- for(i=0, j=1; zArg[i]; i++){
121436
- z[j++] = zArg[i];
121437
- if( zArg[i]=='\'' ){
121438
- z[j++] = '\'';
121439
- }
121440
- }
121441
- z[j++] = '\'';
121442
- z[j] = 0;
121443
- sqlite3_result_text(context, z, j, sqlite3_free);
121444
- }
121473
+ const unsigned char *zArg = sqlite3_value_text(pValue);
121474
+ sqlite3_str_appendf(pStr, "%Q", zArg);
121445121475
break;
121446121476
}
121447121477
default: {
121448
- assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
121449
- sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
121478
+ assert( sqlite3_value_type(pValue)==SQLITE_NULL );
121479
+ sqlite3_str_append(pStr, "NULL", 4);
121450121480
break;
121451121481
}
121452121482
}
121453121483
}
121484
+
121485
+/*
121486
+** Implementation of the QUOTE() function.
121487
+**
121488
+** The quote(X) function returns the text of an SQL literal which is the
121489
+** value of its argument suitable for inclusion into an SQL statement.
121490
+** Strings are surrounded by single-quotes with escapes on interior quotes
121491
+** as needed. BLOBs are encoded as hexadecimal literals. Strings with
121492
+** embedded NUL characters cannot be represented as string literals in SQL
121493
+** and hence the returned string literal is truncated prior to the first NUL.
121494
+*/
121495
+static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
121496
+ sqlite3_str str;
121497
+ sqlite3 *db = sqlite3_context_db_handle(context);
121498
+ assert( argc==1 );
121499
+ UNUSED_PARAMETER(argc);
121500
+ sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
121501
+ sqlite3QuoteValue(&str,argv[0]);
121502
+ sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
121503
+ SQLITE_DYNAMIC);
121504
+ if( str.accError==SQLITE_NOMEM ){
121505
+ sqlite3_result_error_nomem(context);
121506
+ }
121507
+}
121454121508
121455121509
/*
121456121510
** The unicode() function. Return the integer unicode code-point value
121457121511
** for the first character of the input string.
121458121512
*/
@@ -126174,10 +126228,11 @@
126174126228
** the UNIQUE constraints have run.
126175126229
*/
126176126230
if( onError==OE_Replace /* IPK rule is REPLACE */
126177126231
&& onError!=overrideError /* Rules for other constraints are different */
126178126232
&& pTab->pIndex /* There exist other constraints */
126233
+ && !upsertIpkDelay /* IPK check already deferred by UPSERT */
126179126234
){
126180126235
ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
126181126236
VdbeComment((v, "defer IPK REPLACE until last"));
126182126237
}
126183126238
@@ -126582,10 +126637,11 @@
126582126637
126583126638
/* If the IPK constraint is a REPLACE, run it last */
126584126639
if( ipkTop ){
126585126640
sqlite3VdbeGoto(v, ipkTop);
126586126641
VdbeComment((v, "Do IPK REPLACE"));
126642
+ assert( ipkBottom>0 );
126587126643
sqlite3VdbeJumpHere(v, ipkBottom);
126588126644
}
126589126645
126590126646
/* Recheck all uniqueness constraints after replace triggers have run */
126591126647
testcase( regTrigCnt!=0 && nReplaceTrig==0 );
@@ -127802,10 +127858,12 @@
127802127858
sqlite3_int64 (*total_changes64)(sqlite3*);
127803127859
/* Version 3.37.0 and later */
127804127860
int (*autovacuum_pages)(sqlite3*,
127805127861
unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
127806127862
void*, void(*)(void*));
127863
+ /* Version 3.38.0 and later */
127864
+ int (*error_offset)(sqlite3*);
127807127865
};
127808127866
127809127867
/*
127810127868
** This is the function signature used for all extension entry points. It
127811127869
** is also defined in the file "loadext.c".
@@ -128113,10 +128171,12 @@
128113128171
/* Version 3.36.1 and later */
128114128172
#define sqlite3_changes64 sqlite3_api->changes64
128115128173
#define sqlite3_total_changes64 sqlite3_api->total_changes64
128116128174
/* Version 3.37.0 and later */
128117128175
#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
128176
+/* Version 3.38.0 and later */
128177
+#define sqlite3_error_offset sqlite3_api->error_offset
128118128178
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
128119128179
128120128180
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
128121128181
/* This case when the file really is being compiled as a loadable
128122128182
** extension */
@@ -128602,10 +128662,12 @@
128602128662
/* Version 3.36.1 and later */
128603128663
sqlite3_changes64,
128604128664
sqlite3_total_changes64,
128605128665
/* Version 3.37.0 and later */
128606128666
sqlite3_autovacuum_pages,
128667
+ /* Version 3.38.0 and later */
128668
+ sqlite3_error_offset,
128607128669
};
128608128670
128609128671
/* True if x is the directory separator character
128610128672
*/
128611128673
#if SQLITE_OS_WIN
@@ -132941,10 +133003,14 @@
132941133003
/*
132942133004
** Free all memory allocations in the pParse object
132943133005
*/
132944133006
SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
132945133007
sqlite3 *db = pParse->db;
133008
+ assert( pParse->nested==0 );
133009
+#ifndef SQLITE_OMIT_SHARED_CACHE
133010
+ sqlite3DbFree(db, pParse->aTableLock);
133011
+#endif
132946133012
while( pParse->pCleanup ){
132947133013
ParseCleanup *pCleanup = pParse->pCleanup;
132948133014
pParse->pCleanup = pCleanup->pNext;
132949133015
pCleanup->xCleanup(db, pCleanup->pPtr);
132950133016
sqlite3DbFreeNN(db, pCleanup);
@@ -133020,11 +133086,10 @@
133020133086
u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133021133087
Vdbe *pReprepare, /* VM being reprepared */
133022133088
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133023133089
const char **pzTail /* OUT: End of parsed string */
133024133090
){
133025
- char *zErrMsg = 0; /* Error message */
133026133091
int rc = SQLITE_OK; /* Result code */
133027133092
int i; /* Loop counter */
133028133093
Parse sParse; /* Parsing context */
133029133094
133030133095
memset(&sParse, 0, PARSE_HDR_SZ);
@@ -133095,18 +133160,18 @@
133095133160
rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
133096133161
goto end_prepare;
133097133162
}
133098133163
zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
133099133164
if( zSqlCopy ){
133100
- sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
133165
+ sqlite3RunParser(&sParse, zSqlCopy);
133101133166
sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
133102133167
sqlite3DbFree(db, zSqlCopy);
133103133168
}else{
133104133169
sParse.zTail = &zSql[nBytes];
133105133170
}
133106133171
}else{
133107
- sqlite3RunParser(&sParse, zSql, &zErrMsg);
133172
+ sqlite3RunParser(&sParse, zSql);
133108133173
}
133109133174
assert( 0==sParse.nQueryLoop );
133110133175
133111133176
if( pzTail ){
133112133177
*pzTail = sParse.zTail;
@@ -133126,18 +133191,18 @@
133126133191
if( sParse.pVdbe ){
133127133192
sqlite3VdbeFinalize(sParse.pVdbe);
133128133193
}
133129133194
assert( 0==(*ppStmt) );
133130133195
rc = sParse.rc;
133131
- if( zErrMsg ){
133132
- sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
133133
- sqlite3DbFree(db, zErrMsg);
133196
+ if( sParse.zErrMsg ){
133197
+ sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
133198
+ sqlite3DbFree(db, sParse.zErrMsg);
133134133199
}else{
133135133200
sqlite3Error(db, rc);
133136133201
}
133137133202
}else{
133138
- assert( zErrMsg==0 );
133203
+ assert( sParse.zErrMsg==0 );
133139133204
*ppStmt = (sqlite3_stmt*)sParse.pVdbe;
133140133205
rc = SQLITE_OK;
133141133206
sqlite3ErrorClear(db);
133142133207
}
133143133208
@@ -136696,10 +136761,12 @@
136696136761
Select *p, /* The right-most of SELECTs to be coded */
136697136762
SelectDest *pDest /* What to do with query results */
136698136763
){
136699136764
int i, j; /* Loop counters */
136700136765
Select *pPrior; /* Another SELECT immediately to our left */
136766
+ Select *pSplit; /* Left-most SELECT in the right-hand group */
136767
+ int nSelect; /* Number of SELECT statements in the compound */
136701136768
Vdbe *v; /* Generate code to this VDBE */
136702136769
SelectDest destA; /* Destination for coroutine A */
136703136770
SelectDest destB; /* Destination for coroutine B */
136704136771
int regAddrA; /* Address register for select-A coroutine */
136705136772
int regAddrB; /* Address register for select-B coroutine */
@@ -136741,12 +136808,11 @@
136741136808
136742136809
136743136810
/* Patch up the ORDER BY clause
136744136811
*/
136745136812
op = p->op;
136746
- pPrior = p->pPrior;
136747
- assert( pPrior->pOrderBy==0 );
136813
+ assert( p->pPrior->pOrderBy==0 );
136748136814
pOrderBy = p->pOrderBy;
136749136815
assert( pOrderBy );
136750136816
nOrderBy = pOrderBy->nExpr;
136751136817
136752136818
/* For operators other than UNION ALL we have to make sure that
@@ -136792,15 +136858,10 @@
136792136858
pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
136793136859
}else{
136794136860
pKeyMerge = 0;
136795136861
}
136796136862
136797
- /* Reattach the ORDER BY clause to the query.
136798
- */
136799
- p->pOrderBy = pOrderBy;
136800
- pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
136801
-
136802136863
/* Allocate a range of temporary registers and the KeyInfo needed
136803136864
** for the logic that removes duplicate result rows when the
136804136865
** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
136805136866
*/
136806136867
if( op==TK_ALL ){
@@ -136821,16 +136882,33 @@
136821136882
}
136822136883
}
136823136884
136824136885
/* Separate the left and the right query from one another
136825136886
*/
136826
- p->pPrior = 0;
136887
+ nSelect = 1;
136888
+ if( (op==TK_ALL || op==TK_UNION)
136889
+ && OptimizationEnabled(db, SQLITE_BalancedMerge)
136890
+ ){
136891
+ for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
136892
+ nSelect++;
136893
+ assert( pSplit->pPrior->pNext==pSplit );
136894
+ }
136895
+ }
136896
+ if( nSelect<=3 ){
136897
+ pSplit = p;
136898
+ }else{
136899
+ pSplit = p;
136900
+ for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
136901
+ }
136902
+ pPrior = pSplit->pPrior;
136903
+ pSplit->pPrior = 0;
136827136904
pPrior->pNext = 0;
136905
+ assert( p->pOrderBy == pOrderBy );
136906
+ assert( pOrderBy!=0 || db->mallocFailed );
136907
+ pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
136828136908
sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
136829
- if( pPrior->pPrior==0 ){
136830
- sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
136831
- }
136909
+ sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
136832136910
136833136911
/* Compute the limit registers */
136834136912
computeLimitRegisters(pParse, p, labelEnd);
136835136913
if( p->iLimit && op==TK_ALL ){
136836136914
regLimitA = ++pParse->nMem;
@@ -136977,16 +137055,15 @@
136977137055
*/
136978137056
sqlite3VdbeResolveLabel(v, labelEnd);
136979137057
136980137058
/* Reassembly the compound query so that it will be freed correctly
136981137059
** by the calling function */
136982
- if( p->pPrior ){
136983
- sqlite3SelectDelete(db, p->pPrior);
137060
+ if( pSplit->pPrior ){
137061
+ sqlite3SelectDelete(db, pSplit->pPrior);
136984137062
}
136985
- p->pPrior = pPrior;
136986
- pPrior->pNext = p;
136987
-
137063
+ pSplit->pPrior = pPrior;
137064
+ pPrior->pNext = pSplit;
136988137065
sqlite3ExprListDelete(db, pPrior->pOrderBy);
136989137066
pPrior->pOrderBy = 0;
136990137067
136991137068
/*** TBD: Insert subroutine calls to close cursors on incomplete
136992137069
**** subqueries ****/
@@ -142081,11 +142158,16 @@
142081142158
int reg = pParse->nMem+1;
142082142159
pParse->nMem += nCol+2;
142083142160
pReturning->iRetReg = reg;
142084142161
for(i=0; i<nCol; i++){
142085142162
Expr *pCol = pNew->a[i].pExpr;
142163
+ assert( pCol!=0 || pParse->db->mallocFailed );
142164
+ if( pCol==0 ) continue;
142086142165
sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
142166
+ if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
142167
+ sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
142168
+ }
142087142169
}
142088142170
sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
142089142171
sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
142090142172
sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
142091142173
}
@@ -145420,11 +145502,10 @@
145420145502
*/
145421145503
SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
145422145504
VtabCtx *pCtx;
145423145505
int rc = SQLITE_OK;
145424145506
Table *pTab;
145425
- char *zErr = 0;
145426145507
Parse sParse;
145427145508
int initBusy;
145428145509
145429145510
#ifdef SQLITE_ENABLE_API_ARMOR
145430145511
if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
@@ -145449,15 +145530,16 @@
145449145530
** in case a bug arises. */
145450145531
assert( db->init.busy==0 );
145451145532
initBusy = db->init.busy;
145452145533
db->init.busy = 0;
145453145534
sParse.nQueryLoop = 1;
145454
- if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
145455
- && sParse.pNewTable
145456
- && !db->mallocFailed
145535
+ if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable)
145536
+ && ALWAYS(sParse.pNewTable!=0)
145537
+ && ALWAYS(!db->mallocFailed)
145457145538
&& IsOrdinaryTable(sParse.pNewTable)
145458145539
){
145540
+ assert( sParse.zErrMsg==0 );
145459145541
if( !pTab->aCol ){
145460145542
Table *pNew = sParse.pNewTable;
145461145543
Index *pIdx;
145462145544
pTab->aCol = pNew->aCol;
145463145545
sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
@@ -145483,12 +145565,13 @@
145483145565
pIdx->pTable = pTab;
145484145566
}
145485145567
}
145486145568
pCtx->bDeclared = 1;
145487145569
}else{
145488
- sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
145489
- sqlite3DbFree(db, zErr);
145570
+ sqlite3ErrorWithMsg(db, SQLITE_ERROR,
145571
+ (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
145572
+ sqlite3DbFree(db, sParse.zErrMsg);
145490145573
rc = SQLITE_ERROR;
145491145574
}
145492145575
sParse.eParseMode = PARSE_MODE_NORMAL;
145493145576
145494145577
if( sParse.pVdbe ){
@@ -146251,11 +146334,11 @@
146251146334
#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
146252146335
#define TERM_CODED 0x0004 /* This term is already coded */
146253146336
#define TERM_COPIED 0x0008 /* Has a child */
146254146337
#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
146255146338
#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
146256
-#define TERM_OR_OK 0x0040 /* Used during OR-clause processing */
146339
+#define TERM_OK 0x0040 /* Used during OR-clause processing */
146257146340
#define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
146258146341
#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
146259146342
#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
146260146343
#define TERM_LIKE 0x0400 /* The original LIKE operator */
146261146344
#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
@@ -147961,11 +148044,11 @@
147961148044
){
147962148045
while( ++iLevel < pWInfo->nLevel ){
147963148046
WhereLevel *pLevel = &pWInfo->a[iLevel];
147964148047
WhereLoop *pLoop = pLevel->pWLoop;
147965148048
if( pLevel->regFilter==0 ) continue;
147966
- /* ,--- Because constructBloomFilter() has will not have set
148049
+ /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
147967148050
** vvvvv--' pLevel->regFilter if this were true. */
147968148051
if( NEVER(pLoop->prereq & notReady) ) continue;
147969148052
if( pLoop->wsFlags & WHERE_IPK ){
147970148053
WhereTerm *pTerm = pLoop->aLTerm[0];
147971148054
int regRowid;
@@ -147981,10 +148064,11 @@
147981148064
u16 nEq = pLoop->u.btree.nEq;
147982148065
int r1;
147983148066
char *zStartAff;
147984148067
147985148068
assert( pLoop->wsFlags & WHERE_INDEXED );
148069
+ assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
147986148070
r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
147987148071
codeApplyAffinity(pParse, r1, nEq, zStartAff);
147988148072
sqlite3DbFree(pParse->db, zStartAff);
147989148073
sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
147990148074
addrNxt, r1, nEq);
@@ -150043,11 +150127,11 @@
150043150127
for(j=0; j<2 && !okToChngToIN; j++){
150044150128
Expr *pLeft = 0;
150045150129
pOrTerm = pOrWc->a;
150046150130
for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
150047150131
assert( pOrTerm->eOperator & WO_EQ );
150048
- pOrTerm->wtFlags &= ~TERM_OR_OK;
150132
+ pOrTerm->wtFlags &= ~TERM_OK;
150049150133
if( pOrTerm->leftCursor==iCursor ){
150050150134
/* This is the 2-bit case and we are on the second iteration and
150051150135
** current term is from the first iteration. So skip this term. */
150052150136
assert( j==1 );
150053150137
continue;
@@ -150084,11 +150168,11 @@
150084150168
okToChngToIN = 1;
150085150169
for(; i>=0 && okToChngToIN; i--, pOrTerm++){
150086150170
assert( pOrTerm->eOperator & WO_EQ );
150087150171
assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150088150172
if( pOrTerm->leftCursor!=iCursor ){
150089
- pOrTerm->wtFlags &= ~TERM_OR_OK;
150173
+ pOrTerm->wtFlags &= ~TERM_OK;
150090150174
}else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
150091150175
&& sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
150092150176
)){
150093150177
okToChngToIN = 0;
150094150178
}else{
@@ -150100,11 +150184,11 @@
150100150184
affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
150101150185
affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
150102150186
if( affRight!=0 && affRight!=affLeft ){
150103150187
okToChngToIN = 0;
150104150188
}else{
150105
- pOrTerm->wtFlags |= TERM_OR_OK;
150189
+ pOrTerm->wtFlags |= TERM_OK;
150106150190
}
150107150191
}
150108150192
}
150109150193
}
150110150194
@@ -150117,11 +150201,11 @@
150117150201
ExprList *pList = 0; /* The RHS of the IN operator */
150118150202
Expr *pLeft = 0; /* The LHS of the IN operator */
150119150203
Expr *pNew; /* The complete IN operator */
150120150204
150121150205
for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
150122
- if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
150206
+ if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
150123150207
assert( pOrTerm->eOperator & WO_EQ );
150124150208
assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150125150209
assert( pOrTerm->leftCursor==iCursor );
150126150210
assert( pOrTerm->u.x.leftColumn==iColumn );
150127150211
pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
@@ -151651,16 +151735,18 @@
151651151735
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
151652151736
static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
151653151737
int i;
151654151738
if( !sqlite3WhereTrace ) return;
151655151739
for(i=0; i<p->nConstraint; i++){
151656
- sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
151740
+ sqlite3DebugPrintf(
151741
+ " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
151657151742
i,
151658151743
p->aConstraint[i].iColumn,
151659151744
p->aConstraint[i].iTermOffset,
151660151745
p->aConstraint[i].op,
151661
- p->aConstraint[i].usable);
151746
+ p->aConstraint[i].usable,
151747
+ sqlite3_vtab_collation(p,i));
151662151748
}
151663151749
for(i=0; i<p->nOrderBy; i++){
151664151750
sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
151665151751
i,
151666151752
p->aOrderBy[i].iColumn,
@@ -151960,11 +152046,11 @@
151960152046
**
151961152047
** This routine may only be called if it has previously been determined that
151962152048
** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
151963152049
** is set.
151964152050
*/
151965
-static SQLITE_NOINLINE void constructBloomFilter(
152051
+static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
151966152052
WhereInfo *pWInfo, /* The WHERE clause */
151967152053
int iLevel, /* Index in pWInfo->a[] that is pLevel */
151968152054
WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
151969152055
Bitmask notReady /* Loops that are not ready */
151970152056
){
@@ -152044,17 +152130,24 @@
152044152130
sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
152045152131
VdbeCoverage(v);
152046152132
sqlite3VdbeJumpHere(v, addrTop);
152047152133
pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
152048152134
if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
152049
- while( iLevel < pWInfo->nLevel ){
152050
- iLevel++;
152135
+ while( ++iLevel < pWInfo->nLevel ){
152051152136
pLevel = &pWInfo->a[iLevel];
152052152137
pLoop = pLevel->pWLoop;
152053
- if( pLoop==0 ) continue;
152138
+ if( NEVER(pLoop==0) ) continue;
152054152139
if( pLoop->prereq & notReady ) continue;
152055
- if( pLoop->wsFlags & WHERE_BLOOMFILTER ) break;
152140
+ if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
152141
+ ==WHERE_BLOOMFILTER
152142
+ ){
152143
+ /* This is a candidate for bloom-filter pull-down (early evaluation).
152144
+ ** The test that WHERE_COLUMN_IN is omitted is important, as we are
152145
+ ** not able to do early evaluation of bloom filters that make use of
152146
+ ** the IN operator */
152147
+ break;
152148
+ }
152056152149
}
152057152150
}while( iLevel < pWInfo->nLevel );
152058152151
sqlite3VdbeJumpHere(v, addrOnce);
152059152152
}
152060152153
@@ -152081,14 +152174,23 @@
152081152174
struct HiddenIndexInfo *pHidden;
152082152175
WhereTerm *pTerm;
152083152176
int nOrderBy;
152084152177
sqlite3_index_info *pIdxInfo;
152085152178
u16 mNoOmit = 0;
152179
+ const Table *pTab;
152086152180
152087
- /* Count the number of possible WHERE clause constraints referring
152088
- ** to this virtual table */
152181
+ assert( pSrc!=0 );
152182
+ pTab = pSrc->pTab;
152183
+ assert( pTab!=0 );
152184
+ assert( IsVirtual(pTab) );
152185
+
152186
+ /* Find all WHERE clause constraints referring to this virtual table.
152187
+ ** Mark each term with the TERM_OK flag. Set nTerm to the number of
152188
+ ** terms found.
152189
+ */
152089152190
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
152191
+ pTerm->wtFlags &= ~TERM_OK;
152090152192
if( pTerm->leftCursor != pSrc->iCursor ) continue;
152091152193
if( pTerm->prereqRight & mUnusable ) continue;
152092152194
assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
152093152195
testcase( pTerm->eOperator & WO_IN );
152094152196
testcase( pTerm->eOperator & WO_ISNULL );
@@ -152095,12 +152197,23 @@
152095152197
testcase( pTerm->eOperator & WO_IS );
152096152198
testcase( pTerm->eOperator & WO_ALL );
152097152199
if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
152098152200
if( pTerm->wtFlags & TERM_VNULL ) continue;
152099152201
assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152100
- assert( pTerm->u.x.leftColumn>=(-1) );
152202
+ assert( pTerm->u.x.leftColumn>=XN_ROWID );
152203
+ assert( pTerm->u.x.leftColumn<pTab->nCol );
152204
+
152205
+ /* tag-20191211-002: WHERE-clause constraints are not useful to the
152206
+ ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
152207
+ ** equivalent restriction for ordinary tables. */
152208
+ if( (pSrc->fg.jointype & JT_LEFT)!=0
152209
+ && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
152210
+ ){
152211
+ continue;
152212
+ }
152101152213
nTerm++;
152214
+ pTerm->wtFlags |= TERM_OK;
152102152215
}
152103152216
152104152217
/* If the ORDER BY clause contains only columns in the current
152105152218
** virtual table then allocate space for the aOrderBy part of
152106152219
** the sqlite3_index_info structure.
@@ -152108,12 +152221,45 @@
152108152221
nOrderBy = 0;
152109152222
if( pOrderBy ){
152110152223
int n = pOrderBy->nExpr;
152111152224
for(i=0; i<n; i++){
152112152225
Expr *pExpr = pOrderBy->a[i].pExpr;
152113
- if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
152226
+ Expr *pE2;
152227
+
152228
+ /* Skip over constant terms in the ORDER BY clause */
152229
+ if( sqlite3ExprIsConstant(pExpr) ){
152230
+ continue;
152231
+ }
152232
+
152233
+ /* Virtual tables are unable to deal with NULLS FIRST */
152114152234
if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
152235
+
152236
+ /* First case - a direct column references without a COLLATE operator */
152237
+ if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
152238
+ assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
152239
+ continue;
152240
+ }
152241
+
152242
+ /* 2nd case - a column reference with a COLLATE operator. Only match
152243
+ ** of the COLLATE operator matches the collation of the column. */
152244
+ if( pExpr->op==TK_COLLATE
152245
+ && (pE2 = pExpr->pLeft)->op==TK_COLUMN
152246
+ && pE2->iTable==pSrc->iCursor
152247
+ ){
152248
+ const char *zColl; /* The collating sequence name */
152249
+ assert( !ExprHasProperty(pExpr, EP_IntValue) );
152250
+ assert( pExpr->u.zToken!=0 );
152251
+ assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
152252
+ pExpr->iColumn = pE2->iColumn;
152253
+ if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
152254
+ zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
152255
+ if( zColl==0 ) zColl = sqlite3StrBINARY;
152256
+ if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
152257
+ }
152258
+
152259
+ /* No matches cause a break out of the loop */
152260
+ break;
152115152261
}
152116152262
if( i==n){
152117152263
nOrderBy = n;
152118152264
}
152119152265
}
@@ -152129,38 +152275,18 @@
152129152275
}
152130152276
pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
152131152277
pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
152132152278
pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
152133152279
pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
152134
- pIdxInfo->nOrderBy = nOrderBy;
152135152280
pIdxInfo->aConstraint = pIdxCons;
152136152281
pIdxInfo->aOrderBy = pIdxOrderBy;
152137152282
pIdxInfo->aConstraintUsage = pUsage;
152138152283
pHidden->pWC = pWC;
152139152284
pHidden->pParse = pParse;
152140152285
for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
152141152286
u16 op;
152142
- if( pTerm->leftCursor != pSrc->iCursor ) continue;
152143
- if( pTerm->prereqRight & mUnusable ) continue;
152144
- assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
152145
- testcase( pTerm->eOperator & WO_IN );
152146
- testcase( pTerm->eOperator & WO_IS );
152147
- testcase( pTerm->eOperator & WO_ISNULL );
152148
- testcase( pTerm->eOperator & WO_ALL );
152149
- if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
152150
- if( pTerm->wtFlags & TERM_VNULL ) continue;
152151
-
152152
- /* tag-20191211-002: WHERE-clause constraints are not useful to the
152153
- ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
152154
- ** equivalent restriction for ordinary tables. */
152155
- if( (pSrc->fg.jointype & JT_LEFT)!=0
152156
- && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
152157
- ){
152158
- continue;
152159
- }
152160
- assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152161
- assert( pTerm->u.x.leftColumn>=(-1) );
152287
+ if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
152162152288
pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
152163152289
pIdxCons[j].iTermOffset = i;
152164152290
op = pTerm->eOperator & WO_ALL;
152165152291
if( op==WO_IN ) op = WO_EQ;
152166152292
if( op==WO_AUX ){
@@ -152193,16 +152319,23 @@
152193152319
}
152194152320
}
152195152321
152196152322
j++;
152197152323
}
152324
+ assert( j==nTerm );
152198152325
pIdxInfo->nConstraint = j;
152199
- for(i=0; i<nOrderBy; i++){
152326
+ for(i=j=0; i<nOrderBy; i++){
152200152327
Expr *pExpr = pOrderBy->a[i].pExpr;
152201
- pIdxOrderBy[i].iColumn = pExpr->iColumn;
152202
- pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
152328
+ if( sqlite3ExprIsConstant(pExpr) ) continue;
152329
+ assert( pExpr->op==TK_COLUMN
152330
+ || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
152331
+ && pExpr->iColumn==pExpr->pLeft->iColumn) );
152332
+ pIdxOrderBy[j].iColumn = pExpr->iColumn;
152333
+ pIdxOrderBy[j].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
152334
+ j++;
152203152335
}
152336
+ pIdxInfo->nOrderBy = j;
152204152337
152205152338
*pmNoOmit = mNoOmit;
152206152339
return pIdxInfo;
152207152340
}
152208152341
@@ -154530,15 +154663,23 @@
154530154663
154531154664
return rc;
154532154665
}
154533154666
154534154667
/*
154535
-** If this function is invoked from within an xBestIndex() callback, it
154536
-** returns a pointer to a buffer containing the name of the collation
154537
-** sequence associated with element iCons of the sqlite3_index_info.aConstraint
154538
-** array. Or, if iCons is out of range or there is no active xBestIndex
154539
-** call, return NULL.
154668
+** Return the collating sequence for a constraint passed into xBestIndex.
154669
+**
154670
+** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
154671
+** This routine depends on there being a HiddenIndexInfo structure immediately
154672
+** following the sqlite3_index_info structure.
154673
+**
154674
+** Return a pointer to the collation name:
154675
+**
154676
+** 1. If there is an explicit COLLATE operator on the constaint, return it.
154677
+**
154678
+** 2. Else, if the column has an alternative collation, return that.
154679
+**
154680
+** 3. Otherwise, return "BINARY".
154540154681
*/
154541154682
SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
154542154683
HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
154543154684
const char *zRet = 0;
154544154685
if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
@@ -155972,11 +156113,11 @@
155972156113
assert( pWInfo->nLevel>=2 );
155973156114
assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
155974156115
nSearch = pWInfo->a[0].pWLoop->nOut;
155975156116
for(i=1; i<pWInfo->nLevel; i++){
155976156117
WhereLoop *pLoop = pWInfo->a[i].pWLoop;
155977
- const int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ);
156118
+ const unsigned int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ);
155978156119
if( (pLoop->wsFlags & reqFlags)==reqFlags
155979156120
/* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
155980156121
&& ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
155981156122
){
155982156123
SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
@@ -156590,11 +156731,11 @@
156590156731
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
156591156732
constructAutomaticIndex(pParse, &pWInfo->sWC,
156592156733
&pTabList->a[pLevel->iFrom], notReady, pLevel);
156593156734
#endif
156594156735
}else{
156595
- constructBloomFilter(pWInfo, ii, pLevel, notReady);
156736
+ sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady);
156596156737
}
156597156738
if( db->mallocFailed ) goto whereBeginError;
156598156739
}
156599156740
addrExplain = sqlite3WhereExplainOneScan(
156600156741
pParse, pTabList, pLevel, wctrlFlags
@@ -166471,17 +166612,13 @@
166471166612
*tokenType = TK_ID;
166472166613
return i;
166473166614
}
166474166615
166475166616
/*
166476
-** Run the parser on the given SQL string. The parser structure is
166477
-** passed in. An SQLITE_ status code is returned. If an error occurs
166478
-** then an and attempt is made to write an error message into
166479
-** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
166480
-** error message.
166617
+** Run the parser on the given SQL string.
166481166618
*/
166482
-SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
166619
+SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql){
166483166620
int nErr = 0; /* Number of errors encountered */
166484166621
void *pEngine; /* The LEMON-generated LALR(1) parser */
166485166622
int n = 0; /* Length of the next token token */
166486166623
int tokenType; /* type of the next token */
166487166624
int lastTokenParsed = -1; /* type of the previous token */
@@ -166498,11 +166635,10 @@
166498166635
if( db->nVdbeActive==0 ){
166499166636
AtomicStore(&db->u1.isInterrupted, 0);
166500166637
}
166501166638
pParse->rc = SQLITE_OK;
166502166639
pParse->zTail = zSql;
166503
- assert( pzErrMsg!=0 );
166504166640
#ifdef SQLITE_DEBUG
166505166641
if( db->flags & SQLITE_ParserTrace ){
166506166642
printf("parser: [[[%s]]]\n", zSql);
166507166643
sqlite3ParserTrace(stdout, "parser: ");
166508166644
}else{
@@ -166541,10 +166677,11 @@
166541166677
if( tokenType>=TK_SPACE ){
166542166678
assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
166543166679
#endif /* SQLITE_OMIT_WINDOWFUNC */
166544166680
if( AtomicLoad(&db->u1.isInterrupted) ){
166545166681
pParse->rc = SQLITE_INTERRUPT;
166682
+ pParse->nErr++;
166546166683
break;
166547166684
}
166548166685
if( tokenType==TK_SPACE ){
166549166686
zSql += n;
166550166687
continue;
@@ -166598,45 +166735,30 @@
166598166735
sqlite3ParserFree(pEngine, sqlite3_free);
166599166736
#endif
166600166737
if( db->mallocFailed ){
166601166738
pParse->rc = SQLITE_NOMEM_BKPT;
166602166739
}
166603
- if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
166604
- pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
166605
- }
166606
- assert( pzErrMsg!=0 );
166607
- if( pParse->zErrMsg ){
166608
- *pzErrMsg = pParse->zErrMsg;
166609
- sqlite3_log(pParse->rc, "%s in \"%s\"",
166610
- *pzErrMsg, pParse->zTail);
166611
- pParse->zErrMsg = 0;
166740
+ if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
166741
+ if( pParse->zErrMsg==0 ){
166742
+ pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
166743
+ }
166744
+ sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
166612166745
nErr++;
166613166746
}
166614166747
pParse->zTail = zSql;
166615
- if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
166616
- sqlite3VdbeDelete(pParse->pVdbe);
166617
- pParse->pVdbe = 0;
166618
- }
166619
-#ifndef SQLITE_OMIT_SHARED_CACHE
166620
- if( pParse->nested==0 ){
166621
- sqlite3DbFree(db, pParse->aTableLock);
166622
- pParse->aTableLock = 0;
166623
- pParse->nTableLock = 0;
166624
- }
166625
-#endif
166626166748
#ifndef SQLITE_OMIT_VIRTUALTABLE
166627166749
sqlite3_free(pParse->apVtabLock);
166628166750
#endif
166629166751
166630
- if( !IN_SPECIAL_PARSE ){
166752
+ if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
166631166753
/* If the pParse->declareVtab flag is set, do not delete any table
166632166754
** structure built up in pParse->pNewTable. The calling code (see vtab.c)
166633166755
** will take responsibility for freeing the Table structure.
166634166756
*/
166635166757
sqlite3DeleteTable(db, pParse->pNewTable);
166636166758
}
166637
- if( !IN_RENAME_OBJECT ){
166759
+ if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
166638166760
sqlite3DeleteTrigger(db, pParse->pNewTrigger);
166639166761
}
166640166762
sqlite3DbFree(db, pParse->pVList);
166641166763
db->pParse = pParentParse;
166642166764
assert( nErr==0 || pParse->rc!=SQLITE_OK );
@@ -169763,10 +169885,23 @@
169763169885
}
169764169886
}
169765169887
sqlite3_mutex_leave(db->mutex);
169766169888
return z;
169767169889
}
169890
+
169891
+/*
169892
+** Return the byte offset of the most recent error
169893
+*/
169894
+SQLITE_API int sqlite3_error_offset(sqlite3 *db){
169895
+ int iOffset = -1;
169896
+ if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
169897
+ sqlite3_mutex_enter(db->mutex);
169898
+ iOffset = db->errByteOffset;
169899
+ sqlite3_mutex_leave(db->mutex);
169900
+ }
169901
+ return iOffset;
169902
+}
169768169903
169769169904
#ifndef SQLITE_OMIT_UTF16
169770169905
/*
169771169906
** Return UTF-16 encoded English language explanation of the most recent
169772169907
** error.
@@ -173514,11 +173649,11 @@
173514173649
SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
173515173650
SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
173516173651
SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
173517173652
173518173653
/* fts3_tokenize_vtab.c */
173519
-SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
173654
+SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
173520173655
173521173656
/* fts3_unicode2.c (functions generated by parsing unicode text files) */
173522173657
#ifndef SQLITE_DISABLE_FTS3_UNICODE
173523173658
SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
173524173659
SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
@@ -173546,10 +173681,16 @@
173546173681
/* #include "fts3.h" */
173547173682
#ifndef SQLITE_CORE
173548173683
/* # include "sqlite3ext.h" */
173549173684
SQLITE_EXTENSION_INIT1
173550173685
#endif
173686
+
173687
+typedef struct Fts3HashWrapper Fts3HashWrapper;
173688
+struct Fts3HashWrapper {
173689
+ Fts3Hash hash; /* Hash table */
173690
+ int nRef; /* Number of pointers to this object */
173691
+};
173551173692
173552173693
static int fts3EvalNext(Fts3Cursor *pCsr);
173553173694
static int fts3EvalStart(Fts3Cursor *pCsr);
173554173695
static int fts3TermSegReaderCursor(
173555173696
Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
@@ -174411,11 +174552,11 @@
174411174552
int argc, /* Number of elements in argv array */
174412174553
const char * const *argv, /* xCreate/xConnect argument array */
174413174554
sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
174414174555
char **pzErr /* Write any error message here */
174415174556
){
174416
- Fts3Hash *pHash = (Fts3Hash *)pAux;
174557
+ Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
174417174558
Fts3Table *p = 0; /* Pointer to allocated vtab */
174418174559
int rc = SQLITE_OK; /* Return code */
174419174560
int i; /* Iterator variable */
174420174561
sqlite3_int64 nByte; /* Size of allocation used for *p */
174421174562
int iCol; /* Column index */
@@ -177246,13 +177387,16 @@
177246177387
** This function is registered as the module destructor (called when an
177247177388
** FTS3 enabled database connection is closed). It frees the memory
177248177389
** allocated for the tokenizer hash table.
177249177390
*/
177250177391
static void hashDestroy(void *p){
177251
- Fts3Hash *pHash = (Fts3Hash *)p;
177252
- sqlite3Fts3HashClear(pHash);
177253
- sqlite3_free(pHash);
177392
+ Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
177393
+ pHash->nRef--;
177394
+ if( pHash->nRef<=0 ){
177395
+ sqlite3Fts3HashClear(&pHash->hash);
177396
+ sqlite3_free(pHash);
177397
+ }
177254177398
}
177255177399
177256177400
/*
177257177401
** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
177258177402
** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
@@ -177278,11 +177422,11 @@
177278177422
** SQLite. If fts3 is built as a dynamically loadable extension, this
177279177423
** function is called by the sqlite3_extension_init() entry point.
177280177424
*/
177281177425
SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
177282177426
int rc = SQLITE_OK;
177283
- Fts3Hash *pHash = 0;
177427
+ Fts3HashWrapper *pHash = 0;
177284177428
const sqlite3_tokenizer_module *pSimple = 0;
177285177429
const sqlite3_tokenizer_module *pPorter = 0;
177286177430
#ifndef SQLITE_DISABLE_FTS3_UNICODE
177287177431
const sqlite3_tokenizer_module *pUnicode = 0;
177288177432
#endif
@@ -177306,70 +177450,74 @@
177306177450
177307177451
sqlite3Fts3SimpleTokenizerModule(&pSimple);
177308177452
sqlite3Fts3PorterTokenizerModule(&pPorter);
177309177453
177310177454
/* Allocate and initialize the hash-table used to store tokenizers. */
177311
- pHash = sqlite3_malloc(sizeof(Fts3Hash));
177455
+ pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
177312177456
if( !pHash ){
177313177457
rc = SQLITE_NOMEM;
177314177458
}else{
177315
- sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
177459
+ sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1);
177460
+ pHash->nRef = 0;
177316177461
}
177317177462
177318177463
/* Load the built-in tokenizers into the hash table */
177319177464
if( rc==SQLITE_OK ){
177320
- if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
177321
- || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
177465
+ if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
177466
+ || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
177322177467
177323177468
#ifndef SQLITE_DISABLE_FTS3_UNICODE
177324
- || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
177469
+ || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
177325177470
#endif
177326177471
#ifdef SQLITE_ENABLE_ICU
177327
- || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
177472
+ || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
177328177473
#endif
177329177474
){
177330177475
rc = SQLITE_NOMEM;
177331177476
}
177332177477
}
177333177478
177334177479
#ifdef SQLITE_TEST
177335177480
if( rc==SQLITE_OK ){
177336
- rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
177481
+ rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
177337177482
}
177338177483
#endif
177339177484
177340177485
/* Create the virtual table wrapper around the hash-table and overload
177341177486
** the four scalar functions. If this is successful, register the
177342177487
** module with sqlite.
177343177488
*/
177344177489
if( SQLITE_OK==rc
177345
- && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
177490
+ && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
177346177491
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
177347177492
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
177348177493
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
177349177494
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
177350177495
&& SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
177351177496
){
177497
+ pHash->nRef++;
177352177498
rc = sqlite3_create_module_v2(
177353177499
db, "fts3", &fts3Module, (void *)pHash, hashDestroy
177354177500
);
177355177501
if( rc==SQLITE_OK ){
177502
+ pHash->nRef++;
177356177503
rc = sqlite3_create_module_v2(
177357
- db, "fts4", &fts3Module, (void *)pHash, 0
177504
+ db, "fts4", &fts3Module, (void *)pHash, hashDestroy
177358177505
);
177359177506
}
177360177507
if( rc==SQLITE_OK ){
177361
- rc = sqlite3Fts3InitTok(db, (void *)pHash);
177508
+ pHash->nRef++;
177509
+ rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
177362177510
}
177363177511
return rc;
177364177512
}
177365177513
177366177514
177367177515
/* An error has occurred. Delete the hash table and return the error code. */
177368177516
assert( rc!=SQLITE_OK );
177369177517
if( pHash ){
177370
- sqlite3Fts3HashClear(pHash);
177518
+ sqlite3Fts3HashClear(&pHash->hash);
177371177519
sqlite3_free(pHash);
177372177520
}
177373177521
return rc;
177374177522
}
177375177523
@@ -177712,11 +177860,11 @@
177712177860
){
177713177861
char *p = *ppIter;
177714177862
177715177863
assert( nDoclist>0 );
177716177864
assert( *pbEof==0 );
177717
- assert( p || *piDocid==0 );
177865
+ assert_fts3_nc( p || *piDocid==0 );
177718177866
assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
177719177867
177720177868
if( p==0 ){
177721177869
sqlite3_int64 iDocid = 0;
177722177870
char *pNext = 0;
@@ -183429,11 +183577,11 @@
183429183577
183430183578
/*
183431183579
** Register the fts3tok module with database connection db. Return SQLITE_OK
183432183580
** if successful or an error code if sqlite3_create_module() fails.
183433183581
*/
183434
-SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
183582
+SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
183435183583
static const sqlite3_module fts3tok_module = {
183436183584
0, /* iVersion */
183437183585
fts3tokConnectMethod, /* xCreate */
183438183586
fts3tokConnectMethod, /* xConnect */
183439183587
fts3tokBestIndexMethod, /* xBestIndex */
@@ -183458,11 +183606,13 @@
183458183606
0, /* xRollbackTo */
183459183607
0 /* xShadowName */
183460183608
};
183461183609
int rc; /* Return code */
183462183610
183463
- rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
183611
+ rc = sqlite3_create_module_v2(
183612
+ db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
183613
+ );
183464183614
return rc;
183465183615
}
183466183616
183467183617
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
183468183618
@@ -191846,10 +191996,19 @@
191846191996
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
191847191997
#if !defined(SQLITEINT_H)
191848191998
/* #include "sqlite3ext.h" */
191849191999
#endif
191850192000
SQLITE_EXTENSION_INIT1
192001
+
192002
+/* If compiling this extension separately (why would anybody do that when
192003
+** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG
192004
+** is not defined *before* including <assert.h>, in order to disable asserts().
192005
+*/
192006
+#if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG)
192007
+# define NDEBUG 1
192008
+#endif
192009
+
191851192010
/* #include <assert.h> */
191852192011
/* #include <string.h> */
191853192012
/* #include <stdlib.h> */
191854192013
/* #include <stdarg.h> */
191855192014
@@ -233132,11 +233291,11 @@
233132233291
int nArg, /* Number of args */
233133233292
sqlite3_value **apUnused /* Function arguments */
233134233293
){
233135233294
assert( nArg==0 );
233136233295
UNUSED_PARAM2(nArg, apUnused);
233137
- sqlite3_result_text(pCtx, "fts5: 2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882", -1, SQLITE_TRANSIENT);
233296
+ sqlite3_result_text(pCtx, "fts5: 2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10", -1, SQLITE_TRANSIENT);
233138233297
}
233139233298
233140233299
/*
233141233300
** Return true if zName is the extension on one of the shadow tables used
233142233301
** by this module.
233143233302
--- 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 "2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -4128,17 +4128,18 @@
4128 **
4129 ** The values returned by sqlite3_errcode() and/or
4130 ** sqlite3_extended_errcode() might change with each API call.
4131 ** Except, there are some interfaces that are guaranteed to never
4132 ** change the value of the error code. The error-code preserving
4133 ** interfaces are:
4134 **
4135 ** <ul>
4136 ** <li> sqlite3_errcode()
4137 ** <li> sqlite3_extended_errcode()
4138 ** <li> sqlite3_errmsg()
4139 ** <li> sqlite3_errmsg16()
 
4140 ** </ul>
4141 **
4142 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4143 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4144 ** ^(Memory to hold the error message string is managed internally.
@@ -4148,10 +4149,17 @@
4148 **
4149 ** ^The sqlite3_errstr() interface returns the English-language text
4150 ** that describes the [result code], as UTF-8.
4151 ** ^(Memory to hold the error message string is managed internally
4152 ** and must not be freed by the application)^.
 
 
 
 
 
 
 
4153 **
4154 ** When the serialized [threading mode] is in use, it might be the
4155 ** case that a second error occurs on a separate thread in between
4156 ** the time of the first error and the call to these interfaces.
4157 ** When that happens, the second error will be reported since these
@@ -4168,10 +4176,11 @@
4168 SQLITE_API int sqlite3_errcode(sqlite3 *db);
4169 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4170 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4171 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4172 SQLITE_API const char *sqlite3_errstr(int);
 
4173
4174 /*
4175 ** CAPI3REF: Prepared Statement Object
4176 ** KEYWORDS: {prepared statement} {prepared statements}
4177 **
@@ -9768,18 +9777,37 @@
9768
9769 /*
9770 ** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9771 **
9772 ** This function may only be called from within a call to the [xBestIndex]
9773 ** method of a [virtual table].
 
 
9774 **
9775 ** The first argument must be the sqlite3_index_info object that is the
9776 ** first parameter to the xBestIndex() method. The second argument must be
9777 ** an index into the aConstraint[] array belonging to the sqlite3_index_info
9778 ** structure passed to xBestIndex. This function returns a pointer to a buffer
9779 ** containing the name of the collation sequence for the corresponding
9780 ** constraint.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9781 */
9782 SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9783
9784 /*
9785 ** CAPI3REF: Conflict resolution modes
@@ -16489,10 +16517,11 @@
16489 i64 lastRowid; /* ROWID of most recent insert (see above) */
16490 i64 szMmap; /* Default mmap_size setting */
16491 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16492 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16493 int errCode; /* Most recent error code (SQLITE_*) */
 
16494 int errMask; /* & result codes with this before returning */
16495 int iSysErrno; /* Errno value from last system error */
16496 u32 dbOptFlags; /* Flags to enable/disable optimizations */
16497 u8 enc; /* Text encoding */
16498 u8 autoCommit; /* The auto-commit flag. */
@@ -16725,10 +16754,11 @@
16725 #define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
16726 #define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
16727 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
16728 #define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */
16729 #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
 
16730 #define SQLITE_AllOpts 0xffffffff /* All optimizations */
16731
16732 /*
16733 ** Macros for testing whether or not optimizations are enabled or disabled.
16734 */
@@ -18519,10 +18549,12 @@
18519 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18520 #endif
18521 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18522 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18523 Table *pTriggerTab; /* Table triggers are being coded for */
 
 
18524 union {
18525 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
18526 Returning *pReturning; /* The RETURNING clause */
18527 } u1;
18528 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
@@ -18573,13 +18605,11 @@
18573 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18574 #ifndef SQLITE_OMIT_VIRTUALTABLE
18575 Token sArg; /* Complete text of a module argument */
18576 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18577 #endif
18578 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18579 With *pWith; /* Current WITH clause, or NULL */
18580 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
18581 #ifndef SQLITE_OMIT_ALTERTABLE
18582 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18583 #endif
18584 };
18585
@@ -19366,11 +19396,11 @@
19366 SQLITE_PRIVATE void sqlite3Dequote(char*);
19367 SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
19368 SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
19369 SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
19370 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19371 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
19372 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
19373 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
19374 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
19375 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
19376 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
@@ -19646,10 +19676,11 @@
19646 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
19647 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
19648 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
19649 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19650 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
 
19651 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
19652 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
19653 SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19654 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
19655 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -19932,15 +19963,17 @@
19932 SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19933 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19934 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19935
19936 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
 
19937 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19938 SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8);
19939 SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
19940 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19941 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
 
19942
19943 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
19944 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19945
19946 #ifndef SQLITE_OMIT_SUBQUERY
@@ -22181,11 +22214,11 @@
22181 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*);
22182 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
22183 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
22184 SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
22185 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
22186 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
22187 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
22188
22189 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
22190 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
22191 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
@@ -23319,22 +23352,21 @@
23319 **
23320 ** Where NNN is an arbitrary floating-point number and "days" can be one
23321 ** of several units of time.
23322 */
23323 static const struct {
23324 u8 eType; /* Transformation type code */
23325 u8 nName; /* Length of th name */
23326 char *zName; /* Name of the transformation */
23327 double rLimit; /* Maximum NNN value for this transform */
23328 double rXform; /* Constant used for this transform */
23329 } aXformType[] = {
23330 { 0, 6, "second", 464269060800.0, 1000.0 },
23331 { 0, 6, "minute", 7737817680.0, 60000.0 },
23332 { 0, 4, "hour", 128963628.0, 3600000.0 },
23333 { 0, 3, "day", 5373485.0, 86400000.0 },
23334 { 1, 5, "month", 176546.0, 2592000000.0 },
23335 { 2, 4, "year", 14713.0, 31536000000.0 },
23336 };
23337
23338 /*
23339 ** Process a modifier to a date-time stamp. The modifiers are
23340 ** as follows:
@@ -23567,33 +23599,35 @@
23567 for(i=0; i<ArraySize(aXformType); i++){
23568 if( aXformType[i].nName==n
23569 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
23570 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
23571 ){
23572 switch( aXformType[i].eType ){
23573 case 1: { /* Special processing to add months */
23574 int x;
 
23575 computeYMD_HMS(p);
23576 p->M += (int)r;
23577 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
23578 p->Y += x;
23579 p->M -= x*12;
23580 p->validJD = 0;
23581 r -= (int)r;
23582 break;
23583 }
23584 case 2: { /* Special processing to add years */
23585 int y = (int)r;
 
23586 computeYMD_HMS(p);
23587 p->Y += y;
23588 p->validJD = 0;
23589 r -= (int)r;
23590 break;
23591 }
23592 }
23593 computeJD(p);
23594 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
23595 rc = 0;
23596 break;
23597 }
23598 }
23599 clearYMD_HMS_TZ(p);
@@ -29835,10 +29869,11 @@
29835 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29836 pToken = va_arg(ap, Token*);
29837 assert( bArgList==0 );
29838 if( pToken && pToken->n ){
29839 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
 
29840 }
29841 length = width = 0;
29842 break;
29843 }
29844 case etSRCITEM: {
@@ -29889,18 +29924,42 @@
29889 zExtra = 0;
29890 }
29891 }/* End for loop over the format string */
29892 } /* End of function */
29893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29894 /*
29895 ** Enlarge the memory allocation on a StrAccum object so that it is
29896 ** able to accept at least N more bytes of text.
29897 **
29898 ** Return the number of bytes of text that StrAccum is able to accept
29899 ** after the attempted enlargement. The value returned might be zero.
29900 */
29901 static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29902 char *zNew;
29903 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29904 if( p->accError ){
29905 testcase(p->accError==SQLITE_TOOBIG);
29906 testcase(p->accError==SQLITE_NOMEM);
@@ -32212,20 +32271,25 @@
32212 ** that would be appropriate.
32213 */
32214 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
32215 assert( db!=0 );
32216 db->errCode = err_code;
32217 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
 
 
 
 
32218 }
32219
32220 /*
32221 ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
32222 ** and error message.
32223 */
32224 SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
32225 assert( db!=0 );
32226 db->errCode = SQLITE_OK;
 
32227 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
32228 }
32229
32230 /*
32231 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
@@ -32242,21 +32306,12 @@
32242 /*
32243 ** Set the most recent error code and error string for the sqlite
32244 ** handle "db". The error code is set to "err_code".
32245 **
32246 ** If it is not NULL, string zFormat specifies the format of the
32247 ** error string in the style of the printf functions: The following
32248 ** format characters are allowed:
32249 **
32250 ** %s Insert a string
32251 ** %z A string that should be freed after use
32252 ** %d Insert an integer
32253 ** %T Insert a token
32254 ** %S Insert the first element of a SrcList
32255 **
32256 ** zFormat and any string tokens that follow it are assumed to be
32257 ** encoded in UTF-8.
32258 **
32259 ** To clear the most recent error for sqlite handle "db", sqlite3Error
32260 ** should be called with err_code set to SQLITE_OK and zFormat set
32261 ** to NULL.
32262 */
@@ -32276,17 +32331,10 @@
32276 }
32277 }
32278
32279 /*
32280 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
32281 ** The following formatting characters are allowed:
32282 **
32283 ** %s Insert a string
32284 ** %z A string that should be freed after use
32285 ** %d Insert an integer
32286 ** %T Insert a token
32287 ** %S Insert the first element of a SrcList
32288 **
32289 ** This function should be used to report any error that occurs while
32290 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
32291 ** last thing the sqlite3_prepare() function does is copy the error
32292 ** stored by this function into the database handle using sqlite3Error().
@@ -32295,13 +32343,15 @@
32295 */
32296 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
32297 char *zMsg;
32298 va_list ap;
32299 sqlite3 *db = pParse->db;
 
32300 va_start(ap, zFormat);
32301 zMsg = sqlite3VMPrintf(db, zFormat, ap);
32302 va_end(ap);
 
32303 if( db->suppressErr ){
32304 sqlite3DbFree(db, zMsg);
32305 }else{
32306 pParse->nErr++;
32307 sqlite3DbFree(db, pParse->zErrMsg);
@@ -66875,11 +66925,11 @@
66875
66876 pInfo->nKey = *(i64*)&iKey;
66877 pInfo->nPayload = nPayload;
66878 pInfo->pPayload = pIter;
66879 testcase( nPayload==pPage->maxLocal );
66880 testcase( nPayload==pPage->maxLocal+1 );
66881 if( nPayload<=pPage->maxLocal ){
66882 /* This is the (easy) common case where the entire payload fits
66883 ** on the local page. No overflow is required.
66884 */
66885 pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66912,11 +66962,11 @@
66912 pIter++;
66913 pInfo->nKey = nPayload;
66914 pInfo->nPayload = nPayload;
66915 pInfo->pPayload = pIter;
66916 testcase( nPayload==pPage->maxLocal );
66917 testcase( nPayload==pPage->maxLocal+1 );
66918 if( nPayload<=pPage->maxLocal ){
66919 /* This is the (easy) common case where the entire payload fits
66920 ** on the local page. No overflow is required.
66921 */
66922 pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66975,19 +67025,19 @@
66975 ** past the end of the key value. */
66976 pEnd = &pIter[9];
66977 while( (*pIter++)&0x80 && pIter<pEnd );
66978 }
66979 testcase( nSize==pPage->maxLocal );
66980 testcase( nSize==pPage->maxLocal+1 );
66981 if( nSize<=pPage->maxLocal ){
66982 nSize += (u32)(pIter - pCell);
66983 if( nSize<4 ) nSize = 4;
66984 }else{
66985 int minLocal = pPage->minLocal;
66986 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
66987 testcase( nSize==pPage->maxLocal );
66988 testcase( nSize==pPage->maxLocal+1 );
66989 if( nSize>pPage->maxLocal ){
66990 nSize = minLocal;
66991 }
66992 nSize += 4 + (u16)(pIter - pCell);
66993 }
@@ -69882,11 +69932,11 @@
69882 */
69883 static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
69884 int nPage = get4byte(&pPage1->aData[28]);
69885 testcase( nPage==0 );
69886 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
69887 testcase( pBt->nPage!=nPage );
69888 pBt->nPage = nPage;
69889 }
69890
69891 /*
69892 ** Rollback the transaction in progress.
@@ -70903,11 +70953,11 @@
70903 if( pCur->iPage ){
70904 releasePageNotNull(pCur->pPage);
70905 while( --pCur->iPage ){
70906 releasePageNotNull(pCur->apPage[pCur->iPage]);
70907 }
70908 pCur->pPage = pCur->apPage[0];
70909 goto skip_init;
70910 }
70911 }else if( pCur->pgnoRoot==0 ){
70912 pCur->eState = CURSOR_INVALID;
70913 return SQLITE_EMPTY;
@@ -70950,11 +71000,10 @@
70950 skip_init:
70951 pCur->ix = 0;
70952 pCur->info.nSize = 0;
70953 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
70954
70955 pRoot = pCur->pPage;
70956 if( pRoot->nCell>0 ){
70957 pCur->eState = CURSOR_VALID;
70958 }else if( !pRoot->leaf ){
70959 Pgno subpage;
70960 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
@@ -72458,11 +72507,11 @@
72458 assert( pPage->nFree>=0 );
72459 data = pPage->aData;
72460 ptr = &pPage->aCellIdx[2*idx];
72461 pc = get2byte(ptr);
72462 hdr = pPage->hdrOffset;
72463 testcase( pc==get2byte(&data[hdr+5]) );
72464 testcase( pc+sz==pPage->pBt->usableSize );
72465 if( pc+sz > pPage->pBt->usableSize ){
72466 *pRC = SQLITE_CORRUPT_BKPT;
72467 return;
72468 }
@@ -83085,18 +83134,18 @@
83085 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83086 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83087
83088 /*
83089 ** Deserialize the data blob pointed to by buf as serial type serial_type
83090 ** and store the result in pMem. Return the number of bytes read.
83091 **
83092 ** This function is implemented as two separate routines for performance.
83093 ** The few cases that require local variables are broken out into a separate
83094 ** routine so that in most cases the overhead of moving the stack pointer
83095 ** is avoided.
83096 */
83097 static u32 serialGet(
83098 const unsigned char *buf, /* Buffer to deserialize from */
83099 u32 serial_type, /* Serial type to deserialize */
83100 Mem *pMem /* Memory cell to write value into */
83101 ){
83102 u64 x = FOUR_BYTE_UINT(buf);
@@ -83126,13 +83175,12 @@
83126 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
83127 swapMixedEndianFloat(x);
83128 memcpy(&pMem->u.r, &x, sizeof(x));
83129 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
83130 }
83131 return 8;
83132 }
83133 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
83134 const unsigned char *buf, /* Buffer to deserialize from */
83135 u32 serial_type, /* Serial type to deserialize */
83136 Mem *pMem /* Memory cell to write value into */
83137 ){
83138 switch( serial_type ){
@@ -83139,41 +83187,41 @@
83139 case 10: { /* Internal use only: NULL with virtual table
83140 ** UPDATE no-change flag set */
83141 pMem->flags = MEM_Null|MEM_Zero;
83142 pMem->n = 0;
83143 pMem->u.nZero = 0;
83144 break;
83145 }
83146 case 11: /* Reserved for future use */
83147 case 0: { /* Null */
83148 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
83149 pMem->flags = MEM_Null;
83150 break;
83151 }
83152 case 1: {
83153 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
83154 ** integer. */
83155 pMem->u.i = ONE_BYTE_INT(buf);
83156 pMem->flags = MEM_Int;
83157 testcase( pMem->u.i<0 );
83158 return 1;
83159 }
83160 case 2: { /* 2-byte signed integer */
83161 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
83162 ** twos-complement integer. */
83163 pMem->u.i = TWO_BYTE_INT(buf);
83164 pMem->flags = MEM_Int;
83165 testcase( pMem->u.i<0 );
83166 return 2;
83167 }
83168 case 3: { /* 3-byte signed integer */
83169 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
83170 ** twos-complement integer. */
83171 pMem->u.i = THREE_BYTE_INT(buf);
83172 pMem->flags = MEM_Int;
83173 testcase( pMem->u.i<0 );
83174 return 3;
83175 }
83176 case 4: { /* 4-byte signed integer */
83177 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
83178 ** twos-complement integer. */
83179 pMem->u.i = FOUR_BYTE_INT(buf);
@@ -83181,33 +83229,34 @@
83181 /* Work around a sign-extension bug in the HP compiler for HP/UX */
83182 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
83183 #endif
83184 pMem->flags = MEM_Int;
83185 testcase( pMem->u.i<0 );
83186 return 4;
83187 }
83188 case 5: { /* 6-byte signed integer */
83189 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
83190 ** twos-complement integer. */
83191 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
83192 pMem->flags = MEM_Int;
83193 testcase( pMem->u.i<0 );
83194 return 6;
83195 }
83196 case 6: /* 8-byte signed integer */
83197 case 7: { /* IEEE floating point */
83198 /* These use local variables, so do them in a separate routine
83199 ** to avoid having to move the frame pointer in the common case */
83200 return serialGet(buf,serial_type,pMem);
 
83201 }
83202 case 8: /* Integer 0 */
83203 case 9: { /* Integer 1 */
83204 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
83205 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
83206 pMem->u.i = serial_type-8;
83207 pMem->flags = MEM_Int;
83208 return 0;
83209 }
83210 default: {
83211 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
83212 ** length.
83213 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
@@ -83214,14 +83263,14 @@
83214 ** (N-13)/2 bytes in length. */
83215 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
83216 pMem->z = (char *)buf;
83217 pMem->n = (serial_type-12)/2;
83218 pMem->flags = aFlag[serial_type&1];
83219 return pMem->n;
83220 }
83221 }
83222 return 0;
83223 }
83224 /*
83225 ** This routine is used to allocate sufficient space for an UnpackedRecord
83226 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
83227 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
@@ -83280,11 +83329,12 @@
83280 pMem->enc = pKeyInfo->enc;
83281 pMem->db = pKeyInfo->db;
83282 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
83283 pMem->szMalloc = 0;
83284 pMem->z = 0;
83285 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
 
83286 pMem++;
83287 if( (++u)>=p->nField ) break;
83288 }
83289 if( d>(u32)nKey && u ){
83290 assert( CORRUPT_DB );
@@ -83364,11 +83414,12 @@
83364 break;
83365 }
83366
83367 /* Extract the values to be compared.
83368 */
83369 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
 
83370
83371 /* Do the comparison
83372 */
83373 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
83374 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
@@ -84168,11 +84219,11 @@
84168 }
84169
84170 /* The index entry must begin with a header size */
84171 getVarint32NR((u8*)m.z, szHdr);
84172 testcase( szHdr==3 );
84173 testcase( szHdr==m.n );
84174 testcase( szHdr>0x7fffffff );
84175 assert( m.n>=0 );
84176 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
84177 goto idx_rowid_corruption;
84178 }
@@ -87446,11 +87497,10 @@
87446 int i, mx;
87447 u64 h = 0;
87448
87449 i = pOp->p3;
87450 assert( pOp->p4type==P4_INT32 );
87451 mx = i + pOp->p4.i;
87452 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
87453 const Mem *p = &aMem[i];
87454 if( p->flags & (MEM_Int|MEM_IntReal) ){
87455 h += p->u.i;
87456 }else if( p->flags & MEM_Real ){
@@ -100064,12 +100114,13 @@
100064 hit = 1;
100065 }
100066 }
100067 if( hit || zTab==0 ) continue;
100068 }
100069 if( zDb && pTab->pSchema!=pSchema ){
100070 continue;
 
100071 }
100072 if( zTab ){
100073 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
100074 assert( zTabName!=0 );
100075 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
@@ -100196,10 +100247,11 @@
100196 {
100197 assert( ExprUseYTab(pExpr) );
100198 pExpr->y.pTab = pTab;
100199 if( pParse->bReturning ){
100200 eNewExprOp = TK_REGISTER;
 
100201 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
100202 sqlite3TableColumnToStorage(pTab, iCol) + 1;
100203 }else{
100204 pExpr->iColumn = (i16)iCol;
100205 eNewExprOp = TK_TRIGGER;
@@ -109275,11 +109327,10 @@
109275 sqlite3 *db, /* Database handle */
109276 const char *zSql, /* SQL to parse */
109277 int bTemp /* True if SQL is from temp schema */
109278 ){
109279 int rc;
109280 char *zErr = 0;
109281
109282 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109283
109284 /* Parse the SQL statement passed as the first argument. If no error
109285 ** occurs and the parse does not result in a new table, index or
@@ -109286,14 +109337,11 @@
109286 ** trigger object, the database must be corrupt. */
109287 memset(p, 0, sizeof(Parse));
109288 p->eParseMode = PARSE_MODE_RENAME;
109289 p->db = db;
109290 p->nQueryLoop = 1;
109291 rc = zSql ? sqlite3RunParser(p, zSql, &zErr) : SQLITE_NOMEM;
109292 assert( p->zErrMsg==0 );
109293 assert( rc!=SQLITE_OK || zErr==0 );
109294 p->zErrMsg = zErr;
109295 if( db->mallocFailed ) rc = SQLITE_NOMEM;
109296 if( rc==SQLITE_OK
109297 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
109298 ){
109299 rc = SQLITE_CORRUPT_BKPT;
@@ -113496,11 +113544,10 @@
113496 ** built-in function.
113497 */
113498 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
113499 va_list ap;
113500 char *zSql;
113501 char *zErrMsg = 0;
113502 sqlite3 *db = pParse->db;
113503 u32 savedDbFlags = db->mDbFlags;
113504 char saveBuf[PARSE_TAIL_SZ];
113505
113506 if( pParse->nErr ) return;
@@ -113518,13 +113565,12 @@
113518 }
113519 pParse->nested++;
113520 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
113521 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
113522 db->mDbFlags |= DBFLAG_PreferBuiltin;
113523 sqlite3RunParser(pParse, zSql, &zErrMsg);
113524 db->mDbFlags = savedDbFlags;
113525 sqlite3DbFree(db, zErrMsg);
113526 sqlite3DbFree(db, zSql);
113527 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
113528 pParse->nested--;
113529 }
113530
@@ -121372,87 +121418,95 @@
121372 '0', '1', '2', '3', '4', '5', '6', '7',
121373 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
121374 };
121375
121376 /*
121377 ** Implementation of the QUOTE() function. This function takes a single
121378 ** argument. If the argument is numeric, the return value is the same as
121379 ** the argument. If the argument is NULL, the return value is the string
121380 ** "NULL". Otherwise, the argument is enclosed in single quotes with
121381 ** single-quote escapes.
121382 */
121383 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
121384 assert( argc==1 );
121385 UNUSED_PARAMETER(argc);
121386 switch( sqlite3_value_type(argv[0]) ){
 
 
 
121387 case SQLITE_FLOAT: {
121388 double r1, r2;
121389 char zBuf[50];
121390 r1 = sqlite3_value_double(argv[0]);
121391 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
121392 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
121393 if( r1!=r2 ){
121394 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
121395 }
121396 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
 
 
 
121397 break;
121398 }
121399 case SQLITE_INTEGER: {
121400 sqlite3_result_value(context, argv[0]);
121401 break;
121402 }
121403 case SQLITE_BLOB: {
121404 char *zText = 0;
121405 char const *zBlob = sqlite3_value_blob(argv[0]);
121406 int nBlob = sqlite3_value_bytes(argv[0]);
121407 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
121408 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
121409 if( zText ){
121410 int i;
121411 for(i=0; i<nBlob; i++){
121412 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
121413 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
121414 }
121415 zText[(nBlob*2)+2] = '\'';
121416 zText[(nBlob*2)+3] = '\0';
121417 zText[0] = 'X';
121418 zText[1] = '\'';
121419 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
121420 sqlite3_free(zText);
121421 }
121422 break;
121423 }
121424 case SQLITE_TEXT: {
121425 int i,j;
121426 u64 n;
121427 const unsigned char *zArg = sqlite3_value_text(argv[0]);
121428 char *z;
121429
121430 if( zArg==0 ) return;
121431 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
121432 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
121433 if( z ){
121434 z[0] = '\'';
121435 for(i=0, j=1; zArg[i]; i++){
121436 z[j++] = zArg[i];
121437 if( zArg[i]=='\'' ){
121438 z[j++] = '\'';
121439 }
121440 }
121441 z[j++] = '\'';
121442 z[j] = 0;
121443 sqlite3_result_text(context, z, j, sqlite3_free);
121444 }
121445 break;
121446 }
121447 default: {
121448 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
121449 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
121450 break;
121451 }
121452 }
121453 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121454
121455 /*
121456 ** The unicode() function. Return the integer unicode code-point value
121457 ** for the first character of the input string.
121458 */
@@ -126174,10 +126228,11 @@
126174 ** the UNIQUE constraints have run.
126175 */
126176 if( onError==OE_Replace /* IPK rule is REPLACE */
126177 && onError!=overrideError /* Rules for other constraints are different */
126178 && pTab->pIndex /* There exist other constraints */
 
126179 ){
126180 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
126181 VdbeComment((v, "defer IPK REPLACE until last"));
126182 }
126183
@@ -126582,10 +126637,11 @@
126582
126583 /* If the IPK constraint is a REPLACE, run it last */
126584 if( ipkTop ){
126585 sqlite3VdbeGoto(v, ipkTop);
126586 VdbeComment((v, "Do IPK REPLACE"));
 
126587 sqlite3VdbeJumpHere(v, ipkBottom);
126588 }
126589
126590 /* Recheck all uniqueness constraints after replace triggers have run */
126591 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
@@ -127802,10 +127858,12 @@
127802 sqlite3_int64 (*total_changes64)(sqlite3*);
127803 /* Version 3.37.0 and later */
127804 int (*autovacuum_pages)(sqlite3*,
127805 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
127806 void*, void(*)(void*));
 
 
127807 };
127808
127809 /*
127810 ** This is the function signature used for all extension entry points. It
127811 ** is also defined in the file "loadext.c".
@@ -128113,10 +128171,12 @@
128113 /* Version 3.36.1 and later */
128114 #define sqlite3_changes64 sqlite3_api->changes64
128115 #define sqlite3_total_changes64 sqlite3_api->total_changes64
128116 /* Version 3.37.0 and later */
128117 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
 
 
128118 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
128119
128120 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
128121 /* This case when the file really is being compiled as a loadable
128122 ** extension */
@@ -128602,10 +128662,12 @@
128602 /* Version 3.36.1 and later */
128603 sqlite3_changes64,
128604 sqlite3_total_changes64,
128605 /* Version 3.37.0 and later */
128606 sqlite3_autovacuum_pages,
 
 
128607 };
128608
128609 /* True if x is the directory separator character
128610 */
128611 #if SQLITE_OS_WIN
@@ -132941,10 +133003,14 @@
132941 /*
132942 ** Free all memory allocations in the pParse object
132943 */
132944 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
132945 sqlite3 *db = pParse->db;
 
 
 
 
132946 while( pParse->pCleanup ){
132947 ParseCleanup *pCleanup = pParse->pCleanup;
132948 pParse->pCleanup = pCleanup->pNext;
132949 pCleanup->xCleanup(db, pCleanup->pPtr);
132950 sqlite3DbFreeNN(db, pCleanup);
@@ -133020,11 +133086,10 @@
133020 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133021 Vdbe *pReprepare, /* VM being reprepared */
133022 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133023 const char **pzTail /* OUT: End of parsed string */
133024 ){
133025 char *zErrMsg = 0; /* Error message */
133026 int rc = SQLITE_OK; /* Result code */
133027 int i; /* Loop counter */
133028 Parse sParse; /* Parsing context */
133029
133030 memset(&sParse, 0, PARSE_HDR_SZ);
@@ -133095,18 +133160,18 @@
133095 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
133096 goto end_prepare;
133097 }
133098 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
133099 if( zSqlCopy ){
133100 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
133101 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
133102 sqlite3DbFree(db, zSqlCopy);
133103 }else{
133104 sParse.zTail = &zSql[nBytes];
133105 }
133106 }else{
133107 sqlite3RunParser(&sParse, zSql, &zErrMsg);
133108 }
133109 assert( 0==sParse.nQueryLoop );
133110
133111 if( pzTail ){
133112 *pzTail = sParse.zTail;
@@ -133126,18 +133191,18 @@
133126 if( sParse.pVdbe ){
133127 sqlite3VdbeFinalize(sParse.pVdbe);
133128 }
133129 assert( 0==(*ppStmt) );
133130 rc = sParse.rc;
133131 if( zErrMsg ){
133132 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
133133 sqlite3DbFree(db, zErrMsg);
133134 }else{
133135 sqlite3Error(db, rc);
133136 }
133137 }else{
133138 assert( zErrMsg==0 );
133139 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
133140 rc = SQLITE_OK;
133141 sqlite3ErrorClear(db);
133142 }
133143
@@ -136696,10 +136761,12 @@
136696 Select *p, /* The right-most of SELECTs to be coded */
136697 SelectDest *pDest /* What to do with query results */
136698 ){
136699 int i, j; /* Loop counters */
136700 Select *pPrior; /* Another SELECT immediately to our left */
 
 
136701 Vdbe *v; /* Generate code to this VDBE */
136702 SelectDest destA; /* Destination for coroutine A */
136703 SelectDest destB; /* Destination for coroutine B */
136704 int regAddrA; /* Address register for select-A coroutine */
136705 int regAddrB; /* Address register for select-B coroutine */
@@ -136741,12 +136808,11 @@
136741
136742
136743 /* Patch up the ORDER BY clause
136744 */
136745 op = p->op;
136746 pPrior = p->pPrior;
136747 assert( pPrior->pOrderBy==0 );
136748 pOrderBy = p->pOrderBy;
136749 assert( pOrderBy );
136750 nOrderBy = pOrderBy->nExpr;
136751
136752 /* For operators other than UNION ALL we have to make sure that
@@ -136792,15 +136858,10 @@
136792 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
136793 }else{
136794 pKeyMerge = 0;
136795 }
136796
136797 /* Reattach the ORDER BY clause to the query.
136798 */
136799 p->pOrderBy = pOrderBy;
136800 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
136801
136802 /* Allocate a range of temporary registers and the KeyInfo needed
136803 ** for the logic that removes duplicate result rows when the
136804 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
136805 */
136806 if( op==TK_ALL ){
@@ -136821,16 +136882,33 @@
136821 }
136822 }
136823
136824 /* Separate the left and the right query from one another
136825 */
136826 p->pPrior = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136827 pPrior->pNext = 0;
 
 
 
136828 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
136829 if( pPrior->pPrior==0 ){
136830 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
136831 }
136832
136833 /* Compute the limit registers */
136834 computeLimitRegisters(pParse, p, labelEnd);
136835 if( p->iLimit && op==TK_ALL ){
136836 regLimitA = ++pParse->nMem;
@@ -136977,16 +137055,15 @@
136977 */
136978 sqlite3VdbeResolveLabel(v, labelEnd);
136979
136980 /* Reassembly the compound query so that it will be freed correctly
136981 ** by the calling function */
136982 if( p->pPrior ){
136983 sqlite3SelectDelete(db, p->pPrior);
136984 }
136985 p->pPrior = pPrior;
136986 pPrior->pNext = p;
136987
136988 sqlite3ExprListDelete(db, pPrior->pOrderBy);
136989 pPrior->pOrderBy = 0;
136990
136991 /*** TBD: Insert subroutine calls to close cursors on incomplete
136992 **** subqueries ****/
@@ -142081,11 +142158,16 @@
142081 int reg = pParse->nMem+1;
142082 pParse->nMem += nCol+2;
142083 pReturning->iRetReg = reg;
142084 for(i=0; i<nCol; i++){
142085 Expr *pCol = pNew->a[i].pExpr;
 
 
142086 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
 
 
 
142087 }
142088 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
142089 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
142090 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
142091 }
@@ -145420,11 +145502,10 @@
145420 */
145421 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
145422 VtabCtx *pCtx;
145423 int rc = SQLITE_OK;
145424 Table *pTab;
145425 char *zErr = 0;
145426 Parse sParse;
145427 int initBusy;
145428
145429 #ifdef SQLITE_ENABLE_API_ARMOR
145430 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
@@ -145449,15 +145530,16 @@
145449 ** in case a bug arises. */
145450 assert( db->init.busy==0 );
145451 initBusy = db->init.busy;
145452 db->init.busy = 0;
145453 sParse.nQueryLoop = 1;
145454 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
145455 && sParse.pNewTable
145456 && !db->mallocFailed
145457 && IsOrdinaryTable(sParse.pNewTable)
145458 ){
 
145459 if( !pTab->aCol ){
145460 Table *pNew = sParse.pNewTable;
145461 Index *pIdx;
145462 pTab->aCol = pNew->aCol;
145463 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
@@ -145483,12 +145565,13 @@
145483 pIdx->pTable = pTab;
145484 }
145485 }
145486 pCtx->bDeclared = 1;
145487 }else{
145488 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
145489 sqlite3DbFree(db, zErr);
 
145490 rc = SQLITE_ERROR;
145491 }
145492 sParse.eParseMode = PARSE_MODE_NORMAL;
145493
145494 if( sParse.pVdbe ){
@@ -146251,11 +146334,11 @@
146251 #define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
146252 #define TERM_CODED 0x0004 /* This term is already coded */
146253 #define TERM_COPIED 0x0008 /* Has a child */
146254 #define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
146255 #define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
146256 #define TERM_OR_OK 0x0040 /* Used during OR-clause processing */
146257 #define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
146258 #define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
146259 #define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
146260 #define TERM_LIKE 0x0400 /* The original LIKE operator */
146261 #define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
@@ -147961,11 +148044,11 @@
147961 ){
147962 while( ++iLevel < pWInfo->nLevel ){
147963 WhereLevel *pLevel = &pWInfo->a[iLevel];
147964 WhereLoop *pLoop = pLevel->pWLoop;
147965 if( pLevel->regFilter==0 ) continue;
147966 /* ,--- Because constructBloomFilter() has will not have set
147967 ** vvvvv--' pLevel->regFilter if this were true. */
147968 if( NEVER(pLoop->prereq & notReady) ) continue;
147969 if( pLoop->wsFlags & WHERE_IPK ){
147970 WhereTerm *pTerm = pLoop->aLTerm[0];
147971 int regRowid;
@@ -147981,10 +148064,11 @@
147981 u16 nEq = pLoop->u.btree.nEq;
147982 int r1;
147983 char *zStartAff;
147984
147985 assert( pLoop->wsFlags & WHERE_INDEXED );
 
147986 r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
147987 codeApplyAffinity(pParse, r1, nEq, zStartAff);
147988 sqlite3DbFree(pParse->db, zStartAff);
147989 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
147990 addrNxt, r1, nEq);
@@ -150043,11 +150127,11 @@
150043 for(j=0; j<2 && !okToChngToIN; j++){
150044 Expr *pLeft = 0;
150045 pOrTerm = pOrWc->a;
150046 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
150047 assert( pOrTerm->eOperator & WO_EQ );
150048 pOrTerm->wtFlags &= ~TERM_OR_OK;
150049 if( pOrTerm->leftCursor==iCursor ){
150050 /* This is the 2-bit case and we are on the second iteration and
150051 ** current term is from the first iteration. So skip this term. */
150052 assert( j==1 );
150053 continue;
@@ -150084,11 +150168,11 @@
150084 okToChngToIN = 1;
150085 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
150086 assert( pOrTerm->eOperator & WO_EQ );
150087 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150088 if( pOrTerm->leftCursor!=iCursor ){
150089 pOrTerm->wtFlags &= ~TERM_OR_OK;
150090 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
150091 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
150092 )){
150093 okToChngToIN = 0;
150094 }else{
@@ -150100,11 +150184,11 @@
150100 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
150101 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
150102 if( affRight!=0 && affRight!=affLeft ){
150103 okToChngToIN = 0;
150104 }else{
150105 pOrTerm->wtFlags |= TERM_OR_OK;
150106 }
150107 }
150108 }
150109 }
150110
@@ -150117,11 +150201,11 @@
150117 ExprList *pList = 0; /* The RHS of the IN operator */
150118 Expr *pLeft = 0; /* The LHS of the IN operator */
150119 Expr *pNew; /* The complete IN operator */
150120
150121 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
150122 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
150123 assert( pOrTerm->eOperator & WO_EQ );
150124 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150125 assert( pOrTerm->leftCursor==iCursor );
150126 assert( pOrTerm->u.x.leftColumn==iColumn );
150127 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
@@ -151651,16 +151735,18 @@
151651 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
151652 static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
151653 int i;
151654 if( !sqlite3WhereTrace ) return;
151655 for(i=0; i<p->nConstraint; i++){
151656 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
 
151657 i,
151658 p->aConstraint[i].iColumn,
151659 p->aConstraint[i].iTermOffset,
151660 p->aConstraint[i].op,
151661 p->aConstraint[i].usable);
 
151662 }
151663 for(i=0; i<p->nOrderBy; i++){
151664 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
151665 i,
151666 p->aOrderBy[i].iColumn,
@@ -151960,11 +152046,11 @@
151960 **
151961 ** This routine may only be called if it has previously been determined that
151962 ** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
151963 ** is set.
151964 */
151965 static SQLITE_NOINLINE void constructBloomFilter(
151966 WhereInfo *pWInfo, /* The WHERE clause */
151967 int iLevel, /* Index in pWInfo->a[] that is pLevel */
151968 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
151969 Bitmask notReady /* Loops that are not ready */
151970 ){
@@ -152044,17 +152130,24 @@
152044 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
152045 VdbeCoverage(v);
152046 sqlite3VdbeJumpHere(v, addrTop);
152047 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
152048 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
152049 while( iLevel < pWInfo->nLevel ){
152050 iLevel++;
152051 pLevel = &pWInfo->a[iLevel];
152052 pLoop = pLevel->pWLoop;
152053 if( pLoop==0 ) continue;
152054 if( pLoop->prereq & notReady ) continue;
152055 if( pLoop->wsFlags & WHERE_BLOOMFILTER ) break;
 
 
 
 
 
 
 
 
152056 }
152057 }while( iLevel < pWInfo->nLevel );
152058 sqlite3VdbeJumpHere(v, addrOnce);
152059 }
152060
@@ -152081,14 +152174,23 @@
152081 struct HiddenIndexInfo *pHidden;
152082 WhereTerm *pTerm;
152083 int nOrderBy;
152084 sqlite3_index_info *pIdxInfo;
152085 u16 mNoOmit = 0;
 
152086
152087 /* Count the number of possible WHERE clause constraints referring
152088 ** to this virtual table */
 
 
 
 
 
 
 
152089 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
 
152090 if( pTerm->leftCursor != pSrc->iCursor ) continue;
152091 if( pTerm->prereqRight & mUnusable ) continue;
152092 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
152093 testcase( pTerm->eOperator & WO_IN );
152094 testcase( pTerm->eOperator & WO_ISNULL );
@@ -152095,12 +152197,23 @@
152095 testcase( pTerm->eOperator & WO_IS );
152096 testcase( pTerm->eOperator & WO_ALL );
152097 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
152098 if( pTerm->wtFlags & TERM_VNULL ) continue;
152099 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152100 assert( pTerm->u.x.leftColumn>=(-1) );
 
 
 
 
 
 
 
 
 
 
152101 nTerm++;
 
152102 }
152103
152104 /* If the ORDER BY clause contains only columns in the current
152105 ** virtual table then allocate space for the aOrderBy part of
152106 ** the sqlite3_index_info structure.
@@ -152108,12 +152221,45 @@
152108 nOrderBy = 0;
152109 if( pOrderBy ){
152110 int n = pOrderBy->nExpr;
152111 for(i=0; i<n; i++){
152112 Expr *pExpr = pOrderBy->a[i].pExpr;
152113 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
 
 
 
 
 
 
 
152114 if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152115 }
152116 if( i==n){
152117 nOrderBy = n;
152118 }
152119 }
@@ -152129,38 +152275,18 @@
152129 }
152130 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
152131 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
152132 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
152133 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
152134 pIdxInfo->nOrderBy = nOrderBy;
152135 pIdxInfo->aConstraint = pIdxCons;
152136 pIdxInfo->aOrderBy = pIdxOrderBy;
152137 pIdxInfo->aConstraintUsage = pUsage;
152138 pHidden->pWC = pWC;
152139 pHidden->pParse = pParse;
152140 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
152141 u16 op;
152142 if( pTerm->leftCursor != pSrc->iCursor ) continue;
152143 if( pTerm->prereqRight & mUnusable ) continue;
152144 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
152145 testcase( pTerm->eOperator & WO_IN );
152146 testcase( pTerm->eOperator & WO_IS );
152147 testcase( pTerm->eOperator & WO_ISNULL );
152148 testcase( pTerm->eOperator & WO_ALL );
152149 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
152150 if( pTerm->wtFlags & TERM_VNULL ) continue;
152151
152152 /* tag-20191211-002: WHERE-clause constraints are not useful to the
152153 ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
152154 ** equivalent restriction for ordinary tables. */
152155 if( (pSrc->fg.jointype & JT_LEFT)!=0
152156 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
152157 ){
152158 continue;
152159 }
152160 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152161 assert( pTerm->u.x.leftColumn>=(-1) );
152162 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
152163 pIdxCons[j].iTermOffset = i;
152164 op = pTerm->eOperator & WO_ALL;
152165 if( op==WO_IN ) op = WO_EQ;
152166 if( op==WO_AUX ){
@@ -152193,16 +152319,23 @@
152193 }
152194 }
152195
152196 j++;
152197 }
 
152198 pIdxInfo->nConstraint = j;
152199 for(i=0; i<nOrderBy; i++){
152200 Expr *pExpr = pOrderBy->a[i].pExpr;
152201 pIdxOrderBy[i].iColumn = pExpr->iColumn;
152202 pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
 
 
 
 
 
152203 }
 
152204
152205 *pmNoOmit = mNoOmit;
152206 return pIdxInfo;
152207 }
152208
@@ -154530,15 +154663,23 @@
154530
154531 return rc;
154532 }
154533
154534 /*
154535 ** If this function is invoked from within an xBestIndex() callback, it
154536 ** returns a pointer to a buffer containing the name of the collation
154537 ** sequence associated with element iCons of the sqlite3_index_info.aConstraint
154538 ** array. Or, if iCons is out of range or there is no active xBestIndex
154539 ** call, return NULL.
 
 
 
 
 
 
 
 
154540 */
154541 SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
154542 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
154543 const char *zRet = 0;
154544 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
@@ -155972,11 +156113,11 @@
155972 assert( pWInfo->nLevel>=2 );
155973 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
155974 nSearch = pWInfo->a[0].pWLoop->nOut;
155975 for(i=1; i<pWInfo->nLevel; i++){
155976 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
155977 const int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ);
155978 if( (pLoop->wsFlags & reqFlags)==reqFlags
155979 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
155980 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
155981 ){
155982 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
@@ -156590,11 +156731,11 @@
156590 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
156591 constructAutomaticIndex(pParse, &pWInfo->sWC,
156592 &pTabList->a[pLevel->iFrom], notReady, pLevel);
156593 #endif
156594 }else{
156595 constructBloomFilter(pWInfo, ii, pLevel, notReady);
156596 }
156597 if( db->mallocFailed ) goto whereBeginError;
156598 }
156599 addrExplain = sqlite3WhereExplainOneScan(
156600 pParse, pTabList, pLevel, wctrlFlags
@@ -166471,17 +166612,13 @@
166471 *tokenType = TK_ID;
166472 return i;
166473 }
166474
166475 /*
166476 ** Run the parser on the given SQL string. The parser structure is
166477 ** passed in. An SQLITE_ status code is returned. If an error occurs
166478 ** then an and attempt is made to write an error message into
166479 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
166480 ** error message.
166481 */
166482 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
166483 int nErr = 0; /* Number of errors encountered */
166484 void *pEngine; /* The LEMON-generated LALR(1) parser */
166485 int n = 0; /* Length of the next token token */
166486 int tokenType; /* type of the next token */
166487 int lastTokenParsed = -1; /* type of the previous token */
@@ -166498,11 +166635,10 @@
166498 if( db->nVdbeActive==0 ){
166499 AtomicStore(&db->u1.isInterrupted, 0);
166500 }
166501 pParse->rc = SQLITE_OK;
166502 pParse->zTail = zSql;
166503 assert( pzErrMsg!=0 );
166504 #ifdef SQLITE_DEBUG
166505 if( db->flags & SQLITE_ParserTrace ){
166506 printf("parser: [[[%s]]]\n", zSql);
166507 sqlite3ParserTrace(stdout, "parser: ");
166508 }else{
@@ -166541,10 +166677,11 @@
166541 if( tokenType>=TK_SPACE ){
166542 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
166543 #endif /* SQLITE_OMIT_WINDOWFUNC */
166544 if( AtomicLoad(&db->u1.isInterrupted) ){
166545 pParse->rc = SQLITE_INTERRUPT;
 
166546 break;
166547 }
166548 if( tokenType==TK_SPACE ){
166549 zSql += n;
166550 continue;
@@ -166598,45 +166735,30 @@
166598 sqlite3ParserFree(pEngine, sqlite3_free);
166599 #endif
166600 if( db->mallocFailed ){
166601 pParse->rc = SQLITE_NOMEM_BKPT;
166602 }
166603 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
166604 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
166605 }
166606 assert( pzErrMsg!=0 );
166607 if( pParse->zErrMsg ){
166608 *pzErrMsg = pParse->zErrMsg;
166609 sqlite3_log(pParse->rc, "%s in \"%s\"",
166610 *pzErrMsg, pParse->zTail);
166611 pParse->zErrMsg = 0;
166612 nErr++;
166613 }
166614 pParse->zTail = zSql;
166615 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
166616 sqlite3VdbeDelete(pParse->pVdbe);
166617 pParse->pVdbe = 0;
166618 }
166619 #ifndef SQLITE_OMIT_SHARED_CACHE
166620 if( pParse->nested==0 ){
166621 sqlite3DbFree(db, pParse->aTableLock);
166622 pParse->aTableLock = 0;
166623 pParse->nTableLock = 0;
166624 }
166625 #endif
166626 #ifndef SQLITE_OMIT_VIRTUALTABLE
166627 sqlite3_free(pParse->apVtabLock);
166628 #endif
166629
166630 if( !IN_SPECIAL_PARSE ){
166631 /* If the pParse->declareVtab flag is set, do not delete any table
166632 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
166633 ** will take responsibility for freeing the Table structure.
166634 */
166635 sqlite3DeleteTable(db, pParse->pNewTable);
166636 }
166637 if( !IN_RENAME_OBJECT ){
166638 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
166639 }
166640 sqlite3DbFree(db, pParse->pVList);
166641 db->pParse = pParentParse;
166642 assert( nErr==0 || pParse->rc!=SQLITE_OK );
@@ -169763,10 +169885,23 @@
169763 }
169764 }
169765 sqlite3_mutex_leave(db->mutex);
169766 return z;
169767 }
 
 
 
 
 
 
 
 
 
 
 
 
 
169768
169769 #ifndef SQLITE_OMIT_UTF16
169770 /*
169771 ** Return UTF-16 encoded English language explanation of the most recent
169772 ** error.
@@ -173514,11 +173649,11 @@
173514 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
173515 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
173516 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
173517
173518 /* fts3_tokenize_vtab.c */
173519 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
173520
173521 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
173522 #ifndef SQLITE_DISABLE_FTS3_UNICODE
173523 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
173524 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
@@ -173546,10 +173681,16 @@
173546 /* #include "fts3.h" */
173547 #ifndef SQLITE_CORE
173548 /* # include "sqlite3ext.h" */
173549 SQLITE_EXTENSION_INIT1
173550 #endif
 
 
 
 
 
 
173551
173552 static int fts3EvalNext(Fts3Cursor *pCsr);
173553 static int fts3EvalStart(Fts3Cursor *pCsr);
173554 static int fts3TermSegReaderCursor(
173555 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
@@ -174411,11 +174552,11 @@
174411 int argc, /* Number of elements in argv array */
174412 const char * const *argv, /* xCreate/xConnect argument array */
174413 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
174414 char **pzErr /* Write any error message here */
174415 ){
174416 Fts3Hash *pHash = (Fts3Hash *)pAux;
174417 Fts3Table *p = 0; /* Pointer to allocated vtab */
174418 int rc = SQLITE_OK; /* Return code */
174419 int i; /* Iterator variable */
174420 sqlite3_int64 nByte; /* Size of allocation used for *p */
174421 int iCol; /* Column index */
@@ -177246,13 +177387,16 @@
177246 ** This function is registered as the module destructor (called when an
177247 ** FTS3 enabled database connection is closed). It frees the memory
177248 ** allocated for the tokenizer hash table.
177249 */
177250 static void hashDestroy(void *p){
177251 Fts3Hash *pHash = (Fts3Hash *)p;
177252 sqlite3Fts3HashClear(pHash);
177253 sqlite3_free(pHash);
 
 
 
177254 }
177255
177256 /*
177257 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
177258 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
@@ -177278,11 +177422,11 @@
177278 ** SQLite. If fts3 is built as a dynamically loadable extension, this
177279 ** function is called by the sqlite3_extension_init() entry point.
177280 */
177281 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
177282 int rc = SQLITE_OK;
177283 Fts3Hash *pHash = 0;
177284 const sqlite3_tokenizer_module *pSimple = 0;
177285 const sqlite3_tokenizer_module *pPorter = 0;
177286 #ifndef SQLITE_DISABLE_FTS3_UNICODE
177287 const sqlite3_tokenizer_module *pUnicode = 0;
177288 #endif
@@ -177306,70 +177450,74 @@
177306
177307 sqlite3Fts3SimpleTokenizerModule(&pSimple);
177308 sqlite3Fts3PorterTokenizerModule(&pPorter);
177309
177310 /* Allocate and initialize the hash-table used to store tokenizers. */
177311 pHash = sqlite3_malloc(sizeof(Fts3Hash));
177312 if( !pHash ){
177313 rc = SQLITE_NOMEM;
177314 }else{
177315 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
 
177316 }
177317
177318 /* Load the built-in tokenizers into the hash table */
177319 if( rc==SQLITE_OK ){
177320 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
177321 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
177322
177323 #ifndef SQLITE_DISABLE_FTS3_UNICODE
177324 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
177325 #endif
177326 #ifdef SQLITE_ENABLE_ICU
177327 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
177328 #endif
177329 ){
177330 rc = SQLITE_NOMEM;
177331 }
177332 }
177333
177334 #ifdef SQLITE_TEST
177335 if( rc==SQLITE_OK ){
177336 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
177337 }
177338 #endif
177339
177340 /* Create the virtual table wrapper around the hash-table and overload
177341 ** the four scalar functions. If this is successful, register the
177342 ** module with sqlite.
177343 */
177344 if( SQLITE_OK==rc
177345 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
177346 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
177347 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
177348 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
177349 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
177350 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
177351 ){
 
177352 rc = sqlite3_create_module_v2(
177353 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
177354 );
177355 if( rc==SQLITE_OK ){
 
177356 rc = sqlite3_create_module_v2(
177357 db, "fts4", &fts3Module, (void *)pHash, 0
177358 );
177359 }
177360 if( rc==SQLITE_OK ){
177361 rc = sqlite3Fts3InitTok(db, (void *)pHash);
 
177362 }
177363 return rc;
177364 }
177365
177366
177367 /* An error has occurred. Delete the hash table and return the error code. */
177368 assert( rc!=SQLITE_OK );
177369 if( pHash ){
177370 sqlite3Fts3HashClear(pHash);
177371 sqlite3_free(pHash);
177372 }
177373 return rc;
177374 }
177375
@@ -177712,11 +177860,11 @@
177712 ){
177713 char *p = *ppIter;
177714
177715 assert( nDoclist>0 );
177716 assert( *pbEof==0 );
177717 assert( p || *piDocid==0 );
177718 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
177719
177720 if( p==0 ){
177721 sqlite3_int64 iDocid = 0;
177722 char *pNext = 0;
@@ -183429,11 +183577,11 @@
183429
183430 /*
183431 ** Register the fts3tok module with database connection db. Return SQLITE_OK
183432 ** if successful or an error code if sqlite3_create_module() fails.
183433 */
183434 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
183435 static const sqlite3_module fts3tok_module = {
183436 0, /* iVersion */
183437 fts3tokConnectMethod, /* xCreate */
183438 fts3tokConnectMethod, /* xConnect */
183439 fts3tokBestIndexMethod, /* xBestIndex */
@@ -183458,11 +183606,13 @@
183458 0, /* xRollbackTo */
183459 0 /* xShadowName */
183460 };
183461 int rc; /* Return code */
183462
183463 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
 
 
183464 return rc;
183465 }
183466
183467 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
183468
@@ -191846,10 +191996,19 @@
191846 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
191847 #if !defined(SQLITEINT_H)
191848 /* #include "sqlite3ext.h" */
191849 #endif
191850 SQLITE_EXTENSION_INIT1
 
 
 
 
 
 
 
 
 
191851 /* #include <assert.h> */
191852 /* #include <string.h> */
191853 /* #include <stdlib.h> */
191854 /* #include <stdarg.h> */
191855
@@ -233132,11 +233291,11 @@
233132 int nArg, /* Number of args */
233133 sqlite3_value **apUnused /* Function arguments */
233134 ){
233135 assert( nArg==0 );
233136 UNUSED_PARAM2(nArg, apUnused);
233137 sqlite3_result_text(pCtx, "fts5: 2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882", -1, SQLITE_TRANSIENT);
233138 }
233139
233140 /*
233141 ** Return true if zName is the extension on one of the shadow tables used
233142 ** by this module.
233143
--- 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 "2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -4128,17 +4128,18 @@
4128 **
4129 ** The values returned by sqlite3_errcode() and/or
4130 ** sqlite3_extended_errcode() might change with each API call.
4131 ** Except, there are some interfaces that are guaranteed to never
4132 ** change the value of the error code. The error-code preserving
4133 ** interfaces include the following:
4134 **
4135 ** <ul>
4136 ** <li> sqlite3_errcode()
4137 ** <li> sqlite3_extended_errcode()
4138 ** <li> sqlite3_errmsg()
4139 ** <li> sqlite3_errmsg16()
4140 ** <li> sqlite3_error_offset()
4141 ** </ul>
4142 **
4143 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4144 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4145 ** ^(Memory to hold the error message string is managed internally.
@@ -4148,10 +4149,17 @@
4149 **
4150 ** ^The sqlite3_errstr() interface returns the English-language text
4151 ** that describes the [result code], as UTF-8.
4152 ** ^(Memory to hold the error message string is managed internally
4153 ** and must not be freed by the application)^.
4154 **
4155 ** ^If the most recent error references a specific token in the input
4156 ** SQL, the sqlite3_error_offset() interface returns the byte offset
4157 ** of the start of that token. ^The byte offset returned by
4158 ** sqlite3_error_offset() assumes that the input SQL is UTF8.
4159 ** ^If the most error does not reference a specific token in the input
4160 ** SQL, then the sqlite3_error_offset() function returns -1.
4161 **
4162 ** When the serialized [threading mode] is in use, it might be the
4163 ** case that a second error occurs on a separate thread in between
4164 ** the time of the first error and the call to these interfaces.
4165 ** When that happens, the second error will be reported since these
@@ -4168,10 +4176,11 @@
4176 SQLITE_API int sqlite3_errcode(sqlite3 *db);
4177 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4178 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4179 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4180 SQLITE_API const char *sqlite3_errstr(int);
4181 SQLITE_API int sqlite3_error_offset(sqlite3 *db);
4182
4183 /*
4184 ** CAPI3REF: Prepared Statement Object
4185 ** KEYWORDS: {prepared statement} {prepared statements}
4186 **
@@ -9768,18 +9777,37 @@
9777
9778 /*
9779 ** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9780 **
9781 ** This function may only be called from within a call to the [xBestIndex]
9782 ** method of a [virtual table]. This function returns a pointer to a string
9783 ** that is the name of the appropriate collation sequence to use for text
9784 ** comparisons on the constraint identified by its arguments.
9785 **
9786 ** The first argument must be the pointer to the sqlite3_index_info object
9787 ** that is the first parameter to the xBestIndex() method. The second argument
9788 ** must be an index into the aConstraint[] array belonging to the
9789 ** sqlite3_index_info structure passed to xBestIndex.
9790 **
9791 ** Important:
9792 ** The first parameter must be the same pointer that is passed into the
9793 ** xBestMethod() method. The first parameter may not be a pointer to a
9794 ** different sqlite3_index_info object, even an exact copy.
9795 **
9796 ** The return value is computed as follows:
9797 **
9798 ** <ol>
9799 ** <li><p> If the constraint comes from a WHERE clause expression that contains
9800 ** a [COLLATE operator], then the name of the collation specified by
9801 ** that COLLATE operator is returned.
9802 ** <li><p> If there is no COLLATE operator, but the column that is the subject
9803 ** of the constraint specifies an alternative collating sequence via
9804 ** a [COLLATE clause] on the column definition within the CREATE TABLE
9805 ** statement that was passed into [sqlite3_declare_vtab()], then the
9806 ** name of that alternative collating sequence is returned.
9807 ** <li><p> Otherwise, "BINARY" is returned.
9808 ** </ol>
9809 */
9810 SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9811
9812 /*
9813 ** CAPI3REF: Conflict resolution modes
@@ -16489,10 +16517,11 @@
16517 i64 lastRowid; /* ROWID of most recent insert (see above) */
16518 i64 szMmap; /* Default mmap_size setting */
16519 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16520 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16521 int errCode; /* Most recent error code (SQLITE_*) */
16522 int errByteOffset; /* Byte offset of error in SQL statement */
16523 int errMask; /* & result codes with this before returning */
16524 int iSysErrno; /* Errno value from last system error */
16525 u32 dbOptFlags; /* Flags to enable/disable optimizations */
16526 u8 enc; /* Text encoding */
16527 u8 autoCommit; /* The auto-commit flag. */
@@ -16725,10 +16754,11 @@
16754 #define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */
16755 #define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */
16756 /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */
16757 #define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */
16758 #define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */
16759 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
16760 #define SQLITE_AllOpts 0xffffffff /* All optimizations */
16761
16762 /*
16763 ** Macros for testing whether or not optimizations are enabled or disabled.
16764 */
@@ -18519,10 +18549,12 @@
18549 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18550 #endif
18551 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18552 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18553 Table *pTriggerTab; /* Table triggers are being coded for */
18554 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18555 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
18556 union {
18557 int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
18558 Returning *pReturning; /* The RETURNING clause */
18559 } u1;
18560 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
@@ -18573,13 +18605,11 @@
18605 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18606 #ifndef SQLITE_OMIT_VIRTUALTABLE
18607 Token sArg; /* Complete text of a module argument */
18608 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18609 #endif
 
18610 With *pWith; /* Current WITH clause, or NULL */
 
18611 #ifndef SQLITE_OMIT_ALTERTABLE
18612 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18613 #endif
18614 };
18615
@@ -19366,11 +19396,11 @@
19396 SQLITE_PRIVATE void sqlite3Dequote(char*);
19397 SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
19398 SQLITE_PRIVATE void sqlite3DequoteToken(Token*);
19399 SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
19400 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
19401 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*);
19402 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
19403 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
19404 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
19405 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
19406 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
@@ -19646,10 +19676,11 @@
19676 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
19677 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
19678 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
19679 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19680 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19681 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
19682 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
19683 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
19684 SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19685 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
19686 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
@@ -19932,15 +19963,17 @@
19963 SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19964 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19965 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19966
19967 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19968 SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, int);
19969 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19970 SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum*, u8);
19971 SQLITE_PRIVATE void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);
19972 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19973 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19974 SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3*,const char*);
19975
19976 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
19977 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19978
19979 #ifndef SQLITE_OMIT_SUBQUERY
@@ -22181,11 +22214,11 @@
22214 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, u32*);
22215 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
22216 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
22217 SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
22218 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
22219 SQLITE_PRIVATE void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
22220 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
22221
22222 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
22223 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
22224 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
@@ -23319,22 +23352,21 @@
23352 **
23353 ** Where NNN is an arbitrary floating-point number and "days" can be one
23354 ** of several units of time.
23355 */
23356 static const struct {
23357 u8 nName; /* Length of the name */
23358 char zName[7]; /* Name of the transformation */
23359 float rLimit; /* Maximum NNN value for this transform */
23360 float rXform; /* Constant used for this transform */
 
23361 } aXformType[] = {
23362 { 6, "second", 4.6427e+14, 1.0 },
23363 { 6, "minute", 7.7379e+12, 60.0 },
23364 { 4, "hour", 1.2897e+11, 3600.0 },
23365 { 3, "day", 5373485.0, 86400.0 },
23366 { 5, "month", 176546.0, 2592000.0 },
23367 { 4, "year", 14713.0, 31536000.0 },
23368 };
23369
23370 /*
23371 ** Process a modifier to a date-time stamp. The modifiers are
23372 ** as follows:
@@ -23567,33 +23599,35 @@
23599 for(i=0; i<ArraySize(aXformType); i++){
23600 if( aXformType[i].nName==n
23601 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
23602 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
23603 ){
23604 switch( i ){
23605 case 4: { /* Special processing to add months */
23606 int x;
23607 assert( strcmp(aXformType[i].zName,"month")==0 );
23608 computeYMD_HMS(p);
23609 p->M += (int)r;
23610 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
23611 p->Y += x;
23612 p->M -= x*12;
23613 p->validJD = 0;
23614 r -= (int)r;
23615 break;
23616 }
23617 case 5: { /* Special processing to add years */
23618 int y = (int)r;
23619 assert( strcmp(aXformType[i].zName,"year")==0 );
23620 computeYMD_HMS(p);
23621 p->Y += y;
23622 p->validJD = 0;
23623 r -= (int)r;
23624 break;
23625 }
23626 }
23627 computeJD(p);
23628 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
23629 rc = 0;
23630 break;
23631 }
23632 }
23633 clearYMD_HMS_TZ(p);
@@ -29835,10 +29869,11 @@
29869 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29870 pToken = va_arg(ap, Token*);
29871 assert( bArgList==0 );
29872 if( pToken && pToken->n ){
29873 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
29874 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
29875 }
29876 length = width = 0;
29877 break;
29878 }
29879 case etSRCITEM: {
@@ -29889,18 +29924,42 @@
29924 zExtra = 0;
29925 }
29926 }/* End for loop over the format string */
29927 } /* End of function */
29928
29929
29930 /*
29931 ** The z string points to the first character of a token that is
29932 ** associated with an error. If db does not already have an error
29933 ** byte offset recorded, try to compute the error byte offset for
29934 ** z and set the error byte offset in db.
29935 */
29936 SQLITE_PRIVATE void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){
29937 const Parse *pParse;
29938 const char *zText;
29939 const char *zEnd;
29940 assert( z!=0 );
29941 if( NEVER(db==0) ) return;
29942 if( db->errByteOffset!=(-2) ) return;
29943 pParse = db->pParse;
29944 if( NEVER(pParse==0) ) return;
29945 zText =pParse->zTail;
29946 if( NEVER(zText==0) ) return;
29947 zEnd = &zText[strlen(zText)];
29948 if( SQLITE_WITHIN(z,zText,zEnd) ){
29949 db->errByteOffset = (int)(z-zText);
29950 }
29951 }
29952
29953 /*
29954 ** Enlarge the memory allocation on a StrAccum object so that it is
29955 ** able to accept at least N more bytes of text.
29956 **
29957 ** Return the number of bytes of text that StrAccum is able to accept
29958 ** after the attempted enlargement. The value returned might be zero.
29959 */
29960 SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum *p, int N){
29961 char *zNew;
29962 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29963 if( p->accError ){
29964 testcase(p->accError==SQLITE_TOOBIG);
29965 testcase(p->accError==SQLITE_NOMEM);
@@ -32212,20 +32271,25 @@
32271 ** that would be appropriate.
32272 */
32273 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
32274 assert( db!=0 );
32275 db->errCode = err_code;
32276 if( err_code || db->pErr ){
32277 sqlite3ErrorFinish(db, err_code);
32278 }else{
32279 db->errByteOffset = -1;
32280 }
32281 }
32282
32283 /*
32284 ** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state
32285 ** and error message.
32286 */
32287 SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
32288 assert( db!=0 );
32289 db->errCode = SQLITE_OK;
32290 db->errByteOffset = -1;
32291 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
32292 }
32293
32294 /*
32295 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
@@ -32242,21 +32306,12 @@
32306 /*
32307 ** Set the most recent error code and error string for the sqlite
32308 ** handle "db". The error code is set to "err_code".
32309 **
32310 ** If it is not NULL, string zFormat specifies the format of the
32311 ** error string. zFormat and any string tokens that follow it are
32312 ** assumed to be encoded in UTF-8.
 
 
 
 
 
 
 
 
 
32313 **
32314 ** To clear the most recent error for sqlite handle "db", sqlite3Error
32315 ** should be called with err_code set to SQLITE_OK and zFormat set
32316 ** to NULL.
32317 */
@@ -32276,17 +32331,10 @@
32331 }
32332 }
32333
32334 /*
32335 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
 
 
 
 
 
 
 
32336 **
32337 ** This function should be used to report any error that occurs while
32338 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
32339 ** last thing the sqlite3_prepare() function does is copy the error
32340 ** stored by this function into the database handle using sqlite3Error().
@@ -32295,13 +32343,15 @@
32343 */
32344 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
32345 char *zMsg;
32346 va_list ap;
32347 sqlite3 *db = pParse->db;
32348 db->errByteOffset = -2;
32349 va_start(ap, zFormat);
32350 zMsg = sqlite3VMPrintf(db, zFormat, ap);
32351 va_end(ap);
32352 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
32353 if( db->suppressErr ){
32354 sqlite3DbFree(db, zMsg);
32355 }else{
32356 pParse->nErr++;
32357 sqlite3DbFree(db, pParse->zErrMsg);
@@ -66875,11 +66925,11 @@
66925
66926 pInfo->nKey = *(i64*)&iKey;
66927 pInfo->nPayload = nPayload;
66928 pInfo->pPayload = pIter;
66929 testcase( nPayload==pPage->maxLocal );
66930 testcase( nPayload==(u32)pPage->maxLocal+1 );
66931 if( nPayload<=pPage->maxLocal ){
66932 /* This is the (easy) common case where the entire payload fits
66933 ** on the local page. No overflow is required.
66934 */
66935 pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66912,11 +66962,11 @@
66962 pIter++;
66963 pInfo->nKey = nPayload;
66964 pInfo->nPayload = nPayload;
66965 pInfo->pPayload = pIter;
66966 testcase( nPayload==pPage->maxLocal );
66967 testcase( nPayload==(u32)pPage->maxLocal+1 );
66968 if( nPayload<=pPage->maxLocal ){
66969 /* This is the (easy) common case where the entire payload fits
66970 ** on the local page. No overflow is required.
66971 */
66972 pInfo->nSize = nPayload + (u16)(pIter - pCell);
@@ -66975,19 +67025,19 @@
67025 ** past the end of the key value. */
67026 pEnd = &pIter[9];
67027 while( (*pIter++)&0x80 && pIter<pEnd );
67028 }
67029 testcase( nSize==pPage->maxLocal );
67030 testcase( nSize==(u32)pPage->maxLocal+1 );
67031 if( nSize<=pPage->maxLocal ){
67032 nSize += (u32)(pIter - pCell);
67033 if( nSize<4 ) nSize = 4;
67034 }else{
67035 int minLocal = pPage->minLocal;
67036 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
67037 testcase( nSize==pPage->maxLocal );
67038 testcase( nSize==(u32)pPage->maxLocal+1 );
67039 if( nSize>pPage->maxLocal ){
67040 nSize = minLocal;
67041 }
67042 nSize += 4 + (u16)(pIter - pCell);
67043 }
@@ -69882,11 +69932,11 @@
69932 */
69933 static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
69934 int nPage = get4byte(&pPage1->aData[28]);
69935 testcase( nPage==0 );
69936 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
69937 testcase( pBt->nPage!=(u32)nPage );
69938 pBt->nPage = nPage;
69939 }
69940
69941 /*
69942 ** Rollback the transaction in progress.
@@ -70903,11 +70953,11 @@
70953 if( pCur->iPage ){
70954 releasePageNotNull(pCur->pPage);
70955 while( --pCur->iPage ){
70956 releasePageNotNull(pCur->apPage[pCur->iPage]);
70957 }
70958 pRoot = pCur->pPage = pCur->apPage[0];
70959 goto skip_init;
70960 }
70961 }else if( pCur->pgnoRoot==0 ){
70962 pCur->eState = CURSOR_INVALID;
70963 return SQLITE_EMPTY;
@@ -70950,11 +71000,10 @@
71000 skip_init:
71001 pCur->ix = 0;
71002 pCur->info.nSize = 0;
71003 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
71004
 
71005 if( pRoot->nCell>0 ){
71006 pCur->eState = CURSOR_VALID;
71007 }else if( !pRoot->leaf ){
71008 Pgno subpage;
71009 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
@@ -72458,11 +72507,11 @@
72507 assert( pPage->nFree>=0 );
72508 data = pPage->aData;
72509 ptr = &pPage->aCellIdx[2*idx];
72510 pc = get2byte(ptr);
72511 hdr = pPage->hdrOffset;
72512 testcase( pc==(u32)get2byte(&data[hdr+5]) );
72513 testcase( pc+sz==pPage->pBt->usableSize );
72514 if( pc+sz > pPage->pBt->usableSize ){
72515 *pRC = SQLITE_CORRUPT_BKPT;
72516 return;
72517 }
@@ -83085,18 +83134,18 @@
83134 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83135 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
83136
83137 /*
83138 ** Deserialize the data blob pointed to by buf as serial type serial_type
83139 ** and store the result in pMem.
83140 **
83141 ** This function is implemented as two separate routines for performance.
83142 ** The few cases that require local variables are broken out into a separate
83143 ** routine so that in most cases the overhead of moving the stack pointer
83144 ** is avoided.
83145 */
83146 static void serialGet(
83147 const unsigned char *buf, /* Buffer to deserialize from */
83148 u32 serial_type, /* Serial type to deserialize */
83149 Mem *pMem /* Memory cell to write value into */
83150 ){
83151 u64 x = FOUR_BYTE_UINT(buf);
@@ -83126,13 +83175,12 @@
83175 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
83176 swapMixedEndianFloat(x);
83177 memcpy(&pMem->u.r, &x, sizeof(x));
83178 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
83179 }
 
83180 }
83181 SQLITE_PRIVATE void sqlite3VdbeSerialGet(
83182 const unsigned char *buf, /* Buffer to deserialize from */
83183 u32 serial_type, /* Serial type to deserialize */
83184 Mem *pMem /* Memory cell to write value into */
83185 ){
83186 switch( serial_type ){
@@ -83139,41 +83187,41 @@
83187 case 10: { /* Internal use only: NULL with virtual table
83188 ** UPDATE no-change flag set */
83189 pMem->flags = MEM_Null|MEM_Zero;
83190 pMem->n = 0;
83191 pMem->u.nZero = 0;
83192 return;
83193 }
83194 case 11: /* Reserved for future use */
83195 case 0: { /* Null */
83196 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
83197 pMem->flags = MEM_Null;
83198 return;
83199 }
83200 case 1: {
83201 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
83202 ** integer. */
83203 pMem->u.i = ONE_BYTE_INT(buf);
83204 pMem->flags = MEM_Int;
83205 testcase( pMem->u.i<0 );
83206 return;
83207 }
83208 case 2: { /* 2-byte signed integer */
83209 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
83210 ** twos-complement integer. */
83211 pMem->u.i = TWO_BYTE_INT(buf);
83212 pMem->flags = MEM_Int;
83213 testcase( pMem->u.i<0 );
83214 return;
83215 }
83216 case 3: { /* 3-byte signed integer */
83217 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
83218 ** twos-complement integer. */
83219 pMem->u.i = THREE_BYTE_INT(buf);
83220 pMem->flags = MEM_Int;
83221 testcase( pMem->u.i<0 );
83222 return;
83223 }
83224 case 4: { /* 4-byte signed integer */
83225 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
83226 ** twos-complement integer. */
83227 pMem->u.i = FOUR_BYTE_INT(buf);
@@ -83181,33 +83229,34 @@
83229 /* Work around a sign-extension bug in the HP compiler for HP/UX */
83230 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
83231 #endif
83232 pMem->flags = MEM_Int;
83233 testcase( pMem->u.i<0 );
83234 return;
83235 }
83236 case 5: { /* 6-byte signed integer */
83237 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
83238 ** twos-complement integer. */
83239 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
83240 pMem->flags = MEM_Int;
83241 testcase( pMem->u.i<0 );
83242 return;
83243 }
83244 case 6: /* 8-byte signed integer */
83245 case 7: { /* IEEE floating point */
83246 /* These use local variables, so do them in a separate routine
83247 ** to avoid having to move the frame pointer in the common case */
83248 serialGet(buf,serial_type,pMem);
83249 return;
83250 }
83251 case 8: /* Integer 0 */
83252 case 9: { /* Integer 1 */
83253 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
83254 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
83255 pMem->u.i = serial_type-8;
83256 pMem->flags = MEM_Int;
83257 return;
83258 }
83259 default: {
83260 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
83261 ** length.
83262 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
@@ -83214,14 +83263,14 @@
83263 ** (N-13)/2 bytes in length. */
83264 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
83265 pMem->z = (char *)buf;
83266 pMem->n = (serial_type-12)/2;
83267 pMem->flags = aFlag[serial_type&1];
83268 return;
83269 }
83270 }
83271 return;
83272 }
83273 /*
83274 ** This routine is used to allocate sufficient space for an UnpackedRecord
83275 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
83276 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
@@ -83280,11 +83329,12 @@
83329 pMem->enc = pKeyInfo->enc;
83330 pMem->db = pKeyInfo->db;
83331 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
83332 pMem->szMalloc = 0;
83333 pMem->z = 0;
83334 sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
83335 d += sqlite3VdbeSerialTypeLen(serial_type);
83336 pMem++;
83337 if( (++u)>=p->nField ) break;
83338 }
83339 if( d>(u32)nKey && u ){
83340 assert( CORRUPT_DB );
@@ -83364,11 +83414,12 @@
83414 break;
83415 }
83416
83417 /* Extract the values to be compared.
83418 */
83419 sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
83420 d1 += sqlite3VdbeSerialTypeLen(serial_type1);
83421
83422 /* Do the comparison
83423 */
83424 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
83425 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
@@ -84168,11 +84219,11 @@
84219 }
84220
84221 /* The index entry must begin with a header size */
84222 getVarint32NR((u8*)m.z, szHdr);
84223 testcase( szHdr==3 );
84224 testcase( szHdr==(u32)m.n );
84225 testcase( szHdr>0x7fffffff );
84226 assert( m.n>=0 );
84227 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
84228 goto idx_rowid_corruption;
84229 }
@@ -87446,11 +87497,10 @@
87497 int i, mx;
87498 u64 h = 0;
87499
87500 i = pOp->p3;
87501 assert( pOp->p4type==P4_INT32 );
 
87502 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
87503 const Mem *p = &aMem[i];
87504 if( p->flags & (MEM_Int|MEM_IntReal) ){
87505 h += p->u.i;
87506 }else if( p->flags & MEM_Real ){
@@ -100064,12 +100114,13 @@
100114 hit = 1;
100115 }
100116 }
100117 if( hit || zTab==0 ) continue;
100118 }
100119 if( zDb ){
100120 if( pTab->pSchema!=pSchema ) continue;
100121 if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
100122 }
100123 if( zTab ){
100124 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
100125 assert( zTabName!=0 );
100126 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
@@ -100196,10 +100247,11 @@
100247 {
100248 assert( ExprUseYTab(pExpr) );
100249 pExpr->y.pTab = pTab;
100250 if( pParse->bReturning ){
100251 eNewExprOp = TK_REGISTER;
100252 pExpr->op2 = TK_COLUMN;
100253 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
100254 sqlite3TableColumnToStorage(pTab, iCol) + 1;
100255 }else{
100256 pExpr->iColumn = (i16)iCol;
100257 eNewExprOp = TK_TRIGGER;
@@ -109275,11 +109327,10 @@
109327 sqlite3 *db, /* Database handle */
109328 const char *zSql, /* SQL to parse */
109329 int bTemp /* True if SQL is from temp schema */
109330 ){
109331 int rc;
 
109332
109333 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
109334
109335 /* Parse the SQL statement passed as the first argument. If no error
109336 ** occurs and the parse does not result in a new table, index or
@@ -109286,14 +109337,11 @@
109337 ** trigger object, the database must be corrupt. */
109338 memset(p, 0, sizeof(Parse));
109339 p->eParseMode = PARSE_MODE_RENAME;
109340 p->db = db;
109341 p->nQueryLoop = 1;
109342 rc = zSql ? sqlite3RunParser(p, zSql) : SQLITE_NOMEM;
 
 
 
109343 if( db->mallocFailed ) rc = SQLITE_NOMEM;
109344 if( rc==SQLITE_OK
109345 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
109346 ){
109347 rc = SQLITE_CORRUPT_BKPT;
@@ -113496,11 +113544,10 @@
113544 ** built-in function.
113545 */
113546 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
113547 va_list ap;
113548 char *zSql;
 
113549 sqlite3 *db = pParse->db;
113550 u32 savedDbFlags = db->mDbFlags;
113551 char saveBuf[PARSE_TAIL_SZ];
113552
113553 if( pParse->nErr ) return;
@@ -113518,13 +113565,12 @@
113565 }
113566 pParse->nested++;
113567 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
113568 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
113569 db->mDbFlags |= DBFLAG_PreferBuiltin;
113570 sqlite3RunParser(pParse, zSql);
113571 db->mDbFlags = savedDbFlags;
 
113572 sqlite3DbFree(db, zSql);
113573 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
113574 pParse->nested--;
113575 }
113576
@@ -121372,87 +121418,95 @@
121418 '0', '1', '2', '3', '4', '5', '6', '7',
121419 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
121420 };
121421
121422 /*
121423 ** Append to pStr text that is the SQL literal representation of the
121424 ** value contained in pValue.
 
 
 
121425 */
121426 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
121427 /* As currently implemented, the string must be initially empty.
121428 ** we might relax this requirement in the future, but that will
121429 ** require enhancements to the implementation. */
121430 assert( pStr!=0 && pStr->nChar==0 );
121431
121432 switch( sqlite3_value_type(pValue) ){
121433 case SQLITE_FLOAT: {
121434 double r1, r2;
121435 const char *zVal;
121436 r1 = sqlite3_value_double(pValue);
121437 sqlite3_str_appendf(pStr, "%!.15g", r1);
121438 zVal = sqlite3_str_value(pStr);
121439 if( zVal ){
121440 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
121441 if( r1!=r2 ){
121442 sqlite3_str_reset(pStr);
121443 sqlite3_str_appendf(pStr, "%!.20e", r1);
121444 }
121445 }
121446 break;
121447 }
121448 case SQLITE_INTEGER: {
121449 sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue));
121450 break;
121451 }
121452 case SQLITE_BLOB: {
121453 char const *zBlob = sqlite3_value_blob(pValue);
121454 int nBlob = sqlite3_value_bytes(pValue);
121455 assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */
121456 sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4);
121457 if( pStr->accError==0 ){
121458 char *zText = pStr->zText;
121459 int i;
121460 for(i=0; i<nBlob; i++){
121461 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
121462 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
121463 }
121464 zText[(nBlob*2)+2] = '\'';
121465 zText[(nBlob*2)+3] = '\0';
121466 zText[0] = 'X';
121467 zText[1] = '\'';
121468 pStr->nChar = nBlob*2 + 3;
 
121469 }
121470 break;
121471 }
121472 case SQLITE_TEXT: {
121473 const unsigned char *zArg = sqlite3_value_text(pValue);
121474 sqlite3_str_appendf(pStr, "%Q", zArg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121475 break;
121476 }
121477 default: {
121478 assert( sqlite3_value_type(pValue)==SQLITE_NULL );
121479 sqlite3_str_append(pStr, "NULL", 4);
121480 break;
121481 }
121482 }
121483 }
121484
121485 /*
121486 ** Implementation of the QUOTE() function.
121487 **
121488 ** The quote(X) function returns the text of an SQL literal which is the
121489 ** value of its argument suitable for inclusion into an SQL statement.
121490 ** Strings are surrounded by single-quotes with escapes on interior quotes
121491 ** as needed. BLOBs are encoded as hexadecimal literals. Strings with
121492 ** embedded NUL characters cannot be represented as string literals in SQL
121493 ** and hence the returned string literal is truncated prior to the first NUL.
121494 */
121495 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
121496 sqlite3_str str;
121497 sqlite3 *db = sqlite3_context_db_handle(context);
121498 assert( argc==1 );
121499 UNUSED_PARAMETER(argc);
121500 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
121501 sqlite3QuoteValue(&str,argv[0]);
121502 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
121503 SQLITE_DYNAMIC);
121504 if( str.accError==SQLITE_NOMEM ){
121505 sqlite3_result_error_nomem(context);
121506 }
121507 }
121508
121509 /*
121510 ** The unicode() function. Return the integer unicode code-point value
121511 ** for the first character of the input string.
121512 */
@@ -126174,10 +126228,11 @@
126228 ** the UNIQUE constraints have run.
126229 */
126230 if( onError==OE_Replace /* IPK rule is REPLACE */
126231 && onError!=overrideError /* Rules for other constraints are different */
126232 && pTab->pIndex /* There exist other constraints */
126233 && !upsertIpkDelay /* IPK check already deferred by UPSERT */
126234 ){
126235 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
126236 VdbeComment((v, "defer IPK REPLACE until last"));
126237 }
126238
@@ -126582,10 +126637,11 @@
126637
126638 /* If the IPK constraint is a REPLACE, run it last */
126639 if( ipkTop ){
126640 sqlite3VdbeGoto(v, ipkTop);
126641 VdbeComment((v, "Do IPK REPLACE"));
126642 assert( ipkBottom>0 );
126643 sqlite3VdbeJumpHere(v, ipkBottom);
126644 }
126645
126646 /* Recheck all uniqueness constraints after replace triggers have run */
126647 testcase( regTrigCnt!=0 && nReplaceTrig==0 );
@@ -127802,10 +127858,12 @@
127858 sqlite3_int64 (*total_changes64)(sqlite3*);
127859 /* Version 3.37.0 and later */
127860 int (*autovacuum_pages)(sqlite3*,
127861 unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
127862 void*, void(*)(void*));
127863 /* Version 3.38.0 and later */
127864 int (*error_offset)(sqlite3*);
127865 };
127866
127867 /*
127868 ** This is the function signature used for all extension entry points. It
127869 ** is also defined in the file "loadext.c".
@@ -128113,10 +128171,12 @@
128171 /* Version 3.36.1 and later */
128172 #define sqlite3_changes64 sqlite3_api->changes64
128173 #define sqlite3_total_changes64 sqlite3_api->total_changes64
128174 /* Version 3.37.0 and later */
128175 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
128176 /* Version 3.38.0 and later */
128177 #define sqlite3_error_offset sqlite3_api->error_offset
128178 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
128179
128180 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
128181 /* This case when the file really is being compiled as a loadable
128182 ** extension */
@@ -128602,10 +128662,12 @@
128662 /* Version 3.36.1 and later */
128663 sqlite3_changes64,
128664 sqlite3_total_changes64,
128665 /* Version 3.37.0 and later */
128666 sqlite3_autovacuum_pages,
128667 /* Version 3.38.0 and later */
128668 sqlite3_error_offset,
128669 };
128670
128671 /* True if x is the directory separator character
128672 */
128673 #if SQLITE_OS_WIN
@@ -132941,10 +133003,14 @@
133003 /*
133004 ** Free all memory allocations in the pParse object
133005 */
133006 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
133007 sqlite3 *db = pParse->db;
133008 assert( pParse->nested==0 );
133009 #ifndef SQLITE_OMIT_SHARED_CACHE
133010 sqlite3DbFree(db, pParse->aTableLock);
133011 #endif
133012 while( pParse->pCleanup ){
133013 ParseCleanup *pCleanup = pParse->pCleanup;
133014 pParse->pCleanup = pCleanup->pNext;
133015 pCleanup->xCleanup(db, pCleanup->pPtr);
133016 sqlite3DbFreeNN(db, pCleanup);
@@ -133020,11 +133086,10 @@
133086 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
133087 Vdbe *pReprepare, /* VM being reprepared */
133088 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
133089 const char **pzTail /* OUT: End of parsed string */
133090 ){
 
133091 int rc = SQLITE_OK; /* Result code */
133092 int i; /* Loop counter */
133093 Parse sParse; /* Parsing context */
133094
133095 memset(&sParse, 0, PARSE_HDR_SZ);
@@ -133095,18 +133160,18 @@
133160 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
133161 goto end_prepare;
133162 }
133163 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
133164 if( zSqlCopy ){
133165 sqlite3RunParser(&sParse, zSqlCopy);
133166 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
133167 sqlite3DbFree(db, zSqlCopy);
133168 }else{
133169 sParse.zTail = &zSql[nBytes];
133170 }
133171 }else{
133172 sqlite3RunParser(&sParse, zSql);
133173 }
133174 assert( 0==sParse.nQueryLoop );
133175
133176 if( pzTail ){
133177 *pzTail = sParse.zTail;
@@ -133126,18 +133191,18 @@
133191 if( sParse.pVdbe ){
133192 sqlite3VdbeFinalize(sParse.pVdbe);
133193 }
133194 assert( 0==(*ppStmt) );
133195 rc = sParse.rc;
133196 if( sParse.zErrMsg ){
133197 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
133198 sqlite3DbFree(db, sParse.zErrMsg);
133199 }else{
133200 sqlite3Error(db, rc);
133201 }
133202 }else{
133203 assert( sParse.zErrMsg==0 );
133204 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
133205 rc = SQLITE_OK;
133206 sqlite3ErrorClear(db);
133207 }
133208
@@ -136696,10 +136761,12 @@
136761 Select *p, /* The right-most of SELECTs to be coded */
136762 SelectDest *pDest /* What to do with query results */
136763 ){
136764 int i, j; /* Loop counters */
136765 Select *pPrior; /* Another SELECT immediately to our left */
136766 Select *pSplit; /* Left-most SELECT in the right-hand group */
136767 int nSelect; /* Number of SELECT statements in the compound */
136768 Vdbe *v; /* Generate code to this VDBE */
136769 SelectDest destA; /* Destination for coroutine A */
136770 SelectDest destB; /* Destination for coroutine B */
136771 int regAddrA; /* Address register for select-A coroutine */
136772 int regAddrB; /* Address register for select-B coroutine */
@@ -136741,12 +136808,11 @@
136808
136809
136810 /* Patch up the ORDER BY clause
136811 */
136812 op = p->op;
136813 assert( p->pPrior->pOrderBy==0 );
 
136814 pOrderBy = p->pOrderBy;
136815 assert( pOrderBy );
136816 nOrderBy = pOrderBy->nExpr;
136817
136818 /* For operators other than UNION ALL we have to make sure that
@@ -136792,15 +136858,10 @@
136858 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
136859 }else{
136860 pKeyMerge = 0;
136861 }
136862
 
 
 
 
 
136863 /* Allocate a range of temporary registers and the KeyInfo needed
136864 ** for the logic that removes duplicate result rows when the
136865 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
136866 */
136867 if( op==TK_ALL ){
@@ -136821,16 +136882,33 @@
136882 }
136883 }
136884
136885 /* Separate the left and the right query from one another
136886 */
136887 nSelect = 1;
136888 if( (op==TK_ALL || op==TK_UNION)
136889 && OptimizationEnabled(db, SQLITE_BalancedMerge)
136890 ){
136891 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
136892 nSelect++;
136893 assert( pSplit->pPrior->pNext==pSplit );
136894 }
136895 }
136896 if( nSelect<=3 ){
136897 pSplit = p;
136898 }else{
136899 pSplit = p;
136900 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
136901 }
136902 pPrior = pSplit->pPrior;
136903 pSplit->pPrior = 0;
136904 pPrior->pNext = 0;
136905 assert( p->pOrderBy == pOrderBy );
136906 assert( pOrderBy!=0 || db->mallocFailed );
136907 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
136908 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
136909 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
 
 
136910
136911 /* Compute the limit registers */
136912 computeLimitRegisters(pParse, p, labelEnd);
136913 if( p->iLimit && op==TK_ALL ){
136914 regLimitA = ++pParse->nMem;
@@ -136977,16 +137055,15 @@
137055 */
137056 sqlite3VdbeResolveLabel(v, labelEnd);
137057
137058 /* Reassembly the compound query so that it will be freed correctly
137059 ** by the calling function */
137060 if( pSplit->pPrior ){
137061 sqlite3SelectDelete(db, pSplit->pPrior);
137062 }
137063 pSplit->pPrior = pPrior;
137064 pPrior->pNext = pSplit;
 
137065 sqlite3ExprListDelete(db, pPrior->pOrderBy);
137066 pPrior->pOrderBy = 0;
137067
137068 /*** TBD: Insert subroutine calls to close cursors on incomplete
137069 **** subqueries ****/
@@ -142081,11 +142158,16 @@
142158 int reg = pParse->nMem+1;
142159 pParse->nMem += nCol+2;
142160 pReturning->iRetReg = reg;
142161 for(i=0; i<nCol; i++){
142162 Expr *pCol = pNew->a[i].pExpr;
142163 assert( pCol!=0 || pParse->db->mallocFailed );
142164 if( pCol==0 ) continue;
142165 sqlite3ExprCodeFactorable(pParse, pCol, reg+i);
142166 if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){
142167 sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i);
142168 }
142169 }
142170 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i);
142171 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
142172 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
142173 }
@@ -145420,11 +145502,10 @@
145502 */
145503 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
145504 VtabCtx *pCtx;
145505 int rc = SQLITE_OK;
145506 Table *pTab;
 
145507 Parse sParse;
145508 int initBusy;
145509
145510 #ifdef SQLITE_ENABLE_API_ARMOR
145511 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
@@ -145449,15 +145530,16 @@
145530 ** in case a bug arises. */
145531 assert( db->init.busy==0 );
145532 initBusy = db->init.busy;
145533 db->init.busy = 0;
145534 sParse.nQueryLoop = 1;
145535 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable)
145536 && ALWAYS(sParse.pNewTable!=0)
145537 && ALWAYS(!db->mallocFailed)
145538 && IsOrdinaryTable(sParse.pNewTable)
145539 ){
145540 assert( sParse.zErrMsg==0 );
145541 if( !pTab->aCol ){
145542 Table *pNew = sParse.pNewTable;
145543 Index *pIdx;
145544 pTab->aCol = pNew->aCol;
145545 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
@@ -145483,12 +145565,13 @@
145565 pIdx->pTable = pTab;
145566 }
145567 }
145568 pCtx->bDeclared = 1;
145569 }else{
145570 sqlite3ErrorWithMsg(db, SQLITE_ERROR,
145571 (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg);
145572 sqlite3DbFree(db, sParse.zErrMsg);
145573 rc = SQLITE_ERROR;
145574 }
145575 sParse.eParseMode = PARSE_MODE_NORMAL;
145576
145577 if( sParse.pVdbe ){
@@ -146251,11 +146334,11 @@
146334 #define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
146335 #define TERM_CODED 0x0004 /* This term is already coded */
146336 #define TERM_COPIED 0x0008 /* Has a child */
146337 #define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
146338 #define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
146339 #define TERM_OK 0x0040 /* Used during OR-clause processing */
146340 #define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
146341 #define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
146342 #define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
146343 #define TERM_LIKE 0x0400 /* The original LIKE operator */
146344 #define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
@@ -147961,11 +148044,11 @@
148044 ){
148045 while( ++iLevel < pWInfo->nLevel ){
148046 WhereLevel *pLevel = &pWInfo->a[iLevel];
148047 WhereLoop *pLoop = pLevel->pWLoop;
148048 if( pLevel->regFilter==0 ) continue;
148049 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
148050 ** vvvvv--' pLevel->regFilter if this were true. */
148051 if( NEVER(pLoop->prereq & notReady) ) continue;
148052 if( pLoop->wsFlags & WHERE_IPK ){
148053 WhereTerm *pTerm = pLoop->aLTerm[0];
148054 int regRowid;
@@ -147981,10 +148064,11 @@
148064 u16 nEq = pLoop->u.btree.nEq;
148065 int r1;
148066 char *zStartAff;
148067
148068 assert( pLoop->wsFlags & WHERE_INDEXED );
148069 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
148070 r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff);
148071 codeApplyAffinity(pParse, r1, nEq, zStartAff);
148072 sqlite3DbFree(pParse->db, zStartAff);
148073 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
148074 addrNxt, r1, nEq);
@@ -150043,11 +150127,11 @@
150127 for(j=0; j<2 && !okToChngToIN; j++){
150128 Expr *pLeft = 0;
150129 pOrTerm = pOrWc->a;
150130 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
150131 assert( pOrTerm->eOperator & WO_EQ );
150132 pOrTerm->wtFlags &= ~TERM_OK;
150133 if( pOrTerm->leftCursor==iCursor ){
150134 /* This is the 2-bit case and we are on the second iteration and
150135 ** current term is from the first iteration. So skip this term. */
150136 assert( j==1 );
150137 continue;
@@ -150084,11 +150168,11 @@
150168 okToChngToIN = 1;
150169 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
150170 assert( pOrTerm->eOperator & WO_EQ );
150171 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150172 if( pOrTerm->leftCursor!=iCursor ){
150173 pOrTerm->wtFlags &= ~TERM_OK;
150174 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
150175 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
150176 )){
150177 okToChngToIN = 0;
150178 }else{
@@ -150100,11 +150184,11 @@
150184 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
150185 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
150186 if( affRight!=0 && affRight!=affLeft ){
150187 okToChngToIN = 0;
150188 }else{
150189 pOrTerm->wtFlags |= TERM_OK;
150190 }
150191 }
150192 }
150193 }
150194
@@ -150117,11 +150201,11 @@
150201 ExprList *pList = 0; /* The RHS of the IN operator */
150202 Expr *pLeft = 0; /* The LHS of the IN operator */
150203 Expr *pNew; /* The complete IN operator */
150204
150205 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
150206 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
150207 assert( pOrTerm->eOperator & WO_EQ );
150208 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
150209 assert( pOrTerm->leftCursor==iCursor );
150210 assert( pOrTerm->u.x.leftColumn==iColumn );
150211 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
@@ -151651,16 +151735,18 @@
151735 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
151736 static void whereTraceIndexInfoInputs(sqlite3_index_info *p){
151737 int i;
151738 if( !sqlite3WhereTrace ) return;
151739 for(i=0; i<p->nConstraint; i++){
151740 sqlite3DebugPrintf(
151741 " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n",
151742 i,
151743 p->aConstraint[i].iColumn,
151744 p->aConstraint[i].iTermOffset,
151745 p->aConstraint[i].op,
151746 p->aConstraint[i].usable,
151747 sqlite3_vtab_collation(p,i));
151748 }
151749 for(i=0; i<p->nOrderBy; i++){
151750 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
151751 i,
151752 p->aOrderBy[i].iColumn,
@@ -151960,11 +152046,11 @@
152046 **
152047 ** This routine may only be called if it has previously been determined that
152048 ** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
152049 ** is set.
152050 */
152051 static SQLITE_NOINLINE void sqlite3ConstructBloomFilter(
152052 WhereInfo *pWInfo, /* The WHERE clause */
152053 int iLevel, /* Index in pWInfo->a[] that is pLevel */
152054 WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */
152055 Bitmask notReady /* Loops that are not ready */
152056 ){
@@ -152044,17 +152130,24 @@
152130 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
152131 VdbeCoverage(v);
152132 sqlite3VdbeJumpHere(v, addrTop);
152133 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
152134 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
152135 while( ++iLevel < pWInfo->nLevel ){
 
152136 pLevel = &pWInfo->a[iLevel];
152137 pLoop = pLevel->pWLoop;
152138 if( NEVER(pLoop==0) ) continue;
152139 if( pLoop->prereq & notReady ) continue;
152140 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
152141 ==WHERE_BLOOMFILTER
152142 ){
152143 /* This is a candidate for bloom-filter pull-down (early evaluation).
152144 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
152145 ** not able to do early evaluation of bloom filters that make use of
152146 ** the IN operator */
152147 break;
152148 }
152149 }
152150 }while( iLevel < pWInfo->nLevel );
152151 sqlite3VdbeJumpHere(v, addrOnce);
152152 }
152153
@@ -152081,14 +152174,23 @@
152174 struct HiddenIndexInfo *pHidden;
152175 WhereTerm *pTerm;
152176 int nOrderBy;
152177 sqlite3_index_info *pIdxInfo;
152178 u16 mNoOmit = 0;
152179 const Table *pTab;
152180
152181 assert( pSrc!=0 );
152182 pTab = pSrc->pTab;
152183 assert( pTab!=0 );
152184 assert( IsVirtual(pTab) );
152185
152186 /* Find all WHERE clause constraints referring to this virtual table.
152187 ** Mark each term with the TERM_OK flag. Set nTerm to the number of
152188 ** terms found.
152189 */
152190 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
152191 pTerm->wtFlags &= ~TERM_OK;
152192 if( pTerm->leftCursor != pSrc->iCursor ) continue;
152193 if( pTerm->prereqRight & mUnusable ) continue;
152194 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
152195 testcase( pTerm->eOperator & WO_IN );
152196 testcase( pTerm->eOperator & WO_ISNULL );
@@ -152095,12 +152197,23 @@
152197 testcase( pTerm->eOperator & WO_IS );
152198 testcase( pTerm->eOperator & WO_ALL );
152199 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
152200 if( pTerm->wtFlags & TERM_VNULL ) continue;
152201 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
152202 assert( pTerm->u.x.leftColumn>=XN_ROWID );
152203 assert( pTerm->u.x.leftColumn<pTab->nCol );
152204
152205 /* tag-20191211-002: WHERE-clause constraints are not useful to the
152206 ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
152207 ** equivalent restriction for ordinary tables. */
152208 if( (pSrc->fg.jointype & JT_LEFT)!=0
152209 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
152210 ){
152211 continue;
152212 }
152213 nTerm++;
152214 pTerm->wtFlags |= TERM_OK;
152215 }
152216
152217 /* If the ORDER BY clause contains only columns in the current
152218 ** virtual table then allocate space for the aOrderBy part of
152219 ** the sqlite3_index_info structure.
@@ -152108,12 +152221,45 @@
152221 nOrderBy = 0;
152222 if( pOrderBy ){
152223 int n = pOrderBy->nExpr;
152224 for(i=0; i<n; i++){
152225 Expr *pExpr = pOrderBy->a[i].pExpr;
152226 Expr *pE2;
152227
152228 /* Skip over constant terms in the ORDER BY clause */
152229 if( sqlite3ExprIsConstant(pExpr) ){
152230 continue;
152231 }
152232
152233 /* Virtual tables are unable to deal with NULLS FIRST */
152234 if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
152235
152236 /* First case - a direct column references without a COLLATE operator */
152237 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
152238 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
152239 continue;
152240 }
152241
152242 /* 2nd case - a column reference with a COLLATE operator. Only match
152243 ** of the COLLATE operator matches the collation of the column. */
152244 if( pExpr->op==TK_COLLATE
152245 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
152246 && pE2->iTable==pSrc->iCursor
152247 ){
152248 const char *zColl; /* The collating sequence name */
152249 assert( !ExprHasProperty(pExpr, EP_IntValue) );
152250 assert( pExpr->u.zToken!=0 );
152251 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
152252 pExpr->iColumn = pE2->iColumn;
152253 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
152254 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
152255 if( zColl==0 ) zColl = sqlite3StrBINARY;
152256 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
152257 }
152258
152259 /* No matches cause a break out of the loop */
152260 break;
152261 }
152262 if( i==n){
152263 nOrderBy = n;
152264 }
152265 }
@@ -152129,38 +152275,18 @@
152275 }
152276 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
152277 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
152278 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
152279 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
 
152280 pIdxInfo->aConstraint = pIdxCons;
152281 pIdxInfo->aOrderBy = pIdxOrderBy;
152282 pIdxInfo->aConstraintUsage = pUsage;
152283 pHidden->pWC = pWC;
152284 pHidden->pParse = pParse;
152285 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
152286 u16 op;
152287 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152288 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
152289 pIdxCons[j].iTermOffset = i;
152290 op = pTerm->eOperator & WO_ALL;
152291 if( op==WO_IN ) op = WO_EQ;
152292 if( op==WO_AUX ){
@@ -152193,16 +152319,23 @@
152319 }
152320 }
152321
152322 j++;
152323 }
152324 assert( j==nTerm );
152325 pIdxInfo->nConstraint = j;
152326 for(i=j=0; i<nOrderBy; i++){
152327 Expr *pExpr = pOrderBy->a[i].pExpr;
152328 if( sqlite3ExprIsConstant(pExpr) ) continue;
152329 assert( pExpr->op==TK_COLUMN
152330 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
152331 && pExpr->iColumn==pExpr->pLeft->iColumn) );
152332 pIdxOrderBy[j].iColumn = pExpr->iColumn;
152333 pIdxOrderBy[j].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
152334 j++;
152335 }
152336 pIdxInfo->nOrderBy = j;
152337
152338 *pmNoOmit = mNoOmit;
152339 return pIdxInfo;
152340 }
152341
@@ -154530,15 +154663,23 @@
154663
154664 return rc;
154665 }
154666
154667 /*
154668 ** Return the collating sequence for a constraint passed into xBestIndex.
154669 **
154670 ** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex.
154671 ** This routine depends on there being a HiddenIndexInfo structure immediately
154672 ** following the sqlite3_index_info structure.
154673 **
154674 ** Return a pointer to the collation name:
154675 **
154676 ** 1. If there is an explicit COLLATE operator on the constaint, return it.
154677 **
154678 ** 2. Else, if the column has an alternative collation, return that.
154679 **
154680 ** 3. Otherwise, return "BINARY".
154681 */
154682 SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
154683 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
154684 const char *zRet = 0;
154685 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
@@ -155972,11 +156113,11 @@
156113 assert( pWInfo->nLevel>=2 );
156114 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
156115 nSearch = pWInfo->a[0].pWLoop->nOut;
156116 for(i=1; i<pWInfo->nLevel; i++){
156117 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
156118 const unsigned int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ);
156119 if( (pLoop->wsFlags & reqFlags)==reqFlags
156120 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
156121 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
156122 ){
156123 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
@@ -156590,11 +156731,11 @@
156731 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
156732 constructAutomaticIndex(pParse, &pWInfo->sWC,
156733 &pTabList->a[pLevel->iFrom], notReady, pLevel);
156734 #endif
156735 }else{
156736 sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady);
156737 }
156738 if( db->mallocFailed ) goto whereBeginError;
156739 }
156740 addrExplain = sqlite3WhereExplainOneScan(
156741 pParse, pTabList, pLevel, wctrlFlags
@@ -166471,17 +166612,13 @@
166612 *tokenType = TK_ID;
166613 return i;
166614 }
166615
166616 /*
166617 ** Run the parser on the given SQL string.
 
 
 
 
166618 */
166619 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql){
166620 int nErr = 0; /* Number of errors encountered */
166621 void *pEngine; /* The LEMON-generated LALR(1) parser */
166622 int n = 0; /* Length of the next token token */
166623 int tokenType; /* type of the next token */
166624 int lastTokenParsed = -1; /* type of the previous token */
@@ -166498,11 +166635,10 @@
166635 if( db->nVdbeActive==0 ){
166636 AtomicStore(&db->u1.isInterrupted, 0);
166637 }
166638 pParse->rc = SQLITE_OK;
166639 pParse->zTail = zSql;
 
166640 #ifdef SQLITE_DEBUG
166641 if( db->flags & SQLITE_ParserTrace ){
166642 printf("parser: [[[%s]]]\n", zSql);
166643 sqlite3ParserTrace(stdout, "parser: ");
166644 }else{
@@ -166541,10 +166677,11 @@
166677 if( tokenType>=TK_SPACE ){
166678 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
166679 #endif /* SQLITE_OMIT_WINDOWFUNC */
166680 if( AtomicLoad(&db->u1.isInterrupted) ){
166681 pParse->rc = SQLITE_INTERRUPT;
166682 pParse->nErr++;
166683 break;
166684 }
166685 if( tokenType==TK_SPACE ){
166686 zSql += n;
166687 continue;
@@ -166598,45 +166735,30 @@
166735 sqlite3ParserFree(pEngine, sqlite3_free);
166736 #endif
166737 if( db->mallocFailed ){
166738 pParse->rc = SQLITE_NOMEM_BKPT;
166739 }
166740 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
166741 if( pParse->zErrMsg==0 ){
166742 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
166743 }
166744 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
 
 
 
 
166745 nErr++;
166746 }
166747 pParse->zTail = zSql;
 
 
 
 
 
 
 
 
 
 
 
166748 #ifndef SQLITE_OMIT_VIRTUALTABLE
166749 sqlite3_free(pParse->apVtabLock);
166750 #endif
166751
166752 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
166753 /* If the pParse->declareVtab flag is set, do not delete any table
166754 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
166755 ** will take responsibility for freeing the Table structure.
166756 */
166757 sqlite3DeleteTable(db, pParse->pNewTable);
166758 }
166759 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
166760 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
166761 }
166762 sqlite3DbFree(db, pParse->pVList);
166763 db->pParse = pParentParse;
166764 assert( nErr==0 || pParse->rc!=SQLITE_OK );
@@ -169763,10 +169885,23 @@
169885 }
169886 }
169887 sqlite3_mutex_leave(db->mutex);
169888 return z;
169889 }
169890
169891 /*
169892 ** Return the byte offset of the most recent error
169893 */
169894 SQLITE_API int sqlite3_error_offset(sqlite3 *db){
169895 int iOffset = -1;
169896 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
169897 sqlite3_mutex_enter(db->mutex);
169898 iOffset = db->errByteOffset;
169899 sqlite3_mutex_leave(db->mutex);
169900 }
169901 return iOffset;
169902 }
169903
169904 #ifndef SQLITE_OMIT_UTF16
169905 /*
169906 ** Return UTF-16 encoded English language explanation of the most recent
169907 ** error.
@@ -173514,11 +173649,11 @@
173649 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
173650 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
173651 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
173652
173653 /* fts3_tokenize_vtab.c */
173654 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*));
173655
173656 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
173657 #ifndef SQLITE_DISABLE_FTS3_UNICODE
173658 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
173659 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
@@ -173546,10 +173681,16 @@
173681 /* #include "fts3.h" */
173682 #ifndef SQLITE_CORE
173683 /* # include "sqlite3ext.h" */
173684 SQLITE_EXTENSION_INIT1
173685 #endif
173686
173687 typedef struct Fts3HashWrapper Fts3HashWrapper;
173688 struct Fts3HashWrapper {
173689 Fts3Hash hash; /* Hash table */
173690 int nRef; /* Number of pointers to this object */
173691 };
173692
173693 static int fts3EvalNext(Fts3Cursor *pCsr);
173694 static int fts3EvalStart(Fts3Cursor *pCsr);
173695 static int fts3TermSegReaderCursor(
173696 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
@@ -174411,11 +174552,11 @@
174552 int argc, /* Number of elements in argv array */
174553 const char * const *argv, /* xCreate/xConnect argument array */
174554 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
174555 char **pzErr /* Write any error message here */
174556 ){
174557 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
174558 Fts3Table *p = 0; /* Pointer to allocated vtab */
174559 int rc = SQLITE_OK; /* Return code */
174560 int i; /* Iterator variable */
174561 sqlite3_int64 nByte; /* Size of allocation used for *p */
174562 int iCol; /* Column index */
@@ -177246,13 +177387,16 @@
177387 ** This function is registered as the module destructor (called when an
177388 ** FTS3 enabled database connection is closed). It frees the memory
177389 ** allocated for the tokenizer hash table.
177390 */
177391 static void hashDestroy(void *p){
177392 Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
177393 pHash->nRef--;
177394 if( pHash->nRef<=0 ){
177395 sqlite3Fts3HashClear(&pHash->hash);
177396 sqlite3_free(pHash);
177397 }
177398 }
177399
177400 /*
177401 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
177402 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
@@ -177278,11 +177422,11 @@
177422 ** SQLite. If fts3 is built as a dynamically loadable extension, this
177423 ** function is called by the sqlite3_extension_init() entry point.
177424 */
177425 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
177426 int rc = SQLITE_OK;
177427 Fts3HashWrapper *pHash = 0;
177428 const sqlite3_tokenizer_module *pSimple = 0;
177429 const sqlite3_tokenizer_module *pPorter = 0;
177430 #ifndef SQLITE_DISABLE_FTS3_UNICODE
177431 const sqlite3_tokenizer_module *pUnicode = 0;
177432 #endif
@@ -177306,70 +177450,74 @@
177450
177451 sqlite3Fts3SimpleTokenizerModule(&pSimple);
177452 sqlite3Fts3PorterTokenizerModule(&pPorter);
177453
177454 /* Allocate and initialize the hash-table used to store tokenizers. */
177455 pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
177456 if( !pHash ){
177457 rc = SQLITE_NOMEM;
177458 }else{
177459 sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1);
177460 pHash->nRef = 0;
177461 }
177462
177463 /* Load the built-in tokenizers into the hash table */
177464 if( rc==SQLITE_OK ){
177465 if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
177466 || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
177467
177468 #ifndef SQLITE_DISABLE_FTS3_UNICODE
177469 || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
177470 #endif
177471 #ifdef SQLITE_ENABLE_ICU
177472 || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
177473 #endif
177474 ){
177475 rc = SQLITE_NOMEM;
177476 }
177477 }
177478
177479 #ifdef SQLITE_TEST
177480 if( rc==SQLITE_OK ){
177481 rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
177482 }
177483 #endif
177484
177485 /* Create the virtual table wrapper around the hash-table and overload
177486 ** the four scalar functions. If this is successful, register the
177487 ** module with sqlite.
177488 */
177489 if( SQLITE_OK==rc
177490 && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
177491 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
177492 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
177493 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
177494 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
177495 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
177496 ){
177497 pHash->nRef++;
177498 rc = sqlite3_create_module_v2(
177499 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
177500 );
177501 if( rc==SQLITE_OK ){
177502 pHash->nRef++;
177503 rc = sqlite3_create_module_v2(
177504 db, "fts4", &fts3Module, (void *)pHash, hashDestroy
177505 );
177506 }
177507 if( rc==SQLITE_OK ){
177508 pHash->nRef++;
177509 rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
177510 }
177511 return rc;
177512 }
177513
177514
177515 /* An error has occurred. Delete the hash table and return the error code. */
177516 assert( rc!=SQLITE_OK );
177517 if( pHash ){
177518 sqlite3Fts3HashClear(&pHash->hash);
177519 sqlite3_free(pHash);
177520 }
177521 return rc;
177522 }
177523
@@ -177712,11 +177860,11 @@
177860 ){
177861 char *p = *ppIter;
177862
177863 assert( nDoclist>0 );
177864 assert( *pbEof==0 );
177865 assert_fts3_nc( p || *piDocid==0 );
177866 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
177867
177868 if( p==0 ){
177869 sqlite3_int64 iDocid = 0;
177870 char *pNext = 0;
@@ -183429,11 +183577,11 @@
183577
183578 /*
183579 ** Register the fts3tok module with database connection db. Return SQLITE_OK
183580 ** if successful or an error code if sqlite3_create_module() fails.
183581 */
183582 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){
183583 static const sqlite3_module fts3tok_module = {
183584 0, /* iVersion */
183585 fts3tokConnectMethod, /* xCreate */
183586 fts3tokConnectMethod, /* xConnect */
183587 fts3tokBestIndexMethod, /* xBestIndex */
@@ -183458,11 +183606,13 @@
183606 0, /* xRollbackTo */
183607 0 /* xShadowName */
183608 };
183609 int rc; /* Return code */
183610
183611 rc = sqlite3_create_module_v2(
183612 db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy
183613 );
183614 return rc;
183615 }
183616
183617 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
183618
@@ -191846,10 +191996,19 @@
191996 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
191997 #if !defined(SQLITEINT_H)
191998 /* #include "sqlite3ext.h" */
191999 #endif
192000 SQLITE_EXTENSION_INIT1
192001
192002 /* If compiling this extension separately (why would anybody do that when
192003 ** it is built into the amalgamation?) we must set NDEBUG if SQLITE_DEBUG
192004 ** is not defined *before* including <assert.h>, in order to disable asserts().
192005 */
192006 #if !defined(SQLITE_AMALGAMATION) && !defined(SQLITE_DEBUG)
192007 # define NDEBUG 1
192008 #endif
192009
192010 /* #include <assert.h> */
192011 /* #include <string.h> */
192012 /* #include <stdlib.h> */
192013 /* #include <stdarg.h> */
192014
@@ -233132,11 +233291,11 @@
233291 int nArg, /* Number of args */
233292 sqlite3_value **apUnused /* Function arguments */
233293 ){
233294 assert( nArg==0 );
233295 UNUSED_PARAM2(nArg, apUnused);
233296 sqlite3_result_text(pCtx, "fts5: 2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10", -1, SQLITE_TRANSIENT);
233297 }
233298
233299 /*
233300 ** Return true if zName is the extension on one of the shadow tables used
233301 ** by this module.
233302
+37 -9
--- 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 "2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882"
151
+#define SQLITE_SOURCE_ID "2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -3822,17 +3822,18 @@
38223822
**
38233823
** The values returned by sqlite3_errcode() and/or
38243824
** sqlite3_extended_errcode() might change with each API call.
38253825
** Except, there are some interfaces that are guaranteed to never
38263826
** change the value of the error code. The error-code preserving
3827
-** interfaces are:
3827
+** interfaces include the following:
38283828
**
38293829
** <ul>
38303830
** <li> sqlite3_errcode()
38313831
** <li> sqlite3_extended_errcode()
38323832
** <li> sqlite3_errmsg()
38333833
** <li> sqlite3_errmsg16()
3834
+** <li> sqlite3_error_offset()
38343835
** </ul>
38353836
**
38363837
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
38373838
** text that describes the error, as either UTF-8 or UTF-16 respectively.
38383839
** ^(Memory to hold the error message string is managed internally.
@@ -3842,10 +3843,17 @@
38423843
**
38433844
** ^The sqlite3_errstr() interface returns the English-language text
38443845
** that describes the [result code], as UTF-8.
38453846
** ^(Memory to hold the error message string is managed internally
38463847
** and must not be freed by the application)^.
3848
+**
3849
+** ^If the most recent error references a specific token in the input
3850
+** SQL, the sqlite3_error_offset() interface returns the byte offset
3851
+** of the start of that token. ^The byte offset returned by
3852
+** sqlite3_error_offset() assumes that the input SQL is UTF8.
3853
+** ^If the most error does not reference a specific token in the input
3854
+** SQL, then the sqlite3_error_offset() function returns -1.
38473855
**
38483856
** When the serialized [threading mode] is in use, it might be the
38493857
** case that a second error occurs on a separate thread in between
38503858
** the time of the first error and the call to these interfaces.
38513859
** When that happens, the second error will be reported since these
@@ -3862,10 +3870,11 @@
38623870
SQLITE_API int sqlite3_errcode(sqlite3 *db);
38633871
SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
38643872
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
38653873
SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
38663874
SQLITE_API const char *sqlite3_errstr(int);
3875
+SQLITE_API int sqlite3_error_offset(sqlite3 *db);
38673876
38683877
/*
38693878
** CAPI3REF: Prepared Statement Object
38703879
** KEYWORDS: {prepared statement} {prepared statements}
38713880
**
@@ -9462,18 +9471,37 @@
94629471
94639472
/*
94649473
** CAPI3REF: Determine The Collation For a Virtual Table Constraint
94659474
**
94669475
** This function may only be called from within a call to the [xBestIndex]
9467
-** method of a [virtual table].
9476
+** method of a [virtual table]. This function returns a pointer to a string
9477
+** that is the name of the appropriate collation sequence to use for text
9478
+** comparisons on the constraint identified by its arguments.
94689479
**
9469
-** The first argument must be the sqlite3_index_info object that is the
9470
-** first parameter to the xBestIndex() method. The second argument must be
9471
-** an index into the aConstraint[] array belonging to the sqlite3_index_info
9472
-** structure passed to xBestIndex. This function returns a pointer to a buffer
9473
-** containing the name of the collation sequence for the corresponding
9474
-** constraint.
9480
+** The first argument must be the pointer to the sqlite3_index_info object
9481
+** that is the first parameter to the xBestIndex() method. The second argument
9482
+** must be an index into the aConstraint[] array belonging to the
9483
+** sqlite3_index_info structure passed to xBestIndex.
9484
+**
9485
+** Important:
9486
+** The first parameter must be the same pointer that is passed into the
9487
+** xBestMethod() method. The first parameter may not be a pointer to a
9488
+** different sqlite3_index_info object, even an exact copy.
9489
+**
9490
+** The return value is computed as follows:
9491
+**
9492
+** <ol>
9493
+** <li><p> If the constraint comes from a WHERE clause expression that contains
9494
+** a [COLLATE operator], then the name of the collation specified by
9495
+** that COLLATE operator is returned.
9496
+** <li><p> If there is no COLLATE operator, but the column that is the subject
9497
+** of the constraint specifies an alternative collating sequence via
9498
+** a [COLLATE clause] on the column definition within the CREATE TABLE
9499
+** statement that was passed into [sqlite3_declare_vtab()], then the
9500
+** name of that alternative collating sequence is returned.
9501
+** <li><p> Otherwise, "BINARY" is returned.
9502
+** </ol>
94759503
*/
94769504
SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
94779505
94789506
/*
94799507
** CAPI3REF: Conflict resolution modes
94809508
--- 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 "2021-12-09 20:06:18 633bfeeea2bccdd44126acf3f61ecca163c9d933bdc787a2c18a697dc9406882"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -3822,17 +3822,18 @@
3822 **
3823 ** The values returned by sqlite3_errcode() and/or
3824 ** sqlite3_extended_errcode() might change with each API call.
3825 ** Except, there are some interfaces that are guaranteed to never
3826 ** change the value of the error code. The error-code preserving
3827 ** interfaces are:
3828 **
3829 ** <ul>
3830 ** <li> sqlite3_errcode()
3831 ** <li> sqlite3_extended_errcode()
3832 ** <li> sqlite3_errmsg()
3833 ** <li> sqlite3_errmsg16()
 
3834 ** </ul>
3835 **
3836 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3837 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
3838 ** ^(Memory to hold the error message string is managed internally.
@@ -3842,10 +3843,17 @@
3842 **
3843 ** ^The sqlite3_errstr() interface returns the English-language text
3844 ** that describes the [result code], as UTF-8.
3845 ** ^(Memory to hold the error message string is managed internally
3846 ** and must not be freed by the application)^.
 
 
 
 
 
 
 
3847 **
3848 ** When the serialized [threading mode] is in use, it might be the
3849 ** case that a second error occurs on a separate thread in between
3850 ** the time of the first error and the call to these interfaces.
3851 ** When that happens, the second error will be reported since these
@@ -3862,10 +3870,11 @@
3862 SQLITE_API int sqlite3_errcode(sqlite3 *db);
3863 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3864 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3865 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3866 SQLITE_API const char *sqlite3_errstr(int);
 
3867
3868 /*
3869 ** CAPI3REF: Prepared Statement Object
3870 ** KEYWORDS: {prepared statement} {prepared statements}
3871 **
@@ -9462,18 +9471,37 @@
9462
9463 /*
9464 ** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9465 **
9466 ** This function may only be called from within a call to the [xBestIndex]
9467 ** method of a [virtual table].
 
 
9468 **
9469 ** The first argument must be the sqlite3_index_info object that is the
9470 ** first parameter to the xBestIndex() method. The second argument must be
9471 ** an index into the aConstraint[] array belonging to the sqlite3_index_info
9472 ** structure passed to xBestIndex. This function returns a pointer to a buffer
9473 ** containing the name of the collation sequence for the corresponding
9474 ** constraint.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9475 */
9476 SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9477
9478 /*
9479 ** CAPI3REF: Conflict resolution modes
9480
--- 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 "2021-12-31 22:53:15 e654b57a9fc32021453eed48d1c1bba65c833fb1aac3946567968c877e4cbd10"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -3822,17 +3822,18 @@
3822 **
3823 ** The values returned by sqlite3_errcode() and/or
3824 ** sqlite3_extended_errcode() might change with each API call.
3825 ** Except, there are some interfaces that are guaranteed to never
3826 ** change the value of the error code. The error-code preserving
3827 ** interfaces include the following:
3828 **
3829 ** <ul>
3830 ** <li> sqlite3_errcode()
3831 ** <li> sqlite3_extended_errcode()
3832 ** <li> sqlite3_errmsg()
3833 ** <li> sqlite3_errmsg16()
3834 ** <li> sqlite3_error_offset()
3835 ** </ul>
3836 **
3837 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3838 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
3839 ** ^(Memory to hold the error message string is managed internally.
@@ -3842,10 +3843,17 @@
3843 **
3844 ** ^The sqlite3_errstr() interface returns the English-language text
3845 ** that describes the [result code], as UTF-8.
3846 ** ^(Memory to hold the error message string is managed internally
3847 ** and must not be freed by the application)^.
3848 **
3849 ** ^If the most recent error references a specific token in the input
3850 ** SQL, the sqlite3_error_offset() interface returns the byte offset
3851 ** of the start of that token. ^The byte offset returned by
3852 ** sqlite3_error_offset() assumes that the input SQL is UTF8.
3853 ** ^If the most error does not reference a specific token in the input
3854 ** SQL, then the sqlite3_error_offset() function returns -1.
3855 **
3856 ** When the serialized [threading mode] is in use, it might be the
3857 ** case that a second error occurs on a separate thread in between
3858 ** the time of the first error and the call to these interfaces.
3859 ** When that happens, the second error will be reported since these
@@ -3862,10 +3870,11 @@
3870 SQLITE_API int sqlite3_errcode(sqlite3 *db);
3871 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3872 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3873 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3874 SQLITE_API const char *sqlite3_errstr(int);
3875 SQLITE_API int sqlite3_error_offset(sqlite3 *db);
3876
3877 /*
3878 ** CAPI3REF: Prepared Statement Object
3879 ** KEYWORDS: {prepared statement} {prepared statements}
3880 **
@@ -9462,18 +9471,37 @@
9471
9472 /*
9473 ** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9474 **
9475 ** This function may only be called from within a call to the [xBestIndex]
9476 ** method of a [virtual table]. This function returns a pointer to a string
9477 ** that is the name of the appropriate collation sequence to use for text
9478 ** comparisons on the constraint identified by its arguments.
9479 **
9480 ** The first argument must be the pointer to the sqlite3_index_info object
9481 ** that is the first parameter to the xBestIndex() method. The second argument
9482 ** must be an index into the aConstraint[] array belonging to the
9483 ** sqlite3_index_info structure passed to xBestIndex.
9484 **
9485 ** Important:
9486 ** The first parameter must be the same pointer that is passed into the
9487 ** xBestMethod() method. The first parameter may not be a pointer to a
9488 ** different sqlite3_index_info object, even an exact copy.
9489 **
9490 ** The return value is computed as follows:
9491 **
9492 ** <ol>
9493 ** <li><p> If the constraint comes from a WHERE clause expression that contains
9494 ** a [COLLATE operator], then the name of the collation specified by
9495 ** that COLLATE operator is returned.
9496 ** <li><p> If there is no COLLATE operator, but the column that is the subject
9497 ** of the constraint specifies an alternative collating sequence via
9498 ** a [COLLATE clause] on the column definition within the CREATE TABLE
9499 ** statement that was passed into [sqlite3_declare_vtab()], then the
9500 ** name of that alternative collating sequence is returned.
9501 ** <li><p> Otherwise, "BINARY" is returned.
9502 ** </ol>
9503 */
9504 SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9505
9506 /*
9507 ** CAPI3REF: Conflict resolution modes
9508

Keyboard Shortcuts

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