Fossil SCM

Merge from trunk.

brickviking 2025-10-23 08:14 UTC bv-infotool merge
Commit 0ffacb525a925ba8da3152c9041fdf98e67b14f10faf30dc685dea5a8ab4e9c6
147 files changed +1 -1 +826 -980 +2521 -708 +178 -42 +2 -2 +80 -25 +24 -31 +12 -8 +82 -36 +18 +16 -6 +225 +1 -1 +7 -27 +1 -1 +2 -1 +7 -1 +19 -22 +26 -4 +46 -8 +18 -3 +19 -13 +1 -1 +1 +29 -15 +3 -3 +2 -1 +18 -8 +2 -2 +9 -11 +6 -5 +4 -8 +2 -7 +3 -4 -1 +31 +2 -2 +4 -4 +16 -12 +27 -10 +73 -41 +1 -1 +131 -142 +7 -2 +13 +1 -1 +1 -1 +2 -2 +2 -2 +1 -1 +243 -92 +7 +6 -6 +4 -2 +156 -85 +3 +2 -2 +15 -5 +21 -62 +1 -1 +531 -34 +1 -1 +3 -3 +284 -164 +4 +12 -8 +9 +3 +4 -3 +42 -1 +24 -18 -139 +1 -1 +1 -1 -138 +138 +2 -2 +1 +10 -4 +13 +12 +3 -3 +11 -11 +11 -11 +147 -31 +5 -5 +2 -2 +7 -7 +6 -6 +5 -5 +2 -2 +6 -6 +1 -1 +3 -3 +2 -2 +409 -345 +8 -8 +1 -1 +1 -1 +1 -1 +2 -2 +4 -4 +1 -2 +1 -1 +4 -4 +6 -6 +5 -5 +2 -2 +1 -1 +1 -1 +1 -1 +1 -1 +9 -9 +22 -22 +24 -24 +18 -18 +5 +2 -2 +1 -1 +7 -7 +4 -4 +1 -1 +2 -2 +4 -4 +1 -1 +1 -1 +4 -4 +1 -1 +1 -1 +3 -3 +1 -1 +1 -1 +1 -1 +5 -5 +1 -1 +1 -1 +1 -1 +7 -7 +2 -2 +2 -2 +8 -8 +7 -7 +1 -1 +1 -1 +6 -6
~ VERSION ~ extsrc/shell.c ~ extsrc/sqlite3.c ~ extsrc/sqlite3.h ~ src/add.c ~ src/branch.c ~ src/browse.c ~ src/captcha.c ~ src/cgi.c ~ src/chat.c ~ src/checkin.c ~ src/checkout.c ~ src/clearsign.c ~ src/clone.c ~ src/comformat.c ~ src/content.c ~ src/cookies.c ~ src/copybtn.js ~ src/db.c ~ src/default.css ~ src/deltafunc.c ~ src/diff.c ~ src/diff.tcl ~ src/diffcmd.c ~ src/dispatch.c ~ src/doc.c ~ src/extcgi.c ~ src/finfo.c ~ src/forum.c ~ src/fossil.copybutton.js ~ src/fossil.dom.js ~ src/fossil.numbered-lines.js ~ src/fossil.page.chat.js ~ src/fossil.page.pikchrshow.js ~ src/fossil.page.pikchrshowasm.js ~ src/fossil.page.ticket.js ~ src/fossil.page.wikiedit.js ~ src/graph.js ~ src/href.js ~ src/http.c ~ src/info.c ~ src/json.c ~ src/login.c ~ src/main.c ~ src/main.mk ~ src/manifest.c ~ src/markdown.md ~ src/match.c ~ src/pikchrshow.c ~ src/printf.c ~ src/regexp.c ~ src/robot.c ~ src/search.c ~ src/security_audit.c ~ src/setup.c ~ src/sitemap.c ~ src/skins.c ~ src/stash.c ~ src/style.c ~ src/tag.c ~ src/tar.c ~ src/th_lang.c ~ src/th_main.c ~ src/timeline.c ~ src/tkt.c ~ src/tktsetup.c ~ src/unversioned.c ~ src/url.c ~ src/user.c ~ src/util.c ~ src/xfer.c ~ src/zip.c ~ test/commit-warning.test ~ test/json.test ~ test/set-manifest.test - test/settings.test ~ test/settings.test.off + test/settings.test.off ~ tools/makeheaders.c ~ tools/makemake.tcl ~ win/Makefile.dmc ~ win/Makefile.mingw ~ win/Makefile.msc ~ www/aboutcgi.wiki ~ www/aboutdownload.wiki ~ www/alerts.md ~ www/antibot.wiki ~ www/backoffice.md ~ www/backup.md ~ www/blame.wiki ~ www/blockchain.md ~ www/branching.wiki ~ www/build.wiki ~ www/caps/admin-v-setup.md ~ www/caps/login-groups.md ~ www/caps/ref.html ~ www/cgi.wiki ~ www/changes.wiki ~ www/chat.md ~ www/childprojects.wiki ~ www/chroot.md ~ www/ckout-workflows.md ~ www/co-vs-up.md ~ www/concepts.wiki ~ www/containers.md ~ www/contribute.wiki ~ www/customskin.md ~ www/defcsp.md ~ www/delta_format.wiki ~ www/embeddeddoc.wiki ~ www/env-opts.md ~ www/event.wiki ~ www/fileedit-page.md ~ www/forum.wiki ~ www/fossil-v-git.wiki ~ www/gitusers.md ~ www/globs.md ~ www/glossary.md ~ www/grep.md ~ www/hashes.md ~ www/hashpolicy.wiki ~ www/hints.wiki ~ www/index.wiki ~ www/inout.wiki ~ www/interwiki.md ~ www/javascript.md ~ www/json-api/intro.md ~ www/loadmgmt.md ~ www/makefile.wiki ~ www/mdtest/test1.md ~ www/mirrorlimitations.md ~ www/mirrortogithub.md ~ www/password.wiki ~ www/patchcmd.md ~ www/private.wiki ~ www/quickstart.wiki ~ www/rebaseharm.md ~ www/relatedwork.md ~ www/selfhost.wiki ~ www/server/index.html ~ www/server/windows/service.md ~ www/serverext.wiki ~ www/ssl-server.md ~ www/sync.wiki ~ www/tech_overview.wiki ~ www/th1.md ~ www/unvers.wiki
+1 -1
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1
-2.27
1
+2.28
22
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 2.27
2
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 2.28
2
+826 -980
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -122,10 +122,13 @@
122122
typedef sqlite3_int64 i64;
123123
typedef sqlite3_uint64 u64;
124124
typedef unsigned char u8;
125125
#include <ctype.h>
126126
#include <stdarg.h>
127
+#ifndef _WIN32
128
+# include <sys/time.h>
129
+#endif
127130
128131
#if !defined(_WIN32) && !defined(WIN32)
129132
# include <signal.h>
130133
# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
131134
# include <pwd.h>
@@ -646,12 +649,23 @@
646649
if( a==0 ) a = "";
647650
if( b==0 ) b = "";
648651
return strncmp(a,b,n);
649652
}
650653
651
-/* Return the current wall-clock time */
654
+/* Return the current wall-clock time in microseconds since the
655
+** Unix epoch (1970-01-01T00:00:00Z)
656
+*/
652657
static sqlite3_int64 timeOfDay(void){
658
+#if defined(_WIN64)
659
+ sqlite3_uint64 t;
660
+ FILETIME tm;
661
+ GetSystemTimePreciseAsFileTime(&tm);
662
+ t = ((u64)tm.dwHighDateTime<<32) | (u64)tm.dwLowDateTime;
663
+ t += 116444736000000000LL;
664
+ t /= 10;
665
+ return t;
666
+#elif defined(_WIN32)
653667
static sqlite3_vfs *clockVfs = 0;
654668
sqlite3_int64 t;
655669
if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
656670
if( clockVfs==0 ) return 0; /* Never actually happens */
657671
if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
@@ -659,11 +673,16 @@
659673
}else{
660674
double r;
661675
clockVfs->xCurrentTime(clockVfs, &r);
662676
t = (sqlite3_int64)(r*86400000.0);
663677
}
664
- return t;
678
+ return t*1000;
679
+#else
680
+ struct timeval sNow;
681
+ (void)gettimeofday(&sNow,0);
682
+ return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
683
+#endif
665684
}
666685
667686
#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
668687
#include <sys/time.h>
669688
#include <sys/resource.h>
@@ -704,12 +723,12 @@
704723
static void endTimer(FILE *out){
705724
if( enableTimer ){
706725
sqlite3_int64 iEnd = timeOfDay();
707726
struct rusage sEnd;
708727
getrusage(RUSAGE_SELF, &sEnd);
709
- sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
710
- (iEnd - iBegin)*0.001,
728
+ sqlite3_fprintf(out, "Run Time: real %.6f user %.6f sys %.6f\n",
729
+ (iEnd - iBegin)*0.000001,
711730
timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
712731
timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
713732
}
714733
}
715734
@@ -783,14 +802,23 @@
783802
static void endTimer(FILE *out){
784803
if( enableTimer && getProcessTimesAddr){
785804
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
786805
sqlite3_int64 ftWallEnd = timeOfDay();
787806
getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
788
- sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
789
- (ftWallEnd - ftWallBegin)*0.001,
807
+#ifdef _WIN64
808
+ /* microsecond precision on 64-bit windows */
809
+ sqlite3_fprintf(out, "Run Time: real %.6f user %f sys %f\n",
810
+ (ftWallEnd - ftWallBegin)*0.000001,
811
+ timeDiff(&ftUserBegin, &ftUserEnd),
812
+ timeDiff(&ftKernelBegin, &ftKernelEnd));
813
+#else
814
+ /* millisecond precisino on 32-bit windows */
815
+ sqlite3_fprintf(out, "Run Time: real %.3f user %.3f sys %.3f\n",
816
+ (ftWallEnd - ftWallBegin)*0.000001,
790817
timeDiff(&ftUserBegin, &ftUserEnd),
791818
timeDiff(&ftKernelBegin, &ftKernelEnd));
819
+#endif
792820
}
793821
}
794822
795823
#define BEGIN_TIMER beginTimer()
796824
#define END_TIMER(X) endTimer(X)
@@ -1126,11 +1154,11 @@
11261154
}
11271155
if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
11281156
&& (z[3] & 0xc0)==0x80
11291157
){
11301158
*pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
1131
- | (z[4] & 0x3f);
1159
+ | (z[3] & 0x3f);
11321160
return 4;
11331161
}
11341162
*pU = 0;
11351163
return 1;
11361164
}
@@ -1189,18 +1217,28 @@
11891217
** since with %*.*s the width is measured in bytes, not characters.
11901218
**
11911219
** Take into account zero-width and double-width Unicode characters.
11921220
** In other words, a zero-width character does not count toward the
11931221
** the w limit. A double-width character counts as two.
1222
+**
1223
+** w should normally be a small number. A couple hundred at most. This
1224
+** routine caps w at 100 million to avoid integer overflow issues.
11941225
*/
11951226
static void utf8_width_print(FILE *out, int w, const char *zUtf){
11961227
const unsigned char *a = (const unsigned char*)zUtf;
1228
+ static const int mxW = 10000000;
11971229
unsigned char c;
11981230
int i = 0;
11991231
int n = 0;
12001232
int k;
1201
- int aw = w<0 ? -w : w;
1233
+ int aw;
1234
+ if( w<-mxW ){
1235
+ w = -mxW;
1236
+ }else if( w>mxW ){
1237
+ w= mxW;
1238
+ }
1239
+ aw = w<0 ? -w : w;
12021240
if( zUtf==0 ) zUtf = "";
12031241
while( (c = a[i])!=0 ){
12041242
if( (c&0xc0)==0xc0 ){
12051243
int u;
12061244
int len = decodeUtf8(a+i, &u);
@@ -1323,11 +1361,11 @@
13231361
13241362
/*
13251363
** This routine reads a line of text from FILE in, stores
13261364
** the text in memory obtained from malloc() and returns a pointer
13271365
** to the text. NULL is returned at end of file, or if malloc()
1328
-** fails.
1366
+** fails, or if the length of the line is longer than about a gigabyte.
13291367
**
13301368
** If zLine is not NULL then it is a malloced buffer returned from
13311369
** a previous call to this routine that may be reused.
13321370
*/
13331371
static char *local_getline(char *zLine, FILE *in){
@@ -1334,10 +1372,14 @@
13341372
int nLine = zLine==0 ? 0 : 100;
13351373
int n = 0;
13361374
13371375
while( 1 ){
13381376
if( n+100>nLine ){
1377
+ if( nLine>=1073741773 ){
1378
+ free(zLine);
1379
+ return 0;
1380
+ }
13391381
nLine = nLine*2 + 100;
13401382
zLine = realloc(zLine, nLine);
13411383
shell_check_oom(zLine);
13421384
}
13431385
if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
@@ -1417,14 +1459,18 @@
14171459
return -1;
14181460
}
14191461
14201462
/*
14211463
** Interpret zArg as an integer value, possibly with suffixes.
1464
+**
1465
+** If the value specified by zArg is outside the range of values that
1466
+** can be represented using a 64-bit twos-complement integer, then return
1467
+** the nearest representable value.
14221468
*/
14231469
static sqlite3_int64 integerValue(const char *zArg){
1424
- sqlite3_int64 v = 0;
1425
- static const struct { char *zSuffix; int iMult; } aMult[] = {
1470
+ sqlite3_uint64 v = 0;
1471
+ static const struct { char *zSuffix; unsigned int iMult; } aMult[] = {
14261472
{ "KiB", 1024 },
14271473
{ "MiB", 1024*1024 },
14281474
{ "GiB", 1024*1024*1024 },
14291475
{ "KB", 1000 },
14301476
{ "MB", 1000000 },
@@ -1443,46 +1489,54 @@
14431489
}
14441490
if( zArg[0]=='0' && zArg[1]=='x' ){
14451491
int x;
14461492
zArg += 2;
14471493
while( (x = hexDigitValue(zArg[0]))>=0 ){
1494
+ if( v > 0x0fffffffffffffffULL ) goto integer_overflow;
14481495
v = (v<<4) + x;
14491496
zArg++;
14501497
}
14511498
}else{
14521499
while( IsDigit(zArg[0]) ){
1453
- v = v*10 + zArg[0] - '0';
1500
+ if( v>=922337203685477580LL ){
1501
+ if( v>922337203685477580LL || zArg[0]>='8' ) goto integer_overflow;
1502
+ }
1503
+ v = v*10 + (zArg[0] - '0');
14541504
zArg++;
14551505
}
14561506
}
14571507
for(i=0; i<ArraySize(aMult); i++){
14581508
if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1509
+ if( 0x7fffffffffffffffULL/aMult[i].iMult < v ) goto integer_overflow;
14591510
v *= aMult[i].iMult;
14601511
break;
14611512
}
14621513
}
1463
- return isNeg? -v : v;
1514
+ if( isNeg && v>0x7fffffffffffffffULL ) goto integer_overflow;
1515
+ return isNeg? -(sqlite3_int64)v : (sqlite3_int64)v;
1516
+integer_overflow:
1517
+ return isNeg ? (i64)0x8000000000000000LL : 0x7fffffffffffffffLL;
14641518
}
14651519
14661520
/*
14671521
** A variable length string to which one can append text.
14681522
*/
14691523
typedef struct ShellText ShellText;
14701524
struct ShellText {
1471
- char *z;
1472
- int n;
1473
- int nAlloc;
1525
+ char *zTxt; /* The text */
1526
+ i64 n; /* Number of bytes of zTxt[] actually used */
1527
+ i64 nAlloc; /* Number of bytes allocated for zTxt[] */
14741528
};
14751529
14761530
/*
14771531
** Initialize and destroy a ShellText object
14781532
*/
14791533
static void initText(ShellText *p){
14801534
memset(p, 0, sizeof(*p));
14811535
}
14821536
static void freeText(ShellText *p){
1483
- free(p->z);
1537
+ sqlite3_free(p->zTxt);
14841538
initText(p);
14851539
}
14861540
14871541
/* zIn is either a pointer to a NULL-terminated string in memory obtained
14881542
** from malloc(), or a NULL pointer. The string pointed to by zAppend is
@@ -1503,30 +1557,30 @@
15031557
for(i=0; i<nAppend; i++){
15041558
if( zAppend[i]==quote ) len++;
15051559
}
15061560
}
15071561
1508
- if( p->z==0 || p->n+len>=p->nAlloc ){
1562
+ if( p->zTxt==0 || p->n+len>=p->nAlloc ){
15091563
p->nAlloc = p->nAlloc*2 + len + 20;
1510
- p->z = realloc(p->z, p->nAlloc);
1511
- shell_check_oom(p->z);
1564
+ p->zTxt = sqlite3_realloc64(p->zTxt, p->nAlloc);
1565
+ shell_check_oom(p->zTxt);
15121566
}
15131567
15141568
if( quote ){
1515
- char *zCsr = p->z+p->n;
1569
+ char *zCsr = p->zTxt+p->n;
15161570
*zCsr++ = quote;
15171571
for(i=0; i<nAppend; i++){
15181572
*zCsr++ = zAppend[i];
15191573
if( zAppend[i]==quote ) *zCsr++ = quote;
15201574
}
15211575
*zCsr++ = quote;
1522
- p->n = (int)(zCsr - p->z);
1576
+ p->n = (i64)(zCsr - p->zTxt);
15231577
*zCsr = '\0';
15241578
}else{
1525
- memcpy(p->z+p->n, zAppend, nAppend);
1579
+ memcpy(p->zTxt+p->n, zAppend, nAppend);
15261580
p->n += nAppend;
1527
- p->z[p->n] = '\0';
1581
+ p->zTxt[p->n] = '\0';
15281582
}
15291583
}
15301584
15311585
/*
15321586
** Attempt to determine if identifier zName needs to be quoted, either
@@ -1547,10 +1601,13 @@
15471601
}
15481602
15491603
/*
15501604
** Construct a fake object name and column list to describe the structure
15511605
** of the view, virtual table, or table valued function zSchema.zName.
1606
+**
1607
+** The returned string comes from sqlite3_mprintf() and should be freed
1608
+** by the caller using sqlite3_free().
15521609
*/
15531610
static char *shellFakeSchema(
15541611
sqlite3 *db, /* The database connection containing the vtab */
15551612
const char *zSchema, /* Schema of the database holding the vtab */
15561613
const char *zName /* The name of the virtual table */
@@ -1587,13 +1644,13 @@
15871644
}
15881645
appendText(&s, ")", 0);
15891646
sqlite3_finalize(pStmt);
15901647
if( nRow==0 ){
15911648
freeText(&s);
1592
- s.z = 0;
1649
+ s.zTxt = 0;
15931650
}
1594
- return s.z;
1651
+ return s.zTxt;
15951652
}
15961653
15971654
/*
15981655
** SQL function: strtod(X)
15991656
**
@@ -1692,11 +1749,11 @@
16921749
if( z==0 ){
16931750
z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
16941751
}else{
16951752
z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
16961753
}
1697
- free(zFake);
1754
+ sqlite3_free(zFake);
16981755
}
16991756
if( z ){
17001757
sqlite3_result_text(pCtx, z, -1, sqlite3_free);
17011758
return;
17021759
}
@@ -3667,11 +3724,12 @@
36673724
iExp -= p->nFrac;
36683725
p->nFrac = 0;
36693726
}
36703727
}
36713728
if( iExp>0 ){
3672
- p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3729
+ p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
3730
+ + (sqlite3_int64)iExp + 1 );
36733731
if( p->a==0 ) goto new_from_text_failed;
36743732
memset(p->a+p->nDigit, 0, iExp);
36753733
p->nDigit += iExp;
36763734
}
36773735
}else if( iExp<0 ){
@@ -3686,18 +3744,23 @@
36863744
iExp -= nExtra;
36873745
p->nFrac = p->nDigit - 1;
36883746
}
36893747
}
36903748
if( iExp>0 ){
3691
- p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
3749
+ p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
3750
+ + (sqlite3_int64)iExp + 1 );
36923751
if( p->a==0 ) goto new_from_text_failed;
36933752
memmove(p->a+iExp, p->a, p->nDigit);
36943753
memset(p->a, 0, iExp);
36953754
p->nDigit += iExp;
36963755
p->nFrac += iExp;
36973756
}
36983757
}
3758
+ if( p->sign ){
3759
+ for(i=0; i<p->nDigit && p->a[i]==0; i++){}
3760
+ if( i>=p->nDigit ) p->sign = 0;
3761
+ }
36993762
return p;
37003763
37013764
new_from_text_failed:
37023765
if( p ){
37033766
if( p->a ) sqlite3_free(p->a);
@@ -3786,11 +3849,11 @@
37863849
}
37873850
if( p->isNull ){
37883851
sqlite3_result_null(pCtx);
37893852
return;
37903853
}
3791
- z = sqlite3_malloc( p->nDigit+4 );
3854
+ z = sqlite3_malloc64( (sqlite3_int64)p->nDigit+4 );
37923855
if( z==0 ){
37933856
sqlite3_result_error_nomem(pCtx);
37943857
return;
37953858
}
37963859
i = 0;
@@ -3851,11 +3914,11 @@
38513914
}
38523915
for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
38533916
for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
38543917
nFrac = p->nFrac + (nDigit - p->nDigit);
38553918
nDigit -= nZero;
3856
- z = sqlite3_malloc( nDigit+20 );
3919
+ z = sqlite3_malloc64( (sqlite3_int64)nDigit+20 );
38573920
if( z==0 ){
38583921
sqlite3_result_error_nomem(pCtx);
38593922
return;
38603923
}
38613924
if( nDigit==0 ){
@@ -3896,17 +3959,25 @@
38963959
** pA!=0
38973960
** pA->isNull==0
38983961
** pB!=0
38993962
** pB->isNull==0
39003963
*/
3901
-static int decimal_cmp(const Decimal *pA, const Decimal *pB){
3964
+static int decimal_cmp(Decimal *pA, Decimal *pB){
39023965
int nASig, nBSig, rc, n;
3966
+ while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
3967
+ pA->nDigit--;
3968
+ pA->nFrac--;
3969
+ }
3970
+ while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
3971
+ pB->nDigit--;
3972
+ pB->nFrac--;
3973
+ }
39033974
if( pA->sign!=pB->sign ){
39043975
return pA->sign ? -1 : +1;
39053976
}
39063977
if( pA->sign ){
3907
- const Decimal *pTemp = pA;
3978
+ Decimal *pTemp = pA;
39083979
pA = pB;
39093980
pB = pTemp;
39103981
}
39113982
nASig = pA->nDigit - pA->nFrac;
39123983
nBSig = pB->nDigit - pB->nFrac;
@@ -4064,11 +4135,12 @@
40644135
if( pA==0 || pA->oom || pA->isNull
40654136
|| pB==0 || pB->oom || pB->isNull
40664137
){
40674138
goto mul_end;
40684139
}
4069
- acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4140
+ acc = sqlite3_malloc64( (sqlite3_int64)pA->nDigit +
4141
+ (sqlite3_int64)pB->nDigit + 2 );
40704142
if( acc==0 ){
40714143
pA->oom = 1;
40724144
goto mul_end;
40734145
}
40744146
memset(acc, 0, pA->nDigit + pB->nDigit + 2);
@@ -4151,11 +4223,11 @@
41514223
r = -r;
41524224
}else{
41534225
isNeg = 0;
41544226
}
41554227
memcpy(&a,&r,sizeof(a));
4156
- if( a==0 ){
4228
+ if( a==0 || a==(sqlite3_int64)0x8000000000000000LL){
41574229
e = 0;
41584230
m = 0;
41594231
}else{
41604232
e = a>>52;
41614233
m = a & ((((sqlite3_int64)1)<<52)-1);
@@ -4426,516 +4498,10 @@
44264498
}
44274499
return rc;
44284500
}
44294501
44304502
/************************* End ../ext/misc/decimal.c ********************/
4431
-/************************* Begin ../ext/misc/percentile.c ******************/
4432
-/*
4433
-** 2013-05-28
4434
-**
4435
-** The author disclaims copyright to this source code. In place of
4436
-** a legal notice, here is a blessing:
4437
-**
4438
-** May you do good and not evil.
4439
-** May you find forgiveness for yourself and forgive others.
4440
-** May you share freely, never taking more than you give.
4441
-**
4442
-******************************************************************************
4443
-**
4444
-** This file contains code to implement the percentile(Y,P) SQL function
4445
-** and similar as described below:
4446
-**
4447
-** (1) The percentile(Y,P) function is an aggregate function taking
4448
-** exactly two arguments.
4449
-**
4450
-** (2) If the P argument to percentile(Y,P) is not the same for every
4451
-** row in the aggregate then an error is thrown. The word "same"
4452
-** in the previous sentence means that the value differ by less
4453
-** than 0.001.
4454
-**
4455
-** (3) If the P argument to percentile(Y,P) evaluates to anything other
4456
-** than a number in the range of 0.0 to 100.0 inclusive then an
4457
-** error is thrown.
4458
-**
4459
-** (4) If any Y argument to percentile(Y,P) evaluates to a value that
4460
-** is not NULL and is not numeric then an error is thrown.
4461
-**
4462
-** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus
4463
-** infinity then an error is thrown. (SQLite always interprets NaN
4464
-** values as NULL.)
4465
-**
4466
-** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions,
4467
-** including CASE WHEN expressions.
4468
-**
4469
-** (7) The percentile(Y,P) aggregate is able to handle inputs of at least
4470
-** one million (1,000,000) rows.
4471
-**
4472
-** (8) If there are no non-NULL values for Y, then percentile(Y,P)
4473
-** returns NULL.
4474
-**
4475
-** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P)
4476
-** returns the one Y value.
4477
-**
4478
-** (10) If there N non-NULL values of Y where N is two or more and
4479
-** the Y values are ordered from least to greatest and a graph is
4480
-** drawn from 0 to N-1 such that the height of the graph at J is
4481
-** the J-th Y value and such that straight lines are drawn between
4482
-** adjacent Y values, then the percentile(Y,P) function returns
4483
-** the height of the graph at P*(N-1)/100.
4484
-**
4485
-** (11) The percentile(Y,P) function always returns either a floating
4486
-** point number or NULL.
4487
-**
4488
-** (12) The percentile(Y,P) is implemented as a single C99 source-code
4489
-** file that compiles into a shared-library or DLL that can be loaded
4490
-** into SQLite using the sqlite3_load_extension() interface.
4491
-**
4492
-** (13) A separate median(Y) function is the equivalent percentile(Y,50).
4493
-**
4494
-** (14) A separate percentile_cont(Y,P) function is equivalent to
4495
-** percentile(Y,P/100.0). In other words, the fraction value in
4496
-** the second argument is in the range of 0 to 1 instead of 0 to 100.
4497
-**
4498
-** (15) A separate percentile_disc(Y,P) function is like
4499
-** percentile_cont(Y,P) except that instead of returning the weighted
4500
-** average of the nearest two input values, it returns the next lower
4501
-** value. So the percentile_disc(Y,P) will always return a value
4502
-** that was one of the inputs.
4503
-**
4504
-** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and
4505
-** percentile_disc(Y,P) can be used as window functions.
4506
-**
4507
-** Differences from standard SQL:
4508
-**
4509
-** * The percentile_cont(X,P) function is equivalent to the following in
4510
-** standard SQL:
4511
-**
4512
-** (percentile_cont(P) WITHIN GROUP (ORDER BY X))
4513
-**
4514
-** The SQLite syntax is much more compact. The standard SQL syntax
4515
-** is also supported if SQLite is compiled with the
4516
-** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option.
4517
-**
4518
-** * No median(X) function exists in the SQL standard. App developers
4519
-** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)".
4520
-**
4521
-** * No percentile(Y,P) function exists in the SQL standard. Instead of
4522
-** percential(Y,P), developers must write this:
4523
-** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that
4524
-** the fraction parameter to percentile() goes from 0 to 100 whereas
4525
-** the fraction parameter in SQL standard percentile_cont() goes from
4526
-** 0 to 1.
4527
-**
4528
-** Implementation notes as of 2024-08-31:
4529
-**
4530
-** * The regular aggregate-function versions of these routines work
4531
-** by accumulating all values in an array of doubles, then sorting
4532
-** that array using quicksort before computing the answer. Thus
4533
-** the runtime is O(NlogN) where N is the number of rows of input.
4534
-**
4535
-** * For the window-function versions of these routines, the array of
4536
-** inputs is sorted as soon as the first value is computed. Thereafter,
4537
-** the array is kept in sorted order using an insert-sort. This
4538
-** results in O(N*K) performance where K is the size of the window.
4539
-** One can imagine alternative implementations that give O(N*logN*logK)
4540
-** performance, but they require more complex logic and data structures.
4541
-** The developers have elected to keep the asymptotically slower
4542
-** algorithm for now, for simplicity, under the theory that window
4543
-** functions are seldom used and when they are, the window size K is
4544
-** often small. The developers might revisit that decision later,
4545
-** should the need arise.
4546
-*/
4547
-#if defined(SQLITE3_H)
4548
- /* no-op */
4549
-#elif defined(SQLITE_STATIC_PERCENTILE)
4550
-/* # include "sqlite3.h" */
4551
-#else
4552
-/* # include "sqlite3ext.h" */
4553
- SQLITE_EXTENSION_INIT1
4554
-#endif
4555
-#include <assert.h>
4556
-#include <string.h>
4557
-#include <stdlib.h>
4558
-
4559
-/* The following object is the group context for a single percentile()
4560
-** aggregate. Remember all input Y values until the very end.
4561
-** Those values are accumulated in the Percentile.a[] array.
4562
-*/
4563
-typedef struct Percentile Percentile;
4564
-struct Percentile {
4565
- unsigned nAlloc; /* Number of slots allocated for a[] */
4566
- unsigned nUsed; /* Number of slots actually used in a[] */
4567
- char bSorted; /* True if a[] is already in sorted order */
4568
- char bKeepSorted; /* True if advantageous to keep a[] sorted */
4569
- char bPctValid; /* True if rPct is valid */
4570
- double rPct; /* Fraction. 0.0 to 1.0 */
4571
- double *a; /* Array of Y values */
4572
-};
4573
-
4574
-/* Details of each function in the percentile family */
4575
-typedef struct PercentileFunc PercentileFunc;
4576
-struct PercentileFunc {
4577
- const char *zName; /* Function name */
4578
- char nArg; /* Number of arguments */
4579
- char mxFrac; /* Maximum value of the "fraction" input */
4580
- char bDiscrete; /* True for percentile_disc() */
4581
-};
4582
-static const PercentileFunc aPercentFunc[] = {
4583
- { "median", 1, 1, 0 },
4584
- { "percentile", 2, 100, 0 },
4585
- { "percentile_cont", 2, 1, 0 },
4586
- { "percentile_disc", 2, 1, 1 },
4587
-};
4588
-
4589
-/*
4590
-** Return TRUE if the input floating-point number is an infinity.
4591
-*/
4592
-static int percentIsInfinity(double r){
4593
- sqlite3_uint64 u;
4594
- assert( sizeof(u)==sizeof(r) );
4595
- memcpy(&u, &r, sizeof(u));
4596
- return ((u>>52)&0x7ff)==0x7ff;
4597
-}
4598
-
4599
-/*
4600
-** Return TRUE if two doubles differ by 0.001 or less.
4601
-*/
4602
-static int percentSameValue(double a, double b){
4603
- a -= b;
4604
- return a>=-0.001 && a<=0.001;
4605
-}
4606
-
4607
-/*
4608
-** Search p (which must have p->bSorted) looking for an entry with
4609
-** value y. Return the index of that entry.
4610
-**
4611
-** If bExact is true, return -1 if the entry is not found.
4612
-**
4613
-** If bExact is false, return the index at which a new entry with
4614
-** value y should be insert in order to keep the values in sorted
4615
-** order. The smallest return value in this case will be 0, and
4616
-** the largest return value will be p->nUsed.
4617
-*/
4618
-static int percentBinarySearch(Percentile *p, double y, int bExact){
4619
- int iFirst = 0; /* First element of search range */
4620
- int iLast = p->nUsed - 1; /* Last element of search range */
4621
- while( iLast>=iFirst ){
4622
- int iMid = (iFirst+iLast)/2;
4623
- double x = p->a[iMid];
4624
- if( x<y ){
4625
- iFirst = iMid + 1;
4626
- }else if( x>y ){
4627
- iLast = iMid - 1;
4628
- }else{
4629
- return iMid;
4630
- }
4631
- }
4632
- if( bExact ) return -1;
4633
- return iFirst;
4634
-}
4635
-
4636
-/*
4637
-** Generate an error for a percentile function.
4638
-**
4639
-** The error format string must have exactly one occurrence of "%%s()"
4640
-** (with two '%' characters). That substring will be replaced by the name
4641
-** of the function.
4642
-*/
4643
-static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){
4644
- PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4645
- char *zMsg1;
4646
- char *zMsg2;
4647
- va_list ap;
4648
-
4649
- va_start(ap, zFormat);
4650
- zMsg1 = sqlite3_vmprintf(zFormat, ap);
4651
- va_end(ap);
4652
- zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, pFunc->zName) : 0;
4653
- sqlite3_result_error(pCtx, zMsg2, -1);
4654
- sqlite3_free(zMsg1);
4655
- sqlite3_free(zMsg2);
4656
-}
4657
-
4658
-/*
4659
-** The "step" function for percentile(Y,P) is called once for each
4660
-** input row.
4661
-*/
4662
-static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
4663
- Percentile *p;
4664
- double rPct;
4665
- int eType;
4666
- double y;
4667
- assert( argc==2 || argc==1 );
4668
-
4669
- if( argc==1 ){
4670
- /* Requirement 13: median(Y) is the same as percentile(Y,50). */
4671
- rPct = 0.5;
4672
- }else{
4673
- /* Requirement 3: P must be a number between 0 and 100 */
4674
- PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4675
- eType = sqlite3_value_numeric_type(argv[1]);
4676
- rPct = sqlite3_value_double(argv[1])/(double)pFunc->mxFrac;
4677
- if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4678
- || rPct<0.0 || rPct>1.0
4679
- ){
4680
- percentError(pCtx, "the fraction argument to %%s()"
4681
- " is not between 0.0 and %.1f",
4682
- (double)pFunc->mxFrac);
4683
- return;
4684
- }
4685
- }
4686
-
4687
- /* Allocate the session context. */
4688
- p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4689
- if( p==0 ) return;
4690
-
4691
- /* Remember the P value. Throw an error if the P value is different
4692
- ** from any prior row, per Requirement (2). */
4693
- if( !p->bPctValid ){
4694
- p->rPct = rPct;
4695
- p->bPctValid = 1;
4696
- }else if( !percentSameValue(p->rPct,rPct) ){
4697
- percentError(pCtx, "the fraction argument to %%s()"
4698
- " is not the same for all input rows");
4699
- return;
4700
- }
4701
-
4702
- /* Ignore rows for which Y is NULL */
4703
- eType = sqlite3_value_type(argv[0]);
4704
- if( eType==SQLITE_NULL ) return;
4705
-
4706
- /* If not NULL, then Y must be numeric. Otherwise throw an error.
4707
- ** Requirement 4 */
4708
- if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4709
- percentError(pCtx, "input to %%s() is not numeric");
4710
- return;
4711
- }
4712
-
4713
- /* Throw an error if the Y value is infinity or NaN */
4714
- y = sqlite3_value_double(argv[0]);
4715
- if( percentIsInfinity(y) ){
4716
- percentError(pCtx, "Inf input to %%s()");
4717
- return;
4718
- }
4719
-
4720
- /* Allocate and store the Y */
4721
- if( p->nUsed>=p->nAlloc ){
4722
- unsigned n = p->nAlloc*2 + 250;
4723
- double *a = sqlite3_realloc64(p->a, sizeof(double)*n);
4724
- if( a==0 ){
4725
- sqlite3_free(p->a);
4726
- memset(p, 0, sizeof(*p));
4727
- sqlite3_result_error_nomem(pCtx);
4728
- return;
4729
- }
4730
- p->nAlloc = n;
4731
- p->a = a;
4732
- }
4733
- if( p->nUsed==0 ){
4734
- p->a[p->nUsed++] = y;
4735
- p->bSorted = 1;
4736
- }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){
4737
- p->a[p->nUsed++] = y;
4738
- }else if( p->bKeepSorted ){
4739
- int i;
4740
- i = percentBinarySearch(p, y, 0);
4741
- if( i<(int)p->nUsed ){
4742
- memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0]));
4743
- }
4744
- p->a[i] = y;
4745
- p->nUsed++;
4746
- }else{
4747
- p->a[p->nUsed++] = y;
4748
- p->bSorted = 0;
4749
- }
4750
-}
4751
-
4752
-/*
4753
-** Interchange two doubles.
4754
-*/
4755
-#define SWAP_DOUBLE(X,Y) {double ttt=(X);(X)=(Y);(Y)=ttt;}
4756
-
4757
-/*
4758
-** Sort an array of doubles.
4759
-**
4760
-** Algorithm: quicksort
4761
-**
4762
-** This is implemented separately rather than using the qsort() routine
4763
-** from the standard library because:
4764
-**
4765
-** (1) To avoid a dependency on qsort()
4766
-** (2) To avoid the function call to the comparison routine for each
4767
-** comparison.
4768
-*/
4769
-static void percentSort(double *a, unsigned int n){
4770
- int iLt; /* Entries before a[iLt] are less than rPivot */
4771
- int iGt; /* Entries at or after a[iGt] are greater than rPivot */
4772
- int i; /* Loop counter */
4773
- double rPivot; /* The pivot value */
4774
-
4775
- assert( n>=2 );
4776
- if( a[0]>a[n-1] ){
4777
- SWAP_DOUBLE(a[0],a[n-1])
4778
- }
4779
- if( n==2 ) return;
4780
- iGt = n-1;
4781
- i = n/2;
4782
- if( a[0]>a[i] ){
4783
- SWAP_DOUBLE(a[0],a[i])
4784
- }else if( a[i]>a[iGt] ){
4785
- SWAP_DOUBLE(a[i],a[iGt])
4786
- }
4787
- if( n==3 ) return;
4788
- rPivot = a[i];
4789
- iLt = i = 1;
4790
- do{
4791
- if( a[i]<rPivot ){
4792
- if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt])
4793
- iLt++;
4794
- i++;
4795
- }else if( a[i]>rPivot ){
4796
- do{
4797
- iGt--;
4798
- }while( iGt>i && a[iGt]>rPivot );
4799
- SWAP_DOUBLE(a[i],a[iGt])
4800
- }else{
4801
- i++;
4802
- }
4803
- }while( i<iGt );
4804
- if( iLt>=2 ) percentSort(a, iLt);
4805
- if( n-iGt>=2 ) percentSort(a+iGt, n-iGt);
4806
-
4807
-/* Uncomment for testing */
4808
-#if 0
4809
- for(i=0; i<n-1; i++){
4810
- assert( a[i]<=a[i+1] );
4811
- }
4812
-#endif
4813
-}
4814
-
4815
-
4816
-/*
4817
-** The "inverse" function for percentile(Y,P) is called to remove a
4818
-** row that was previously inserted by "step".
4819
-*/
4820
-static void percentInverse(sqlite3_context *pCtx,int argc,sqlite3_value **argv){
4821
- Percentile *p;
4822
- int eType;
4823
- double y;
4824
- int i;
4825
- assert( argc==2 || argc==1 );
4826
-
4827
- /* Allocate the session context. */
4828
- p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4829
- assert( p!=0 );
4830
-
4831
- /* Ignore rows for which Y is NULL */
4832
- eType = sqlite3_value_type(argv[0]);
4833
- if( eType==SQLITE_NULL ) return;
4834
-
4835
- /* If not NULL, then Y must be numeric. Otherwise throw an error.
4836
- ** Requirement 4 */
4837
- if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4838
- return;
4839
- }
4840
-
4841
- /* Ignore the Y value if it is infinity or NaN */
4842
- y = sqlite3_value_double(argv[0]);
4843
- if( percentIsInfinity(y) ){
4844
- return;
4845
- }
4846
- if( p->bSorted==0 ){
4847
- assert( p->nUsed>1 );
4848
- percentSort(p->a, p->nUsed);
4849
- p->bSorted = 1;
4850
- }
4851
- p->bKeepSorted = 1;
4852
-
4853
- /* Find and remove the row */
4854
- i = percentBinarySearch(p, y, 1);
4855
- if( i>=0 ){
4856
- p->nUsed--;
4857
- if( i<(int)p->nUsed ){
4858
- memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0]));
4859
- }
4860
- }
4861
-}
4862
-
4863
-/*
4864
-** Compute the final output of percentile(). Clean up all allocated
4865
-** memory if and only if bIsFinal is true.
4866
-*/
4867
-static void percentCompute(sqlite3_context *pCtx, int bIsFinal){
4868
- Percentile *p;
4869
- PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4870
- unsigned i1, i2;
4871
- double v1, v2;
4872
- double ix, vx;
4873
- p = (Percentile*)sqlite3_aggregate_context(pCtx, 0);
4874
- if( p==0 ) return;
4875
- if( p->a==0 ) return;
4876
- if( p->nUsed ){
4877
- if( p->bSorted==0 ){
4878
- assert( p->nUsed>1 );
4879
- percentSort(p->a, p->nUsed);
4880
- p->bSorted = 1;
4881
- }
4882
- ix = p->rPct*(p->nUsed-1);
4883
- i1 = (unsigned)ix;
4884
- if( pFunc->bDiscrete ){
4885
- vx = p->a[i1];
4886
- }else{
4887
- i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1;
4888
- v1 = p->a[i1];
4889
- v2 = p->a[i2];
4890
- vx = v1 + (v2-v1)*(ix-i1);
4891
- }
4892
- sqlite3_result_double(pCtx, vx);
4893
- }
4894
- if( bIsFinal ){
4895
- sqlite3_free(p->a);
4896
- memset(p, 0, sizeof(*p));
4897
- }else{
4898
- p->bKeepSorted = 1;
4899
- }
4900
-}
4901
-static void percentFinal(sqlite3_context *pCtx){
4902
- percentCompute(pCtx, 1);
4903
-}
4904
-static void percentValue(sqlite3_context *pCtx){
4905
- percentCompute(pCtx, 0);
4906
-}
4907
-
4908
-#if defined(_WIN32) && !defined(SQLITE3_H) && !defined(SQLITE_STATIC_PERCENTILE)
4909
-
4910
-#endif
4911
-int sqlite3_percentile_init(
4912
- sqlite3 *db,
4913
- char **pzErrMsg,
4914
- const sqlite3_api_routines *pApi
4915
-){
4916
- int rc = SQLITE_OK;
4917
- unsigned int i;
4918
-#ifdef SQLITE3EXT_H
4919
- SQLITE_EXTENSION_INIT2(pApi);
4920
-#else
4921
- (void)pApi; /* Unused parameter */
4922
-#endif
4923
- (void)pzErrMsg; /* Unused parameter */
4924
- for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
4925
- rc = sqlite3_create_window_function(db,
4926
- aPercentFunc[i].zName,
4927
- aPercentFunc[i].nArg,
4928
- SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_SELFORDER1,
4929
- (void*)&aPercentFunc[i],
4930
- percentStep, percentFinal, percentValue, percentInverse, 0);
4931
- if( rc ) break;
4932
- }
4933
- return rc;
4934
-}
4935
-
4936
-/************************* End ../ext/misc/percentile.c ********************/
49374503
#undef sqlite3_base_init
49384504
#define sqlite3_base_init sqlite3_base64_init
49394505
/************************* Begin ../ext/misc/base64.c ******************/
49404506
/*
49414507
** 2022-11-18
@@ -5144,20 +4710,22 @@
51444710
return pOut;
51454711
}
51464712
51474713
/* This function does the work for the SQLite base64(x) UDF. */
51484714
static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
5149
- int nb, nc, nv = sqlite3_value_bytes(av[0]);
4715
+ sqlite3_int64 nb;
4716
+ sqlite3_int64 nv = sqlite3_value_bytes(av[0]);
4717
+ sqlite3_int64 nc;
51504718
int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
51514719
SQLITE_LIMIT_LENGTH, -1);
51524720
char *cBuf;
51534721
u8 *bBuf;
51544722
assert(na==1);
51554723
switch( sqlite3_value_type(av[0]) ){
51564724
case SQLITE_BLOB:
51574725
nb = nv;
5158
- nc = 4*(nv+2/3); /* quads needed */
4726
+ nc = 4*((nv+2)/3); /* quads needed */
51594727
nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
51604728
if( nvMax < nc ){
51614729
sqlite3_result_error(context, "blob expanded to base64 too big", -1);
51624730
return;
51634731
}
@@ -5524,11 +5092,11 @@
55245092
}
55255093
# endif
55265094
55275095
/* This function does the work for the SQLite base85(x) UDF. */
55285096
static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5529
- int nb, nc, nv = sqlite3_value_bytes(av[0]);
5097
+ sqlite3_int64 nb, nc, nv = sqlite3_value_bytes(av[0]);
55305098
int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
55315099
SQLITE_LIMIT_LENGTH, -1);
55325100
char *cBuf;
55335101
u8 *bBuf;
55345102
assert(na==1);
@@ -5829,10 +5397,13 @@
58295397
}
58305398
memcpy(&a,&r,sizeof(a));
58315399
if( a==0 ){
58325400
e = 0;
58335401
m = 0;
5402
+ }else if( a==(sqlite3_int64)0x8000000000000000LL ){
5403
+ e = -1996;
5404
+ m = -1;
58345405
}else{
58355406
e = a>>52;
58365407
m = a & ((((sqlite3_int64)1)<<52)-1);
58375408
if( e==0 ){
58385409
m <<= 1;
@@ -6052,23 +5623,24 @@
60525623
** Examples:
60535624
**
60545625
** SELECT * FROM generate_series(0,100,5);
60555626
**
60565627
** The query above returns integers from 0 through 100 counting by steps
6057
-** of 5.
5628
+** of 5. In other words, 0, 5, 10, 15, ..., 90, 95, 100. There are a total
5629
+** of 21 rows.
60585630
**
60595631
** SELECT * FROM generate_series(0,100);
60605632
**
6061
-** Integers from 0 through 100 with a step size of 1.
5633
+** Integers from 0 through 100 with a step size of 1. 101 rows.
60625634
**
60635635
** SELECT * FROM generate_series(20) LIMIT 10;
60645636
**
6065
-** Integers 20 through 29.
5637
+** Integers 20 through 29. 10 rows.
60665638
**
60675639
** SELECT * FROM generate_series(0,-100,-5);
60685640
**
6069
-** Integers 0 -5 -10 ... -100.
5641
+** Integers 0 -5 -10 ... -100. 21 rows.
60705642
**
60715643
** SELECT * FROM generate_series(0,-1);
60725644
**
60735645
** Empty sequence.
60745646
**
@@ -6140,143 +5712,92 @@
61405712
#include <string.h>
61415713
#include <limits.h>
61425714
#include <math.h>
61435715
61445716
#ifndef SQLITE_OMIT_VIRTUALTABLE
6145
-/*
6146
-** Return that member of a generate_series(...) sequence whose 0-based
6147
-** index is ix. The 0th member is given by smBase. The sequence members
6148
-** progress per ix increment by smStep.
6149
-*/
6150
-static sqlite3_int64 genSeqMember(
6151
- sqlite3_int64 smBase,
6152
- sqlite3_int64 smStep,
6153
- sqlite3_uint64 ix
6154
-){
6155
- static const sqlite3_uint64 mxI64 =
6156
- ((sqlite3_uint64)0x7fffffff)<<32 | 0xffffffff;
6157
- if( ix>=mxI64 ){
6158
- /* Get ix into signed i64 range. */
6159
- ix -= mxI64;
6160
- /* With 2's complement ALU, this next can be 1 step, but is split into
6161
- * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
6162
- smBase += (mxI64/2) * smStep;
6163
- smBase += (mxI64 - mxI64/2) * smStep;
6164
- }
6165
- /* Under UBSAN (or on 1's complement machines), must do this last term
6166
- * in steps to avoid the dreaded (and harmless) signed multiply overflow. */
6167
- if( ix>=2 ){
6168
- sqlite3_int64 ix2 = (sqlite3_int64)ix/2;
6169
- smBase += ix2*smStep;
6170
- ix -= ix2;
6171
- }
6172
- return smBase + ((sqlite3_int64)ix)*smStep;
6173
-}
6174
-
6175
-/* typedef unsigned char u8; */
6176
-
6177
-typedef struct SequenceSpec {
6178
- sqlite3_int64 iOBase; /* Original starting value ("start") */
6179
- sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
6180
- sqlite3_int64 iBase; /* Starting value to actually use */
6181
- sqlite3_int64 iTerm; /* Terminal value to actually use */
6182
- sqlite3_int64 iStep; /* Increment ("step") */
6183
- sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
6184
- sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
6185
- sqlite3_int64 iValueNow; /* Current value during generation */
6186
- u8 isNotEOF; /* Sequence generation not exhausted */
6187
- u8 isReversing; /* Sequence is being reverse generated */
6188
-} SequenceSpec;
6189
-
6190
-/*
6191
-** Prepare a SequenceSpec for use in generating an integer series
6192
-** given initialized iBase, iTerm and iStep values. Sequence is
6193
-** initialized per given isReversing. Other members are computed.
6194
-*/
6195
-static void setupSequence( SequenceSpec *pss ){
6196
- int bSameSigns;
6197
- pss->uSeqIndexMax = 0;
6198
- pss->isNotEOF = 0;
6199
- bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0);
6200
- if( pss->iTerm < pss->iBase ){
6201
- sqlite3_uint64 nuspan = 0;
6202
- if( bSameSigns ){
6203
- nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm);
6204
- }else{
6205
- /* Under UBSAN (or on 1's complement machines), must do this in steps.
6206
- * In this clause, iBase>=0 and iTerm<0 . */
6207
- nuspan = 1;
6208
- nuspan += pss->iBase;
6209
- nuspan += -(pss->iTerm+1);
6210
- }
6211
- if( pss->iStep<0 ){
6212
- pss->isNotEOF = 1;
6213
- if( nuspan==ULONG_MAX ){
6214
- pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1;
6215
- }else if( pss->iStep>LLONG_MIN ){
6216
- pss->uSeqIndexMax = nuspan/-pss->iStep;
6217
- }
6218
- }
6219
- }else if( pss->iTerm > pss->iBase ){
6220
- sqlite3_uint64 puspan = 0;
6221
- if( bSameSigns ){
6222
- puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase);
6223
- }else{
6224
- /* Under UBSAN (or on 1's complement machines), must do this in steps.
6225
- * In this clause, iTerm>=0 and iBase<0 . */
6226
- puspan = 1;
6227
- puspan += pss->iTerm;
6228
- puspan += -(pss->iBase+1);
6229
- }
6230
- if( pss->iStep>0 ){
6231
- pss->isNotEOF = 1;
6232
- pss->uSeqIndexMax = puspan/pss->iStep;
6233
- }
6234
- }else if( pss->iTerm == pss->iBase ){
6235
- pss->isNotEOF = 1;
6236
- pss->uSeqIndexMax = 0;
6237
- }
6238
- pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0;
6239
- pss->iValueNow = (pss->isReversing)
6240
- ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax)
6241
- : pss->iBase;
6242
-}
6243
-
6244
-/*
6245
-** Progress sequence generator to yield next value, if any.
6246
-** Leave its state to either yield next value or be at EOF.
6247
-** Return whether there is a next value, or 0 at EOF.
6248
-*/
6249
-static int progressSequence( SequenceSpec *pss ){
6250
- if( !pss->isNotEOF ) return 0;
6251
- if( pss->isReversing ){
6252
- if( pss->uSeqIndexNow > 0 ){
6253
- pss->uSeqIndexNow--;
6254
- pss->iValueNow -= pss->iStep;
6255
- }else{
6256
- pss->isNotEOF = 0;
6257
- }
6258
- }else{
6259
- if( pss->uSeqIndexNow < pss->uSeqIndexMax ){
6260
- pss->uSeqIndexNow++;
6261
- pss->iValueNow += pss->iStep;
6262
- }else{
6263
- pss->isNotEOF = 0;
6264
- }
6265
- }
6266
- return pss->isNotEOF;
6267
-}
62685717
62695718
/* series_cursor is a subclass of sqlite3_vtab_cursor which will
62705719
** serve as the underlying representation of a cursor that scans
6271
-** over rows of the result
5720
+** over rows of the result.
5721
+**
5722
+** iOBase, iOTerm, and iOStep are the original values of the
5723
+** start=, stop=, and step= constraints on the query. These are
5724
+** the values reported by the start, stop, and step columns of the
5725
+** virtual table.
5726
+**
5727
+** iBase, iTerm, iStep, and bDescp are the actual values used to generate
5728
+** the sequence. These might be different from the iOxxxx values.
5729
+** For example in
5730
+**
5731
+** SELECT value FROM generate_series(1,11,2)
5732
+** WHERE value BETWEEN 4 AND 8;
5733
+**
5734
+** The iOBase is 1, but the iBase is 5. iOTerm is 11 but iTerm is 7.
5735
+** Another example:
5736
+**
5737
+** SELECT value FROM generate_series(1,15,3) ORDER BY value DESC;
5738
+**
5739
+** The cursor initialization for the above query is:
5740
+**
5741
+** iOBase = 1 iBase = 13
5742
+** iOTerm = 15 iTerm = 1
5743
+** iOStep = 3 iStep = 3 bDesc = 1
5744
+**
5745
+** The actual step size is unsigned so that can have a value of
5746
+** +9223372036854775808 which is needed for querys like this:
5747
+**
5748
+** SELECT value
5749
+** FROM generate_series(9223372036854775807,
5750
+** -9223372036854775808,
5751
+** -9223372036854775808)
5752
+** ORDER BY value ASC;
5753
+**
5754
+** The setup for the previous query will be:
5755
+**
5756
+** iOBase = 9223372036854775807 iBase = -1
5757
+** iOTerm = -9223372036854775808 iTerm = 9223372036854775807
5758
+** iOStep = -9223372036854775808 iStep = 9223372036854775808 bDesc = 0
62725759
*/
5760
+/* typedef unsigned char u8; */
62735761
typedef struct series_cursor series_cursor;
62745762
struct series_cursor {
62755763
sqlite3_vtab_cursor base; /* Base class - must be first */
6276
- SequenceSpec ss; /* (this) Derived class data */
5764
+ sqlite3_int64 iOBase; /* Original starting value ("start") */
5765
+ sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
5766
+ sqlite3_int64 iOStep; /* Original step value */
5767
+ sqlite3_int64 iBase; /* Starting value to actually use */
5768
+ sqlite3_int64 iTerm; /* Terminal value to actually use */
5769
+ sqlite3_uint64 iStep; /* The step size */
5770
+ sqlite3_int64 iValue; /* Current value */
5771
+ u8 bDesc; /* iStep is really negative */
5772
+ u8 bDone; /* True if stepped past last element */
62775773
};
5774
+
5775
+/*
5776
+** Computed the difference between two 64-bit signed integers using a
5777
+** convoluted computation designed to work around the silly restriction
5778
+** against signed integer overflow in C.
5779
+*/
5780
+static sqlite3_uint64 span64(sqlite3_int64 a, sqlite3_int64 b){
5781
+ assert( a>=b );
5782
+ return (*(sqlite3_uint64*)&a) - (*(sqlite3_uint64*)&b);
5783
+}
5784
+
5785
+/*
5786
+** Add or substract an unsigned 64-bit integer from a signed 64-bit integer
5787
+** and return the new signed 64-bit integer.
5788
+*/
5789
+static sqlite3_int64 add64(sqlite3_int64 a, sqlite3_uint64 b){
5790
+ sqlite3_uint64 x = *(sqlite3_uint64*)&a;
5791
+ x += b;
5792
+ return *(sqlite3_int64*)&x;
5793
+}
5794
+static sqlite3_int64 sub64(sqlite3_int64 a, sqlite3_uint64 b){
5795
+ sqlite3_uint64 x = *(sqlite3_uint64*)&a;
5796
+ x -= b;
5797
+ return *(sqlite3_int64*)&x;
5798
+}
62785799
62795800
/*
62805801
** The seriesConnect() method is invoked to create a new
62815802
** series_vtab that describes the generate_series virtual table.
62825803
**
@@ -6354,11 +5875,19 @@
63545875
/*
63555876
** Advance a series_cursor to its next row of output.
63565877
*/
63575878
static int seriesNext(sqlite3_vtab_cursor *cur){
63585879
series_cursor *pCur = (series_cursor*)cur;
6359
- progressSequence( & pCur->ss );
5880
+ if( pCur->iValue==pCur->iTerm ){
5881
+ pCur->bDone = 1;
5882
+ }else if( pCur->bDesc ){
5883
+ pCur->iValue = sub64(pCur->iValue, pCur->iStep);
5884
+ assert( pCur->iValue>=pCur->iTerm );
5885
+ }else{
5886
+ pCur->iValue = add64(pCur->iValue, pCur->iStep);
5887
+ assert( pCur->iValue<=pCur->iTerm );
5888
+ }
63605889
return SQLITE_OK;
63615890
}
63625891
63635892
/*
63645893
** Return values of columns for the row at which the series_cursor
@@ -6370,41 +5899,41 @@
63705899
int i /* Which column to return */
63715900
){
63725901
series_cursor *pCur = (series_cursor*)cur;
63735902
sqlite3_int64 x = 0;
63745903
switch( i ){
6375
- case SERIES_COLUMN_START: x = pCur->ss.iOBase; break;
6376
- case SERIES_COLUMN_STOP: x = pCur->ss.iOTerm; break;
6377
- case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
6378
- default: x = pCur->ss.iValueNow; break;
5904
+ case SERIES_COLUMN_START: x = pCur->iOBase; break;
5905
+ case SERIES_COLUMN_STOP: x = pCur->iOTerm; break;
5906
+ case SERIES_COLUMN_STEP: x = pCur->iOStep; break;
5907
+ default: x = pCur->iValue; break;
63795908
}
63805909
sqlite3_result_int64(ctx, x);
63815910
return SQLITE_OK;
63825911
}
63835912
63845913
#ifndef LARGEST_UINT64
6385
-#define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
6386
-#define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
6387
-#define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
5914
+#define LARGEST_INT64 ((sqlite3_int64)0x7fffffffffffffffLL)
5915
+#define LARGEST_UINT64 ((sqlite3_uint64)0xffffffffffffffffULL)
5916
+#define SMALLEST_INT64 ((sqlite3_int64)0x8000000000000000LL)
63885917
#endif
63895918
63905919
/*
63915920
** The rowid is the same as the value.
63925921
*/
63935922
static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
63945923
series_cursor *pCur = (series_cursor*)cur;
6395
- *pRowid = pCur->ss.iValueNow;
5924
+ *pRowid = pCur->iValue;
63965925
return SQLITE_OK;
63975926
}
63985927
63995928
/*
64005929
** Return TRUE if the cursor has been moved off of the last
64015930
** row of output.
64025931
*/
64035932
static int seriesEof(sqlite3_vtab_cursor *cur){
64045933
series_cursor *pCur = (series_cursor*)cur;
6405
- return !pCur->ss.isNotEOF;
5934
+ return pCur->bDone;
64065935
}
64075936
64085937
/* True to cause run-time checking of the start=, stop=, and/or step=
64095938
** parameters. The only reason to do this is for testing the
64105939
** constraint checking logic for virtual tables in the SQLite core.
@@ -6411,10 +5940,63 @@
64115940
*/
64125941
#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
64135942
# define SQLITE_SERIES_CONSTRAINT_VERIFY 0
64145943
#endif
64155944
5945
+/*
5946
+** Return the number of steps between pCur->iBase and pCur->iTerm if
5947
+** the step width is pCur->iStep.
5948
+*/
5949
+static sqlite3_uint64 seriesSteps(series_cursor *pCur){
5950
+ if( pCur->bDesc ){
5951
+ assert( pCur->iBase >= pCur->iTerm );
5952
+ return span64(pCur->iBase, pCur->iTerm)/pCur->iStep;
5953
+ }else{
5954
+ assert( pCur->iBase <= pCur->iTerm );
5955
+ return span64(pCur->iTerm, pCur->iBase)/pCur->iStep;
5956
+ }
5957
+}
5958
+
5959
+#if defined(SQLITE_ENABLE_MATH_FUNCTIONS) || defined(_WIN32)
5960
+/*
5961
+** Case 1 (the most common case):
5962
+** The standard math library is available so use ceil() and floor() from there.
5963
+*/
5964
+static double seriesCeil(double r){ return ceil(r); }
5965
+static double seriesFloor(double r){ return floor(r); }
5966
+#elif defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
5967
+/*
5968
+** Case 2 (2nd most common): Use GCC/Clang builtins
5969
+*/
5970
+static double seriesCeil(double r){ return __builtin_ceil(r); }
5971
+static double seriesFloor(double r){ return __builtin_floor(r); }
5972
+#else
5973
+/*
5974
+** Case 3 (rarely happens): Use home-grown ceil() and floor() routines.
5975
+*/
5976
+static double seriesCeil(double r){
5977
+ sqlite3_int64 x;
5978
+ if( r!=r ) return r;
5979
+ if( r<=(-4503599627370496.0) ) return r;
5980
+ if( r>=(+4503599627370496.0) ) return r;
5981
+ x = (sqlite3_int64)r;
5982
+ if( r==(double)x ) return r;
5983
+ if( r>(double)x ) x++;
5984
+ return (double)x;
5985
+}
5986
+static double seriesFloor(double r){
5987
+ sqlite3_int64 x;
5988
+ if( r!=r ) return r;
5989
+ if( r<=(-4503599627370496.0) ) return r;
5990
+ if( r>=(+4503599627370496.0) ) return r;
5991
+ x = (sqlite3_int64)r;
5992
+ if( r==(double)x ) return r;
5993
+ if( r<(double)x ) x--;
5994
+ return (double)x;
5995
+}
5996
+#endif
5997
+
64165998
/*
64175999
** This method is called to "rewind" the series_cursor object back
64186000
** to the first row of output. This method is always called at least
64196001
** once prior to any call to seriesColumn() or seriesRowid() or
64206002
** seriesEof().
@@ -6444,185 +6026,243 @@
64446026
sqlite3_vtab_cursor *pVtabCursor,
64456027
int idxNum, const char *idxStrUnused,
64466028
int argc, sqlite3_value **argv
64476029
){
64486030
series_cursor *pCur = (series_cursor *)pVtabCursor;
6449
- int i = 0;
6450
- int returnNoRows = 0;
6451
- sqlite3_int64 iMin = SMALLEST_INT64;
6452
- sqlite3_int64 iMax = LARGEST_INT64;
6453
- sqlite3_int64 iLimit = 0;
6454
- sqlite3_int64 iOffset = 0;
6031
+ int iArg = 0; /* Arguments used so far */
6032
+ int i; /* Loop counter */
6033
+ sqlite3_int64 iMin = SMALLEST_INT64; /* Smallest allowed output value */
6034
+ sqlite3_int64 iMax = LARGEST_INT64; /* Largest allowed output value */
6035
+ sqlite3_int64 iLimit = 0; /* if >0, the value of the LIMIT */
6036
+ sqlite3_int64 iOffset = 0; /* if >0, the value of the OFFSET */
64556037
64566038
(void)idxStrUnused;
6039
+
6040
+ /* If any constraints have a NULL value, then return no rows.
6041
+ ** See ticket https://sqlite.org/src/info/fac496b61722daf2
6042
+ */
6043
+ for(i=0; i<argc; i++){
6044
+ if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6045
+ goto series_no_rows;
6046
+ }
6047
+ }
6048
+
6049
+ /* Capture the three HIDDEN parameters to the virtual table and insert
6050
+ ** default values for any parameters that are omitted.
6051
+ */
64576052
if( idxNum & 0x01 ){
6458
- pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6053
+ pCur->iOBase = sqlite3_value_int64(argv[iArg++]);
64596054
}else{
6460
- pCur->ss.iBase = 0;
6055
+ pCur->iOBase = 0;
64616056
}
64626057
if( idxNum & 0x02 ){
6463
- pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6058
+ pCur->iOTerm = sqlite3_value_int64(argv[iArg++]);
64646059
}else{
6465
- pCur->ss.iTerm = 0xffffffff;
6060
+ pCur->iOTerm = 0xffffffff;
64666061
}
64676062
if( idxNum & 0x04 ){
6468
- pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6469
- if( pCur->ss.iStep==0 ){
6470
- pCur->ss.iStep = 1;
6471
- }else if( pCur->ss.iStep<0 ){
6472
- if( (idxNum & 0x10)==0 ) idxNum |= 0x08;
6473
- }
6063
+ pCur->iOStep = sqlite3_value_int64(argv[iArg++]);
6064
+ if( pCur->iOStep==0 ) pCur->iOStep = 1;
64746065
}else{
6475
- pCur->ss.iStep = 1;
6066
+ pCur->iOStep = 1;
64766067
}
64776068
64786069
/* If there are constraints on the value column but there are
64796070
** no constraints on the start, stop, and step columns, then
64806071
** initialize the default range to be the entire range of 64-bit signed
64816072
** integers. This range will contracted by the value column constraints
64826073
** further below.
64836074
*/
64846075
if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
6485
- pCur->ss.iBase = SMALLEST_INT64;
6076
+ pCur->iOBase = SMALLEST_INT64;
64866077
}
64876078
if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
6488
- pCur->ss.iTerm = LARGEST_INT64;
6079
+ pCur->iOTerm = LARGEST_INT64;
64896080
}
6490
- pCur->ss.iOBase = pCur->ss.iBase;
6491
- pCur->ss.iOTerm = pCur->ss.iTerm;
6081
+ pCur->iBase = pCur->iOBase;
6082
+ pCur->iTerm = pCur->iOTerm;
6083
+ if( pCur->iOStep>0 ){
6084
+ pCur->iStep = pCur->iOStep;
6085
+ }else if( pCur->iOStep>SMALLEST_INT64 ){
6086
+ pCur->iStep = -pCur->iOStep;
6087
+ }else{
6088
+ pCur->iStep = LARGEST_INT64;
6089
+ pCur->iStep++;
6090
+ }
6091
+ pCur->bDesc = pCur->iOStep<0;
6092
+ if( pCur->bDesc==0 && pCur->iBase>pCur->iTerm ){
6093
+ goto series_no_rows;
6094
+ }
6095
+ if( pCur->bDesc!=0 && pCur->iBase<pCur->iTerm ){
6096
+ goto series_no_rows;
6097
+ }
64926098
64936099
/* Extract the LIMIT and OFFSET values, but do not apply them yet.
64946100
** The range must first be constrained by the limits on value.
64956101
*/
64966102
if( idxNum & 0x20 ){
6497
- iLimit = sqlite3_value_int64(argv[i++]);
6103
+ iLimit = sqlite3_value_int64(argv[iArg++]);
64986104
if( idxNum & 0x40 ){
6499
- iOffset = sqlite3_value_int64(argv[i++]);
6105
+ iOffset = sqlite3_value_int64(argv[iArg++]);
65006106
}
65016107
}
65026108
6109
+ /* Narrow the range of iMin and iMax (the minimum and maximum outputs)
6110
+ ** based on equality and inequality constraints on the "value" column.
6111
+ */
65036112
if( idxNum & 0x3380 ){
6504
- /* Extract the maximum range of output values determined by
6505
- ** constraints on the "value" column.
6506
- */
6507
- if( idxNum & 0x0080 ){
6508
- if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6509
- double r = sqlite3_value_double(argv[i++]);
6510
- if( r==ceil(r) ){
6113
+ if( idxNum & 0x0080 ){ /* value=X */
6114
+ if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6115
+ double r = sqlite3_value_double(argv[iArg++]);
6116
+ if( r==seriesCeil(r)
6117
+ && r>=(double)SMALLEST_INT64
6118
+ && r<=(double)LARGEST_INT64
6119
+ ){
65116120
iMin = iMax = (sqlite3_int64)r;
65126121
}else{
6513
- returnNoRows = 1;
6514
- }
6515
- }else{
6516
- iMin = iMax = sqlite3_value_int64(argv[i++]);
6517
- }
6518
- }else{
6519
- if( idxNum & 0x0300 ){
6520
- if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6521
- double r = sqlite3_value_double(argv[i++]);
6522
- if( idxNum & 0x0200 && r==ceil(r) ){
6523
- iMin = (sqlite3_int64)ceil(r+1.0);
6524
- }else{
6525
- iMin = (sqlite3_int64)ceil(r);
6526
- }
6527
- }else{
6528
- iMin = sqlite3_value_int64(argv[i++]);
6529
- if( idxNum & 0x0200 ){
6122
+ goto series_no_rows;
6123
+ }
6124
+ }else{
6125
+ iMin = iMax = sqlite3_value_int64(argv[iArg++]);
6126
+ }
6127
+ }else{
6128
+ if( idxNum & 0x0300 ){ /* value>X or value>=X */
6129
+ if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6130
+ double r = sqlite3_value_double(argv[iArg++]);
6131
+ if( r<(double)SMALLEST_INT64 ){
6132
+ iMin = SMALLEST_INT64;
6133
+ }else if( (idxNum & 0x0200)!=0 && r==seriesCeil(r) ){
6134
+ iMin = (sqlite3_int64)seriesCeil(r+1.0);
6135
+ }else{
6136
+ iMin = (sqlite3_int64)seriesCeil(r);
6137
+ }
6138
+ }else{
6139
+ iMin = sqlite3_value_int64(argv[iArg++]);
6140
+ if( (idxNum & 0x0200)!=0 ){
65306141
if( iMin==LARGEST_INT64 ){
6531
- returnNoRows = 1;
6142
+ goto series_no_rows;
65326143
}else{
65336144
iMin++;
65346145
}
65356146
}
65366147
}
65376148
}
6538
- if( idxNum & 0x3000 ){
6539
- if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6540
- double r = sqlite3_value_double(argv[i++]);
6541
- if( (idxNum & 0x2000)!=0 && r==floor(r) ){
6149
+ if( idxNum & 0x3000 ){ /* value<X or value<=X */
6150
+ if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6151
+ double r = sqlite3_value_double(argv[iArg++]);
6152
+ if( r>(double)LARGEST_INT64 ){
6153
+ iMax = LARGEST_INT64;
6154
+ }else if( (idxNum & 0x2000)!=0 && r==seriesFloor(r) ){
65426155
iMax = (sqlite3_int64)(r-1.0);
65436156
}else{
6544
- iMax = (sqlite3_int64)floor(r);
6157
+ iMax = (sqlite3_int64)seriesFloor(r);
65456158
}
65466159
}else{
6547
- iMax = sqlite3_value_int64(argv[i++]);
6160
+ iMax = sqlite3_value_int64(argv[iArg++]);
65486161
if( idxNum & 0x2000 ){
65496162
if( iMax==SMALLEST_INT64 ){
6550
- returnNoRows = 1;
6163
+ goto series_no_rows;
65516164
}else{
65526165
iMax--;
65536166
}
65546167
}
65556168
}
65566169
}
65576170
if( iMin>iMax ){
6558
- returnNoRows = 1;
6171
+ goto series_no_rows;
65596172
}
65606173
}
65616174
65626175
/* Try to reduce the range of values to be generated based on
65636176
** constraints on the "value" column.
65646177
*/
6565
- if( pCur->ss.iStep>0 ){
6566
- sqlite3_int64 szStep = pCur->ss.iStep;
6567
- if( pCur->ss.iBase<iMin ){
6568
- sqlite3_uint64 d = iMin - pCur->ss.iBase;
6569
- pCur->ss.iBase += ((d+szStep-1)/szStep)*szStep;
6570
- }
6571
- if( pCur->ss.iTerm>iMax ){
6572
- pCur->ss.iTerm = iMax;
6178
+ if( pCur->bDesc==0 ){
6179
+ if( pCur->iBase<iMin ){
6180
+ sqlite3_uint64 span = span64(iMin,pCur->iBase);
6181
+ pCur->iBase = add64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
6182
+ if( pCur->iBase<iMin ){
6183
+ if( pCur->iBase > sub64(LARGEST_INT64, pCur->iStep) ){
6184
+ goto series_no_rows;
6185
+ }
6186
+ pCur->iBase = add64(pCur->iBase, pCur->iStep);
6187
+ }
6188
+ }
6189
+ if( pCur->iTerm>iMax ){
6190
+ pCur->iTerm = iMax;
65736191
}
65746192
}else{
6575
- sqlite3_int64 szStep = -pCur->ss.iStep;
6576
- assert( szStep>0 );
6577
- if( pCur->ss.iBase>iMax ){
6578
- sqlite3_uint64 d = pCur->ss.iBase - iMax;
6579
- pCur->ss.iBase -= ((d+szStep-1)/szStep)*szStep;
6580
- }
6581
- if( pCur->ss.iTerm<iMin ){
6582
- pCur->ss.iTerm = iMin;
6193
+ if( pCur->iBase>iMax ){
6194
+ sqlite3_uint64 span = span64(pCur->iBase,iMax);
6195
+ pCur->iBase = sub64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
6196
+ if( pCur->iBase>iMax ){
6197
+ if( pCur->iBase < add64(SMALLEST_INT64, pCur->iStep) ){
6198
+ goto series_no_rows;
6199
+ }
6200
+ pCur->iBase = sub64(pCur->iBase, pCur->iStep);
6201
+ }
6202
+ }
6203
+ if( pCur->iTerm<iMin ){
6204
+ pCur->iTerm = iMin;
65836205
}
65846206
}
65856207
}
6208
+
6209
+ /* Adjust iTerm so that it is exactly the last value of the series.
6210
+ */
6211
+ if( pCur->bDesc==0 ){
6212
+ if( pCur->iBase>pCur->iTerm ){
6213
+ goto series_no_rows;
6214
+ }
6215
+ pCur->iTerm = sub64(pCur->iTerm,
6216
+ span64(pCur->iTerm,pCur->iBase) % pCur->iStep);
6217
+ }else{
6218
+ if( pCur->iBase<pCur->iTerm ){
6219
+ goto series_no_rows;
6220
+ }
6221
+ pCur->iTerm = add64(pCur->iTerm,
6222
+ span64(pCur->iBase,pCur->iTerm) % pCur->iStep);
6223
+ }
6224
+
6225
+ /* Transform the series generator to output values in the requested
6226
+ ** order.
6227
+ */
6228
+ if( ((idxNum & 0x0008)!=0 && pCur->bDesc==0)
6229
+ || ((idxNum & 0x0010)!=0 && pCur->bDesc!=0)
6230
+ ){
6231
+ sqlite3_int64 tmp = pCur->iBase;
6232
+ pCur->iBase = pCur->iTerm;
6233
+ pCur->iTerm = tmp;
6234
+ pCur->bDesc = !pCur->bDesc;
6235
+ }
65866236
65876237
/* Apply LIMIT and OFFSET constraints, if any */
6238
+ assert( pCur->iStep!=0 );
65886239
if( idxNum & 0x20 ){
65896240
if( iOffset>0 ){
6590
- pCur->ss.iBase += pCur->ss.iStep*iOffset;
6591
- }
6592
- if( iLimit>=0 ){
6593
- sqlite3_int64 iTerm;
6594
- iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
6595
- if( pCur->ss.iStep<0 ){
6596
- if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6597
- }else{
6598
- if( iTerm<pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6599
- }
6600
- }
6601
- }
6602
-
6603
-
6604
- for(i=0; i<argc; i++){
6605
- if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6606
- /* If any of the constraints have a NULL value, then return no rows.
6607
- ** See ticket https://sqlite.org/src/info/fac496b61722daf2 */
6608
- returnNoRows = 1;
6609
- break;
6610
- }
6611
- }
6612
- if( returnNoRows ){
6613
- pCur->ss.iBase = 1;
6614
- pCur->ss.iTerm = 0;
6615
- pCur->ss.iStep = 1;
6616
- }
6617
- if( idxNum & 0x08 ){
6618
- pCur->ss.isReversing = pCur->ss.iStep > 0;
6619
- }else{
6620
- pCur->ss.isReversing = pCur->ss.iStep < 0;
6621
- }
6622
- setupSequence( &pCur->ss );
6241
+ if( seriesSteps(pCur) < (sqlite3_uint64)iOffset ){
6242
+ goto series_no_rows;
6243
+ }else if( pCur->bDesc ){
6244
+ pCur->iBase = sub64(pCur->iBase, pCur->iStep*iOffset);
6245
+ }else{
6246
+ pCur->iBase = add64(pCur->iBase, pCur->iStep*iOffset);
6247
+ }
6248
+ }
6249
+ if( iLimit>=0 && seriesSteps(pCur) > (sqlite3_uint64)iLimit ){
6250
+ pCur->iTerm = add64(pCur->iBase, (iLimit - 1)*pCur->iStep);
6251
+ }
6252
+ }
6253
+ pCur->iValue = pCur->iBase;
6254
+ pCur->bDone = 0;
66236255
return SQLITE_OK;
6256
+
6257
+series_no_rows:
6258
+ pCur->iBase = 0;
6259
+ pCur->iTerm = 0;
6260
+ pCur->iStep = 1;
6261
+ pCur->bDesc = 0;
6262
+ pCur->bDone = 1;
6263
+ return SQLITE_OK;
66246264
}
66256265
66266266
/*
66276267
** SQLite will invoke this method one or more times while planning a query
66286268
** that uses the generate_series virtual table. This routine needs to create
@@ -6948,10 +6588,12 @@
69486588
** expression and M is the size of the input string. The matcher never
69496589
** exhibits exponential behavior. Note that the X{p,q} operator expands
69506590
** to p copies of X following by q-p copies of X? and that the size of the
69516591
** regular expression in the O(N*M) performance bound is computed after
69526592
** this expansion.
6593
+**
6594
+** To help prevent DoS attacks, the maximum size of the NFA is restricted.
69536595
*/
69546596
#include <string.h>
69556597
#include <stdlib.h>
69566598
/* #include "sqlite3ext.h" */
69576599
SQLITE_EXTENSION_INIT1
@@ -6988,36 +6630,10 @@
69886630
#define RE_OP_NOTDIGIT 14 /* Not a digit */
69896631
#define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
69906632
#define RE_OP_NOTSPACE 16 /* Not a digit */
69916633
#define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
69926634
#define RE_OP_ATSTART 18 /* Currently at the start of the string */
6993
-
6994
-#if defined(SQLITE_DEBUG)
6995
-/* Opcode names used for symbolic debugging */
6996
-static const char *ReOpName[] = {
6997
- "EOF",
6998
- "MATCH",
6999
- "ANY",
7000
- "ANYSTAR",
7001
- "FORK",
7002
- "GOTO",
7003
- "ACCEPT",
7004
- "CC_INC",
7005
- "CC_EXC",
7006
- "CC_VALUE",
7007
- "CC_RANGE",
7008
- "WORD",
7009
- "NOTWORD",
7010
- "DIGIT",
7011
- "NOTDIGIT",
7012
- "SPACE",
7013
- "NOTSPACE",
7014
- "BOUNDARY",
7015
- "ATSTART",
7016
-};
7017
-#endif /* SQLITE_DEBUG */
7018
-
70196635
70206636
/* Each opcode is a "state" in the NFA */
70216637
typedef unsigned short ReStateNumber;
70226638
70236639
/* Because this is an NFA and not a DFA, multiple states can be active at
@@ -7051,10 +6667,11 @@
70516667
unsigned (*xNextChar)(ReInput*); /* Next character function */
70526668
unsigned char zInit[12]; /* Initial text to match */
70536669
int nInit; /* Number of bytes in zInit */
70546670
unsigned nState; /* Number of entries in aOp[] and aArg[] */
70556671
unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
6672
+ unsigned mxAlloc; /* Complexity limit */
70566673
};
70576674
70586675
/* Add a state to the given state set if it is not already there */
70596676
static void re_add_state(ReStateSet *pSet, int newState){
70606677
unsigned i;
@@ -7265,18 +6882,19 @@
72656882
return rc;
72666883
}
72676884
72686885
/* Resize the opcode and argument arrays for an RE under construction.
72696886
*/
7270
-static int re_resize(ReCompiled *p, int N){
6887
+static int re_resize(ReCompiled *p, unsigned int N){
72716888
char *aOp;
72726889
int *aArg;
6890
+ if( N>p->mxAlloc ){ p->zErr = "REGEXP pattern too big"; return 1; }
72736891
aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
7274
- if( aOp==0 ) return 1;
6892
+ if( aOp==0 ){ p->zErr = "out of memory"; return 1; }
72756893
p->aOp = aOp;
72766894
aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
7277
- if( aArg==0 ) return 1;
6895
+ if( aArg==0 ){ p->zErr = "out of memory"; return 1; }
72786896
p->aArg = aArg;
72796897
p->nAlloc = N;
72806898
return 0;
72816899
}
72826900
@@ -7303,11 +6921,11 @@
73036921
}
73046922
73056923
/* Make a copy of N opcodes starting at iStart onto the end of the RE
73066924
** under construction.
73076925
*/
7308
-static void re_copy(ReCompiled *p, int iStart, int N){
6926
+static void re_copy(ReCompiled *p, int iStart, unsigned int N){
73096927
if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
73106928
memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
73116929
memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
73126930
p->nState += N;
73136931
}
@@ -7456,22 +7074,30 @@
74567074
case '^': {
74577075
re_append(p, RE_OP_ATSTART, 0);
74587076
break;
74597077
}
74607078
case '{': {
7461
- int m = 0, n = 0;
7462
- int sz, j;
7079
+ unsigned int m = 0, n = 0;
7080
+ unsigned int sz, j;
74637081
if( iPrev<0 ) return "'{m,n}' without operand";
7464
- while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
7082
+ while( (c=rePeek(p))>='0' && c<='9' ){
7083
+ m = m*10 + c - '0';
7084
+ if( m*2>p->mxAlloc ) return "REGEXP pattern too big";
7085
+ p->sIn.i++;
7086
+ }
74657087
n = m;
74667088
if( c==',' ){
74677089
p->sIn.i++;
74687090
n = 0;
7469
- while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
7091
+ while( (c=rePeek(p))>='0' && c<='9' ){
7092
+ n = n*10 + c-'0';
7093
+ if( n*2>p->mxAlloc ) return "REGEXP pattern too big";
7094
+ p->sIn.i++;
7095
+ }
74707096
}
74717097
if( c!='}' ) return "unmatched '{'";
7472
- if( n>0 && n<m ) return "n less than m in '{m,n}'";
7098
+ if( n<m ) return "n less than m in '{m,n}'";
74737099
p->sIn.i++;
74747100
sz = p->nState - iPrev;
74757101
if( m==0 ){
74767102
if( n==0 ) return "both m and n are zero in '{m,n}'";
74777103
re_insert(p, iPrev, RE_OP_FORK, sz+1);
@@ -7483,11 +7109,11 @@
74837109
for(j=m; j<n; j++){
74847110
re_append(p, RE_OP_FORK, sz+1);
74857111
re_copy(p, iPrev, sz);
74867112
}
74877113
if( n==0 && m>0 ){
7488
- re_append(p, RE_OP_FORK, -sz);
7114
+ re_append(p, RE_OP_FORK, -(int)sz);
74897115
}
74907116
break;
74917117
}
74927118
case '[': {
74937119
unsigned int iFirst = p->nState;
@@ -7549,12 +7175,11 @@
75497175
75507176
/* Free and reclaim all the memory used by a previously compiled
75517177
** regular expression. Applications should invoke this routine once
75527178
** for every call to re_compile() to avoid memory leaks.
75537179
*/
7554
-static void re_free(void *p){
7555
- ReCompiled *pRe = (ReCompiled*)p;
7180
+static void re_free(ReCompiled *pRe){
75567181
if( pRe ){
75577182
sqlite3_free(pRe->aOp);
75587183
sqlite3_free(pRe->aArg);
75597184
sqlite3_free(pRe);
75607185
}
@@ -7564,11 +7189,16 @@
75647189
** Compile a textual regular expression in zIn[] into a compiled regular
75657190
** expression suitable for us by re_match() and return a pointer to the
75667191
** compiled regular expression in *ppRe. Return NULL on success or an
75677192
** error message if something goes wrong.
75687193
*/
7569
-static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
7194
+static const char *re_compile(
7195
+ ReCompiled **ppRe, /* OUT: write compiled NFA here */
7196
+ const char *zIn, /* Input regular expression */
7197
+ int mxRe, /* Complexity limit */
7198
+ int noCase /* True for caseless comparisons */
7199
+){
75707200
ReCompiled *pRe;
75717201
const char *zErr;
75727202
int i, j;
75737203
75747204
*ppRe = 0;
@@ -7576,13 +7206,15 @@
75767206
if( pRe==0 ){
75777207
return "out of memory";
75787208
}
75797209
memset(pRe, 0, sizeof(*pRe));
75807210
pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
7211
+ pRe->mxAlloc = mxRe;
75817212
if( re_resize(pRe, 30) ){
7213
+ zErr = pRe->zErr;
75827214
re_free(pRe);
7583
- return "out of memory";
7215
+ return zErr;
75847216
}
75857217
if( zIn[0]=='^' ){
75867218
zIn++;
75877219
}else{
75887220
re_append(pRe, RE_OP_ANYSTAR, 0);
@@ -7630,10 +7262,18 @@
76307262
if( j>0 && pRe->zInit[j-1]==0 ) j--;
76317263
pRe->nInit = j;
76327264
}
76337265
return pRe->zErr;
76347266
}
7267
+
7268
+/*
7269
+** Compute a reasonable limit on the length of the REGEXP NFA.
7270
+*/
7271
+static int re_maxlen(sqlite3_context *context){
7272
+ sqlite3 *db = sqlite3_context_db_handle(context);
7273
+ return 75 + sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH,-1)/2;
7274
+}
76357275
76367276
/*
76377277
** Implementation of the regexp() SQL function. This function implements
76387278
** the build-in REGEXP operator. The first argument to the function is the
76397279
** pattern and the second argument is the string. So, the SQL statements:
@@ -7656,11 +7296,12 @@
76567296
(void)argc; /* Unused */
76577297
pRe = sqlite3_get_auxdata(context, 0);
76587298
if( pRe==0 ){
76597299
zPattern = (const char*)sqlite3_value_text(argv[0]);
76607300
if( zPattern==0 ) return;
7661
- zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
7301
+ zErr = re_compile(&pRe, zPattern, re_maxlen(context),
7302
+ sqlite3_user_data(context)!=0);
76627303
if( zErr ){
76637304
re_free(pRe);
76647305
sqlite3_result_error(context, zErr, -1);
76657306
return;
76667307
}
@@ -7698,14 +7339,36 @@
76987339
sqlite3_str *pStr;
76997340
int i;
77007341
int n;
77017342
char *z;
77027343
(void)argc;
7344
+ static const char *ReOpName[] = {
7345
+ "EOF",
7346
+ "MATCH",
7347
+ "ANY",
7348
+ "ANYSTAR",
7349
+ "FORK",
7350
+ "GOTO",
7351
+ "ACCEPT",
7352
+ "CC_INC",
7353
+ "CC_EXC",
7354
+ "CC_VALUE",
7355
+ "CC_RANGE",
7356
+ "WORD",
7357
+ "NOTWORD",
7358
+ "DIGIT",
7359
+ "NOTDIGIT",
7360
+ "SPACE",
7361
+ "NOTSPACE",
7362
+ "BOUNDARY",
7363
+ "ATSTART",
7364
+ };
77037365
77047366
zPattern = (const char*)sqlite3_value_text(argv[0]);
77057367
if( zPattern==0 ) return;
7706
- zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
7368
+ zErr = re_compile(&pRe, zPattern, re_maxlen(context),
7369
+ sqlite3_user_data(context)!=0);
77077370
if( zErr ){
77087371
re_free(pRe);
77097372
sqlite3_result_error(context, zErr, -1);
77107373
return;
77117374
}
@@ -9284,18 +8947,20 @@
92848947
if( idxNum & 1 ){
92858948
pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
92868949
if( pCur->nPrefix>0 ){
92878950
pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
92888951
if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8952
+ pCur->nPrefix = (int)strlen(pCur->zPrefix);
92898953
}
92908954
iArg = 1;
92918955
}
92928956
if( idxNum & 2 ){
92938957
pCur->nLine = sqlite3_value_bytes(argv[iArg]);
92948958
if( pCur->nLine>0 ){
92958959
pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
92968960
if( pCur->zLine==0 ) return SQLITE_NOMEM;
8961
+ pCur->nLine = (int)strlen(pCur->zLine);
92978962
}
92988963
}
92998964
if( pCur->zLine!=0 && pCur->zPrefix==0 ){
93008965
int i = pCur->nLine;
93018966
while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
@@ -9303,10 +8968,11 @@
93038968
}
93048969
pCur->nPrefix = pCur->nLine - i;
93058970
if( pCur->nPrefix>0 ){
93068971
pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
93078972
if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8973
+ pCur->nPrefix = (int)strlen(pCur->zPrefix);
93088974
}
93098975
}
93108976
pCur->iRowid = 0;
93118977
pCur->ePhase = COMPLETION_FIRST_PHASE;
93128978
return completionNext(pVtabCursor);
@@ -10216,11 +9882,17 @@
102169882
"method," /* 6: Compression method (integer) */
102179883
"z HIDDEN" /* 7: Name of zip file */
102189884
") WITHOUT ROWID;";
102199885
102209886
#define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
10221
-#define ZIPFILE_BUFFER_SIZE (64*1024)
9887
+#define ZIPFILE_MX_NAME (250) /* Windows limitation on filename size */
9888
+
9889
+/*
9890
+** The buffer should be large enough to contain 3 65536 byte strings - the
9891
+** filename, the extra field and the file comment.
9892
+*/
9893
+#define ZIPFILE_BUFFER_SIZE (200*1024)
102229894
102239895
102249896
/*
102259897
** Magic numbers used to read and write zip files.
102269898
**
@@ -10773,10 +10445,11 @@
1077310445
pLFH->crc32 = zipfileRead32(aRead);
1077410446
pLFH->szCompressed = zipfileRead32(aRead);
1077510447
pLFH->szUncompressed = zipfileRead32(aRead);
1077610448
pLFH->nFile = zipfileRead16(aRead);
1077710449
pLFH->nExtra = zipfileRead16(aRead);
10450
+ if( pLFH->nFile>ZIPFILE_MX_NAME ) rc = SQLITE_ERROR;
1077810451
}
1077910452
return rc;
1078010453
}
1078110454
1078210455
@@ -10898,10 +10571,19 @@
1089810571
|| mUnixTime==zipfileMtime(pCds)
1089910572
|| ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
1090010573
/* || (mUnixTime % 2) */
1090110574
);
1090210575
}
10576
+
10577
+/*
10578
+** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a
10579
+** generic corruption message and return SQLITE_CORRUPT;
10580
+*/
10581
+static int zipfileCorrupt(char **pzErr){
10582
+ *pzErr = sqlite3_mprintf("zip archive is corrupt");
10583
+ return SQLITE_CORRUPT;
10584
+}
1090310585
1090410586
/*
1090510587
** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
1090610588
** size) containing an entire zip archive image. Or, if aBlob is NULL,
1090710589
** then pFile is a file-handle open on a zip file. In either case, this
@@ -10921,16 +10603,19 @@
1092110603
ZipfileEntry **ppEntry /* OUT: Pointer to new object */
1092210604
){
1092310605
u8 *aRead;
1092410606
char **pzErr = &pTab->base.zErrMsg;
1092510607
int rc = SQLITE_OK;
10926
- (void)nBlob;
1092710608
1092810609
if( aBlob==0 ){
1092910610
aRead = pTab->aBuffer;
1093010611
rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
1093110612
}else{
10613
+ if( (iOff+ZIPFILE_CDS_FIXED_SZ)>nBlob ){
10614
+ /* Not enough data for the CDS structure. Corruption. */
10615
+ return zipfileCorrupt(pzErr);
10616
+ }
1093210617
aRead = (u8*)&aBlob[iOff];
1093310618
}
1093410619
1093510620
if( rc==SQLITE_OK ){
1093610621
sqlite3_int64 nAlloc;
@@ -10957,10 +10642,13 @@
1095710642
rc = zipfileReadData(
1095810643
pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
1095910644
);
1096010645
}else{
1096110646
aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
10647
+ if( (iOff + ZIPFILE_LFH_FIXED_SZ + nFile + nExtra)>nBlob ){
10648
+ rc = zipfileCorrupt(pzErr);
10649
+ }
1096210650
}
1096310651
}
1096410652
1096510653
if( rc==SQLITE_OK ){
1096610654
u32 *pt = &pNew->mUnixTime;
@@ -10979,19 +10667,26 @@
1097910667
ZipfileLFH lfh;
1098010668
if( pFile ){
1098110669
rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
1098210670
}else{
1098310671
aRead = (u8*)&aBlob[pNew->cds.iOffset];
10672
+ if( (pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>nBlob ){
10673
+ rc = zipfileCorrupt(pzErr);
10674
+ }
1098410675
}
1098510676
1098610677
if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
1098710678
if( rc==SQLITE_OK ){
1098810679
pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
1098910680
pNew->iDataOff += lfh.nFile + lfh.nExtra;
1099010681
if( aBlob && pNew->cds.szCompressed ){
10991
- pNew->aData = &pNew->aExtra[nExtra];
10992
- memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
10682
+ if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
10683
+ rc = zipfileCorrupt(pzErr);
10684
+ }else{
10685
+ pNew->aData = &pNew->aExtra[nExtra];
10686
+ memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
10687
+ }
1099310688
}
1099410689
}else{
1099510690
*pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
1099610691
(int)pNew->cds.iOffset
1099710692
);
@@ -11774,10 +11469,15 @@
1177411469
1177511470
if( rc==SQLITE_OK ){
1177611471
zPath = (const char*)sqlite3_value_text(apVal[2]);
1177711472
if( zPath==0 ) zPath = "";
1177811473
nPath = (int)strlen(zPath);
11474
+ if( nPath>ZIPFILE_MX_NAME ){
11475
+ zipfileTableErr(pTab, "filename too long; max: %d bytes",
11476
+ ZIPFILE_MX_NAME);
11477
+ rc = SQLITE_CONSTRAINT;
11478
+ }
1177911479
mTime = zipfileGetTime(apVal[4]);
1178011480
}
1178111481
1178211482
if( rc==SQLITE_OK && bIsDir ){
1178311483
/* For a directory, check that the last character in the path is a
@@ -12135,10 +11835,17 @@
1213511835
if( zName==0 ){
1213611836
zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
1213711837
rc = SQLITE_ERROR;
1213811838
goto zipfile_step_out;
1213911839
}
11840
+ if( nName>ZIPFILE_MX_NAME ){
11841
+ zErr = sqlite3_mprintf(
11842
+ "filename argument to zipfile() too big; max: %d bytes",
11843
+ ZIPFILE_MX_NAME);
11844
+ rc = SQLITE_ERROR;
11845
+ goto zipfile_step_out;
11846
+ }
1214011847
1214111848
/* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
1214211849
** deflate compression) or NULL (choose automatically). */
1214311850
if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
1214411851
iMethod = (int)sqlite3_value_int64(pMethod);
@@ -12477,10 +12184,11 @@
1247712184
return rc;
1247812185
}
1247912186
1248012187
/************************* End ../ext/misc/sqlar.c ********************/
1248112188
#endif
12189
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
1248212190
/************************* Begin ../ext/expert/sqlite3expert.h ******************/
1248312191
/*
1248412192
** 2017 April 07
1248512193
**
1248612194
** The author disclaims copyright to this source code. In place of
@@ -14885,10 +14593,11 @@
1488514593
}
1488614594
1488714595
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1488814596
1488914597
/************************* End ../ext/expert/sqlite3expert.c ********************/
14598
+#endif
1489014599
/************************* Begin ../ext/intck/sqlite3intck.h ******************/
1489114600
/*
1489214601
** 2024-02-08
1489314602
**
1489414603
** The author disclaims copyright to this source code. In place of
@@ -21525,15 +21234,17 @@
2152521234
char **azFilter; /* Array of xFilter rejection GLOB patterns */
2152621235
sqlite3_session *p; /* The open session */
2152721236
};
2152821237
#endif
2152921238
21239
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2153021240
typedef struct ExpertInfo ExpertInfo;
2153121241
struct ExpertInfo {
2153221242
sqlite3expert *pExpert;
2153321243
int bVerbose;
2153421244
};
21245
+#endif
2153521246
2153621247
/* A single line in the EQP output */
2153721248
typedef struct EQPGraphRow EQPGraphRow;
2153821249
struct EQPGraphRow {
2153921250
int iEqpId; /* ID for this row */
@@ -21633,11 +21344,13 @@
2163321344
int *aiIndent; /* Array of indents used in MODE_Explain */
2163421345
int nIndent; /* Size of array aiIndent[] */
2163521346
int iIndent; /* Index of current op in aiIndent[] */
2163621347
char *zNonce; /* Nonce for temporary safe-mode escapes */
2163721348
EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
21349
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2163821350
ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
21351
+#endif
2163921352
#ifdef SQLITE_SHELL_FIDDLE
2164021353
struct {
2164121354
const char * zInput; /* Input string from wasm/JS proxy */
2164221355
const char * zPos; /* Cursor pos into zInput */
2164321356
const char * zDefaultDbName; /* Default name for db file */
@@ -21661,13 +21374,12 @@
2166121374
*/
2166221375
#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
2166321376
#define SHELL_OPEN_NORMAL 1 /* Normal database file */
2166421377
#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
2166521378
#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
21666
-#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
21667
-#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
21668
-#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
21379
+#define SHELL_OPEN_DESERIALIZE 4 /* Open using sqlite3_deserialize() */
21380
+#define SHELL_OPEN_HEXDB 5 /* Use "dbtotxt" output as data source */
2166921381
2167021382
/* Allowed values for ShellState.eTraceType
2167121383
*/
2167221384
#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
2167321385
#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
@@ -22006,11 +21718,11 @@
2200621718
*/
2200721719
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
2200821720
int i;
2200921721
unsigned char *aBlob = (unsigned char*)pBlob;
2201021722
22011
- char *zStr = sqlite3_malloc(nBlob*2 + 1);
21723
+ char *zStr = sqlite3_malloc64((i64)nBlob*2 + 1);
2201221724
shell_check_oom(zStr);
2201321725
2201421726
for(i=0; i<nBlob; i++){
2201521727
static const char aHex[] = {
2201621728
'0', '1', '2', '3', '4', '5', '6', '7',
@@ -22027,11 +21739,11 @@
2202721739
2202821740
/*
2202921741
** Output the given string as a quoted string using SQL quoting conventions:
2203021742
**
2203121743
** (1) Single quotes (') within the string are doubled
22032
-** (2) The whle string is enclosed in '...'
21744
+** (2) The while string is enclosed in '...'
2203321745
** (3) Control characters other than \n, \t, and \r\n are escaped
2203421746
** using \u00XX notation and if such substitutions occur,
2203521747
** the whole string is enclosed in unistr('...') instead of '...'.
2203621748
**
2203721749
** Step (3) is omitted if the control-character escape mode is OFF.
@@ -22273,11 +21985,11 @@
2227321985
**
2227421986
** Escaping is needed if the string contains any control characters
2227521987
** other than \t, \n, and \r\n
2227621988
**
2227721989
** If no escaping is needed (the common case) then set *ppFree to NULL
22278
-** and return the original string. If escapingn is needed, write the
21990
+** and return the original string. If escaping is needed, write the
2227921991
** escaped string into memory obtained from sqlite3_malloc64() or the
2228021992
** equivalent, and return the new string and set *ppFree to the new string
2228121993
** as well.
2228221994
**
2228321995
** The caller is responsible for freeing *ppFree if it is non-NULL in order
@@ -23278,32 +22990,21 @@
2327822990
** Set the destination table field of the ShellState structure to
2327922991
** the name of the table given. Escape any quote characters in the
2328022992
** table name.
2328122993
*/
2328222994
static void set_table_name(ShellState *p, const char *zName){
23283
- int i, n;
23284
- char cQuote;
23285
- char *z;
23286
-
2328722995
if( p->zDestTable ){
23288
- free(p->zDestTable);
22996
+ sqlite3_free(p->zDestTable);
2328922997
p->zDestTable = 0;
2329022998
}
2329122999
if( zName==0 ) return;
23292
- cQuote = quoteChar(zName);
23293
- n = strlen30(zName);
23294
- if( cQuote ) n += n+2;
23295
- z = p->zDestTable = malloc( n+1 );
23296
- shell_check_oom(z);
23297
- n = 0;
23298
- if( cQuote ) z[n++] = cQuote;
23299
- for(i=0; zName[i]; i++){
23300
- z[n++] = zName[i];
23301
- if( zName[i]==cQuote ) z[n++] = cQuote;
23302
- }
23303
- if( cQuote ) z[n++] = cQuote;
23304
- z[n] = 0;
23000
+ if( quoteChar(zName) ){
23001
+ p->zDestTable = sqlite3_mprintf("\"%w\"", zName);
23002
+ }else{
23003
+ p->zDestTable = sqlite3_mprintf("%s", zName);
23004
+ }
23005
+ shell_check_oom(p->zDestTable);
2330523006
}
2330623007
2330723008
/*
2330823009
** Maybe construct two lines of text that point out the position of a
2330923010
** syntax error. Return a pointer to the text, in memory obtained from
@@ -23496,12 +23197,12 @@
2349623197
static int display_stats(
2349723198
sqlite3 *db, /* Database to query */
2349823199
ShellState *pArg, /* Pointer to ShellState */
2349923200
int bReset /* True to reset the stats */
2350023201
){
23501
- int iCur;
23502
- int iHiwtr;
23202
+ int iCur, iHiwtr;
23203
+ sqlite3_int64 iCur64, iHiwtr64;
2350323204
FILE *out;
2350423205
if( pArg==0 || pArg->out==0 ) return 0;
2350523206
out = pArg->out;
2350623207
2350723208
if( pArg->pStmt && pArg->statsOn==2 ){
@@ -23586,18 +23287,25 @@
2358623287
"Page cache hits: %d\n", iCur);
2358723288
iHiwtr = iCur = -1;
2358823289
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2358923290
sqlite3_fprintf(out,
2359023291
"Page cache misses: %d\n", iCur);
23292
+ iHiwtr64 = iCur64 = -1;
23293
+ sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
23294
+ 0);
2359123295
iHiwtr = iCur = -1;
2359223296
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2359323297
sqlite3_fprintf(out,
2359423298
"Page cache writes: %d\n", iCur);
2359523299
iHiwtr = iCur = -1;
2359623300
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2359723301
sqlite3_fprintf(out,
2359823302
"Page cache spills: %d\n", iCur);
23303
+ sqlite3_fprintf(out,
23304
+ "Temporary data spilled to disk: %lld\n", iCur64);
23305
+ sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
23306
+ 1);
2359923307
iHiwtr = iCur = -1;
2360023308
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2360123309
sqlite3_fprintf(out,
2360223310
"Schema Heap Usage: %d bytes\n", iCur);
2360323311
iHiwtr = iCur = -1;
@@ -23999,10 +23707,40 @@
2399923707
char *zBuf = sqlite3_malloc64( szVar-5 );
2400023708
if( zBuf ){
2400123709
memcpy(zBuf, &zVar[6], szVar-5);
2400223710
sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
2400323711
}
23712
+#ifdef SQLITE_ENABLE_CARRAY
23713
+ }else if( strncmp(zVar, "$carray_", 8)==0 ){
23714
+ static char *azColorNames[] = {
23715
+ "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold",
23716
+ "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon",
23717
+ "navy", "olive", "orange", "pink", "purple", "red", "silver",
23718
+ "tan", "teal", "violet", "white", "yellow"
23719
+ };
23720
+ static int aPrimes[] = {
23721
+ 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
23722
+ 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
23723
+ };
23724
+ /* Special bindings: carray($carray_clr), carray($carray_primes)
23725
+ ** with --unsafe-testing: carray($carray_clr_p,26,'char*'),
23726
+ ** carray($carray_primes_p,26,'int32')
23727
+ */
23728
+ if( strcmp(zVar+8,"clr")==0 ){
23729
+ sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0);
23730
+ }else if( strcmp(zVar+8,"primes")==0 ){
23731
+ sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0);
23732
+ }else if( strcmp(zVar+8,"clr_p")==0
23733
+ && ShellHasFlag(pArg,SHFLG_TestingMode) ){
23734
+ sqlite3_bind_pointer(pStmt,i,azColorNames,"carray",0);
23735
+ }else if( strcmp(zVar+8,"primes_p")==0
23736
+ && ShellHasFlag(pArg,SHFLG_TestingMode) ){
23737
+ sqlite3_bind_pointer(pStmt,i,aPrimes,"carray",0);
23738
+ }else{
23739
+ sqlite3_bind_null(pStmt, i);
23740
+ }
23741
+#endif
2400423742
}else{
2400523743
sqlite3_bind_null(pStmt, i);
2400623744
}
2400723745
sqlite3_reset(pQ);
2400823746
}
@@ -24581,11 +24319,11 @@
2458124319
}
2458224320
}
2458324321
}
2458424322
}
2458524323
24586
-#ifndef SQLITE_OMIT_VIRTUALTABLE
24324
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2458724325
/*
2458824326
** This function is called to process SQL if the previous shell command
2458924327
** was ".expert". It passes the SQL in the second argument directly to
2459024328
** the sqlite3expert object.
2459124329
**
@@ -24713,11 +24451,11 @@
2471324451
}
2471424452
sqlite3_free(zErr);
2471524453
2471624454
return rc;
2471724455
}
24718
-#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
24456
+#endif /* !SQLITE_OMIT_VIRTUALTABLE && !SQLITE_OMIT_AUTHORIZATION */
2471924457
2472024458
/*
2472124459
** Execute a statement or set of statements. Print
2472224460
** any result rows/columns depending on the current mode
2472324461
** set via the supplied callback.
@@ -24739,11 +24477,11 @@
2473924477
2474024478
if( pzErrMsg ){
2474124479
*pzErrMsg = NULL;
2474224480
}
2474324481
24744
-#ifndef SQLITE_OMIT_VIRTUALTABLE
24482
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2474524483
if( pArg->expert.pExpert ){
2474624484
rc = expertHandleSQL(pArg, zSql, pzErrMsg);
2474724485
return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
2474824486
}
2474924487
#endif
@@ -24901,11 +24639,11 @@
2490124639
static char **tableColumnList(ShellState *p, const char *zTab){
2490224640
char **azCol = 0;
2490324641
sqlite3_stmt *pStmt;
2490424642
char *zSql;
2490524643
int nCol = 0;
24906
- int nAlloc = 0;
24644
+ i64 nAlloc = 0;
2490724645
int nPK = 0; /* Number of PRIMARY KEY columns seen */
2490824646
int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2490924647
int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2491024648
int rc;
2491124649
@@ -24915,11 +24653,11 @@
2491524653
sqlite3_free(zSql);
2491624654
if( rc ) return 0;
2491724655
while( sqlite3_step(pStmt)==SQLITE_ROW ){
2491824656
if( nCol>=nAlloc-2 ){
2491924657
nAlloc = nAlloc*2 + nCol + 10;
24920
- azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
24658
+ azCol = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0]));
2492124659
shell_check_oom(azCol);
2492224660
}
2492324661
azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2492424662
shell_check_oom(azCol[nCol]);
2492524663
if( sqlite3_column_int(pStmt, 5) ){
@@ -25106,17 +24844,17 @@
2510624844
appendText(&sSelect, " FROM ", 0);
2510724845
appendText(&sSelect, zTable, quoteChar(zTable));
2510824846
2510924847
savedDestTable = p->zDestTable;
2511024848
savedMode = p->mode;
25111
- p->zDestTable = sTable.z;
24849
+ p->zDestTable = sTable.zTxt;
2511224850
p->mode = p->cMode = MODE_Insert;
25113
- rc = shell_exec(p, sSelect.z, 0);
24851
+ rc = shell_exec(p, sSelect.zTxt, 0);
2511424852
if( (rc&0xff)==SQLITE_CORRUPT ){
2511524853
sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
2511624854
toggleSelectOrder(p->db);
25117
- shell_exec(p, sSelect.z, 0);
24855
+ shell_exec(p, sSelect.zTxt, 0);
2511824856
toggleSelectOrder(p->db);
2511924857
}
2512024858
p->zDestTable = savedDestTable;
2512124859
p->mode = savedMode;
2512224860
freeText(&sTable);
@@ -25245,11 +24983,13 @@
2524524983
" --bom Put a UTF8 byte-order mark on intermediate file",
2524624984
#endif
2524724985
#ifndef SQLITE_SHELL_FIDDLE
2524824986
".exit ?CODE? Exit this program with return-code CODE",
2524924987
#endif
24988
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2525024989
".expert EXPERIMENTAL. Suggest indexes for queries",
24990
+#endif
2525124991
".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
2525224992
".filectrl CMD ... Run various sqlite3_file_control() operations",
2525324993
" --schema SCHEMA Use SCHEMA instead of \"main\"",
2525424994
" --help Show CMD details",
2525524995
".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
@@ -25270,11 +25010,11 @@
2527025010
" from the \".mode\" output mode",
2527125011
" * If FILE begins with \"|\" then it is a command that generates the",
2527225012
" input text.",
2527325013
#endif
2527425014
#ifndef SQLITE_OMIT_TEST_CONTROL
25275
- ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
25015
+ ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
2527625016
#endif
2527725017
".indexes ?TABLE? Show names of indexes",
2527825018
" If TABLE is specified, only show indexes for",
2527925019
" tables matching TABLE using the LIKE operator.",
2528025020
".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
@@ -25337,11 +25077,17 @@
2533725077
" Options:",
2533825078
" --append Use appendvfs to append database to the end of FILE",
2533925079
#endif
2534025080
#ifndef SQLITE_OMIT_DESERIALIZE
2534125081
" --deserialize Load into memory using sqlite3_deserialize()",
25082
+#endif
25083
+/*" --exclusive Set the SQLITE_OPEN_EXCLUSIVE flag", UNDOCUMENTED */
25084
+#ifndef SQLITE_OMIT_DESERIALIZE
2534225085
" --hexdb Load the output of \"dbtotxt\" as an in-memory db",
25086
+#endif
25087
+ " --ifexist Only open if FILE already exists",
25088
+#ifndef SQLITE_OMIT_DESERIALIZE
2534325089
" --maxsize N Maximum size for --hexdb or --deserialized database",
2534425090
#endif
2534525091
" --new Initialize FILE to an empty database",
2534625092
" --nofollow Do not follow symbolic links",
2534725093
" --readonly Open FILE readonly",
@@ -25726,14 +25472,14 @@
2572625472
** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
2572725473
*/
2572825474
static unsigned char *readHexDb(ShellState *p, int *pnData){
2572925475
unsigned char *a = 0;
2573025476
int nLine;
25731
- int n = 0;
25477
+ int n = 0; /* Size of db per first line of hex dump */
25478
+ i64 sz = 0; /* n rounded up to nearest page boundary */
2573225479
int pgsz = 0;
25733
- int iOffset = 0;
25734
- int j, k;
25480
+ i64 iOffset = 0;
2573525481
int rc;
2573625482
FILE *in;
2573725483
const char *zDbFilename = p->pAuxDb->zDbFilename;
2573825484
unsigned int x[16];
2573925485
char zLine[1000];
@@ -25753,20 +25499,21 @@
2575325499
nLine++;
2575425500
if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
2575525501
rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
2575625502
if( rc!=2 ) goto readHexDb_error;
2575725503
if( n<0 ) goto readHexDb_error;
25758
- if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
25759
- n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
25760
- a = sqlite3_malloc( n ? n : 1 );
25761
- shell_check_oom(a);
25762
- memset(a, 0, n);
2576325504
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
2576425505
sqlite3_fputs("invalid pagesize\n", stderr);
2576525506
goto readHexDb_error;
2576625507
}
25508
+ sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
25509
+ a = sqlite3_malloc64( sz ? sz : 1 );
25510
+ shell_check_oom(a);
25511
+ memset(a, 0, sz);
2576725512
for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
25513
+ int j = 0; /* Page number from "| page" line */
25514
+ int k = 0; /* Offset from "| page" line */
2576825515
rc = sscanf(zLine, "| page %d offset %d", &j, &k);
2576925516
if( rc==2 ){
2577025517
iOffset = k;
2577125518
continue;
2577225519
}
@@ -25775,18 +25522,18 @@
2577525522
}
2577625523
rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
2577725524
&j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
2577825525
&x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
2577925526
if( rc==17 ){
25780
- k = iOffset+j;
25781
- if( k+16<=n && k>=0 ){
25527
+ i64 iOff = iOffset+j;
25528
+ if( iOff+16<=sz && iOff>=0 ){
2578225529
int ii;
25783
- for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
25530
+ for(ii=0; ii<16; ii++) a[iOff+ii] = x[ii]&0xff;
2578425531
}
2578525532
}
2578625533
}
25787
- *pnData = n;
25534
+ *pnData = sz;
2578825535
if( in!=p->in ){
2578925536
fclose(in);
2579025537
}else{
2579125538
p->lineno = nLine;
2579225539
}
@@ -25849,11 +25596,11 @@
2584925596
p->pLog = pSavedLog;
2585025597
2585125598
if( zFake ){
2585225599
sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
2585325600
-1, sqlite3_free);
25854
- free(zFake);
25601
+ sqlite3_free(zFake);
2585525602
}
2585625603
}
2585725604
2585825605
/* Flags for open_db().
2585925606
**
@@ -25881,14 +25628,17 @@
2588125628
}else{
2588225629
p->openMode = (u8)deduceDatabaseType(zDbFilename,
2588325630
(openFlags & OPEN_DB_ZIPFILE)!=0);
2588425631
}
2588525632
}
25633
+ if( (p->openFlags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE))==0 ){
25634
+ if( p->openFlags==0 ) p->openFlags = SQLITE_OPEN_CREATE;
25635
+ p->openFlags |= SQLITE_OPEN_READWRITE;
25636
+ }
2588625637
switch( p->openMode ){
2588725638
case SHELL_OPEN_APPENDVFS: {
25888
- sqlite3_open_v2(zDbFilename, &p->db,
25889
- SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
25639
+ sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, "apndvfs");
2589025640
break;
2589125641
}
2589225642
case SHELL_OPEN_HEXDB:
2589325643
case SHELL_OPEN_DESERIALIZE: {
2589425644
sqlite3_open(0, &p->db);
@@ -25896,19 +25646,13 @@
2589625646
}
2589725647
case SHELL_OPEN_ZIPFILE: {
2589825648
sqlite3_open(":memory:", &p->db);
2589925649
break;
2590025650
}
25901
- case SHELL_OPEN_READONLY: {
25902
- sqlite3_open_v2(zDbFilename, &p->db,
25903
- SQLITE_OPEN_READONLY|p->openFlags, 0);
25904
- break;
25905
- }
2590625651
case SHELL_OPEN_UNSPEC:
2590725652
case SHELL_OPEN_NORMAL: {
25908
- sqlite3_open_v2(zDbFilename, &p->db,
25909
- SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
25653
+ sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, 0);
2591025654
break;
2591125655
}
2591225656
}
2591325657
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2591425658
sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
@@ -25944,11 +25688,10 @@
2594425688
sqlite3_sha_init(p->db, 0, 0);
2594525689
sqlite3_shathree_init(p->db, 0, 0);
2594625690
sqlite3_uint_init(p->db, 0, 0);
2594725691
sqlite3_stmtrand_init(p->db, 0, 0);
2594825692
sqlite3_decimal_init(p->db, 0, 0);
25949
- sqlite3_percentile_init(p->db, 0, 0);
2595025693
sqlite3_base64_init(p->db, 0, 0);
2595125694
sqlite3_base85_init(p->db, 0, 0);
2595225695
sqlite3_regexp_init(p->db, 0, 0);
2595325696
sqlite3_ieee_init(p->db, 0, 0);
2595425697
sqlite3_series_init(p->db, 0, 0);
@@ -26043,13 +25786,15 @@
2604325786
}
2604425787
}
2604525788
#endif
2604625789
}
2604725790
if( p->db!=0 ){
25791
+#ifndef SQLITE_OMIT_AUTHORIZATION
2604825792
if( p->bSafeModePersist ){
2604925793
sqlite3_set_authorizer(p->db, safeModeAuth, p);
2605025794
}
25795
+#endif
2605125796
sqlite3_db_config(
2605225797
p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
2605325798
);
2605425799
}
2605525800
}
@@ -26358,12 +26103,12 @@
2635826103
struct ImportCtx {
2635926104
const char *zFile; /* Name of the input file */
2636026105
FILE *in; /* Read the CSV text from this input stream */
2636126106
int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
2636226107
char *z; /* Accumulated text for a field */
26363
- int n; /* Number of bytes in z */
26364
- int nAlloc; /* Space allocated for z[] */
26108
+ i64 n; /* Number of bytes in z */
26109
+ i64 nAlloc; /* Space allocated for z[] */
2636526110
int nLine; /* Current line number */
2636626111
int nRow; /* Number of rows imported */
2636726112
int nErr; /* Number of errors encountered */
2636826113
int bNotFirst; /* True if one or more bytes already read */
2636926114
int cTerm; /* Character that terminated the most recent field */
@@ -26829,14 +26574,17 @@
2682926574
#if SQLITE_SHELL_HAVE_RECOVER
2683026575
/*
2683126576
** Convert a 2-byte or 4-byte big-endian integer into a native integer
2683226577
*/
2683326578
static unsigned int get2byteInt(unsigned char *a){
26834
- return (a[0]<<8) + a[1];
26579
+ return ((unsigned int)a[0]<<8) + (unsigned int)a[1];
2683526580
}
2683626581
static unsigned int get4byteInt(unsigned char *a){
26837
- return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
26582
+ return ((unsigned int)a[0]<<24)
26583
+ + ((unsigned int)a[1]<<16)
26584
+ + ((unsigned int)a[2]<<8)
26585
+ + (unsigned int)a[3];
2683826586
}
2683926587
2684026588
/*
2684126589
** Implementation of the ".dbinfo" command.
2684226590
**
@@ -26969,11 +26717,11 @@
2696926717
if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
2697026718
nPage = sqlite3_column_int64(pStmt, 0);
2697126719
sqlite3_finalize(pStmt);
2697226720
pStmt = 0;
2697326721
if( nPage<1 ) goto dbtotxt_error;
26974
- rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0);
26722
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
2697526723
if( rc ) goto dbtotxt_error;
2697626724
if( sqlite3_step(pStmt)!=SQLITE_ROW ){
2697726725
zTail = "unk.db";
2697826726
}else{
2697926727
const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
@@ -26980,10 +26728,15 @@
2698026728
if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
2698126729
zTail = strrchr(zFilename, '/');
2698226730
#if defined(_WIN32)
2698326731
if( zTail==0 ) zTail = strrchr(zFilename, '\\');
2698426732
#endif
26733
+ if( zTail==0 ){
26734
+ zTail = zFilename;
26735
+ }else if( zTail[1]!=0 ){
26736
+ zTail++;
26737
+ }
2698526738
}
2698626739
zName = strdup(zTail);
2698726740
shell_check_oom(zName);
2698826741
sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
2698926742
nPage*pgSz, pgSz, zName);
@@ -27148,10 +26901,47 @@
2714826901
if( zStr[0]!='-' ) return 0;
2714926902
zStr++;
2715026903
if( zStr[0]=='-' ) zStr++;
2715126904
return cli_strcmp(zStr, zOpt)==0;
2715226905
}
26906
+
26907
+/*
26908
+** The input zFN is guaranteed to start with "file:" and is thus a URI
26909
+** filename. Extract the actual filename and return a pointer to that
26910
+** filename in spaced obtained from sqlite3_malloc().
26911
+**
26912
+** The caller is responsible for freeing space using sqlite3_free() when
26913
+** it has finished with the filename.
26914
+*/
26915
+static char *shellFilenameFromUri(const char *zFN){
26916
+ char *zOut;
26917
+ int i, j, d1, d2;
26918
+
26919
+ assert( cli_strncmp(zFN,"file:",5)==0 );
26920
+ zOut = sqlite3_mprintf("%s", zFN+5);
26921
+ shell_check_oom(zOut);
26922
+ for(i=j=0; zOut[i]!=0 && zOut[i]!='?'; i++){
26923
+ if( zOut[i]!='%' ){
26924
+ zOut[j++] = zOut[i];
26925
+ continue;
26926
+ }
26927
+ d1 = hexDigitValue(zOut[i+1]);
26928
+ if( d1<0 ){
26929
+ zOut[j] = 0;
26930
+ break;
26931
+ }
26932
+ d2 = hexDigitValue(zOut[i+2]);
26933
+ if( d2<0 ){
26934
+ zOut[j] = 0;
26935
+ break;
26936
+ }
26937
+ zOut[j++] = d1*16 + d2;
26938
+ i += 2;
26939
+ }
26940
+ zOut[j] = 0;
26941
+ return zOut;
26942
+}
2715326943
2715426944
/*
2715526945
** Delete a file.
2715626946
*/
2715726947
int shellDeleteFile(const char *zFilename){
@@ -28704,11 +28494,11 @@
2870428494
int nArg = 0;
2870528495
int n, c;
2870628496
int rc = 0;
2870728497
char *azArg[52];
2870828498
28709
-#ifndef SQLITE_OMIT_VIRTUALTABLE
28499
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2871028500
if( p->expert.pExpert ){
2871128501
expertFinish(p, 1, 0);
2871228502
}
2871328503
#endif
2871428504
@@ -29005,11 +28795,11 @@
2900528795
}else{
2900628796
while( sqlite3_step(pStmt)==SQLITE_ROW ){
2900728797
const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
2900828798
const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
2900928799
if( zSchema==0 || zFile==0 ) continue;
29010
- azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
28800
+ azName = sqlite3_realloc64(azName, (nName+1)*2*sizeof(char*));
2901128801
shell_check_oom(azName);
2901228802
azName[nName*2] = strdup(zSchema);
2901328803
azName[nName*2+1] = strdup(zFile);
2901428804
nName++;
2901528805
}
@@ -29208,10 +28998,11 @@
2920828998
rc = 1;
2920928999
}
2921029000
}else
2921129001
2921229002
if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
29003
+ open_db(p, 0);
2921329004
rc = shell_dbtotxt_command(p, nArg, azArg);
2921429005
}else
2921529006
2921629007
if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
2921729008
if( nArg==2 ){
@@ -29273,11 +29064,11 @@
2927329064
if( p->mode==MODE_Explain ) p->mode = p->normalMode;
2927429065
p->autoExplain = 1;
2927529066
}
2927629067
}else
2927729068
29278
-#ifndef SQLITE_OMIT_VIRTUALTABLE
29069
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
2927929070
if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
2928029071
if( p->bSafeMode ){
2928129072
sqlite3_fprintf(stderr,
2928229073
"Cannot run experimental commands such as \"%s\" in safe mode\n",
2928329074
azArg[0]);
@@ -29840,16 +29631,10 @@
2984029631
sqlite3_stmt *pStmt;
2984129632
int tnum = 0;
2984229633
int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
2984329634
int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
2984429635
int i;
29845
- if( !ShellHasFlag(p,SHFLG_TestingMode) ){
29846
- sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
29847
- "imposter");
29848
- rc = 1;
29849
- goto meta_command_exit;
29850
- }
2985129636
if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
2985229637
eputz("Usage: .imposter INDEX IMPOSTER\n"
2985329638
" .imposter off\n");
2985429639
/* Also allowed, but not documented:
2985529640
**
@@ -29917,22 +29702,19 @@
2991729702
if( lenPK==0 ) lenPK = 100000;
2991829703
zSql = sqlite3_mprintf(
2991929704
"CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
2992029705
azArg[2], zCollist, lenPK, zCollist);
2992129706
sqlite3_free(zCollist);
29922
- rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
29707
+ rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 2, tnum);
2992329708
if( rc==SQLITE_OK ){
2992429709
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
2992529710
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
2992629711
if( rc ){
2992729712
sqlite3_fprintf(stderr,
2992829713
"Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
2992929714
}else{
2993029715
sqlite3_fprintf(stdout, "%s;\n", zSql);
29931
- sqlite3_fprintf(stdout,
29932
- "WARNING: writing to an imposter table will corrupt"
29933
- " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
2993429716
}
2993529717
}else{
2993629718
sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
2993729719
rc = 1;
2993829720
}
@@ -30282,10 +30064,11 @@
3028230064
const char *zFN = 0; /* Pointer to constant filename */
3028330065
char *zNewFilename = 0; /* Name of the database file to open */
3028430066
int iName = 1; /* Index in azArg[] of the filename */
3028530067
int newFlag = 0; /* True to delete file before opening */
3028630068
int openMode = SHELL_OPEN_UNSPEC;
30069
+ int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
3028730070
3028830071
/* Check for command-line arguments */
3028930072
for(iName=1; iName<nArg; iName++){
3029030073
const char *z = azArg[iName];
3029130074
#ifndef SQLITE_SHELL_FIDDLE
@@ -30296,13 +30079,18 @@
3029630079
openMode = SHELL_OPEN_ZIPFILE;
3029730080
#endif
3029830081
}else if( optionMatch(z, "append") ){
3029930082
openMode = SHELL_OPEN_APPENDVFS;
3030030083
}else if( optionMatch(z, "readonly") ){
30301
- openMode = SHELL_OPEN_READONLY;
30084
+ openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
30085
+ openFlags |= SQLITE_OPEN_READONLY;
30086
+ }else if( optionMatch(z, "exclusive") ){
30087
+ openFlags |= SQLITE_OPEN_EXCLUSIVE;
30088
+ }else if( optionMatch(z, "ifexists") ){
30089
+ openFlags &= ~(SQLITE_OPEN_CREATE);
3030230090
}else if( optionMatch(z, "nofollow") ){
30303
- p->openFlags |= SQLITE_OPEN_NOFOLLOW;
30091
+ openFlags |= SQLITE_OPEN_NOFOLLOW;
3030430092
#ifndef SQLITE_OMIT_DESERIALIZE
3030530093
}else if( optionMatch(z, "deserialize") ){
3030630094
openMode = SHELL_OPEN_DESERIALIZE;
3030730095
}else if( optionMatch(z, "hexdb") ){
3030830096
openMode = SHELL_OPEN_HEXDB;
@@ -30330,16 +30118,25 @@
3033030118
p->db = 0;
3033130119
p->pAuxDb->zDbFilename = 0;
3033230120
sqlite3_free(p->pAuxDb->zFreeOnClose);
3033330121
p->pAuxDb->zFreeOnClose = 0;
3033430122
p->openMode = openMode;
30335
- p->openFlags = 0;
30123
+ p->openFlags = openFlags;
3033630124
p->szMax = 0;
3033730125
3033830126
/* If a filename is specified, try to open it first */
3033930127
if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
30340
- if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
30128
+ if( newFlag && zFN && !p->bSafeMode ){
30129
+ if( cli_strncmp(zFN,"file:",5)==0 ){
30130
+ char *zDel = shellFilenameFromUri(zFN);
30131
+ shell_check_oom(zDel);
30132
+ shellDeleteFile(zDel);
30133
+ sqlite3_free(zDel);
30134
+ }else{
30135
+ shellDeleteFile(zFN);
30136
+ }
30137
+ }
3034130138
#ifndef SQLITE_SHELL_FIDDLE
3034230139
if( p->bSafeMode
3034330140
&& p->openMode!=SHELL_OPEN_HEXDB
3034430141
&& zFN
3034530142
&& cli_strcmp(zFN,":memory:")!=0
@@ -30938,13 +30735,13 @@
3093830735
appendText(&sSelect, "name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ", 0);
3093930736
}
3094030737
appendText(&sSelect, "sql IS NOT NULL"
3094130738
" ORDER BY snum, rowid", 0);
3094230739
if( bDebug ){
30943
- sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
30740
+ sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.zTxt);
3094430741
}else{
30945
- rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
30742
+ rc = sqlite3_exec(p->db, sSelect.zTxt, callback, &data, &zErrMsg);
3094630743
}
3094730744
freeText(&sSelect);
3094830745
}
3094930746
if( zErrMsg ){
3095030747
shellEmitError(zErrMsg);
@@ -31072,19 +30869,20 @@
3107230869
3107330870
/* .session filter GLOB ....
3107430871
** Set a list of GLOB patterns of table names to be excluded.
3107530872
*/
3107630873
if( cli_strcmp(azCmd[0], "filter")==0 ){
31077
- int ii, nByte;
30874
+ int ii;
30875
+ i64 nByte;
3107830876
if( nCmd<2 ) goto session_syntax_error;
3107930877
if( pAuxDb->nSession ){
3108030878
for(ii=0; ii<pSession->nFilter; ii++){
3108130879
sqlite3_free(pSession->azFilter[ii]);
3108230880
}
3108330881
sqlite3_free(pSession->azFilter);
3108430882
nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
31085
- pSession->azFilter = sqlite3_malloc( nByte );
30883
+ pSession->azFilter = sqlite3_malloc64( nByte );
3108630884
shell_check_oom( pSession->azFilter );
3108730885
for(ii=1; ii<nCmd; ii++){
3108830886
char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
3108930887
shell_check_oom(x);
3109030888
}
@@ -31264,26 +31062,26 @@
3126431062
sqlite3_fprintf(p->out, "%s\n", zSql);
3126531063
}else
3126631064
if( cli_strcmp(zOp,"run")==0 ){
3126731065
char *zErrMsg = 0;
3126831066
str.n = 0;
31269
- str.z[0] = 0;
31067
+ str.zTxt[0] = 0;
3127031068
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
3127131069
nTest++;
3127231070
if( bVerbose ){
31273
- sqlite3_fprintf(p->out, "Result: %s\n", str.z);
31071
+ sqlite3_fprintf(p->out, "Result: %s\n", str.zTxt);
3127431072
}
3127531073
if( rc || zErrMsg ){
3127631074
nErr++;
3127731075
rc = 1;
3127831076
sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
3127931077
sqlite3_free(zErrMsg);
31280
- }else if( cli_strcmp(zAns,str.z)!=0 ){
31078
+ }else if( cli_strcmp(zAns,str.zTxt)!=0 ){
3128131079
nErr++;
3128231080
rc = 1;
3128331081
sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
31284
- sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
31082
+ sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.zTxt);
3128531083
}
3128631084
}
3128731085
else{
3128831086
sqlite3_fprintf(stderr,
3128931087
"Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
@@ -31395,11 +31193,11 @@
3139531193
appendText(&sQuery, "SELECT * FROM ", 0);
3139631194
appendText(&sQuery, zTab, 0);
3139731195
appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
3139831196
}
3139931197
appendText(&sSql, zSep, 0);
31400
- appendText(&sSql, sQuery.z, '\'');
31198
+ appendText(&sSql, sQuery.zTxt, '\'');
3140131199
sQuery.n = 0;
3140231200
appendText(&sSql, ",", 0);
3140331201
appendText(&sSql, zTab, '\'');
3140431202
zSep = "),(";
3140531203
}
@@ -31407,17 +31205,17 @@
3140731205
if( bSeparate ){
3140831206
zSql = sqlite3_mprintf(
3140931207
"%s))"
3141031208
" SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
3141131209
" FROM [sha3sum$query]",
31412
- sSql.z, iSize);
31210
+ sSql.zTxt, iSize);
3141331211
}else{
3141431212
zSql = sqlite3_mprintf(
3141531213
"%s))"
3141631214
" SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
3141731215
" FROM [sha3sum$query]",
31418
- sSql.z, iSize);
31216
+ sSql.zTxt, iSize);
3141931217
}
3142031218
shell_check_oom(zSql);
3142131219
freeText(&sQuery);
3142231220
freeText(&sSql);
3142331221
if( bDebug ){
@@ -31613,11 +31411,11 @@
3161331411
goto meta_command_exit;
3161431412
}
3161531413
for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
3161631414
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
3161731415
if( zDbName==0 ) continue;
31618
- if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
31416
+ if( s.zTxt && s.zTxt[0] ) appendText(&s, " UNION ALL ", 0);
3161931417
if( sqlite3_stricmp(zDbName, "main")==0 ){
3162031418
appendText(&s, "SELECT name FROM ", 0);
3162131419
}else{
3162231420
appendText(&s, "SELECT ", 0);
3162331421
appendText(&s, zDbName, '\'');
@@ -31635,11 +31433,11 @@
3163531433
}
3163631434
}
3163731435
rc = sqlite3_finalize(pStmt);
3163831436
if( rc==SQLITE_OK ){
3163931437
appendText(&s, " ORDER BY 1", 0);
31640
- rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
31438
+ rc = sqlite3_prepare_v2(p->db, s.zTxt, -1, &pStmt, 0);
3164131439
}
3164231440
freeText(&s);
3164331441
if( rc ) return shellDatabaseError(p->db);
3164431442
3164531443
/* Run the SQL statement prepared by the above block. Store the results
@@ -31652,14 +31450,14 @@
3165231450
sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
3165331451
}
3165431452
while( sqlite3_step(pStmt)==SQLITE_ROW ){
3165531453
if( nRow>=nAlloc ){
3165631454
char **azNew;
31657
- int n2 = nAlloc*2 + 10;
31455
+ sqlite3_int64 n2 = 2*(sqlite3_int64)nAlloc + 10;
3165831456
azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
3165931457
shell_check_oom(azNew);
31660
- nAlloc = n2;
31458
+ nAlloc = (int)n2;
3166131459
azResult = azNew;
3166231460
}
3166331461
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
3166431462
shell_check_oom(azResult[nRow]);
3166531463
nRow++;
@@ -31818,11 +31616,11 @@
3181831616
{ 0x00000020, 1, "CoverIdxScan" },
3181931617
{ 0x00000040, 1, "OrderByIdxJoin" },
3182031618
{ 0x00000080, 1, "Transitive" },
3182131619
{ 0x00000100, 1, "OmitNoopJoin" },
3182231620
{ 0x00000200, 1, "CountOfView" },
31823
- { 0x00000400, 1, "CurosrHints" },
31621
+ { 0x00000400, 1, "CursorHints" },
3182431622
{ 0x00000800, 1, "Stat4" },
3182531623
{ 0x00001000, 1, "PushDown" },
3182631624
{ 0x00002000, 1, "SimplifyJoin" },
3182731625
{ 0x00004000, 1, "SkipScan" },
3182831626
{ 0x00008000, 1, "PropagateConst" },
@@ -32365,11 +32163,14 @@
3236532163
p->nWidth = nArg-1;
3236632164
p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
3236732165
if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
3236832166
if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
3236932167
for(j=1; j<nArg; j++){
32370
- p->colWidth[j-1] = (int)integerValue(azArg[j]);
32168
+ i64 w = integerValue(azArg[j]);
32169
+ if( w < -30000 ) w = -30000;
32170
+ if( w > +30000 ) w = +30000;
32171
+ p->colWidth[j-1] = (int)w;
3237132172
}
3237232173
}else
3237332174
3237432175
{
3237532176
sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
@@ -32888,76 +32689,95 @@
3288832689
3288932690
return home_dir;
3289032691
}
3289132692
3289232693
/*
32893
-** On non-Windows platforms, look for $XDG_CONFIG_HOME.
32894
-** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
32895
-** the path to it. If there is no $(XDG_CONFIG_HOME) then
32896
-** look for $(HOME)/.config/sqlite3/sqliterc and if found
32897
-** return that. If none of these are found, return 0.
32694
+** On non-Windows platforms, look for:
32695
+**
32696
+** - ${zEnvVar}/${zBaseName}
32697
+** - ${HOME}/${zSubdir}/${zBaseName}
32698
+**
32699
+** $zEnvVar is intended to be the name of an XDG_... environment
32700
+** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME. If zEnvVar is
32701
+** NULL or getenv(zEnvVar) is NULL then fall back to the second
32702
+** option. If the selected option is not found in the filesystem,
32703
+** return 0.
32704
+**
32705
+** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName}
32706
+** becomes the fallback.
32707
+**
32708
+** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir
32709
+** will conventionally be ".config" or ".local/state", which, not
32710
+** coincidentally, is the typical subdir of the corresponding XDG_...
32711
+** var with the XDG var's $HOME prefix.
3289832712
**
32899
-** The string returned is obtained from sqlite3_malloc() and
32900
-** should be freed by the caller.
32713
+** The returned string is obtained from sqlite3_malloc() and should be
32714
+** sqlite3_free()'d by the caller.
3290132715
*/
32902
-static char *find_xdg_config(void){
32716
+static char *find_xdg_file(const char *zEnvVar, const char *zSubdir,
32717
+ const char *zBaseName){
3290332718
#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
3290432719
|| defined(__RTP__) || defined(_WRS_KERNEL)
3290532720
return 0;
3290632721
#else
32907
- char *zConfig = 0;
32908
- const char *zXdgHome;
32909
-
32910
- zXdgHome = getenv("XDG_CONFIG_HOME");
32911
- if( zXdgHome==0 ){
32912
- const char *zHome = getenv("HOME");
32913
- if( zHome==0 ) return 0;
32914
- zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome);
32722
+ char *zConfigFile = 0;
32723
+ const char *zXdgDir;
32724
+
32725
+ zXdgDir = zEnvVar ? getenv(zEnvVar) : 0;
32726
+ if( zXdgDir ){
32727
+ zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName);
3291532728
}else{
32916
- zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
32917
- }
32918
- shell_check_oom(zConfig);
32919
- if( access(zConfig,0)!=0 ){
32920
- sqlite3_free(zConfig);
32921
- zConfig = 0;
32922
- }
32923
- return zConfig;
32729
+ const char * zHome = find_home_dir(0);
32730
+ if( zHome==0 ) return 0;
32731
+ zConfigFile = (zSubdir && *zSubdir)
32732
+ ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName)
32733
+ : sqlite3_mprintf("%s/%s", zHome, zBaseName);
32734
+ }
32735
+ shell_check_oom(zConfigFile);
32736
+ if( access(zConfigFile,0)!=0 ){
32737
+ sqlite3_free(zConfigFile);
32738
+ zConfigFile = 0;
32739
+ }
32740
+ return zConfigFile;
3292432741
#endif
3292532742
}
3292632743
3292732744
/*
32928
-** Read input from the file given by sqliterc_override. Or if that
32929
-** parameter is NULL, take input from the first of find_xdg_config()
32930
-** or ~/.sqliterc which is found.
32745
+** Read input from the file sqliterc_override. If that parameter is
32746
+** NULL, take it from find_xdg_file(), if found, or fall back to
32747
+** ~/.sqliterc.
3293132748
**
32932
-** Returns the number of errors.
32749
+** Failure to read the config is only considered a failure if
32750
+** sqliterc_override is not NULL, in which case this function may emit
32751
+** a warning or, if ::bail_on_error is true, fail fatally if the file
32752
+** named by sqliterc_override is not found.
3293332753
*/
3293432754
static void process_sqliterc(
3293532755
ShellState *p, /* Configuration data */
3293632756
const char *sqliterc_override /* Name of config file. NULL to use default */
3293732757
){
3293832758
char *home_dir = NULL;
32939
- const char *sqliterc = sqliterc_override;
32940
- char *zBuf = 0;
32759
+ char *sqliterc = (char*)sqliterc_override;
3294132760
FILE *inSaved = p->in;
3294232761
int savedLineno = p->lineno;
3294332762
3294432763
if( sqliterc == NULL ){
32945
- sqliterc = zBuf = find_xdg_config();
32764
+ sqliterc = find_xdg_file("XDG_CONFIG_HOME",
32765
+ ".config",
32766
+ "sqlite3/sqliterc");
3294632767
}
3294732768
if( sqliterc == NULL ){
3294832769
home_dir = find_home_dir(0);
3294932770
if( home_dir==0 ){
3295032771
eputz("-- warning: cannot find home directory;"
3295132772
" cannot read ~/.sqliterc\n");
3295232773
return;
3295332774
}
32954
- zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
32955
- shell_check_oom(zBuf);
32956
- sqliterc = zBuf;
32775
+ sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir);
32776
+ shell_check_oom(sqliterc);
3295732777
}
32958
- p->in = sqlite3_fopen(sqliterc,"rb");
32778
+ p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0;
3295932779
if( p->in ){
3296032780
if( stdin_is_interactive ){
3296132781
sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
3296232782
}
3296332783
if( process_input(p) && bail_on_error ) exit(1);
@@ -32966,11 +32786,13 @@
3296632786
sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
3296732787
if( bail_on_error ) exit(1);
3296832788
}
3296932789
p->in = inSaved;
3297032790
p->lineno = savedLineno;
32971
- sqlite3_free(zBuf);
32791
+ if( sqliterc != sqliterc_override ){
32792
+ sqlite3_free(sqliterc);
32793
+ }
3297232794
}
3297332795
3297432796
/*
3297532797
** Show available command line options
3297632798
*/
@@ -32997,10 +32819,11 @@
3299732819
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3299832820
" -heap SIZE Size of heap for memsys3 or memsys5\n"
3299932821
#endif
3300032822
" -help show this message\n"
3300132823
" -html set output mode to HTML\n"
32824
+ " -ifexists only open if database already exists\n"
3300232825
" -interactive force interactive I/O\n"
3300332826
" -json set output mode to 'json'\n"
3300432827
" -line set output mode to 'line'\n"
3300532828
" -list set output mode to 'list'\n"
3300632829
" -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
@@ -33341,16 +33164,24 @@
3334133164
(void)cmdline_option_value(argc, argv, ++i);
3334233165
#endif
3334333166
}else if( cli_strcmp(z,"-pagecache")==0 ){
3334433167
sqlite3_int64 n, sz;
3334533168
sz = integerValue(cmdline_option_value(argc,argv,++i));
33346
- if( sz>70000 ) sz = 70000;
33169
+ if( sz>65536 ) sz = 65536;
3334733170
if( sz<0 ) sz = 0;
3334833171
n = integerValue(cmdline_option_value(argc,argv,++i));
3334933172
if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
3335033173
n = 0xffffffffffffLL/sz;
3335133174
}
33175
+ if( sz>0 && (sz & (sz-1))==0 ){
33176
+ /* If SIZE is a power of two, round it up by the PCACHE_HDRSZ */
33177
+ int szHdr = 0;
33178
+ sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &szHdr);
33179
+ sz += szHdr;
33180
+ sqlite3_fprintf(stdout, "Page cache size increased to %d to accommodate"
33181
+ " the %d-byte headers\n", (int)sz, szHdr);
33182
+ }
3335233183
verify_uninitialized();
3335333184
sqlite3_config(SQLITE_CONFIG_PAGECACHE,
3335433185
(n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
3335533186
data.shellFlgs |= SHFLG_Pagecache;
3335633187
}else if( cli_strcmp(z,"-lookaside")==0 ){
@@ -33359,11 +33190,11 @@
3335933190
if( sz<0 ) sz = 0;
3336033191
n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3336133192
if( n<0 ) n = 0;
3336233193
verify_uninitialized();
3336333194
sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
33364
- if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
33195
+ if( (i64)sz*(i64)n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
3336533196
}else if( cli_strcmp(z,"-threadsafe")==0 ){
3336633197
int n;
3336733198
n = (int)integerValue(cmdline_option_value(argc,argv,++i));
3336833199
verify_uninitialized();
3336933200
switch( n ){
@@ -33401,13 +33232,19 @@
3340133232
data.openMode = SHELL_OPEN_DESERIALIZE;
3340233233
}else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
3340333234
data.szMax = integerValue(argv[++i]);
3340433235
#endif
3340533236
}else if( cli_strcmp(z,"-readonly")==0 ){
33406
- data.openMode = SHELL_OPEN_READONLY;
33237
+ data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
33238
+ data.openFlags |= SQLITE_OPEN_READONLY;
3340733239
}else if( cli_strcmp(z,"-nofollow")==0 ){
33408
- data.openFlags = SQLITE_OPEN_NOFOLLOW;
33240
+ data.openFlags |= SQLITE_OPEN_NOFOLLOW;
33241
+ }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */
33242
+ data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
33243
+ }else if( cli_strcmp(z,"-ifexists")==0 ){
33244
+ data.openFlags &= ~(SQLITE_OPEN_CREATE);
33245
+ if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
3340933246
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
3341033247
}else if( cli_strncmp(z, "-A",2)==0 ){
3341133248
/* All remaining command-line arguments are passed to the ".archive"
3341233249
** command, so ignore them */
3341333250
break;
@@ -33557,13 +33394,19 @@
3355733394
data.openMode = SHELL_OPEN_DESERIALIZE;
3355833395
}else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
3355933396
data.szMax = integerValue(argv[++i]);
3356033397
#endif
3356133398
}else if( cli_strcmp(z,"-readonly")==0 ){
33562
- data.openMode = SHELL_OPEN_READONLY;
33399
+ data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
33400
+ data.openFlags |= SQLITE_OPEN_READONLY;
3356333401
}else if( cli_strcmp(z,"-nofollow")==0 ){
3356433402
data.openFlags |= SQLITE_OPEN_NOFOLLOW;
33403
+ }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */
33404
+ data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
33405
+ }else if( cli_strcmp(z,"-ifexists")==0 ){
33406
+ data.openFlags &= ~(SQLITE_OPEN_CREATE);
33407
+ if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
3356533408
}else if( cli_strcmp(z,"-ascii")==0 ){
3356633409
data.mode = MODE_Ascii;
3356733410
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
3356833411
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
3356933412
}else if( cli_strcmp(z,"-tabs")==0 ){
@@ -33734,11 +33577,10 @@
3373433577
/* Run commands received from standard input
3373533578
*/
3373633579
if( stdin_is_interactive ){
3373733580
char *zHome;
3373833581
char *zHistory;
33739
- int nHistory;
3374033582
sqlite3_fprintf(stdout,
3374133583
"SQLite version %s %.19s\n" /*extra-version-info*/
3374233584
"Enter \".help\" for usage hints.\n",
3374333585
sqlite3_libversion(), sqlite3_sourceid());
3374433586
if( warnInmemoryDb ){
@@ -33747,15 +33589,19 @@
3374733589
sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
3374833590
" persistent database.\n");
3374933591
}
3375033592
zHistory = getenv("SQLITE_HISTORY");
3375133593
if( zHistory ){
33752
- zHistory = strdup(zHistory);
33753
- }else if( (zHome = find_home_dir(0))!=0 ){
33754
- nHistory = strlen30(zHome) + 20;
33755
- if( (zHistory = malloc(nHistory))!=0 ){
33756
- sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
33594
+ zHistory = sqlite3_mprintf("%s", zHistory);
33595
+ shell_check_oom(zHistory);
33596
+ }else{
33597
+ zHistory = find_xdg_file("XDG_STATE_HOME",
33598
+ ".local/state",
33599
+ "sqlite_history");
33600
+ if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){
33601
+ zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome);
33602
+ shell_check_oom(zHistory);
3375733603
}
3375833604
}
3375933605
if( zHistory ){ shell_read_history(zHistory); }
3376033606
#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
3376133607
rl_attempted_completion_function = readline_completion;
@@ -33767,21 +33613,21 @@
3376733613
data.in = 0;
3376833614
rc = process_input(&data);
3376933615
if( zHistory ){
3377033616
shell_stifle_history(2000);
3377133617
shell_write_history(zHistory);
33772
- free(zHistory);
33618
+ sqlite3_free(zHistory);
3377333619
}
3377433620
}else{
3377533621
data.in = stdin;
3377633622
rc = process_input(&data);
3377733623
}
3377833624
}
3377933625
#ifndef SQLITE_SHELL_FIDDLE
3378033626
/* In WASM mode we have to leave the db state in place so that
3378133627
** client code can "push" SQL into it after this call returns. */
33782
-#ifndef SQLITE_OMIT_VIRTUALTABLE
33628
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
3378333629
if( data.expert.pExpert ){
3378433630
expertFinish(&data, 1, 0);
3378533631
}
3378633632
#endif
3378733633
shell_main_exit:
3378833634
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -122,10 +122,13 @@
122 typedef sqlite3_int64 i64;
123 typedef sqlite3_uint64 u64;
124 typedef unsigned char u8;
125 #include <ctype.h>
126 #include <stdarg.h>
 
 
 
127
128 #if !defined(_WIN32) && !defined(WIN32)
129 # include <signal.h>
130 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
131 # include <pwd.h>
@@ -646,12 +649,23 @@
646 if( a==0 ) a = "";
647 if( b==0 ) b = "";
648 return strncmp(a,b,n);
649 }
650
651 /* Return the current wall-clock time */
 
 
652 static sqlite3_int64 timeOfDay(void){
 
 
 
 
 
 
 
 
 
653 static sqlite3_vfs *clockVfs = 0;
654 sqlite3_int64 t;
655 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
656 if( clockVfs==0 ) return 0; /* Never actually happens */
657 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
@@ -659,11 +673,16 @@
659 }else{
660 double r;
661 clockVfs->xCurrentTime(clockVfs, &r);
662 t = (sqlite3_int64)(r*86400000.0);
663 }
664 return t;
 
 
 
 
 
665 }
666
667 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
668 #include <sys/time.h>
669 #include <sys/resource.h>
@@ -704,12 +723,12 @@
704 static void endTimer(FILE *out){
705 if( enableTimer ){
706 sqlite3_int64 iEnd = timeOfDay();
707 struct rusage sEnd;
708 getrusage(RUSAGE_SELF, &sEnd);
709 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
710 (iEnd - iBegin)*0.001,
711 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
712 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
713 }
714 }
715
@@ -783,14 +802,23 @@
783 static void endTimer(FILE *out){
784 if( enableTimer && getProcessTimesAddr){
785 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
786 sqlite3_int64 ftWallEnd = timeOfDay();
787 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
788 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
789 (ftWallEnd - ftWallBegin)*0.001,
 
 
 
 
 
 
 
 
790 timeDiff(&ftUserBegin, &ftUserEnd),
791 timeDiff(&ftKernelBegin, &ftKernelEnd));
 
792 }
793 }
794
795 #define BEGIN_TIMER beginTimer()
796 #define END_TIMER(X) endTimer(X)
@@ -1126,11 +1154,11 @@
1126 }
1127 if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
1128 && (z[3] & 0xc0)==0x80
1129 ){
1130 *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
1131 | (z[4] & 0x3f);
1132 return 4;
1133 }
1134 *pU = 0;
1135 return 1;
1136 }
@@ -1189,18 +1217,28 @@
1189 ** since with %*.*s the width is measured in bytes, not characters.
1190 **
1191 ** Take into account zero-width and double-width Unicode characters.
1192 ** In other words, a zero-width character does not count toward the
1193 ** the w limit. A double-width character counts as two.
 
 
 
1194 */
1195 static void utf8_width_print(FILE *out, int w, const char *zUtf){
1196 const unsigned char *a = (const unsigned char*)zUtf;
 
1197 unsigned char c;
1198 int i = 0;
1199 int n = 0;
1200 int k;
1201 int aw = w<0 ? -w : w;
 
 
 
 
 
 
1202 if( zUtf==0 ) zUtf = "";
1203 while( (c = a[i])!=0 ){
1204 if( (c&0xc0)==0xc0 ){
1205 int u;
1206 int len = decodeUtf8(a+i, &u);
@@ -1323,11 +1361,11 @@
1323
1324 /*
1325 ** This routine reads a line of text from FILE in, stores
1326 ** the text in memory obtained from malloc() and returns a pointer
1327 ** to the text. NULL is returned at end of file, or if malloc()
1328 ** fails.
1329 **
1330 ** If zLine is not NULL then it is a malloced buffer returned from
1331 ** a previous call to this routine that may be reused.
1332 */
1333 static char *local_getline(char *zLine, FILE *in){
@@ -1334,10 +1372,14 @@
1334 int nLine = zLine==0 ? 0 : 100;
1335 int n = 0;
1336
1337 while( 1 ){
1338 if( n+100>nLine ){
 
 
 
 
1339 nLine = nLine*2 + 100;
1340 zLine = realloc(zLine, nLine);
1341 shell_check_oom(zLine);
1342 }
1343 if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
@@ -1417,14 +1459,18 @@
1417 return -1;
1418 }
1419
1420 /*
1421 ** Interpret zArg as an integer value, possibly with suffixes.
 
 
 
 
1422 */
1423 static sqlite3_int64 integerValue(const char *zArg){
1424 sqlite3_int64 v = 0;
1425 static const struct { char *zSuffix; int iMult; } aMult[] = {
1426 { "KiB", 1024 },
1427 { "MiB", 1024*1024 },
1428 { "GiB", 1024*1024*1024 },
1429 { "KB", 1000 },
1430 { "MB", 1000000 },
@@ -1443,46 +1489,54 @@
1443 }
1444 if( zArg[0]=='0' && zArg[1]=='x' ){
1445 int x;
1446 zArg += 2;
1447 while( (x = hexDigitValue(zArg[0]))>=0 ){
 
1448 v = (v<<4) + x;
1449 zArg++;
1450 }
1451 }else{
1452 while( IsDigit(zArg[0]) ){
1453 v = v*10 + zArg[0] - '0';
 
 
 
1454 zArg++;
1455 }
1456 }
1457 for(i=0; i<ArraySize(aMult); i++){
1458 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
 
1459 v *= aMult[i].iMult;
1460 break;
1461 }
1462 }
1463 return isNeg? -v : v;
 
 
 
1464 }
1465
1466 /*
1467 ** A variable length string to which one can append text.
1468 */
1469 typedef struct ShellText ShellText;
1470 struct ShellText {
1471 char *z;
1472 int n;
1473 int nAlloc;
1474 };
1475
1476 /*
1477 ** Initialize and destroy a ShellText object
1478 */
1479 static void initText(ShellText *p){
1480 memset(p, 0, sizeof(*p));
1481 }
1482 static void freeText(ShellText *p){
1483 free(p->z);
1484 initText(p);
1485 }
1486
1487 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1488 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
@@ -1503,30 +1557,30 @@
1503 for(i=0; i<nAppend; i++){
1504 if( zAppend[i]==quote ) len++;
1505 }
1506 }
1507
1508 if( p->z==0 || p->n+len>=p->nAlloc ){
1509 p->nAlloc = p->nAlloc*2 + len + 20;
1510 p->z = realloc(p->z, p->nAlloc);
1511 shell_check_oom(p->z);
1512 }
1513
1514 if( quote ){
1515 char *zCsr = p->z+p->n;
1516 *zCsr++ = quote;
1517 for(i=0; i<nAppend; i++){
1518 *zCsr++ = zAppend[i];
1519 if( zAppend[i]==quote ) *zCsr++ = quote;
1520 }
1521 *zCsr++ = quote;
1522 p->n = (int)(zCsr - p->z);
1523 *zCsr = '\0';
1524 }else{
1525 memcpy(p->z+p->n, zAppend, nAppend);
1526 p->n += nAppend;
1527 p->z[p->n] = '\0';
1528 }
1529 }
1530
1531 /*
1532 ** Attempt to determine if identifier zName needs to be quoted, either
@@ -1547,10 +1601,13 @@
1547 }
1548
1549 /*
1550 ** Construct a fake object name and column list to describe the structure
1551 ** of the view, virtual table, or table valued function zSchema.zName.
 
 
 
1552 */
1553 static char *shellFakeSchema(
1554 sqlite3 *db, /* The database connection containing the vtab */
1555 const char *zSchema, /* Schema of the database holding the vtab */
1556 const char *zName /* The name of the virtual table */
@@ -1587,13 +1644,13 @@
1587 }
1588 appendText(&s, ")", 0);
1589 sqlite3_finalize(pStmt);
1590 if( nRow==0 ){
1591 freeText(&s);
1592 s.z = 0;
1593 }
1594 return s.z;
1595 }
1596
1597 /*
1598 ** SQL function: strtod(X)
1599 **
@@ -1692,11 +1749,11 @@
1692 if( z==0 ){
1693 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1694 }else{
1695 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1696 }
1697 free(zFake);
1698 }
1699 if( z ){
1700 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1701 return;
1702 }
@@ -3667,11 +3724,12 @@
3667 iExp -= p->nFrac;
3668 p->nFrac = 0;
3669 }
3670 }
3671 if( iExp>0 ){
3672 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
 
3673 if( p->a==0 ) goto new_from_text_failed;
3674 memset(p->a+p->nDigit, 0, iExp);
3675 p->nDigit += iExp;
3676 }
3677 }else if( iExp<0 ){
@@ -3686,18 +3744,23 @@
3686 iExp -= nExtra;
3687 p->nFrac = p->nDigit - 1;
3688 }
3689 }
3690 if( iExp>0 ){
3691 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
 
3692 if( p->a==0 ) goto new_from_text_failed;
3693 memmove(p->a+iExp, p->a, p->nDigit);
3694 memset(p->a, 0, iExp);
3695 p->nDigit += iExp;
3696 p->nFrac += iExp;
3697 }
3698 }
 
 
 
 
3699 return p;
3700
3701 new_from_text_failed:
3702 if( p ){
3703 if( p->a ) sqlite3_free(p->a);
@@ -3786,11 +3849,11 @@
3786 }
3787 if( p->isNull ){
3788 sqlite3_result_null(pCtx);
3789 return;
3790 }
3791 z = sqlite3_malloc( p->nDigit+4 );
3792 if( z==0 ){
3793 sqlite3_result_error_nomem(pCtx);
3794 return;
3795 }
3796 i = 0;
@@ -3851,11 +3914,11 @@
3851 }
3852 for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3853 for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3854 nFrac = p->nFrac + (nDigit - p->nDigit);
3855 nDigit -= nZero;
3856 z = sqlite3_malloc( nDigit+20 );
3857 if( z==0 ){
3858 sqlite3_result_error_nomem(pCtx);
3859 return;
3860 }
3861 if( nDigit==0 ){
@@ -3896,17 +3959,25 @@
3896 ** pA!=0
3897 ** pA->isNull==0
3898 ** pB!=0
3899 ** pB->isNull==0
3900 */
3901 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
3902 int nASig, nBSig, rc, n;
 
 
 
 
 
 
 
 
3903 if( pA->sign!=pB->sign ){
3904 return pA->sign ? -1 : +1;
3905 }
3906 if( pA->sign ){
3907 const Decimal *pTemp = pA;
3908 pA = pB;
3909 pB = pTemp;
3910 }
3911 nASig = pA->nDigit - pA->nFrac;
3912 nBSig = pB->nDigit - pB->nFrac;
@@ -4064,11 +4135,12 @@
4064 if( pA==0 || pA->oom || pA->isNull
4065 || pB==0 || pB->oom || pB->isNull
4066 ){
4067 goto mul_end;
4068 }
4069 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
 
4070 if( acc==0 ){
4071 pA->oom = 1;
4072 goto mul_end;
4073 }
4074 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
@@ -4151,11 +4223,11 @@
4151 r = -r;
4152 }else{
4153 isNeg = 0;
4154 }
4155 memcpy(&a,&r,sizeof(a));
4156 if( a==0 ){
4157 e = 0;
4158 m = 0;
4159 }else{
4160 e = a>>52;
4161 m = a & ((((sqlite3_int64)1)<<52)-1);
@@ -4426,516 +4498,10 @@
4426 }
4427 return rc;
4428 }
4429
4430 /************************* End ../ext/misc/decimal.c ********************/
4431 /************************* Begin ../ext/misc/percentile.c ******************/
4432 /*
4433 ** 2013-05-28
4434 **
4435 ** The author disclaims copyright to this source code. In place of
4436 ** a legal notice, here is a blessing:
4437 **
4438 ** May you do good and not evil.
4439 ** May you find forgiveness for yourself and forgive others.
4440 ** May you share freely, never taking more than you give.
4441 **
4442 ******************************************************************************
4443 **
4444 ** This file contains code to implement the percentile(Y,P) SQL function
4445 ** and similar as described below:
4446 **
4447 ** (1) The percentile(Y,P) function is an aggregate function taking
4448 ** exactly two arguments.
4449 **
4450 ** (2) If the P argument to percentile(Y,P) is not the same for every
4451 ** row in the aggregate then an error is thrown. The word "same"
4452 ** in the previous sentence means that the value differ by less
4453 ** than 0.001.
4454 **
4455 ** (3) If the P argument to percentile(Y,P) evaluates to anything other
4456 ** than a number in the range of 0.0 to 100.0 inclusive then an
4457 ** error is thrown.
4458 **
4459 ** (4) If any Y argument to percentile(Y,P) evaluates to a value that
4460 ** is not NULL and is not numeric then an error is thrown.
4461 **
4462 ** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus
4463 ** infinity then an error is thrown. (SQLite always interprets NaN
4464 ** values as NULL.)
4465 **
4466 ** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions,
4467 ** including CASE WHEN expressions.
4468 **
4469 ** (7) The percentile(Y,P) aggregate is able to handle inputs of at least
4470 ** one million (1,000,000) rows.
4471 **
4472 ** (8) If there are no non-NULL values for Y, then percentile(Y,P)
4473 ** returns NULL.
4474 **
4475 ** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P)
4476 ** returns the one Y value.
4477 **
4478 ** (10) If there N non-NULL values of Y where N is two or more and
4479 ** the Y values are ordered from least to greatest and a graph is
4480 ** drawn from 0 to N-1 such that the height of the graph at J is
4481 ** the J-th Y value and such that straight lines are drawn between
4482 ** adjacent Y values, then the percentile(Y,P) function returns
4483 ** the height of the graph at P*(N-1)/100.
4484 **
4485 ** (11) The percentile(Y,P) function always returns either a floating
4486 ** point number or NULL.
4487 **
4488 ** (12) The percentile(Y,P) is implemented as a single C99 source-code
4489 ** file that compiles into a shared-library or DLL that can be loaded
4490 ** into SQLite using the sqlite3_load_extension() interface.
4491 **
4492 ** (13) A separate median(Y) function is the equivalent percentile(Y,50).
4493 **
4494 ** (14) A separate percentile_cont(Y,P) function is equivalent to
4495 ** percentile(Y,P/100.0). In other words, the fraction value in
4496 ** the second argument is in the range of 0 to 1 instead of 0 to 100.
4497 **
4498 ** (15) A separate percentile_disc(Y,P) function is like
4499 ** percentile_cont(Y,P) except that instead of returning the weighted
4500 ** average of the nearest two input values, it returns the next lower
4501 ** value. So the percentile_disc(Y,P) will always return a value
4502 ** that was one of the inputs.
4503 **
4504 ** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and
4505 ** percentile_disc(Y,P) can be used as window functions.
4506 **
4507 ** Differences from standard SQL:
4508 **
4509 ** * The percentile_cont(X,P) function is equivalent to the following in
4510 ** standard SQL:
4511 **
4512 ** (percentile_cont(P) WITHIN GROUP (ORDER BY X))
4513 **
4514 ** The SQLite syntax is much more compact. The standard SQL syntax
4515 ** is also supported if SQLite is compiled with the
4516 ** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option.
4517 **
4518 ** * No median(X) function exists in the SQL standard. App developers
4519 ** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)".
4520 **
4521 ** * No percentile(Y,P) function exists in the SQL standard. Instead of
4522 ** percential(Y,P), developers must write this:
4523 ** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that
4524 ** the fraction parameter to percentile() goes from 0 to 100 whereas
4525 ** the fraction parameter in SQL standard percentile_cont() goes from
4526 ** 0 to 1.
4527 **
4528 ** Implementation notes as of 2024-08-31:
4529 **
4530 ** * The regular aggregate-function versions of these routines work
4531 ** by accumulating all values in an array of doubles, then sorting
4532 ** that array using quicksort before computing the answer. Thus
4533 ** the runtime is O(NlogN) where N is the number of rows of input.
4534 **
4535 ** * For the window-function versions of these routines, the array of
4536 ** inputs is sorted as soon as the first value is computed. Thereafter,
4537 ** the array is kept in sorted order using an insert-sort. This
4538 ** results in O(N*K) performance where K is the size of the window.
4539 ** One can imagine alternative implementations that give O(N*logN*logK)
4540 ** performance, but they require more complex logic and data structures.
4541 ** The developers have elected to keep the asymptotically slower
4542 ** algorithm for now, for simplicity, under the theory that window
4543 ** functions are seldom used and when they are, the window size K is
4544 ** often small. The developers might revisit that decision later,
4545 ** should the need arise.
4546 */
4547 #if defined(SQLITE3_H)
4548 /* no-op */
4549 #elif defined(SQLITE_STATIC_PERCENTILE)
4550 /* # include "sqlite3.h" */
4551 #else
4552 /* # include "sqlite3ext.h" */
4553 SQLITE_EXTENSION_INIT1
4554 #endif
4555 #include <assert.h>
4556 #include <string.h>
4557 #include <stdlib.h>
4558
4559 /* The following object is the group context for a single percentile()
4560 ** aggregate. Remember all input Y values until the very end.
4561 ** Those values are accumulated in the Percentile.a[] array.
4562 */
4563 typedef struct Percentile Percentile;
4564 struct Percentile {
4565 unsigned nAlloc; /* Number of slots allocated for a[] */
4566 unsigned nUsed; /* Number of slots actually used in a[] */
4567 char bSorted; /* True if a[] is already in sorted order */
4568 char bKeepSorted; /* True if advantageous to keep a[] sorted */
4569 char bPctValid; /* True if rPct is valid */
4570 double rPct; /* Fraction. 0.0 to 1.0 */
4571 double *a; /* Array of Y values */
4572 };
4573
4574 /* Details of each function in the percentile family */
4575 typedef struct PercentileFunc PercentileFunc;
4576 struct PercentileFunc {
4577 const char *zName; /* Function name */
4578 char nArg; /* Number of arguments */
4579 char mxFrac; /* Maximum value of the "fraction" input */
4580 char bDiscrete; /* True for percentile_disc() */
4581 };
4582 static const PercentileFunc aPercentFunc[] = {
4583 { "median", 1, 1, 0 },
4584 { "percentile", 2, 100, 0 },
4585 { "percentile_cont", 2, 1, 0 },
4586 { "percentile_disc", 2, 1, 1 },
4587 };
4588
4589 /*
4590 ** Return TRUE if the input floating-point number is an infinity.
4591 */
4592 static int percentIsInfinity(double r){
4593 sqlite3_uint64 u;
4594 assert( sizeof(u)==sizeof(r) );
4595 memcpy(&u, &r, sizeof(u));
4596 return ((u>>52)&0x7ff)==0x7ff;
4597 }
4598
4599 /*
4600 ** Return TRUE if two doubles differ by 0.001 or less.
4601 */
4602 static int percentSameValue(double a, double b){
4603 a -= b;
4604 return a>=-0.001 && a<=0.001;
4605 }
4606
4607 /*
4608 ** Search p (which must have p->bSorted) looking for an entry with
4609 ** value y. Return the index of that entry.
4610 **
4611 ** If bExact is true, return -1 if the entry is not found.
4612 **
4613 ** If bExact is false, return the index at which a new entry with
4614 ** value y should be insert in order to keep the values in sorted
4615 ** order. The smallest return value in this case will be 0, and
4616 ** the largest return value will be p->nUsed.
4617 */
4618 static int percentBinarySearch(Percentile *p, double y, int bExact){
4619 int iFirst = 0; /* First element of search range */
4620 int iLast = p->nUsed - 1; /* Last element of search range */
4621 while( iLast>=iFirst ){
4622 int iMid = (iFirst+iLast)/2;
4623 double x = p->a[iMid];
4624 if( x<y ){
4625 iFirst = iMid + 1;
4626 }else if( x>y ){
4627 iLast = iMid - 1;
4628 }else{
4629 return iMid;
4630 }
4631 }
4632 if( bExact ) return -1;
4633 return iFirst;
4634 }
4635
4636 /*
4637 ** Generate an error for a percentile function.
4638 **
4639 ** The error format string must have exactly one occurrence of "%%s()"
4640 ** (with two '%' characters). That substring will be replaced by the name
4641 ** of the function.
4642 */
4643 static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){
4644 PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4645 char *zMsg1;
4646 char *zMsg2;
4647 va_list ap;
4648
4649 va_start(ap, zFormat);
4650 zMsg1 = sqlite3_vmprintf(zFormat, ap);
4651 va_end(ap);
4652 zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, pFunc->zName) : 0;
4653 sqlite3_result_error(pCtx, zMsg2, -1);
4654 sqlite3_free(zMsg1);
4655 sqlite3_free(zMsg2);
4656 }
4657
4658 /*
4659 ** The "step" function for percentile(Y,P) is called once for each
4660 ** input row.
4661 */
4662 static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
4663 Percentile *p;
4664 double rPct;
4665 int eType;
4666 double y;
4667 assert( argc==2 || argc==1 );
4668
4669 if( argc==1 ){
4670 /* Requirement 13: median(Y) is the same as percentile(Y,50). */
4671 rPct = 0.5;
4672 }else{
4673 /* Requirement 3: P must be a number between 0 and 100 */
4674 PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4675 eType = sqlite3_value_numeric_type(argv[1]);
4676 rPct = sqlite3_value_double(argv[1])/(double)pFunc->mxFrac;
4677 if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4678 || rPct<0.0 || rPct>1.0
4679 ){
4680 percentError(pCtx, "the fraction argument to %%s()"
4681 " is not between 0.0 and %.1f",
4682 (double)pFunc->mxFrac);
4683 return;
4684 }
4685 }
4686
4687 /* Allocate the session context. */
4688 p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4689 if( p==0 ) return;
4690
4691 /* Remember the P value. Throw an error if the P value is different
4692 ** from any prior row, per Requirement (2). */
4693 if( !p->bPctValid ){
4694 p->rPct = rPct;
4695 p->bPctValid = 1;
4696 }else if( !percentSameValue(p->rPct,rPct) ){
4697 percentError(pCtx, "the fraction argument to %%s()"
4698 " is not the same for all input rows");
4699 return;
4700 }
4701
4702 /* Ignore rows for which Y is NULL */
4703 eType = sqlite3_value_type(argv[0]);
4704 if( eType==SQLITE_NULL ) return;
4705
4706 /* If not NULL, then Y must be numeric. Otherwise throw an error.
4707 ** Requirement 4 */
4708 if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4709 percentError(pCtx, "input to %%s() is not numeric");
4710 return;
4711 }
4712
4713 /* Throw an error if the Y value is infinity or NaN */
4714 y = sqlite3_value_double(argv[0]);
4715 if( percentIsInfinity(y) ){
4716 percentError(pCtx, "Inf input to %%s()");
4717 return;
4718 }
4719
4720 /* Allocate and store the Y */
4721 if( p->nUsed>=p->nAlloc ){
4722 unsigned n = p->nAlloc*2 + 250;
4723 double *a = sqlite3_realloc64(p->a, sizeof(double)*n);
4724 if( a==0 ){
4725 sqlite3_free(p->a);
4726 memset(p, 0, sizeof(*p));
4727 sqlite3_result_error_nomem(pCtx);
4728 return;
4729 }
4730 p->nAlloc = n;
4731 p->a = a;
4732 }
4733 if( p->nUsed==0 ){
4734 p->a[p->nUsed++] = y;
4735 p->bSorted = 1;
4736 }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){
4737 p->a[p->nUsed++] = y;
4738 }else if( p->bKeepSorted ){
4739 int i;
4740 i = percentBinarySearch(p, y, 0);
4741 if( i<(int)p->nUsed ){
4742 memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0]));
4743 }
4744 p->a[i] = y;
4745 p->nUsed++;
4746 }else{
4747 p->a[p->nUsed++] = y;
4748 p->bSorted = 0;
4749 }
4750 }
4751
4752 /*
4753 ** Interchange two doubles.
4754 */
4755 #define SWAP_DOUBLE(X,Y) {double ttt=(X);(X)=(Y);(Y)=ttt;}
4756
4757 /*
4758 ** Sort an array of doubles.
4759 **
4760 ** Algorithm: quicksort
4761 **
4762 ** This is implemented separately rather than using the qsort() routine
4763 ** from the standard library because:
4764 **
4765 ** (1) To avoid a dependency on qsort()
4766 ** (2) To avoid the function call to the comparison routine for each
4767 ** comparison.
4768 */
4769 static void percentSort(double *a, unsigned int n){
4770 int iLt; /* Entries before a[iLt] are less than rPivot */
4771 int iGt; /* Entries at or after a[iGt] are greater than rPivot */
4772 int i; /* Loop counter */
4773 double rPivot; /* The pivot value */
4774
4775 assert( n>=2 );
4776 if( a[0]>a[n-1] ){
4777 SWAP_DOUBLE(a[0],a[n-1])
4778 }
4779 if( n==2 ) return;
4780 iGt = n-1;
4781 i = n/2;
4782 if( a[0]>a[i] ){
4783 SWAP_DOUBLE(a[0],a[i])
4784 }else if( a[i]>a[iGt] ){
4785 SWAP_DOUBLE(a[i],a[iGt])
4786 }
4787 if( n==3 ) return;
4788 rPivot = a[i];
4789 iLt = i = 1;
4790 do{
4791 if( a[i]<rPivot ){
4792 if( i>iLt ) SWAP_DOUBLE(a[i],a[iLt])
4793 iLt++;
4794 i++;
4795 }else if( a[i]>rPivot ){
4796 do{
4797 iGt--;
4798 }while( iGt>i && a[iGt]>rPivot );
4799 SWAP_DOUBLE(a[i],a[iGt])
4800 }else{
4801 i++;
4802 }
4803 }while( i<iGt );
4804 if( iLt>=2 ) percentSort(a, iLt);
4805 if( n-iGt>=2 ) percentSort(a+iGt, n-iGt);
4806
4807 /* Uncomment for testing */
4808 #if 0
4809 for(i=0; i<n-1; i++){
4810 assert( a[i]<=a[i+1] );
4811 }
4812 #endif
4813 }
4814
4815
4816 /*
4817 ** The "inverse" function for percentile(Y,P) is called to remove a
4818 ** row that was previously inserted by "step".
4819 */
4820 static void percentInverse(sqlite3_context *pCtx,int argc,sqlite3_value **argv){
4821 Percentile *p;
4822 int eType;
4823 double y;
4824 int i;
4825 assert( argc==2 || argc==1 );
4826
4827 /* Allocate the session context. */
4828 p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4829 assert( p!=0 );
4830
4831 /* Ignore rows for which Y is NULL */
4832 eType = sqlite3_value_type(argv[0]);
4833 if( eType==SQLITE_NULL ) return;
4834
4835 /* If not NULL, then Y must be numeric. Otherwise throw an error.
4836 ** Requirement 4 */
4837 if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4838 return;
4839 }
4840
4841 /* Ignore the Y value if it is infinity or NaN */
4842 y = sqlite3_value_double(argv[0]);
4843 if( percentIsInfinity(y) ){
4844 return;
4845 }
4846 if( p->bSorted==0 ){
4847 assert( p->nUsed>1 );
4848 percentSort(p->a, p->nUsed);
4849 p->bSorted = 1;
4850 }
4851 p->bKeepSorted = 1;
4852
4853 /* Find and remove the row */
4854 i = percentBinarySearch(p, y, 1);
4855 if( i>=0 ){
4856 p->nUsed--;
4857 if( i<(int)p->nUsed ){
4858 memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0]));
4859 }
4860 }
4861 }
4862
4863 /*
4864 ** Compute the final output of percentile(). Clean up all allocated
4865 ** memory if and only if bIsFinal is true.
4866 */
4867 static void percentCompute(sqlite3_context *pCtx, int bIsFinal){
4868 Percentile *p;
4869 PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx);
4870 unsigned i1, i2;
4871 double v1, v2;
4872 double ix, vx;
4873 p = (Percentile*)sqlite3_aggregate_context(pCtx, 0);
4874 if( p==0 ) return;
4875 if( p->a==0 ) return;
4876 if( p->nUsed ){
4877 if( p->bSorted==0 ){
4878 assert( p->nUsed>1 );
4879 percentSort(p->a, p->nUsed);
4880 p->bSorted = 1;
4881 }
4882 ix = p->rPct*(p->nUsed-1);
4883 i1 = (unsigned)ix;
4884 if( pFunc->bDiscrete ){
4885 vx = p->a[i1];
4886 }else{
4887 i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1;
4888 v1 = p->a[i1];
4889 v2 = p->a[i2];
4890 vx = v1 + (v2-v1)*(ix-i1);
4891 }
4892 sqlite3_result_double(pCtx, vx);
4893 }
4894 if( bIsFinal ){
4895 sqlite3_free(p->a);
4896 memset(p, 0, sizeof(*p));
4897 }else{
4898 p->bKeepSorted = 1;
4899 }
4900 }
4901 static void percentFinal(sqlite3_context *pCtx){
4902 percentCompute(pCtx, 1);
4903 }
4904 static void percentValue(sqlite3_context *pCtx){
4905 percentCompute(pCtx, 0);
4906 }
4907
4908 #if defined(_WIN32) && !defined(SQLITE3_H) && !defined(SQLITE_STATIC_PERCENTILE)
4909
4910 #endif
4911 int sqlite3_percentile_init(
4912 sqlite3 *db,
4913 char **pzErrMsg,
4914 const sqlite3_api_routines *pApi
4915 ){
4916 int rc = SQLITE_OK;
4917 unsigned int i;
4918 #ifdef SQLITE3EXT_H
4919 SQLITE_EXTENSION_INIT2(pApi);
4920 #else
4921 (void)pApi; /* Unused parameter */
4922 #endif
4923 (void)pzErrMsg; /* Unused parameter */
4924 for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
4925 rc = sqlite3_create_window_function(db,
4926 aPercentFunc[i].zName,
4927 aPercentFunc[i].nArg,
4928 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_SELFORDER1,
4929 (void*)&aPercentFunc[i],
4930 percentStep, percentFinal, percentValue, percentInverse, 0);
4931 if( rc ) break;
4932 }
4933 return rc;
4934 }
4935
4936 /************************* End ../ext/misc/percentile.c ********************/
4937 #undef sqlite3_base_init
4938 #define sqlite3_base_init sqlite3_base64_init
4939 /************************* Begin ../ext/misc/base64.c ******************/
4940 /*
4941 ** 2022-11-18
@@ -5144,20 +4710,22 @@
5144 return pOut;
5145 }
5146
5147 /* This function does the work for the SQLite base64(x) UDF. */
5148 static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
5149 int nb, nc, nv = sqlite3_value_bytes(av[0]);
 
 
5150 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
5151 SQLITE_LIMIT_LENGTH, -1);
5152 char *cBuf;
5153 u8 *bBuf;
5154 assert(na==1);
5155 switch( sqlite3_value_type(av[0]) ){
5156 case SQLITE_BLOB:
5157 nb = nv;
5158 nc = 4*(nv+2/3); /* quads needed */
5159 nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
5160 if( nvMax < nc ){
5161 sqlite3_result_error(context, "blob expanded to base64 too big", -1);
5162 return;
5163 }
@@ -5524,11 +5092,11 @@
5524 }
5525 # endif
5526
5527 /* This function does the work for the SQLite base85(x) UDF. */
5528 static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5529 int nb, nc, nv = sqlite3_value_bytes(av[0]);
5530 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
5531 SQLITE_LIMIT_LENGTH, -1);
5532 char *cBuf;
5533 u8 *bBuf;
5534 assert(na==1);
@@ -5829,10 +5397,13 @@
5829 }
5830 memcpy(&a,&r,sizeof(a));
5831 if( a==0 ){
5832 e = 0;
5833 m = 0;
 
 
 
5834 }else{
5835 e = a>>52;
5836 m = a & ((((sqlite3_int64)1)<<52)-1);
5837 if( e==0 ){
5838 m <<= 1;
@@ -6052,23 +5623,24 @@
6052 ** Examples:
6053 **
6054 ** SELECT * FROM generate_series(0,100,5);
6055 **
6056 ** The query above returns integers from 0 through 100 counting by steps
6057 ** of 5.
 
6058 **
6059 ** SELECT * FROM generate_series(0,100);
6060 **
6061 ** Integers from 0 through 100 with a step size of 1.
6062 **
6063 ** SELECT * FROM generate_series(20) LIMIT 10;
6064 **
6065 ** Integers 20 through 29.
6066 **
6067 ** SELECT * FROM generate_series(0,-100,-5);
6068 **
6069 ** Integers 0 -5 -10 ... -100.
6070 **
6071 ** SELECT * FROM generate_series(0,-1);
6072 **
6073 ** Empty sequence.
6074 **
@@ -6140,143 +5712,92 @@
6140 #include <string.h>
6141 #include <limits.h>
6142 #include <math.h>
6143
6144 #ifndef SQLITE_OMIT_VIRTUALTABLE
6145 /*
6146 ** Return that member of a generate_series(...) sequence whose 0-based
6147 ** index is ix. The 0th member is given by smBase. The sequence members
6148 ** progress per ix increment by smStep.
6149 */
6150 static sqlite3_int64 genSeqMember(
6151 sqlite3_int64 smBase,
6152 sqlite3_int64 smStep,
6153 sqlite3_uint64 ix
6154 ){
6155 static const sqlite3_uint64 mxI64 =
6156 ((sqlite3_uint64)0x7fffffff)<<32 | 0xffffffff;
6157 if( ix>=mxI64 ){
6158 /* Get ix into signed i64 range. */
6159 ix -= mxI64;
6160 /* With 2's complement ALU, this next can be 1 step, but is split into
6161 * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */
6162 smBase += (mxI64/2) * smStep;
6163 smBase += (mxI64 - mxI64/2) * smStep;
6164 }
6165 /* Under UBSAN (or on 1's complement machines), must do this last term
6166 * in steps to avoid the dreaded (and harmless) signed multiply overflow. */
6167 if( ix>=2 ){
6168 sqlite3_int64 ix2 = (sqlite3_int64)ix/2;
6169 smBase += ix2*smStep;
6170 ix -= ix2;
6171 }
6172 return smBase + ((sqlite3_int64)ix)*smStep;
6173 }
6174
6175 /* typedef unsigned char u8; */
6176
6177 typedef struct SequenceSpec {
6178 sqlite3_int64 iOBase; /* Original starting value ("start") */
6179 sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
6180 sqlite3_int64 iBase; /* Starting value to actually use */
6181 sqlite3_int64 iTerm; /* Terminal value to actually use */
6182 sqlite3_int64 iStep; /* Increment ("step") */
6183 sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
6184 sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
6185 sqlite3_int64 iValueNow; /* Current value during generation */
6186 u8 isNotEOF; /* Sequence generation not exhausted */
6187 u8 isReversing; /* Sequence is being reverse generated */
6188 } SequenceSpec;
6189
6190 /*
6191 ** Prepare a SequenceSpec for use in generating an integer series
6192 ** given initialized iBase, iTerm and iStep values. Sequence is
6193 ** initialized per given isReversing. Other members are computed.
6194 */
6195 static void setupSequence( SequenceSpec *pss ){
6196 int bSameSigns;
6197 pss->uSeqIndexMax = 0;
6198 pss->isNotEOF = 0;
6199 bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0);
6200 if( pss->iTerm < pss->iBase ){
6201 sqlite3_uint64 nuspan = 0;
6202 if( bSameSigns ){
6203 nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm);
6204 }else{
6205 /* Under UBSAN (or on 1's complement machines), must do this in steps.
6206 * In this clause, iBase>=0 and iTerm<0 . */
6207 nuspan = 1;
6208 nuspan += pss->iBase;
6209 nuspan += -(pss->iTerm+1);
6210 }
6211 if( pss->iStep<0 ){
6212 pss->isNotEOF = 1;
6213 if( nuspan==ULONG_MAX ){
6214 pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1;
6215 }else if( pss->iStep>LLONG_MIN ){
6216 pss->uSeqIndexMax = nuspan/-pss->iStep;
6217 }
6218 }
6219 }else if( pss->iTerm > pss->iBase ){
6220 sqlite3_uint64 puspan = 0;
6221 if( bSameSigns ){
6222 puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase);
6223 }else{
6224 /* Under UBSAN (or on 1's complement machines), must do this in steps.
6225 * In this clause, iTerm>=0 and iBase<0 . */
6226 puspan = 1;
6227 puspan += pss->iTerm;
6228 puspan += -(pss->iBase+1);
6229 }
6230 if( pss->iStep>0 ){
6231 pss->isNotEOF = 1;
6232 pss->uSeqIndexMax = puspan/pss->iStep;
6233 }
6234 }else if( pss->iTerm == pss->iBase ){
6235 pss->isNotEOF = 1;
6236 pss->uSeqIndexMax = 0;
6237 }
6238 pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0;
6239 pss->iValueNow = (pss->isReversing)
6240 ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax)
6241 : pss->iBase;
6242 }
6243
6244 /*
6245 ** Progress sequence generator to yield next value, if any.
6246 ** Leave its state to either yield next value or be at EOF.
6247 ** Return whether there is a next value, or 0 at EOF.
6248 */
6249 static int progressSequence( SequenceSpec *pss ){
6250 if( !pss->isNotEOF ) return 0;
6251 if( pss->isReversing ){
6252 if( pss->uSeqIndexNow > 0 ){
6253 pss->uSeqIndexNow--;
6254 pss->iValueNow -= pss->iStep;
6255 }else{
6256 pss->isNotEOF = 0;
6257 }
6258 }else{
6259 if( pss->uSeqIndexNow < pss->uSeqIndexMax ){
6260 pss->uSeqIndexNow++;
6261 pss->iValueNow += pss->iStep;
6262 }else{
6263 pss->isNotEOF = 0;
6264 }
6265 }
6266 return pss->isNotEOF;
6267 }
6268
6269 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
6270 ** serve as the underlying representation of a cursor that scans
6271 ** over rows of the result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6272 */
 
6273 typedef struct series_cursor series_cursor;
6274 struct series_cursor {
6275 sqlite3_vtab_cursor base; /* Base class - must be first */
6276 SequenceSpec ss; /* (this) Derived class data */
 
 
 
 
 
 
 
 
6277 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6278
6279 /*
6280 ** The seriesConnect() method is invoked to create a new
6281 ** series_vtab that describes the generate_series virtual table.
6282 **
@@ -6354,11 +5875,19 @@
6354 /*
6355 ** Advance a series_cursor to its next row of output.
6356 */
6357 static int seriesNext(sqlite3_vtab_cursor *cur){
6358 series_cursor *pCur = (series_cursor*)cur;
6359 progressSequence( & pCur->ss );
 
 
 
 
 
 
 
 
6360 return SQLITE_OK;
6361 }
6362
6363 /*
6364 ** Return values of columns for the row at which the series_cursor
@@ -6370,41 +5899,41 @@
6370 int i /* Which column to return */
6371 ){
6372 series_cursor *pCur = (series_cursor*)cur;
6373 sqlite3_int64 x = 0;
6374 switch( i ){
6375 case SERIES_COLUMN_START: x = pCur->ss.iOBase; break;
6376 case SERIES_COLUMN_STOP: x = pCur->ss.iOTerm; break;
6377 case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
6378 default: x = pCur->ss.iValueNow; break;
6379 }
6380 sqlite3_result_int64(ctx, x);
6381 return SQLITE_OK;
6382 }
6383
6384 #ifndef LARGEST_UINT64
6385 #define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
6386 #define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
6387 #define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
6388 #endif
6389
6390 /*
6391 ** The rowid is the same as the value.
6392 */
6393 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
6394 series_cursor *pCur = (series_cursor*)cur;
6395 *pRowid = pCur->ss.iValueNow;
6396 return SQLITE_OK;
6397 }
6398
6399 /*
6400 ** Return TRUE if the cursor has been moved off of the last
6401 ** row of output.
6402 */
6403 static int seriesEof(sqlite3_vtab_cursor *cur){
6404 series_cursor *pCur = (series_cursor*)cur;
6405 return !pCur->ss.isNotEOF;
6406 }
6407
6408 /* True to cause run-time checking of the start=, stop=, and/or step=
6409 ** parameters. The only reason to do this is for testing the
6410 ** constraint checking logic for virtual tables in the SQLite core.
@@ -6411,10 +5940,63 @@
6411 */
6412 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
6413 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
6414 #endif
6415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6416 /*
6417 ** This method is called to "rewind" the series_cursor object back
6418 ** to the first row of output. This method is always called at least
6419 ** once prior to any call to seriesColumn() or seriesRowid() or
6420 ** seriesEof().
@@ -6444,185 +6026,243 @@
6444 sqlite3_vtab_cursor *pVtabCursor,
6445 int idxNum, const char *idxStrUnused,
6446 int argc, sqlite3_value **argv
6447 ){
6448 series_cursor *pCur = (series_cursor *)pVtabCursor;
6449 int i = 0;
6450 int returnNoRows = 0;
6451 sqlite3_int64 iMin = SMALLEST_INT64;
6452 sqlite3_int64 iMax = LARGEST_INT64;
6453 sqlite3_int64 iLimit = 0;
6454 sqlite3_int64 iOffset = 0;
6455
6456 (void)idxStrUnused;
 
 
 
 
 
 
 
 
 
 
 
 
 
6457 if( idxNum & 0x01 ){
6458 pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
6459 }else{
6460 pCur->ss.iBase = 0;
6461 }
6462 if( idxNum & 0x02 ){
6463 pCur->ss.iTerm = sqlite3_value_int64(argv[i++]);
6464 }else{
6465 pCur->ss.iTerm = 0xffffffff;
6466 }
6467 if( idxNum & 0x04 ){
6468 pCur->ss.iStep = sqlite3_value_int64(argv[i++]);
6469 if( pCur->ss.iStep==0 ){
6470 pCur->ss.iStep = 1;
6471 }else if( pCur->ss.iStep<0 ){
6472 if( (idxNum & 0x10)==0 ) idxNum |= 0x08;
6473 }
6474 }else{
6475 pCur->ss.iStep = 1;
6476 }
6477
6478 /* If there are constraints on the value column but there are
6479 ** no constraints on the start, stop, and step columns, then
6480 ** initialize the default range to be the entire range of 64-bit signed
6481 ** integers. This range will contracted by the value column constraints
6482 ** further below.
6483 */
6484 if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
6485 pCur->ss.iBase = SMALLEST_INT64;
6486 }
6487 if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
6488 pCur->ss.iTerm = LARGEST_INT64;
6489 }
6490 pCur->ss.iOBase = pCur->ss.iBase;
6491 pCur->ss.iOTerm = pCur->ss.iTerm;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6492
6493 /* Extract the LIMIT and OFFSET values, but do not apply them yet.
6494 ** The range must first be constrained by the limits on value.
6495 */
6496 if( idxNum & 0x20 ){
6497 iLimit = sqlite3_value_int64(argv[i++]);
6498 if( idxNum & 0x40 ){
6499 iOffset = sqlite3_value_int64(argv[i++]);
6500 }
6501 }
6502
 
 
 
6503 if( idxNum & 0x3380 ){
6504 /* Extract the maximum range of output values determined by
6505 ** constraints on the "value" column.
6506 */
6507 if( idxNum & 0x0080 ){
6508 if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6509 double r = sqlite3_value_double(argv[i++]);
6510 if( r==ceil(r) ){
6511 iMin = iMax = (sqlite3_int64)r;
6512 }else{
6513 returnNoRows = 1;
6514 }
6515 }else{
6516 iMin = iMax = sqlite3_value_int64(argv[i++]);
6517 }
6518 }else{
6519 if( idxNum & 0x0300 ){
6520 if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6521 double r = sqlite3_value_double(argv[i++]);
6522 if( idxNum & 0x0200 && r==ceil(r) ){
6523 iMin = (sqlite3_int64)ceil(r+1.0);
6524 }else{
6525 iMin = (sqlite3_int64)ceil(r);
6526 }
6527 }else{
6528 iMin = sqlite3_value_int64(argv[i++]);
6529 if( idxNum & 0x0200 ){
 
 
6530 if( iMin==LARGEST_INT64 ){
6531 returnNoRows = 1;
6532 }else{
6533 iMin++;
6534 }
6535 }
6536 }
6537 }
6538 if( idxNum & 0x3000 ){
6539 if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){
6540 double r = sqlite3_value_double(argv[i++]);
6541 if( (idxNum & 0x2000)!=0 && r==floor(r) ){
 
 
6542 iMax = (sqlite3_int64)(r-1.0);
6543 }else{
6544 iMax = (sqlite3_int64)floor(r);
6545 }
6546 }else{
6547 iMax = sqlite3_value_int64(argv[i++]);
6548 if( idxNum & 0x2000 ){
6549 if( iMax==SMALLEST_INT64 ){
6550 returnNoRows = 1;
6551 }else{
6552 iMax--;
6553 }
6554 }
6555 }
6556 }
6557 if( iMin>iMax ){
6558 returnNoRows = 1;
6559 }
6560 }
6561
6562 /* Try to reduce the range of values to be generated based on
6563 ** constraints on the "value" column.
6564 */
6565 if( pCur->ss.iStep>0 ){
6566 sqlite3_int64 szStep = pCur->ss.iStep;
6567 if( pCur->ss.iBase<iMin ){
6568 sqlite3_uint64 d = iMin - pCur->ss.iBase;
6569 pCur->ss.iBase += ((d+szStep-1)/szStep)*szStep;
6570 }
6571 if( pCur->ss.iTerm>iMax ){
6572 pCur->ss.iTerm = iMax;
 
 
 
 
 
6573 }
6574 }else{
6575 sqlite3_int64 szStep = -pCur->ss.iStep;
6576 assert( szStep>0 );
6577 if( pCur->ss.iBase>iMax ){
6578 sqlite3_uint64 d = pCur->ss.iBase - iMax;
6579 pCur->ss.iBase -= ((d+szStep-1)/szStep)*szStep;
6580 }
6581 if( pCur->ss.iTerm<iMin ){
6582 pCur->ss.iTerm = iMin;
 
 
 
 
6583 }
6584 }
6585 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6586
6587 /* Apply LIMIT and OFFSET constraints, if any */
 
6588 if( idxNum & 0x20 ){
6589 if( iOffset>0 ){
6590 pCur->ss.iBase += pCur->ss.iStep*iOffset;
6591 }
6592 if( iLimit>=0 ){
6593 sqlite3_int64 iTerm;
6594 iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
6595 if( pCur->ss.iStep<0 ){
6596 if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6597 }else{
6598 if( iTerm<pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
6599 }
6600 }
6601 }
6602
6603
6604 for(i=0; i<argc; i++){
6605 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6606 /* If any of the constraints have a NULL value, then return no rows.
6607 ** See ticket https://sqlite.org/src/info/fac496b61722daf2 */
6608 returnNoRows = 1;
6609 break;
6610 }
6611 }
6612 if( returnNoRows ){
6613 pCur->ss.iBase = 1;
6614 pCur->ss.iTerm = 0;
6615 pCur->ss.iStep = 1;
6616 }
6617 if( idxNum & 0x08 ){
6618 pCur->ss.isReversing = pCur->ss.iStep > 0;
6619 }else{
6620 pCur->ss.isReversing = pCur->ss.iStep < 0;
6621 }
6622 setupSequence( &pCur->ss );
6623 return SQLITE_OK;
 
 
 
 
 
 
 
 
6624 }
6625
6626 /*
6627 ** SQLite will invoke this method one or more times while planning a query
6628 ** that uses the generate_series virtual table. This routine needs to create
@@ -6948,10 +6588,12 @@
6948 ** expression and M is the size of the input string. The matcher never
6949 ** exhibits exponential behavior. Note that the X{p,q} operator expands
6950 ** to p copies of X following by q-p copies of X? and that the size of the
6951 ** regular expression in the O(N*M) performance bound is computed after
6952 ** this expansion.
 
 
6953 */
6954 #include <string.h>
6955 #include <stdlib.h>
6956 /* #include "sqlite3ext.h" */
6957 SQLITE_EXTENSION_INIT1
@@ -6988,36 +6630,10 @@
6988 #define RE_OP_NOTDIGIT 14 /* Not a digit */
6989 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
6990 #define RE_OP_NOTSPACE 16 /* Not a digit */
6991 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6992 #define RE_OP_ATSTART 18 /* Currently at the start of the string */
6993
6994 #if defined(SQLITE_DEBUG)
6995 /* Opcode names used for symbolic debugging */
6996 static const char *ReOpName[] = {
6997 "EOF",
6998 "MATCH",
6999 "ANY",
7000 "ANYSTAR",
7001 "FORK",
7002 "GOTO",
7003 "ACCEPT",
7004 "CC_INC",
7005 "CC_EXC",
7006 "CC_VALUE",
7007 "CC_RANGE",
7008 "WORD",
7009 "NOTWORD",
7010 "DIGIT",
7011 "NOTDIGIT",
7012 "SPACE",
7013 "NOTSPACE",
7014 "BOUNDARY",
7015 "ATSTART",
7016 };
7017 #endif /* SQLITE_DEBUG */
7018
7019
7020 /* Each opcode is a "state" in the NFA */
7021 typedef unsigned short ReStateNumber;
7022
7023 /* Because this is an NFA and not a DFA, multiple states can be active at
@@ -7051,10 +6667,11 @@
7051 unsigned (*xNextChar)(ReInput*); /* Next character function */
7052 unsigned char zInit[12]; /* Initial text to match */
7053 int nInit; /* Number of bytes in zInit */
7054 unsigned nState; /* Number of entries in aOp[] and aArg[] */
7055 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
 
7056 };
7057
7058 /* Add a state to the given state set if it is not already there */
7059 static void re_add_state(ReStateSet *pSet, int newState){
7060 unsigned i;
@@ -7265,18 +6882,19 @@
7265 return rc;
7266 }
7267
7268 /* Resize the opcode and argument arrays for an RE under construction.
7269 */
7270 static int re_resize(ReCompiled *p, int N){
7271 char *aOp;
7272 int *aArg;
 
7273 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
7274 if( aOp==0 ) return 1;
7275 p->aOp = aOp;
7276 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
7277 if( aArg==0 ) return 1;
7278 p->aArg = aArg;
7279 p->nAlloc = N;
7280 return 0;
7281 }
7282
@@ -7303,11 +6921,11 @@
7303 }
7304
7305 /* Make a copy of N opcodes starting at iStart onto the end of the RE
7306 ** under construction.
7307 */
7308 static void re_copy(ReCompiled *p, int iStart, int N){
7309 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
7310 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
7311 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
7312 p->nState += N;
7313 }
@@ -7456,22 +7074,30 @@
7456 case '^': {
7457 re_append(p, RE_OP_ATSTART, 0);
7458 break;
7459 }
7460 case '{': {
7461 int m = 0, n = 0;
7462 int sz, j;
7463 if( iPrev<0 ) return "'{m,n}' without operand";
7464 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
 
 
 
 
7465 n = m;
7466 if( c==',' ){
7467 p->sIn.i++;
7468 n = 0;
7469 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
 
 
 
 
7470 }
7471 if( c!='}' ) return "unmatched '{'";
7472 if( n>0 && n<m ) return "n less than m in '{m,n}'";
7473 p->sIn.i++;
7474 sz = p->nState - iPrev;
7475 if( m==0 ){
7476 if( n==0 ) return "both m and n are zero in '{m,n}'";
7477 re_insert(p, iPrev, RE_OP_FORK, sz+1);
@@ -7483,11 +7109,11 @@
7483 for(j=m; j<n; j++){
7484 re_append(p, RE_OP_FORK, sz+1);
7485 re_copy(p, iPrev, sz);
7486 }
7487 if( n==0 && m>0 ){
7488 re_append(p, RE_OP_FORK, -sz);
7489 }
7490 break;
7491 }
7492 case '[': {
7493 unsigned int iFirst = p->nState;
@@ -7549,12 +7175,11 @@
7549
7550 /* Free and reclaim all the memory used by a previously compiled
7551 ** regular expression. Applications should invoke this routine once
7552 ** for every call to re_compile() to avoid memory leaks.
7553 */
7554 static void re_free(void *p){
7555 ReCompiled *pRe = (ReCompiled*)p;
7556 if( pRe ){
7557 sqlite3_free(pRe->aOp);
7558 sqlite3_free(pRe->aArg);
7559 sqlite3_free(pRe);
7560 }
@@ -7564,11 +7189,16 @@
7564 ** Compile a textual regular expression in zIn[] into a compiled regular
7565 ** expression suitable for us by re_match() and return a pointer to the
7566 ** compiled regular expression in *ppRe. Return NULL on success or an
7567 ** error message if something goes wrong.
7568 */
7569 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
 
 
 
 
 
7570 ReCompiled *pRe;
7571 const char *zErr;
7572 int i, j;
7573
7574 *ppRe = 0;
@@ -7576,13 +7206,15 @@
7576 if( pRe==0 ){
7577 return "out of memory";
7578 }
7579 memset(pRe, 0, sizeof(*pRe));
7580 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
 
7581 if( re_resize(pRe, 30) ){
 
7582 re_free(pRe);
7583 return "out of memory";
7584 }
7585 if( zIn[0]=='^' ){
7586 zIn++;
7587 }else{
7588 re_append(pRe, RE_OP_ANYSTAR, 0);
@@ -7630,10 +7262,18 @@
7630 if( j>0 && pRe->zInit[j-1]==0 ) j--;
7631 pRe->nInit = j;
7632 }
7633 return pRe->zErr;
7634 }
 
 
 
 
 
 
 
 
7635
7636 /*
7637 ** Implementation of the regexp() SQL function. This function implements
7638 ** the build-in REGEXP operator. The first argument to the function is the
7639 ** pattern and the second argument is the string. So, the SQL statements:
@@ -7656,11 +7296,12 @@
7656 (void)argc; /* Unused */
7657 pRe = sqlite3_get_auxdata(context, 0);
7658 if( pRe==0 ){
7659 zPattern = (const char*)sqlite3_value_text(argv[0]);
7660 if( zPattern==0 ) return;
7661 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
 
7662 if( zErr ){
7663 re_free(pRe);
7664 sqlite3_result_error(context, zErr, -1);
7665 return;
7666 }
@@ -7698,14 +7339,36 @@
7698 sqlite3_str *pStr;
7699 int i;
7700 int n;
7701 char *z;
7702 (void)argc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7703
7704 zPattern = (const char*)sqlite3_value_text(argv[0]);
7705 if( zPattern==0 ) return;
7706 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
 
7707 if( zErr ){
7708 re_free(pRe);
7709 sqlite3_result_error(context, zErr, -1);
7710 return;
7711 }
@@ -9284,18 +8947,20 @@
9284 if( idxNum & 1 ){
9285 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
9286 if( pCur->nPrefix>0 ){
9287 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
9288 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
 
9289 }
9290 iArg = 1;
9291 }
9292 if( idxNum & 2 ){
9293 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
9294 if( pCur->nLine>0 ){
9295 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
9296 if( pCur->zLine==0 ) return SQLITE_NOMEM;
 
9297 }
9298 }
9299 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
9300 int i = pCur->nLine;
9301 while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
@@ -9303,10 +8968,11 @@
9303 }
9304 pCur->nPrefix = pCur->nLine - i;
9305 if( pCur->nPrefix>0 ){
9306 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
9307 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
 
9308 }
9309 }
9310 pCur->iRowid = 0;
9311 pCur->ePhase = COMPLETION_FIRST_PHASE;
9312 return completionNext(pVtabCursor);
@@ -10216,11 +9882,17 @@
10216 "method," /* 6: Compression method (integer) */
10217 "z HIDDEN" /* 7: Name of zip file */
10218 ") WITHOUT ROWID;";
10219
10220 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
10221 #define ZIPFILE_BUFFER_SIZE (64*1024)
 
 
 
 
 
 
10222
10223
10224 /*
10225 ** Magic numbers used to read and write zip files.
10226 **
@@ -10773,10 +10445,11 @@
10773 pLFH->crc32 = zipfileRead32(aRead);
10774 pLFH->szCompressed = zipfileRead32(aRead);
10775 pLFH->szUncompressed = zipfileRead32(aRead);
10776 pLFH->nFile = zipfileRead16(aRead);
10777 pLFH->nExtra = zipfileRead16(aRead);
 
10778 }
10779 return rc;
10780 }
10781
10782
@@ -10898,10 +10571,19 @@
10898 || mUnixTime==zipfileMtime(pCds)
10899 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
10900 /* || (mUnixTime % 2) */
10901 );
10902 }
 
 
 
 
 
 
 
 
 
10903
10904 /*
10905 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
10906 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
10907 ** then pFile is a file-handle open on a zip file. In either case, this
@@ -10921,16 +10603,19 @@
10921 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
10922 ){
10923 u8 *aRead;
10924 char **pzErr = &pTab->base.zErrMsg;
10925 int rc = SQLITE_OK;
10926 (void)nBlob;
10927
10928 if( aBlob==0 ){
10929 aRead = pTab->aBuffer;
10930 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
10931 }else{
 
 
 
 
10932 aRead = (u8*)&aBlob[iOff];
10933 }
10934
10935 if( rc==SQLITE_OK ){
10936 sqlite3_int64 nAlloc;
@@ -10957,10 +10642,13 @@
10957 rc = zipfileReadData(
10958 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
10959 );
10960 }else{
10961 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
 
 
 
10962 }
10963 }
10964
10965 if( rc==SQLITE_OK ){
10966 u32 *pt = &pNew->mUnixTime;
@@ -10979,19 +10667,26 @@
10979 ZipfileLFH lfh;
10980 if( pFile ){
10981 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
10982 }else{
10983 aRead = (u8*)&aBlob[pNew->cds.iOffset];
 
 
 
10984 }
10985
10986 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
10987 if( rc==SQLITE_OK ){
10988 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
10989 pNew->iDataOff += lfh.nFile + lfh.nExtra;
10990 if( aBlob && pNew->cds.szCompressed ){
10991 pNew->aData = &pNew->aExtra[nExtra];
10992 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
 
 
 
 
10993 }
10994 }else{
10995 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
10996 (int)pNew->cds.iOffset
10997 );
@@ -11774,10 +11469,15 @@
11774
11775 if( rc==SQLITE_OK ){
11776 zPath = (const char*)sqlite3_value_text(apVal[2]);
11777 if( zPath==0 ) zPath = "";
11778 nPath = (int)strlen(zPath);
 
 
 
 
 
11779 mTime = zipfileGetTime(apVal[4]);
11780 }
11781
11782 if( rc==SQLITE_OK && bIsDir ){
11783 /* For a directory, check that the last character in the path is a
@@ -12135,10 +11835,17 @@
12135 if( zName==0 ){
12136 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
12137 rc = SQLITE_ERROR;
12138 goto zipfile_step_out;
12139 }
 
 
 
 
 
 
 
12140
12141 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
12142 ** deflate compression) or NULL (choose automatically). */
12143 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
12144 iMethod = (int)sqlite3_value_int64(pMethod);
@@ -12477,10 +12184,11 @@
12477 return rc;
12478 }
12479
12480 /************************* End ../ext/misc/sqlar.c ********************/
12481 #endif
 
12482 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
12483 /*
12484 ** 2017 April 07
12485 **
12486 ** The author disclaims copyright to this source code. In place of
@@ -14885,10 +14593,11 @@
14885 }
14886
14887 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14888
14889 /************************* End ../ext/expert/sqlite3expert.c ********************/
 
14890 /************************* Begin ../ext/intck/sqlite3intck.h ******************/
14891 /*
14892 ** 2024-02-08
14893 **
14894 ** The author disclaims copyright to this source code. In place of
@@ -21525,15 +21234,17 @@
21525 char **azFilter; /* Array of xFilter rejection GLOB patterns */
21526 sqlite3_session *p; /* The open session */
21527 };
21528 #endif
21529
 
21530 typedef struct ExpertInfo ExpertInfo;
21531 struct ExpertInfo {
21532 sqlite3expert *pExpert;
21533 int bVerbose;
21534 };
 
21535
21536 /* A single line in the EQP output */
21537 typedef struct EQPGraphRow EQPGraphRow;
21538 struct EQPGraphRow {
21539 int iEqpId; /* ID for this row */
@@ -21633,11 +21344,13 @@
21633 int *aiIndent; /* Array of indents used in MODE_Explain */
21634 int nIndent; /* Size of array aiIndent[] */
21635 int iIndent; /* Index of current op in aiIndent[] */
21636 char *zNonce; /* Nonce for temporary safe-mode escapes */
21637 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
 
21638 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
 
21639 #ifdef SQLITE_SHELL_FIDDLE
21640 struct {
21641 const char * zInput; /* Input string from wasm/JS proxy */
21642 const char * zPos; /* Cursor pos into zInput */
21643 const char * zDefaultDbName; /* Default name for db file */
@@ -21661,13 +21374,12 @@
21661 */
21662 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
21663 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
21664 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
21665 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
21666 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
21667 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
21668 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
21669
21670 /* Allowed values for ShellState.eTraceType
21671 */
21672 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
21673 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
@@ -22006,11 +21718,11 @@
22006 */
22007 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
22008 int i;
22009 unsigned char *aBlob = (unsigned char*)pBlob;
22010
22011 char *zStr = sqlite3_malloc(nBlob*2 + 1);
22012 shell_check_oom(zStr);
22013
22014 for(i=0; i<nBlob; i++){
22015 static const char aHex[] = {
22016 '0', '1', '2', '3', '4', '5', '6', '7',
@@ -22027,11 +21739,11 @@
22027
22028 /*
22029 ** Output the given string as a quoted string using SQL quoting conventions:
22030 **
22031 ** (1) Single quotes (') within the string are doubled
22032 ** (2) The whle string is enclosed in '...'
22033 ** (3) Control characters other than \n, \t, and \r\n are escaped
22034 ** using \u00XX notation and if such substitutions occur,
22035 ** the whole string is enclosed in unistr('...') instead of '...'.
22036 **
22037 ** Step (3) is omitted if the control-character escape mode is OFF.
@@ -22273,11 +21985,11 @@
22273 **
22274 ** Escaping is needed if the string contains any control characters
22275 ** other than \t, \n, and \r\n
22276 **
22277 ** If no escaping is needed (the common case) then set *ppFree to NULL
22278 ** and return the original string. If escapingn is needed, write the
22279 ** escaped string into memory obtained from sqlite3_malloc64() or the
22280 ** equivalent, and return the new string and set *ppFree to the new string
22281 ** as well.
22282 **
22283 ** The caller is responsible for freeing *ppFree if it is non-NULL in order
@@ -23278,32 +22990,21 @@
23278 ** Set the destination table field of the ShellState structure to
23279 ** the name of the table given. Escape any quote characters in the
23280 ** table name.
23281 */
23282 static void set_table_name(ShellState *p, const char *zName){
23283 int i, n;
23284 char cQuote;
23285 char *z;
23286
23287 if( p->zDestTable ){
23288 free(p->zDestTable);
23289 p->zDestTable = 0;
23290 }
23291 if( zName==0 ) return;
23292 cQuote = quoteChar(zName);
23293 n = strlen30(zName);
23294 if( cQuote ) n += n+2;
23295 z = p->zDestTable = malloc( n+1 );
23296 shell_check_oom(z);
23297 n = 0;
23298 if( cQuote ) z[n++] = cQuote;
23299 for(i=0; zName[i]; i++){
23300 z[n++] = zName[i];
23301 if( zName[i]==cQuote ) z[n++] = cQuote;
23302 }
23303 if( cQuote ) z[n++] = cQuote;
23304 z[n] = 0;
23305 }
23306
23307 /*
23308 ** Maybe construct two lines of text that point out the position of a
23309 ** syntax error. Return a pointer to the text, in memory obtained from
@@ -23496,12 +23197,12 @@
23496 static int display_stats(
23497 sqlite3 *db, /* Database to query */
23498 ShellState *pArg, /* Pointer to ShellState */
23499 int bReset /* True to reset the stats */
23500 ){
23501 int iCur;
23502 int iHiwtr;
23503 FILE *out;
23504 if( pArg==0 || pArg->out==0 ) return 0;
23505 out = pArg->out;
23506
23507 if( pArg->pStmt && pArg->statsOn==2 ){
@@ -23586,18 +23287,25 @@
23586 "Page cache hits: %d\n", iCur);
23587 iHiwtr = iCur = -1;
23588 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
23589 sqlite3_fprintf(out,
23590 "Page cache misses: %d\n", iCur);
 
 
 
23591 iHiwtr = iCur = -1;
23592 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
23593 sqlite3_fprintf(out,
23594 "Page cache writes: %d\n", iCur);
23595 iHiwtr = iCur = -1;
23596 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
23597 sqlite3_fprintf(out,
23598 "Page cache spills: %d\n", iCur);
 
 
 
 
23599 iHiwtr = iCur = -1;
23600 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
23601 sqlite3_fprintf(out,
23602 "Schema Heap Usage: %d bytes\n", iCur);
23603 iHiwtr = iCur = -1;
@@ -23999,10 +23707,40 @@
23999 char *zBuf = sqlite3_malloc64( szVar-5 );
24000 if( zBuf ){
24001 memcpy(zBuf, &zVar[6], szVar-5);
24002 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
24003 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24004 }else{
24005 sqlite3_bind_null(pStmt, i);
24006 }
24007 sqlite3_reset(pQ);
24008 }
@@ -24581,11 +24319,11 @@
24581 }
24582 }
24583 }
24584 }
24585
24586 #ifndef SQLITE_OMIT_VIRTUALTABLE
24587 /*
24588 ** This function is called to process SQL if the previous shell command
24589 ** was ".expert". It passes the SQL in the second argument directly to
24590 ** the sqlite3expert object.
24591 **
@@ -24713,11 +24451,11 @@
24713 }
24714 sqlite3_free(zErr);
24715
24716 return rc;
24717 }
24718 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
24719
24720 /*
24721 ** Execute a statement or set of statements. Print
24722 ** any result rows/columns depending on the current mode
24723 ** set via the supplied callback.
@@ -24739,11 +24477,11 @@
24739
24740 if( pzErrMsg ){
24741 *pzErrMsg = NULL;
24742 }
24743
24744 #ifndef SQLITE_OMIT_VIRTUALTABLE
24745 if( pArg->expert.pExpert ){
24746 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
24747 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
24748 }
24749 #endif
@@ -24901,11 +24639,11 @@
24901 static char **tableColumnList(ShellState *p, const char *zTab){
24902 char **azCol = 0;
24903 sqlite3_stmt *pStmt;
24904 char *zSql;
24905 int nCol = 0;
24906 int nAlloc = 0;
24907 int nPK = 0; /* Number of PRIMARY KEY columns seen */
24908 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
24909 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
24910 int rc;
24911
@@ -24915,11 +24653,11 @@
24915 sqlite3_free(zSql);
24916 if( rc ) return 0;
24917 while( sqlite3_step(pStmt)==SQLITE_ROW ){
24918 if( nCol>=nAlloc-2 ){
24919 nAlloc = nAlloc*2 + nCol + 10;
24920 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
24921 shell_check_oom(azCol);
24922 }
24923 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
24924 shell_check_oom(azCol[nCol]);
24925 if( sqlite3_column_int(pStmt, 5) ){
@@ -25106,17 +24844,17 @@
25106 appendText(&sSelect, " FROM ", 0);
25107 appendText(&sSelect, zTable, quoteChar(zTable));
25108
25109 savedDestTable = p->zDestTable;
25110 savedMode = p->mode;
25111 p->zDestTable = sTable.z;
25112 p->mode = p->cMode = MODE_Insert;
25113 rc = shell_exec(p, sSelect.z, 0);
25114 if( (rc&0xff)==SQLITE_CORRUPT ){
25115 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
25116 toggleSelectOrder(p->db);
25117 shell_exec(p, sSelect.z, 0);
25118 toggleSelectOrder(p->db);
25119 }
25120 p->zDestTable = savedDestTable;
25121 p->mode = savedMode;
25122 freeText(&sTable);
@@ -25245,11 +24983,13 @@
25245 " --bom Put a UTF8 byte-order mark on intermediate file",
25246 #endif
25247 #ifndef SQLITE_SHELL_FIDDLE
25248 ".exit ?CODE? Exit this program with return-code CODE",
25249 #endif
 
25250 ".expert EXPERIMENTAL. Suggest indexes for queries",
 
25251 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
25252 ".filectrl CMD ... Run various sqlite3_file_control() operations",
25253 " --schema SCHEMA Use SCHEMA instead of \"main\"",
25254 " --help Show CMD details",
25255 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
@@ -25270,11 +25010,11 @@
25270 " from the \".mode\" output mode",
25271 " * If FILE begins with \"|\" then it is a command that generates the",
25272 " input text.",
25273 #endif
25274 #ifndef SQLITE_OMIT_TEST_CONTROL
25275 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
25276 #endif
25277 ".indexes ?TABLE? Show names of indexes",
25278 " If TABLE is specified, only show indexes for",
25279 " tables matching TABLE using the LIKE operator.",
25280 ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
@@ -25337,11 +25077,17 @@
25337 " Options:",
25338 " --append Use appendvfs to append database to the end of FILE",
25339 #endif
25340 #ifndef SQLITE_OMIT_DESERIALIZE
25341 " --deserialize Load into memory using sqlite3_deserialize()",
 
 
 
25342 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
 
 
 
25343 " --maxsize N Maximum size for --hexdb or --deserialized database",
25344 #endif
25345 " --new Initialize FILE to an empty database",
25346 " --nofollow Do not follow symbolic links",
25347 " --readonly Open FILE readonly",
@@ -25726,14 +25472,14 @@
25726 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
25727 */
25728 static unsigned char *readHexDb(ShellState *p, int *pnData){
25729 unsigned char *a = 0;
25730 int nLine;
25731 int n = 0;
 
25732 int pgsz = 0;
25733 int iOffset = 0;
25734 int j, k;
25735 int rc;
25736 FILE *in;
25737 const char *zDbFilename = p->pAuxDb->zDbFilename;
25738 unsigned int x[16];
25739 char zLine[1000];
@@ -25753,20 +25499,21 @@
25753 nLine++;
25754 if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
25755 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
25756 if( rc!=2 ) goto readHexDb_error;
25757 if( n<0 ) goto readHexDb_error;
25758 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
25759 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
25760 a = sqlite3_malloc( n ? n : 1 );
25761 shell_check_oom(a);
25762 memset(a, 0, n);
25763 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
25764 sqlite3_fputs("invalid pagesize\n", stderr);
25765 goto readHexDb_error;
25766 }
 
 
 
 
25767 for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
 
 
25768 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
25769 if( rc==2 ){
25770 iOffset = k;
25771 continue;
25772 }
@@ -25775,18 +25522,18 @@
25775 }
25776 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
25777 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
25778 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
25779 if( rc==17 ){
25780 k = iOffset+j;
25781 if( k+16<=n && k>=0 ){
25782 int ii;
25783 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
25784 }
25785 }
25786 }
25787 *pnData = n;
25788 if( in!=p->in ){
25789 fclose(in);
25790 }else{
25791 p->lineno = nLine;
25792 }
@@ -25849,11 +25596,11 @@
25849 p->pLog = pSavedLog;
25850
25851 if( zFake ){
25852 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
25853 -1, sqlite3_free);
25854 free(zFake);
25855 }
25856 }
25857
25858 /* Flags for open_db().
25859 **
@@ -25881,14 +25628,17 @@
25881 }else{
25882 p->openMode = (u8)deduceDatabaseType(zDbFilename,
25883 (openFlags & OPEN_DB_ZIPFILE)!=0);
25884 }
25885 }
 
 
 
 
25886 switch( p->openMode ){
25887 case SHELL_OPEN_APPENDVFS: {
25888 sqlite3_open_v2(zDbFilename, &p->db,
25889 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
25890 break;
25891 }
25892 case SHELL_OPEN_HEXDB:
25893 case SHELL_OPEN_DESERIALIZE: {
25894 sqlite3_open(0, &p->db);
@@ -25896,19 +25646,13 @@
25896 }
25897 case SHELL_OPEN_ZIPFILE: {
25898 sqlite3_open(":memory:", &p->db);
25899 break;
25900 }
25901 case SHELL_OPEN_READONLY: {
25902 sqlite3_open_v2(zDbFilename, &p->db,
25903 SQLITE_OPEN_READONLY|p->openFlags, 0);
25904 break;
25905 }
25906 case SHELL_OPEN_UNSPEC:
25907 case SHELL_OPEN_NORMAL: {
25908 sqlite3_open_v2(zDbFilename, &p->db,
25909 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
25910 break;
25911 }
25912 }
25913 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25914 sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
@@ -25944,11 +25688,10 @@
25944 sqlite3_sha_init(p->db, 0, 0);
25945 sqlite3_shathree_init(p->db, 0, 0);
25946 sqlite3_uint_init(p->db, 0, 0);
25947 sqlite3_stmtrand_init(p->db, 0, 0);
25948 sqlite3_decimal_init(p->db, 0, 0);
25949 sqlite3_percentile_init(p->db, 0, 0);
25950 sqlite3_base64_init(p->db, 0, 0);
25951 sqlite3_base85_init(p->db, 0, 0);
25952 sqlite3_regexp_init(p->db, 0, 0);
25953 sqlite3_ieee_init(p->db, 0, 0);
25954 sqlite3_series_init(p->db, 0, 0);
@@ -26043,13 +25786,15 @@
26043 }
26044 }
26045 #endif
26046 }
26047 if( p->db!=0 ){
 
26048 if( p->bSafeModePersist ){
26049 sqlite3_set_authorizer(p->db, safeModeAuth, p);
26050 }
 
26051 sqlite3_db_config(
26052 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
26053 );
26054 }
26055 }
@@ -26358,12 +26103,12 @@
26358 struct ImportCtx {
26359 const char *zFile; /* Name of the input file */
26360 FILE *in; /* Read the CSV text from this input stream */
26361 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
26362 char *z; /* Accumulated text for a field */
26363 int n; /* Number of bytes in z */
26364 int nAlloc; /* Space allocated for z[] */
26365 int nLine; /* Current line number */
26366 int nRow; /* Number of rows imported */
26367 int nErr; /* Number of errors encountered */
26368 int bNotFirst; /* True if one or more bytes already read */
26369 int cTerm; /* Character that terminated the most recent field */
@@ -26829,14 +26574,17 @@
26829 #if SQLITE_SHELL_HAVE_RECOVER
26830 /*
26831 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
26832 */
26833 static unsigned int get2byteInt(unsigned char *a){
26834 return (a[0]<<8) + a[1];
26835 }
26836 static unsigned int get4byteInt(unsigned char *a){
26837 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
 
 
 
26838 }
26839
26840 /*
26841 ** Implementation of the ".dbinfo" command.
26842 **
@@ -26969,11 +26717,11 @@
26969 if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26970 nPage = sqlite3_column_int64(pStmt, 0);
26971 sqlite3_finalize(pStmt);
26972 pStmt = 0;
26973 if( nPage<1 ) goto dbtotxt_error;
26974 rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0);
26975 if( rc ) goto dbtotxt_error;
26976 if( sqlite3_step(pStmt)!=SQLITE_ROW ){
26977 zTail = "unk.db";
26978 }else{
26979 const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
@@ -26980,10 +26728,15 @@
26980 if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
26981 zTail = strrchr(zFilename, '/');
26982 #if defined(_WIN32)
26983 if( zTail==0 ) zTail = strrchr(zFilename, '\\');
26984 #endif
 
 
 
 
 
26985 }
26986 zName = strdup(zTail);
26987 shell_check_oom(zName);
26988 sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
26989 nPage*pgSz, pgSz, zName);
@@ -27148,10 +26901,47 @@
27148 if( zStr[0]!='-' ) return 0;
27149 zStr++;
27150 if( zStr[0]=='-' ) zStr++;
27151 return cli_strcmp(zStr, zOpt)==0;
27152 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27153
27154 /*
27155 ** Delete a file.
27156 */
27157 int shellDeleteFile(const char *zFilename){
@@ -28704,11 +28494,11 @@
28704 int nArg = 0;
28705 int n, c;
28706 int rc = 0;
28707 char *azArg[52];
28708
28709 #ifndef SQLITE_OMIT_VIRTUALTABLE
28710 if( p->expert.pExpert ){
28711 expertFinish(p, 1, 0);
28712 }
28713 #endif
28714
@@ -29005,11 +28795,11 @@
29005 }else{
29006 while( sqlite3_step(pStmt)==SQLITE_ROW ){
29007 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
29008 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
29009 if( zSchema==0 || zFile==0 ) continue;
29010 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
29011 shell_check_oom(azName);
29012 azName[nName*2] = strdup(zSchema);
29013 azName[nName*2+1] = strdup(zFile);
29014 nName++;
29015 }
@@ -29208,10 +28998,11 @@
29208 rc = 1;
29209 }
29210 }else
29211
29212 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
 
29213 rc = shell_dbtotxt_command(p, nArg, azArg);
29214 }else
29215
29216 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
29217 if( nArg==2 ){
@@ -29273,11 +29064,11 @@
29273 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
29274 p->autoExplain = 1;
29275 }
29276 }else
29277
29278 #ifndef SQLITE_OMIT_VIRTUALTABLE
29279 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
29280 if( p->bSafeMode ){
29281 sqlite3_fprintf(stderr,
29282 "Cannot run experimental commands such as \"%s\" in safe mode\n",
29283 azArg[0]);
@@ -29840,16 +29631,10 @@
29840 sqlite3_stmt *pStmt;
29841 int tnum = 0;
29842 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
29843 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
29844 int i;
29845 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
29846 sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
29847 "imposter");
29848 rc = 1;
29849 goto meta_command_exit;
29850 }
29851 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
29852 eputz("Usage: .imposter INDEX IMPOSTER\n"
29853 " .imposter off\n");
29854 /* Also allowed, but not documented:
29855 **
@@ -29917,22 +29702,19 @@
29917 if( lenPK==0 ) lenPK = 100000;
29918 zSql = sqlite3_mprintf(
29919 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
29920 azArg[2], zCollist, lenPK, zCollist);
29921 sqlite3_free(zCollist);
29922 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
29923 if( rc==SQLITE_OK ){
29924 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
29925 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
29926 if( rc ){
29927 sqlite3_fprintf(stderr,
29928 "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
29929 }else{
29930 sqlite3_fprintf(stdout, "%s;\n", zSql);
29931 sqlite3_fprintf(stdout,
29932 "WARNING: writing to an imposter table will corrupt"
29933 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
29934 }
29935 }else{
29936 sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
29937 rc = 1;
29938 }
@@ -30282,10 +30064,11 @@
30282 const char *zFN = 0; /* Pointer to constant filename */
30283 char *zNewFilename = 0; /* Name of the database file to open */
30284 int iName = 1; /* Index in azArg[] of the filename */
30285 int newFlag = 0; /* True to delete file before opening */
30286 int openMode = SHELL_OPEN_UNSPEC;
 
30287
30288 /* Check for command-line arguments */
30289 for(iName=1; iName<nArg; iName++){
30290 const char *z = azArg[iName];
30291 #ifndef SQLITE_SHELL_FIDDLE
@@ -30296,13 +30079,18 @@
30296 openMode = SHELL_OPEN_ZIPFILE;
30297 #endif
30298 }else if( optionMatch(z, "append") ){
30299 openMode = SHELL_OPEN_APPENDVFS;
30300 }else if( optionMatch(z, "readonly") ){
30301 openMode = SHELL_OPEN_READONLY;
 
 
 
 
 
30302 }else if( optionMatch(z, "nofollow") ){
30303 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
30304 #ifndef SQLITE_OMIT_DESERIALIZE
30305 }else if( optionMatch(z, "deserialize") ){
30306 openMode = SHELL_OPEN_DESERIALIZE;
30307 }else if( optionMatch(z, "hexdb") ){
30308 openMode = SHELL_OPEN_HEXDB;
@@ -30330,16 +30118,25 @@
30330 p->db = 0;
30331 p->pAuxDb->zDbFilename = 0;
30332 sqlite3_free(p->pAuxDb->zFreeOnClose);
30333 p->pAuxDb->zFreeOnClose = 0;
30334 p->openMode = openMode;
30335 p->openFlags = 0;
30336 p->szMax = 0;
30337
30338 /* If a filename is specified, try to open it first */
30339 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
30340 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
 
 
 
 
 
 
 
 
 
30341 #ifndef SQLITE_SHELL_FIDDLE
30342 if( p->bSafeMode
30343 && p->openMode!=SHELL_OPEN_HEXDB
30344 && zFN
30345 && cli_strcmp(zFN,":memory:")!=0
@@ -30938,13 +30735,13 @@
30938 appendText(&sSelect, "name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ", 0);
30939 }
30940 appendText(&sSelect, "sql IS NOT NULL"
30941 " ORDER BY snum, rowid", 0);
30942 if( bDebug ){
30943 sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
30944 }else{
30945 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
30946 }
30947 freeText(&sSelect);
30948 }
30949 if( zErrMsg ){
30950 shellEmitError(zErrMsg);
@@ -31072,19 +30869,20 @@
31072
31073 /* .session filter GLOB ....
31074 ** Set a list of GLOB patterns of table names to be excluded.
31075 */
31076 if( cli_strcmp(azCmd[0], "filter")==0 ){
31077 int ii, nByte;
 
31078 if( nCmd<2 ) goto session_syntax_error;
31079 if( pAuxDb->nSession ){
31080 for(ii=0; ii<pSession->nFilter; ii++){
31081 sqlite3_free(pSession->azFilter[ii]);
31082 }
31083 sqlite3_free(pSession->azFilter);
31084 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
31085 pSession->azFilter = sqlite3_malloc( nByte );
31086 shell_check_oom( pSession->azFilter );
31087 for(ii=1; ii<nCmd; ii++){
31088 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
31089 shell_check_oom(x);
31090 }
@@ -31264,26 +31062,26 @@
31264 sqlite3_fprintf(p->out, "%s\n", zSql);
31265 }else
31266 if( cli_strcmp(zOp,"run")==0 ){
31267 char *zErrMsg = 0;
31268 str.n = 0;
31269 str.z[0] = 0;
31270 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
31271 nTest++;
31272 if( bVerbose ){
31273 sqlite3_fprintf(p->out, "Result: %s\n", str.z);
31274 }
31275 if( rc || zErrMsg ){
31276 nErr++;
31277 rc = 1;
31278 sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
31279 sqlite3_free(zErrMsg);
31280 }else if( cli_strcmp(zAns,str.z)!=0 ){
31281 nErr++;
31282 rc = 1;
31283 sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
31284 sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
31285 }
31286 }
31287 else{
31288 sqlite3_fprintf(stderr,
31289 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
@@ -31395,11 +31193,11 @@
31395 appendText(&sQuery, "SELECT * FROM ", 0);
31396 appendText(&sQuery, zTab, 0);
31397 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
31398 }
31399 appendText(&sSql, zSep, 0);
31400 appendText(&sSql, sQuery.z, '\'');
31401 sQuery.n = 0;
31402 appendText(&sSql, ",", 0);
31403 appendText(&sSql, zTab, '\'');
31404 zSep = "),(";
31405 }
@@ -31407,17 +31205,17 @@
31407 if( bSeparate ){
31408 zSql = sqlite3_mprintf(
31409 "%s))"
31410 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
31411 " FROM [sha3sum$query]",
31412 sSql.z, iSize);
31413 }else{
31414 zSql = sqlite3_mprintf(
31415 "%s))"
31416 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
31417 " FROM [sha3sum$query]",
31418 sSql.z, iSize);
31419 }
31420 shell_check_oom(zSql);
31421 freeText(&sQuery);
31422 freeText(&sSql);
31423 if( bDebug ){
@@ -31613,11 +31411,11 @@
31613 goto meta_command_exit;
31614 }
31615 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
31616 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
31617 if( zDbName==0 ) continue;
31618 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
31619 if( sqlite3_stricmp(zDbName, "main")==0 ){
31620 appendText(&s, "SELECT name FROM ", 0);
31621 }else{
31622 appendText(&s, "SELECT ", 0);
31623 appendText(&s, zDbName, '\'');
@@ -31635,11 +31433,11 @@
31635 }
31636 }
31637 rc = sqlite3_finalize(pStmt);
31638 if( rc==SQLITE_OK ){
31639 appendText(&s, " ORDER BY 1", 0);
31640 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
31641 }
31642 freeText(&s);
31643 if( rc ) return shellDatabaseError(p->db);
31644
31645 /* Run the SQL statement prepared by the above block. Store the results
@@ -31652,14 +31450,14 @@
31652 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
31653 }
31654 while( sqlite3_step(pStmt)==SQLITE_ROW ){
31655 if( nRow>=nAlloc ){
31656 char **azNew;
31657 int n2 = nAlloc*2 + 10;
31658 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
31659 shell_check_oom(azNew);
31660 nAlloc = n2;
31661 azResult = azNew;
31662 }
31663 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
31664 shell_check_oom(azResult[nRow]);
31665 nRow++;
@@ -31818,11 +31616,11 @@
31818 { 0x00000020, 1, "CoverIdxScan" },
31819 { 0x00000040, 1, "OrderByIdxJoin" },
31820 { 0x00000080, 1, "Transitive" },
31821 { 0x00000100, 1, "OmitNoopJoin" },
31822 { 0x00000200, 1, "CountOfView" },
31823 { 0x00000400, 1, "CurosrHints" },
31824 { 0x00000800, 1, "Stat4" },
31825 { 0x00001000, 1, "PushDown" },
31826 { 0x00002000, 1, "SimplifyJoin" },
31827 { 0x00004000, 1, "SkipScan" },
31828 { 0x00008000, 1, "PropagateConst" },
@@ -32365,11 +32163,14 @@
32365 p->nWidth = nArg-1;
32366 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
32367 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
32368 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
32369 for(j=1; j<nArg; j++){
32370 p->colWidth[j-1] = (int)integerValue(azArg[j]);
 
 
 
32371 }
32372 }else
32373
32374 {
32375 sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
@@ -32888,76 +32689,95 @@
32888
32889 return home_dir;
32890 }
32891
32892 /*
32893 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
32894 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
32895 ** the path to it. If there is no $(XDG_CONFIG_HOME) then
32896 ** look for $(HOME)/.config/sqlite3/sqliterc and if found
32897 ** return that. If none of these are found, return 0.
 
 
 
 
 
 
 
 
 
 
 
 
 
32898 **
32899 ** The string returned is obtained from sqlite3_malloc() and
32900 ** should be freed by the caller.
32901 */
32902 static char *find_xdg_config(void){
 
32903 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
32904 || defined(__RTP__) || defined(_WRS_KERNEL)
32905 return 0;
32906 #else
32907 char *zConfig = 0;
32908 const char *zXdgHome;
32909
32910 zXdgHome = getenv("XDG_CONFIG_HOME");
32911 if( zXdgHome==0 ){
32912 const char *zHome = getenv("HOME");
32913 if( zHome==0 ) return 0;
32914 zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome);
32915 }else{
32916 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
32917 }
32918 shell_check_oom(zConfig);
32919 if( access(zConfig,0)!=0 ){
32920 sqlite3_free(zConfig);
32921 zConfig = 0;
32922 }
32923 return zConfig;
 
 
 
 
32924 #endif
32925 }
32926
32927 /*
32928 ** Read input from the file given by sqliterc_override. Or if that
32929 ** parameter is NULL, take input from the first of find_xdg_config()
32930 ** or ~/.sqliterc which is found.
32931 **
32932 ** Returns the number of errors.
 
 
 
32933 */
32934 static void process_sqliterc(
32935 ShellState *p, /* Configuration data */
32936 const char *sqliterc_override /* Name of config file. NULL to use default */
32937 ){
32938 char *home_dir = NULL;
32939 const char *sqliterc = sqliterc_override;
32940 char *zBuf = 0;
32941 FILE *inSaved = p->in;
32942 int savedLineno = p->lineno;
32943
32944 if( sqliterc == NULL ){
32945 sqliterc = zBuf = find_xdg_config();
 
 
32946 }
32947 if( sqliterc == NULL ){
32948 home_dir = find_home_dir(0);
32949 if( home_dir==0 ){
32950 eputz("-- warning: cannot find home directory;"
32951 " cannot read ~/.sqliterc\n");
32952 return;
32953 }
32954 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
32955 shell_check_oom(zBuf);
32956 sqliterc = zBuf;
32957 }
32958 p->in = sqlite3_fopen(sqliterc,"rb");
32959 if( p->in ){
32960 if( stdin_is_interactive ){
32961 sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
32962 }
32963 if( process_input(p) && bail_on_error ) exit(1);
@@ -32966,11 +32786,13 @@
32966 sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
32967 if( bail_on_error ) exit(1);
32968 }
32969 p->in = inSaved;
32970 p->lineno = savedLineno;
32971 sqlite3_free(zBuf);
 
 
32972 }
32973
32974 /*
32975 ** Show available command line options
32976 */
@@ -32997,10 +32819,11 @@
32997 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
32998 " -heap SIZE Size of heap for memsys3 or memsys5\n"
32999 #endif
33000 " -help show this message\n"
33001 " -html set output mode to HTML\n"
 
33002 " -interactive force interactive I/O\n"
33003 " -json set output mode to 'json'\n"
33004 " -line set output mode to 'line'\n"
33005 " -list set output mode to 'list'\n"
33006 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
@@ -33341,16 +33164,24 @@
33341 (void)cmdline_option_value(argc, argv, ++i);
33342 #endif
33343 }else if( cli_strcmp(z,"-pagecache")==0 ){
33344 sqlite3_int64 n, sz;
33345 sz = integerValue(cmdline_option_value(argc,argv,++i));
33346 if( sz>70000 ) sz = 70000;
33347 if( sz<0 ) sz = 0;
33348 n = integerValue(cmdline_option_value(argc,argv,++i));
33349 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
33350 n = 0xffffffffffffLL/sz;
33351 }
 
 
 
 
 
 
 
 
33352 verify_uninitialized();
33353 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
33354 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
33355 data.shellFlgs |= SHFLG_Pagecache;
33356 }else if( cli_strcmp(z,"-lookaside")==0 ){
@@ -33359,11 +33190,11 @@
33359 if( sz<0 ) sz = 0;
33360 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
33361 if( n<0 ) n = 0;
33362 verify_uninitialized();
33363 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
33364 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
33365 }else if( cli_strcmp(z,"-threadsafe")==0 ){
33366 int n;
33367 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
33368 verify_uninitialized();
33369 switch( n ){
@@ -33401,13 +33232,19 @@
33401 data.openMode = SHELL_OPEN_DESERIALIZE;
33402 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
33403 data.szMax = integerValue(argv[++i]);
33404 #endif
33405 }else if( cli_strcmp(z,"-readonly")==0 ){
33406 data.openMode = SHELL_OPEN_READONLY;
 
33407 }else if( cli_strcmp(z,"-nofollow")==0 ){
33408 data.openFlags = SQLITE_OPEN_NOFOLLOW;
 
 
 
 
 
33409 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
33410 }else if( cli_strncmp(z, "-A",2)==0 ){
33411 /* All remaining command-line arguments are passed to the ".archive"
33412 ** command, so ignore them */
33413 break;
@@ -33557,13 +33394,19 @@
33557 data.openMode = SHELL_OPEN_DESERIALIZE;
33558 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
33559 data.szMax = integerValue(argv[++i]);
33560 #endif
33561 }else if( cli_strcmp(z,"-readonly")==0 ){
33562 data.openMode = SHELL_OPEN_READONLY;
 
33563 }else if( cli_strcmp(z,"-nofollow")==0 ){
33564 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
 
 
 
 
 
33565 }else if( cli_strcmp(z,"-ascii")==0 ){
33566 data.mode = MODE_Ascii;
33567 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
33568 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
33569 }else if( cli_strcmp(z,"-tabs")==0 ){
@@ -33734,11 +33577,10 @@
33734 /* Run commands received from standard input
33735 */
33736 if( stdin_is_interactive ){
33737 char *zHome;
33738 char *zHistory;
33739 int nHistory;
33740 sqlite3_fprintf(stdout,
33741 "SQLite version %s %.19s\n" /*extra-version-info*/
33742 "Enter \".help\" for usage hints.\n",
33743 sqlite3_libversion(), sqlite3_sourceid());
33744 if( warnInmemoryDb ){
@@ -33747,15 +33589,19 @@
33747 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
33748 " persistent database.\n");
33749 }
33750 zHistory = getenv("SQLITE_HISTORY");
33751 if( zHistory ){
33752 zHistory = strdup(zHistory);
33753 }else if( (zHome = find_home_dir(0))!=0 ){
33754 nHistory = strlen30(zHome) + 20;
33755 if( (zHistory = malloc(nHistory))!=0 ){
33756 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
 
 
 
 
33757 }
33758 }
33759 if( zHistory ){ shell_read_history(zHistory); }
33760 #if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
33761 rl_attempted_completion_function = readline_completion;
@@ -33767,21 +33613,21 @@
33767 data.in = 0;
33768 rc = process_input(&data);
33769 if( zHistory ){
33770 shell_stifle_history(2000);
33771 shell_write_history(zHistory);
33772 free(zHistory);
33773 }
33774 }else{
33775 data.in = stdin;
33776 rc = process_input(&data);
33777 }
33778 }
33779 #ifndef SQLITE_SHELL_FIDDLE
33780 /* In WASM mode we have to leave the db state in place so that
33781 ** client code can "push" SQL into it after this call returns. */
33782 #ifndef SQLITE_OMIT_VIRTUALTABLE
33783 if( data.expert.pExpert ){
33784 expertFinish(&data, 1, 0);
33785 }
33786 #endif
33787 shell_main_exit:
33788
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -122,10 +122,13 @@
122 typedef sqlite3_int64 i64;
123 typedef sqlite3_uint64 u64;
124 typedef unsigned char u8;
125 #include <ctype.h>
126 #include <stdarg.h>
127 #ifndef _WIN32
128 # include <sys/time.h>
129 #endif
130
131 #if !defined(_WIN32) && !defined(WIN32)
132 # include <signal.h>
133 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
134 # include <pwd.h>
@@ -646,12 +649,23 @@
649 if( a==0 ) a = "";
650 if( b==0 ) b = "";
651 return strncmp(a,b,n);
652 }
653
654 /* Return the current wall-clock time in microseconds since the
655 ** Unix epoch (1970-01-01T00:00:00Z)
656 */
657 static sqlite3_int64 timeOfDay(void){
658 #if defined(_WIN64)
659 sqlite3_uint64 t;
660 FILETIME tm;
661 GetSystemTimePreciseAsFileTime(&tm);
662 t = ((u64)tm.dwHighDateTime<<32) | (u64)tm.dwLowDateTime;
663 t += 116444736000000000LL;
664 t /= 10;
665 return t;
666 #elif defined(_WIN32)
667 static sqlite3_vfs *clockVfs = 0;
668 sqlite3_int64 t;
669 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
670 if( clockVfs==0 ) return 0; /* Never actually happens */
671 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
@@ -659,11 +673,16 @@
673 }else{
674 double r;
675 clockVfs->xCurrentTime(clockVfs, &r);
676 t = (sqlite3_int64)(r*86400000.0);
677 }
678 return t*1000;
679 #else
680 struct timeval sNow;
681 (void)gettimeofday(&sNow,0);
682 return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
683 #endif
684 }
685
686 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
687 #include <sys/time.h>
688 #include <sys/resource.h>
@@ -704,12 +723,12 @@
723 static void endTimer(FILE *out){
724 if( enableTimer ){
725 sqlite3_int64 iEnd = timeOfDay();
726 struct rusage sEnd;
727 getrusage(RUSAGE_SELF, &sEnd);
728 sqlite3_fprintf(out, "Run Time: real %.6f user %.6f sys %.6f\n",
729 (iEnd - iBegin)*0.000001,
730 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
731 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
732 }
733 }
734
@@ -783,14 +802,23 @@
802 static void endTimer(FILE *out){
803 if( enableTimer && getProcessTimesAddr){
804 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
805 sqlite3_int64 ftWallEnd = timeOfDay();
806 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
807 #ifdef _WIN64
808 /* microsecond precision on 64-bit windows */
809 sqlite3_fprintf(out, "Run Time: real %.6f user %f sys %f\n",
810 (ftWallEnd - ftWallBegin)*0.000001,
811 timeDiff(&ftUserBegin, &ftUserEnd),
812 timeDiff(&ftKernelBegin, &ftKernelEnd));
813 #else
814 /* millisecond precisino on 32-bit windows */
815 sqlite3_fprintf(out, "Run Time: real %.3f user %.3f sys %.3f\n",
816 (ftWallEnd - ftWallBegin)*0.000001,
817 timeDiff(&ftUserBegin, &ftUserEnd),
818 timeDiff(&ftKernelBegin, &ftKernelEnd));
819 #endif
820 }
821 }
822
823 #define BEGIN_TIMER beginTimer()
824 #define END_TIMER(X) endTimer(X)
@@ -1126,11 +1154,11 @@
1154 }
1155 if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
1156 && (z[3] & 0xc0)==0x80
1157 ){
1158 *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
1159 | (z[3] & 0x3f);
1160 return 4;
1161 }
1162 *pU = 0;
1163 return 1;
1164 }
@@ -1189,18 +1217,28 @@
1217 ** since with %*.*s the width is measured in bytes, not characters.
1218 **
1219 ** Take into account zero-width and double-width Unicode characters.
1220 ** In other words, a zero-width character does not count toward the
1221 ** the w limit. A double-width character counts as two.
1222 **
1223 ** w should normally be a small number. A couple hundred at most. This
1224 ** routine caps w at 100 million to avoid integer overflow issues.
1225 */
1226 static void utf8_width_print(FILE *out, int w, const char *zUtf){
1227 const unsigned char *a = (const unsigned char*)zUtf;
1228 static const int mxW = 10000000;
1229 unsigned char c;
1230 int i = 0;
1231 int n = 0;
1232 int k;
1233 int aw;
1234 if( w<-mxW ){
1235 w = -mxW;
1236 }else if( w>mxW ){
1237 w= mxW;
1238 }
1239 aw = w<0 ? -w : w;
1240 if( zUtf==0 ) zUtf = "";
1241 while( (c = a[i])!=0 ){
1242 if( (c&0xc0)==0xc0 ){
1243 int u;
1244 int len = decodeUtf8(a+i, &u);
@@ -1323,11 +1361,11 @@
1361
1362 /*
1363 ** This routine reads a line of text from FILE in, stores
1364 ** the text in memory obtained from malloc() and returns a pointer
1365 ** to the text. NULL is returned at end of file, or if malloc()
1366 ** fails, or if the length of the line is longer than about a gigabyte.
1367 **
1368 ** If zLine is not NULL then it is a malloced buffer returned from
1369 ** a previous call to this routine that may be reused.
1370 */
1371 static char *local_getline(char *zLine, FILE *in){
@@ -1334,10 +1372,14 @@
1372 int nLine = zLine==0 ? 0 : 100;
1373 int n = 0;
1374
1375 while( 1 ){
1376 if( n+100>nLine ){
1377 if( nLine>=1073741773 ){
1378 free(zLine);
1379 return 0;
1380 }
1381 nLine = nLine*2 + 100;
1382 zLine = realloc(zLine, nLine);
1383 shell_check_oom(zLine);
1384 }
1385 if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
@@ -1417,14 +1459,18 @@
1459 return -1;
1460 }
1461
1462 /*
1463 ** Interpret zArg as an integer value, possibly with suffixes.
1464 **
1465 ** If the value specified by zArg is outside the range of values that
1466 ** can be represented using a 64-bit twos-complement integer, then return
1467 ** the nearest representable value.
1468 */
1469 static sqlite3_int64 integerValue(const char *zArg){
1470 sqlite3_uint64 v = 0;
1471 static const struct { char *zSuffix; unsigned int iMult; } aMult[] = {
1472 { "KiB", 1024 },
1473 { "MiB", 1024*1024 },
1474 { "GiB", 1024*1024*1024 },
1475 { "KB", 1000 },
1476 { "MB", 1000000 },
@@ -1443,46 +1489,54 @@
1489 }
1490 if( zArg[0]=='0' && zArg[1]=='x' ){
1491 int x;
1492 zArg += 2;
1493 while( (x = hexDigitValue(zArg[0]))>=0 ){
1494 if( v > 0x0fffffffffffffffULL ) goto integer_overflow;
1495 v = (v<<4) + x;
1496 zArg++;
1497 }
1498 }else{
1499 while( IsDigit(zArg[0]) ){
1500 if( v>=922337203685477580LL ){
1501 if( v>922337203685477580LL || zArg[0]>='8' ) goto integer_overflow;
1502 }
1503 v = v*10 + (zArg[0] - '0');
1504 zArg++;
1505 }
1506 }
1507 for(i=0; i<ArraySize(aMult); i++){
1508 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1509 if( 0x7fffffffffffffffULL/aMult[i].iMult < v ) goto integer_overflow;
1510 v *= aMult[i].iMult;
1511 break;
1512 }
1513 }
1514 if( isNeg && v>0x7fffffffffffffffULL ) goto integer_overflow;
1515 return isNeg? -(sqlite3_int64)v : (sqlite3_int64)v;
1516 integer_overflow:
1517 return isNeg ? (i64)0x8000000000000000LL : 0x7fffffffffffffffLL;
1518 }
1519
1520 /*
1521 ** A variable length string to which one can append text.
1522 */
1523 typedef struct ShellText ShellText;
1524 struct ShellText {
1525 char *zTxt; /* The text */
1526 i64 n; /* Number of bytes of zTxt[] actually used */
1527 i64 nAlloc; /* Number of bytes allocated for zTxt[] */
1528 };
1529
1530 /*
1531 ** Initialize and destroy a ShellText object
1532 */
1533 static void initText(ShellText *p){
1534 memset(p, 0, sizeof(*p));
1535 }
1536 static void freeText(ShellText *p){
1537 sqlite3_free(p->zTxt);
1538 initText(p);
1539 }
1540
1541 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1542 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
@@ -1503,30 +1557,30 @@
1557 for(i=0; i<nAppend; i++){
1558 if( zAppend[i]==quote ) len++;
1559 }
1560 }
1561
1562 if( p->zTxt==0 || p->n+len>=p->nAlloc ){
1563 p->nAlloc = p->nAlloc*2 + len + 20;
1564 p->zTxt = sqlite3_realloc64(p->zTxt, p->nAlloc);
1565 shell_check_oom(p->zTxt);
1566 }
1567
1568 if( quote ){
1569 char *zCsr = p->zTxt+p->n;
1570 *zCsr++ = quote;
1571 for(i=0; i<nAppend; i++){
1572 *zCsr++ = zAppend[i];
1573 if( zAppend[i]==quote ) *zCsr++ = quote;
1574 }
1575 *zCsr++ = quote;
1576 p->n = (i64)(zCsr - p->zTxt);
1577 *zCsr = '\0';
1578 }else{
1579 memcpy(p->zTxt+p->n, zAppend, nAppend);
1580 p->n += nAppend;
1581 p->zTxt[p->n] = '\0';
1582 }
1583 }
1584
1585 /*
1586 ** Attempt to determine if identifier zName needs to be quoted, either
@@ -1547,10 +1601,13 @@
1601 }
1602
1603 /*
1604 ** Construct a fake object name and column list to describe the structure
1605 ** of the view, virtual table, or table valued function zSchema.zName.
1606 **
1607 ** The returned string comes from sqlite3_mprintf() and should be freed
1608 ** by the caller using sqlite3_free().
1609 */
1610 static char *shellFakeSchema(
1611 sqlite3 *db, /* The database connection containing the vtab */
1612 const char *zSchema, /* Schema of the database holding the vtab */
1613 const char *zName /* The name of the virtual table */
@@ -1587,13 +1644,13 @@
1644 }
1645 appendText(&s, ")", 0);
1646 sqlite3_finalize(pStmt);
1647 if( nRow==0 ){
1648 freeText(&s);
1649 s.zTxt = 0;
1650 }
1651 return s.zTxt;
1652 }
1653
1654 /*
1655 ** SQL function: strtod(X)
1656 **
@@ -1692,11 +1749,11 @@
1749 if( z==0 ){
1750 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1751 }else{
1752 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1753 }
1754 sqlite3_free(zFake);
1755 }
1756 if( z ){
1757 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1758 return;
1759 }
@@ -3667,11 +3724,12 @@
3724 iExp -= p->nFrac;
3725 p->nFrac = 0;
3726 }
3727 }
3728 if( iExp>0 ){
3729 p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
3730 + (sqlite3_int64)iExp + 1 );
3731 if( p->a==0 ) goto new_from_text_failed;
3732 memset(p->a+p->nDigit, 0, iExp);
3733 p->nDigit += iExp;
3734 }
3735 }else if( iExp<0 ){
@@ -3686,18 +3744,23 @@
3744 iExp -= nExtra;
3745 p->nFrac = p->nDigit - 1;
3746 }
3747 }
3748 if( iExp>0 ){
3749 p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit
3750 + (sqlite3_int64)iExp + 1 );
3751 if( p->a==0 ) goto new_from_text_failed;
3752 memmove(p->a+iExp, p->a, p->nDigit);
3753 memset(p->a, 0, iExp);
3754 p->nDigit += iExp;
3755 p->nFrac += iExp;
3756 }
3757 }
3758 if( p->sign ){
3759 for(i=0; i<p->nDigit && p->a[i]==0; i++){}
3760 if( i>=p->nDigit ) p->sign = 0;
3761 }
3762 return p;
3763
3764 new_from_text_failed:
3765 if( p ){
3766 if( p->a ) sqlite3_free(p->a);
@@ -3786,11 +3849,11 @@
3849 }
3850 if( p->isNull ){
3851 sqlite3_result_null(pCtx);
3852 return;
3853 }
3854 z = sqlite3_malloc64( (sqlite3_int64)p->nDigit+4 );
3855 if( z==0 ){
3856 sqlite3_result_error_nomem(pCtx);
3857 return;
3858 }
3859 i = 0;
@@ -3851,11 +3914,11 @@
3914 }
3915 for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3916 for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3917 nFrac = p->nFrac + (nDigit - p->nDigit);
3918 nDigit -= nZero;
3919 z = sqlite3_malloc64( (sqlite3_int64)nDigit+20 );
3920 if( z==0 ){
3921 sqlite3_result_error_nomem(pCtx);
3922 return;
3923 }
3924 if( nDigit==0 ){
@@ -3896,17 +3959,25 @@
3959 ** pA!=0
3960 ** pA->isNull==0
3961 ** pB!=0
3962 ** pB->isNull==0
3963 */
3964 static int decimal_cmp(Decimal *pA, Decimal *pB){
3965 int nASig, nBSig, rc, n;
3966 while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){
3967 pA->nDigit--;
3968 pA->nFrac--;
3969 }
3970 while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){
3971 pB->nDigit--;
3972 pB->nFrac--;
3973 }
3974 if( pA->sign!=pB->sign ){
3975 return pA->sign ? -1 : +1;
3976 }
3977 if( pA->sign ){
3978 Decimal *pTemp = pA;
3979 pA = pB;
3980 pB = pTemp;
3981 }
3982 nASig = pA->nDigit - pA->nFrac;
3983 nBSig = pB->nDigit - pB->nFrac;
@@ -4064,11 +4135,12 @@
4135 if( pA==0 || pA->oom || pA->isNull
4136 || pB==0 || pB->oom || pB->isNull
4137 ){
4138 goto mul_end;
4139 }
4140 acc = sqlite3_malloc64( (sqlite3_int64)pA->nDigit +
4141 (sqlite3_int64)pB->nDigit + 2 );
4142 if( acc==0 ){
4143 pA->oom = 1;
4144 goto mul_end;
4145 }
4146 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
@@ -4151,11 +4223,11 @@
4223 r = -r;
4224 }else{
4225 isNeg = 0;
4226 }
4227 memcpy(&a,&r,sizeof(a));
4228 if( a==0 || a==(sqlite3_int64)0x8000000000000000LL){
4229 e = 0;
4230 m = 0;
4231 }else{
4232 e = a>>52;
4233 m = a & ((((sqlite3_int64)1)<<52)-1);
@@ -4426,516 +4498,10 @@
4498 }
4499 return rc;
4500 }
4501
4502 /************************* End ../ext/misc/decimal.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4503 #undef sqlite3_base_init
4504 #define sqlite3_base_init sqlite3_base64_init
4505 /************************* Begin ../ext/misc/base64.c ******************/
4506 /*
4507 ** 2022-11-18
@@ -5144,20 +4710,22 @@
4710 return pOut;
4711 }
4712
4713 /* This function does the work for the SQLite base64(x) UDF. */
4714 static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){
4715 sqlite3_int64 nb;
4716 sqlite3_int64 nv = sqlite3_value_bytes(av[0]);
4717 sqlite3_int64 nc;
4718 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
4719 SQLITE_LIMIT_LENGTH, -1);
4720 char *cBuf;
4721 u8 *bBuf;
4722 assert(na==1);
4723 switch( sqlite3_value_type(av[0]) ){
4724 case SQLITE_BLOB:
4725 nb = nv;
4726 nc = 4*((nv+2)/3); /* quads needed */
4727 nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */
4728 if( nvMax < nc ){
4729 sqlite3_result_error(context, "blob expanded to base64 too big", -1);
4730 return;
4731 }
@@ -5524,11 +5092,11 @@
5092 }
5093 # endif
5094
5095 /* This function does the work for the SQLite base85(x) UDF. */
5096 static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){
5097 sqlite3_int64 nb, nc, nv = sqlite3_value_bytes(av[0]);
5098 int nvMax = sqlite3_limit(sqlite3_context_db_handle(context),
5099 SQLITE_LIMIT_LENGTH, -1);
5100 char *cBuf;
5101 u8 *bBuf;
5102 assert(na==1);
@@ -5829,10 +5397,13 @@
5397 }
5398 memcpy(&a,&r,sizeof(a));
5399 if( a==0 ){
5400 e = 0;
5401 m = 0;
5402 }else if( a==(sqlite3_int64)0x8000000000000000LL ){
5403 e = -1996;
5404 m = -1;
5405 }else{
5406 e = a>>52;
5407 m = a & ((((sqlite3_int64)1)<<52)-1);
5408 if( e==0 ){
5409 m <<= 1;
@@ -6052,23 +5623,24 @@
5623 ** Examples:
5624 **
5625 ** SELECT * FROM generate_series(0,100,5);
5626 **
5627 ** The query above returns integers from 0 through 100 counting by steps
5628 ** of 5. In other words, 0, 5, 10, 15, ..., 90, 95, 100. There are a total
5629 ** of 21 rows.
5630 **
5631 ** SELECT * FROM generate_series(0,100);
5632 **
5633 ** Integers from 0 through 100 with a step size of 1. 101 rows.
5634 **
5635 ** SELECT * FROM generate_series(20) LIMIT 10;
5636 **
5637 ** Integers 20 through 29. 10 rows.
5638 **
5639 ** SELECT * FROM generate_series(0,-100,-5);
5640 **
5641 ** Integers 0 -5 -10 ... -100. 21 rows.
5642 **
5643 ** SELECT * FROM generate_series(0,-1);
5644 **
5645 ** Empty sequence.
5646 **
@@ -6140,143 +5712,92 @@
5712 #include <string.h>
5713 #include <limits.h>
5714 #include <math.h>
5715
5716 #ifndef SQLITE_OMIT_VIRTUALTABLE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5717
5718 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5719 ** serve as the underlying representation of a cursor that scans
5720 ** over rows of the result.
5721 **
5722 ** iOBase, iOTerm, and iOStep are the original values of the
5723 ** start=, stop=, and step= constraints on the query. These are
5724 ** the values reported by the start, stop, and step columns of the
5725 ** virtual table.
5726 **
5727 ** iBase, iTerm, iStep, and bDescp are the actual values used to generate
5728 ** the sequence. These might be different from the iOxxxx values.
5729 ** For example in
5730 **
5731 ** SELECT value FROM generate_series(1,11,2)
5732 ** WHERE value BETWEEN 4 AND 8;
5733 **
5734 ** The iOBase is 1, but the iBase is 5. iOTerm is 11 but iTerm is 7.
5735 ** Another example:
5736 **
5737 ** SELECT value FROM generate_series(1,15,3) ORDER BY value DESC;
5738 **
5739 ** The cursor initialization for the above query is:
5740 **
5741 ** iOBase = 1 iBase = 13
5742 ** iOTerm = 15 iTerm = 1
5743 ** iOStep = 3 iStep = 3 bDesc = 1
5744 **
5745 ** The actual step size is unsigned so that can have a value of
5746 ** +9223372036854775808 which is needed for querys like this:
5747 **
5748 ** SELECT value
5749 ** FROM generate_series(9223372036854775807,
5750 ** -9223372036854775808,
5751 ** -9223372036854775808)
5752 ** ORDER BY value ASC;
5753 **
5754 ** The setup for the previous query will be:
5755 **
5756 ** iOBase = 9223372036854775807 iBase = -1
5757 ** iOTerm = -9223372036854775808 iTerm = 9223372036854775807
5758 ** iOStep = -9223372036854775808 iStep = 9223372036854775808 bDesc = 0
5759 */
5760 /* typedef unsigned char u8; */
5761 typedef struct series_cursor series_cursor;
5762 struct series_cursor {
5763 sqlite3_vtab_cursor base; /* Base class - must be first */
5764 sqlite3_int64 iOBase; /* Original starting value ("start") */
5765 sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
5766 sqlite3_int64 iOStep; /* Original step value */
5767 sqlite3_int64 iBase; /* Starting value to actually use */
5768 sqlite3_int64 iTerm; /* Terminal value to actually use */
5769 sqlite3_uint64 iStep; /* The step size */
5770 sqlite3_int64 iValue; /* Current value */
5771 u8 bDesc; /* iStep is really negative */
5772 u8 bDone; /* True if stepped past last element */
5773 };
5774
5775 /*
5776 ** Computed the difference between two 64-bit signed integers using a
5777 ** convoluted computation designed to work around the silly restriction
5778 ** against signed integer overflow in C.
5779 */
5780 static sqlite3_uint64 span64(sqlite3_int64 a, sqlite3_int64 b){
5781 assert( a>=b );
5782 return (*(sqlite3_uint64*)&a) - (*(sqlite3_uint64*)&b);
5783 }
5784
5785 /*
5786 ** Add or substract an unsigned 64-bit integer from a signed 64-bit integer
5787 ** and return the new signed 64-bit integer.
5788 */
5789 static sqlite3_int64 add64(sqlite3_int64 a, sqlite3_uint64 b){
5790 sqlite3_uint64 x = *(sqlite3_uint64*)&a;
5791 x += b;
5792 return *(sqlite3_int64*)&x;
5793 }
5794 static sqlite3_int64 sub64(sqlite3_int64 a, sqlite3_uint64 b){
5795 sqlite3_uint64 x = *(sqlite3_uint64*)&a;
5796 x -= b;
5797 return *(sqlite3_int64*)&x;
5798 }
5799
5800 /*
5801 ** The seriesConnect() method is invoked to create a new
5802 ** series_vtab that describes the generate_series virtual table.
5803 **
@@ -6354,11 +5875,19 @@
5875 /*
5876 ** Advance a series_cursor to its next row of output.
5877 */
5878 static int seriesNext(sqlite3_vtab_cursor *cur){
5879 series_cursor *pCur = (series_cursor*)cur;
5880 if( pCur->iValue==pCur->iTerm ){
5881 pCur->bDone = 1;
5882 }else if( pCur->bDesc ){
5883 pCur->iValue = sub64(pCur->iValue, pCur->iStep);
5884 assert( pCur->iValue>=pCur->iTerm );
5885 }else{
5886 pCur->iValue = add64(pCur->iValue, pCur->iStep);
5887 assert( pCur->iValue<=pCur->iTerm );
5888 }
5889 return SQLITE_OK;
5890 }
5891
5892 /*
5893 ** Return values of columns for the row at which the series_cursor
@@ -6370,41 +5899,41 @@
5899 int i /* Which column to return */
5900 ){
5901 series_cursor *pCur = (series_cursor*)cur;
5902 sqlite3_int64 x = 0;
5903 switch( i ){
5904 case SERIES_COLUMN_START: x = pCur->iOBase; break;
5905 case SERIES_COLUMN_STOP: x = pCur->iOTerm; break;
5906 case SERIES_COLUMN_STEP: x = pCur->iOStep; break;
5907 default: x = pCur->iValue; break;
5908 }
5909 sqlite3_result_int64(ctx, x);
5910 return SQLITE_OK;
5911 }
5912
5913 #ifndef LARGEST_UINT64
5914 #define LARGEST_INT64 ((sqlite3_int64)0x7fffffffffffffffLL)
5915 #define LARGEST_UINT64 ((sqlite3_uint64)0xffffffffffffffffULL)
5916 #define SMALLEST_INT64 ((sqlite3_int64)0x8000000000000000LL)
5917 #endif
5918
5919 /*
5920 ** The rowid is the same as the value.
5921 */
5922 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5923 series_cursor *pCur = (series_cursor*)cur;
5924 *pRowid = pCur->iValue;
5925 return SQLITE_OK;
5926 }
5927
5928 /*
5929 ** Return TRUE if the cursor has been moved off of the last
5930 ** row of output.
5931 */
5932 static int seriesEof(sqlite3_vtab_cursor *cur){
5933 series_cursor *pCur = (series_cursor*)cur;
5934 return pCur->bDone;
5935 }
5936
5937 /* True to cause run-time checking of the start=, stop=, and/or step=
5938 ** parameters. The only reason to do this is for testing the
5939 ** constraint checking logic for virtual tables in the SQLite core.
@@ -6411,10 +5940,63 @@
5940 */
5941 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5942 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5943 #endif
5944
5945 /*
5946 ** Return the number of steps between pCur->iBase and pCur->iTerm if
5947 ** the step width is pCur->iStep.
5948 */
5949 static sqlite3_uint64 seriesSteps(series_cursor *pCur){
5950 if( pCur->bDesc ){
5951 assert( pCur->iBase >= pCur->iTerm );
5952 return span64(pCur->iBase, pCur->iTerm)/pCur->iStep;
5953 }else{
5954 assert( pCur->iBase <= pCur->iTerm );
5955 return span64(pCur->iTerm, pCur->iBase)/pCur->iStep;
5956 }
5957 }
5958
5959 #if defined(SQLITE_ENABLE_MATH_FUNCTIONS) || defined(_WIN32)
5960 /*
5961 ** Case 1 (the most common case):
5962 ** The standard math library is available so use ceil() and floor() from there.
5963 */
5964 static double seriesCeil(double r){ return ceil(r); }
5965 static double seriesFloor(double r){ return floor(r); }
5966 #elif defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
5967 /*
5968 ** Case 2 (2nd most common): Use GCC/Clang builtins
5969 */
5970 static double seriesCeil(double r){ return __builtin_ceil(r); }
5971 static double seriesFloor(double r){ return __builtin_floor(r); }
5972 #else
5973 /*
5974 ** Case 3 (rarely happens): Use home-grown ceil() and floor() routines.
5975 */
5976 static double seriesCeil(double r){
5977 sqlite3_int64 x;
5978 if( r!=r ) return r;
5979 if( r<=(-4503599627370496.0) ) return r;
5980 if( r>=(+4503599627370496.0) ) return r;
5981 x = (sqlite3_int64)r;
5982 if( r==(double)x ) return r;
5983 if( r>(double)x ) x++;
5984 return (double)x;
5985 }
5986 static double seriesFloor(double r){
5987 sqlite3_int64 x;
5988 if( r!=r ) return r;
5989 if( r<=(-4503599627370496.0) ) return r;
5990 if( r>=(+4503599627370496.0) ) return r;
5991 x = (sqlite3_int64)r;
5992 if( r==(double)x ) return r;
5993 if( r<(double)x ) x--;
5994 return (double)x;
5995 }
5996 #endif
5997
5998 /*
5999 ** This method is called to "rewind" the series_cursor object back
6000 ** to the first row of output. This method is always called at least
6001 ** once prior to any call to seriesColumn() or seriesRowid() or
6002 ** seriesEof().
@@ -6444,185 +6026,243 @@
6026 sqlite3_vtab_cursor *pVtabCursor,
6027 int idxNum, const char *idxStrUnused,
6028 int argc, sqlite3_value **argv
6029 ){
6030 series_cursor *pCur = (series_cursor *)pVtabCursor;
6031 int iArg = 0; /* Arguments used so far */
6032 int i; /* Loop counter */
6033 sqlite3_int64 iMin = SMALLEST_INT64; /* Smallest allowed output value */
6034 sqlite3_int64 iMax = LARGEST_INT64; /* Largest allowed output value */
6035 sqlite3_int64 iLimit = 0; /* if >0, the value of the LIMIT */
6036 sqlite3_int64 iOffset = 0; /* if >0, the value of the OFFSET */
6037
6038 (void)idxStrUnused;
6039
6040 /* If any constraints have a NULL value, then return no rows.
6041 ** See ticket https://sqlite.org/src/info/fac496b61722daf2
6042 */
6043 for(i=0; i<argc; i++){
6044 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
6045 goto series_no_rows;
6046 }
6047 }
6048
6049 /* Capture the three HIDDEN parameters to the virtual table and insert
6050 ** default values for any parameters that are omitted.
6051 */
6052 if( idxNum & 0x01 ){
6053 pCur->iOBase = sqlite3_value_int64(argv[iArg++]);
6054 }else{
6055 pCur->iOBase = 0;
6056 }
6057 if( idxNum & 0x02 ){
6058 pCur->iOTerm = sqlite3_value_int64(argv[iArg++]);
6059 }else{
6060 pCur->iOTerm = 0xffffffff;
6061 }
6062 if( idxNum & 0x04 ){
6063 pCur->iOStep = sqlite3_value_int64(argv[iArg++]);
6064 if( pCur->iOStep==0 ) pCur->iOStep = 1;
 
 
 
 
6065 }else{
6066 pCur->iOStep = 1;
6067 }
6068
6069 /* If there are constraints on the value column but there are
6070 ** no constraints on the start, stop, and step columns, then
6071 ** initialize the default range to be the entire range of 64-bit signed
6072 ** integers. This range will contracted by the value column constraints
6073 ** further below.
6074 */
6075 if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
6076 pCur->iOBase = SMALLEST_INT64;
6077 }
6078 if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
6079 pCur->iOTerm = LARGEST_INT64;
6080 }
6081 pCur->iBase = pCur->iOBase;
6082 pCur->iTerm = pCur->iOTerm;
6083 if( pCur->iOStep>0 ){
6084 pCur->iStep = pCur->iOStep;
6085 }else if( pCur->iOStep>SMALLEST_INT64 ){
6086 pCur->iStep = -pCur->iOStep;
6087 }else{
6088 pCur->iStep = LARGEST_INT64;
6089 pCur->iStep++;
6090 }
6091 pCur->bDesc = pCur->iOStep<0;
6092 if( pCur->bDesc==0 && pCur->iBase>pCur->iTerm ){
6093 goto series_no_rows;
6094 }
6095 if( pCur->bDesc!=0 && pCur->iBase<pCur->iTerm ){
6096 goto series_no_rows;
6097 }
6098
6099 /* Extract the LIMIT and OFFSET values, but do not apply them yet.
6100 ** The range must first be constrained by the limits on value.
6101 */
6102 if( idxNum & 0x20 ){
6103 iLimit = sqlite3_value_int64(argv[iArg++]);
6104 if( idxNum & 0x40 ){
6105 iOffset = sqlite3_value_int64(argv[iArg++]);
6106 }
6107 }
6108
6109 /* Narrow the range of iMin and iMax (the minimum and maximum outputs)
6110 ** based on equality and inequality constraints on the "value" column.
6111 */
6112 if( idxNum & 0x3380 ){
6113 if( idxNum & 0x0080 ){ /* value=X */
6114 if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6115 double r = sqlite3_value_double(argv[iArg++]);
6116 if( r==seriesCeil(r)
6117 && r>=(double)SMALLEST_INT64
6118 && r<=(double)LARGEST_INT64
6119 ){
6120 iMin = iMax = (sqlite3_int64)r;
6121 }else{
6122 goto series_no_rows;
6123 }
6124 }else{
6125 iMin = iMax = sqlite3_value_int64(argv[iArg++]);
6126 }
6127 }else{
6128 if( idxNum & 0x0300 ){ /* value>X or value>=X */
6129 if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6130 double r = sqlite3_value_double(argv[iArg++]);
6131 if( r<(double)SMALLEST_INT64 ){
6132 iMin = SMALLEST_INT64;
6133 }else if( (idxNum & 0x0200)!=0 && r==seriesCeil(r) ){
6134 iMin = (sqlite3_int64)seriesCeil(r+1.0);
6135 }else{
6136 iMin = (sqlite3_int64)seriesCeil(r);
6137 }
6138 }else{
6139 iMin = sqlite3_value_int64(argv[iArg++]);
6140 if( (idxNum & 0x0200)!=0 ){
6141 if( iMin==LARGEST_INT64 ){
6142 goto series_no_rows;
6143 }else{
6144 iMin++;
6145 }
6146 }
6147 }
6148 }
6149 if( idxNum & 0x3000 ){ /* value<X or value<=X */
6150 if( sqlite3_value_numeric_type(argv[iArg])==SQLITE_FLOAT ){
6151 double r = sqlite3_value_double(argv[iArg++]);
6152 if( r>(double)LARGEST_INT64 ){
6153 iMax = LARGEST_INT64;
6154 }else if( (idxNum & 0x2000)!=0 && r==seriesFloor(r) ){
6155 iMax = (sqlite3_int64)(r-1.0);
6156 }else{
6157 iMax = (sqlite3_int64)seriesFloor(r);
6158 }
6159 }else{
6160 iMax = sqlite3_value_int64(argv[iArg++]);
6161 if( idxNum & 0x2000 ){
6162 if( iMax==SMALLEST_INT64 ){
6163 goto series_no_rows;
6164 }else{
6165 iMax--;
6166 }
6167 }
6168 }
6169 }
6170 if( iMin>iMax ){
6171 goto series_no_rows;
6172 }
6173 }
6174
6175 /* Try to reduce the range of values to be generated based on
6176 ** constraints on the "value" column.
6177 */
6178 if( pCur->bDesc==0 ){
6179 if( pCur->iBase<iMin ){
6180 sqlite3_uint64 span = span64(iMin,pCur->iBase);
6181 pCur->iBase = add64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
6182 if( pCur->iBase<iMin ){
6183 if( pCur->iBase > sub64(LARGEST_INT64, pCur->iStep) ){
6184 goto series_no_rows;
6185 }
6186 pCur->iBase = add64(pCur->iBase, pCur->iStep);
6187 }
6188 }
6189 if( pCur->iTerm>iMax ){
6190 pCur->iTerm = iMax;
6191 }
6192 }else{
6193 if( pCur->iBase>iMax ){
6194 sqlite3_uint64 span = span64(pCur->iBase,iMax);
6195 pCur->iBase = sub64(pCur->iBase, (span/pCur->iStep)*pCur->iStep);
6196 if( pCur->iBase>iMax ){
6197 if( pCur->iBase < add64(SMALLEST_INT64, pCur->iStep) ){
6198 goto series_no_rows;
6199 }
6200 pCur->iBase = sub64(pCur->iBase, pCur->iStep);
6201 }
6202 }
6203 if( pCur->iTerm<iMin ){
6204 pCur->iTerm = iMin;
6205 }
6206 }
6207 }
6208
6209 /* Adjust iTerm so that it is exactly the last value of the series.
6210 */
6211 if( pCur->bDesc==0 ){
6212 if( pCur->iBase>pCur->iTerm ){
6213 goto series_no_rows;
6214 }
6215 pCur->iTerm = sub64(pCur->iTerm,
6216 span64(pCur->iTerm,pCur->iBase) % pCur->iStep);
6217 }else{
6218 if( pCur->iBase<pCur->iTerm ){
6219 goto series_no_rows;
6220 }
6221 pCur->iTerm = add64(pCur->iTerm,
6222 span64(pCur->iBase,pCur->iTerm) % pCur->iStep);
6223 }
6224
6225 /* Transform the series generator to output values in the requested
6226 ** order.
6227 */
6228 if( ((idxNum & 0x0008)!=0 && pCur->bDesc==0)
6229 || ((idxNum & 0x0010)!=0 && pCur->bDesc!=0)
6230 ){
6231 sqlite3_int64 tmp = pCur->iBase;
6232 pCur->iBase = pCur->iTerm;
6233 pCur->iTerm = tmp;
6234 pCur->bDesc = !pCur->bDesc;
6235 }
6236
6237 /* Apply LIMIT and OFFSET constraints, if any */
6238 assert( pCur->iStep!=0 );
6239 if( idxNum & 0x20 ){
6240 if( iOffset>0 ){
6241 if( seriesSteps(pCur) < (sqlite3_uint64)iOffset ){
6242 goto series_no_rows;
6243 }else if( pCur->bDesc ){
6244 pCur->iBase = sub64(pCur->iBase, pCur->iStep*iOffset);
6245 }else{
6246 pCur->iBase = add64(pCur->iBase, pCur->iStep*iOffset);
6247 }
6248 }
6249 if( iLimit>=0 && seriesSteps(pCur) > (sqlite3_uint64)iLimit ){
6250 pCur->iTerm = add64(pCur->iBase, (iLimit - 1)*pCur->iStep);
6251 }
6252 }
6253 pCur->iValue = pCur->iBase;
6254 pCur->bDone = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6255 return SQLITE_OK;
6256
6257 series_no_rows:
6258 pCur->iBase = 0;
6259 pCur->iTerm = 0;
6260 pCur->iStep = 1;
6261 pCur->bDesc = 0;
6262 pCur->bDone = 1;
6263 return SQLITE_OK;
6264 }
6265
6266 /*
6267 ** SQLite will invoke this method one or more times while planning a query
6268 ** that uses the generate_series virtual table. This routine needs to create
@@ -6948,10 +6588,12 @@
6588 ** expression and M is the size of the input string. The matcher never
6589 ** exhibits exponential behavior. Note that the X{p,q} operator expands
6590 ** to p copies of X following by q-p copies of X? and that the size of the
6591 ** regular expression in the O(N*M) performance bound is computed after
6592 ** this expansion.
6593 **
6594 ** To help prevent DoS attacks, the maximum size of the NFA is restricted.
6595 */
6596 #include <string.h>
6597 #include <stdlib.h>
6598 /* #include "sqlite3ext.h" */
6599 SQLITE_EXTENSION_INIT1
@@ -6988,36 +6630,10 @@
6630 #define RE_OP_NOTDIGIT 14 /* Not a digit */
6631 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
6632 #define RE_OP_NOTSPACE 16 /* Not a digit */
6633 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6634 #define RE_OP_ATSTART 18 /* Currently at the start of the string */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6635
6636 /* Each opcode is a "state" in the NFA */
6637 typedef unsigned short ReStateNumber;
6638
6639 /* Because this is an NFA and not a DFA, multiple states can be active at
@@ -7051,10 +6667,11 @@
6667 unsigned (*xNextChar)(ReInput*); /* Next character function */
6668 unsigned char zInit[12]; /* Initial text to match */
6669 int nInit; /* Number of bytes in zInit */
6670 unsigned nState; /* Number of entries in aOp[] and aArg[] */
6671 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
6672 unsigned mxAlloc; /* Complexity limit */
6673 };
6674
6675 /* Add a state to the given state set if it is not already there */
6676 static void re_add_state(ReStateSet *pSet, int newState){
6677 unsigned i;
@@ -7265,18 +6882,19 @@
6882 return rc;
6883 }
6884
6885 /* Resize the opcode and argument arrays for an RE under construction.
6886 */
6887 static int re_resize(ReCompiled *p, unsigned int N){
6888 char *aOp;
6889 int *aArg;
6890 if( N>p->mxAlloc ){ p->zErr = "REGEXP pattern too big"; return 1; }
6891 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6892 if( aOp==0 ){ p->zErr = "out of memory"; return 1; }
6893 p->aOp = aOp;
6894 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6895 if( aArg==0 ){ p->zErr = "out of memory"; return 1; }
6896 p->aArg = aArg;
6897 p->nAlloc = N;
6898 return 0;
6899 }
6900
@@ -7303,11 +6921,11 @@
6921 }
6922
6923 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6924 ** under construction.
6925 */
6926 static void re_copy(ReCompiled *p, int iStart, unsigned int N){
6927 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6928 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6929 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6930 p->nState += N;
6931 }
@@ -7456,22 +7074,30 @@
7074 case '^': {
7075 re_append(p, RE_OP_ATSTART, 0);
7076 break;
7077 }
7078 case '{': {
7079 unsigned int m = 0, n = 0;
7080 unsigned int sz, j;
7081 if( iPrev<0 ) return "'{m,n}' without operand";
7082 while( (c=rePeek(p))>='0' && c<='9' ){
7083 m = m*10 + c - '0';
7084 if( m*2>p->mxAlloc ) return "REGEXP pattern too big";
7085 p->sIn.i++;
7086 }
7087 n = m;
7088 if( c==',' ){
7089 p->sIn.i++;
7090 n = 0;
7091 while( (c=rePeek(p))>='0' && c<='9' ){
7092 n = n*10 + c-'0';
7093 if( n*2>p->mxAlloc ) return "REGEXP pattern too big";
7094 p->sIn.i++;
7095 }
7096 }
7097 if( c!='}' ) return "unmatched '{'";
7098 if( n<m ) return "n less than m in '{m,n}'";
7099 p->sIn.i++;
7100 sz = p->nState - iPrev;
7101 if( m==0 ){
7102 if( n==0 ) return "both m and n are zero in '{m,n}'";
7103 re_insert(p, iPrev, RE_OP_FORK, sz+1);
@@ -7483,11 +7109,11 @@
7109 for(j=m; j<n; j++){
7110 re_append(p, RE_OP_FORK, sz+1);
7111 re_copy(p, iPrev, sz);
7112 }
7113 if( n==0 && m>0 ){
7114 re_append(p, RE_OP_FORK, -(int)sz);
7115 }
7116 break;
7117 }
7118 case '[': {
7119 unsigned int iFirst = p->nState;
@@ -7549,12 +7175,11 @@
7175
7176 /* Free and reclaim all the memory used by a previously compiled
7177 ** regular expression. Applications should invoke this routine once
7178 ** for every call to re_compile() to avoid memory leaks.
7179 */
7180 static void re_free(ReCompiled *pRe){
 
7181 if( pRe ){
7182 sqlite3_free(pRe->aOp);
7183 sqlite3_free(pRe->aArg);
7184 sqlite3_free(pRe);
7185 }
@@ -7564,11 +7189,16 @@
7189 ** Compile a textual regular expression in zIn[] into a compiled regular
7190 ** expression suitable for us by re_match() and return a pointer to the
7191 ** compiled regular expression in *ppRe. Return NULL on success or an
7192 ** error message if something goes wrong.
7193 */
7194 static const char *re_compile(
7195 ReCompiled **ppRe, /* OUT: write compiled NFA here */
7196 const char *zIn, /* Input regular expression */
7197 int mxRe, /* Complexity limit */
7198 int noCase /* True for caseless comparisons */
7199 ){
7200 ReCompiled *pRe;
7201 const char *zErr;
7202 int i, j;
7203
7204 *ppRe = 0;
@@ -7576,13 +7206,15 @@
7206 if( pRe==0 ){
7207 return "out of memory";
7208 }
7209 memset(pRe, 0, sizeof(*pRe));
7210 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
7211 pRe->mxAlloc = mxRe;
7212 if( re_resize(pRe, 30) ){
7213 zErr = pRe->zErr;
7214 re_free(pRe);
7215 return zErr;
7216 }
7217 if( zIn[0]=='^' ){
7218 zIn++;
7219 }else{
7220 re_append(pRe, RE_OP_ANYSTAR, 0);
@@ -7630,10 +7262,18 @@
7262 if( j>0 && pRe->zInit[j-1]==0 ) j--;
7263 pRe->nInit = j;
7264 }
7265 return pRe->zErr;
7266 }
7267
7268 /*
7269 ** Compute a reasonable limit on the length of the REGEXP NFA.
7270 */
7271 static int re_maxlen(sqlite3_context *context){
7272 sqlite3 *db = sqlite3_context_db_handle(context);
7273 return 75 + sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH,-1)/2;
7274 }
7275
7276 /*
7277 ** Implementation of the regexp() SQL function. This function implements
7278 ** the build-in REGEXP operator. The first argument to the function is the
7279 ** pattern and the second argument is the string. So, the SQL statements:
@@ -7656,11 +7296,12 @@
7296 (void)argc; /* Unused */
7297 pRe = sqlite3_get_auxdata(context, 0);
7298 if( pRe==0 ){
7299 zPattern = (const char*)sqlite3_value_text(argv[0]);
7300 if( zPattern==0 ) return;
7301 zErr = re_compile(&pRe, zPattern, re_maxlen(context),
7302 sqlite3_user_data(context)!=0);
7303 if( zErr ){
7304 re_free(pRe);
7305 sqlite3_result_error(context, zErr, -1);
7306 return;
7307 }
@@ -7698,14 +7339,36 @@
7339 sqlite3_str *pStr;
7340 int i;
7341 int n;
7342 char *z;
7343 (void)argc;
7344 static const char *ReOpName[] = {
7345 "EOF",
7346 "MATCH",
7347 "ANY",
7348 "ANYSTAR",
7349 "FORK",
7350 "GOTO",
7351 "ACCEPT",
7352 "CC_INC",
7353 "CC_EXC",
7354 "CC_VALUE",
7355 "CC_RANGE",
7356 "WORD",
7357 "NOTWORD",
7358 "DIGIT",
7359 "NOTDIGIT",
7360 "SPACE",
7361 "NOTSPACE",
7362 "BOUNDARY",
7363 "ATSTART",
7364 };
7365
7366 zPattern = (const char*)sqlite3_value_text(argv[0]);
7367 if( zPattern==0 ) return;
7368 zErr = re_compile(&pRe, zPattern, re_maxlen(context),
7369 sqlite3_user_data(context)!=0);
7370 if( zErr ){
7371 re_free(pRe);
7372 sqlite3_result_error(context, zErr, -1);
7373 return;
7374 }
@@ -9284,18 +8947,20 @@
8947 if( idxNum & 1 ){
8948 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
8949 if( pCur->nPrefix>0 ){
8950 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8951 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8952 pCur->nPrefix = (int)strlen(pCur->zPrefix);
8953 }
8954 iArg = 1;
8955 }
8956 if( idxNum & 2 ){
8957 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
8958 if( pCur->nLine>0 ){
8959 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
8960 if( pCur->zLine==0 ) return SQLITE_NOMEM;
8961 pCur->nLine = (int)strlen(pCur->zLine);
8962 }
8963 }
8964 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
8965 int i = pCur->nLine;
8966 while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
@@ -9303,10 +8968,11 @@
8968 }
8969 pCur->nPrefix = pCur->nLine - i;
8970 if( pCur->nPrefix>0 ){
8971 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
8972 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
8973 pCur->nPrefix = (int)strlen(pCur->zPrefix);
8974 }
8975 }
8976 pCur->iRowid = 0;
8977 pCur->ePhase = COMPLETION_FIRST_PHASE;
8978 return completionNext(pVtabCursor);
@@ -10216,11 +9882,17 @@
9882 "method," /* 6: Compression method (integer) */
9883 "z HIDDEN" /* 7: Name of zip file */
9884 ") WITHOUT ROWID;";
9885
9886 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
9887 #define ZIPFILE_MX_NAME (250) /* Windows limitation on filename size */
9888
9889 /*
9890 ** The buffer should be large enough to contain 3 65536 byte strings - the
9891 ** filename, the extra field and the file comment.
9892 */
9893 #define ZIPFILE_BUFFER_SIZE (200*1024)
9894
9895
9896 /*
9897 ** Magic numbers used to read and write zip files.
9898 **
@@ -10773,10 +10445,11 @@
10445 pLFH->crc32 = zipfileRead32(aRead);
10446 pLFH->szCompressed = zipfileRead32(aRead);
10447 pLFH->szUncompressed = zipfileRead32(aRead);
10448 pLFH->nFile = zipfileRead16(aRead);
10449 pLFH->nExtra = zipfileRead16(aRead);
10450 if( pLFH->nFile>ZIPFILE_MX_NAME ) rc = SQLITE_ERROR;
10451 }
10452 return rc;
10453 }
10454
10455
@@ -10898,10 +10571,19 @@
10571 || mUnixTime==zipfileMtime(pCds)
10572 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
10573 /* || (mUnixTime % 2) */
10574 );
10575 }
10576
10577 /*
10578 ** Set (*pzErr) to point to a buffer from sqlite3_malloc() containing a
10579 ** generic corruption message and return SQLITE_CORRUPT;
10580 */
10581 static int zipfileCorrupt(char **pzErr){
10582 *pzErr = sqlite3_mprintf("zip archive is corrupt");
10583 return SQLITE_CORRUPT;
10584 }
10585
10586 /*
10587 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
10588 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
10589 ** then pFile is a file-handle open on a zip file. In either case, this
@@ -10921,16 +10603,19 @@
10603 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
10604 ){
10605 u8 *aRead;
10606 char **pzErr = &pTab->base.zErrMsg;
10607 int rc = SQLITE_OK;
 
10608
10609 if( aBlob==0 ){
10610 aRead = pTab->aBuffer;
10611 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
10612 }else{
10613 if( (iOff+ZIPFILE_CDS_FIXED_SZ)>nBlob ){
10614 /* Not enough data for the CDS structure. Corruption. */
10615 return zipfileCorrupt(pzErr);
10616 }
10617 aRead = (u8*)&aBlob[iOff];
10618 }
10619
10620 if( rc==SQLITE_OK ){
10621 sqlite3_int64 nAlloc;
@@ -10957,10 +10642,13 @@
10642 rc = zipfileReadData(
10643 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
10644 );
10645 }else{
10646 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
10647 if( (iOff + ZIPFILE_LFH_FIXED_SZ + nFile + nExtra)>nBlob ){
10648 rc = zipfileCorrupt(pzErr);
10649 }
10650 }
10651 }
10652
10653 if( rc==SQLITE_OK ){
10654 u32 *pt = &pNew->mUnixTime;
@@ -10979,19 +10667,26 @@
10667 ZipfileLFH lfh;
10668 if( pFile ){
10669 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
10670 }else{
10671 aRead = (u8*)&aBlob[pNew->cds.iOffset];
10672 if( (pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ)>nBlob ){
10673 rc = zipfileCorrupt(pzErr);
10674 }
10675 }
10676
10677 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
10678 if( rc==SQLITE_OK ){
10679 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
10680 pNew->iDataOff += lfh.nFile + lfh.nExtra;
10681 if( aBlob && pNew->cds.szCompressed ){
10682 if( pNew->iDataOff + pNew->cds.szCompressed > nBlob ){
10683 rc = zipfileCorrupt(pzErr);
10684 }else{
10685 pNew->aData = &pNew->aExtra[nExtra];
10686 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
10687 }
10688 }
10689 }else{
10690 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
10691 (int)pNew->cds.iOffset
10692 );
@@ -11774,10 +11469,15 @@
11469
11470 if( rc==SQLITE_OK ){
11471 zPath = (const char*)sqlite3_value_text(apVal[2]);
11472 if( zPath==0 ) zPath = "";
11473 nPath = (int)strlen(zPath);
11474 if( nPath>ZIPFILE_MX_NAME ){
11475 zipfileTableErr(pTab, "filename too long; max: %d bytes",
11476 ZIPFILE_MX_NAME);
11477 rc = SQLITE_CONSTRAINT;
11478 }
11479 mTime = zipfileGetTime(apVal[4]);
11480 }
11481
11482 if( rc==SQLITE_OK && bIsDir ){
11483 /* For a directory, check that the last character in the path is a
@@ -12135,10 +11835,17 @@
11835 if( zName==0 ){
11836 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
11837 rc = SQLITE_ERROR;
11838 goto zipfile_step_out;
11839 }
11840 if( nName>ZIPFILE_MX_NAME ){
11841 zErr = sqlite3_mprintf(
11842 "filename argument to zipfile() too big; max: %d bytes",
11843 ZIPFILE_MX_NAME);
11844 rc = SQLITE_ERROR;
11845 goto zipfile_step_out;
11846 }
11847
11848 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
11849 ** deflate compression) or NULL (choose automatically). */
11850 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
11851 iMethod = (int)sqlite3_value_int64(pMethod);
@@ -12477,10 +12184,11 @@
12184 return rc;
12185 }
12186
12187 /************************* End ../ext/misc/sqlar.c ********************/
12188 #endif
12189 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
12190 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
12191 /*
12192 ** 2017 April 07
12193 **
12194 ** The author disclaims copyright to this source code. In place of
@@ -14885,10 +14593,11 @@
14593 }
14594
14595 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14596
14597 /************************* End ../ext/expert/sqlite3expert.c ********************/
14598 #endif
14599 /************************* Begin ../ext/intck/sqlite3intck.h ******************/
14600 /*
14601 ** 2024-02-08
14602 **
14603 ** The author disclaims copyright to this source code. In place of
@@ -21525,15 +21234,17 @@
21234 char **azFilter; /* Array of xFilter rejection GLOB patterns */
21235 sqlite3_session *p; /* The open session */
21236 };
21237 #endif
21238
21239 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
21240 typedef struct ExpertInfo ExpertInfo;
21241 struct ExpertInfo {
21242 sqlite3expert *pExpert;
21243 int bVerbose;
21244 };
21245 #endif
21246
21247 /* A single line in the EQP output */
21248 typedef struct EQPGraphRow EQPGraphRow;
21249 struct EQPGraphRow {
21250 int iEqpId; /* ID for this row */
@@ -21633,11 +21344,13 @@
21344 int *aiIndent; /* Array of indents used in MODE_Explain */
21345 int nIndent; /* Size of array aiIndent[] */
21346 int iIndent; /* Index of current op in aiIndent[] */
21347 char *zNonce; /* Nonce for temporary safe-mode escapes */
21348 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
21349 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
21350 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
21351 #endif
21352 #ifdef SQLITE_SHELL_FIDDLE
21353 struct {
21354 const char * zInput; /* Input string from wasm/JS proxy */
21355 const char * zPos; /* Cursor pos into zInput */
21356 const char * zDefaultDbName; /* Default name for db file */
@@ -21661,13 +21374,12 @@
21374 */
21375 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
21376 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
21377 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
21378 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
21379 #define SHELL_OPEN_DESERIALIZE 4 /* Open using sqlite3_deserialize() */
21380 #define SHELL_OPEN_HEXDB 5 /* Use "dbtotxt" output as data source */
 
21381
21382 /* Allowed values for ShellState.eTraceType
21383 */
21384 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
21385 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
@@ -22006,11 +21718,11 @@
21718 */
21719 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
21720 int i;
21721 unsigned char *aBlob = (unsigned char*)pBlob;
21722
21723 char *zStr = sqlite3_malloc64((i64)nBlob*2 + 1);
21724 shell_check_oom(zStr);
21725
21726 for(i=0; i<nBlob; i++){
21727 static const char aHex[] = {
21728 '0', '1', '2', '3', '4', '5', '6', '7',
@@ -22027,11 +21739,11 @@
21739
21740 /*
21741 ** Output the given string as a quoted string using SQL quoting conventions:
21742 **
21743 ** (1) Single quotes (') within the string are doubled
21744 ** (2) The while string is enclosed in '...'
21745 ** (3) Control characters other than \n, \t, and \r\n are escaped
21746 ** using \u00XX notation and if such substitutions occur,
21747 ** the whole string is enclosed in unistr('...') instead of '...'.
21748 **
21749 ** Step (3) is omitted if the control-character escape mode is OFF.
@@ -22273,11 +21985,11 @@
21985 **
21986 ** Escaping is needed if the string contains any control characters
21987 ** other than \t, \n, and \r\n
21988 **
21989 ** If no escaping is needed (the common case) then set *ppFree to NULL
21990 ** and return the original string. If escaping is needed, write the
21991 ** escaped string into memory obtained from sqlite3_malloc64() or the
21992 ** equivalent, and return the new string and set *ppFree to the new string
21993 ** as well.
21994 **
21995 ** The caller is responsible for freeing *ppFree if it is non-NULL in order
@@ -23278,32 +22990,21 @@
22990 ** Set the destination table field of the ShellState structure to
22991 ** the name of the table given. Escape any quote characters in the
22992 ** table name.
22993 */
22994 static void set_table_name(ShellState *p, const char *zName){
 
 
 
 
22995 if( p->zDestTable ){
22996 sqlite3_free(p->zDestTable);
22997 p->zDestTable = 0;
22998 }
22999 if( zName==0 ) return;
23000 if( quoteChar(zName) ){
23001 p->zDestTable = sqlite3_mprintf("\"%w\"", zName);
23002 }else{
23003 p->zDestTable = sqlite3_mprintf("%s", zName);
23004 }
23005 shell_check_oom(p->zDestTable);
 
 
 
 
 
 
 
23006 }
23007
23008 /*
23009 ** Maybe construct two lines of text that point out the position of a
23010 ** syntax error. Return a pointer to the text, in memory obtained from
@@ -23496,12 +23197,12 @@
23197 static int display_stats(
23198 sqlite3 *db, /* Database to query */
23199 ShellState *pArg, /* Pointer to ShellState */
23200 int bReset /* True to reset the stats */
23201 ){
23202 int iCur, iHiwtr;
23203 sqlite3_int64 iCur64, iHiwtr64;
23204 FILE *out;
23205 if( pArg==0 || pArg->out==0 ) return 0;
23206 out = pArg->out;
23207
23208 if( pArg->pStmt && pArg->statsOn==2 ){
@@ -23586,18 +23287,25 @@
23287 "Page cache hits: %d\n", iCur);
23288 iHiwtr = iCur = -1;
23289 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
23290 sqlite3_fprintf(out,
23291 "Page cache misses: %d\n", iCur);
23292 iHiwtr64 = iCur64 = -1;
23293 sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
23294 0);
23295 iHiwtr = iCur = -1;
23296 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
23297 sqlite3_fprintf(out,
23298 "Page cache writes: %d\n", iCur);
23299 iHiwtr = iCur = -1;
23300 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
23301 sqlite3_fprintf(out,
23302 "Page cache spills: %d\n", iCur);
23303 sqlite3_fprintf(out,
23304 "Temporary data spilled to disk: %lld\n", iCur64);
23305 sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64,
23306 1);
23307 iHiwtr = iCur = -1;
23308 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
23309 sqlite3_fprintf(out,
23310 "Schema Heap Usage: %d bytes\n", iCur);
23311 iHiwtr = iCur = -1;
@@ -23999,10 +23707,40 @@
23707 char *zBuf = sqlite3_malloc64( szVar-5 );
23708 if( zBuf ){
23709 memcpy(zBuf, &zVar[6], szVar-5);
23710 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
23711 }
23712 #ifdef SQLITE_ENABLE_CARRAY
23713 }else if( strncmp(zVar, "$carray_", 8)==0 ){
23714 static char *azColorNames[] = {
23715 "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold",
23716 "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon",
23717 "navy", "olive", "orange", "pink", "purple", "red", "silver",
23718 "tan", "teal", "violet", "white", "yellow"
23719 };
23720 static int aPrimes[] = {
23721 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
23722 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
23723 };
23724 /* Special bindings: carray($carray_clr), carray($carray_primes)
23725 ** with --unsafe-testing: carray($carray_clr_p,26,'char*'),
23726 ** carray($carray_primes_p,26,'int32')
23727 */
23728 if( strcmp(zVar+8,"clr")==0 ){
23729 sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0);
23730 }else if( strcmp(zVar+8,"primes")==0 ){
23731 sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0);
23732 }else if( strcmp(zVar+8,"clr_p")==0
23733 && ShellHasFlag(pArg,SHFLG_TestingMode) ){
23734 sqlite3_bind_pointer(pStmt,i,azColorNames,"carray",0);
23735 }else if( strcmp(zVar+8,"primes_p")==0
23736 && ShellHasFlag(pArg,SHFLG_TestingMode) ){
23737 sqlite3_bind_pointer(pStmt,i,aPrimes,"carray",0);
23738 }else{
23739 sqlite3_bind_null(pStmt, i);
23740 }
23741 #endif
23742 }else{
23743 sqlite3_bind_null(pStmt, i);
23744 }
23745 sqlite3_reset(pQ);
23746 }
@@ -24581,11 +24319,11 @@
24319 }
24320 }
24321 }
24322 }
24323
24324 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
24325 /*
24326 ** This function is called to process SQL if the previous shell command
24327 ** was ".expert". It passes the SQL in the second argument directly to
24328 ** the sqlite3expert object.
24329 **
@@ -24713,11 +24451,11 @@
24451 }
24452 sqlite3_free(zErr);
24453
24454 return rc;
24455 }
24456 #endif /* !SQLITE_OMIT_VIRTUALTABLE && !SQLITE_OMIT_AUTHORIZATION */
24457
24458 /*
24459 ** Execute a statement or set of statements. Print
24460 ** any result rows/columns depending on the current mode
24461 ** set via the supplied callback.
@@ -24739,11 +24477,11 @@
24477
24478 if( pzErrMsg ){
24479 *pzErrMsg = NULL;
24480 }
24481
24482 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
24483 if( pArg->expert.pExpert ){
24484 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
24485 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
24486 }
24487 #endif
@@ -24901,11 +24639,11 @@
24639 static char **tableColumnList(ShellState *p, const char *zTab){
24640 char **azCol = 0;
24641 sqlite3_stmt *pStmt;
24642 char *zSql;
24643 int nCol = 0;
24644 i64 nAlloc = 0;
24645 int nPK = 0; /* Number of PRIMARY KEY columns seen */
24646 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
24647 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
24648 int rc;
24649
@@ -24915,11 +24653,11 @@
24653 sqlite3_free(zSql);
24654 if( rc ) return 0;
24655 while( sqlite3_step(pStmt)==SQLITE_ROW ){
24656 if( nCol>=nAlloc-2 ){
24657 nAlloc = nAlloc*2 + nCol + 10;
24658 azCol = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0]));
24659 shell_check_oom(azCol);
24660 }
24661 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
24662 shell_check_oom(azCol[nCol]);
24663 if( sqlite3_column_int(pStmt, 5) ){
@@ -25106,17 +24844,17 @@
24844 appendText(&sSelect, " FROM ", 0);
24845 appendText(&sSelect, zTable, quoteChar(zTable));
24846
24847 savedDestTable = p->zDestTable;
24848 savedMode = p->mode;
24849 p->zDestTable = sTable.zTxt;
24850 p->mode = p->cMode = MODE_Insert;
24851 rc = shell_exec(p, sSelect.zTxt, 0);
24852 if( (rc&0xff)==SQLITE_CORRUPT ){
24853 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
24854 toggleSelectOrder(p->db);
24855 shell_exec(p, sSelect.zTxt, 0);
24856 toggleSelectOrder(p->db);
24857 }
24858 p->zDestTable = savedDestTable;
24859 p->mode = savedMode;
24860 freeText(&sTable);
@@ -25245,11 +24983,13 @@
24983 " --bom Put a UTF8 byte-order mark on intermediate file",
24984 #endif
24985 #ifndef SQLITE_SHELL_FIDDLE
24986 ".exit ?CODE? Exit this program with return-code CODE",
24987 #endif
24988 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
24989 ".expert EXPERIMENTAL. Suggest indexes for queries",
24990 #endif
24991 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
24992 ".filectrl CMD ... Run various sqlite3_file_control() operations",
24993 " --schema SCHEMA Use SCHEMA instead of \"main\"",
24994 " --help Show CMD details",
24995 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
@@ -25270,11 +25010,11 @@
25010 " from the \".mode\" output mode",
25011 " * If FILE begins with \"|\" then it is a command that generates the",
25012 " input text.",
25013 #endif
25014 #ifndef SQLITE_OMIT_TEST_CONTROL
25015 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
25016 #endif
25017 ".indexes ?TABLE? Show names of indexes",
25018 " If TABLE is specified, only show indexes for",
25019 " tables matching TABLE using the LIKE operator.",
25020 ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
@@ -25337,11 +25077,17 @@
25077 " Options:",
25078 " --append Use appendvfs to append database to the end of FILE",
25079 #endif
25080 #ifndef SQLITE_OMIT_DESERIALIZE
25081 " --deserialize Load into memory using sqlite3_deserialize()",
25082 #endif
25083 /*" --exclusive Set the SQLITE_OPEN_EXCLUSIVE flag", UNDOCUMENTED */
25084 #ifndef SQLITE_OMIT_DESERIALIZE
25085 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
25086 #endif
25087 " --ifexist Only open if FILE already exists",
25088 #ifndef SQLITE_OMIT_DESERIALIZE
25089 " --maxsize N Maximum size for --hexdb or --deserialized database",
25090 #endif
25091 " --new Initialize FILE to an empty database",
25092 " --nofollow Do not follow symbolic links",
25093 " --readonly Open FILE readonly",
@@ -25726,14 +25472,14 @@
25472 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
25473 */
25474 static unsigned char *readHexDb(ShellState *p, int *pnData){
25475 unsigned char *a = 0;
25476 int nLine;
25477 int n = 0; /* Size of db per first line of hex dump */
25478 i64 sz = 0; /* n rounded up to nearest page boundary */
25479 int pgsz = 0;
25480 i64 iOffset = 0;
 
25481 int rc;
25482 FILE *in;
25483 const char *zDbFilename = p->pAuxDb->zDbFilename;
25484 unsigned int x[16];
25485 char zLine[1000];
@@ -25753,20 +25499,21 @@
25499 nLine++;
25500 if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
25501 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
25502 if( rc!=2 ) goto readHexDb_error;
25503 if( n<0 ) goto readHexDb_error;
 
 
 
 
 
25504 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
25505 sqlite3_fputs("invalid pagesize\n", stderr);
25506 goto readHexDb_error;
25507 }
25508 sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
25509 a = sqlite3_malloc64( sz ? sz : 1 );
25510 shell_check_oom(a);
25511 memset(a, 0, sz);
25512 for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
25513 int j = 0; /* Page number from "| page" line */
25514 int k = 0; /* Offset from "| page" line */
25515 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
25516 if( rc==2 ){
25517 iOffset = k;
25518 continue;
25519 }
@@ -25775,18 +25522,18 @@
25522 }
25523 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
25524 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
25525 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
25526 if( rc==17 ){
25527 i64 iOff = iOffset+j;
25528 if( iOff+16<=sz && iOff>=0 ){
25529 int ii;
25530 for(ii=0; ii<16; ii++) a[iOff+ii] = x[ii]&0xff;
25531 }
25532 }
25533 }
25534 *pnData = sz;
25535 if( in!=p->in ){
25536 fclose(in);
25537 }else{
25538 p->lineno = nLine;
25539 }
@@ -25849,11 +25596,11 @@
25596 p->pLog = pSavedLog;
25597
25598 if( zFake ){
25599 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
25600 -1, sqlite3_free);
25601 sqlite3_free(zFake);
25602 }
25603 }
25604
25605 /* Flags for open_db().
25606 **
@@ -25881,14 +25628,17 @@
25628 }else{
25629 p->openMode = (u8)deduceDatabaseType(zDbFilename,
25630 (openFlags & OPEN_DB_ZIPFILE)!=0);
25631 }
25632 }
25633 if( (p->openFlags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE))==0 ){
25634 if( p->openFlags==0 ) p->openFlags = SQLITE_OPEN_CREATE;
25635 p->openFlags |= SQLITE_OPEN_READWRITE;
25636 }
25637 switch( p->openMode ){
25638 case SHELL_OPEN_APPENDVFS: {
25639 sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, "apndvfs");
 
25640 break;
25641 }
25642 case SHELL_OPEN_HEXDB:
25643 case SHELL_OPEN_DESERIALIZE: {
25644 sqlite3_open(0, &p->db);
@@ -25896,19 +25646,13 @@
25646 }
25647 case SHELL_OPEN_ZIPFILE: {
25648 sqlite3_open(":memory:", &p->db);
25649 break;
25650 }
 
 
 
 
 
25651 case SHELL_OPEN_UNSPEC:
25652 case SHELL_OPEN_NORMAL: {
25653 sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, 0);
 
25654 break;
25655 }
25656 }
25657 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
25658 sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
@@ -25944,11 +25688,10 @@
25688 sqlite3_sha_init(p->db, 0, 0);
25689 sqlite3_shathree_init(p->db, 0, 0);
25690 sqlite3_uint_init(p->db, 0, 0);
25691 sqlite3_stmtrand_init(p->db, 0, 0);
25692 sqlite3_decimal_init(p->db, 0, 0);
 
25693 sqlite3_base64_init(p->db, 0, 0);
25694 sqlite3_base85_init(p->db, 0, 0);
25695 sqlite3_regexp_init(p->db, 0, 0);
25696 sqlite3_ieee_init(p->db, 0, 0);
25697 sqlite3_series_init(p->db, 0, 0);
@@ -26043,13 +25786,15 @@
25786 }
25787 }
25788 #endif
25789 }
25790 if( p->db!=0 ){
25791 #ifndef SQLITE_OMIT_AUTHORIZATION
25792 if( p->bSafeModePersist ){
25793 sqlite3_set_authorizer(p->db, safeModeAuth, p);
25794 }
25795 #endif
25796 sqlite3_db_config(
25797 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
25798 );
25799 }
25800 }
@@ -26358,12 +26103,12 @@
26103 struct ImportCtx {
26104 const char *zFile; /* Name of the input file */
26105 FILE *in; /* Read the CSV text from this input stream */
26106 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
26107 char *z; /* Accumulated text for a field */
26108 i64 n; /* Number of bytes in z */
26109 i64 nAlloc; /* Space allocated for z[] */
26110 int nLine; /* Current line number */
26111 int nRow; /* Number of rows imported */
26112 int nErr; /* Number of errors encountered */
26113 int bNotFirst; /* True if one or more bytes already read */
26114 int cTerm; /* Character that terminated the most recent field */
@@ -26829,14 +26574,17 @@
26574 #if SQLITE_SHELL_HAVE_RECOVER
26575 /*
26576 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
26577 */
26578 static unsigned int get2byteInt(unsigned char *a){
26579 return ((unsigned int)a[0]<<8) + (unsigned int)a[1];
26580 }
26581 static unsigned int get4byteInt(unsigned char *a){
26582 return ((unsigned int)a[0]<<24)
26583 + ((unsigned int)a[1]<<16)
26584 + ((unsigned int)a[2]<<8)
26585 + (unsigned int)a[3];
26586 }
26587
26588 /*
26589 ** Implementation of the ".dbinfo" command.
26590 **
@@ -26969,11 +26717,11 @@
26717 if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26718 nPage = sqlite3_column_int64(pStmt, 0);
26719 sqlite3_finalize(pStmt);
26720 pStmt = 0;
26721 if( nPage<1 ) goto dbtotxt_error;
26722 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
26723 if( rc ) goto dbtotxt_error;
26724 if( sqlite3_step(pStmt)!=SQLITE_ROW ){
26725 zTail = "unk.db";
26726 }else{
26727 const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
@@ -26980,10 +26728,15 @@
26728 if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
26729 zTail = strrchr(zFilename, '/');
26730 #if defined(_WIN32)
26731 if( zTail==0 ) zTail = strrchr(zFilename, '\\');
26732 #endif
26733 if( zTail==0 ){
26734 zTail = zFilename;
26735 }else if( zTail[1]!=0 ){
26736 zTail++;
26737 }
26738 }
26739 zName = strdup(zTail);
26740 shell_check_oom(zName);
26741 sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
26742 nPage*pgSz, pgSz, zName);
@@ -27148,10 +26901,47 @@
26901 if( zStr[0]!='-' ) return 0;
26902 zStr++;
26903 if( zStr[0]=='-' ) zStr++;
26904 return cli_strcmp(zStr, zOpt)==0;
26905 }
26906
26907 /*
26908 ** The input zFN is guaranteed to start with "file:" and is thus a URI
26909 ** filename. Extract the actual filename and return a pointer to that
26910 ** filename in spaced obtained from sqlite3_malloc().
26911 **
26912 ** The caller is responsible for freeing space using sqlite3_free() when
26913 ** it has finished with the filename.
26914 */
26915 static char *shellFilenameFromUri(const char *zFN){
26916 char *zOut;
26917 int i, j, d1, d2;
26918
26919 assert( cli_strncmp(zFN,"file:",5)==0 );
26920 zOut = sqlite3_mprintf("%s", zFN+5);
26921 shell_check_oom(zOut);
26922 for(i=j=0; zOut[i]!=0 && zOut[i]!='?'; i++){
26923 if( zOut[i]!='%' ){
26924 zOut[j++] = zOut[i];
26925 continue;
26926 }
26927 d1 = hexDigitValue(zOut[i+1]);
26928 if( d1<0 ){
26929 zOut[j] = 0;
26930 break;
26931 }
26932 d2 = hexDigitValue(zOut[i+2]);
26933 if( d2<0 ){
26934 zOut[j] = 0;
26935 break;
26936 }
26937 zOut[j++] = d1*16 + d2;
26938 i += 2;
26939 }
26940 zOut[j] = 0;
26941 return zOut;
26942 }
26943
26944 /*
26945 ** Delete a file.
26946 */
26947 int shellDeleteFile(const char *zFilename){
@@ -28704,11 +28494,11 @@
28494 int nArg = 0;
28495 int n, c;
28496 int rc = 0;
28497 char *azArg[52];
28498
28499 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
28500 if( p->expert.pExpert ){
28501 expertFinish(p, 1, 0);
28502 }
28503 #endif
28504
@@ -29005,11 +28795,11 @@
28795 }else{
28796 while( sqlite3_step(pStmt)==SQLITE_ROW ){
28797 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
28798 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
28799 if( zSchema==0 || zFile==0 ) continue;
28800 azName = sqlite3_realloc64(azName, (nName+1)*2*sizeof(char*));
28801 shell_check_oom(azName);
28802 azName[nName*2] = strdup(zSchema);
28803 azName[nName*2+1] = strdup(zFile);
28804 nName++;
28805 }
@@ -29208,10 +28998,11 @@
28998 rc = 1;
28999 }
29000 }else
29001
29002 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
29003 open_db(p, 0);
29004 rc = shell_dbtotxt_command(p, nArg, azArg);
29005 }else
29006
29007 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
29008 if( nArg==2 ){
@@ -29273,11 +29064,11 @@
29064 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
29065 p->autoExplain = 1;
29066 }
29067 }else
29068
29069 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
29070 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
29071 if( p->bSafeMode ){
29072 sqlite3_fprintf(stderr,
29073 "Cannot run experimental commands such as \"%s\" in safe mode\n",
29074 azArg[0]);
@@ -29840,16 +29631,10 @@
29631 sqlite3_stmt *pStmt;
29632 int tnum = 0;
29633 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
29634 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
29635 int i;
 
 
 
 
 
 
29636 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
29637 eputz("Usage: .imposter INDEX IMPOSTER\n"
29638 " .imposter off\n");
29639 /* Also allowed, but not documented:
29640 **
@@ -29917,22 +29702,19 @@
29702 if( lenPK==0 ) lenPK = 100000;
29703 zSql = sqlite3_mprintf(
29704 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
29705 azArg[2], zCollist, lenPK, zCollist);
29706 sqlite3_free(zCollist);
29707 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 2, tnum);
29708 if( rc==SQLITE_OK ){
29709 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
29710 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
29711 if( rc ){
29712 sqlite3_fprintf(stderr,
29713 "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
29714 }else{
29715 sqlite3_fprintf(stdout, "%s;\n", zSql);
 
 
 
29716 }
29717 }else{
29718 sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
29719 rc = 1;
29720 }
@@ -30282,10 +30064,11 @@
30064 const char *zFN = 0; /* Pointer to constant filename */
30065 char *zNewFilename = 0; /* Name of the database file to open */
30066 int iName = 1; /* Index in azArg[] of the filename */
30067 int newFlag = 0; /* True to delete file before opening */
30068 int openMode = SHELL_OPEN_UNSPEC;
30069 int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
30070
30071 /* Check for command-line arguments */
30072 for(iName=1; iName<nArg; iName++){
30073 const char *z = azArg[iName];
30074 #ifndef SQLITE_SHELL_FIDDLE
@@ -30296,13 +30079,18 @@
30079 openMode = SHELL_OPEN_ZIPFILE;
30080 #endif
30081 }else if( optionMatch(z, "append") ){
30082 openMode = SHELL_OPEN_APPENDVFS;
30083 }else if( optionMatch(z, "readonly") ){
30084 openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
30085 openFlags |= SQLITE_OPEN_READONLY;
30086 }else if( optionMatch(z, "exclusive") ){
30087 openFlags |= SQLITE_OPEN_EXCLUSIVE;
30088 }else if( optionMatch(z, "ifexists") ){
30089 openFlags &= ~(SQLITE_OPEN_CREATE);
30090 }else if( optionMatch(z, "nofollow") ){
30091 openFlags |= SQLITE_OPEN_NOFOLLOW;
30092 #ifndef SQLITE_OMIT_DESERIALIZE
30093 }else if( optionMatch(z, "deserialize") ){
30094 openMode = SHELL_OPEN_DESERIALIZE;
30095 }else if( optionMatch(z, "hexdb") ){
30096 openMode = SHELL_OPEN_HEXDB;
@@ -30330,16 +30118,25 @@
30118 p->db = 0;
30119 p->pAuxDb->zDbFilename = 0;
30120 sqlite3_free(p->pAuxDb->zFreeOnClose);
30121 p->pAuxDb->zFreeOnClose = 0;
30122 p->openMode = openMode;
30123 p->openFlags = openFlags;
30124 p->szMax = 0;
30125
30126 /* If a filename is specified, try to open it first */
30127 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
30128 if( newFlag && zFN && !p->bSafeMode ){
30129 if( cli_strncmp(zFN,"file:",5)==0 ){
30130 char *zDel = shellFilenameFromUri(zFN);
30131 shell_check_oom(zDel);
30132 shellDeleteFile(zDel);
30133 sqlite3_free(zDel);
30134 }else{
30135 shellDeleteFile(zFN);
30136 }
30137 }
30138 #ifndef SQLITE_SHELL_FIDDLE
30139 if( p->bSafeMode
30140 && p->openMode!=SHELL_OPEN_HEXDB
30141 && zFN
30142 && cli_strcmp(zFN,":memory:")!=0
@@ -30938,13 +30735,13 @@
30735 appendText(&sSelect, "name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ", 0);
30736 }
30737 appendText(&sSelect, "sql IS NOT NULL"
30738 " ORDER BY snum, rowid", 0);
30739 if( bDebug ){
30740 sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.zTxt);
30741 }else{
30742 rc = sqlite3_exec(p->db, sSelect.zTxt, callback, &data, &zErrMsg);
30743 }
30744 freeText(&sSelect);
30745 }
30746 if( zErrMsg ){
30747 shellEmitError(zErrMsg);
@@ -31072,19 +30869,20 @@
30869
30870 /* .session filter GLOB ....
30871 ** Set a list of GLOB patterns of table names to be excluded.
30872 */
30873 if( cli_strcmp(azCmd[0], "filter")==0 ){
30874 int ii;
30875 i64 nByte;
30876 if( nCmd<2 ) goto session_syntax_error;
30877 if( pAuxDb->nSession ){
30878 for(ii=0; ii<pSession->nFilter; ii++){
30879 sqlite3_free(pSession->azFilter[ii]);
30880 }
30881 sqlite3_free(pSession->azFilter);
30882 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
30883 pSession->azFilter = sqlite3_malloc64( nByte );
30884 shell_check_oom( pSession->azFilter );
30885 for(ii=1; ii<nCmd; ii++){
30886 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
30887 shell_check_oom(x);
30888 }
@@ -31264,26 +31062,26 @@
31062 sqlite3_fprintf(p->out, "%s\n", zSql);
31063 }else
31064 if( cli_strcmp(zOp,"run")==0 ){
31065 char *zErrMsg = 0;
31066 str.n = 0;
31067 str.zTxt[0] = 0;
31068 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
31069 nTest++;
31070 if( bVerbose ){
31071 sqlite3_fprintf(p->out, "Result: %s\n", str.zTxt);
31072 }
31073 if( rc || zErrMsg ){
31074 nErr++;
31075 rc = 1;
31076 sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
31077 sqlite3_free(zErrMsg);
31078 }else if( cli_strcmp(zAns,str.zTxt)!=0 ){
31079 nErr++;
31080 rc = 1;
31081 sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
31082 sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.zTxt);
31083 }
31084 }
31085 else{
31086 sqlite3_fprintf(stderr,
31087 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
@@ -31395,11 +31193,11 @@
31193 appendText(&sQuery, "SELECT * FROM ", 0);
31194 appendText(&sQuery, zTab, 0);
31195 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
31196 }
31197 appendText(&sSql, zSep, 0);
31198 appendText(&sSql, sQuery.zTxt, '\'');
31199 sQuery.n = 0;
31200 appendText(&sSql, ",", 0);
31201 appendText(&sSql, zTab, '\'');
31202 zSep = "),(";
31203 }
@@ -31407,17 +31205,17 @@
31205 if( bSeparate ){
31206 zSql = sqlite3_mprintf(
31207 "%s))"
31208 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
31209 " FROM [sha3sum$query]",
31210 sSql.zTxt, iSize);
31211 }else{
31212 zSql = sqlite3_mprintf(
31213 "%s))"
31214 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
31215 " FROM [sha3sum$query]",
31216 sSql.zTxt, iSize);
31217 }
31218 shell_check_oom(zSql);
31219 freeText(&sQuery);
31220 freeText(&sSql);
31221 if( bDebug ){
@@ -31613,11 +31411,11 @@
31411 goto meta_command_exit;
31412 }
31413 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
31414 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
31415 if( zDbName==0 ) continue;
31416 if( s.zTxt && s.zTxt[0] ) appendText(&s, " UNION ALL ", 0);
31417 if( sqlite3_stricmp(zDbName, "main")==0 ){
31418 appendText(&s, "SELECT name FROM ", 0);
31419 }else{
31420 appendText(&s, "SELECT ", 0);
31421 appendText(&s, zDbName, '\'');
@@ -31635,11 +31433,11 @@
31433 }
31434 }
31435 rc = sqlite3_finalize(pStmt);
31436 if( rc==SQLITE_OK ){
31437 appendText(&s, " ORDER BY 1", 0);
31438 rc = sqlite3_prepare_v2(p->db, s.zTxt, -1, &pStmt, 0);
31439 }
31440 freeText(&s);
31441 if( rc ) return shellDatabaseError(p->db);
31442
31443 /* Run the SQL statement prepared by the above block. Store the results
@@ -31652,14 +31450,14 @@
31450 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
31451 }
31452 while( sqlite3_step(pStmt)==SQLITE_ROW ){
31453 if( nRow>=nAlloc ){
31454 char **azNew;
31455 sqlite3_int64 n2 = 2*(sqlite3_int64)nAlloc + 10;
31456 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
31457 shell_check_oom(azNew);
31458 nAlloc = (int)n2;
31459 azResult = azNew;
31460 }
31461 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
31462 shell_check_oom(azResult[nRow]);
31463 nRow++;
@@ -31818,11 +31616,11 @@
31616 { 0x00000020, 1, "CoverIdxScan" },
31617 { 0x00000040, 1, "OrderByIdxJoin" },
31618 { 0x00000080, 1, "Transitive" },
31619 { 0x00000100, 1, "OmitNoopJoin" },
31620 { 0x00000200, 1, "CountOfView" },
31621 { 0x00000400, 1, "CursorHints" },
31622 { 0x00000800, 1, "Stat4" },
31623 { 0x00001000, 1, "PushDown" },
31624 { 0x00002000, 1, "SimplifyJoin" },
31625 { 0x00004000, 1, "SkipScan" },
31626 { 0x00008000, 1, "PropagateConst" },
@@ -32365,11 +32163,14 @@
32163 p->nWidth = nArg-1;
32164 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
32165 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
32166 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
32167 for(j=1; j<nArg; j++){
32168 i64 w = integerValue(azArg[j]);
32169 if( w < -30000 ) w = -30000;
32170 if( w > +30000 ) w = +30000;
32171 p->colWidth[j-1] = (int)w;
32172 }
32173 }else
32174
32175 {
32176 sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
@@ -32888,76 +32689,95 @@
32689
32690 return home_dir;
32691 }
32692
32693 /*
32694 ** On non-Windows platforms, look for:
32695 **
32696 ** - ${zEnvVar}/${zBaseName}
32697 ** - ${HOME}/${zSubdir}/${zBaseName}
32698 **
32699 ** $zEnvVar is intended to be the name of an XDG_... environment
32700 ** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME. If zEnvVar is
32701 ** NULL or getenv(zEnvVar) is NULL then fall back to the second
32702 ** option. If the selected option is not found in the filesystem,
32703 ** return 0.
32704 **
32705 ** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName}
32706 ** becomes the fallback.
32707 **
32708 ** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir
32709 ** will conventionally be ".config" or ".local/state", which, not
32710 ** coincidentally, is the typical subdir of the corresponding XDG_...
32711 ** var with the XDG var's $HOME prefix.
32712 **
32713 ** The returned string is obtained from sqlite3_malloc() and should be
32714 ** sqlite3_free()'d by the caller.
32715 */
32716 static char *find_xdg_file(const char *zEnvVar, const char *zSubdir,
32717 const char *zBaseName){
32718 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
32719 || defined(__RTP__) || defined(_WRS_KERNEL)
32720 return 0;
32721 #else
32722 char *zConfigFile = 0;
32723 const char *zXdgDir;
32724
32725 zXdgDir = zEnvVar ? getenv(zEnvVar) : 0;
32726 if( zXdgDir ){
32727 zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName);
 
 
32728 }else{
32729 const char * zHome = find_home_dir(0);
32730 if( zHome==0 ) return 0;
32731 zConfigFile = (zSubdir && *zSubdir)
32732 ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName)
32733 : sqlite3_mprintf("%s/%s", zHome, zBaseName);
32734 }
32735 shell_check_oom(zConfigFile);
32736 if( access(zConfigFile,0)!=0 ){
32737 sqlite3_free(zConfigFile);
32738 zConfigFile = 0;
32739 }
32740 return zConfigFile;
32741 #endif
32742 }
32743
32744 /*
32745 ** Read input from the file sqliterc_override. If that parameter is
32746 ** NULL, take it from find_xdg_file(), if found, or fall back to
32747 ** ~/.sqliterc.
32748 **
32749 ** Failure to read the config is only considered a failure if
32750 ** sqliterc_override is not NULL, in which case this function may emit
32751 ** a warning or, if ::bail_on_error is true, fail fatally if the file
32752 ** named by sqliterc_override is not found.
32753 */
32754 static void process_sqliterc(
32755 ShellState *p, /* Configuration data */
32756 const char *sqliterc_override /* Name of config file. NULL to use default */
32757 ){
32758 char *home_dir = NULL;
32759 char *sqliterc = (char*)sqliterc_override;
 
32760 FILE *inSaved = p->in;
32761 int savedLineno = p->lineno;
32762
32763 if( sqliterc == NULL ){
32764 sqliterc = find_xdg_file("XDG_CONFIG_HOME",
32765 ".config",
32766 "sqlite3/sqliterc");
32767 }
32768 if( sqliterc == NULL ){
32769 home_dir = find_home_dir(0);
32770 if( home_dir==0 ){
32771 eputz("-- warning: cannot find home directory;"
32772 " cannot read ~/.sqliterc\n");
32773 return;
32774 }
32775 sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir);
32776 shell_check_oom(sqliterc);
 
32777 }
32778 p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0;
32779 if( p->in ){
32780 if( stdin_is_interactive ){
32781 sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
32782 }
32783 if( process_input(p) && bail_on_error ) exit(1);
@@ -32966,11 +32786,13 @@
32786 sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
32787 if( bail_on_error ) exit(1);
32788 }
32789 p->in = inSaved;
32790 p->lineno = savedLineno;
32791 if( sqliterc != sqliterc_override ){
32792 sqlite3_free(sqliterc);
32793 }
32794 }
32795
32796 /*
32797 ** Show available command line options
32798 */
@@ -32997,10 +32819,11 @@
32819 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
32820 " -heap SIZE Size of heap for memsys3 or memsys5\n"
32821 #endif
32822 " -help show this message\n"
32823 " -html set output mode to HTML\n"
32824 " -ifexists only open if database already exists\n"
32825 " -interactive force interactive I/O\n"
32826 " -json set output mode to 'json'\n"
32827 " -line set output mode to 'line'\n"
32828 " -list set output mode to 'list'\n"
32829 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
@@ -33341,16 +33164,24 @@
33164 (void)cmdline_option_value(argc, argv, ++i);
33165 #endif
33166 }else if( cli_strcmp(z,"-pagecache")==0 ){
33167 sqlite3_int64 n, sz;
33168 sz = integerValue(cmdline_option_value(argc,argv,++i));
33169 if( sz>65536 ) sz = 65536;
33170 if( sz<0 ) sz = 0;
33171 n = integerValue(cmdline_option_value(argc,argv,++i));
33172 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
33173 n = 0xffffffffffffLL/sz;
33174 }
33175 if( sz>0 && (sz & (sz-1))==0 ){
33176 /* If SIZE is a power of two, round it up by the PCACHE_HDRSZ */
33177 int szHdr = 0;
33178 sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &szHdr);
33179 sz += szHdr;
33180 sqlite3_fprintf(stdout, "Page cache size increased to %d to accommodate"
33181 " the %d-byte headers\n", (int)sz, szHdr);
33182 }
33183 verify_uninitialized();
33184 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
33185 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
33186 data.shellFlgs |= SHFLG_Pagecache;
33187 }else if( cli_strcmp(z,"-lookaside")==0 ){
@@ -33359,11 +33190,11 @@
33190 if( sz<0 ) sz = 0;
33191 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
33192 if( n<0 ) n = 0;
33193 verify_uninitialized();
33194 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
33195 if( (i64)sz*(i64)n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
33196 }else if( cli_strcmp(z,"-threadsafe")==0 ){
33197 int n;
33198 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
33199 verify_uninitialized();
33200 switch( n ){
@@ -33401,13 +33232,19 @@
33232 data.openMode = SHELL_OPEN_DESERIALIZE;
33233 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
33234 data.szMax = integerValue(argv[++i]);
33235 #endif
33236 }else if( cli_strcmp(z,"-readonly")==0 ){
33237 data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
33238 data.openFlags |= SQLITE_OPEN_READONLY;
33239 }else if( cli_strcmp(z,"-nofollow")==0 ){
33240 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
33241 }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */
33242 data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
33243 }else if( cli_strcmp(z,"-ifexists")==0 ){
33244 data.openFlags &= ~(SQLITE_OPEN_CREATE);
33245 if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
33246 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
33247 }else if( cli_strncmp(z, "-A",2)==0 ){
33248 /* All remaining command-line arguments are passed to the ".archive"
33249 ** command, so ignore them */
33250 break;
@@ -33557,13 +33394,19 @@
33394 data.openMode = SHELL_OPEN_DESERIALIZE;
33395 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
33396 data.szMax = integerValue(argv[++i]);
33397 #endif
33398 }else if( cli_strcmp(z,"-readonly")==0 ){
33399 data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
33400 data.openFlags |= SQLITE_OPEN_READONLY;
33401 }else if( cli_strcmp(z,"-nofollow")==0 ){
33402 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
33403 }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */
33404 data.openFlags |= SQLITE_OPEN_EXCLUSIVE;
33405 }else if( cli_strcmp(z,"-ifexists")==0 ){
33406 data.openFlags &= ~(SQLITE_OPEN_CREATE);
33407 if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE;
33408 }else if( cli_strcmp(z,"-ascii")==0 ){
33409 data.mode = MODE_Ascii;
33410 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
33411 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
33412 }else if( cli_strcmp(z,"-tabs")==0 ){
@@ -33734,11 +33577,10 @@
33577 /* Run commands received from standard input
33578 */
33579 if( stdin_is_interactive ){
33580 char *zHome;
33581 char *zHistory;
 
33582 sqlite3_fprintf(stdout,
33583 "SQLite version %s %.19s\n" /*extra-version-info*/
33584 "Enter \".help\" for usage hints.\n",
33585 sqlite3_libversion(), sqlite3_sourceid());
33586 if( warnInmemoryDb ){
@@ -33747,15 +33589,19 @@
33589 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
33590 " persistent database.\n");
33591 }
33592 zHistory = getenv("SQLITE_HISTORY");
33593 if( zHistory ){
33594 zHistory = sqlite3_mprintf("%s", zHistory);
33595 shell_check_oom(zHistory);
33596 }else{
33597 zHistory = find_xdg_file("XDG_STATE_HOME",
33598 ".local/state",
33599 "sqlite_history");
33600 if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){
33601 zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome);
33602 shell_check_oom(zHistory);
33603 }
33604 }
33605 if( zHistory ){ shell_read_history(zHistory); }
33606 #if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
33607 rl_attempted_completion_function = readline_completion;
@@ -33767,21 +33613,21 @@
33613 data.in = 0;
33614 rc = process_input(&data);
33615 if( zHistory ){
33616 shell_stifle_history(2000);
33617 shell_write_history(zHistory);
33618 sqlite3_free(zHistory);
33619 }
33620 }else{
33621 data.in = stdin;
33622 rc = process_input(&data);
33623 }
33624 }
33625 #ifndef SQLITE_SHELL_FIDDLE
33626 /* In WASM mode we have to leave the db state in place so that
33627 ** client code can "push" SQL into it after this call returns. */
33628 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
33629 if( data.expert.pExpert ){
33630 expertFinish(&data, 1, 0);
33631 }
33632 #endif
33633 shell_main_exit:
33634
+2521 -708
--- 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
-** 9f184f8dfa5ef6d57e10376adc30e0060ced with changes in files:
21
+** 5cbccab499bc3983aac1f57355552db607de with changes in files:
2222
**
2323
**
2424
*/
2525
#ifndef SQLITE_AMALGAMATION
2626
#define SQLITE_CORE 1
@@ -168,11 +168,13 @@
168168
#define SQLITE_OMIT_LOAD_EXTENSION 1
169169
#define SQLITE_ENABLE_LOCKING_STYLE 0
170170
#define HAVE_UTIME 1
171171
#else
172172
/* This is not VxWorks. */
173
-#define OS_VXWORKS 0
173
+#ifndef OS_VXWORKS
174
+# define OS_VXWORKS 0
175
+#endif
174176
#define HAVE_FCHOWN 1
175177
#define HAVE_READLINK 1
176178
#define HAVE_LSTAT 1
177179
#endif /* defined(_WRS_KERNEL) */
178180
@@ -465,11 +467,14 @@
465467
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
466468
** [sqlite_version()] and [sqlite_source_id()].
467469
*/
468470
#define SQLITE_VERSION "3.51.0"
469471
#define SQLITE_VERSION_NUMBER 3051000
470
-#define SQLITE_SOURCE_ID "2025-07-15 19:00:01 9f184f8dfa5ef6d57e10376adc30e0060ceda07d283c23dfdfe3dbdd6608f839"
472
+#define SQLITE_SOURCE_ID "2025-10-15 10:52:45 5cbccab499bc3983aac1f57355552db607dee6c7ef4eb00d794dbee89c18db70"
473
+#define SQLITE_SCM_BRANCH "trunk"
474
+#define SQLITE_SCM_TAGS ""
475
+#define SQLITE_SCM_DATETIME "2025-10-15T10:52:45.276Z"
471476
472477
/*
473478
** CAPI3REF: Run-Time Library Version Numbers
474479
** KEYWORDS: sqlite3_version sqlite3_sourceid
475480
**
@@ -814,10 +819,13 @@
814819
** [sqlite3_extended_errcode()].
815820
*/
816821
#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8))
817822
#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
818823
#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
824
+#define SQLITE_ERROR_RESERVESIZE (SQLITE_ERROR | (4<<8))
825
+#define SQLITE_ERROR_KEY (SQLITE_ERROR | (5<<8))
826
+#define SQLITE_ERROR_UNABLE (SQLITE_ERROR | (6<<8))
819827
#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
820828
#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
821829
#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
822830
#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
823831
#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
@@ -848,10 +856,12 @@
848856
#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
849857
#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
850858
#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
851859
#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8))
852860
#define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8))
861
+#define SQLITE_IOERR_BADKEY (SQLITE_IOERR | (35<<8))
862
+#define SQLITE_IOERR_CODEC (SQLITE_IOERR | (36<<8))
853863
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
854864
#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
855865
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
856866
#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
857867
#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8))
@@ -2652,21 +2662,24 @@
26522662
** views in the main database schema or in the schemas of ATTACH-ed
26532663
** databases.)^ </dd>
26542664
**
26552665
** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
26562666
** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2657
-** <dd> ^This option is used to enable or disable the
2658
-** [fts3_tokenizer()] function which is part of the
2659
-** [FTS3] full-text search engine extension.
2660
-** There must be two additional arguments.
2661
-** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2662
-** positive to enable fts3_tokenizer() or negative to leave the setting
2663
-** unchanged.
2664
-** The second parameter is a pointer to an integer into which
2665
-** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2666
-** following this call. The second parameter may be a NULL pointer, in
2667
-** which case the new setting is not reported back. </dd>
2667
+** <dd> ^This option is used to enable or disable using the
2668
+** [fts3_tokenizer()] function - part of the [FTS3] full-text search engine
2669
+** extension - without using bound parameters as the parameters. Doing so
2670
+** is disabled by default. There must be two additional arguments. The first
2671
+** argument is an integer. If it is passed 0, then using fts3_tokenizer()
2672
+** without bound parameters is disabled. If it is passed a positive value,
2673
+** then calling fts3_tokenizer without bound parameters is enabled. If it
2674
+** is passed a negative value, this setting is not modified - this can be
2675
+** used to query for the current setting. The second parameter is a pointer
2676
+** to an integer into which is written 0 or 1 to indicate the current value
2677
+** of this setting (after it is modified, if applicable). The second
2678
+** parameter may be a NULL pointer, in which case the value of the setting
2679
+** is not reported back. Refer to [FTS3] documentation for further details.
2680
+** </dd>
26682681
**
26692682
** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
26702683
** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
26712684
** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
26722685
** interface independently of the [load_extension()] SQL function.
@@ -4512,10 +4525,38 @@
45124525
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
45134526
SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
45144527
SQLITE_API const char *sqlite3_errstr(int);
45154528
SQLITE_API int sqlite3_error_offset(sqlite3 *db);
45164529
4530
+/*
4531
+** CAPI3REF: Set Error Codes And Message
4532
+** METHOD: sqlite3
4533
+**
4534
+** Set the error code of the database handle passed as the first argument
4535
+** to errcode, and the error message to a copy of nul-terminated string
4536
+** zErrMsg. If zErrMsg is passed NULL, then the error message is set to
4537
+** the default message associated with the supplied error code. Subsequent
4538
+** calls to [sqlite3_errcode()] and [sqlite3_errmsg()] and similar will
4539
+** return the values set by this routine in place of what was previously
4540
+** set by SQLite itself.
4541
+**
4542
+** This function returns SQLITE_OK if the error code and error message are
4543
+** successfully set, SQLITE_NOMEM if an OOM occurs, and SQLITE_MISUSE if
4544
+** the database handle is NULL or invalid.
4545
+**
4546
+** The error code and message set by this routine remains in effect until
4547
+** they are changed, either by another call to this routine or until they are
4548
+** changed to by SQLite itself to reflect the result of some subsquent
4549
+** API call.
4550
+**
4551
+** This function is intended for use by SQLite extensions or wrappers. The
4552
+** idea is that an extension or wrapper can use this routine to set error
4553
+** messages and error codes and thus behave more like a core SQLite
4554
+** feature from the point of view of an application.
4555
+*/
4556
+SQLITE_API int sqlite3_set_errmsg(sqlite3 *db, int errcode, const char *zErrMsg);
4557
+
45174558
/*
45184559
** CAPI3REF: Prepared Statement Object
45194560
** KEYWORDS: {prepared statement} {prepared statements}
45204561
**
45214562
** An instance of this object represents a single SQL statement that
@@ -6522,10 +6563,11 @@
65226563
** to be attached to [database connection] D using name N. Subsequent
65236564
** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P
65246565
** or a NULL pointer if there were no prior calls to
65256566
** sqlite3_set_clientdata() with the same values of D and N.
65266567
** Names are compared using strcmp() and are thus case sensitive.
6568
+** It returns 0 on success and SQLITE_NOMEM on allocation failure.
65276569
**
65286570
** If P and X are both non-NULL, then the destructor X is invoked with
65296571
** argument P on the first of the following occurrences:
65306572
** <ul>
65316573
** <li> An out-of-memory error occurs during the call to
@@ -9197,14 +9239,23 @@
91979239
** the resetFlg is true, then the highest instantaneous value is
91989240
** reset back down to the current value.
91999241
**
92009242
** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
92019243
** non-zero [error code] on failure.
9244
+**
9245
+** ^The sqlite3_db_status64(D,O,C,H,R) routine works exactly the same
9246
+** way as sqlite3_db_status(D,O,C,H,R) routine except that the C and H
9247
+** parameters are pointer to 64-bit integers (type: sqlite3_int64) instead
9248
+** of pointers to 32-bit integers, which allows larger status values
9249
+** to be returned. If a status value exceeds 2,147,483,647 then
9250
+** sqlite3_db_status() will truncate the value whereas sqlite3_db_status64()
9251
+** will return the full value.
92029252
**
92039253
** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
92049254
*/
92059255
SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
9256
+SQLITE_API int sqlite3_db_status64(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
92069257
92079258
/*
92089259
** CAPI3REF: Status Parameters for database connections
92099260
** KEYWORDS: {SQLITE_DBSTATUS options}
92109261
**
@@ -9297,10 +9348,14 @@
92979348
** database file in rollback mode databases. Any pages written as part of
92989349
** transaction rollback or database recovery operations are not included.
92999350
** If an IO or other error occurs while writing a page to disk, the effect
93009351
** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
93019352
** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
9353
+** <p>
9354
+** ^(There is overlap between the quantities measured by this parameter
9355
+** (SQLITE_DBSTATUS_CACHE_WRITE) and SQLITE_DBSTATUS_TEMPBUF_SPILL.
9356
+** Resetting one will reduce the other.)^
93029357
** </dd>
93039358
**
93049359
** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
93059360
** <dd>This parameter returns the number of dirty cache entries that have
93069361
** been written to disk in the middle of a transaction due to the page
@@ -9312,10 +9367,22 @@
93129367
**
93139368
** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
93149369
** <dd>This parameter returns zero for the current value if and only if
93159370
** all foreign key constraints (deferred or immediate) have been
93169371
** resolved.)^ ^The highwater mark is always 0.
9372
+**
9373
+** [[SQLITE_DBSTATUS_TEMPBUF_SPILL] ^(<dt>SQLITE_DBSTATUS_TEMPBUF_SPILL</dt>
9374
+** <dd>^(This parameter returns the number of bytes written to temporary
9375
+** files on disk that could have been kept in memory had sufficient memory
9376
+** been available. This value includes writes to intermediate tables that
9377
+** are part of complex queries, external sorts that spill to disk, and
9378
+** writes to TEMP tables.)^
9379
+** ^The highwater mark is always 0.
9380
+** <p>
9381
+** ^(There is overlap between the quantities measured by this parameter
9382
+** (SQLITE_DBSTATUS_TEMPBUF_SPILL) and SQLITE_DBSTATUS_CACHE_WRITE.
9383
+** Resetting one will reduce the other.)^
93179384
** </dd>
93189385
** </dl>
93199386
*/
93209387
#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
93219388
#define SQLITE_DBSTATUS_CACHE_USED 1
@@ -9328,11 +9395,12 @@
93289395
#define SQLITE_DBSTATUS_CACHE_MISS 8
93299396
#define SQLITE_DBSTATUS_CACHE_WRITE 9
93309397
#define SQLITE_DBSTATUS_DEFERRED_FKS 10
93319398
#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
93329399
#define SQLITE_DBSTATUS_CACHE_SPILL 12
9333
-#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
9400
+#define SQLITE_DBSTATUS_TEMPBUF_SPILL 13
9401
+#define SQLITE_DBSTATUS_MAX 13 /* Largest defined DBSTATUS */
93349402
93359403
93369404
/*
93379405
** CAPI3REF: Prepared Statement Status
93389406
** METHOD: sqlite3_stmt
@@ -10093,25 +10161,38 @@
1009310161
** ^The third parameter is the name of the database that was written to -
1009410162
** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
1009510163
** is the number of pages currently in the write-ahead log file,
1009610164
** including those that were just committed.
1009710165
**
10098
-** The callback function should normally return [SQLITE_OK]. ^If an error
10166
+** ^The callback function should normally return [SQLITE_OK]. ^If an error
1009910167
** code is returned, that error will propagate back up through the
1010010168
** SQLite code base to cause the statement that provoked the callback
1010110169
** to report an error, though the commit will have still occurred. If the
1010210170
** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
1010310171
** that does not correspond to any valid SQLite error code, the results
1010410172
** are undefined.
1010510173
**
10106
-** A single database handle may have at most a single write-ahead log callback
10107
-** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
10108
-** previously registered write-ahead log callback. ^The return value is
10109
-** a copy of the third parameter from the previous call, if any, or 0.
10110
-** ^Note that the [sqlite3_wal_autocheckpoint()] interface and the
10111
-** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
10112
-** overwrite any prior [sqlite3_wal_hook()] settings.
10174
+** ^A single database handle may have at most a single write-ahead log
10175
+** callback registered at one time. ^Calling [sqlite3_wal_hook()]
10176
+** replaces the default behavior or previously registered write-ahead
10177
+** log callback.
10178
+**
10179
+** ^The return value is a copy of the third parameter from the
10180
+** previous call, if any, or 0.
10181
+**
10182
+** ^The [sqlite3_wal_autocheckpoint()] interface and the
10183
+** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and
10184
+** will overwrite any prior [sqlite3_wal_hook()] settings.
10185
+**
10186
+** ^If a write-ahead log callback is set using this function then
10187
+** [sqlite3_wal_checkpoint_v2()] or [PRAGMA wal_checkpoint]
10188
+** should be invoked periodically to keep the write-ahead log file
10189
+** from growing without bound.
10190
+**
10191
+** ^Passing a NULL pointer for the callback disables automatic
10192
+** checkpointing entirely. To re-enable the default behavior, call
10193
+** sqlite3_wal_autocheckpoint(db,1000) or use [PRAGMA wal_checkpoint].
1011310194
*/
1011410195
SQLITE_API void *sqlite3_wal_hook(
1011510196
sqlite3*,
1011610197
int(*)(void *,sqlite3*,const char*,int),
1011710198
void*
@@ -10124,11 +10205,11 @@
1012410205
** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
1012510206
** [sqlite3_wal_hook()] that causes any database on [database connection] D
1012610207
** to automatically [checkpoint]
1012710208
** after committing a transaction if there are N or
1012810209
** more frames in the [write-ahead log] file. ^Passing zero or
10129
-** a negative value as the nFrame parameter disables automatic
10210
+** a negative value as the N parameter disables automatic
1013010211
** checkpoints entirely.
1013110212
**
1013210213
** ^The callback registered by this function replaces any existing callback
1013310214
** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
1013410215
** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
@@ -10140,13 +10221,14 @@
1014010221
** ^Checkpoints initiated by this mechanism are
1014110222
** [sqlite3_wal_checkpoint_v2|PASSIVE].
1014210223
**
1014310224
** ^Every new [database connection] defaults to having the auto-checkpoint
1014410225
** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
10145
-** pages. The use of this interface
10146
-** is only necessary if the default setting is found to be suboptimal
10147
-** for a particular application.
10226
+** pages.
10227
+**
10228
+** ^The use of this interface is only necessary if the default setting
10229
+** is found to be suboptimal for a particular application.
1014810230
*/
1014910231
SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
1015010232
1015110233
/*
1015210234
** CAPI3REF: Checkpoint a database
@@ -10207,10 +10289,15 @@
1020710289
**
1020810290
** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
1020910291
** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
1021010292
** addition that it also truncates the log file to zero bytes just prior
1021110293
** to a successful return.
10294
+**
10295
+** <dt>SQLITE_CHECKPOINT_NOOP<dd>
10296
+** ^This mode always checkpoints zero frames. The only reason to invoke
10297
+** a NOOP checkpoint is to access the values returned by
10298
+** sqlite3_wal_checkpoint_v2() via output parameters *pnLog and *pnCkpt.
1021210299
** </dl>
1021310300
**
1021410301
** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
1021510302
** the log file or to -1 if the checkpoint could not run because
1021610303
** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
@@ -10277,10 +10364,11 @@
1027710364
** These constants define all valid values for the "checkpoint mode" passed
1027810365
** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
1027910366
** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
1028010367
** meaning of each of these checkpoint modes.
1028110368
*/
10369
+#define SQLITE_CHECKPOINT_NOOP -1 /* Do no work at all */
1028210370
#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
1028310371
#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
1028410372
#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */
1028510373
#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
1028610374
@@ -11104,11 +11192,11 @@
1110411192
** to avoid a memory leak.
1110511193
**
1110611194
** The [sqlite3_snapshot_get()] interface is only available when the
1110711195
** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
1110811196
*/
11109
-SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
11197
+SQLITE_API int sqlite3_snapshot_get(
1111011198
sqlite3 *db,
1111111199
const char *zSchema,
1111211200
sqlite3_snapshot **ppSnapshot
1111311201
);
1111411202
@@ -11153,11 +11241,11 @@
1115311241
** database connection in order to make it ready to use snapshots.)
1115411242
**
1115511243
** The [sqlite3_snapshot_open()] interface is only available when the
1115611244
** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
1115711245
*/
11158
-SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
11246
+SQLITE_API int sqlite3_snapshot_open(
1115911247
sqlite3 *db,
1116011248
const char *zSchema,
1116111249
sqlite3_snapshot *pSnapshot
1116211250
);
1116311251
@@ -11170,11 +11258,11 @@
1117011258
** using this routine to avoid a memory leak.
1117111259
**
1117211260
** The [sqlite3_snapshot_free()] interface is only available when the
1117311261
** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
1117411262
*/
11175
-SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
11263
+SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot*);
1117611264
1117711265
/*
1117811266
** CAPI3REF: Compare the ages of two snapshot handles.
1117911267
** METHOD: sqlite3_snapshot
1118011268
**
@@ -11197,11 +11285,11 @@
1119711285
** snapshot, and a positive value if P1 is a newer snapshot than P2.
1119811286
**
1119911287
** This interface is only available if SQLite is compiled with the
1120011288
** [SQLITE_ENABLE_SNAPSHOT] option.
1120111289
*/
11202
-SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
11290
+SQLITE_API int sqlite3_snapshot_cmp(
1120311291
sqlite3_snapshot *p1,
1120411292
sqlite3_snapshot *p2
1120511293
);
1120611294
1120711295
/*
@@ -11225,11 +11313,11 @@
1122511313
** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
1122611314
**
1122711315
** This interface is only available if SQLite is compiled with the
1122811316
** [SQLITE_ENABLE_SNAPSHOT] option.
1122911317
*/
11230
-SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
11318
+SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
1123111319
1123211320
/*
1123311321
** CAPI3REF: Serialize a database
1123411322
**
1123511323
** The sqlite3_serialize(D,S,P,F) interface returns a pointer to
@@ -11299,16 +11387,17 @@
1129911387
/*
1130011388
** CAPI3REF: Deserialize a database
1130111389
**
1130211390
** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
1130311391
** [database connection] D to disconnect from database S and then
11304
-** reopen S as an in-memory database based on the serialization contained
11305
-** in P. The serialized database P is N bytes in size. M is the size of
11306
-** the buffer P, which might be larger than N. If M is larger than N, and
11307
-** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
11308
-** permitted to add content to the in-memory database as long as the total
11309
-** size does not exceed M bytes.
11392
+** reopen S as an in-memory database based on the serialization
11393
+** contained in P. If S is a NULL pointer, the main database is
11394
+** used. The serialized database P is N bytes in size. M is the size
11395
+** of the buffer P, which might be larger than N. If M is larger than
11396
+** N, and the SQLITE_DESERIALIZE_READONLY bit is not set in F, then
11397
+** SQLite is permitted to add content to the in-memory database as
11398
+** long as the total size does not exceed M bytes.
1131011399
**
1131111400
** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
1131211401
** invoke sqlite3_free() on the serialization buffer when the database
1131311402
** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
1131411403
** SQLite will try to increase the buffer size using sqlite3_realloc64()
@@ -11371,10 +11460,56 @@
1137111460
*/
1137211461
#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
1137311462
#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
1137411463
#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
1137511464
11465
+/*
11466
+** CAPI3REF: Bind array values to the CARRAY table-valued function
11467
+**
11468
+** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to
11469
+** one of the first argument of the [carray() table-valued function]. The
11470
+** S parameter is a pointer to the [prepared statement] that uses the carray()
11471
+** functions. I is the parameter index to be bound. P is a pointer to the
11472
+** array to be bound, and N is the number of eements in the array. The
11473
+** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64],
11474
+** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to
11475
+** indicate the datatype of the array being bound. The X argument is not a
11476
+** NULL pointer, then SQLite will invoke the function X on the P parameter
11477
+** after it has finished using P.
11478
+*/
11479
+SQLITE_API SQLITE_API int sqlite3_carray_bind(
11480
+ sqlite3_stmt *pStmt, /* Statement to be bound */
11481
+ int i, /* Parameter index */
11482
+ void *aData, /* Pointer to array data */
11483
+ int nData, /* Number of data elements */
11484
+ int mFlags, /* CARRAY flags */
11485
+ void (*xDel)(void*) /* Destructor for aData */
11486
+);
11487
+
11488
+/*
11489
+** CAPI3REF: Datatypes for the CARRAY table-valued funtion
11490
+**
11491
+** The fifth argument to the [sqlite3_carray_bind()] interface musts be
11492
+** one of the following constants, to specify the datatype of the array
11493
+** that is being bound into the [carray table-valued function].
11494
+*/
11495
+#define SQLITE_CARRAY_INT32 0 /* Data is 32-bit signed integers */
11496
+#define SQLITE_CARRAY_INT64 1 /* Data is 64-bit signed integers */
11497
+#define SQLITE_CARRAY_DOUBLE 2 /* Data is doubles */
11498
+#define SQLITE_CARRAY_TEXT 3 /* Data is char* */
11499
+#define SQLITE_CARRAY_BLOB 4 /* Data is struct iovec */
11500
+
11501
+/*
11502
+** Versions of the above #defines that omit the initial SQLITE_, for
11503
+** legacy compatibility.
11504
+*/
11505
+#define CARRAY_INT32 0 /* Data is 32-bit signed integers */
11506
+#define CARRAY_INT64 1 /* Data is 64-bit signed integers */
11507
+#define CARRAY_DOUBLE 2 /* Data is doubles */
11508
+#define CARRAY_TEXT 3 /* Data is char* */
11509
+#define CARRAY_BLOB 4 /* Data is struct iovec */
11510
+
1137611511
/*
1137711512
** Undo the hack that converts floating point types to integer for
1137811513
** builds on processors without floating point support.
1137911514
*/
1138011515
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -12629,10 +12764,19 @@
1262912764
** CAPI3REF: Apply A Changeset To A Database
1263012765
**
1263112766
** Apply a changeset or patchset to a database. These functions attempt to
1263212767
** update the "main" database attached to handle db with the changes found in
1263312768
** the changeset passed via the second and third arguments.
12769
+**
12770
+** All changes made by these functions are enclosed in a savepoint transaction.
12771
+** If any other error (aside from a constraint failure when attempting to
12772
+** write to the target database) occurs, then the savepoint transaction is
12773
+** rolled back, restoring the target database to its original state, and an
12774
+** SQLite error code returned. Additionally, starting with version 3.51.0,
12775
+** an error code and error message that may be accessed using the
12776
+** [sqlite3_errcode()] and [sqlite3_errmsg()] APIs are left in the database
12777
+** handle.
1263412778
**
1263512779
** The fourth argument (xFilter) passed to these functions is the "filter
1263612780
** callback". This may be passed NULL, in which case all changes in the
1263712781
** changeset are applied to the database. For sqlite3changeset_apply() and
1263812782
** sqlite3_changeset_apply_v2(), if it is not NULL, then it is invoked once
@@ -12767,16 +12911,10 @@
1276712911
** It is safe to execute SQL statements, including those that write to the
1276812912
** table that the callback related to, from within the xConflict callback.
1276912913
** This can be used to further customize the application's conflict
1277012914
** resolution strategy.
1277112915
**
12772
-** All changes made by these functions are enclosed in a savepoint transaction.
12773
-** If any other error (aside from a constraint failure when attempting to
12774
-** write to the target database) occurs, then the savepoint transaction is
12775
-** rolled back, restoring the target database to its original state, and an
12776
-** SQLite error code returned.
12777
-**
1277812916
** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
1277912917
** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
1278012918
** may set (*ppRebase) to point to a "rebase" that may be used with the
1278112919
** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
1278212920
** is set to the size of the buffer in bytes. It is the responsibility of the
@@ -14351,11 +14489,11 @@
1435114489
1435214490
/*
1435314491
** Maximum number of pages in one database file.
1435414492
**
1435514493
** This is really just the default value for the max_page_count pragma.
14356
-** This value can be lowered (or raised) at run-time using that the
14494
+** This value can be lowered (or raised) at run-time using the
1435714495
** max_page_count macro.
1435814496
*/
1435914497
#ifndef SQLITE_MAX_PAGE_COUNT
1436014498
# define SQLITE_MAX_PAGE_COUNT 0xfffffffe /* 4294967294 */
1436114499
#endif
@@ -15947,12 +16085,12 @@
1594716085
**
1594816086
** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
1594916087
** must provide its own VFS implementation together with sqlite3_os_init()
1595016088
** and sqlite3_os_end() routines.
1595116089
*/
15952
-#if !defined(SQLITE_OS_KV) && !defined(SQLITE_OS_OTHER) && \
15953
- !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_WIN)
16090
+#if SQLITE_OS_KV+1<=1 && SQLITE_OS_OTHER+1<=1 && \
16091
+ SQLITE_OS_WIN+1<=1 && SQLITE_OS_UNIX+1<=1
1595416092
# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
1595516093
defined(__MINGW32__) || defined(__BORLANDC__)
1595616094
# define SQLITE_OS_WIN 1
1595716095
# define SQLITE_OS_UNIX 0
1595816096
# else
@@ -17450,10 +17588,13 @@
1745017588
#ifndef SQLITE_OMIT_TRACE
1745117589
SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
1745217590
#endif
1745317591
SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1745417592
SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
17593
+#ifdef SQLITE_ENABLE_PERCENTILE
17594
+SQLITE_PRIVATE const char *sqlite3VdbeFuncName(const sqlite3_context*);
17595
+#endif
1745517596
1745617597
SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(int,const void*,UnpackedRecord*);
1745717598
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
1745817599
SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
1745917600
SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
@@ -18122,11 +18263,11 @@
1812218263
struct sqlite3InitInfo { /* Information used during initialization */
1812318264
Pgno newTnum; /* Rootpage of table being initialized */
1812418265
u8 iDb; /* Which db file is being initialized */
1812518266
u8 busy; /* TRUE if currently initializing */
1812618267
unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
18127
- unsigned imposterTable : 1; /* Building an imposter table */
18268
+ unsigned imposterTable : 2; /* Building an imposter table */
1812818269
unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
1812918270
const char **azInit; /* "type", "name", and "tbl_name" columns */
1813018271
} init;
1813118272
int nVdbeActive; /* Number of VDBEs currently running */
1813218273
int nVdbeRead; /* Number of active VDBEs that read or write */
@@ -18205,10 +18346,11 @@
1820518346
int nStatement; /* Number of nested statement-transactions */
1820618347
i64 nDeferredCons; /* Net deferred constraints this transaction. */
1820718348
i64 nDeferredImmCons; /* Net deferred immediate constraints */
1820818349
int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
1820918350
DbClientData *pDbData; /* sqlite3_set_clientdata() content */
18351
+ u64 nSpill; /* TEMP content spilled to disk */
1821018352
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1821118353
/* The following variables are all protected by the STATIC_MAIN
1821218354
** mutex, not by sqlite3.mutex. They are used by code in notify.c.
1821318355
**
1821418356
** When X.pUnlockConnection==Y, that means that X is waiting for Y to
@@ -18915,10 +19057,11 @@
1891519057
#define TF_Shadow 0x00001000 /* True for a shadow table */
1891619058
#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */
1891719059
#define TF_Ephemeral 0x00004000 /* An ephemeral table */
1891819060
#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
1891919061
#define TF_Strict 0x00010000 /* STRICT mode */
19062
+#define TF_Imposter 0x00020000 /* An imposter table */
1892019063
1892119064
/*
1892219065
** Allowed values for Table.eTabType
1892319066
*/
1892419067
#define TABTYP_NORM 0 /* Ordinary table */
@@ -19503,10 +19646,11 @@
1950319646
AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
1950419647
union {
1950519648
Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
1950619649
** for a column of an index on an expression */
1950719650
Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */
19651
+ int nReg; /* TK_NULLS: Number of registers to NULL out */
1950819652
struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
1950919653
int iAddr; /* Subroutine entry address */
1951019654
int regReturn; /* Register used to hold return address */
1951119655
} sub;
1951219656
} y;
@@ -20080,10 +20224,11 @@
2008020224
#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */
2008120225
#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */
2008220226
#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */
2008320227
#define SF_UpdateFrom 0x10000000 /* Query originates with UPDATE FROM */
2008420228
#define SF_Correlated 0x20000000 /* True if references the outer context */
20229
+#define SF_OnToWhere 0x40000000 /* One or more ON clauses moved to WHERE */
2008520230
2008620231
/* True if SrcItem X is a subquery that has SF_NestedFrom */
2008720232
#define IsNestedFrom(X) \
2008820233
((X)->fg.isSubquery && \
2008920234
((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom)!=0)
@@ -20833,10 +20978,11 @@
2083320978
struct Table *pTab; /* Table of generated column */
2083420979
struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */
2083520980
SrcItem *pSrcItem; /* A single FROM clause item */
2083620981
DbFixer *pFix; /* See sqlite3FixSelect() */
2083720982
Mem *aMem; /* See sqlite3BtreeCursorHint() */
20983
+ struct CheckOnCtx *pCheckOnCtx; /* See selectCheckOnClauses() */
2083820984
} u;
2083920985
};
2084020986
2084120987
/*
2084220988
** The following structure contains information used by the sqliteFix...
@@ -21540,10 +21686,11 @@
2154021686
SQLITE_PRIVATE void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int);
2154121687
#endif
2154221688
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
2154321689
SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
2154421690
SQLITE_PRIVATE int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int);
21691
+SQLITE_PRIVATE void sqlite3ExprNullRegisterRange(Parse*, int, int);
2154521692
SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
2154621693
SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
2154721694
SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
2154821695
#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
2154921696
#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
@@ -21636,16 +21783,20 @@
2163621783
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
2163721784
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
2163821785
SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
2163921786
SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
2164021787
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
21641
-SQLITE_PRIVATE int sqlite3JsonTableFunctions(sqlite3*);
21788
+SQLITE_PRIVATE Module *sqlite3JsonVtabRegister(sqlite3*,const char*);
2164221789
#endif
2164321790
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
2164421791
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
2164521792
SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
2164621793
SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p);
21794
+
21795
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY)
21796
+SQLITE_PRIVATE Module *sqlite3CarrayRegister(sqlite3*);
21797
+#endif
2164721798
2164821799
#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
2164921800
SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
2165021801
#endif
2165121802
@@ -22628,10 +22779,13 @@
2262822779
#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
2262922780
"ENABLE_BATCH_ATOMIC_WRITE",
2263022781
#endif
2263122782
#ifdef SQLITE_ENABLE_BYTECODE_VTAB
2263222783
"ENABLE_BYTECODE_VTAB",
22784
+#endif
22785
+#ifdef SQLITE_ENABLE_CARRAY
22786
+ "ENABLE_CARRAY",
2263322787
#endif
2263422788
#ifdef SQLITE_ENABLE_CEROD
2263522789
"ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
2263622790
#endif
2263722791
#ifdef SQLITE_ENABLE_COLUMN_METADATA
@@ -22718,10 +22872,13 @@
2271822872
#ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES
2271922873
"ENABLE_ORDERED_SET_AGGREGATES",
2272022874
#endif
2272122875
#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
2272222876
"ENABLE_OVERSIZE_CELL_CHECK",
22877
+#endif
22878
+#ifdef SQLITE_ENABLE_PERCENTILE
22879
+ "ENABLE_PERCENTILE",
2272322880
#endif
2272422881
#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
2272522882
"ENABLE_PREUPDATE_HOOK",
2272622883
#endif
2272722884
#ifdef SQLITE_ENABLE_QPSG
@@ -24202,11 +24359,14 @@
2420224359
Mem oldipk; /* Memory cell holding "old" IPK value */
2420324360
Mem *aNew; /* Array of new.* values */
2420424361
Table *pTab; /* Schema object being updated */
2420524362
Index *pPk; /* PK index if pTab is WITHOUT ROWID */
2420624363
sqlite3_value **apDflt; /* Array of default values, if required */
24207
- u8 keyinfoSpace[SZ_KEYINFO_0]; /* Space to hold pKeyinfo[0] content */
24364
+ union {
24365
+ KeyInfo sKey;
24366
+ u8 keyinfoSpace[SZ_KEYINFO_0]; /* Space to hold pKeyinfo[0] content */
24367
+ } uKey;
2420824368
};
2420924369
2421024370
/*
2421124371
** An instance of this object is used to pass an vector of values into
2421224372
** OP_VFilter, the xFilter method of a virtual table. The vector is the
@@ -24366,13 +24526,15 @@
2436624526
SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
2436724527
SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
2436824528
#endif
2436924529
2437024530
#ifndef SQLITE_OMIT_FOREIGN_KEY
24371
-SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
24531
+SQLITE_PRIVATE int sqlite3VdbeCheckFkImmediate(Vdbe*);
24532
+SQLITE_PRIVATE int sqlite3VdbeCheckFkDeferred(Vdbe*);
2437224533
#else
24373
-# define sqlite3VdbeCheckFk(p,i) 0
24534
+# define sqlite3VdbeCheckFkImmediate(p) 0
24535
+# define sqlite3VdbeCheckFkDeferred(p) 0
2437424536
#endif
2437524537
2437624538
#ifdef SQLITE_DEBUG
2437724539
SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
2437824540
SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr);
@@ -24577,27 +24739,29 @@
2457724739
}
2457824740
2457924741
/*
2458024742
** Query status information for a single database connection
2458124743
*/
24582
-SQLITE_API int sqlite3_db_status(
24583
- sqlite3 *db, /* The database connection whose status is desired */
24584
- int op, /* Status verb */
24585
- int *pCurrent, /* Write current value here */
24586
- int *pHighwater, /* Write high-water mark here */
24587
- int resetFlag /* Reset high-water mark if true */
24744
+SQLITE_API int sqlite3_db_status64(
24745
+ sqlite3 *db, /* The database connection whose status is desired */
24746
+ int op, /* Status verb */
24747
+ sqlite3_int64 *pCurrent, /* Write current value here */
24748
+ sqlite3_int64 *pHighwtr, /* Write high-water mark here */
24749
+ int resetFlag /* Reset high-water mark if true */
2458824750
){
2458924751
int rc = SQLITE_OK; /* Return code */
2459024752
#ifdef SQLITE_ENABLE_API_ARMOR
24591
- if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
24753
+ if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){
2459224754
return SQLITE_MISUSE_BKPT;
2459324755
}
2459424756
#endif
2459524757
sqlite3_mutex_enter(db->mutex);
2459624758
switch( op ){
2459724759
case SQLITE_DBSTATUS_LOOKASIDE_USED: {
24598
- *pCurrent = sqlite3LookasideUsed(db, pHighwater);
24760
+ int H = 0;
24761
+ *pCurrent = sqlite3LookasideUsed(db, &H);
24762
+ *pHighwtr = H;
2459924763
if( resetFlag ){
2460024764
LookasideSlot *p = db->lookaside.pFree;
2460124765
if( p ){
2460224766
while( p->pNext ) p = p->pNext;
2460324767
p->pNext = db->lookaside.pInit;
@@ -24624,11 +24788,11 @@
2462424788
testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
2462524789
testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
2462624790
assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
2462724791
assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
2462824792
*pCurrent = 0;
24629
- *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
24793
+ *pHighwtr = db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
2463024794
if( resetFlag ){
2463124795
db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
2463224796
}
2463324797
break;
2463424798
}
@@ -24638,11 +24802,11 @@
2463824802
** by all pagers associated with the given database connection. The
2463924803
** highwater mark is meaningless and is returned as zero.
2464024804
*/
2464124805
case SQLITE_DBSTATUS_CACHE_USED_SHARED:
2464224806
case SQLITE_DBSTATUS_CACHE_USED: {
24643
- int totalUsed = 0;
24807
+ sqlite3_int64 totalUsed = 0;
2464424808
int i;
2464524809
sqlite3BtreeEnterAll(db);
2464624810
for(i=0; i<db->nDb; i++){
2464724811
Btree *pBt = db->aDb[i].pBt;
2464824812
if( pBt ){
@@ -24654,22 +24818,22 @@
2465424818
totalUsed += nByte;
2465524819
}
2465624820
}
2465724821
sqlite3BtreeLeaveAll(db);
2465824822
*pCurrent = totalUsed;
24659
- *pHighwater = 0;
24823
+ *pHighwtr = 0;
2466024824
break;
2466124825
}
2466224826
2466324827
/*
2466424828
** *pCurrent gets an accurate estimate of the amount of memory used
2466524829
** to store the schema for all databases (main, temp, and any ATTACHed
24666
- ** databases. *pHighwater is set to zero.
24830
+ ** databases. *pHighwtr is set to zero.
2466724831
*/
2466824832
case SQLITE_DBSTATUS_SCHEMA_USED: {
24669
- int i; /* Used to iterate through schemas */
24670
- int nByte = 0; /* Used to accumulate return value */
24833
+ int i; /* Used to iterate through schemas */
24834
+ int nByte = 0; /* Used to accumulate return value */
2467124835
2467224836
sqlite3BtreeEnterAll(db);
2467324837
db->pnBytesFreed = &nByte;
2467424838
assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
2467524839
db->lookaside.pEnd = db->lookaside.pStart;
@@ -24699,19 +24863,19 @@
2469924863
}
2470024864
db->pnBytesFreed = 0;
2470124865
db->lookaside.pEnd = db->lookaside.pTrueEnd;
2470224866
sqlite3BtreeLeaveAll(db);
2470324867
24704
- *pHighwater = 0;
24868
+ *pHighwtr = 0;
2470524869
*pCurrent = nByte;
2470624870
break;
2470724871
}
2470824872
2470924873
/*
2471024874
** *pCurrent gets an accurate estimate of the amount of memory used
2471124875
** to store all prepared statements.
24712
- ** *pHighwater is set to zero.
24876
+ ** *pHighwtr is set to zero.
2471324877
*/
2471424878
case SQLITE_DBSTATUS_STMT_USED: {
2471524879
struct Vdbe *pVdbe; /* Used to iterate through VMs */
2471624880
int nByte = 0; /* Used to accumulate return value */
2471724881
@@ -24722,19 +24886,19 @@
2472224886
sqlite3VdbeDelete(pVdbe);
2472324887
}
2472424888
db->lookaside.pEnd = db->lookaside.pTrueEnd;
2472524889
db->pnBytesFreed = 0;
2472624890
24727
- *pHighwater = 0; /* IMP: R-64479-57858 */
24891
+ *pHighwtr = 0; /* IMP: R-64479-57858 */
2472824892
*pCurrent = nByte;
2472924893
2473024894
break;
2473124895
}
2473224896
2473324897
/*
2473424898
** Set *pCurrent to the total cache hits or misses encountered by all
24735
- ** pagers the database handle is connected to. *pHighwater is always set
24899
+ ** pagers the database handle is connected to. *pHighwtr is always set
2473624900
** to zero.
2473724901
*/
2473824902
case SQLITE_DBSTATUS_CACHE_SPILL:
2473924903
op = SQLITE_DBSTATUS_CACHE_WRITE+1;
2474024904
/* no break */ deliberate_fall_through
@@ -24750,23 +24914,43 @@
2475024914
if( db->aDb[i].pBt ){
2475124915
Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
2475224916
sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
2475324917
}
2475424918
}
24755
- *pHighwater = 0; /* IMP: R-42420-56072 */
24919
+ *pHighwtr = 0; /* IMP: R-42420-56072 */
2475624920
/* IMP: R-54100-20147 */
2475724921
/* IMP: R-29431-39229 */
24758
- *pCurrent = (int)nRet & 0x7fffffff;
24922
+ *pCurrent = nRet;
24923
+ break;
24924
+ }
24925
+
24926
+ /* Set *pCurrent to the number of bytes that the db database connection
24927
+ ** has spilled to the filesystem in temporary files that could have been
24928
+ ** stored in memory, had sufficient memory been available.
24929
+ ** The *pHighwater is always set to zero.
24930
+ */
24931
+ case SQLITE_DBSTATUS_TEMPBUF_SPILL: {
24932
+ u64 nRet = 0;
24933
+ if( db->aDb[1].pBt ){
24934
+ Pager *pPager = sqlite3BtreePager(db->aDb[1].pBt);
24935
+ sqlite3PagerCacheStat(pPager, SQLITE_DBSTATUS_CACHE_WRITE,
24936
+ resetFlag, &nRet);
24937
+ nRet *= sqlite3BtreeGetPageSize(db->aDb[1].pBt);
24938
+ }
24939
+ nRet += db->nSpill;
24940
+ if( resetFlag ) db->nSpill = 0;
24941
+ *pHighwtr = 0;
24942
+ *pCurrent = nRet;
2475924943
break;
2476024944
}
2476124945
2476224946
/* Set *pCurrent to non-zero if there are unresolved deferred foreign
2476324947
** key constraints. Set *pCurrent to zero if all foreign key constraints
24764
- ** have been satisfied. The *pHighwater is always set to zero.
24948
+ ** have been satisfied. The *pHighwtr is always set to zero.
2476524949
*/
2476624950
case SQLITE_DBSTATUS_DEFERRED_FKS: {
24767
- *pHighwater = 0; /* IMP: R-11967-56545 */
24951
+ *pHighwtr = 0; /* IMP: R-11967-56545 */
2476824952
*pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
2476924953
break;
2477024954
}
2477124955
2477224956
default: {
@@ -24774,10 +24958,35 @@
2477424958
}
2477524959
}
2477624960
sqlite3_mutex_leave(db->mutex);
2477724961
return rc;
2477824962
}
24963
+
24964
+/*
24965
+** 32-bit variant of sqlite3_db_status64()
24966
+*/
24967
+SQLITE_API int sqlite3_db_status(
24968
+ sqlite3 *db, /* The database connection whose status is desired */
24969
+ int op, /* Status verb */
24970
+ int *pCurrent, /* Write current value here */
24971
+ int *pHighwtr, /* Write high-water mark here */
24972
+ int resetFlag /* Reset high-water mark if true */
24973
+){
24974
+ sqlite3_int64 C = 0, H = 0;
24975
+ int rc;
24976
+#ifdef SQLITE_ENABLE_API_ARMOR
24977
+ if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){
24978
+ return SQLITE_MISUSE_BKPT;
24979
+ }
24980
+#endif
24981
+ rc = sqlite3_db_status64(db, op, &C, &H, resetFlag);
24982
+ if( rc==0 ){
24983
+ *pCurrent = C & 0x7fffffff;
24984
+ *pHighwtr = H & 0x7fffffff;
24985
+ }
24986
+ return rc;
24987
+}
2477924988
2478024989
/************** End of status.c **********************************************/
2478124990
/************** Begin file date.c ********************************************/
2478224991
/*
2478324992
** 2003 October 31
@@ -24967,10 +25176,14 @@
2496725176
if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
2496825177
return 1;
2496925178
}
2497025179
zDate += 5;
2497125180
p->tz = sgn*(nMn + nHr*60);
25181
+ if( p->tz==0 ){ /* Forum post 2025-09-17T10:12:14z */
25182
+ p->isLocal = 0;
25183
+ p->isUtc = 1;
25184
+ }
2497225185
zulu_time:
2497325186
while( sqlite3Isspace(*zDate) ){ zDate++; }
2497425187
return *zDate!=0;
2497525188
}
2497625189
@@ -26162,12 +26375,12 @@
2616226375
** %j day of year 001-366
2616326376
** %J ** julian day number
2616426377
** %l hour 1-12 (leading zero converted to space)
2616526378
** %m month 01-12
2616626379
** %M minute 00-59
26167
-** %p "am" or "pm"
26168
-** %P "AM" or "PM"
26380
+** %p "AM" or "PM"
26381
+** %P "am" or "pm"
2616926382
** %R time as HH:MM
2617026383
** %s seconds since 1970-01-01
2617126384
** %S seconds 00-59
2617226385
** %T time as HH:MM:SS
2617326386
** %u day of week 1-7 Monday==1, Sunday==7
@@ -31770,56 +31983,74 @@
3177031983
etByte base; /* The base for radix conversion */
3177131984
etByte flags; /* One or more of FLAG_ constants below */
3177231985
etByte type; /* Conversion paradigm */
3177331986
etByte charset; /* Offset into aDigits[] of the digits string */
3177431987
etByte prefix; /* Offset into aPrefix[] of the prefix string */
31988
+ char iNxt; /* Next with same hash, or 0 for end of chain */
3177531989
} et_info;
3177631990
3177731991
/*
3177831992
** Allowed values for et_info.flags
3177931993
*/
3178031994
#define FLAG_SIGNED 1 /* True if the value to convert is signed */
3178131995
#define FLAG_STRING 4 /* Allow infinite precision */
3178231996
31997
+/*
31998
+** The table is searched by hash. In the case of %C where C is the character
31999
+** and that character has ASCII value j, then the hash is j%23.
32000
+**
32001
+** The order of the entries in fmtinfo[] and the hash chain was entered
32002
+** manually, but based on the output of the following TCL script:
32003
+*/
32004
+#if 0 /***** Beginning of script ******/
32005
+foreach c {d s g z q Q w c o u x X f e E G i n % p T S r} {
32006
+ scan $c %c x
32007
+ set n($c) $x
32008
+}
32009
+set mx [llength [array names n]]
32010
+puts "count: $mx"
3178332011
31784
-/*
31785
-** The following table is searched linearly, so it is good to put the
31786
-** most frequently used conversion types first.
31787
-*/
32012
+set mx 27
32013
+puts "*********** mx=$mx ************"
32014
+for {set r 0} {$r<$mx} {incr r} {
32015
+ puts -nonewline [format %2d: $r]
32016
+ foreach c [array names n] {
32017
+ if {($n($c))%$mx==$r} {puts -nonewline " $c"}
32018
+ }
32019
+ puts ""
32020
+}
32021
+#endif /***** End of script ********/
32022
+
3178832023
static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
3178932024
static const char aPrefix[] = "-x0\000X0";
31790
-static const et_info fmtinfo[] = {
31791
- { 'd', 10, 1, etDECIMAL, 0, 0 },
31792
- { 's', 0, 4, etSTRING, 0, 0 },
31793
- { 'g', 0, 1, etGENERIC, 30, 0 },
31794
- { 'z', 0, 4, etDYNSTRING, 0, 0 },
31795
- { 'q', 0, 4, etESCAPE_q, 0, 0 },
31796
- { 'Q', 0, 4, etESCAPE_Q, 0, 0 },
31797
- { 'w', 0, 4, etESCAPE_w, 0, 0 },
31798
- { 'c', 0, 0, etCHARX, 0, 0 },
31799
- { 'o', 8, 0, etRADIX, 0, 2 },
31800
- { 'u', 10, 0, etDECIMAL, 0, 0 },
31801
- { 'x', 16, 0, etRADIX, 16, 1 },
31802
- { 'X', 16, 0, etRADIX, 0, 4 },
31803
-#ifndef SQLITE_OMIT_FLOATING_POINT
31804
- { 'f', 0, 1, etFLOAT, 0, 0 },
31805
- { 'e', 0, 1, etEXP, 30, 0 },
31806
- { 'E', 0, 1, etEXP, 14, 0 },
31807
- { 'G', 0, 1, etGENERIC, 14, 0 },
31808
-#endif
31809
- { 'i', 10, 1, etDECIMAL, 0, 0 },
31810
- { 'n', 0, 0, etSIZE, 0, 0 },
31811
- { '%', 0, 0, etPERCENT, 0, 0 },
31812
- { 'p', 16, 0, etPOINTER, 0, 1 },
31813
-
31814
- /* All the rest are undocumented and are for internal use only */
31815
- { 'T', 0, 0, etTOKEN, 0, 0 },
31816
- { 'S', 0, 0, etSRCITEM, 0, 0 },
31817
- { 'r', 10, 1, etORDINAL, 0, 0 },
32025
+static const et_info fmtinfo[23] = {
32026
+ /* 0 */ { 's', 0, 4, etSTRING, 0, 0, 1 },
32027
+ /* 1 */ { 'E', 0, 1, etEXP, 14, 0, 0 }, /* Hash: 0 */
32028
+ /* 2 */ { 'u', 10, 0, etDECIMAL, 0, 0, 3 },
32029
+ /* 3 */ { 'G', 0, 1, etGENERIC, 14, 0, 0 }, /* Hash: 2 */
32030
+ /* 4 */ { 'w', 0, 4, etESCAPE_w, 0, 0, 0 },
32031
+ /* 5 */ { 'x', 16, 0, etRADIX, 16, 1, 0 },
32032
+ /* 6 */ { 'c', 0, 0, etCHARX, 0, 0, 0 }, /* Hash: 7 */
32033
+ /* 7 */ { 'z', 0, 4, etDYNSTRING, 0, 0, 6 },
32034
+ /* 8 */ { 'd', 10, 1, etDECIMAL, 0, 0, 0 },
32035
+ /* 9 */ { 'e', 0, 1, etEXP, 30, 0, 0 },
32036
+ /* 10 */ { 'f', 0, 1, etFLOAT, 0, 0, 0 },
32037
+ /* 11 */ { 'g', 0, 1, etGENERIC, 30, 0, 0 },
32038
+ /* 12 */ { 'Q', 0, 4, etESCAPE_Q, 0, 0, 0 },
32039
+ /* 13 */ { 'i', 10, 1, etDECIMAL, 0, 0, 0 },
32040
+ /* 14 */ { '%', 0, 0, etPERCENT, 0, 0, 16 },
32041
+ /* 15 */ { 'T', 0, 0, etTOKEN, 0, 0, 0 },
32042
+ /* 16 */ { 'S', 0, 0, etSRCITEM, 0, 0, 0 }, /* Hash: 14 */
32043
+ /* 17 */ { 'X', 16, 0, etRADIX, 0, 4, 0 }, /* Hash: 19 */
32044
+ /* 18 */ { 'n', 0, 0, etSIZE, 0, 0, 0 },
32045
+ /* 19 */ { 'o', 8, 0, etRADIX, 0, 2, 17 },
32046
+ /* 20 */ { 'p', 16, 0, etPOINTER, 0, 1, 0 },
32047
+ /* 21 */ { 'q', 0, 4, etESCAPE_q, 0, 0, 0 },
32048
+ /* 22 */ { 'r', 10, 1, etORDINAL, 0, 0, 0 }
3181832049
};
3181932050
31820
-/* Notes:
32051
+/* Additional Notes:
3182132052
**
3182232053
** %S Takes a pointer to SrcItem. Shows name or database.name
3182332054
** %!S Like %S but prefer the zName over the zAlias
3182432055
*/
3182532056
@@ -31942,11 +32173,14 @@
3194232173
if( c!='%' ){
3194332174
bufpt = (char *)fmt;
3194432175
#if HAVE_STRCHRNUL
3194532176
fmt = strchrnul(fmt, '%');
3194632177
#else
31947
- do{ fmt++; }while( *fmt && *fmt != '%' );
32178
+ fmt = strchr(fmt, '%');
32179
+ if( fmt==0 ){
32180
+ fmt = bufpt + strlen(bufpt);
32181
+ }
3194832182
#endif
3194932183
sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
3195032184
if( *fmt==0 ) break;
3195132185
}
3195232186
if( (c=(*++fmt))==0 ){
@@ -32056,19 +32290,36 @@
3205632290
}
3205732291
}
3205832292
}while( !done && (c=(*++fmt))!=0 );
3205932293
3206032294
/* Fetch the info entry for the field */
32295
+#ifdef SQLITE_EBCDIC
32296
+ /* The hash table only works for ASCII. For EBCDIC, we need to do
32297
+ ** a linear search of the table */
3206132298
infop = &fmtinfo[0];
3206232299
xtype = etINVALID;
3206332300
for(idx=0; idx<ArraySize(fmtinfo); idx++){
3206432301
if( c==fmtinfo[idx].fmttype ){
3206532302
infop = &fmtinfo[idx];
3206632303
xtype = infop->type;
3206732304
break;
3206832305
}
3206932306
}
32307
+#else
32308
+ /* Fast hash-table lookup */
32309
+ assert( ArraySize(fmtinfo)==23 );
32310
+ idx = ((unsigned)c) % 23;
32311
+ if( fmtinfo[idx].fmttype==c
32312
+ || fmtinfo[idx = fmtinfo[idx].iNxt].fmttype==c
32313
+ ){
32314
+ infop = &fmtinfo[idx];
32315
+ xtype = infop->type;
32316
+ }else{
32317
+ infop = &fmtinfo[0];
32318
+ xtype = etINVALID;
32319
+ }
32320
+#endif
3207032321
3207132322
/*
3207232323
** At this point, variables are initialized as follows:
3207332324
**
3207432325
** flag_alternateform TRUE if a '#' is present.
@@ -32252,11 +32503,25 @@
3225232503
length = sqlite3Strlen30(bufpt);
3225332504
break;
3225432505
}
3225532506
}
3225632507
if( s.sign=='-' ){
32257
- prefix = '-';
32508
+ if( flag_alternateform
32509
+ && !flag_prefix
32510
+ && xtype==etFLOAT
32511
+ && s.iDP<=iRound
32512
+ ){
32513
+ /* Suppress the minus sign if all of the following are true:
32514
+ ** * The value displayed is zero
32515
+ ** * The '#' flag is used
32516
+ ** * The '+' flag is not used, and
32517
+ ** * The format is %f
32518
+ */
32519
+ prefix = 0;
32520
+ }else{
32521
+ prefix = '-';
32522
+ }
3225832523
}else{
3225932524
prefix = flag_prefix;
3226032525
}
3226132526
3226232527
exp = s.iDP-1;
@@ -33463,13 +33728,17 @@
3346333728
sqlite3StrAccumFinish(&x);
3346433729
sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
3346533730
n = 0;
3346633731
if( pItem->fg.isSubquery ) n++;
3346733732
if( pItem->fg.isTabFunc ) n++;
33468
- if( pItem->fg.isUsing ) n++;
33733
+ if( pItem->fg.isUsing || pItem->u3.pOn!=0 ) n++;
3346933734
if( pItem->fg.isUsing ){
3347033735
sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
33736
+ }else if( pItem->u3.pOn!=0 ){
33737
+ sqlite3TreeViewItem(pView, "ON", (--n)>0);
33738
+ sqlite3TreeViewExpr(pView, pItem->u3.pOn, 0);
33739
+ sqlite3TreeViewPop(&pView);
3347133740
}
3347233741
if( pItem->fg.isSubquery ){
3347333742
assert( n==1 );
3347433743
if( pItem->pSTab ){
3347533744
Table *pTab = pItem->pSTab;
@@ -38108,11 +38377,11 @@
3810838377
static int kvstorageDelete(const char*, const char *zKey);
3810938378
static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf);
3811038379
#define KVSTORAGE_KEY_SZ 32
3811138380
3811238381
/* Expand the key name with an appropriate prefix and put the result
38113
-** zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
38382
+** in zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least
3811438383
** KVSTORAGE_KEY_SZ bytes.
3811538384
*/
3811638385
static void kvstorageMakeKey(
3811738386
const char *zClass,
3811838387
const char *zKeyIn,
@@ -38167,14 +38436,16 @@
3816738436
** enough to hold it all. The value put into zBuf must always be zero
3816838437
** terminated, even if it gets truncated because nBuf is not large enough.
3816938438
**
3817038439
** Return the total number of bytes in the data, without truncation, and
3817138440
** not counting the final zero terminator. Return -1 if the key does
38172
-** not exist.
38441
+** not exist or its key cannot be read.
3817338442
**
38174
-** If nBuf<=0 then this routine simply returns the size of the data without
38175
-** actually reading it.
38443
+** If nBuf<=0 then this routine simply returns the size of the data
38444
+** without actually reading it. Similarly, if nBuf==1 then it
38445
+** zero-terminates zBuf at zBuf[0] and returns the size of the data
38446
+** without reading it.
3817638447
*/
3817738448
static int kvstorageRead(
3817838449
const char *zClass,
3817938450
const char *zKey,
3818038451
char *zBuf,
@@ -38219,15 +38490,13 @@
3821938490
/*
3822038491
** An internal level of indirection which enables us to replace the
3822138492
** kvvfs i/o methods with JavaScript implementations in WASM builds.
3822238493
** Maintenance reminder: if this struct changes in any way, the JSON
3822338494
** rendering of its structure must be updated in
38224
-** sqlite3_wasm_enum_json(). There are no binary compatibility
38225
-** concerns, so it does not need an iVersion member. This file is
38226
-** necessarily always compiled together with sqlite3_wasm_enum_json(),
38227
-** and JS code dynamically creates the mapping of members based on
38228
-** that JSON description.
38495
+** sqlite3-wasm.c:sqlite3__wasm_enum_json(). There are no binary
38496
+** compatibility concerns, so it does not need an iVersion
38497
+** member.
3822938498
*/
3823038499
typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods;
3823138500
struct sqlite3_kvvfs_methods {
3823238501
int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf);
3823338502
int (*xWrite)(const char *zClass, const char *zKey, const char *zData);
@@ -38240,12 +38509,12 @@
3824038509
** for JavaScript-side implementations in WASM builds. In such builds
3824138510
** it cannot be const, but in native builds it should be so that
3824238511
** the compiler can hopefully optimize this level of indirection out.
3824338512
** That said, kvvfs is intended primarily for use in WASM builds.
3824438513
**
38245
-** Note that this is not explicitly flagged as static because the
38246
-** amalgamation build will tag it with SQLITE_PRIVATE.
38514
+** This is not explicitly flagged as static because the amalgamation
38515
+** build will tag it with SQLITE_PRIVATE.
3824738516
*/
3824838517
#ifndef SQLITE_WASM
3824938518
const
3825038519
#endif
3825138520
SQLITE_PRIVATE sqlite3_kvvfs_methods sqlite3KvvfsMethods = {
@@ -39414,14 +39683,15 @@
3941439683
#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
3941539684
aSyscall[13].pCurrent)
3941639685
3941739686
#if defined(HAVE_FCHMOD)
3941839687
{ "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
39688
+#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
3941939689
#else
3942039690
{ "fchmod", (sqlite3_syscall_ptr)0, 0 },
39691
+#define osFchmod(FID,MODE) 0
3942139692
#endif
39422
-#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
3942339693
3942439694
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
3942539695
{ "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
3942639696
#else
3942739697
{ "fallocate", (sqlite3_syscall_ptr)0, 0 },
@@ -39675,13 +39945,12 @@
3967539945
return fd;
3967639946
}
3967739947
3967839948
/*
3967939949
** Helper functions to obtain and relinquish the global mutex. The
39680
-** global mutex is used to protect the unixInodeInfo and
39681
-** vxworksFileId objects used by this file, all of which may be
39682
-** shared by multiple threads.
39950
+** global mutex is used to protect the unixInodeInfo objects used by
39951
+** this file, all of which may be shared by multiple threads.
3968339952
**
3968439953
** Function unixMutexHeld() is used to assert() that the global mutex
3968539954
** is held when required. This function is only used as part of assert()
3968639955
** statements. e.g.
3968739956
**
@@ -39879,10 +40148,11 @@
3987940148
/*
3988040149
** All unique filenames are held on a linked list headed by this
3988140150
** variable:
3988240151
*/
3988340152
static struct vxworksFileId *vxworksFileList = 0;
40153
+static sqlite3_mutex *vxworksMutex = 0;
3988440154
3988540155
/*
3988640156
** Simplify a filename into its canonical form
3988740157
** by making the following changes:
3988840158
**
@@ -39944,47 +40214,47 @@
3994440214
3994540215
/* Search for an existing entry that matching the canonical name.
3994640216
** If found, increment the reference count and return a pointer to
3994740217
** the existing file ID.
3994840218
*/
39949
- unixEnterMutex();
40219
+ sqlite3_mutex_enter(vxworksMutex);
3995040220
for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
3995140221
if( pCandidate->nName==n
3995240222
&& memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
3995340223
){
3995440224
sqlite3_free(pNew);
3995540225
pCandidate->nRef++;
39956
- unixLeaveMutex();
40226
+ sqlite3_mutex_leave(vxworksMutex);
3995740227
return pCandidate;
3995840228
}
3995940229
}
3996040230
3996140231
/* No match was found. We will make a new file ID */
3996240232
pNew->nRef = 1;
3996340233
pNew->nName = n;
3996440234
pNew->pNext = vxworksFileList;
3996540235
vxworksFileList = pNew;
39966
- unixLeaveMutex();
40236
+ sqlite3_mutex_leave(vxworksMutex);
3996740237
return pNew;
3996840238
}
3996940239
3997040240
/*
3997140241
** Decrement the reference count on a vxworksFileId object. Free
3997240242
** the object when the reference count reaches zero.
3997340243
*/
3997440244
static void vxworksReleaseFileId(struct vxworksFileId *pId){
39975
- unixEnterMutex();
40245
+ sqlite3_mutex_enter(vxworksMutex);
3997640246
assert( pId->nRef>0 );
3997740247
pId->nRef--;
3997840248
if( pId->nRef==0 ){
3997940249
struct vxworksFileId **pp;
3998040250
for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
3998140251
assert( *pp==pId );
3998240252
*pp = pId->pNext;
3998340253
sqlite3_free(pId);
3998440254
}
39985
- unixLeaveMutex();
40255
+ sqlite3_mutex_leave(vxworksMutex);
3998640256
}
3998740257
#endif /* OS_VXWORKS */
3998840258
/*************** End of Unique File ID Utility Used By VxWorks ****************
3998940259
******************************************************************************/
3999040260
@@ -40368,10 +40638,14 @@
4036840638
do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
4036940639
if( rc!=1 ){
4037040640
storeLastErrno(pFile, errno);
4037140641
return SQLITE_IOERR;
4037240642
}
40643
+ if( fsync(fd) ){
40644
+ storeLastErrno(pFile, errno);
40645
+ return SQLITE_IOERR_FSYNC;
40646
+ }
4037340647
rc = osFstat(fd, &statbuf);
4037440648
if( rc!=0 ){
4037540649
storeLastErrno(pFile, errno);
4037640650
return SQLITE_IOERR;
4037740651
}
@@ -40537,22 +40811,46 @@
4053740811
static int osSetPosixAdvisoryLock(
4053840812
int h, /* The file descriptor on which to take the lock */
4053940813
struct flock *pLock, /* The description of the lock */
4054040814
unixFile *pFile /* Structure holding timeout value */
4054140815
){
40542
- int tm = pFile->iBusyTimeout;
40543
- int rc = osFcntl(h,F_SETLK,pLock);
40544
- while( rc<0 && tm>0 ){
40545
- /* On systems that support some kind of blocking file lock with a timeout,
40546
- ** make appropriate changes here to invoke that blocking file lock. On
40547
- ** generic posix, however, there is no such API. So we simply try the
40548
- ** lock once every millisecond until either the timeout expires, or until
40549
- ** the lock is obtained. */
40550
- unixSleep(0,1000);
40816
+ int rc = 0;
40817
+
40818
+ if( pFile->iBusyTimeout==0 ){
40819
+ /* unixFile->iBusyTimeout is set to 0. In this case, attempt a
40820
+ ** non-blocking lock. */
40821
+ rc = osFcntl(h,F_SETLK,pLock);
40822
+ }else{
40823
+ /* unixFile->iBusyTimeout is set to greater than zero. In this case,
40824
+ ** attempt a blocking-lock with a unixFile->iBusyTimeout ms timeout.
40825
+ **
40826
+ ** On systems that support some kind of blocking file lock operation,
40827
+ ** this block should be replaced by code to attempt a blocking lock
40828
+ ** with a timeout of unixFile->iBusyTimeout ms. The code below is
40829
+ ** placeholder code. If SQLITE_TEST is defined, the placeholder code
40830
+ ** retries the lock once every 1ms until it succeeds or the timeout
40831
+ ** is reached. Or, if SQLITE_TEST is not defined, the placeholder
40832
+ ** code attempts a non-blocking lock and sets unixFile->iBusyTimeout
40833
+ ** to 0. This causes the caller to return SQLITE_BUSY, instead of
40834
+ ** SQLITE_BUSY_TIMEOUT to SQLite - as required by a VFS that does not
40835
+ ** support blocking locks.
40836
+ */
40837
+#ifdef SQLITE_TEST
40838
+ int tm = pFile->iBusyTimeout;
40839
+ while( tm>0 ){
40840
+ rc = osFcntl(h,F_SETLK,pLock);
40841
+ if( rc==0 ) break;
40842
+ unixSleep(0,1000);
40843
+ tm--;
40844
+ }
40845
+#else
4055140846
rc = osFcntl(h,F_SETLK,pLock);
40552
- tm--;
40847
+ pFile->iBusyTimeout = 0;
40848
+#endif
40849
+ /* End of code to replace with real blocking-locks code. */
4055340850
}
40851
+
4055440852
return rc;
4055540853
}
4055640854
#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
4055740855
4055840856
@@ -44869,14 +45167,21 @@
4486945167
#endif
4487045168
4487145169
storeLastErrno(pNew, 0);
4487245170
#if OS_VXWORKS
4487345171
if( rc!=SQLITE_OK ){
44874
- if( h>=0 ) robust_close(pNew, h, __LINE__);
44875
- h = -1;
44876
- osUnlink(zFilename);
44877
- pNew->ctrlFlags |= UNIXFILE_DELETE;
45172
+ if( h>=0 ){
45173
+ robust_close(pNew, h, __LINE__);
45174
+ h = -1;
45175
+ }
45176
+ if( pNew->ctrlFlags & UNIXFILE_DELETE ){
45177
+ osUnlink(zFilename);
45178
+ }
45179
+ if( pNew->pId ){
45180
+ vxworksReleaseFileId(pNew->pId);
45181
+ pNew->pId = 0;
45182
+ }
4487845183
}
4487945184
#endif
4488045185
if( rc!=SQLITE_OK ){
4488145186
if( h>=0 ) robust_close(pNew, h, __LINE__);
4488245187
}else{
@@ -44916,10 +45221,13 @@
4491645221
struct stat buf;
4491745222
const char *zDir = sqlite3_temp_directory;
4491845223
4491945224
while(1){
4492045225
if( zDir!=0
45226
+#if OS_VXWORKS
45227
+ && zDir[0]=='/'
45228
+#endif
4492145229
&& osStat(zDir, &buf)==0
4492245230
&& S_ISDIR(buf.st_mode)
4492345231
&& osAccess(zDir, 03)==0
4492445232
){
4492545233
return zDir;
@@ -45229,10 +45537,16 @@
4522945537
assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
4523045538
|| eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
4523145539
|| eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
4523245540
|| eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
4523345541
);
45542
+
45543
+#if OS_VXWORKS
45544
+ /* The file-ID mechanism used in Vxworks requires that all pathnames
45545
+ ** provided to unixOpen must be absolute pathnames. */
45546
+ if( zPath!=0 && zPath[0]!='/' ){ return SQLITE_CANTOPEN; }
45547
+#endif
4523445548
4523545549
/* Detect a pid change and reset the PRNG. There is a race condition
4523645550
** here such that two or more threads all trying to open databases at
4523745551
** the same instant might all reset the PRNG. But multiple resets
4523845552
** are harmless.
@@ -45430,12 +45744,15 @@
4543045744
goto open_finished;
4543145745
}
4543245746
}
4543345747
#endif
4543445748
45435
- assert( zPath==0 || zPath[0]=='/'
45436
- || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
45749
+ assert( zPath==0
45750
+ || zPath[0]=='/'
45751
+ || eType==SQLITE_OPEN_SUPER_JOURNAL
45752
+ || eType==SQLITE_OPEN_MAIN_JOURNAL
45753
+ || eType==SQLITE_OPEN_TEMP_JOURNAL
4543745754
);
4543845755
rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
4543945756
4544045757
open_finished:
4544145758
if( rc!=SQLITE_OK ){
@@ -47160,10 +47477,13 @@
4716047477
}
4716147478
#ifdef SQLITE_OS_KV_OPTIONAL
4716247479
sqlite3KvvfsInit();
4716347480
#endif
4716447481
unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
47482
+#if OS_VXWORKS
47483
+ vxworksMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS2);
47484
+#endif
4716547485
4716647486
#ifndef SQLITE_OMIT_WAL
4716747487
/* Validate lock assumptions */
4716847488
assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */
4716947489
assert( UNIX_SHM_BASE==120 ); /* Start of locking area */
@@ -47194,10 +47514,13 @@
4719447514
** to release dynamically allocated objects. But not on unix.
4719547515
** This routine is a no-op for unix.
4719647516
*/
4719747517
SQLITE_API int sqlite3_os_end(void){
4719847518
unixBigLock = 0;
47519
+#if OS_VXWORKS
47520
+ vxworksMutex = 0;
47521
+#endif
4719947522
return SQLITE_OK;
4720047523
}
4720147524
4720247525
#endif /* SQLITE_OS_UNIX */
4720347526
@@ -51192,204 +51515,10 @@
5119251515
** on allocation size granularity boundaries.
5119351516
** During sqlite3_os_init() we do a GetSystemInfo()
5119451517
** to get the granularity size.
5119551518
*/
5119651519
static SYSTEM_INFO winSysInfo;
51197
-
51198
-#ifndef SQLITE_OMIT_WAL
51199
-
51200
-/*
51201
-** Helper functions to obtain and relinquish the global mutex. The
51202
-** global mutex is used to protect the winLockInfo objects used by
51203
-** this file, all of which may be shared by multiple threads.
51204
-**
51205
-** Function winShmMutexHeld() is used to assert() that the global mutex
51206
-** is held when required. This function is only used as part of assert()
51207
-** statements. e.g.
51208
-**
51209
-** winShmEnterMutex()
51210
-** assert( winShmMutexHeld() );
51211
-** winShmLeaveMutex()
51212
-*/
51213
-static sqlite3_mutex *winBigLock = 0;
51214
-static void winShmEnterMutex(void){
51215
- sqlite3_mutex_enter(winBigLock);
51216
-}
51217
-static void winShmLeaveMutex(void){
51218
- sqlite3_mutex_leave(winBigLock);
51219
-}
51220
-#ifndef NDEBUG
51221
-static int winShmMutexHeld(void) {
51222
- return sqlite3_mutex_held(winBigLock);
51223
-}
51224
-#endif
51225
-
51226
-/*
51227
-** Object used to represent a single file opened and mmapped to provide
51228
-** shared memory. When multiple threads all reference the same
51229
-** log-summary, each thread has its own winFile object, but they all
51230
-** point to a single instance of this object. In other words, each
51231
-** log-summary is opened only once per process.
51232
-**
51233
-** winShmMutexHeld() must be true when creating or destroying
51234
-** this object or while reading or writing the following fields:
51235
-**
51236
-** nRef
51237
-** pNext
51238
-**
51239
-** The following fields are read-only after the object is created:
51240
-**
51241
-** zFilename
51242
-**
51243
-** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
51244
-** winShmMutexHeld() is true when reading or writing any other field
51245
-** in this structure.
51246
-**
51247
-** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51248
-** the *-shm file if the DMS-locking protocol demands it, and (c) map
51249
-** regions of the *-shm file into memory using MapViewOfFile() or
51250
-** similar. Other locks are taken by individual clients using the
51251
-** winShm.hShm handles.
51252
-*/
51253
-struct winShmNode {
51254
- sqlite3_mutex *mutex; /* Mutex to access this object */
51255
- char *zFilename; /* Name of the file */
51256
- HANDLE hSharedShm; /* File handle open on zFilename */
51257
-
51258
- int isUnlocked; /* DMS lock has not yet been obtained */
51259
- int isReadonly; /* True if read-only */
51260
- int szRegion; /* Size of shared-memory regions */
51261
- int nRegion; /* Size of array apRegion */
51262
-
51263
- struct ShmRegion {
51264
- HANDLE hMap; /* File handle from CreateFileMapping */
51265
- void *pMap;
51266
- } *aRegion;
51267
- DWORD lastErrno; /* The Windows errno from the last I/O error */
51268
-
51269
- int nRef; /* Number of winShm objects pointing to this */
51270
- winShmNode *pNext; /* Next in list of all winShmNode objects */
51271
-#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51272
- u8 nextShmId; /* Next available winShm.id value */
51273
-#endif
51274
-};
51275
-
51276
-/*
51277
-** A global array of all winShmNode objects.
51278
-**
51279
-** The winShmMutexHeld() must be true while reading or writing this list.
51280
-*/
51281
-static winShmNode *winShmNodeList = 0;
51282
-
51283
-/*
51284
-** Structure used internally by this VFS to record the state of an
51285
-** open shared memory connection. There is one such structure for each
51286
-** winFile open on a wal mode database.
51287
-*/
51288
-struct winShm {
51289
- winShmNode *pShmNode; /* The underlying winShmNode object */
51290
- u16 sharedMask; /* Mask of shared locks held */
51291
- u16 exclMask; /* Mask of exclusive locks held */
51292
- HANDLE hShm; /* File-handle on *-shm file. For locking. */
51293
- int bReadonly; /* True if hShm is opened read-only */
51294
-#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51295
- u8 id; /* Id of this connection with its winShmNode */
51296
-#endif
51297
-};
51298
-
51299
-/*
51300
-** Constants used for locking
51301
-*/
51302
-#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
51303
-#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
51304
-
51305
-/* Forward references to VFS methods */
51306
-static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
51307
-static int winDelete(sqlite3_vfs *,const char*,int);
51308
-
51309
-/*
51310
-** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
51311
-**
51312
-** This is not a VFS shared-memory method; it is a utility function called
51313
-** by VFS shared-memory methods.
51314
-*/
51315
-static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
51316
- winShmNode **pp;
51317
- winShmNode *p;
51318
- assert( winShmMutexHeld() );
51319
- OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
51320
- osGetCurrentProcessId(), deleteFlag));
51321
- pp = &winShmNodeList;
51322
- while( (p = *pp)!=0 ){
51323
- if( p->nRef==0 ){
51324
- int i;
51325
- if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
51326
- for(i=0; i<p->nRegion; i++){
51327
- BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
51328
- OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
51329
- osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51330
- UNUSED_VARIABLE_VALUE(bRc);
51331
- bRc = osCloseHandle(p->aRegion[i].hMap);
51332
- OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
51333
- osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51334
- UNUSED_VARIABLE_VALUE(bRc);
51335
- }
51336
- winHandleClose(p->hSharedShm);
51337
- if( deleteFlag ){
51338
- SimulateIOErrorBenign(1);
51339
- sqlite3BeginBenignMalloc();
51340
- winDelete(pVfs, p->zFilename, 0);
51341
- sqlite3EndBenignMalloc();
51342
- SimulateIOErrorBenign(0);
51343
- }
51344
- *pp = p->pNext;
51345
- sqlite3_free(p->aRegion);
51346
- sqlite3_free(p);
51347
- }else{
51348
- pp = &p->pNext;
51349
- }
51350
- }
51351
-}
51352
-
51353
-/*
51354
-** The DMS lock has not yet been taken on the shm file associated with
51355
-** pShmNode. Take the lock. Truncate the *-shm file if required.
51356
-** Return SQLITE_OK if successful, or an SQLite error code otherwise.
51357
-*/
51358
-static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){
51359
- HANDLE h = pShmNode->hSharedShm;
51360
- int rc = SQLITE_OK;
51361
-
51362
- assert( sqlite3_mutex_held(pShmNode->mutex) );
51363
- rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0);
51364
- if( rc==SQLITE_OK ){
51365
- /* We have an EXCLUSIVE lock on the DMS byte. This means that this
51366
- ** is the first process to open the file. Truncate it to zero bytes
51367
- ** in this case. */
51368
- if( pShmNode->isReadonly ){
51369
- rc = SQLITE_READONLY_CANTINIT;
51370
- }else{
51371
- rc = winHandleTruncate(h, 0);
51372
- }
51373
-
51374
- /* Release the EXCLUSIVE lock acquired above. */
51375
- winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0);
51376
- }else if( (rc & 0xFF)==SQLITE_BUSY ){
51377
- rc = SQLITE_OK;
51378
- }
51379
-
51380
- if( rc==SQLITE_OK ){
51381
- /* Take a SHARED lock on the DMS byte. */
51382
- rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs);
51383
- if( rc==SQLITE_OK ){
51384
- pShmNode->isUnlocked = 0;
51385
- }
51386
- }
51387
-
51388
- return rc;
51389
-}
51390
-
5139151520
5139251521
/*
5139351522
** Convert a UTF-8 filename into whatever form the underlying
5139451523
** operating system wants filenames in. Space to hold the result
5139551524
** is obtained from malloc and must be freed by the calling
@@ -51483,10 +51612,212 @@
5148351612
}
5148451613
#endif
5148551614
/* caller will handle out of memory */
5148651615
return zConverted;
5148751616
}
51617
+
51618
+#ifndef SQLITE_OMIT_WAL
51619
+
51620
+/*
51621
+** Helper functions to obtain and relinquish the global mutex. The
51622
+** global mutex is used to protect the winLockInfo objects used by
51623
+** this file, all of which may be shared by multiple threads.
51624
+**
51625
+** Function winShmMutexHeld() is used to assert() that the global mutex
51626
+** is held when required. This function is only used as part of assert()
51627
+** statements. e.g.
51628
+**
51629
+** winShmEnterMutex()
51630
+** assert( winShmMutexHeld() );
51631
+** winShmLeaveMutex()
51632
+*/
51633
+static sqlite3_mutex *winBigLock = 0;
51634
+static void winShmEnterMutex(void){
51635
+ sqlite3_mutex_enter(winBigLock);
51636
+}
51637
+static void winShmLeaveMutex(void){
51638
+ sqlite3_mutex_leave(winBigLock);
51639
+}
51640
+#ifndef NDEBUG
51641
+static int winShmMutexHeld(void) {
51642
+ return sqlite3_mutex_held(winBigLock);
51643
+}
51644
+#endif
51645
+
51646
+/*
51647
+** Object used to represent a single file opened and mmapped to provide
51648
+** shared memory. When multiple threads all reference the same
51649
+** log-summary, each thread has its own winFile object, but they all
51650
+** point to a single instance of this object. In other words, each
51651
+** log-summary is opened only once per process.
51652
+**
51653
+** winShmMutexHeld() must be true when creating or destroying
51654
+** this object, or while editing the global linked list that starts
51655
+** at winShmNodeList.
51656
+**
51657
+** When reading or writing the linked list starting at winShmNode.pWinShmList,
51658
+** pShmNode->mutex must be held.
51659
+**
51660
+** The following fields are constant after the object is created:
51661
+**
51662
+** zFilename
51663
+** hSharedShm
51664
+** mutex
51665
+** bUseSharedLockHandle
51666
+**
51667
+** Either winShmNode.mutex must be held or winShmNode.pWinShmList==0 and
51668
+** winShmMutexHeld() is true when reading or writing any other field
51669
+** in this structure.
51670
+**
51671
+** File-handle hSharedShm is always used to (a) take the DMS lock, (b)
51672
+** truncate the *-shm file if the DMS-locking protocol demands it, and
51673
+** (c) map regions of the *-shm file into memory using MapViewOfFile()
51674
+** or similar. If bUseSharedLockHandle is true, then other locks are also
51675
+** taken on hSharedShm. Or, if bUseSharedLockHandle is false, then other
51676
+** locks are taken using each connection's winShm.hShm handles.
51677
+*/
51678
+struct winShmNode {