Fossil SCM

Update the built-in SQLite to the latest trunk version for beta testing.

drh 2026-10-25 00:50 UTC trunk
Commit e66a8c79ad286ed3acdacc595c0ce7196d578982bd3c333552741d94dbfff99a
3 files changed +699 -333 +839 -266 +21 -3
+699 -333
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1775,19 +1775,27 @@
17751775
n = strlen(z);
17761776
if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0;
17771777
if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0;
17781778
i = (z[0]=='-' || z[0]=='+');
17791779
if( strcmp(z+i,"Inf")==0 ) return 0;
1780
- if( !qrfDigit(z[i]) ) return 1;
1781
- i++;
1780
+ if( z[i]=='0' && (z[i+1]=='x' || z[i+1]=='X') ){
1781
+ /* Hexadecimal integer literal: 0x1f */
1782
+ i += 2;
1783
+ n = strspn(&z[i], "0123456789abcdefABCDEF");
1784
+ return n==0 || z[i+n]!=0;
1785
+ }
1786
+ if( z[i]=='.' && qrfDigit(z[i+1]) ){
1787
+ /* Real literal with no digits before the ".", such as ".5" */
1788
+ i++;
1789
+ }else if( !qrfDigit(z[i]) ){
1790
+ return 1;
1791
+ }else{
1792
+ while( qrfDigit(z[i]) ){ i++; }
1793
+ if( z[i]=='.' ) i++;
1794
+ }
17821795
while( qrfDigit(z[i]) ){ i++; }
17831796
if( z[i]==0 ) return 0;
1784
- if( z[i]=='.' ){
1785
- i++;
1786
- while( qrfDigit(z[i]) ){ i++; }
1787
- if( z[i]==0 ) return 0;
1788
- }
17891797
if( z[i]=='e' || z[i]=='E' ){
17901798
i++;
17911799
if( z[i]=='+' || z[i]=='-' ){ i++; }
17921800
if( !qrfDigit(z[i]) ) return 1;
17931801
i++;
@@ -3724,10 +3732,16 @@
37243732
}
37253733
if( p->nRow ) sqlite3_str_append(p->pOut, "\n", 1);
37263734
pVal = sqlite3_str_new(p->db);
37273735
nSep = (int)strlen(p->spec.zColumnSep);
37283736
mxW = p->mxWidth - (nSep + p->u.sLine.mxColWth);
3737
+ if( mxW<2 ){
3738
+ /* The column names leave little or no room for the value. Use a
3739
+ ** width of at least 2, enough for any one character, so that
3740
+ ** qrfWrapLine() always makes progress. */
3741
+ mxW = 2;
3742
+ }
37293743
bWW = p->spec.bWordWrap==QRF_Yes;
37303744
for(i=0; i<p->nCol; i++){
37313745
const char *zVal;
37323746
int cnt = 0;
37333747
qrfWidthPrint(p, p->pOut, -p->u.sLine.mxColWth, p->u.sLine.azCol[i]);
@@ -3846,11 +3860,11 @@
38463860
}
38473861
case QRF_STYLE_JObject:
38483862
case QRF_STYLE_Json: {
38493863
p->spec.eText = QRF_TEXT_Json;
38503864
p->spec.zNull = "null";
3851
- if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%0.16g";
3865
+ if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%!0.17g";
38523866
break;
38533867
}
38543868
case QRF_STYLE_Html: {
38553869
p->spec.eText = QRF_TEXT_Html;
38563870
p->spec.zNull = "null";
@@ -3860,11 +3874,11 @@
38603874
p->spec.eText = QRF_TEXT_Sql;
38613875
p->spec.zNull = "NULL";
38623876
if( p->spec.zTableName==0 || p->spec.zTableName[0]==0 ){
38633877
p->spec.zTableName = "tab";
38643878
}
3865
- if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%0.16g";
3879
+ if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%!0.17g";
38663880
p->u.nIns = 0;
38673881
break;
38683882
}
38693883
case QRF_STYLE_Line: {
38703884
if( p->spec.zColumnSep==0 ){
@@ -3906,11 +3920,15 @@
39063920
}
39073921
break;
39083922
}
39093923
}
39103924
if( p->spec.eEsc==QRF_Auto ){
3911
- p->spec.eEsc = QRF_ESC_Ascii;
3925
+ if( p->spec.eText==QRF_TEXT_Csv ){
3926
+ p->spec.eEsc = QRF_ESC_Off;
3927
+ }else{
3928
+ p->spec.eEsc = QRF_ESC_Ascii;
3929
+ }
39123930
}
39133931
if( p->spec.eText==QRF_Auto ){
39143932
p->spec.eText = QRF_TEXT_Plain;
39153933
}
39163934
if( p->spec.eTitle==QRF_Auto ){
@@ -4032,11 +4050,12 @@
40324050
sqlite3_free(p->spec.pzOutput[0]);
40334051
p->spec.pzOutput[0] = 0;
40344052
qrfOom(p);
40354053
}else{
40364054
p->spec.pzOutput[0] = zCombined;
4037
- memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n+1);
4055
+ if( n ) memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n);
4056
+ zCombined[sz+n] = 0;
40384057
}
40394058
sqlite3_str_free(p->pOut);
40404059
}else{
40414060
p->spec.pzOutput[0] = qrfStrFinish(p, p->pOut);
40424061
}
@@ -6437,11 +6456,11 @@
64376456
** pA->isNull==0
64386457
** pB!=0
64396458
** pB->isNull==0
64406459
*/
64416460
static int decimal_cmp(Decimal *pA, Decimal *pB){
6442
- int nASig, nBSig, rc, n;
6461
+ int nASig, nBSig, rc, n, iA, iB;
64436462
while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
64446463
pA->nDigit--;
64456464
pA->nFrac--;
64466465
}
64476466
while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
@@ -6454,20 +6473,22 @@
64546473
if( pA->sign ){
64556474
Decimal *pTemp = pA;
64566475
pA = pB;
64576476
pB = pTemp;
64586477
}
6459
- nASig = pA->nDigit - pA->nFrac;
6460
- nBSig = pB->nDigit - pB->nFrac;
6478
+ for(iA=0; iA<pA->nDigit-pA->nFrac && pA->a[iA]==0; iA++){}
6479
+ for(iB=0; iB<pB->nDigit-pB->nFrac && pB->a[iB]==0; iB++){}
6480
+ nASig = pA->nDigit - pA->nFrac - iA;
6481
+ nBSig = pB->nDigit - pB->nFrac - iB;
64616482
if( nASig!=nBSig ){
64626483
return nASig - nBSig;
64636484
}
6464
- n = pA->nDigit;
6465
- if( n>pB->nDigit ) n = pB->nDigit;
6466
- rc = memcmp(pA->a, pB->a, n);
6485
+ n = pA->nDigit - iA;
6486
+ if( n>pB->nDigit - iB ) n = pB->nDigit - iB;
6487
+ rc = memcmp(pA->a+iA, pB->a+iB, n);
64676488
if( rc==0 ){
6468
- rc = pA->nDigit - pB->nDigit;
6489
+ rc = (pA->nDigit - iA) - (pB->nDigit - iB);
64696490
}
64706491
return rc;
64716492
}
64726493
64736494
/*
@@ -6749,21 +6770,21 @@
67496770
static int decimalSafeInt(
67506771
sqlite3_context *context,
67516772
sqlite3_value *pVal,
67526773
int *pResult
67536774
){
6754
- sqlite3_int64 i64 = sqlite3_value_int64(pVal);
6755
- if( i64<-2147483648LL || i64>2147483647LL ){
6775
+ sqlite3_int64 ii = sqlite3_value_int64(pVal);
6776
+ if( ii<-2147483648LL || ii>2147483647LL ){
67566777
char *zMsg = sqlite3_mprintf(
67576778
"value %lld is too large: range limit -2147483648..+2147483647",
6758
- i64);
6779
+ ii);
67596780
sqlite3_result_error(context, zMsg, -1);
67606781
sqlite3_free(zMsg);
67616782
*pResult = 0;
67626783
return 1;
67636784
}else{
6764
- *pResult = (int)i64;
6785
+ *pResult = (int)ii;
67656786
return 0;
67666787
}
67676788
}
67686789
67696790
/*
@@ -6843,12 +6864,14 @@
68436864
sqlite3_value **argv
68446865
){
68456866
Decimal *pA = decimal_new(context, argv[0], 1);
68466867
Decimal *pB = decimal_new(context, argv[1], 1);
68476868
UNUSED_PARAMETER(argc);
6848
- decimal_add(pA, pB);
6849
- decimal_result(context, pA);
6869
+ if( pA && pB ){
6870
+ decimal_add(pA, pB);
6871
+ decimal_result(context, pA);
6872
+ }
68506873
decimal_free(pA);
68516874
decimal_free(pB);
68526875
}
68536876
static void decimalSubFunc(
68546877
sqlite3_context *context,
@@ -6856,11 +6879,11 @@
68566879
sqlite3_value **argv
68576880
){
68586881
Decimal *pA = decimal_new(context, argv[0], 1);
68596882
Decimal *pB = decimal_new(context, argv[1], 1);
68606883
UNUSED_PARAMETER(argc);
6861
- if( pB ){
6884
+ if( pA && pB ){
68626885
pB->sign = !pB->sign;
68636886
decimal_add(pA, pB);
68646887
decimal_result(context, pA);
68656888
}
68666889
decimal_free(pA);
@@ -8082,11 +8105,11 @@
80828105
80838106
if( m<0 ){
80848107
if( m<(-9223372036854775807LL) ) return;
80858108
isNeg = 1;
80868109
m = -m;
8087
- }else if( m==0 && e>-1000 && e<1000 ){
8110
+ }else if( m==0 ){
80888111
sqlite3_result_double(context, 0.0);
80898112
return;
80908113
}
80918114
while( (m>>32)&0xffe00000 ){
80928115
m >>= 1;
@@ -9645,11 +9668,14 @@
96459668
static unsigned re_esc_char(ReCompiled *p){
96469669
static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]-";
96479670
static const char zTrans[] = "\a\f\n\r\t\v";
96489671
int i, v = 0;
96499672
char c;
9650
- if( p->sIn.i>=p->sIn.mx ) return 0;
9673
+ if( p->sIn.i>=p->sIn.mx ){
9674
+ p->zErr = "unknown \\ escape";
9675
+ return 0;
9676
+ }
96519677
c = p->sIn.z[p->sIn.i];
96529678
if( c=='u' && p->sIn.i+4<p->sIn.mx ){
96539679
const unsigned char *zIn = p->sIn.z + p->sIn.i;
96549680
if( re_hex(zIn[1],&v)
96559681
&& re_hex(zIn[2],&v)
@@ -10376,41 +10402,70 @@
1037610402
** Throw an SQLITE_IOERR if there are difficulties pulling the file
1037710403
** off of disk.
1037810404
*/
1037910405
static void readFileContents(sqlite3_context *ctx, const char *zName){
1038010406
FILE *in;
10381
- sqlite3_int64 nIn;
10382
- void *pBuf;
10407
+ sqlite3_int64 nIn; /* Size of the file according to ftell() */
10408
+ sqlite3_int64 nAlloc; /* Bytes allocated for pBuf */
10409
+ sqlite3_int64 nRead = 0; /* Bytes read so far */
10410
+ unsigned char *pBuf;
1038310411
sqlite3 *db;
1038410412
int mxBlob;
1038510413
1038610414
in = sqlite3_fopen(zName, "rb");
1038710415
if( in==0 ){
1038810416
/* File does not exist or is unreadable. Leave the result set to NULL. */
1038910417
return;
1039010418
}
10419
+ /* The size reported by ftell() is only a hint. It is -1 for pipes and
10420
+ ** FIFOs and 0 for many files under /proc. Keep reading until EOF. */
1039110421
fseek(in, 0, SEEK_END);
1039210422
nIn = ftell(in);
1039310423
rewind(in);
10424
+ if( nIn<0 ) nIn = 0;
1039410425
db = sqlite3_context_db_handle(ctx);
1039510426
mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
1039610427
if( nIn>mxBlob ){
1039710428
sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
1039810429
fclose(in);
1039910430
return;
1040010431
}
10401
- pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
10432
+ /* One extra byte so that EOF on a regular file is seen without a
10433
+ ** reallocation. */
10434
+ nAlloc = nIn+1;
10435
+ pBuf = sqlite3_malloc64( nAlloc );
1040210436
if( pBuf==0 ){
1040310437
sqlite3_result_error_nomem(ctx);
1040410438
fclose(in);
1040510439
return;
1040610440
}
10407
- if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
10408
- sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
10409
- }else{
10441
+ while( 1 ){
10442
+ unsigned char *pNew;
10443
+ nRead += fread(pBuf+nRead, 1, (size_t)(nAlloc-nRead), in);
10444
+ if( nRead>mxBlob ){
10445
+ sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
10446
+ sqlite3_free(pBuf);
10447
+ fclose(in);
10448
+ return;
10449
+ }
10450
+ if( nRead<nAlloc ) break; /* Short read means EOF or an error */
10451
+ nAlloc *= 2;
10452
+ if( nAlloc>(sqlite3_int64)mxBlob+1 ) nAlloc = (sqlite3_int64)mxBlob+1;
10453
+ pNew = sqlite3_realloc64(pBuf, nAlloc);
10454
+ if( pNew==0 ){
10455
+ sqlite3_result_error_nomem(ctx);
10456
+ sqlite3_free(pBuf);
10457
+ fclose(in);
10458
+ return;
10459
+ }
10460
+ pBuf = pNew;
10461
+ }
10462
+ if( ferror(in) ){
1041010463
sqlite3_result_error_code(ctx, SQLITE_IOERR);
1041110464
sqlite3_free(pBuf);
10465
+ }else{
10466
+ sqlite3_result_blob64(ctx, pBuf, nRead, sqlite3_free);
1041210467
}
1041310468
fclose(in);
1041410469
}
1041510470
1041610471
/*
@@ -11088,17 +11143,42 @@
1108811143
i = 1;
1108911144
if( (idxNum & 0x02)!=0 ){
1109011145
assert( argc>i );
1109111146
pCur->zBase = (const char*)sqlite3_value_text(argv[i++]);
1109211147
}
11148
+ pCur->mxLvl = 1000000000;
1109311149
if( (idxNum & 0x0c)!=0 ){
11150
+ /* The LEVEL constraint is only used to limit the depth of the
11151
+ ** search. SQLite still checks the constraint against every row, so
11152
+ ** mxLvl may admit rows that do not match, but must never exclude
11153
+ ** a row that does. Only entries with level<mxLvl are visited. */
11154
+ sqlite3_value *pMx;
1109411155
assert( argc>i );
11095
- pCur->mxLvl = sqlite3_value_int(argv[i++]);
11096
- if( idxNum & 0x08 ) pCur->mxLvl++;
11097
- if( pCur->mxLvl<=0 ) pCur->mxLvl = 1000000000;
11098
- }else{
11099
- pCur->mxLvl = 1000000000;
11156
+ pMx = argv[i++];
11157
+ switch( sqlite3_value_type(pMx) ){
11158
+ case SQLITE_NULL: {
11159
+ /* No row can match a NULL constraint */
11160
+ pCur->mxLvl = 1;
11161
+ break;
11162
+ }
11163
+ case SQLITE_INTEGER:
11164
+ case SQLITE_FLOAT: {
11165
+ double r = sqlite3_value_double(pMx);
11166
+ if( r<1.0 ){
11167
+ pCur->mxLvl = 1;
11168
+ }else if( r<1.0e9 ){
11169
+ int iMx = (int)r;
11170
+ if( (idxNum & 0x08)!=0 || (double)iMx<r ) iMx++;
11171
+ pCur->mxLvl = iMx;
11172
+ }
11173
+ break;
11174
+ }
11175
+ default: {
11176
+ /* TEXT or BLOB. Do not limit the search. */
11177
+ break;
11178
+ }
11179
+ }
1110011180
}
1110111181
if( pCur->zBase ){
1110211182
pCur->nBase = (int)strlen(pCur->zBase)+1;
1110311183
pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
1110411184
}else{
@@ -11127,11 +11207,15 @@
1112711207
**
1112811208
** The query plan is represented by bits in idxNum:
1112911209
**
1113011210
** 0x01 The path value is supplied by argv[0]
1113111211
** 0x02 dir is in argv[1]
11132
-** 0x04 maxdepth is in argv[1] or [2]
11212
+** 0x04 LEVEL< bound is in argv[1] or [2]
11213
+** 0x08 LEVEL<= or LEVEL= bound is in argv[1] or [2]
11214
+**
11215
+** The LEVEL constraint is never omitted. It is used only to limit the
11216
+** depth of the directory search, and SQLite rechecks it for each row.
1113311217
*/
1113411218
static int fsdirBestIndex(
1113511219
sqlite3_vtab *tab,
1113611220
sqlite3_index_info *pIdxInfo
1113711221
){
@@ -11138,11 +11222,10 @@
1113811222
int i; /* Loop over constraints */
1113911223
int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
1114011224
int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
1114111225
int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */
1114211226
int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */
11143
- int omitLevel = 0; /* omit the LEVEL constraint */
1114411227
int seenPath = 0; /* True if an unusable PATH= constraint is seen */
1114511228
int seenDir = 0; /* True if an unusable DIR= constraint is seen */
1114611229
const struct sqlite3_index_constraint *pConstraint;
1114711230
1114811231
(void)tab;
@@ -11170,11 +11253,10 @@
1117011253
}
1117111254
case FSDIR_COLUMN_LEVEL: {
1117211255
if( pConstraint->usable && idxLevel<0 ){
1117311256
idxLevel = i;
1117411257
idxLevelEQ = 0x08;
11175
- omitLevel = 0;
1117611258
}
1117711259
break;
1117811260
}
1117911261
}
1118011262
}else
@@ -11183,15 +11265,13 @@
1118311265
&& idxLevel<0
1118411266
){
1118511267
if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
1118611268
idxLevel = i;
1118711269
idxLevelEQ = 0x08;
11188
- omitLevel = 1;
1118911270
}else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
1119011271
idxLevel = i;
1119111272
idxLevelEQ = 0x04;
11192
- omitLevel = 1;
1119311273
}
1119411274
}
1119511275
}
1119611276
if( seenPath || seenDir ){
1119711277
/* If input parameters are unusable, disallow this plan */
@@ -11214,11 +11294,10 @@
1121411294
pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++;
1121511295
pIdxInfo->idxNum |= 0x02;
1121611296
pIdxInfo->estimatedCost /= 1.0e4;
1121711297
}
1121811298
if( idxLevel>=0 ){
11219
- pIdxInfo->aConstraintUsage[idxLevel].omit = omitLevel;
1122011299
pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++;
1122111300
pIdxInfo->idxNum |= idxLevelEQ;
1122211301
pIdxInfo->estimatedCost /= 1.0e4;
1122311302
}
1122411303
}
@@ -12775,10 +12854,11 @@
1277512854
"z HIDDEN" /* 7: Name of zip file */
1277612855
") WITHOUT ROWID;";
1277712856
1277812857
#define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
1277912858
#define ZIPFILE_MX_NAME (250) /* Windows limitation on filename size */
12859
+#define ZIPFILE_MX_ENTRY (65535) /* Max entries without zip64 extensions */
1278012860
1278112861
/*
1278212862
** The buffer should be large enough to contain 3 65536 byte strings - the
1278312863
** filename, the extra field and the file comment.
1278412864
*/
@@ -14403,11 +14483,13 @@
1440314483
1440414484
/* Check that we're not inserting a duplicate entry -OR- updating an
1440514485
** entry with a path, thereby making it into a duplicate. */
1440614486
if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
1440714487
ZipfileEntry *p;
14488
+ int nEntry = 0;
1440814489
for(p=pTab->pFirstEntry; p; p=p->pNext){
14490
+ nEntry++;
1440914491
if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
1441014492
switch( sqlite3_vtab_on_conflict(pTab->db) ){
1441114493
case SQLITE_IGNORE: {
1441214494
goto zipfile_update_done;
1441314495
}
@@ -14422,10 +14504,17 @@
1442214504
}
1442314505
}
1442414506
break;
1442514507
}
1442614508
}
14509
+
14510
+ /* The EOCD record stores the number of entries as a 16-bit value.
14511
+ ** Refuse to add an entry that would cause it to overflow. */
14512
+ if( p==0 && pOld==0 && nEntry>=ZIPFILE_MX_ENTRY ){
14513
+ zipfileTableErr(pTab, "too many entries; max: %d", ZIPFILE_MX_ENTRY);
14514
+ rc = SQLITE_CONSTRAINT;
14515
+ }
1442714516
}
1442814517
1442914518
if( rc==SQLITE_OK ){
1443014519
/* Create the new CDS record. */
1443114520
pNew = zipfileNewEntry(zPath);
@@ -14555,10 +14644,11 @@
1455514644
}
1455614645
1455714646
/* Write out the EOCD record */
1455814647
eocd.iDisk = 0;
1455914648
eocd.iFirstDisk = 0;
14649
+ assert( nEntry<=ZIPFILE_MX_ENTRY );
1456014650
eocd.nEntry = (u16)nEntry;
1456114651
eocd.nEntryTotal = (u16)nEntry;
1456214652
eocd.nSize = (u32)(pTab->szCurrent - iOffset);
1456314653
eocd.iOffset = (u32)iOffset;
1456414654
rc = zipfileAppendEOCD(pTab, &eocd);
@@ -14747,10 +14837,16 @@
1474714837
"filename argument to zipfile() too big; max: %d bytes",
1474814838
ZIPFILE_MX_NAME);
1474914839
rc = SQLITE_ERROR;
1475014840
goto zipfile_step_out;
1475114841
}
14842
+ if( p->nEntry>=ZIPFILE_MX_ENTRY ){
14843
+ zErr = sqlite3_mprintf("too many entries for zipfile(); max: %d",
14844
+ ZIPFILE_MX_ENTRY);
14845
+ rc = SQLITE_ERROR;
14846
+ goto zipfile_step_out;
14847
+ }
1475214848
1475314849
/* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
1475414850
** deflate compression) or NULL (choose automatically). */
1475514851
if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
1475614852
iMethod = (int)sqlite3_value_int64(pMethod);
@@ -14874,10 +14970,11 @@
1487414970
1487514971
p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
1487614972
if( p==0 ) return;
1487714973
if( p->nEntry>0 ){
1487814974
memset(&eocd, 0, sizeof(eocd));
14975
+ assert( p->nEntry<=ZIPFILE_MX_ENTRY );
1487914976
eocd.nEntry = (u16)p->nEntry;
1488014977
eocd.nEntryTotal = (u16)p->nEntry;
1488114978
eocd.nSize = p->cds.n;
1488214979
eocd.iOffset = p->body.n;
1488314980
@@ -20235,11 +20332,16 @@
2023520332
sqlite3_str_appendchar(p->pOut, 1, '\n');
2023620333
return;
2023720334
}else{
2023820335
r = num*100.0/(double)denom;
2023920336
}
20240
- sqlite3_snprintf(sizeof(zNum)-5, zNum, r>=10.0 ? "%.3g" :"%.2g", r);
20337
+ if( r>0.0 && r<0.0001 ){
20338
+ /* Avoid exponential notation for very small percentages */
20339
+ sqlite3_snprintf(sizeof(zNum)-5, zNum, "<0.0001");
20340
+ }else{
20341
+ sqlite3_snprintf(sizeof(zNum)-5, zNum, r>=10.0 ? "%.3g" :"%.2g", r);
20342
+ }
2024120343
sz = (int)strlen(zNum);
2024220344
zDP = strchr(zNum, '.');
2024320345
if( zDP==0 ){
2024420346
memcpy(zNum+sz,".0",3);
2024520347
nLeadingDigit = sz;
@@ -21717,15 +21819,43 @@
2171721819
int n = ((eType-12) / 2);
2171821820
if( eType % 2 ){
2171921821
switch( enc ){
2172021822
#ifndef SQLITE_OMIT_UTF16
2172121823
case SQLITE_UTF16BE:
21722
- sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
21723
- break;
21724
- case SQLITE_UTF16LE:
21725
- sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
21824
+ case SQLITE_UTF16LE: {
21825
+ /* sqlite3_result_text16be/le() remove a leading BOM. If
21826
+ ** the stored text begins with U+FEFF or U+FFFE, prepend
21827
+ ** a BOM in the database byte order, so that only the
21828
+ ** added BOM is removed. */
21829
+ u8 *aBuf = 0;
21830
+ if( n>=2 && ((pData[0]==0xFE && pData[1]==0xFF)
21831
+ || (pData[0]==0xFF && pData[1]==0xFE))
21832
+ ){
21833
+ aBuf = (u8*)sqlite3_malloc64(n+2);
21834
+ if( aBuf==0 ){
21835
+ sqlite3_result_error_nomem(pCtx);
21836
+ break;
21837
+ }
21838
+ aBuf[0] = (enc==SQLITE_UTF16BE) ? 0xFE : 0xFF;
21839
+ aBuf[1] = (enc==SQLITE_UTF16BE) ? 0xFF : 0xFE;
21840
+ memcpy(&aBuf[2], pData, n);
21841
+ }
21842
+ if( enc==SQLITE_UTF16BE ){
21843
+ if( aBuf ){
21844
+ sqlite3_result_text16be(pCtx, aBuf, n+2, sqlite3_free);
21845
+ }else{
21846
+ sqlite3_result_text16be(pCtx, pData, n, SQLITE_TRANSIENT);
21847
+ }
21848
+ }else{
21849
+ if( aBuf ){
21850
+ sqlite3_result_text16le(pCtx, aBuf, n+2, sqlite3_free);
21851
+ }else{
21852
+ sqlite3_result_text16le(pCtx, pData, n, SQLITE_TRANSIENT);
21853
+ }
21854
+ }
2172621855
break;
21856
+ }
2172721857
#endif
2172821858
default:
2172921859
sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
2173021860
break;
2173121861
}
@@ -22294,10 +22424,16 @@
2229422424
**
2229522425
** nCol/aCol[]:
2229622426
** aCol[] is an array of nCol columns. In the order in which they appear
2229722427
** in the table.
2229822428
**
22429
+** nField:
22430
+** The number of fields in each record of the table's b-tree. This may
22431
+** be less than nCol (VIRTUAL generated columns have no field) or, for
22432
+** a WITHOUT ROWID table with a column that appears more than once in
22433
+** the PRIMARY KEY, greater than nCol.
22434
+**
2229922435
** bIntkey:
2230022436
** Set to true for intkey tables, false for WITHOUT ROWID.
2230122437
**
2230222438
** iRowidBind:
2230322439
** Each column in the aCol[] array has associated with it the index of
@@ -22318,10 +22454,11 @@
2231822454
*/
2231922455
struct RecoverTable {
2232022456
u32 iRoot; /* Root page in original database */
2232122457
char *zTab; /* Name of table */
2232222458
int nCol; /* Number of columns in table */
22459
+ int nField; /* Number of fields in each record */
2232322460
RecoverColumn *aCol; /* Array of columns */
2232422461
int bIntkey; /* True for intkey, false for without rowid */
2232522462
int iRowidBind; /* If >0, bind rowid to INSERT here */
2232622463
RecoverTable *pNext;
2232722464
};
@@ -22401,11 +22538,11 @@
2240122538
sqlite3_stmt *pSel;
2240222539
sqlite3_stmt *pInsert;
2240322540
int nInsert;
2240422541
2240522542
RecoverTable *pTab; /* Table currently being written */
22406
- int nMax; /* Max column count in any schema table */
22543
+ int nMax; /* Max field count in any schema table */
2240722544
sqlite3_value **apVal; /* Array of nMax values */
2240822545
int nVal; /* Number of valid entries in apVal[] */
2240922546
int bHaveRowid;
2241022547
i64 iRowid;
2241122548
i64 iPrevPage;
@@ -22647,11 +22784,11 @@
2264722784
2264822785
/*
2264922786
** Set the bit associated with page iPg in bitvec pMap.
2265022787
*/
2265122788
static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
22652
- if( iPg<=pMap->nPg ){
22789
+ if( iPg<=pMap->nPg && iPg>0 ){
2265322790
int iElem = (iPg / 32);
2265422791
int iBit = (iPg % 32);
2265522792
pMap->aElem[iElem] |= (((u32)1) << iBit);
2265622793
}
2265722794
}
@@ -23361,26 +23498,36 @@
2336123498
}
2336223499
memcpy(csr, z, n);
2336323500
csr += (n+1);
2336423501
}
2336523502
23503
+ pNew->nField = iField;
2336623504
pNew->pNext = p->pTblList;
2336723505
p->pTblList = pNew;
2336823506
pNew->bIntkey = 1;
2336923507
}
2337023508
2337123509
recoverFinalize(p, pStmt);
2337223510
23511
+ /* If this is a WITHOUT ROWID table, the fields of each record are
23512
+ ** ordered as reported by PRAGMA index_xinfo on the table. A column may
23513
+ ** appear more than once if it is listed more than once in the PRIMARY
23514
+ ** KEY (with different collation sequences). In that case use the first
23515
+ ** field - the others are redundant copies. */
2337323516
pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
2337423517
while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
2337523518
int iField = sqlite3_column_int(pStmt, 0);
2337623519
int iCol = sqlite3_column_int(pStmt, 1);
2337723520
23378
- assert( iCol<pNew->nCol );
23379
- pNew->aCol[iCol].iField = iField;
23380
-
23381
- pNew->bIntkey = 0;
23521
+ assert( iCol>=0 && iCol<pNew->nCol );
23522
+ if( pNew->bIntkey ){
23523
+ int ii;
23524
+ for(ii=0; ii<pNew->nCol; ii++) pNew->aCol[ii].iField = -1;
23525
+ pNew->bIntkey = 0;
23526
+ }
23527
+ if( pNew->aCol[iCol].iField<0 ) pNew->aCol[iCol].iField = iField;
23528
+ pNew->nField = iField+1;
2338223529
iPk = -2;
2338323530
}
2338423531
recoverFinalize(p, pStmt);
2338523532
2338623533
if( p->errCode==SQLITE_OK ){
@@ -23566,11 +23713,11 @@
2356623713
int ii;
2356723714
int bSql = p->xSql ? 1 : 0;
2356823715
2356923716
if( nField<=0 ) return 0;
2357023717
23571
- assert( nField<=pTab->nCol );
23718
+ assert( nField<=pTab->nField );
2357223719
2357323720
zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
2357423721
2357523722
if( pTab->iRowidBind ){
2357623723
assert( pTab->bIntkey );
@@ -23582,16 +23729,22 @@
2358223729
}
2358323730
zSqlSep = "||', '||";
2358423731
zSep = ", ";
2358523732
}
2358623733
23587
- for(ii=0; ii<nField; ii++){
23588
- int eHidden = pTab->aCol[ii].eHidden;
23589
- if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
23590
- && eHidden!=RECOVER_EHIDDEN_STORED
23591
- ){
23592
- assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
23734
+ /* Include each column that has a bind parameter and either has a field
23735
+ ** in the nField-field record or is the IPK. This must match the set of
23736
+ ** parameters bound by recoverWriteDataStep(). Note that nField counts
23737
+ ** record fields, not table columns - VIRTUAL generated columns have no
23738
+ ** field (iField<0), and the order of fields need not match the order of
23739
+ ** columns (WITHOUT ROWID tables). */
23740
+ for(ii=0; ii<pTab->nCol; ii++){
23741
+ RecoverColumn *pCol = &pTab->aCol[ii];
23742
+ if( pCol->iBind>0 && (pCol->iField<nField || pCol->bIPK) ){
23743
+ assert( pCol->iField>=0 );
23744
+ assert( pCol->eHidden!=RECOVER_EHIDDEN_VIRTUAL );
23745
+ assert( pCol->eHidden!=RECOVER_EHIDDEN_STORED );
2359323746
zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
2359423747
2359523748
if( bSql ){
2359623749
zBind = recoverMPrintf(p,
2359723750
"%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
@@ -23927,14 +24080,14 @@
2392724080
static int recoverWriteDataInit(sqlite3_recover *p){
2392824081
RecoverStateW1 *p1 = &p->w1;
2392924082
RecoverTable *pTbl = 0;
2393024083
int nByte = 0;
2393124084
23932
- /* Figure out the maximum number of columns for any table in the schema */
24085
+ /* Figure out the maximum number of fields for any table in the schema */
2393324086
assert( p1->nMax==0 );
2393424087
for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
23935
- if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
24088
+ if( pTbl->nField>p1->nMax ) p1->nMax = pTbl->nField;
2393624089
}
2393724090
2393824091
/* Allocate an array of (sqlite3_value*) in which to accumulate the values
2393924092
** that will be written to the output database in a single row. */
2394024093
nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
@@ -24037,11 +24190,11 @@
2403724190
int iField = sqlite3_column_int(pSel, 2);
2403824191
sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
2403924192
int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
2404024193
2404124194
assert( bNewCell==0 || (iField==-1 || iField==0) );
24042
- assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
24195
+ assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nField );
2404324196
2404424197
if( bNewCell ){
2404524198
int ii = 0;
2404624199
if( p1->nVal>=0 ){
2404724200
if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
@@ -24087,19 +24240,19 @@
2408724240
if( iField<0 ){
2408824241
p1->iRowid = sqlite3_column_int64(pSel, 3);
2408924242
assert( p1->nVal==-1 );
2409024243
p1->nVal = 0;
2409124244
p1->bHaveRowid = 1;
24092
- }else if( iField<pTab->nCol ){
24245
+ }else if( iField<pTab->nField ){
2409324246
assert( apVal[iField]==0 );
2409424247
apVal[iField] = sqlite3_value_dup( pVal );
2409524248
if( apVal[iField]==0 ){
2409624249
recoverError(p, SQLITE_NOMEM, 0);
2409724250
}
2409824251
p1->nVal = iField+1;
24099
- }else if( pTab->nCol==0 ){
24100
- p1->nVal = pTab->nCol;
24252
+ }else if( pTab->nField==0 ){
24253
+ p1->nVal = pTab->nField;
2410124254
}
2410224255
p1->iPrevCell = iCell;
2410324256
p1->iPrevPage = iPage;
2410424257
}
2410524258
}else{
@@ -25235,10 +25388,15 @@
2523525388
typedef struct Unlink {
2523625389
sqlite3_int64 tm; /* Unlink after this time */
2523725390
char *zFN; /* Name of file. Space from sqlite3_malloc() */
2523825391
} Unlink;
2523925392
25393
+/*
25394
+** Maximum nested depth for .if statements.
25395
+*/
25396
+#define MX_IF 10
25397
+
2524025398
/*
2524125399
** State information about the database connection is contained in an
2524225400
** instance of the following structure.
2524325401
*/
2524425402
typedef struct ShellState ShellState;
@@ -25275,10 +25433,12 @@
2527525433
unsigned flgProgress; /* Flags for the progress callback */
2527625434
unsigned shellFlgs; /* Various flags */
2527725435
unsigned nTestRun; /* Number of test cases run */
2527825436
unsigned nTestErr; /* Number of test cases that failed */
2527925437
sqlite3_int64 szMax; /* --maxsize argument to .open */
25438
+ char *zInput; /* Simulated inputs */
25439
+ i64 nInputUsed; /* Simulated input read so far */
2528025440
char *zDestTable; /* Name of destination table when MODE_Insert */
2528125441
char *zTempFile; /* Temporary file that might need deleting */
2528225442
char *zErrPrefix; /* Alternative error message prefix */
2528325443
char *zAuthIgnore; /* Authorizer returns SQLITE_IGNORE for glob matches */
2528425444
char *zAuthDeny; /* Authorizer returns SQLITE_DENY for glob matches */
@@ -25317,10 +25477,17 @@
2531725477
int nArg; /* Number of argument slots actually used */
2531825478
char **azArg; /* Pointer to each argument, dequoted */
2531925479
int *aiOfst; /* Offset into zOrig[] for start of each arg */
2532025480
char *abQuot; /* True if the argment was originally quoted */
2532125481
} dot;
25482
+ u32 mIf; /* Mask of false if conditions */
25483
+ u32 mElse; /* An else seen at this level */
25484
+ u32 mIfHasBeenTrue; /* True branch seen in if-elseif-elseif-else chain */
25485
+ int nIfLevel; /* Current number of if levels */
25486
+ int nIfAtStart; /* If level at start of current script */
25487
+ i64 lnIf[MX_IF]; /* Line number starting an if */
25488
+ i64 lnElse[MX_IF]; /* Line number of the else */
2532225489
#ifdef SQLITE_SHELL_FIDDLE
2532325490
struct {
2532425491
const char * zInput; /* Input string from wasm/JS proxy */
2532525492
const char * zPos; /* Cursor pos into zInput */
2532625493
const char * zDefaultDbName; /* Default name for db file */
@@ -25629,11 +25796,11 @@
2562925796
}
2563025797
#endif
2563125798
static void cli_exit(int rc){
2563225799
if( cli_output_capture ){
2563325800
char *z = sqlite3_str_finish(cli_output_capture);
25634
- sqlite3_fputs(z, stdout);
25801
+ sqlite3_fputs(z ? z : "", stdout);
2563525802
fflush(stdout);
2563625803
}
2563725804
exit(rc);
2563825805
}
2563925806
@@ -25759,10 +25926,29 @@
2575925926
return 0;
2576025927
}
2576125928
#endif
2576225929
#undef STAT_CHR_SRC
2576325930
}
25931
+
25932
+/*
25933
+** Works like fgets except that if "in" really points to p->zInput
25934
+** then text is read out of the p->zInput buffer.
25935
+*/
25936
+static char *shell_fgets(ShellState *p, char *zOut, int nOut, FILE *in){
25937
+ int i, j;
25938
+ char *zIn;
25939
+ if( p==0 || in!=(FILE*)&p->zInput ){
25940
+ return sqlite3_fgets(zOut, nOut, in);
25941
+ }
25942
+ zIn = p->zInput;
25943
+ for(i=0, j=p->nInputUsed; i<nOut-1 && zIn[j] && zIn[j]!='\n'; i++, j++){}
25944
+ if( zIn[j] ){ i++; }
25945
+ memcpy(zOut, &zIn[p->nInputUsed], i);
25946
+ zOut[i] = 0;
25947
+ p->nInputUsed += i;
25948
+ return i>0 ? zOut : 0;
25949
+}
2576425950
2576525951
/*
2576625952
** This routine reads a line of text from FILE in, stores
2576725953
** the text in memory obtained from malloc() and returns a pointer
2576825954
** to the text. NULL is returned at end of file, or if malloc()
@@ -25769,11 +25955,11 @@
2576925955
** fails, or if the length of the line is longer than about a gigabyte.
2577025956
**
2577125957
** If zLine is not NULL then it is a malloced buffer returned from
2577225958
** a previous call to this routine that may be reused.
2577325959
*/
25774
-static char *local_getline(char *zLine, FILE *in){
25960
+static char *local_getline(ShellState *p, char *zLine, FILE *in){
2577525961
int nLine = zLine==0 ? 0 : 100;
2577625962
int n = 0;
2577725963
2577825964
while( seenInterrupt<2 ){
2577925965
if( n+100>nLine ){
@@ -25783,11 +25969,11 @@
2578325969
}
2578425970
nLine = nLine*2 + 100;
2578525971
zLine = realloc(zLine, nLine);
2578625972
shell_check_oom(zLine);
2578725973
}
25788
- if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
25974
+ if( shell_fgets(p, &zLine[n], nLine - n, in)==0 ){
2578925975
if( n==0 ){
2579025976
free(zLine);
2579125977
return 0;
2579225978
}
2579325979
zLine[n] = 0;
@@ -26397,13 +26583,12 @@
2639726583
char *zPrior, /* Just the previous line */
2639826584
const char *zAll, /* All statement text accumulated so far */
2639926585
int bContinue /* True for a continuation prompt */
2640026586
){
2640126587
char *zResult;
26402
- FILE *in = p->in;
26403
- if( in!=0 ){
26404
- zResult = local_getline(zPrior, in);
26588
+ if( p->in!=0 ){
26589
+ zResult = local_getline(p, zPrior, p->in);
2640526590
}else{
2640626591
const char *zBase = prompt_string(p, bContinue!=0);
2640726592
char *zPrompt = expand_prompt(p, zAll, zBase);
2640826593
shell_check_oom(zPrompt);
2640926594
#if SHELL_CMDLINE_EDIT_AVAILABLE
@@ -26421,11 +26606,11 @@
2642126606
#endif
2642226607
{
2642326608
sputz(stdout, zPrompt);
2642426609
fflush(stdout);
2642526610
do{
26426
- zResult = local_getline(zPrior, stdin);
26611
+ zResult = local_getline(p, zPrior, stdin);
2642726612
zPrior = 0;
2642826613
/* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
2642926614
if( zResult==0 ) sqlite3_sleep(50);
2643026615
}while( zResult==0 && seenInterrupt>0 );
2643126616
}
@@ -26637,10 +26822,76 @@
2663726822
}else{
2663826823
while( s.zTxt && (zCom = strstr(s.zTxt,"*/"))!=0 ){ zCom[1] = '*'; }
2663926824
}
2664026825
return s.zTxt;
2664126826
}
26827
+
26828
+/*
26829
+** SQL function: capable(X)
26830
+**
26831
+** Return true if the current build has capability X. Return 0
26832
+** if X is not a capability or NULL if X is NULL.
26833
+**
26834
+** This function is intended to be used with the ".if" command
26835
+** to bypass test cases that use features that are not part of
26836
+** the build. Examples:
26837
+**
26838
+** .if capable('fts5')
26839
+** # do tests that require the FTS5 extension
26840
+** .endif
26841
+**
26842
+** .if capable('generate_series')
26843
+** # do tests that require the generate_series() table-valued functions
26844
+** .endif
26845
+*/
26846
+static void shellCapable(
26847
+ sqlite3_context *pCtx,
26848
+ int nVal,
26849
+ sqlite3_value **apVal
26850
+){
26851
+ ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
26852
+ const char *z = (const char*)sqlite3_value_text(apVal[0]);
26853
+ static const char *azSql[] = {
26854
+ "PRAGMA compile_options",
26855
+ "PRAGMA module_list",
26856
+ "PRAGMA function_list"
26857
+ };
26858
+ static const char *azInversions[] = {
26859
+ "vtab", "OMIT_VIRTUALTABLE",
26860
+ "fkey", "OMIT_FOREIGN_KEY",
26861
+ "load_ext", "OMIT_LOAD_EXTENSION",
26862
+ };
26863
+ size_t i;
26864
+ int invert = 0;
26865
+ int res = 0;
26866
+
26867
+ UNUSED_PARAMETER(nVal);
26868
+ if( z==0 ) return;
26869
+ for(i=0; i<sizeof(azInversions)/sizeof(azInversions[0])-1; i+=2){
26870
+ if( sqlite3_stricmp(z, azInversions[i])==0 ){
26871
+ z = azInversions[i+1];
26872
+ invert = 1;
26873
+ break;
26874
+ }
26875
+ }
26876
+ for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){
26877
+ sqlite3_stmt *pStmt = 0;
26878
+ sqlite3_prepare_v2(p->db, azSql[i], -1, &pStmt, 0);
26879
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
26880
+ const char *zRow = (const char*)sqlite3_column_text(pStmt, 0);
26881
+ if( sqlite3_stricmp(zRow, z)==0 ){
26882
+ res = 1;
26883
+ break;
26884
+ }
26885
+ }
26886
+ sqlite3_finalize(pStmt);
26887
+ if( res ) break;
26888
+ }
26889
+ if( invert ) res = !res;
26890
+ sqlite3_result_int(pCtx, res);
26891
+}
26892
+
2664226893
2664326894
/*
2664426895
** SQL function: strtod(X)
2664526896
**
2664626897
** For use internally and for debugging and testing only. This function
@@ -29112,10 +29363,12 @@
2911229363
/* Build an appropriate SELECT statement */
2911329364
initText(&sSelect);
2911429365
appendText(&sSelect, "SELECT ", 0);
2911529366
if( azCol[0] ){
2911629367
appendText(&sSelect, azCol[0], 0);
29368
+ appendText(&sSelect, " AS ", 0);
29369
+ appendText(&sSelect, azCol[0], 0);
2911729370
appendText(&sSelect, ",", 0);
2911829371
}
2911929372
for(i=1; azCol[i]; i++){
2912029373
appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
2912129374
if( azCol[i+1] ){
@@ -29132,10 +29385,13 @@
2913229385
p->mode.eMode = MODE_Insert;
2913329386
p->mode.spec.eText = QRF_TEXT_Sql;
2913429387
p->mode.spec.eBlob = QRF_BLOB_Sql;
2913529388
p->mode.spec.bTitles = QRF_No;
2913629389
p->mode.spec.nCharLimit = 0;
29390
+ p->mode.spec.bTextJsonb = QRF_No;
29391
+ p->mode.spec.zIFmt = 0;
29392
+ p->mode.spec.zFpFmt = 0;
2913729393
rc = shell_exec(p, sSelect.zTxt, 0);
2913829394
if( (rc&0xff)==SQLITE_CORRUPT ){
2913929395
cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
2914029396
toggleSelectOrder(p->db);
2914129397
shell_exec(p, sSelect.zTxt, 0);
@@ -29470,10 +29726,21 @@
2947029726
** "Usage" help text automatically generated from comments */
2947129727
static const struct {
2947229728
const char *zCmd; /* Name of the dot-command */
2947329729
const char *zUsage; /* Documentation */
2947429730
} aUsage[] = {
29731
+ { ".clone",
29732
+"USAGE: .clone FILE\n"
29733
+"\n"
29734
+"The original purpose of the \".clone\" command (when it was first introduced\n"
29735
+"in 2014) was to try to recover content for a corrupt database file and\n"
29736
+"write the recovered content into a new database named FILE. However,\n"
29737
+"the command has always been experimental and undocumented. Today, it\n"
29738
+"does nothing more than make a copy of the main database into a new\n"
29739
+"database named by its only argument, FILE. No repair attempts are\n"
29740
+"made. The command is retained for legacy only.\n"
29741
+ },
2947529742
{ ".import",
2947629743
"USAGE: .import [OPTIONS] FILE TABLE\n"
2947729744
"\n"
2947829745
"Import CSV or similar text from FILE into TABLE. If TABLE does\n"
2947929746
"not exist, it is created using the first row of FILE as the column\n"
@@ -29677,10 +29944,41 @@
2967729944
"\".check\" command for additional informatioon.\n"
2967829945
"\n"
2967929946
"Options:\n"
2968029947
" --error-prefix TEXT Change error message prefix text to TEXT\n"
2968129948
},
29949
+ { ".if",
29950
+"USAGE: .if SQL-EXPRESSION\n"
29951
+"\n"
29952
+"The argument can be any SQL expression, including a subquery in\n"
29953
+"parentheses. If the expression evalutes to true (if it is non-zero\n"
29954
+"when converted into an number) then subsequent statements are\n"
29955
+"evaluated. If SQL-EXPRESSION evalutes to false or NULL or raises\n"
29956
+"an error, then subsequent statements are skipped, up until the first\n"
29957
+"matching .elseif, .else, or .endif.\n"
29958
+"\n"
29959
+"Maximum nested depth is limited. An error is raised if .if statements\n"
29960
+"are nested to deep.\n"
29961
+"\n"
29962
+"Every .if must be followed by a matching .endif, or an error is raised.\n"
29963
+ },
29964
+ { ".elseif",
29965
+"USAGE: .elseif SQL-EXPRESSION\n"
29966
+"\n"
29967
+"An \"else-if\". The prior .if is finished and a new condition is applied.\n"
29968
+ },
29969
+ { ".else",
29970
+"USAGE: .else\n"
29971
+"\n"
29972
+"Invert the sense of the inner-most .if. Start processing if the prior\n"
29973
+".if was false. Stop processing if the prior .if was true.\n"
29974
+ },
29975
+ { ".endif",
29976
+"USAGE: .endif\n"
29977
+"\n"
29978
+"Terminate an .if statement.\n"
29979
+ },
2968229980
};
2968329981
2968429982
/*
2968529983
** Return a pointer to usage text for zCmd, or NULL if none exists.
2968629984
*/
@@ -29834,20 +30132,25 @@
2983430132
cli_printf(stderr,"Error: '%s' not seekable\n", zName);
2983530133
fclose(in);
2983630134
return 0;
2983730135
}
2983830136
nIn = ftell(in);
30137
+ if( nIn<0 ){
30138
+ cli_printf(stderr,"Error: cannot read '%s'\n", zName);
30139
+ fclose(in);
30140
+ return 0;
30141
+ }
2983930142
rewind(in);
2984030143
pBuf = sqlite3_malloc64( nIn+1 );
2984130144
if( pBuf==0 ){
2984230145
cli_puts("Error: out of memory\n", stderr);
2984330146
fclose(in);
2984430147
return 0;
2984530148
}
29846
- nRead = fread(pBuf, nIn, 1, in);
30149
+ nRead = fread(pBuf, 1, nIn, in);
2984730150
fclose(in);
29848
- if( nRead!=1 ){
30151
+ if( nRead!=(size_t)nIn ){
2984930152
sqlite3_free(pBuf);
2985030153
cli_printf(stderr,"Error: cannot read '%s'\n", zName);
2985130154
return 0;
2985230155
}
2985330156
pBuf[nIn] = 0;
@@ -30060,11 +30363,11 @@
3006030363
nLine = p->lineno;
3006130364
if( in==0 ) in = stdin;
3006230365
}
3006330366
*pnData = 0;
3006430367
nLine++;
30065
- if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
30368
+ if( shell_fgets(p, zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
3006630369
rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
3006730370
if( rc!=2 ) goto readHexDb_error;
3006830371
if( n<0 ) goto readHexDb_error;
3006930372
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
3007030373
cli_puts("invalid pagesize\n", stderr);
@@ -30072,11 +30375,11 @@
3007230375
}
3007330376
sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
3007430377
a = sqlite3_malloc64( sz ? sz : 1 );
3007530378
shell_check_oom(a);
3007630379
memset(a, 0, sz);
30077
- for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
30380
+ for(nLine++; shell_fgets(p, zLine, sizeof(zLine), in)!=0; nLine++){
3007830381
int j = 0; /* Page number from "| page" line */
3007930382
int k = 0; /* Offset from "| page" line */
3008030383
if( nLine>=2000000000 ){
3008130384
cli_printf(stderr, "input too big\n");
3008230385
goto readHexDb_error;
@@ -30110,11 +30413,11 @@
3011030413
3011130414
readHexDb_error:
3011230415
if( in!=p->in ){
3011330416
fclose(in);
3011430417
}else{
30115
- while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
30418
+ while( shell_fgets(p, zLine, sizeof(zLine), p->in)!=0 ){
3011630419
nLine++;
3011730420
if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
3011830421
}
3011930422
p->lineno = nLine;
3012030423
}
@@ -30336,10 +30639,12 @@
3033630639
#undef SHELL_SUB_MACRO
3033730640
#undef SHELL_SUBMACRO
3033830641
}
3033930642
#endif
3034030643
30644
+ sqlite3_create_function(p->db, "capable", 1, SQLITE_UTF8, p,
30645
+ shellCapable, 0, 0);
3034130646
sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
3034230647
shellStrtod, 0, 0);
3034330648
sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
3034430649
shellDtostr, 0, 0);
3034530650
sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
@@ -30912,227 +31217,48 @@
3091231217
p->cTerm = c;
3091331218
if( p->z ) p->z[p->n] = 0;
3091431219
return p->z;
3091531220
}
3091631221
30917
-/*
30918
-** Try to transfer data for table zTable. If an error is seen while
30919
-** moving forward, try to go backwards. The backwards movement won't
30920
-** work for WITHOUT ROWID tables.
30921
-*/
30922
-static void tryToCloneData(
30923
- ShellState *p,
30924
- sqlite3 *newDb,
30925
- const char *zTable
30926
-){
30927
- sqlite3_stmt *pQuery = 0;
30928
- sqlite3_stmt *pInsert = 0;
30929
- char *zQuery = 0;
30930
- char *zInsert = 0;
30931
- int rc;
30932
- int i, j, n;
30933
- int nTable = strlen30(zTable);
30934
- int k = 0;
30935
- int cnt = 0;
30936
- const int spinRate = 10000;
30937
-
30938
- zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
30939
- shell_check_oom(zQuery);
30940
- rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
30941
- if( rc ){
30942
- cli_printf(stderr,"Error %d: %s on [%s]\n",
30943
- sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
30944
- goto end_data_xfer;
30945
- }
30946
- n = sqlite3_column_count(pQuery);
30947
- zInsert = sqlite3_malloc64(200 + nTable + n*3);
30948
- shell_check_oom(zInsert);
30949
- sqlite3_snprintf(200+nTable,zInsert,
30950
- "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
30951
- i = strlen30(zInsert);
30952
- for(j=1; j<n; j++){
30953
- memcpy(zInsert+i, ",?", 2);
30954
- i += 2;
30955
- }
30956
- memcpy(zInsert+i, ");", 3);
30957
- rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
30958
- if( rc ){
30959
- cli_printf(stderr,"Error %d: %s on [%s]\n",
30960
- sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
30961
- goto end_data_xfer;
30962
- }
30963
- for(k=0; k<2; k++){
30964
- while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
30965
- for(i=0; i<n; i++){
30966
- switch( sqlite3_column_type(pQuery, i) ){
30967
- case SQLITE_NULL: {
30968
- sqlite3_bind_null(pInsert, i+1);
30969
- break;
30970
- }
30971
- case SQLITE_INTEGER: {
30972
- sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
30973
- break;
30974
- }
30975
- case SQLITE_FLOAT: {
30976
- sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
30977
- break;
30978
- }
30979
- case SQLITE_TEXT: {
30980
- sqlite3_bind_text(pInsert, i+1,
30981
- (const char*)sqlite3_column_text(pQuery,i),
30982
- -1, SQLITE_STATIC);
30983
- break;
30984
- }
30985
- case SQLITE_BLOB: {
30986
- sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
30987
- sqlite3_column_bytes(pQuery,i),
30988
- SQLITE_STATIC);
30989
- break;
30990
- }
30991
- }
30992
- } /* End for */
30993
- rc = sqlite3_step(pInsert);
30994
- if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
30995
- cli_printf(stderr,"Error %d: %s\n",
30996
- sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
30997
- }
30998
- sqlite3_reset(pInsert);
30999
- cnt++;
31000
- if( (cnt%spinRate)==0 ){
31001
- printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
31002
- fflush(stdout);
31003
- }
31004
- } /* End while */
31005
- if( rc==SQLITE_DONE ) break;
31006
- sqlite3_finalize(pQuery);
31007
- sqlite3_free(zQuery);
31008
- zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
31009
- zTable);
31010
- shell_check_oom(zQuery);
31011
- rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31012
- if( rc ){
31013
- cli_printf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
31014
- break;
31015
- }
31016
- } /* End for(k=0...) */
31017
-
31018
-end_data_xfer:
31019
- sqlite3_finalize(pQuery);
31020
- sqlite3_finalize(pInsert);
31021
- sqlite3_free(zQuery);
31022
- sqlite3_free(zInsert);
31023
-}
31024
-
31025
-
31026
-/*
31027
-** Try to transfer all rows of the schema that match zWhere. For
31028
-** each row, invoke xForEach() on the object defined by that row.
31029
-** If an error is encountered while moving forward through the
31030
-** sqlite_schema table, try again moving backwards.
31031
-*/
31032
-static void tryToCloneSchema(
31033
- ShellState *p,
31034
- sqlite3 *newDb,
31035
- const char *zWhere,
31036
- void (*xForEach)(ShellState*,sqlite3*,const char*)
31037
-){
31038
- sqlite3_stmt *pQuery = 0;
31039
- char *zQuery = 0;
31040
- int rc;
31041
- const unsigned char *zName;
31042
- const unsigned char *zSql;
31043
- char *zErrMsg = 0;
31044
-
31045
- zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
31046
- " WHERE %s ORDER BY rowid ASC", zWhere);
31047
- shell_check_oom(zQuery);
31048
- rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31049
- if( rc ){
31050
- cli_printf(stderr,
31051
- "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
31052
- sqlite3_errmsg(p->db), zQuery);
31053
- goto end_schema_xfer;
31054
- }
31055
- while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
31056
- zName = sqlite3_column_text(pQuery, 0);
31057
- zSql = sqlite3_column_text(pQuery, 1);
31058
- if( zName==0 || zSql==0 ) continue;
31059
- if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
31060
- cli_printf(stdout, "%s... ", zName); fflush(stdout);
31061
- sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
31062
- if( zErrMsg ){
31063
- cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
31064
- sqlite3_free(zErrMsg);
31065
- zErrMsg = 0;
31066
- }
31067
- }
31068
- if( xForEach ){
31069
- xForEach(p, newDb, (const char*)zName);
31070
- }
31071
- sputz(stdout, "done\n");
31072
- }
31073
- if( rc!=SQLITE_DONE ){
31074
- sqlite3_finalize(pQuery);
31075
- sqlite3_free(zQuery);
31076
- zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
31077
- " WHERE %s ORDER BY rowid DESC", zWhere);
31078
- shell_check_oom(zQuery);
31079
- rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31080
- if( rc ){
31081
- cli_printf(stderr,"Error: (%d) %s on [%s]\n",
31082
- sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
31083
- goto end_schema_xfer;
31084
- }
31085
- while( sqlite3_step(pQuery)==SQLITE_ROW ){
31086
- zName = sqlite3_column_text(pQuery, 0);
31087
- zSql = sqlite3_column_text(pQuery, 1);
31088
- if( zName==0 || zSql==0 ) continue;
31089
- if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
31090
- cli_printf(stdout, "%s... ", zName); fflush(stdout);
31091
- sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
31092
- if( zErrMsg ){
31093
- cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
31094
- sqlite3_free(zErrMsg);
31095
- zErrMsg = 0;
31096
- }
31097
- if( xForEach ){
31098
- xForEach(p, newDb, (const char*)zName);
31099
- }
31100
- sputz(stdout, "done\n");
31101
- }
31102
- }
31103
-end_schema_xfer:
31104
- sqlite3_finalize(pQuery);
31105
- sqlite3_free(zQuery);
31106
-}
31107
-
31108
-/*
31109
-** Open a new database file named "zNewDb". Try to recover as much information
31110
-** as possible out of the main database (which might be corrupt) and write it
31111
-** into zNewDb.
31112
-*/
31113
-static void tryToClone(ShellState *p, const char *zNewDb){
31114
- int rc;
31115
- sqlite3 *newDb = 0;
31116
- if( access(zNewDb,0)==0 ){
31117
- cli_printf(stderr,"File \"%s\" already exists.\n", zNewDb);
31118
- return;
31119
- }
31120
- rc = sqlite3_open(zNewDb, &newDb);
31121
- if( rc ){
31122
- cli_printf(stderr,
31123
- "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
31124
- }else{
31125
- sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
31126
- sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
31127
- tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
31128
- tryToCloneSchema(p, newDb, "type!='table'", 0);
31129
- sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
31130
- sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
31131
- }
31132
- close_db(newDb);
31133
-}
31222
+#ifndef SQLITE_SHELL_FIDDLE
31223
+/*
31224
+** DOT-COMMAND: .clone
31225
+**
31226
+** USAGE: .clone FILE
31227
+**
31228
+** The original purpose of the ".clone" command (when it was first introduced
31229
+** in 2014) was to try to recover content for a corrupt database file and
31230
+** write the recovered content into a new database named FILE. However,
31231
+** the command has always been experimental and undocumented. Today, it
31232
+** does nothing more than make a copy of the main database into a new
31233
+** database named by its only argument, FILE. No repair attempts are
31234
+** made. The command is retained for legacy only.
31235
+*/
31236
+static int dotCmdClone(ShellState *p){
31237
+ int nArg = p->dot.nArg; /* Number of arguments */
31238
+ char **azArg = p->dot.azArg;/* Argument list */
31239
+ char *zSql;
31240
+ sqlite3_stmt *pStmt = 0;
31241
+ int rc;
31242
+
31243
+ failIfSafeMode(p, "cannot run .clone in safe mode");
31244
+ if( nArg!=2 ){
31245
+ dotCmdError(p, nArg, 0, "missing FILE argument");
31246
+ return 1;
31247
+ }
31248
+ zSql = sqlite3_mprintf("VACUUM INTO %Q", azArg[1]);
31249
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
31250
+ sqlite3_free(zSql);
31251
+ if( rc!=SQLITE_OK || SQLITE_DONE!=sqlite3_step(pStmt) ){
31252
+ cli_printf(stderr,"Error: cannot clone into \"%s\"\n", azArg[1]);
31253
+ sqlite3_finalize(pStmt);
31254
+ return 1;
31255
+ }
31256
+ sqlite3_finalize(pStmt);
31257
+ return 0;
31258
+}
31259
+#endif /* !defined(SQLITE_SHELL_FIDDLE) */
3113431260
3113531261
#ifndef SQLITE_SHELL_FIDDLE
3113631262
/*
3113731263
** Change the output stream (file or pipe or console) to something else.
3113831264
*/
@@ -32493,11 +32619,11 @@
3249332619
arWhereClause(&rc, pAr, &zWhere);
3249432620
3249532621
shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
3249632622
pAr->zSrcTable, zWhere);
3249732623
if( pAr->bDryRun ){
32498
- cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
32624
+ if( pSql ) cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
3249932625
}else{
3250032626
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
3250132627
if( pAr->bVerbose ){
3250232628
cli_printf(pAr->out, "%s % 10d %s %s\n",
3250332629
sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
@@ -33553,11 +33679,11 @@
3355333679
i64 iStart = p->lineno;
3355433680
char zLine[2000];
3355533681
sCtx.zFile = p->zInFile;
3355633682
sCtx.nLine = p->lineno+1;
3355733683
iLineOffset = p->lineno;
33558
- while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
33684
+ while( shell_fgets(p, zLine,sizeof(zLine),p->in) ){
3355933685
if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){
3356033686
ckEnd = 2;
3356133687
if( strchr(zLine,'\n') ) p->lineno++;
3356233688
break;
3356333689
}
@@ -34627,11 +34753,11 @@
3462734753
dotCmdError(p, i, "incompatible with prior options",0);
3462834754
goto dotCmdOutput_error;
3462934755
}
3463034756
eMode = z[1];
3463134757
}else if( cli_strcmp(z,"-show")==0 ){
34632
- if( cli_output_capture ){
34758
+ if( sqlite3_str_length(cli_output_capture)>0 ){
3463334759
sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
3463434760
}
3463534761
}else if( cli_strcmp(z,"-keep")==0 ){
3463634762
bKeep = 1;
3463734763
}else if( optionMatch(z,"error-prefix") ){
@@ -34762,10 +34888,34 @@
3476234888
3476334889
dotCmdOutput_error:
3476434890
sqlite3_free(zFile);
3476534891
return 1;
3476634892
}
34893
+
34894
+/*
34895
+** Return true if the argument is <<MARK for some arbitrary mark
34896
+*/
34897
+static int isHereDoc(const char *z){
34898
+ return z[0]=='<' && z[1]=='<' && z[2]!=0;
34899
+}
34900
+
34901
+/*
34902
+** Capture lines of input text up until a line that matches zTerminator.
34903
+** Return a memory buffer (allocated using sqlite3_malloc64()) that
34904
+** contains the complete text, without the terminator.
34905
+*/
34906
+static char *captureHereDoc(ShellState *p, const char *zTerminator){
34907
+ int nTerminator = strlen30(zTerminator);
34908
+ sqlite3_str *pHeredoc = sqlite3_str_new(p->db);
34909
+ char zLine[2000];
34910
+ while( shell_fgets(p, zLine,sizeof(zLine),p->in) ){
34911
+ if( strchr(zLine,'\n') ) p->lineno++;
34912
+ if( cli_strncmp(zTerminator,zLine,nTerminator)==0 ) break;
34913
+ sqlite3_str_appendall(pHeredoc, zLine);
34914
+ }
34915
+ return sqlite3_str_finish(pHeredoc);
34916
+}
3476734917
3476834918
/*
3476934919
** DOT-COMMAND: .check
3477034920
** USAGE: .check [OPTIONS] PATTERN
3477134921
**
@@ -34796,10 +34946,19 @@
3479634946
char *zPattern = 0; /* Actual test pattern */
3479734947
int eCheck = 0; /* 1: --glob, 2: --notglob, 3: --exact */
3479834948
int isOk; /* True if results are OK */
3479934949
sqlite3_int64 iStart = p->lineno; /* Line number of .check statement */
3480034950
34951
+ if( p->mIf!=0 ){
34952
+ /* Unlike most other dot-commands, the .check command runs even if
34953
+ ** inside the false branch of a .if. It needs to do this so that it
34954
+ ** can capture any "<<MARKER" heredoc text specified. */
34955
+ if( nArg>0 && isHereDoc(azArg[nArg-1]) ){
34956
+ sqlite3_free(captureHereDoc(p, azArg[nArg-1]+2));
34957
+ }
34958
+ return 0;
34959
+ }
3480134960
if( p->zTestcase[0]==0 ){
3480234961
dotCmdError(p, 0, "no .testcase is active", 0);
3480334962
return 1;
3480434963
}
3480534964
for(i=1; i<nArg; i++){
@@ -34806,11 +34965,11 @@
3480634965
char *z = azArg[i];
3480734966
if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
3480834967
if( cli_strcmp(z,"-keep")==0 ){
3480934968
bKeep = 1;
3481034969
}else if( cli_strcmp(z,"-show")==0 ){
34811
- if( cli_output_capture ){
34970
+ if( sqlite3_str_length(cli_output_capture)>0 ){
3481234971
sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
3481334972
}
3481434973
bKeep = 1;
3481534974
}else if( z[0]=='-'
3481634975
&& (k = pickStr(&z[1],0,"glob","notglob","exact",""))>=0
@@ -34829,27 +34988,19 @@
3482934988
}
3483034989
if( zCheck==0 ){
3483134990
dotCmdError(p, 0, "no PATTERN specified", 0);
3483234991
return 1;
3483334992
}
34834
- if( cli_output_capture && sqlite3_str_length(cli_output_capture) ){
34993
+ if( sqlite3_str_length(cli_output_capture) ){
3483534994
zTest = sqlite3_str_value(cli_output_capture);
3483634995
shell_check_oom(zTest);
3483734996
}else{
3483834997
zTest = "";
3483934998
}
3484034999
p->nTestRun++;
34841
- if( zCheck[0]=='<' && zCheck[1]=='<' && zCheck[2]!=0 ){
34842
- int nCheck = strlen30(zCheck);
34843
- sqlite3_str *pPattern = sqlite3_str_new(p->db);
34844
- char zLine[2000];
34845
- while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
34846
- if( strchr(zLine,'\n') ) p->lineno++;
34847
- if( cli_strncmp(&zCheck[2],zLine,nCheck-2)==0 ) break;
34848
- sqlite3_str_appendall(pPattern, zLine);
34849
- }
34850
- zPattern = sqlite3_str_finish(pPattern);
35000
+ if( isHereDoc(zCheck) ){
35001
+ zPattern = captureHereDoc(p, zCheck+2);
3485135002
if( zPattern==0 ){
3485235003
zPattern = sqlite3_mprintf("");
3485335004
}
3485435005
}else{
3485535006
zPattern = zCheck;
@@ -35042,10 +35193,147 @@
3504235193
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s:%lld",
3504335194
p->zInFile, p->lineno);
3504435195
}
3504535196
return 0;
3504635197
}
35198
+
35199
+/*
35200
+** Evaluate the text of zExpr as an SQL expression. Return true if the
35201
+** expression is true. Return false if the expression evaluates to
35202
+** false or NULL. Return false if there is any kind of error, either
35203
+** a run-time or a compile-time error.
35204
+*/
35205
+static int sqlExprIsTrue(ShellState *p, const char *zExpr){
35206
+ char *zSql = sqlite3_mprintf("SELECT %s", zExpr);
35207
+ sqlite3_stmt *pStmt;
35208
+ int rc;
35209
+
35210
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
35211
+ sqlite3_free(zSql);
35212
+ if( rc ){
35213
+ rc = 0;
35214
+ }else{
35215
+ bind_prepared_stmt(p, pStmt);
35216
+ rc = sqlite3_step(pStmt);
35217
+ if( rc==SQLITE_ROW ){
35218
+ rc = sqlite3_column_double(pStmt,0)!=0.0;
35219
+ }else{
35220
+ rc = 0;
35221
+ }
35222
+ }
35223
+ sqlite3_finalize(pStmt);
35224
+ return rc;
35225
+}
35226
+
35227
+/*
35228
+** DOT-COMMAND: .if
35229
+** USAGE: .if SQL-EXPRESSION
35230
+**
35231
+** The argument can be any SQL expression, including a subquery in
35232
+** parentheses. If the expression evalutes to true (if it is non-zero
35233
+** when converted into an number) then subsequent statements are
35234
+** evaluated. If SQL-EXPRESSION evalutes to false or NULL or raises
35235
+** an error, then subsequent statements are skipped, up until the first
35236
+** matching .elseif, .else, or .endif.
35237
+**
35238
+** Maximum nested depth is limited. An error is raised if .if statements
35239
+** are nested to deep.
35240
+**
35241
+** Every .if must be followed by a matching .endif, or an error is raised.
35242
+*/
35243
+static int dotCmdIf(ShellState *p){
35244
+ int jj;
35245
+ if( p->nIfLevel==MX_IF ){
35246
+ dotCmdError(p, 0, 0, ".if nested too deep");
35247
+ return 1;
35248
+ }
35249
+ jj = p->nIfLevel++;
35250
+ open_db(p, 0);
35251
+ if( sqlExprIsTrue(p, p->dot.zOrig+3) ){
35252
+ p->mIfHasBeenTrue |= 1<<jj;
35253
+ }else{
35254
+ p->mIf |= 1<<jj;
35255
+ }
35256
+ p->lnIf[jj] = p->lineno;
35257
+ return 0;
35258
+}
35259
+
35260
+/*
35261
+** DOT-COMMAND: .elseif
35262
+** USAGE: .elseif SQL-EXPRESSION
35263
+**
35264
+** An "else-if". The prior .if is finished and a new condition is applied.
35265
+*/
35266
+static int dotCmdElseif(ShellState *p){
35267
+ int jj = p->nIfLevel-1;
35268
+ if( jj<p->nIfAtStart ){
35269
+ dotCmdError(p, 0, 0, ".elseif with no .if");
35270
+ return 1;
35271
+ }
35272
+ if( (p->mElse & (1<<jj))!=0 ){
35273
+ dotCmdError(p, 0, 0, ".elseif follows a .else on line %lld", p->lnElse[jj]);
35274
+ p->mIf |= 1<<jj;
35275
+ return 1;
35276
+ }
35277
+ if( p->mIfHasBeenTrue & (1<<jj) ){
35278
+ p->mIf |= 1<<jj;
35279
+ }else if( sqlExprIsTrue(p, p->dot.zOrig+7) ){
35280
+ p->mIf &= ~(1<<jj);
35281
+ p->mIfHasBeenTrue |= (1<<jj);
35282
+ }else{
35283
+ p->mIf |= 1<<jj;
35284
+ }
35285
+ return 0;
35286
+}
35287
+
35288
+/*
35289
+** DOT-COMMAND: .else
35290
+** USAGE: .else
35291
+**
35292
+** Invert the sense of the inner-most .if. Start processing if the prior
35293
+** .if was false. Stop processing if the prior .if was true.
35294
+*/
35295
+static int dotCmdElse(ShellState *p){
35296
+ int ii;
35297
+ if( p->nIfLevel<=p->nIfAtStart ){
35298
+ dotCmdError(p, 0, 0, ".else without a .if");
35299
+ return 1;
35300
+ }
35301
+ ii = p->nIfLevel-1;
35302
+ if( (p->mElse & (1<<ii))!=0 ){
35303
+ dotCmdError(p, 0, 0, "already saw a .else at line %lld", p->lnElse[ii]);
35304
+ return 1;
35305
+ }
35306
+ if( p->mIfHasBeenTrue & (1<<ii) ){
35307
+ p->mIf |= (1<<ii);
35308
+ }else{
35309
+ p->mIf &= ~(1<<ii);
35310
+ }
35311
+ p->mElse |= (1<<ii);
35312
+ p->lnElse[ii] = p->lineno;
35313
+ return 0;
35314
+}
35315
+
35316
+/*
35317
+** DOT-COMMAND: .endif
35318
+** USAGE: .endif
35319
+**
35320
+** Terminate an .if statement.
35321
+*/
35322
+static int dotCmdEndif(ShellState *p){
35323
+ if( p->nIfLevel<=p->nIfAtStart ){
35324
+ dotCmdError(p, 0, 0, ".endif without a .if");
35325
+ return 1;
35326
+ }
35327
+ p->nIfLevel--;
35328
+ p->mIf &= ~(1<<p->nIfLevel);
35329
+ p->mElse &= ~(1<<p->nIfLevel);
35330
+ p->mIfHasBeenTrue &= ~(1<<p->nIfLevel);
35331
+ return 0;
35332
+}
35333
+
35334
+
3504735335
3504835336
/*
3504935337
** Enlarge the space allocated in p->dot so that it can hold more
3505035338
** than nArg parsed command-line arguments.
3505135339
*/
@@ -35348,17 +35636,11 @@
3534835636
rc = dotCmdCheck(p);
3534935637
}else
3535035638
3535135639
#ifndef SQLITE_SHELL_FIDDLE
3535235640
if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
35353
- failIfSafeMode(p, "cannot run .clone in safe mode");
35354
- if( nArg==2 ){
35355
- tryToClone(p, azArg[1]);
35356
- }else{
35357
- eputz("Usage: .clone FILENAME\n");
35358
- rc = 1;
35359
- }
35641
+ rc = dotCmdClone(p);
3536035642
}else
3536135643
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
3536235644
3536335645
if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
3536435646
if( nArg==1 ){
@@ -35667,10 +35949,12 @@
3566735949
cli_puts(p->nErr?";\nROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
3566835950
}
3566935951
p->shellFlgs = savedShellFlags;
3567035952
modeFree(&p->mode);
3567135953
p->mode = saved_mode;
35954
+ if( p->mode.spec.zFpFmt ) p->mode.spec.zFpFmt = p->mode.zFpFmt;
35955
+ if( p->mode.spec.zIFmt ) p->mode.spec.zIFmt = p->mode.zIFmt;
3567235956
rc = p->nErr>0;
3567335957
}else
3567435958
3567535959
if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
3567635960
if( nArg==2 ){
@@ -35682,10 +35966,22 @@
3568235966
}else{
3568335967
eputz("Usage: .echo on|off\n");
3568435968
rc = 1;
3568535969
}
3568635970
}else
35971
+
35972
+ if( c=='e' && n==6 && cli_strcmp(azArg[0],"elseif")==0 ){
35973
+ rc = dotCmdElseif(p);
35974
+ }else
35975
+
35976
+ if( c=='e' && n==4 && cli_strcmp(azArg[0],"else")==0 ){
35977
+ rc = dotCmdElse(p);
35978
+ }else
35979
+
35980
+ if( c=='e' && n==5 && cli_strcmp(azArg[0],"endif")==0 ){
35981
+ rc = dotCmdEndif(p);
35982
+ }else
3568735983
3568835984
if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
3568935985
if( nArg==2 ){
3569035986
if( p->mode.autoEQPtrace ){
3569135987
if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
@@ -35981,10 +36277,14 @@
3598136277
}
3598236278
}else{
3598336279
showHelp(p->out, 0);
3598436280
}
3598536281
}else
36282
+
36283
+ if( c=='i' && n==2 && cli_strcmp(azArg[0],"if")==0 ){
36284
+ rc = dotCmdIf(p);
36285
+ }else
3598636286
3598736287
#ifndef SQLITE_SHELL_FIDDLE
3598836288
if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
3598936289
rc = dotCmdImport(p);
3599036290
}else
@@ -36238,10 +36538,11 @@
3623836538
{ "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
3623936539
{ "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
3624036540
{ "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
3624136541
{ "schema", SQLITE_LIMIT_SCHEMA },
3624236542
{ "trigger_steps", SQLITE_LIMIT_TRIGGER_STEPS },
36543
+ { "ctes", SQLITE_LIMIT_CTES },
3624336544
};
3624436545
int i, n2;
3624536546
open_db(p, 0);
3624636547
if( nArg==1 ){
3624736548
for(i=0; i<ArraySize(aLimit); i++){
@@ -36272,12 +36573,12 @@
3627236573
azArg[1]);
3627336574
rc = 1;
3627436575
goto meta_command_exit;
3627536576
}
3627636577
if( nArg==3 ){
36277
- sqlite3_limit(p->db, aLimit[iLimit].limitCode,
36278
- (int)integerValue(azArg[2]));
36578
+ int v = (int)integerValue(azArg[2]);
36579
+ sqlite3_limit(p->db, aLimit[iLimit].limitCode, v);
3627936580
}else{
3628036581
cli_printf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
3628136582
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
3628236583
}
3628336584
}
@@ -36729,19 +37030,27 @@
3672937030
rc = 2;
3673037031
}else
3673137032
#endif
3673237033
3673337034
#ifndef SQLITE_SHELL_FIDDLE
36734
- if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
37035
+ if( c=='r' && n==4 && cli_strcmp(azArg[0], "read")==0 ){
3673537036
FILE *inSaved = p->in;
3673637037
i64 savedLineno = p->lineno;
36737
- failIfSafeMode(p, "cannot run .read in safe mode");
37038
+ if( p->mIf ){
37039
+ if( nArg==2 && isHereDoc(azArg[1]) ){
37040
+ sqlite3_free(captureHereDoc(p, azArg[1]+2));
37041
+ }
37042
+ return 0;
37043
+ }
3673837044
if( nArg!=2 ){
3673937045
eputz("Usage: .read FILE\n");
3674037046
rc = 1;
3674137047
goto meta_command_exit;
3674237048
}
37049
+ if( !isHereDoc(azArg[1]) ){
37050
+ failIfSafeMode(p, "cannot run .read in safe mode");
37051
+ }
3674337052
if( azArg[1][0]=='|' ){
3674437053
#ifdef SQLITE_OMIT_POPEN
3674537054
eputz("Error: pipes are not supported in this OS\n");
3674637055
rc = 1;
3674737056
#else
@@ -36752,10 +37061,24 @@
3675237061
}else{
3675337062
rc = process_input(p, "<pipe>");
3675437063
pclose(p->in);
3675537064
}
3675637065
#endif
37066
+ }else if( isHereDoc(azArg[1]) ){
37067
+ char *saved_zInput;
37068
+ i64 saved_nInputUsed;
37069
+ char *zScript = captureHereDoc(p, azArg[1]+2);
37070
+ savedLineno = p->lineno;
37071
+ saved_zInput = p->zInput;
37072
+ saved_nInputUsed = p->nInputUsed;
37073
+ p->zInput = zScript;
37074
+ p->nInputUsed = 0;
37075
+ p->in = (FILE*)&p->zInput;
37076
+ rc = process_input(p, "<heredoc>");
37077
+ sqlite3_free(p->zInput);
37078
+ p->zInput = saved_zInput;
37079
+ p->nInputUsed = saved_nInputUsed;
3675737080
}else if( (p->in = openChrSource(azArg[1]))==0 ){
3675837081
cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
3675937082
rc = 1;
3676037083
}else{
3676137084
char *zFilename = strdup(azArg[1]);
@@ -38518,14 +38841,14 @@
3851838841
}else{
3851938842
zPre[nPre++] = c;
3852038843
}
3852138844
}
3852238845
zPre[nPre] = 0;
38523
- j = n - 20;
38524
- if( j<i ){
38846
+ if( n<=i+20 ){
3852538847
j = i;
3852638848
}else{
38849
+ j = n - 20;
3852738850
memcpy(zSuf," ... ", 5);
3852838851
nSuf = 5;
3852938852
}
3853038853
for(i=j; i<n; i++){
3853138854
char c = zSql[i];
@@ -38680,10 +39003,34 @@
3868039003
if( x==0x00 || x==0x10 ) return 1;
3868139004
}
3868239005
return 0;
3868339006
}
3868439007
39008
+/*
39009
+** Return true if zLine is a dot-command that should always be run
39010
+** even if inside of the false leg of an ".if"
39011
+*/
39012
+static int alwaysRun(const char *zLine){
39013
+ const char *az[] = {
39014
+ "3.if",
39015
+ "7.elseif",
39016
+ "5.else",
39017
+ "6.endif",
39018
+ "6.check", /* For its heredoc */
39019
+ "5.read", /* For its heredoc */
39020
+ "0"
39021
+ };
39022
+ int jj, n;
39023
+ for(jj=0; (n = az[jj][0] - '0')>0; jj++){
39024
+ if( strncmp(zLine,az[jj]+1,n)==0 && (IsSpace(zLine[n]) || zLine[n]==0) ){
39025
+ return 1;
39026
+ }
39027
+ }
39028
+ return 0;
39029
+}
39030
+
39031
+
3868539032
/*
3868639033
** Read input from *in and process it. If *in==0 then input
3868739034
** is interactive - the user is typing it it. Otherwise, input
3868839035
** is coming from a file or device. A prompt is issued and history
3868939036
** is saved only if input is interactive. An interrupt signal will
@@ -38701,10 +39048,11 @@
3870139048
int errCnt = 0; /* Number of errors seen */
3870239049
int hasSemi = 0; /* Input line contains a semicolon */
3870339050
i64 startline = 0; /* Line number for start of current input */
3870439051
const char *saved_zInFile; /* Prior value of p->zInFile */
3870539052
i64 saved_lineno; /* Prior value of p->lineno */
39053
+ int saved_nIfAtStart; /* Prior value of p->nIfAtStart */
3870639054
3870739055
if( p->inputNesting==MAX_INPUT_NESTING ){
3870839056
/* This will be more informative in a later version. */
3870939057
cli_printf(stderr,"%s: Input nesting limit (%d) reached at line %lld."
3871039058
" Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno);
@@ -38713,10 +39061,12 @@
3871339061
++p->inputNesting;
3871439062
saved_zInFile = p->zInFile;
3871539063
p->zInFile = zSrc;
3871639064
saved_lineno = p->lineno;
3871739065
p->lineno = 0;
39066
+ saved_nIfAtStart = p->nIfAtStart;
39067
+ p->nIfAtStart = p->nIfLevel;
3871839068
while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
3871939069
fflush(p->out);
3872039070
zLine = one_input_line(p, zLine, zSql, nSql>0);
3872139071
if( zLine==0 ){
3872239072
/* End of input */
@@ -38734,15 +39084,18 @@
3873439084
continue;
3873539085
}
3873639086
if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
3873739087
echo_group_input(p, zLine);
3873839088
if( zLine[0]=='.' ){
38739
- rc = do_meta_command(zLine, p);
38740
- if( rc==2 ){ /* exit requested */
38741
- break;
38742
- }else if( rc ){
38743
- errCnt++;
39089
+ if( p->mIf==0 || alwaysRun(zLine) ){
39090
+ rc = do_meta_command(zLine, p);
39091
+ if( rc==2 ){ /* exit requested */
39092
+ p->nIfLevel = p->nIfAtStart;
39093
+ break;
39094
+ }else if( rc ){
39095
+ errCnt++;
39096
+ }
3874439097
}
3874539098
}
3874639099
continue;
3874739100
}
3874839101
#ifdef SQLITE_SHELL_LEGACY_COMMAND_TERMINATOR
@@ -38779,11 +39132,13 @@
3877939132
errCnt++;
3878039133
break;
3878139134
}
3878239135
if( nSql && hasSemi && line_is_complete(zSql, nSql) ){
3878339136
echo_group_input(p, zSql);
38784
- errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39137
+ if( p->mIf==0 ){
39138
+ errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39139
+ }
3878539140
nSql = 0;
3878639141
if( p->nPopOutput ){
3878739142
output_reset(p);
3878839143
p->nPopOutput = 0;
3878939144
}else{
@@ -38800,17 +39155,28 @@
3880039155
}
3880139156
}
3880239157
if( nSql ){
3880339158
/* This may be incomplete. Let the SQL parser deal with that. */
3880439159
echo_group_input(p, zSql);
38805
- errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39160
+ if( p->mIf==0 ){
39161
+ errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39162
+ }
3880639163
}
3880739164
free(zSql);
3880839165
free(zLine);
3880939166
--p->inputNesting;
39167
+ while( p->nIfLevel > p->nIfAtStart ){
39168
+ int ii = --p->nIfLevel;
39169
+ cli_printf(stderr,"%s:%lld: unterminated .if\n", zSrc, p->lnIf[ii]);
39170
+ p->mIf &= ~(1<<ii);
39171
+ p->mElse &= ~(1<<ii);
39172
+ p->mIfHasBeenTrue &= ~(1<<ii);
39173
+ errCnt++;
39174
+ }
3881039175
p->zInFile = saved_zInFile;
3881139176
p->lineno = saved_lineno;
39177
+ p->nIfAtStart = saved_nIfAtStart;
3881239178
return errCnt>0;
3881339179
}
3881439180
3881539181
/*
3881639182
** Return a pathname which is the user's home directory. A
@@ -39515,11 +39881,11 @@
3951539881
data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
3951639882
}else if( cli_strcmp(z,"-unsafe-testing")==0
3951739883
|| cli_strcmp(z,"-unsafe")==0 ){
3951839884
ShellSetFlag(&data,SHFLG_TestingMode);
3951939885
}else if( cli_strcmp(z,"-safe")==0 ){
39520
- /* no-op - catch this on the second pass */
39886
+ noInit = 1; /* --safe implies --noinit */
3952139887
}else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
3952239888
/* skip over the argument */
3952339889
i++;
3952439890
}else if( cli_strcmp(z,"-cmdline-edit")==0 ){
3952539891
shellCmdLineEdit = booleanValue(cmdline_option_value(argc,argv,++i));
3952639892
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1775,19 +1775,27 @@
1775 n = strlen(z);
1776 if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0;
1777 if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0;
1778 i = (z[0]=='-' || z[0]=='+');
1779 if( strcmp(z+i,"Inf")==0 ) return 0;
1780 if( !qrfDigit(z[i]) ) return 1;
1781 i++;
 
 
 
 
 
 
 
 
 
 
 
 
 
1782 while( qrfDigit(z[i]) ){ i++; }
1783 if( z[i]==0 ) return 0;
1784 if( z[i]=='.' ){
1785 i++;
1786 while( qrfDigit(z[i]) ){ i++; }
1787 if( z[i]==0 ) return 0;
1788 }
1789 if( z[i]=='e' || z[i]=='E' ){
1790 i++;
1791 if( z[i]=='+' || z[i]=='-' ){ i++; }
1792 if( !qrfDigit(z[i]) ) return 1;
1793 i++;
@@ -3724,10 +3732,16 @@
3724 }
3725 if( p->nRow ) sqlite3_str_append(p->pOut, "\n", 1);
3726 pVal = sqlite3_str_new(p->db);
3727 nSep = (int)strlen(p->spec.zColumnSep);
3728 mxW = p->mxWidth - (nSep + p->u.sLine.mxColWth);
 
 
 
 
 
 
3729 bWW = p->spec.bWordWrap==QRF_Yes;
3730 for(i=0; i<p->nCol; i++){
3731 const char *zVal;
3732 int cnt = 0;
3733 qrfWidthPrint(p, p->pOut, -p->u.sLine.mxColWth, p->u.sLine.azCol[i]);
@@ -3846,11 +3860,11 @@
3846 }
3847 case QRF_STYLE_JObject:
3848 case QRF_STYLE_Json: {
3849 p->spec.eText = QRF_TEXT_Json;
3850 p->spec.zNull = "null";
3851 if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%0.16g";
3852 break;
3853 }
3854 case QRF_STYLE_Html: {
3855 p->spec.eText = QRF_TEXT_Html;
3856 p->spec.zNull = "null";
@@ -3860,11 +3874,11 @@
3860 p->spec.eText = QRF_TEXT_Sql;
3861 p->spec.zNull = "NULL";
3862 if( p->spec.zTableName==0 || p->spec.zTableName[0]==0 ){
3863 p->spec.zTableName = "tab";
3864 }
3865 if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%0.16g";
3866 p->u.nIns = 0;
3867 break;
3868 }
3869 case QRF_STYLE_Line: {
3870 if( p->spec.zColumnSep==0 ){
@@ -3906,11 +3920,15 @@
3906 }
3907 break;
3908 }
3909 }
3910 if( p->spec.eEsc==QRF_Auto ){
3911 p->spec.eEsc = QRF_ESC_Ascii;
 
 
 
 
3912 }
3913 if( p->spec.eText==QRF_Auto ){
3914 p->spec.eText = QRF_TEXT_Plain;
3915 }
3916 if( p->spec.eTitle==QRF_Auto ){
@@ -4032,11 +4050,12 @@
4032 sqlite3_free(p->spec.pzOutput[0]);
4033 p->spec.pzOutput[0] = 0;
4034 qrfOom(p);
4035 }else{
4036 p->spec.pzOutput[0] = zCombined;
4037 memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n+1);
 
4038 }
4039 sqlite3_str_free(p->pOut);
4040 }else{
4041 p->spec.pzOutput[0] = qrfStrFinish(p, p->pOut);
4042 }
@@ -6437,11 +6456,11 @@
6437 ** pA->isNull==0
6438 ** pB!=0
6439 ** pB->isNull==0
6440 */
6441 static int decimal_cmp(Decimal *pA, Decimal *pB){
6442 int nASig, nBSig, rc, n;
6443 while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
6444 pA->nDigit--;
6445 pA->nFrac--;
6446 }
6447 while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
@@ -6454,20 +6473,22 @@
6454 if( pA->sign ){
6455 Decimal *pTemp = pA;
6456 pA = pB;
6457 pB = pTemp;
6458 }
6459 nASig = pA->nDigit - pA->nFrac;
6460 nBSig = pB->nDigit - pB->nFrac;
 
 
6461 if( nASig!=nBSig ){
6462 return nASig - nBSig;
6463 }
6464 n = pA->nDigit;
6465 if( n>pB->nDigit ) n = pB->nDigit;
6466 rc = memcmp(pA->a, pB->a, n);
6467 if( rc==0 ){
6468 rc = pA->nDigit - pB->nDigit;
6469 }
6470 return rc;
6471 }
6472
6473 /*
@@ -6749,21 +6770,21 @@
6749 static int decimalSafeInt(
6750 sqlite3_context *context,
6751 sqlite3_value *pVal,
6752 int *pResult
6753 ){
6754 sqlite3_int64 i64 = sqlite3_value_int64(pVal);
6755 if( i64<-2147483648LL || i64>2147483647LL ){
6756 char *zMsg = sqlite3_mprintf(
6757 "value %lld is too large: range limit -2147483648..+2147483647",
6758 i64);
6759 sqlite3_result_error(context, zMsg, -1);
6760 sqlite3_free(zMsg);
6761 *pResult = 0;
6762 return 1;
6763 }else{
6764 *pResult = (int)i64;
6765 return 0;
6766 }
6767 }
6768
6769 /*
@@ -6843,12 +6864,14 @@
6843 sqlite3_value **argv
6844 ){
6845 Decimal *pA = decimal_new(context, argv[0], 1);
6846 Decimal *pB = decimal_new(context, argv[1], 1);
6847 UNUSED_PARAMETER(argc);
6848 decimal_add(pA, pB);
6849 decimal_result(context, pA);
 
 
6850 decimal_free(pA);
6851 decimal_free(pB);
6852 }
6853 static void decimalSubFunc(
6854 sqlite3_context *context,
@@ -6856,11 +6879,11 @@
6856 sqlite3_value **argv
6857 ){
6858 Decimal *pA = decimal_new(context, argv[0], 1);
6859 Decimal *pB = decimal_new(context, argv[1], 1);
6860 UNUSED_PARAMETER(argc);
6861 if( pB ){
6862 pB->sign = !pB->sign;
6863 decimal_add(pA, pB);
6864 decimal_result(context, pA);
6865 }
6866 decimal_free(pA);
@@ -8082,11 +8105,11 @@
8082
8083 if( m<0 ){
8084 if( m<(-9223372036854775807LL) ) return;
8085 isNeg = 1;
8086 m = -m;
8087 }else if( m==0 && e>-1000 && e<1000 ){
8088 sqlite3_result_double(context, 0.0);
8089 return;
8090 }
8091 while( (m>>32)&0xffe00000 ){
8092 m >>= 1;
@@ -9645,11 +9668,14 @@
9645 static unsigned re_esc_char(ReCompiled *p){
9646 static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]-";
9647 static const char zTrans[] = "\a\f\n\r\t\v";
9648 int i, v = 0;
9649 char c;
9650 if( p->sIn.i>=p->sIn.mx ) return 0;
 
 
 
9651 c = p->sIn.z[p->sIn.i];
9652 if( c=='u' && p->sIn.i+4<p->sIn.mx ){
9653 const unsigned char *zIn = p->sIn.z + p->sIn.i;
9654 if( re_hex(zIn[1],&v)
9655 && re_hex(zIn[2],&v)
@@ -10376,41 +10402,70 @@
10376 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
10377 ** off of disk.
10378 */
10379 static void readFileContents(sqlite3_context *ctx, const char *zName){
10380 FILE *in;
10381 sqlite3_int64 nIn;
10382 void *pBuf;
 
 
10383 sqlite3 *db;
10384 int mxBlob;
10385
10386 in = sqlite3_fopen(zName, "rb");
10387 if( in==0 ){
10388 /* File does not exist or is unreadable. Leave the result set to NULL. */
10389 return;
10390 }
 
 
10391 fseek(in, 0, SEEK_END);
10392 nIn = ftell(in);
10393 rewind(in);
 
10394 db = sqlite3_context_db_handle(ctx);
10395 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
10396 if( nIn>mxBlob ){
10397 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
10398 fclose(in);
10399 return;
10400 }
10401 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
 
 
 
10402 if( pBuf==0 ){
10403 sqlite3_result_error_nomem(ctx);
10404 fclose(in);
10405 return;
10406 }
10407 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
10408 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
10409 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10410 sqlite3_result_error_code(ctx, SQLITE_IOERR);
10411 sqlite3_free(pBuf);
 
 
10412 }
10413 fclose(in);
10414 }
10415
10416 /*
@@ -11088,17 +11143,42 @@
11088 i = 1;
11089 if( (idxNum & 0x02)!=0 ){
11090 assert( argc>i );
11091 pCur->zBase = (const char*)sqlite3_value_text(argv[i++]);
11092 }
 
11093 if( (idxNum & 0x0c)!=0 ){
 
 
 
 
 
11094 assert( argc>i );
11095 pCur->mxLvl = sqlite3_value_int(argv[i++]);
11096 if( idxNum & 0x08 ) pCur->mxLvl++;
11097 if( pCur->mxLvl<=0 ) pCur->mxLvl = 1000000000;
11098 }else{
11099 pCur->mxLvl = 1000000000;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11100 }
11101 if( pCur->zBase ){
11102 pCur->nBase = (int)strlen(pCur->zBase)+1;
11103 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
11104 }else{
@@ -11127,11 +11207,15 @@
11127 **
11128 ** The query plan is represented by bits in idxNum:
11129 **
11130 ** 0x01 The path value is supplied by argv[0]
11131 ** 0x02 dir is in argv[1]
11132 ** 0x04 maxdepth is in argv[1] or [2]
 
 
 
 
11133 */
11134 static int fsdirBestIndex(
11135 sqlite3_vtab *tab,
11136 sqlite3_index_info *pIdxInfo
11137 ){
@@ -11138,11 +11222,10 @@
11138 int i; /* Loop over constraints */
11139 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
11140 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
11141 int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */
11142 int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */
11143 int omitLevel = 0; /* omit the LEVEL constraint */
11144 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
11145 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
11146 const struct sqlite3_index_constraint *pConstraint;
11147
11148 (void)tab;
@@ -11170,11 +11253,10 @@
11170 }
11171 case FSDIR_COLUMN_LEVEL: {
11172 if( pConstraint->usable && idxLevel<0 ){
11173 idxLevel = i;
11174 idxLevelEQ = 0x08;
11175 omitLevel = 0;
11176 }
11177 break;
11178 }
11179 }
11180 }else
@@ -11183,15 +11265,13 @@
11183 && idxLevel<0
11184 ){
11185 if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
11186 idxLevel = i;
11187 idxLevelEQ = 0x08;
11188 omitLevel = 1;
11189 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
11190 idxLevel = i;
11191 idxLevelEQ = 0x04;
11192 omitLevel = 1;
11193 }
11194 }
11195 }
11196 if( seenPath || seenDir ){
11197 /* If input parameters are unusable, disallow this plan */
@@ -11214,11 +11294,10 @@
11214 pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++;
11215 pIdxInfo->idxNum |= 0x02;
11216 pIdxInfo->estimatedCost /= 1.0e4;
11217 }
11218 if( idxLevel>=0 ){
11219 pIdxInfo->aConstraintUsage[idxLevel].omit = omitLevel;
11220 pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++;
11221 pIdxInfo->idxNum |= idxLevelEQ;
11222 pIdxInfo->estimatedCost /= 1.0e4;
11223 }
11224 }
@@ -12775,10 +12854,11 @@
12775 "z HIDDEN" /* 7: Name of zip file */
12776 ") WITHOUT ROWID;";
12777
12778 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
12779 #define ZIPFILE_MX_NAME (250) /* Windows limitation on filename size */
 
12780
12781 /*
12782 ** The buffer should be large enough to contain 3 65536 byte strings - the
12783 ** filename, the extra field and the file comment.
12784 */
@@ -14403,11 +14483,13 @@
14403
14404 /* Check that we're not inserting a duplicate entry -OR- updating an
14405 ** entry with a path, thereby making it into a duplicate. */
14406 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
14407 ZipfileEntry *p;
 
14408 for(p=pTab->pFirstEntry; p; p=p->pNext){
 
14409 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
14410 switch( sqlite3_vtab_on_conflict(pTab->db) ){
14411 case SQLITE_IGNORE: {
14412 goto zipfile_update_done;
14413 }
@@ -14422,10 +14504,17 @@
14422 }
14423 }
14424 break;
14425 }
14426 }
 
 
 
 
 
 
 
14427 }
14428
14429 if( rc==SQLITE_OK ){
14430 /* Create the new CDS record. */
14431 pNew = zipfileNewEntry(zPath);
@@ -14555,10 +14644,11 @@
14555 }
14556
14557 /* Write out the EOCD record */
14558 eocd.iDisk = 0;
14559 eocd.iFirstDisk = 0;
 
14560 eocd.nEntry = (u16)nEntry;
14561 eocd.nEntryTotal = (u16)nEntry;
14562 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
14563 eocd.iOffset = (u32)iOffset;
14564 rc = zipfileAppendEOCD(pTab, &eocd);
@@ -14747,10 +14837,16 @@
14747 "filename argument to zipfile() too big; max: %d bytes",
14748 ZIPFILE_MX_NAME);
14749 rc = SQLITE_ERROR;
14750 goto zipfile_step_out;
14751 }
 
 
 
 
 
 
14752
14753 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
14754 ** deflate compression) or NULL (choose automatically). */
14755 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
14756 iMethod = (int)sqlite3_value_int64(pMethod);
@@ -14874,10 +14970,11 @@
14874
14875 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14876 if( p==0 ) return;
14877 if( p->nEntry>0 ){
14878 memset(&eocd, 0, sizeof(eocd));
 
14879 eocd.nEntry = (u16)p->nEntry;
14880 eocd.nEntryTotal = (u16)p->nEntry;
14881 eocd.nSize = p->cds.n;
14882 eocd.iOffset = p->body.n;
14883
@@ -20235,11 +20332,16 @@
20235 sqlite3_str_appendchar(p->pOut, 1, '\n');
20236 return;
20237 }else{
20238 r = num*100.0/(double)denom;
20239 }
20240 sqlite3_snprintf(sizeof(zNum)-5, zNum, r>=10.0 ? "%.3g" :"%.2g", r);
 
 
 
 
 
20241 sz = (int)strlen(zNum);
20242 zDP = strchr(zNum, '.');
20243 if( zDP==0 ){
20244 memcpy(zNum+sz,".0",3);
20245 nLeadingDigit = sz;
@@ -21717,15 +21819,43 @@
21717 int n = ((eType-12) / 2);
21718 if( eType % 2 ){
21719 switch( enc ){
21720 #ifndef SQLITE_OMIT_UTF16
21721 case SQLITE_UTF16BE:
21722 sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
21723 break;
21724 case SQLITE_UTF16LE:
21725 sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21726 break;
 
21727 #endif
21728 default:
21729 sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
21730 break;
21731 }
@@ -22294,10 +22424,16 @@
22294 **
22295 ** nCol/aCol[]:
22296 ** aCol[] is an array of nCol columns. In the order in which they appear
22297 ** in the table.
22298 **
 
 
 
 
 
 
22299 ** bIntkey:
22300 ** Set to true for intkey tables, false for WITHOUT ROWID.
22301 **
22302 ** iRowidBind:
22303 ** Each column in the aCol[] array has associated with it the index of
@@ -22318,10 +22454,11 @@
22318 */
22319 struct RecoverTable {
22320 u32 iRoot; /* Root page in original database */
22321 char *zTab; /* Name of table */
22322 int nCol; /* Number of columns in table */
 
22323 RecoverColumn *aCol; /* Array of columns */
22324 int bIntkey; /* True for intkey, false for without rowid */
22325 int iRowidBind; /* If >0, bind rowid to INSERT here */
22326 RecoverTable *pNext;
22327 };
@@ -22401,11 +22538,11 @@
22401 sqlite3_stmt *pSel;
22402 sqlite3_stmt *pInsert;
22403 int nInsert;
22404
22405 RecoverTable *pTab; /* Table currently being written */
22406 int nMax; /* Max column count in any schema table */
22407 sqlite3_value **apVal; /* Array of nMax values */
22408 int nVal; /* Number of valid entries in apVal[] */
22409 int bHaveRowid;
22410 i64 iRowid;
22411 i64 iPrevPage;
@@ -22647,11 +22784,11 @@
22647
22648 /*
22649 ** Set the bit associated with page iPg in bitvec pMap.
22650 */
22651 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
22652 if( iPg<=pMap->nPg ){
22653 int iElem = (iPg / 32);
22654 int iBit = (iPg % 32);
22655 pMap->aElem[iElem] |= (((u32)1) << iBit);
22656 }
22657 }
@@ -23361,26 +23498,36 @@
23361 }
23362 memcpy(csr, z, n);
23363 csr += (n+1);
23364 }
23365
 
23366 pNew->pNext = p->pTblList;
23367 p->pTblList = pNew;
23368 pNew->bIntkey = 1;
23369 }
23370
23371 recoverFinalize(p, pStmt);
23372
 
 
 
 
 
23373 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
23374 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
23375 int iField = sqlite3_column_int(pStmt, 0);
23376 int iCol = sqlite3_column_int(pStmt, 1);
23377
23378 assert( iCol<pNew->nCol );
23379 pNew->aCol[iCol].iField = iField;
23380
23381 pNew->bIntkey = 0;
 
 
 
 
23382 iPk = -2;
23383 }
23384 recoverFinalize(p, pStmt);
23385
23386 if( p->errCode==SQLITE_OK ){
@@ -23566,11 +23713,11 @@
23566 int ii;
23567 int bSql = p->xSql ? 1 : 0;
23568
23569 if( nField<=0 ) return 0;
23570
23571 assert( nField<=pTab->nCol );
23572
23573 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
23574
23575 if( pTab->iRowidBind ){
23576 assert( pTab->bIntkey );
@@ -23582,16 +23729,22 @@
23582 }
23583 zSqlSep = "||', '||";
23584 zSep = ", ";
23585 }
23586
23587 for(ii=0; ii<nField; ii++){
23588 int eHidden = pTab->aCol[ii].eHidden;
23589 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
23590 && eHidden!=RECOVER_EHIDDEN_STORED
23591 ){
23592 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
 
 
 
 
 
 
23593 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
23594
23595 if( bSql ){
23596 zBind = recoverMPrintf(p,
23597 "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
@@ -23927,14 +24080,14 @@
23927 static int recoverWriteDataInit(sqlite3_recover *p){
23928 RecoverStateW1 *p1 = &p->w1;
23929 RecoverTable *pTbl = 0;
23930 int nByte = 0;
23931
23932 /* Figure out the maximum number of columns for any table in the schema */
23933 assert( p1->nMax==0 );
23934 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
23935 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
23936 }
23937
23938 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
23939 ** that will be written to the output database in a single row. */
23940 nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
@@ -24037,11 +24190,11 @@
24037 int iField = sqlite3_column_int(pSel, 2);
24038 sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
24039 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
24040
24041 assert( bNewCell==0 || (iField==-1 || iField==0) );
24042 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
24043
24044 if( bNewCell ){
24045 int ii = 0;
24046 if( p1->nVal>=0 ){
24047 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
@@ -24087,19 +24240,19 @@
24087 if( iField<0 ){
24088 p1->iRowid = sqlite3_column_int64(pSel, 3);
24089 assert( p1->nVal==-1 );
24090 p1->nVal = 0;
24091 p1->bHaveRowid = 1;
24092 }else if( iField<pTab->nCol ){
24093 assert( apVal[iField]==0 );
24094 apVal[iField] = sqlite3_value_dup( pVal );
24095 if( apVal[iField]==0 ){
24096 recoverError(p, SQLITE_NOMEM, 0);
24097 }
24098 p1->nVal = iField+1;
24099 }else if( pTab->nCol==0 ){
24100 p1->nVal = pTab->nCol;
24101 }
24102 p1->iPrevCell = iCell;
24103 p1->iPrevPage = iPage;
24104 }
24105 }else{
@@ -25235,10 +25388,15 @@
25235 typedef struct Unlink {
25236 sqlite3_int64 tm; /* Unlink after this time */
25237 char *zFN; /* Name of file. Space from sqlite3_malloc() */
25238 } Unlink;
25239
 
 
 
 
 
25240 /*
25241 ** State information about the database connection is contained in an
25242 ** instance of the following structure.
25243 */
25244 typedef struct ShellState ShellState;
@@ -25275,10 +25433,12 @@
25275 unsigned flgProgress; /* Flags for the progress callback */
25276 unsigned shellFlgs; /* Various flags */
25277 unsigned nTestRun; /* Number of test cases run */
25278 unsigned nTestErr; /* Number of test cases that failed */
25279 sqlite3_int64 szMax; /* --maxsize argument to .open */
 
 
25280 char *zDestTable; /* Name of destination table when MODE_Insert */
25281 char *zTempFile; /* Temporary file that might need deleting */
25282 char *zErrPrefix; /* Alternative error message prefix */
25283 char *zAuthIgnore; /* Authorizer returns SQLITE_IGNORE for glob matches */
25284 char *zAuthDeny; /* Authorizer returns SQLITE_DENY for glob matches */
@@ -25317,10 +25477,17 @@
25317 int nArg; /* Number of argument slots actually used */
25318 char **azArg; /* Pointer to each argument, dequoted */
25319 int *aiOfst; /* Offset into zOrig[] for start of each arg */
25320 char *abQuot; /* True if the argment was originally quoted */
25321 } dot;
 
 
 
 
 
 
 
25322 #ifdef SQLITE_SHELL_FIDDLE
25323 struct {
25324 const char * zInput; /* Input string from wasm/JS proxy */
25325 const char * zPos; /* Cursor pos into zInput */
25326 const char * zDefaultDbName; /* Default name for db file */
@@ -25629,11 +25796,11 @@
25629 }
25630 #endif
25631 static void cli_exit(int rc){
25632 if( cli_output_capture ){
25633 char *z = sqlite3_str_finish(cli_output_capture);
25634 sqlite3_fputs(z, stdout);
25635 fflush(stdout);
25636 }
25637 exit(rc);
25638 }
25639
@@ -25759,10 +25926,29 @@
25759 return 0;
25760 }
25761 #endif
25762 #undef STAT_CHR_SRC
25763 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25764
25765 /*
25766 ** This routine reads a line of text from FILE in, stores
25767 ** the text in memory obtained from malloc() and returns a pointer
25768 ** to the text. NULL is returned at end of file, or if malloc()
@@ -25769,11 +25955,11 @@
25769 ** fails, or if the length of the line is longer than about a gigabyte.
25770 **
25771 ** If zLine is not NULL then it is a malloced buffer returned from
25772 ** a previous call to this routine that may be reused.
25773 */
25774 static char *local_getline(char *zLine, FILE *in){
25775 int nLine = zLine==0 ? 0 : 100;
25776 int n = 0;
25777
25778 while( seenInterrupt<2 ){
25779 if( n+100>nLine ){
@@ -25783,11 +25969,11 @@
25783 }
25784 nLine = nLine*2 + 100;
25785 zLine = realloc(zLine, nLine);
25786 shell_check_oom(zLine);
25787 }
25788 if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
25789 if( n==0 ){
25790 free(zLine);
25791 return 0;
25792 }
25793 zLine[n] = 0;
@@ -26397,13 +26583,12 @@
26397 char *zPrior, /* Just the previous line */
26398 const char *zAll, /* All statement text accumulated so far */
26399 int bContinue /* True for a continuation prompt */
26400 ){
26401 char *zResult;
26402 FILE *in = p->in;
26403 if( in!=0 ){
26404 zResult = local_getline(zPrior, in);
26405 }else{
26406 const char *zBase = prompt_string(p, bContinue!=0);
26407 char *zPrompt = expand_prompt(p, zAll, zBase);
26408 shell_check_oom(zPrompt);
26409 #if SHELL_CMDLINE_EDIT_AVAILABLE
@@ -26421,11 +26606,11 @@
26421 #endif
26422 {
26423 sputz(stdout, zPrompt);
26424 fflush(stdout);
26425 do{
26426 zResult = local_getline(zPrior, stdin);
26427 zPrior = 0;
26428 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
26429 if( zResult==0 ) sqlite3_sleep(50);
26430 }while( zResult==0 && seenInterrupt>0 );
26431 }
@@ -26637,10 +26822,76 @@
26637 }else{
26638 while( s.zTxt && (zCom = strstr(s.zTxt,"*/"))!=0 ){ zCom[1] = '*'; }
26639 }
26640 return s.zTxt;
26641 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26642
26643 /*
26644 ** SQL function: strtod(X)
26645 **
26646 ** For use internally and for debugging and testing only. This function
@@ -29112,10 +29363,12 @@
29112 /* Build an appropriate SELECT statement */
29113 initText(&sSelect);
29114 appendText(&sSelect, "SELECT ", 0);
29115 if( azCol[0] ){
29116 appendText(&sSelect, azCol[0], 0);
 
 
29117 appendText(&sSelect, ",", 0);
29118 }
29119 for(i=1; azCol[i]; i++){
29120 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
29121 if( azCol[i+1] ){
@@ -29132,10 +29385,13 @@
29132 p->mode.eMode = MODE_Insert;
29133 p->mode.spec.eText = QRF_TEXT_Sql;
29134 p->mode.spec.eBlob = QRF_BLOB_Sql;
29135 p->mode.spec.bTitles = QRF_No;
29136 p->mode.spec.nCharLimit = 0;
 
 
 
29137 rc = shell_exec(p, sSelect.zTxt, 0);
29138 if( (rc&0xff)==SQLITE_CORRUPT ){
29139 cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
29140 toggleSelectOrder(p->db);
29141 shell_exec(p, sSelect.zTxt, 0);
@@ -29470,10 +29726,21 @@
29470 ** "Usage" help text automatically generated from comments */
29471 static const struct {
29472 const char *zCmd; /* Name of the dot-command */
29473 const char *zUsage; /* Documentation */
29474 } aUsage[] = {
 
 
 
 
 
 
 
 
 
 
 
29475 { ".import",
29476 "USAGE: .import [OPTIONS] FILE TABLE\n"
29477 "\n"
29478 "Import CSV or similar text from FILE into TABLE. If TABLE does\n"
29479 "not exist, it is created using the first row of FILE as the column\n"
@@ -29677,10 +29944,41 @@
29677 "\".check\" command for additional informatioon.\n"
29678 "\n"
29679 "Options:\n"
29680 " --error-prefix TEXT Change error message prefix text to TEXT\n"
29681 },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29682 };
29683
29684 /*
29685 ** Return a pointer to usage text for zCmd, or NULL if none exists.
29686 */
@@ -29834,20 +30132,25 @@
29834 cli_printf(stderr,"Error: '%s' not seekable\n", zName);
29835 fclose(in);
29836 return 0;
29837 }
29838 nIn = ftell(in);
 
 
 
 
 
29839 rewind(in);
29840 pBuf = sqlite3_malloc64( nIn+1 );
29841 if( pBuf==0 ){
29842 cli_puts("Error: out of memory\n", stderr);
29843 fclose(in);
29844 return 0;
29845 }
29846 nRead = fread(pBuf, nIn, 1, in);
29847 fclose(in);
29848 if( nRead!=1 ){
29849 sqlite3_free(pBuf);
29850 cli_printf(stderr,"Error: cannot read '%s'\n", zName);
29851 return 0;
29852 }
29853 pBuf[nIn] = 0;
@@ -30060,11 +30363,11 @@
30060 nLine = p->lineno;
30061 if( in==0 ) in = stdin;
30062 }
30063 *pnData = 0;
30064 nLine++;
30065 if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
30066 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
30067 if( rc!=2 ) goto readHexDb_error;
30068 if( n<0 ) goto readHexDb_error;
30069 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
30070 cli_puts("invalid pagesize\n", stderr);
@@ -30072,11 +30375,11 @@
30072 }
30073 sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
30074 a = sqlite3_malloc64( sz ? sz : 1 );
30075 shell_check_oom(a);
30076 memset(a, 0, sz);
30077 for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
30078 int j = 0; /* Page number from "| page" line */
30079 int k = 0; /* Offset from "| page" line */
30080 if( nLine>=2000000000 ){
30081 cli_printf(stderr, "input too big\n");
30082 goto readHexDb_error;
@@ -30110,11 +30413,11 @@
30110
30111 readHexDb_error:
30112 if( in!=p->in ){
30113 fclose(in);
30114 }else{
30115 while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
30116 nLine++;
30117 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
30118 }
30119 p->lineno = nLine;
30120 }
@@ -30336,10 +30639,12 @@
30336 #undef SHELL_SUB_MACRO
30337 #undef SHELL_SUBMACRO
30338 }
30339 #endif
30340
 
 
30341 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
30342 shellStrtod, 0, 0);
30343 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
30344 shellDtostr, 0, 0);
30345 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
@@ -30912,227 +31217,48 @@
30912 p->cTerm = c;
30913 if( p->z ) p->z[p->n] = 0;
30914 return p->z;
30915 }
30916
30917 /*
30918 ** Try to transfer data for table zTable. If an error is seen while
30919 ** moving forward, try to go backwards. The backwards movement won't
30920 ** work for WITHOUT ROWID tables.
30921 */
30922 static void tryToCloneData(
30923 ShellState *p,
30924 sqlite3 *newDb,
30925 const char *zTable
30926 ){
30927 sqlite3_stmt *pQuery = 0;
30928 sqlite3_stmt *pInsert = 0;
30929 char *zQuery = 0;
30930 char *zInsert = 0;
30931 int rc;
30932 int i, j, n;
30933 int nTable = strlen30(zTable);
30934 int k = 0;
30935 int cnt = 0;
30936 const int spinRate = 10000;
30937
30938 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
30939 shell_check_oom(zQuery);
30940 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
30941 if( rc ){
30942 cli_printf(stderr,"Error %d: %s on [%s]\n",
30943 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
30944 goto end_data_xfer;
30945 }
30946 n = sqlite3_column_count(pQuery);
30947 zInsert = sqlite3_malloc64(200 + nTable + n*3);
30948 shell_check_oom(zInsert);
30949 sqlite3_snprintf(200+nTable,zInsert,
30950 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
30951 i = strlen30(zInsert);
30952 for(j=1; j<n; j++){
30953 memcpy(zInsert+i, ",?", 2);
30954 i += 2;
30955 }
30956 memcpy(zInsert+i, ");", 3);
30957 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
30958 if( rc ){
30959 cli_printf(stderr,"Error %d: %s on [%s]\n",
30960 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
30961 goto end_data_xfer;
30962 }
30963 for(k=0; k<2; k++){
30964 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
30965 for(i=0; i<n; i++){
30966 switch( sqlite3_column_type(pQuery, i) ){
30967 case SQLITE_NULL: {
30968 sqlite3_bind_null(pInsert, i+1);
30969 break;
30970 }
30971 case SQLITE_INTEGER: {
30972 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
30973 break;
30974 }
30975 case SQLITE_FLOAT: {
30976 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
30977 break;
30978 }
30979 case SQLITE_TEXT: {
30980 sqlite3_bind_text(pInsert, i+1,
30981 (const char*)sqlite3_column_text(pQuery,i),
30982 -1, SQLITE_STATIC);
30983 break;
30984 }
30985 case SQLITE_BLOB: {
30986 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
30987 sqlite3_column_bytes(pQuery,i),
30988 SQLITE_STATIC);
30989 break;
30990 }
30991 }
30992 } /* End for */
30993 rc = sqlite3_step(pInsert);
30994 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
30995 cli_printf(stderr,"Error %d: %s\n",
30996 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
30997 }
30998 sqlite3_reset(pInsert);
30999 cnt++;
31000 if( (cnt%spinRate)==0 ){
31001 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
31002 fflush(stdout);
31003 }
31004 } /* End while */
31005 if( rc==SQLITE_DONE ) break;
31006 sqlite3_finalize(pQuery);
31007 sqlite3_free(zQuery);
31008 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
31009 zTable);
31010 shell_check_oom(zQuery);
31011 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31012 if( rc ){
31013 cli_printf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
31014 break;
31015 }
31016 } /* End for(k=0...) */
31017
31018 end_data_xfer:
31019 sqlite3_finalize(pQuery);
31020 sqlite3_finalize(pInsert);
31021 sqlite3_free(zQuery);
31022 sqlite3_free(zInsert);
31023 }
31024
31025
31026 /*
31027 ** Try to transfer all rows of the schema that match zWhere. For
31028 ** each row, invoke xForEach() on the object defined by that row.
31029 ** If an error is encountered while moving forward through the
31030 ** sqlite_schema table, try again moving backwards.
31031 */
31032 static void tryToCloneSchema(
31033 ShellState *p,
31034 sqlite3 *newDb,
31035 const char *zWhere,
31036 void (*xForEach)(ShellState*,sqlite3*,const char*)
31037 ){
31038 sqlite3_stmt *pQuery = 0;
31039 char *zQuery = 0;
31040 int rc;
31041 const unsigned char *zName;
31042 const unsigned char *zSql;
31043 char *zErrMsg = 0;
31044
31045 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
31046 " WHERE %s ORDER BY rowid ASC", zWhere);
31047 shell_check_oom(zQuery);
31048 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31049 if( rc ){
31050 cli_printf(stderr,
31051 "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
31052 sqlite3_errmsg(p->db), zQuery);
31053 goto end_schema_xfer;
31054 }
31055 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
31056 zName = sqlite3_column_text(pQuery, 0);
31057 zSql = sqlite3_column_text(pQuery, 1);
31058 if( zName==0 || zSql==0 ) continue;
31059 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
31060 cli_printf(stdout, "%s... ", zName); fflush(stdout);
31061 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
31062 if( zErrMsg ){
31063 cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
31064 sqlite3_free(zErrMsg);
31065 zErrMsg = 0;
31066 }
31067 }
31068 if( xForEach ){
31069 xForEach(p, newDb, (const char*)zName);
31070 }
31071 sputz(stdout, "done\n");
31072 }
31073 if( rc!=SQLITE_DONE ){
31074 sqlite3_finalize(pQuery);
31075 sqlite3_free(zQuery);
31076 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
31077 " WHERE %s ORDER BY rowid DESC", zWhere);
31078 shell_check_oom(zQuery);
31079 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
31080 if( rc ){
31081 cli_printf(stderr,"Error: (%d) %s on [%s]\n",
31082 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
31083 goto end_schema_xfer;
31084 }
31085 while( sqlite3_step(pQuery)==SQLITE_ROW ){
31086 zName = sqlite3_column_text(pQuery, 0);
31087 zSql = sqlite3_column_text(pQuery, 1);
31088 if( zName==0 || zSql==0 ) continue;
31089 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
31090 cli_printf(stdout, "%s... ", zName); fflush(stdout);
31091 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
31092 if( zErrMsg ){
31093 cli_printf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
31094 sqlite3_free(zErrMsg);
31095 zErrMsg = 0;
31096 }
31097 if( xForEach ){
31098 xForEach(p, newDb, (const char*)zName);
31099 }
31100 sputz(stdout, "done\n");
31101 }
31102 }
31103 end_schema_xfer:
31104 sqlite3_finalize(pQuery);
31105 sqlite3_free(zQuery);
31106 }
31107
31108 /*
31109 ** Open a new database file named "zNewDb". Try to recover as much information
31110 ** as possible out of the main database (which might be corrupt) and write it
31111 ** into zNewDb.
31112 */
31113 static void tryToClone(ShellState *p, const char *zNewDb){
31114 int rc;
31115 sqlite3 *newDb = 0;
31116 if( access(zNewDb,0)==0 ){
31117 cli_printf(stderr,"File \"%s\" already exists.\n", zNewDb);
31118 return;
31119 }
31120 rc = sqlite3_open(zNewDb, &newDb);
31121 if( rc ){
31122 cli_printf(stderr,
31123 "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
31124 }else{
31125 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
31126 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
31127 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
31128 tryToCloneSchema(p, newDb, "type!='table'", 0);
31129 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
31130 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
31131 }
31132 close_db(newDb);
31133 }
31134
31135 #ifndef SQLITE_SHELL_FIDDLE
31136 /*
31137 ** Change the output stream (file or pipe or console) to something else.
31138 */
@@ -32493,11 +32619,11 @@
32493 arWhereClause(&rc, pAr, &zWhere);
32494
32495 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
32496 pAr->zSrcTable, zWhere);
32497 if( pAr->bDryRun ){
32498 cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
32499 }else{
32500 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
32501 if( pAr->bVerbose ){
32502 cli_printf(pAr->out, "%s % 10d %s %s\n",
32503 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
@@ -33553,11 +33679,11 @@
33553 i64 iStart = p->lineno;
33554 char zLine[2000];
33555 sCtx.zFile = p->zInFile;
33556 sCtx.nLine = p->lineno+1;
33557 iLineOffset = p->lineno;
33558 while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
33559 if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){
33560 ckEnd = 2;
33561 if( strchr(zLine,'\n') ) p->lineno++;
33562 break;
33563 }
@@ -34627,11 +34753,11 @@
34627 dotCmdError(p, i, "incompatible with prior options",0);
34628 goto dotCmdOutput_error;
34629 }
34630 eMode = z[1];
34631 }else if( cli_strcmp(z,"-show")==0 ){
34632 if( cli_output_capture ){
34633 sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
34634 }
34635 }else if( cli_strcmp(z,"-keep")==0 ){
34636 bKeep = 1;
34637 }else if( optionMatch(z,"error-prefix") ){
@@ -34762,10 +34888,34 @@
34762
34763 dotCmdOutput_error:
34764 sqlite3_free(zFile);
34765 return 1;
34766 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34767
34768 /*
34769 ** DOT-COMMAND: .check
34770 ** USAGE: .check [OPTIONS] PATTERN
34771 **
@@ -34796,10 +34946,19 @@
34796 char *zPattern = 0; /* Actual test pattern */
34797 int eCheck = 0; /* 1: --glob, 2: --notglob, 3: --exact */
34798 int isOk; /* True if results are OK */
34799 sqlite3_int64 iStart = p->lineno; /* Line number of .check statement */
34800
 
 
 
 
 
 
 
 
 
34801 if( p->zTestcase[0]==0 ){
34802 dotCmdError(p, 0, "no .testcase is active", 0);
34803 return 1;
34804 }
34805 for(i=1; i<nArg; i++){
@@ -34806,11 +34965,11 @@
34806 char *z = azArg[i];
34807 if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
34808 if( cli_strcmp(z,"-keep")==0 ){
34809 bKeep = 1;
34810 }else if( cli_strcmp(z,"-show")==0 ){
34811 if( cli_output_capture ){
34812 sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
34813 }
34814 bKeep = 1;
34815 }else if( z[0]=='-'
34816 && (k = pickStr(&z[1],0,"glob","notglob","exact",""))>=0
@@ -34829,27 +34988,19 @@
34829 }
34830 if( zCheck==0 ){
34831 dotCmdError(p, 0, "no PATTERN specified", 0);
34832 return 1;
34833 }
34834 if( cli_output_capture && sqlite3_str_length(cli_output_capture) ){
34835 zTest = sqlite3_str_value(cli_output_capture);
34836 shell_check_oom(zTest);
34837 }else{
34838 zTest = "";
34839 }
34840 p->nTestRun++;
34841 if( zCheck[0]=='<' && zCheck[1]=='<' && zCheck[2]!=0 ){
34842 int nCheck = strlen30(zCheck);
34843 sqlite3_str *pPattern = sqlite3_str_new(p->db);
34844 char zLine[2000];
34845 while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){
34846 if( strchr(zLine,'\n') ) p->lineno++;
34847 if( cli_strncmp(&zCheck[2],zLine,nCheck-2)==0 ) break;
34848 sqlite3_str_appendall(pPattern, zLine);
34849 }
34850 zPattern = sqlite3_str_finish(pPattern);
34851 if( zPattern==0 ){
34852 zPattern = sqlite3_mprintf("");
34853 }
34854 }else{
34855 zPattern = zCheck;
@@ -35042,10 +35193,147 @@
35042 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s:%lld",
35043 p->zInFile, p->lineno);
35044 }
35045 return 0;
35046 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35047
35048 /*
35049 ** Enlarge the space allocated in p->dot so that it can hold more
35050 ** than nArg parsed command-line arguments.
35051 */
@@ -35348,17 +35636,11 @@
35348 rc = dotCmdCheck(p);
35349 }else
35350
35351 #ifndef SQLITE_SHELL_FIDDLE
35352 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
35353 failIfSafeMode(p, "cannot run .clone in safe mode");
35354 if( nArg==2 ){
35355 tryToClone(p, azArg[1]);
35356 }else{
35357 eputz("Usage: .clone FILENAME\n");
35358 rc = 1;
35359 }
35360 }else
35361 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
35362
35363 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
35364 if( nArg==1 ){
@@ -35667,10 +35949,12 @@
35667 cli_puts(p->nErr?";\nROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
35668 }
35669 p->shellFlgs = savedShellFlags;
35670 modeFree(&p->mode);
35671 p->mode = saved_mode;
 
 
35672 rc = p->nErr>0;
35673 }else
35674
35675 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
35676 if( nArg==2 ){
@@ -35682,10 +35966,22 @@
35682 }else{
35683 eputz("Usage: .echo on|off\n");
35684 rc = 1;
35685 }
35686 }else
 
 
 
 
 
 
 
 
 
 
 
 
35687
35688 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
35689 if( nArg==2 ){
35690 if( p->mode.autoEQPtrace ){
35691 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
@@ -35981,10 +36277,14 @@
35981 }
35982 }else{
35983 showHelp(p->out, 0);
35984 }
35985 }else
 
 
 
 
35986
35987 #ifndef SQLITE_SHELL_FIDDLE
35988 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
35989 rc = dotCmdImport(p);
35990 }else
@@ -36238,10 +36538,11 @@
36238 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
36239 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
36240 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
36241 { "schema", SQLITE_LIMIT_SCHEMA },
36242 { "trigger_steps", SQLITE_LIMIT_TRIGGER_STEPS },
 
36243 };
36244 int i, n2;
36245 open_db(p, 0);
36246 if( nArg==1 ){
36247 for(i=0; i<ArraySize(aLimit); i++){
@@ -36272,12 +36573,12 @@
36272 azArg[1]);
36273 rc = 1;
36274 goto meta_command_exit;
36275 }
36276 if( nArg==3 ){
36277 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
36278 (int)integerValue(azArg[2]));
36279 }else{
36280 cli_printf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
36281 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
36282 }
36283 }
@@ -36729,19 +37030,27 @@
36729 rc = 2;
36730 }else
36731 #endif
36732
36733 #ifndef SQLITE_SHELL_FIDDLE
36734 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
36735 FILE *inSaved = p->in;
36736 i64 savedLineno = p->lineno;
36737 failIfSafeMode(p, "cannot run .read in safe mode");
 
 
 
 
 
36738 if( nArg!=2 ){
36739 eputz("Usage: .read FILE\n");
36740 rc = 1;
36741 goto meta_command_exit;
36742 }
 
 
 
36743 if( azArg[1][0]=='|' ){
36744 #ifdef SQLITE_OMIT_POPEN
36745 eputz("Error: pipes are not supported in this OS\n");
36746 rc = 1;
36747 #else
@@ -36752,10 +37061,24 @@
36752 }else{
36753 rc = process_input(p, "<pipe>");
36754 pclose(p->in);
36755 }
36756 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36757 }else if( (p->in = openChrSource(azArg[1]))==0 ){
36758 cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
36759 rc = 1;
36760 }else{
36761 char *zFilename = strdup(azArg[1]);
@@ -38518,14 +38841,14 @@
38518 }else{
38519 zPre[nPre++] = c;
38520 }
38521 }
38522 zPre[nPre] = 0;
38523 j = n - 20;
38524 if( j<i ){
38525 j = i;
38526 }else{
 
38527 memcpy(zSuf," ... ", 5);
38528 nSuf = 5;
38529 }
38530 for(i=j; i<n; i++){
38531 char c = zSql[i];
@@ -38680,10 +39003,34 @@
38680 if( x==0x00 || x==0x10 ) return 1;
38681 }
38682 return 0;
38683 }
38684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38685 /*
38686 ** Read input from *in and process it. If *in==0 then input
38687 ** is interactive - the user is typing it it. Otherwise, input
38688 ** is coming from a file or device. A prompt is issued and history
38689 ** is saved only if input is interactive. An interrupt signal will
@@ -38701,10 +39048,11 @@
38701 int errCnt = 0; /* Number of errors seen */
38702 int hasSemi = 0; /* Input line contains a semicolon */
38703 i64 startline = 0; /* Line number for start of current input */
38704 const char *saved_zInFile; /* Prior value of p->zInFile */
38705 i64 saved_lineno; /* Prior value of p->lineno */
 
38706
38707 if( p->inputNesting==MAX_INPUT_NESTING ){
38708 /* This will be more informative in a later version. */
38709 cli_printf(stderr,"%s: Input nesting limit (%d) reached at line %lld."
38710 " Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno);
@@ -38713,10 +39061,12 @@
38713 ++p->inputNesting;
38714 saved_zInFile = p->zInFile;
38715 p->zInFile = zSrc;
38716 saved_lineno = p->lineno;
38717 p->lineno = 0;
 
 
38718 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
38719 fflush(p->out);
38720 zLine = one_input_line(p, zLine, zSql, nSql>0);
38721 if( zLine==0 ){
38722 /* End of input */
@@ -38734,15 +39084,18 @@
38734 continue;
38735 }
38736 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
38737 echo_group_input(p, zLine);
38738 if( zLine[0]=='.' ){
38739 rc = do_meta_command(zLine, p);
38740 if( rc==2 ){ /* exit requested */
38741 break;
38742 }else if( rc ){
38743 errCnt++;
 
 
 
38744 }
38745 }
38746 continue;
38747 }
38748 #ifdef SQLITE_SHELL_LEGACY_COMMAND_TERMINATOR
@@ -38779,11 +39132,13 @@
38779 errCnt++;
38780 break;
38781 }
38782 if( nSql && hasSemi && line_is_complete(zSql, nSql) ){
38783 echo_group_input(p, zSql);
38784 errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
 
 
38785 nSql = 0;
38786 if( p->nPopOutput ){
38787 output_reset(p);
38788 p->nPopOutput = 0;
38789 }else{
@@ -38800,17 +39155,28 @@
38800 }
38801 }
38802 if( nSql ){
38803 /* This may be incomplete. Let the SQL parser deal with that. */
38804 echo_group_input(p, zSql);
38805 errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
 
 
38806 }
38807 free(zSql);
38808 free(zLine);
38809 --p->inputNesting;
 
 
 
 
 
 
 
 
38810 p->zInFile = saved_zInFile;
38811 p->lineno = saved_lineno;
 
38812 return errCnt>0;
38813 }
38814
38815 /*
38816 ** Return a pathname which is the user's home directory. A
@@ -39515,11 +39881,11 @@
39515 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
39516 }else if( cli_strcmp(z,"-unsafe-testing")==0
39517 || cli_strcmp(z,"-unsafe")==0 ){
39518 ShellSetFlag(&data,SHFLG_TestingMode);
39519 }else if( cli_strcmp(z,"-safe")==0 ){
39520 /* no-op - catch this on the second pass */
39521 }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
39522 /* skip over the argument */
39523 i++;
39524 }else if( cli_strcmp(z,"-cmdline-edit")==0 ){
39525 shellCmdLineEdit = booleanValue(cmdline_option_value(argc,argv,++i));
39526
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1775,19 +1775,27 @@
1775 n = strlen(z);
1776 if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0;
1777 if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0;
1778 i = (z[0]=='-' || z[0]=='+');
1779 if( strcmp(z+i,"Inf")==0 ) return 0;
1780 if( z[i]=='0' && (z[i+1]=='x' || z[i+1]=='X') ){
1781 /* Hexadecimal integer literal: 0x1f */
1782 i += 2;
1783 n = strspn(&z[i], "0123456789abcdefABCDEF");
1784 return n==0 || z[i+n]!=0;
1785 }
1786 if( z[i]=='.' && qrfDigit(z[i+1]) ){
1787 /* Real literal with no digits before the ".", such as ".5" */
1788 i++;
1789 }else if( !qrfDigit(z[i]) ){
1790 return 1;
1791 }else{
1792 while( qrfDigit(z[i]) ){ i++; }
1793 if( z[i]=='.' ) i++;
1794 }
1795 while( qrfDigit(z[i]) ){ i++; }
1796 if( z[i]==0 ) return 0;
 
 
 
 
 
1797 if( z[i]=='e' || z[i]=='E' ){
1798 i++;
1799 if( z[i]=='+' || z[i]=='-' ){ i++; }
1800 if( !qrfDigit(z[i]) ) return 1;
1801 i++;
@@ -3724,10 +3732,16 @@
3732 }
3733 if( p->nRow ) sqlite3_str_append(p->pOut, "\n", 1);
3734 pVal = sqlite3_str_new(p->db);
3735 nSep = (int)strlen(p->spec.zColumnSep);
3736 mxW = p->mxWidth - (nSep + p->u.sLine.mxColWth);
3737 if( mxW<2 ){
3738 /* The column names leave little or no room for the value. Use a
3739 ** width of at least 2, enough for any one character, so that
3740 ** qrfWrapLine() always makes progress. */
3741 mxW = 2;
3742 }
3743 bWW = p->spec.bWordWrap==QRF_Yes;
3744 for(i=0; i<p->nCol; i++){
3745 const char *zVal;
3746 int cnt = 0;
3747 qrfWidthPrint(p, p->pOut, -p->u.sLine.mxColWth, p->u.sLine.azCol[i]);
@@ -3846,11 +3860,11 @@
3860 }
3861 case QRF_STYLE_JObject:
3862 case QRF_STYLE_Json: {
3863 p->spec.eText = QRF_TEXT_Json;
3864 p->spec.zNull = "null";
3865 if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%!0.17g";
3866 break;
3867 }
3868 case QRF_STYLE_Html: {
3869 p->spec.eText = QRF_TEXT_Html;
3870 p->spec.zNull = "null";
@@ -3860,11 +3874,11 @@
3874 p->spec.eText = QRF_TEXT_Sql;
3875 p->spec.zNull = "NULL";
3876 if( p->spec.zTableName==0 || p->spec.zTableName[0]==0 ){
3877 p->spec.zTableName = "tab";
3878 }
3879 if( p->spec.zFpFmt==0 ) p->spec.zFpFmt = "%!0.17g";
3880 p->u.nIns = 0;
3881 break;
3882 }
3883 case QRF_STYLE_Line: {
3884 if( p->spec.zColumnSep==0 ){
@@ -3906,11 +3920,15 @@
3920 }
3921 break;
3922 }
3923 }
3924 if( p->spec.eEsc==QRF_Auto ){
3925 if( p->spec.eText==QRF_TEXT_Csv ){
3926 p->spec.eEsc = QRF_ESC_Off;
3927 }else{
3928 p->spec.eEsc = QRF_ESC_Ascii;
3929 }
3930 }
3931 if( p->spec.eText==QRF_Auto ){
3932 p->spec.eText = QRF_TEXT_Plain;
3933 }
3934 if( p->spec.eTitle==QRF_Auto ){
@@ -4032,11 +4050,12 @@
4050 sqlite3_free(p->spec.pzOutput[0]);
4051 p->spec.pzOutput[0] = 0;
4052 qrfOom(p);
4053 }else{
4054 p->spec.pzOutput[0] = zCombined;
4055 if( n ) memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n);
4056 zCombined[sz+n] = 0;
4057 }
4058 sqlite3_str_free(p->pOut);
4059 }else{
4060 p->spec.pzOutput[0] = qrfStrFinish(p, p->pOut);
4061 }
@@ -6437,11 +6456,11 @@
6456 ** pA->isNull==0
6457 ** pB!=0
6458 ** pB->isNull==0
6459 */
6460 static int decimal_cmp(Decimal *pA, Decimal *pB){
6461 int nASig, nBSig, rc, n, iA, iB;
6462 while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
6463 pA->nDigit--;
6464 pA->nFrac--;
6465 }
6466 while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
@@ -6454,20 +6473,22 @@
6473 if( pA->sign ){
6474 Decimal *pTemp = pA;
6475 pA = pB;
6476 pB = pTemp;
6477 }
6478 for(iA=0; iA<pA->nDigit-pA->nFrac && pA->a[iA]==0; iA++){}
6479 for(iB=0; iB<pB->nDigit-pB->nFrac && pB->a[iB]==0; iB++){}
6480 nASig = pA->nDigit - pA->nFrac - iA;
6481 nBSig = pB->nDigit - pB->nFrac - iB;
6482 if( nASig!=nBSig ){
6483 return nASig - nBSig;
6484 }
6485 n = pA->nDigit - iA;
6486 if( n>pB->nDigit - iB ) n = pB->nDigit - iB;
6487 rc = memcmp(pA->a+iA, pB->a+iB, n);
6488 if( rc==0 ){
6489 rc = (pA->nDigit - iA) - (pB->nDigit - iB);
6490 }
6491 return rc;
6492 }
6493
6494 /*
@@ -6749,21 +6770,21 @@
6770 static int decimalSafeInt(
6771 sqlite3_context *context,
6772 sqlite3_value *pVal,
6773 int *pResult
6774 ){
6775 sqlite3_int64 ii = sqlite3_value_int64(pVal);
6776 if( ii<-2147483648LL || ii>2147483647LL ){
6777 char *zMsg = sqlite3_mprintf(
6778 "value %lld is too large: range limit -2147483648..+2147483647",
6779 ii);
6780 sqlite3_result_error(context, zMsg, -1);
6781 sqlite3_free(zMsg);
6782 *pResult = 0;
6783 return 1;
6784 }else{
6785 *pResult = (int)ii;
6786 return 0;
6787 }
6788 }
6789
6790 /*
@@ -6843,12 +6864,14 @@
6864 sqlite3_value **argv
6865 ){
6866 Decimal *pA = decimal_new(context, argv[0], 1);
6867 Decimal *pB = decimal_new(context, argv[1], 1);
6868 UNUSED_PARAMETER(argc);
6869 if( pA && pB ){
6870 decimal_add(pA, pB);
6871 decimal_result(context, pA);
6872 }
6873 decimal_free(pA);
6874 decimal_free(pB);
6875 }
6876 static void decimalSubFunc(
6877 sqlite3_context *context,
@@ -6856,11 +6879,11 @@
6879 sqlite3_value **argv
6880 ){
6881 Decimal *pA = decimal_new(context, argv[0], 1);
6882 Decimal *pB = decimal_new(context, argv[1], 1);
6883 UNUSED_PARAMETER(argc);
6884 if( pA && pB ){
6885 pB->sign = !pB->sign;
6886 decimal_add(pA, pB);
6887 decimal_result(context, pA);
6888 }
6889 decimal_free(pA);
@@ -8082,11 +8105,11 @@
8105
8106 if( m<0 ){
8107 if( m<(-9223372036854775807LL) ) return;
8108 isNeg = 1;
8109 m = -m;
8110 }else if( m==0 ){
8111 sqlite3_result_double(context, 0.0);
8112 return;
8113 }
8114 while( (m>>32)&0xffe00000 ){
8115 m >>= 1;
@@ -9645,11 +9668,14 @@
9668 static unsigned re_esc_char(ReCompiled *p){
9669 static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]-";
9670 static const char zTrans[] = "\a\f\n\r\t\v";
9671 int i, v = 0;
9672 char c;
9673 if( p->sIn.i>=p->sIn.mx ){
9674 p->zErr = "unknown \\ escape";
9675 return 0;
9676 }
9677 c = p->sIn.z[p->sIn.i];
9678 if( c=='u' && p->sIn.i+4<p->sIn.mx ){
9679 const unsigned char *zIn = p->sIn.z + p->sIn.i;
9680 if( re_hex(zIn[1],&v)
9681 && re_hex(zIn[2],&v)
@@ -10376,41 +10402,70 @@
10402 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
10403 ** off of disk.
10404 */
10405 static void readFileContents(sqlite3_context *ctx, const char *zName){
10406 FILE *in;
10407 sqlite3_int64 nIn; /* Size of the file according to ftell() */
10408 sqlite3_int64 nAlloc; /* Bytes allocated for pBuf */
10409 sqlite3_int64 nRead = 0; /* Bytes read so far */
10410 unsigned char *pBuf;
10411 sqlite3 *db;
10412 int mxBlob;
10413
10414 in = sqlite3_fopen(zName, "rb");
10415 if( in==0 ){
10416 /* File does not exist or is unreadable. Leave the result set to NULL. */
10417 return;
10418 }
10419 /* The size reported by ftell() is only a hint. It is -1 for pipes and
10420 ** FIFOs and 0 for many files under /proc. Keep reading until EOF. */
10421 fseek(in, 0, SEEK_END);
10422 nIn = ftell(in);
10423 rewind(in);
10424 if( nIn<0 ) nIn = 0;
10425 db = sqlite3_context_db_handle(ctx);
10426 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
10427 if( nIn>mxBlob ){
10428 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
10429 fclose(in);
10430 return;
10431 }
10432 /* One extra byte so that EOF on a regular file is seen without a
10433 ** reallocation. */
10434 nAlloc = nIn+1;
10435 pBuf = sqlite3_malloc64( nAlloc );
10436 if( pBuf==0 ){
10437 sqlite3_result_error_nomem(ctx);
10438 fclose(in);
10439 return;
10440 }
10441 while( 1 ){
10442 unsigned char *pNew;
10443 nRead += fread(pBuf+nRead, 1, (size_t)(nAlloc-nRead), in);
10444 if( nRead>mxBlob ){
10445 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
10446 sqlite3_free(pBuf);
10447 fclose(in);
10448 return;
10449 }
10450 if( nRead<nAlloc ) break; /* Short read means EOF or an error */
10451 nAlloc *= 2;
10452 if( nAlloc>(sqlite3_int64)mxBlob+1 ) nAlloc = (sqlite3_int64)mxBlob+1;
10453 pNew = sqlite3_realloc64(pBuf, nAlloc);
10454 if( pNew==0 ){
10455 sqlite3_result_error_nomem(ctx);
10456 sqlite3_free(pBuf);
10457 fclose(in);
10458 return;
10459 }
10460 pBuf = pNew;
10461 }
10462 if( ferror(in) ){
10463 sqlite3_result_error_code(ctx, SQLITE_IOERR);
10464 sqlite3_free(pBuf);
10465 }else{
10466 sqlite3_result_blob64(ctx, pBuf, nRead, sqlite3_free);
10467 }
10468 fclose(in);
10469 }
10470
10471 /*
@@ -11088,17 +11143,42 @@
11143 i = 1;
11144 if( (idxNum & 0x02)!=0 ){
11145 assert( argc>i );
11146 pCur->zBase = (const char*)sqlite3_value_text(argv[i++]);
11147 }
11148 pCur->mxLvl = 1000000000;
11149 if( (idxNum & 0x0c)!=0 ){
11150 /* The LEVEL constraint is only used to limit the depth of the
11151 ** search. SQLite still checks the constraint against every row, so
11152 ** mxLvl may admit rows that do not match, but must never exclude
11153 ** a row that does. Only entries with level<mxLvl are visited. */
11154 sqlite3_value *pMx;
11155 assert( argc>i );
11156 pMx = argv[i++];
11157 switch( sqlite3_value_type(pMx) ){
11158 case SQLITE_NULL: {
11159 /* No row can match a NULL constraint */
11160 pCur->mxLvl = 1;
11161 break;
11162 }
11163 case SQLITE_INTEGER:
11164 case SQLITE_FLOAT: {
11165 double r = sqlite3_value_double(pMx);
11166 if( r<1.0 ){
11167 pCur->mxLvl = 1;
11168 }else if( r<1.0e9 ){
11169 int iMx = (int)r;
11170 if( (idxNum & 0x08)!=0 || (double)iMx<r ) iMx++;
11171 pCur->mxLvl = iMx;
11172 }
11173 break;
11174 }
11175 default: {
11176 /* TEXT or BLOB. Do not limit the search. */
11177 break;
11178 }
11179 }
11180 }
11181 if( pCur->zBase ){
11182 pCur->nBase = (int)strlen(pCur->zBase)+1;
11183 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
11184 }else{
@@ -11127,11 +11207,15 @@
11207 **
11208 ** The query plan is represented by bits in idxNum:
11209 **
11210 ** 0x01 The path value is supplied by argv[0]
11211 ** 0x02 dir is in argv[1]
11212 ** 0x04 LEVEL< bound is in argv[1] or [2]
11213 ** 0x08 LEVEL<= or LEVEL= bound is in argv[1] or [2]
11214 **
11215 ** The LEVEL constraint is never omitted. It is used only to limit the
11216 ** depth of the directory search, and SQLite rechecks it for each row.
11217 */
11218 static int fsdirBestIndex(
11219 sqlite3_vtab *tab,
11220 sqlite3_index_info *pIdxInfo
11221 ){
@@ -11138,11 +11222,10 @@
11222 int i; /* Loop over constraints */
11223 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
11224 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
11225 int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */
11226 int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */
 
11227 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
11228 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
11229 const struct sqlite3_index_constraint *pConstraint;
11230
11231 (void)tab;
@@ -11170,11 +11253,10 @@
11253 }
11254 case FSDIR_COLUMN_LEVEL: {
11255 if( pConstraint->usable && idxLevel<0 ){
11256 idxLevel = i;
11257 idxLevelEQ = 0x08;
 
11258 }
11259 break;
11260 }
11261 }
11262 }else
@@ -11183,15 +11265,13 @@
11265 && idxLevel<0
11266 ){
11267 if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
11268 idxLevel = i;
11269 idxLevelEQ = 0x08;
 
11270 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
11271 idxLevel = i;
11272 idxLevelEQ = 0x04;
 
11273 }
11274 }
11275 }
11276 if( seenPath || seenDir ){
11277 /* If input parameters are unusable, disallow this plan */
@@ -11214,11 +11294,10 @@
11294 pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++;
11295 pIdxInfo->idxNum |= 0x02;
11296 pIdxInfo->estimatedCost /= 1.0e4;
11297 }
11298 if( idxLevel>=0 ){
 
11299 pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++;
11300 pIdxInfo->idxNum |= idxLevelEQ;
11301 pIdxInfo->estimatedCost /= 1.0e4;
11302 }
11303 }
@@ -12775,10 +12854,11 @@
12854 "z HIDDEN" /* 7: Name of zip file */
12855 ") WITHOUT ROWID;";
12856
12857 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
12858 #define ZIPFILE_MX_NAME (250) /* Windows limitation on filename size */
12859 #define ZIPFILE_MX_ENTRY (65535) /* Max entries without zip64 extensions */
12860
12861 /*
12862 ** The buffer should be large enough to contain 3 65536 byte strings - the
12863 ** filename, the extra field and the file comment.
12864 */
@@ -14403,11 +14483,13 @@
14483
14484 /* Check that we're not inserting a duplicate entry -OR- updating an
14485 ** entry with a path, thereby making it into a duplicate. */
14486 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
14487 ZipfileEntry *p;
14488 int nEntry = 0;
14489 for(p=pTab->pFirstEntry; p; p=p->pNext){
14490 nEntry++;
14491 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
14492 switch( sqlite3_vtab_on_conflict(pTab->db) ){
14493 case SQLITE_IGNORE: {
14494 goto zipfile_update_done;
14495 }
@@ -14422,10 +14504,17 @@
14504 }
14505 }
14506 break;
14507 }
14508 }
14509
14510 /* The EOCD record stores the number of entries as a 16-bit value.
14511 ** Refuse to add an entry that would cause it to overflow. */
14512 if( p==0 && pOld==0 && nEntry>=ZIPFILE_MX_ENTRY ){
14513 zipfileTableErr(pTab, "too many entries; max: %d", ZIPFILE_MX_ENTRY);
14514 rc = SQLITE_CONSTRAINT;
14515 }
14516 }
14517
14518 if( rc==SQLITE_OK ){
14519 /* Create the new CDS record. */
14520 pNew = zipfileNewEntry(zPath);
@@ -14555,10 +14644,11 @@
14644 }
14645
14646 /* Write out the EOCD record */
14647 eocd.iDisk = 0;
14648 eocd.iFirstDisk = 0;
14649 assert( nEntry<=ZIPFILE_MX_ENTRY );
14650 eocd.nEntry = (u16)nEntry;
14651 eocd.nEntryTotal = (u16)nEntry;
14652 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
14653 eocd.iOffset = (u32)iOffset;
14654 rc = zipfileAppendEOCD(pTab, &eocd);
@@ -14747,10 +14837,16 @@
14837 "filename argument to zipfile() too big; max: %d bytes",
14838 ZIPFILE_MX_NAME);
14839 rc = SQLITE_ERROR;
14840 goto zipfile_step_out;
14841 }
14842 if( p->nEntry>=ZIPFILE_MX_ENTRY ){
14843 zErr = sqlite3_mprintf("too many entries for zipfile(); max: %d",
14844 ZIPFILE_MX_ENTRY);
14845 rc = SQLITE_ERROR;
14846 goto zipfile_step_out;
14847 }
14848
14849 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
14850 ** deflate compression) or NULL (choose automatically). */
14851 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
14852 iMethod = (int)sqlite3_value_int64(pMethod);
@@ -14874,10 +14970,11 @@
14970
14971 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
14972 if( p==0 ) return;
14973 if( p->nEntry>0 ){
14974 memset(&eocd, 0, sizeof(eocd));
14975 assert( p->nEntry<=ZIPFILE_MX_ENTRY );
14976 eocd.nEntry = (u16)p->nEntry;
14977 eocd.nEntryTotal = (u16)p->nEntry;
14978 eocd.nSize = p->cds.n;
14979 eocd.iOffset = p->body.n;
14980
@@ -20235,11 +20332,16 @@
20332 sqlite3_str_appendchar(p->pOut, 1, '\n');
20333 return;
20334 }else{
20335 r = num*100.0/(double)denom;
20336 }
20337 if( r>0.0 && r<0.0001 ){
20338 /* Avoid exponential notation for very small percentages */
20339 sqlite3_snprintf(sizeof(zNum)-5, zNum, "<0.0001");
20340 }else{
20341 sqlite3_snprintf(sizeof(zNum)-5, zNum, r>=10.0 ? "%.3g" :"%.2g", r);
20342 }
20343 sz = (int)strlen(zNum);
20344 zDP = strchr(zNum, '.');
20345 if( zDP==0 ){
20346 memcpy(zNum+sz,".0",3);
20347 nLeadingDigit = sz;
@@ -21717,15 +21819,43 @@
21819 int n = ((eType-12) / 2);
21820 if( eType % 2 ){
21821 switch( enc ){
21822 #ifndef SQLITE_OMIT_UTF16
21823 case SQLITE_UTF16BE:
21824 case SQLITE_UTF16LE: {
21825 /* sqlite3_result_text16be/le() remove a leading BOM. If
21826 ** the stored text begins with U+FEFF or U+FFFE, prepend
21827 ** a BOM in the database byte order, so that only the
21828 ** added BOM is removed. */
21829 u8 *aBuf = 0;
21830 if( n>=2 && ((pData[0]==0xFE && pData[1]==0xFF)
21831 || (pData[0]==0xFF && pData[1]==0xFE))
21832 ){
21833 aBuf = (u8*)sqlite3_malloc64(n+2);
21834 if( aBuf==0 ){
21835 sqlite3_result_error_nomem(pCtx);
21836 break;
21837 }
21838 aBuf[0] = (enc==SQLITE_UTF16BE) ? 0xFE : 0xFF;
21839 aBuf[1] = (enc==SQLITE_UTF16BE) ? 0xFF : 0xFE;
21840 memcpy(&aBuf[2], pData, n);
21841 }
21842 if( enc==SQLITE_UTF16BE ){
21843 if( aBuf ){
21844 sqlite3_result_text16be(pCtx, aBuf, n+2, sqlite3_free);
21845 }else{
21846 sqlite3_result_text16be(pCtx, pData, n, SQLITE_TRANSIENT);
21847 }
21848 }else{
21849 if( aBuf ){
21850 sqlite3_result_text16le(pCtx, aBuf, n+2, sqlite3_free);
21851 }else{
21852 sqlite3_result_text16le(pCtx, pData, n, SQLITE_TRANSIENT);
21853 }
21854 }
21855 break;
21856 }
21857 #endif
21858 default:
21859 sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
21860 break;
21861 }
@@ -22294,10 +22424,16 @@
22424 **
22425 ** nCol/aCol[]:
22426 ** aCol[] is an array of nCol columns. In the order in which they appear
22427 ** in the table.
22428 **
22429 ** nField:
22430 ** The number of fields in each record of the table's b-tree. This may
22431 ** be less than nCol (VIRTUAL generated columns have no field) or, for
22432 ** a WITHOUT ROWID table with a column that appears more than once in
22433 ** the PRIMARY KEY, greater than nCol.
22434 **
22435 ** bIntkey:
22436 ** Set to true for intkey tables, false for WITHOUT ROWID.
22437 **
22438 ** iRowidBind:
22439 ** Each column in the aCol[] array has associated with it the index of
@@ -22318,10 +22454,11 @@
22454 */
22455 struct RecoverTable {
22456 u32 iRoot; /* Root page in original database */
22457 char *zTab; /* Name of table */
22458 int nCol; /* Number of columns in table */
22459 int nField; /* Number of fields in each record */
22460 RecoverColumn *aCol; /* Array of columns */
22461 int bIntkey; /* True for intkey, false for without rowid */
22462 int iRowidBind; /* If >0, bind rowid to INSERT here */
22463 RecoverTable *pNext;
22464 };
@@ -22401,11 +22538,11 @@
22538 sqlite3_stmt *pSel;
22539 sqlite3_stmt *pInsert;
22540 int nInsert;
22541
22542 RecoverTable *pTab; /* Table currently being written */
22543 int nMax; /* Max field count in any schema table */
22544 sqlite3_value **apVal; /* Array of nMax values */
22545 int nVal; /* Number of valid entries in apVal[] */
22546 int bHaveRowid;
22547 i64 iRowid;
22548 i64 iPrevPage;
@@ -22647,11 +22784,11 @@
22784
22785 /*
22786 ** Set the bit associated with page iPg in bitvec pMap.
22787 */
22788 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
22789 if( iPg<=pMap->nPg && iPg>0 ){
22790 int iElem = (iPg / 32);
22791 int iBit = (iPg % 32);
22792 pMap->aElem[iElem] |= (((u32)1) << iBit);
22793 }
22794 }
@@ -23361,26 +23498,36 @@
23498 }
23499 memcpy(csr, z, n);
23500 csr += (n+1);
23501 }
23502
23503 pNew->nField = iField;
23504 pNew->pNext = p->pTblList;
23505 p->pTblList = pNew;
23506 pNew->bIntkey = 1;
23507 }
23508
23509 recoverFinalize(p, pStmt);
23510
23511 /* If this is a WITHOUT ROWID table, the fields of each record are
23512 ** ordered as reported by PRAGMA index_xinfo on the table. A column may
23513 ** appear more than once if it is listed more than once in the PRIMARY
23514 ** KEY (with different collation sequences). In that case use the first
23515 ** field - the others are redundant copies. */
23516 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
23517 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
23518 int iField = sqlite3_column_int(pStmt, 0);
23519 int iCol = sqlite3_column_int(pStmt, 1);
23520
23521 assert( iCol>=0 && iCol<pNew->nCol );
23522 if( pNew->bIntkey ){
23523 int ii;
23524 for(ii=0; ii<pNew->nCol; ii++) pNew->aCol[ii].iField = -1;
23525 pNew->bIntkey = 0;
23526 }
23527 if( pNew->aCol[iCol].iField<0 ) pNew->aCol[iCol].iField = iField;
23528 pNew->nField = iField+1;
23529 iPk = -2;
23530 }
23531 recoverFinalize(p, pStmt);
23532
23533 if( p->errCode==SQLITE_OK ){
@@ -23566,11 +23713,11 @@
23713 int ii;
23714 int bSql = p->xSql ? 1 : 0;
23715
23716 if( nField<=0 ) return 0;
23717
23718 assert( nField<=pTab->nField );
23719
23720 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
23721
23722 if( pTab->iRowidBind ){
23723 assert( pTab->bIntkey );
@@ -23582,16 +23729,22 @@
23729 }
23730 zSqlSep = "||', '||";
23731 zSep = ", ";
23732 }
23733
23734 /* Include each column that has a bind parameter and either has a field
23735 ** in the nField-field record or is the IPK. This must match the set of
23736 ** parameters bound by recoverWriteDataStep(). Note that nField counts
23737 ** record fields, not table columns - VIRTUAL generated columns have no
23738 ** field (iField<0), and the order of fields need not match the order of
23739 ** columns (WITHOUT ROWID tables). */
23740 for(ii=0; ii<pTab->nCol; ii++){
23741 RecoverColumn *pCol = &pTab->aCol[ii];
23742 if( pCol->iBind>0 && (pCol->iField<nField || pCol->bIPK) ){
23743 assert( pCol->iField>=0 );
23744 assert( pCol->eHidden!=RECOVER_EHIDDEN_VIRTUAL );
23745 assert( pCol->eHidden!=RECOVER_EHIDDEN_STORED );
23746 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
23747
23748 if( bSql ){
23749 zBind = recoverMPrintf(p,
23750 "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
@@ -23927,14 +24080,14 @@
24080 static int recoverWriteDataInit(sqlite3_recover *p){
24081 RecoverStateW1 *p1 = &p->w1;
24082 RecoverTable *pTbl = 0;
24083 int nByte = 0;
24084
24085 /* Figure out the maximum number of fields for any table in the schema */
24086 assert( p1->nMax==0 );
24087 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
24088 if( pTbl->nField>p1->nMax ) p1->nMax = pTbl->nField;
24089 }
24090
24091 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
24092 ** that will be written to the output database in a single row. */
24093 nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
@@ -24037,11 +24190,11 @@
24190 int iField = sqlite3_column_int(pSel, 2);
24191 sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
24192 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
24193
24194 assert( bNewCell==0 || (iField==-1 || iField==0) );
24195 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nField );
24196
24197 if( bNewCell ){
24198 int ii = 0;
24199 if( p1->nVal>=0 ){
24200 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
@@ -24087,19 +24240,19 @@
24240 if( iField<0 ){
24241 p1->iRowid = sqlite3_column_int64(pSel, 3);
24242 assert( p1->nVal==-1 );
24243 p1->nVal = 0;
24244 p1->bHaveRowid = 1;
24245 }else if( iField<pTab->nField ){
24246 assert( apVal[iField]==0 );
24247 apVal[iField] = sqlite3_value_dup( pVal );
24248 if( apVal[iField]==0 ){
24249 recoverError(p, SQLITE_NOMEM, 0);
24250 }
24251 p1->nVal = iField+1;
24252 }else if( pTab->nField==0 ){
24253 p1->nVal = pTab->nField;
24254 }
24255 p1->iPrevCell = iCell;
24256 p1->iPrevPage = iPage;
24257 }
24258 }else{
@@ -25235,10 +25388,15 @@
25388 typedef struct Unlink {
25389 sqlite3_int64 tm; /* Unlink after this time */
25390 char *zFN; /* Name of file. Space from sqlite3_malloc() */
25391 } Unlink;
25392
25393 /*
25394 ** Maximum nested depth for .if statements.
25395 */
25396 #define MX_IF 10
25397
25398 /*
25399 ** State information about the database connection is contained in an
25400 ** instance of the following structure.
25401 */
25402 typedef struct ShellState ShellState;
@@ -25275,10 +25433,12 @@
25433 unsigned flgProgress; /* Flags for the progress callback */
25434 unsigned shellFlgs; /* Various flags */
25435 unsigned nTestRun; /* Number of test cases run */
25436 unsigned nTestErr; /* Number of test cases that failed */
25437 sqlite3_int64 szMax; /* --maxsize argument to .open */
25438 char *zInput; /* Simulated inputs */
25439 i64 nInputUsed; /* Simulated input read so far */
25440 char *zDestTable; /* Name of destination table when MODE_Insert */
25441 char *zTempFile; /* Temporary file that might need deleting */
25442 char *zErrPrefix; /* Alternative error message prefix */
25443 char *zAuthIgnore; /* Authorizer returns SQLITE_IGNORE for glob matches */
25444 char *zAuthDeny; /* Authorizer returns SQLITE_DENY for glob matches */
@@ -25317,10 +25477,17 @@
25477 int nArg; /* Number of argument slots actually used */
25478 char **azArg; /* Pointer to each argument, dequoted */
25479 int *aiOfst; /* Offset into zOrig[] for start of each arg */
25480 char *abQuot; /* True if the argment was originally quoted */
25481 } dot;
25482 u32 mIf; /* Mask of false if conditions */
25483 u32 mElse; /* An else seen at this level */
25484 u32 mIfHasBeenTrue; /* True branch seen in if-elseif-elseif-else chain */
25485 int nIfLevel; /* Current number of if levels */
25486 int nIfAtStart; /* If level at start of current script */
25487 i64 lnIf[MX_IF]; /* Line number starting an if */
25488 i64 lnElse[MX_IF]; /* Line number of the else */
25489 #ifdef SQLITE_SHELL_FIDDLE
25490 struct {
25491 const char * zInput; /* Input string from wasm/JS proxy */
25492 const char * zPos; /* Cursor pos into zInput */
25493 const char * zDefaultDbName; /* Default name for db file */
@@ -25629,11 +25796,11 @@
25796 }
25797 #endif
25798 static void cli_exit(int rc){
25799 if( cli_output_capture ){
25800 char *z = sqlite3_str_finish(cli_output_capture);
25801 sqlite3_fputs(z ? z : "", stdout);
25802 fflush(stdout);
25803 }
25804 exit(rc);
25805 }
25806
@@ -25759,10 +25926,29 @@
25926 return 0;
25927 }
25928 #endif
25929 #undef STAT_CHR_SRC
25930 }
25931
25932 /*
25933 ** Works like fgets except that if "in" really points to p->zInput
25934 ** then text is read out of the p->zInput buffer.
25935 */
25936 static char *shell_fgets(ShellState *p, char *zOut, int nOut, FILE *in){
25937 int i, j;
25938 char *zIn;
25939 if( p==0 || in!=(FILE*)&p->zInput ){
25940 return sqlite3_fgets(zOut, nOut, in);
25941 }
25942 zIn = p->zInput;
25943 for(i=0, j=p->nInputUsed; i<nOut-1 && zIn[j] && zIn[j]!='\n'; i++, j++){}
25944 if( zIn[j] ){ i++; }
25945 memcpy(zOut, &zIn[p->nInputUsed], i);
25946 zOut[i] = 0;
25947 p->nInputUsed += i;
25948 return i>0 ? zOut : 0;
25949 }
25950
25951 /*
25952 ** This routine reads a line of text from FILE in, stores
25953 ** the text in memory obtained from malloc() and returns a pointer
25954 ** to the text. NULL is returned at end of file, or if malloc()
@@ -25769,11 +25955,11 @@
25955 ** fails, or if the length of the line is longer than about a gigabyte.
25956 **
25957 ** If zLine is not NULL then it is a malloced buffer returned from
25958 ** a previous call to this routine that may be reused.
25959 */
25960 static char *local_getline(ShellState *p, char *zLine, FILE *in){
25961 int nLine = zLine==0 ? 0 : 100;
25962 int n = 0;
25963
25964 while( seenInterrupt<2 ){
25965 if( n+100>nLine ){
@@ -25783,11 +25969,11 @@
25969 }
25970 nLine = nLine*2 + 100;
25971 zLine = realloc(zLine, nLine);
25972 shell_check_oom(zLine);
25973 }
25974 if( shell_fgets(p, &zLine[n], nLine - n, in)==0 ){
25975 if( n==0 ){
25976 free(zLine);
25977 return 0;
25978 }
25979 zLine[n] = 0;
@@ -26397,13 +26583,12 @@
26583 char *zPrior, /* Just the previous line */
26584 const char *zAll, /* All statement text accumulated so far */
26585 int bContinue /* True for a continuation prompt */
26586 ){
26587 char *zResult;
26588 if( p->in!=0 ){
26589 zResult = local_getline(p, zPrior, p->in);
 
26590 }else{
26591 const char *zBase = prompt_string(p, bContinue!=0);
26592 char *zPrompt = expand_prompt(p, zAll, zBase);
26593 shell_check_oom(zPrompt);
26594 #if SHELL_CMDLINE_EDIT_AVAILABLE
@@ -26421,11 +26606,11 @@
26606 #endif
26607 {
26608 sputz(stdout, zPrompt);
26609 fflush(stdout);
26610 do{
26611 zResult = local_getline(p, zPrior, stdin);
26612 zPrior = 0;
26613 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
26614 if( zResult==0 ) sqlite3_sleep(50);
26615 }while( zResult==0 && seenInterrupt>0 );
26616 }
@@ -26637,10 +26822,76 @@
26822 }else{
26823 while( s.zTxt && (zCom = strstr(s.zTxt,"*/"))!=0 ){ zCom[1] = '*'; }
26824 }
26825 return s.zTxt;
26826 }
26827
26828 /*
26829 ** SQL function: capable(X)
26830 **
26831 ** Return true if the current build has capability X. Return 0
26832 ** if X is not a capability or NULL if X is NULL.
26833 **
26834 ** This function is intended to be used with the ".if" command
26835 ** to bypass test cases that use features that are not part of
26836 ** the build. Examples:
26837 **
26838 ** .if capable('fts5')
26839 ** # do tests that require the FTS5 extension
26840 ** .endif
26841 **
26842 ** .if capable('generate_series')
26843 ** # do tests that require the generate_series() table-valued functions
26844 ** .endif
26845 */
26846 static void shellCapable(
26847 sqlite3_context *pCtx,
26848 int nVal,
26849 sqlite3_value **apVal
26850 ){
26851 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
26852 const char *z = (const char*)sqlite3_value_text(apVal[0]);
26853 static const char *azSql[] = {
26854 "PRAGMA compile_options",
26855 "PRAGMA module_list",
26856 "PRAGMA function_list"
26857 };
26858 static const char *azInversions[] = {
26859 "vtab", "OMIT_VIRTUALTABLE",
26860 "fkey", "OMIT_FOREIGN_KEY",
26861 "load_ext", "OMIT_LOAD_EXTENSION",
26862 };
26863 size_t i;
26864 int invert = 0;
26865 int res = 0;
26866
26867 UNUSED_PARAMETER(nVal);
26868 if( z==0 ) return;
26869 for(i=0; i<sizeof(azInversions)/sizeof(azInversions[0])-1; i+=2){
26870 if( sqlite3_stricmp(z, azInversions[i])==0 ){
26871 z = azInversions[i+1];
26872 invert = 1;
26873 break;
26874 }
26875 }
26876 for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){
26877 sqlite3_stmt *pStmt = 0;
26878 sqlite3_prepare_v2(p->db, azSql[i], -1, &pStmt, 0);
26879 while( sqlite3_step(pStmt)==SQLITE_ROW ){
26880 const char *zRow = (const char*)sqlite3_column_text(pStmt, 0);
26881 if( sqlite3_stricmp(zRow, z)==0 ){
26882 res = 1;
26883 break;
26884 }
26885 }
26886 sqlite3_finalize(pStmt);
26887 if( res ) break;
26888 }
26889 if( invert ) res = !res;
26890 sqlite3_result_int(pCtx, res);
26891 }
26892
26893
26894 /*
26895 ** SQL function: strtod(X)
26896 **
26897 ** For use internally and for debugging and testing only. This function
@@ -29112,10 +29363,12 @@
29363 /* Build an appropriate SELECT statement */
29364 initText(&sSelect);
29365 appendText(&sSelect, "SELECT ", 0);
29366 if( azCol[0] ){
29367 appendText(&sSelect, azCol[0], 0);
29368 appendText(&sSelect, " AS ", 0);
29369 appendText(&sSelect, azCol[0], 0);
29370 appendText(&sSelect, ",", 0);
29371 }
29372 for(i=1; azCol[i]; i++){
29373 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
29374 if( azCol[i+1] ){
@@ -29132,10 +29385,13 @@
29385 p->mode.eMode = MODE_Insert;
29386 p->mode.spec.eText = QRF_TEXT_Sql;
29387 p->mode.spec.eBlob = QRF_BLOB_Sql;
29388 p->mode.spec.bTitles = QRF_No;
29389 p->mode.spec.nCharLimit = 0;
29390 p->mode.spec.bTextJsonb = QRF_No;
29391 p->mode.spec.zIFmt = 0;
29392 p->mode.spec.zFpFmt = 0;
29393 rc = shell_exec(p, sSelect.zTxt, 0);
29394 if( (rc&0xff)==SQLITE_CORRUPT ){
29395 cli_puts("/****** CORRUPTION ERROR *******/\n", p->out);
29396 toggleSelectOrder(p->db);
29397 shell_exec(p, sSelect.zTxt, 0);
@@ -29470,10 +29726,21 @@
29726 ** "Usage" help text automatically generated from comments */
29727 static const struct {
29728 const char *zCmd; /* Name of the dot-command */
29729 const char *zUsage; /* Documentation */
29730 } aUsage[] = {
29731 { ".clone",
29732 "USAGE: .clone FILE\n"
29733 "\n"
29734 "The original purpose of the \".clone\" command (when it was first introduced\n"
29735 "in 2014) was to try to recover content for a corrupt database file and\n"
29736 "write the recovered content into a new database named FILE. However,\n"
29737 "the command has always been experimental and undocumented. Today, it\n"
29738 "does nothing more than make a copy of the main database into a new\n"
29739 "database named by its only argument, FILE. No repair attempts are\n"
29740 "made. The command is retained for legacy only.\n"
29741 },
29742 { ".import",
29743 "USAGE: .import [OPTIONS] FILE TABLE\n"
29744 "\n"
29745 "Import CSV or similar text from FILE into TABLE. If TABLE does\n"
29746 "not exist, it is created using the first row of FILE as the column\n"
@@ -29677,10 +29944,41 @@
29944 "\".check\" command for additional informatioon.\n"
29945 "\n"
29946 "Options:\n"
29947 " --error-prefix TEXT Change error message prefix text to TEXT\n"
29948 },
29949 { ".if",
29950 "USAGE: .if SQL-EXPRESSION\n"
29951 "\n"
29952 "The argument can be any SQL expression, including a subquery in\n"
29953 "parentheses. If the expression evalutes to true (if it is non-zero\n"
29954 "when converted into an number) then subsequent statements are\n"
29955 "evaluated. If SQL-EXPRESSION evalutes to false or NULL or raises\n"
29956 "an error, then subsequent statements are skipped, up until the first\n"
29957 "matching .elseif, .else, or .endif.\n"
29958 "\n"
29959 "Maximum nested depth is limited. An error is raised if .if statements\n"
29960 "are nested to deep.\n"
29961 "\n"
29962 "Every .if must be followed by a matching .endif, or an error is raised.\n"
29963 },
29964 { ".elseif",
29965 "USAGE: .elseif SQL-EXPRESSION\n"
29966 "\n"
29967 "An \"else-if\". The prior .if is finished and a new condition is applied.\n"
29968 },
29969 { ".else",
29970 "USAGE: .else\n"
29971 "\n"
29972 "Invert the sense of the inner-most .if. Start processing if the prior\n"
29973 ".if was false. Stop processing if the prior .if was true.\n"
29974 },
29975 { ".endif",
29976 "USAGE: .endif\n"
29977 "\n"
29978 "Terminate an .if statement.\n"
29979 },
29980 };
29981
29982 /*
29983 ** Return a pointer to usage text for zCmd, or NULL if none exists.
29984 */
@@ -29834,20 +30132,25 @@
30132 cli_printf(stderr,"Error: '%s' not seekable\n", zName);
30133 fclose(in);
30134 return 0;
30135 }
30136 nIn = ftell(in);
30137 if( nIn<0 ){
30138 cli_printf(stderr,"Error: cannot read '%s'\n", zName);
30139 fclose(in);
30140 return 0;
30141 }
30142 rewind(in);
30143 pBuf = sqlite3_malloc64( nIn+1 );
30144 if( pBuf==0 ){
30145 cli_puts("Error: out of memory\n", stderr);
30146 fclose(in);
30147 return 0;
30148 }
30149 nRead = fread(pBuf, 1, nIn, in);
30150 fclose(in);
30151 if( nRead!=(size_t)nIn ){
30152 sqlite3_free(pBuf);
30153 cli_printf(stderr,"Error: cannot read '%s'\n", zName);
30154 return 0;
30155 }
30156 pBuf[nIn] = 0;
@@ -30060,11 +30363,11 @@
30363 nLine = p->lineno;
30364 if( in==0 ) in = stdin;
30365 }
30366 *pnData = 0;
30367 nLine++;
30368 if( shell_fgets(p, zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
30369 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
30370 if( rc!=2 ) goto readHexDb_error;
30371 if( n<0 ) goto readHexDb_error;
30372 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
30373 cli_puts("invalid pagesize\n", stderr);
@@ -30072,11 +30375,11 @@
30375 }
30376 sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
30377 a = sqlite3_malloc64( sz ? sz : 1 );
30378 shell_check_oom(a);
30379 memset(a, 0, sz);
30380 for(nLine++; shell_fgets(p, zLine, sizeof(zLine), in)!=0; nLine++){
30381 int j = 0; /* Page number from "| page" line */
30382 int k = 0; /* Offset from "| page" line */
30383 if( nLine>=2000000000 ){
30384 cli_printf(stderr, "input too big\n");
30385 goto readHexDb_error;
@@ -30110,11 +30413,11 @@
30413
30414 readHexDb_error:
30415 if( in!=p->in ){
30416 fclose(in);
30417 }else{
30418 while( shell_fgets(p, zLine, sizeof(zLine), p->in)!=0 ){
30419 nLine++;
30420 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
30421 }
30422 p->lineno = nLine;
30423 }
@@ -30336,10 +30639,12 @@
30639 #undef SHELL_SUB_MACRO
30640 #undef SHELL_SUBMACRO
30641 }
30642 #endif
30643
30644 sqlite3_create_function(p->db, "capable", 1, SQLITE_UTF8, p,
30645 shellCapable, 0, 0);
30646 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
30647 shellStrtod, 0, 0);
30648 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
30649 shellDtostr, 0, 0);
30650 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
@@ -30912,227 +31217,48 @@
31217 p->cTerm = c;
31218 if( p->z ) p->z[p->n] = 0;
31219 return p->z;
31220 }
31221
31222 #ifndef SQLITE_SHELL_FIDDLE
31223 /*
31224 ** DOT-COMMAND: .clone
31225 **
31226 ** USAGE: .clone FILE
31227 **
31228 ** The original purpose of the ".clone" command (when it was first introduced
31229 ** in 2014) was to try to recover content for a corrupt database file and
31230 ** write the recovered content into a new database named FILE. However,
31231 ** the command has always been experimental and undocumented. Today, it
31232 ** does nothing more than make a copy of the main database into a new
31233 ** database named by its only argument, FILE. No repair attempts are
31234 ** made. The command is retained for legacy only.
31235 */
31236 static int dotCmdClone(ShellState *p){
31237 int nArg = p->dot.nArg; /* Number of arguments */
31238 char **azArg = p->dot.azArg;/* Argument list */
31239 char *zSql;
31240 sqlite3_stmt *pStmt = 0;
31241 int rc;
31242
31243 failIfSafeMode(p, "cannot run .clone in safe mode");
31244 if( nArg!=2 ){
31245 dotCmdError(p, nArg, 0, "missing FILE argument");
31246 return 1;
31247 }
31248 zSql = sqlite3_mprintf("VACUUM INTO %Q", azArg[1]);
31249 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
31250 sqlite3_free(zSql);
31251 if( rc!=SQLITE_OK || SQLITE_DONE!=sqlite3_step(pStmt) ){
31252 cli_printf(stderr,"Error: cannot clone into \"%s\"\n", azArg[1]);
31253 sqlite3_finalize(pStmt);
31254 return 1;
31255 }
31256 sqlite3_finalize(pStmt);
31257 return 0;
31258 }
31259 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31260
31261 #ifndef SQLITE_SHELL_FIDDLE
31262 /*
31263 ** Change the output stream (file or pipe or console) to something else.
31264 */
@@ -32493,11 +32619,11 @@
32619 arWhereClause(&rc, pAr, &zWhere);
32620
32621 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
32622 pAr->zSrcTable, zWhere);
32623 if( pAr->bDryRun ){
32624 if( pSql ) cli_printf(pAr->out, "%s\n", sqlite3_sql(pSql));
32625 }else{
32626 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
32627 if( pAr->bVerbose ){
32628 cli_printf(pAr->out, "%s % 10d %s %s\n",
32629 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
@@ -33553,11 +33679,11 @@
33679 i64 iStart = p->lineno;
33680 char zLine[2000];
33681 sCtx.zFile = p->zInFile;
33682 sCtx.nLine = p->lineno+1;
33683 iLineOffset = p->lineno;
33684 while( shell_fgets(p, zLine,sizeof(zLine),p->in) ){
33685 if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){
33686 ckEnd = 2;
33687 if( strchr(zLine,'\n') ) p->lineno++;
33688 break;
33689 }
@@ -34627,11 +34753,11 @@
34753 dotCmdError(p, i, "incompatible with prior options",0);
34754 goto dotCmdOutput_error;
34755 }
34756 eMode = z[1];
34757 }else if( cli_strcmp(z,"-show")==0 ){
34758 if( sqlite3_str_length(cli_output_capture)>0 ){
34759 sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
34760 }
34761 }else if( cli_strcmp(z,"-keep")==0 ){
34762 bKeep = 1;
34763 }else if( optionMatch(z,"error-prefix") ){
@@ -34762,10 +34888,34 @@
34888
34889 dotCmdOutput_error:
34890 sqlite3_free(zFile);
34891 return 1;
34892 }
34893
34894 /*
34895 ** Return true if the argument is <<MARK for some arbitrary mark
34896 */
34897 static int isHereDoc(const char *z){
34898 return z[0]=='<' && z[1]=='<' && z[2]!=0;
34899 }
34900
34901 /*
34902 ** Capture lines of input text up until a line that matches zTerminator.
34903 ** Return a memory buffer (allocated using sqlite3_malloc64()) that
34904 ** contains the complete text, without the terminator.
34905 */
34906 static char *captureHereDoc(ShellState *p, const char *zTerminator){
34907 int nTerminator = strlen30(zTerminator);
34908 sqlite3_str *pHeredoc = sqlite3_str_new(p->db);
34909 char zLine[2000];
34910 while( shell_fgets(p, zLine,sizeof(zLine),p->in) ){
34911 if( strchr(zLine,'\n') ) p->lineno++;
34912 if( cli_strncmp(zTerminator,zLine,nTerminator)==0 ) break;
34913 sqlite3_str_appendall(pHeredoc, zLine);
34914 }
34915 return sqlite3_str_finish(pHeredoc);
34916 }
34917
34918 /*
34919 ** DOT-COMMAND: .check
34920 ** USAGE: .check [OPTIONS] PATTERN
34921 **
@@ -34796,10 +34946,19 @@
34946 char *zPattern = 0; /* Actual test pattern */
34947 int eCheck = 0; /* 1: --glob, 2: --notglob, 3: --exact */
34948 int isOk; /* True if results are OK */
34949 sqlite3_int64 iStart = p->lineno; /* Line number of .check statement */
34950
34951 if( p->mIf!=0 ){
34952 /* Unlike most other dot-commands, the .check command runs even if
34953 ** inside the false branch of a .if. It needs to do this so that it
34954 ** can capture any "<<MARKER" heredoc text specified. */
34955 if( nArg>0 && isHereDoc(azArg[nArg-1]) ){
34956 sqlite3_free(captureHereDoc(p, azArg[nArg-1]+2));
34957 }
34958 return 0;
34959 }
34960 if( p->zTestcase[0]==0 ){
34961 dotCmdError(p, 0, "no .testcase is active", 0);
34962 return 1;
34963 }
34964 for(i=1; i<nArg; i++){
@@ -34806,11 +34965,11 @@
34965 char *z = azArg[i];
34966 if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++;
34967 if( cli_strcmp(z,"-keep")==0 ){
34968 bKeep = 1;
34969 }else if( cli_strcmp(z,"-show")==0 ){
34970 if( sqlite3_str_length(cli_output_capture)>0 ){
34971 sqlite3_fprintf(stdout, "%s", sqlite3_str_value(cli_output_capture));
34972 }
34973 bKeep = 1;
34974 }else if( z[0]=='-'
34975 && (k = pickStr(&z[1],0,"glob","notglob","exact",""))>=0
@@ -34829,27 +34988,19 @@
34988 }
34989 if( zCheck==0 ){
34990 dotCmdError(p, 0, "no PATTERN specified", 0);
34991 return 1;
34992 }
34993 if( sqlite3_str_length(cli_output_capture) ){
34994 zTest = sqlite3_str_value(cli_output_capture);
34995 shell_check_oom(zTest);
34996 }else{
34997 zTest = "";
34998 }
34999 p->nTestRun++;
35000 if( isHereDoc(zCheck) ){
35001 zPattern = captureHereDoc(p, zCheck+2);
 
 
 
 
 
 
 
 
35002 if( zPattern==0 ){
35003 zPattern = sqlite3_mprintf("");
35004 }
35005 }else{
35006 zPattern = zCheck;
@@ -35042,10 +35193,147 @@
35193 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s:%lld",
35194 p->zInFile, p->lineno);
35195 }
35196 return 0;
35197 }
35198
35199 /*
35200 ** Evaluate the text of zExpr as an SQL expression. Return true if the
35201 ** expression is true. Return false if the expression evaluates to
35202 ** false or NULL. Return false if there is any kind of error, either
35203 ** a run-time or a compile-time error.
35204 */
35205 static int sqlExprIsTrue(ShellState *p, const char *zExpr){
35206 char *zSql = sqlite3_mprintf("SELECT %s", zExpr);
35207 sqlite3_stmt *pStmt;
35208 int rc;
35209
35210 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
35211 sqlite3_free(zSql);
35212 if( rc ){
35213 rc = 0;
35214 }else{
35215 bind_prepared_stmt(p, pStmt);
35216 rc = sqlite3_step(pStmt);
35217 if( rc==SQLITE_ROW ){
35218 rc = sqlite3_column_double(pStmt,0)!=0.0;
35219 }else{
35220 rc = 0;
35221 }
35222 }
35223 sqlite3_finalize(pStmt);
35224 return rc;
35225 }
35226
35227 /*
35228 ** DOT-COMMAND: .if
35229 ** USAGE: .if SQL-EXPRESSION
35230 **
35231 ** The argument can be any SQL expression, including a subquery in
35232 ** parentheses. If the expression evalutes to true (if it is non-zero
35233 ** when converted into an number) then subsequent statements are
35234 ** evaluated. If SQL-EXPRESSION evalutes to false or NULL or raises
35235 ** an error, then subsequent statements are skipped, up until the first
35236 ** matching .elseif, .else, or .endif.
35237 **
35238 ** Maximum nested depth is limited. An error is raised if .if statements
35239 ** are nested to deep.
35240 **
35241 ** Every .if must be followed by a matching .endif, or an error is raised.
35242 */
35243 static int dotCmdIf(ShellState *p){
35244 int jj;
35245 if( p->nIfLevel==MX_IF ){
35246 dotCmdError(p, 0, 0, ".if nested too deep");
35247 return 1;
35248 }
35249 jj = p->nIfLevel++;
35250 open_db(p, 0);
35251 if( sqlExprIsTrue(p, p->dot.zOrig+3) ){
35252 p->mIfHasBeenTrue |= 1<<jj;
35253 }else{
35254 p->mIf |= 1<<jj;
35255 }
35256 p->lnIf[jj] = p->lineno;
35257 return 0;
35258 }
35259
35260 /*
35261 ** DOT-COMMAND: .elseif
35262 ** USAGE: .elseif SQL-EXPRESSION
35263 **
35264 ** An "else-if". The prior .if is finished and a new condition is applied.
35265 */
35266 static int dotCmdElseif(ShellState *p){
35267 int jj = p->nIfLevel-1;
35268 if( jj<p->nIfAtStart ){
35269 dotCmdError(p, 0, 0, ".elseif with no .if");
35270 return 1;
35271 }
35272 if( (p->mElse & (1<<jj))!=0 ){
35273 dotCmdError(p, 0, 0, ".elseif follows a .else on line %lld", p->lnElse[jj]);
35274 p->mIf |= 1<<jj;
35275 return 1;
35276 }
35277 if( p->mIfHasBeenTrue & (1<<jj) ){
35278 p->mIf |= 1<<jj;
35279 }else if( sqlExprIsTrue(p, p->dot.zOrig+7) ){
35280 p->mIf &= ~(1<<jj);
35281 p->mIfHasBeenTrue |= (1<<jj);
35282 }else{
35283 p->mIf |= 1<<jj;
35284 }
35285 return 0;
35286 }
35287
35288 /*
35289 ** DOT-COMMAND: .else
35290 ** USAGE: .else
35291 **
35292 ** Invert the sense of the inner-most .if. Start processing if the prior
35293 ** .if was false. Stop processing if the prior .if was true.
35294 */
35295 static int dotCmdElse(ShellState *p){
35296 int ii;
35297 if( p->nIfLevel<=p->nIfAtStart ){
35298 dotCmdError(p, 0, 0, ".else without a .if");
35299 return 1;
35300 }
35301 ii = p->nIfLevel-1;
35302 if( (p->mElse & (1<<ii))!=0 ){
35303 dotCmdError(p, 0, 0, "already saw a .else at line %lld", p->lnElse[ii]);
35304 return 1;
35305 }
35306 if( p->mIfHasBeenTrue & (1<<ii) ){
35307 p->mIf |= (1<<ii);
35308 }else{
35309 p->mIf &= ~(1<<ii);
35310 }
35311 p->mElse |= (1<<ii);
35312 p->lnElse[ii] = p->lineno;
35313 return 0;
35314 }
35315
35316 /*
35317 ** DOT-COMMAND: .endif
35318 ** USAGE: .endif
35319 **
35320 ** Terminate an .if statement.
35321 */
35322 static int dotCmdEndif(ShellState *p){
35323 if( p->nIfLevel<=p->nIfAtStart ){
35324 dotCmdError(p, 0, 0, ".endif without a .if");
35325 return 1;
35326 }
35327 p->nIfLevel--;
35328 p->mIf &= ~(1<<p->nIfLevel);
35329 p->mElse &= ~(1<<p->nIfLevel);
35330 p->mIfHasBeenTrue &= ~(1<<p->nIfLevel);
35331 return 0;
35332 }
35333
35334
35335
35336 /*
35337 ** Enlarge the space allocated in p->dot so that it can hold more
35338 ** than nArg parsed command-line arguments.
35339 */
@@ -35348,17 +35636,11 @@
35636 rc = dotCmdCheck(p);
35637 }else
35638
35639 #ifndef SQLITE_SHELL_FIDDLE
35640 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
35641 rc = dotCmdClone(p);
 
 
 
 
 
 
35642 }else
35643 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
35644
35645 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
35646 if( nArg==1 ){
@@ -35667,10 +35949,12 @@
35949 cli_puts(p->nErr?";\nROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
35950 }
35951 p->shellFlgs = savedShellFlags;
35952 modeFree(&p->mode);
35953 p->mode = saved_mode;
35954 if( p->mode.spec.zFpFmt ) p->mode.spec.zFpFmt = p->mode.zFpFmt;
35955 if( p->mode.spec.zIFmt ) p->mode.spec.zIFmt = p->mode.zIFmt;
35956 rc = p->nErr>0;
35957 }else
35958
35959 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
35960 if( nArg==2 ){
@@ -35682,10 +35966,22 @@
35966 }else{
35967 eputz("Usage: .echo on|off\n");
35968 rc = 1;
35969 }
35970 }else
35971
35972 if( c=='e' && n==6 && cli_strcmp(azArg[0],"elseif")==0 ){
35973 rc = dotCmdElseif(p);
35974 }else
35975
35976 if( c=='e' && n==4 && cli_strcmp(azArg[0],"else")==0 ){
35977 rc = dotCmdElse(p);
35978 }else
35979
35980 if( c=='e' && n==5 && cli_strcmp(azArg[0],"endif")==0 ){
35981 rc = dotCmdEndif(p);
35982 }else
35983
35984 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
35985 if( nArg==2 ){
35986 if( p->mode.autoEQPtrace ){
35987 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
@@ -35981,10 +36277,14 @@
36277 }
36278 }else{
36279 showHelp(p->out, 0);
36280 }
36281 }else
36282
36283 if( c=='i' && n==2 && cli_strcmp(azArg[0],"if")==0 ){
36284 rc = dotCmdIf(p);
36285 }else
36286
36287 #ifndef SQLITE_SHELL_FIDDLE
36288 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
36289 rc = dotCmdImport(p);
36290 }else
@@ -36238,10 +36538,11 @@
36538 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
36539 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
36540 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
36541 { "schema", SQLITE_LIMIT_SCHEMA },
36542 { "trigger_steps", SQLITE_LIMIT_TRIGGER_STEPS },
36543 { "ctes", SQLITE_LIMIT_CTES },
36544 };
36545 int i, n2;
36546 open_db(p, 0);
36547 if( nArg==1 ){
36548 for(i=0; i<ArraySize(aLimit); i++){
@@ -36272,12 +36573,12 @@
36573 azArg[1]);
36574 rc = 1;
36575 goto meta_command_exit;
36576 }
36577 if( nArg==3 ){
36578 int v = (int)integerValue(azArg[2]);
36579 sqlite3_limit(p->db, aLimit[iLimit].limitCode, v);
36580 }else{
36581 cli_printf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
36582 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
36583 }
36584 }
@@ -36729,19 +37030,27 @@
37030 rc = 2;
37031 }else
37032 #endif
37033
37034 #ifndef SQLITE_SHELL_FIDDLE
37035 if( c=='r' && n==4 && cli_strcmp(azArg[0], "read")==0 ){
37036 FILE *inSaved = p->in;
37037 i64 savedLineno = p->lineno;
37038 if( p->mIf ){
37039 if( nArg==2 && isHereDoc(azArg[1]) ){
37040 sqlite3_free(captureHereDoc(p, azArg[1]+2));
37041 }
37042 return 0;
37043 }
37044 if( nArg!=2 ){
37045 eputz("Usage: .read FILE\n");
37046 rc = 1;
37047 goto meta_command_exit;
37048 }
37049 if( !isHereDoc(azArg[1]) ){
37050 failIfSafeMode(p, "cannot run .read in safe mode");
37051 }
37052 if( azArg[1][0]=='|' ){
37053 #ifdef SQLITE_OMIT_POPEN
37054 eputz("Error: pipes are not supported in this OS\n");
37055 rc = 1;
37056 #else
@@ -36752,10 +37061,24 @@
37061 }else{
37062 rc = process_input(p, "<pipe>");
37063 pclose(p->in);
37064 }
37065 #endif
37066 }else if( isHereDoc(azArg[1]) ){
37067 char *saved_zInput;
37068 i64 saved_nInputUsed;
37069 char *zScript = captureHereDoc(p, azArg[1]+2);
37070 savedLineno = p->lineno;
37071 saved_zInput = p->zInput;
37072 saved_nInputUsed = p->nInputUsed;
37073 p->zInput = zScript;
37074 p->nInputUsed = 0;
37075 p->in = (FILE*)&p->zInput;
37076 rc = process_input(p, "<heredoc>");
37077 sqlite3_free(p->zInput);
37078 p->zInput = saved_zInput;
37079 p->nInputUsed = saved_nInputUsed;
37080 }else if( (p->in = openChrSource(azArg[1]))==0 ){
37081 cli_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
37082 rc = 1;
37083 }else{
37084 char *zFilename = strdup(azArg[1]);
@@ -38518,14 +38841,14 @@
38841 }else{
38842 zPre[nPre++] = c;
38843 }
38844 }
38845 zPre[nPre] = 0;
38846 if( n<=i+20 ){
 
38847 j = i;
38848 }else{
38849 j = n - 20;
38850 memcpy(zSuf," ... ", 5);
38851 nSuf = 5;
38852 }
38853 for(i=j; i<n; i++){
38854 char c = zSql[i];
@@ -38680,10 +39003,34 @@
39003 if( x==0x00 || x==0x10 ) return 1;
39004 }
39005 return 0;
39006 }
39007
39008 /*
39009 ** Return true if zLine is a dot-command that should always be run
39010 ** even if inside of the false leg of an ".if"
39011 */
39012 static int alwaysRun(const char *zLine){
39013 const char *az[] = {
39014 "3.if",
39015 "7.elseif",
39016 "5.else",
39017 "6.endif",
39018 "6.check", /* For its heredoc */
39019 "5.read", /* For its heredoc */
39020 "0"
39021 };
39022 int jj, n;
39023 for(jj=0; (n = az[jj][0] - '0')>0; jj++){
39024 if( strncmp(zLine,az[jj]+1,n)==0 && (IsSpace(zLine[n]) || zLine[n]==0) ){
39025 return 1;
39026 }
39027 }
39028 return 0;
39029 }
39030
39031
39032 /*
39033 ** Read input from *in and process it. If *in==0 then input
39034 ** is interactive - the user is typing it it. Otherwise, input
39035 ** is coming from a file or device. A prompt is issued and history
39036 ** is saved only if input is interactive. An interrupt signal will
@@ -38701,10 +39048,11 @@
39048 int errCnt = 0; /* Number of errors seen */
39049 int hasSemi = 0; /* Input line contains a semicolon */
39050 i64 startline = 0; /* Line number for start of current input */
39051 const char *saved_zInFile; /* Prior value of p->zInFile */
39052 i64 saved_lineno; /* Prior value of p->lineno */
39053 int saved_nIfAtStart; /* Prior value of p->nIfAtStart */
39054
39055 if( p->inputNesting==MAX_INPUT_NESTING ){
39056 /* This will be more informative in a later version. */
39057 cli_printf(stderr,"%s: Input nesting limit (%d) reached at line %lld."
39058 " Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno);
@@ -38713,10 +39061,12 @@
39061 ++p->inputNesting;
39062 saved_zInFile = p->zInFile;
39063 p->zInFile = zSrc;
39064 saved_lineno = p->lineno;
39065 p->lineno = 0;
39066 saved_nIfAtStart = p->nIfAtStart;
39067 p->nIfAtStart = p->nIfLevel;
39068 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
39069 fflush(p->out);
39070 zLine = one_input_line(p, zLine, zSql, nSql>0);
39071 if( zLine==0 ){
39072 /* End of input */
@@ -38734,15 +39084,18 @@
39084 continue;
39085 }
39086 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
39087 echo_group_input(p, zLine);
39088 if( zLine[0]=='.' ){
39089 if( p->mIf==0 || alwaysRun(zLine) ){
39090 rc = do_meta_command(zLine, p);
39091 if( rc==2 ){ /* exit requested */
39092 p->nIfLevel = p->nIfAtStart;
39093 break;
39094 }else if( rc ){
39095 errCnt++;
39096 }
39097 }
39098 }
39099 continue;
39100 }
39101 #ifdef SQLITE_SHELL_LEGACY_COMMAND_TERMINATOR
@@ -38779,11 +39132,13 @@
39132 errCnt++;
39133 break;
39134 }
39135 if( nSql && hasSemi && line_is_complete(zSql, nSql) ){
39136 echo_group_input(p, zSql);
39137 if( p->mIf==0 ){
39138 errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39139 }
39140 nSql = 0;
39141 if( p->nPopOutput ){
39142 output_reset(p);
39143 p->nPopOutput = 0;
39144 }else{
@@ -38800,17 +39155,28 @@
39155 }
39156 }
39157 if( nSql ){
39158 /* This may be incomplete. Let the SQL parser deal with that. */
39159 echo_group_input(p, zSql);
39160 if( p->mIf==0 ){
39161 errCnt += runOneSqlLine(p, zSql, p->zInFile, startline);
39162 }
39163 }
39164 free(zSql);
39165 free(zLine);
39166 --p->inputNesting;
39167 while( p->nIfLevel > p->nIfAtStart ){
39168 int ii = --p->nIfLevel;
39169 cli_printf(stderr,"%s:%lld: unterminated .if\n", zSrc, p->lnIf[ii]);
39170 p->mIf &= ~(1<<ii);
39171 p->mElse &= ~(1<<ii);
39172 p->mIfHasBeenTrue &= ~(1<<ii);
39173 errCnt++;
39174 }
39175 p->zInFile = saved_zInFile;
39176 p->lineno = saved_lineno;
39177 p->nIfAtStart = saved_nIfAtStart;
39178 return errCnt>0;
39179 }
39180
39181 /*
39182 ** Return a pathname which is the user's home directory. A
@@ -39515,11 +39881,11 @@
39881 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
39882 }else if( cli_strcmp(z,"-unsafe-testing")==0
39883 || cli_strcmp(z,"-unsafe")==0 ){
39884 ShellSetFlag(&data,SHFLG_TestingMode);
39885 }else if( cli_strcmp(z,"-safe")==0 ){
39886 noInit = 1; /* --safe implies --noinit */
39887 }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
39888 /* skip over the argument */
39889 i++;
39890 }else if( cli_strcmp(z,"-cmdline-edit")==0 ){
39891 shellCmdLineEdit = booleanValue(cmdline_option_value(argc,argv,++i));
39892
+839 -266
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
1616
** if you want a wrapper to interface SQLite with your choice of programming
1717
** language. The code for the "sqlite3" command-line shell is also in a
1818
** separate file. This file contains only code for the core SQLite library.
1919
**
2020
** The content in this amalgamation comes from Fossil check-in
21
-** c75eff906c11d8f17ae198e5f6cfea34636d with changes in files:
21
+** d08cabd778f7844d04f3852d93197e80c0d0 with changes in files:
2222
**
2323
**
2424
*/
2525
#ifndef SQLITE_AMALGAMATION
2626
#define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467467
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468468
** [sqlite_version()] and [sqlite_source_id()].
469469
*/
470470
#define SQLITE_VERSION "3.55.0"
471471
#define SQLITE_VERSION_NUMBER 3055000
472
-#define SQLITE_SOURCE_ID "2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918"
472
+#define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b"
473473
#define SQLITE_SCM_BRANCH "trunk"
474474
#define SQLITE_SCM_TAGS ""
475
-#define SQLITE_SCM_DATETIME "2026-10-17T12:10:02.088Z"
475
+#define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z"
476476
477477
/*
478478
** CAPI3REF: Run-Time Library Version Numbers
479479
** KEYWORDS: sqlite3_version sqlite3_sourceid
480480
**
@@ -4768,10 +4768,19 @@
47684768
** defined by the database schema.</dd>)^
47694769
**
47704770
** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
47714771
** <dd>The maximum number of SQL statements that can be contained within
47724772
** a single trigger.</dd>)^
4773
+**
4774
+** [[SQLITE_LIMIT_CTES]] ^(<dt>SQLITE_LIMIT_CTES</dt>
4775
+** <dd>The maximum number of [common table expressions] (CTEs) allowed
4776
+** in a single SQL statements. This limit is used to limit possible
4777
+** exponential growth in the number of required cursors required for
4778
+** nested joins of CTEs. The limit is an absolute upper bound, and
4779
+** SQLite may refuse to compile a statement if the number of CTEs simply
4780
+** approaches this limit. Set this limit to 0 to disable the restriction.
4781
+** </dd>)^
47734782
** </dl>
47744783
*/
47754784
#define SQLITE_LIMIT_LENGTH 0
47764785
#define SQLITE_LIMIT_SQL_LENGTH 1
47774786
#define SQLITE_LIMIT_COLUMN 2
@@ -4785,10 +4794,11 @@
47854794
#define SQLITE_LIMIT_TRIGGER_DEPTH 10
47864795
#define SQLITE_LIMIT_WORKER_THREADS 11
47874796
#define SQLITE_LIMIT_PARSER_DEPTH 12
47884797
#define SQLITE_LIMIT_SCHEMA 13
47894798
#define SQLITE_LIMIT_TRIGGER_STEPS 14
4799
+#define SQLITE_LIMIT_CTES 15
47904800
47914801
/*
47924802
** CAPI3REF: Prepare Flags
47934803
**
47944804
** These constants define various flags that can be passed into the
@@ -6197,10 +6207,14 @@
61976207
** CAPI3REF: Text Encodings
61986208
**
61996209
** These constants define integer codes that represent the various
62006210
** text encodings supported by SQLite.
62016211
**
6212
+** If any value other than the ones defined here are passed into the
6213
+** "encoding" argument of an API function, then the results are
6214
+** unspecified and subject to change.
6215
+**
62026216
** <dl>
62036217
** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
62046218
**
62056219
** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
62066220
** with each code point being expressed "little endian" - the least significant
@@ -7179,10 +7193,14 @@
71797193
** VFS and operating system. Some system treat a negative argument as an
71807194
** instruction to sleep forever. Others understand it to mean do not sleep
71817195
** at all. ^In SQLite version 3.42.0 and later, a negative
71827196
** argument passed into sqlite3_sleep() is changed to zero before it is relayed
71837197
** down into the xSleep method of the VFS.
7198
+**
7199
+** The maximum sleep time is 2147483 milliseconds (about 35 minutes and
7200
+** 47 seconds). If the argument to sqlite3_sleep() is greater than 2147483,
7201
+** the sleep time is capped at 2147483 milliseconds.
71847202
*/
71857203
SQLITE_API int sqlite3_sleep(int);
71867204
71877205
/*
71887206
** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11573,11 +11591,11 @@
1157311591
** buffer. If the bit had been clear, the returned buffer will not
1157411592
** be accessed by SQLite after the call.
1157511593
**
1157611594
** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
1157711595
** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11578
-** allocation error occurs.
11596
+** allocation or I/O error occurs.
1157911597
**
1158011598
** This interface is omitted if SQLite is compiled with the
1158111599
** [SQLITE_OMIT_DESERIALIZE] option.
1158211600
*/
1158311601
SQLITE_API unsigned char *sqlite3_serialize(
@@ -15089,10 +15107,31 @@
1508915107
*/
1509015108
#ifndef SQLITE_MAX_TRIGGER_STEPS
1509115109
# define SQLITE_MAX_TRIGGER_STEPS 65000
1509215110
#endif
1509315111
15112
+/*
15113
+** Maximum number of CTE cursors (approximately). This limit will be
15114
+** exceeded if any CTE uses a cursor number larger than this value, even
15115
+** of some of the cursors are used for something other than CTEs.
15116
+**
15117
+** The purpose of this limit is to prevent resource exhaustion caused
15118
+** by hostile SQL inputs such as:
15119
+**
15120
+** WITH c0 AS (SELECT 1 AS x),
15121
+** c1 AS (SELECT a.x AS x FROM c0 a, c0 b),
15122
+** c2 AS (SELECT a.x AS x FROM c1 a, c1 b),
15123
+** ...
15124
+** c17 AS (SELECT a.x AS x FROM c16 a, c16 b)
15125
+** SELECT count(*) FROM c17;
15126
+**
15127
+** See Bug 2026-10-04T05:35:14Z
15128
+*/
15129
+#ifndef SQLITE_MAX_CTES
15130
+# define SQLITE_MAX_CTES 50000
15131
+#endif
15132
+
1509415133
/************** End of sqliteLimit.h *****************************************/
1509515134
/************** Continuing where we left off in sqliteInt.h ******************/
1509615135
1509715136
/* Disable nuisance warnings on Borland compilers */
1509815137
#if defined(__BORLANDC__)
@@ -18737,11 +18776,11 @@
1873718776
1873818777
/*
1873918778
** The number of different kinds of things that can be limited
1874018779
** using the sqlite3_limit() interface.
1874118780
*/
18742
-#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_STEPS+1)
18781
+#define SQLITE_N_LIMIT (SQLITE_LIMIT_CTES+1)
1874318782
1874418783
/*
1874518784
** Lookaside malloc is a set of fixed-size buffers that can be used
1874618785
** to satisfy small transient memory allocation requests for objects
1874718786
** associated with a particular database connection. The use of
@@ -19006,14 +19045,17 @@
1900619045
sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
1900719046
#endif
1900819047
};
1900919048
1901019049
/*
19011
-** A macro to discover the encoding of a database.
19050
+** A macro to discover the encoding of a database or for managing
19051
+** encoding values.
1901219052
*/
1901319053
#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
1901419054
#define ENC(db) ((db)->enc)
19055
+#define ENC_MASK 0x1f /* Usable encoding bits */
19056
+#define ENC_KEEPBOM 0x80 /* Do not remove BOM */
1901519057
1901619058
/*
1901719059
** A u64 constant where the lower 32 bits are all zeros. Only the
1901819060
** upper 32 bits are included in the argument. Necessary because some
1901919061
** C-compilers still do not accept LL integer literals.
@@ -21141,10 +21183,11 @@
2114121183
#endif
2114221184
Parse *pToplevel; /* Parse structure for main program (or NULL) */
2114321185
Table *pTriggerTab; /* Table triggers are being coded for */
2114421186
TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
2114521187
ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
21188
+ Trigger *pTrigCoded; /* Trigger whose sub-program is being coded */
2114621189
2114721190
/**************************************************************************
2114821191
** Fields above must be initialized to zero. The fields that follow,
2114921192
** down to the beginning of the recursive section, do not need to be
2115021193
** initialized as they will be set before being used. The boundary is
@@ -25222,11 +25265,11 @@
2522225265
SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*);
2522325266
SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
2522425267
SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
2522525268
SQLITE_PRIVATE int sqlite3MemRealValueRC(Mem*, double*);
2522625269
SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
25227
-SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
25270
+SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*,int bFromText);
2522825271
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
2522925272
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
2523025273
SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
2523125274
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
2523225275
SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
@@ -26108,10 +26151,11 @@
2610826151
neg = 0;
2610926152
}
2611026153
if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
2611126154
return 1;
2611226155
}
26156
+ if( neg ) Y = -Y;
2611326157
zDate += 10;
2611426158
while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
2611526159
if( parseHhMmSs(zDate, p)==0 ){
2611626160
if( p->h==24 ){
2611726161
int mxD = daysOfMonth(Y,M);
@@ -26131,11 +26175,11 @@
2613126175
}else{
2613226176
return 1;
2613326177
}
2613426178
p->validJD = 0;
2613526179
p->validYMD = 1;
26136
- p->Y = neg ? -Y : Y;
26180
+ p->Y = Y;
2613726181
p->M = M;
2613826182
p->D = D;
2613926183
computeFloor(p);
2614026184
if( p->tz ){
2614126185
computeJD(p);
@@ -30620,10 +30664,16 @@
3062030664
pTo->xMutexLeave = pFrom->xMutexLeave;
3062130665
pTo->xMutexHeld = pFrom->xMutexHeld;
3062230666
pTo->xMutexNotheld = pFrom->xMutexNotheld;
3062330667
sqlite3MemoryBarrier();
3062430668
pTo->xMutexAlloc = pFrom->xMutexAlloc;
30669
+ }else{
30670
+ /* Another thread may have just installed the default methods above.
30671
+ ** This barrier pairs with the one prior to setting xMutexAlloc, so
30672
+ ** that the load of xMutexInit below cannot be satisfied before the
30673
+ ** load of xMutexAlloc on processors with weak memory ordering. */
30674
+ sqlite3MemoryBarrier();
3062530675
}
3062630676
assert( sqlite3GlobalConfig.mutex.xMutexInit );
3062730677
rc = sqlite3GlobalConfig.mutex.xMutexInit();
3062830678
3062930679
#ifdef SQLITE_DEBUG
@@ -38212,11 +38262,13 @@
3821238262
** the right of the decimal point, or to a maximum of mxRound total
3821338263
** significant digits.
3821438264
**
3821538265
** If iRound>0 round to min(iRound,mxRound) significant digits total.
3821638266
**
38217
-** mxRound must be positive.
38267
+** mxRound must be positive. iRound must not be less than -100000000
38268
+** (the SQLITE_FP_PRECISION_LIMIT maximum in printf.c) so that the
38269
+** computation of the number of significant digits cannot overflow.
3821838270
**
3821938271
** The significant digits of the decimal representation are
3822038272
** stored in p->z[] which is a often (but not always) a pointer
3822138273
** into the middle of p->zBuf[]. There are p->n significant digits.
3822238274
** The p->z[] array is *not* zero-terminated.
@@ -38229,10 +38281,11 @@
3822938281
char *zBuf; /* Local alias for p->zBuf */
3823038282
char *z; /* Local alias for p->z */
3823138283
3823238284
p->isSpecial = 0;
3823338285
assert( mxRound>0 );
38286
+ assert( iRound>=-100000000 );
3823438287
3823538288
/* Convert negative numbers to positive. Deal with Infinity, 0.0, and
3823638289
** NaN. */
3823738290
if( r<0.0 ){
3823838291
p->sign = '-';
@@ -47295,10 +47348,13 @@
4729547348
*/
4729647349
zName = unixTempFileDir();
4729747350
if( zName
4729847351
&& (fd = robust_open(zName, O_RDWR|O_EXCL|O_TMPFILE, 0600))>=0
4729947352
){
47353
+ if( pOutFlags ){
47354
+ *pOutFlags = flags;
47355
+ }
4730047356
rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags|UNIXFILE_NOLOCK);
4730147357
goto open_finished;
4730247358
}
4730347359
#endif
4730447360
rc = unixGetTempname(pVfs->mxPathname, zTmpname);
@@ -47816,11 +47872,16 @@
4781647872
if( microseconds>=1000000 ) sleep(microseconds/1000000);
4781747873
if( microseconds%1000000 ) usleep(microseconds%1000000);
4781847874
UNUSED_PARAMETER(NotUsed);
4781947875
return microseconds;
4782047876
#else
47821
- int seconds = (microseconds+999999)/1000000;
47877
+ int seconds;
47878
+ if( microseconds>2146000000 ){
47879
+ seconds = 2147;
47880
+ }else{
47881
+ seconds = (microseconds+999999)/1000000;
47882
+ }
4782247883
sleep(seconds);
4782347884
UNUSED_PARAMETER(NotUsed);
4782447885
return seconds*1000000;
4782547886
#endif
4782647887
}
@@ -55359,11 +55420,12 @@
5535955420
memdbLeave(pStore);
5536055421
return p;
5536155422
}
5536255423
5536355424
/*
55364
-** Return the serialization of a database
55425
+** Return the serialization of a database. Or return NULL if there
55426
+** is an OOM or I/O error.
5536555427
*/
5536655428
SQLITE_API unsigned char *sqlite3_serialize(
5536755429
sqlite3 *db, /* The database connection */
5536855430
const char *zSchema, /* Which database within the connection */
5536955431
sqlite3_int64 *piSize, /* Write size here, if not NULL */
@@ -55435,12 +55497,16 @@
5543555497
DbPage *pPage = 0;
5543655498
unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
5543755499
rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
5543855500
if( rc==SQLITE_OK ){
5543955501
memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
55440
- }else{
55502
+ }else if( pgno==PENDING_BYTE/szPage+1 ){
5544155503
memset(pTo, 0, szPage);
55504
+ }else{
55505
+ sqlite3_free(pOut);
55506
+ pOut = 0;
55507
+ nPage = 0;
5544255508
}
5544355509
sqlite3PagerUnref(pPage);
5544455510
}
5544555511
}
5544655512
}
@@ -57089,38 +57155,66 @@
5708957155
** When reading a corrupt database file, it is possible that SQLite might
5709057156
** read a few bytes (no more than 16 bytes) past the end of the page buffer.
5709157157
** It will only read past the end of the page buffer, never write. This
5709257158
** object is positioned immediately after the page buffer to serve as an
5709357159
** overrun area, so that overreads are harmless.
57160
+**
57161
+** Under some circumstances, using a strategically corrupt db file, one
57162
+** or two varints may be read from past the end of the page buffer and
57163
+** returned to the user (as rowid values). For this reason, we ensure that
57164
+** the first few elements of PgHdr1 are not pointers. If they were, then
57165
+** a determined attacker might be able to read the value of the pointer,
57166
+** which can be used as part of a larger exploit. Furthermore, because the
57167
+** first two elements of PgHdr1 are isBulkLocal and isAnchor - u16 variables
57168
+** that always contain either 1 or 0 - the PgHdr1 structure always begins
57169
+** with 4 bytes with the high-bit cleared, and so code attempting to read
57170
+** varints can never progress past this point and read any more sensitive
57171
+** information further inside the structure.
5709457172
**
5709557173
** Variables isBulkLocal and isAnchor were once type "u8". That works,
5709657174
** but causes a 2-byte gap in the structure for most architectures (since
5709757175
** pointers must be either 4 or 8-byte aligned). As this structure is located
5709857176
** in memory directly after the associated page data, if the database is
5709957177
** corrupt, code at the b-tree layer may overread the page buffer and
5710057178
** read part of this structure before the corruption is detected. This
5710157179
** can cause a valgrind error if the uninitialized gap is accessed. Using u16
5710257180
** ensures there is no such gap, and therefore no bytes of uninitialized
57103
-** memory in the structure.
57181
+** memory in the structure. Keeping the u16 type also lets isBulkLocal,
57182
+** isAnchor and iKey fill exactly the 8 bytes (2+2+4) that precede the
57183
+** 8-byte-aligned "page" member, so no uninitialized padding is exposed to
57184
+** an overread.
5710457185
**
5710557186
** The pLruNext and pLruPrev pointers form a double-linked circular list
5710657187
** of all pages that are unpinned. The PGroup.lru element (which should be
5710757188
** the only element on the list with PgHdr1.isAnchor set to 1) forms the
5710857189
** beginning and the end of the list.
5710957190
*/
5711057191
struct PgHdr1 {
57111
- sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
57112
- unsigned int iKey; /* Key value (page number) */
5711357192
u16 isBulkLocal; /* This page from bulk local storage */
5711457193
u16 isAnchor; /* This is the PGroup.lru element */
57194
+ unsigned int iKey; /* Key value (page number) */
57195
+ sqlite3_pcache_page page; /* Base class. pBuf & pExtra. Deliberately not the
57196
+ ** first member - see the note above. */
5711557197
PgHdr1 *pNext; /* Next in hash table chain */
5711657198
PCache1 *pCache; /* Cache that currently owns this page */
5711757199
PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
5711857200
PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
5711957201
/* NB: pLruPrev is only valid if pLruNext!=0 */
5712057202
};
5712157203
57204
+/*
57205
+** Given the sqlite3_pcache_page object handed out to the pcache.c layer,
57206
+** recover the PgHdr1 that contains it. PgHdr1.page is no longer required to
57207
+** be the first member of PgHdr1, so this goes through the "page" member
57208
+** rather than casting the structure pointer. The fetch methods produce the
57209
+** other direction directly with &pPage->page, only on paths where pPage is
57210
+** known to be non-NULL. PAGE_TO_PGHDR1() is likewise only ever given a
57211
+** non-NULL page that this module previously handed out.
57212
+*/
57213
+#define PAGE_TO_PGHDR1(pPg) \
57214
+ ((PgHdr1*)(((char*)(pPg)) - offsetof(PgHdr1,page)))
57215
+
5712257216
/*
5712357217
** A page is pinned if it is not on the LRU list. To be "pinned" means
5712457218
** that the page is in active use and must not be deallocated.
5712557219
*/
5712657220
#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
@@ -57567,11 +57661,11 @@
5756757661
** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
5756857662
** LRU list, then this function is a no-op.
5756957663
**
5757057664
** The PGroup mutex must be held when this function is called.
5757157665
*/
57572
-static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
57666
+static sqlite3_pcache_page *pcache1PinPage(PgHdr1 *pPage){
5757357667
assert( pPage!=0 );
5757457668
assert( PAGE_IS_UNPINNED(pPage) );
5757557669
assert( pPage->pLruNext );
5757657670
assert( pPage->pLruPrev );
5757757671
assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
@@ -57581,11 +57675,11 @@
5758157675
/* pPage->pLruPrev = 0;
5758257676
** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
5758357677
assert( pPage->isAnchor==0 );
5758457678
assert( pPage->pCache->pGroup->lru.isAnchor==1 );
5758557679
pPage->pCache->nRecyclable--;
57586
- return pPage;
57680
+ return &pPage->page;
5758757681
}
5758857682
5758957683
5759057684
/*
5759157685
** Remove the page supplied as an argument from the hash table
@@ -57864,11 +57958,11 @@
5786457958
**
5786557959
** This steps are broken out into a separate procedure because they are
5786657960
** usually not needed, and by avoiding the stack initialization required
5786757961
** for these steps, the main pcache1Fetch() procedure can run faster.
5786857962
*/
57869
-static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
57963
+static SQLITE_NOINLINE sqlite3_pcache_page *pcache1FetchStage2(
5787057964
PCache1 *pCache,
5787157965
unsigned int iKey,
5787257966
int createFlag
5787357967
){
5787457968
unsigned int nPinned;
@@ -57930,12 +58024,13 @@
5793058024
*(void **)pPage->page.pExtra = 0;
5793158025
pCache->apHash[h] = pPage;
5793258026
if( iKey>pCache->iMaxKey ){
5793358027
pCache->iMaxKey = iKey;
5793458028
}
58029
+ return &pPage->page;
5793558030
}
57936
- return pPage;
58031
+ return 0;
5793758032
}
5793858033
5793958034
/*
5794058035
** Implementation of the sqlite3_pcache.xFetch method.
5794158036
**
@@ -57993,11 +58088,11 @@
5799358088
** There are two versions of this routine. pcache1FetchWithMutex() is
5799458089
** the general case. pcache1FetchNoMutex() is a faster implementation for
5799558090
** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
5799658091
** invokes the appropriate routine.
5799758092
*/
57998
-static PgHdr1 *pcache1FetchNoMutex(
58093
+static sqlite3_pcache_page *pcache1FetchNoMutex(
5799958094
sqlite3_pcache *p,
5800058095
unsigned int iKey,
5800158096
int createFlag
5800258097
){
5800358098
PCache1 *pCache = (PCache1 *)p;
@@ -58016,27 +58111,27 @@
5801658111
** subsequent steps to try to create the page. */
5801758112
if( pPage ){
5801858113
if( PAGE_IS_UNPINNED(pPage) ){
5801958114
return pcache1PinPage(pPage);
5802058115
}else{
58021
- return pPage;
58116
+ return &pPage->page;
5802258117
}
5802358118
}else if( createFlag ){
5802458119
/* Steps 3, 4, and 5 implemented by this subroutine */
5802558120
return pcache1FetchStage2(pCache, iKey, createFlag);
5802658121
}else{
5802758122
return 0;
5802858123
}
5802958124
}
5803058125
#if PCACHE1_MIGHT_USE_GROUP_MUTEX
58031
-static PgHdr1 *pcache1FetchWithMutex(
58126
+static sqlite3_pcache_page *pcache1FetchWithMutex(
5803258127
sqlite3_pcache *p,
5803358128
unsigned int iKey,
5803458129
int createFlag
5803558130
){
5803658131
PCache1 *pCache = (PCache1 *)p;
58037
- PgHdr1 *pPage;
58132
+ sqlite3_pcache_page *pPage;
5803858133
5803958134
pcache1EnterMutex(pCache->pGroup);
5804058135
pPage = pcache1FetchNoMutex(p, iKey, createFlag);
5804158136
assert( pPage==0 || pCache->iMaxKey>=iKey );
5804258137
pcache1LeaveMutex(pCache->pGroup);
@@ -58050,23 +58145,22 @@
5805058145
){
5805158146
#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
5805258147
PCache1 *pCache = (PCache1 *)p;
5805358148
#endif
5805458149
58055
- assert( offsetof(PgHdr1,page)==0 );
5805658150
assert( pCache->bPurgeable || createFlag!=1 );
5805758151
assert( pCache->bPurgeable || pCache->nMin==0 );
5805858152
assert( pCache->bPurgeable==0 || pCache->nMin==10 );
5805958153
assert( pCache->nMin==0 || pCache->bPurgeable );
5806058154
assert( pCache->nHash>0 );
5806158155
#if PCACHE1_MIGHT_USE_GROUP_MUTEX
5806258156
if( pCache->pGroup->mutex ){
58063
- return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
58157
+ return pcache1FetchWithMutex(p, iKey, createFlag);
5806458158
}else
5806558159
#endif
5806658160
{
58067
- return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
58161
+ return pcache1FetchNoMutex(p, iKey, createFlag);
5806858162
}
5806958163
}
5807058164
5807158165
5807258166
/*
@@ -58078,11 +58172,11 @@
5807858172
sqlite3_pcache *p,
5807958173
sqlite3_pcache_page *pPg,
5808058174
int reuseUnlikely
5808158175
){
5808258176
PCache1 *pCache = (PCache1 *)p;
58083
- PgHdr1 *pPage = (PgHdr1 *)pPg;
58177
+ PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
5808458178
PGroup *pGroup = pCache->pGroup;
5808558179
5808658180
assert( pPage->pCache==pCache );
5808758181
pcache1EnterMutex(pGroup);
5808858182
@@ -58114,20 +58208,20 @@
5811458208
sqlite3_pcache_page *pPg,
5811558209
unsigned int iOld,
5811658210
unsigned int iNew
5811758211
){
5811858212
PCache1 *pCache = (PCache1 *)p;
58119
- PgHdr1 *pPage = (PgHdr1 *)pPg;
58213
+ PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
5812058214
PgHdr1 **pp;
5812158215
unsigned int hOld, hNew;
5812258216
assert( pPage->iKey==iOld );
5812358217
assert( pPage->pCache==pCache );
5812458218
assert( iOld!=iNew ); /* The page number really is changing */
5812558219
5812658220
pcache1EnterMutex(pCache->pGroup);
5812758221
58128
- assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
58222
+ assert( pcache1FetchNoMutex(p, iOld, 0)==&pPage->page ); /* pPg really iOld */
5812958223
assert( pCache->nHash>0 && (pCache->nHash & (pCache->nHash-1))==0 );
5813058224
hOld = iOld & (pCache->nHash-1);
5813158225
pp = &pCache->apHash[hOld];
5813258226
while( (*pp)!=pPage ){
5813358227
pp = &(*pp)->pNext;
@@ -68381,10 +68475,11 @@
6838168475
SEH_FREE_ON_ERROR(0, aFrame);
6838268476
if( !aFrame ){
6838368477
rc = SQLITE_NOMEM_BKPT;
6838468478
goto recovery_error;
6838568479
}
68480
+ memset(aFrame, 0, szFrame + WALINDEX_PGSZ);
6838668481
aData = &aFrame[WAL_FRAME_HDRSIZE];
6838768482
aPrivate = (u32*)&aData[szPage];
6838868483
6838968484
/* Read all frames from the log file. */
6839068485
iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
@@ -69654,12 +69749,16 @@
6965469749
** with a writer. So get a WRITE lock and try again.
6965569750
*/
6965669751
if( badHdr ){
6965769752
if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
6965869753
if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
69754
+ /* Retry the header now that the WRITE_LOCK is held. If it still
69755
+ ** fails, the header really is corrupt and recovery is required.
69756
+ ** Otherwise, continue with the now valid header. */
69757
+ if( page0 ) badHdr = walIndexTryHdr(pWal, pChanged);
6965969758
walUnlockShared(pWal, WAL_WRITE_LOCK);
69660
- rc = SQLITE_READONLY_RECOVERY;
69759
+ if( badHdr ) rc = SQLITE_READONLY_RECOVERY;
6966169760
}
6966269761
}else{
6966369762
int bWriteLock = pWal->writeLock;
6966469763
if( bWriteLock
6966569764
|| SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
@@ -70136,29 +70235,39 @@
7013670235
assert( thisMark!=READMARK_NOT_USED );
7013770236
mxReadMark = thisMark;
7013870237
mxI = i;
7013970238
}
7014070239
}
70141
- if( (pWal->readOnly & WAL_SHM_RDONLY)==0
70142
- && (mxReadMark<mxFrame || mxI==0)
70143
- ){
70144
- for(i=1; i<WAL_NREADER; i++){
70145
- rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
70146
- if( rc==SQLITE_OK ){
70147
- AtomicStore(pInfo->aReadMark+i,mxFrame);
70148
- mxReadMark = mxFrame;
70149
- mxI = i;
70150
- walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
70151
- break;
70152
- }else if( rc!=SQLITE_BUSY ){
70153
- return rc;
70154
- }
70155
- }
70156
- }
70240
+
70241
+ if( (pWal->readOnly & WAL_SHM_RDONLY)==0 ){
70242
+ if( mxReadMark<mxFrame || mxI==0 ){
70243
+ for(i=1; i<WAL_NREADER; i++){
70244
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
70245
+ if( rc==SQLITE_OK ){
70246
+ AtomicStore(pInfo->aReadMark+i,mxFrame);
70247
+ mxReadMark = mxFrame;
70248
+ mxI = i;
70249
+ walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
70250
+ break;
70251
+ }else if( rc!=SQLITE_BUSY ){
70252
+ return rc;
70253
+ }
70254
+ }
70255
+ }
70256
+ }else if( mxI==0 ){
70257
+ /* If the loop above could not find a usable slot (mxI==0), then check
70258
+ ** if the wal-index header has been modified by another connection since
70259
+ ** it was read. If not, return SQLITE_READONLY_CANTINIT. If it has
70260
+ ** been modified, fall through and return WAL_RETRY. */
70261
+ walShmBarrier(pWal);
70262
+ if( 0==memcmp((void*)walIndexHdr(pWal),&pWal->hdr,sizeof(WalIndexHdr)) ){
70263
+ return SQLITE_READONLY_CANTINIT;
70264
+ }
70265
+ }
70266
+
7015770267
if( mxI==0 ){
70158
- assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
70159
- return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
70268
+ return WAL_RETRY;
7016070269
}
7016170270
7016270271
(void)walEnableBlockingMs(pWal, nBlockTmout);
7016370272
rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
7016470273
walDisableBlocking(pWal);
@@ -74930,11 +75039,12 @@
7493075039
return SQLITE_OK;
7493175040
}
7493275041
7493375042
/*
7493475043
** Do additional sanity check after btreeInitPage() if
74935
-** PRAGMA cell_size_check=ON
75044
+** PRAGMA cell_size_check=ON. Raise a corruption error and
75045
+** leave the page uninitialized if any problems are found.
7493675046
*/
7493775047
static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
7493875048
int iCellFirst; /* First allowable cell or freeblock offset */
7493975049
int iCellLast; /* Last possible cell or freeblock offset */
7494075050
int i; /* Index into the cell pointer array */
@@ -74953,15 +75063,17 @@
7495375063
for(i=0; i<pPage->nCell; i++){
7495475064
pc = get2byteAligned(&data[cellOffset+i*2]);
7495575065
testcase( pc==iCellFirst );
7495675066
testcase( pc==iCellLast );
7495775067
if( pc<iCellFirst || pc>iCellLast ){
75068
+ pPage->isInit = 0;
7495875069
return SQLITE_CORRUPT_PAGE(pPage);
7495975070
}
7496075071
sz = pPage->xCellSize(pPage, &data[pc]);
7496175072
testcase( pc+sz==usableSize );
7496275073
if( pc+sz>usableSize ){
75074
+ pPage->isInit = 0;
7496375075
return SQLITE_CORRUPT_PAGE(pPage);
7496475076
}
7496575077
}
7496675078
return SQLITE_OK;
7496775079
}
@@ -82791,10 +82903,16 @@
8279182903
pCur->skipNext = -1;
8279282904
pCur->ix = pPage->nCell-1;
8279382905
}else{
8279482906
pCur->skipNext = 1;
8279582907
}
82908
+ /* The pCur->info structure is currently populated with the details
82909
+ ** of the cell that was just removed. But pCur->ix now points to a
82910
+ ** different cell in the b-tree. So zero info.nSize here so that if
82911
+ ** saveCursorPosition() is called on this cursor it does not trust
82912
+ ** the contents of pCur->info. */
82913
+ pCur->info.nSize = 0;
8279682914
}else{
8279782915
rc = moveToRoot(pCur);
8279882916
if( bPreserve ){
8279982917
btreeReleaseAllCursorPages(pCur);
8280082918
pCur->eState = CURSOR_REQUIRESEEK;
@@ -85989,12 +86107,32 @@
8598986107
}
8599086108
8599186109
/*
8599286110
** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
8599386111
** make it a MEM_Int if we can.
86112
+**
86113
+** This routine only converts REAL values into INTEGER if the REAL
86114
+** has no fractional part and if the value can be exactly expressed
86115
+** as a signed 64-bit integer.
86116
+**
86117
+** The bFromText parameter is true if the MEM was originally text.
86118
+** If the value was originally text then this routine will not convert
86119
+** -9223372036854775808.0 into -9223372036854775808 because if the value
86120
+** was originally text, it would have been converted into an integer
86121
+** already, and since it was not, that means that the original text
86122
+** content had the last four significant digits greater than "5808"
86123
+** and the hence the text value was not really -9223372036854775808.0
86124
+** but rather some nearby value that happens to have the same IEEE 754
86125
+** representation. We don't want text '-9223372036854775900.0' to get
86126
+** converted into -9223372036854775808.
86127
+**
86128
+** But if bFromText is false, then the value started out as REAL and
86129
+** so it is ok to convert -9223372036854775808.0 into -9223372036854775808
86130
+** in that case, because -9223372036854775808.0 is the exact value of
86131
+** the underlying IEEE 754 representation.
8599486132
*/
85995
-SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
86133
+SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem, int bFromText){
8599686134
assert( pMem!=0 );
8599786135
assert( pMem->flags & (MEM_Real|MEM_IntReal) );
8599886136
assert( !sqlite3VdbeMemIsRowSet(pMem) );
8599986137
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
8600086138
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
@@ -86012,11 +86150,13 @@
8601286150
**
8601386151
** The second and third terms in the following conditional enforces
8601486152
** the second condition under the assumption that addition overflow causes
8601586153
** values to wrap around.
8601686154
*/
86017
- if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
86155
+ if( pMem->u.r==ix && ix<LARGEST_INT64
86156
+ && (!bFromText || ix>SMALLEST_INT64)
86157
+ ){
8601886158
pMem->u.i = ix;
8601986159
MemSetTypeFlag(pMem, MEM_Int);
8602086160
}
8602186161
}
8602286162
}
@@ -86476,15 +86616,17 @@
8647686616
void (*xDel)(void*) /* Destructor function */
8647786617
){
8647886618
i64 nByte = n; /* New value for pMem->n */
8647986619
int iLimit; /* Maximum allowed string or blob size */
8648086620
u16 flags; /* New value for pMem->flags */
86621
+ u8 xenc; /* Encoding without the ENC_KEEPBOM bit */
8648186622
8648286623
assert( pMem!=0 );
8648386624
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
8648486625
assert( !sqlite3VdbeMemIsRowSet(pMem) );
86485
- assert( enc!=0 || n>=0 );
86626
+ xenc = enc & ENC_MASK;
86627
+ assert( xenc!=0 || n>=0 );
8648686628
8648786629
/* If z is a NULL pointer, set pMem to contain an SQL NULL. */
8648886630
if( !z ){
8648986631
sqlite3VdbeMemSetNull(pMem);
8649086632
return SQLITE_OK;
@@ -86494,20 +86636,20 @@
8649486636
iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
8649586637
}else{
8649686638
iLimit = SQLITE_MAX_LENGTH;
8649786639
}
8649886640
if( nByte<0 ){
86499
- assert( enc!=0 );
86500
- if( enc==SQLITE_UTF8 ){
86641
+ assert( xenc!=0 );
86642
+ if( xenc==SQLITE_UTF8 ){
8650186643
nByte = strlen(z);
8650286644
}else{
8650386645
for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
8650486646
}
8650586647
flags= MEM_Str|MEM_Term;
86506
- }else if( enc==0 ){
86648
+ }else if( xenc==0 ){
8650786649
flags = MEM_Blob;
86508
- enc = SQLITE_UTF8;
86650
+ xenc = SQLITE_UTF8;
8650986651
}else{
8651086652
flags = MEM_Str;
8651186653
}
8651286654
if( nByte>iLimit ){
8651386655
if( xDel && xDel!=SQLITE_TRANSIENT ){
@@ -86526,11 +86668,11 @@
8652686668
** management (one of MEM_Dyn or MEM_Static).
8652786669
*/
8652886670
if( xDel==SQLITE_TRANSIENT ){
8652986671
i64 nAlloc = nByte;
8653086672
if( flags&MEM_Term ){
86531
- nAlloc += (enc==SQLITE_UTF8?1:2);
86673
+ nAlloc += (xenc==SQLITE_UTF8?1:2);
8653286674
}
8653386675
testcase( nAlloc==0 );
8653486676
testcase( nAlloc==31 );
8653586677
testcase( nAlloc==32 );
8653686678
if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
@@ -86550,18 +86692,17 @@
8655086692
}
8655186693
}
8655286694
8655386695
pMem->n = (int)(nByte & 0x7fffffff);
8655486696
pMem->flags = flags;
86555
- pMem->enc = enc;
86697
+ pMem->enc = xenc;
8655686698
8655786699
#ifndef SQLITE_OMIT_UTF16
86558
- /* tag-20261016-1: To retain the BOM, use enc==SQLITE_UTF8, even
86559
- ** for UTF-16 text, and then set pMem->enc correctly after this
86560
- ** function returns. Note, however, that n must be non-negative
86561
- ** for this trick to work. */
86562
- if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
86700
+ if( xenc>SQLITE_UTF8
86701
+ && (enc & ENC_KEEPBOM)==0
86702
+ && sqlite3VdbeMemHandleBom(pMem)
86703
+ ){
8656386704
return SQLITE_NOMEM_BKPT;
8656486705
}
8656586706
#endif
8656686707
8656786708
@@ -86675,14 +86816,14 @@
8667586816
return SQLITE_NOMEM_BKPT;
8667686817
}
8667786818
if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){
8667886819
return SQLITE_CORRUPT_BKPT;
8667986820
}
86680
- if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
86821
+ if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+8)) ){
8668186822
rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
8668286823
if( rc==SQLITE_OK ){
86683
- pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
86824
+ memset(pMem->z+amt,0,8); /* Overrun area for malformed records */
8668486825
pMem->flags = MEM_Blob;
8668586826
pMem->n = (int)amt;
8668686827
}else{
8668786828
sqlite3VdbeMemRelease(pMem);
8668886829
}
@@ -87067,18 +87208,19 @@
8706787208
sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
8706887209
}else{
8706987210
zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
8707087211
if( zVal==0 ) goto no_mem;
8707187212
sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
87213
+ if( op==TK_FLOAT ){
87214
+ assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
87215
+ sqlite3AtoF(pVal->z, &pVal->u.r);
87216
+ pVal->flags = MEM_Real;
87217
+ }
8707287218
}
8707387219
}
8707487220
if( affinity==SQLITE_AFF_BLOB ){
87075
- if( op==TK_FLOAT ){
87076
- assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
87077
- sqlite3AtoF(pVal->z, &pVal->u.r);
87078
- pVal->flags = MEM_Real;
87079
- }else if( op==TK_INTEGER ){
87221
+ if( op==TK_INTEGER ){
8708087222
/* This case is required by -9223372036854775808 and other strings
8708187223
** that look like integers but cannot be handled by the
8708287224
** sqlite3DecOrHexToI64() call above. */
8708387225
sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
8708487226
}
@@ -93949,10 +94091,11 @@
9394994091
sqlite3_result_error_toobig(pCtx);
9395094092
return SQLITE_TOOBIG;
9395194093
}
9395294094
#ifndef SQLITE_OMIT_INCRBLOB
9395394095
sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
94096
+ pOut->enc = pCtx->enc;
9395494097
return SQLITE_OK;
9395594098
#else
9395694099
return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
9395794100
#endif
9395894101
}
@@ -95041,16 +95184,17 @@
9504195184
sqlite3_stmt *pStmt, /* The statement to bind against */
9504295185
int i, /* Index of the parameter to bind */
9504395186
const void *zData, /* Pointer to the data to be bound */
9504495187
i64 nData, /* Number of bytes of data to be bound */
9504595188
void (*xDel)(void*), /* Destructor for the data */
95046
- u8 encoding /* Encoding for the data */
95189
+ u8 encoding /* Encoding for the data + optional ENC_KEEPBOM */
9504795190
){
9504895191
Vdbe *p = (Vdbe *)pStmt;
9504995192
Mem *pVar;
9505095193
int rc;
9505195194
95195
+ assert( (encoding&ENC_KEEPBOM)==0 || (encoding&ENC_MASK)>SQLITE_UTF8 );
9505295196
rc = vdbeUnbind(p, (u32)(i-1));
9505395197
if( rc==SQLITE_OK ){
9505495198
assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
9505595199
if( zData!=0 ){
9505695200
pVar = &p->aVar[i-1];
@@ -95253,12 +95397,13 @@
9525395397
rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
9525495398
}
9525595399
break;
9525695400
}
9525795401
case SQLITE_TEXT: {
95402
+ assert( pValue->enc!=0 );
9525895403
rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
95259
- pValue->enc);
95404
+ pValue->enc<=SQLITE_UTF8 ? pValue->enc : pValue->enc|ENC_KEEPBOM);
9526095405
break;
9526195406
}
9526295407
default: {
9526395408
rc = sqlite3_bind_null(pStmt, i);
9526495409
break;
@@ -95275,10 +95420,11 @@
9527595420
#ifndef SQLITE_OMIT_INCRBLOB
9527695421
sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
9527795422
#else
9527895423
rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
9527995424
#endif
95425
+ p->aVar[i-1].enc = ENC(p->db);
9528095426
sqlite3_mutex_leave(p->db->mutex);
9528195427
}
9528295428
return rc;
9528395429
}
9528495430
SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
@@ -95428,13 +95574,18 @@
9542895574
}else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
9542995575
/* No reprepare necessary */
9543095576
v->explain = eMode;
9543195577
rc = SQLITE_OK;
9543295578
}else{
95579
+ int savedMode = v->explain;
9543395580
v->explain = eMode;
9543495581
rc = sqlite3Reprepare(v);
95435
- v->haveEqpOps = eMode==2;
95582
+ if( rc ){
95583
+ v->explain = savedMode;
95584
+ }else{
95585
+ v->haveEqpOps = eMode==2;
95586
+ }
9543695587
}
9543795588
if( v->explain ){
9543895589
v->nResColumn = 12 - 4*v->explain;
9543995590
}else{
9544095591
v->nResColumn = v->nResAlloc;
@@ -95622,14 +95773,15 @@
9562295773
u32 nRec;
9562395774
u8 *aRec;
9562495775
9562595776
assert( p->pCsr->eCurType==CURTYPE_BTREE );
9562695777
nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
95627
- aRec = sqlite3DbMallocRaw(db, nRec);
95778
+ aRec = sqlite3DbMallocRaw(db, (u64)nRec+8);
9562895779
if( !aRec ) goto preupdate_old_out;
9562995780
rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
9563095781
if( rc==SQLITE_OK ){
95782
+ memset(aRec+nRec, 0, 8);
9563195783
p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
9563295784
if( !p->pUnpacked ) rc = SQLITE_NOMEM;
9563395785
}
9563495786
if( rc!=SQLITE_OK ){
9563595787
sqlite3DbFree(db, aRec);
@@ -96658,11 +96810,11 @@
9665896810
if( (rc&2)==0 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
9665996811
pRec->flags |= MEM_Int;
9666096812
}else{
9666196813
pRec->u.r = rValue;
9666296814
pRec->flags |= MEM_Real;
96663
- if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
96815
+ if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec, 1);
9666496816
}
9666596817
/* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
9666696818
** string representation after computing a numeric equivalent, because the
9666796819
** string representation might not be the canonical representation for the
9666896820
** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
@@ -96702,11 +96854,11 @@
9670296854
|| affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM );
9670396855
if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
9670496856
if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
9670596857
if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
9670696858
}else if( affinity<=SQLITE_AFF_REAL ){
96707
- sqlite3VdbeIntegerAffinity(pRec);
96859
+ sqlite3VdbeIntegerAffinity(pRec, 0);
9670896860
}
9670996861
}
9671096862
}else if( affinity==SQLITE_AFF_TEXT ){
9671196863
/* Only attempt the conversion to TEXT if there is an integer or real
9671296864
** representation (blob and NULL do not get converted) but no string
@@ -97092,22 +97244,18 @@
9709297244
pBuf = pCache->pCValue;
9709397245
}
9709497246
assert( t>=12 );
9709597247
sqlite3RCStrRef(pBuf);
9709697248
if( t&1 ){
97097
- /* Prevents the removal of a BOM in UTF16 text. Otherwise
97098
- ** it causes no harm as long as len>=0, and pDest->enc is set
97099
- ** to the correct encoding afterwards: ----
97100
- ** (tag-20261016-1) `vvvvvvvvvvv */
97101
- rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, SQLITE_UTF8,
97249
+ rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding|ENC_KEEPBOM,
9710297250
sqlite3RCStrUnref);
9710397251
pDest->flags |= MEM_Term;
9710497252
}else{
9710597253
rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0,
9710697254
sqlite3RCStrUnref);
97107
- }
97108
- pDest->enc = encoding;
97255
+ pDest->enc = encoding;
97256
+ }
9710997257
}else{
9711097258
rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
9711197259
if( rc ) return rc;
9711297260
sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
9711397261
if( (t&1)!=0 && encoding==SQLITE_UTF8 ){
@@ -111096,10 +111244,13 @@
111096111244
if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
111097111245
Upsert *pUpsert = pNC->uNC.pUpsert;
111098111246
if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
111099111247
pTab = pUpsert->pUpsertSrc->a[0].pSTab;
111100111248
pExpr->iTable = EXCLUDED_TABLE_NUMBER;
111249
+ if( IN_RENAME_OBJECT ){
111250
+ sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
111251
+ }
111101111252
}
111102111253
}
111103111254
#endif /* SQLITE_OMIT_UPSERT */
111104111255
111105111256
if( pTab ){
@@ -112118,11 +112269,11 @@
112118112269
NameContext nc; /* Name context for resolving pE */
112119112270
sqlite3 *db; /* Database connection */
112120112271
int rc; /* Return code from subprocedures */
112121112272
u8 savedSuppErr; /* Saved value of db->suppressErr */
112122112273
112123
- assert( sqlite3ExprIsInteger(pE, &i, 0, 0)==0 );
112274
+ assert( sqlite3ExprIsInteger(pE, &i, 0, 1)==0 );
112124112275
pEList = pSelect->pEList;
112125112276
112126112277
/* Resolve all names in the ORDER BY term expression
112127112278
*/
112128112279
memset(&nc, 0, sizeof(nc));
@@ -112217,11 +112368,11 @@
112217112368
int iCol = -1;
112218112369
Expr *pE, *pDup;
112219112370
if( pItem->fg.done ) continue;
112220112371
pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
112221112372
if( NEVER(pE==0) ) continue;
112222
- if( sqlite3ExprIsInteger(pE, &iCol, 0, 0) ){
112373
+ if( sqlite3ExprIsInteger(pE, &iCol, 0, 1) ){
112223112374
if( iCol<=0 || iCol>pEList->nExpr ){
112224112375
resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
112225112376
return 1;
112226112377
}
112227112378
}else{
@@ -112400,11 +112551,11 @@
112400112551
** copy of the iCol-th result-set expression. */
112401112552
pItem->u.x.iOrderByCol = (u16)iCol;
112402112553
continue;
112403112554
}
112404112555
}
112405
- if( sqlite3ExprIsInteger(pE2, &iCol, 0, 0) ){
112556
+ if( sqlite3ExprIsInteger(pE2, &iCol, 0, 1) ){
112406112557
/* The ORDER BY term is an integer constant. Again, set the column
112407112558
** number so that sqlite3ResolveOrderGroupBy() will convert the
112408112559
** order-by term to a copy of the result-set expression */
112409112560
if( iCol<1 || iCol>0xffff ){
112410112561
resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
@@ -114088,13 +114239,16 @@
114088114239
}else{
114089114240
u32 f = pLeft->flags | pRight->flags;
114090114241
if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse|EP_HasFunc))==EP_IsFalse
114091114242
&& !IN_RENAME_OBJECT
114092114243
){
114244
+ Expr *p;
114093114245
sqlite3ExprDeferredDelete(pParse, pLeft);
114094114246
sqlite3ExprDeferredDelete(pParse, pRight);
114095
- return sqlite3ExprInt32(db, 0);
114247
+ p = sqlite3ExprInt32(db, 0);
114248
+ if( p ) p->op2 = 1;
114249
+ return p;
114096114250
}else{
114097114251
return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
114098114252
}
114099114253
}
114100114254
}
@@ -115872,17 +116026,43 @@
115872116026
** If pParse is not NULL and expression p is a variable, then the variable
115873116027
** is marked so as to cause the statement to be reprepared each time a new
115874116028
** value is bound to it. Except, if parameter bRSI is true, then the statement
115875116029
** will only be reprepared if the rebind changes the value to or from a
115876116030
** "small integer" (either 0 or 1).
116031
+**
116032
+** If pParse is NULL but bRSI is non-zero, then to not accept the input
116033
+** as an integer if the TK_INTEGER opcode has its op2 field set to non-zero.
116034
+** A non-zero Expr.op2 indicates that the expression is a compile-time
116035
+** reduction of a constant expression. For example, "123 NOTNULL" would
116036
+** be rendered by the parser as a TK_INTEGER with value 1, but with op2 set.
116037
+**
116038
+** Summary:
116039
+**
116040
+** pParse bRsi What happens
116041
+** -------- ---- ------------------------------------------------
116042
+** NULL 0 Reject if TK_VARIABLE. Accept if op2!=0
116043
+**
116044
+** NULL 1 Reject if TK_VARIABLE or op2!=0
116045
+**
116046
+** not-NULL 0 Accept TK_VARIABLE and/or op2!=0 but for TK_VARIABLE,
116047
+** cause the statement to reprepare on *any*
116048
+** binding change
116049
+**
116050
+** not-NULL 1 As previous, except reprepare only happens if a binding
116051
+** change move the variable value to or from an integer 0
116052
+** or 1.
115877116053
*/
115878116054
SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse, int bRSI){
115879116055
int iSign = 1; /* Either +1 or -1. */
115880116056
while( 1/*exit-by-break*/ ){
115881116057
if( NEVER(p==0) ) return 0;
115882116058
if( ExprUseUValue(p) ){
116059
+ assert( p->op==TK_INTEGER );
115883116060
*pValue = p->u.iValue*iSign;
116061
+ if( bRSI && pParse==0 && p->op2 ){
116062
+ return 0;
116063
+ }
115884116064
return 1;
115885116065
}
115886116066
if( p->op==TK_UPLUS ){
115887116067
p = p->pLeft;
115888116068
pParse = 0;
@@ -121456,17 +121636,18 @@
121456121636
" AND (type != 'index' OR tbl_name = %Q)",
121457121637
zDb,
121458121638
zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
121459121639
pTab->zName
121460121640
);
121461
-
121462
- sqlite3NestedParse(pParse,
121463
- "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
121464
- "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
121465
- "WHERE type IN ('trigger', 'view')",
121466
- zDb, pTab->zName, iCol, zNew, bQuote
121467
- );
121641
+ if( iSchema!=1 ){
121642
+ sqlite3NestedParse(pParse,
121643
+ "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
121644
+ "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
121645
+ "WHERE type IN ('trigger', 'view')",
121646
+ zDb, pTab->zName, iCol, zNew, bQuote
121647
+ );
121648
+ }
121468121649
121469121650
/* Drop and reload the database schema. */
121470121651
renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename);
121471121652
renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
121472121653
@@ -123152,16 +123333,16 @@
123152123333
regOut = reg+1+nField;
123153123334
}
123154123335
if( i==pTab->iPKey ){
123155123336
sqlite3VdbeAddOp2(v, OP_Null, 0, regOut);
123156123337
}else{
123157
- char aff = pTab->aCol[i].affinity;
123158
- if( aff==SQLITE_AFF_REAL ){
123159
- pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
123160
- }
123161123338
sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
123162
- pTab->aCol[i].affinity = aff;
123339
+ assert( pParse->nErr
123340
+ || sqlite3VdbeGetOp(v,sqlite3VdbeCurrentAddr(v)-1)->opcode
123341
+ != OP_RealAffinity
123342
+ || pTab->aCol[i].affinity==SQLITE_AFF_REAL );
123343
+ sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
123163123344
}
123164123345
nField++;
123165123346
}
123166123347
}
123167123348
if( nField==0 ){
@@ -123208,19 +123389,23 @@
123208123389
** z points to the second token of the constraint definition. For an
123209123390
** unnamed NOT NULL constraint, z points to the first byte past the NOT
123210123391
** keyword.
123211123392
**
123212123393
** Argument eTok may be the token value of the first token of the constraint
123213
-** (e.g. TK_CHECK or TK_REFERENCES) or zero. If it is either TK_REFERENCES
123214
-** or TK_FOREIGN, special parsing is enabled to find the end of the foreign-key
123215
-** constraint definition.
123394
+** (e.g. TK_CHECK or TK_REFERENCES or TK_NOT) or zero. If it is either
123395
+** TK_REFERENCES or TK_FOREIGN, special parsing is enabled to find the
123396
+** end of the foreign-key constraint definition. If eTok is TK_NOT then
123397
+** special processing is enabled to find the end of a NOT NULL constraint
123398
+** including its optional ON CONFLICT clause.
123216123399
**
123217123400
** Return the number of bytes until the end of the constraint.
123218123401
*/
123219123402
static int getConstraint(const u8 *z, int eTok){
123220123403
int iOff = 0;
123221123404
int t = 0;
123405
+ int t2 = 0;
123406
+ int prev = 0;
123222123407
123223123408
#ifndef SQLITE_OMIT_FOREIGN_KEY
123224123409
if( eTok==TK_FOREIGN ){
123225123410
/* For a FOREIGN KEY constraint, use getConstraint() to parse everything
123226123411
** up to the REFERENCES keyword. Then getConstraintToken() to consume
@@ -123232,10 +123417,18 @@
123232123417
123233123418
if( eTok==TK_REFERENCES ){
123234123419
/* REFERENCES is followed by a table name. Gobble this up here in
123235123420
** case the table name is a fallback token like TK_GENERATED. */
123236123421
iOff += getConstraintToken(&z[iOff], &t);
123422
+
123423
+ /* If the table names is followed by a parenthesized list of columns,
123424
+ ** also skip that, lest the close ")" end the constraint in the
123425
+ ** while loop below. */
123426
+ iOff += getWhitespace(&z[iOff]);
123427
+ if( z[iOff]=='(' ){
123428
+ iOff += getConstraintToken(&z[iOff], &t);
123429
+ }
123237123430
}
123238123431
#endif
123239123432
123240123433
/* Now, the current constraint proceeds until the next occurence of one
123241123434
** of the following tokens:
@@ -123242,21 +123435,38 @@
123242123435
**
123243123436
** CONSTRAINT, PRIMARY, NOT, UNIQUE, CHECK, DEFAULT,
123244123437
** COLLATE, REFERENCES, FOREIGN, GENERATED, AS, RP, or COMMA
123245123438
**
123246123439
** Also exit the loop if ILLEGAL turns up.
123440
+ **
123441
+ ** Exceptions:
123442
+ **
123443
+ ** * For NOT NULL constraints, also terminate at DEFERRABLE
123444
+ **
123445
+ ** * Do not terminate at DEFAULT if the previous token was SET
123247123446
*/
123248123447
while( 1 ){
123249123448
int n = getConstraintToken(&z[iOff], &t);
123250
- if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_NOT || t==TK_UNIQUE
123251
- || t==TK_CHECK || t==TK_DEFAULT || t==TK_COLLATE || t==TK_REFERENCES
123449
+ if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_UNIQUE
123450
+ || t==TK_CHECK || t==TK_COLLATE || t==TK_REFERENCES
123451
+ || (t==TK_DEFAULT && prev!=TK_SET)
123252123452
|| t==TK_FOREIGN || t==TK_RP || t==TK_COMMA || t==TK_ILLEGAL
123253123453
|| t==TK_AS || t==TK_GENERATED
123454
+ || (eTok==TK_NOT && t==TK_DEFERRABLE)
123254123455
){
123255123456
break;
123457
+ }
123458
+ if( t==TK_NOT
123459
+ && (eTok!=TK_REFERENCES
123460
+ || (getConstraintToken(&z[iOff+n],&t2), t2!=TK_DEFERRABLE))
123461
+ ){
123462
+ /* NOT ends the constraint unless eTok is TK_REFERENCES and the next
123463
+ ** token is DEFERRABLE */
123464
+ break;
123256123465
}
123257123466
iOff += n;
123467
+ prev = t;
123258123468
}
123259123469
123260123470
return iOff;
123261123471
}
123262123472
@@ -123419,12 +123629,16 @@
123419123629
iEnd = iOff;
123420123630
break;
123421123631
}
123422123632
123423123633
}else if( t==TK_NOT && iNotNull==ii ){
123424
- iEnd = iOff + getConstraint(&zSql[iOff], 0);
123425
- break;
123634
+ int t2;
123635
+ (void)getConstraintToken(zSql+iOff, &t2);
123636
+ if( t2==TK_NULL ){
123637
+ iEnd = iOff + getConstraint(zSql+iOff, TK_NOT);
123638
+ break;
123639
+ }
123426123640
}else if( t==TK_RP || t==TK_ILLEGAL ){
123427123641
iEnd = -1;
123428123642
break;
123429123643
}else if( t==TK_COMMA ){
123430123644
break;
@@ -136891,11 +137105,11 @@
136891137105
break;
136892137106
default:
136893137107
return;
136894137108
}
136895137109
if( argc==2 ){
136896
- switch( sqlite3_value_numeric_type(argv[0]) ){
137110
+ switch( sqlite3_value_numeric_type(argv[1]) ){
136897137111
case SQLITE_INTEGER:
136898137112
case SQLITE_FLOAT:
136899137113
b = log(x);
136900137114
if( b<=0.0 ) return;
136901137115
x = sqlite3_value_double(argv[1]);
@@ -137625,10 +137839,11 @@
137625137839
assert( argc==3 );
137626137840
x = sqlite3_value_double(argv[0]);
137627137841
y = sqlite3_value_int(argv[1]);
137628137842
z = sqlite3_value_int(argv[2]);
137629137843
if( z<=0 ) z = 1;
137844
+ if( y<-100000000 ) y = -100000000; /* Same limit as printf() */
137630137845
sqlite3FpDecode(&s, x, y, z);
137631137846
if( s.isSpecial==2 ){
137632137847
sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
137633137848
}else{
137634137849
sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
@@ -138446,10 +138661,148 @@
138446138661
pExpr->iTable = iCursor;
138447138662
pExpr->iColumn = iCol;
138448138663
}
138449138664
return pExpr;
138450138665
}
138666
+
138667
+/*
138668
+** Column iCol1 of table p1 is to be compared to column iCol2 of table p2.
138669
+** Return non-zero if the comparison will use an affinity compatible
138670
+** with the affinity of column iCol1 of table p1. Or zero if it will not.
138671
+**
138672
+** This is used in cases where (p1.iCol1 = p2.iCol2) must be evaluated
138673
+** using the affinity of p1.iCol1 as part of foreign key processing.
138674
+*/
138675
+static int fkUsesLhsAffinity(Table *p1, int iCol1, Table *p2, int iCol2){
138676
+ u8 aff1 = iCol1>=0 ? p1->aCol[iCol1].affinity : SQLITE_AFF_NUMERIC;
138677
+ u8 aff2 = p2->aCol[iCol2].affinity;
138678
+
138679
+ /* If either side has a numeric affinity, then the comparison uses numeric
138680
+ ** affinity. So the affinity matches if aff1 is numeric, but not if aff1
138681
+ ** is not numeric but aff2 is. */
138682
+ if( sqlite3IsNumericAffinity(aff1) ) return 1;
138683
+ if( sqlite3IsNumericAffinity(aff2) ) return 0;
138684
+
138685
+ assert( aff1==SQLITE_AFF_TEXT || aff1==SQLITE_AFF_BLOB );
138686
+ assert( aff2==SQLITE_AFF_TEXT || aff2==SQLITE_AFF_BLOB );
138687
+
138688
+ /* At this point, either the affinities are the same or one is TEXT and
138689
+ ** the other BLOB. In the second case the comparison will be done with
138690
+ ** BLOB affinity, which does not match if aff1 is TEXT. */
138691
+ return (aff1==aff2 || aff1==SQLITE_AFF_BLOB);
138692
+}
138693
+
138694
+#ifndef SQLITE_OMIT_CAST
138695
+/*
138696
+** Return non-zero if a lookup on column iCol of table pTab, using collation
138697
+** sequence zColl, can be done using either the rowid or an index. That is,
138698
+** if iCol is the INTEGER PRIMARY KEY of pTab, or if pTab has an index
138699
+** whose left-most column is iCol and whose collation sequence for that
138700
+** column is zColl. Return zero otherwise.
138701
+*/
138702
+static int fkChildKeyIsIndexed(Table *pTab, int iCol, const char *zColl){
138703
+ Index *pIdx;
138704
+ if( iCol==pTab->iPKey ) return 1;
138705
+ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138706
+ if( pIdx->aiColumn[0]==iCol
138707
+ && pIdx->pPartIdxWhere==0
138708
+ && sqlite3StrICmp(pIdx->azColl[0], zColl)==0
138709
+ ){
138710
+ return 1;
138711
+ }
138712
+ }
138713
+ return 0;
138714
+}
138715
+#endif
138716
+
138717
+/*
138718
+** Return an expression equivalent to (pLeft = pRight), where pLeft is a
138719
+** parent key value and pRight is an unresolved reference to column
138720
+** iChildCol of child table pChild. The comparison uses the affinity and
138721
+** collation sequence of column iCol of parent table pTab. pLeft must
138722
+** already carry that affinity and collation sequence. Ownership of
138723
+** both pLeft and pRight passes to this routine.
138724
+**
138725
+** If the simple (pLeft = pRight) does not naturally use the affinity of
138726
+** the parent column, the comparison is coded as (pLeft = +pRight). But
138727
+** the unary + prevents any index on the child column from being used.
138728
+** So in that case, if the child column is indexed, an extra term is added:
138729
+**
138730
+** (pLeft = +pRight) AND
138731
+** (pRight COLLATE <coll> IN (+pLeft, +CAST(pLeft AS NUMERIC)))
138732
+**
138733
+** Each child value that matches pLeft under the affinity of the parent
138734
+** column is either equal to pLeft itself or numerically equal to
138735
+** CAST(pLeft AS NUMERIC). So the IN term matches a superset of the
138736
+** child rows that the exact (pLeft = +pRight) term does, and since it
138737
+** compares using the affinity of the child column, it may be used with
138738
+** an index on the child column. The unary + operators on the RHS of the
138739
+** IN ensure that no affinity other than that of the child column is
138740
+** applied.
138741
+*/
138742
+static Expr *fkParentChildEq(
138743
+ Parse *pParse, /* Parsing context */
138744
+ Expr *pLeft, /* Parent key value */
138745
+ Expr *pRight, /* Unresolved reference to child key column */
138746
+ Table *pTab, /* Parent table */
138747
+ int iCol, /* Parent key column, or <0 for rowid */
138748
+ Table *pChild, /* Child table */
138749
+ int iChildCol, /* Child key column */
138750
+ int bComposite /* This is part of a composite FK */
138751
+){
138752
+ sqlite3 *db = pParse->db;
138753
+ Expr *pEq;
138754
+#ifndef SQLITE_OMIT_CAST
138755
+ Expr *pIn = 0;
138756
+#endif
138757
+
138758
+ if( fkUsesLhsAffinity(pTab, iCol, pChild, iChildCol) ){
138759
+ return sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
138760
+ }
138761
+ assert( iCol>=0 );
138762
+
138763
+#ifndef SQLITE_OMIT_CAST
138764
+ {
138765
+ const char *zColl = sqlite3ColumnColl(&pTab->aCol[iCol]);
138766
+ if( zColl==0 ) zColl = sqlite3StrBINARY;
138767
+
138768
+ /* The IN term is only useful if it can drive a lookup on the child
138769
+ ** table. Omit it otherwise, so that a full scan of the child table
138770
+ ** does not have to evaluate it for every row. */
138771
+ if( bComposite || fkChildKeyIsIndexed(pChild, iChildCol, zColl) ){
138772
+ static const Token tNumeric = { "NUMERIC", 7 };
138773
+ Expr *pCast;
138774
+ ExprList *pList;
138775
+
138776
+ pCast = sqlite3ExprAlloc(db, TK_CAST, &tNumeric, 0);
138777
+ sqlite3ExprAttachSubtrees(db, pCast, sqlite3ExprDup(db, pLeft, 0), 0);
138778
+ pList = sqlite3ExprListAppend(pParse, 0,
138779
+ sqlite3PExpr(pParse, TK_UPLUS, sqlite3ExprDup(db, pLeft, 0), 0)
138780
+ );
138781
+ pList = sqlite3ExprListAppend(pParse, pList,
138782
+ sqlite3PExpr(pParse, TK_UPLUS, pCast, 0)
138783
+ );
138784
+ pIn = sqlite3PExpr(pParse, TK_IN, sqlite3ExprAddCollateString(pParse,
138785
+ sqlite3ExprDup(db, pRight, 0), zColl
138786
+ ), 0);
138787
+ if( pIn ){
138788
+ pIn->x.pList = pList;
138789
+ sqlite3ExprSetHeightAndFlags(pParse, pIn);
138790
+ }else{
138791
+ sqlite3ExprListDelete(db, pList);
138792
+ }
138793
+ }
138794
+ }
138795
+#endif
138796
+
138797
+ pRight = sqlite3PExpr(pParse, TK_UPLUS, pRight, 0);
138798
+ pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
138799
+#ifndef SQLITE_OMIT_CAST
138800
+ pEq = sqlite3ExprAnd(pParse, pEq, pIn);
138801
+#endif
138802
+ return pEq;
138803
+}
138451138804
138452138805
/*
138453138806
** This function is called to generate code executed when a row is deleted
138454138807
** from the parent table of foreign key constraint pFKey and, if pFKey is
138455138808
** deferred, when a row is inserted into the same table. When generating
@@ -138515,20 +138868,22 @@
138515138868
*/
138516138869
for(i=0; i<pFKey->nCol; i++){
138517138870
Expr *pLeft; /* Value from parent table row */
138518138871
Expr *pRight; /* Column ref to child table */
138519138872
Expr *pEq; /* Expression (pLeft = pRight) */
138520
- i16 iCol; /* Index of column in child table */
138873
+ i16 iCol; /* Index of column in parent table */
138874
+ i16 iCol2; /* Index of column in child table */
138521138875
const char *zCol; /* Name of column in child table */
138522138876
138523138877
iCol = pIdx ? pIdx->aiColumn[i] : -1;
138524138878
pLeft = exprTableRegister(pParse, pTab, regData, iCol);
138525
- iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
138526
- assert( iCol>=0 );
138527
- zCol = pFKey->pFrom->aCol[iCol].zCnName;
138879
+ iCol2 = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
138880
+ assert( iCol2>=0 );
138881
+ zCol = pFKey->pFrom->aCol[iCol2].zCnName;
138528138882
pRight = sqlite3Expr(db, TK_ID, zCol);
138529
- pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
138883
+ pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iCol,
138884
+ pFKey->pFrom, iCol2, pFKey->nCol>0);
138530138885
pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
138531138886
}
138532138887
138533138888
/* If the child table is the same as the parent table, then add terms
138534138889
** to the WHERE clause that prevent this entry from being scanned.
@@ -138784,17 +139139,16 @@
138784139139
** Return true if the parser passed as the first argument is being
138785139140
** used to code a trigger that is really a "SET NULL" action belonging
138786139141
** to trigger pFKey.
138787139142
*/
138788139143
static int isSetNullAction(Parse *pParse, FKey *pFKey){
138789
- Parse *pTop = sqlite3ParseToplevel(pParse);
138790
- if( pTop->pTriggerPrg ){
138791
- Trigger *p = pTop->pTriggerPrg->pTrigger;
139144
+ Trigger *p = pParse->pTrigCoded;
139145
+ if( p ){
138792139146
if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
138793139147
|| (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
138794139148
){
138795
- assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
139149
+ assert( (pParse->db->flags & SQLITE_FkNoAction)==0 );
138796139150
return 1;
138797139151
}
138798139152
}
138799139153
return 0;
138800139154
}
@@ -139184,30 +139538,39 @@
139184139538
Token tOld = { "old", 3 }; /* Literal "old" token */
139185139539
Token tNew = { "new", 3 }; /* Literal "new" token */
139186139540
Token tFromCol; /* Name of column in child table */
139187139541
Token tToCol; /* Name of column in parent table */
139188139542
int iFromCol; /* Idx of column in child table */
139189
- Expr *pEq; /* tFromCol = OLD.tToCol */
139543
+ int iToCol; /* Idx of column in parent table */
139544
+ Expr *pEq; /* OLD.tToCol = tFromCol */
139545
+ Expr *pLeft; /* OLD.tToCol */
139546
+ Expr *pRight; /* tFromCol */
139190139547
139191139548
iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
139192139549
assert( iFromCol>=0 );
139193139550
assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
139194139551
assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
139195
- sqlite3TokenInit(&tToCol,
139196
- pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
139552
+ iToCol = pIdx ? pIdx->aiColumn[i] : pTab->iPKey;
139553
+ sqlite3TokenInit(&tToCol, pTab->aCol[iToCol].zCnName);
139197139554
sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
139198139555
139199
- /* Create the expression "OLD.zToCol = zFromCol". It is important
139200
- ** that the "OLD.zToCol" term is on the LHS of the = operator, so
139201
- ** that the affinity and collation sequence associated with the
139202
- ** parent table are used for the comparison. */
139203
- pEq = sqlite3PExpr(pParse, TK_EQ,
139204
- sqlite3PExpr(pParse, TK_DOT,
139205
- sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
139206
- sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
139207
- sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
139556
+ pLeft = sqlite3PExpr(pParse, TK_DOT,
139557
+ sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
139558
+ sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
139208139559
);
139560
+ pRight = sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0);
139561
+
139562
+ /* OLD.* columns do not normally have an affinity. So set the affinity
139563
+ ** of OLD.tToCol (the parent key) explicitly here. */
139564
+ if( pLeft ) pLeft->affExpr = pTab->aCol[iToCol].affinity;
139565
+
139566
+ /* Create the expression "OLD.tToCol = tFromCol". It is important
139567
+ ** that the "OLD.tToCol" term is on the LHS of the = operator, so
139568
+ ** that the collation sequence associated with the parent table is
139569
+ ** used for the comparison. */
139570
+ pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iToCol,
139571
+ pFKey->pFrom, iFromCol, pFKey->nCol>1);
139209139572
pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
139210139573
139211139574
/* For ON UPDATE, construct the next term of the WHEN clause.
139212139575
** The final WHEN clause will be like this:
139213139576
**
@@ -144652,29 +145015,36 @@
144652145015
** was not on the list.
144653145016
*/
144654145017
SQLITE_API int sqlite3_cancel_auto_extension(
144655145018
void (*xInit)(void)
144656145019
){
145020
+ int n = 0;
145021
+#ifndef SQLITE_OMIT_AUTOINIT
145022
+ if( sqlite3_initialize() ){
145023
+ return 0;
145024
+ }else
145025
+#endif
145026
+ {
144657145027
#if SQLITE_THREADSAFE
144658
- sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
145028
+ sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
144659145029
#endif
144660
- int i;
144661
- int n = 0;
144662
- wsdAutoextInit;
145030
+ int i;
145031
+ wsdAutoextInit;
144663145032
#ifdef SQLITE_ENABLE_API_ARMOR
144664
- if( xInit==0 ) return 0;
145033
+ if( xInit==0 ) return 0;
144665145034
#endif
144666
- sqlite3_mutex_enter(mutex);
144667
- for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
144668
- if( wsdAutoext.aExt[i]==xInit ){
144669
- wsdAutoext.nExt--;
144670
- wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
144671
- n++;
144672
- break;
144673
- }
144674
- }
144675
- sqlite3_mutex_leave(mutex);
145035
+ sqlite3_mutex_enter(mutex);
145036
+ for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
145037
+ if( wsdAutoext.aExt[i]==xInit ){
145038
+ wsdAutoext.nExt--;
145039
+ wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
145040
+ n++;
145041
+ break;
145042
+ }
145043
+ }
145044
+ sqlite3_mutex_leave(mutex);
145045
+ }
144676145046
return n;
144677145047
}
144678145048
144679145049
/*
144680145050
** Reset the automatic extension loading mechanism.
@@ -147256,18 +147626,13 @@
147256147626
sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
147257147627
}
147258147628
sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
147259147629
1, 0, &iDataCur, &iIdxCur);
147260147630
/* reg[7] counts the number of entries in the table.
147261
- ** reg[8+i] counts the number of entries in the i-th index
147262147631
*/
147263147632
sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
147264
- for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
147265
- sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
147266
- }
147267
- assert( pParse->nMem>=8+j );
147268
- assert( sqlite3NoTempsInRange(pParse,1,7+j) );
147633
+ assert( sqlite3NoTempsInRange(pParse,1,7) );
147269147634
sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
147270147635
loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
147271147636
147272147637
/* Fetch the right-most column from the table. This will cause
147273147638
** the entire record header to be parsed and sanity checked. It
@@ -147476,11 +147841,10 @@
147476147841
int ckUniq = sqlite3VdbeMakeLabel(pParse);
147477147842
if( pPk==pIdx ) continue;
147478147843
r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
147479147844
pPrior, r1);
147480147845
pPrior = pIdx;
147481
- sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
147482147846
/* Verify that an index entry exists for the current table row */
147483147847
sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
147484147848
pIdx->nColumn); VdbeCoverage(v);
147485147849
jmp2 = sqlite3VdbeAddOp3(v, OP_IFindKey, iIdxCur+j, ckUniq, r1);
147486147850
VdbeCoverage(v);
@@ -151940,11 +152304,11 @@
151940152304
sqlite3HashInit(&ht);
151941152305
if( pEList ){
151942152306
nCol = pEList->nExpr;
151943152307
aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
151944152308
testcase( aCol==0 );
151945
- if( NEVER(nCol>32767) ) nCol = 32767;
152309
+ if( nCol>32767 ) nCol = 32767;
151946152310
}else{
151947152311
nCol = 0;
151948152312
aCol = 0;
151949152313
}
151950152314
assert( nCol==(i16)nCol );
@@ -153185,13 +153549,16 @@
153185153549
assert( pOrderBy );
153186153550
nOrderBy = pOrderBy->nExpr;
153187153551
153188153552
/* For operators other than UNION ALL we have to make sure that
153189153553
** the ORDER BY clause covers every term of the result set. Add
153190
- ** terms to the ORDER BY clause as necessary.
153554
+ ** terms to the ORDER BY clause as necessary. Do the same for UNION ALL
153555
+ ** if any operator to the left is not UNION ALL, so that the ORDER BY
153556
+ ** carries the collation of every column into that sub-compound.
153191153557
*/
153192
- if( op!=TK_ALL ){
153558
+ for(pPrior=p; pPrior && pPrior->op==TK_ALL; pPrior=pPrior->pPrior){}
153559
+ if( pPrior && pPrior->pPrior ){
153193153560
for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
153194153561
struct ExprList_item *pItem;
153195153562
for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
153196153563
assert( pItem!=0 );
153197153564
assert( pItem->u.x.iOrderByCol>0 );
@@ -153244,13 +153611,15 @@
153244153611
pParse->nMem += nExpr+1;
153245153612
sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
153246153613
pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
153247153614
if( pKeyDup ){
153248153615
assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
153249
- for(i=0; i<nExpr; i++){
153250
- pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
153251
- pKeyDup->aSortFlags[i] = 0;
153616
+ /* Use the ORDER BY collations, not multiSelectCollSeq(p), because
153617
+ ** p might be just one part of a larger compound, in which case
153618
+ ** the ORDER BY carries the COLLATE of the larger compound. */
153619
+ for(i=0; ALWAYS(pKeyMerge) && i<nOrderBy; i++){
153620
+ pKeyDup->aColl[pOrderBy->a[i].u.x.iOrderByCol-1] = pKeyMerge->aColl[i];
153252153621
}
153253153622
}
153254153623
}
153255153624
153256153625
/* Separate the left and the right query from one another
@@ -155303,20 +155672,20 @@
155303155672
*pNew = *p;
155304155673
p->pSrc = pNewSrc;
155305155674
p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
155306155675
p->op = TK_SELECT;
155307155676
p->pWhere = 0;
155308
- pNew->pGroupBy = 0;
155309
- pNew->pHaving = 0;
155677
+ p->pGroupBy = 0;
155678
+ p->pHaving = 0;
155310155679
pNew->pOrderBy = 0;
155311155680
p->pPrior = 0;
155312155681
p->pNext = 0;
155313155682
p->pWith = 0;
155314155683
#ifndef SQLITE_OMIT_WINDOWFUNC
155315155684
p->pWinDefn = 0;
155316155685
#endif
155317
- p->selFlags &= ~(u32)SF_Compound;
155686
+ p->selFlags &= ~(u32)(SF_Compound|SF_Distinct);
155318155687
assert( (p->selFlags & SF_Converted)==0 );
155319155688
p->selFlags |= SF_Converted;
155320155689
assert( pNew->pPrior!=0 );
155321155690
pNew->pPrior->pNext = pNew;
155322155691
pNew->pLimit = 0;
@@ -155399,10 +155768,12 @@
155399155768
pParse->pWith = pWith;
155400155769
}
155401155770
}
155402155771
return pWith;
155403155772
}
155773
+
155774
+static int selectExpanderWalk(Walker*, Select*);
155404155775
155405155776
/*
155406155777
** This function checks to see if argument pFrom refers to a CTE declared by
155407155778
** a WITH clause on the stack currently maintained by the parser (on the
155408155779
** pParse->pWith linked list). And if currently processing a CTE expression,
@@ -155536,19 +155907,17 @@
155536155907
}
155537155908
if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
155538155909
pRecTerm = pRecTerm->pPrior;
155539155910
}
155540155911
155912
+ if( pParse->nTab >= db->aLimit[SQLITE_LIMIT_CTES]
155913
+ && db->aLimit[SQLITE_LIMIT_CTES]>0
155914
+ ){
155915
+ sqlite3ErrorMsg(pParse, "too many CTEs");
155916
+ return 2;
155917
+ }
155541155918
#if SQLITE_MAX_EXPR_DEPTH>0
155542
- if( pParse->nTab>=db->aLimit[SQLITE_LIMIT_EXPR_DEPTH] ){
155543
- /* Bug 2026-10-04T05:35:14Z: Prevent nested CTEs from generating
155544
- ** an exponential number of cursors. The error message here will
155545
- ** be "Nested too deep", which isn't exactly correct, but it is
155546
- ** sufficient, and we don't want to use extra code space for more
155547
- ** detail on such an obscure error. */
155548
- pParse->nNestSel = pParse->nTab;
155549
- }
155550155919
pParse->nNestSel++;
155551155920
if( checkSubqueryNestingDepth(pParse) ) return 2;
155552155921
#endif
155553155922
pCte->zCteErr = "circular reference: %s";
155554155923
pSavedWith = pParse->pWith;
@@ -155566,11 +155935,11 @@
155566155935
if( rc ){
155567155936
pParse->pWith = pSavedWith;
155568155937
return 2;
155569155938
}
155570155939
}else{
155571
- if( sqlite3WalkSelect(pWalker, pSel) ){
155940
+ if( selectExpanderWalk(pWalker, pSel) ){
155572155941
pParse->pWith = pSavedWith;
155573155942
return 2;
155574155943
}
155575155944
}
155576155945
pParse->pWith = pWith;
@@ -155828,11 +156197,14 @@
155828156197
#endif
155829156198
nCol = pTab->nCol;
155830156199
pTab->nCol = -1;
155831156200
pWalker->eCode = 1; /* Turn on Select.selId renumbering */
155832156201
if( pFrom->fg.isSubquery ){
155833
- sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect);
156202
+ bft hasCompound = pParse->hasCompound;
156203
+ pParse->hasCompound = 1;
156204
+ selectExpanderWalk(pWalker, pFrom->u4.pSubq->pSelect);
156205
+ pParse->hasCompound = hasCompound;
155834156206
}
155835156207
pWalker->eCode = eCodeOrig;
155836156208
pTab->nCol = nCol;
155837156209
}
155838156210
#endif
@@ -156118,10 +156490,31 @@
156118156490
SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
156119156491
UNUSED_PARAMETER2(NotUsed, NotUsed2);
156120156492
assert( 0 );
156121156493
}
156122156494
#endif
156495
+
156496
+/*
156497
+** The first argument is a Walker object created by sqlite3SelectExpand().
156498
+** This function uses the walker to expand the SELECT statement passed
156499
+** as the second argument. If necessary, it also converts any compound
156500
+** SELECT statements that have COLLATE clauses on ORDER BY expressions
156501
+** to sub-queries. Return the return value of sqlite3WalkSelect().
156502
+*/
156503
+static int selectExpanderWalk(Walker *pWalker, Select *pSelect){
156504
+ assert( pWalker->xExprCallback==sqlite3ExprWalkNoop );
156505
+ assert( pWalker->pParse!=0 );
156506
+ if( OK_IF_ALWAYS_TRUE(pWalker->pParse->hasCompound) ){
156507
+ pWalker->xSelectCallback = convertCompoundSelectToSubquery;
156508
+ pWalker->xSelectCallback2 = 0;
156509
+ sqlite3WalkSelect(pWalker, pSelect);
156510
+ }
156511
+ pWalker->xSelectCallback = selectExpander;
156512
+ pWalker->xSelectCallback2 = sqlite3SelectPopWith;
156513
+ return sqlite3WalkSelect(pWalker, pSelect);
156514
+}
156515
+
156123156516
/*
156124156517
** This routine "expands" a SELECT statement and all of its subqueries.
156125156518
** For additional information on what it means to "expand" a SELECT
156126156519
** statement, see the comment on the selectExpand worker callback above.
156127156520
**
@@ -156135,19 +156528,12 @@
156135156528
*/
156136156529
static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
156137156530
Walker w;
156138156531
w.xExprCallback = sqlite3ExprWalkNoop;
156139156532
w.pParse = pParse;
156140
- if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
156141
- w.xSelectCallback = convertCompoundSelectToSubquery;
156142
- w.xSelectCallback2 = 0;
156143
- sqlite3WalkSelect(&w, pSelect);
156144
- }
156145
- w.xSelectCallback = selectExpander;
156146
- w.xSelectCallback2 = sqlite3SelectPopWith;
156147156533
w.eCode = 0;
156148
- sqlite3WalkSelect(&w, pSelect);
156534
+ selectExpanderWalk(&w, pSelect);
156149156535
}
156150156536
156151156537
156152156538
#ifndef SQLITE_OMIT_SUBQUERY
156153156539
/*
@@ -157868,15 +158254,25 @@
157868158254
/* This is a CTE for which materialization code has already been
157869158255
** generated. Invoke the subroutine to compute the materialization,
157870158256
** then make the pItem->iCursor be a copy of the ephemeral table that
157871158257
** holds the result of the materialization. tag-select-0484 */
157872158258
CteUse *pCteUse = pItem->u2.pCteUse;
158259
+ /* The materialization is computed once per run of the statement or
158260
+ ** trigger program and does not change after that. So the subroutine
158261
+ ** call and the OP_OpenDup are needed on the first pass only. The
158262
+ ** OP_NullRow runs on every pass and leaves the duplicate cursor not
158263
+ ** positioned on a row, as OP_OpenDup does. */
158264
+ int addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
157873158265
sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
157874158266
if( pItem->iCursor!=pCteUse->iCur ){
157875158267
sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
157876158268
VdbeComment((v, "%!S", pItem));
157877158269
}
158270
+ sqlite3VdbeJumpHere(v, addrOnce);
158271
+ if( pItem->iCursor!=pCteUse->iCur ){
158272
+ sqlite3VdbeAddOp1(v, OP_NullRow, pItem->iCursor);
158273
+ }
157878158274
pSub->nSelectRow = pCteUse->nRowEst;
157879158275
}else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){
157880158276
/* This view has already been materialized by a prior entry in
157881158277
** this same FROM clause. Reuse it. tag-select-0486 */
157882158278
Subquery *pPriorSubq;
@@ -160259,10 +160655,11 @@
160259160655
sqlite3ParseObjectInit(&sSubParse, db);
160260160656
memset(&sNC, 0, sizeof(sNC));
160261160657
sNC.pParse = &sSubParse;
160262160658
sSubParse.pTriggerTab = pTab;
160263160659
sSubParse.pToplevel = pTop;
160660
+ sSubParse.pTrigCoded = pTrigger;
160264160661
sSubParse.zAuthContext = pTrigger->zName;
160265160662
sSubParse.eTriggerOp = pTrigger->op;
160266160663
sSubParse.nQueryLoop = pParse->nQueryLoop;
160267160664
sSubParse.prepFlags = pParse->prepFlags;
160268160665
sSubParse.oldmask = 0;
@@ -161073,14 +161470,18 @@
161073161470
{
161074161471
int rc;
161075161472
rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
161076161473
j<0 ? "ROWID" : pTab->aCol[j].zCnName,
161077161474
db->aDb[iDb].zDbSName);
161078
- if( rc==SQLITE_DENY ){
161079
- goto update_cleanup;
161080
- }else if( rc==SQLITE_IGNORE ){
161081
- aXRef[j] = -1;
161475
+ if( rc!=SQLITE_OK ){
161476
+ if( rc==SQLITE_DENY ){
161477
+ goto update_cleanup;
161478
+ }else if( j<0 || j==pTab->iPKey ){
161479
+ chngRowid = 0;
161480
+ }else{
161481
+ aXRef[j] = -1;
161482
+ }
161082161483
}
161083161484
}
161084161485
#endif
161085161486
}
161086161487
assert( (chngRowid & chngPk)==0 );
@@ -163496,11 +163897,21 @@
163496163897
/* Justification of ALWAYS(): The xConstructor method is required to
163497163898
** create a valid sqlite3_vtab if it returns SQLITE_OK. */
163498163899
if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
163499163900
rc = growVTrans(db);
163500163901
if( rc==SQLITE_OK ){
163501
- addToVTrans(db, sqlite3GetVTable(db, pTab));
163902
+ VTable *pVTab = sqlite3GetVTable(db, pTab);
163903
+ const sqlite3_module *pModule = pMod->pModule;
163904
+ int iSvpt = db->nStatement + db->nSavepoint;
163905
+ addToVTrans(db, pVTab);
163906
+ /* As in sqlite3VtabBegin(): open the current savepoint on the new
163907
+ ** table, so that a ROLLBACK TO that undoes the CREATE also invokes
163908
+ ** its xRollbackTo method. */
163909
+ if( iSvpt && pModule->iVersion>=2 && pModule->xSavepoint ){
163910
+ pVTab->iSavepoint = iSvpt;
163911
+ rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
163912
+ }
163502163913
}
163503163914
}
163504163915
163505163916
return rc;
163506163917
}
@@ -165382,10 +165793,13 @@
165382165793
pOrigLhs->a[iField].pExpr = 0;
165383165794
}
165384165795
nRhs++;
165385165796
}
165386165797
}
165798
+ if( nRhs<pOrigRhs->nExpr ){
165799
+ ExprClearProperty(pNew, EP_Subrtn); /* See bug 2026-10-16T22:07:15Z */
165800
+ }
165387165801
sqlite3ExprListDelete(db, pOrigRhs);
165388165802
if( pOrigLhs ){
165389165803
sqlite3ExprListDelete(db, pOrigLhs);
165390165804
pNew->pLeft->x.pList = pLhs;
165391165805
}
@@ -166907,11 +167321,13 @@
166907167321
SWAP(WhereTerm *, pRangeEnd, pRangeStart);
166908167322
SWAP(u8, bSeekPastNull, bStopAtNull);
166909167323
SWAP(u8, nBtm, nTop);
166910167324
}
166911167325
166912
- if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
167326
+ if( (iLevel>0 || (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0)
167327
+ && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0
167328
+ ){
166913167329
/* In case OP_SeekScan is used, ensure that the index cursor does not
166914167330
** point to a valid row for the first iteration of this loop. */
166915167331
sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
166916167332
}
166917167333
@@ -168887,11 +169303,11 @@
168887169303
if( pLeft==0 ){
168888169304
pNew = 0; /* Collating sequence mismatch */
168889169305
}else{
168890169306
pDup = sqlite3ExprDup(db, pLeft, 0);
168891169307
if( sqlite3ExprCollSeq(pParse, pDup)!=pCollSeq
168892
- && ALWAYS(pCollSeq!=0)
169308
+ && pCollSeq!=0
168893169309
){
168894169310
assert( pCollSeq->zName!=0 );
168895169311
pDup = sqlite3ExprAddCollateString(pParse, pDup, pCollSeq->zName);
168896169312
}
168897169313
pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
@@ -173972,11 +174388,11 @@
173972174388
i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
173973174389
SrcList *pTabList; /* The FROM clause */
173974174390
SrcItem *pSrc; /* The FROM clause btree term to add */
173975174391
WhereLoop *pNew; /* Template WhereLoop object */
173976174392
int rc = SQLITE_OK; /* Return code */
173977
- int iSortIdx = 1; /* Index number */
174393
+ int iSortIdx; /* Index number */
173978174394
int b; /* A boolean value */
173979174395
LogEst rSize; /* number of rows in the table */
173980174396
WhereClause *pWC; /* The parsed WHERE clause */
173981174397
Table *pTab; /* Table being queried */
173982174398
@@ -173991,11 +174407,15 @@
173991174407
if( pSrc->fg.isIndexedBy ){
173992174408
assert( pSrc->fg.isCte==0 );
173993174409
/* An INDEXED BY clause specifies a particular index to use */
173994174410
pProbe = pSrc->u2.pIBIndex;
173995174411
}else if( !HasRowid(pTab) ){
173996
- pProbe = pTab->pIndex;
174412
+ if( pSrc->fg.notIndexed ){
174413
+ pProbe = sqlite3PrimaryKeyIndex(pTab);
174414
+ }else{
174415
+ pProbe = pTab->pIndex;
174416
+ }
173997174417
}else{
173998174418
/* There is no INDEXED BY clause. Create a fake Index object in local
173999174419
** variable sPk to represent the rowid primary key index. Make this
174000174420
** fake index the first in a chain of Index objects with all of the real
174001174421
** indices to follow */
@@ -174010,15 +174430,11 @@
174010174430
sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
174011174431
sPk.idxType = SQLITE_IDXTYPE_IPK;
174012174432
aiRowEstPk[0] = pTab->nRowLogEst;
174013174433
aiRowEstPk[1] = 0;
174014174434
pFirst = pSrc->pSTab->pIndex;
174015
- if( pSrc->fg.notIndexed==0 ){
174016
- /* The real indices of the table are only considered if the
174017
- ** NOT INDEXED qualifier is omitted from the FROM clause */
174018
- sPk.pNext = pFirst;
174019
- }
174435
+ sPk.pNext = pFirst;
174020174436
pProbe = &sPk;
174021174437
}
174022174438
rSize = pTab->nRowLogEst;
174023174439
174024174440
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
@@ -174076,12 +174492,14 @@
174076174492
}
174077174493
#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
174078174494
174079174495
/* Loop over all indices. If there was an INDEXED BY clause, then only
174080174496
** consider index pProbe. */
174081
- for(; rc==SQLITE_OK && pProbe;
174082
- pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
174497
+ for(iSortIdx=1;
174498
+ rc==SQLITE_OK && pProbe;
174499
+ pProbe=(pSrc->fg.isIndexedBy||pSrc->fg.notIndexed)?0:pProbe->pNext,
174500
+ iSortIdx++
174083174501
){
174084174502
if( pProbe->pPartIdxWhere!=0
174085174503
&& !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
174086174504
pProbe->pPartIdxWhere)
174087174505
){
@@ -174797,10 +175215,12 @@
174797175215
if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
174798175216
174799175217
for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
174800175218
if( (pTerm->eOperator & WO_OR)!=0
174801175219
&& (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
175220
+ && (!ExprHasProperty(pTerm->pExpr, EP_OuterON)
175221
+ || pTerm->pExpr->w.iJoin==iCur)
174802175222
){
174803175223
WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
174804175224
WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
174805175225
WhereTerm *pOrTerm;
174806175226
int once = 1;
@@ -178662,15 +179082,20 @@
178662179082
assert( p!=0 );
178663179083
assert( p->pWin!=0 );
178664179084
178665179085
/* If this function is being called from within a scalar sub-select
178666179086
** that used by the SELECT statement being processed, only process
178667
- ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
178668
- ** not process aggregates or window functions at all, as they belong
178669
- ** to the scalar sub-select. */
179087
+ ** TK_COLUMN and TK_AGGREGATE expressions that refer to it (the outer
179088
+ ** SELECT). Do not process window functions at all, as they belong to the
179089
+ ** scalar sub-select. */
178670179090
if( p->pSubSelect ){
178671
- if( pExpr->op!=TK_COLUMN ){
179091
+ if( pExpr->op==TK_AGG_FUNCTION ){
179092
+ /* If op2==walkerDepth, then this expression aggregates over the rows
179093
+ ** of the SELECT being processed, even though it is within a scalar
179094
+ ** sub-select. In that case proceed, otherwise skip this node. */
179095
+ if( pExpr->op2!=pWalker->walkerDepth ) return WRC_Continue;
179096
+ }else if( pExpr->op!=TK_COLUMN ){
178672179097
return WRC_Continue;
178673179098
}else{
178674179099
int nSrc = p->pSrc->nSrc;
178675179100
int i;
178676179101
for(i=0; i<nSrc; i++){
@@ -178747,11 +179172,13 @@
178747179172
Select *pSave = p->pSubSelect;
178748179173
if( pSave==pSelect ){
178749179174
return WRC_Continue;
178750179175
}else{
178751179176
p->pSubSelect = pSelect;
179177
+ pWalker->walkerDepth++;
178752179178
sqlite3WalkSelect(pWalker, pSelect);
179179
+ pWalker->walkerDepth--;
178753179180
p->pSubSelect = pSave;
178754179181
}
178755179182
return WRC_Prune;
178756179183
}
178757179184
@@ -178851,12 +179278,19 @@
178851179278
pExpr->op2++;
178852179279
}
178853179280
return WRC_Continue;
178854179281
}
178855179282
179283
+/*
179284
+** Walker callback used to detect aggregate functions in the ORDER BY
179285
+** clause of an otherwise non-aggregate SELECT.
179286
+*/
178856179287
static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
178857
- if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
179288
+ if( pExpr->op==TK_AGG_FUNCTION
179289
+ && pExpr->pAggInfo==0
179290
+ && pExpr->op2==pWalker->walkerDepth
179291
+ ){
178858179292
assert( !ExprHasProperty(pExpr, EP_IntValue) );
178859179293
sqlite3ErrorMsg(pWalker->pParse,
178860179294
"misuse of aggregate: %s()", pExpr->u.zToken);
178861179295
}
178862179296
return WRC_Continue;
@@ -178899,11 +179333,13 @@
178899179333
}
178900179334
sqlite3AggInfoPersistWalkerInit(&w, pParse);
178901179335
sqlite3WalkSelect(&w, p);
178902179336
if( (p->selFlags & SF_Aggregate)==0 ){
178903179337
w.xExprCallback = disallowAggregatesInOrderByCb;
178904
- w.xSelectCallback = 0;
179338
+ w.xSelectCallback = sqlite3WalkerDepthIncrease;
179339
+ w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
179340
+ w.walkerDepth = 0;
178905179341
sqlite3WalkExprList(&w, p->pOrderBy);
178906179342
}
178907179343
178908179344
p->pSrc = 0;
178909179345
p->pWhere = 0;
@@ -181273,11 +181709,13 @@
181273181709
case TK_INTEGER:
181274181710
case TK_STRING:
181275181711
case TK_FLOAT:
181276181712
case TK_BLOB:
181277181713
sqlite3ExprDeferredDelete(pParse, pLeft);
181278
- return sqlite3ExprInt32(pParse->db, op==TK_NOTNULL);
181714
+ p = sqlite3ExprInt32(pParse->db, op==TK_NOTNULL);
181715
+ if( p ) p->op2 = 1;
181716
+ return p;
181279181717
default:
181280181718
break;
181281181719
}
181282181720
return sqlite3PExpr(pParse, op, pLeft, 0);
181283181721
}
@@ -191632,10 +192070,11 @@
191632192070
SQLITE_MAX_TRIGGER_DEPTH,
191633192071
SQLITE_MAX_WORKER_THREADS,
191634192072
SQLITE_MAX_PARSER_DEPTH,
191635192073
SQLITE_MAX_SCHEMA,
191636192074
SQLITE_MAX_TRIGGER_STEPS,
192075
+ SQLITE_MAX_CTES,
191637192076
};
191638192077
191639192078
/*
191640192079
** Make sure the hard limits are set to reasonable values
191641192080
*/
@@ -191719,11 +192158,12 @@
191719192158
assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
191720192159
assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
191721192160
assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
191722192161
assert( aHardLimit[SQLITE_LIMIT_SCHEMA]==SQLITE_MAX_SCHEMA );
191723192162
assert( aHardLimit[SQLITE_LIMIT_TRIGGER_STEPS]==SQLITE_MAX_TRIGGER_STEPS );
191724
- assert( SQLITE_LIMIT_TRIGGER_STEPS==(SQLITE_N_LIMIT-1) );
192163
+ assert( aHardLimit[SQLITE_LIMIT_CTES]==SQLITE_MAX_CTES );
192164
+ assert( SQLITE_LIMIT_CTES==(SQLITE_N_LIMIT-1) );
191725192165
191726192166
191727192167
if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
191728192168
return -1;
191729192169
}
@@ -192834,13 +193274,18 @@
192834193274
int rc;
192835193275
pVfs = sqlite3_vfs_find(0);
192836193276
if( pVfs==0 ) return 0;
192837193277
192838193278
/* This function works in milliseconds, but the underlying OsSleep()
192839
- ** API uses microseconds. Hence the 1000's.
193279
+ ** API uses microseconds. Hence the range limitation and the "*1000".
192840193280
*/
192841
- rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000);
193281
+ if( ms<0 ){
193282
+ ms = 0;
193283
+ }else if( ms>2147483 ){
193284
+ ms = 2147483; /* Maximum sleep time of 35 minutes and 47.484 seconds */
193285
+ }
193286
+ rc = sqlite3OsSleep(pVfs, ms*1000)/1000;
192842193287
return rc;
192843193288
}
192844193289
192845193290
/*
192846193291
** Enable or disable the extended result codes.
@@ -217559,11 +218004,16 @@
217559218004
for(iIn=iOut=0; iIn<sz; iIn++){
217560218005
char c = z[iIn];
217561218006
if( c=='\\' ){
217562218007
u32 v;
217563218008
u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
217564
- if( v<=0x7f ){
218009
+ if( v==0 && iIn+szEscape==sz
218010
+ && (z[iIn+1]=='\n' || z[iIn+1]=='\r' || (u8)z[iIn+1]==0xe2)
218011
+ ){
218012
+ /* A JSON5 line continuation at the end of the string
218013
+ ** contributes no characters */
218014
+ }else if( v<=0x7f ){
217565218015
zOut[iOut++] = (char)v;
217566218016
}else if( v<=0x7ff ){
217567218017
assert( szEscape>=2 );
217568218018
zOut[iOut++] = (char)(0xc0 | (v>>6));
217569218019
zOut[iOut++] = 0x80 | (v&0x3f);
@@ -221778,10 +222228,14 @@
221778222228
221779222229
if( rc ) return rc;
221780222230
if( NEVER(p==0) ) return SQLITE_OK;
221781222231
if( p->iCell>=NCELL(pNode) ) return SQLITE_ABORT;
221782222232
if( i==0 ){
222233
+ /* If this is an UPDATE that does not assign a value to the first
222234
+ ** column, leave it as a "no-change" value. This allows rtreeUpdate()
222235
+ ** to take the new rowid from aData[1] instead. */
222236
+ if( sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
221783222237
sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
221784222238
}else if( i<=pRtree->nDim2 ){
221785222239
nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
221786222240
#ifndef SQLITE_RTREE_INT_ONLY
221787222241
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
@@ -221875,10 +222329,24 @@
221875222329
pCons->pInfo = pInfo;
221876222330
return SQLITE_OK;
221877222331
}
221878222332
221879222333
SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
222334
+
222335
+/*
222336
+** Argument pVal is the right-hand side of a "rowid = ?" constraint. If
222337
+** there is an integer that compares equal to pVal, set *piRowid to it and
222338
+** return 1. Otherwise - pVal is NULL, a blob, a non-numeric text value or
222339
+** a real value that is not an integer - return 0.
222340
+*/
222341
+static int rtreeRowidEqValue(sqlite3_value *pVal, i64 *piRowid){
222342
+ int eType = sqlite3_value_numeric_type(pVal);
222343
+ *piRowid = sqlite3_value_int64(pVal);
222344
+ return eType==SQLITE_INTEGER
222345
+ || (eType==SQLITE_FLOAT
222346
+ && 0==sqlite3IntFloatCompare(*piRowid, sqlite3_value_double(pVal)));
222347
+}
221880222348
221881222349
/*
221882222350
** Rtree virtual table module xFilter method.
221883222351
*/
221884222352
static int rtreeFilter(
@@ -221901,17 +222369,13 @@
221901222369
pCsr->iStrategy = idxNum;
221902222370
if( idxNum==1 ){
221903222371
/* Special case - lookup by rowid. */
221904222372
RtreeNode *pLeaf; /* Leaf on which the required cell resides */
221905222373
RtreeSearchPoint *p; /* Search point for the leaf */
221906
- i64 iRowid = sqlite3_value_int64(argv[0]);
222374
+ i64 iRowid;
221907222375
i64 iNode = 0;
221908
- int eType = sqlite3_value_numeric_type(argv[0]);
221909
- if( eType==SQLITE_INTEGER
221910
- || (eType==SQLITE_FLOAT
221911
- && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0])))
221912
- ){
222376
+ if( rtreeRowidEqValue(argv[0], &iRowid) ){
221913222377
rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
221914222378
}else{
221915222379
rc = SQLITE_OK;
221916222380
pLeaf = 0;
221917222381
}
@@ -223164,10 +223628,11 @@
223164223628
** conflict-handling mode specified by the user.
223165223629
*/
223166223630
if( nData>1 ){
223167223631
int ii;
223168223632
int nn = nData - 4;
223633
+ sqlite3_value *pNewRowid; /* Value of the new rowid, or NULL */
223169223634
223170223635
if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
223171223636
/* Populate the cell.aCoord[] array. The first coordinate is aData[3].
223172223637
**
223173223638
** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
@@ -223197,15 +223662,29 @@
223197223662
rc = rtreeConstraintError(pRtree, ii+1);
223198223663
goto constraint;
223199223664
}
223200223665
}
223201223666
}
223667
+
223668
+ /* The new rowid is normally the value of the first column, aData[2].
223669
+ ** But SQLite does not treat that column as an alias for the rowid,
223670
+ ** so a new rowid assigned using the name "rowid", "oid" or "_rowid_"
223671
+ ** is passed in aData[1]. Use aData[1] if aData[2] is a "no-change"
223672
+ ** value (an UPDATE that does not assign the first column - see
223673
+ ** rtreeColumn()), or if aData[2] is NULL in an INSERT. */
223674
+ pNewRowid = aData[2];
223675
+ if( sqlite3_value_nochange(pNewRowid)
223676
+ || (sqlite3_value_type(aData[0])==SQLITE_NULL
223677
+ && sqlite3_value_type(pNewRowid)==SQLITE_NULL)
223678
+ ){
223679
+ pNewRowid = aData[1];
223680
+ }
223202223681
223203223682
/* If a rowid value was supplied, check if it is already present in
223204223683
** the table. If so, the constraint has failed. */
223205
- if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
223206
- cell.iRowid = sqlite3_value_int64(aData[2]);
223684
+ if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL ){
223685
+ cell.iRowid = sqlite3_value_int64(pNewRowid);
223207223686
if( sqlite3_value_type(aData[0])==SQLITE_NULL
223208223687
|| sqlite3_value_int64(aData[0])!=cell.iRowid
223209223688
){
223210223689
int steprc;
223211223690
sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
@@ -224621,10 +225100,12 @@
224621225100
return pOut;
224622225101
}else{
224623225102
s.nErr++;
224624225103
rc = SQLITE_ERROR;
224625225104
}
225105
+ }else{
225106
+ rc = SQLITE_ERROR;
224626225107
}
224627225108
parse_json_err:
224628225109
if( pRc ) *pRc = rc;
224629225110
sqlite3_free(s.a);
224630225111
return 0;
@@ -224671,13 +225152,15 @@
224671225152
geopolySwab32((unsigned char*)&GeoX(p,ii));
224672225153
geopolySwab32((unsigned char*)&GeoY(p,ii));
224673225154
}
224674225155
p->hdr[0] ^= 1;
224675225156
}
225157
+ if( pRc ) *pRc = SQLITE_OK;
224676225158
}
225159
+ }else{
225160
+ if( pRc ) *pRc = SQLITE_ERROR;
224677225161
}
224678
- if( pRc ) *pRc = SQLITE_OK;
224679225162
return p;
224680225163
}else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
224681225164
const unsigned char *zJson = sqlite3_value_text(pVal);
224682225165
if( zJson==0 ){
224683225166
if( pRc ) *pRc = SQLITE_NOMEM;
@@ -225728,13 +226211,17 @@
225728226211
pCsr->iStrategy = idxNum;
225729226212
if( idxNum==1 ){
225730226213
/* Special case - lookup by rowid. */
225731226214
RtreeNode *pLeaf; /* Leaf on which the required cell resides */
225732226215
RtreeSearchPoint *p; /* Search point for the leaf */
225733
- i64 iRowid = sqlite3_value_int64(argv[0]);
226216
+ i64 iRowid;
225734226217
i64 iNode = 0;
225735
- rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
226218
+ if( rtreeRowidEqValue(argv[0], &iRowid) ){
226219
+ rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
226220
+ }else{
226221
+ pLeaf = 0;
226222
+ }
225736226223
if( rc==SQLITE_OK && pLeaf!=0 ){
225737226224
p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
225738226225
assert( p!=0 ); /* Always returns pCsr->sPoint */
225739226226
pCsr->aNode[0] = pLeaf;
225740226227
p->id = iNode;
@@ -225960,10 +226447,12 @@
225960226447
i64 oldRowid; /* The old rowid */
225961226448
int oldRowidValid; /* True if oldRowid is valid */
225962226449
i64 newRowid; /* The new rowid */
225963226450
int newRowidValid; /* True if newRowid is valid */
225964226451
int coordChange = 0; /* Change in coordinates */
226452
+ sqlite3_value *pShape = 0; /* New _shape value */
226453
+ sqlite3_value *pOldShape = 0; /* Copy of the unchanged _shape */
225965226454
225966226455
if( pRtree->nNodeRef ){
225967226456
/* Unable to write to the btree while another cursor is reading from it,
225968226457
** since the write might do a rebalance which would disrupt the read
225969226458
** cursor. */
@@ -225975,18 +226464,43 @@
225975226464
oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
225976226465
oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
225977226466
newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
225978226467
newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
225979226468
cell.iRowid = newRowid;
226469
+
226470
+ if( nData>1 ){
226471
+ pShape = aData[2];
226472
+ if( oldRowidValid && oldRowid!=newRowid && sqlite3_value_nochange(pShape) ){
226473
+ /* The rowid changes but _shape does not. aData[2] holds no value in
226474
+ ** this case, so read the current _shape from the %_rowid table. It
226475
+ ** is needed for the bounding box of the new r-tree entry and for the
226476
+ ** %_rowid record of the new rowid. */
226477
+ sqlite3_stmt *pRead = 0;
226478
+ rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
226479
+ &pRead, 0);
226480
+ if( rc==SQLITE_OK ){
226481
+ int rc2;
226482
+ sqlite3_bind_int64(pRead, 1, oldRowid);
226483
+ if( sqlite3_step(pRead)==SQLITE_ROW ){
226484
+ pOldShape = sqlite3_value_dup(sqlite3_column_value(pRead, 2));
226485
+ if( pOldShape==0 ) rc = SQLITE_NOMEM;
226486
+ pShape = pOldShape;
226487
+ }
226488
+ rc2 = sqlite3_finalize(pRead);
226489
+ if( rc==SQLITE_OK ) rc = rc2;
226490
+ }
226491
+ if( rc ) goto geopoly_update_end;
226492
+ }
226493
+ }
225980226494
225981226495
if( nData>1 /* not a DELETE */
225982226496
&& (!oldRowidValid /* INSERT */
225983
- || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
226497
+ || !sqlite3_value_nochange(pShape) /* UPDATE _shape */
225984226498
|| oldRowid!=newRowid) /* Rowid change */
225985226499
){
225986
- assert( aData[2]!=0 );
225987
- geopolyBBox(0, aData[2], cell.aCoord, &rc);
226500
+ assert( pShape!=0 );
226501
+ geopolyBBox(0, pShape, cell.aCoord, &rc);
225988226502
if( rc ){
225989226503
if( rc==SQLITE_ERROR ){
225990226504
pVtab->zErrMsg =
225991226505
sqlite3_mprintf("_shape does not contain a valid polygon");
225992226506
}
@@ -226048,21 +226562,21 @@
226048226562
sqlite3_stmt *pUp = pRtree->pWriteAux;
226049226563
int jj;
226050226564
int nChange = 0;
226051226565
sqlite3_bind_int64(pUp, 1, cell.iRowid);
226052226566
assert( pRtree->nAux>=1 );
226053
- if( sqlite3_value_nochange(aData[2]) ){
226567
+ if( sqlite3_value_nochange(pShape) ){
226054226568
sqlite3_bind_null(pUp, 2);
226055226569
}else{
226056226570
GeoPoly *p = 0;
226057
- if( sqlite3_value_type(aData[2])==SQLITE_TEXT
226058
- && (p = geopolyFuncParam(0, aData[2], &rc))!=0
226571
+ if( sqlite3_value_type(pShape)==SQLITE_TEXT
226572
+ && (p = geopolyFuncParam(0, pShape, &rc))!=0
226059226573
&& rc==SQLITE_OK
226060226574
){
226061226575
sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
226062226576
}else{
226063
- sqlite3_bind_value(pUp, 2, aData[2]);
226577
+ sqlite3_bind_value(pUp, 2, pShape);
226064226578
}
226065226579
sqlite3_free(p);
226066226580
nChange = 1;
226067226581
}
226068226582
for(jj=1; jj<nData-2; jj++){
@@ -226074,10 +226588,11 @@
226074226588
rc = sqlite3_reset(pUp);
226075226589
}
226076226590
}
226077226591
226078226592
geopoly_update_end:
226593
+ sqlite3_value_free(pOldShape);
226079226594
rtreeRelease(pRtree);
226080226595
return rc;
226081226596
}
226082226597
226083226598
/*
@@ -230763,11 +231278,11 @@
230763231278
int bOpen = 0;
230764231279
int rc;
230765231280
p->nRbu = 0;
230766231281
p->pRbuFd = 0;
230767231282
rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
230768
- if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
231283
+ if( rc!=SQLITE_NOTFOUND || p->pRbuFd==0 ) p->rc = rc;
230769231284
if( p->eStage>=RBU_STAGE_MOVE ){
230770231285
bOpen = 1;
230771231286
}else{
230772231287
RbuState *pState = rbuLoadState(p);
230773231288
if( pState ){
@@ -235257,11 +235772,11 @@
235257235772
for(i=0; i<nData; i++){
235258235773
sz += ((struct iovec*)aData)[i].iov_len;
235259235774
}
235260235775
}
235261235776
235262
- pNew->aData = sqlite3_malloc64( sz );
235777
+ pNew->aData = sqlite3_malloc64( sz>0 ? sz : 1 );
235263235778
if( pNew->aData==0 ){
235264235779
rc = SQLITE_NOMEM;
235265235780
goto carray_bind_error;
235266235781
}
235267235782
@@ -235288,11 +235803,11 @@
235288235803
p[i].iov_len = n;
235289235804
p[i].iov_base = z;
235290235805
z += n;
235291235806
memcpy(p[i].iov_base, ((struct iovec*)aData)[i].iov_base, n);
235292235807
}
235293
- }else{
235808
+ }else if( sz>0 ){
235294235809
memcpy(pNew->aData, aData, sz);
235295235810
}
235296235811
pNew->xDel = sqlite3_free;
235297235812
pNew->pDel = pNew->aData;
235298235813
}else{
@@ -241551,11 +242066,11 @@
241551242066
}
241552242067
pNew->nRecord = pOut - pNew->aRecord;
241553242068
}
241554242069
}else if( bRebase ){
241555242070
if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
241556
- *ppNew = pExist;
242071
+ pNew = pExist;
241557242072
}else{
241558242073
sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
241559242074
pNew = (SessionChange*)sqlite3_malloc64(nByte);
241560242075
if( pNew==0 ){
241561242076
rc = SQLITE_NOMEM;
@@ -242404,12 +242919,12 @@
242404242919
}
242405242920
if( bData ){
242406242921
a2 = aChange;
242407242922
for(i=0; i<pIter->nCol; i++){
242408242923
int n1 = sessionSerialLen(a1);
242409
- int n2 = sessionSerialLen(a2);
242410
- if( pIter->abPK[i] || a2[0]!=0xFF ){
242924
+ int n2 = (a2>=a2Eof) ? 0 : sessionSerialLen(a2);
242925
+ if( pIter->abPK[i] || n2<=0 || a2[0]!=0xFF ){
242411242926
memcpy(pOut, a1, n1);
242412242927
pOut += n1;
242413242928
}else{
242414242929
*pOut++ = '\0';
242415242930
}
@@ -242463,10 +242978,13 @@
242463242978
bNew = 0;
242464242979
242465242980
/* A patchset may not be rebased */
242466242981
if( pIter->bPatchset ){
242467242982
rc = SQLITE_ERROR;
242983
+ }
242984
+ if( pTab && pTab->nCol>pIter->nCol ){
242985
+ rc = SQLITE_SCHEMA;
242468242986
}
242469242987
242470242988
/* Append a table header to the output for this new table */
242471242989
sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
242472242990
sessionAppendVarint(&sOut, pIter->nCol, &rc);
@@ -242560,10 +243078,11 @@
242560243078
*pnOut = sOut.nBuf;
242561243079
sOut.aBuf = 0;
242562243080
}
242563243081
}
242564243082
sqlite3_free(sOut.aBuf);
243083
+ if( rc==SQLITE_OK ) rc = pIter->rc;
242565243084
return rc;
242566243085
}
242567243086
242568243087
/*
242569243088
** Create a new rebaser object.
@@ -247651,10 +248170,13 @@
247651248170
p++;
247652248171
while( p ){
247653248172
if( *p=='\'' ){
247654248173
p++;
247655248174
if( *p!='\'' ) break;
248175
+ }else if( *p==0 ){
248176
+ p = 0;
248177
+ break;
247656248178
}
247657248179
p++;
247658248180
if( *p==0 ) p = 0;
247659248181
}
247660248182
break;
@@ -249075,11 +249597,11 @@
249075249597
if( zText[jj]=='"' ) zExpr[iOut++] = '"';
249076249598
}
249077249599
zExpr[iOut++] = '"';
249078249600
zExpr[iOut++] = ' ';
249079249601
}
249080
- if( zText[i]==aSpec[2] ){
249602
+ if( i<nText && zText[i]==aSpec[2] ){
249081249603
i += 2;
249082249604
if( zText[i-1]=='^' ) i++;
249083249605
while( i<nText && zText[i]!=']' ) i++;
249084249606
}
249085249607
iFirst = i+1;
@@ -249614,10 +250136,17 @@
249614250136
249615250137
*pRc = rc;
249616250138
if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
249617250139
return 1;
249618250140
}
250141
+
250142
+ /* Not a match. Clear the position lists of all phrases, as some may
250143
+ ** have been populated before the loop above exited early, and others
250144
+ ** may still contain data left over from a previous row. */
250145
+ for(i=0; i<pNear->nPhrase; i++){
250146
+ pNear->apPhrase[i]->poslist.n = 0;
250147
+ }
249619250148
return 0;
249620250149
}
249621250150
}
249622250151
249623250152
@@ -249729,10 +250258,28 @@
249729250258
for(i=0; i<pNode->nChild; i++){
249730250259
fts5ExprNodeZeroPoslist(pNode->apChild[i]);
249731250260
}
249732250261
}
249733250262
}
250263
+
250264
+/*
250265
+** Zero the position lists of all phrases within the sub-tree headed by
250266
+** pNode that are currently positioned at rowid iRowid. Phrases positioned
250267
+** at other rowids are not modified - their position lists belong to rows
250268
+** that have not yet been tested by the parent node.
250269
+*/
250270
+static void fts5ExprNodeZeroPoslistAt(Fts5ExprNode *pNode, i64 iRowid){
250271
+ if( pNode->iRowid!=iRowid ) return;
250272
+ if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
250273
+ fts5ExprNodeZeroPoslist(pNode);
250274
+ }else{
250275
+ int i;
250276
+ for(i=0; i<pNode->nChild; i++){
250277
+ fts5ExprNodeZeroPoslistAt(pNode->apChild[i], iRowid);
250278
+ }
250279
+ }
250280
+}
249734250281
249735250282
249736250283
249737250284
/*
249738250285
** Compare the values currently indicated by the two nodes as follows:
@@ -250048,14 +250595,14 @@
250048250595
pAnd->bNomatch = 1;
250049250596
}
250050250597
}
250051250598
}while( bMatch==0 );
250052250599
250600
+ pAnd->iRowid = iLast;
250053250601
if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
250054
- fts5ExprNodeZeroPoslist(pAnd);
250602
+ fts5ExprNodeZeroPoslistAt(pAnd, iLast);
250055250603
}
250056
- pAnd->iRowid = iLast;
250057250604
return SQLITE_OK;
250058250605
}
250059250606
250060250607
static int fts5ExprNodeNext_AND(
250061250608
Fts5Expr *pExpr,
@@ -250197,10 +250744,11 @@
250197250744
break;
250198250745
250199250746
default:
250200250747
assert( pNode->eType==FTS5_NOT );
250201250748
pNode->bEof = pNode->apChild[0]->bEof;
250749
+ if( pNode->bEof ) fts5ExprNodeZeroPoslist(pNode->apChild[1]);
250202250750
break;
250203250751
}
250204250752
}
250205250753
250206250754
if( rc==SQLITE_OK ){
@@ -253453,12 +254001,11 @@
253453254001
}
253454254002
if( rc!=SQLITE_OK ){
253455254003
sqlite3_free(pRet);
253456254004
pRet = 0;
253457254005
}else{
253458
- pRet->p[nByte] = 0x00;
253459
- pRet->p[nByte+1] = 0x00;
254006
+ memset(&pRet->p[nByte], 0, FTS5_DATA_PADDING);
253460254007
}
253461254008
}
253462254009
p->rc = rc;
253463254010
p->nRead++;
253464254011
}
@@ -255308,11 +255855,11 @@
255308255855
pIter->flags |= FTS5_SEGITER_ONETERM;
255309255856
if( pIter->pLeaf ){
255310255857
if( flags & FTS5INDEX_QUERY_DESC ){
255311255858
pIter->flags |= FTS5_SEGITER_REVERSE;
255312255859
}
255313
- if( bDlidx ){
255860
+ if( bDlidx && iPg==pIter->iTermLeafPgno ){
255314255861
fts5SegIterLoadDlidx(p, pIter);
255315255862
}
255316255863
if( flags & FTS5INDEX_QUERY_DESC ){
255317255864
fts5SegIterReverse(p, pIter);
255318255865
}
@@ -255707,10 +256254,17 @@
255707256254
}else{
255708256255
iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
255709256256
pIter->iLeafOffset = iOff;
255710256257
fts5SegIterLoadNPos(p, pIter);
255711256258
}
256259
+ break;
256260
+ }else if( !fts5LeafIsTermless(pIter->pLeaf) ){
256261
+ /* There are no rowids on this page, but there is another term.
256262
+ ** Since there are no more rowids for the current term, set the
256263
+ ** iterator to EOF. */
256264
+ fts5DataRelease(pIter->pLeaf);
256265
+ pIter->pLeaf = 0;
255712256266
break;
255713256267
}
255714256268
}
255715256269
}
255716256270
}
@@ -259800,15 +260354,13 @@
259800260354
for(ii=0; ii<pT->nIter; ii++){
259801260355
Fts5Iter *p = pT->apIter[ii];
259802260356
if( p->base.bEof==0
259803260357
&& (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
259804260358
){
259805
- fts5MultiIterNext(pIndex, p, bFrom, iFrom);
259806
- while( bFrom && p->base.bEof==0
259807
- && p->base.iRowid<iFrom
259808
- && pIndex->rc==SQLITE_OK
259809
- ){
260359
+ if( bFrom ){
260360
+ fts5MultiIterNextFrom(pIndex, p, iFrom);
260361
+ }else{
259810260362
fts5MultiIterNext(pIndex, p, 0, 0);
259811260363
}
259812260364
}
259813260365
}
259814260366
@@ -260000,10 +260552,19 @@
260000260552
** queries performed as part of an integrity-check, or by the fts5vocab
260001260553
** module. */
260002260554
if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){
260003260555
bTokendata = 0;
260004260556
}
260557
+
260558
+ /* fts5SetupTokendataIter() flushes the in-memory hash table to disk.
260559
+ ** The flush frees the cached Fts5Structure object, which iterators
260560
+ ** opened earlier by this function point into without holding a
260561
+ ** reference (for example one opened for a prefix index query in the
260562
+ ** same MATCH expression). So, on a tokendata=1 table, flush first. */
260563
+ if( pConfig->bTokendata ){
260564
+ fts5IndexFlush(p);
260565
+ }
260005260566
260006260567
/* Figure out which index to search and set iIdx accordingly. If this
260007260568
** is a prefix query for which there is no prefix index, set iIdx to
260008260569
** greater than pConfig->nPrefix to indicate that the query will be
260009260570
** satisfied by scanning multiple terms in the main index.
@@ -262818,11 +263379,13 @@
262818263379
}
262819263380
pInfo->aConstraintUsage[i].argvIndex = ++iCons;
262820263381
pInfo->aConstraintUsage[i].omit = 1;
262821263382
}
262822263383
}else if( p->usable ){
262823
- if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
263384
+ if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p)
263385
+ && pConfig->abUnindexed[iCol]==0
263386
+ ){
262824263387
assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB );
262825263388
idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G';
262826263389
sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
262827263390
idxStr += strlen(&idxStr[iIdxStr]);
262828263391
pInfo->aConstraintUsage[i].argvIndex = ++iCons;
@@ -262830,11 +263393,10 @@
262830263393
nSeenMatch++;
262831263394
}else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){
262832263395
idxStr[iIdxStr++] = '=';
262833263396
bSeenEq = 1;
262834263397
pInfo->aConstraintUsage[i].argvIndex = ++iCons;
262835
- pInfo->aConstraintUsage[i].omit = 1;
262836263398
}
262837263399
}
262838263400
}
262839263401
262840263402
if( bSeenEq==0 ){
@@ -264481,19 +265043,21 @@
264481265043
if( aPopulator==0 ) rc = SQLITE_NOMEM;
264482265044
if( rc==SQLITE_OK ){
264483265045
rc = fts5SeekCursor(pCsr, 0);
264484265046
}
264485265047
for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
264486
- const char *z = 0;
264487
- int n = 0;
264488
- rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
264489
- if( rc==SQLITE_OK ){
264490
- rc = sqlite3Fts5ExprPopulatePoslists(
264491
- pConfig, pCsr->pExpr, aPopulator, i, z, n
264492
- );
264493
- }
264494
- sqlite3Fts5ClearLocale(pConfig);
265048
+ if( pConfig->abUnindexed[i]==0 ){
265049
+ const char *z = 0;
265050
+ int n = 0;
265051
+ rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
265052
+ if( rc==SQLITE_OK ){
265053
+ rc = sqlite3Fts5ExprPopulatePoslists(
265054
+ pConfig, pCsr->pExpr, aPopulator, i, z, n
265055
+ );
265056
+ }
265057
+ sqlite3Fts5ClearLocale(pConfig);
265058
+ }
264495265059
}
264496265060
sqlite3_free(aPopulator);
264497265061
264498265062
if( pCsr->pSorter ){
264499265063
sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
@@ -265771,11 +266335,11 @@
265771266335
int nArg, /* Number of args */
265772266336
sqlite3_value **apUnused /* Function arguments */
265773266337
){
265774266338
assert( nArg==0 );
265775266339
UNUSED_PARAM2(nArg, apUnused);
265776
- sqlite3_result_text(pCtx, "fts5: 2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918", -1, SQLITE_TRANSIENT);
266340
+ sqlite3_result_text(pCtx, "fts5: 2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b", -1, SQLITE_TRANSIENT);
265777266341
}
265778266342
265779266343
/*
265780266344
** Implementation of fts5_locale(LOCALE, TEXT) function.
265781266345
**
@@ -266131,13 +266695,15 @@
266131266695
*/
266132266696
static int fts5StorageGetStmt(
266133266697
Fts5Storage *p, /* Storage handle */
266134266698
int eStmt, /* FTS5_STMT_XXX constant */
266135266699
sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
266136
- char **pzErrMsg /* OUT: Error message (if any) */
266700
+ char **pzErrMsg /* OUT: Error message (if any) (not used) */
266137266701
){
266138266702
int rc = SQLITE_OK;
266703
+
266704
+ (void)pzErrMsg;
266139266705
266140266706
/* If there is no %_docsize table, there should be no requests for
266141266707
** statements to operate on it. */
266142266708
assert( p->pConfig->bColumnsize || (
266143266709
eStmt!=FTS5_STMT_REPLACE_DOCSIZE
@@ -266310,15 +266876,19 @@
266310266876
rc = fts5ExecPrintf(pConfig->db, 0,
266311266877
"DROP TABLE IF EXISTS %Q.'%q_docsize';",
266312266878
pConfig->zDb, pConfig->zName
266313266879
);
266314266880
}
266315
- if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
266316
- rc = fts5ExecPrintf(pConfig->db, 0,
266317
- "DROP TABLE IF EXISTS %Q.'%q_content';",
266318
- pConfig->zDb, pConfig->zName
266319
- );
266881
+ if( rc==SQLITE_OK ){
266882
+ if( pConfig->eContent==FTS5_CONTENT_NORMAL
266883
+ || pConfig->bContentlessUnindexed
266884
+ ){
266885
+ rc = fts5ExecPrintf(pConfig->db, 0,
266886
+ "DROP TABLE IF EXISTS %Q.'%q_content';",
266887
+ pConfig->zDb, pConfig->zName
266888
+ );
266889
+ }
266320266890
}
266321266891
return rc;
266322266892
}
266323266893
266324266894
static void fts5StorageRenameOne(
@@ -266343,11 +266913,13 @@
266343266913
fts5StorageRenameOne(pConfig, &rc, "idx", zName);
266344266914
fts5StorageRenameOne(pConfig, &rc, "config", zName);
266345266915
if( pConfig->bColumnsize ){
266346266916
fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
266347266917
}
266348
- if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
266918
+ if( pConfig->eContent==FTS5_CONTENT_NORMAL
266919
+ || pConfig->bContentlessUnindexed
266920
+ ){
266349266921
fts5StorageRenameOne(pConfig, &rc, "content", zName);
266350266922
}
266351266923
return rc;
266352266924
}
266353266925
@@ -266423,11 +266995,11 @@
266423266995
for(i=0; i<pConfig->nCol; i++){
266424266996
if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->abUnindexed[i] ){
266425266997
sqlite3_str_appendf(pDefn, ", c%d", i);
266426266998
}
266427266999
}
266428
- if( pConfig->bLocale ){
267000
+ if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL ){
266429267001
for(i=0; i<pConfig->nCol; i++){
266430267002
if( pConfig->abUnindexed[i]==0 ){
266431267003
sqlite3_str_appendf(pDefn, ", l%d", i);
266432267004
}
266433267005
}
@@ -266764,11 +267336,11 @@
266764267336
p->aTotalSize[ii] -= aCol[ii];
266765267337
}
266766267338
}
266767267339
sqlite3_free(aCol);
266768267340
}
266769
- iOrigin = sqlite3_column_int(pLookup, 1);
267341
+ iOrigin = sqlite3_column_int64(pLookup, 1);
266770267342
}
266771267343
rc2 = sqlite3_reset(pLookup);
266772267344
if( rc==SQLITE_OK ) rc = rc2;
266773267345
}
266774267346
@@ -270300,13 +270872,14 @@
270300270872
270301270873
#define FTS5_VOCAB_COL 0
270302270874
#define FTS5_VOCAB_ROW 1
270303270875
#define FTS5_VOCAB_INSTANCE 2
270304270876
270305
-#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
270306
-#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
270307
-#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
270877
+#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt, PRIMARY KEY(term, col)"
270878
+#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt, PRIMARY KEY(term)"
270879
+#define FTS5_VOCAB_INST_SCHEMA \
270880
+ "term, doc, col, offset, PRIMARY KEY(term, doc, col, offset)"
270308270881
270309270882
/*
270310270883
** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
270311270884
*/
270312270885
#define FTS5_VOCAB_TERM_EQ 0x0100
@@ -270394,13 +270967,13 @@
270394270967
const char * const *argv, /* xCreate/xConnect argument array */
270395270968
sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
270396270969
char **pzErr /* Write any error message here */
270397270970
){
270398270971
const char *azSchema[] = {
270399
- "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
270400
- "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
270401
- "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
270972
+ "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ") WITHOUT ROWID",
270973
+ "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ") WITHOUT ROWID",
270974
+ "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ") WITHOUT ROWID"
270402270975
};
270403270976
270404270977
Fts5VocabTable *pRet = 0;
270405270978
int rc = SQLITE_OK; /* Return code */
270406270979
int bDb;
270407270980
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** c75eff906c11d8f17ae198e5f6cfea34636d with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.55.0"
471 #define SQLITE_VERSION_NUMBER 3055000
472 #define SQLITE_SOURCE_ID "2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-10-17T12:10:02.088Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -4768,10 +4768,19 @@
4768 ** defined by the database schema.</dd>)^
4769 **
4770 ** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
4771 ** <dd>The maximum number of SQL statements that can be contained within
4772 ** a single trigger.</dd>)^
 
 
 
 
 
 
 
 
 
4773 ** </dl>
4774 */
4775 #define SQLITE_LIMIT_LENGTH 0
4776 #define SQLITE_LIMIT_SQL_LENGTH 1
4777 #define SQLITE_LIMIT_COLUMN 2
@@ -4785,10 +4794,11 @@
4785 #define SQLITE_LIMIT_TRIGGER_DEPTH 10
4786 #define SQLITE_LIMIT_WORKER_THREADS 11
4787 #define SQLITE_LIMIT_PARSER_DEPTH 12
4788 #define SQLITE_LIMIT_SCHEMA 13
4789 #define SQLITE_LIMIT_TRIGGER_STEPS 14
 
4790
4791 /*
4792 ** CAPI3REF: Prepare Flags
4793 **
4794 ** These constants define various flags that can be passed into the
@@ -6197,10 +6207,14 @@
6197 ** CAPI3REF: Text Encodings
6198 **
6199 ** These constants define integer codes that represent the various
6200 ** text encodings supported by SQLite.
6201 **
 
 
 
 
6202 ** <dl>
6203 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
6204 **
6205 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
6206 ** with each code point being expressed "little endian" - the least significant
@@ -7179,10 +7193,14 @@
7179 ** VFS and operating system. Some system treat a negative argument as an
7180 ** instruction to sleep forever. Others understand it to mean do not sleep
7181 ** at all. ^In SQLite version 3.42.0 and later, a negative
7182 ** argument passed into sqlite3_sleep() is changed to zero before it is relayed
7183 ** down into the xSleep method of the VFS.
 
 
 
 
7184 */
7185 SQLITE_API int sqlite3_sleep(int);
7186
7187 /*
7188 ** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11573,11 +11591,11 @@
11573 ** buffer. If the bit had been clear, the returned buffer will not
11574 ** be accessed by SQLite after the call.
11575 **
11576 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11577 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11578 ** allocation error occurs.
11579 **
11580 ** This interface is omitted if SQLite is compiled with the
11581 ** [SQLITE_OMIT_DESERIALIZE] option.
11582 */
11583 SQLITE_API unsigned char *sqlite3_serialize(
@@ -15089,10 +15107,31 @@
15089 */
15090 #ifndef SQLITE_MAX_TRIGGER_STEPS
15091 # define SQLITE_MAX_TRIGGER_STEPS 65000
15092 #endif
15093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15094 /************** End of sqliteLimit.h *****************************************/
15095 /************** Continuing where we left off in sqliteInt.h ******************/
15096
15097 /* Disable nuisance warnings on Borland compilers */
15098 #if defined(__BORLANDC__)
@@ -18737,11 +18776,11 @@
18737
18738 /*
18739 ** The number of different kinds of things that can be limited
18740 ** using the sqlite3_limit() interface.
18741 */
18742 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_STEPS+1)
18743
18744 /*
18745 ** Lookaside malloc is a set of fixed-size buffers that can be used
18746 ** to satisfy small transient memory allocation requests for objects
18747 ** associated with a particular database connection. The use of
@@ -19006,14 +19045,17 @@
19006 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
19007 #endif
19008 };
19009
19010 /*
19011 ** A macro to discover the encoding of a database.
 
19012 */
19013 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
19014 #define ENC(db) ((db)->enc)
 
 
19015
19016 /*
19017 ** A u64 constant where the lower 32 bits are all zeros. Only the
19018 ** upper 32 bits are included in the argument. Necessary because some
19019 ** C-compilers still do not accept LL integer literals.
@@ -21141,10 +21183,11 @@
21141 #endif
21142 Parse *pToplevel; /* Parse structure for main program (or NULL) */
21143 Table *pTriggerTab; /* Table triggers are being coded for */
21144 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
21145 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
 
21146
21147 /**************************************************************************
21148 ** Fields above must be initialized to zero. The fields that follow,
21149 ** down to the beginning of the recursive section, do not need to be
21150 ** initialized as they will be set before being used. The boundary is
@@ -25222,11 +25265,11 @@
25222 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*);
25223 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
25224 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
25225 SQLITE_PRIVATE int sqlite3MemRealValueRC(Mem*, double*);
25226 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
25227 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
25228 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
25229 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
25230 SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
25231 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
25232 SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
@@ -26108,10 +26151,11 @@
26108 neg = 0;
26109 }
26110 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
26111 return 1;
26112 }
 
26113 zDate += 10;
26114 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
26115 if( parseHhMmSs(zDate, p)==0 ){
26116 if( p->h==24 ){
26117 int mxD = daysOfMonth(Y,M);
@@ -26131,11 +26175,11 @@
26131 }else{
26132 return 1;
26133 }
26134 p->validJD = 0;
26135 p->validYMD = 1;
26136 p->Y = neg ? -Y : Y;
26137 p->M = M;
26138 p->D = D;
26139 computeFloor(p);
26140 if( p->tz ){
26141 computeJD(p);
@@ -30620,10 +30664,16 @@
30620 pTo->xMutexLeave = pFrom->xMutexLeave;
30621 pTo->xMutexHeld = pFrom->xMutexHeld;
30622 pTo->xMutexNotheld = pFrom->xMutexNotheld;
30623 sqlite3MemoryBarrier();
30624 pTo->xMutexAlloc = pFrom->xMutexAlloc;
 
 
 
 
 
 
30625 }
30626 assert( sqlite3GlobalConfig.mutex.xMutexInit );
30627 rc = sqlite3GlobalConfig.mutex.xMutexInit();
30628
30629 #ifdef SQLITE_DEBUG
@@ -38212,11 +38262,13 @@
38212 ** the right of the decimal point, or to a maximum of mxRound total
38213 ** significant digits.
38214 **
38215 ** If iRound>0 round to min(iRound,mxRound) significant digits total.
38216 **
38217 ** mxRound must be positive.
 
 
38218 **
38219 ** The significant digits of the decimal representation are
38220 ** stored in p->z[] which is a often (but not always) a pointer
38221 ** into the middle of p->zBuf[]. There are p->n significant digits.
38222 ** The p->z[] array is *not* zero-terminated.
@@ -38229,10 +38281,11 @@
38229 char *zBuf; /* Local alias for p->zBuf */
38230 char *z; /* Local alias for p->z */
38231
38232 p->isSpecial = 0;
38233 assert( mxRound>0 );
 
38234
38235 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
38236 ** NaN. */
38237 if( r<0.0 ){
38238 p->sign = '-';
@@ -47295,10 +47348,13 @@
47295 */
47296 zName = unixTempFileDir();
47297 if( zName
47298 && (fd = robust_open(zName, O_RDWR|O_EXCL|O_TMPFILE, 0600))>=0
47299 ){
 
 
 
47300 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags|UNIXFILE_NOLOCK);
47301 goto open_finished;
47302 }
47303 #endif
47304 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
@@ -47816,11 +47872,16 @@
47816 if( microseconds>=1000000 ) sleep(microseconds/1000000);
47817 if( microseconds%1000000 ) usleep(microseconds%1000000);
47818 UNUSED_PARAMETER(NotUsed);
47819 return microseconds;
47820 #else
47821 int seconds = (microseconds+999999)/1000000;
 
 
 
 
 
47822 sleep(seconds);
47823 UNUSED_PARAMETER(NotUsed);
47824 return seconds*1000000;
47825 #endif
47826 }
@@ -55359,11 +55420,12 @@
55359 memdbLeave(pStore);
55360 return p;
55361 }
55362
55363 /*
55364 ** Return the serialization of a database
 
55365 */
55366 SQLITE_API unsigned char *sqlite3_serialize(
55367 sqlite3 *db, /* The database connection */
55368 const char *zSchema, /* Which database within the connection */
55369 sqlite3_int64 *piSize, /* Write size here, if not NULL */
@@ -55435,12 +55497,16 @@
55435 DbPage *pPage = 0;
55436 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
55437 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
55438 if( rc==SQLITE_OK ){
55439 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
55440 }else{
55441 memset(pTo, 0, szPage);
 
 
 
 
55442 }
55443 sqlite3PagerUnref(pPage);
55444 }
55445 }
55446 }
@@ -57089,38 +57155,66 @@
57089 ** When reading a corrupt database file, it is possible that SQLite might
57090 ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
57091 ** It will only read past the end of the page buffer, never write. This
57092 ** object is positioned immediately after the page buffer to serve as an
57093 ** overrun area, so that overreads are harmless.
 
 
 
 
 
 
 
 
 
 
 
 
57094 **
57095 ** Variables isBulkLocal and isAnchor were once type "u8". That works,
57096 ** but causes a 2-byte gap in the structure for most architectures (since
57097 ** pointers must be either 4 or 8-byte aligned). As this structure is located
57098 ** in memory directly after the associated page data, if the database is
57099 ** corrupt, code at the b-tree layer may overread the page buffer and
57100 ** read part of this structure before the corruption is detected. This
57101 ** can cause a valgrind error if the uninitialized gap is accessed. Using u16
57102 ** ensures there is no such gap, and therefore no bytes of uninitialized
57103 ** memory in the structure.
 
 
 
57104 **
57105 ** The pLruNext and pLruPrev pointers form a double-linked circular list
57106 ** of all pages that are unpinned. The PGroup.lru element (which should be
57107 ** the only element on the list with PgHdr1.isAnchor set to 1) forms the
57108 ** beginning and the end of the list.
57109 */
57110 struct PgHdr1 {
57111 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
57112 unsigned int iKey; /* Key value (page number) */
57113 u16 isBulkLocal; /* This page from bulk local storage */
57114 u16 isAnchor; /* This is the PGroup.lru element */
 
 
 
57115 PgHdr1 *pNext; /* Next in hash table chain */
57116 PCache1 *pCache; /* Cache that currently owns this page */
57117 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
57118 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
57119 /* NB: pLruPrev is only valid if pLruNext!=0 */
57120 };
57121
 
 
 
 
 
 
 
 
 
 
 
 
57122 /*
57123 ** A page is pinned if it is not on the LRU list. To be "pinned" means
57124 ** that the page is in active use and must not be deallocated.
57125 */
57126 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
@@ -57567,11 +57661,11 @@
57567 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
57568 ** LRU list, then this function is a no-op.
57569 **
57570 ** The PGroup mutex must be held when this function is called.
57571 */
57572 static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
57573 assert( pPage!=0 );
57574 assert( PAGE_IS_UNPINNED(pPage) );
57575 assert( pPage->pLruNext );
57576 assert( pPage->pLruPrev );
57577 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
@@ -57581,11 +57675,11 @@
57581 /* pPage->pLruPrev = 0;
57582 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
57583 assert( pPage->isAnchor==0 );
57584 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
57585 pPage->pCache->nRecyclable--;
57586 return pPage;
57587 }
57588
57589
57590 /*
57591 ** Remove the page supplied as an argument from the hash table
@@ -57864,11 +57958,11 @@
57864 **
57865 ** This steps are broken out into a separate procedure because they are
57866 ** usually not needed, and by avoiding the stack initialization required
57867 ** for these steps, the main pcache1Fetch() procedure can run faster.
57868 */
57869 static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
57870 PCache1 *pCache,
57871 unsigned int iKey,
57872 int createFlag
57873 ){
57874 unsigned int nPinned;
@@ -57930,12 +58024,13 @@
57930 *(void **)pPage->page.pExtra = 0;
57931 pCache->apHash[h] = pPage;
57932 if( iKey>pCache->iMaxKey ){
57933 pCache->iMaxKey = iKey;
57934 }
 
57935 }
57936 return pPage;
57937 }
57938
57939 /*
57940 ** Implementation of the sqlite3_pcache.xFetch method.
57941 **
@@ -57993,11 +58088,11 @@
57993 ** There are two versions of this routine. pcache1FetchWithMutex() is
57994 ** the general case. pcache1FetchNoMutex() is a faster implementation for
57995 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
57996 ** invokes the appropriate routine.
57997 */
57998 static PgHdr1 *pcache1FetchNoMutex(
57999 sqlite3_pcache *p,
58000 unsigned int iKey,
58001 int createFlag
58002 ){
58003 PCache1 *pCache = (PCache1 *)p;
@@ -58016,27 +58111,27 @@
58016 ** subsequent steps to try to create the page. */
58017 if( pPage ){
58018 if( PAGE_IS_UNPINNED(pPage) ){
58019 return pcache1PinPage(pPage);
58020 }else{
58021 return pPage;
58022 }
58023 }else if( createFlag ){
58024 /* Steps 3, 4, and 5 implemented by this subroutine */
58025 return pcache1FetchStage2(pCache, iKey, createFlag);
58026 }else{
58027 return 0;
58028 }
58029 }
58030 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
58031 static PgHdr1 *pcache1FetchWithMutex(
58032 sqlite3_pcache *p,
58033 unsigned int iKey,
58034 int createFlag
58035 ){
58036 PCache1 *pCache = (PCache1 *)p;
58037 PgHdr1 *pPage;
58038
58039 pcache1EnterMutex(pCache->pGroup);
58040 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
58041 assert( pPage==0 || pCache->iMaxKey>=iKey );
58042 pcache1LeaveMutex(pCache->pGroup);
@@ -58050,23 +58145,22 @@
58050 ){
58051 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
58052 PCache1 *pCache = (PCache1 *)p;
58053 #endif
58054
58055 assert( offsetof(PgHdr1,page)==0 );
58056 assert( pCache->bPurgeable || createFlag!=1 );
58057 assert( pCache->bPurgeable || pCache->nMin==0 );
58058 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
58059 assert( pCache->nMin==0 || pCache->bPurgeable );
58060 assert( pCache->nHash>0 );
58061 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
58062 if( pCache->pGroup->mutex ){
58063 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
58064 }else
58065 #endif
58066 {
58067 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
58068 }
58069 }
58070
58071
58072 /*
@@ -58078,11 +58172,11 @@
58078 sqlite3_pcache *p,
58079 sqlite3_pcache_page *pPg,
58080 int reuseUnlikely
58081 ){
58082 PCache1 *pCache = (PCache1 *)p;
58083 PgHdr1 *pPage = (PgHdr1 *)pPg;
58084 PGroup *pGroup = pCache->pGroup;
58085
58086 assert( pPage->pCache==pCache );
58087 pcache1EnterMutex(pGroup);
58088
@@ -58114,20 +58208,20 @@
58114 sqlite3_pcache_page *pPg,
58115 unsigned int iOld,
58116 unsigned int iNew
58117 ){
58118 PCache1 *pCache = (PCache1 *)p;
58119 PgHdr1 *pPage = (PgHdr1 *)pPg;
58120 PgHdr1 **pp;
58121 unsigned int hOld, hNew;
58122 assert( pPage->iKey==iOld );
58123 assert( pPage->pCache==pCache );
58124 assert( iOld!=iNew ); /* The page number really is changing */
58125
58126 pcache1EnterMutex(pCache->pGroup);
58127
58128 assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
58129 assert( pCache->nHash>0 && (pCache->nHash & (pCache->nHash-1))==0 );
58130 hOld = iOld & (pCache->nHash-1);
58131 pp = &pCache->apHash[hOld];
58132 while( (*pp)!=pPage ){
58133 pp = &(*pp)->pNext;
@@ -68381,10 +68475,11 @@
68381 SEH_FREE_ON_ERROR(0, aFrame);
68382 if( !aFrame ){
68383 rc = SQLITE_NOMEM_BKPT;
68384 goto recovery_error;
68385 }
 
68386 aData = &aFrame[WAL_FRAME_HDRSIZE];
68387 aPrivate = (u32*)&aData[szPage];
68388
68389 /* Read all frames from the log file. */
68390 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
@@ -69654,12 +69749,16 @@
69654 ** with a writer. So get a WRITE lock and try again.
69655 */
69656 if( badHdr ){
69657 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
69658 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
 
 
 
 
69659 walUnlockShared(pWal, WAL_WRITE_LOCK);
69660 rc = SQLITE_READONLY_RECOVERY;
69661 }
69662 }else{
69663 int bWriteLock = pWal->writeLock;
69664 if( bWriteLock
69665 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
@@ -70136,29 +70235,39 @@
70136 assert( thisMark!=READMARK_NOT_USED );
70137 mxReadMark = thisMark;
70138 mxI = i;
70139 }
70140 }
70141 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
70142 && (mxReadMark<mxFrame || mxI==0)
70143 ){
70144 for(i=1; i<WAL_NREADER; i++){
70145 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
70146 if( rc==SQLITE_OK ){
70147 AtomicStore(pInfo->aReadMark+i,mxFrame);
70148 mxReadMark = mxFrame;
70149 mxI = i;
70150 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
70151 break;
70152 }else if( rc!=SQLITE_BUSY ){
70153 return rc;
70154 }
70155 }
70156 }
 
 
 
 
 
 
 
 
 
 
 
70157 if( mxI==0 ){
70158 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
70159 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
70160 }
70161
70162 (void)walEnableBlockingMs(pWal, nBlockTmout);
70163 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
70164 walDisableBlocking(pWal);
@@ -74930,11 +75039,12 @@
74930 return SQLITE_OK;
74931 }
74932
74933 /*
74934 ** Do additional sanity check after btreeInitPage() if
74935 ** PRAGMA cell_size_check=ON
 
74936 */
74937 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
74938 int iCellFirst; /* First allowable cell or freeblock offset */
74939 int iCellLast; /* Last possible cell or freeblock offset */
74940 int i; /* Index into the cell pointer array */
@@ -74953,15 +75063,17 @@
74953 for(i=0; i<pPage->nCell; i++){
74954 pc = get2byteAligned(&data[cellOffset+i*2]);
74955 testcase( pc==iCellFirst );
74956 testcase( pc==iCellLast );
74957 if( pc<iCellFirst || pc>iCellLast ){
 
74958 return SQLITE_CORRUPT_PAGE(pPage);
74959 }
74960 sz = pPage->xCellSize(pPage, &data[pc]);
74961 testcase( pc+sz==usableSize );
74962 if( pc+sz>usableSize ){
 
74963 return SQLITE_CORRUPT_PAGE(pPage);
74964 }
74965 }
74966 return SQLITE_OK;
74967 }
@@ -82791,10 +82903,16 @@
82791 pCur->skipNext = -1;
82792 pCur->ix = pPage->nCell-1;
82793 }else{
82794 pCur->skipNext = 1;
82795 }
 
 
 
 
 
 
82796 }else{
82797 rc = moveToRoot(pCur);
82798 if( bPreserve ){
82799 btreeReleaseAllCursorPages(pCur);
82800 pCur->eState = CURSOR_REQUIRESEEK;
@@ -85989,12 +86107,32 @@
85989 }
85990
85991 /*
85992 ** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
85993 ** make it a MEM_Int if we can.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85994 */
85995 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
85996 assert( pMem!=0 );
85997 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
85998 assert( !sqlite3VdbeMemIsRowSet(pMem) );
85999 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
86000 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
@@ -86012,11 +86150,13 @@
86012 **
86013 ** The second and third terms in the following conditional enforces
86014 ** the second condition under the assumption that addition overflow causes
86015 ** values to wrap around.
86016 */
86017 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
 
 
86018 pMem->u.i = ix;
86019 MemSetTypeFlag(pMem, MEM_Int);
86020 }
86021 }
86022 }
@@ -86476,15 +86616,17 @@
86476 void (*xDel)(void*) /* Destructor function */
86477 ){
86478 i64 nByte = n; /* New value for pMem->n */
86479 int iLimit; /* Maximum allowed string or blob size */
86480 u16 flags; /* New value for pMem->flags */
 
86481
86482 assert( pMem!=0 );
86483 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
86484 assert( !sqlite3VdbeMemIsRowSet(pMem) );
86485 assert( enc!=0 || n>=0 );
 
86486
86487 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
86488 if( !z ){
86489 sqlite3VdbeMemSetNull(pMem);
86490 return SQLITE_OK;
@@ -86494,20 +86636,20 @@
86494 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
86495 }else{
86496 iLimit = SQLITE_MAX_LENGTH;
86497 }
86498 if( nByte<0 ){
86499 assert( enc!=0 );
86500 if( enc==SQLITE_UTF8 ){
86501 nByte = strlen(z);
86502 }else{
86503 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
86504 }
86505 flags= MEM_Str|MEM_Term;
86506 }else if( enc==0 ){
86507 flags = MEM_Blob;
86508 enc = SQLITE_UTF8;
86509 }else{
86510 flags = MEM_Str;
86511 }
86512 if( nByte>iLimit ){
86513 if( xDel && xDel!=SQLITE_TRANSIENT ){
@@ -86526,11 +86668,11 @@
86526 ** management (one of MEM_Dyn or MEM_Static).
86527 */
86528 if( xDel==SQLITE_TRANSIENT ){
86529 i64 nAlloc = nByte;
86530 if( flags&MEM_Term ){
86531 nAlloc += (enc==SQLITE_UTF8?1:2);
86532 }
86533 testcase( nAlloc==0 );
86534 testcase( nAlloc==31 );
86535 testcase( nAlloc==32 );
86536 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
@@ -86550,18 +86692,17 @@
86550 }
86551 }
86552
86553 pMem->n = (int)(nByte & 0x7fffffff);
86554 pMem->flags = flags;
86555 pMem->enc = enc;
86556
86557 #ifndef SQLITE_OMIT_UTF16
86558 /* tag-20261016-1: To retain the BOM, use enc==SQLITE_UTF8, even
86559 ** for UTF-16 text, and then set pMem->enc correctly after this
86560 ** function returns. Note, however, that n must be non-negative
86561 ** for this trick to work. */
86562 if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
86563 return SQLITE_NOMEM_BKPT;
86564 }
86565 #endif
86566
86567
@@ -86675,14 +86816,14 @@
86675 return SQLITE_NOMEM_BKPT;
86676 }
86677 if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){
86678 return SQLITE_CORRUPT_BKPT;
86679 }
86680 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
86681 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
86682 if( rc==SQLITE_OK ){
86683 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
86684 pMem->flags = MEM_Blob;
86685 pMem->n = (int)amt;
86686 }else{
86687 sqlite3VdbeMemRelease(pMem);
86688 }
@@ -87067,18 +87208,19 @@
87067 sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
87068 }else{
87069 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
87070 if( zVal==0 ) goto no_mem;
87071 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
 
 
 
 
 
87072 }
87073 }
87074 if( affinity==SQLITE_AFF_BLOB ){
87075 if( op==TK_FLOAT ){
87076 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
87077 sqlite3AtoF(pVal->z, &pVal->u.r);
87078 pVal->flags = MEM_Real;
87079 }else if( op==TK_INTEGER ){
87080 /* This case is required by -9223372036854775808 and other strings
87081 ** that look like integers but cannot be handled by the
87082 ** sqlite3DecOrHexToI64() call above. */
87083 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
87084 }
@@ -93949,10 +94091,11 @@
93949 sqlite3_result_error_toobig(pCtx);
93950 return SQLITE_TOOBIG;
93951 }
93952 #ifndef SQLITE_OMIT_INCRBLOB
93953 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
 
93954 return SQLITE_OK;
93955 #else
93956 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
93957 #endif
93958 }
@@ -95041,16 +95184,17 @@
95041 sqlite3_stmt *pStmt, /* The statement to bind against */
95042 int i, /* Index of the parameter to bind */
95043 const void *zData, /* Pointer to the data to be bound */
95044 i64 nData, /* Number of bytes of data to be bound */
95045 void (*xDel)(void*), /* Destructor for the data */
95046 u8 encoding /* Encoding for the data */
95047 ){
95048 Vdbe *p = (Vdbe *)pStmt;
95049 Mem *pVar;
95050 int rc;
95051
 
95052 rc = vdbeUnbind(p, (u32)(i-1));
95053 if( rc==SQLITE_OK ){
95054 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
95055 if( zData!=0 ){
95056 pVar = &p->aVar[i-1];
@@ -95253,12 +95397,13 @@
95253 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
95254 }
95255 break;
95256 }
95257 case SQLITE_TEXT: {
 
95258 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
95259 pValue->enc);
95260 break;
95261 }
95262 default: {
95263 rc = sqlite3_bind_null(pStmt, i);
95264 break;
@@ -95275,10 +95420,11 @@
95275 #ifndef SQLITE_OMIT_INCRBLOB
95276 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
95277 #else
95278 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
95279 #endif
 
95280 sqlite3_mutex_leave(p->db->mutex);
95281 }
95282 return rc;
95283 }
95284 SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
@@ -95428,13 +95574,18 @@
95428 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
95429 /* No reprepare necessary */
95430 v->explain = eMode;
95431 rc = SQLITE_OK;
95432 }else{
 
95433 v->explain = eMode;
95434 rc = sqlite3Reprepare(v);
95435 v->haveEqpOps = eMode==2;
 
 
 
 
95436 }
95437 if( v->explain ){
95438 v->nResColumn = 12 - 4*v->explain;
95439 }else{
95440 v->nResColumn = v->nResAlloc;
@@ -95622,14 +95773,15 @@
95622 u32 nRec;
95623 u8 *aRec;
95624
95625 assert( p->pCsr->eCurType==CURTYPE_BTREE );
95626 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
95627 aRec = sqlite3DbMallocRaw(db, nRec);
95628 if( !aRec ) goto preupdate_old_out;
95629 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
95630 if( rc==SQLITE_OK ){
 
95631 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
95632 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
95633 }
95634 if( rc!=SQLITE_OK ){
95635 sqlite3DbFree(db, aRec);
@@ -96658,11 +96810,11 @@
96658 if( (rc&2)==0 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
96659 pRec->flags |= MEM_Int;
96660 }else{
96661 pRec->u.r = rValue;
96662 pRec->flags |= MEM_Real;
96663 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
96664 }
96665 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
96666 ** string representation after computing a numeric equivalent, because the
96667 ** string representation might not be the canonical representation for the
96668 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
@@ -96702,11 +96854,11 @@
96702 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM );
96703 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
96704 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
96705 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
96706 }else if( affinity<=SQLITE_AFF_REAL ){
96707 sqlite3VdbeIntegerAffinity(pRec);
96708 }
96709 }
96710 }else if( affinity==SQLITE_AFF_TEXT ){
96711 /* Only attempt the conversion to TEXT if there is an integer or real
96712 ** representation (blob and NULL do not get converted) but no string
@@ -97092,22 +97244,18 @@
97092 pBuf = pCache->pCValue;
97093 }
97094 assert( t>=12 );
97095 sqlite3RCStrRef(pBuf);
97096 if( t&1 ){
97097 /* Prevents the removal of a BOM in UTF16 text. Otherwise
97098 ** it causes no harm as long as len>=0, and pDest->enc is set
97099 ** to the correct encoding afterwards: ----
97100 ** (tag-20261016-1) `vvvvvvvvvvv */
97101 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, SQLITE_UTF8,
97102 sqlite3RCStrUnref);
97103 pDest->flags |= MEM_Term;
97104 }else{
97105 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0,
97106 sqlite3RCStrUnref);
97107 }
97108 pDest->enc = encoding;
97109 }else{
97110 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
97111 if( rc ) return rc;
97112 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
97113 if( (t&1)!=0 && encoding==SQLITE_UTF8 ){
@@ -111096,10 +111244,13 @@
111096 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
111097 Upsert *pUpsert = pNC->uNC.pUpsert;
111098 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
111099 pTab = pUpsert->pUpsertSrc->a[0].pSTab;
111100 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
 
 
 
111101 }
111102 }
111103 #endif /* SQLITE_OMIT_UPSERT */
111104
111105 if( pTab ){
@@ -112118,11 +112269,11 @@
112118 NameContext nc; /* Name context for resolving pE */
112119 sqlite3 *db; /* Database connection */
112120 int rc; /* Return code from subprocedures */
112121 u8 savedSuppErr; /* Saved value of db->suppressErr */
112122
112123 assert( sqlite3ExprIsInteger(pE, &i, 0, 0)==0 );
112124 pEList = pSelect->pEList;
112125
112126 /* Resolve all names in the ORDER BY term expression
112127 */
112128 memset(&nc, 0, sizeof(nc));
@@ -112217,11 +112368,11 @@
112217 int iCol = -1;
112218 Expr *pE, *pDup;
112219 if( pItem->fg.done ) continue;
112220 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
112221 if( NEVER(pE==0) ) continue;
112222 if( sqlite3ExprIsInteger(pE, &iCol, 0, 0) ){
112223 if( iCol<=0 || iCol>pEList->nExpr ){
112224 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
112225 return 1;
112226 }
112227 }else{
@@ -112400,11 +112551,11 @@
112400 ** copy of the iCol-th result-set expression. */
112401 pItem->u.x.iOrderByCol = (u16)iCol;
112402 continue;
112403 }
112404 }
112405 if( sqlite3ExprIsInteger(pE2, &iCol, 0, 0) ){
112406 /* The ORDER BY term is an integer constant. Again, set the column
112407 ** number so that sqlite3ResolveOrderGroupBy() will convert the
112408 ** order-by term to a copy of the result-set expression */
112409 if( iCol<1 || iCol>0xffff ){
112410 resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
@@ -114088,13 +114239,16 @@
114088 }else{
114089 u32 f = pLeft->flags | pRight->flags;
114090 if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse|EP_HasFunc))==EP_IsFalse
114091 && !IN_RENAME_OBJECT
114092 ){
 
114093 sqlite3ExprDeferredDelete(pParse, pLeft);
114094 sqlite3ExprDeferredDelete(pParse, pRight);
114095 return sqlite3ExprInt32(db, 0);
 
 
114096 }else{
114097 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
114098 }
114099 }
114100 }
@@ -115872,17 +116026,43 @@
115872 ** If pParse is not NULL and expression p is a variable, then the variable
115873 ** is marked so as to cause the statement to be reprepared each time a new
115874 ** value is bound to it. Except, if parameter bRSI is true, then the statement
115875 ** will only be reprepared if the rebind changes the value to or from a
115876 ** "small integer" (either 0 or 1).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115877 */
115878 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse, int bRSI){
115879 int iSign = 1; /* Either +1 or -1. */
115880 while( 1/*exit-by-break*/ ){
115881 if( NEVER(p==0) ) return 0;
115882 if( ExprUseUValue(p) ){
 
115883 *pValue = p->u.iValue*iSign;
 
 
 
115884 return 1;
115885 }
115886 if( p->op==TK_UPLUS ){
115887 p = p->pLeft;
115888 pParse = 0;
@@ -121456,17 +121636,18 @@
121456 " AND (type != 'index' OR tbl_name = %Q)",
121457 zDb,
121458 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
121459 pTab->zName
121460 );
121461
121462 sqlite3NestedParse(pParse,
121463 "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
121464 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
121465 "WHERE type IN ('trigger', 'view')",
121466 zDb, pTab->zName, iCol, zNew, bQuote
121467 );
 
121468
121469 /* Drop and reload the database schema. */
121470 renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename);
121471 renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
121472
@@ -123152,16 +123333,16 @@
123152 regOut = reg+1+nField;
123153 }
123154 if( i==pTab->iPKey ){
123155 sqlite3VdbeAddOp2(v, OP_Null, 0, regOut);
123156 }else{
123157 char aff = pTab->aCol[i].affinity;
123158 if( aff==SQLITE_AFF_REAL ){
123159 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
123160 }
123161 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
123162 pTab->aCol[i].affinity = aff;
 
 
 
 
123163 }
123164 nField++;
123165 }
123166 }
123167 if( nField==0 ){
@@ -123208,19 +123389,23 @@
123208 ** z points to the second token of the constraint definition. For an
123209 ** unnamed NOT NULL constraint, z points to the first byte past the NOT
123210 ** keyword.
123211 **
123212 ** Argument eTok may be the token value of the first token of the constraint
123213 ** (e.g. TK_CHECK or TK_REFERENCES) or zero. If it is either TK_REFERENCES
123214 ** or TK_FOREIGN, special parsing is enabled to find the end of the foreign-key
123215 ** constraint definition.
 
 
123216 **
123217 ** Return the number of bytes until the end of the constraint.
123218 */
123219 static int getConstraint(const u8 *z, int eTok){
123220 int iOff = 0;
123221 int t = 0;
 
 
123222
123223 #ifndef SQLITE_OMIT_FOREIGN_KEY
123224 if( eTok==TK_FOREIGN ){
123225 /* For a FOREIGN KEY constraint, use getConstraint() to parse everything
123226 ** up to the REFERENCES keyword. Then getConstraintToken() to consume
@@ -123232,10 +123417,18 @@
123232
123233 if( eTok==TK_REFERENCES ){
123234 /* REFERENCES is followed by a table name. Gobble this up here in
123235 ** case the table name is a fallback token like TK_GENERATED. */
123236 iOff += getConstraintToken(&z[iOff], &t);
 
 
 
 
 
 
 
 
123237 }
123238 #endif
123239
123240 /* Now, the current constraint proceeds until the next occurence of one
123241 ** of the following tokens:
@@ -123242,21 +123435,38 @@
123242 **
123243 ** CONSTRAINT, PRIMARY, NOT, UNIQUE, CHECK, DEFAULT,
123244 ** COLLATE, REFERENCES, FOREIGN, GENERATED, AS, RP, or COMMA
123245 **
123246 ** Also exit the loop if ILLEGAL turns up.
 
 
 
 
 
 
123247 */
123248 while( 1 ){
123249 int n = getConstraintToken(&z[iOff], &t);
123250 if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_NOT || t==TK_UNIQUE
123251 || t==TK_CHECK || t==TK_DEFAULT || t==TK_COLLATE || t==TK_REFERENCES
 
123252 || t==TK_FOREIGN || t==TK_RP || t==TK_COMMA || t==TK_ILLEGAL
123253 || t==TK_AS || t==TK_GENERATED
 
123254 ){
123255 break;
 
 
 
 
 
 
 
 
123256 }
123257 iOff += n;
 
123258 }
123259
123260 return iOff;
123261 }
123262
@@ -123419,12 +123629,16 @@
123419 iEnd = iOff;
123420 break;
123421 }
123422
123423 }else if( t==TK_NOT && iNotNull==ii ){
123424 iEnd = iOff + getConstraint(&zSql[iOff], 0);
123425 break;
 
 
 
 
123426 }else if( t==TK_RP || t==TK_ILLEGAL ){
123427 iEnd = -1;
123428 break;
123429 }else if( t==TK_COMMA ){
123430 break;
@@ -136891,11 +137105,11 @@
136891 break;
136892 default:
136893 return;
136894 }
136895 if( argc==2 ){
136896 switch( sqlite3_value_numeric_type(argv[0]) ){
136897 case SQLITE_INTEGER:
136898 case SQLITE_FLOAT:
136899 b = log(x);
136900 if( b<=0.0 ) return;
136901 x = sqlite3_value_double(argv[1]);
@@ -137625,10 +137839,11 @@
137625 assert( argc==3 );
137626 x = sqlite3_value_double(argv[0]);
137627 y = sqlite3_value_int(argv[1]);
137628 z = sqlite3_value_int(argv[2]);
137629 if( z<=0 ) z = 1;
 
137630 sqlite3FpDecode(&s, x, y, z);
137631 if( s.isSpecial==2 ){
137632 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
137633 }else{
137634 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
@@ -138446,10 +138661,148 @@
138446 pExpr->iTable = iCursor;
138447 pExpr->iColumn = iCol;
138448 }
138449 return pExpr;
138450 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138451
138452 /*
138453 ** This function is called to generate code executed when a row is deleted
138454 ** from the parent table of foreign key constraint pFKey and, if pFKey is
138455 ** deferred, when a row is inserted into the same table. When generating
@@ -138515,20 +138868,22 @@
138515 */
138516 for(i=0; i<pFKey->nCol; i++){
138517 Expr *pLeft; /* Value from parent table row */
138518 Expr *pRight; /* Column ref to child table */
138519 Expr *pEq; /* Expression (pLeft = pRight) */
138520 i16 iCol; /* Index of column in child table */
 
138521 const char *zCol; /* Name of column in child table */
138522
138523 iCol = pIdx ? pIdx->aiColumn[i] : -1;
138524 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
138525 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
138526 assert( iCol>=0 );
138527 zCol = pFKey->pFrom->aCol[iCol].zCnName;
138528 pRight = sqlite3Expr(db, TK_ID, zCol);
138529 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
 
138530 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
138531 }
138532
138533 /* If the child table is the same as the parent table, then add terms
138534 ** to the WHERE clause that prevent this entry from being scanned.
@@ -138784,17 +139139,16 @@
138784 ** Return true if the parser passed as the first argument is being
138785 ** used to code a trigger that is really a "SET NULL" action belonging
138786 ** to trigger pFKey.
138787 */
138788 static int isSetNullAction(Parse *pParse, FKey *pFKey){
138789 Parse *pTop = sqlite3ParseToplevel(pParse);
138790 if( pTop->pTriggerPrg ){
138791 Trigger *p = pTop->pTriggerPrg->pTrigger;
138792 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
138793 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
138794 ){
138795 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
138796 return 1;
138797 }
138798 }
138799 return 0;
138800 }
@@ -139184,30 +139538,39 @@
139184 Token tOld = { "old", 3 }; /* Literal "old" token */
139185 Token tNew = { "new", 3 }; /* Literal "new" token */
139186 Token tFromCol; /* Name of column in child table */
139187 Token tToCol; /* Name of column in parent table */
139188 int iFromCol; /* Idx of column in child table */
139189 Expr *pEq; /* tFromCol = OLD.tToCol */
 
 
 
139190
139191 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
139192 assert( iFromCol>=0 );
139193 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
139194 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
139195 sqlite3TokenInit(&tToCol,
139196 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
139197 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
139198
139199 /* Create the expression "OLD.zToCol = zFromCol". It is important
139200 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
139201 ** that the affinity and collation sequence associated with the
139202 ** parent table are used for the comparison. */
139203 pEq = sqlite3PExpr(pParse, TK_EQ,
139204 sqlite3PExpr(pParse, TK_DOT,
139205 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
139206 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
139207 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
139208 );
 
 
 
 
 
 
 
 
 
 
 
 
139209 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
139210
139211 /* For ON UPDATE, construct the next term of the WHEN clause.
139212 ** The final WHEN clause will be like this:
139213 **
@@ -144652,29 +145015,36 @@
144652 ** was not on the list.
144653 */
144654 SQLITE_API int sqlite3_cancel_auto_extension(
144655 void (*xInit)(void)
144656 ){
 
 
 
 
 
 
 
144657 #if SQLITE_THREADSAFE
144658 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
144659 #endif
144660 int i;
144661 int n = 0;
144662 wsdAutoextInit;
144663 #ifdef SQLITE_ENABLE_API_ARMOR
144664 if( xInit==0 ) return 0;
144665 #endif
144666 sqlite3_mutex_enter(mutex);
144667 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
144668 if( wsdAutoext.aExt[i]==xInit ){
144669 wsdAutoext.nExt--;
144670 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
144671 n++;
144672 break;
144673 }
144674 }
144675 sqlite3_mutex_leave(mutex);
 
144676 return n;
144677 }
144678
144679 /*
144680 ** Reset the automatic extension loading mechanism.
@@ -147256,18 +147626,13 @@
147256 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
147257 }
147258 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
147259 1, 0, &iDataCur, &iIdxCur);
147260 /* reg[7] counts the number of entries in the table.
147261 ** reg[8+i] counts the number of entries in the i-th index
147262 */
147263 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
147264 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
147265 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
147266 }
147267 assert( pParse->nMem>=8+j );
147268 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
147269 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
147270 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
147271
147272 /* Fetch the right-most column from the table. This will cause
147273 ** the entire record header to be parsed and sanity checked. It
@@ -147476,11 +147841,10 @@
147476 int ckUniq = sqlite3VdbeMakeLabel(pParse);
147477 if( pPk==pIdx ) continue;
147478 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
147479 pPrior, r1);
147480 pPrior = pIdx;
147481 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
147482 /* Verify that an index entry exists for the current table row */
147483 sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
147484 pIdx->nColumn); VdbeCoverage(v);
147485 jmp2 = sqlite3VdbeAddOp3(v, OP_IFindKey, iIdxCur+j, ckUniq, r1);
147486 VdbeCoverage(v);
@@ -151940,11 +152304,11 @@
151940 sqlite3HashInit(&ht);
151941 if( pEList ){
151942 nCol = pEList->nExpr;
151943 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
151944 testcase( aCol==0 );
151945 if( NEVER(nCol>32767) ) nCol = 32767;
151946 }else{
151947 nCol = 0;
151948 aCol = 0;
151949 }
151950 assert( nCol==(i16)nCol );
@@ -153185,13 +153549,16 @@
153185 assert( pOrderBy );
153186 nOrderBy = pOrderBy->nExpr;
153187
153188 /* For operators other than UNION ALL we have to make sure that
153189 ** the ORDER BY clause covers every term of the result set. Add
153190 ** terms to the ORDER BY clause as necessary.
 
 
153191 */
153192 if( op!=TK_ALL ){
 
153193 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
153194 struct ExprList_item *pItem;
153195 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
153196 assert( pItem!=0 );
153197 assert( pItem->u.x.iOrderByCol>0 );
@@ -153244,13 +153611,15 @@
153244 pParse->nMem += nExpr+1;
153245 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
153246 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
153247 if( pKeyDup ){
153248 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
153249 for(i=0; i<nExpr; i++){
153250 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
153251 pKeyDup->aSortFlags[i] = 0;
 
 
153252 }
153253 }
153254 }
153255
153256 /* Separate the left and the right query from one another
@@ -155303,20 +155672,20 @@
155303 *pNew = *p;
155304 p->pSrc = pNewSrc;
155305 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
155306 p->op = TK_SELECT;
155307 p->pWhere = 0;
155308 pNew->pGroupBy = 0;
155309 pNew->pHaving = 0;
155310 pNew->pOrderBy = 0;
155311 p->pPrior = 0;
155312 p->pNext = 0;
155313 p->pWith = 0;
155314 #ifndef SQLITE_OMIT_WINDOWFUNC
155315 p->pWinDefn = 0;
155316 #endif
155317 p->selFlags &= ~(u32)SF_Compound;
155318 assert( (p->selFlags & SF_Converted)==0 );
155319 p->selFlags |= SF_Converted;
155320 assert( pNew->pPrior!=0 );
155321 pNew->pPrior->pNext = pNew;
155322 pNew->pLimit = 0;
@@ -155399,10 +155768,12 @@
155399 pParse->pWith = pWith;
155400 }
155401 }
155402 return pWith;
155403 }
 
 
155404
155405 /*
155406 ** This function checks to see if argument pFrom refers to a CTE declared by
155407 ** a WITH clause on the stack currently maintained by the parser (on the
155408 ** pParse->pWith linked list). And if currently processing a CTE expression,
@@ -155536,19 +155907,17 @@
155536 }
155537 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
155538 pRecTerm = pRecTerm->pPrior;
155539 }
155540
 
 
 
 
 
 
155541 #if SQLITE_MAX_EXPR_DEPTH>0
155542 if( pParse->nTab>=db->aLimit[SQLITE_LIMIT_EXPR_DEPTH] ){
155543 /* Bug 2026-10-04T05:35:14Z: Prevent nested CTEs from generating
155544 ** an exponential number of cursors. The error message here will
155545 ** be "Nested too deep", which isn't exactly correct, but it is
155546 ** sufficient, and we don't want to use extra code space for more
155547 ** detail on such an obscure error. */
155548 pParse->nNestSel = pParse->nTab;
155549 }
155550 pParse->nNestSel++;
155551 if( checkSubqueryNestingDepth(pParse) ) return 2;
155552 #endif
155553 pCte->zCteErr = "circular reference: %s";
155554 pSavedWith = pParse->pWith;
@@ -155566,11 +155935,11 @@
155566 if( rc ){
155567 pParse->pWith = pSavedWith;
155568 return 2;
155569 }
155570 }else{
155571 if( sqlite3WalkSelect(pWalker, pSel) ){
155572 pParse->pWith = pSavedWith;
155573 return 2;
155574 }
155575 }
155576 pParse->pWith = pWith;
@@ -155828,11 +156197,14 @@
155828 #endif
155829 nCol = pTab->nCol;
155830 pTab->nCol = -1;
155831 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
155832 if( pFrom->fg.isSubquery ){
155833 sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect);
 
 
 
155834 }
155835 pWalker->eCode = eCodeOrig;
155836 pTab->nCol = nCol;
155837 }
155838 #endif
@@ -156118,10 +156490,31 @@
156118 SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
156119 UNUSED_PARAMETER2(NotUsed, NotUsed2);
156120 assert( 0 );
156121 }
156122 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156123 /*
156124 ** This routine "expands" a SELECT statement and all of its subqueries.
156125 ** For additional information on what it means to "expand" a SELECT
156126 ** statement, see the comment on the selectExpand worker callback above.
156127 **
@@ -156135,19 +156528,12 @@
156135 */
156136 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
156137 Walker w;
156138 w.xExprCallback = sqlite3ExprWalkNoop;
156139 w.pParse = pParse;
156140 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
156141 w.xSelectCallback = convertCompoundSelectToSubquery;
156142 w.xSelectCallback2 = 0;
156143 sqlite3WalkSelect(&w, pSelect);
156144 }
156145 w.xSelectCallback = selectExpander;
156146 w.xSelectCallback2 = sqlite3SelectPopWith;
156147 w.eCode = 0;
156148 sqlite3WalkSelect(&w, pSelect);
156149 }
156150
156151
156152 #ifndef SQLITE_OMIT_SUBQUERY
156153 /*
@@ -157868,15 +158254,25 @@
157868 /* This is a CTE for which materialization code has already been
157869 ** generated. Invoke the subroutine to compute the materialization,
157870 ** then make the pItem->iCursor be a copy of the ephemeral table that
157871 ** holds the result of the materialization. tag-select-0484 */
157872 CteUse *pCteUse = pItem->u2.pCteUse;
 
 
 
 
 
 
157873 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
157874 if( pItem->iCursor!=pCteUse->iCur ){
157875 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
157876 VdbeComment((v, "%!S", pItem));
157877 }
 
 
 
 
157878 pSub->nSelectRow = pCteUse->nRowEst;
157879 }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){
157880 /* This view has already been materialized by a prior entry in
157881 ** this same FROM clause. Reuse it. tag-select-0486 */
157882 Subquery *pPriorSubq;
@@ -160259,10 +160655,11 @@
160259 sqlite3ParseObjectInit(&sSubParse, db);
160260 memset(&sNC, 0, sizeof(sNC));
160261 sNC.pParse = &sSubParse;
160262 sSubParse.pTriggerTab = pTab;
160263 sSubParse.pToplevel = pTop;
 
160264 sSubParse.zAuthContext = pTrigger->zName;
160265 sSubParse.eTriggerOp = pTrigger->op;
160266 sSubParse.nQueryLoop = pParse->nQueryLoop;
160267 sSubParse.prepFlags = pParse->prepFlags;
160268 sSubParse.oldmask = 0;
@@ -161073,14 +161470,18 @@
161073 {
161074 int rc;
161075 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
161076 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
161077 db->aDb[iDb].zDbSName);
161078 if( rc==SQLITE_DENY ){
161079 goto update_cleanup;
161080 }else if( rc==SQLITE_IGNORE ){
161081 aXRef[j] = -1;
 
 
 
 
161082 }
161083 }
161084 #endif
161085 }
161086 assert( (chngRowid & chngPk)==0 );
@@ -163496,11 +163897,21 @@
163496 /* Justification of ALWAYS(): The xConstructor method is required to
163497 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
163498 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
163499 rc = growVTrans(db);
163500 if( rc==SQLITE_OK ){
163501 addToVTrans(db, sqlite3GetVTable(db, pTab));
 
 
 
 
 
 
 
 
 
 
163502 }
163503 }
163504
163505 return rc;
163506 }
@@ -165382,10 +165793,13 @@
165382 pOrigLhs->a[iField].pExpr = 0;
165383 }
165384 nRhs++;
165385 }
165386 }
 
 
 
165387 sqlite3ExprListDelete(db, pOrigRhs);
165388 if( pOrigLhs ){
165389 sqlite3ExprListDelete(db, pOrigLhs);
165390 pNew->pLeft->x.pList = pLhs;
165391 }
@@ -166907,11 +167321,13 @@
166907 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
166908 SWAP(u8, bSeekPastNull, bStopAtNull);
166909 SWAP(u8, nBtm, nTop);
166910 }
166911
166912 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
 
 
166913 /* In case OP_SeekScan is used, ensure that the index cursor does not
166914 ** point to a valid row for the first iteration of this loop. */
166915 sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
166916 }
166917
@@ -168887,11 +169303,11 @@
168887 if( pLeft==0 ){
168888 pNew = 0; /* Collating sequence mismatch */
168889 }else{
168890 pDup = sqlite3ExprDup(db, pLeft, 0);
168891 if( sqlite3ExprCollSeq(pParse, pDup)!=pCollSeq
168892 && ALWAYS(pCollSeq!=0)
168893 ){
168894 assert( pCollSeq->zName!=0 );
168895 pDup = sqlite3ExprAddCollateString(pParse, pDup, pCollSeq->zName);
168896 }
168897 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
@@ -173972,11 +174388,11 @@
173972 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
173973 SrcList *pTabList; /* The FROM clause */
173974 SrcItem *pSrc; /* The FROM clause btree term to add */
173975 WhereLoop *pNew; /* Template WhereLoop object */
173976 int rc = SQLITE_OK; /* Return code */
173977 int iSortIdx = 1; /* Index number */
173978 int b; /* A boolean value */
173979 LogEst rSize; /* number of rows in the table */
173980 WhereClause *pWC; /* The parsed WHERE clause */
173981 Table *pTab; /* Table being queried */
173982
@@ -173991,11 +174407,15 @@
173991 if( pSrc->fg.isIndexedBy ){
173992 assert( pSrc->fg.isCte==0 );
173993 /* An INDEXED BY clause specifies a particular index to use */
173994 pProbe = pSrc->u2.pIBIndex;
173995 }else if( !HasRowid(pTab) ){
173996 pProbe = pTab->pIndex;
 
 
 
 
173997 }else{
173998 /* There is no INDEXED BY clause. Create a fake Index object in local
173999 ** variable sPk to represent the rowid primary key index. Make this
174000 ** fake index the first in a chain of Index objects with all of the real
174001 ** indices to follow */
@@ -174010,15 +174430,11 @@
174010 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
174011 sPk.idxType = SQLITE_IDXTYPE_IPK;
174012 aiRowEstPk[0] = pTab->nRowLogEst;
174013 aiRowEstPk[1] = 0;
174014 pFirst = pSrc->pSTab->pIndex;
174015 if( pSrc->fg.notIndexed==0 ){
174016 /* The real indices of the table are only considered if the
174017 ** NOT INDEXED qualifier is omitted from the FROM clause */
174018 sPk.pNext = pFirst;
174019 }
174020 pProbe = &sPk;
174021 }
174022 rSize = pTab->nRowLogEst;
174023
174024 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
@@ -174076,12 +174492,14 @@
174076 }
174077 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
174078
174079 /* Loop over all indices. If there was an INDEXED BY clause, then only
174080 ** consider index pProbe. */
174081 for(; rc==SQLITE_OK && pProbe;
174082 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
 
 
174083 ){
174084 if( pProbe->pPartIdxWhere!=0
174085 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
174086 pProbe->pPartIdxWhere)
174087 ){
@@ -174797,10 +175215,12 @@
174797 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
174798
174799 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
174800 if( (pTerm->eOperator & WO_OR)!=0
174801 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
 
 
174802 ){
174803 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
174804 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
174805 WhereTerm *pOrTerm;
174806 int once = 1;
@@ -178662,15 +179082,20 @@
178662 assert( p!=0 );
178663 assert( p->pWin!=0 );
178664
178665 /* If this function is being called from within a scalar sub-select
178666 ** that used by the SELECT statement being processed, only process
178667 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
178668 ** not process aggregates or window functions at all, as they belong
178669 ** to the scalar sub-select. */
178670 if( p->pSubSelect ){
178671 if( pExpr->op!=TK_COLUMN ){
 
 
 
 
 
178672 return WRC_Continue;
178673 }else{
178674 int nSrc = p->pSrc->nSrc;
178675 int i;
178676 for(i=0; i<nSrc; i++){
@@ -178747,11 +179172,13 @@
178747 Select *pSave = p->pSubSelect;
178748 if( pSave==pSelect ){
178749 return WRC_Continue;
178750 }else{
178751 p->pSubSelect = pSelect;
 
178752 sqlite3WalkSelect(pWalker, pSelect);
 
178753 p->pSubSelect = pSave;
178754 }
178755 return WRC_Prune;
178756 }
178757
@@ -178851,12 +179278,19 @@
178851 pExpr->op2++;
178852 }
178853 return WRC_Continue;
178854 }
178855
 
 
 
 
178856 static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
178857 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
 
 
 
178858 assert( !ExprHasProperty(pExpr, EP_IntValue) );
178859 sqlite3ErrorMsg(pWalker->pParse,
178860 "misuse of aggregate: %s()", pExpr->u.zToken);
178861 }
178862 return WRC_Continue;
@@ -178899,11 +179333,13 @@
178899 }
178900 sqlite3AggInfoPersistWalkerInit(&w, pParse);
178901 sqlite3WalkSelect(&w, p);
178902 if( (p->selFlags & SF_Aggregate)==0 ){
178903 w.xExprCallback = disallowAggregatesInOrderByCb;
178904 w.xSelectCallback = 0;
 
 
178905 sqlite3WalkExprList(&w, p->pOrderBy);
178906 }
178907
178908 p->pSrc = 0;
178909 p->pWhere = 0;
@@ -181273,11 +181709,13 @@
181273 case TK_INTEGER:
181274 case TK_STRING:
181275 case TK_FLOAT:
181276 case TK_BLOB:
181277 sqlite3ExprDeferredDelete(pParse, pLeft);
181278 return sqlite3ExprInt32(pParse->db, op==TK_NOTNULL);
 
 
181279 default:
181280 break;
181281 }
181282 return sqlite3PExpr(pParse, op, pLeft, 0);
181283 }
@@ -191632,10 +192070,11 @@
191632 SQLITE_MAX_TRIGGER_DEPTH,
191633 SQLITE_MAX_WORKER_THREADS,
191634 SQLITE_MAX_PARSER_DEPTH,
191635 SQLITE_MAX_SCHEMA,
191636 SQLITE_MAX_TRIGGER_STEPS,
 
191637 };
191638
191639 /*
191640 ** Make sure the hard limits are set to reasonable values
191641 */
@@ -191719,11 +192158,12 @@
191719 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
191720 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
191721 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
191722 assert( aHardLimit[SQLITE_LIMIT_SCHEMA]==SQLITE_MAX_SCHEMA );
191723 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_STEPS]==SQLITE_MAX_TRIGGER_STEPS );
191724 assert( SQLITE_LIMIT_TRIGGER_STEPS==(SQLITE_N_LIMIT-1) );
 
191725
191726
191727 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
191728 return -1;
191729 }
@@ -192834,13 +193274,18 @@
192834 int rc;
192835 pVfs = sqlite3_vfs_find(0);
192836 if( pVfs==0 ) return 0;
192837
192838 /* This function works in milliseconds, but the underlying OsSleep()
192839 ** API uses microseconds. Hence the 1000's.
192840 */
192841 rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000);
 
 
 
 
 
192842 return rc;
192843 }
192844
192845 /*
192846 ** Enable or disable the extended result codes.
@@ -217559,11 +218004,16 @@
217559 for(iIn=iOut=0; iIn<sz; iIn++){
217560 char c = z[iIn];
217561 if( c=='\\' ){
217562 u32 v;
217563 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
217564 if( v<=0x7f ){
 
 
 
 
 
217565 zOut[iOut++] = (char)v;
217566 }else if( v<=0x7ff ){
217567 assert( szEscape>=2 );
217568 zOut[iOut++] = (char)(0xc0 | (v>>6));
217569 zOut[iOut++] = 0x80 | (v&0x3f);
@@ -221778,10 +222228,14 @@
221778
221779 if( rc ) return rc;
221780 if( NEVER(p==0) ) return SQLITE_OK;
221781 if( p->iCell>=NCELL(pNode) ) return SQLITE_ABORT;
221782 if( i==0 ){
 
 
 
 
221783 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
221784 }else if( i<=pRtree->nDim2 ){
221785 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
221786 #ifndef SQLITE_RTREE_INT_ONLY
221787 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
@@ -221875,10 +222329,24 @@
221875 pCons->pInfo = pInfo;
221876 return SQLITE_OK;
221877 }
221878
221879 SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221880
221881 /*
221882 ** Rtree virtual table module xFilter method.
221883 */
221884 static int rtreeFilter(
@@ -221901,17 +222369,13 @@
221901 pCsr->iStrategy = idxNum;
221902 if( idxNum==1 ){
221903 /* Special case - lookup by rowid. */
221904 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
221905 RtreeSearchPoint *p; /* Search point for the leaf */
221906 i64 iRowid = sqlite3_value_int64(argv[0]);
221907 i64 iNode = 0;
221908 int eType = sqlite3_value_numeric_type(argv[0]);
221909 if( eType==SQLITE_INTEGER
221910 || (eType==SQLITE_FLOAT
221911 && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0])))
221912 ){
221913 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
221914 }else{
221915 rc = SQLITE_OK;
221916 pLeaf = 0;
221917 }
@@ -223164,10 +223628,11 @@
223164 ** conflict-handling mode specified by the user.
223165 */
223166 if( nData>1 ){
223167 int ii;
223168 int nn = nData - 4;
 
223169
223170 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
223171 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
223172 **
223173 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
@@ -223197,15 +223662,29 @@
223197 rc = rtreeConstraintError(pRtree, ii+1);
223198 goto constraint;
223199 }
223200 }
223201 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223202
223203 /* If a rowid value was supplied, check if it is already present in
223204 ** the table. If so, the constraint has failed. */
223205 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
223206 cell.iRowid = sqlite3_value_int64(aData[2]);
223207 if( sqlite3_value_type(aData[0])==SQLITE_NULL
223208 || sqlite3_value_int64(aData[0])!=cell.iRowid
223209 ){
223210 int steprc;
223211 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
@@ -224621,10 +225100,12 @@
224621 return pOut;
224622 }else{
224623 s.nErr++;
224624 rc = SQLITE_ERROR;
224625 }
 
 
224626 }
224627 parse_json_err:
224628 if( pRc ) *pRc = rc;
224629 sqlite3_free(s.a);
224630 return 0;
@@ -224671,13 +225152,15 @@
224671 geopolySwab32((unsigned char*)&GeoX(p,ii));
224672 geopolySwab32((unsigned char*)&GeoY(p,ii));
224673 }
224674 p->hdr[0] ^= 1;
224675 }
 
224676 }
 
 
224677 }
224678 if( pRc ) *pRc = SQLITE_OK;
224679 return p;
224680 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
224681 const unsigned char *zJson = sqlite3_value_text(pVal);
224682 if( zJson==0 ){
224683 if( pRc ) *pRc = SQLITE_NOMEM;
@@ -225728,13 +226211,17 @@
225728 pCsr->iStrategy = idxNum;
225729 if( idxNum==1 ){
225730 /* Special case - lookup by rowid. */
225731 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
225732 RtreeSearchPoint *p; /* Search point for the leaf */
225733 i64 iRowid = sqlite3_value_int64(argv[0]);
225734 i64 iNode = 0;
225735 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
 
 
 
 
225736 if( rc==SQLITE_OK && pLeaf!=0 ){
225737 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
225738 assert( p!=0 ); /* Always returns pCsr->sPoint */
225739 pCsr->aNode[0] = pLeaf;
225740 p->id = iNode;
@@ -225960,10 +226447,12 @@
225960 i64 oldRowid; /* The old rowid */
225961 int oldRowidValid; /* True if oldRowid is valid */
225962 i64 newRowid; /* The new rowid */
225963 int newRowidValid; /* True if newRowid is valid */
225964 int coordChange = 0; /* Change in coordinates */
 
 
225965
225966 if( pRtree->nNodeRef ){
225967 /* Unable to write to the btree while another cursor is reading from it,
225968 ** since the write might do a rebalance which would disrupt the read
225969 ** cursor. */
@@ -225975,18 +226464,43 @@
225975 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
225976 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
225977 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
225978 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
225979 cell.iRowid = newRowid;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225980
225981 if( nData>1 /* not a DELETE */
225982 && (!oldRowidValid /* INSERT */
225983 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
225984 || oldRowid!=newRowid) /* Rowid change */
225985 ){
225986 assert( aData[2]!=0 );
225987 geopolyBBox(0, aData[2], cell.aCoord, &rc);
225988 if( rc ){
225989 if( rc==SQLITE_ERROR ){
225990 pVtab->zErrMsg =
225991 sqlite3_mprintf("_shape does not contain a valid polygon");
225992 }
@@ -226048,21 +226562,21 @@
226048 sqlite3_stmt *pUp = pRtree->pWriteAux;
226049 int jj;
226050 int nChange = 0;
226051 sqlite3_bind_int64(pUp, 1, cell.iRowid);
226052 assert( pRtree->nAux>=1 );
226053 if( sqlite3_value_nochange(aData[2]) ){
226054 sqlite3_bind_null(pUp, 2);
226055 }else{
226056 GeoPoly *p = 0;
226057 if( sqlite3_value_type(aData[2])==SQLITE_TEXT
226058 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
226059 && rc==SQLITE_OK
226060 ){
226061 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
226062 }else{
226063 sqlite3_bind_value(pUp, 2, aData[2]);
226064 }
226065 sqlite3_free(p);
226066 nChange = 1;
226067 }
226068 for(jj=1; jj<nData-2; jj++){
@@ -226074,10 +226588,11 @@
226074 rc = sqlite3_reset(pUp);
226075 }
226076 }
226077
226078 geopoly_update_end:
 
226079 rtreeRelease(pRtree);
226080 return rc;
226081 }
226082
226083 /*
@@ -230763,11 +231278,11 @@
230763 int bOpen = 0;
230764 int rc;
230765 p->nRbu = 0;
230766 p->pRbuFd = 0;
230767 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
230768 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
230769 if( p->eStage>=RBU_STAGE_MOVE ){
230770 bOpen = 1;
230771 }else{
230772 RbuState *pState = rbuLoadState(p);
230773 if( pState ){
@@ -235257,11 +235772,11 @@
235257 for(i=0; i<nData; i++){
235258 sz += ((struct iovec*)aData)[i].iov_len;
235259 }
235260 }
235261
235262 pNew->aData = sqlite3_malloc64( sz );
235263 if( pNew->aData==0 ){
235264 rc = SQLITE_NOMEM;
235265 goto carray_bind_error;
235266 }
235267
@@ -235288,11 +235803,11 @@
235288 p[i].iov_len = n;
235289 p[i].iov_base = z;
235290 z += n;
235291 memcpy(p[i].iov_base, ((struct iovec*)aData)[i].iov_base, n);
235292 }
235293 }else{
235294 memcpy(pNew->aData, aData, sz);
235295 }
235296 pNew->xDel = sqlite3_free;
235297 pNew->pDel = pNew->aData;
235298 }else{
@@ -241551,11 +242066,11 @@
241551 }
241552 pNew->nRecord = pOut - pNew->aRecord;
241553 }
241554 }else if( bRebase ){
241555 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
241556 *ppNew = pExist;
241557 }else{
241558 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
241559 pNew = (SessionChange*)sqlite3_malloc64(nByte);
241560 if( pNew==0 ){
241561 rc = SQLITE_NOMEM;
@@ -242404,12 +242919,12 @@
242404 }
242405 if( bData ){
242406 a2 = aChange;
242407 for(i=0; i<pIter->nCol; i++){
242408 int n1 = sessionSerialLen(a1);
242409 int n2 = sessionSerialLen(a2);
242410 if( pIter->abPK[i] || a2[0]!=0xFF ){
242411 memcpy(pOut, a1, n1);
242412 pOut += n1;
242413 }else{
242414 *pOut++ = '\0';
242415 }
@@ -242463,10 +242978,13 @@
242463 bNew = 0;
242464
242465 /* A patchset may not be rebased */
242466 if( pIter->bPatchset ){
242467 rc = SQLITE_ERROR;
 
 
 
242468 }
242469
242470 /* Append a table header to the output for this new table */
242471 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
242472 sessionAppendVarint(&sOut, pIter->nCol, &rc);
@@ -242560,10 +243078,11 @@
242560 *pnOut = sOut.nBuf;
242561 sOut.aBuf = 0;
242562 }
242563 }
242564 sqlite3_free(sOut.aBuf);
 
242565 return rc;
242566 }
242567
242568 /*
242569 ** Create a new rebaser object.
@@ -247651,10 +248170,13 @@
247651 p++;
247652 while( p ){
247653 if( *p=='\'' ){
247654 p++;
247655 if( *p!='\'' ) break;
 
 
 
247656 }
247657 p++;
247658 if( *p==0 ) p = 0;
247659 }
247660 break;
@@ -249075,11 +249597,11 @@
249075 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
249076 }
249077 zExpr[iOut++] = '"';
249078 zExpr[iOut++] = ' ';
249079 }
249080 if( zText[i]==aSpec[2] ){
249081 i += 2;
249082 if( zText[i-1]=='^' ) i++;
249083 while( i<nText && zText[i]!=']' ) i++;
249084 }
249085 iFirst = i+1;
@@ -249614,10 +250136,17 @@
249614
249615 *pRc = rc;
249616 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
249617 return 1;
249618 }
 
 
 
 
 
 
 
249619 return 0;
249620 }
249621 }
249622
249623
@@ -249729,10 +250258,28 @@
249729 for(i=0; i<pNode->nChild; i++){
249730 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
249731 }
249732 }
249733 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249734
249735
249736
249737 /*
249738 ** Compare the values currently indicated by the two nodes as follows:
@@ -250048,14 +250595,14 @@
250048 pAnd->bNomatch = 1;
250049 }
250050 }
250051 }while( bMatch==0 );
250052
 
250053 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
250054 fts5ExprNodeZeroPoslist(pAnd);
250055 }
250056 pAnd->iRowid = iLast;
250057 return SQLITE_OK;
250058 }
250059
250060 static int fts5ExprNodeNext_AND(
250061 Fts5Expr *pExpr,
@@ -250197,10 +250744,11 @@
250197 break;
250198
250199 default:
250200 assert( pNode->eType==FTS5_NOT );
250201 pNode->bEof = pNode->apChild[0]->bEof;
 
250202 break;
250203 }
250204 }
250205
250206 if( rc==SQLITE_OK ){
@@ -253453,12 +254001,11 @@
253453 }
253454 if( rc!=SQLITE_OK ){
253455 sqlite3_free(pRet);
253456 pRet = 0;
253457 }else{
253458 pRet->p[nByte] = 0x00;
253459 pRet->p[nByte+1] = 0x00;
253460 }
253461 }
253462 p->rc = rc;
253463 p->nRead++;
253464 }
@@ -255308,11 +255855,11 @@
255308 pIter->flags |= FTS5_SEGITER_ONETERM;
255309 if( pIter->pLeaf ){
255310 if( flags & FTS5INDEX_QUERY_DESC ){
255311 pIter->flags |= FTS5_SEGITER_REVERSE;
255312 }
255313 if( bDlidx ){
255314 fts5SegIterLoadDlidx(p, pIter);
255315 }
255316 if( flags & FTS5INDEX_QUERY_DESC ){
255317 fts5SegIterReverse(p, pIter);
255318 }
@@ -255707,10 +256254,17 @@
255707 }else{
255708 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
255709 pIter->iLeafOffset = iOff;
255710 fts5SegIterLoadNPos(p, pIter);
255711 }
 
 
 
 
 
 
 
255712 break;
255713 }
255714 }
255715 }
255716 }
@@ -259800,15 +260354,13 @@
259800 for(ii=0; ii<pT->nIter; ii++){
259801 Fts5Iter *p = pT->apIter[ii];
259802 if( p->base.bEof==0
259803 && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
259804 ){
259805 fts5MultiIterNext(pIndex, p, bFrom, iFrom);
259806 while( bFrom && p->base.bEof==0
259807 && p->base.iRowid<iFrom
259808 && pIndex->rc==SQLITE_OK
259809 ){
259810 fts5MultiIterNext(pIndex, p, 0, 0);
259811 }
259812 }
259813 }
259814
@@ -260000,10 +260552,19 @@
260000 ** queries performed as part of an integrity-check, or by the fts5vocab
260001 ** module. */
260002 if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){
260003 bTokendata = 0;
260004 }
 
 
 
 
 
 
 
 
 
260005
260006 /* Figure out which index to search and set iIdx accordingly. If this
260007 ** is a prefix query for which there is no prefix index, set iIdx to
260008 ** greater than pConfig->nPrefix to indicate that the query will be
260009 ** satisfied by scanning multiple terms in the main index.
@@ -262818,11 +263379,13 @@
262818 }
262819 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
262820 pInfo->aConstraintUsage[i].omit = 1;
262821 }
262822 }else if( p->usable ){
262823 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p) ){
 
 
262824 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB );
262825 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G';
262826 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
262827 idxStr += strlen(&idxStr[iIdxStr]);
262828 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
@@ -262830,11 +263393,10 @@
262830 nSeenMatch++;
262831 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){
262832 idxStr[iIdxStr++] = '=';
262833 bSeenEq = 1;
262834 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
262835 pInfo->aConstraintUsage[i].omit = 1;
262836 }
262837 }
262838 }
262839
262840 if( bSeenEq==0 ){
@@ -264481,19 +265043,21 @@
264481 if( aPopulator==0 ) rc = SQLITE_NOMEM;
264482 if( rc==SQLITE_OK ){
264483 rc = fts5SeekCursor(pCsr, 0);
264484 }
264485 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
264486 const char *z = 0;
264487 int n = 0;
264488 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
264489 if( rc==SQLITE_OK ){
264490 rc = sqlite3Fts5ExprPopulatePoslists(
264491 pConfig, pCsr->pExpr, aPopulator, i, z, n
264492 );
264493 }
264494 sqlite3Fts5ClearLocale(pConfig);
 
 
264495 }
264496 sqlite3_free(aPopulator);
264497
264498 if( pCsr->pSorter ){
264499 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
@@ -265771,11 +266335,11 @@
265771 int nArg, /* Number of args */
265772 sqlite3_value **apUnused /* Function arguments */
265773 ){
265774 assert( nArg==0 );
265775 UNUSED_PARAM2(nArg, apUnused);
265776 sqlite3_result_text(pCtx, "fts5: 2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918", -1, SQLITE_TRANSIENT);
265777 }
265778
265779 /*
265780 ** Implementation of fts5_locale(LOCALE, TEXT) function.
265781 **
@@ -266131,13 +266695,15 @@
266131 */
266132 static int fts5StorageGetStmt(
266133 Fts5Storage *p, /* Storage handle */
266134 int eStmt, /* FTS5_STMT_XXX constant */
266135 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
266136 char **pzErrMsg /* OUT: Error message (if any) */
266137 ){
266138 int rc = SQLITE_OK;
 
 
266139
266140 /* If there is no %_docsize table, there should be no requests for
266141 ** statements to operate on it. */
266142 assert( p->pConfig->bColumnsize || (
266143 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
@@ -266310,15 +266876,19 @@
266310 rc = fts5ExecPrintf(pConfig->db, 0,
266311 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
266312 pConfig->zDb, pConfig->zName
266313 );
266314 }
266315 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
266316 rc = fts5ExecPrintf(pConfig->db, 0,
266317 "DROP TABLE IF EXISTS %Q.'%q_content';",
266318 pConfig->zDb, pConfig->zName
266319 );
 
 
 
 
266320 }
266321 return rc;
266322 }
266323
266324 static void fts5StorageRenameOne(
@@ -266343,11 +266913,13 @@
266343 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
266344 fts5StorageRenameOne(pConfig, &rc, "config", zName);
266345 if( pConfig->bColumnsize ){
266346 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
266347 }
266348 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
 
 
266349 fts5StorageRenameOne(pConfig, &rc, "content", zName);
266350 }
266351 return rc;
266352 }
266353
@@ -266423,11 +266995,11 @@
266423 for(i=0; i<pConfig->nCol; i++){
266424 if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->abUnindexed[i] ){
266425 sqlite3_str_appendf(pDefn, ", c%d", i);
266426 }
266427 }
266428 if( pConfig->bLocale ){
266429 for(i=0; i<pConfig->nCol; i++){
266430 if( pConfig->abUnindexed[i]==0 ){
266431 sqlite3_str_appendf(pDefn, ", l%d", i);
266432 }
266433 }
@@ -266764,11 +267336,11 @@
266764 p->aTotalSize[ii] -= aCol[ii];
266765 }
266766 }
266767 sqlite3_free(aCol);
266768 }
266769 iOrigin = sqlite3_column_int(pLookup, 1);
266770 }
266771 rc2 = sqlite3_reset(pLookup);
266772 if( rc==SQLITE_OK ) rc = rc2;
266773 }
266774
@@ -270300,13 +270872,14 @@
270300
270301 #define FTS5_VOCAB_COL 0
270302 #define FTS5_VOCAB_ROW 1
270303 #define FTS5_VOCAB_INSTANCE 2
270304
270305 #define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
270306 #define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
270307 #define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
 
270308
270309 /*
270310 ** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
270311 */
270312 #define FTS5_VOCAB_TERM_EQ 0x0100
@@ -270394,13 +270967,13 @@
270394 const char * const *argv, /* xCreate/xConnect argument array */
270395 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
270396 char **pzErr /* Write any error message here */
270397 ){
270398 const char *azSchema[] = {
270399 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
270400 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
270401 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
270402 };
270403
270404 Fts5VocabTable *pRet = 0;
270405 int rc = SQLITE_OK; /* Return code */
270406 int bDb;
270407
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** d08cabd778f7844d04f3852d93197e80c0d0 with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.55.0"
471 #define SQLITE_VERSION_NUMBER 3055000
472 #define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -4768,10 +4768,19 @@
4768 ** defined by the database schema.</dd>)^
4769 **
4770 ** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
4771 ** <dd>The maximum number of SQL statements that can be contained within
4772 ** a single trigger.</dd>)^
4773 **
4774 ** [[SQLITE_LIMIT_CTES]] ^(<dt>SQLITE_LIMIT_CTES</dt>
4775 ** <dd>The maximum number of [common table expressions] (CTEs) allowed
4776 ** in a single SQL statements. This limit is used to limit possible
4777 ** exponential growth in the number of required cursors required for
4778 ** nested joins of CTEs. The limit is an absolute upper bound, and
4779 ** SQLite may refuse to compile a statement if the number of CTEs simply
4780 ** approaches this limit. Set this limit to 0 to disable the restriction.
4781 ** </dd>)^
4782 ** </dl>
4783 */
4784 #define SQLITE_LIMIT_LENGTH 0
4785 #define SQLITE_LIMIT_SQL_LENGTH 1
4786 #define SQLITE_LIMIT_COLUMN 2
@@ -4785,10 +4794,11 @@
4794 #define SQLITE_LIMIT_TRIGGER_DEPTH 10
4795 #define SQLITE_LIMIT_WORKER_THREADS 11
4796 #define SQLITE_LIMIT_PARSER_DEPTH 12
4797 #define SQLITE_LIMIT_SCHEMA 13
4798 #define SQLITE_LIMIT_TRIGGER_STEPS 14
4799 #define SQLITE_LIMIT_CTES 15
4800
4801 /*
4802 ** CAPI3REF: Prepare Flags
4803 **
4804 ** These constants define various flags that can be passed into the
@@ -6197,10 +6207,14 @@
6207 ** CAPI3REF: Text Encodings
6208 **
6209 ** These constants define integer codes that represent the various
6210 ** text encodings supported by SQLite.
6211 **
6212 ** If any value other than the ones defined here are passed into the
6213 ** "encoding" argument of an API function, then the results are
6214 ** unspecified and subject to change.
6215 **
6216 ** <dl>
6217 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
6218 **
6219 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
6220 ** with each code point being expressed "little endian" - the least significant
@@ -7179,10 +7193,14 @@
7193 ** VFS and operating system. Some system treat a negative argument as an
7194 ** instruction to sleep forever. Others understand it to mean do not sleep
7195 ** at all. ^In SQLite version 3.42.0 and later, a negative
7196 ** argument passed into sqlite3_sleep() is changed to zero before it is relayed
7197 ** down into the xSleep method of the VFS.
7198 **
7199 ** The maximum sleep time is 2147483 milliseconds (about 35 minutes and
7200 ** 47 seconds). If the argument to sqlite3_sleep() is greater than 2147483,
7201 ** the sleep time is capped at 2147483 milliseconds.
7202 */
7203 SQLITE_API int sqlite3_sleep(int);
7204
7205 /*
7206 ** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11573,11 +11591,11 @@
11591 ** buffer. If the bit had been clear, the returned buffer will not
11592 ** be accessed by SQLite after the call.
11593 **
11594 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11595 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11596 ** allocation or I/O error occurs.
11597 **
11598 ** This interface is omitted if SQLite is compiled with the
11599 ** [SQLITE_OMIT_DESERIALIZE] option.
11600 */
11601 SQLITE_API unsigned char *sqlite3_serialize(
@@ -15089,10 +15107,31 @@
15107 */
15108 #ifndef SQLITE_MAX_TRIGGER_STEPS
15109 # define SQLITE_MAX_TRIGGER_STEPS 65000
15110 #endif
15111
15112 /*
15113 ** Maximum number of CTE cursors (approximately). This limit will be
15114 ** exceeded if any CTE uses a cursor number larger than this value, even
15115 ** of some of the cursors are used for something other than CTEs.
15116 **
15117 ** The purpose of this limit is to prevent resource exhaustion caused
15118 ** by hostile SQL inputs such as:
15119 **
15120 ** WITH c0 AS (SELECT 1 AS x),
15121 ** c1 AS (SELECT a.x AS x FROM c0 a, c0 b),
15122 ** c2 AS (SELECT a.x AS x FROM c1 a, c1 b),
15123 ** ...
15124 ** c17 AS (SELECT a.x AS x FROM c16 a, c16 b)
15125 ** SELECT count(*) FROM c17;
15126 **
15127 ** See Bug 2026-10-04T05:35:14Z
15128 */
15129 #ifndef SQLITE_MAX_CTES
15130 # define SQLITE_MAX_CTES 50000
15131 #endif
15132
15133 /************** End of sqliteLimit.h *****************************************/
15134 /************** Continuing where we left off in sqliteInt.h ******************/
15135
15136 /* Disable nuisance warnings on Borland compilers */
15137 #if defined(__BORLANDC__)
@@ -18737,11 +18776,11 @@
18776
18777 /*
18778 ** The number of different kinds of things that can be limited
18779 ** using the sqlite3_limit() interface.
18780 */
18781 #define SQLITE_N_LIMIT (SQLITE_LIMIT_CTES+1)
18782
18783 /*
18784 ** Lookaside malloc is a set of fixed-size buffers that can be used
18785 ** to satisfy small transient memory allocation requests for objects
18786 ** associated with a particular database connection. The use of
@@ -19006,14 +19045,17 @@
19045 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
19046 #endif
19047 };
19048
19049 /*
19050 ** A macro to discover the encoding of a database or for managing
19051 ** encoding values.
19052 */
19053 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
19054 #define ENC(db) ((db)->enc)
19055 #define ENC_MASK 0x1f /* Usable encoding bits */
19056 #define ENC_KEEPBOM 0x80 /* Do not remove BOM */
19057
19058 /*
19059 ** A u64 constant where the lower 32 bits are all zeros. Only the
19060 ** upper 32 bits are included in the argument. Necessary because some
19061 ** C-compilers still do not accept LL integer literals.
@@ -21141,10 +21183,11 @@
21183 #endif
21184 Parse *pToplevel; /* Parse structure for main program (or NULL) */
21185 Table *pTriggerTab; /* Table triggers are being coded for */
21186 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
21187 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
21188 Trigger *pTrigCoded; /* Trigger whose sub-program is being coded */
21189
21190 /**************************************************************************
21191 ** Fields above must be initialized to zero. The fields that follow,
21192 ** down to the beginning of the recursive section, do not need to be
21193 ** initialized as they will be set before being used. The boundary is
@@ -25222,11 +25265,11 @@
25265 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(const Mem*);
25266 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
25267 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
25268 SQLITE_PRIVATE int sqlite3MemRealValueRC(Mem*, double*);
25269 SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
25270 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*,int bFromText);
25271 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
25272 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
25273 SQLITE_PRIVATE int sqlite3VdbeMemCast(Mem*,u8,u8);
25274 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
25275 SQLITE_PRIVATE int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*);
@@ -26108,10 +26151,11 @@
26151 neg = 0;
26152 }
26153 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
26154 return 1;
26155 }
26156 if( neg ) Y = -Y;
26157 zDate += 10;
26158 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
26159 if( parseHhMmSs(zDate, p)==0 ){
26160 if( p->h==24 ){
26161 int mxD = daysOfMonth(Y,M);
@@ -26131,11 +26175,11 @@
26175 }else{
26176 return 1;
26177 }
26178 p->validJD = 0;
26179 p->validYMD = 1;
26180 p->Y = Y;
26181 p->M = M;
26182 p->D = D;
26183 computeFloor(p);
26184 if( p->tz ){
26185 computeJD(p);
@@ -30620,10 +30664,16 @@
30664 pTo->xMutexLeave = pFrom->xMutexLeave;
30665 pTo->xMutexHeld = pFrom->xMutexHeld;
30666 pTo->xMutexNotheld = pFrom->xMutexNotheld;
30667 sqlite3MemoryBarrier();
30668 pTo->xMutexAlloc = pFrom->xMutexAlloc;
30669 }else{
30670 /* Another thread may have just installed the default methods above.
30671 ** This barrier pairs with the one prior to setting xMutexAlloc, so
30672 ** that the load of xMutexInit below cannot be satisfied before the
30673 ** load of xMutexAlloc on processors with weak memory ordering. */
30674 sqlite3MemoryBarrier();
30675 }
30676 assert( sqlite3GlobalConfig.mutex.xMutexInit );
30677 rc = sqlite3GlobalConfig.mutex.xMutexInit();
30678
30679 #ifdef SQLITE_DEBUG
@@ -38212,11 +38262,13 @@
38262 ** the right of the decimal point, or to a maximum of mxRound total
38263 ** significant digits.
38264 **
38265 ** If iRound>0 round to min(iRound,mxRound) significant digits total.
38266 **
38267 ** mxRound must be positive. iRound must not be less than -100000000
38268 ** (the SQLITE_FP_PRECISION_LIMIT maximum in printf.c) so that the
38269 ** computation of the number of significant digits cannot overflow.
38270 **
38271 ** The significant digits of the decimal representation are
38272 ** stored in p->z[] which is a often (but not always) a pointer
38273 ** into the middle of p->zBuf[]. There are p->n significant digits.
38274 ** The p->z[] array is *not* zero-terminated.
@@ -38229,10 +38281,11 @@
38281 char *zBuf; /* Local alias for p->zBuf */
38282 char *z; /* Local alias for p->z */
38283
38284 p->isSpecial = 0;
38285 assert( mxRound>0 );
38286 assert( iRound>=-100000000 );
38287
38288 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
38289 ** NaN. */
38290 if( r<0.0 ){
38291 p->sign = '-';
@@ -47295,10 +47348,13 @@
47348 */
47349 zName = unixTempFileDir();
47350 if( zName
47351 && (fd = robust_open(zName, O_RDWR|O_EXCL|O_TMPFILE, 0600))>=0
47352 ){
47353 if( pOutFlags ){
47354 *pOutFlags = flags;
47355 }
47356 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags|UNIXFILE_NOLOCK);
47357 goto open_finished;
47358 }
47359 #endif
47360 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
@@ -47816,11 +47872,16 @@
47872 if( microseconds>=1000000 ) sleep(microseconds/1000000);
47873 if( microseconds%1000000 ) usleep(microseconds%1000000);
47874 UNUSED_PARAMETER(NotUsed);
47875 return microseconds;
47876 #else
47877 int seconds;
47878 if( microseconds>2146000000 ){
47879 seconds = 2147;
47880 }else{
47881 seconds = (microseconds+999999)/1000000;
47882 }
47883 sleep(seconds);
47884 UNUSED_PARAMETER(NotUsed);
47885 return seconds*1000000;
47886 #endif
47887 }
@@ -55359,11 +55420,12 @@
55420 memdbLeave(pStore);
55421 return p;
55422 }
55423
55424 /*
55425 ** Return the serialization of a database. Or return NULL if there
55426 ** is an OOM or I/O error.
55427 */
55428 SQLITE_API unsigned char *sqlite3_serialize(
55429 sqlite3 *db, /* The database connection */
55430 const char *zSchema, /* Which database within the connection */
55431 sqlite3_int64 *piSize, /* Write size here, if not NULL */
@@ -55435,12 +55497,16 @@
55497 DbPage *pPage = 0;
55498 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
55499 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
55500 if( rc==SQLITE_OK ){
55501 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
55502 }else if( pgno==PENDING_BYTE/szPage+1 ){
55503 memset(pTo, 0, szPage);
55504 }else{
55505 sqlite3_free(pOut);
55506 pOut = 0;
55507 nPage = 0;
55508 }
55509 sqlite3PagerUnref(pPage);
55510 }
55511 }
55512 }
@@ -57089,38 +57155,66 @@
57155 ** When reading a corrupt database file, it is possible that SQLite might
57156 ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
57157 ** It will only read past the end of the page buffer, never write. This
57158 ** object is positioned immediately after the page buffer to serve as an
57159 ** overrun area, so that overreads are harmless.
57160 **
57161 ** Under some circumstances, using a strategically corrupt db file, one
57162 ** or two varints may be read from past the end of the page buffer and
57163 ** returned to the user (as rowid values). For this reason, we ensure that
57164 ** the first few elements of PgHdr1 are not pointers. If they were, then
57165 ** a determined attacker might be able to read the value of the pointer,
57166 ** which can be used as part of a larger exploit. Furthermore, because the
57167 ** first two elements of PgHdr1 are isBulkLocal and isAnchor - u16 variables
57168 ** that always contain either 1 or 0 - the PgHdr1 structure always begins
57169 ** with 4 bytes with the high-bit cleared, and so code attempting to read
57170 ** varints can never progress past this point and read any more sensitive
57171 ** information further inside the structure.
57172 **
57173 ** Variables isBulkLocal and isAnchor were once type "u8". That works,
57174 ** but causes a 2-byte gap in the structure for most architectures (since
57175 ** pointers must be either 4 or 8-byte aligned). As this structure is located
57176 ** in memory directly after the associated page data, if the database is
57177 ** corrupt, code at the b-tree layer may overread the page buffer and
57178 ** read part of this structure before the corruption is detected. This
57179 ** can cause a valgrind error if the uninitialized gap is accessed. Using u16
57180 ** ensures there is no such gap, and therefore no bytes of uninitialized
57181 ** memory in the structure. Keeping the u16 type also lets isBulkLocal,
57182 ** isAnchor and iKey fill exactly the 8 bytes (2+2+4) that precede the
57183 ** 8-byte-aligned "page" member, so no uninitialized padding is exposed to
57184 ** an overread.
57185 **
57186 ** The pLruNext and pLruPrev pointers form a double-linked circular list
57187 ** of all pages that are unpinned. The PGroup.lru element (which should be
57188 ** the only element on the list with PgHdr1.isAnchor set to 1) forms the
57189 ** beginning and the end of the list.
57190 */
57191 struct PgHdr1 {
 
 
57192 u16 isBulkLocal; /* This page from bulk local storage */
57193 u16 isAnchor; /* This is the PGroup.lru element */
57194 unsigned int iKey; /* Key value (page number) */
57195 sqlite3_pcache_page page; /* Base class. pBuf & pExtra. Deliberately not the
57196 ** first member - see the note above. */
57197 PgHdr1 *pNext; /* Next in hash table chain */
57198 PCache1 *pCache; /* Cache that currently owns this page */
57199 PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */
57200 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
57201 /* NB: pLruPrev is only valid if pLruNext!=0 */
57202 };
57203
57204 /*
57205 ** Given the sqlite3_pcache_page object handed out to the pcache.c layer,
57206 ** recover the PgHdr1 that contains it. PgHdr1.page is no longer required to
57207 ** be the first member of PgHdr1, so this goes through the "page" member
57208 ** rather than casting the structure pointer. The fetch methods produce the
57209 ** other direction directly with &pPage->page, only on paths where pPage is
57210 ** known to be non-NULL. PAGE_TO_PGHDR1() is likewise only ever given a
57211 ** non-NULL page that this module previously handed out.
57212 */
57213 #define PAGE_TO_PGHDR1(pPg) \
57214 ((PgHdr1*)(((char*)(pPg)) - offsetof(PgHdr1,page)))
57215
57216 /*
57217 ** A page is pinned if it is not on the LRU list. To be "pinned" means
57218 ** that the page is in active use and must not be deallocated.
57219 */
57220 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
@@ -57567,11 +57661,11 @@
57661 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
57662 ** LRU list, then this function is a no-op.
57663 **
57664 ** The PGroup mutex must be held when this function is called.
57665 */
57666 static sqlite3_pcache_page *pcache1PinPage(PgHdr1 *pPage){
57667 assert( pPage!=0 );
57668 assert( PAGE_IS_UNPINNED(pPage) );
57669 assert( pPage->pLruNext );
57670 assert( pPage->pLruPrev );
57671 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
@@ -57581,11 +57675,11 @@
57675 /* pPage->pLruPrev = 0;
57676 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
57677 assert( pPage->isAnchor==0 );
57678 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
57679 pPage->pCache->nRecyclable--;
57680 return &pPage->page;
57681 }
57682
57683
57684 /*
57685 ** Remove the page supplied as an argument from the hash table
@@ -57864,11 +57958,11 @@
57958 **
57959 ** This steps are broken out into a separate procedure because they are
57960 ** usually not needed, and by avoiding the stack initialization required
57961 ** for these steps, the main pcache1Fetch() procedure can run faster.
57962 */
57963 static SQLITE_NOINLINE sqlite3_pcache_page *pcache1FetchStage2(
57964 PCache1 *pCache,
57965 unsigned int iKey,
57966 int createFlag
57967 ){
57968 unsigned int nPinned;
@@ -57930,12 +58024,13 @@
58024 *(void **)pPage->page.pExtra = 0;
58025 pCache->apHash[h] = pPage;
58026 if( iKey>pCache->iMaxKey ){
58027 pCache->iMaxKey = iKey;
58028 }
58029 return &pPage->page;
58030 }
58031 return 0;
58032 }
58033
58034 /*
58035 ** Implementation of the sqlite3_pcache.xFetch method.
58036 **
@@ -57993,11 +58088,11 @@
58088 ** There are two versions of this routine. pcache1FetchWithMutex() is
58089 ** the general case. pcache1FetchNoMutex() is a faster implementation for
58090 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
58091 ** invokes the appropriate routine.
58092 */
58093 static sqlite3_pcache_page *pcache1FetchNoMutex(
58094 sqlite3_pcache *p,
58095 unsigned int iKey,
58096 int createFlag
58097 ){
58098 PCache1 *pCache = (PCache1 *)p;
@@ -58016,27 +58111,27 @@
58111 ** subsequent steps to try to create the page. */
58112 if( pPage ){
58113 if( PAGE_IS_UNPINNED(pPage) ){
58114 return pcache1PinPage(pPage);
58115 }else{
58116 return &pPage->page;
58117 }
58118 }else if( createFlag ){
58119 /* Steps 3, 4, and 5 implemented by this subroutine */
58120 return pcache1FetchStage2(pCache, iKey, createFlag);
58121 }else{
58122 return 0;
58123 }
58124 }
58125 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
58126 static sqlite3_pcache_page *pcache1FetchWithMutex(
58127 sqlite3_pcache *p,
58128 unsigned int iKey,
58129 int createFlag
58130 ){
58131 PCache1 *pCache = (PCache1 *)p;
58132 sqlite3_pcache_page *pPage;
58133
58134 pcache1EnterMutex(pCache->pGroup);
58135 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
58136 assert( pPage==0 || pCache->iMaxKey>=iKey );
58137 pcache1LeaveMutex(pCache->pGroup);
@@ -58050,23 +58145,22 @@
58145 ){
58146 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
58147 PCache1 *pCache = (PCache1 *)p;
58148 #endif
58149
 
58150 assert( pCache->bPurgeable || createFlag!=1 );
58151 assert( pCache->bPurgeable || pCache->nMin==0 );
58152 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
58153 assert( pCache->nMin==0 || pCache->bPurgeable );
58154 assert( pCache->nHash>0 );
58155 #if PCACHE1_MIGHT_USE_GROUP_MUTEX
58156 if( pCache->pGroup->mutex ){
58157 return pcache1FetchWithMutex(p, iKey, createFlag);
58158 }else
58159 #endif
58160 {
58161 return pcache1FetchNoMutex(p, iKey, createFlag);
58162 }
58163 }
58164
58165
58166 /*
@@ -58078,11 +58172,11 @@
58172 sqlite3_pcache *p,
58173 sqlite3_pcache_page *pPg,
58174 int reuseUnlikely
58175 ){
58176 PCache1 *pCache = (PCache1 *)p;
58177 PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
58178 PGroup *pGroup = pCache->pGroup;
58179
58180 assert( pPage->pCache==pCache );
58181 pcache1EnterMutex(pGroup);
58182
@@ -58114,20 +58208,20 @@
58208 sqlite3_pcache_page *pPg,
58209 unsigned int iOld,
58210 unsigned int iNew
58211 ){
58212 PCache1 *pCache = (PCache1 *)p;
58213 PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
58214 PgHdr1 **pp;
58215 unsigned int hOld, hNew;
58216 assert( pPage->iKey==iOld );
58217 assert( pPage->pCache==pCache );
58218 assert( iOld!=iNew ); /* The page number really is changing */
58219
58220 pcache1EnterMutex(pCache->pGroup);
58221
58222 assert( pcache1FetchNoMutex(p, iOld, 0)==&pPage->page ); /* pPg really iOld */
58223 assert( pCache->nHash>0 && (pCache->nHash & (pCache->nHash-1))==0 );
58224 hOld = iOld & (pCache->nHash-1);
58225 pp = &pCache->apHash[hOld];
58226 while( (*pp)!=pPage ){
58227 pp = &(*pp)->pNext;
@@ -68381,10 +68475,11 @@
68475 SEH_FREE_ON_ERROR(0, aFrame);
68476 if( !aFrame ){
68477 rc = SQLITE_NOMEM_BKPT;
68478 goto recovery_error;
68479 }
68480 memset(aFrame, 0, szFrame + WALINDEX_PGSZ);
68481 aData = &aFrame[WAL_FRAME_HDRSIZE];
68482 aPrivate = (u32*)&aData[szPage];
68483
68484 /* Read all frames from the log file. */
68485 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
@@ -69654,12 +69749,16 @@
69749 ** with a writer. So get a WRITE lock and try again.
69750 */
69751 if( badHdr ){
69752 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
69753 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
69754 /* Retry the header now that the WRITE_LOCK is held. If it still
69755 ** fails, the header really is corrupt and recovery is required.
69756 ** Otherwise, continue with the now valid header. */
69757 if( page0 ) badHdr = walIndexTryHdr(pWal, pChanged);
69758 walUnlockShared(pWal, WAL_WRITE_LOCK);
69759 if( badHdr ) rc = SQLITE_READONLY_RECOVERY;
69760 }
69761 }else{
69762 int bWriteLock = pWal->writeLock;
69763 if( bWriteLock
69764 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
@@ -70136,29 +70235,39 @@
70235 assert( thisMark!=READMARK_NOT_USED );
70236 mxReadMark = thisMark;
70237 mxI = i;
70238 }
70239 }
70240
70241 if( (pWal->readOnly & WAL_SHM_RDONLY)==0 ){
70242 if( mxReadMark<mxFrame || mxI==0 ){
70243 for(i=1; i<WAL_NREADER; i++){
70244 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
70245 if( rc==SQLITE_OK ){
70246 AtomicStore(pInfo->aReadMark+i,mxFrame);
70247 mxReadMark = mxFrame;
70248 mxI = i;
70249 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
70250 break;
70251 }else if( rc!=SQLITE_BUSY ){
70252 return rc;
70253 }
70254 }
70255 }
70256 }else if( mxI==0 ){
70257 /* If the loop above could not find a usable slot (mxI==0), then check
70258 ** if the wal-index header has been modified by another connection since
70259 ** it was read. If not, return SQLITE_READONLY_CANTINIT. If it has
70260 ** been modified, fall through and return WAL_RETRY. */
70261 walShmBarrier(pWal);
70262 if( 0==memcmp((void*)walIndexHdr(pWal),&pWal->hdr,sizeof(WalIndexHdr)) ){
70263 return SQLITE_READONLY_CANTINIT;
70264 }
70265 }
70266
70267 if( mxI==0 ){
70268 return WAL_RETRY;
 
70269 }
70270
70271 (void)walEnableBlockingMs(pWal, nBlockTmout);
70272 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
70273 walDisableBlocking(pWal);
@@ -74930,11 +75039,12 @@
75039 return SQLITE_OK;
75040 }
75041
75042 /*
75043 ** Do additional sanity check after btreeInitPage() if
75044 ** PRAGMA cell_size_check=ON. Raise a corruption error and
75045 ** leave the page uninitialized if any problems are found.
75046 */
75047 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
75048 int iCellFirst; /* First allowable cell or freeblock offset */
75049 int iCellLast; /* Last possible cell or freeblock offset */
75050 int i; /* Index into the cell pointer array */
@@ -74953,15 +75063,17 @@
75063 for(i=0; i<pPage->nCell; i++){
75064 pc = get2byteAligned(&data[cellOffset+i*2]);
75065 testcase( pc==iCellFirst );
75066 testcase( pc==iCellLast );
75067 if( pc<iCellFirst || pc>iCellLast ){
75068 pPage->isInit = 0;
75069 return SQLITE_CORRUPT_PAGE(pPage);
75070 }
75071 sz = pPage->xCellSize(pPage, &data[pc]);
75072 testcase( pc+sz==usableSize );
75073 if( pc+sz>usableSize ){
75074 pPage->isInit = 0;
75075 return SQLITE_CORRUPT_PAGE(pPage);
75076 }
75077 }
75078 return SQLITE_OK;
75079 }
@@ -82791,10 +82903,16 @@
82903 pCur->skipNext = -1;
82904 pCur->ix = pPage->nCell-1;
82905 }else{
82906 pCur->skipNext = 1;
82907 }
82908 /* The pCur->info structure is currently populated with the details
82909 ** of the cell that was just removed. But pCur->ix now points to a
82910 ** different cell in the b-tree. So zero info.nSize here so that if
82911 ** saveCursorPosition() is called on this cursor it does not trust
82912 ** the contents of pCur->info. */
82913 pCur->info.nSize = 0;
82914 }else{
82915 rc = moveToRoot(pCur);
82916 if( bPreserve ){
82917 btreeReleaseAllCursorPages(pCur);
82918 pCur->eState = CURSOR_REQUIRESEEK;
@@ -85989,12 +86107,32 @@
86107 }
86108
86109 /*
86110 ** The MEM structure is already a MEM_Real or MEM_IntReal. Try to
86111 ** make it a MEM_Int if we can.
86112 **
86113 ** This routine only converts REAL values into INTEGER if the REAL
86114 ** has no fractional part and if the value can be exactly expressed
86115 ** as a signed 64-bit integer.
86116 **
86117 ** The bFromText parameter is true if the MEM was originally text.
86118 ** If the value was originally text then this routine will not convert
86119 ** -9223372036854775808.0 into -9223372036854775808 because if the value
86120 ** was originally text, it would have been converted into an integer
86121 ** already, and since it was not, that means that the original text
86122 ** content had the last four significant digits greater than "5808"
86123 ** and the hence the text value was not really -9223372036854775808.0
86124 ** but rather some nearby value that happens to have the same IEEE 754
86125 ** representation. We don't want text '-9223372036854775900.0' to get
86126 ** converted into -9223372036854775808.
86127 **
86128 ** But if bFromText is false, then the value started out as REAL and
86129 ** so it is ok to convert -9223372036854775808.0 into -9223372036854775808
86130 ** in that case, because -9223372036854775808.0 is the exact value of
86131 ** the underlying IEEE 754 representation.
86132 */
86133 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem, int bFromText){
86134 assert( pMem!=0 );
86135 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
86136 assert( !sqlite3VdbeMemIsRowSet(pMem) );
86137 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
86138 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
@@ -86012,11 +86150,13 @@
86150 **
86151 ** The second and third terms in the following conditional enforces
86152 ** the second condition under the assumption that addition overflow causes
86153 ** values to wrap around.
86154 */
86155 if( pMem->u.r==ix && ix<LARGEST_INT64
86156 && (!bFromText || ix>SMALLEST_INT64)
86157 ){
86158 pMem->u.i = ix;
86159 MemSetTypeFlag(pMem, MEM_Int);
86160 }
86161 }
86162 }
@@ -86476,15 +86616,17 @@
86616 void (*xDel)(void*) /* Destructor function */
86617 ){
86618 i64 nByte = n; /* New value for pMem->n */
86619 int iLimit; /* Maximum allowed string or blob size */
86620 u16 flags; /* New value for pMem->flags */
86621 u8 xenc; /* Encoding without the ENC_KEEPBOM bit */
86622
86623 assert( pMem!=0 );
86624 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
86625 assert( !sqlite3VdbeMemIsRowSet(pMem) );
86626 xenc = enc & ENC_MASK;
86627 assert( xenc!=0 || n>=0 );
86628
86629 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
86630 if( !z ){
86631 sqlite3VdbeMemSetNull(pMem);
86632 return SQLITE_OK;
@@ -86494,20 +86636,20 @@
86636 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
86637 }else{
86638 iLimit = SQLITE_MAX_LENGTH;
86639 }
86640 if( nByte<0 ){
86641 assert( xenc!=0 );
86642 if( xenc==SQLITE_UTF8 ){
86643 nByte = strlen(z);
86644 }else{
86645 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
86646 }
86647 flags= MEM_Str|MEM_Term;
86648 }else if( xenc==0 ){
86649 flags = MEM_Blob;
86650 xenc = SQLITE_UTF8;
86651 }else{
86652 flags = MEM_Str;
86653 }
86654 if( nByte>iLimit ){
86655 if( xDel && xDel!=SQLITE_TRANSIENT ){
@@ -86526,11 +86668,11 @@
86668 ** management (one of MEM_Dyn or MEM_Static).
86669 */
86670 if( xDel==SQLITE_TRANSIENT ){
86671 i64 nAlloc = nByte;
86672 if( flags&MEM_Term ){
86673 nAlloc += (xenc==SQLITE_UTF8?1:2);
86674 }
86675 testcase( nAlloc==0 );
86676 testcase( nAlloc==31 );
86677 testcase( nAlloc==32 );
86678 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
@@ -86550,18 +86692,17 @@
86692 }
86693 }
86694
86695 pMem->n = (int)(nByte & 0x7fffffff);
86696 pMem->flags = flags;
86697 pMem->enc = xenc;
86698
86699 #ifndef SQLITE_OMIT_UTF16
86700 if( xenc>SQLITE_UTF8
86701 && (enc & ENC_KEEPBOM)==0
86702 && sqlite3VdbeMemHandleBom(pMem)
86703 ){
 
86704 return SQLITE_NOMEM_BKPT;
86705 }
86706 #endif
86707
86708
@@ -86675,14 +86816,14 @@
86816 return SQLITE_NOMEM_BKPT;
86817 }
86818 if( (u64)amt + (u64)offset > (u64)sqlite3BtreeMaxRecordSize(pCur) ){
86819 return SQLITE_CORRUPT_BKPT;
86820 }
86821 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+8)) ){
86822 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
86823 if( rc==SQLITE_OK ){
86824 memset(pMem->z+amt,0,8); /* Overrun area for malformed records */
86825 pMem->flags = MEM_Blob;
86826 pMem->n = (int)amt;
86827 }else{
86828 sqlite3VdbeMemRelease(pMem);
86829 }
@@ -87067,18 +87208,19 @@
87208 sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
87209 }else{
87210 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
87211 if( zVal==0 ) goto no_mem;
87212 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
87213 if( op==TK_FLOAT ){
87214 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
87215 sqlite3AtoF(pVal->z, &pVal->u.r);
87216 pVal->flags = MEM_Real;
87217 }
87218 }
87219 }
87220 if( affinity==SQLITE_AFF_BLOB ){
87221 if( op==TK_INTEGER ){
 
 
 
 
87222 /* This case is required by -9223372036854775808 and other strings
87223 ** that look like integers but cannot be handled by the
87224 ** sqlite3DecOrHexToI64() call above. */
87225 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
87226 }
@@ -93949,10 +94091,11 @@
94091 sqlite3_result_error_toobig(pCtx);
94092 return SQLITE_TOOBIG;
94093 }
94094 #ifndef SQLITE_OMIT_INCRBLOB
94095 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
94096 pOut->enc = pCtx->enc;
94097 return SQLITE_OK;
94098 #else
94099 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
94100 #endif
94101 }
@@ -95041,16 +95184,17 @@
95184 sqlite3_stmt *pStmt, /* The statement to bind against */
95185 int i, /* Index of the parameter to bind */
95186 const void *zData, /* Pointer to the data to be bound */
95187 i64 nData, /* Number of bytes of data to be bound */
95188 void (*xDel)(void*), /* Destructor for the data */
95189 u8 encoding /* Encoding for the data + optional ENC_KEEPBOM */
95190 ){
95191 Vdbe *p = (Vdbe *)pStmt;
95192 Mem *pVar;
95193 int rc;
95194
95195 assert( (encoding&ENC_KEEPBOM)==0 || (encoding&ENC_MASK)>SQLITE_UTF8 );
95196 rc = vdbeUnbind(p, (u32)(i-1));
95197 if( rc==SQLITE_OK ){
95198 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
95199 if( zData!=0 ){
95200 pVar = &p->aVar[i-1];
@@ -95253,12 +95397,13 @@
95397 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
95398 }
95399 break;
95400 }
95401 case SQLITE_TEXT: {
95402 assert( pValue->enc!=0 );
95403 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
95404 pValue->enc<=SQLITE_UTF8 ? pValue->enc : pValue->enc|ENC_KEEPBOM);
95405 break;
95406 }
95407 default: {
95408 rc = sqlite3_bind_null(pStmt, i);
95409 break;
@@ -95275,10 +95420,11 @@
95420 #ifndef SQLITE_OMIT_INCRBLOB
95421 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
95422 #else
95423 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
95424 #endif
95425 p->aVar[i-1].enc = ENC(p->db);
95426 sqlite3_mutex_leave(p->db->mutex);
95427 }
95428 return rc;
95429 }
95430 SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
@@ -95428,13 +95574,18 @@
95574 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
95575 /* No reprepare necessary */
95576 v->explain = eMode;
95577 rc = SQLITE_OK;
95578 }else{
95579 int savedMode = v->explain;
95580 v->explain = eMode;
95581 rc = sqlite3Reprepare(v);
95582 if( rc ){
95583 v->explain = savedMode;
95584 }else{
95585 v->haveEqpOps = eMode==2;
95586 }
95587 }
95588 if( v->explain ){
95589 v->nResColumn = 12 - 4*v->explain;
95590 }else{
95591 v->nResColumn = v->nResAlloc;
@@ -95622,14 +95773,15 @@
95773 u32 nRec;
95774 u8 *aRec;
95775
95776 assert( p->pCsr->eCurType==CURTYPE_BTREE );
95777 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
95778 aRec = sqlite3DbMallocRaw(db, (u64)nRec+8);
95779 if( !aRec ) goto preupdate_old_out;
95780 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
95781 if( rc==SQLITE_OK ){
95782 memset(aRec+nRec, 0, 8);
95783 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
95784 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
95785 }
95786 if( rc!=SQLITE_OK ){
95787 sqlite3DbFree(db, aRec);
@@ -96658,11 +96810,11 @@
96810 if( (rc&2)==0 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
96811 pRec->flags |= MEM_Int;
96812 }else{
96813 pRec->u.r = rValue;
96814 pRec->flags |= MEM_Real;
96815 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec, 1);
96816 }
96817 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
96818 ** string representation after computing a numeric equivalent, because the
96819 ** string representation might not be the canonical representation for the
96820 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
@@ -96702,11 +96854,11 @@
96854 || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM );
96855 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
96856 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
96857 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
96858 }else if( affinity<=SQLITE_AFF_REAL ){
96859 sqlite3VdbeIntegerAffinity(pRec, 0);
96860 }
96861 }
96862 }else if( affinity==SQLITE_AFF_TEXT ){
96863 /* Only attempt the conversion to TEXT if there is an integer or real
96864 ** representation (blob and NULL do not get converted) but no string
@@ -97092,22 +97244,18 @@
97244 pBuf = pCache->pCValue;
97245 }
97246 assert( t>=12 );
97247 sqlite3RCStrRef(pBuf);
97248 if( t&1 ){
97249 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding|ENC_KEEPBOM,
 
 
 
 
97250 sqlite3RCStrUnref);
97251 pDest->flags |= MEM_Term;
97252 }else{
97253 rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0,
97254 sqlite3RCStrUnref);
97255 pDest->enc = encoding;
97256 }
97257 }else{
97258 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
97259 if( rc ) return rc;
97260 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
97261 if( (t&1)!=0 && encoding==SQLITE_UTF8 ){
@@ -111096,10 +111244,13 @@
111244 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
111245 Upsert *pUpsert = pNC->uNC.pUpsert;
111246 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
111247 pTab = pUpsert->pUpsertSrc->a[0].pSTab;
111248 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
111249 if( IN_RENAME_OBJECT ){
111250 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
111251 }
111252 }
111253 }
111254 #endif /* SQLITE_OMIT_UPSERT */
111255
111256 if( pTab ){
@@ -112118,11 +112269,11 @@
112269 NameContext nc; /* Name context for resolving pE */
112270 sqlite3 *db; /* Database connection */
112271 int rc; /* Return code from subprocedures */
112272 u8 savedSuppErr; /* Saved value of db->suppressErr */
112273
112274 assert( sqlite3ExprIsInteger(pE, &i, 0, 1)==0 );
112275 pEList = pSelect->pEList;
112276
112277 /* Resolve all names in the ORDER BY term expression
112278 */
112279 memset(&nc, 0, sizeof(nc));
@@ -112217,11 +112368,11 @@
112368 int iCol = -1;
112369 Expr *pE, *pDup;
112370 if( pItem->fg.done ) continue;
112371 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
112372 if( NEVER(pE==0) ) continue;
112373 if( sqlite3ExprIsInteger(pE, &iCol, 0, 1) ){
112374 if( iCol<=0 || iCol>pEList->nExpr ){
112375 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
112376 return 1;
112377 }
112378 }else{
@@ -112400,11 +112551,11 @@
112551 ** copy of the iCol-th result-set expression. */
112552 pItem->u.x.iOrderByCol = (u16)iCol;
112553 continue;
112554 }
112555 }
112556 if( sqlite3ExprIsInteger(pE2, &iCol, 0, 1) ){
112557 /* The ORDER BY term is an integer constant. Again, set the column
112558 ** number so that sqlite3ResolveOrderGroupBy() will convert the
112559 ** order-by term to a copy of the result-set expression */
112560 if( iCol<1 || iCol>0xffff ){
112561 resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
@@ -114088,13 +114239,16 @@
114239 }else{
114240 u32 f = pLeft->flags | pRight->flags;
114241 if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse|EP_HasFunc))==EP_IsFalse
114242 && !IN_RENAME_OBJECT
114243 ){
114244 Expr *p;
114245 sqlite3ExprDeferredDelete(pParse, pLeft);
114246 sqlite3ExprDeferredDelete(pParse, pRight);
114247 p = sqlite3ExprInt32(db, 0);
114248 if( p ) p->op2 = 1;
114249 return p;
114250 }else{
114251 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
114252 }
114253 }
114254 }
@@ -115872,17 +116026,43 @@
116026 ** If pParse is not NULL and expression p is a variable, then the variable
116027 ** is marked so as to cause the statement to be reprepared each time a new
116028 ** value is bound to it. Except, if parameter bRSI is true, then the statement
116029 ** will only be reprepared if the rebind changes the value to or from a
116030 ** "small integer" (either 0 or 1).
116031 **
116032 ** If pParse is NULL but bRSI is non-zero, then to not accept the input
116033 ** as an integer if the TK_INTEGER opcode has its op2 field set to non-zero.
116034 ** A non-zero Expr.op2 indicates that the expression is a compile-time
116035 ** reduction of a constant expression. For example, "123 NOTNULL" would
116036 ** be rendered by the parser as a TK_INTEGER with value 1, but with op2 set.
116037 **
116038 ** Summary:
116039 **
116040 ** pParse bRsi What happens
116041 ** -------- ---- ------------------------------------------------
116042 ** NULL 0 Reject if TK_VARIABLE. Accept if op2!=0
116043 **
116044 ** NULL 1 Reject if TK_VARIABLE or op2!=0
116045 **
116046 ** not-NULL 0 Accept TK_VARIABLE and/or op2!=0 but for TK_VARIABLE,
116047 ** cause the statement to reprepare on *any*
116048 ** binding change
116049 **
116050 ** not-NULL 1 As previous, except reprepare only happens if a binding
116051 ** change move the variable value to or from an integer 0
116052 ** or 1.
116053 */
116054 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse, int bRSI){
116055 int iSign = 1; /* Either +1 or -1. */
116056 while( 1/*exit-by-break*/ ){
116057 if( NEVER(p==0) ) return 0;
116058 if( ExprUseUValue(p) ){
116059 assert( p->op==TK_INTEGER );
116060 *pValue = p->u.iValue*iSign;
116061 if( bRSI && pParse==0 && p->op2 ){
116062 return 0;
116063 }
116064 return 1;
116065 }
116066 if( p->op==TK_UPLUS ){
116067 p = p->pLeft;
116068 pParse = 0;
@@ -121456,17 +121636,18 @@
121636 " AND (type != 'index' OR tbl_name = %Q)",
121637 zDb,
121638 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
121639 pTab->zName
121640 );
121641 if( iSchema!=1 ){
121642 sqlite3NestedParse(pParse,
121643 "UPDATE temp." LEGACY_SCHEMA_TABLE " SET "
121644 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
121645 "WHERE type IN ('trigger', 'view')",
121646 zDb, pTab->zName, iCol, zNew, bQuote
121647 );
121648 }
121649
121650 /* Drop and reload the database schema. */
121651 renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename);
121652 renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1);
121653
@@ -123152,16 +123333,16 @@
123333 regOut = reg+1+nField;
123334 }
123335 if( i==pTab->iPKey ){
123336 sqlite3VdbeAddOp2(v, OP_Null, 0, regOut);
123337 }else{
 
 
 
 
123338 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut);
123339 assert( pParse->nErr
123340 || sqlite3VdbeGetOp(v,sqlite3VdbeCurrentAddr(v)-1)->opcode
123341 != OP_RealAffinity
123342 || pTab->aCol[i].affinity==SQLITE_AFF_REAL );
123343 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
123344 }
123345 nField++;
123346 }
123347 }
123348 if( nField==0 ){
@@ -123208,19 +123389,23 @@
123389 ** z points to the second token of the constraint definition. For an
123390 ** unnamed NOT NULL constraint, z points to the first byte past the NOT
123391 ** keyword.
123392 **
123393 ** Argument eTok may be the token value of the first token of the constraint
123394 ** (e.g. TK_CHECK or TK_REFERENCES or TK_NOT) or zero. If it is either
123395 ** TK_REFERENCES or TK_FOREIGN, special parsing is enabled to find the
123396 ** end of the foreign-key constraint definition. If eTok is TK_NOT then
123397 ** special processing is enabled to find the end of a NOT NULL constraint
123398 ** including its optional ON CONFLICT clause.
123399 **
123400 ** Return the number of bytes until the end of the constraint.
123401 */
123402 static int getConstraint(const u8 *z, int eTok){
123403 int iOff = 0;
123404 int t = 0;
123405 int t2 = 0;
123406 int prev = 0;
123407
123408 #ifndef SQLITE_OMIT_FOREIGN_KEY
123409 if( eTok==TK_FOREIGN ){
123410 /* For a FOREIGN KEY constraint, use getConstraint() to parse everything
123411 ** up to the REFERENCES keyword. Then getConstraintToken() to consume
@@ -123232,10 +123417,18 @@
123417
123418 if( eTok==TK_REFERENCES ){
123419 /* REFERENCES is followed by a table name. Gobble this up here in
123420 ** case the table name is a fallback token like TK_GENERATED. */
123421 iOff += getConstraintToken(&z[iOff], &t);
123422
123423 /* If the table names is followed by a parenthesized list of columns,
123424 ** also skip that, lest the close ")" end the constraint in the
123425 ** while loop below. */
123426 iOff += getWhitespace(&z[iOff]);
123427 if( z[iOff]=='(' ){
123428 iOff += getConstraintToken(&z[iOff], &t);
123429 }
123430 }
123431 #endif
123432
123433 /* Now, the current constraint proceeds until the next occurence of one
123434 ** of the following tokens:
@@ -123242,21 +123435,38 @@
123435 **
123436 ** CONSTRAINT, PRIMARY, NOT, UNIQUE, CHECK, DEFAULT,
123437 ** COLLATE, REFERENCES, FOREIGN, GENERATED, AS, RP, or COMMA
123438 **
123439 ** Also exit the loop if ILLEGAL turns up.
123440 **
123441 ** Exceptions:
123442 **
123443 ** * For NOT NULL constraints, also terminate at DEFERRABLE
123444 **
123445 ** * Do not terminate at DEFAULT if the previous token was SET
123446 */
123447 while( 1 ){
123448 int n = getConstraintToken(&z[iOff], &t);
123449 if( t==TK_CONSTRAINT || t==TK_PRIMARY || t==TK_UNIQUE
123450 || t==TK_CHECK || t==TK_COLLATE || t==TK_REFERENCES
123451 || (t==TK_DEFAULT && prev!=TK_SET)
123452 || t==TK_FOREIGN || t==TK_RP || t==TK_COMMA || t==TK_ILLEGAL
123453 || t==TK_AS || t==TK_GENERATED
123454 || (eTok==TK_NOT && t==TK_DEFERRABLE)
123455 ){
123456 break;
123457 }
123458 if( t==TK_NOT
123459 && (eTok!=TK_REFERENCES
123460 || (getConstraintToken(&z[iOff+n],&t2), t2!=TK_DEFERRABLE))
123461 ){
123462 /* NOT ends the constraint unless eTok is TK_REFERENCES and the next
123463 ** token is DEFERRABLE */
123464 break;
123465 }
123466 iOff += n;
123467 prev = t;
123468 }
123469
123470 return iOff;
123471 }
123472
@@ -123419,12 +123629,16 @@
123629 iEnd = iOff;
123630 break;
123631 }
123632
123633 }else if( t==TK_NOT && iNotNull==ii ){
123634 int t2;
123635 (void)getConstraintToken(zSql+iOff, &t2);
123636 if( t2==TK_NULL ){
123637 iEnd = iOff + getConstraint(zSql+iOff, TK_NOT);
123638 break;
123639 }
123640 }else if( t==TK_RP || t==TK_ILLEGAL ){
123641 iEnd = -1;
123642 break;
123643 }else if( t==TK_COMMA ){
123644 break;
@@ -136891,11 +137105,11 @@
137105 break;
137106 default:
137107 return;
137108 }
137109 if( argc==2 ){
137110 switch( sqlite3_value_numeric_type(argv[1]) ){
137111 case SQLITE_INTEGER:
137112 case SQLITE_FLOAT:
137113 b = log(x);
137114 if( b<=0.0 ) return;
137115 x = sqlite3_value_double(argv[1]);
@@ -137625,10 +137839,11 @@
137839 assert( argc==3 );
137840 x = sqlite3_value_double(argv[0]);
137841 y = sqlite3_value_int(argv[1]);
137842 z = sqlite3_value_int(argv[2]);
137843 if( z<=0 ) z = 1;
137844 if( y<-100000000 ) y = -100000000; /* Same limit as printf() */
137845 sqlite3FpDecode(&s, x, y, z);
137846 if( s.isSpecial==2 ){
137847 sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN");
137848 }else{
137849 sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP);
@@ -138446,10 +138661,148 @@
138661 pExpr->iTable = iCursor;
138662 pExpr->iColumn = iCol;
138663 }
138664 return pExpr;
138665 }
138666
138667 /*
138668 ** Column iCol1 of table p1 is to be compared to column iCol2 of table p2.
138669 ** Return non-zero if the comparison will use an affinity compatible
138670 ** with the affinity of column iCol1 of table p1. Or zero if it will not.
138671 **
138672 ** This is used in cases where (p1.iCol1 = p2.iCol2) must be evaluated
138673 ** using the affinity of p1.iCol1 as part of foreign key processing.
138674 */
138675 static int fkUsesLhsAffinity(Table *p1, int iCol1, Table *p2, int iCol2){
138676 u8 aff1 = iCol1>=0 ? p1->aCol[iCol1].affinity : SQLITE_AFF_NUMERIC;
138677 u8 aff2 = p2->aCol[iCol2].affinity;
138678
138679 /* If either side has a numeric affinity, then the comparison uses numeric
138680 ** affinity. So the affinity matches if aff1 is numeric, but not if aff1
138681 ** is not numeric but aff2 is. */
138682 if( sqlite3IsNumericAffinity(aff1) ) return 1;
138683 if( sqlite3IsNumericAffinity(aff2) ) return 0;
138684
138685 assert( aff1==SQLITE_AFF_TEXT || aff1==SQLITE_AFF_BLOB );
138686 assert( aff2==SQLITE_AFF_TEXT || aff2==SQLITE_AFF_BLOB );
138687
138688 /* At this point, either the affinities are the same or one is TEXT and
138689 ** the other BLOB. In the second case the comparison will be done with
138690 ** BLOB affinity, which does not match if aff1 is TEXT. */
138691 return (aff1==aff2 || aff1==SQLITE_AFF_BLOB);
138692 }
138693
138694 #ifndef SQLITE_OMIT_CAST
138695 /*
138696 ** Return non-zero if a lookup on column iCol of table pTab, using collation
138697 ** sequence zColl, can be done using either the rowid or an index. That is,
138698 ** if iCol is the INTEGER PRIMARY KEY of pTab, or if pTab has an index
138699 ** whose left-most column is iCol and whose collation sequence for that
138700 ** column is zColl. Return zero otherwise.
138701 */
138702 static int fkChildKeyIsIndexed(Table *pTab, int iCol, const char *zColl){
138703 Index *pIdx;
138704 if( iCol==pTab->iPKey ) return 1;
138705 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138706 if( pIdx->aiColumn[0]==iCol
138707 && pIdx->pPartIdxWhere==0
138708 && sqlite3StrICmp(pIdx->azColl[0], zColl)==0
138709 ){
138710 return 1;
138711 }
138712 }
138713 return 0;
138714 }
138715 #endif
138716
138717 /*
138718 ** Return an expression equivalent to (pLeft = pRight), where pLeft is a
138719 ** parent key value and pRight is an unresolved reference to column
138720 ** iChildCol of child table pChild. The comparison uses the affinity and
138721 ** collation sequence of column iCol of parent table pTab. pLeft must
138722 ** already carry that affinity and collation sequence. Ownership of
138723 ** both pLeft and pRight passes to this routine.
138724 **
138725 ** If the simple (pLeft = pRight) does not naturally use the affinity of
138726 ** the parent column, the comparison is coded as (pLeft = +pRight). But
138727 ** the unary + prevents any index on the child column from being used.
138728 ** So in that case, if the child column is indexed, an extra term is added:
138729 **
138730 ** (pLeft = +pRight) AND
138731 ** (pRight COLLATE <coll> IN (+pLeft, +CAST(pLeft AS NUMERIC)))
138732 **
138733 ** Each child value that matches pLeft under the affinity of the parent
138734 ** column is either equal to pLeft itself or numerically equal to
138735 ** CAST(pLeft AS NUMERIC). So the IN term matches a superset of the
138736 ** child rows that the exact (pLeft = +pRight) term does, and since it
138737 ** compares using the affinity of the child column, it may be used with
138738 ** an index on the child column. The unary + operators on the RHS of the
138739 ** IN ensure that no affinity other than that of the child column is
138740 ** applied.
138741 */
138742 static Expr *fkParentChildEq(
138743 Parse *pParse, /* Parsing context */
138744 Expr *pLeft, /* Parent key value */
138745 Expr *pRight, /* Unresolved reference to child key column */
138746 Table *pTab, /* Parent table */
138747 int iCol, /* Parent key column, or <0 for rowid */
138748 Table *pChild, /* Child table */
138749 int iChildCol, /* Child key column */
138750 int bComposite /* This is part of a composite FK */
138751 ){
138752 sqlite3 *db = pParse->db;
138753 Expr *pEq;
138754 #ifndef SQLITE_OMIT_CAST
138755 Expr *pIn = 0;
138756 #endif
138757
138758 if( fkUsesLhsAffinity(pTab, iCol, pChild, iChildCol) ){
138759 return sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
138760 }
138761 assert( iCol>=0 );
138762
138763 #ifndef SQLITE_OMIT_CAST
138764 {
138765 const char *zColl = sqlite3ColumnColl(&pTab->aCol[iCol]);
138766 if( zColl==0 ) zColl = sqlite3StrBINARY;
138767
138768 /* The IN term is only useful if it can drive a lookup on the child
138769 ** table. Omit it otherwise, so that a full scan of the child table
138770 ** does not have to evaluate it for every row. */
138771 if( bComposite || fkChildKeyIsIndexed(pChild, iChildCol, zColl) ){
138772 static const Token tNumeric = { "NUMERIC", 7 };
138773 Expr *pCast;
138774 ExprList *pList;
138775
138776 pCast = sqlite3ExprAlloc(db, TK_CAST, &tNumeric, 0);
138777 sqlite3ExprAttachSubtrees(db, pCast, sqlite3ExprDup(db, pLeft, 0), 0);
138778 pList = sqlite3ExprListAppend(pParse, 0,
138779 sqlite3PExpr(pParse, TK_UPLUS, sqlite3ExprDup(db, pLeft, 0), 0)
138780 );
138781 pList = sqlite3ExprListAppend(pParse, pList,
138782 sqlite3PExpr(pParse, TK_UPLUS, pCast, 0)
138783 );
138784 pIn = sqlite3PExpr(pParse, TK_IN, sqlite3ExprAddCollateString(pParse,
138785 sqlite3ExprDup(db, pRight, 0), zColl
138786 ), 0);
138787 if( pIn ){
138788 pIn->x.pList = pList;
138789 sqlite3ExprSetHeightAndFlags(pParse, pIn);
138790 }else{
138791 sqlite3ExprListDelete(db, pList);
138792 }
138793 }
138794 }
138795 #endif
138796
138797 pRight = sqlite3PExpr(pParse, TK_UPLUS, pRight, 0);
138798 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
138799 #ifndef SQLITE_OMIT_CAST
138800 pEq = sqlite3ExprAnd(pParse, pEq, pIn);
138801 #endif
138802 return pEq;
138803 }
138804
138805 /*
138806 ** This function is called to generate code executed when a row is deleted
138807 ** from the parent table of foreign key constraint pFKey and, if pFKey is
138808 ** deferred, when a row is inserted into the same table. When generating
@@ -138515,20 +138868,22 @@
138868 */
138869 for(i=0; i<pFKey->nCol; i++){
138870 Expr *pLeft; /* Value from parent table row */
138871 Expr *pRight; /* Column ref to child table */
138872 Expr *pEq; /* Expression (pLeft = pRight) */
138873 i16 iCol; /* Index of column in parent table */
138874 i16 iCol2; /* Index of column in child table */
138875 const char *zCol; /* Name of column in child table */
138876
138877 iCol = pIdx ? pIdx->aiColumn[i] : -1;
138878 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
138879 iCol2 = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
138880 assert( iCol2>=0 );
138881 zCol = pFKey->pFrom->aCol[iCol2].zCnName;
138882 pRight = sqlite3Expr(db, TK_ID, zCol);
138883 pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iCol,
138884 pFKey->pFrom, iCol2, pFKey->nCol>0);
138885 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
138886 }
138887
138888 /* If the child table is the same as the parent table, then add terms
138889 ** to the WHERE clause that prevent this entry from being scanned.
@@ -138784,17 +139139,16 @@
139139 ** Return true if the parser passed as the first argument is being
139140 ** used to code a trigger that is really a "SET NULL" action belonging
139141 ** to trigger pFKey.
139142 */
139143 static int isSetNullAction(Parse *pParse, FKey *pFKey){
139144 Trigger *p = pParse->pTrigCoded;
139145 if( p ){
 
139146 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
139147 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
139148 ){
139149 assert( (pParse->db->flags & SQLITE_FkNoAction)==0 );
139150 return 1;
139151 }
139152 }
139153 return 0;
139154 }
@@ -139184,30 +139538,39 @@
139538 Token tOld = { "old", 3 }; /* Literal "old" token */
139539 Token tNew = { "new", 3 }; /* Literal "new" token */
139540 Token tFromCol; /* Name of column in child table */
139541 Token tToCol; /* Name of column in parent table */
139542 int iFromCol; /* Idx of column in child table */
139543 int iToCol; /* Idx of column in parent table */
139544 Expr *pEq; /* OLD.tToCol = tFromCol */
139545 Expr *pLeft; /* OLD.tToCol */
139546 Expr *pRight; /* tFromCol */
139547
139548 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
139549 assert( iFromCol>=0 );
139550 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
139551 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
139552 iToCol = pIdx ? pIdx->aiColumn[i] : pTab->iPKey;
139553 sqlite3TokenInit(&tToCol, pTab->aCol[iToCol].zCnName);
139554 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
139555
139556 pLeft = sqlite3PExpr(pParse, TK_DOT,
139557 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
139558 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
 
 
 
 
 
 
139559 );
139560 pRight = sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0);
139561
139562 /* OLD.* columns do not normally have an affinity. So set the affinity
139563 ** of OLD.tToCol (the parent key) explicitly here. */
139564 if( pLeft ) pLeft->affExpr = pTab->aCol[iToCol].affinity;
139565
139566 /* Create the expression "OLD.tToCol = tFromCol". It is important
139567 ** that the "OLD.tToCol" term is on the LHS of the = operator, so
139568 ** that the collation sequence associated with the parent table is
139569 ** used for the comparison. */
139570 pEq = fkParentChildEq(pParse, pLeft, pRight, pTab, iToCol,
139571 pFKey->pFrom, iFromCol, pFKey->nCol>1);
139572 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
139573
139574 /* For ON UPDATE, construct the next term of the WHEN clause.
139575 ** The final WHEN clause will be like this:
139576 **
@@ -144652,29 +145015,36 @@
145015 ** was not on the list.
145016 */
145017 SQLITE_API int sqlite3_cancel_auto_extension(
145018 void (*xInit)(void)
145019 ){
145020 int n = 0;
145021 #ifndef SQLITE_OMIT_AUTOINIT
145022 if( sqlite3_initialize() ){
145023 return 0;
145024 }else
145025 #endif
145026 {
145027 #if SQLITE_THREADSAFE
145028 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
145029 #endif
145030 int i;
145031 wsdAutoextInit;
 
145032 #ifdef SQLITE_ENABLE_API_ARMOR
145033 if( xInit==0 ) return 0;
145034 #endif
145035 sqlite3_mutex_enter(mutex);
145036 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
145037 if( wsdAutoext.aExt[i]==xInit ){
145038 wsdAutoext.nExt--;
145039 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
145040 n++;
145041 break;
145042 }
145043 }
145044 sqlite3_mutex_leave(mutex);
145045 }
145046 return n;
145047 }
145048
145049 /*
145050 ** Reset the automatic extension loading mechanism.
@@ -147256,18 +147626,13 @@
147626 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
147627 }
147628 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
147629 1, 0, &iDataCur, &iIdxCur);
147630 /* reg[7] counts the number of entries in the table.
 
147631 */
147632 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
147633 assert( sqlite3NoTempsInRange(pParse,1,7) );
 
 
 
 
147634 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
147635 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
147636
147637 /* Fetch the right-most column from the table. This will cause
147638 ** the entire record header to be parsed and sanity checked. It
@@ -147476,11 +147841,10 @@
147841 int ckUniq = sqlite3VdbeMakeLabel(pParse);
147842 if( pPk==pIdx ) continue;
147843 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
147844 pPrior, r1);
147845 pPrior = pIdx;
 
147846 /* Verify that an index entry exists for the current table row */
147847 sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
147848 pIdx->nColumn); VdbeCoverage(v);
147849 jmp2 = sqlite3VdbeAddOp3(v, OP_IFindKey, iIdxCur+j, ckUniq, r1);
147850 VdbeCoverage(v);
@@ -151940,11 +152304,11 @@
152304 sqlite3HashInit(&ht);
152305 if( pEList ){
152306 nCol = pEList->nExpr;
152307 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
152308 testcase( aCol==0 );
152309 if( nCol>32767 ) nCol = 32767;
152310 }else{
152311 nCol = 0;
152312 aCol = 0;
152313 }
152314 assert( nCol==(i16)nCol );
@@ -153185,13 +153549,16 @@
153549 assert( pOrderBy );
153550 nOrderBy = pOrderBy->nExpr;
153551
153552 /* For operators other than UNION ALL we have to make sure that
153553 ** the ORDER BY clause covers every term of the result set. Add
153554 ** terms to the ORDER BY clause as necessary. Do the same for UNION ALL
153555 ** if any operator to the left is not UNION ALL, so that the ORDER BY
153556 ** carries the collation of every column into that sub-compound.
153557 */
153558 for(pPrior=p; pPrior && pPrior->op==TK_ALL; pPrior=pPrior->pPrior){}
153559 if( pPrior && pPrior->pPrior ){
153560 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
153561 struct ExprList_item *pItem;
153562 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
153563 assert( pItem!=0 );
153564 assert( pItem->u.x.iOrderByCol>0 );
@@ -153244,13 +153611,15 @@
153611 pParse->nMem += nExpr+1;
153612 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
153613 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
153614 if( pKeyDup ){
153615 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
153616 /* Use the ORDER BY collations, not multiSelectCollSeq(p), because
153617 ** p might be just one part of a larger compound, in which case
153618 ** the ORDER BY carries the COLLATE of the larger compound. */
153619 for(i=0; ALWAYS(pKeyMerge) && i<nOrderBy; i++){
153620 pKeyDup->aColl[pOrderBy->a[i].u.x.iOrderByCol-1] = pKeyMerge->aColl[i];
153621 }
153622 }
153623 }
153624
153625 /* Separate the left and the right query from one another
@@ -155303,20 +155672,20 @@
155672 *pNew = *p;
155673 p->pSrc = pNewSrc;
155674 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
155675 p->op = TK_SELECT;
155676 p->pWhere = 0;
155677 p->pGroupBy = 0;
155678 p->pHaving = 0;
155679 pNew->pOrderBy = 0;
155680 p->pPrior = 0;
155681 p->pNext = 0;
155682 p->pWith = 0;
155683 #ifndef SQLITE_OMIT_WINDOWFUNC
155684 p->pWinDefn = 0;
155685 #endif
155686 p->selFlags &= ~(u32)(SF_Compound|SF_Distinct);
155687 assert( (p->selFlags & SF_Converted)==0 );
155688 p->selFlags |= SF_Converted;
155689 assert( pNew->pPrior!=0 );
155690 pNew->pPrior->pNext = pNew;
155691 pNew->pLimit = 0;
@@ -155399,10 +155768,12 @@
155768 pParse->pWith = pWith;
155769 }
155770 }
155771 return pWith;
155772 }
155773
155774 static int selectExpanderWalk(Walker*, Select*);
155775
155776 /*
155777 ** This function checks to see if argument pFrom refers to a CTE declared by
155778 ** a WITH clause on the stack currently maintained by the parser (on the
155779 ** pParse->pWith linked list). And if currently processing a CTE expression,
@@ -155536,19 +155907,17 @@
155907 }
155908 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
155909 pRecTerm = pRecTerm->pPrior;
155910 }
155911
155912 if( pParse->nTab >= db->aLimit[SQLITE_LIMIT_CTES]
155913 && db->aLimit[SQLITE_LIMIT_CTES]>0
155914 ){
155915 sqlite3ErrorMsg(pParse, "too many CTEs");
155916 return 2;
155917 }
155918 #if SQLITE_MAX_EXPR_DEPTH>0
 
 
 
 
 
 
 
 
155919 pParse->nNestSel++;
155920 if( checkSubqueryNestingDepth(pParse) ) return 2;
155921 #endif
155922 pCte->zCteErr = "circular reference: %s";
155923 pSavedWith = pParse->pWith;
@@ -155566,11 +155935,11 @@
155935 if( rc ){
155936 pParse->pWith = pSavedWith;
155937 return 2;
155938 }
155939 }else{
155940 if( selectExpanderWalk(pWalker, pSel) ){
155941 pParse->pWith = pSavedWith;
155942 return 2;
155943 }
155944 }
155945 pParse->pWith = pWith;
@@ -155828,11 +156197,14 @@
156197 #endif
156198 nCol = pTab->nCol;
156199 pTab->nCol = -1;
156200 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
156201 if( pFrom->fg.isSubquery ){
156202 bft hasCompound = pParse->hasCompound;
156203 pParse->hasCompound = 1;
156204 selectExpanderWalk(pWalker, pFrom->u4.pSubq->pSelect);
156205 pParse->hasCompound = hasCompound;
156206 }
156207 pWalker->eCode = eCodeOrig;
156208 pTab->nCol = nCol;
156209 }
156210 #endif
@@ -156118,10 +156490,31 @@
156490 SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
156491 UNUSED_PARAMETER2(NotUsed, NotUsed2);
156492 assert( 0 );
156493 }
156494 #endif
156495
156496 /*
156497 ** The first argument is a Walker object created by sqlite3SelectExpand().
156498 ** This function uses the walker to expand the SELECT statement passed
156499 ** as the second argument. If necessary, it also converts any compound
156500 ** SELECT statements that have COLLATE clauses on ORDER BY expressions
156501 ** to sub-queries. Return the return value of sqlite3WalkSelect().
156502 */
156503 static int selectExpanderWalk(Walker *pWalker, Select *pSelect){
156504 assert( pWalker->xExprCallback==sqlite3ExprWalkNoop );
156505 assert( pWalker->pParse!=0 );
156506 if( OK_IF_ALWAYS_TRUE(pWalker->pParse->hasCompound) ){
156507 pWalker->xSelectCallback = convertCompoundSelectToSubquery;
156508 pWalker->xSelectCallback2 = 0;
156509 sqlite3WalkSelect(pWalker, pSelect);
156510 }
156511 pWalker->xSelectCallback = selectExpander;
156512 pWalker->xSelectCallback2 = sqlite3SelectPopWith;
156513 return sqlite3WalkSelect(pWalker, pSelect);
156514 }
156515
156516 /*
156517 ** This routine "expands" a SELECT statement and all of its subqueries.
156518 ** For additional information on what it means to "expand" a SELECT
156519 ** statement, see the comment on the selectExpand worker callback above.
156520 **
@@ -156135,19 +156528,12 @@
156528 */
156529 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
156530 Walker w;
156531 w.xExprCallback = sqlite3ExprWalkNoop;
156532 w.pParse = pParse;
 
 
 
 
 
 
 
156533 w.eCode = 0;
156534 selectExpanderWalk(&w, pSelect);
156535 }
156536
156537
156538 #ifndef SQLITE_OMIT_SUBQUERY
156539 /*
@@ -157868,15 +158254,25 @@
158254 /* This is a CTE for which materialization code has already been
158255 ** generated. Invoke the subroutine to compute the materialization,
158256 ** then make the pItem->iCursor be a copy of the ephemeral table that
158257 ** holds the result of the materialization. tag-select-0484 */
158258 CteUse *pCteUse = pItem->u2.pCteUse;
158259 /* The materialization is computed once per run of the statement or
158260 ** trigger program and does not change after that. So the subroutine
158261 ** call and the OP_OpenDup are needed on the first pass only. The
158262 ** OP_NullRow runs on every pass and leaves the duplicate cursor not
158263 ** positioned on a row, as OP_OpenDup does. */
158264 int addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
158265 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
158266 if( pItem->iCursor!=pCteUse->iCur ){
158267 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
158268 VdbeComment((v, "%!S", pItem));
158269 }
158270 sqlite3VdbeJumpHere(v, addrOnce);
158271 if( pItem->iCursor!=pCteUse->iCur ){
158272 sqlite3VdbeAddOp1(v, OP_NullRow, pItem->iCursor);
158273 }
158274 pSub->nSelectRow = pCteUse->nRowEst;
158275 }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){
158276 /* This view has already been materialized by a prior entry in
158277 ** this same FROM clause. Reuse it. tag-select-0486 */
158278 Subquery *pPriorSubq;
@@ -160259,10 +160655,11 @@
160655 sqlite3ParseObjectInit(&sSubParse, db);
160656 memset(&sNC, 0, sizeof(sNC));
160657 sNC.pParse = &sSubParse;
160658 sSubParse.pTriggerTab = pTab;
160659 sSubParse.pToplevel = pTop;
160660 sSubParse.pTrigCoded = pTrigger;
160661 sSubParse.zAuthContext = pTrigger->zName;
160662 sSubParse.eTriggerOp = pTrigger->op;
160663 sSubParse.nQueryLoop = pParse->nQueryLoop;
160664 sSubParse.prepFlags = pParse->prepFlags;
160665 sSubParse.oldmask = 0;
@@ -161073,14 +161470,18 @@
161470 {
161471 int rc;
161472 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
161473 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
161474 db->aDb[iDb].zDbSName);
161475 if( rc!=SQLITE_OK ){
161476 if( rc==SQLITE_DENY ){
161477 goto update_cleanup;
161478 }else if( j<0 || j==pTab->iPKey ){
161479 chngRowid = 0;
161480 }else{
161481 aXRef[j] = -1;
161482 }
161483 }
161484 }
161485 #endif
161486 }
161487 assert( (chngRowid & chngPk)==0 );
@@ -163496,11 +163897,21 @@
163897 /* Justification of ALWAYS(): The xConstructor method is required to
163898 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
163899 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
163900 rc = growVTrans(db);
163901 if( rc==SQLITE_OK ){
163902 VTable *pVTab = sqlite3GetVTable(db, pTab);
163903 const sqlite3_module *pModule = pMod->pModule;
163904 int iSvpt = db->nStatement + db->nSavepoint;
163905 addToVTrans(db, pVTab);
163906 /* As in sqlite3VtabBegin(): open the current savepoint on the new
163907 ** table, so that a ROLLBACK TO that undoes the CREATE also invokes
163908 ** its xRollbackTo method. */
163909 if( iSvpt && pModule->iVersion>=2 && pModule->xSavepoint ){
163910 pVTab->iSavepoint = iSvpt;
163911 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
163912 }
163913 }
163914 }
163915
163916 return rc;
163917 }
@@ -165382,10 +165793,13 @@
165793 pOrigLhs->a[iField].pExpr = 0;
165794 }
165795 nRhs++;
165796 }
165797 }
165798 if( nRhs<pOrigRhs->nExpr ){
165799 ExprClearProperty(pNew, EP_Subrtn); /* See bug 2026-10-16T22:07:15Z */
165800 }
165801 sqlite3ExprListDelete(db, pOrigRhs);
165802 if( pOrigLhs ){
165803 sqlite3ExprListDelete(db, pOrigLhs);
165804 pNew->pLeft->x.pList = pLhs;
165805 }
@@ -166907,11 +167321,13 @@
167321 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
167322 SWAP(u8, bSeekPastNull, bStopAtNull);
167323 SWAP(u8, nBtm, nTop);
167324 }
167325
167326 if( (iLevel>0 || (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0)
167327 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0
167328 ){
167329 /* In case OP_SeekScan is used, ensure that the index cursor does not
167330 ** point to a valid row for the first iteration of this loop. */
167331 sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
167332 }
167333
@@ -168887,11 +169303,11 @@
169303 if( pLeft==0 ){
169304 pNew = 0; /* Collating sequence mismatch */
169305 }else{
169306 pDup = sqlite3ExprDup(db, pLeft, 0);
169307 if( sqlite3ExprCollSeq(pParse, pDup)!=pCollSeq
169308 && pCollSeq!=0
169309 ){
169310 assert( pCollSeq->zName!=0 );
169311 pDup = sqlite3ExprAddCollateString(pParse, pDup, pCollSeq->zName);
169312 }
169313 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
@@ -173972,11 +174388,11 @@
174388 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
174389 SrcList *pTabList; /* The FROM clause */
174390 SrcItem *pSrc; /* The FROM clause btree term to add */
174391 WhereLoop *pNew; /* Template WhereLoop object */
174392 int rc = SQLITE_OK; /* Return code */
174393 int iSortIdx; /* Index number */
174394 int b; /* A boolean value */
174395 LogEst rSize; /* number of rows in the table */
174396 WhereClause *pWC; /* The parsed WHERE clause */
174397 Table *pTab; /* Table being queried */
174398
@@ -173991,11 +174407,15 @@
174407 if( pSrc->fg.isIndexedBy ){
174408 assert( pSrc->fg.isCte==0 );
174409 /* An INDEXED BY clause specifies a particular index to use */
174410 pProbe = pSrc->u2.pIBIndex;
174411 }else if( !HasRowid(pTab) ){
174412 if( pSrc->fg.notIndexed ){
174413 pProbe = sqlite3PrimaryKeyIndex(pTab);
174414 }else{
174415 pProbe = pTab->pIndex;
174416 }
174417 }else{
174418 /* There is no INDEXED BY clause. Create a fake Index object in local
174419 ** variable sPk to represent the rowid primary key index. Make this
174420 ** fake index the first in a chain of Index objects with all of the real
174421 ** indices to follow */
@@ -174010,15 +174430,11 @@
174430 sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */
174431 sPk.idxType = SQLITE_IDXTYPE_IPK;
174432 aiRowEstPk[0] = pTab->nRowLogEst;
174433 aiRowEstPk[1] = 0;
174434 pFirst = pSrc->pSTab->pIndex;
174435 sPk.pNext = pFirst;
 
 
 
 
174436 pProbe = &sPk;
174437 }
174438 rSize = pTab->nRowLogEst;
174439
174440 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
@@ -174076,12 +174492,14 @@
174492 }
174493 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
174494
174495 /* Loop over all indices. If there was an INDEXED BY clause, then only
174496 ** consider index pProbe. */
174497 for(iSortIdx=1;
174498 rc==SQLITE_OK && pProbe;
174499 pProbe=(pSrc->fg.isIndexedBy||pSrc->fg.notIndexed)?0:pProbe->pNext,
174500 iSortIdx++
174501 ){
174502 if( pProbe->pPartIdxWhere!=0
174503 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
174504 pProbe->pPartIdxWhere)
174505 ){
@@ -174797,10 +175215,12 @@
175215 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
175216
175217 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
175218 if( (pTerm->eOperator & WO_OR)!=0
175219 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
175220 && (!ExprHasProperty(pTerm->pExpr, EP_OuterON)
175221 || pTerm->pExpr->w.iJoin==iCur)
175222 ){
175223 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
175224 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
175225 WhereTerm *pOrTerm;
175226 int once = 1;
@@ -178662,15 +179082,20 @@
179082 assert( p!=0 );
179083 assert( p->pWin!=0 );
179084
179085 /* If this function is being called from within a scalar sub-select
179086 ** that used by the SELECT statement being processed, only process
179087 ** TK_COLUMN and TK_AGGREGATE expressions that refer to it (the outer
179088 ** SELECT). Do not process window functions at all, as they belong to the
179089 ** scalar sub-select. */
179090 if( p->pSubSelect ){
179091 if( pExpr->op==TK_AGG_FUNCTION ){
179092 /* If op2==walkerDepth, then this expression aggregates over the rows
179093 ** of the SELECT being processed, even though it is within a scalar
179094 ** sub-select. In that case proceed, otherwise skip this node. */
179095 if( pExpr->op2!=pWalker->walkerDepth ) return WRC_Continue;
179096 }else if( pExpr->op!=TK_COLUMN ){
179097 return WRC_Continue;
179098 }else{
179099 int nSrc = p->pSrc->nSrc;
179100 int i;
179101 for(i=0; i<nSrc; i++){
@@ -178747,11 +179172,13 @@
179172 Select *pSave = p->pSubSelect;
179173 if( pSave==pSelect ){
179174 return WRC_Continue;
179175 }else{
179176 p->pSubSelect = pSelect;
179177 pWalker->walkerDepth++;
179178 sqlite3WalkSelect(pWalker, pSelect);
179179 pWalker->walkerDepth--;
179180 p->pSubSelect = pSave;
179181 }
179182 return WRC_Prune;
179183 }
179184
@@ -178851,12 +179278,19 @@
179278 pExpr->op2++;
179279 }
179280 return WRC_Continue;
179281 }
179282
179283 /*
179284 ** Walker callback used to detect aggregate functions in the ORDER BY
179285 ** clause of an otherwise non-aggregate SELECT.
179286 */
179287 static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){
179288 if( pExpr->op==TK_AGG_FUNCTION
179289 && pExpr->pAggInfo==0
179290 && pExpr->op2==pWalker->walkerDepth
179291 ){
179292 assert( !ExprHasProperty(pExpr, EP_IntValue) );
179293 sqlite3ErrorMsg(pWalker->pParse,
179294 "misuse of aggregate: %s()", pExpr->u.zToken);
179295 }
179296 return WRC_Continue;
@@ -178899,11 +179333,13 @@
179333 }
179334 sqlite3AggInfoPersistWalkerInit(&w, pParse);
179335 sqlite3WalkSelect(&w, p);
179336 if( (p->selFlags & SF_Aggregate)==0 ){
179337 w.xExprCallback = disallowAggregatesInOrderByCb;
179338 w.xSelectCallback = sqlite3WalkerDepthIncrease;
179339 w.xSelectCallback2 = sqlite3WalkerDepthDecrease;
179340 w.walkerDepth = 0;
179341 sqlite3WalkExprList(&w, p->pOrderBy);
179342 }
179343
179344 p->pSrc = 0;
179345 p->pWhere = 0;
@@ -181273,11 +181709,13 @@
181709 case TK_INTEGER:
181710 case TK_STRING:
181711 case TK_FLOAT:
181712 case TK_BLOB:
181713 sqlite3ExprDeferredDelete(pParse, pLeft);
181714 p = sqlite3ExprInt32(pParse->db, op==TK_NOTNULL);
181715 if( p ) p->op2 = 1;
181716 return p;
181717 default:
181718 break;
181719 }
181720 return sqlite3PExpr(pParse, op, pLeft, 0);
181721 }
@@ -191632,10 +192070,11 @@
192070 SQLITE_MAX_TRIGGER_DEPTH,
192071 SQLITE_MAX_WORKER_THREADS,
192072 SQLITE_MAX_PARSER_DEPTH,
192073 SQLITE_MAX_SCHEMA,
192074 SQLITE_MAX_TRIGGER_STEPS,
192075 SQLITE_MAX_CTES,
192076 };
192077
192078 /*
192079 ** Make sure the hard limits are set to reasonable values
192080 */
@@ -191719,11 +192158,12 @@
192158 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
192159 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
192160 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
192161 assert( aHardLimit[SQLITE_LIMIT_SCHEMA]==SQLITE_MAX_SCHEMA );
192162 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_STEPS]==SQLITE_MAX_TRIGGER_STEPS );
192163 assert( aHardLimit[SQLITE_LIMIT_CTES]==SQLITE_MAX_CTES );
192164 assert( SQLITE_LIMIT_CTES==(SQLITE_N_LIMIT-1) );
192165
192166
192167 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
192168 return -1;
192169 }
@@ -192834,13 +193274,18 @@
193274 int rc;
193275 pVfs = sqlite3_vfs_find(0);
193276 if( pVfs==0 ) return 0;
193277
193278 /* This function works in milliseconds, but the underlying OsSleep()
193279 ** API uses microseconds. Hence the range limitation and the "*1000".
193280 */
193281 if( ms<0 ){
193282 ms = 0;
193283 }else if( ms>2147483 ){
193284 ms = 2147483; /* Maximum sleep time of 35 minutes and 47.484 seconds */
193285 }
193286 rc = sqlite3OsSleep(pVfs, ms*1000)/1000;
193287 return rc;
193288 }
193289
193290 /*
193291 ** Enable or disable the extended result codes.
@@ -217559,11 +218004,16 @@
218004 for(iIn=iOut=0; iIn<sz; iIn++){
218005 char c = z[iIn];
218006 if( c=='\\' ){
218007 u32 v;
218008 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
218009 if( v==0 && iIn+szEscape==sz
218010 && (z[iIn+1]=='\n' || z[iIn+1]=='\r' || (u8)z[iIn+1]==0xe2)
218011 ){
218012 /* A JSON5 line continuation at the end of the string
218013 ** contributes no characters */
218014 }else if( v<=0x7f ){
218015 zOut[iOut++] = (char)v;
218016 }else if( v<=0x7ff ){
218017 assert( szEscape>=2 );
218018 zOut[iOut++] = (char)(0xc0 | (v>>6));
218019 zOut[iOut++] = 0x80 | (v&0x3f);
@@ -221778,10 +222228,14 @@
222228
222229 if( rc ) return rc;
222230 if( NEVER(p==0) ) return SQLITE_OK;
222231 if( p->iCell>=NCELL(pNode) ) return SQLITE_ABORT;
222232 if( i==0 ){
222233 /* If this is an UPDATE that does not assign a value to the first
222234 ** column, leave it as a "no-change" value. This allows rtreeUpdate()
222235 ** to take the new rowid from aData[1] instead. */
222236 if( sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
222237 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
222238 }else if( i<=pRtree->nDim2 ){
222239 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
222240 #ifndef SQLITE_RTREE_INT_ONLY
222241 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
@@ -221875,10 +222329,24 @@
222329 pCons->pInfo = pInfo;
222330 return SQLITE_OK;
222331 }
222332
222333 SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
222334
222335 /*
222336 ** Argument pVal is the right-hand side of a "rowid = ?" constraint. If
222337 ** there is an integer that compares equal to pVal, set *piRowid to it and
222338 ** return 1. Otherwise - pVal is NULL, a blob, a non-numeric text value or
222339 ** a real value that is not an integer - return 0.
222340 */
222341 static int rtreeRowidEqValue(sqlite3_value *pVal, i64 *piRowid){
222342 int eType = sqlite3_value_numeric_type(pVal);
222343 *piRowid = sqlite3_value_int64(pVal);
222344 return eType==SQLITE_INTEGER
222345 || (eType==SQLITE_FLOAT
222346 && 0==sqlite3IntFloatCompare(*piRowid, sqlite3_value_double(pVal)));
222347 }
222348
222349 /*
222350 ** Rtree virtual table module xFilter method.
222351 */
222352 static int rtreeFilter(
@@ -221901,17 +222369,13 @@
222369 pCsr->iStrategy = idxNum;
222370 if( idxNum==1 ){
222371 /* Special case - lookup by rowid. */
222372 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
222373 RtreeSearchPoint *p; /* Search point for the leaf */
222374 i64 iRowid;
222375 i64 iNode = 0;
222376 if( rtreeRowidEqValue(argv[0], &iRowid) ){
 
 
 
 
222377 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
222378 }else{
222379 rc = SQLITE_OK;
222380 pLeaf = 0;
222381 }
@@ -223164,10 +223628,11 @@
223628 ** conflict-handling mode specified by the user.
223629 */
223630 if( nData>1 ){
223631 int ii;
223632 int nn = nData - 4;
223633 sqlite3_value *pNewRowid; /* Value of the new rowid, or NULL */
223634
223635 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
223636 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
223637 **
223638 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
@@ -223197,15 +223662,29 @@
223662 rc = rtreeConstraintError(pRtree, ii+1);
223663 goto constraint;
223664 }
223665 }
223666 }
223667
223668 /* The new rowid is normally the value of the first column, aData[2].
223669 ** But SQLite does not treat that column as an alias for the rowid,
223670 ** so a new rowid assigned using the name "rowid", "oid" or "_rowid_"
223671 ** is passed in aData[1]. Use aData[1] if aData[2] is a "no-change"
223672 ** value (an UPDATE that does not assign the first column - see
223673 ** rtreeColumn()), or if aData[2] is NULL in an INSERT. */
223674 pNewRowid = aData[2];
223675 if( sqlite3_value_nochange(pNewRowid)
223676 || (sqlite3_value_type(aData[0])==SQLITE_NULL
223677 && sqlite3_value_type(pNewRowid)==SQLITE_NULL)
223678 ){
223679 pNewRowid = aData[1];
223680 }
223681
223682 /* If a rowid value was supplied, check if it is already present in
223683 ** the table. If so, the constraint has failed. */
223684 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL ){
223685 cell.iRowid = sqlite3_value_int64(pNewRowid);
223686 if( sqlite3_value_type(aData[0])==SQLITE_NULL
223687 || sqlite3_value_int64(aData[0])!=cell.iRowid
223688 ){
223689 int steprc;
223690 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
@@ -224621,10 +225100,12 @@
225100 return pOut;
225101 }else{
225102 s.nErr++;
225103 rc = SQLITE_ERROR;
225104 }
225105 }else{
225106 rc = SQLITE_ERROR;
225107 }
225108 parse_json_err:
225109 if( pRc ) *pRc = rc;
225110 sqlite3_free(s.a);
225111 return 0;
@@ -224671,13 +225152,15 @@
225152 geopolySwab32((unsigned char*)&GeoX(p,ii));
225153 geopolySwab32((unsigned char*)&GeoY(p,ii));
225154 }
225155 p->hdr[0] ^= 1;
225156 }
225157 if( pRc ) *pRc = SQLITE_OK;
225158 }
225159 }else{
225160 if( pRc ) *pRc = SQLITE_ERROR;
225161 }
 
225162 return p;
225163 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
225164 const unsigned char *zJson = sqlite3_value_text(pVal);
225165 if( zJson==0 ){
225166 if( pRc ) *pRc = SQLITE_NOMEM;
@@ -225728,13 +226211,17 @@
226211 pCsr->iStrategy = idxNum;
226212 if( idxNum==1 ){
226213 /* Special case - lookup by rowid. */
226214 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
226215 RtreeSearchPoint *p; /* Search point for the leaf */
226216 i64 iRowid;
226217 i64 iNode = 0;
226218 if( rtreeRowidEqValue(argv[0], &iRowid) ){
226219 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
226220 }else{
226221 pLeaf = 0;
226222 }
226223 if( rc==SQLITE_OK && pLeaf!=0 ){
226224 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
226225 assert( p!=0 ); /* Always returns pCsr->sPoint */
226226 pCsr->aNode[0] = pLeaf;
226227 p->id = iNode;
@@ -225960,10 +226447,12 @@
226447 i64 oldRowid; /* The old rowid */
226448 int oldRowidValid; /* True if oldRowid is valid */
226449 i64 newRowid; /* The new rowid */
226450 int newRowidValid; /* True if newRowid is valid */
226451 int coordChange = 0; /* Change in coordinates */
226452 sqlite3_value *pShape = 0; /* New _shape value */
226453 sqlite3_value *pOldShape = 0; /* Copy of the unchanged _shape */
226454
226455 if( pRtree->nNodeRef ){
226456 /* Unable to write to the btree while another cursor is reading from it,
226457 ** since the write might do a rebalance which would disrupt the read
226458 ** cursor. */
@@ -225975,18 +226464,43 @@
226464 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
226465 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
226466 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
226467 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
226468 cell.iRowid = newRowid;
226469
226470 if( nData>1 ){
226471 pShape = aData[2];
226472 if( oldRowidValid && oldRowid!=newRowid && sqlite3_value_nochange(pShape) ){
226473 /* The rowid changes but _shape does not. aData[2] holds no value in
226474 ** this case, so read the current _shape from the %_rowid table. It
226475 ** is needed for the bounding box of the new r-tree entry and for the
226476 ** %_rowid record of the new rowid. */
226477 sqlite3_stmt *pRead = 0;
226478 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
226479 &pRead, 0);
226480 if( rc==SQLITE_OK ){
226481 int rc2;
226482 sqlite3_bind_int64(pRead, 1, oldRowid);
226483 if( sqlite3_step(pRead)==SQLITE_ROW ){
226484 pOldShape = sqlite3_value_dup(sqlite3_column_value(pRead, 2));
226485 if( pOldShape==0 ) rc = SQLITE_NOMEM;
226486 pShape = pOldShape;
226487 }
226488 rc2 = sqlite3_finalize(pRead);
226489 if( rc==SQLITE_OK ) rc = rc2;
226490 }
226491 if( rc ) goto geopoly_update_end;
226492 }
226493 }
226494
226495 if( nData>1 /* not a DELETE */
226496 && (!oldRowidValid /* INSERT */
226497 || !sqlite3_value_nochange(pShape) /* UPDATE _shape */
226498 || oldRowid!=newRowid) /* Rowid change */
226499 ){
226500 assert( pShape!=0 );
226501 geopolyBBox(0, pShape, cell.aCoord, &rc);
226502 if( rc ){
226503 if( rc==SQLITE_ERROR ){
226504 pVtab->zErrMsg =
226505 sqlite3_mprintf("_shape does not contain a valid polygon");
226506 }
@@ -226048,21 +226562,21 @@
226562 sqlite3_stmt *pUp = pRtree->pWriteAux;
226563 int jj;
226564 int nChange = 0;
226565 sqlite3_bind_int64(pUp, 1, cell.iRowid);
226566 assert( pRtree->nAux>=1 );
226567 if( sqlite3_value_nochange(pShape) ){
226568 sqlite3_bind_null(pUp, 2);
226569 }else{
226570 GeoPoly *p = 0;
226571 if( sqlite3_value_type(pShape)==SQLITE_TEXT
226572 && (p = geopolyFuncParam(0, pShape, &rc))!=0
226573 && rc==SQLITE_OK
226574 ){
226575 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
226576 }else{
226577 sqlite3_bind_value(pUp, 2, pShape);
226578 }
226579 sqlite3_free(p);
226580 nChange = 1;
226581 }
226582 for(jj=1; jj<nData-2; jj++){
@@ -226074,10 +226588,11 @@
226588 rc = sqlite3_reset(pUp);
226589 }
226590 }
226591
226592 geopoly_update_end:
226593 sqlite3_value_free(pOldShape);
226594 rtreeRelease(pRtree);
226595 return rc;
226596 }
226597
226598 /*
@@ -230763,11 +231278,11 @@
231278 int bOpen = 0;
231279 int rc;
231280 p->nRbu = 0;
231281 p->pRbuFd = 0;
231282 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
231283 if( rc!=SQLITE_NOTFOUND || p->pRbuFd==0 ) p->rc = rc;
231284 if( p->eStage>=RBU_STAGE_MOVE ){
231285 bOpen = 1;
231286 }else{
231287 RbuState *pState = rbuLoadState(p);
231288 if( pState ){
@@ -235257,11 +235772,11 @@
235772 for(i=0; i<nData; i++){
235773 sz += ((struct iovec*)aData)[i].iov_len;
235774 }
235775 }
235776
235777 pNew->aData = sqlite3_malloc64( sz>0 ? sz : 1 );
235778 if( pNew->aData==0 ){
235779 rc = SQLITE_NOMEM;
235780 goto carray_bind_error;
235781 }
235782
@@ -235288,11 +235803,11 @@
235803 p[i].iov_len = n;
235804 p[i].iov_base = z;
235805 z += n;
235806 memcpy(p[i].iov_base, ((struct iovec*)aData)[i].iov_base, n);
235807 }
235808 }else if( sz>0 ){
235809 memcpy(pNew->aData, aData, sz);
235810 }
235811 pNew->xDel = sqlite3_free;
235812 pNew->pDel = pNew->aData;
235813 }else{
@@ -241551,11 +242066,11 @@
242066 }
242067 pNew->nRecord = pOut - pNew->aRecord;
242068 }
242069 }else if( bRebase ){
242070 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
242071 pNew = pExist;
242072 }else{
242073 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
242074 pNew = (SessionChange*)sqlite3_malloc64(nByte);
242075 if( pNew==0 ){
242076 rc = SQLITE_NOMEM;
@@ -242404,12 +242919,12 @@
242919 }
242920 if( bData ){
242921 a2 = aChange;
242922 for(i=0; i<pIter->nCol; i++){
242923 int n1 = sessionSerialLen(a1);
242924 int n2 = (a2>=a2Eof) ? 0 : sessionSerialLen(a2);
242925 if( pIter->abPK[i] || n2<=0 || a2[0]!=0xFF ){
242926 memcpy(pOut, a1, n1);
242927 pOut += n1;
242928 }else{
242929 *pOut++ = '\0';
242930 }
@@ -242463,10 +242978,13 @@
242978 bNew = 0;
242979
242980 /* A patchset may not be rebased */
242981 if( pIter->bPatchset ){
242982 rc = SQLITE_ERROR;
242983 }
242984 if( pTab && pTab->nCol>pIter->nCol ){
242985 rc = SQLITE_SCHEMA;
242986 }
242987
242988 /* Append a table header to the output for this new table */
242989 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
242990 sessionAppendVarint(&sOut, pIter->nCol, &rc);
@@ -242560,10 +243078,11 @@
243078 *pnOut = sOut.nBuf;
243079 sOut.aBuf = 0;
243080 }
243081 }
243082 sqlite3_free(sOut.aBuf);
243083 if( rc==SQLITE_OK ) rc = pIter->rc;
243084 return rc;
243085 }
243086
243087 /*
243088 ** Create a new rebaser object.
@@ -247651,10 +248170,13 @@
248170 p++;
248171 while( p ){
248172 if( *p=='\'' ){
248173 p++;
248174 if( *p!='\'' ) break;
248175 }else if( *p==0 ){
248176 p = 0;
248177 break;
248178 }
248179 p++;
248180 if( *p==0 ) p = 0;
248181 }
248182 break;
@@ -249075,11 +249597,11 @@
249597 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
249598 }
249599 zExpr[iOut++] = '"';
249600 zExpr[iOut++] = ' ';
249601 }
249602 if( i<nText && zText[i]==aSpec[2] ){
249603 i += 2;
249604 if( zText[i-1]=='^' ) i++;
249605 while( i<nText && zText[i]!=']' ) i++;
249606 }
249607 iFirst = i+1;
@@ -249614,10 +250136,17 @@
250136
250137 *pRc = rc;
250138 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
250139 return 1;
250140 }
250141
250142 /* Not a match. Clear the position lists of all phrases, as some may
250143 ** have been populated before the loop above exited early, and others
250144 ** may still contain data left over from a previous row. */
250145 for(i=0; i<pNear->nPhrase; i++){
250146 pNear->apPhrase[i]->poslist.n = 0;
250147 }
250148 return 0;
250149 }
250150 }
250151
250152
@@ -249729,10 +250258,28 @@
250258 for(i=0; i<pNode->nChild; i++){
250259 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
250260 }
250261 }
250262 }
250263
250264 /*
250265 ** Zero the position lists of all phrases within the sub-tree headed by
250266 ** pNode that are currently positioned at rowid iRowid. Phrases positioned
250267 ** at other rowids are not modified - their position lists belong to rows
250268 ** that have not yet been tested by the parent node.
250269 */
250270 static void fts5ExprNodeZeroPoslistAt(Fts5ExprNode *pNode, i64 iRowid){
250271 if( pNode->iRowid!=iRowid ) return;
250272 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
250273 fts5ExprNodeZeroPoslist(pNode);
250274 }else{
250275 int i;
250276 for(i=0; i<pNode->nChild; i++){
250277 fts5ExprNodeZeroPoslistAt(pNode->apChild[i], iRowid);
250278 }
250279 }
250280 }
250281
250282
250283
250284 /*
250285 ** Compare the values currently indicated by the two nodes as follows:
@@ -250048,14 +250595,14 @@
250595 pAnd->bNomatch = 1;
250596 }
250597 }
250598 }while( bMatch==0 );
250599
250600 pAnd->iRowid = iLast;
250601 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
250602 fts5ExprNodeZeroPoslistAt(pAnd, iLast);
250603 }
 
250604 return SQLITE_OK;
250605 }
250606
250607 static int fts5ExprNodeNext_AND(
250608 Fts5Expr *pExpr,
@@ -250197,10 +250744,11 @@
250744 break;
250745
250746 default:
250747 assert( pNode->eType==FTS5_NOT );
250748 pNode->bEof = pNode->apChild[0]->bEof;
250749 if( pNode->bEof ) fts5ExprNodeZeroPoslist(pNode->apChild[1]);
250750 break;
250751 }
250752 }
250753
250754 if( rc==SQLITE_OK ){
@@ -253453,12 +254001,11 @@
254001 }
254002 if( rc!=SQLITE_OK ){
254003 sqlite3_free(pRet);
254004 pRet = 0;
254005 }else{
254006 memset(&pRet->p[nByte], 0, FTS5_DATA_PADDING);
 
254007 }
254008 }
254009 p->rc = rc;
254010 p->nRead++;
254011 }
@@ -255308,11 +255855,11 @@
255855 pIter->flags |= FTS5_SEGITER_ONETERM;
255856 if( pIter->pLeaf ){
255857 if( flags & FTS5INDEX_QUERY_DESC ){
255858 pIter->flags |= FTS5_SEGITER_REVERSE;
255859 }
255860 if( bDlidx && iPg==pIter->iTermLeafPgno ){
255861 fts5SegIterLoadDlidx(p, pIter);
255862 }
255863 if( flags & FTS5INDEX_QUERY_DESC ){
255864 fts5SegIterReverse(p, pIter);
255865 }
@@ -255707,10 +256254,17 @@
256254 }else{
256255 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
256256 pIter->iLeafOffset = iOff;
256257 fts5SegIterLoadNPos(p, pIter);
256258 }
256259 break;
256260 }else if( !fts5LeafIsTermless(pIter->pLeaf) ){
256261 /* There are no rowids on this page, but there is another term.
256262 ** Since there are no more rowids for the current term, set the
256263 ** iterator to EOF. */
256264 fts5DataRelease(pIter->pLeaf);
256265 pIter->pLeaf = 0;
256266 break;
256267 }
256268 }
256269 }
256270 }
@@ -259800,15 +260354,13 @@
260354 for(ii=0; ii<pT->nIter; ii++){
260355 Fts5Iter *p = pT->apIter[ii];
260356 if( p->base.bEof==0
260357 && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowid<iFrom))
260358 ){
260359 if( bFrom ){
260360 fts5MultiIterNextFrom(pIndex, p, iFrom);
260361 }else{
 
 
260362 fts5MultiIterNext(pIndex, p, 0, 0);
260363 }
260364 }
260365 }
260366
@@ -260000,10 +260552,19 @@
260552 ** queries performed as part of an integrity-check, or by the fts5vocab
260553 ** module. */
260554 if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){
260555 bTokendata = 0;
260556 }
260557
260558 /* fts5SetupTokendataIter() flushes the in-memory hash table to disk.
260559 ** The flush frees the cached Fts5Structure object, which iterators
260560 ** opened earlier by this function point into without holding a
260561 ** reference (for example one opened for a prefix index query in the
260562 ** same MATCH expression). So, on a tokendata=1 table, flush first. */
260563 if( pConfig->bTokendata ){
260564 fts5IndexFlush(p);
260565 }
260566
260567 /* Figure out which index to search and set iIdx accordingly. If this
260568 ** is a prefix query for which there is no prefix index, set iIdx to
260569 ** greater than pConfig->nPrefix to indicate that the query will be
260570 ** satisfied by scanning multiple terms in the main index.
@@ -262818,11 +263379,13 @@
263379 }
263380 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
263381 pInfo->aConstraintUsage[i].omit = 1;
263382 }
263383 }else if( p->usable ){
263384 if( iCol>=0 && iCol<nCol && fts5UsePatternMatch(pConfig, p)
263385 && pConfig->abUnindexed[iCol]==0
263386 ){
263387 assert( p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB );
263388 idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G';
263389 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
263390 idxStr += strlen(&idxStr[iIdxStr]);
263391 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
@@ -262830,11 +263393,10 @@
263393 nSeenMatch++;
263394 }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){
263395 idxStr[iIdxStr++] = '=';
263396 bSeenEq = 1;
263397 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
 
263398 }
263399 }
263400 }
263401
263402 if( bSeenEq==0 ){
@@ -264481,19 +265043,21 @@
265043 if( aPopulator==0 ) rc = SQLITE_NOMEM;
265044 if( rc==SQLITE_OK ){
265045 rc = fts5SeekCursor(pCsr, 0);
265046 }
265047 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
265048 if( pConfig->abUnindexed[i]==0 ){
265049 const char *z = 0;
265050 int n = 0;
265051 rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n);
265052 if( rc==SQLITE_OK ){
265053 rc = sqlite3Fts5ExprPopulatePoslists(
265054 pConfig, pCsr->pExpr, aPopulator, i, z, n
265055 );
265056 }
265057 sqlite3Fts5ClearLocale(pConfig);
265058 }
265059 }
265060 sqlite3_free(aPopulator);
265061
265062 if( pCsr->pSorter ){
265063 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
@@ -265771,11 +266335,11 @@
266335 int nArg, /* Number of args */
266336 sqlite3_value **apUnused /* Function arguments */
266337 ){
266338 assert( nArg==0 );
266339 UNUSED_PARAM2(nArg, apUnused);
266340 sqlite3_result_text(pCtx, "fts5: 2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b", -1, SQLITE_TRANSIENT);
266341 }
266342
266343 /*
266344 ** Implementation of fts5_locale(LOCALE, TEXT) function.
266345 **
@@ -266131,13 +266695,15 @@
266695 */
266696 static int fts5StorageGetStmt(
266697 Fts5Storage *p, /* Storage handle */
266698 int eStmt, /* FTS5_STMT_XXX constant */
266699 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
266700 char **pzErrMsg /* OUT: Error message (if any) (not used) */
266701 ){
266702 int rc = SQLITE_OK;
266703
266704 (void)pzErrMsg;
266705
266706 /* If there is no %_docsize table, there should be no requests for
266707 ** statements to operate on it. */
266708 assert( p->pConfig->bColumnsize || (
266709 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
@@ -266310,15 +266876,19 @@
266876 rc = fts5ExecPrintf(pConfig->db, 0,
266877 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
266878 pConfig->zDb, pConfig->zName
266879 );
266880 }
266881 if( rc==SQLITE_OK ){
266882 if( pConfig->eContent==FTS5_CONTENT_NORMAL
266883 || pConfig->bContentlessUnindexed
266884 ){
266885 rc = fts5ExecPrintf(pConfig->db, 0,
266886 "DROP TABLE IF EXISTS %Q.'%q_content';",
266887 pConfig->zDb, pConfig->zName
266888 );
266889 }
266890 }
266891 return rc;
266892 }
266893
266894 static void fts5StorageRenameOne(
@@ -266343,11 +266913,13 @@
266913 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
266914 fts5StorageRenameOne(pConfig, &rc, "config", zName);
266915 if( pConfig->bColumnsize ){
266916 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
266917 }
266918 if( pConfig->eContent==FTS5_CONTENT_NORMAL
266919 || pConfig->bContentlessUnindexed
266920 ){
266921 fts5StorageRenameOne(pConfig, &rc, "content", zName);
266922 }
266923 return rc;
266924 }
266925
@@ -266423,11 +266995,11 @@
266995 for(i=0; i<pConfig->nCol; i++){
266996 if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->abUnindexed[i] ){
266997 sqlite3_str_appendf(pDefn, ", c%d", i);
266998 }
266999 }
267000 if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL ){
267001 for(i=0; i<pConfig->nCol; i++){
267002 if( pConfig->abUnindexed[i]==0 ){
267003 sqlite3_str_appendf(pDefn, ", l%d", i);
267004 }
267005 }
@@ -266764,11 +267336,11 @@
267336 p->aTotalSize[ii] -= aCol[ii];
267337 }
267338 }
267339 sqlite3_free(aCol);
267340 }
267341 iOrigin = sqlite3_column_int64(pLookup, 1);
267342 }
267343 rc2 = sqlite3_reset(pLookup);
267344 if( rc==SQLITE_OK ) rc = rc2;
267345 }
267346
@@ -270300,13 +270872,14 @@
270872
270873 #define FTS5_VOCAB_COL 0
270874 #define FTS5_VOCAB_ROW 1
270875 #define FTS5_VOCAB_INSTANCE 2
270876
270877 #define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt, PRIMARY KEY(term, col)"
270878 #define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt, PRIMARY KEY(term)"
270879 #define FTS5_VOCAB_INST_SCHEMA \
270880 "term, doc, col, offset, PRIMARY KEY(term, doc, col, offset)"
270881
270882 /*
270883 ** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
270884 */
270885 #define FTS5_VOCAB_TERM_EQ 0x0100
@@ -270394,13 +270967,13 @@
270967 const char * const *argv, /* xCreate/xConnect argument array */
270968 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
270969 char **pzErr /* Write any error message here */
270970 ){
270971 const char *azSchema[] = {
270972 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ") WITHOUT ROWID",
270973 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ") WITHOUT ROWID",
270974 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ") WITHOUT ROWID"
270975 };
270976
270977 Fts5VocabTable *pRet = 0;
270978 int rc = SQLITE_OK; /* Return code */
270979 int bDb;
270980
+21 -3
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.55.0"
150150
#define SQLITE_VERSION_NUMBER 3055000
151
-#define SQLITE_SOURCE_ID "2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918"
151
+#define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b"
152152
#define SQLITE_SCM_BRANCH "trunk"
153153
#define SQLITE_SCM_TAGS ""
154
-#define SQLITE_SCM_DATETIME "2026-10-17T12:10:02.088Z"
154
+#define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z"
155155
156156
/*
157157
** CAPI3REF: Run-Time Library Version Numbers
158158
** KEYWORDS: sqlite3_version sqlite3_sourceid
159159
**
@@ -4447,10 +4447,19 @@
44474447
** defined by the database schema.</dd>)^
44484448
**
44494449
** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
44504450
** <dd>The maximum number of SQL statements that can be contained within
44514451
** a single trigger.</dd>)^
4452
+**
4453
+** [[SQLITE_LIMIT_CTES]] ^(<dt>SQLITE_LIMIT_CTES</dt>
4454
+** <dd>The maximum number of [common table expressions] (CTEs) allowed
4455
+** in a single SQL statements. This limit is used to limit possible
4456
+** exponential growth in the number of required cursors required for
4457
+** nested joins of CTEs. The limit is an absolute upper bound, and
4458
+** SQLite may refuse to compile a statement if the number of CTEs simply
4459
+** approaches this limit. Set this limit to 0 to disable the restriction.
4460
+** </dd>)^
44524461
** </dl>
44534462
*/
44544463
#define SQLITE_LIMIT_LENGTH 0
44554464
#define SQLITE_LIMIT_SQL_LENGTH 1
44564465
#define SQLITE_LIMIT_COLUMN 2
@@ -4464,10 +4473,11 @@
44644473
#define SQLITE_LIMIT_TRIGGER_DEPTH 10
44654474
#define SQLITE_LIMIT_WORKER_THREADS 11
44664475
#define SQLITE_LIMIT_PARSER_DEPTH 12
44674476
#define SQLITE_LIMIT_SCHEMA 13
44684477
#define SQLITE_LIMIT_TRIGGER_STEPS 14
4478
+#define SQLITE_LIMIT_CTES 15
44694479
44704480
/*
44714481
** CAPI3REF: Prepare Flags
44724482
**
44734483
** These constants define various flags that can be passed into the
@@ -5876,10 +5886,14 @@
58765886
** CAPI3REF: Text Encodings
58775887
**
58785888
** These constants define integer codes that represent the various
58795889
** text encodings supported by SQLite.
58805890
**
5891
+** If any value other than the ones defined here are passed into the
5892
+** "encoding" argument of an API function, then the results are
5893
+** unspecified and subject to change.
5894
+**
58815895
** <dl>
58825896
** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
58835897
**
58845898
** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
58855899
** with each code point being expressed "little endian" - the least significant
@@ -6858,10 +6872,14 @@
68586872
** VFS and operating system. Some system treat a negative argument as an
68596873
** instruction to sleep forever. Others understand it to mean do not sleep
68606874
** at all. ^In SQLite version 3.42.0 and later, a negative
68616875
** argument passed into sqlite3_sleep() is changed to zero before it is relayed
68626876
** down into the xSleep method of the VFS.
6877
+**
6878
+** The maximum sleep time is 2147483 milliseconds (about 35 minutes and
6879
+** 47 seconds). If the argument to sqlite3_sleep() is greater than 2147483,
6880
+** the sleep time is capped at 2147483 milliseconds.
68636881
*/
68646882
SQLITE_API int sqlite3_sleep(int);
68656883
68666884
/*
68676885
** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11252,11 +11270,11 @@
1125211270
** buffer. If the bit had been clear, the returned buffer will not
1125311271
** be accessed by SQLite after the call.
1125411272
**
1125511273
** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
1125611274
** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11257
-** allocation error occurs.
11275
+** allocation or I/O error occurs.
1125811276
**
1125911277
** This interface is omitted if SQLite is compiled with the
1126011278
** [SQLITE_OMIT_DESERIALIZE] option.
1126111279
*/
1126211280
SQLITE_API unsigned char *sqlite3_serialize(
1126311281
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.55.0"
150 #define SQLITE_VERSION_NUMBER 3055000
151 #define SQLITE_SOURCE_ID "2026-10-17 12:10:02 c75eff906c11d8f17ae198e5f6cfea34636df2bde0104440fd0b6983ddb2f918"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-10-17T12:10:02.088Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -4447,10 +4447,19 @@
4447 ** defined by the database schema.</dd>)^
4448 **
4449 ** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
4450 ** <dd>The maximum number of SQL statements that can be contained within
4451 ** a single trigger.</dd>)^
 
 
 
 
 
 
 
 
 
4452 ** </dl>
4453 */
4454 #define SQLITE_LIMIT_LENGTH 0
4455 #define SQLITE_LIMIT_SQL_LENGTH 1
4456 #define SQLITE_LIMIT_COLUMN 2
@@ -4464,10 +4473,11 @@
4464 #define SQLITE_LIMIT_TRIGGER_DEPTH 10
4465 #define SQLITE_LIMIT_WORKER_THREADS 11
4466 #define SQLITE_LIMIT_PARSER_DEPTH 12
4467 #define SQLITE_LIMIT_SCHEMA 13
4468 #define SQLITE_LIMIT_TRIGGER_STEPS 14
 
4469
4470 /*
4471 ** CAPI3REF: Prepare Flags
4472 **
4473 ** These constants define various flags that can be passed into the
@@ -5876,10 +5886,14 @@
5876 ** CAPI3REF: Text Encodings
5877 **
5878 ** These constants define integer codes that represent the various
5879 ** text encodings supported by SQLite.
5880 **
 
 
 
 
5881 ** <dl>
5882 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
5883 **
5884 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
5885 ** with each code point being expressed "little endian" - the least significant
@@ -6858,10 +6872,14 @@
6858 ** VFS and operating system. Some system treat a negative argument as an
6859 ** instruction to sleep forever. Others understand it to mean do not sleep
6860 ** at all. ^In SQLite version 3.42.0 and later, a negative
6861 ** argument passed into sqlite3_sleep() is changed to zero before it is relayed
6862 ** down into the xSleep method of the VFS.
 
 
 
 
6863 */
6864 SQLITE_API int sqlite3_sleep(int);
6865
6866 /*
6867 ** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11252,11 +11270,11 @@
11252 ** buffer. If the bit had been clear, the returned buffer will not
11253 ** be accessed by SQLite after the call.
11254 **
11255 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11256 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11257 ** allocation error occurs.
11258 **
11259 ** This interface is omitted if SQLite is compiled with the
11260 ** [SQLITE_OMIT_DESERIALIZE] option.
11261 */
11262 SQLITE_API unsigned char *sqlite3_serialize(
11263
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.55.0"
150 #define SQLITE_VERSION_NUMBER 3055000
151 #define SQLITE_SOURCE_ID "2026-10-24 22:25:07 d08cabd778f7844d04f3852d93197e80c0d0f4f30a589f7fd0d28577fa3fa68b"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-10-24T22:25:07.764Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -4447,10 +4447,19 @@
4447 ** defined by the database schema.</dd>)^
4448 **
4449 ** [[SQLITE_LIMIT_TRIGGER_STEPS]] ^(<dt>SQLITE_LIMIT_TRIGGER_STEPS</dt>
4450 ** <dd>The maximum number of SQL statements that can be contained within
4451 ** a single trigger.</dd>)^
4452 **
4453 ** [[SQLITE_LIMIT_CTES]] ^(<dt>SQLITE_LIMIT_CTES</dt>
4454 ** <dd>The maximum number of [common table expressions] (CTEs) allowed
4455 ** in a single SQL statements. This limit is used to limit possible
4456 ** exponential growth in the number of required cursors required for
4457 ** nested joins of CTEs. The limit is an absolute upper bound, and
4458 ** SQLite may refuse to compile a statement if the number of CTEs simply
4459 ** approaches this limit. Set this limit to 0 to disable the restriction.
4460 ** </dd>)^
4461 ** </dl>
4462 */
4463 #define SQLITE_LIMIT_LENGTH 0
4464 #define SQLITE_LIMIT_SQL_LENGTH 1
4465 #define SQLITE_LIMIT_COLUMN 2
@@ -4464,10 +4473,11 @@
4473 #define SQLITE_LIMIT_TRIGGER_DEPTH 10
4474 #define SQLITE_LIMIT_WORKER_THREADS 11
4475 #define SQLITE_LIMIT_PARSER_DEPTH 12
4476 #define SQLITE_LIMIT_SCHEMA 13
4477 #define SQLITE_LIMIT_TRIGGER_STEPS 14
4478 #define SQLITE_LIMIT_CTES 15
4479
4480 /*
4481 ** CAPI3REF: Prepare Flags
4482 **
4483 ** These constants define various flags that can be passed into the
@@ -5876,10 +5886,14 @@
5886 ** CAPI3REF: Text Encodings
5887 **
5888 ** These constants define integer codes that represent the various
5889 ** text encodings supported by SQLite.
5890 **
5891 ** If any value other than the ones defined here are passed into the
5892 ** "encoding" argument of an API function, then the results are
5893 ** unspecified and subject to change.
5894 **
5895 ** <dl>
5896 ** [[SQLITE_UTF8]] <dt>SQLITE_UTF8</dt><dd>Text is encoding as UTF-8</dd>
5897 **
5898 ** [[SQLITE_UTF16LE]] <dt>SQLITE_UTF16LE</dt><dd>Text is encoding as UTF-16
5899 ** with each code point being expressed "little endian" - the least significant
@@ -6858,10 +6872,14 @@
6872 ** VFS and operating system. Some system treat a negative argument as an
6873 ** instruction to sleep forever. Others understand it to mean do not sleep
6874 ** at all. ^In SQLite version 3.42.0 and later, a negative
6875 ** argument passed into sqlite3_sleep() is changed to zero before it is relayed
6876 ** down into the xSleep method of the VFS.
6877 **
6878 ** The maximum sleep time is 2147483 milliseconds (about 35 minutes and
6879 ** 47 seconds). If the argument to sqlite3_sleep() is greater than 2147483,
6880 ** the sleep time is capped at 2147483 milliseconds.
6881 */
6882 SQLITE_API int sqlite3_sleep(int);
6883
6884 /*
6885 ** CAPI3REF: Name Of The Folder Holding Temporary Files
@@ -11252,11 +11270,11 @@
11270 ** buffer. If the bit had been clear, the returned buffer will not
11271 ** be accessed by SQLite after the call.
11272 **
11273 ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11274 ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11275 ** allocation or I/O error occurs.
11276 **
11277 ** This interface is omitted if SQLite is compiled with the
11278 ** [SQLITE_OMIT_DESERIALIZE] option.
11279 */
11280 SQLITE_API unsigned char *sqlite3_serialize(
11281

Keyboard Shortcuts

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