Fossil SCM

Merge trunk.

danield 2025-01-04 23:28 UTC ssh-signing merge
Commit 02cdfa5e0866368cbb2d5bf881b544fbcf924c6ad76d20f42c91f62ae266eb7b
86 files changed +1 -1 +6 -6 +1 -1 +448 -156 +1286 -709 +54 -8 +22 -17 +4 -1 +7 -6 +2 -3 +23 -6 +8 -1 +38 -27 +49 -1 +2 -2 +1 -1 +77 -16 +3 -4 +110 -40 +10 -4 +50 -48 +49 -39 +145 -6 +6 -2 +97 -21 +26 -1 +8 -1 +18 +1 -1 +94 -8 +94 -8 +60 -35 +80 -26 +3 -1 +1 -1 +28 -12 +6 +4 -1 +466 -36 +2 -1 +13 -1 +24 -10 +14 -1 +386 +477 -14 +619 +804 -168 +151 -24 +50 -17 +1 +14 -1 +9 -3 +7 -7 +12 -1 +7 -5 +5 -40 +4 -4 +2 +474 -462 +1 -1 +71 -13 +4 -4 +68 +55 -2 +25 +28 -25 +6 -4 +2 +1 -1 +3 -1 +1 -1 +16 -5 +10 -4 +13 +12 +2 -2 +1 -1 +51 -1 +1 -1 +1 -1 +2 -2 +1 -1 +4 -4 +1 -1 +197 -88 +8
+1 -1
--- BUILD.txt
+++ BUILD.txt
@@ -42,11 +42,11 @@
4242
mkdir build
4343
cd build
4444
../configure
4545
make
4646
47
-This will now keep all generates files separate from the maintained
47
+This will now keep all generated files separate from the maintained
4848
source code.
4949
5050
--------------------------------------------------------------------------
5151
5252
Here are some notes on what is happening behind the scenes:
5353
--- BUILD.txt
+++ BUILD.txt
@@ -42,11 +42,11 @@
42 mkdir build
43 cd build
44 ../configure
45 make
46
47 This will now keep all generates files separate from the maintained
48 source code.
49
50 --------------------------------------------------------------------------
51
52 Here are some notes on what is happening behind the scenes:
53
--- BUILD.txt
+++ BUILD.txt
@@ -42,11 +42,11 @@
42 mkdir build
43 cd build
44 ../configure
45 make
46
47 This will now keep all generated files separate from the maintained
48 source code.
49
50 --------------------------------------------------------------------------
51
52 Here are some notes on what is happening behind the scenes:
53
+6 -6
--- Dockerfile
+++ Dockerfile
@@ -3,19 +3,19 @@
33
44
## ---------------------------------------------------------------------
55
## STAGE 1: Build static Fossil binary
66
## ---------------------------------------------------------------------
77
8
-### We aren't pinning to a more stable version of Alpine because we want
8
+### We don't pin a more stable version of our base layer because we want
99
### to build with the latest tools and libraries available in case they
1010
### fixed something that matters to us since the last build. Everything
1111
### below depends on this layer, and so, alas, we toss this container's
1212
### cache on Alpine's release schedule, roughly once a month.
1313
FROM alpine:latest AS bld
1414
WORKDIR /fsl
1515
16
-### Bake the basic Alpine Linux into a base layer so it only changes
16
+### Bake the build-time userland into a base layer so it only changes
1717
### when the upstream image is updated or we change the package set.
1818
RUN set -x \
1919
&& apk update \
2020
&& apk upgrade --no-cache \
2121
&& apk add --no-cache \
@@ -23,19 +23,19 @@
2323
linux-headers musl-dev \
2424
openssl-dev openssl-libs-static \
2525
zlib-dev zlib-static
2626
2727
### Build Fossil as a separate layer so we don't have to rebuild the
28
-### Alpine environment for each iteration of Fossil's dev cycle.
28
+### userland for each iteration of Fossil's dev cycle.
2929
###
3030
### We must cope with a bizarre ADD misfeature here: it unpacks tarballs
3131
### automatically when you give it a local file name but not if you give
3232
### it a /tarball URL! It matters because we default to a URL in case
3333
### you're building outside a Fossil checkout, but when building via the
34
-### container-image target, we avoid a costly hit on fossil-scm.org
35
-### by leveraging its DVCS nature via the "tarball" command and passing
36
-### the resulting file's name in.
34
+### container-image target, we avoid a costly hit on fossil-scm.org by
35
+### leveraging its DVCS nature via the "tarball" command and passing the
36
+### resulting file's name in.
3737
ARG FSLCFG=""
3838
ARG FSLVER="trunk"
3939
ARG FSLURL="https://fossil-scm.org/home/tarball/src?r=${FSLVER}"
4040
ENV FSLSTB=/fsl/src.tar.gz
4141
ADD $FSLURL $FSLSTB
4242
--- Dockerfile
+++ Dockerfile
@@ -3,19 +3,19 @@
3
4 ## ---------------------------------------------------------------------
5 ## STAGE 1: Build static Fossil binary
6 ## ---------------------------------------------------------------------
7
8 ### We aren't pinning to a more stable version of Alpine because we want
9 ### to build with the latest tools and libraries available in case they
10 ### fixed something that matters to us since the last build. Everything
11 ### below depends on this layer, and so, alas, we toss this container's
12 ### cache on Alpine's release schedule, roughly once a month.
13 FROM alpine:latest AS bld
14 WORKDIR /fsl
15
16 ### Bake the basic Alpine Linux into a base layer so it only changes
17 ### when the upstream image is updated or we change the package set.
18 RUN set -x \
19 && apk update \
20 && apk upgrade --no-cache \
21 && apk add --no-cache \
@@ -23,19 +23,19 @@
23 linux-headers musl-dev \
24 openssl-dev openssl-libs-static \
25 zlib-dev zlib-static
26
27 ### Build Fossil as a separate layer so we don't have to rebuild the
28 ### Alpine environment for each iteration of Fossil's dev cycle.
29 ###
30 ### We must cope with a bizarre ADD misfeature here: it unpacks tarballs
31 ### automatically when you give it a local file name but not if you give
32 ### it a /tarball URL! It matters because we default to a URL in case
33 ### you're building outside a Fossil checkout, but when building via the
34 ### container-image target, we avoid a costly hit on fossil-scm.org
35 ### by leveraging its DVCS nature via the "tarball" command and passing
36 ### the resulting file's name in.
37 ARG FSLCFG=""
38 ARG FSLVER="trunk"
39 ARG FSLURL="https://fossil-scm.org/home/tarball/src?r=${FSLVER}"
40 ENV FSLSTB=/fsl/src.tar.gz
41 ADD $FSLURL $FSLSTB
42
--- Dockerfile
+++ Dockerfile
@@ -3,19 +3,19 @@
3
4 ## ---------------------------------------------------------------------
5 ## STAGE 1: Build static Fossil binary
6 ## ---------------------------------------------------------------------
7
8 ### We don't pin a more stable version of our base layer because we want
9 ### to build with the latest tools and libraries available in case they
10 ### fixed something that matters to us since the last build. Everything
11 ### below depends on this layer, and so, alas, we toss this container's
12 ### cache on Alpine's release schedule, roughly once a month.
13 FROM alpine:latest AS bld
14 WORKDIR /fsl
15
16 ### Bake the build-time userland into a base layer so it only changes
17 ### when the upstream image is updated or we change the package set.
18 RUN set -x \
19 && apk update \
20 && apk upgrade --no-cache \
21 && apk add --no-cache \
@@ -23,19 +23,19 @@
23 linux-headers musl-dev \
24 openssl-dev openssl-libs-static \
25 zlib-dev zlib-static
26
27 ### Build Fossil as a separate layer so we don't have to rebuild the
28 ### userland for each iteration of Fossil's dev cycle.
29 ###
30 ### We must cope with a bizarre ADD misfeature here: it unpacks tarballs
31 ### automatically when you give it a local file name but not if you give
32 ### it a /tarball URL! It matters because we default to a URL in case
33 ### you're building outside a Fossil checkout, but when building via the
34 ### container-image target, we avoid a costly hit on fossil-scm.org by
35 ### leveraging its DVCS nature via the "tarball" command and passing the
36 ### resulting file's name in.
37 ARG FSLCFG=""
38 ARG FSLVER="trunk"
39 ARG FSLURL="https://fossil-scm.org/home/tarball/src?r=${FSLVER}"
40 ENV FSLSTB=/fsl/src.tar.gz
41 ADD $FSLURL $FSLSTB
42
+1 -1
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1
-2.25
1
+2.26
22
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 2.25
2
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 2.26
2
+448 -156
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -120,13 +120,10 @@
120120
#include <math.h>
121121
#include "sqlite3.h"
122122
typedef sqlite3_int64 i64;
123123
typedef sqlite3_uint64 u64;
124124
typedef unsigned char u8;
125
-#if SQLITE_USER_AUTHENTICATION
126
-# include "sqlite3userauth.h"
127
-#endif
128125
#include <ctype.h>
129126
#include <stdarg.h>
130127
131128
#if !defined(_WIN32) && !defined(WIN32)
132129
# include <signal.h>
@@ -408,21 +405,21 @@
408405
wchar_t *b1, *b2;
409406
int sz1, sz2;
410407
411408
sz1 = (int)strlen(zFilename);
412409
sz2 = (int)strlen(zMode);
413
- b1 = malloc( (sz1+1)*sizeof(b1[0]) );
414
- b2 = malloc( (sz2+1)*sizeof(b1[0]) );
410
+ b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
411
+ b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
415412
if( b1 && b2 ){
416413
sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
417414
b1[sz1] = 0;
418415
sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
419416
b2[sz2] = 0;
420417
fp = _wfopen(b1, b2);
421418
}
422
- free(b1);
423
- free(b2);
419
+ sqlite3_free(b1);
420
+ sqlite3_free(b2);
424421
simBinaryOther = 0;
425422
return fp;
426423
}
427424
428425
@@ -434,21 +431,21 @@
434431
wchar_t *b1, *b2;
435432
int sz1, sz2;
436433
437434
sz1 = (int)strlen(zCommand);
438435
sz2 = (int)strlen(zMode);
439
- b1 = malloc( (sz1+1)*sizeof(b1[0]) );
440
- b2 = malloc( (sz2+1)*sizeof(b1[0]) );
436
+ b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
437
+ b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
441438
if( b1 && b2 ){
442439
sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
443440
b1[sz1] = 0;
444441
sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
445442
b2[sz2] = 0;
446443
fp = _wpopen(b1, b2);
447444
}
448
- free(b1);
449
- free(b2);
445
+ sqlite3_free(b1);
446
+ sqlite3_free(b2);
450447
return fp;
451448
}
452449
453450
/*
454451
** Work-alike for fgets() from the standard C library.
@@ -458,16 +455,26 @@
458455
/* When reading from the command-prompt in Windows, it is necessary
459456
** to use _O_WTEXT input mode to read UTF-16 characters, then translate
460457
** that into UTF-8. Otherwise, non-ASCII characters all get translated
461458
** into '?'.
462459
*/
463
- wchar_t *b1 = malloc( sz*sizeof(wchar_t) );
460
+ wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) );
464461
if( b1==0 ) return 0;
465
- _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
466
- if( fgetws(b1, sz/4, in)==0 ){
467
- sqlite3_free(b1);
468
- return 0;
462
+#ifndef SQLITE_USE_STDIO_FOR_CONSOLE
463
+ DWORD nRead = 0;
464
+ if( IsConsole(in)
465
+ && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz, &nRead, 0)
466
+ ){
467
+ b1[nRead] = 0;
468
+ }else
469
+#endif
470
+ {
471
+ _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
472
+ if( fgetws(b1, sz/4, in)==0 ){
473
+ sqlite3_free(b1);
474
+ return 0;
475
+ }
469476
}
470477
WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
471478
sqlite3_free(b1);
472479
return buf;
473480
}else{
@@ -519,24 +526,37 @@
519526
if( !UseWtextForOutput(out) ){
520527
/* Writing to a file or other destination, just write bytes without
521528
** any translation. */
522529
return fputs(z, out);
523530
}else{
524
- /* When writing to the command-prompt in Windows, it is necessary
525
- ** to use O_U8TEXT to render Unicode U+0080 and greater. Go ahead
526
- ** use O_U8TEXT for everything in text mode.
531
+ /* One must use UTF16 in order to get unicode support when writing
532
+ ** to the console on Windows.
527533
*/
528534
int sz = (int)strlen(z);
529
- wchar_t *b1 = malloc( (sz+1)*sizeof(wchar_t) );
535
+ wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) );
530536
if( b1==0 ) return 0;
531537
sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
532538
b1[sz] = 0;
533
- _setmode(_fileno(out), _O_U8TEXT);
534
- if( UseBinaryWText(out) ){
535
- piecemealOutput(b1, sz, out);
536
- }else{
537
- fputws(b1, out);
539
+
540
+#ifndef SQLITE_STDIO_FOR_CONSOLE
541
+ DWORD nWr = 0;
542
+ if( IsConsole(out)
543
+ && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0)
544
+ ){
545
+ /* If writing to the console, then the WriteConsoleW() is all we
546
+ ** need to do. */
547
+ }else
548
+#endif
549
+ {
550
+ /* For non-console I/O, or if SQLITE_USE_STDIO_FOR_CONSOLE is defined
551
+ ** then write using the standard library. */
552
+ _setmode(_fileno(out), _O_U8TEXT);
553
+ if( UseBinaryWText(out) ){
554
+ piecemealOutput(b1, sz, out);
555
+ }else{
556
+ fputws(b1, out);
557
+ }
538558
}
539559
sqlite3_free(b1);
540560
return 0;
541561
}
542562
}
@@ -2347,11 +2367,11 @@
23472367
**
23482368
******************************************************************************
23492369
**
23502370
** This SQLite extension implements functions that compute SHA3 hashes
23512371
** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2352
-** Two SQL functions are implemented:
2372
+** Three SQL functions are implemented:
23532373
**
23542374
** sha3(X,SIZE)
23552375
** sha3_agg(Y,SIZE)
23562376
** sha3_query(Z,SIZE)
23572377
**
@@ -5070,14 +5090,14 @@
50705090
char **pzErrMsg,
50715091
const sqlite3_api_routines *pApi
50725092
){
50735093
int rc = SQLITE_OK;
50745094
unsigned int i;
5075
-#if defined(SQLITE3_H) || defined(SQLITE_STATIC_PERCENTILE)
5076
- (void)pApi; /* Unused parameter */
5077
-#else
5095
+#ifdef SQLITE3EXT_H
50785096
SQLITE_EXTENSION_INIT2(pApi);
5097
+#else
5098
+ (void)pApi; /* Unused parameter */
50795099
#endif
50805100
(void)pzErrMsg; /* Unused parameter */
50815101
for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
50825102
rc = sqlite3_create_window_function(db,
50835103
aPercentFunc[i].zName,
@@ -6831,35 +6851,41 @@
68316851
idxNum |= 0x40;
68326852
}
68336853
continue;
68346854
}
68356855
if( pConstraint->iColumn<SERIES_COLUMN_START ){
6836
- if( pConstraint->iColumn==SERIES_COLUMN_VALUE ){
6856
+ if( pConstraint->iColumn==SERIES_COLUMN_VALUE && pConstraint->usable ){
68376857
switch( op ){
68386858
case SQLITE_INDEX_CONSTRAINT_EQ:
68396859
case SQLITE_INDEX_CONSTRAINT_IS: {
68406860
idxNum |= 0x0080;
68416861
idxNum &= ~0x3300;
68426862
aIdx[5] = i;
68436863
aIdx[6] = -1;
6864
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
68446865
bStartSeen = 1;
6866
+#endif
68456867
break;
68466868
}
68476869
case SQLITE_INDEX_CONSTRAINT_GE: {
68486870
if( idxNum & 0x0080 ) break;
68496871
idxNum |= 0x0100;
68506872
idxNum &= ~0x0200;
68516873
aIdx[5] = i;
6874
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
68526875
bStartSeen = 1;
6876
+#endif
68536877
break;
68546878
}
68556879
case SQLITE_INDEX_CONSTRAINT_GT: {
68566880
if( idxNum & 0x0080 ) break;
68576881
idxNum |= 0x0200;
68586882
idxNum &= ~0x0100;
68596883
aIdx[5] = i;
6884
+#ifndef ZERO_ARGUMENT_GENERATE_SERIES
68606885
bStartSeen = 1;
6886
+#endif
68616887
break;
68626888
}
68636889
case SQLITE_INDEX_CONSTRAINT_LE: {
68646890
if( idxNum & 0x0080 ) break;
68656891
idxNum |= 0x1000;
@@ -14167,11 +14193,11 @@
1416714193
/* A view. Or a trigger on a view. */
1416814194
if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg);
1416914195
}else{
1417014196
IdxTable *pTab;
1417114197
rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
14172
- if( rc==SQLITE_OK ){
14198
+ if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){
1417314199
int i;
1417414200
char *zInner = 0;
1417514201
char *zOuter = 0;
1417614202
pTab->pNext = p->pTable;
1417714203
p->pTable = pTab;
@@ -16235,11 +16261,31 @@
1623516261
**
1623616262
** Similar compiler commands will work on different systems. The key
1623716263
** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
1623816264
** the shell.c source file will know to include the -vfstrace command-line
1623916265
** option and (2) you must compile and link the three source files
16240
-** shell,c, test_vfstrace.c, and sqlite3.c.
16266
+** shell,c, test_vfstrace.c, and sqlite3.c.
16267
+**
16268
+** RUNTIME CONTROL OF VFSTRACE OUTPUT
16269
+**
16270
+** The application can use the "vfstrace" pragma to control which VFS
16271
+** APIs are traced. To disable all output:
16272
+**
16273
+** PRAGMA vfstrace('-all');
16274
+**
16275
+** To enable all output (which is the default setting):
16276
+**
16277
+** PRAGMA vfstrace('+all');
16278
+**
16279
+** Individual APIs can be enabled or disabled by name, with or without
16280
+** the initial "x" character. For example, to set up for tracing lock
16281
+** primatives only:
16282
+**
16283
+** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock');
16284
+**
16285
+** The argument to the vfstrace pragma ignores capitalization and any
16286
+** characters other than alphabetics, '+', and '-'.
1624116287
*/
1624216288
#include <stdlib.h>
1624316289
#include <string.h>
1624416290
/* #include "sqlite3.h" */
1624516291
@@ -16249,10 +16295,12 @@
1624916295
*/
1625016296
typedef struct vfstrace_info vfstrace_info;
1625116297
struct vfstrace_info {
1625216298
sqlite3_vfs *pRootVfs; /* The underlying real VFS */
1625316299
int (*xOut)(const char*, void*); /* Send output here */
16300
+ unsigned int mTrace; /* Mask of interfaces to trace */
16301
+ u8 bOn; /* Tracing on/off */
1625416302
void *pOutArg; /* First argument to xOut */
1625516303
const char *zVfsName; /* Name of this trace-VFS */
1625616304
sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
1625716305
};
1625816306
@@ -16265,10 +16313,42 @@
1626516313
vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
1626616314
const char *zFName; /* Base name of the file */
1626716315
sqlite3_file *pReal; /* The real underlying file */
1626816316
};
1626916317
16318
+/*
16319
+** Bit values for vfstrace_info.mTrace.
16320
+*/
16321
+#define VTR_CLOSE 0x00000001
16322
+#define VTR_READ 0x00000002
16323
+#define VTR_WRITE 0x00000004
16324
+#define VTR_TRUNC 0x00000008
16325
+#define VTR_SYNC 0x00000010
16326
+#define VTR_FSIZE 0x00000020
16327
+#define VTR_LOCK 0x00000040
16328
+#define VTR_UNLOCK 0x00000080
16329
+#define VTR_CRL 0x00000100
16330
+#define VTR_FCTRL 0x00000200
16331
+#define VTR_SECSZ 0x00000400
16332
+#define VTR_DEVCHAR 0x00000800
16333
+#define VTR_SHMLOCK 0x00001000
16334
+#define VTR_SHMMAP 0x00002000
16335
+#define VTR_SHMBAR 0x00004000
16336
+#define VTR_SHMUNMAP 0x00008000
16337
+#define VTR_OPEN 0x00010000
16338
+#define VTR_DELETE 0x00020000
16339
+#define VTR_ACCESS 0x00040000
16340
+#define VTR_FULLPATH 0x00080000
16341
+#define VTR_DLOPEN 0x00100000
16342
+#define VTR_DLERR 0x00200000
16343
+#define VTR_DLSYM 0x00400000
16344
+#define VTR_DLCLOSE 0x00800000
16345
+#define VTR_RAND 0x01000000
16346
+#define VTR_SLEEP 0x02000000
16347
+#define VTR_CURTIME 0x04000000
16348
+#define VTR_LASTERR 0x08000000
16349
+
1627016350
/*
1627116351
** Method declarations for vfstrace_file.
1627216352
*/
1627316353
static int vfstraceClose(sqlite3_file*);
1627416354
static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
@@ -16329,15 +16409,17 @@
1632916409
const char *zFormat,
1633016410
...
1633116411
){
1633216412
va_list ap;
1633316413
char *zMsg;
16334
- va_start(ap, zFormat);
16335
- zMsg = sqlite3_vmprintf(zFormat, ap);
16336
- va_end(ap);
16337
- pInfo->xOut(zMsg, pInfo->pOutArg);
16338
- sqlite3_free(zMsg);
16414
+ if( pInfo->bOn ){
16415
+ va_start(ap, zFormat);
16416
+ zMsg = sqlite3_vmprintf(zFormat, ap);
16417
+ va_end(ap);
16418
+ pInfo->xOut(zMsg, pInfo->pOutArg);
16419
+ sqlite3_free(zMsg);
16420
+ }
1633916421
}
1634016422
1634116423
/*
1634216424
** Try to convert an error code into a symbolic name for that error code.
1634316425
*/
@@ -16431,18 +16513,26 @@
1643116513
int i = *pI;
1643216514
while( zAppend[0] ){ z[i++] = *(zAppend++); }
1643316515
z[i] = 0;
1643416516
*pI = i;
1643516517
}
16518
+
16519
+/*
16520
+** Turn tracing output on or off according to mMask.
16521
+*/
16522
+static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){
16523
+ pInfo->bOn = (pInfo->mTrace & mMask)!=0;
16524
+}
1643616525
1643716526
/*
1643816527
** Close an vfstrace-file.
1643916528
*/
1644016529
static int vfstraceClose(sqlite3_file *pFile){
1644116530
vfstrace_file *p = (vfstrace_file *)pFile;
1644216531
vfstrace_info *pInfo = p->pInfo;
1644316532
int rc;
16533
+ vfstraceOnOff(pInfo, VTR_CLOSE);
1644416534
vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
1644516535
rc = p->pReal->pMethods->xClose(p->pReal);
1644616536
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1644716537
if( rc==SQLITE_OK ){
1644816538
sqlite3_free((void*)p->base.pMethods);
@@ -16461,10 +16551,11 @@
1646116551
sqlite_int64 iOfst
1646216552
){
1646316553
vfstrace_file *p = (vfstrace_file *)pFile;
1646416554
vfstrace_info *pInfo = p->pInfo;
1646516555
int rc;
16556
+ vfstraceOnOff(pInfo, VTR_READ);
1646616557
vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
1646716558
pInfo->zVfsName, p->zFName, iAmt, iOfst);
1646816559
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
1646916560
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1647016561
return rc;
@@ -16480,10 +16571,11 @@
1648016571
sqlite_int64 iOfst
1648116572
){
1648216573
vfstrace_file *p = (vfstrace_file *)pFile;
1648316574
vfstrace_info *pInfo = p->pInfo;
1648416575
int rc;
16576
+ vfstraceOnOff(pInfo, VTR_WRITE);
1648516577
vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
1648616578
pInfo->zVfsName, p->zFName, iAmt, iOfst);
1648716579
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
1648816580
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1648916581
return rc;
@@ -16494,10 +16586,11 @@
1649416586
*/
1649516587
static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
1649616588
vfstrace_file *p = (vfstrace_file *)pFile;
1649716589
vfstrace_info *pInfo = p->pInfo;
1649816590
int rc;
16591
+ vfstraceOnOff(pInfo, VTR_TRUNC);
1649916592
vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
1650016593
size);
1650116594
rc = p->pReal->pMethods->xTruncate(p->pReal, size);
1650216595
vfstrace_printf(pInfo, " -> %d\n", rc);
1650316596
return rc;
@@ -16518,10 +16611,11 @@
1651816611
else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
1651916612
if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
1652016613
if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
1652116614
sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
1652216615
}
16616
+ vfstraceOnOff(pInfo, VTR_SYNC);
1652316617
vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
1652416618
&zBuf[1]);
1652516619
rc = p->pReal->pMethods->xSync(p->pReal, flags);
1652616620
vfstrace_printf(pInfo, " -> %d\n", rc);
1652716621
return rc;
@@ -16532,10 +16626,11 @@
1653216626
*/
1653316627
static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
1653416628
vfstrace_file *p = (vfstrace_file *)pFile;
1653516629
vfstrace_info *pInfo = p->pInfo;
1653616630
int rc;
16631
+ vfstraceOnOff(pInfo, VTR_FSIZE);
1653716632
vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
1653816633
rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
1653916634
vfstrace_print_errcode(pInfo, " -> %s,", rc);
1654016635
vfstrace_printf(pInfo, " size=%lld\n", *pSize);
1654116636
return rc;
@@ -16560,10 +16655,11 @@
1656016655
*/
1656116656
static int vfstraceLock(sqlite3_file *pFile, int eLock){
1656216657
vfstrace_file *p = (vfstrace_file *)pFile;
1656316658
vfstrace_info *pInfo = p->pInfo;
1656416659
int rc;
16660
+ vfstraceOnOff(pInfo, VTR_LOCK);
1656516661
vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
1656616662
lockName(eLock));
1656716663
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
1656816664
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1656916665
return rc;
@@ -16574,10 +16670,11 @@
1657416670
*/
1657516671
static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
1657616672
vfstrace_file *p = (vfstrace_file *)pFile;
1657716673
vfstrace_info *pInfo = p->pInfo;
1657816674
int rc;
16675
+ vfstraceOnOff(pInfo, VTR_UNLOCK);
1657916676
vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
1658016677
lockName(eLock));
1658116678
rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
1658216679
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1658316680
return rc;
@@ -16588,10 +16685,11 @@
1658816685
*/
1658916686
static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
1659016687
vfstrace_file *p = (vfstrace_file *)pFile;
1659116688
vfstrace_info *pInfo = p->pInfo;
1659216689
int rc;
16690
+ vfstraceOnOff(pInfo, VTR_CRL);
1659316691
vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
1659416692
pInfo->zVfsName, p->zFName);
1659516693
rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
1659616694
vfstrace_print_errcode(pInfo, " -> %s", rc);
1659716695
vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16607,10 +16705,11 @@
1660716705
int rc;
1660816706
char zBuf[100];
1660916707
char zBuf2[100];
1661016708
char *zOp;
1661116709
char *zRVal = 0;
16710
+ vfstraceOnOff(pInfo, VTR_FCTRL);
1661216711
switch( op ){
1661316712
case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
1661416713
case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
1661516714
case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
1661616715
case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
@@ -16635,10 +16734,83 @@
1663516734
case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
1663616735
case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
1663716736
case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
1663816737
case SQLITE_FCNTL_PRAGMA: {
1663916738
const char *const* a = (const char*const*)pArg;
16739
+ if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){
16740
+ const u8 *zArg = (const u8*)a[2];
16741
+ if( zArg[0]>='0' && zArg[0]<=9 ){
16742
+ pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0);
16743
+ }else{
16744
+ static const struct {
16745
+ const char *z;
16746
+ unsigned int m;
16747
+ } aKw[] = {
16748
+ { "all", 0xffffffff },
16749
+ { "close", VTR_CLOSE },
16750
+ { "read", VTR_READ },
16751
+ { "write", VTR_WRITE },
16752
+ { "truncate", VTR_TRUNC },
16753
+ { "sync", VTR_SYNC },
16754
+ { "filesize", VTR_FSIZE },
16755
+ { "lock", VTR_LOCK },
16756
+ { "unlock", VTR_UNLOCK },
16757
+ { "checkreservedlock", VTR_CRL },
16758
+ { "filecontrol", VTR_FCTRL },
16759
+ { "sectorsize", VTR_SECSZ },
16760
+ { "devicecharacteristics", VTR_DEVCHAR },
16761
+ { "shmlock", VTR_SHMLOCK },
16762
+ { "shmmap", VTR_SHMMAP },
16763
+ { "shmummap", VTR_SHMUNMAP },
16764
+ { "shmbarrier", VTR_SHMBAR },
16765
+ { "open", VTR_OPEN },
16766
+ { "delete", VTR_DELETE },
16767
+ { "access", VTR_ACCESS },
16768
+ { "fullpathname", VTR_FULLPATH },
16769
+ { "dlopen", VTR_DLOPEN },
16770
+ { "dlerror", VTR_DLERR },
16771
+ { "dlsym", VTR_DLSYM },
16772
+ { "dlclose", VTR_DLCLOSE },
16773
+ { "randomness", VTR_RAND },
16774
+ { "sleep", VTR_SLEEP },
16775
+ { "currenttime", VTR_CURTIME },
16776
+ { "currenttimeint64", VTR_CURTIME },
16777
+ { "getlasterror", VTR_LASTERR },
16778
+ };
16779
+ int onOff = 1;
16780
+ while( zArg[0] ){
16781
+ int jj, n;
16782
+ while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+'
16783
+ && !isalpha(zArg[0]) ) zArg++;
16784
+ if( zArg[0]==0 ) break;
16785
+ if( zArg[0]=='-' ){
16786
+ onOff = 0;
16787
+ zArg++;
16788
+ }else if( zArg[0]=='+' ){
16789
+ onOff = 1;
16790
+ zArg++;
16791
+ }
16792
+ while( !isalpha(zArg[0]) ){
16793
+ if( zArg[0]==0 ) break;
16794
+ zArg++;
16795
+ }
16796
+ if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++;
16797
+ for(n=0; isalpha(zArg[n]); n++){}
16798
+ for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){
16799
+ if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){
16800
+ if( onOff ){
16801
+ pInfo->mTrace |= aKw[jj].m;
16802
+ }else{
16803
+ pInfo->mTrace &= ~aKw[jj].m;
16804
+ }
16805
+ break;
16806
+ }
16807
+ }
16808
+ zArg += n;
16809
+ }
16810
+ }
16811
+ }
1664016812
sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
1664116813
zOp = zBuf;
1664216814
break;
1664316815
}
1664416816
case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break;
@@ -16730,10 +16902,11 @@
1673016902
*/
1673116903
static int vfstraceSectorSize(sqlite3_file *pFile){
1673216904
vfstrace_file *p = (vfstrace_file *)pFile;
1673316905
vfstrace_info *pInfo = p->pInfo;
1673416906
int rc;
16907
+ vfstraceOnOff(pInfo, VTR_SECSZ);
1673516908
vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
1673616909
rc = p->pReal->pMethods->xSectorSize(p->pReal);
1673716910
vfstrace_printf(pInfo, " -> %d\n", rc);
1673816911
return rc;
1673916912
}
@@ -16743,10 +16916,11 @@
1674316916
*/
1674416917
static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
1674516918
vfstrace_file *p = (vfstrace_file *)pFile;
1674616919
vfstrace_info *pInfo = p->pInfo;
1674716920
int rc;
16921
+ vfstraceOnOff(pInfo, VTR_DEVCHAR);
1674816922
vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
1674916923
pInfo->zVfsName, p->zFName);
1675016924
rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
1675116925
vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
1675216926
return rc;
@@ -16754,25 +16928,43 @@
1675416928
1675516929
/*
1675616930
** Shared-memory operations.
1675716931
*/
1675816932
static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
16933
+ static const char *azLockName[] = {
16934
+ "WRITE",
16935
+ "CKPT",
16936
+ "RECOVER",
16937
+ "READ0",
16938
+ "READ1",
16939
+ "READ2",
16940
+ "READ3",
16941
+ "READ4",
16942
+ };
1675916943
vfstrace_file *p = (vfstrace_file *)pFile;
1676016944
vfstrace_info *pInfo = p->pInfo;
1676116945
int rc;
1676216946
char zLck[100];
1676316947
int i = 0;
16948
+ vfstraceOnOff(pInfo, VTR_SHMLOCK);
1676416949
memcpy(zLck, "|0", 3);
1676516950
if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
1676616951
if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
1676716952
if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
1676816953
if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
1676916954
if( flags & ~(0xf) ){
1677016955
sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
1677116956
}
16772
- vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)",
16773
- pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]);
16957
+ if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){
16958
+ vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)",
16959
+ pInfo->zVfsName, p->zFName, ofst, azLockName[ofst],
16960
+ n, &zLck[1]);
16961
+ }else{
16962
+ vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)",
16963
+ pInfo->zVfsName, p->zFName, ofst,
16964
+ n, &zLck[1]);
16965
+ }
1677416966
rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
1677516967
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1677616968
return rc;
1677716969
}
1677816970
static int vfstraceShmMap(
@@ -16783,26 +16975,29 @@
1678316975
void volatile **pp
1678416976
){
1678516977
vfstrace_file *p = (vfstrace_file *)pFile;
1678616978
vfstrace_info *pInfo = p->pInfo;
1678716979
int rc;
16980
+ vfstraceOnOff(pInfo, VTR_SHMMAP);
1678816981
vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
1678916982
pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
1679016983
rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
1679116984
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1679216985
return rc;
1679316986
}
1679416987
static void vfstraceShmBarrier(sqlite3_file *pFile){
1679516988
vfstrace_file *p = (vfstrace_file *)pFile;
1679616989
vfstrace_info *pInfo = p->pInfo;
16990
+ vfstraceOnOff(pInfo, VTR_SHMBAR);
1679716991
vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
1679816992
p->pReal->pMethods->xShmBarrier(p->pReal);
1679916993
}
1680016994
static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
1680116995
vfstrace_file *p = (vfstrace_file *)pFile;
1680216996
vfstrace_info *pInfo = p->pInfo;
1680316997
int rc;
16998
+ vfstraceOnOff(pInfo, VTR_SHMUNMAP);
1680416999
vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
1680517000
pInfo->zVfsName, p->zFName, delFlag);
1680617001
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
1680717002
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1680817003
return rc;
@@ -16826,10 +17021,11 @@
1682617021
sqlite3_vfs *pRoot = pInfo->pRootVfs;
1682717022
p->pInfo = pInfo;
1682817023
p->zFName = zName ? fileTail(zName) : "<temp>";
1682917024
p->pReal = (sqlite3_file *)&p[1];
1683017025
rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
17026
+ vfstraceOnOff(pInfo, VTR_OPEN);
1683117027
vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
1683217028
pInfo->zVfsName, p->zFName, flags);
1683317029
if( p->pReal->pMethods ){
1683417030
sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
1683517031
const sqlite3_io_methods *pSub = p->pReal->pMethods;
@@ -16871,10 +17067,11 @@
1687117067
*/
1687217068
static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
1687317069
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1687417070
sqlite3_vfs *pRoot = pInfo->pRootVfs;
1687517071
int rc;
17072
+ vfstraceOnOff(pInfo, VTR_DELETE);
1687617073
vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
1687717074
pInfo->zVfsName, zPath, dirSync);
1687817075
rc = pRoot->xDelete(pRoot, zPath, dirSync);
1687917076
vfstrace_print_errcode(pInfo, " -> %s\n", rc);
1688017077
return rc;
@@ -16891,10 +17088,11 @@
1689117088
int *pResOut
1689217089
){
1689317090
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1689417091
sqlite3_vfs *pRoot = pInfo->pRootVfs;
1689517092
int rc;
17093
+ vfstraceOnOff(pInfo, VTR_ACCESS);
1689617094
vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
1689717095
pInfo->zVfsName, zPath, flags);
1689817096
rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
1689917097
vfstrace_print_errcode(pInfo, " -> %s", rc);
1690017098
vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16913,10 +17111,11 @@
1691317111
char *zOut
1691417112
){
1691517113
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1691617114
sqlite3_vfs *pRoot = pInfo->pRootVfs;
1691717115
int rc;
17116
+ vfstraceOnOff(pInfo, VTR_FULLPATH);
1691817117
vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
1691917118
pInfo->zVfsName, zPath);
1692017119
rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
1692117120
vfstrace_print_errcode(pInfo, " -> %s", rc);
1692217121
vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
@@ -16927,10 +17126,11 @@
1692717126
** Open the dynamic library located at zPath and return a handle.
1692817127
*/
1692917128
static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
1693017129
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1693117130
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17131
+ vfstraceOnOff(pInfo, VTR_DLOPEN);
1693217132
vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
1693317133
return pRoot->xDlOpen(pRoot, zPath);
1693417134
}
1693517135
1693617136
/*
@@ -16939,10 +17139,11 @@
1693917139
** with dynamic libraries.
1694017140
*/
1694117141
static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
1694217142
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1694317143
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17144
+ vfstraceOnOff(pInfo, VTR_DLERR);
1694417145
vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
1694517146
pRoot->xDlError(pRoot, nByte, zErrMsg);
1694617147
vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
1694717148
}
1694817149
@@ -16960,10 +17161,11 @@
1696017161
** Close the dynamic library handle pHandle.
1696117162
*/
1696217163
static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
1696317164
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1696417165
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17166
+ vfstraceOnOff(pInfo, VTR_DLCLOSE);
1696517167
vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
1696617168
pRoot->xDlClose(pRoot, pHandle);
1696717169
}
1696817170
1696917171
/*
@@ -16971,10 +17173,11 @@
1697117173
** random data.
1697217174
*/
1697317175
static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
1697417176
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1697517177
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17178
+ vfstraceOnOff(pInfo, VTR_RAND);
1697617179
vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
1697717180
return pRoot->xRandomness(pRoot, nByte, zBufOut);
1697817181
}
1697917182
1698017183
/*
@@ -16982,34 +17185,52 @@
1698217185
** actually slept.
1698317186
*/
1698417187
static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
1698517188
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1698617189
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17190
+ vfstraceOnOff(pInfo, VTR_SLEEP);
17191
+ vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro);
1698717192
return pRoot->xSleep(pRoot, nMicro);
1698817193
}
1698917194
1699017195
/*
1699117196
** Return the current time as a Julian Day number in *pTimeOut.
1699217197
*/
1699317198
static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
1699417199
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1699517200
sqlite3_vfs *pRoot = pInfo->pRootVfs;
16996
- return pRoot->xCurrentTime(pRoot, pTimeOut);
17201
+ int rc;
17202
+ vfstraceOnOff(pInfo, VTR_CURTIME);
17203
+ vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName);
17204
+ rc = pRoot->xCurrentTime(pRoot, pTimeOut);
17205
+ vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut);
17206
+ return rc;
1699717207
}
1699817208
static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
1699917209
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1700017210
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17001
- return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
17211
+ int rc;
17212
+ vfstraceOnOff(pInfo, VTR_CURTIME);
17213
+ vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName);
17214
+ rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
17215
+ vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut);
17216
+ return rc;
1700217217
}
1700317218
1700417219
/*
17005
-** Return th3 most recent error code and message
17220
+** Return the most recent error code and message
1700617221
*/
17007
-static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
17222
+static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){
1700817223
vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
1700917224
sqlite3_vfs *pRoot = pInfo->pRootVfs;
17010
- return pRoot->xGetLastError(pRoot, iErr, zErr);
17225
+ int rc;
17226
+ vfstraceOnOff(pInfo, VTR_LASTERR);
17227
+ vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr);
17228
+ if( nErr ) zErr[0] = 0;
17229
+ rc = pRoot->xGetLastError(pRoot, nErr, zErr);
17230
+ vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc);
17231
+ return rc;
1701117232
}
1701217233
1701317234
/*
1701417235
** Override system calls.
1701517236
*/
@@ -17099,10 +17320,12 @@
1709917320
pInfo->pRootVfs = pRoot;
1710017321
pInfo->xOut = xOut;
1710117322
pInfo->pOutArg = pOutArg;
1710217323
pInfo->zVfsName = pNew->zName;
1710317324
pInfo->pTraceVfs = pNew;
17325
+ pInfo->mTrace = 0xffffffff;
17326
+ pInfo->bOn = 1;
1710417327
vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
1710517328
pInfo->zVfsName, pRoot->zName);
1710617329
return sqlite3_vfs_register(pNew, makeDefault);
1710717330
}
1710817331
@@ -20228,10 +20451,12 @@
2022820451
apVal[iField] = sqlite3_value_dup( pVal );
2022920452
if( apVal[iField]==0 ){
2023020453
recoverError(p, SQLITE_NOMEM, 0);
2023120454
}
2023220455
p1->nVal = iField+1;
20456
+ }else if( pTab->nCol==0 ){
20457
+ p1->nVal = pTab->nCol;
2023320458
}
2023420459
p1->iPrevCell = iCell;
2023520460
p1->iPrevPage = iPage;
2023620461
}
2023720462
}else{
@@ -21935,12 +22160,12 @@
2193522160
const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
2193622161
int ng = (nAccept<0)? -nAccept : 0;
2193722162
const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
2193822163
assert(z!=0);
2193922164
while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
21940
- char c = *z;
21941
- if( (c & 0x80) == 0 ){
22165
+ unsigned char c = *(u8*)z;
22166
+ if( c<0x7f ){
2194222167
if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
2194322168
++z; /* ASCII */
2194422169
}else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
2194522170
else{
2194622171
const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
@@ -22004,14 +22229,14 @@
2200422229
}
2200522230
sqlite3_fputs(zq, out);
2200622231
}
2200722232
2200822233
/*
22009
-** Output the given string as a quoted according to JSON quoting rules.
22234
+** Output the given string as quoted according to JSON quoting rules.
2201022235
*/
2201122236
static void output_json_string(FILE *out, const char *z, i64 n){
22012
- char c;
22237
+ unsigned char c;
2201322238
static const char *zq = "\"";
2201422239
static long ctrlMask = ~0L;
2201522240
static const char *zDQBS = "\"\\";
2201622241
const char *pcLimit;
2201722242
char ace[3] = "\\?";
@@ -22027,11 +22252,11 @@
2202722252
if( pcEnd > z ){
2202822253
sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
2202922254
z = pcEnd;
2203022255
}
2203122256
if( z >= pcLimit ) break;
22032
- c = *(z++);
22257
+ c = (unsigned char)*(z++);
2203322258
switch( c ){
2203422259
case '"': case '\\':
2203522260
cbsSay = (char)c;
2203622261
break;
2203722262
case '\b': cbsSay = 'b'; break;
@@ -22042,12 +22267,12 @@
2204222267
default: cbsSay = 0; break;
2204322268
}
2204422269
if( cbsSay ){
2204522270
ace[1] = cbsSay;
2204622271
sqlite3_fputs(ace, out);
22047
- }else if( c<=0x1f ){
22048
- sqlite3_fprintf(out, "u%04x", c);
22272
+ }else if( c<=0x1f || c>=0x7f ){
22273
+ sqlite3_fprintf(out, "\\u%04x", c);
2204922274
}else{
2205022275
ace[1] = (char)c;
2205122276
sqlite3_fputs(ace+1, out);
2205222277
}
2205322278
}
@@ -24784,13 +25009,13 @@
2478425009
if( rc ){
2478525010
sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
2478625011
}else{
2478725012
rc = SQLITE_CORRUPT;
2478825013
}
24789
- sqlite3_free(zErr);
2479025014
free(zQ2);
2479125015
}
25016
+ sqlite3_free(zErr);
2479225017
return rc;
2479325018
}
2479425019
2479525020
/*
2479625021
** Text of help messages.
@@ -24849,10 +25074,11 @@
2484925074
".databases List names and files of attached databases",
2485025075
".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
2485125076
#if SQLITE_SHELL_HAVE_RECOVER
2485225077
".dbinfo ?DB? Show status information about the database",
2485325078
#endif
25079
+ ".dbtotxt Hex dump of the database file",
2485425080
".dump ?OBJECTS? Render database content as SQL",
2485525081
" Options:",
2485625082
" --data-only Output only INSERT statements",
2485725083
" --newlines Allow unescaped newline characters in output",
2485825084
" --nosys Omit system tables (ex: \"sqlite_stat1\")",
@@ -25654,11 +25880,12 @@
2565425880
sqlite3_fprintf(stderr,
2565525881
"Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
2565625882
}
2565725883
}
2565825884
25659
-#if HAVE_READLINE || HAVE_EDITLINE
25885
+#if (HAVE_READLINE || HAVE_EDITLINE) \
25886
+ && !defined(SQLITE_OMIT_READLINE_COMPLETION)
2566025887
/*
2566125888
** Readline completion callbacks
2566225889
*/
2566325890
static char *readline_completion_generator(const char *text, int state){
2566425891
static sqlite3_stmt *pStmt = 0;
@@ -25692,19 +25919,26 @@
2569225919
#elif HAVE_LINENOISE
2569325920
/*
2569425921
** Linenoise completion callback. Note that the 3rd argument is from
2569525922
** the "msteveb" version of linenoise, not the "antirez" version.
2569625923
*/
25697
-static void linenoise_completion(const char *zLine, linenoiseCompletions *lc,
25698
- void *pUserData){
25924
+static void linenoise_completion(
25925
+ const char *zLine,
25926
+ linenoiseCompletions *lc
25927
+#if HAVE_LINENOISE==2
25928
+ ,void *pUserData
25929
+#endif
25930
+){
2569925931
i64 nLine = strlen(zLine);
2570025932
i64 i, iStart;
2570125933
sqlite3_stmt *pStmt = 0;
2570225934
char *zSql;
2570325935
char zBuf[1000];
2570425936
25937
+#if HAVE_LINENOISE==2
2570525938
UNUSED_PARAMETER(pUserData);
25939
+#endif
2570625940
if( nLine>(i64)sizeof(zBuf)-30 ) return;
2570725941
if( zLine[0]=='.' || zLine[0]=='#') return;
2570825942
for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
2570925943
if( i==nLine-1 ) return;
2571025944
iStart = i+1;
@@ -26390,18 +26624,24 @@
2639026624
#endif
2639126625
2639226626
/*
2639326627
** Run an SQL command and return the single integer result.
2639426628
*/
26395
-static int db_int(sqlite3 *db, const char *zSql){
26629
+static int db_int(sqlite3 *db, const char *zSql, ...){
2639626630
sqlite3_stmt *pStmt;
2639726631
int res = 0;
26398
- sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
26632
+ char *z;
26633
+ va_list ap;
26634
+ va_start(ap, zSql);
26635
+ z = sqlite3_vmprintf(zSql, ap);
26636
+ va_end(ap);
26637
+ sqlite3_prepare_v2(db, z, -1, &pStmt, 0);
2639926638
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
2640026639
res = sqlite3_column_int(pStmt,0);
2640126640
}
2640226641
sqlite3_finalize(pStmt);
26642
+ sqlite3_free(z);
2640326643
return res;
2640426644
}
2640526645
2640626646
#if SQLITE_SHELL_HAVE_RECOVER
2640726647
/*
@@ -26500,21 +26740,112 @@
2650026740
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
2650126741
}else{
2650226742
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
2650326743
}
2650426744
for(i=0; i<ArraySize(aQuery); i++){
26505
- char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
26506
- int val = db_int(p->db, zSql);
26507
- sqlite3_free(zSql);
26745
+ int val = db_int(p->db, aQuery[i].zSql, zSchemaTab);
2650826746
sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
2650926747
}
2651026748
sqlite3_free(zSchemaTab);
2651126749
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
2651226750
sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
2651326751
return 0;
2651426752
}
2651526753
#endif /* SQLITE_SHELL_HAVE_RECOVER */
26754
+
26755
+/*
26756
+** Implementation of the ".dbtotxt" command.
26757
+**
26758
+** Return 1 on error, 2 to exit, and 0 otherwise.
26759
+*/
26760
+static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){
26761
+ sqlite3_stmt *pStmt = 0;
26762
+ sqlite3_int64 nPage = 0;
26763
+ int pgSz = 0;
26764
+ const char *zTail;
26765
+ char *zName = 0;
26766
+ int rc, i, j;
26767
+ unsigned char bShow[256]; /* Characters ok to display */
26768
+
26769
+ UNUSED_PARAMETER(nArg);
26770
+ UNUSED_PARAMETER(azArg);
26771
+ memset(bShow, '.', sizeof(bShow));
26772
+ for(i=' '; i<='~'; i++){
26773
+ if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
26774
+ }
26775
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0);
26776
+ if( rc ) goto dbtotxt_error;
26777
+ rc = 0;
26778
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26779
+ pgSz = sqlite3_column_int(pStmt, 0);
26780
+ sqlite3_finalize(pStmt);
26781
+ pStmt = 0;
26782
+ if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error;
26783
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0);
26784
+ if( rc ) goto dbtotxt_error;
26785
+ rc = 0;
26786
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26787
+ nPage = sqlite3_column_int64(pStmt, 0);
26788
+ sqlite3_finalize(pStmt);
26789
+ pStmt = 0;
26790
+ if( nPage<1 ) goto dbtotxt_error;
26791
+ rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0);
26792
+ if( rc ) goto dbtotxt_error;
26793
+ if( sqlite3_step(pStmt)!=SQLITE_ROW ){
26794
+ zTail = "unk.db";
26795
+ }else{
26796
+ const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
26797
+ if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
26798
+ zTail = strrchr(zFilename, '/');
26799
+#if defined(_WIN32)
26800
+ if( zTail==0 ) zTail = strrchr(zFilename, '\\');
26801
+#endif
26802
+ }
26803
+ zName = strdup(zTail);
26804
+ shell_check_oom(zName);
26805
+ sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
26806
+ nPage*pgSz, pgSz, zName);
26807
+ sqlite3_finalize(pStmt);
26808
+ pStmt = 0;
26809
+ rc = sqlite3_prepare_v2(p->db,
26810
+ "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0);
26811
+ if( rc ) goto dbtotxt_error;
26812
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
26813
+ sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0);
26814
+ const u8 *aData = sqlite3_column_blob(pStmt, 1);
26815
+ int seenPageLabel = 0;
26816
+ for(i=0; i<pgSz; i+=16){
26817
+ const u8 *aLine = aData+i;
26818
+ for(j=0; j<16 && aLine[j]==0; j++){}
26819
+ if( j==16 ) continue;
26820
+ if( !seenPageLabel ){
26821
+ sqlite3_fprintf(p->out, "| page %lld offset %lld\n", pgno, pgno*pgSz);
26822
+ seenPageLabel = 1;
26823
+ }
26824
+ sqlite3_fprintf(p->out, "| %5d:", i);
26825
+ for(j=0; j<16; j++) sqlite3_fprintf(p->out, " %02x", aLine[j]);
26826
+ sqlite3_fprintf(p->out, " ");
26827
+ for(j=0; j<16; j++){
26828
+ unsigned char c = (unsigned char)aLine[j];
26829
+ sqlite3_fprintf(p->out, "%c", bShow[c]);
26830
+ }
26831
+ sqlite3_fprintf(p->out, "\n");
26832
+ }
26833
+ }
26834
+ sqlite3_finalize(pStmt);
26835
+ sqlite3_fprintf(p->out, "| end %s\n", zName);
26836
+ free(zName);
26837
+ return 0;
26838
+
26839
+dbtotxt_error:
26840
+ if( rc ){
26841
+ sqlite3_fprintf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db));
26842
+ }
26843
+ sqlite3_finalize(pStmt);
26844
+ free(zName);
26845
+ return 1;
26846
+}
2651626847
2651726848
/*
2651826849
** Print the given string as an error message.
2651926850
*/
2652026851
static void shellEmitError(const char *zErr){
@@ -28067,12 +28398,12 @@
2806728398
}else if( *pDb==0 ){
2806828399
return 0;
2806928400
}else{
2807028401
/* Formulate the columns spec, close the DB, zero *pDb. */
2807128402
char *zColsSpec = 0;
28072
- int hasDupes = db_int(*pDb, zHasDupes);
28073
- int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
28403
+ int hasDupes = db_int(*pDb, "%s", zHasDupes);
28404
+ int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0;
2807428405
if( hasDupes ){
2807528406
#ifdef SHELL_COLUMN_RENAME_CLEAN
2807628407
rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
2807728408
rc_err_oom_die(rc);
2807828409
#endif
@@ -28083,11 +28414,11 @@
2808328414
sqlite3_bind_int(pStmt, 1, nDigits);
2808428415
rc = sqlite3_step(pStmt);
2808528416
sqlite3_finalize(pStmt);
2808628417
if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
2808728418
}
28088
- assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
28419
+ assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */
2808928420
rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
2809028421
rc_err_oom_die(rc);
2809128422
rc = sqlite3_step(pStmt);
2809228423
if( rc==SQLITE_ROW ){
2809328424
zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
@@ -28688,10 +29019,14 @@
2868829019
}else{
2868929020
eputz("Usage: .echo on|off\n");
2869029021
rc = 1;
2869129022
}
2869229023
}else
29024
+
29025
+ if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
29026
+ rc = shell_dbtotxt_command(p, nArg, azArg);
29027
+ }else
2869329028
2869429029
if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
2869529030
if( nArg==2 ){
2869629031
p->autoEQPtest = 0;
2869729032
if( p->autoEQPtrace ){
@@ -29129,11 +29464,15 @@
2912929464
/* Below, resources must be freed before exit. */
2913029465
while( (nSkip--)>0 ){
2913129466
while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
2913229467
}
2913329468
import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
29134
- if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
29469
+ if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0)
29470
+ && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema"
29471
+ " WHERE name=%Q AND type='view'",
29472
+ zSchema ? zSchema : "main", zTable)
29473
+ ){
2913529474
/* Table does not exist. Create it. */
2913629475
sqlite3 *dbCols = 0;
2913729476
char *zRenames = 0;
2913829477
char *zColDefs;
2913929478
zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
@@ -30201,11 +30540,14 @@
3020130540
}
3020230541
close_db(pSrc);
3020330542
}else
3020430543
#endif /* !defined(SQLITE_SHELL_FIDDLE) */
3020530544
30206
- if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
30545
+ if( c=='s' &&
30546
+ (cli_strncmp(azArg[0], "scanstats", n)==0 ||
30547
+ cli_strncmp(azArg[0], "scanstatus", n)==0)
30548
+ ){
3020730549
if( nArg==2 ){
3020830550
if( cli_strcmp(azArg[1], "vm")==0 ){
3020930551
p->scanstatsOn = 3;
3021030552
}else
3021130553
if( cli_strcmp(azArg[1], "est")==0 ){
@@ -31256,11 +31598,13 @@
3125631598
{ 0x10000000, 1, "OrderBySubq" },
3125731599
{ 0xffffffff, 0, "All" },
3125831600
};
3125931601
unsigned int curOpt;
3126031602
unsigned int newOpt;
31603
+ unsigned int m;
3126131604
int ii;
31605
+ int nOff;
3126231606
sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
3126331607
newOpt = curOpt;
3126431608
for(ii=2; ii<nArg; ii++){
3126531609
const char *z = azArg[ii];
3126631610
int useLabel = 0;
@@ -31297,28 +31641,32 @@
3129731641
}
3129831642
}
3129931643
}
3130031644
if( curOpt!=newOpt ){
3130131645
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
31302
- }else if( nArg<3 ){
31303
- curOpt = ~newOpt;
31646
+ }
31647
+ for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){
31648
+ if( m & newOpt ) nOff++;
3130431649
}
31305
- if( newOpt==0 ){
31306
- sqlite3_fputs("+All\n", p->out);
31307
- }else if( newOpt==0xffffffff ){
31308
- sqlite3_fputs("-All\n", p->out);
31650
+ if( nOff<12 ){
31651
+ sqlite3_fputs("+All", p->out);
31652
+ for(ii=0; ii<ArraySize(aLabel); ii++){
31653
+ if( !aLabel[ii].bDsply ) continue;
31654
+ if( (newOpt & aLabel[ii].mask)!=0 ){
31655
+ sqlite3_fprintf(p->out, " -%s", aLabel[ii].zLabel);
31656
+ }
31657
+ }
3130931658
}else{
31310
- int jj;
31311
- for(jj=0; jj<ArraySize(aLabel); jj++){
31312
- unsigned int m = aLabel[jj].mask;
31313
- if( !aLabel[jj].bDsply ) continue;
31314
- if( (curOpt&m)!=(newOpt&m) ){
31315
- sqlite3_fprintf(p->out, "%c%s\n", (newOpt & m)==0 ? '+' : '-',
31316
- aLabel[jj].zLabel);
31659
+ sqlite3_fputs("-All", p->out);
31660
+ for(ii=0; ii<ArraySize(aLabel); ii++){
31661
+ if( !aLabel[ii].bDsply ) continue;
31662
+ if( (newOpt & aLabel[ii].mask)==0 ){
31663
+ sqlite3_fprintf(p->out, " +%s", aLabel[ii].zLabel);
3131731664
}
3131831665
}
3131931666
}
31667
+ sqlite3_fputs("\n", p->out);
3132031668
rc2 = isOk = 3;
3132131669
break;
3132231670
}
3132331671
3132431672
/* sqlite3_test_control(int, db, int) */
@@ -31652,73 +32000,10 @@
3165232000
}
3165332001
}
3165432002
}else
3165532003
#endif
3165632004
31657
-#if SQLITE_USER_AUTHENTICATION
31658
- if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
31659
- if( nArg<2 ){
31660
- eputz("Usage: .user SUBCOMMAND ...\n");
31661
- rc = 1;
31662
- goto meta_command_exit;
31663
- }
31664
- open_db(p, 0);
31665
- if( cli_strcmp(azArg[1],"login")==0 ){
31666
- if( nArg!=4 ){
31667
- eputz("Usage: .user login USER PASSWORD\n");
31668
- rc = 1;
31669
- goto meta_command_exit;
31670
- }
31671
- rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
31672
- strlen30(azArg[3]));
31673
- if( rc ){
31674
- sqlite3_fprintf(stderr,"Authentication failed for user %s\n", azArg[2]);
31675
- rc = 1;
31676
- }
31677
- }else if( cli_strcmp(azArg[1],"add")==0 ){
31678
- if( nArg!=5 ){
31679
- eputz("Usage: .user add USER PASSWORD ISADMIN\n");
31680
- rc = 1;
31681
- goto meta_command_exit;
31682
- }
31683
- rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31684
- booleanValue(azArg[4]));
31685
- if( rc ){
31686
- sqlite3_fprintf(stderr,"User-Add failed: %d\n", rc);
31687
- rc = 1;
31688
- }
31689
- }else if( cli_strcmp(azArg[1],"edit")==0 ){
31690
- if( nArg!=5 ){
31691
- eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
31692
- rc = 1;
31693
- goto meta_command_exit;
31694
- }
31695
- rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31696
- booleanValue(azArg[4]));
31697
- if( rc ){
31698
- sqlite3_fprintf(stderr,"User-Edit failed: %d\n", rc);
31699
- rc = 1;
31700
- }
31701
- }else if( cli_strcmp(azArg[1],"delete")==0 ){
31702
- if( nArg!=3 ){
31703
- eputz("Usage: .user delete USER\n");
31704
- rc = 1;
31705
- goto meta_command_exit;
31706
- }
31707
- rc = sqlite3_user_delete(p->db, azArg[2]);
31708
- if( rc ){
31709
- sqlite3_fprintf(stderr,"User-Delete failed: %d\n", rc);
31710
- rc = 1;
31711
- }
31712
- }else{
31713
- eputz("Usage: .user login|add|edit|delete ...\n");
31714
- rc = 1;
31715
- goto meta_command_exit;
31716
- }
31717
- }else
31718
-#endif /* SQLITE_USER_AUTHENTICATION */
31719
-
3172032005
if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
3172132006
char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
3172232007
sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
3172332008
sqlite3_libversion(), sqlite3_sourceid());
3172432009
#if SQLITE_HAVE_ZLIB
@@ -31838,11 +32123,10 @@
3183832123
SCAN_TRACKER_REFTYPE pst){
3183932124
char cin;
3184032125
char cWait = (char)qss; /* intentional narrowing loss */
3184132126
if( cWait==0 ){
3184232127
PlainScan:
31843
- assert( cWait==0 );
3184432128
while( (cin = *zLine++)!=0 ){
3184532129
if( IsSpace(cin) )
3184632130
continue;
3184732131
switch (cin){
3184832132
case '-':
@@ -31890,11 +32174,10 @@
3189032174
switch( cWait ){
3189132175
case '*':
3189232176
if( *zLine != '/' )
3189332177
continue;
3189432178
++zLine;
31895
- cWait = 0;
3189632179
CONTINUE_PROMPT_AWAITC(pst, 0);
3189732180
qss = QSS_SETV(qss, 0);
3189832181
goto PlainScan;
3189932182
case '`': case '\'': case '"':
3190032183
if(*zLine==cWait){
@@ -31902,11 +32185,10 @@
3190232185
++zLine;
3190332186
continue;
3190432187
}
3190532188
deliberate_fall_through;
3190632189
case ']':
31907
- cWait = 0;
3190832190
CONTINUE_PROMPT_AWAITC(pst, 0);
3190932191
qss = QSS_SETV(qss, 0);
3191032192
goto PlainScan;
3191132193
default: assert(0);
3191232194
}
@@ -32090,11 +32372,14 @@
3209032372
if( doAutoDetectRestore(p, zSql) ) return 1;
3209132373
return 0;
3209232374
}
3209332375
3209432376
static void echo_group_input(ShellState *p, const char *zDo){
32095
- if( ShellHasFlag(p, SHFLG_Echo) ) sqlite3_fprintf(p->out, "%s\n", zDo);
32377
+ if( ShellHasFlag(p, SHFLG_Echo) ){
32378
+ sqlite3_fprintf(p->out, "%s\n", zDo);
32379
+ fflush(p->out);
32380
+ }
3209632381
}
3209732382
3209832383
#ifdef SQLITE_SHELL_FIDDLE
3209932384
/*
3210032385
** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32550,10 +32835,19 @@
3255032835
}
3255132836
3255232837
static void sayAbnormalExit(void){
3255332838
if( seenInterrupt ) eputz("Program interrupted.\n");
3255432839
}
32840
+
32841
+/* Routine to output from vfstrace
32842
+*/
32843
+static int vfstraceOut(const char *z, void *pArg){
32844
+ ShellState *p = (ShellState*)pArg;
32845
+ sqlite3_fputs(z, p->out);
32846
+ fflush(p->out);
32847
+ return 1;
32848
+}
3255532849
3255632850
#ifndef SQLITE_SHELL_IS_UTF8
3255732851
# if (defined(_WIN32) || defined(WIN32)) \
3255832852
&& (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
3255932853
# define SQLITE_SHELL_IS_UTF8 (0)
@@ -32787,12 +33081,10 @@
3278733081
case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
3278833082
case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
3278933083
default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
3279033084
}
3279133085
}else if( cli_strcmp(z,"-vfstrace")==0 ){
32792
- vfstrace_register("trace",0,(int(*)(const char*,void*))sqlite3_fputs,
32793
- stderr,1);
3279433086
bEnableVfstrace = 1;
3279533087
#ifdef SQLITE_ENABLE_MULTIPLEX
3279633088
}else if( cli_strcmp(z,"-multiplex")==0 ){
3279733089
extern int sqlite3_multiplex_initialize(const char*,int);
3279833090
sqlite3_multiplex_initialize(0, 1);
@@ -32885,10 +33177,13 @@
3288533177
"%s: Error: no database filename specified\n", Argv0);
3288633178
return 1;
3288733179
#endif
3288833180
}
3288933181
data.out = stdout;
33182
+ if( bEnableVfstrace ){
33183
+ vfstrace_register("trace",0,vfstraceOut, &data, 1);
33184
+ }
3289033185
#ifndef SQLITE_SHELL_FIDDLE
3289133186
sqlite3_appendvfs_init(0,0,0);
3289233187
#endif
3289333188
3289433189
/* Go ahead and open the database file if it already exists. If the
@@ -33129,19 +33424,14 @@
3312933424
*/
3313033425
if( stdin_is_interactive ){
3313133426
char *zHome;
3313233427
char *zHistory;
3313333428
int nHistory;
33134
-#if CIO_WIN_WC_XLATE
33135
-# define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
33136
-#else
33137
-# define SHELL_CIO_CHAR_SET ""
33138
-#endif
3313933429
sqlite3_fprintf(stdout,
33140
- "SQLite version %s %.19s%s\n" /*extra-version-info*/
33430
+ "SQLite version %s %.19s\n" /*extra-version-info*/
3314133431
"Enter \".help\" for usage hints.\n",
33142
- sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
33432
+ sqlite3_libversion(), sqlite3_sourceid());
3314333433
if( warnInmemoryDb ){
3314433434
sputz(stdout, "Connected to a ");
3314533435
printBold("transient in-memory database");
3314633436
sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
3314733437
" persistent database.\n");
@@ -33154,13 +33444,15 @@
3315433444
if( (zHistory = malloc(nHistory))!=0 ){
3315533445
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
3315633446
}
3315733447
}
3315833448
if( zHistory ){ shell_read_history(zHistory); }
33159
-#if HAVE_READLINE || HAVE_EDITLINE
33449
+#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
3316033450
rl_attempted_completion_function = readline_completion;
33161
-#elif HAVE_LINENOISE
33451
+#elif HAVE_LINENOISE==1
33452
+ linenoiseSetCompletionCallback(linenoise_completion);
33453
+#elif HAVE_LINENOISE==2
3316233454
linenoiseSetCompletionCallback(linenoise_completion, NULL);
3316333455
#endif
3316433456
data.in = 0;
3316533457
rc = process_input(&data);
3316633458
if( zHistory ){
3316733459
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -120,13 +120,10 @@
120 #include <math.h>
121 #include "sqlite3.h"
122 typedef sqlite3_int64 i64;
123 typedef sqlite3_uint64 u64;
124 typedef unsigned char u8;
125 #if SQLITE_USER_AUTHENTICATION
126 # include "sqlite3userauth.h"
127 #endif
128 #include <ctype.h>
129 #include <stdarg.h>
130
131 #if !defined(_WIN32) && !defined(WIN32)
132 # include <signal.h>
@@ -408,21 +405,21 @@
408 wchar_t *b1, *b2;
409 int sz1, sz2;
410
411 sz1 = (int)strlen(zFilename);
412 sz2 = (int)strlen(zMode);
413 b1 = malloc( (sz1+1)*sizeof(b1[0]) );
414 b2 = malloc( (sz2+1)*sizeof(b1[0]) );
415 if( b1 && b2 ){
416 sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
417 b1[sz1] = 0;
418 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
419 b2[sz2] = 0;
420 fp = _wfopen(b1, b2);
421 }
422 free(b1);
423 free(b2);
424 simBinaryOther = 0;
425 return fp;
426 }
427
428
@@ -434,21 +431,21 @@
434 wchar_t *b1, *b2;
435 int sz1, sz2;
436
437 sz1 = (int)strlen(zCommand);
438 sz2 = (int)strlen(zMode);
439 b1 = malloc( (sz1+1)*sizeof(b1[0]) );
440 b2 = malloc( (sz2+1)*sizeof(b1[0]) );
441 if( b1 && b2 ){
442 sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
443 b1[sz1] = 0;
444 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
445 b2[sz2] = 0;
446 fp = _wpopen(b1, b2);
447 }
448 free(b1);
449 free(b2);
450 return fp;
451 }
452
453 /*
454 ** Work-alike for fgets() from the standard C library.
@@ -458,16 +455,26 @@
458 /* When reading from the command-prompt in Windows, it is necessary
459 ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
460 ** that into UTF-8. Otherwise, non-ASCII characters all get translated
461 ** into '?'.
462 */
463 wchar_t *b1 = malloc( sz*sizeof(wchar_t) );
464 if( b1==0 ) return 0;
465 _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
466 if( fgetws(b1, sz/4, in)==0 ){
467 sqlite3_free(b1);
468 return 0;
 
 
 
 
 
 
 
 
 
 
469 }
470 WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
471 sqlite3_free(b1);
472 return buf;
473 }else{
@@ -519,24 +526,37 @@
519 if( !UseWtextForOutput(out) ){
520 /* Writing to a file or other destination, just write bytes without
521 ** any translation. */
522 return fputs(z, out);
523 }else{
524 /* When writing to the command-prompt in Windows, it is necessary
525 ** to use O_U8TEXT to render Unicode U+0080 and greater. Go ahead
526 ** use O_U8TEXT for everything in text mode.
527 */
528 int sz = (int)strlen(z);
529 wchar_t *b1 = malloc( (sz+1)*sizeof(wchar_t) );
530 if( b1==0 ) return 0;
531 sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
532 b1[sz] = 0;
533 _setmode(_fileno(out), _O_U8TEXT);
534 if( UseBinaryWText(out) ){
535 piecemealOutput(b1, sz, out);
536 }else{
537 fputws(b1, out);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538 }
539 sqlite3_free(b1);
540 return 0;
541 }
542 }
@@ -2347,11 +2367,11 @@
2347 **
2348 ******************************************************************************
2349 **
2350 ** This SQLite extension implements functions that compute SHA3 hashes
2351 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2352 ** Two SQL functions are implemented:
2353 **
2354 ** sha3(X,SIZE)
2355 ** sha3_agg(Y,SIZE)
2356 ** sha3_query(Z,SIZE)
2357 **
@@ -5070,14 +5090,14 @@
5070 char **pzErrMsg,
5071 const sqlite3_api_routines *pApi
5072 ){
5073 int rc = SQLITE_OK;
5074 unsigned int i;
5075 #if defined(SQLITE3_H) || defined(SQLITE_STATIC_PERCENTILE)
5076 (void)pApi; /* Unused parameter */
5077 #else
5078 SQLITE_EXTENSION_INIT2(pApi);
 
 
5079 #endif
5080 (void)pzErrMsg; /* Unused parameter */
5081 for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
5082 rc = sqlite3_create_window_function(db,
5083 aPercentFunc[i].zName,
@@ -6831,35 +6851,41 @@
6831 idxNum |= 0x40;
6832 }
6833 continue;
6834 }
6835 if( pConstraint->iColumn<SERIES_COLUMN_START ){
6836 if( pConstraint->iColumn==SERIES_COLUMN_VALUE ){
6837 switch( op ){
6838 case SQLITE_INDEX_CONSTRAINT_EQ:
6839 case SQLITE_INDEX_CONSTRAINT_IS: {
6840 idxNum |= 0x0080;
6841 idxNum &= ~0x3300;
6842 aIdx[5] = i;
6843 aIdx[6] = -1;
 
6844 bStartSeen = 1;
 
6845 break;
6846 }
6847 case SQLITE_INDEX_CONSTRAINT_GE: {
6848 if( idxNum & 0x0080 ) break;
6849 idxNum |= 0x0100;
6850 idxNum &= ~0x0200;
6851 aIdx[5] = i;
 
6852 bStartSeen = 1;
 
6853 break;
6854 }
6855 case SQLITE_INDEX_CONSTRAINT_GT: {
6856 if( idxNum & 0x0080 ) break;
6857 idxNum |= 0x0200;
6858 idxNum &= ~0x0100;
6859 aIdx[5] = i;
 
6860 bStartSeen = 1;
 
6861 break;
6862 }
6863 case SQLITE_INDEX_CONSTRAINT_LE: {
6864 if( idxNum & 0x0080 ) break;
6865 idxNum |= 0x1000;
@@ -14167,11 +14193,11 @@
14167 /* A view. Or a trigger on a view. */
14168 if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg);
14169 }else{
14170 IdxTable *pTab;
14171 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
14172 if( rc==SQLITE_OK ){
14173 int i;
14174 char *zInner = 0;
14175 char *zOuter = 0;
14176 pTab->pNext = p->pTable;
14177 p->pTable = pTab;
@@ -16235,11 +16261,31 @@
16235 **
16236 ** Similar compiler commands will work on different systems. The key
16237 ** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
16238 ** the shell.c source file will know to include the -vfstrace command-line
16239 ** option and (2) you must compile and link the three source files
16240 ** shell,c, test_vfstrace.c, and sqlite3.c.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16241 */
16242 #include <stdlib.h>
16243 #include <string.h>
16244 /* #include "sqlite3.h" */
16245
@@ -16249,10 +16295,12 @@
16249 */
16250 typedef struct vfstrace_info vfstrace_info;
16251 struct vfstrace_info {
16252 sqlite3_vfs *pRootVfs; /* The underlying real VFS */
16253 int (*xOut)(const char*, void*); /* Send output here */
 
 
16254 void *pOutArg; /* First argument to xOut */
16255 const char *zVfsName; /* Name of this trace-VFS */
16256 sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
16257 };
16258
@@ -16265,10 +16313,42 @@
16265 vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
16266 const char *zFName; /* Base name of the file */
16267 sqlite3_file *pReal; /* The real underlying file */
16268 };
16269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16270 /*
16271 ** Method declarations for vfstrace_file.
16272 */
16273 static int vfstraceClose(sqlite3_file*);
16274 static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
@@ -16329,15 +16409,17 @@
16329 const char *zFormat,
16330 ...
16331 ){
16332 va_list ap;
16333 char *zMsg;
16334 va_start(ap, zFormat);
16335 zMsg = sqlite3_vmprintf(zFormat, ap);
16336 va_end(ap);
16337 pInfo->xOut(zMsg, pInfo->pOutArg);
16338 sqlite3_free(zMsg);
 
 
16339 }
16340
16341 /*
16342 ** Try to convert an error code into a symbolic name for that error code.
16343 */
@@ -16431,18 +16513,26 @@
16431 int i = *pI;
16432 while( zAppend[0] ){ z[i++] = *(zAppend++); }
16433 z[i] = 0;
16434 *pI = i;
16435 }
 
 
 
 
 
 
 
16436
16437 /*
16438 ** Close an vfstrace-file.
16439 */
16440 static int vfstraceClose(sqlite3_file *pFile){
16441 vfstrace_file *p = (vfstrace_file *)pFile;
16442 vfstrace_info *pInfo = p->pInfo;
16443 int rc;
 
16444 vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
16445 rc = p->pReal->pMethods->xClose(p->pReal);
16446 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16447 if( rc==SQLITE_OK ){
16448 sqlite3_free((void*)p->base.pMethods);
@@ -16461,10 +16551,11 @@
16461 sqlite_int64 iOfst
16462 ){
16463 vfstrace_file *p = (vfstrace_file *)pFile;
16464 vfstrace_info *pInfo = p->pInfo;
16465 int rc;
 
16466 vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
16467 pInfo->zVfsName, p->zFName, iAmt, iOfst);
16468 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
16469 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16470 return rc;
@@ -16480,10 +16571,11 @@
16480 sqlite_int64 iOfst
16481 ){
16482 vfstrace_file *p = (vfstrace_file *)pFile;
16483 vfstrace_info *pInfo = p->pInfo;
16484 int rc;
 
16485 vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
16486 pInfo->zVfsName, p->zFName, iAmt, iOfst);
16487 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
16488 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16489 return rc;
@@ -16494,10 +16586,11 @@
16494 */
16495 static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
16496 vfstrace_file *p = (vfstrace_file *)pFile;
16497 vfstrace_info *pInfo = p->pInfo;
16498 int rc;
 
16499 vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
16500 size);
16501 rc = p->pReal->pMethods->xTruncate(p->pReal, size);
16502 vfstrace_printf(pInfo, " -> %d\n", rc);
16503 return rc;
@@ -16518,10 +16611,11 @@
16518 else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
16519 if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
16520 if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
16521 sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
16522 }
 
16523 vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
16524 &zBuf[1]);
16525 rc = p->pReal->pMethods->xSync(p->pReal, flags);
16526 vfstrace_printf(pInfo, " -> %d\n", rc);
16527 return rc;
@@ -16532,10 +16626,11 @@
16532 */
16533 static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
16534 vfstrace_file *p = (vfstrace_file *)pFile;
16535 vfstrace_info *pInfo = p->pInfo;
16536 int rc;
 
16537 vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
16538 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
16539 vfstrace_print_errcode(pInfo, " -> %s,", rc);
16540 vfstrace_printf(pInfo, " size=%lld\n", *pSize);
16541 return rc;
@@ -16560,10 +16655,11 @@
16560 */
16561 static int vfstraceLock(sqlite3_file *pFile, int eLock){
16562 vfstrace_file *p = (vfstrace_file *)pFile;
16563 vfstrace_info *pInfo = p->pInfo;
16564 int rc;
 
16565 vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
16566 lockName(eLock));
16567 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
16568 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16569 return rc;
@@ -16574,10 +16670,11 @@
16574 */
16575 static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
16576 vfstrace_file *p = (vfstrace_file *)pFile;
16577 vfstrace_info *pInfo = p->pInfo;
16578 int rc;
 
16579 vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
16580 lockName(eLock));
16581 rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
16582 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16583 return rc;
@@ -16588,10 +16685,11 @@
16588 */
16589 static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
16590 vfstrace_file *p = (vfstrace_file *)pFile;
16591 vfstrace_info *pInfo = p->pInfo;
16592 int rc;
 
16593 vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
16594 pInfo->zVfsName, p->zFName);
16595 rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
16596 vfstrace_print_errcode(pInfo, " -> %s", rc);
16597 vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16607,10 +16705,11 @@
16607 int rc;
16608 char zBuf[100];
16609 char zBuf2[100];
16610 char *zOp;
16611 char *zRVal = 0;
 
16612 switch( op ){
16613 case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
16614 case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
16615 case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
16616 case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
@@ -16635,10 +16734,83 @@
16635 case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
16636 case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
16637 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
16638 case SQLITE_FCNTL_PRAGMA: {
16639 const char *const* a = (const char*const*)pArg;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16640 sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
16641 zOp = zBuf;
16642 break;
16643 }
16644 case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break;
@@ -16730,10 +16902,11 @@
16730 */
16731 static int vfstraceSectorSize(sqlite3_file *pFile){
16732 vfstrace_file *p = (vfstrace_file *)pFile;
16733 vfstrace_info *pInfo = p->pInfo;
16734 int rc;
 
16735 vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
16736 rc = p->pReal->pMethods->xSectorSize(p->pReal);
16737 vfstrace_printf(pInfo, " -> %d\n", rc);
16738 return rc;
16739 }
@@ -16743,10 +16916,11 @@
16743 */
16744 static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
16745 vfstrace_file *p = (vfstrace_file *)pFile;
16746 vfstrace_info *pInfo = p->pInfo;
16747 int rc;
 
16748 vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
16749 pInfo->zVfsName, p->zFName);
16750 rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
16751 vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
16752 return rc;
@@ -16754,25 +16928,43 @@
16754
16755 /*
16756 ** Shared-memory operations.
16757 */
16758 static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
 
 
 
 
 
 
 
 
 
 
16759 vfstrace_file *p = (vfstrace_file *)pFile;
16760 vfstrace_info *pInfo = p->pInfo;
16761 int rc;
16762 char zLck[100];
16763 int i = 0;
 
16764 memcpy(zLck, "|0", 3);
16765 if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
16766 if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
16767 if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
16768 if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
16769 if( flags & ~(0xf) ){
16770 sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
16771 }
16772 vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d,n=%d,%s)",
16773 pInfo->zVfsName, p->zFName, ofst, n, &zLck[1]);
 
 
 
 
 
 
 
16774 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
16775 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16776 return rc;
16777 }
16778 static int vfstraceShmMap(
@@ -16783,26 +16975,29 @@
16783 void volatile **pp
16784 ){
16785 vfstrace_file *p = (vfstrace_file *)pFile;
16786 vfstrace_info *pInfo = p->pInfo;
16787 int rc;
 
16788 vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
16789 pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
16790 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
16791 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16792 return rc;
16793 }
16794 static void vfstraceShmBarrier(sqlite3_file *pFile){
16795 vfstrace_file *p = (vfstrace_file *)pFile;
16796 vfstrace_info *pInfo = p->pInfo;
 
16797 vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
16798 p->pReal->pMethods->xShmBarrier(p->pReal);
16799 }
16800 static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
16801 vfstrace_file *p = (vfstrace_file *)pFile;
16802 vfstrace_info *pInfo = p->pInfo;
16803 int rc;
 
16804 vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
16805 pInfo->zVfsName, p->zFName, delFlag);
16806 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
16807 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16808 return rc;
@@ -16826,10 +17021,11 @@
16826 sqlite3_vfs *pRoot = pInfo->pRootVfs;
16827 p->pInfo = pInfo;
16828 p->zFName = zName ? fileTail(zName) : "<temp>";
16829 p->pReal = (sqlite3_file *)&p[1];
16830 rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
 
16831 vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
16832 pInfo->zVfsName, p->zFName, flags);
16833 if( p->pReal->pMethods ){
16834 sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
16835 const sqlite3_io_methods *pSub = p->pReal->pMethods;
@@ -16871,10 +17067,11 @@
16871 */
16872 static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
16873 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16874 sqlite3_vfs *pRoot = pInfo->pRootVfs;
16875 int rc;
 
16876 vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
16877 pInfo->zVfsName, zPath, dirSync);
16878 rc = pRoot->xDelete(pRoot, zPath, dirSync);
16879 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16880 return rc;
@@ -16891,10 +17088,11 @@
16891 int *pResOut
16892 ){
16893 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16894 sqlite3_vfs *pRoot = pInfo->pRootVfs;
16895 int rc;
 
16896 vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
16897 pInfo->zVfsName, zPath, flags);
16898 rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
16899 vfstrace_print_errcode(pInfo, " -> %s", rc);
16900 vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16913,10 +17111,11 @@
16913 char *zOut
16914 ){
16915 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16916 sqlite3_vfs *pRoot = pInfo->pRootVfs;
16917 int rc;
 
16918 vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
16919 pInfo->zVfsName, zPath);
16920 rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
16921 vfstrace_print_errcode(pInfo, " -> %s", rc);
16922 vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
@@ -16927,10 +17126,11 @@
16927 ** Open the dynamic library located at zPath and return a handle.
16928 */
16929 static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
16930 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16931 sqlite3_vfs *pRoot = pInfo->pRootVfs;
 
16932 vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
16933 return pRoot->xDlOpen(pRoot, zPath);
16934 }
16935
16936 /*
@@ -16939,10 +17139,11 @@
16939 ** with dynamic libraries.
16940 */
16941 static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
16942 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16943 sqlite3_vfs *pRoot = pInfo->pRootVfs;
 
16944 vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
16945 pRoot->xDlError(pRoot, nByte, zErrMsg);
16946 vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
16947 }
16948
@@ -16960,10 +17161,11 @@
16960 ** Close the dynamic library handle pHandle.
16961 */
16962 static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
16963 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16964 sqlite3_vfs *pRoot = pInfo->pRootVfs;
 
16965 vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
16966 pRoot->xDlClose(pRoot, pHandle);
16967 }
16968
16969 /*
@@ -16971,10 +17173,11 @@
16971 ** random data.
16972 */
16973 static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
16974 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16975 sqlite3_vfs *pRoot = pInfo->pRootVfs;
 
16976 vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
16977 return pRoot->xRandomness(pRoot, nByte, zBufOut);
16978 }
16979
16980 /*
@@ -16982,34 +17185,52 @@
16982 ** actually slept.
16983 */
16984 static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
16985 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16986 sqlite3_vfs *pRoot = pInfo->pRootVfs;
 
 
16987 return pRoot->xSleep(pRoot, nMicro);
16988 }
16989
16990 /*
16991 ** Return the current time as a Julian Day number in *pTimeOut.
16992 */
16993 static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
16994 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
16995 sqlite3_vfs *pRoot = pInfo->pRootVfs;
16996 return pRoot->xCurrentTime(pRoot, pTimeOut);
 
 
 
 
 
16997 }
16998 static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
16999 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17000 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17001 return pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
 
 
 
 
 
17002 }
17003
17004 /*
17005 ** Return th3 most recent error code and message
17006 */
17007 static int vfstraceGetLastError(sqlite3_vfs *pVfs, int iErr, char *zErr){
17008 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17009 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17010 return pRoot->xGetLastError(pRoot, iErr, zErr);
 
 
 
 
 
 
17011 }
17012
17013 /*
17014 ** Override system calls.
17015 */
@@ -17099,10 +17320,12 @@
17099 pInfo->pRootVfs = pRoot;
17100 pInfo->xOut = xOut;
17101 pInfo->pOutArg = pOutArg;
17102 pInfo->zVfsName = pNew->zName;
17103 pInfo->pTraceVfs = pNew;
 
 
17104 vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
17105 pInfo->zVfsName, pRoot->zName);
17106 return sqlite3_vfs_register(pNew, makeDefault);
17107 }
17108
@@ -20228,10 +20451,12 @@
20228 apVal[iField] = sqlite3_value_dup( pVal );
20229 if( apVal[iField]==0 ){
20230 recoverError(p, SQLITE_NOMEM, 0);
20231 }
20232 p1->nVal = iField+1;
 
 
20233 }
20234 p1->iPrevCell = iCell;
20235 p1->iPrevPage = iPage;
20236 }
20237 }else{
@@ -21935,12 +22160,12 @@
21935 const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
21936 int ng = (nAccept<0)? -nAccept : 0;
21937 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
21938 assert(z!=0);
21939 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
21940 char c = *z;
21941 if( (c & 0x80) == 0 ){
21942 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
21943 ++z; /* ASCII */
21944 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
21945 else{
21946 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
@@ -22004,14 +22229,14 @@
22004 }
22005 sqlite3_fputs(zq, out);
22006 }
22007
22008 /*
22009 ** Output the given string as a quoted according to JSON quoting rules.
22010 */
22011 static void output_json_string(FILE *out, const char *z, i64 n){
22012 char c;
22013 static const char *zq = "\"";
22014 static long ctrlMask = ~0L;
22015 static const char *zDQBS = "\"\\";
22016 const char *pcLimit;
22017 char ace[3] = "\\?";
@@ -22027,11 +22252,11 @@
22027 if( pcEnd > z ){
22028 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
22029 z = pcEnd;
22030 }
22031 if( z >= pcLimit ) break;
22032 c = *(z++);
22033 switch( c ){
22034 case '"': case '\\':
22035 cbsSay = (char)c;
22036 break;
22037 case '\b': cbsSay = 'b'; break;
@@ -22042,12 +22267,12 @@
22042 default: cbsSay = 0; break;
22043 }
22044 if( cbsSay ){
22045 ace[1] = cbsSay;
22046 sqlite3_fputs(ace, out);
22047 }else if( c<=0x1f ){
22048 sqlite3_fprintf(out, "u%04x", c);
22049 }else{
22050 ace[1] = (char)c;
22051 sqlite3_fputs(ace+1, out);
22052 }
22053 }
@@ -24784,13 +25009,13 @@
24784 if( rc ){
24785 sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
24786 }else{
24787 rc = SQLITE_CORRUPT;
24788 }
24789 sqlite3_free(zErr);
24790 free(zQ2);
24791 }
 
24792 return rc;
24793 }
24794
24795 /*
24796 ** Text of help messages.
@@ -24849,10 +25074,11 @@
24849 ".databases List names and files of attached databases",
24850 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
24851 #if SQLITE_SHELL_HAVE_RECOVER
24852 ".dbinfo ?DB? Show status information about the database",
24853 #endif
 
24854 ".dump ?OBJECTS? Render database content as SQL",
24855 " Options:",
24856 " --data-only Output only INSERT statements",
24857 " --newlines Allow unescaped newline characters in output",
24858 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
@@ -25654,11 +25880,12 @@
25654 sqlite3_fprintf(stderr,
25655 "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
25656 }
25657 }
25658
25659 #if HAVE_READLINE || HAVE_EDITLINE
 
25660 /*
25661 ** Readline completion callbacks
25662 */
25663 static char *readline_completion_generator(const char *text, int state){
25664 static sqlite3_stmt *pStmt = 0;
@@ -25692,19 +25919,26 @@
25692 #elif HAVE_LINENOISE
25693 /*
25694 ** Linenoise completion callback. Note that the 3rd argument is from
25695 ** the "msteveb" version of linenoise, not the "antirez" version.
25696 */
25697 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc,
25698 void *pUserData){
 
 
 
 
 
25699 i64 nLine = strlen(zLine);
25700 i64 i, iStart;
25701 sqlite3_stmt *pStmt = 0;
25702 char *zSql;
25703 char zBuf[1000];
25704
 
25705 UNUSED_PARAMETER(pUserData);
 
25706 if( nLine>(i64)sizeof(zBuf)-30 ) return;
25707 if( zLine[0]=='.' || zLine[0]=='#') return;
25708 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
25709 if( i==nLine-1 ) return;
25710 iStart = i+1;
@@ -26390,18 +26624,24 @@
26390 #endif
26391
26392 /*
26393 ** Run an SQL command and return the single integer result.
26394 */
26395 static int db_int(sqlite3 *db, const char *zSql){
26396 sqlite3_stmt *pStmt;
26397 int res = 0;
26398 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
 
 
 
 
 
26399 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
26400 res = sqlite3_column_int(pStmt,0);
26401 }
26402 sqlite3_finalize(pStmt);
 
26403 return res;
26404 }
26405
26406 #if SQLITE_SHELL_HAVE_RECOVER
26407 /*
@@ -26500,21 +26740,112 @@
26500 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
26501 }else{
26502 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
26503 }
26504 for(i=0; i<ArraySize(aQuery); i++){
26505 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
26506 int val = db_int(p->db, zSql);
26507 sqlite3_free(zSql);
26508 sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
26509 }
26510 sqlite3_free(zSchemaTab);
26511 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
26512 sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
26513 return 0;
26514 }
26515 #endif /* SQLITE_SHELL_HAVE_RECOVER */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26516
26517 /*
26518 ** Print the given string as an error message.
26519 */
26520 static void shellEmitError(const char *zErr){
@@ -28067,12 +28398,12 @@
28067 }else if( *pDb==0 ){
28068 return 0;
28069 }else{
28070 /* Formulate the columns spec, close the DB, zero *pDb. */
28071 char *zColsSpec = 0;
28072 int hasDupes = db_int(*pDb, zHasDupes);
28073 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
28074 if( hasDupes ){
28075 #ifdef SHELL_COLUMN_RENAME_CLEAN
28076 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
28077 rc_err_oom_die(rc);
28078 #endif
@@ -28083,11 +28414,11 @@
28083 sqlite3_bind_int(pStmt, 1, nDigits);
28084 rc = sqlite3_step(pStmt);
28085 sqlite3_finalize(pStmt);
28086 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
28087 }
28088 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
28089 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
28090 rc_err_oom_die(rc);
28091 rc = sqlite3_step(pStmt);
28092 if( rc==SQLITE_ROW ){
28093 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
@@ -28688,10 +29019,14 @@
28688 }else{
28689 eputz("Usage: .echo on|off\n");
28690 rc = 1;
28691 }
28692 }else
 
 
 
 
28693
28694 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
28695 if( nArg==2 ){
28696 p->autoEQPtest = 0;
28697 if( p->autoEQPtrace ){
@@ -29129,11 +29464,15 @@
29129 /* Below, resources must be freed before exit. */
29130 while( (nSkip--)>0 ){
29131 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
29132 }
29133 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
29134 if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
 
 
 
 
29135 /* Table does not exist. Create it. */
29136 sqlite3 *dbCols = 0;
29137 char *zRenames = 0;
29138 char *zColDefs;
29139 zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
@@ -30201,11 +30540,14 @@
30201 }
30202 close_db(pSrc);
30203 }else
30204 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
30205
30206 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
 
 
 
30207 if( nArg==2 ){
30208 if( cli_strcmp(azArg[1], "vm")==0 ){
30209 p->scanstatsOn = 3;
30210 }else
30211 if( cli_strcmp(azArg[1], "est")==0 ){
@@ -31256,11 +31598,13 @@
31256 { 0x10000000, 1, "OrderBySubq" },
31257 { 0xffffffff, 0, "All" },
31258 };
31259 unsigned int curOpt;
31260 unsigned int newOpt;
 
31261 int ii;
 
31262 sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
31263 newOpt = curOpt;
31264 for(ii=2; ii<nArg; ii++){
31265 const char *z = azArg[ii];
31266 int useLabel = 0;
@@ -31297,28 +31641,32 @@
31297 }
31298 }
31299 }
31300 if( curOpt!=newOpt ){
31301 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
31302 }else if( nArg<3 ){
31303 curOpt = ~newOpt;
 
31304 }
31305 if( newOpt==0 ){
31306 sqlite3_fputs("+All\n", p->out);
31307 }else if( newOpt==0xffffffff ){
31308 sqlite3_fputs("-All\n", p->out);
 
 
 
 
31309 }else{
31310 int jj;
31311 for(jj=0; jj<ArraySize(aLabel); jj++){
31312 unsigned int m = aLabel[jj].mask;
31313 if( !aLabel[jj].bDsply ) continue;
31314 if( (curOpt&m)!=(newOpt&m) ){
31315 sqlite3_fprintf(p->out, "%c%s\n", (newOpt & m)==0 ? '+' : '-',
31316 aLabel[jj].zLabel);
31317 }
31318 }
31319 }
 
31320 rc2 = isOk = 3;
31321 break;
31322 }
31323
31324 /* sqlite3_test_control(int, db, int) */
@@ -31652,73 +32000,10 @@
31652 }
31653 }
31654 }else
31655 #endif
31656
31657 #if SQLITE_USER_AUTHENTICATION
31658 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
31659 if( nArg<2 ){
31660 eputz("Usage: .user SUBCOMMAND ...\n");
31661 rc = 1;
31662 goto meta_command_exit;
31663 }
31664 open_db(p, 0);
31665 if( cli_strcmp(azArg[1],"login")==0 ){
31666 if( nArg!=4 ){
31667 eputz("Usage: .user login USER PASSWORD\n");
31668 rc = 1;
31669 goto meta_command_exit;
31670 }
31671 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
31672 strlen30(azArg[3]));
31673 if( rc ){
31674 sqlite3_fprintf(stderr,"Authentication failed for user %s\n", azArg[2]);
31675 rc = 1;
31676 }
31677 }else if( cli_strcmp(azArg[1],"add")==0 ){
31678 if( nArg!=5 ){
31679 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
31680 rc = 1;
31681 goto meta_command_exit;
31682 }
31683 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31684 booleanValue(azArg[4]));
31685 if( rc ){
31686 sqlite3_fprintf(stderr,"User-Add failed: %d\n", rc);
31687 rc = 1;
31688 }
31689 }else if( cli_strcmp(azArg[1],"edit")==0 ){
31690 if( nArg!=5 ){
31691 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
31692 rc = 1;
31693 goto meta_command_exit;
31694 }
31695 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
31696 booleanValue(azArg[4]));
31697 if( rc ){
31698 sqlite3_fprintf(stderr,"User-Edit failed: %d\n", rc);
31699 rc = 1;
31700 }
31701 }else if( cli_strcmp(azArg[1],"delete")==0 ){
31702 if( nArg!=3 ){
31703 eputz("Usage: .user delete USER\n");
31704 rc = 1;
31705 goto meta_command_exit;
31706 }
31707 rc = sqlite3_user_delete(p->db, azArg[2]);
31708 if( rc ){
31709 sqlite3_fprintf(stderr,"User-Delete failed: %d\n", rc);
31710 rc = 1;
31711 }
31712 }else{
31713 eputz("Usage: .user login|add|edit|delete ...\n");
31714 rc = 1;
31715 goto meta_command_exit;
31716 }
31717 }else
31718 #endif /* SQLITE_USER_AUTHENTICATION */
31719
31720 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
31721 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
31722 sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
31723 sqlite3_libversion(), sqlite3_sourceid());
31724 #if SQLITE_HAVE_ZLIB
@@ -31838,11 +32123,10 @@
31838 SCAN_TRACKER_REFTYPE pst){
31839 char cin;
31840 char cWait = (char)qss; /* intentional narrowing loss */
31841 if( cWait==0 ){
31842 PlainScan:
31843 assert( cWait==0 );
31844 while( (cin = *zLine++)!=0 ){
31845 if( IsSpace(cin) )
31846 continue;
31847 switch (cin){
31848 case '-':
@@ -31890,11 +32174,10 @@
31890 switch( cWait ){
31891 case '*':
31892 if( *zLine != '/' )
31893 continue;
31894 ++zLine;
31895 cWait = 0;
31896 CONTINUE_PROMPT_AWAITC(pst, 0);
31897 qss = QSS_SETV(qss, 0);
31898 goto PlainScan;
31899 case '`': case '\'': case '"':
31900 if(*zLine==cWait){
@@ -31902,11 +32185,10 @@
31902 ++zLine;
31903 continue;
31904 }
31905 deliberate_fall_through;
31906 case ']':
31907 cWait = 0;
31908 CONTINUE_PROMPT_AWAITC(pst, 0);
31909 qss = QSS_SETV(qss, 0);
31910 goto PlainScan;
31911 default: assert(0);
31912 }
@@ -32090,11 +32372,14 @@
32090 if( doAutoDetectRestore(p, zSql) ) return 1;
32091 return 0;
32092 }
32093
32094 static void echo_group_input(ShellState *p, const char *zDo){
32095 if( ShellHasFlag(p, SHFLG_Echo) ) sqlite3_fprintf(p->out, "%s\n", zDo);
 
 
 
32096 }
32097
32098 #ifdef SQLITE_SHELL_FIDDLE
32099 /*
32100 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32550,10 +32835,19 @@
32550 }
32551
32552 static void sayAbnormalExit(void){
32553 if( seenInterrupt ) eputz("Program interrupted.\n");
32554 }
 
 
 
 
 
 
 
 
 
32555
32556 #ifndef SQLITE_SHELL_IS_UTF8
32557 # if (defined(_WIN32) || defined(WIN32)) \
32558 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
32559 # define SQLITE_SHELL_IS_UTF8 (0)
@@ -32787,12 +33081,10 @@
32787 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
32788 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
32789 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
32790 }
32791 }else if( cli_strcmp(z,"-vfstrace")==0 ){
32792 vfstrace_register("trace",0,(int(*)(const char*,void*))sqlite3_fputs,
32793 stderr,1);
32794 bEnableVfstrace = 1;
32795 #ifdef SQLITE_ENABLE_MULTIPLEX
32796 }else if( cli_strcmp(z,"-multiplex")==0 ){
32797 extern int sqlite3_multiplex_initialize(const char*,int);
32798 sqlite3_multiplex_initialize(0, 1);
@@ -32885,10 +33177,13 @@
32885 "%s: Error: no database filename specified\n", Argv0);
32886 return 1;
32887 #endif
32888 }
32889 data.out = stdout;
 
 
 
32890 #ifndef SQLITE_SHELL_FIDDLE
32891 sqlite3_appendvfs_init(0,0,0);
32892 #endif
32893
32894 /* Go ahead and open the database file if it already exists. If the
@@ -33129,19 +33424,14 @@
33129 */
33130 if( stdin_is_interactive ){
33131 char *zHome;
33132 char *zHistory;
33133 int nHistory;
33134 #if CIO_WIN_WC_XLATE
33135 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
33136 #else
33137 # define SHELL_CIO_CHAR_SET ""
33138 #endif
33139 sqlite3_fprintf(stdout,
33140 "SQLite version %s %.19s%s\n" /*extra-version-info*/
33141 "Enter \".help\" for usage hints.\n",
33142 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
33143 if( warnInmemoryDb ){
33144 sputz(stdout, "Connected to a ");
33145 printBold("transient in-memory database");
33146 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
33147 " persistent database.\n");
@@ -33154,13 +33444,15 @@
33154 if( (zHistory = malloc(nHistory))!=0 ){
33155 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
33156 }
33157 }
33158 if( zHistory ){ shell_read_history(zHistory); }
33159 #if HAVE_READLINE || HAVE_EDITLINE
33160 rl_attempted_completion_function = readline_completion;
33161 #elif HAVE_LINENOISE
 
 
33162 linenoiseSetCompletionCallback(linenoise_completion, NULL);
33163 #endif
33164 data.in = 0;
33165 rc = process_input(&data);
33166 if( zHistory ){
33167
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -120,13 +120,10 @@
120 #include <math.h>
121 #include "sqlite3.h"
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>
@@ -408,21 +405,21 @@
405 wchar_t *b1, *b2;
406 int sz1, sz2;
407
408 sz1 = (int)strlen(zFilename);
409 sz2 = (int)strlen(zMode);
410 b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
411 b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
412 if( b1 && b2 ){
413 sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1);
414 b1[sz1] = 0;
415 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
416 b2[sz2] = 0;
417 fp = _wfopen(b1, b2);
418 }
419 sqlite3_free(b1);
420 sqlite3_free(b2);
421 simBinaryOther = 0;
422 return fp;
423 }
424
425
@@ -434,21 +431,21 @@
431 wchar_t *b1, *b2;
432 int sz1, sz2;
433
434 sz1 = (int)strlen(zCommand);
435 sz2 = (int)strlen(zMode);
436 b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) );
437 b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) );
438 if( b1 && b2 ){
439 sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1);
440 b1[sz1] = 0;
441 sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2);
442 b2[sz2] = 0;
443 fp = _wpopen(b1, b2);
444 }
445 sqlite3_free(b1);
446 sqlite3_free(b2);
447 return fp;
448 }
449
450 /*
451 ** Work-alike for fgets() from the standard C library.
@@ -458,16 +455,26 @@
455 /* When reading from the command-prompt in Windows, it is necessary
456 ** to use _O_WTEXT input mode to read UTF-16 characters, then translate
457 ** that into UTF-8. Otherwise, non-ASCII characters all get translated
458 ** into '?'.
459 */
460 wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) );
461 if( b1==0 ) return 0;
462 #ifndef SQLITE_USE_STDIO_FOR_CONSOLE
463 DWORD nRead = 0;
464 if( IsConsole(in)
465 && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz, &nRead, 0)
466 ){
467 b1[nRead] = 0;
468 }else
469 #endif
470 {
471 _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT);
472 if( fgetws(b1, sz/4, in)==0 ){
473 sqlite3_free(b1);
474 return 0;
475 }
476 }
477 WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0);
478 sqlite3_free(b1);
479 return buf;
480 }else{
@@ -519,24 +526,37 @@
526 if( !UseWtextForOutput(out) ){
527 /* Writing to a file or other destination, just write bytes without
528 ** any translation. */
529 return fputs(z, out);
530 }else{
531 /* One must use UTF16 in order to get unicode support when writing
532 ** to the console on Windows.
 
533 */
534 int sz = (int)strlen(z);
535 wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) );
536 if( b1==0 ) return 0;
537 sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz);
538 b1[sz] = 0;
539
540 #ifndef SQLITE_STDIO_FOR_CONSOLE
541 DWORD nWr = 0;
542 if( IsConsole(out)
543 && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0)
544 ){
545 /* If writing to the console, then the WriteConsoleW() is all we
546 ** need to do. */
547 }else
548 #endif
549 {
550 /* For non-console I/O, or if SQLITE_USE_STDIO_FOR_CONSOLE is defined
551 ** then write using the standard library. */
552 _setmode(_fileno(out), _O_U8TEXT);
553 if( UseBinaryWText(out) ){
554 piecemealOutput(b1, sz, out);
555 }else{
556 fputws(b1, out);
557 }
558 }
559 sqlite3_free(b1);
560 return 0;
561 }
562 }
@@ -2347,11 +2367,11 @@
2367 **
2368 ******************************************************************************
2369 **
2370 ** This SQLite extension implements functions that compute SHA3 hashes
2371 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2372 ** Three SQL functions are implemented:
2373 **
2374 ** sha3(X,SIZE)
2375 ** sha3_agg(Y,SIZE)
2376 ** sha3_query(Z,SIZE)
2377 **
@@ -5070,14 +5090,14 @@
5090 char **pzErrMsg,
5091 const sqlite3_api_routines *pApi
5092 ){
5093 int rc = SQLITE_OK;
5094 unsigned int i;
5095 #ifdef SQLITE3EXT_H
 
 
5096 SQLITE_EXTENSION_INIT2(pApi);
5097 #else
5098 (void)pApi; /* Unused parameter */
5099 #endif
5100 (void)pzErrMsg; /* Unused parameter */
5101 for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){
5102 rc = sqlite3_create_window_function(db,
5103 aPercentFunc[i].zName,
@@ -6831,35 +6851,41 @@
6851 idxNum |= 0x40;
6852 }
6853 continue;
6854 }
6855 if( pConstraint->iColumn<SERIES_COLUMN_START ){
6856 if( pConstraint->iColumn==SERIES_COLUMN_VALUE && pConstraint->usable ){
6857 switch( op ){
6858 case SQLITE_INDEX_CONSTRAINT_EQ:
6859 case SQLITE_INDEX_CONSTRAINT_IS: {
6860 idxNum |= 0x0080;
6861 idxNum &= ~0x3300;
6862 aIdx[5] = i;
6863 aIdx[6] = -1;
6864 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6865 bStartSeen = 1;
6866 #endif
6867 break;
6868 }
6869 case SQLITE_INDEX_CONSTRAINT_GE: {
6870 if( idxNum & 0x0080 ) break;
6871 idxNum |= 0x0100;
6872 idxNum &= ~0x0200;
6873 aIdx[5] = i;
6874 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6875 bStartSeen = 1;
6876 #endif
6877 break;
6878 }
6879 case SQLITE_INDEX_CONSTRAINT_GT: {
6880 if( idxNum & 0x0080 ) break;
6881 idxNum |= 0x0200;
6882 idxNum &= ~0x0100;
6883 aIdx[5] = i;
6884 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
6885 bStartSeen = 1;
6886 #endif
6887 break;
6888 }
6889 case SQLITE_INDEX_CONSTRAINT_LE: {
6890 if( idxNum & 0x0080 ) break;
6891 idxNum |= 0x1000;
@@ -14167,11 +14193,11 @@
14193 /* A view. Or a trigger on a view. */
14194 if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg);
14195 }else{
14196 IdxTable *pTab;
14197 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
14198 if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){
14199 int i;
14200 char *zInner = 0;
14201 char *zOuter = 0;
14202 pTab->pNext = p->pTable;
14203 p->pTable = pTab;
@@ -16235,11 +16261,31 @@
16261 **
16262 ** Similar compiler commands will work on different systems. The key
16263 ** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that
16264 ** the shell.c source file will know to include the -vfstrace command-line
16265 ** option and (2) you must compile and link the three source files
16266 ** shell,c, test_vfstrace.c, and sqlite3.c.
16267 **
16268 ** RUNTIME CONTROL OF VFSTRACE OUTPUT
16269 **
16270 ** The application can use the "vfstrace" pragma to control which VFS
16271 ** APIs are traced. To disable all output:
16272 **
16273 ** PRAGMA vfstrace('-all');
16274 **
16275 ** To enable all output (which is the default setting):
16276 **
16277 ** PRAGMA vfstrace('+all');
16278 **
16279 ** Individual APIs can be enabled or disabled by name, with or without
16280 ** the initial "x" character. For example, to set up for tracing lock
16281 ** primatives only:
16282 **
16283 ** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock');
16284 **
16285 ** The argument to the vfstrace pragma ignores capitalization and any
16286 ** characters other than alphabetics, '+', and '-'.
16287 */
16288 #include <stdlib.h>
16289 #include <string.h>
16290 /* #include "sqlite3.h" */
16291
@@ -16249,10 +16295,12 @@
16295 */
16296 typedef struct vfstrace_info vfstrace_info;
16297 struct vfstrace_info {
16298 sqlite3_vfs *pRootVfs; /* The underlying real VFS */
16299 int (*xOut)(const char*, void*); /* Send output here */
16300 unsigned int mTrace; /* Mask of interfaces to trace */
16301 u8 bOn; /* Tracing on/off */
16302 void *pOutArg; /* First argument to xOut */
16303 const char *zVfsName; /* Name of this trace-VFS */
16304 sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */
16305 };
16306
@@ -16265,10 +16313,42 @@
16313 vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */
16314 const char *zFName; /* Base name of the file */
16315 sqlite3_file *pReal; /* The real underlying file */
16316 };
16317
16318 /*
16319 ** Bit values for vfstrace_info.mTrace.
16320 */
16321 #define VTR_CLOSE 0x00000001
16322 #define VTR_READ 0x00000002
16323 #define VTR_WRITE 0x00000004
16324 #define VTR_TRUNC 0x00000008
16325 #define VTR_SYNC 0x00000010
16326 #define VTR_FSIZE 0x00000020
16327 #define VTR_LOCK 0x00000040
16328 #define VTR_UNLOCK 0x00000080
16329 #define VTR_CRL 0x00000100
16330 #define VTR_FCTRL 0x00000200
16331 #define VTR_SECSZ 0x00000400
16332 #define VTR_DEVCHAR 0x00000800
16333 #define VTR_SHMLOCK 0x00001000
16334 #define VTR_SHMMAP 0x00002000
16335 #define VTR_SHMBAR 0x00004000
16336 #define VTR_SHMUNMAP 0x00008000
16337 #define VTR_OPEN 0x00010000
16338 #define VTR_DELETE 0x00020000
16339 #define VTR_ACCESS 0x00040000
16340 #define VTR_FULLPATH 0x00080000
16341 #define VTR_DLOPEN 0x00100000
16342 #define VTR_DLERR 0x00200000
16343 #define VTR_DLSYM 0x00400000
16344 #define VTR_DLCLOSE 0x00800000
16345 #define VTR_RAND 0x01000000
16346 #define VTR_SLEEP 0x02000000
16347 #define VTR_CURTIME 0x04000000
16348 #define VTR_LASTERR 0x08000000
16349
16350 /*
16351 ** Method declarations for vfstrace_file.
16352 */
16353 static int vfstraceClose(sqlite3_file*);
16354 static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
@@ -16329,15 +16409,17 @@
16409 const char *zFormat,
16410 ...
16411 ){
16412 va_list ap;
16413 char *zMsg;
16414 if( pInfo->bOn ){
16415 va_start(ap, zFormat);
16416 zMsg = sqlite3_vmprintf(zFormat, ap);
16417 va_end(ap);
16418 pInfo->xOut(zMsg, pInfo->pOutArg);
16419 sqlite3_free(zMsg);
16420 }
16421 }
16422
16423 /*
16424 ** Try to convert an error code into a symbolic name for that error code.
16425 */
@@ -16431,18 +16513,26 @@
16513 int i = *pI;
16514 while( zAppend[0] ){ z[i++] = *(zAppend++); }
16515 z[i] = 0;
16516 *pI = i;
16517 }
16518
16519 /*
16520 ** Turn tracing output on or off according to mMask.
16521 */
16522 static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){
16523 pInfo->bOn = (pInfo->mTrace & mMask)!=0;
16524 }
16525
16526 /*
16527 ** Close an vfstrace-file.
16528 */
16529 static int vfstraceClose(sqlite3_file *pFile){
16530 vfstrace_file *p = (vfstrace_file *)pFile;
16531 vfstrace_info *pInfo = p->pInfo;
16532 int rc;
16533 vfstraceOnOff(pInfo, VTR_CLOSE);
16534 vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
16535 rc = p->pReal->pMethods->xClose(p->pReal);
16536 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16537 if( rc==SQLITE_OK ){
16538 sqlite3_free((void*)p->base.pMethods);
@@ -16461,10 +16551,11 @@
16551 sqlite_int64 iOfst
16552 ){
16553 vfstrace_file *p = (vfstrace_file *)pFile;
16554 vfstrace_info *pInfo = p->pInfo;
16555 int rc;
16556 vfstraceOnOff(pInfo, VTR_READ);
16557 vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)",
16558 pInfo->zVfsName, p->zFName, iAmt, iOfst);
16559 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
16560 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16561 return rc;
@@ -16480,10 +16571,11 @@
16571 sqlite_int64 iOfst
16572 ){
16573 vfstrace_file *p = (vfstrace_file *)pFile;
16574 vfstrace_info *pInfo = p->pInfo;
16575 int rc;
16576 vfstraceOnOff(pInfo, VTR_WRITE);
16577 vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)",
16578 pInfo->zVfsName, p->zFName, iAmt, iOfst);
16579 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
16580 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16581 return rc;
@@ -16494,10 +16586,11 @@
16586 */
16587 static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){
16588 vfstrace_file *p = (vfstrace_file *)pFile;
16589 vfstrace_info *pInfo = p->pInfo;
16590 int rc;
16591 vfstraceOnOff(pInfo, VTR_TRUNC);
16592 vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName,
16593 size);
16594 rc = p->pReal->pMethods->xTruncate(p->pReal, size);
16595 vfstrace_printf(pInfo, " -> %d\n", rc);
16596 return rc;
@@ -16518,10 +16611,11 @@
16611 else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL");
16612 if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY");
16613 if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){
16614 sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags);
16615 }
16616 vfstraceOnOff(pInfo, VTR_SYNC);
16617 vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName,
16618 &zBuf[1]);
16619 rc = p->pReal->pMethods->xSync(p->pReal, flags);
16620 vfstrace_printf(pInfo, " -> %d\n", rc);
16621 return rc;
@@ -16532,10 +16626,11 @@
16626 */
16627 static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
16628 vfstrace_file *p = (vfstrace_file *)pFile;
16629 vfstrace_info *pInfo = p->pInfo;
16630 int rc;
16631 vfstraceOnOff(pInfo, VTR_FSIZE);
16632 vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName);
16633 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
16634 vfstrace_print_errcode(pInfo, " -> %s,", rc);
16635 vfstrace_printf(pInfo, " size=%lld\n", *pSize);
16636 return rc;
@@ -16560,10 +16655,11 @@
16655 */
16656 static int vfstraceLock(sqlite3_file *pFile, int eLock){
16657 vfstrace_file *p = (vfstrace_file *)pFile;
16658 vfstrace_info *pInfo = p->pInfo;
16659 int rc;
16660 vfstraceOnOff(pInfo, VTR_LOCK);
16661 vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName,
16662 lockName(eLock));
16663 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
16664 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16665 return rc;
@@ -16574,10 +16670,11 @@
16670 */
16671 static int vfstraceUnlock(sqlite3_file *pFile, int eLock){
16672 vfstrace_file *p = (vfstrace_file *)pFile;
16673 vfstrace_info *pInfo = p->pInfo;
16674 int rc;
16675 vfstraceOnOff(pInfo, VTR_UNLOCK);
16676 vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName,
16677 lockName(eLock));
16678 rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
16679 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16680 return rc;
@@ -16588,10 +16685,11 @@
16685 */
16686 static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){
16687 vfstrace_file *p = (vfstrace_file *)pFile;
16688 vfstrace_info *pInfo = p->pInfo;
16689 int rc;
16690 vfstraceOnOff(pInfo, VTR_CRL);
16691 vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)",
16692 pInfo->zVfsName, p->zFName);
16693 rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
16694 vfstrace_print_errcode(pInfo, " -> %s", rc);
16695 vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16607,10 +16705,11 @@
16705 int rc;
16706 char zBuf[100];
16707 char zBuf2[100];
16708 char *zOp;
16709 char *zRVal = 0;
16710 vfstraceOnOff(pInfo, VTR_FCTRL);
16711 switch( op ){
16712 case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break;
16713 case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break;
16714 case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break;
16715 case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break;
@@ -16635,10 +16734,83 @@
16734 case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break;
16735 case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break;
16736 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break;
16737 case SQLITE_FCNTL_PRAGMA: {
16738 const char *const* a = (const char*const*)pArg;
16739 if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){
16740 const u8 *zArg = (const u8*)a[2];
16741 if( zArg[0]>='0' && zArg[0]<=9 ){
16742 pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0);
16743 }else{
16744 static const struct {
16745 const char *z;
16746 unsigned int m;
16747 } aKw[] = {
16748 { "all", 0xffffffff },
16749 { "close", VTR_CLOSE },
16750 { "read", VTR_READ },
16751 { "write", VTR_WRITE },
16752 { "truncate", VTR_TRUNC },
16753 { "sync", VTR_SYNC },
16754 { "filesize", VTR_FSIZE },
16755 { "lock", VTR_LOCK },
16756 { "unlock", VTR_UNLOCK },
16757 { "checkreservedlock", VTR_CRL },
16758 { "filecontrol", VTR_FCTRL },
16759 { "sectorsize", VTR_SECSZ },
16760 { "devicecharacteristics", VTR_DEVCHAR },
16761 { "shmlock", VTR_SHMLOCK },
16762 { "shmmap", VTR_SHMMAP },
16763 { "shmummap", VTR_SHMUNMAP },
16764 { "shmbarrier", VTR_SHMBAR },
16765 { "open", VTR_OPEN },
16766 { "delete", VTR_DELETE },
16767 { "access", VTR_ACCESS },
16768 { "fullpathname", VTR_FULLPATH },
16769 { "dlopen", VTR_DLOPEN },
16770 { "dlerror", VTR_DLERR },
16771 { "dlsym", VTR_DLSYM },
16772 { "dlclose", VTR_DLCLOSE },
16773 { "randomness", VTR_RAND },
16774 { "sleep", VTR_SLEEP },
16775 { "currenttime", VTR_CURTIME },
16776 { "currenttimeint64", VTR_CURTIME },
16777 { "getlasterror", VTR_LASTERR },
16778 };
16779 int onOff = 1;
16780 while( zArg[0] ){
16781 int jj, n;
16782 while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+'
16783 && !isalpha(zArg[0]) ) zArg++;
16784 if( zArg[0]==0 ) break;
16785 if( zArg[0]=='-' ){
16786 onOff = 0;
16787 zArg++;
16788 }else if( zArg[0]=='+' ){
16789 onOff = 1;
16790 zArg++;
16791 }
16792 while( !isalpha(zArg[0]) ){
16793 if( zArg[0]==0 ) break;
16794 zArg++;
16795 }
16796 if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++;
16797 for(n=0; isalpha(zArg[n]); n++){}
16798 for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){
16799 if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){
16800 if( onOff ){
16801 pInfo->mTrace |= aKw[jj].m;
16802 }else{
16803 pInfo->mTrace &= ~aKw[jj].m;
16804 }
16805 break;
16806 }
16807 }
16808 zArg += n;
16809 }
16810 }
16811 }
16812 sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]);
16813 zOp = zBuf;
16814 break;
16815 }
16816 case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break;
@@ -16730,10 +16902,11 @@
16902 */
16903 static int vfstraceSectorSize(sqlite3_file *pFile){
16904 vfstrace_file *p = (vfstrace_file *)pFile;
16905 vfstrace_info *pInfo = p->pInfo;
16906 int rc;
16907 vfstraceOnOff(pInfo, VTR_SECSZ);
16908 vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName);
16909 rc = p->pReal->pMethods->xSectorSize(p->pReal);
16910 vfstrace_printf(pInfo, " -> %d\n", rc);
16911 return rc;
16912 }
@@ -16743,10 +16916,11 @@
16916 */
16917 static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){
16918 vfstrace_file *p = (vfstrace_file *)pFile;
16919 vfstrace_info *pInfo = p->pInfo;
16920 int rc;
16921 vfstraceOnOff(pInfo, VTR_DEVCHAR);
16922 vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)",
16923 pInfo->zVfsName, p->zFName);
16924 rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
16925 vfstrace_printf(pInfo, " -> 0x%08x\n", rc);
16926 return rc;
@@ -16754,25 +16928,43 @@
16928
16929 /*
16930 ** Shared-memory operations.
16931 */
16932 static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
16933 static const char *azLockName[] = {
16934 "WRITE",
16935 "CKPT",
16936 "RECOVER",
16937 "READ0",
16938 "READ1",
16939 "READ2",
16940 "READ3",
16941 "READ4",
16942 };
16943 vfstrace_file *p = (vfstrace_file *)pFile;
16944 vfstrace_info *pInfo = p->pInfo;
16945 int rc;
16946 char zLck[100];
16947 int i = 0;
16948 vfstraceOnOff(pInfo, VTR_SHMLOCK);
16949 memcpy(zLck, "|0", 3);
16950 if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK");
16951 if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK");
16952 if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED");
16953 if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE");
16954 if( flags & ~(0xf) ){
16955 sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags);
16956 }
16957 if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){
16958 vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)",
16959 pInfo->zVfsName, p->zFName, ofst, azLockName[ofst],
16960 n, &zLck[1]);
16961 }else{
16962 vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)",
16963 pInfo->zVfsName, p->zFName, ofst,
16964 n, &zLck[1]);
16965 }
16966 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
16967 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16968 return rc;
16969 }
16970 static int vfstraceShmMap(
@@ -16783,26 +16975,29 @@
16975 void volatile **pp
16976 ){
16977 vfstrace_file *p = (vfstrace_file *)pFile;
16978 vfstrace_info *pInfo = p->pInfo;
16979 int rc;
16980 vfstraceOnOff(pInfo, VTR_SHMMAP);
16981 vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)",
16982 pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite);
16983 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
16984 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
16985 return rc;
16986 }
16987 static void vfstraceShmBarrier(sqlite3_file *pFile){
16988 vfstrace_file *p = (vfstrace_file *)pFile;
16989 vfstrace_info *pInfo = p->pInfo;
16990 vfstraceOnOff(pInfo, VTR_SHMBAR);
16991 vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName);
16992 p->pReal->pMethods->xShmBarrier(p->pReal);
16993 }
16994 static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){
16995 vfstrace_file *p = (vfstrace_file *)pFile;
16996 vfstrace_info *pInfo = p->pInfo;
16997 int rc;
16998 vfstraceOnOff(pInfo, VTR_SHMUNMAP);
16999 vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)",
17000 pInfo->zVfsName, p->zFName, delFlag);
17001 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
17002 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
17003 return rc;
@@ -16826,10 +17021,11 @@
17021 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17022 p->pInfo = pInfo;
17023 p->zFName = zName ? fileTail(zName) : "<temp>";
17024 p->pReal = (sqlite3_file *)&p[1];
17025 rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
17026 vfstraceOnOff(pInfo, VTR_OPEN);
17027 vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",
17028 pInfo->zVfsName, p->zFName, flags);
17029 if( p->pReal->pMethods ){
17030 sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) );
17031 const sqlite3_io_methods *pSub = p->pReal->pMethods;
@@ -16871,10 +17067,11 @@
17067 */
17068 static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
17069 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17070 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17071 int rc;
17072 vfstraceOnOff(pInfo, VTR_DELETE);
17073 vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)",
17074 pInfo->zVfsName, zPath, dirSync);
17075 rc = pRoot->xDelete(pRoot, zPath, dirSync);
17076 vfstrace_print_errcode(pInfo, " -> %s\n", rc);
17077 return rc;
@@ -16891,10 +17088,11 @@
17088 int *pResOut
17089 ){
17090 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17091 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17092 int rc;
17093 vfstraceOnOff(pInfo, VTR_ACCESS);
17094 vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)",
17095 pInfo->zVfsName, zPath, flags);
17096 rc = pRoot->xAccess(pRoot, zPath, flags, pResOut);
17097 vfstrace_print_errcode(pInfo, " -> %s", rc);
17098 vfstrace_printf(pInfo, ", out=%d\n", *pResOut);
@@ -16913,10 +17111,11 @@
17111 char *zOut
17112 ){
17113 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17114 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17115 int rc;
17116 vfstraceOnOff(pInfo, VTR_FULLPATH);
17117 vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")",
17118 pInfo->zVfsName, zPath);
17119 rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut);
17120 vfstrace_print_errcode(pInfo, " -> %s", rc);
17121 vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut);
@@ -16927,10 +17126,11 @@
17126 ** Open the dynamic library located at zPath and return a handle.
17127 */
17128 static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){
17129 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17130 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17131 vfstraceOnOff(pInfo, VTR_DLOPEN);
17132 vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath);
17133 return pRoot->xDlOpen(pRoot, zPath);
17134 }
17135
17136 /*
@@ -16939,10 +17139,11 @@
17139 ** with dynamic libraries.
17140 */
17141 static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
17142 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17143 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17144 vfstraceOnOff(pInfo, VTR_DLERR);
17145 vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte);
17146 pRoot->xDlError(pRoot, nByte, zErrMsg);
17147 vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg);
17148 }
17149
@@ -16960,10 +17161,11 @@
17161 ** Close the dynamic library handle pHandle.
17162 */
17163 static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){
17164 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17165 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17166 vfstraceOnOff(pInfo, VTR_DLCLOSE);
17167 vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName);
17168 pRoot->xDlClose(pRoot, pHandle);
17169 }
17170
17171 /*
@@ -16971,10 +17173,11 @@
17173 ** random data.
17174 */
17175 static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
17176 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17177 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17178 vfstraceOnOff(pInfo, VTR_RAND);
17179 vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte);
17180 return pRoot->xRandomness(pRoot, nByte, zBufOut);
17181 }
17182
17183 /*
@@ -16982,34 +17185,52 @@
17185 ** actually slept.
17186 */
17187 static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){
17188 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17189 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17190 vfstraceOnOff(pInfo, VTR_SLEEP);
17191 vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro);
17192 return pRoot->xSleep(pRoot, nMicro);
17193 }
17194
17195 /*
17196 ** Return the current time as a Julian Day number in *pTimeOut.
17197 */
17198 static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
17199 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17200 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17201 int rc;
17202 vfstraceOnOff(pInfo, VTR_CURTIME);
17203 vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName);
17204 rc = pRoot->xCurrentTime(pRoot, pTimeOut);
17205 vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut);
17206 return rc;
17207 }
17208 static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
17209 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17210 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17211 int rc;
17212 vfstraceOnOff(pInfo, VTR_CURTIME);
17213 vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName);
17214 rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut);
17215 vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut);
17216 return rc;
17217 }
17218
17219 /*
17220 ** Return the most recent error code and message
17221 */
17222 static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){
17223 vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData;
17224 sqlite3_vfs *pRoot = pInfo->pRootVfs;
17225 int rc;
17226 vfstraceOnOff(pInfo, VTR_LASTERR);
17227 vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr);
17228 if( nErr ) zErr[0] = 0;
17229 rc = pRoot->xGetLastError(pRoot, nErr, zErr);
17230 vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc);
17231 return rc;
17232 }
17233
17234 /*
17235 ** Override system calls.
17236 */
@@ -17099,10 +17320,12 @@
17320 pInfo->pRootVfs = pRoot;
17321 pInfo->xOut = xOut;
17322 pInfo->pOutArg = pOutArg;
17323 pInfo->zVfsName = pNew->zName;
17324 pInfo->pTraceVfs = pNew;
17325 pInfo->mTrace = 0xffffffff;
17326 pInfo->bOn = 1;
17327 vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n",
17328 pInfo->zVfsName, pRoot->zName);
17329 return sqlite3_vfs_register(pNew, makeDefault);
17330 }
17331
@@ -20228,10 +20451,12 @@
20451 apVal[iField] = sqlite3_value_dup( pVal );
20452 if( apVal[iField]==0 ){
20453 recoverError(p, SQLITE_NOMEM, 0);
20454 }
20455 p1->nVal = iField+1;
20456 }else if( pTab->nCol==0 ){
20457 p1->nVal = pTab->nCol;
20458 }
20459 p1->iPrevCell = iCell;
20460 p1->iPrevPage = iPage;
20461 }
20462 }else{
@@ -21935,12 +22160,12 @@
22160 const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
22161 int ng = (nAccept<0)? -nAccept : 0;
22162 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
22163 assert(z!=0);
22164 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
22165 unsigned char c = *(u8*)z;
22166 if( c<0x7f ){
22167 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
22168 ++z; /* ASCII */
22169 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
22170 else{
22171 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
@@ -22004,14 +22229,14 @@
22229 }
22230 sqlite3_fputs(zq, out);
22231 }
22232
22233 /*
22234 ** Output the given string as quoted according to JSON quoting rules.
22235 */
22236 static void output_json_string(FILE *out, const char *z, i64 n){
22237 unsigned char c;
22238 static const char *zq = "\"";
22239 static long ctrlMask = ~0L;
22240 static const char *zDQBS = "\"\\";
22241 const char *pcLimit;
22242 char ace[3] = "\\?";
@@ -22027,11 +22252,11 @@
22252 if( pcEnd > z ){
22253 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
22254 z = pcEnd;
22255 }
22256 if( z >= pcLimit ) break;
22257 c = (unsigned char)*(z++);
22258 switch( c ){
22259 case '"': case '\\':
22260 cbsSay = (char)c;
22261 break;
22262 case '\b': cbsSay = 'b'; break;
@@ -22042,12 +22267,12 @@
22267 default: cbsSay = 0; break;
22268 }
22269 if( cbsSay ){
22270 ace[1] = cbsSay;
22271 sqlite3_fputs(ace, out);
22272 }else if( c<=0x1f || c>=0x7f ){
22273 sqlite3_fprintf(out, "\\u%04x", c);
22274 }else{
22275 ace[1] = (char)c;
22276 sqlite3_fputs(ace+1, out);
22277 }
22278 }
@@ -24784,13 +25009,13 @@
25009 if( rc ){
25010 sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
25011 }else{
25012 rc = SQLITE_CORRUPT;
25013 }
 
25014 free(zQ2);
25015 }
25016 sqlite3_free(zErr);
25017 return rc;
25018 }
25019
25020 /*
25021 ** Text of help messages.
@@ -24849,10 +25074,11 @@
25074 ".databases List names and files of attached databases",
25075 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
25076 #if SQLITE_SHELL_HAVE_RECOVER
25077 ".dbinfo ?DB? Show status information about the database",
25078 #endif
25079 ".dbtotxt Hex dump of the database file",
25080 ".dump ?OBJECTS? Render database content as SQL",
25081 " Options:",
25082 " --data-only Output only INSERT statements",
25083 " --newlines Allow unescaped newline characters in output",
25084 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
@@ -25654,11 +25880,12 @@
25880 sqlite3_fprintf(stderr,
25881 "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
25882 }
25883 }
25884
25885 #if (HAVE_READLINE || HAVE_EDITLINE) \
25886 && !defined(SQLITE_OMIT_READLINE_COMPLETION)
25887 /*
25888 ** Readline completion callbacks
25889 */
25890 static char *readline_completion_generator(const char *text, int state){
25891 static sqlite3_stmt *pStmt = 0;
@@ -25692,19 +25919,26 @@
25919 #elif HAVE_LINENOISE
25920 /*
25921 ** Linenoise completion callback. Note that the 3rd argument is from
25922 ** the "msteveb" version of linenoise, not the "antirez" version.
25923 */
25924 static void linenoise_completion(
25925 const char *zLine,
25926 linenoiseCompletions *lc
25927 #if HAVE_LINENOISE==2
25928 ,void *pUserData
25929 #endif
25930 ){
25931 i64 nLine = strlen(zLine);
25932 i64 i, iStart;
25933 sqlite3_stmt *pStmt = 0;
25934 char *zSql;
25935 char zBuf[1000];
25936
25937 #if HAVE_LINENOISE==2
25938 UNUSED_PARAMETER(pUserData);
25939 #endif
25940 if( nLine>(i64)sizeof(zBuf)-30 ) return;
25941 if( zLine[0]=='.' || zLine[0]=='#') return;
25942 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
25943 if( i==nLine-1 ) return;
25944 iStart = i+1;
@@ -26390,18 +26624,24 @@
26624 #endif
26625
26626 /*
26627 ** Run an SQL command and return the single integer result.
26628 */
26629 static int db_int(sqlite3 *db, const char *zSql, ...){
26630 sqlite3_stmt *pStmt;
26631 int res = 0;
26632 char *z;
26633 va_list ap;
26634 va_start(ap, zSql);
26635 z = sqlite3_vmprintf(zSql, ap);
26636 va_end(ap);
26637 sqlite3_prepare_v2(db, z, -1, &pStmt, 0);
26638 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
26639 res = sqlite3_column_int(pStmt,0);
26640 }
26641 sqlite3_finalize(pStmt);
26642 sqlite3_free(z);
26643 return res;
26644 }
26645
26646 #if SQLITE_SHELL_HAVE_RECOVER
26647 /*
@@ -26500,21 +26740,112 @@
26740 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
26741 }else{
26742 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
26743 }
26744 for(i=0; i<ArraySize(aQuery); i++){
26745 int val = db_int(p->db, aQuery[i].zSql, zSchemaTab);
 
 
26746 sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
26747 }
26748 sqlite3_free(zSchemaTab);
26749 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
26750 sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
26751 return 0;
26752 }
26753 #endif /* SQLITE_SHELL_HAVE_RECOVER */
26754
26755 /*
26756 ** Implementation of the ".dbtotxt" command.
26757 **
26758 ** Return 1 on error, 2 to exit, and 0 otherwise.
26759 */
26760 static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){
26761 sqlite3_stmt *pStmt = 0;
26762 sqlite3_int64 nPage = 0;
26763 int pgSz = 0;
26764 const char *zTail;
26765 char *zName = 0;
26766 int rc, i, j;
26767 unsigned char bShow[256]; /* Characters ok to display */
26768
26769 UNUSED_PARAMETER(nArg);
26770 UNUSED_PARAMETER(azArg);
26771 memset(bShow, '.', sizeof(bShow));
26772 for(i=' '; i<='~'; i++){
26773 if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
26774 }
26775 rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0);
26776 if( rc ) goto dbtotxt_error;
26777 rc = 0;
26778 if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26779 pgSz = sqlite3_column_int(pStmt, 0);
26780 sqlite3_finalize(pStmt);
26781 pStmt = 0;
26782 if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error;
26783 rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0);
26784 if( rc ) goto dbtotxt_error;
26785 rc = 0;
26786 if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error;
26787 nPage = sqlite3_column_int64(pStmt, 0);
26788 sqlite3_finalize(pStmt);
26789 pStmt = 0;
26790 if( nPage<1 ) goto dbtotxt_error;
26791 rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0);
26792 if( rc ) goto dbtotxt_error;
26793 if( sqlite3_step(pStmt)!=SQLITE_ROW ){
26794 zTail = "unk.db";
26795 }else{
26796 const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2);
26797 if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db";
26798 zTail = strrchr(zFilename, '/');
26799 #if defined(_WIN32)
26800 if( zTail==0 ) zTail = strrchr(zFilename, '\\');
26801 #endif
26802 }
26803 zName = strdup(zTail);
26804 shell_check_oom(zName);
26805 sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n",
26806 nPage*pgSz, pgSz, zName);
26807 sqlite3_finalize(pStmt);
26808 pStmt = 0;
26809 rc = sqlite3_prepare_v2(p->db,
26810 "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0);
26811 if( rc ) goto dbtotxt_error;
26812 while( sqlite3_step(pStmt)==SQLITE_ROW ){
26813 sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0);
26814 const u8 *aData = sqlite3_column_blob(pStmt, 1);
26815 int seenPageLabel = 0;
26816 for(i=0; i<pgSz; i+=16){
26817 const u8 *aLine = aData+i;
26818 for(j=0; j<16 && aLine[j]==0; j++){}
26819 if( j==16 ) continue;
26820 if( !seenPageLabel ){
26821 sqlite3_fprintf(p->out, "| page %lld offset %lld\n", pgno, pgno*pgSz);
26822 seenPageLabel = 1;
26823 }
26824 sqlite3_fprintf(p->out, "| %5d:", i);
26825 for(j=0; j<16; j++) sqlite3_fprintf(p->out, " %02x", aLine[j]);
26826 sqlite3_fprintf(p->out, " ");
26827 for(j=0; j<16; j++){
26828 unsigned char c = (unsigned char)aLine[j];
26829 sqlite3_fprintf(p->out, "%c", bShow[c]);
26830 }
26831 sqlite3_fprintf(p->out, "\n");
26832 }
26833 }
26834 sqlite3_finalize(pStmt);
26835 sqlite3_fprintf(p->out, "| end %s\n", zName);
26836 free(zName);
26837 return 0;
26838
26839 dbtotxt_error:
26840 if( rc ){
26841 sqlite3_fprintf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db));
26842 }
26843 sqlite3_finalize(pStmt);
26844 free(zName);
26845 return 1;
26846 }
26847
26848 /*
26849 ** Print the given string as an error message.
26850 */
26851 static void shellEmitError(const char *zErr){
@@ -28067,12 +28398,12 @@
28398 }else if( *pDb==0 ){
28399 return 0;
28400 }else{
28401 /* Formulate the columns spec, close the DB, zero *pDb. */
28402 char *zColsSpec = 0;
28403 int hasDupes = db_int(*pDb, "%s", zHasDupes);
28404 int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0;
28405 if( hasDupes ){
28406 #ifdef SHELL_COLUMN_RENAME_CLEAN
28407 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
28408 rc_err_oom_die(rc);
28409 #endif
@@ -28083,11 +28414,11 @@
28414 sqlite3_bind_int(pStmt, 1, nDigits);
28415 rc = sqlite3_step(pStmt);
28416 sqlite3_finalize(pStmt);
28417 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
28418 }
28419 assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */
28420 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
28421 rc_err_oom_die(rc);
28422 rc = sqlite3_step(pStmt);
28423 if( rc==SQLITE_ROW ){
28424 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
@@ -28688,10 +29019,14 @@
29019 }else{
29020 eputz("Usage: .echo on|off\n");
29021 rc = 1;
29022 }
29023 }else
29024
29025 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){
29026 rc = shell_dbtotxt_command(p, nArg, azArg);
29027 }else
29028
29029 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
29030 if( nArg==2 ){
29031 p->autoEQPtest = 0;
29032 if( p->autoEQPtrace ){
@@ -29129,11 +29464,15 @@
29464 /* Below, resources must be freed before exit. */
29465 while( (nSkip--)>0 ){
29466 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
29467 }
29468 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
29469 if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0)
29470 && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema"
29471 " WHERE name=%Q AND type='view'",
29472 zSchema ? zSchema : "main", zTable)
29473 ){
29474 /* Table does not exist. Create it. */
29475 sqlite3 *dbCols = 0;
29476 char *zRenames = 0;
29477 char *zColDefs;
29478 zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
@@ -30201,11 +30540,14 @@
30540 }
30541 close_db(pSrc);
30542 }else
30543 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
30544
30545 if( c=='s' &&
30546 (cli_strncmp(azArg[0], "scanstats", n)==0 ||
30547 cli_strncmp(azArg[0], "scanstatus", n)==0)
30548 ){
30549 if( nArg==2 ){
30550 if( cli_strcmp(azArg[1], "vm")==0 ){
30551 p->scanstatsOn = 3;
30552 }else
30553 if( cli_strcmp(azArg[1], "est")==0 ){
@@ -31256,11 +31598,13 @@
31598 { 0x10000000, 1, "OrderBySubq" },
31599 { 0xffffffff, 0, "All" },
31600 };
31601 unsigned int curOpt;
31602 unsigned int newOpt;
31603 unsigned int m;
31604 int ii;
31605 int nOff;
31606 sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
31607 newOpt = curOpt;
31608 for(ii=2; ii<nArg; ii++){
31609 const char *z = azArg[ii];
31610 int useLabel = 0;
@@ -31297,28 +31641,32 @@
31641 }
31642 }
31643 }
31644 if( curOpt!=newOpt ){
31645 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
31646 }
31647 for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){
31648 if( m & newOpt ) nOff++;
31649 }
31650 if( nOff<12 ){
31651 sqlite3_fputs("+All", p->out);
31652 for(ii=0; ii<ArraySize(aLabel); ii++){
31653 if( !aLabel[ii].bDsply ) continue;
31654 if( (newOpt & aLabel[ii].mask)!=0 ){
31655 sqlite3_fprintf(p->out, " -%s", aLabel[ii].zLabel);
31656 }
31657 }
31658 }else{
31659 sqlite3_fputs("-All", p->out);
31660 for(ii=0; ii<ArraySize(aLabel); ii++){
31661 if( !aLabel[ii].bDsply ) continue;
31662 if( (newOpt & aLabel[ii].mask)==0 ){
31663 sqlite3_fprintf(p->out, " +%s", aLabel[ii].zLabel);
 
 
31664 }
31665 }
31666 }
31667 sqlite3_fputs("\n", p->out);
31668 rc2 = isOk = 3;
31669 break;
31670 }
31671
31672 /* sqlite3_test_control(int, db, int) */
@@ -31652,73 +32000,10 @@
32000 }
32001 }
32002 }else
32003 #endif
32004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32005 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
32006 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
32007 sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
32008 sqlite3_libversion(), sqlite3_sourceid());
32009 #if SQLITE_HAVE_ZLIB
@@ -31838,11 +32123,10 @@
32123 SCAN_TRACKER_REFTYPE pst){
32124 char cin;
32125 char cWait = (char)qss; /* intentional narrowing loss */
32126 if( cWait==0 ){
32127 PlainScan:
 
32128 while( (cin = *zLine++)!=0 ){
32129 if( IsSpace(cin) )
32130 continue;
32131 switch (cin){
32132 case '-':
@@ -31890,11 +32174,10 @@
32174 switch( cWait ){
32175 case '*':
32176 if( *zLine != '/' )
32177 continue;
32178 ++zLine;
 
32179 CONTINUE_PROMPT_AWAITC(pst, 0);
32180 qss = QSS_SETV(qss, 0);
32181 goto PlainScan;
32182 case '`': case '\'': case '"':
32183 if(*zLine==cWait){
@@ -31902,11 +32185,10 @@
32185 ++zLine;
32186 continue;
32187 }
32188 deliberate_fall_through;
32189 case ']':
 
32190 CONTINUE_PROMPT_AWAITC(pst, 0);
32191 qss = QSS_SETV(qss, 0);
32192 goto PlainScan;
32193 default: assert(0);
32194 }
@@ -32090,11 +32372,14 @@
32372 if( doAutoDetectRestore(p, zSql) ) return 1;
32373 return 0;
32374 }
32375
32376 static void echo_group_input(ShellState *p, const char *zDo){
32377 if( ShellHasFlag(p, SHFLG_Echo) ){
32378 sqlite3_fprintf(p->out, "%s\n", zDo);
32379 fflush(p->out);
32380 }
32381 }
32382
32383 #ifdef SQLITE_SHELL_FIDDLE
32384 /*
32385 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
@@ -32550,10 +32835,19 @@
32835 }
32836
32837 static void sayAbnormalExit(void){
32838 if( seenInterrupt ) eputz("Program interrupted.\n");
32839 }
32840
32841 /* Routine to output from vfstrace
32842 */
32843 static int vfstraceOut(const char *z, void *pArg){
32844 ShellState *p = (ShellState*)pArg;
32845 sqlite3_fputs(z, p->out);
32846 fflush(p->out);
32847 return 1;
32848 }
32849
32850 #ifndef SQLITE_SHELL_IS_UTF8
32851 # if (defined(_WIN32) || defined(WIN32)) \
32852 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
32853 # define SQLITE_SHELL_IS_UTF8 (0)
@@ -32787,12 +33081,10 @@
33081 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
33082 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
33083 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
33084 }
33085 }else if( cli_strcmp(z,"-vfstrace")==0 ){
 
 
33086 bEnableVfstrace = 1;
33087 #ifdef SQLITE_ENABLE_MULTIPLEX
33088 }else if( cli_strcmp(z,"-multiplex")==0 ){
33089 extern int sqlite3_multiplex_initialize(const char*,int);
33090 sqlite3_multiplex_initialize(0, 1);
@@ -32885,10 +33177,13 @@
33177 "%s: Error: no database filename specified\n", Argv0);
33178 return 1;
33179 #endif
33180 }
33181 data.out = stdout;
33182 if( bEnableVfstrace ){
33183 vfstrace_register("trace",0,vfstraceOut, &data, 1);
33184 }
33185 #ifndef SQLITE_SHELL_FIDDLE
33186 sqlite3_appendvfs_init(0,0,0);
33187 #endif
33188
33189 /* Go ahead and open the database file if it already exists. If the
@@ -33129,19 +33424,14 @@
33424 */
33425 if( stdin_is_interactive ){
33426 char *zHome;
33427 char *zHistory;
33428 int nHistory;
 
 
 
 
 
33429 sqlite3_fprintf(stdout,
33430 "SQLite version %s %.19s\n" /*extra-version-info*/
33431 "Enter \".help\" for usage hints.\n",
33432 sqlite3_libversion(), sqlite3_sourceid());
33433 if( warnInmemoryDb ){
33434 sputz(stdout, "Connected to a ");
33435 printBold("transient in-memory database");
33436 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
33437 " persistent database.\n");
@@ -33154,13 +33444,15 @@
33444 if( (zHistory = malloc(nHistory))!=0 ){
33445 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
33446 }
33447 }
33448 if( zHistory ){ shell_read_history(zHistory); }
33449 #if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION)
33450 rl_attempted_completion_function = readline_completion;
33451 #elif HAVE_LINENOISE==1
33452 linenoiseSetCompletionCallback(linenoise_completion);
33453 #elif HAVE_LINENOISE==2
33454 linenoiseSetCompletionCallback(linenoise_completion, NULL);
33455 #endif
33456 data.in = 0;
33457 rc = process_input(&data);
33458 if( zHistory ){
33459
+1286 -709
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.47.0. By combining all the individual C code files into this
3
+** version 3.48.0. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -16,12 +16,15 @@
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
-** 03a9703e27c44437c39363d0baf82db4ebc9.
21
+** 2b17bc49655c577029919c2d409de994b0d2 with changes in files:
22
+**
23
+**
2224
*/
25
+#ifndef SQLITE_AMALGAMATION
2326
#define SQLITE_CORE 1
2427
#define SQLITE_AMALGAMATION 1
2528
#ifndef SQLITE_PRIVATE
2629
# define SQLITE_PRIVATE static
2730
#endif
@@ -460,13 +463,13 @@
460463
**
461464
** See also: [sqlite3_libversion()],
462465
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463466
** [sqlite_version()] and [sqlite_source_id()].
464467
*/
465
-#define SQLITE_VERSION "3.47.0"
466
-#define SQLITE_VERSION_NUMBER 3047000
467
-#define SQLITE_SOURCE_ID "2024-10-21 16:30:22 03a9703e27c44437c39363d0baf82db4ebc94538a0f28411c85dda156f82636e"
468
+#define SQLITE_VERSION "3.48.0"
469
+#define SQLITE_VERSION_NUMBER 3048000
470
+#define SQLITE_SOURCE_ID "2024-12-30 21:23:53 2b17bc49655c577029919c2d409de994b0d252f8efb5da1ba0913f2c96bee552"
468471
469472
/*
470473
** CAPI3REF: Run-Time Library Version Numbers
471474
** KEYWORDS: sqlite3_version sqlite3_sourceid
472475
**
@@ -966,10 +969,17 @@
966969
**
967970
** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
968971
** filesystem supports doing multiple write operations atomically when those
969972
** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
970973
** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
974
+**
975
+** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read
976
+** from the database file in amounts that are not a multiple of the
977
+** page size and that do not begin at a page boundary. Without this
978
+** property, SQLite is careful to only do full-page reads and write
979
+** on aligned pages, with the one exception that it will do a sub-page
980
+** read of the first page to access the database header.
971981
*/
972982
#define SQLITE_IOCAP_ATOMIC 0x00000001
973983
#define SQLITE_IOCAP_ATOMIC512 0x00000002
974984
#define SQLITE_IOCAP_ATOMIC1K 0x00000004
975985
#define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -982,10 +992,11 @@
982992
#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
983993
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
984994
#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
985995
#define SQLITE_IOCAP_IMMUTABLE 0x00002000
986996
#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
997
+#define SQLITE_IOCAP_SUBPAGE_READ 0x00008000
987998
988999
/*
9891000
** CAPI3REF: File Locking Levels
9901001
**
9911002
** SQLite uses one of these integer values as the second
@@ -1128,10 +1139,11 @@
11281139
** <li> [SQLITE_IOCAP_SEQUENTIAL]
11291140
** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
11301141
** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
11311142
** <li> [SQLITE_IOCAP_IMMUTABLE]
11321143
** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1144
+** <li> [SQLITE_IOCAP_SUBPAGE_READ]
11331145
** </ul>
11341146
**
11351147
** The SQLITE_IOCAP_ATOMIC property means that all writes of
11361148
** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
11371149
** mean that writes of blocks that are nnn bytes in size and
@@ -1405,10 +1417,15 @@
14051417
** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
14061418
** opcode causes the xFileControl method to swap the file handle with the one
14071419
** pointed to by the pArg argument. This capability is used during testing
14081420
** and only needs to be supported when SQLITE_TEST is defined.
14091421
**
1422
+** <li>[[SQLITE_FCNTL_NULL_IO]]
1423
+** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor
1424
+** or file handle for the [sqlite3_file] object such that it will no longer
1425
+** read or write to the database file.
1426
+**
14101427
** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
14111428
** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
14121429
** be advantageous to block on the next WAL lock if the lock is not immediately
14131430
** available. The WAL subsystem issues this signal during rare
14141431
** circumstances in order to fix a problem with priority inversion.
@@ -1558,10 +1575,11 @@
15581575
#define SQLITE_FCNTL_RESERVE_BYTES 38
15591576
#define SQLITE_FCNTL_CKPT_START 39
15601577
#define SQLITE_FCNTL_EXTERNAL_READER 40
15611578
#define SQLITE_FCNTL_CKSM_FILE 41
15621579
#define SQLITE_FCNTL_RESET_CACHE 42
1580
+#define SQLITE_FCNTL_NULL_IO 43
15631581
15641582
/* deprecated names */
15651583
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
15661584
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
15671585
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -2936,14 +2954,18 @@
29362954
**
29372955
** ^These functions return the number of rows modified, inserted or
29382956
** deleted by the most recently completed INSERT, UPDATE or DELETE
29392957
** statement on the database connection specified by the only parameter.
29402958
** The two functions are identical except for the type of the return value
2941
-** and that if the number of rows modified by the most recent INSERT, UPDATE
2959
+** and that if the number of rows modified by the most recent INSERT, UPDATE,
29422960
** or DELETE is greater than the maximum value supported by type "int", then
29432961
** the return value of sqlite3_changes() is undefined. ^Executing any other
29442962
** type of SQL statement does not modify the value returned by these functions.
2963
+** For the purposes of this interface, a CREATE TABLE AS SELECT statement
2964
+** does not count as an INSERT, UPDATE or DELETE statement and hence the rows
2965
+** added to the new table by the CREATE TABLE AS SELECT statement are not
2966
+** counted.
29452967
**
29462968
** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
29472969
** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
29482970
** [foreign key actions] or [REPLACE] constraint resolution are not counted.
29492971
**
@@ -4499,15 +4521,26 @@
44994521
**
45004522
** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
45014523
** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
45024524
** to return an error (error code SQLITE_ERROR) if the statement uses
45034525
** any virtual tables.
4526
+**
4527
+** [[SQLITE_PREPARE_DONT_LOG]] <dt>SQLITE_PREPARE_DONT_LOG</dt>
4528
+** <dd>The SQLITE_PREPARE_DONT_LOG flag prevents SQL compiler
4529
+** errors from being sent to the error log defined by
4530
+** [SQLITE_CONFIG_LOG]. This can be used, for example, to do test
4531
+** compiles to see if some SQL syntax is well-formed, without generating
4532
+** messages on the global error log when it is not. If the test compile
4533
+** fails, the sqlite3_prepare_v3() call returns the same error indications
4534
+** with or without this flag; it just omits the call to [sqlite3_log()] that
4535
+** logs the error.
45044536
** </dl>
45054537
*/
45064538
#define SQLITE_PREPARE_PERSISTENT 0x01
45074539
#define SQLITE_PREPARE_NORMALIZE 0x02
45084540
#define SQLITE_PREPARE_NO_VTAB 0x04
4541
+#define SQLITE_PREPARE_DONT_LOG 0x10
45094542
45104543
/*
45114544
** CAPI3REF: Compiling An SQL Statement
45124545
** KEYWORDS: {SQL statement compiler}
45134546
** METHOD: sqlite3
@@ -11194,11 +11227,11 @@
1119411227
#endif
1119511228
1119611229
#if 0
1119711230
} /* End of the 'extern "C"' block */
1119811231
#endif
11199
-#endif /* SQLITE3_H */
11232
+/* #endif for SQLITE3_H will be added by mksqlite3.tcl */
1120011233
1120111234
/******** Begin file sqlite3rtree.h *********/
1120211235
/*
1120311236
** 2010 August 30
1120411237
**
@@ -13445,17 +13478,32 @@
1344513478
** This is used to access token iToken of phrase hit iIdx within the
1344613479
** current row. If iIdx is less than zero or greater than or equal to the
1344713480
** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
1344813481
** output variable (*ppToken) is set to point to a buffer containing the
1344913482
** matching document token, and (*pnToken) to the size of that buffer in
13450
-** bytes. This API is not available if the specified token matches a
13451
-** prefix query term. In that case both output variables are always set
13452
-** to 0.
13483
+** bytes.
1345313484
**
1345413485
** The output text is not a copy of the document text that was tokenized.
1345513486
** It is the output of the tokenizer module. For tokendata=1 tables, this
1345613487
** includes any embedded 0x00 and trailing data.
13488
+**
13489
+** This API may be slow in some cases if the token identified by parameters
13490
+** iIdx and iToken matched a prefix token in the query. In most cases, the
13491
+** first call to this API for each prefix token in the query is forced
13492
+** to scan the portion of the full-text index that matches the prefix
13493
+** token to collect the extra data required by this API. If the prefix
13494
+** token matches a large number of token instances in the document set,
13495
+** this may be a performance problem.
13496
+**
13497
+** If the user knows in advance that a query may use this API for a
13498
+** prefix token, FTS5 may be configured to collect all required data as part
13499
+** of the initial querying of the full-text index, avoiding the second scan
13500
+** entirely. This also causes prefix queries that do not use this API to
13501
+** run more slowly and use more memory. FTS5 may be configured in this way
13502
+** either on a per-table basis using the [FTS5 insttoken | 'insttoken']
13503
+** option, or on a per-query basis using the
13504
+** [fts5_insttoken | fts5_insttoken()] user function.
1345713505
**
1345813506
** This API can be quite slow if used with an FTS5 table created with the
1345913507
** "detail=none" or "detail=column" option.
1346013508
**
1346113509
** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
@@ -13886,10 +13934,11 @@
1388613934
#endif
1388713935
1388813936
#endif /* _FTS5_H */
1388913937
1389013938
/******** End of fts5.h *********/
13939
+#endif /* SQLITE3_H */
1389113940
1389213941
/************** End of sqlite3.h *********************************************/
1389313942
/************** Continuing where we left off in sqliteInt.h ******************/
1389413943
1389513944
/*
@@ -13931,10 +13980,11 @@
1393113980
** to count the size: 2^31-1 or 2147483647.
1393213981
*/
1393313982
#ifndef SQLITE_MAX_LENGTH
1393413983
# define SQLITE_MAX_LENGTH 1000000000
1393513984
#endif
13985
+#define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */
1393613986
1393713987
/*
1393813988
** This is the maximum number of
1393913989
**
1394013990
** * Columns in a table
@@ -13996,13 +14046,17 @@
1399614046
# define SQLITE_MAX_VDBE_OP 250000000
1399714047
#endif
1399814048
1399914049
/*
1400014050
** The maximum number of arguments to an SQL function.
14051
+**
14052
+** This value has a hard upper limit of 32767 due to storage
14053
+** constraints (it needs to fit inside a i16). We keep it
14054
+** lower than that to prevent abuse.
1400114055
*/
1400214056
#ifndef SQLITE_MAX_FUNCTION_ARG
14003
-# define SQLITE_MAX_FUNCTION_ARG 127
14057
+# define SQLITE_MAX_FUNCTION_ARG 1000
1400414058
#endif
1400514059
1400614060
/*
1400714061
** The suggested maximum number of in-memory pages to use for
1400814062
** the main database table and for temporary tables.
@@ -16000,10 +16054,26 @@
1600016054
#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
1600116055
#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
1600216056
#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
1600316057
#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
1600416058
16059
+#define isWalMode(x) ((x)==PAGER_JOURNALMODE_WAL)
16060
+
16061
+/*
16062
+** The argument to this macro is a file descriptor (type sqlite3_file*).
16063
+** Return 0 if it is not open, or non-zero (but not 1) if it is.
16064
+**
16065
+** This is so that expressions can be written as:
16066
+**
16067
+** if( isOpen(pPager->jfd) ){ ...
16068
+**
16069
+** instead of
16070
+**
16071
+** if( pPager->jfd->pMethods ){ ...
16072
+*/
16073
+#define isOpen(pFd) ((pFd)->pMethods!=0)
16074
+
1600516075
/*
1600616076
** Flags that make up the mask passed to sqlite3PagerGet().
1600716077
*/
1600816078
#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
1600916079
#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
@@ -17025,11 +17095,11 @@
1702517095
1702617096
/*
1702717097
** Additional non-public SQLITE_PREPARE_* flags
1702817098
*/
1702917099
#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
17030
-#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
17100
+#define SQLITE_PREPARE_MASK 0x1f /* Mask of public flags */
1703117101
1703217102
/*
1703317103
** Prototypes for the VDBE interface. See comments on the implementation
1703417104
** for a description of what each of these routines does.
1703517105
*/
@@ -17740,51 +17810,15 @@
1774017810
struct FuncDefHash {
1774117811
FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
1774217812
};
1774317813
#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
1774417814
17745
-#if defined(SQLITE_USER_AUTHENTICATION)
17746
-# warning "The SQLITE_USER_AUTHENTICATION extension is deprecated. \
17747
- See ext/userauth/user-auth.txt for details."
17748
-#endif
17749
-#ifdef SQLITE_USER_AUTHENTICATION
17750
-/*
17751
-** Information held in the "sqlite3" database connection object and used
17752
-** to manage user authentication.
17753
-*/
17754
-typedef struct sqlite3_userauth sqlite3_userauth;
17755
-struct sqlite3_userauth {
17756
- u8 authLevel; /* Current authentication level */
17757
- int nAuthPW; /* Size of the zAuthPW in bytes */
17758
- char *zAuthPW; /* Password used to authenticate */
17759
- char *zAuthUser; /* User name used to authenticate */
17760
-};
17761
-
17762
-/* Allowed values for sqlite3_userauth.authLevel */
17763
-#define UAUTH_Unknown 0 /* Authentication not yet checked */
17764
-#define UAUTH_Fail 1 /* User authentication failed */
17765
-#define UAUTH_User 2 /* Authenticated as a normal user */
17766
-#define UAUTH_Admin 3 /* Authenticated as an administrator */
17767
-
17768
-/* Functions used only by user authorization logic */
17769
-SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
17770
-SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
17771
-SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
17772
-SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
17773
-
17774
-#endif /* SQLITE_USER_AUTHENTICATION */
17775
-
1777617815
/*
1777717816
** typedef for the authorization callback function.
1777817817
*/
17779
-#ifdef SQLITE_USER_AUTHENTICATION
17780
- typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17781
- const char*, const char*);
17782
-#else
17783
- typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17784
- const char*);
17785
-#endif
17818
+typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17819
+ const char*);
1778617820
1778717821
#ifndef SQLITE_OMIT_DEPRECATED
1778817822
/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
1778917823
** in the style of sqlite3_trace()
1779017824
*/
@@ -17941,13 +17975,10 @@
1794117975
sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
1794217976
void *pUnlockArg; /* Argument to xUnlockNotify */
1794317977
void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
1794417978
sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
1794517979
#endif
17946
-#ifdef SQLITE_USER_AUTHENTICATION
17947
- sqlite3_userauth auth; /* User authentication information */
17948
-#endif
1794917980
};
1795017981
1795117982
/*
1795217983
** A macro to discover the encoding of a database.
1795317984
*/
@@ -18102,11 +18133,11 @@
1810218133
**
1810318134
** The u.pHash field is used by the global built-ins. The u.pDestructor
1810418135
** field is used by per-connection app-def functions.
1810518136
*/
1810618137
struct FuncDef {
18107
- i8 nArg; /* Number of arguments. -1 means unlimited */
18138
+ i16 nArg; /* Number of arguments. -1 means unlimited */
1810818139
u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
1810918140
void *pUserData; /* User data parameter */
1811018141
FuncDef *pNext; /* Next function with same name */
1811118142
void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
1811218143
void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
@@ -22850,13 +22881,10 @@
2285022881
"UNLINK_AFTER_CLOSE",
2285122882
#endif
2285222883
#ifdef SQLITE_UNTESTABLE
2285322884
"UNTESTABLE",
2285422885
#endif
22855
-#ifdef SQLITE_USER_AUTHENTICATION
22856
- "USER_AUTHENTICATION",
22857
-#endif
2285822886
#ifdef SQLITE_USE_ALLOCA
2285922887
"USE_ALLOCA",
2286022888
#endif
2286122889
#ifdef SQLITE_USE_FCNTL_TRACE
2286222890
"USE_FCNTL_TRACE",
@@ -23700,11 +23728,11 @@
2370023728
Vdbe *pVdbe; /* The VM that owns this context */
2370123729
int iOp; /* Instruction number of OP_Function */
2370223730
int isError; /* Error code returned by the function. */
2370323731
u8 enc; /* Encoding to use for results */
2370423732
u8 skipFlag; /* Skip accumulator loading if true */
23705
- u8 argc; /* Number of arguments */
23733
+ u16 argc; /* Number of arguments */
2370623734
sqlite3_value *argv[1]; /* Argument set */
2370723735
};
2370823736
2370923737
/* A bitfield type for use inside of structures. Always follow with :N where
2371023738
** N is the number of bits.
@@ -23847,10 +23875,11 @@
2384723875
UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
2384823876
int iNewReg; /* Register for new.* values */
2384923877
int iBlobWrite; /* Value returned by preupdate_blobwrite() */
2385023878
i64 iKey1; /* First key value passed to hook */
2385123879
i64 iKey2; /* Second key value passed to hook */
23880
+ Mem oldipk; /* Memory cell holding "old" IPK value */
2385223881
Mem *aNew; /* Array of new.* values */
2385323882
Table *pTab; /* Schema object being updated */
2385423883
Index *pPk; /* PK index if pTab is WITHOUT ROWID */
2385523884
sqlite3_value **apDflt; /* Array of default values, if required */
2385623885
};
@@ -32296,10 +32325,11 @@
3229632325
&& (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
3229732326
){
3229832327
pExpr = pExpr->pLeft;
3229932328
}
3230032329
if( pExpr==0 ) return;
32330
+ if( ExprHasProperty(pExpr, EP_FromDDL) ) return;
3230132331
db->errByteOffset = pExpr->w.iOfst;
3230232332
}
3230332333
3230432334
/*
3230532335
** Enlarge the memory allocation on a StrAccum object so that it is
@@ -33025,11 +33055,11 @@
3302533055
}
3302633056
if( pItem->fg.isCte ){
3302733057
sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
3302833058
}
3302933059
if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
33030
- sqlite3_str_appendf(&x, " ON");
33060
+ sqlite3_str_appendf(&x, " isOn");
3303133061
}
3303233062
if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc");
3303333063
if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated");
3303433064
if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized");
3303533065
if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine");
@@ -34109,10 +34139,14 @@
3410934139
**
3411034140
** This routines are given external linkage so that they will always be
3411134141
** accessible to the debugging, and to avoid warnings about unused
3411234142
** functions. But these routines only exist in debugging builds, so they
3411334143
** do not contaminate the interface.
34144
+**
34145
+** See Also:
34146
+**
34147
+** sqlite3ShowWhereTerm() in where.c
3411434148
*/
3411534149
SQLITE_PRIVATE void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
3411634150
SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
3411734151
SQLITE_PRIVATE void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
3411834152
SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
@@ -35685,12 +35719,12 @@
3568535719
int esign = 1; /* sign of exponent */
3568635720
int e = 0; /* exponent */
3568735721
int eValid = 1; /* True exponent is either not used or is well-formed */
3568835722
int nDigit = 0; /* Number of digits processed */
3568935723
int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
35724
+ u64 s2; /* round-tripped significand */
3569035725
double rr[2];
35691
- u64 s2;
3569235726
3569335727
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
3569435728
*pResult = 0.0; /* Default return value, in case of an error */
3569535729
if( length==0 ) return 0;
3569635730
@@ -35789,25 +35823,36 @@
3578935823
3579035824
/* adjust exponent by d, and update sign */
3579135825
e = (e*esign) + d;
3579235826
3579335827
/* Try to adjust the exponent to make it smaller */
35794
- while( e>0 && s<(LARGEST_UINT64/10) ){
35828
+ while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){
3579535829
s *= 10;
3579635830
e--;
3579735831
}
3579835832
while( e<0 && (s%10)==0 ){
3579935833
s /= 10;
3580035834
e++;
3580135835
}
3580235836
3580335837
rr[0] = (double)s;
35804
- s2 = (u64)rr[0];
35805
-#if defined(_MSC_VER) && _MSC_VER<1700
35806
- if( s2==0x8000000000000000LL ){ s2 = 2*(u64)(0.5*rr[0]); }
35838
+ assert( sizeof(s2)==sizeof(rr[0]) );
35839
+#ifdef SQLITE_DEBUG
35840
+ rr[1] = 18446744073709549568.0;
35841
+ memcpy(&s2, &rr[1], sizeof(s2));
35842
+ assert( s2==0x43efffffffffffffLL );
3580735843
#endif
35808
- rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
35844
+ /* Largest double that can be safely converted to u64
35845
+ ** vvvvvvvvvvvvvvvvvvvvvv */
35846
+ if( rr[0]<=18446744073709549568.0 ){
35847
+ s2 = (u64)rr[0];
35848
+ rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
35849
+ }else{
35850
+ rr[1] = 0.0;
35851
+ }
35852
+ assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */
35853
+
3580935854
if( e>0 ){
3581035855
while( e>=100 ){
3581135856
e -= 100;
3581235857
dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
3581335858
}
@@ -38674,11 +38719,11 @@
3867438719
# define F_SETLKW 7
3867538720
# endif
3867638721
# endif
3867738722
#else /* !SQLITE_WASI */
3867838723
# ifndef HAVE_FCHMOD
38679
-# define HAVE_FCHMOD
38724
+# define HAVE_FCHMOD 1
3868038725
# endif
3868138726
#endif /* SQLITE_WASI */
3868238727
3868338728
#ifdef SQLITE_WASI
3868438729
# define osGetpid(X) (pid_t)1
@@ -42448,10 +42493,15 @@
4244842493
int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
4244942494
return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
4245042495
}
4245142496
#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
4245242497
42498
+ case SQLITE_FCNTL_NULL_IO: {
42499
+ osClose(pFile->h);
42500
+ pFile->h = -1;
42501
+ return SQLITE_OK;
42502
+ }
4245342503
case SQLITE_FCNTL_LOCKSTATE: {
4245442504
*(int*)pArg = pFile->eFileLock;
4245542505
return SQLITE_OK;
4245642506
}
4245742507
case SQLITE_FCNTL_LAST_ERRNO: {
@@ -42589,10 +42639,11 @@
4258942639
4259042640
/* Set the POWERSAFE_OVERWRITE flag if requested. */
4259142641
if( pFd->ctrlFlags & UNIXFILE_PSOW ){
4259242642
pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
4259342643
}
42644
+ pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ;
4259442645
4259542646
pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
4259642647
}
4259742648
}
4259842649
#else
@@ -50328,10 +50379,15 @@
5032850379
OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
5032950380
hOldFile, pFile->h));
5033050381
return SQLITE_OK;
5033150382
}
5033250383
#endif
50384
+ case SQLITE_FCNTL_NULL_IO: {
50385
+ (void)osCloseHandle(pFile->h);
50386
+ pFile->h = NULL;
50387
+ return SQLITE_OK;
50388
+ }
5033350389
case SQLITE_FCNTL_TEMPFILENAME: {
5033450390
char *zTFile = 0;
5033550391
int rc = winGetTempname(pFile->pVfs, &zTFile);
5033650392
if( rc==SQLITE_OK ){
5033750393
*(char**)pArg = zTFile;
@@ -50389,11 +50445,11 @@
5038950445
/*
5039050446
** Return a vector of device characteristics.
5039150447
*/
5039250448
static int winDeviceCharacteristics(sqlite3_file *id){
5039350449
winFile *p = (winFile*)id;
50394
- return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
50450
+ return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | SQLITE_IOCAP_SUBPAGE_READ |
5039550451
((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
5039650452
}
5039750453
5039850454
/*
5039950455
** Windows will only let you create file view mappings
@@ -51777,11 +51833,11 @@
5177751833
*/
5177851834
char *zTmpname = 0; /* For temporary filename, if necessary. */
5177951835
5178051836
int rc = SQLITE_OK; /* Function Return Code */
5178151837
#if !defined(NDEBUG) || SQLITE_OS_WINCE
51782
- int eType = flags&0xFFFFFF00; /* Type of file to open */
51838
+ int eType = flags&0x0FFF00; /* Type of file to open */
5178351839
#endif
5178451840
5178551841
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
5178651842
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
5178751843
int isCreate = (flags & SQLITE_OPEN_CREATE);
@@ -57978,43 +58034,37 @@
5797858034
# define USEFETCH(x) ((x)->bUseFetch)
5797958035
#else
5798058036
# define USEFETCH(x) 0
5798158037
#endif
5798258038
57983
-/*
57984
-** The argument to this macro is a file descriptor (type sqlite3_file*).
57985
-** Return 0 if it is not open, or non-zero (but not 1) if it is.
57986
-**
57987
-** This is so that expressions can be written as:
57988
-**
57989
-** if( isOpen(pPager->jfd) ){ ...
57990
-**
57991
-** instead of
57992
-**
57993
-** if( pPager->jfd->pMethods ){ ...
57994
-*/
57995
-#define isOpen(pFd) ((pFd)->pMethods!=0)
57996
-
5799758039
#ifdef SQLITE_DIRECT_OVERFLOW_READ
5799858040
/*
5799958041
** Return true if page pgno can be read directly from the database file
5800058042
** by the b-tree layer. This is the case if:
5800158043
**
58002
-** * the database file is open,
58003
-** * there are no dirty pages in the cache, and
58004
-** * the desired page is not currently in the wal file.
58044
+** (1) the database file is open
58045
+** (2) the VFS for the database is able to do unaligned sub-page reads
58046
+** (3) there are no dirty pages in the cache, and
58047
+** (4) the desired page is not currently in the wal file.
5800558048
*/
5800658049
SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
58007
- if( pPager->fd->pMethods==0 ) return 0;
58008
- if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
58050
+ assert( pPager!=0 );
58051
+ assert( pPager->fd!=0 );
58052
+ if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */
58053
+ if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */
5800958054
#ifndef SQLITE_OMIT_WAL
5801058055
if( pPager->pWal ){
5801158056
u32 iRead = 0;
5801258057
(void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
58013
- return iRead==0;
58058
+ return iRead==0; /* Condition (4) */
5801458059
}
5801558060
#endif
58061
+ assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 );
58062
+ if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd)
58063
+ & SQLITE_IOCAP_SUBPAGE_READ)==0 ){
58064
+ return 0; /* Case (2) */
58065
+ }
5801658066
return 1;
5801758067
}
5801858068
#endif
5801958069
5802058070
#ifndef SQLITE_OMIT_WAL
@@ -59269,11 +59319,11 @@
5926959319
rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
5927059320
}
5927159321
}
5927259322
pPager->journalOff = 0;
5927359323
}else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
59274
- || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
59324
+ || (pPager->exclusiveMode && pPager->journalMode<PAGER_JOURNALMODE_WAL)
5927559325
){
5927659326
rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
5927759327
pPager->journalOff = 0;
5927859328
}else{
5927959329
/* This branch may be executed with Pager.journalMode==MEMORY if
@@ -67979,15 +68029,11 @@
6797968029
** so it takes care to hold an exclusive lock on the corresponding
6798068030
** WAL_READ_LOCK() while changing values.
6798168031
*/
6798268032
static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
6798368033
volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
67984
- u32 mxReadMark; /* Largest aReadMark[] value */
67985
- int mxI; /* Index of largest aReadMark[] value */
67986
- int i; /* Loop counter */
6798768034
int rc = SQLITE_OK; /* Return code */
67988
- u32 mxFrame; /* Wal frame to lock to */
6798968035
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
6799068036
int nBlockTmout = 0;
6799168037
#endif
6799268038
6799368039
assert( pWal->readLock<0 ); /* Not currently locked */
@@ -68089,145 +68135,151 @@
6808968135
6809068136
assert( pWal->nWiData>0 );
6809168137
assert( pWal->apWiData[0]!=0 );
6809268138
pInfo = walCkptInfo(pWal);
6809368139
SEH_INJECT_FAULT;
68094
- if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68095
-#ifdef SQLITE_ENABLE_SNAPSHOT
68096
- && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
68097
-#endif
68098
- ){
68099
- /* The WAL has been completely backfilled (or it is empty).
68100
- ** and can be safely ignored.
68101
- */
68102
- rc = walLockShared(pWal, WAL_READ_LOCK(0));
68103
- walShmBarrier(pWal);
68104
- if( rc==SQLITE_OK ){
68105
- if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
68106
- /* It is not safe to allow the reader to continue here if frames
68107
- ** may have been appended to the log before READ_LOCK(0) was obtained.
68108
- ** When holding READ_LOCK(0), the reader ignores the entire log file,
68109
- ** which implies that the database file contains a trustworthy
68110
- ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
68111
- ** happening, this is usually correct.
68112
- **
68113
- ** However, if frames have been appended to the log (or if the log
68114
- ** is wrapped and written for that matter) before the READ_LOCK(0)
68115
- ** is obtained, that is not necessarily true. A checkpointer may
68116
- ** have started to backfill the appended frames but crashed before
68117
- ** it finished. Leaving a corrupt image in the database file.
68118
- */
68119
- walUnlockShared(pWal, WAL_READ_LOCK(0));
68120
- return WAL_RETRY;
68121
- }
68122
- pWal->readLock = 0;
68123
- return SQLITE_OK;
68124
- }else if( rc!=SQLITE_BUSY ){
68125
- return rc;
68126
- }
68127
- }
68128
-
68129
- /* If we get this far, it means that the reader will want to use
68130
- ** the WAL to get at content from recent commits. The job now is
68131
- ** to select one of the aReadMark[] entries that is closest to
68132
- ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68133
- */
68134
- mxReadMark = 0;
68135
- mxI = 0;
68136
- mxFrame = pWal->hdr.mxFrame;
68137
-#ifdef SQLITE_ENABLE_SNAPSHOT
68138
- if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68139
- mxFrame = pWal->pSnapshot->mxFrame;
68140
- }
68141
-#endif
68142
- for(i=1; i<WAL_NREADER; i++){
68143
- u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68144
- if( mxReadMark<=thisMark && thisMark<=mxFrame ){
68145
- assert( thisMark!=READMARK_NOT_USED );
68146
- mxReadMark = thisMark;
68147
- mxI = i;
68148
- }
68149
- }
68150
- if( (pWal->readOnly & WAL_SHM_RDONLY)==0
68151
- && (mxReadMark<mxFrame || mxI==0)
68152
- ){
68153
- for(i=1; i<WAL_NREADER; i++){
68154
- rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
68155
- if( rc==SQLITE_OK ){
68156
- AtomicStore(pInfo->aReadMark+i,mxFrame);
68157
- mxReadMark = mxFrame;
68158
- mxI = i;
68159
- walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
68160
- break;
68161
- }else if( rc!=SQLITE_BUSY ){
68162
- return rc;
68163
- }
68164
- }
68165
- }
68166
- if( mxI==0 ){
68167
- assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
68168
- return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
68169
- }
68170
-
68171
- (void)walEnableBlockingMs(pWal, nBlockTmout);
68172
- rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
68173
- walDisableBlocking(pWal);
68174
- if( rc ){
68175
-#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68176
- if( rc==SQLITE_BUSY_TIMEOUT ){
68177
- *pCnt |= WAL_RETRY_BLOCKED_MASK;
68178
- }
68179
-#else
68180
- assert( rc!=SQLITE_BUSY_TIMEOUT );
68181
-#endif
68182
- assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT );
68183
- return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc;
68184
- }
68185
- /* Now that the read-lock has been obtained, check that neither the
68186
- ** value in the aReadMark[] array or the contents of the wal-index
68187
- ** header have changed.
68188
- **
68189
- ** It is necessary to check that the wal-index header did not change
68190
- ** between the time it was read and when the shared-lock was obtained
68191
- ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
68192
- ** that the log file may have been wrapped by a writer, or that frames
68193
- ** that occur later in the log than pWal->hdr.mxFrame may have been
68194
- ** copied into the database by a checkpointer. If either of these things
68195
- ** happened, then reading the database with the current value of
68196
- ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68197
- ** instead.
68198
- **
68199
- ** Before checking that the live wal-index header has not changed
68200
- ** since it was read, set Wal.minFrame to the first frame in the wal
68201
- ** file that has not yet been checkpointed. This client will not need
68202
- ** to read any frames earlier than minFrame from the wal file - they
68203
- ** can be safely read directly from the database file.
68204
- **
68205
- ** Because a ShmBarrier() call is made between taking the copy of
68206
- ** nBackfill and checking that the wal-header in shared-memory still
68207
- ** matches the one cached in pWal->hdr, it is guaranteed that the
68208
- ** checkpointer that set nBackfill was not working with a wal-index
68209
- ** header newer than that cached in pWal->hdr. If it were, that could
68210
- ** cause a problem. The checkpointer could omit to checkpoint
68211
- ** a version of page X that lies before pWal->minFrame (call that version
68212
- ** A) on the basis that there is a newer version (version B) of the same
68213
- ** page later in the wal file. But if version B happens to like past
68214
- ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68215
- ** that it can read version A from the database file. However, since
68216
- ** we can guarantee that the checkpointer that set nBackfill could not
68217
- ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68218
- */
68219
- pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
68220
- walShmBarrier(pWal);
68221
- if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
68222
- || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68223
- ){
68224
- walUnlockShared(pWal, WAL_READ_LOCK(mxI));
68225
- return WAL_RETRY;
68226
- }else{
68227
- assert( mxReadMark<=pWal->hdr.mxFrame );
68228
- pWal->readLock = (i16)mxI;
68140
+ {
68141
+ u32 mxReadMark; /* Largest aReadMark[] value */
68142
+ int mxI; /* Index of largest aReadMark[] value */
68143
+ int i; /* Loop counter */
68144
+ u32 mxFrame; /* Wal frame to lock to */
68145
+ if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68146
+#ifdef SQLITE_ENABLE_SNAPSHOT
68147
+ && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
68148
+#endif
68149
+ ){
68150
+ /* The WAL has been completely backfilled (or it is empty).
68151
+ ** and can be safely ignored.
68152
+ */
68153
+ rc = walLockShared(pWal, WAL_READ_LOCK(0));
68154
+ walShmBarrier(pWal);
68155
+ if( rc==SQLITE_OK ){
68156
+ if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){
68157
+ /* It is not safe to allow the reader to continue here if frames
68158
+ ** may have been appended to the log before READ_LOCK(0) was obtained.
68159
+ ** When holding READ_LOCK(0), the reader ignores the entire log file,
68160
+ ** which implies that the database file contains a trustworthy
68161
+ ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
68162
+ ** happening, this is usually correct.
68163
+ **
68164
+ ** However, if frames have been appended to the log (or if the log
68165
+ ** is wrapped and written for that matter) before the READ_LOCK(0)
68166
+ ** is obtained, that is not necessarily true. A checkpointer may
68167
+ ** have started to backfill the appended frames but crashed before
68168
+ ** it finished. Leaving a corrupt image in the database file.
68169
+ */
68170
+ walUnlockShared(pWal, WAL_READ_LOCK(0));
68171
+ return WAL_RETRY;
68172
+ }
68173
+ pWal->readLock = 0;
68174
+ return SQLITE_OK;
68175
+ }else if( rc!=SQLITE_BUSY ){
68176
+ return rc;
68177
+ }
68178
+ }
68179
+
68180
+ /* If we get this far, it means that the reader will want to use
68181
+ ** the WAL to get at content from recent commits. The job now is
68182
+ ** to select one of the aReadMark[] entries that is closest to
68183
+ ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68184
+ */
68185
+ mxReadMark = 0;
68186
+ mxI = 0;
68187
+ mxFrame = pWal->hdr.mxFrame;
68188
+#ifdef SQLITE_ENABLE_SNAPSHOT
68189
+ if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68190
+ mxFrame = pWal->pSnapshot->mxFrame;
68191
+ }
68192
+#endif
68193
+ for(i=1; i<WAL_NREADER; i++){
68194
+ u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68195
+ if( mxReadMark<=thisMark && thisMark<=mxFrame ){
68196
+ assert( thisMark!=READMARK_NOT_USED );
68197
+ mxReadMark = thisMark;
68198
+ mxI = i;
68199
+ }
68200
+ }
68201
+ if( (pWal->readOnly & WAL_SHM_RDONLY)==0
68202
+ && (mxReadMark<mxFrame || mxI==0)
68203
+ ){
68204
+ for(i=1; i<WAL_NREADER; i++){
68205
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
68206
+ if( rc==SQLITE_OK ){
68207
+ AtomicStore(pInfo->aReadMark+i,mxFrame);
68208
+ mxReadMark = mxFrame;
68209
+ mxI = i;
68210
+ walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
68211
+ break;
68212
+ }else if( rc!=SQLITE_BUSY ){
68213
+ return rc;
68214
+ }
68215
+ }
68216
+ }
68217
+ if( mxI==0 ){
68218
+ assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
68219
+ return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
68220
+ }
68221
+
68222
+ (void)walEnableBlockingMs(pWal, nBlockTmout);
68223
+ rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
68224
+ walDisableBlocking(pWal);
68225
+ if( rc ){
68226
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68227
+ if( rc==SQLITE_BUSY_TIMEOUT ){
68228
+ *pCnt |= WAL_RETRY_BLOCKED_MASK;
68229
+ }
68230
+#else
68231
+ assert( rc!=SQLITE_BUSY_TIMEOUT );
68232
+#endif
68233
+ assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT);
68234
+ return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc;
68235
+ }
68236
+ /* Now that the read-lock has been obtained, check that neither the
68237
+ ** value in the aReadMark[] array or the contents of the wal-index
68238
+ ** header have changed.
68239
+ **
68240
+ ** It is necessary to check that the wal-index header did not change
68241
+ ** between the time it was read and when the shared-lock was obtained
68242
+ ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
68243
+ ** that the log file may have been wrapped by a writer, or that frames
68244
+ ** that occur later in the log than pWal->hdr.mxFrame may have been
68245
+ ** copied into the database by a checkpointer. If either of these things
68246
+ ** happened, then reading the database with the current value of
68247
+ ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68248
+ ** instead.
68249
+ **
68250
+ ** Before checking that the live wal-index header has not changed
68251
+ ** since it was read, set Wal.minFrame to the first frame in the wal
68252
+ ** file that has not yet been checkpointed. This client will not need
68253
+ ** to read any frames earlier than minFrame from the wal file - they
68254
+ ** can be safely read directly from the database file.
68255
+ **
68256
+ ** Because a ShmBarrier() call is made between taking the copy of
68257
+ ** nBackfill and checking that the wal-header in shared-memory still
68258
+ ** matches the one cached in pWal->hdr, it is guaranteed that the
68259
+ ** checkpointer that set nBackfill was not working with a wal-index
68260
+ ** header newer than that cached in pWal->hdr. If it were, that could
68261
+ ** cause a problem. The checkpointer could omit to checkpoint
68262
+ ** a version of page X that lies before pWal->minFrame (call that version
68263
+ ** A) on the basis that there is a newer version (version B) of the same
68264
+ ** page later in the wal file. But if version B happens to like past
68265
+ ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68266
+ ** that it can read version A from the database file. However, since
68267
+ ** we can guarantee that the checkpointer that set nBackfill could not
68268
+ ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68269
+ */
68270
+ pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
68271
+ walShmBarrier(pWal);
68272
+ if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
68273
+ || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68274
+ ){
68275
+ walUnlockShared(pWal, WAL_READ_LOCK(mxI));
68276
+ return WAL_RETRY;
68277
+ }else{
68278
+ assert( mxReadMark<=pWal->hdr.mxFrame );
68279
+ pWal->readLock = (i16)mxI;
68280
+ }
6822968281
}
6823068282
return rc;
6823168283
}
6823268284
6823368285
#ifdef SQLITE_ENABLE_SNAPSHOT
@@ -87103,10 +87155,11 @@
8710387155
**
8710487156
** All other fields of Mem can safely remain uninitialized for now. They
8710587157
** will be initialized before use.
8710687158
*/
8710787159
static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
87160
+ assert( db!=0 );
8710887161
if( N>0 ){
8710987162
do{
8711087163
p->flags = flags;
8711187164
p->db = db;
8711287165
p->szMalloc = 0;
@@ -87128,10 +87181,11 @@
8712887181
*/
8712987182
static void releaseMemArray(Mem *p, int N){
8713087183
if( p && N ){
8713187184
Mem *pEnd = &p[N];
8713287185
sqlite3 *db = p->db;
87186
+ assert( db!=0 );
8713387187
if( db->pnBytesFreed ){
8713487188
do{
8713587189
if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
8713687190
}while( (++p)<pEnd );
8713787191
return;
@@ -87608,10 +87662,11 @@
8760887662
assert( p!=0 );
8760987663
assert( p->nOp>0 );
8761087664
assert( pParse!=0 );
8761187665
assert( p->eVdbeState==VDBE_INIT_STATE );
8761287666
assert( pParse==p->pParse );
87667
+ assert( pParse->db==p->db );
8761387668
p->pVList = pParse->pVList;
8761487669
pParse->pVList = 0;
8761587670
db = p->db;
8761687671
assert( db->mallocFailed==0 );
8761787672
nVar = pParse->nVar;
@@ -90488,10 +90543,11 @@
9048890543
db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
9048990544
db->pPreUpdate = 0;
9049090545
sqlite3DbFree(db, preupdate.aRecord);
9049190546
vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
9049290547
vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
90548
+ sqlite3VdbeMemRelease(&preupdate.oldipk);
9049390549
if( preupdate.aNew ){
9049490550
int i;
9049590551
for(i=0; i<pCsr->nField; i++){
9049690552
sqlite3VdbeMemRelease(&preupdate.aNew[i]);
9049790553
}
@@ -91844,11 +91900,11 @@
9184491900
**
9184591901
** sqlite3_column_int()
9184691902
** sqlite3_column_int64()
9184791903
** sqlite3_column_text()
9184891904
** sqlite3_column_text16()
91849
-** sqlite3_column_real()
91905
+** sqlite3_column_double()
9185091906
** sqlite3_column_bytes()
9185191907
** sqlite3_column_bytes16()
9185291908
** sqlite3_column_blob()
9185391909
*/
9185491910
static void columnMallocFailure(sqlite3_stmt *pStmt)
@@ -92706,64 +92762,68 @@
9270692762
if( iIdx>=p->pCsr->nField || iIdx<0 ){
9270792763
rc = SQLITE_RANGE;
9270892764
goto preupdate_old_out;
9270992765
}
9271092766
92711
- /* If the old.* record has not yet been loaded into memory, do so now. */
92712
- if( p->pUnpacked==0 ){
92713
- u32 nRec;
92714
- u8 *aRec;
92715
-
92716
- assert( p->pCsr->eCurType==CURTYPE_BTREE );
92717
- nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92718
- aRec = sqlite3DbMallocRaw(db, nRec);
92719
- if( !aRec ) goto preupdate_old_out;
92720
- rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92721
- if( rc==SQLITE_OK ){
92722
- p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92723
- if( !p->pUnpacked ) rc = SQLITE_NOMEM;
92724
- }
92725
- if( rc!=SQLITE_OK ){
92726
- sqlite3DbFree(db, aRec);
92727
- goto preupdate_old_out;
92728
- }
92729
- p->aRecord = aRec;
92730
- }
92731
-
92732
- pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
9273392767
if( iIdx==p->pTab->iPKey ){
92768
+ *ppValue = pMem = &p->oldipk;
9273492769
sqlite3VdbeMemSetInt64(pMem, p->iKey1);
92735
- }else if( iIdx>=p->pUnpacked->nField ){
92736
- /* This occurs when the table has been extended using ALTER TABLE
92737
- ** ADD COLUMN. The value to return is the default value of the column. */
92738
- Column *pCol = &p->pTab->aCol[iIdx];
92739
- if( pCol->iDflt>0 ){
92740
- if( p->apDflt==0 ){
92741
- int nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
92742
- p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
92743
- if( p->apDflt==0 ) goto preupdate_old_out;
92744
- }
92745
- if( p->apDflt[iIdx]==0 ){
92746
- sqlite3_value *pVal = 0;
92747
- Expr *pDflt;
92748
- assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) );
92749
- pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
92750
- rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal);
92751
- if( rc==SQLITE_OK && pVal==0 ){
92752
- rc = SQLITE_CORRUPT_BKPT;
92753
- }
92754
- p->apDflt[iIdx] = pVal;
92755
- }
92756
- *ppValue = p->apDflt[iIdx];
92757
- }else{
92758
- *ppValue = (sqlite3_value *)columnNullValue();
92759
- }
92760
- }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
92761
- if( pMem->flags & (MEM_Int|MEM_IntReal) ){
92762
- testcase( pMem->flags & MEM_Int );
92763
- testcase( pMem->flags & MEM_IntReal );
92764
- sqlite3VdbeMemRealify(pMem);
92770
+ }else{
92771
+
92772
+ /* If the old.* record has not yet been loaded into memory, do so now. */
92773
+ if( p->pUnpacked==0 ){
92774
+ u32 nRec;
92775
+ u8 *aRec;
92776
+
92777
+ assert( p->pCsr->eCurType==CURTYPE_BTREE );
92778
+ nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92779
+ aRec = sqlite3DbMallocRaw(db, nRec);
92780
+ if( !aRec ) goto preupdate_old_out;
92781
+ rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92782
+ if( rc==SQLITE_OK ){
92783
+ p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92784
+ if( !p->pUnpacked ) rc = SQLITE_NOMEM;
92785
+ }
92786
+ if( rc!=SQLITE_OK ){
92787
+ sqlite3DbFree(db, aRec);
92788
+ goto preupdate_old_out;
92789
+ }
92790
+ p->aRecord = aRec;
92791
+ }
92792
+
92793
+ pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
92794
+ if( iIdx>=p->pUnpacked->nField ){
92795
+ /* This occurs when the table has been extended using ALTER TABLE
92796
+ ** ADD COLUMN. The value to return is the default value of the column. */
92797
+ Column *pCol = &p->pTab->aCol[iIdx];
92798
+ if( pCol->iDflt>0 ){
92799
+ if( p->apDflt==0 ){
92800
+ int nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
92801
+ p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
92802
+ if( p->apDflt==0 ) goto preupdate_old_out;
92803
+ }
92804
+ if( p->apDflt[iIdx]==0 ){
92805
+ sqlite3_value *pVal = 0;
92806
+ Expr *pDflt;
92807
+ assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) );
92808
+ pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
92809
+ rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal);
92810
+ if( rc==SQLITE_OK && pVal==0 ){
92811
+ rc = SQLITE_CORRUPT_BKPT;
92812
+ }
92813
+ p->apDflt[iIdx] = pVal;
92814
+ }
92815
+ *ppValue = p->apDflt[iIdx];
92816
+ }else{
92817
+ *ppValue = (sqlite3_value *)columnNullValue();
92818
+ }
92819
+ }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
92820
+ if( pMem->flags & (MEM_Int|MEM_IntReal) ){
92821
+ testcase( pMem->flags & MEM_Int );
92822
+ testcase( pMem->flags & MEM_IntReal );
92823
+ sqlite3VdbeMemRealify(pMem);
92824
+ }
9276592825
}
9276692826
}
9276792827
9276892828
preupdate_old_out:
9276992829
sqlite3Error(db, rc);
@@ -97913,13 +97973,15 @@
9791397973
0, pCx->uc.pCursor);
9791497974
pCx->isTable = 1;
9791597975
}
9791697976
}
9791797977
pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
97978
+ assert( p->apCsr[pOp->p1]==pCx );
9791897979
if( rc ){
9791997980
assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
9792097981
sqlite3BtreeClose(pCx->ub.pBtx);
97982
+ p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */
9792197983
}else{
9792297984
assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
9792397985
}
9792497986
}
9792597987
}
@@ -109845,11 +109907,11 @@
109845109907
p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
109846109908
}
109847109909
p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
109848109910
addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
109849109911
(void*)p4, P4_COLLSEQ);
109850
- sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
109912
+ sqlite3VdbeChangeP5(pParse->pVdbe, (u16)p5);
109851109913
return addr;
109852109914
}
109853109915
109854109916
/*
109855109917
** Return true if expression pExpr is a vector, or false otherwise.
@@ -112012,11 +112074,11 @@
112012112074
**
112013112075
** (4) If pSrc is the right operand of a LEFT JOIN, then...
112014112076
** (4a) pExpr must come from an ON clause..
112015112077
** (4b) and specifically the ON clause associated with the LEFT JOIN.
112016112078
**
112017
-** (5) If pSrc is not the right operand of a LEFT JOIN or the left
112079
+** (5) If pSrc is the right operand of a LEFT JOIN or the left
112018112080
** operand of a RIGHT JOIN, then pExpr must be from the WHERE
112019112081
** clause, not an ON clause.
112020112082
**
112021112083
** (6) Either:
112022112084
**
@@ -115546,35 +115608,41 @@
115546115608
**
115547115609
** Additionally, if pExpr is a simple SQL value and the value is the
115548115610
** same as that currently bound to variable pVar, non-zero is returned.
115549115611
** Otherwise, if the values are not the same or if pExpr is not a simple
115550115612
** SQL value, zero is returned.
115613
+**
115614
+** If the SQLITE_EnableQPSG flag is set on the database connection, then
115615
+** this routine always returns false.
115551115616
*/
115552
-static int exprCompareVariable(
115617
+static SQLITE_NOINLINE int exprCompareVariable(
115553115618
const Parse *pParse,
115554115619
const Expr *pVar,
115555115620
const Expr *pExpr
115556115621
){
115557
- int res = 0;
115622
+ int res = 2;
115558115623
int iVar;
115559115624
sqlite3_value *pL, *pR = 0;
115560115625
115626
+ if( pExpr->op==TK_VARIABLE && pVar->iColumn==pExpr->iColumn ){
115627
+ return 0;
115628
+ }
115629
+ if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) return 2;
115561115630
sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
115562115631
if( pR ){
115563115632
iVar = pVar->iColumn;
115564115633
sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
115565115634
pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
115566115635
if( pL ){
115567115636
if( sqlite3_value_type(pL)==SQLITE_TEXT ){
115568115637
sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
115569115638
}
115570
- res = 0==sqlite3MemCompare(pL, pR, 0);
115639
+ res = sqlite3MemCompare(pL, pR, 0) ? 2 : 0;
115571115640
}
115572115641
sqlite3ValueFree(pR);
115573115642
sqlite3ValueFree(pL);
115574115643
}
115575
-
115576115644
return res;
115577115645
}
115578115646
115579115647
/*
115580115648
** Do a deep comparison of two expression trees. Return 0 if the two
@@ -115596,16 +115664,14 @@
115596115664
** can be sure the expressions are the same. In the places where
115597115665
** this routine is used, it does not hurt to get an extra 2 - that
115598115666
** just might result in some slightly slower code. But returning
115599115667
** an incorrect 0 or 1 could lead to a malfunction.
115600115668
**
115601
-** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
115602
-** pParse->pReprepare can be matched against literals in pB. The
115603
-** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
115604
-** If pParse is NULL (the normal case) then any TK_VARIABLE term in
115605
-** Argument pParse should normally be NULL. If it is not NULL and pA or
115606
-** pB causes a return value of 2.
115669
+** If pParse is not NULL and SQLITE_EnableQPSG is off then TK_VARIABLE
115670
+** terms in pA with bindings in pParse->pReprepare can be matched against
115671
+** literals in pB. The pParse->pVdbe->expmask bitmask is updated for
115672
+** each variable referenced.
115607115673
*/
115608115674
SQLITE_PRIVATE int sqlite3ExprCompare(
115609115675
const Parse *pParse,
115610115676
const Expr *pA,
115611115677
const Expr *pB,
@@ -115613,12 +115679,12 @@
115613115679
){
115614115680
u32 combinedFlags;
115615115681
if( pA==0 || pB==0 ){
115616115682
return pB==pA ? 0 : 2;
115617115683
}
115618
- if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
115619
- return 0;
115684
+ if( pParse && pA->op==TK_VARIABLE ){
115685
+ return exprCompareVariable(pParse, pA, pB);
115620115686
}
115621115687
combinedFlags = pA->flags | pB->flags;
115622115688
if( combinedFlags & EP_IntValue ){
115623115689
if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
115624115690
return 0;
@@ -115808,23 +115874,75 @@
115808115874
return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115809115875
}
115810115876
}
115811115877
return 0;
115812115878
}
115879
+
115880
+/*
115881
+** Return true if the boolean value of the expression is always either
115882
+** FALSE or NULL.
115883
+*/
115884
+static int sqlite3ExprIsNotTrue(Expr *pExpr){
115885
+ int v;
115886
+ if( pExpr->op==TK_NULL ) return 1;
115887
+ if( pExpr->op==TK_TRUEFALSE && sqlite3ExprTruthValue(pExpr)==0 ) return 1;
115888
+ v = 1;
115889
+ if( sqlite3ExprIsInteger(pExpr, &v, 0) && v==0 ) return 1;
115890
+ return 0;
115891
+}
115892
+
115893
+/*
115894
+** Return true if the expression is one of the following:
115895
+**
115896
+** CASE WHEN x THEN y END
115897
+** CASE WHEN x THEN y ELSE NULL END
115898
+** CASE WHEN x THEN y ELSE false END
115899
+** iif(x,y)
115900
+** iif(x,y,NULL)
115901
+** iif(x,y,false)
115902
+*/
115903
+static int sqlite3ExprIsIIF(sqlite3 *db, const Expr *pExpr){
115904
+ ExprList *pList;
115905
+ if( pExpr->op==TK_FUNCTION ){
115906
+ const char *z = pExpr->u.zToken;
115907
+ FuncDef *pDef;
115908
+ if( (z[0]!='i' && z[0]!='I') ) return 0;
115909
+ if( pExpr->x.pList==0 ) return 0;
115910
+ pDef = sqlite3FindFunction(db, z, pExpr->x.pList->nExpr, ENC(db), 0);
115911
+#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
115912
+ if( pDef==0 ) return 0;
115913
+#else
115914
+ if( NEVER(pDef==0) ) return 0;
115915
+#endif
115916
+ if( (pDef->funcFlags & SQLITE_FUNC_INLINE)==0 ) return 0;
115917
+ if( SQLITE_PTR_TO_INT(pDef->pUserData)!=INLINEFUNC_iif ) return 0;
115918
+ }else if( pExpr->op==TK_CASE ){
115919
+ if( pExpr->pLeft!=0 ) return 0;
115920
+ }else{
115921
+ return 0;
115922
+ }
115923
+ pList = pExpr->x.pList;
115924
+ assert( pList!=0 );
115925
+ if( pList->nExpr==2 ) return 1;
115926
+ if( pList->nExpr==3 && sqlite3ExprIsNotTrue(pList->a[2].pExpr) ) return 1;
115927
+ return 0;
115928
+}
115813115929
115814115930
/*
115815115931
** Return true if we can prove the pE2 will always be true if pE1 is
115816115932
** true. Return false if we cannot complete the proof or if pE2 might
115817115933
** be false. Examples:
115818115934
**
115819
-** pE1: x==5 pE2: x==5 Result: true
115820
-** pE1: x>0 pE2: x==5 Result: false
115821
-** pE1: x=21 pE2: x=21 OR y=43 Result: true
115822
-** pE1: x!=123 pE2: x IS NOT NULL Result: true
115823
-** pE1: x!=?1 pE2: x IS NOT NULL Result: true
115824
-** pE1: x IS NULL pE2: x IS NOT NULL Result: false
115825
-** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
115935
+** pE1: x==5 pE2: x==5 Result: true
115936
+** pE1: x>0 pE2: x==5 Result: false
115937
+** pE1: x=21 pE2: x=21 OR y=43 Result: true
115938
+** pE1: x!=123 pE2: x IS NOT NULL Result: true
115939
+** pE1: x!=?1 pE2: x IS NOT NULL Result: true
115940
+** pE1: x IS NULL pE2: x IS NOT NULL Result: false
115941
+** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
115942
+** pE1: iif(x,y) pE2: x Result: true
115943
+** PE1: iif(x,y,0) pE2: x Result: true
115826115944
**
115827115945
** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
115828115946
** Expr.iTable<0 then assume a table number given by iTab.
115829115947
**
115830115948
** If pParse is not NULL, then the values of bound variables in pE1 are
@@ -115854,10 +115972,13 @@
115854115972
if( pE2->op==TK_NOTNULL
115855115973
&& exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
115856115974
){
115857115975
return 1;
115858115976
}
115977
+ if( sqlite3ExprIsIIF(pParse->db, pE1) ){
115978
+ return sqlite3ExprImpliesExpr(pParse,pE1->x.pList->a[0].pExpr,pE2,iTab);
115979
+ }
115859115980
return 0;
115860115981
}
115861115982
115862115983
/* This is a helper function to impliesNotNullRow(). In this routine,
115863115984
** set pWalker->eCode to one only if *both* of the input expressions
@@ -121265,19 +121386,10 @@
121265121386
rc = sqlite3Init(db, &zErrDyn);
121266121387
}
121267121388
sqlite3BtreeLeaveAll(db);
121268121389
assert( zErrDyn==0 || rc!=SQLITE_OK );
121269121390
}
121270
-#ifdef SQLITE_USER_AUTHENTICATION
121271
- if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
121272
- u8 newAuth = 0;
121273
- rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
121274
- if( newAuth<db->auth.authLevel ){
121275
- rc = SQLITE_AUTH_USER;
121276
- }
121277
- }
121278
-#endif
121279121391
if( rc ){
121280121392
if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){
121281121393
int iDb = db->nDb - 1;
121282121394
assert( iDb>=2 );
121283121395
if( db->aDb[iDb].pBt ){
@@ -121771,15 +121883,11 @@
121771121883
sqlite3 *db = pParse->db; /* Database handle */
121772121884
char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
121773121885
int rc; /* Auth callback return code */
121774121886
121775121887
if( db->init.busy ) return SQLITE_OK;
121776
- rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
121777
-#ifdef SQLITE_USER_AUTHENTICATION
121778
- ,db->auth.zAuthUser
121779
-#endif
121780
- );
121888
+ rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
121781121889
if( rc==SQLITE_DENY ){
121782121890
char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
121783121891
if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
121784121892
sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
121785121893
pParse->rc = SQLITE_AUTH;
@@ -121882,15 +121990,11 @@
121882121990
testcase( zArg1==0 );
121883121991
testcase( zArg2==0 );
121884121992
testcase( zArg3==0 );
121885121993
testcase( pParse->zAuthContext==0 );
121886121994
121887
- rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
121888
-#ifdef SQLITE_USER_AUTHENTICATION
121889
- ,db->auth.zAuthUser
121890
-#endif
121891
- );
121995
+ rc = db->xAuth(db->pAuthArg,code,zArg1,zArg2,zArg3,pParse->zAuthContext);
121892121996
if( rc==SQLITE_DENY ){
121893121997
sqlite3ErrorMsg(pParse, "not authorized");
121894121998
pParse->rc = SQLITE_AUTH;
121895121999
}else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
121896122000
rc = SQLITE_DENY;
@@ -122119,21 +122223,10 @@
122119122223
sqlite3VdbeJumpHere(v, addrRewind);
122120122224
}
122121122225
}
122122122226
sqlite3VdbeAddOp0(v, OP_Halt);
122123122227
122124
-#if SQLITE_USER_AUTHENTICATION && !defined(SQLITE_OMIT_SHARED_CACHE)
122125
- if( pParse->nTableLock>0 && db->init.busy==0 ){
122126
- sqlite3UserAuthInit(db);
122127
- if( db->auth.authLevel<UAUTH_User ){
122128
- sqlite3ErrorMsg(pParse, "user not authenticated");
122129
- pParse->rc = SQLITE_AUTH_USER;
122130
- return;
122131
- }
122132
- }
122133
-#endif
122134
-
122135122228
/* The cookie mask contains one bit for each database file open.
122136122229
** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
122137122230
** set for each database that is used. Generate code to start a
122138122231
** transaction on each used database and to verify the schema cookie
122139122232
** on each used database.
@@ -122258,20 +122351,10 @@
122258122351
sqlite3DbFree(db, zSql);
122259122352
memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
122260122353
pParse->nested--;
122261122354
}
122262122355
122263
-#if SQLITE_USER_AUTHENTICATION
122264
-/*
122265
-** Return TRUE if zTable is the name of the system table that stores the
122266
-** list of users and their access credentials.
122267
-*/
122268
-SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
122269
- return sqlite3_stricmp(zTable, "sqlite_user")==0;
122270
-}
122271
-#endif
122272
-
122273122356
/*
122274122357
** Locate the in-memory structure that describes a particular database
122275122358
** table given the name of that table and (optionally) the name of the
122276122359
** database containing the table. Return NULL if not found.
122277122360
**
@@ -122286,17 +122369,10 @@
122286122369
Table *p = 0;
122287122370
int i;
122288122371
122289122372
/* All mutexes are required for schema access. Make sure we hold them. */
122290122373
assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
122291
-#if SQLITE_USER_AUTHENTICATION
122292
- /* Only the admin user is allowed to know that the sqlite_user table
122293
- ** exists */
122294
- if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
122295
- return 0;
122296
- }
122297
-#endif
122298122374
if( zDatabase ){
122299122375
for(i=0; i<db->nDb; i++){
122300122376
if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
122301122377
}
122302122378
if( i>=db->nDb ){
@@ -125951,13 +126027,10 @@
125951126027
125952126028
assert( pTab!=0 );
125953126029
if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
125954126030
&& db->init.busy==0
125955126031
&& pTblName!=0
125956
-#if SQLITE_USER_AUTHENTICATION
125957
- && sqlite3UserAuthTable(pTab->zName)==0
125958
-#endif
125959126032
){
125960126033
sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
125961126034
goto exit_create_index;
125962126035
}
125963126036
#ifndef SQLITE_OMIT_VIEW
@@ -129661,20 +129734,19 @@
129661129734
const unsigned char *z;
129662129735
const unsigned char *z2;
129663129736
int len;
129664129737
int p0type;
129665129738
i64 p1, p2;
129666
- int negP2 = 0;
129667129739
129668129740
assert( argc==3 || argc==2 );
129669129741
if( sqlite3_value_type(argv[1])==SQLITE_NULL
129670129742
|| (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
129671129743
){
129672129744
return;
129673129745
}
129674129746
p0type = sqlite3_value_type(argv[0]);
129675
- p1 = sqlite3_value_int(argv[1]);
129747
+ p1 = sqlite3_value_int64(argv[1]);
129676129748
if( p0type==SQLITE_BLOB ){
129677129749
len = sqlite3_value_bytes(argv[0]);
129678129750
z = sqlite3_value_blob(argv[0]);
129679129751
if( z==0 ) return;
129680129752
assert( len==sqlite3_value_bytes(argv[0]) );
@@ -129695,36 +129767,36 @@
129695129767
** from 2009-02-02 for compatibility of applications that exploited the
129696129768
** old buggy behavior. */
129697129769
if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
129698129770
#endif
129699129771
if( argc==3 ){
129700
- p2 = sqlite3_value_int(argv[2]);
129701
- if( p2<0 ){
129702
- p2 = -p2;
129703
- negP2 = 1;
129704
- }
129772
+ p2 = sqlite3_value_int64(argv[2]);
129705129773
}else{
129706129774
p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
129707129775
}
129708129776
if( p1<0 ){
129709129777
p1 += len;
129710129778
if( p1<0 ){
129711
- p2 += p1;
129712
- if( p2<0 ) p2 = 0;
129779
+ if( p2<0 ){
129780
+ p2 = 0;
129781
+ }else{
129782
+ p2 += p1;
129783
+ }
129713129784
p1 = 0;
129714129785
}
129715129786
}else if( p1>0 ){
129716129787
p1--;
129717129788
}else if( p2>0 ){
129718129789
p2--;
129719129790
}
129720
- if( negP2 ){
129791
+ if( p2<0 ){
129792
+ if( p2<-p1 ){
129793
+ p2 = p1;
129794
+ }else{
129795
+ p2 = -p2;
129796
+ }
129721129797
p1 -= p2;
129722
- if( p1<0 ){
129723
- p2 += p1;
129724
- p1 = 0;
129725
- }
129726129798
}
129727129799
assert( p1>=0 && p2>=0 );
129728129800
if( p0type!=SQLITE_BLOB ){
129729129801
while( *z && p1 ){
129730129802
SQLITE_SKIP_UTF8(z);
@@ -129734,13 +129806,15 @@
129734129806
SQLITE_SKIP_UTF8(z2);
129735129807
}
129736129808
sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
129737129809
SQLITE_UTF8);
129738129810
}else{
129739
- if( p1+p2>len ){
129811
+ if( p1>=len ){
129812
+ p1 = p2 = 0;
129813
+ }else if( p2>len-p1 ){
129740129814
p2 = len-p1;
129741
- if( p2<0 ) p2 = 0;
129815
+ assert( p2>0 );
129742129816
}
129743129817
sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
129744129818
}
129745129819
}
129746129820
@@ -129747,17 +129821,17 @@
129747129821
/*
129748129822
** Implementation of the round() function
129749129823
*/
129750129824
#ifndef SQLITE_OMIT_FLOATING_POINT
129751129825
static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129752
- int n = 0;
129826
+ i64 n = 0;
129753129827
double r;
129754129828
char *zBuf;
129755129829
assert( argc==1 || argc==2 );
129756129830
if( argc==2 ){
129757129831
if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
129758
- n = sqlite3_value_int(argv[1]);
129832
+ n = sqlite3_value_int64(argv[1]);
129759129833
if( n>30 ) n = 30;
129760129834
if( n<0 ) n = 0;
129761129835
}
129762129836
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
129763129837
r = sqlite3_value_double(argv[0]);
@@ -129768,11 +129842,11 @@
129768129842
if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
129769129843
/* The value has no fractional part so there is nothing to round */
129770129844
}else if( n==0 ){
129771129845
r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
129772129846
}else{
129773
- zBuf = sqlite3_mprintf("%!.*f",n,r);
129847
+ zBuf = sqlite3_mprintf("%!.*f",(int)n,r);
129774129848
if( zBuf==0 ){
129775129849
sqlite3_result_error_nomem(context);
129776129850
return;
129777129851
}
129778129852
sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
@@ -131985,13 +132059,10 @@
131985132059
#endif
131986132060
#ifndef SQLITE_OMIT_LOAD_EXTENSION
131987132061
SFUNCTION(load_extension, 1, 0, 0, loadExt ),
131988132062
SFUNCTION(load_extension, 2, 0, 0, loadExt ),
131989132063
#endif
131990
-#if SQLITE_USER_AUTHENTICATION
131991
- FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
131992
-#endif
131993132064
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
131994132065
DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
131995132066
DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
131996132067
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
131997132068
INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
@@ -132124,11 +132195,14 @@
132124132195
MFUNCTION(degrees, 1, radToDeg, math1Func ),
132125132196
MFUNCTION(pi, 0, 0, piFunc ),
132126132197
#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
132127132198
FUNCTION(sign, 1, 0, 0, signFunc ),
132128132199
INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
132200
+ INLINE_FUNC(iif, 2, INLINEFUNC_iif, 0 ),
132129132201
INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ),
132202
+ INLINE_FUNC(if, 2, INLINEFUNC_iif, 0 ),
132203
+ INLINE_FUNC(if, 3, INLINEFUNC_iif, 0 ),
132130132204
};
132131132205
#ifndef SQLITE_OMIT_ALTERTABLE
132132132206
sqlite3AlterFunctions();
132133132207
#endif
132134132208
sqlite3WindowFunctions();
@@ -140637,16 +140711,10 @@
140637140711
if( db->autoCommit==0 ){
140638140712
/* Foreign key support may not be enabled or disabled while not
140639140713
** in auto-commit mode. */
140640140714
mask &= ~(SQLITE_ForeignKeys);
140641140715
}
140642
-#if SQLITE_USER_AUTHENTICATION
140643
- if( db->auth.authLevel==UAUTH_User ){
140644
- /* Do not allow non-admin users to modify the schema arbitrarily */
140645
- mask &= ~(SQLITE_WriteSchema);
140646
- }
140647
-#endif
140648140716
140649140717
if( sqlite3GetBoolean(zRight, 0) ){
140650140718
if( (mask & SQLITE_WriteSchema)==0
140651140719
|| (db->flags & SQLITE_Defensive)==0
140652140720
){
@@ -140778,11 +140846,12 @@
140778140846
pTab = sqliteHashData(k);
140779140847
if( pTab->nCol==0 ){
140780140848
char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
140781140849
if( zSql ){
140782140850
sqlite3_stmt *pDummy = 0;
140783
- (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
140851
+ (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG,
140852
+ &pDummy, 0);
140784140853
(void)sqlite3_finalize(pDummy);
140785140854
sqlite3DbFree(db, zSql);
140786140855
}
140787140856
if( db->mallocFailed ){
140788140857
sqlite3ErrorMsg(db->pParse, "out of memory");
@@ -141259,11 +141328,11 @@
141259141328
sqlite3VdbeAddOp3(v, OP_Null, 0, 8, 8+cnt);
141260141329
sqlite3ClearTempRegCache(pParse);
141261141330
141262141331
/* Do the b-tree integrity checks */
141263141332
sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY);
141264
- sqlite3VdbeChangeP5(v, (u8)i);
141333
+ sqlite3VdbeChangeP5(v, (u16)i);
141265141334
addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
141266141335
sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
141267141336
sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
141268141337
P4_DYNAMIC);
141269141338
sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
@@ -142879,18 +142948,11 @@
142879142948
encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
142880142949
if( encoding==0 ) encoding = SQLITE_UTF8;
142881142950
#else
142882142951
encoding = SQLITE_UTF8;
142883142952
#endif
142884
- if( db->nVdbeActive>0 && encoding!=ENC(db)
142885
- && (db->mDbFlags & DBFLAG_Vacuum)==0
142886
- ){
142887
- rc = SQLITE_LOCKED;
142888
- goto initone_error_out;
142889
- }else{
142890
- sqlite3SetTextEncoding(db, encoding);
142891
- }
142953
+ sqlite3SetTextEncoding(db, encoding);
142892142954
}else{
142893142955
/* If opening an attached database, the encoding much match ENC(db) */
142894142956
if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
142895142957
sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
142896142958
" text encoding as main database");
@@ -147584,36 +147646,36 @@
147584147646
return pExpr;
147585147647
}
147586147648
if( pSubst->isOuterJoin ){
147587147649
ExprSetProperty(pNew, EP_CanBeNull);
147588147650
}
147651
+ if( pNew->op==TK_TRUEFALSE ){
147652
+ pNew->u.iValue = sqlite3ExprTruthValue(pNew);
147653
+ pNew->op = TK_INTEGER;
147654
+ ExprSetProperty(pNew, EP_IntValue);
147655
+ }
147656
+
147657
+ /* Ensure that the expression now has an implicit collation sequence,
147658
+ ** just as it did when it was a column of a view or sub-query. */
147659
+ {
147660
+ CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pNew);
147661
+ CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
147662
+ pSubst->pCList->a[iColumn].pExpr
147663
+ );
147664
+ if( pNat!=pColl || (pNew->op!=TK_COLUMN && pNew->op!=TK_COLLATE) ){
147665
+ pNew = sqlite3ExprAddCollateString(pSubst->pParse, pNew,
147666
+ (pColl ? pColl->zName : "BINARY")
147667
+ );
147668
+ }
147669
+ }
147670
+ ExprClearProperty(pNew, EP_Collate);
147589147671
if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){
147590147672
sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
147591147673
pExpr->flags & (EP_OuterON|EP_InnerON));
147592147674
}
147593147675
sqlite3ExprDelete(db, pExpr);
147594147676
pExpr = pNew;
147595
- if( pExpr->op==TK_TRUEFALSE ){
147596
- pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
147597
- pExpr->op = TK_INTEGER;
147598
- ExprSetProperty(pExpr, EP_IntValue);
147599
- }
147600
-
147601
- /* Ensure that the expression now has an implicit collation sequence,
147602
- ** just as it did when it was a column of a view or sub-query. */
147603
- {
147604
- CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
147605
- CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
147606
- pSubst->pCList->a[iColumn].pExpr
147607
- );
147608
- if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
147609
- pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
147610
- (pColl ? pColl->zName : "BINARY")
147611
- );
147612
- }
147613
- }
147614
- ExprClearProperty(pExpr, EP_Collate);
147615147677
}
147616147678
}
147617147679
}else{
147618147680
if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
147619147681
pExpr->iTable = pSubst->iNewTable;
@@ -148346,20 +148408,20 @@
148346148408
}
148347148409
148348148410
/* Transfer the FROM clause terms from the subquery into the
148349148411
** outer query.
148350148412
*/
148413
+ iNewParent = pSubSrc->a[0].iCursor;
148351148414
for(i=0; i<nSubSrc; i++){
148352148415
SrcItem *pItem = &pSrc->a[i+iFrom];
148353148416
assert( pItem->fg.isTabFunc==0 );
148354148417
assert( pItem->fg.isSubquery
148355148418
|| pItem->fg.fixedSchema
148356148419
|| pItem->u4.zDatabase==0 );
148357148420
if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
148358148421
*pItem = pSubSrc->a[i];
148359148422
pItem->fg.jointype |= ltorj;
148360
- iNewParent = pSubSrc->a[i].iCursor;
148361148423
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
148362148424
}
148363148425
pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
148364148426
pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
148365148427
@@ -148395,10 +148457,11 @@
148395148457
pSub->pOrderBy = 0;
148396148458
}
148397148459
pWhere = pSub->pWhere;
148398148460
pSub->pWhere = 0;
148399148461
if( isOuterJoin>0 ){
148462
+ assert( pSubSrc->nSrc==1 );
148400148463
sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON);
148401148464
}
148402148465
if( pWhere ){
148403148466
if( pParent->pWhere ){
148404148467
pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
@@ -150498,11 +150561,11 @@
150498150561
}
150499150562
sqlite3ReleaseTempReg(pParse, regSubtype);
150500150563
}
150501150564
sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
150502150565
sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
150503
- sqlite3VdbeChangeP5(v, (u8)nArg);
150566
+ sqlite3VdbeChangeP5(v, (u16)nArg);
150504150567
sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v);
150505150568
sqlite3VdbeJumpHere(v, iTop);
150506150569
sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150507150570
}
150508150571
sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i),
@@ -150661,11 +150724,11 @@
150661150724
sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0,
150662150725
(char *)pColl, P4_COLLSEQ);
150663150726
}
150664150727
sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
150665150728
sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
150666
- sqlite3VdbeChangeP5(v, (u8)nArg);
150729
+ sqlite3VdbeChangeP5(v, (u16)nArg);
150667150730
sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150668150731
}
150669150732
if( addrNext ){
150670150733
sqlite3VdbeResolveLabel(v, addrNext);
150671150734
}
@@ -151494,11 +151557,11 @@
151494151557
sqlite3TreeViewSelect(0, p, 0);
151495151558
}
151496151559
#endif
151497151560
assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 );
151498151561
}else{
151499
- TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n"));
151562
+ TREETRACE(0x4000,pParse,p,("WHERE-clause push-down not possible\n"));
151500151563
}
151501151564
151502151565
/* Convert unused result columns of the subquery into simple NULL
151503151566
** expressions, to avoid unneeded searching and computation.
151504151567
** tag-select-0440
@@ -154055,11 +154118,11 @@
154055154118
/* Set the P5 operand of the OP_Program instruction to non-zero if
154056154119
** recursive invocation of this trigger program is disallowed. Recursive
154057154120
** invocation is disallowed if (a) the sub-program is really a trigger,
154058154121
** not a foreign key action, and (b) the flag to enable recursive triggers
154059154122
** is clear. */
154060
- sqlite3VdbeChangeP5(v, (u8)bRecursive);
154123
+ sqlite3VdbeChangeP5(v, (u16)bRecursive);
154061154124
}
154062154125
}
154063154126
154064154127
/*
154065154128
** This is called to code the required FOR EACH ROW triggers for an operation
@@ -158268,13 +158331,21 @@
158268158331
SQLITE_PRIVATE int sqlite3WhereExplainBloomFilter(
158269158332
const Parse *pParse, /* Parse context */
158270158333
const WhereInfo *pWInfo, /* WHERE clause */
158271158334
const WhereLevel *pLevel /* Bloom filter on this level */
158272158335
);
158336
+SQLITE_PRIVATE void sqlite3WhereAddExplainText(
158337
+ Parse *pParse, /* Parse context */
158338
+ int addr,
158339
+ SrcList *pTabList, /* Table list this loop refers to */
158340
+ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158341
+ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158342
+);
158273158343
#else
158274158344
# define sqlite3WhereExplainOneScan(u,v,w,x) 0
158275158345
# define sqlite3WhereExplainBloomFilter(u,v,w) 0
158346
+# define sqlite3WhereAddExplainText(u,v,w,x,y)
158276158347
#endif /* SQLITE_OMIT_EXPLAIN */
158277158348
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
158278158349
SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
158279158350
Vdbe *v, /* Vdbe to add scanstatus entry to */
158280158351
SrcList *pSrclist, /* FROM clause pLvl reads data from */
@@ -158472,42 +158543,42 @@
158472158543
}
158473158544
sqlite3_str_append(pStr, ")", 1);
158474158545
}
158475158546
158476158547
/*
158477
-** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
158478
-** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
158479
-** was defined at compile-time. If it is not a no-op, a single OP_Explain
158480
-** opcode is added to the output to describe the table scan strategy in pLevel.
158481
-**
158482
-** If an OP_Explain opcode is added to the VM, its address is returned.
158483
-** Otherwise, if no OP_Explain is coded, zero is returned.
158548
+** This function sets the P4 value of an existing OP_Explain opcode to
158549
+** text describing the loop in pLevel. If the OP_Explain opcode already has
158550
+** a P4 value, it is freed before it is overwritten.
158484158551
*/
158485
-SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
158552
+SQLITE_PRIVATE void sqlite3WhereAddExplainText(
158486158553
Parse *pParse, /* Parse context */
158554
+ int addr, /* Address of OP_Explain opcode */
158487158555
SrcList *pTabList, /* Table list this loop refers to */
158488158556
WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158489158557
u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158490158558
){
158491
- int ret = 0;
158492158559
#if !defined(SQLITE_DEBUG)
158493158560
if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
158494158561
#endif
158495158562
{
158563
+ VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr);
158564
+
158496158565
SrcItem *pItem = &pTabList->a[pLevel->iFrom];
158497
- Vdbe *v = pParse->pVdbe; /* VM being constructed */
158498158566
sqlite3 *db = pParse->db; /* Database handle */
158499158567
int isSearch; /* True for a SEARCH. False for SCAN. */
158500158568
WhereLoop *pLoop; /* The controlling WhereLoop object */
158501158569
u32 flags; /* Flags that describe this loop */
158570
+#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
158502158571
char *zMsg; /* Text to add to EQP output */
158572
+#endif
158503158573
StrAccum str; /* EQP output string */
158504158574
char zBuf[100]; /* Initial space for EQP output string */
158505158575
158576
+ if( db->mallocFailed ) return;
158577
+
158506158578
pLoop = pLevel->pWLoop;
158507158579
flags = pLoop->wsFlags;
158508
- if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
158509158580
158510158581
isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
158511158582
|| ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
158512158583
|| (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
158513158584
@@ -158527,11 +158598,11 @@
158527158598
}
158528158599
}else if( flags & WHERE_PARTIALIDX ){
158529158600
zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
158530158601
}else if( flags & WHERE_AUTO_INDEX ){
158531158602
zFmt = "AUTOMATIC COVERING INDEX";
158532
- }else if( flags & WHERE_IDX_ONLY ){
158603
+ }else if( flags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){
158533158604
zFmt = "COVERING INDEX %s";
158534158605
}else{
158535158606
zFmt = "INDEX %s";
158536158607
}
158537158608
if( zFmt ){
@@ -158579,15 +158650,54 @@
158579158650
sqlite3LogEstToInt(pLoop->nOut));
158580158651
}else{
158581158652
sqlite3_str_append(&str, " (~1 row)", 9);
158582158653
}
158583158654
#endif
158655
+#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
158584158656
zMsg = sqlite3StrAccumFinish(&str);
158585158657
sqlite3ExplainBreakpoint("",zMsg);
158586
- ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
158587
- pParse->addrExplain, pLoop->rRun,
158588
- zMsg, P4_DYNAMIC);
158658
+#endif
158659
+
158660
+ assert( pOp->opcode==OP_Explain );
158661
+ assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 );
158662
+ sqlite3DbFree(db, pOp->p4.z);
158663
+ pOp->p4type = P4_DYNAMIC;
158664
+ pOp->p4.z = sqlite3StrAccumFinish(&str);
158665
+ }
158666
+}
158667
+
158668
+
158669
+/*
158670
+** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
158671
+** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
158672
+** was defined at compile-time. If it is not a no-op, a single OP_Explain
158673
+** opcode is added to the output to describe the table scan strategy in pLevel.
158674
+**
158675
+** If an OP_Explain opcode is added to the VM, its address is returned.
158676
+** Otherwise, if no OP_Explain is coded, zero is returned.
158677
+*/
158678
+SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
158679
+ Parse *pParse, /* Parse context */
158680
+ SrcList *pTabList, /* Table list this loop refers to */
158681
+ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158682
+ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158683
+){
158684
+ int ret = 0;
158685
+#if !defined(SQLITE_DEBUG)
158686
+ if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
158687
+#endif
158688
+ {
158689
+ if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0
158690
+ && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
158691
+ ){
158692
+ Vdbe *v = pParse->pVdbe;
158693
+ int addr = sqlite3VdbeCurrentAddr(v);
158694
+ ret = sqlite3VdbeAddOp3(
158695
+ v, OP_Explain, addr, pParse->addrExplain, pLevel->pWLoop->rRun
158696
+ );
158697
+ sqlite3WhereAddExplainText(pParse, addr, pTabList, pLevel, wctrlFlags);
158698
+ }
158589158699
}
158590158700
return ret;
158591158701
}
158592158702
158593158703
/*
@@ -158682,13 +158792,14 @@
158682158792
if( wsFlags & WHERE_INDEXED ){
158683158793
sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
158684158794
}
158685158795
}else{
158686158796
int addr;
158797
+ VdbeOp *pOp;
158687158798
assert( pSrclist->a[pLvl->iFrom].fg.isSubquery );
158688158799
addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
158689
- VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
158800
+ pOp = sqlite3VdbeGetOp(v, addr-1);
158690158801
assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
158691158802
assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
158692158803
sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
158693158804
}
158694158805
}
@@ -158937,10 +159048,11 @@
158937159048
if( pOrigLhs ){
158938159049
sqlite3ExprListDelete(db, pOrigLhs);
158939159050
pNew->pLeft->x.pList = pLhs;
158940159051
}
158941159052
pSelect->pEList = pRhs;
159053
+ pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */
158942159054
if( pLhs && pLhs->nExpr==1 ){
158943159055
/* Take care here not to generate a TK_VECTOR containing only a
158944159056
** single value. Since the parser never creates such a vector, some
158945159057
** of the subroutines do not handle this case. */
158946159058
Expr *p = pLhs->a[0].pExpr;
@@ -164009,11 +164121,11 @@
164009164121
|| pTerm->pExpr->w.iJoin != pSrc->iCursor
164010164122
){
164011164123
return 0;
164012164124
}
164013164125
if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
164014
- && ExprHasProperty(pTerm->pExpr, EP_InnerON)
164126
+ && NEVER(ExprHasProperty(pTerm->pExpr, EP_InnerON))
164015164127
){
164016164128
return 0;
164017164129
}
164018164130
return 1;
164019164131
}
@@ -165502,11 +165614,11 @@
165502165614
return rc;
165503165615
}
165504165616
#endif /* SQLITE_ENABLE_STAT4 */
165505165617
165506165618
165507
-#ifdef WHERETRACE_ENABLED
165619
+#if defined(WHERETRACE_ENABLED) || defined(SQLITE_DEBUG)
165508165620
/*
165509165621
** Print the content of a WhereTerm object
165510165622
*/
165511165623
SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
165512165624
if( pTerm==0 ){
@@ -165545,10 +165657,13 @@
165545165657
sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
165546165658
}
165547165659
sqlite3DebugPrintf("\n");
165548165660
sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
165549165661
}
165662
+}
165663
+SQLITE_PRIVATE void sqlite3ShowWhereTerm(WhereTerm *pTerm){
165664
+ sqlite3WhereTermPrint(pTerm, 0);
165550165665
}
165551165666
#endif
165552165667
165553165668
#ifdef WHERETRACE_ENABLED
165554165669
/*
@@ -166731,11 +166846,10 @@
166731166846
pParse = pWC->pWInfo->pParse;
166732166847
while( pWhere->op==TK_AND ){
166733166848
if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
166734166849
pWhere = pWhere->pRight;
166735166850
}
166736
- if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
166737166851
for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
166738166852
Expr *pExpr;
166739166853
pExpr = pTerm->pExpr;
166740166854
if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
166741166855
&& ((jointype & JT_OUTER)==0 || ExprHasProperty(pExpr, EP_OuterON))
@@ -169392,11 +169506,11 @@
169392169506
break;
169393169507
}
169394169508
}
169395169509
if( hasRightJoin
169396169510
&& ExprHasProperty(pTerm->pExpr, EP_InnerON)
169397
- && pTerm->pExpr->w.iJoin==pItem->iCursor
169511
+ && NEVER(pTerm->pExpr->w.iJoin==pItem->iCursor)
169398169512
){
169399169513
break; /* restriction (5) */
169400169514
}
169401169515
}
169402169516
if( pTerm<pEnd ) continue;
@@ -170312,10 +170426,11 @@
170312170426
int pc,
170313170427
VdbeOp *pOp
170314170428
){
170315170429
if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
170316170430
sqlite3VdbePrintOp(0, pc, pOp);
170431
+ sqlite3ShowWhereTerm(0); /* So compiler won't complain about unused func */
170317170432
}
170318170433
#endif
170319170434
170320170435
/*
170321170436
** Generate the end of the WHERE loop. See comments on
@@ -170611,18 +170726,32 @@
170611170726
x = sqlite3TableColumnToIndex(pIdx, x);
170612170727
if( x>=0 ){
170613170728
pOp->p2 = x;
170614170729
pOp->p1 = pLevel->iIdxCur;
170615170730
OpcodeRewriteTrace(db, k, pOp);
170616
- }else{
170617
- /* Unable to translate the table reference into an index
170618
- ** reference. Verify that this is harmless - that the
170619
- ** table being referenced really is open.
170620
- */
170731
+ }else if( pLoop->wsFlags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){
170621170732
if( pLoop->wsFlags & WHERE_IDX_ONLY ){
170733
+ /* An error. pLoop is supposed to be a covering index loop,
170734
+ ** and yet the VM code refers to a column of the table that
170735
+ ** is not part of the index. */
170622170736
sqlite3ErrorMsg(pParse, "internal query planner error");
170623170737
pParse->rc = SQLITE_INTERNAL;
170738
+ }else{
170739
+ /* The WHERE_EXPRIDX flag is set by the planner when it is likely
170740
+ ** that pLoop is a covering index loop, but it is not possible
170741
+ ** to be 100% sure. In this case, any OP_Explain opcode
170742
+ ** corresponding to this loop describes the index as a "COVERING
170743
+ ** INDEX". But, pOp proves that pLoop is not actually a covering
170744
+ ** index loop. So clear the WHERE_EXPRIDX flag and rewrite the
170745
+ ** text that accompanies the OP_Explain opcode, if any. */
170746
+ pLoop->wsFlags &= ~WHERE_EXPRIDX;
170747
+ sqlite3WhereAddExplainText(pParse,
170748
+ pLevel->addrBody-1,
170749
+ pTabList,
170750
+ pLevel,
170751
+ pWInfo->wctrlFlags
170752
+ );
170624170753
}
170625170754
}
170626170755
}else if( pOp->opcode==OP_Rowid ){
170627170756
pOp->p1 = pLevel->iIdxCur;
170628170757
pOp->opcode = OP_IdxRowid;
@@ -172326,10 +172455,11 @@
172326172455
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172327172456
FuncDef *pFunc = pWin->pWFunc;
172328172457
int regArg;
172329172458
int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
172330172459
int i;
172460
+ int addrIf = 0;
172331172461
172332172462
assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
172333172463
172334172464
/* All OVER clauses in the same window function aggregate step must
172335172465
** be the same. */
@@ -172341,10 +172471,22 @@
172341172471
}else{
172342172472
sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
172343172473
}
172344172474
}
172345172475
regArg = reg;
172476
+
172477
+ if( pWin->pFilter ){
172478
+ int regTmp;
172479
+ assert( ExprUseXList(pWin->pOwner) );
172480
+ assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
172481
+ assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
172482
+ regTmp = sqlite3GetTempReg(pParse);
172483
+ sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
172484
+ addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
172485
+ VdbeCoverage(v);
172486
+ sqlite3ReleaseTempReg(pParse, regTmp);
172487
+ }
172346172488
172347172489
if( pMWin->regStartRowid==0
172348172490
&& (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
172349172491
&& (pWin->eStart!=TK_UNBOUNDED)
172350172492
){
@@ -172361,29 +172503,17 @@
172361172503
sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
172362172504
sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
172363172505
}
172364172506
sqlite3VdbeJumpHere(v, addrIsNull);
172365172507
}else if( pWin->regApp ){
172508
+ assert( pWin->pFilter==0 );
172366172509
assert( pFunc->zName==nth_valueName
172367172510
|| pFunc->zName==first_valueName
172368172511
);
172369172512
assert( bInverse==0 || bInverse==1 );
172370172513
sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
172371172514
}else if( pFunc->xSFunc!=noopStepFunc ){
172372
- int addrIf = 0;
172373
- if( pWin->pFilter ){
172374
- int regTmp;
172375
- assert( ExprUseXList(pWin->pOwner) );
172376
- assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
172377
- assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
172378
- regTmp = sqlite3GetTempReg(pParse);
172379
- sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
172380
- addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
172381
- VdbeCoverage(v);
172382
- sqlite3ReleaseTempReg(pParse, regTmp);
172383
- }
172384
-
172385172515
if( pWin->bExprArgs ){
172386172516
int iOp = sqlite3VdbeCurrentAddr(v);
172387172517
int iEnd;
172388172518
172389172519
assert( ExprUseXList(pWin->pOwner) );
@@ -172406,16 +172536,17 @@
172406172536
sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
172407172537
}
172408172538
sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
172409172539
bInverse, regArg, pWin->regAccum);
172410172540
sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
172411
- sqlite3VdbeChangeP5(v, (u8)nArg);
172541
+ sqlite3VdbeChangeP5(v, (u16)nArg);
172412172542
if( pWin->bExprArgs ){
172413172543
sqlite3ReleaseTempRange(pParse, regArg, nArg);
172414172544
}
172415
- if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
172416172545
}
172546
+
172547
+ if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
172417172548
}
172418172549
}
172419172550
172420172551
/*
172421172552
** Values that may be passed as the second argument to windowCodeOp().
@@ -173837,10 +173968,17 @@
173837173968
** Then the "b" IdList records the list "a,b,c".
173838173969
*/
173839173970
struct TrigEvent { int a; IdList * b; };
173840173971
173841173972
struct FrameBound { int eType; Expr *pExpr; };
173973
+
173974
+/*
173975
+** Generate a syntax error
173976
+*/
173977
+static void parserSyntaxError(Parse *pParse, Token *p){
173978
+ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p);
173979
+}
173842173980
173843173981
/*
173844173982
** Disable lookaside memory allocation for objects that might be
173845173983
** shared across database connections.
173846173984
*/
@@ -177730,11 +177868,15 @@
177730177868
}
177731177869
break;
177732177870
case 84: /* cmd ::= select */
177733177871
{
177734177872
SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
177735
- sqlite3Select(pParse, yymsp[0].minor.yy555, &dest);
177873
+ if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0
177874
+ || sqlite3ReadSchema(pParse)==SQLITE_OK
177875
+ ){
177876
+ sqlite3Select(pParse, yymsp[0].minor.yy555, &dest);
177877
+ }
177736177878
sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy555);
177737177879
}
177738177880
break;
177739177881
case 85: /* select ::= WITH wqlist selectnowith */
177740177882
{yymsp[-2].minor.yy555 = attachWithToSelect(pParse,yymsp[0].minor.yy555,yymsp[-1].minor.yy59);}
@@ -178201,11 +178343,11 @@
178201178343
** that look like this: #1 #2 ... These terms refer to registers
178202178344
** in the virtual machine. #N is the N-th register. */
178203178345
Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
178204178346
assert( t.n>=2 );
178205178347
if( pParse->nested==0 ){
178206
- sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
178348
+ parserSyntaxError(pParse, &t);
178207178349
yymsp[0].minor.yy454 = 0;
178208178350
}else{
178209178351
yymsp[0].minor.yy454 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
178210178352
if( yymsp[0].minor.yy454 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy454->iTable);
178211178353
}
@@ -179049,11 +179191,11 @@
179049179191
#define TOKEN yyminor
179050179192
/************ Begin %syntax_error code ****************************************/
179051179193
179052179194
UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
179053179195
if( TOKEN.z[0] ){
179054
- sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
179196
+ parserSyntaxError(pParse, &TOKEN);
179055179197
}else{
179056179198
sqlite3ErrorMsg(pParse, "incomplete input");
179057179199
}
179058179200
/************ End %syntax_error code ******************************************/
179059179201
sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
@@ -180540,11 +180682,13 @@
180540180682
}
180541180683
if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
180542180684
if( pParse->zErrMsg==0 ){
180543180685
pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
180544180686
}
180545
- sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
180687
+ if( (pParse->prepFlags & SQLITE_PREPARE_DONT_LOG)==0 ){
180688
+ sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
180689
+ }
180546180690
nErr++;
180547180691
}
180548180692
pParse->zTail = zSql;
180549180693
#ifndef SQLITE_OMIT_VIRTUALTABLE
180550180694
sqlite3_free(pParse->apVtabLock);
@@ -182513,14 +182657,10 @@
182513182657
#endif
182514182658
182515182659
sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
182516182660
sqlite3ValueFree(db->pErr);
182517182661
sqlite3CloseExtensions(db);
182518
-#if SQLITE_USER_AUTHENTICATION
182519
- sqlite3_free(db->auth.zAuthUser);
182520
- sqlite3_free(db->auth.zAuthPW);
182521
-#endif
182522182662
182523182663
db->eOpenState = SQLITE_STATE_ERROR;
182524182664
182525182665
/* The temp-database schema is allocated differently from the other schema
182526182666
** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
@@ -183951,12 +184091,12 @@
183951184091
# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
183952184092
#endif
183953184093
#if SQLITE_MAX_VDBE_OP<40
183954184094
# error SQLITE_MAX_VDBE_OP must be at least 40
183955184095
#endif
183956
-#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
183957
-# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
184096
+#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>32767
184097
+# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 32767
183958184098
#endif
183959184099
#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
183960184100
# error SQLITE_MAX_ATTACHED must be between 0 and 125
183961184101
#endif
183962184102
#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
@@ -184019,12 +184159,12 @@
184019184159
}
184020184160
oldLimit = db->aLimit[limitId];
184021184161
if( newLimit>=0 ){ /* IMP: R-52476-28732 */
184022184162
if( newLimit>aHardLimit[limitId] ){
184023184163
newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
184024
- }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH ){
184025
- newLimit = 1;
184164
+ }else if( newLimit<SQLITE_MIN_LENGTH && limitId==SQLITE_LIMIT_LENGTH ){
184165
+ newLimit = SQLITE_MIN_LENGTH;
184026184166
}
184027184167
db->aLimit[limitId] = newLimit;
184028184168
}
184029184169
return oldLimit; /* IMP: R-53341-35419 */
184030184170
}
@@ -185364,11 +185504,10 @@
185364185504
rc = x;
185365185505
#if defined(SQLITE_DEBUG)
185366185506
/* Invoke these debugging routines so that the compiler does not
185367185507
** issue "defined but not used" warnings. */
185368185508
if( x==9999 ){
185369
- sqlite3ShowExpr(0);
185370185509
sqlite3ShowExpr(0);
185371185510
sqlite3ShowExprList(0);
185372185511
sqlite3ShowIdList(0);
185373185512
sqlite3ShowSrcList(0);
185374185513
sqlite3ShowWith(0);
@@ -189796,14 +189935,19 @@
189796189935
189797189936
assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
189798189937
if( *p1==POS_COLUMN ){
189799189938
p1++;
189800189939
p1 += fts3GetVarint32(p1, &iCol1);
189940
+ /* iCol1==0 indicates corruption. Column 0 does not have a POS_COLUMN
189941
+ ** entry, so this is actually end-of-doclist. */
189942
+ if( iCol1==0 ) return 0;
189801189943
}
189802189944
if( *p2==POS_COLUMN ){
189803189945
p2++;
189804189946
p2 += fts3GetVarint32(p2, &iCol2);
189947
+ /* As above, iCol2==0 indicates corruption. */
189948
+ if( iCol2==0 ) return 0;
189805189949
}
189806189950
189807189951
while( 1 ){
189808189952
if( iCol1==iCol2 ){
189809189953
char *pSave = p;
@@ -192970,11 +193114,11 @@
192970193114
for(p=pExpr; p->pLeft; p=p->pLeft){
192971193115
assert( p->pRight->pPhrase->doclist.nList>0 );
192972193116
nTmp += p->pRight->pPhrase->doclist.nList;
192973193117
}
192974193118
nTmp += p->pPhrase->doclist.nList;
192975
- aTmp = sqlite3_malloc64(nTmp*2);
193119
+ aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX);
192976193120
if( !aTmp ){
192977193121
*pRc = SQLITE_NOMEM;
192978193122
res = 0;
192979193123
}else{
192980193124
char *aPoslist = p->pPhrase->doclist.pList;
@@ -193621,11 +193765,11 @@
193621193765
SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
193622193766
return SQLITE_CORRUPT_VTAB;
193623193767
}
193624193768
#endif
193625193769
193626
-#if !SQLITE_CORE
193770
+#if !defined(SQLITE_CORE)
193627193771
/*
193628193772
** Initialize API pointer table, if required.
193629193773
*/
193630193774
#ifdef _WIN32
193631193775
__declspec(dllexport)
@@ -194523,14 +194667,15 @@
194523194667
rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
194524194668
if( rc==SQLITE_OK ){
194525194669
Fts3PhraseToken *pToken;
194526194670
194527194671
p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
194528
- if( !p ) goto no_mem;
194529
-
194530194672
zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
194531
- if( !zTemp ) goto no_mem;
194673
+ if( !zTemp || !p ){
194674
+ rc = SQLITE_NOMEM;
194675
+ goto getnextstring_out;
194676
+ }
194532194677
194533194678
assert( nToken==ii );
194534194679
pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
194535194680
memset(pToken, 0, sizeof(Fts3PhraseToken));
194536194681
@@ -194541,53 +194686,51 @@
194541194686
pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
194542194687
pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
194543194688
nToken = ii+1;
194544194689
}
194545194690
}
194546
-
194547
- pModule->xClose(pCursor);
194548
- pCursor = 0;
194549194691
}
194550194692
194551194693
if( rc==SQLITE_DONE ){
194552194694
int jj;
194553194695
char *zBuf = 0;
194554194696
194555194697
p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
194556
- if( !p ) goto no_mem;
194698
+ if( !p ){
194699
+ rc = SQLITE_NOMEM;
194700
+ goto getnextstring_out;
194701
+ }
194557194702
memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
194558194703
p->eType = FTSQUERY_PHRASE;
194559194704
p->pPhrase = (Fts3Phrase *)&p[1];
194560194705
p->pPhrase->iColumn = pParse->iDefaultCol;
194561194706
p->pPhrase->nToken = nToken;
194562194707
194563194708
zBuf = (char *)&p->pPhrase->aToken[nToken];
194709
+ assert( nTemp==0 || zTemp );
194564194710
if( zTemp ){
194565194711
memcpy(zBuf, zTemp, nTemp);
194566
- sqlite3_free(zTemp);
194567
- }else{
194568
- assert( nTemp==0 );
194569194712
}
194570194713
194571194714
for(jj=0; jj<p->pPhrase->nToken; jj++){
194572194715
p->pPhrase->aToken[jj].z = zBuf;
194573194716
zBuf += p->pPhrase->aToken[jj].n;
194574194717
}
194575194718
rc = SQLITE_OK;
194576194719
}
194577194720
194578
- *ppExpr = p;
194579
- return rc;
194580
-no_mem:
194581
-
194721
+ getnextstring_out:
194582194722
if( pCursor ){
194583194723
pModule->xClose(pCursor);
194584194724
}
194585194725
sqlite3_free(zTemp);
194586
- sqlite3_free(p);
194587
- *ppExpr = 0;
194588
- return SQLITE_NOMEM;
194726
+ if( rc!=SQLITE_OK ){
194727
+ sqlite3_free(p);
194728
+ p = 0;
194729
+ }
194730
+ *ppExpr = p;
194731
+ return rc;
194589194732
}
194590194733
194591194734
/*
194592194735
** The output variable *ppExpr is populated with an allocated Fts3Expr
194593194736
** structure, or set to 0 if the end of the input buffer is reached.
@@ -215395,12 +215538,12 @@
215395215538
#endif
215396215539
}
215397215540
sqlite3_str_append(pOut, "}", 1);
215398215541
}
215399215542
errCode = sqlite3_str_errcode(pOut);
215400
- sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
215401215543
sqlite3_result_error_code(ctx, errCode);
215544
+ sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
215402215545
}
215403215546
215404215547
/* This routine implements an SQL function that returns the "depth" parameter
215405215548
** from the front of a blob that is an r-tree node. For example:
215406215549
**
@@ -217912,11 +218055,11 @@
217912218055
return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
217913218056
(void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
217914218057
);
217915218058
}
217916218059
217917
-#if !SQLITE_CORE
218060
+#ifndef SQLITE_CORE
217918218061
#ifdef _WIN32
217919218062
__declspec(dllexport)
217920218063
#endif
217921218064
SQLITE_API int sqlite3_rtree_init(
217922218065
sqlite3 *db,
@@ -218503,11 +218646,11 @@
218503218646
}
218504218647
218505218648
return rc;
218506218649
}
218507218650
218508
-#if !SQLITE_CORE
218651
+#ifndef SQLITE_CORE
218509218652
#ifdef _WIN32
218510218653
__declspec(dllexport)
218511218654
#endif
218512218655
SQLITE_API int sqlite3_icu_init(
218513218656
sqlite3 *db,
@@ -226177,10 +226320,12 @@
226177226320
if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK && pData ){
226178226321
unsigned char *aPage = sqlite3PagerGetData(pDbPage);
226179226322
memcpy(aPage, pData, szPage);
226180226323
pTab->pgnoTrunc = 0;
226181226324
}
226325
+ }else{
226326
+ pTab->pgnoTrunc = 0;
226182226327
}
226183226328
sqlite3PagerUnref(pDbPage);
226184226329
return rc;
226185226330
226186226331
update_fail:
@@ -226210,11 +226355,15 @@
226210226355
static int dbpageSync(sqlite3_vtab *pVtab){
226211226356
DbpageTable *pTab = (DbpageTable *)pVtab;
226212226357
if( pTab->pgnoTrunc>0 ){
226213226358
Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt;
226214226359
Pager *pPager = sqlite3BtreePager(pBt);
226215
- sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc);
226360
+ sqlite3BtreeEnter(pBt);
226361
+ if( pTab->pgnoTrunc<sqlite3BtreeLastPage(pBt) ){
226362
+ sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc);
226363
+ }
226364
+ sqlite3BtreeLeave(pBt);
226216226365
}
226217226366
pTab->pgnoTrunc = 0;
226218226367
return SQLITE_OK;
226219226368
}
226220226369
@@ -232804,20 +232953,46 @@
232804232953
#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
232805232954
232806232955
/************** End of sqlite3session.c **************************************/
232807232956
/************** Begin file fts5.c ********************************************/
232808232957
232809
-
232958
+/*
232959
+** This, the "fts5.c" source file, is a composite file that is itself
232960
+** assembled from the following files:
232961
+**
232962
+** fts5.h
232963
+** fts5Int.h
232964
+** fts5parse.h <--- Generated from fts5parse.y by Lemon
232965
+** fts5parse.c <--- Generated from fts5parse.y by Lemon
232966
+** fts5_aux.c
232967
+** fts5_buffer.c
232968
+** fts5_config.c
232969
+** fts5_expr.c
232970
+** fts5_hash.c
232971
+** fts5_index.c
232972
+** fts5_main.c
232973
+** fts5_storage.c
232974
+** fts5_tokenize.c
232975
+** fts5_unicode2.c
232976
+** fts5_varint.c
232977
+** fts5_vocab.c
232978
+*/
232810232979
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
232811232980
232812232981
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
232813232982
# define NDEBUG 1
232814232983
#endif
232815232984
#if defined(NDEBUG) && defined(SQLITE_DEBUG)
232816232985
# undef NDEBUG
232817232986
#endif
232818232987
232988
+#ifdef HAVE_STDINT_H
232989
+/* #include <stdint.h> */
232990
+#endif
232991
+#ifdef HAVE_INTTYPES_H
232992
+/* #include <inttypes.h> */
232993
+#endif
232819232994
/*
232820232995
** 2014 May 31
232821232996
**
232822232997
** The author disclaims copyright to this source code. In place of
232823232998
** a legal notice, here is a blessing:
@@ -233114,17 +233289,32 @@
233114233289
** This is used to access token iToken of phrase hit iIdx within the
233115233290
** current row. If iIdx is less than zero or greater than or equal to the
233116233291
** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
233117233292
** output variable (*ppToken) is set to point to a buffer containing the
233118233293
** matching document token, and (*pnToken) to the size of that buffer in
233119
-** bytes. This API is not available if the specified token matches a
233120
-** prefix query term. In that case both output variables are always set
233121
-** to 0.
233294
+** bytes.
233122233295
**
233123233296
** The output text is not a copy of the document text that was tokenized.
233124233297
** It is the output of the tokenizer module. For tokendata=1 tables, this
233125233298
** includes any embedded 0x00 and trailing data.
233299
+**
233300
+** This API may be slow in some cases if the token identified by parameters
233301
+** iIdx and iToken matched a prefix token in the query. In most cases, the
233302
+** first call to this API for each prefix token in the query is forced
233303
+** to scan the portion of the full-text index that matches the prefix
233304
+** token to collect the extra data required by this API. If the prefix
233305
+** token matches a large number of token instances in the document set,
233306
+** this may be a performance problem.
233307
+**
233308
+** If the user knows in advance that a query may use this API for a
233309
+** prefix token, FTS5 may be configured to collect all required data as part
233310
+** of the initial querying of the full-text index, avoiding the second scan
233311
+** entirely. This also causes prefix queries that do not use this API to
233312
+** run more slowly and use more memory. FTS5 may be configured in this way
233313
+** either on a per-table basis using the [FTS5 insttoken | 'insttoken']
233314
+** option, or on a per-query basis using the
233315
+** [fts5_insttoken | fts5_insttoken()] user function.
233126233316
**
233127233317
** This API can be quite slow if used with an FTS5 table created with the
233128233318
** "detail=none" or "detail=column" option.
233129233319
**
233130233320
** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
@@ -233803,11 +233993,12 @@
233803233993
int nUsermerge; /* 'usermerge' setting */
233804233994
int nHashSize; /* Bytes of memory for in-memory hash */
233805233995
char *zRank; /* Name of rank function */
233806233996
char *zRankArgs; /* Arguments to rank function */
233807233997
int bSecureDelete; /* 'secure-delete' */
233808
- int nDeleteMerge; /* 'deletemerge' */
233998
+ int nDeleteMerge; /* 'deletemerge' */
233999
+ int bPrefixInsttoken; /* 'prefix-insttoken' */
233809234000
233810234001
/* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
233811234002
char **pzErrmsg;
233812234003
233813234004
#ifdef SQLITE_DEBUG
@@ -234060,11 +234251,18 @@
234060234251
static int sqlite3Fts5StructureTest(Fts5Index*, void*);
234061234252
234062234253
/*
234063234254
** Used by xInstToken():
234064234255
*/
234065
-static int sqlite3Fts5IterToken(Fts5IndexIter*, i64, int, int, const char**, int*);
234256
+static int sqlite3Fts5IterToken(
234257
+ Fts5IndexIter *pIndexIter,
234258
+ const char *pToken, int nToken,
234259
+ i64 iRowid,
234260
+ int iCol,
234261
+ int iOff,
234262
+ const char **ppOut, int *pnOut
234263
+);
234066234264
234067234265
/*
234068234266
** Insert or remove data to or from the index. Each time a document is
234069234267
** added to or removed from the index, this function is called one or more
234070234268
** times.
@@ -238274,10 +238472,23 @@
238274238472
if( bVal<0 ){
238275238473
*pbBadkey = 1;
238276238474
}else{
238277238475
pConfig->bSecureDelete = (bVal ? 1 : 0);
238278238476
}
238477
+ }
238478
+
238479
+ else if( 0==sqlite3_stricmp(zKey, "insttoken") ){
238480
+ int bVal = -1;
238481
+ if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
238482
+ bVal = sqlite3_value_int(pVal);
238483
+ }
238484
+ if( bVal<0 ){
238485
+ *pbBadkey = 1;
238486
+ }else{
238487
+ pConfig->bPrefixInsttoken = (bVal ? 1 : 0);
238488
+ }
238489
+
238279238490
}else{
238280238491
*pbBadkey = 1;
238281238492
}
238282238493
return rc;
238283238494
}
@@ -241409,11 +241620,11 @@
241409241620
&& memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
241410241621
){
241411241622
int rc = sqlite3Fts5PoslistWriterAppend(
241412241623
&pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
241413241624
);
241414
- if( rc==SQLITE_OK && pExpr->pConfig->bTokendata && !pT->bPrefix ){
241625
+ if( rc==SQLITE_OK && (pExpr->pConfig->bTokendata || pT->bPrefix) ){
241415241626
int iCol = p->iOff>>32;
241416241627
int iTokOff = p->iOff & 0x7FFFFFFF;
241417241628
rc = sqlite3Fts5IndexIterWriteTokendata(
241418241629
pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
241419241630
);
@@ -241602,19 +241813,18 @@
241602241813
pPhrase = pExpr->apExprPhrase[iPhrase];
241603241814
if( iToken<0 || iToken>=pPhrase->nTerm ){
241604241815
return SQLITE_RANGE;
241605241816
}
241606241817
pTerm = &pPhrase->aTerm[iToken];
241607
- if( pTerm->bPrefix==0 ){
241608
- if( pExpr->pConfig->bTokendata ){
241609
- rc = sqlite3Fts5IterToken(
241610
- pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut
241611
- );
241612
- }else{
241613
- *ppOut = pTerm->pTerm;
241614
- *pnOut = pTerm->nFullTerm;
241615
- }
241818
+ if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){
241819
+ rc = sqlite3Fts5IterToken(
241820
+ pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm,
241821
+ iRowid, iCol, iOff+iToken, ppOut, pnOut
241822
+ );
241823
+ }else{
241824
+ *ppOut = pTerm->pTerm;
241825
+ *pnOut = pTerm->nFullTerm;
241616241826
}
241617241827
return rc;
241618241828
}
241619241829
241620241830
/*
@@ -248425,10 +248635,387 @@
248425248635
fts5BufferFree(&tmp);
248426248636
memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING);
248427248637
*p1 = out;
248428248638
}
248429248639
248640
+
248641
+/*
248642
+** Iterate through a range of entries in the FTS index, invoking the xVisit
248643
+** callback for each of them.
248644
+**
248645
+** Parameter pToken points to an nToken buffer containing an FTS index term
248646
+** (i.e. a document term with the preceding 1 byte index identifier -
248647
+** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits
248648
+** all entries for terms that have pToken/nToken as a prefix. If bPrefix
248649
+** is false, then only entries with pToken/nToken as the entire key are
248650
+** visited.
248651
+**
248652
+** If the current table is a tokendata=1 table, then if bPrefix is true then
248653
+** each index term is treated separately. However, if bPrefix is false, then
248654
+** all index terms corresponding to pToken/nToken are collapsed into a single
248655
+** term before the callback is invoked.
248656
+**
248657
+** The callback invoked for each entry visited is specified by paramter xVisit.
248658
+** Each time it is invoked, it is passed a pointer to the Fts5Index object,
248659
+** a copy of the 7th paramter to this function (pCtx) and a pointer to the
248660
+** iterator that indicates the current entry. If the current entry is the
248661
+** first with a new term (i.e. different from that of the previous entry,
248662
+** including the very first term), then the final two parameters are passed
248663
+** a pointer to the term and its size in bytes, respectively. If the current
248664
+** entry is not the first associated with its term, these two parameters
248665
+** are passed 0.
248666
+**
248667
+** If parameter pColset is not NULL, then it is used to filter entries before
248668
+** the callback is invoked.
248669
+*/
248670
+static int fts5VisitEntries(
248671
+ Fts5Index *p, /* Fts5 index object */
248672
+ Fts5Colset *pColset, /* Columns filter to apply, or NULL */
248673
+ u8 *pToken, /* Buffer containing token */
248674
+ int nToken, /* Size of buffer pToken in bytes */
248675
+ int bPrefix, /* True for a prefix scan */
248676
+ void (*xVisit)(Fts5Index*, void *pCtx, Fts5Iter *pIter, const u8*, int),
248677
+ void *pCtx /* Passed as second argument to xVisit() */
248678
+){
248679
+ const int flags = (bPrefix ? FTS5INDEX_QUERY_SCAN : 0)
248680
+ | FTS5INDEX_QUERY_SKIPEMPTY
248681
+ | FTS5INDEX_QUERY_NOOUTPUT;
248682
+ Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
248683
+ int bNewTerm = 1;
248684
+ Fts5Structure *pStruct = fts5StructureRead(p);
248685
+
248686
+ fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
248687
+ fts5IterSetOutputCb(&p->rc, p1);
248688
+ for( /* no-op */ ;
248689
+ fts5MultiIterEof(p, p1)==0;
248690
+ fts5MultiIterNext2(p, p1, &bNewTerm)
248691
+ ){
248692
+ Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
248693
+ int nNew = 0;
248694
+ const u8 *pNew = 0;
248695
+
248696
+ p1->xSetOutputs(p1, pSeg);
248697
+ if( p->rc ) break;
248698
+
248699
+ if( bNewTerm ){
248700
+ nNew = pSeg->term.n;
248701
+ pNew = pSeg->term.p;
248702
+ if( nNew<nToken || memcmp(pToken, pNew, nToken) ) break;
248703
+ }
248704
+
248705
+ xVisit(p, pCtx, p1, pNew, nNew);
248706
+ }
248707
+ fts5MultiIterFree(p1);
248708
+
248709
+ fts5StructureRelease(pStruct);
248710
+ return p->rc;
248711
+}
248712
+
248713
+
248714
+/*
248715
+** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an
248716
+** array of these for each row it visits (so all iRowid fields are the same).
248717
+** Or, for an iterator used by an "ORDER BY rank" query, it accumulates an
248718
+** array of these for the entire query (in which case iRowid fields may take
248719
+** a variety of values).
248720
+**
248721
+** Each instance in the array indicates the iterator (and therefore term)
248722
+** associated with position iPos of rowid iRowid. This is used by the
248723
+** xInstToken() API.
248724
+**
248725
+** iRowid:
248726
+** Rowid for the current entry.
248727
+**
248728
+** iPos:
248729
+** Position of current entry within row. In the usual ((iCol<<32)+iOff)
248730
+** format (e.g. see macros FTS5_POS2COLUMN() and FTS5_POS2OFFSET()).
248731
+**
248732
+** iIter:
248733
+** If the Fts5TokenDataIter iterator that the entry is part of is
248734
+** actually an iterator (i.e. with nIter>0, not just a container for
248735
+** Fts5TokenDataMap structures), then this variable is an index into
248736
+** the apIter[] array. The corresponding term is that which the iterator
248737
+** at apIter[iIter] currently points to.
248738
+**
248739
+** Or, if the Fts5TokenDataIter iterator is just a container object
248740
+** (nIter==0), then iIter is an index into the term.p[] buffer where
248741
+** the term is stored.
248742
+**
248743
+** nByte:
248744
+** In the case where iIter is an index into term.p[], this variable
248745
+** is the size of the term in bytes. If iIter is an index into apIter[],
248746
+** this variable is unused.
248747
+*/
248748
+struct Fts5TokenDataMap {
248749
+ i64 iRowid; /* Row this token is located in */
248750
+ i64 iPos; /* Position of token */
248751
+ int iIter; /* Iterator token was read from */
248752
+ int nByte; /* Length of token in bytes (or 0) */
248753
+};
248754
+
248755
+/*
248756
+** An object used to supplement Fts5Iter for tokendata=1 iterators.
248757
+**
248758
+** This object serves two purposes. The first is as a container for an array
248759
+** of Fts5TokenDataMap structures, which are used to find the token required
248760
+** when the xInstToken() API is used. This is done by the nMapAlloc, nMap and
248761
+** aMap[] variables.
248762
+*/
248763
+struct Fts5TokenDataIter {
248764
+ int nMapAlloc; /* Allocated size of aMap[] in entries */
248765
+ int nMap; /* Number of valid entries in aMap[] */
248766
+ Fts5TokenDataMap *aMap; /* Array of (rowid+pos -> token) mappings */
248767
+
248768
+ /* The following are used for prefix-queries only. */
248769
+ Fts5Buffer terms;
248770
+
248771
+ /* The following are used for other full-token tokendata queries only. */
248772
+ int nIter;
248773
+ int nIterAlloc;
248774
+ Fts5PoslistReader *aPoslistReader;
248775
+ int *aPoslistToIter;
248776
+ Fts5Iter *apIter[1];
248777
+};
248778
+
248779
+/*
248780
+** The two input arrays - a1[] and a2[] - are in sorted order. This function
248781
+** merges the two arrays together and writes the result to output array
248782
+** aOut[]. aOut[] is guaranteed to be large enough to hold the result.
248783
+**
248784
+** Duplicate entries are copied into the output. So the size of the output
248785
+** array is always (n1+n2) entries.
248786
+*/
248787
+static void fts5TokendataMerge(
248788
+ Fts5TokenDataMap *a1, int n1, /* Input array 1 */
248789
+ Fts5TokenDataMap *a2, int n2, /* Input array 2 */
248790
+ Fts5TokenDataMap *aOut /* Output array */
248791
+){
248792
+ int i1 = 0;
248793
+ int i2 = 0;
248794
+
248795
+ assert( n1>=0 && n2>=0 );
248796
+ while( i1<n1 || i2<n2 ){
248797
+ Fts5TokenDataMap *pOut = &aOut[i1+i2];
248798
+ if( i2>=n2 || (i1<n1 && (
248799
+ a1[i1].iRowid<a2[i2].iRowid
248800
+ || (a1[i1].iRowid==a2[i2].iRowid && a1[i1].iPos<=a2[i2].iPos)
248801
+ ))){
248802
+ memcpy(pOut, &a1[i1], sizeof(Fts5TokenDataMap));
248803
+ i1++;
248804
+ }else{
248805
+ memcpy(pOut, &a2[i2], sizeof(Fts5TokenDataMap));
248806
+ i2++;
248807
+ }
248808
+ }
248809
+}
248810
+
248811
+
248812
+/*
248813
+** Append a mapping to the token-map belonging to object pT.
248814
+*/
248815
+static void fts5TokendataIterAppendMap(
248816
+ Fts5Index *p,
248817
+ Fts5TokenDataIter *pT,
248818
+ int iIter,
248819
+ int nByte,
248820
+ i64 iRowid,
248821
+ i64 iPos
248822
+){
248823
+ if( p->rc==SQLITE_OK ){
248824
+ if( pT->nMap==pT->nMapAlloc ){
248825
+ int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64;
248826
+ int nAlloc = nNew * sizeof(Fts5TokenDataMap);
248827
+ Fts5TokenDataMap *aNew;
248828
+
248829
+ aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nAlloc);
248830
+ if( aNew==0 ){
248831
+ p->rc = SQLITE_NOMEM;
248832
+ return;
248833
+ }
248834
+
248835
+ pT->aMap = aNew;
248836
+ pT->nMapAlloc = nNew;
248837
+ }
248838
+
248839
+ pT->aMap[pT->nMap].iRowid = iRowid;
248840
+ pT->aMap[pT->nMap].iPos = iPos;
248841
+ pT->aMap[pT->nMap].iIter = iIter;
248842
+ pT->aMap[pT->nMap].nByte = nByte;
248843
+ pT->nMap++;
248844
+ }
248845
+}
248846
+
248847
+/*
248848
+** Sort the contents of the pT->aMap[] array.
248849
+**
248850
+** The sorting algorithm requries a malloc(). If this fails, an error code
248851
+** is left in Fts5Index.rc before returning.
248852
+*/
248853
+static void fts5TokendataIterSortMap(Fts5Index *p, Fts5TokenDataIter *pT){
248854
+ Fts5TokenDataMap *aTmp = 0;
248855
+ int nByte = pT->nMap * sizeof(Fts5TokenDataMap);
248856
+
248857
+ aTmp = (Fts5TokenDataMap*)sqlite3Fts5MallocZero(&p->rc, nByte);
248858
+ if( aTmp ){
248859
+ Fts5TokenDataMap *a1 = pT->aMap;
248860
+ Fts5TokenDataMap *a2 = aTmp;
248861
+ i64 nHalf;
248862
+
248863
+ for(nHalf=1; nHalf<pT->nMap; nHalf=nHalf*2){
248864
+ int i1;
248865
+ for(i1=0; i1<pT->nMap; i1+=(nHalf*2)){
248866
+ int n1 = MIN(nHalf, pT->nMap-i1);
248867
+ int n2 = MIN(nHalf, pT->nMap-i1-n1);
248868
+ fts5TokendataMerge(&a1[i1], n1, &a1[i1+n1], n2, &a2[i1]);
248869
+ }
248870
+ SWAPVAL(Fts5TokenDataMap*, a1, a2);
248871
+ }
248872
+
248873
+ if( a1!=pT->aMap ){
248874
+ memcpy(pT->aMap, a1, pT->nMap*sizeof(Fts5TokenDataMap));
248875
+ }
248876
+ sqlite3_free(aTmp);
248877
+
248878
+#ifdef SQLITE_DEBUG
248879
+ {
248880
+ int ii;
248881
+ for(ii=1; ii<pT->nMap; ii++){
248882
+ Fts5TokenDataMap *p1 = &pT->aMap[ii-1];
248883
+ Fts5TokenDataMap *p2 = &pT->aMap[ii];
248884
+ assert( p1->iRowid<p2->iRowid
248885
+ || (p1->iRowid==p2->iRowid && p1->iPos<=p2->iPos)
248886
+ );
248887
+ }
248888
+ }
248889
+#endif
248890
+ }
248891
+}
248892
+
248893
+/*
248894
+** Delete an Fts5TokenDataIter structure and its contents.
248895
+*/
248896
+static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){
248897
+ if( pSet ){
248898
+ int ii;
248899
+ for(ii=0; ii<pSet->nIter; ii++){
248900
+ fts5MultiIterFree(pSet->apIter[ii]);
248901
+ }
248902
+ fts5BufferFree(&pSet->terms);
248903
+ sqlite3_free(pSet->aPoslistReader);
248904
+ sqlite3_free(pSet->aMap);
248905
+ sqlite3_free(pSet);
248906
+ }
248907
+}
248908
+
248909
+
248910
+/*
248911
+** fts5VisitEntries() context object used by fts5SetupPrefixIterTokendata()
248912
+** to pass data to prefixIterSetupTokendataCb().
248913
+*/
248914
+typedef struct TokendataSetupCtx TokendataSetupCtx;
248915
+struct TokendataSetupCtx {
248916
+ Fts5TokenDataIter *pT; /* Object being populated with mappings */
248917
+ int iTermOff; /* Offset of current term in terms.p[] */
248918
+ int nTermByte; /* Size of current term in bytes */
248919
+};
248920
+
248921
+/*
248922
+** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This
248923
+** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each
248924
+** position in the current position-list. It doesn't matter that some of
248925
+** these may be out of order - they will be sorted later.
248926
+*/
248927
+static void prefixIterSetupTokendataCb(
248928
+ Fts5Index *p,
248929
+ void *pCtx,
248930
+ Fts5Iter *p1,
248931
+ const u8 *pNew,
248932
+ int nNew
248933
+){
248934
+ TokendataSetupCtx *pSetup = (TokendataSetupCtx*)pCtx;
248935
+ int iPosOff = 0;
248936
+ i64 iPos = 0;
248937
+
248938
+ if( pNew ){
248939
+ pSetup->nTermByte = nNew-1;
248940
+ pSetup->iTermOff = pSetup->pT->terms.n;
248941
+ fts5BufferAppendBlob(&p->rc, &pSetup->pT->terms, nNew-1, pNew+1);
248942
+ }
248943
+
248944
+ while( 0==sqlite3Fts5PoslistNext64(
248945
+ p1->base.pData, p1->base.nData, &iPosOff, &iPos
248946
+ ) ){
248947
+ fts5TokendataIterAppendMap(p,
248948
+ pSetup->pT, pSetup->iTermOff, pSetup->nTermByte, p1->base.iRowid, iPos
248949
+ );
248950
+ }
248951
+}
248952
+
248953
+
248954
+/*
248955
+** Context object passed by fts5SetupPrefixIter() to fts5VisitEntries().
248956
+*/
248957
+typedef struct PrefixSetupCtx PrefixSetupCtx;
248958
+struct PrefixSetupCtx {
248959
+ void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
248960
+ void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
248961
+ i64 iLastRowid;
248962
+ int nMerge;
248963
+ Fts5Buffer *aBuf;
248964
+ int nBuf;
248965
+ Fts5Buffer doclist;
248966
+ TokendataSetupCtx *pTokendata;
248967
+};
248968
+
248969
+/*
248970
+** fts5VisitEntries() callback used by fts5SetupPrefixIter()
248971
+*/
248972
+static void prefixIterSetupCb(
248973
+ Fts5Index *p,
248974
+ void *pCtx,
248975
+ Fts5Iter *p1,
248976
+ const u8 *pNew,
248977
+ int nNew
248978
+){
248979
+ PrefixSetupCtx *pSetup = (PrefixSetupCtx*)pCtx;
248980
+ const int nMerge = pSetup->nMerge;
248981
+
248982
+ if( p1->base.nData>0 ){
248983
+ if( p1->base.iRowid<=pSetup->iLastRowid && pSetup->doclist.n>0 ){
248984
+ int i;
248985
+ for(i=0; p->rc==SQLITE_OK && pSetup->doclist.n; i++){
248986
+ int i1 = i*nMerge;
248987
+ int iStore;
248988
+ assert( i1+nMerge<=pSetup->nBuf );
248989
+ for(iStore=i1; iStore<i1+nMerge; iStore++){
248990
+ if( pSetup->aBuf[iStore].n==0 ){
248991
+ fts5BufferSwap(&pSetup->doclist, &pSetup->aBuf[iStore]);
248992
+ fts5BufferZero(&pSetup->doclist);
248993
+ break;
248994
+ }
248995
+ }
248996
+ if( iStore==i1+nMerge ){
248997
+ pSetup->xMerge(p, &pSetup->doclist, nMerge, &pSetup->aBuf[i1]);
248998
+ for(iStore=i1; iStore<i1+nMerge; iStore++){
248999
+ fts5BufferZero(&pSetup->aBuf[iStore]);
249000
+ }
249001
+ }
249002
+ }
249003
+ pSetup->iLastRowid = 0;
249004
+ }
249005
+
249006
+ pSetup->xAppend(
249007
+ p, (u64)p1->base.iRowid-(u64)pSetup->iLastRowid, p1, &pSetup->doclist
249008
+ );
249009
+ pSetup->iLastRowid = p1->base.iRowid;
249010
+ }
249011
+
249012
+ if( pSetup->pTokendata ){
249013
+ prefixIterSetupTokendataCb(p, (void*)pSetup->pTokendata, p1, pNew, nNew);
249014
+ }
249015
+}
249016
+
248430249017
static void fts5SetupPrefixIter(
248431249018
Fts5Index *p, /* Index to read from */
248432249019
int bDesc, /* True for "ORDER BY rowid DESC" */
248433249020
int iIdx, /* Index to scan for data */
248434249021
u8 *pToken, /* Buffer containing prefix to match */
@@ -248435,137 +249022,91 @@
248435249022
int nToken, /* Size of buffer pToken in bytes */
248436249023
Fts5Colset *pColset, /* Restrict matches to these columns */
248437249024
Fts5Iter **ppIter /* OUT: New iterator */
248438249025
){
248439249026
Fts5Structure *pStruct;
248440
- Fts5Buffer *aBuf;
248441
- int nBuf = 32;
248442
- int nMerge = 1;
249027
+ PrefixSetupCtx s;
249028
+ TokendataSetupCtx s2;
248443249029
248444
- void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
248445
- void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
249030
+ memset(&s, 0, sizeof(s));
249031
+ memset(&s2, 0, sizeof(s2));
249032
+
249033
+ s.nMerge = 1;
249034
+ s.iLastRowid = 0;
249035
+ s.nBuf = 32;
249036
+ if( iIdx==0
249037
+ && p->pConfig->eDetail==FTS5_DETAIL_FULL
249038
+ && p->pConfig->bPrefixInsttoken
249039
+ ){
249040
+ s.pTokendata = &s2;
249041
+ s2.pT = (Fts5TokenDataIter*)fts5IdxMalloc(p, sizeof(*s2.pT));
249042
+ }
249043
+
248446249044
if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
248447
- xMerge = fts5MergeRowidLists;
248448
- xAppend = fts5AppendRowid;
249045
+ s.xMerge = fts5MergeRowidLists;
249046
+ s.xAppend = fts5AppendRowid;
248449249047
}else{
248450
- nMerge = FTS5_MERGE_NLIST-1;
248451
- nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
248452
- xMerge = fts5MergePrefixLists;
248453
- xAppend = fts5AppendPoslist;
249048
+ s.nMerge = FTS5_MERGE_NLIST-1;
249049
+ s.nBuf = s.nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
249050
+ s.xMerge = fts5MergePrefixLists;
249051
+ s.xAppend = fts5AppendPoslist;
248454249052
}
248455249053
248456
- aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
249054
+ s.aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*s.nBuf);
248457249055
pStruct = fts5StructureRead(p);
248458
- assert( p->rc!=SQLITE_OK || (aBuf && pStruct) );
249056
+ assert( p->rc!=SQLITE_OK || (s.aBuf && pStruct) );
248459249057
248460249058
if( p->rc==SQLITE_OK ){
248461
- const int flags = FTS5INDEX_QUERY_SCAN
248462
- | FTS5INDEX_QUERY_SKIPEMPTY
248463
- | FTS5INDEX_QUERY_NOOUTPUT;
249059
+ void *pCtx = (void*)&s;
248464249060
int i;
248465
- i64 iLastRowid = 0;
248466
- Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
248467249061
Fts5Data *pData;
248468
- Fts5Buffer doclist;
248469
- int bNewTerm = 1;
248470
-
248471
- memset(&doclist, 0, sizeof(doclist));
248472249062
248473249063
/* If iIdx is non-zero, then it is the number of a prefix-index for
248474249064
** prefixes 1 character longer than the prefix being queried for. That
248475249065
** index contains all the doclists required, except for the one
248476249066
** corresponding to the prefix itself. That one is extracted from the
248477249067
** main term index here. */
248478249068
if( iIdx!=0 ){
248479
- int dummy = 0;
248480
- const int f2 = FTS5INDEX_QUERY_SKIPEMPTY|FTS5INDEX_QUERY_NOOUTPUT;
248481249069
pToken[0] = FTS5_MAIN_PREFIX;
248482
- fts5MultiIterNew(p, pStruct, f2, pColset, pToken, nToken, -1, 0, &p1);
248483
- fts5IterSetOutputCb(&p->rc, p1);
248484
- for(;
248485
- fts5MultiIterEof(p, p1)==0;
248486
- fts5MultiIterNext2(p, p1, &dummy)
248487
- ){
248488
- Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
248489
- p1->xSetOutputs(p1, pSeg);
248490
- if( p1->base.nData ){
248491
- xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
248492
- iLastRowid = p1->base.iRowid;
248493
- }
248494
- }
248495
- fts5MultiIterFree(p1);
249070
+ fts5VisitEntries(p, pColset, pToken, nToken, 0, prefixIterSetupCb, pCtx);
248496249071
}
248497249072
248498249073
pToken[0] = FTS5_MAIN_PREFIX + iIdx;
248499
- fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
248500
- fts5IterSetOutputCb(&p->rc, p1);
248501
-
248502
- for( /* no-op */ ;
248503
- fts5MultiIterEof(p, p1)==0;
248504
- fts5MultiIterNext2(p, p1, &bNewTerm)
248505
- ){
248506
- Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
248507
- int nTerm = pSeg->term.n;
248508
- const u8 *pTerm = pSeg->term.p;
248509
- p1->xSetOutputs(p1, pSeg);
248510
-
248511
- assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
248512
- if( bNewTerm ){
248513
- if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
248514
- }
248515
-
248516
- if( p1->base.nData==0 ) continue;
248517
- if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
248518
- for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
248519
- int i1 = i*nMerge;
248520
- int iStore;
248521
- assert( i1+nMerge<=nBuf );
248522
- for(iStore=i1; iStore<i1+nMerge; iStore++){
248523
- if( aBuf[iStore].n==0 ){
248524
- fts5BufferSwap(&doclist, &aBuf[iStore]);
248525
- fts5BufferZero(&doclist);
248526
- break;
248527
- }
248528
- }
248529
- if( iStore==i1+nMerge ){
248530
- xMerge(p, &doclist, nMerge, &aBuf[i1]);
248531
- for(iStore=i1; iStore<i1+nMerge; iStore++){
248532
- fts5BufferZero(&aBuf[iStore]);
248533
- }
248534
- }
248535
- }
248536
- iLastRowid = 0;
248537
- }
248538
-
248539
- xAppend(p, (u64)p1->base.iRowid-(u64)iLastRowid, p1, &doclist);
248540
- iLastRowid = p1->base.iRowid;
248541
- }
248542
-
248543
- assert( (nBuf%nMerge)==0 );
248544
- for(i=0; i<nBuf; i+=nMerge){
249074
+ fts5VisitEntries(p, pColset, pToken, nToken, 1, prefixIterSetupCb, pCtx);
249075
+
249076
+ assert( (s.nBuf%s.nMerge)==0 );
249077
+ for(i=0; i<s.nBuf; i+=s.nMerge){
248545249078
int iFree;
248546249079
if( p->rc==SQLITE_OK ){
248547
- xMerge(p, &doclist, nMerge, &aBuf[i]);
249080
+ s.xMerge(p, &s.doclist, s.nMerge, &s.aBuf[i]);
248548249081
}
248549
- for(iFree=i; iFree<i+nMerge; iFree++){
248550
- fts5BufferFree(&aBuf[iFree]);
249082
+ for(iFree=i; iFree<i+s.nMerge; iFree++){
249083
+ fts5BufferFree(&s.aBuf[iFree]);
248551249084
}
248552249085
}
248553
- fts5MultiIterFree(p1);
248554249086
248555
- pData = fts5IdxMalloc(p, sizeof(*pData)+doclist.n+FTS5_DATA_ZERO_PADDING);
249087
+ pData = fts5IdxMalloc(p, sizeof(*pData)+s.doclist.n+FTS5_DATA_ZERO_PADDING);
249088
+ assert( pData!=0 || p->rc!=SQLITE_OK );
248556249089
if( pData ){
248557249090
pData->p = (u8*)&pData[1];
248558
- pData->nn = pData->szLeaf = doclist.n;
248559
- if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
249091
+ pData->nn = pData->szLeaf = s.doclist.n;
249092
+ if( s.doclist.n ) memcpy(pData->p, s.doclist.p, s.doclist.n);
248560249093
fts5MultiIterNew2(p, pData, bDesc, ppIter);
248561249094
}
248562
- fts5BufferFree(&doclist);
249095
+
249096
+ assert( (*ppIter)!=0 || p->rc!=SQLITE_OK );
249097
+ if( p->rc==SQLITE_OK && s.pTokendata ){
249098
+ fts5TokendataIterSortMap(p, s2.pT);
249099
+ (*ppIter)->pTokenDataIter = s2.pT;
249100
+ s2.pT = 0;
249101
+ }
248563249102
}
248564249103
249104
+ fts5TokendataIterDelete(s2.pT);
249105
+ fts5BufferFree(&s.doclist);
248565249106
fts5StructureRelease(pStruct);
248566
- sqlite3_free(aBuf);
249107
+ sqlite3_free(s.aBuf);
248567249108
}
248568249109
248569249110
248570249111
/*
248571249112
** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
@@ -248815,42 +249356,10 @@
248815249356
static void fts5SegIterSetEOF(Fts5SegIter *pSeg){
248816249357
fts5DataRelease(pSeg->pLeaf);
248817249358
pSeg->pLeaf = 0;
248818249359
}
248819249360
248820
-/*
248821
-** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an
248822
-** array of these for each row it visits. Or, for an iterator used by an
248823
-** "ORDER BY rank" query, it accumulates an array of these for the entire
248824
-** query.
248825
-**
248826
-** Each instance in the array indicates the iterator (and therefore term)
248827
-** associated with position iPos of rowid iRowid. This is used by the
248828
-** xInstToken() API.
248829
-*/
248830
-struct Fts5TokenDataMap {
248831
- i64 iRowid; /* Row this token is located in */
248832
- i64 iPos; /* Position of token */
248833
- int iIter; /* Iterator token was read from */
248834
-};
248835
-
248836
-/*
248837
-** An object used to supplement Fts5Iter for tokendata=1 iterators.
248838
-*/
248839
-struct Fts5TokenDataIter {
248840
- int nIter;
248841
- int nIterAlloc;
248842
-
248843
- int nMap;
248844
- int nMapAlloc;
248845
- Fts5TokenDataMap *aMap;
248846
-
248847
- Fts5PoslistReader *aPoslistReader;
248848
- int *aPoslistToIter;
248849
- Fts5Iter *apIter[1];
248850
-};
248851
-
248852249361
/*
248853249362
** This function appends iterator pAppend to Fts5TokenDataIter pIn and
248854249363
** returns the result.
248855249364
*/
248856249365
static Fts5TokenDataIter *fts5AppendTokendataIter(
@@ -248883,58 +249392,10 @@
248883249392
assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc );
248884249393
248885249394
return pRet;
248886249395
}
248887249396
248888
-/*
248889
-** Delete an Fts5TokenDataIter structure and its contents.
248890
-*/
248891
-static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){
248892
- if( pSet ){
248893
- int ii;
248894
- for(ii=0; ii<pSet->nIter; ii++){
248895
- fts5MultiIterFree(pSet->apIter[ii]);
248896
- }
248897
- sqlite3_free(pSet->aPoslistReader);
248898
- sqlite3_free(pSet->aMap);
248899
- sqlite3_free(pSet);
248900
- }
248901
-}
248902
-
248903
-/*
248904
-** Append a mapping to the token-map belonging to object pT.
248905
-*/
248906
-static void fts5TokendataIterAppendMap(
248907
- Fts5Index *p,
248908
- Fts5TokenDataIter *pT,
248909
- int iIter,
248910
- i64 iRowid,
248911
- i64 iPos
248912
-){
248913
- if( p->rc==SQLITE_OK ){
248914
- if( pT->nMap==pT->nMapAlloc ){
248915
- int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64;
248916
- int nByte = nNew * sizeof(Fts5TokenDataMap);
248917
- Fts5TokenDataMap *aNew;
248918
-
248919
- aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nByte);
248920
- if( aNew==0 ){
248921
- p->rc = SQLITE_NOMEM;
248922
- return;
248923
- }
248924
-
248925
- pT->aMap = aNew;
248926
- pT->nMapAlloc = nNew;
248927
- }
248928
-
248929
- pT->aMap[pT->nMap].iRowid = iRowid;
248930
- pT->aMap[pT->nMap].iPos = iPos;
248931
- pT->aMap[pT->nMap].iIter = iIter;
248932
- pT->nMap++;
248933
- }
248934
-}
248935
-
248936249397
/*
248937249398
** The iterator passed as the only argument must be a tokendata=1 iterator
248938249399
** (pIter->pTokenDataIter!=0). This function sets the iterator output
248939249400
** variables (pIter->base.*) according to the contents of the current
248940249401
** row.
@@ -248971,11 +249432,11 @@
248971249432
int eDetail = pIter->pIndex->pConfig->eDetail;
248972249433
pIter->base.bEof = 0;
248973249434
pIter->base.iRowid = iRowid;
248974249435
248975249436
if( nHit==1 && eDetail==FTS5_DETAIL_FULL ){
248976
- fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, iRowid, -1);
249437
+ fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, 0, iRowid, -1);
248977249438
}else
248978249439
if( nHit>1 && eDetail!=FTS5_DETAIL_NONE ){
248979249440
int nReader = 0;
248980249441
int nByte = 0;
248981249442
i64 iPrev = 0;
@@ -249224,10 +249685,11 @@
249224249685
249225249686
if( p->rc==SQLITE_OK ){
249226249687
pRet = fts5MultiIterAlloc(p, 0);
249227249688
}
249228249689
if( pRet ){
249690
+ pRet->nSeg = 0;
249229249691
pRet->pTokenDataIter = pSet;
249230249692
if( pSet ){
249231249693
fts5IterSetOutputsTokendata(pRet);
249232249694
}else{
249233249695
pRet->base.bEof = 1;
@@ -249238,11 +249700,10 @@
249238249700
249239249701
fts5StructureRelease(pStruct);
249240249702
fts5BufferFree(&bSeek);
249241249703
return pRet;
249242249704
}
249243
-
249244249705
249245249706
/*
249246249707
** Open a new iterator to iterate though all rowid that match the
249247249708
** specified token or token prefix.
249248249709
*/
@@ -249262,12 +249723,18 @@
249262249723
249263249724
if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
249264249725
int iIdx = 0; /* Index to search */
249265249726
int iPrefixIdx = 0; /* +1 prefix index */
249266249727
int bTokendata = pConfig->bTokendata;
249728
+ assert( buf.p!=0 );
249267249729
if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken);
249268249730
249731
+ /* The NOTOKENDATA flag is set when each token in a tokendata=1 table
249732
+ ** should be treated individually, instead of merging all those with
249733
+ ** a common prefix into a single entry. This is used, for example, by
249734
+ ** queries performed as part of an integrity-check, or by the fts5vocab
249735
+ ** module. */
249269249736
if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){
249270249737
bTokendata = 0;
249271249738
}
249272249739
249273249740
/* Figure out which index to search and set iIdx accordingly. If this
@@ -249294,11 +249761,11 @@
249294249761
if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx;
249295249762
}
249296249763
}
249297249764
249298249765
if( bTokendata && iIdx==0 ){
249299
- buf.p[0] = '0';
249766
+ buf.p[0] = FTS5_MAIN_PREFIX;
249300249767
pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset);
249301249768
}else if( iIdx<=pConfig->nPrefix ){
249302249769
/* Straight index lookup */
249303249770
Fts5Structure *pStruct = fts5StructureRead(p);
249304249771
buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
@@ -249307,11 +249774,11 @@
249307249774
pColset, buf.p, nToken+1, -1, 0, &pRet
249308249775
);
249309249776
fts5StructureRelease(pStruct);
249310249777
}
249311249778
}else{
249312
- /* Scan multiple terms in the main index */
249779
+ /* Scan multiple terms in the main index for a prefix query. */
249313249780
int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
249314249781
fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet);
249315249782
if( pRet==0 ){
249316249783
assert( p->rc!=SQLITE_OK );
249317249784
}else{
@@ -249343,11 +249810,12 @@
249343249810
** Move to the next matching rowid.
249344249811
*/
249345249812
static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
249346249813
Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249347249814
assert( pIter->pIndex->rc==SQLITE_OK );
249348
- if( pIter->pTokenDataIter ){
249815
+ if( pIter->nSeg==0 ){
249816
+ assert( pIter->pTokenDataIter );
249349249817
fts5TokendataIterNext(pIter, 0, 0);
249350249818
}else{
249351249819
fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
249352249820
}
249353249821
return fts5IndexReturn(pIter->pIndex);
@@ -249380,11 +249848,12 @@
249380249848
** definition of "at or after" depends on whether this iterator iterates
249381249849
** in ascending or descending rowid order.
249382249850
*/
249383249851
static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
249384249852
Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249385
- if( pIter->pTokenDataIter ){
249853
+ if( pIter->nSeg==0 ){
249854
+ assert( pIter->pTokenDataIter );
249386249855
fts5TokendataIterNext(pIter, 1, iMatch);
249387249856
}else{
249388249857
fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
249389249858
}
249390249859
return fts5IndexReturn(pIter->pIndex);
@@ -249398,32 +249867,88 @@
249398249867
const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
249399249868
assert_nc( z || n<=1 );
249400249869
*pn = n-1;
249401249870
return (z ? &z[1] : 0);
249402249871
}
249872
+
249873
+/*
249874
+** pIter is a prefix query. This function populates pIter->pTokenDataIter
249875
+** with an Fts5TokenDataIter object containing mappings for all rows
249876
+** matched by the query.
249877
+*/
249878
+static int fts5SetupPrefixIterTokendata(
249879
+ Fts5Iter *pIter,
249880
+ const char *pToken, /* Token prefix to search for */
249881
+ int nToken /* Size of pToken in bytes */
249882
+){
249883
+ Fts5Index *p = pIter->pIndex;
249884
+ Fts5Buffer token = {0, 0, 0};
249885
+ TokendataSetupCtx ctx;
249886
+
249887
+ memset(&ctx, 0, sizeof(ctx));
249888
+
249889
+ fts5BufferGrow(&p->rc, &token, nToken+1);
249890
+ assert( token.p!=0 || p->rc!=SQLITE_OK );
249891
+ ctx.pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, sizeof(*ctx.pT));
249892
+
249893
+ if( p->rc==SQLITE_OK ){
249894
+
249895
+ /* Fill in the token prefix to search for */
249896
+ token.p[0] = FTS5_MAIN_PREFIX;
249897
+ memcpy(&token.p[1], pToken, nToken);
249898
+ token.n = nToken+1;
249899
+
249900
+ fts5VisitEntries(
249901
+ p, 0, token.p, token.n, 1, prefixIterSetupTokendataCb, (void*)&ctx
249902
+ );
249903
+
249904
+ fts5TokendataIterSortMap(p, ctx.pT);
249905
+ }
249906
+
249907
+ if( p->rc==SQLITE_OK ){
249908
+ pIter->pTokenDataIter = ctx.pT;
249909
+ }else{
249910
+ fts5TokendataIterDelete(ctx.pT);
249911
+ }
249912
+ fts5BufferFree(&token);
249913
+
249914
+ return fts5IndexReturn(p);
249915
+}
249403249916
249404249917
/*
249405249918
** This is used by xInstToken() to access the token at offset iOff, column
249406249919
** iCol of row iRowid. The token is returned via output variables *ppOut
249407249920
** and *pnOut. The iterator passed as the first argument must be a tokendata=1
249408249921
** iterator (pIter->pTokenDataIter!=0).
249922
+**
249923
+** pToken/nToken:
249409249924
*/
249410249925
static int sqlite3Fts5IterToken(
249411249926
Fts5IndexIter *pIndexIter,
249927
+ const char *pToken, int nToken,
249412249928
i64 iRowid,
249413249929
int iCol,
249414249930
int iOff,
249415249931
const char **ppOut, int *pnOut
249416249932
){
249417249933
Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249418249934
Fts5TokenDataIter *pT = pIter->pTokenDataIter;
249419
- Fts5TokenDataMap *aMap = pT->aMap;
249420249935
i64 iPos = (((i64)iCol)<<32) + iOff;
249421
-
249936
+ Fts5TokenDataMap *aMap = 0;
249422249937
int i1 = 0;
249423
- int i2 = pT->nMap;
249938
+ int i2 = 0;
249424249939
int iTest = 0;
249940
+
249941
+ assert( pT || (pToken && pIter->nSeg>0) );
249942
+ if( pT==0 ){
249943
+ int rc = fts5SetupPrefixIterTokendata(pIter, pToken, nToken);
249944
+ if( rc!=SQLITE_OK ) return rc;
249945
+ pT = pIter->pTokenDataIter;
249946
+ }
249947
+
249948
+ i2 = pT->nMap;
249949
+ aMap = pT->aMap;
249425249950
249426249951
while( i2>i1 ){
249427249952
iTest = (i1 + i2) / 2;
249428249953
249429249954
if( aMap[iTest].iRowid<iRowid ){
@@ -249443,13 +249968,19 @@
249443249968
}
249444249969
}
249445249970
}
249446249971
249447249972
if( i2>i1 ){
249448
- Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter];
249449
- *ppOut = (const char*)pMap->aSeg[0].term.p+1;
249450
- *pnOut = pMap->aSeg[0].term.n-1;
249973
+ if( pIter->nSeg==0 ){
249974
+ Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter];
249975
+ *ppOut = (const char*)pMap->aSeg[0].term.p+1;
249976
+ *pnOut = pMap->aSeg[0].term.n-1;
249977
+ }else{
249978
+ Fts5TokenDataMap *p = &aMap[iTest];
249979
+ *ppOut = (const char*)&pT->terms.p[p->iIter];
249980
+ *pnOut = aMap[iTest].nByte;
249981
+ }
249451249982
}
249452249983
249453249984
return SQLITE_OK;
249454249985
}
249455249986
@@ -249457,11 +249988,13 @@
249457249988
** Clear any existing entries from the token-map associated with the
249458249989
** iterator passed as the only argument.
249459249990
*/
249460249991
static void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){
249461249992
Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249462
- if( pIter && pIter->pTokenDataIter ){
249993
+ if( pIter && pIter->pTokenDataIter
249994
+ && (pIter->nSeg==0 || pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_FULL)
249995
+ ){
249463249996
pIter->pTokenDataIter->nMap = 0;
249464249997
}
249465249998
}
249466249999
249467250000
/*
@@ -249477,21 +250010,33 @@
249477250010
i64 iRowid, int iCol, int iOff
249478250011
){
249479250012
Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
249480250013
Fts5TokenDataIter *pT = pIter->pTokenDataIter;
249481250014
Fts5Index *p = pIter->pIndex;
249482
- int ii;
250015
+ i64 iPos = (((i64)iCol)<<32) + iOff;
249483250016
249484250017
assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL );
249485
- assert( pIter->pTokenDataIter );
249486
-
249487
- for(ii=0; ii<pT->nIter; ii++){
249488
- Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term;
249489
- if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break;
249490
- }
249491
- if( ii<pT->nIter ){
249492
- fts5TokendataIterAppendMap(p, pT, ii, iRowid, (((i64)iCol)<<32) + iOff);
250018
+ assert( pIter->pTokenDataIter || pIter->nSeg>0 );
250019
+ if( pIter->nSeg>0 ){
250020
+ /* This is a prefix term iterator. */
250021
+ if( pT==0 ){
250022
+ pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, sizeof(*pT));
250023
+ pIter->pTokenDataIter = pT;
250024
+ }
250025
+ if( pT ){
250026
+ fts5TokendataIterAppendMap(p, pT, pT->terms.n, nToken, iRowid, iPos);
250027
+ fts5BufferAppendBlob(&p->rc, &pT->terms, nToken, (const u8*)pToken);
250028
+ }
250029
+ }else{
250030
+ int ii;
250031
+ for(ii=0; ii<pT->nIter; ii++){
250032
+ Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term;
250033
+ if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break;
250034
+ }
250035
+ if( ii<pT->nIter ){
250036
+ fts5TokendataIterAppendMap(p, pT, ii, 0, iRowid, iPos);
250037
+ }
249493250038
}
249494250039
return fts5IndexReturn(p);
249495250040
}
249496250041
249497250042
/*
@@ -251392,10 +251937,11 @@
251392251937
** containing a copy of the header from an Fts5Config pointer.
251393251938
*/
251394251939
#define FTS5_LOCALE_HDR_SIZE ((int)sizeof( ((Fts5Global*)0)->aLocaleHdr ))
251395251940
#define FTS5_LOCALE_HDR(pConfig) ((const u8*)(pConfig->pGlobal->aLocaleHdr))
251396251941
251942
+#define FTS5_INSTTOKEN_SUBTYPE 73
251397251943
251398251944
/*
251399251945
** Each auxiliary function registered with the FTS5 module is represented
251400251946
** by an object of the following type. All such objects are stored as part
251401251947
** of the Fts5Global.pAux list.
@@ -251931,10 +252477,11 @@
251931252477
){
251932252478
/* A MATCH operator or equivalent */
251933252479
if( p->usable==0 || iCol<0 ){
251934252480
/* As there exists an unusable MATCH constraint this is an
251935252481
** unusable plan. Return SQLITE_CONSTRAINT. */
252482
+ idxStr[iIdxStr] = 0;
251936252483
return SQLITE_CONSTRAINT;
251937252484
}else{
251938252485
if( iCol==nCol+1 ){
251939252486
if( bSeenRank ) continue;
251940252487
idxStr[iIdxStr++] = 'r';
@@ -252716,10 +253263,11 @@
252716253263
sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
252717253264
sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
252718253265
sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
252719253266
int iCol; /* Column on LHS of MATCH operator */
252720253267
char **pzErrmsg = pConfig->pzErrmsg;
253268
+ int bPrefixInsttoken = pConfig->bPrefixInsttoken;
252721253269
int i;
252722253270
int iIdxStr = 0;
252723253271
Fts5Expr *pExpr = 0;
252724253272
252725253273
assert( pConfig->bLock==0 );
@@ -252751,10 +253299,13 @@
252751253299
int bInternal = 0;
252752253300
252753253301
rc = fts5ExtractExprText(pConfig, apVal[i], &zText, &bFreeAndReset);
252754253302
if( rc!=SQLITE_OK ) goto filter_out;
252755253303
if( zText==0 ) zText = "";
253304
+ if( sqlite3_value_subtype(apVal[i])==FTS5_INSTTOKEN_SUBTYPE ){
253305
+ pConfig->bPrefixInsttoken = 1;
253306
+ }
252756253307
252757253308
iCol = 0;
252758253309
do{
252759253310
iCol = iCol*10 + (idxStr[iIdxStr]-'0');
252760253311
iIdxStr++;
@@ -252891,10 +253442,11 @@
252891253442
}
252892253443
252893253444
filter_out:
252894253445
sqlite3Fts5ExprFree(pExpr);
252895253446
pConfig->pzErrmsg = pzErrmsg;
253447
+ pConfig->bPrefixInsttoken = bPrefixInsttoken;
252896253448
return rc;
252897253449
}
252898253450
252899253451
/*
252900253452
** This is the xEof method of the virtual table. SQLite calls this
@@ -254886,11 +255438,11 @@
254886255438
int nArg, /* Number of args */
254887255439
sqlite3_value **apUnused /* Function arguments */
254888255440
){
254889255441
assert( nArg==0 );
254890255442
UNUSED_PARAM2(nArg, apUnused);
254891
- sqlite3_result_text(pCtx, "fts5: 2024-10-21 16:30:22 03a9703e27c44437c39363d0baf82db4ebc94538a0f28411c85dda156f82636e", -1, SQLITE_TRANSIENT);
255443
+ sqlite3_result_text(pCtx, "fts5: 2024-12-30 21:23:53 2b17bc49655c577029919c2d409de994b0d252f8efb5da1ba0913f2c96bee552", -1, SQLITE_TRANSIENT);
254892255444
}
254893255445
254894255446
/*
254895255447
** Implementation of fts5_locale(LOCALE, TEXT) function.
254896255448
**
@@ -254949,10 +255501,24 @@
254949255501
assert( &pCsr[nText]==&pBlob[nBlob] );
254950255502
254951255503
sqlite3_result_blob(pCtx, pBlob, nBlob, sqlite3_free);
254952255504
}
254953255505
}
255506
+
255507
+/*
255508
+** Implementation of fts5_insttoken() function.
255509
+*/
255510
+static void fts5InsttokenFunc(
255511
+ sqlite3_context *pCtx, /* Function call context */
255512
+ int nArg, /* Number of args */
255513
+ sqlite3_value **apArg /* Function arguments */
255514
+){
255515
+ assert( nArg==1 );
255516
+ (void)nArg;
255517
+ sqlite3_result_value(pCtx, apArg[0]);
255518
+ sqlite3_result_subtype(pCtx, FTS5_INSTTOKEN_SUBTYPE);
255519
+}
254954255520
254955255521
/*
254956255522
** Return true if zName is the extension on one of the shadow tables used
254957255523
** by this module.
254958255524
*/
@@ -255079,13 +255645,20 @@
255079255645
);
255080255646
}
255081255647
if( rc==SQLITE_OK ){
255082255648
rc = sqlite3_create_function(
255083255649
db, "fts5_locale", 2,
255084
- SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE,
255650
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE|SQLITE_SUBTYPE,
255085255651
p, fts5LocaleFunc, 0, 0
255086255652
);
255653
+ }
255654
+ if( rc==SQLITE_OK ){
255655
+ rc = sqlite3_create_function(
255656
+ db, "fts5_insttoken", 1,
255657
+ SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE,
255658
+ p, fts5InsttokenFunc, 0, 0
255659
+ );
255087255660
}
255088255661
}
255089255662
255090255663
/* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
255091255664
** fts5_test_mi.c is compiled and linked into the executable. And call
@@ -258007,22 +258580,22 @@
258007258580
int rc = SQLITE_OK;
258008258581
char aBuf[32];
258009258582
char *zOut = aBuf;
258010258583
int ii;
258011258584
const unsigned char *zIn = (const unsigned char*)pText;
258012
- const unsigned char *zEof = &zIn[nText];
258013
- u32 iCode;
258585
+ const unsigned char *zEof = (zIn ? &zIn[nText] : 0);
258586
+ u32 iCode = 0;
258014258587
int aStart[3]; /* Input offset of each character in aBuf[] */
258015258588
258016258589
UNUSED_PARAM(unusedFlags);
258017258590
258018258591
/* Populate aBuf[] with the characters for the first trigram. */
258019258592
for(ii=0; ii<3; ii++){
258020258593
do {
258021258594
aStart[ii] = zIn - (const unsigned char*)pText;
258595
+ if( zIn>=zEof ) return SQLITE_OK;
258022258596
READ_UTF8(zIn, zEof, iCode);
258023
- if( iCode==0 ) return SQLITE_OK;
258024258597
if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
258025258598
}while( iCode==0 );
258026258599
WRITE_UTF8(zOut, iCode);
258027258600
}
258028258601
@@ -258039,12 +258612,15 @@
258039258612
const char *z1;
258040258613
258041258614
/* Read characters from the input up until the first non-diacritic */
258042258615
do {
258043258616
iNext = zIn - (const unsigned char*)pText;
258617
+ if( zIn>=zEof ){
258618
+ iCode = 0;
258619
+ break;
258620
+ }
258044258621
READ_UTF8(zIn, zEof, iCode);
258045
- if( iCode==0 ) break;
258046258622
if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam);
258047258623
}while( iCode==0 );
258048258624
258049258625
/* Pass the current trigram back to fts5 */
258050258626
rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext);
@@ -260077,11 +260653,11 @@
260077260653
260078260654
return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
260079260655
}
260080260656
260081260657
260082
-
260658
+/* Here ends the fts5.c composite file. */
260083260659
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
260084260660
260085260661
/************** End of fts5.c ************************************************/
260086260662
/************** Begin file stmt.c ********************************************/
260087260663
/*
@@ -260433,6 +261009,7 @@
260433261009
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
260434261010
260435261011
/************** End of stmt.c ************************************************/
260436261012
/* Return the source-id for this library */
260437261013
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
261014
+#endif /* SQLITE_AMALGAMATION */
260438261015
/************************** End of sqlite3.c ******************************/
260439261016
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.47.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -16,12 +16,15 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** 03a9703e27c44437c39363d0baf82db4ebc9.
 
 
22 */
 
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
27 #endif
@@ -460,13 +463,13 @@
460 **
461 ** See also: [sqlite3_libversion()],
462 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463 ** [sqlite_version()] and [sqlite_source_id()].
464 */
465 #define SQLITE_VERSION "3.47.0"
466 #define SQLITE_VERSION_NUMBER 3047000
467 #define SQLITE_SOURCE_ID "2024-10-21 16:30:22 03a9703e27c44437c39363d0baf82db4ebc94538a0f28411c85dda156f82636e"
468
469 /*
470 ** CAPI3REF: Run-Time Library Version Numbers
471 ** KEYWORDS: sqlite3_version sqlite3_sourceid
472 **
@@ -966,10 +969,17 @@
966 **
967 ** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
968 ** filesystem supports doing multiple write operations atomically when those
969 ** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
970 ** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
 
 
 
 
 
 
 
971 */
972 #define SQLITE_IOCAP_ATOMIC 0x00000001
973 #define SQLITE_IOCAP_ATOMIC512 0x00000002
974 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
975 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -982,10 +992,11 @@
982 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
983 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
984 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
985 #define SQLITE_IOCAP_IMMUTABLE 0x00002000
986 #define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
 
987
988 /*
989 ** CAPI3REF: File Locking Levels
990 **
991 ** SQLite uses one of these integer values as the second
@@ -1128,10 +1139,11 @@
1128 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
1129 ** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1130 ** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1131 ** <li> [SQLITE_IOCAP_IMMUTABLE]
1132 ** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
 
1133 ** </ul>
1134 **
1135 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
1136 ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1137 ** mean that writes of blocks that are nnn bytes in size and
@@ -1405,10 +1417,15 @@
1405 ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
1406 ** opcode causes the xFileControl method to swap the file handle with the one
1407 ** pointed to by the pArg argument. This capability is used during testing
1408 ** and only needs to be supported when SQLITE_TEST is defined.
1409 **
 
 
 
 
 
1410 ** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1411 ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1412 ** be advantageous to block on the next WAL lock if the lock is not immediately
1413 ** available. The WAL subsystem issues this signal during rare
1414 ** circumstances in order to fix a problem with priority inversion.
@@ -1558,10 +1575,11 @@
1558 #define SQLITE_FCNTL_RESERVE_BYTES 38
1559 #define SQLITE_FCNTL_CKPT_START 39
1560 #define SQLITE_FCNTL_EXTERNAL_READER 40
1561 #define SQLITE_FCNTL_CKSM_FILE 41
1562 #define SQLITE_FCNTL_RESET_CACHE 42
 
1563
1564 /* deprecated names */
1565 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1566 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1567 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -2936,14 +2954,18 @@
2936 **
2937 ** ^These functions return the number of rows modified, inserted or
2938 ** deleted by the most recently completed INSERT, UPDATE or DELETE
2939 ** statement on the database connection specified by the only parameter.
2940 ** The two functions are identical except for the type of the return value
2941 ** and that if the number of rows modified by the most recent INSERT, UPDATE
2942 ** or DELETE is greater than the maximum value supported by type "int", then
2943 ** the return value of sqlite3_changes() is undefined. ^Executing any other
2944 ** type of SQL statement does not modify the value returned by these functions.
 
 
 
 
2945 **
2946 ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2947 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2948 ** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2949 **
@@ -4499,15 +4521,26 @@
4499 **
4500 ** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4501 ** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4502 ** to return an error (error code SQLITE_ERROR) if the statement uses
4503 ** any virtual tables.
 
 
 
 
 
 
 
 
 
 
4504 ** </dl>
4505 */
4506 #define SQLITE_PREPARE_PERSISTENT 0x01
4507 #define SQLITE_PREPARE_NORMALIZE 0x02
4508 #define SQLITE_PREPARE_NO_VTAB 0x04
 
4509
4510 /*
4511 ** CAPI3REF: Compiling An SQL Statement
4512 ** KEYWORDS: {SQL statement compiler}
4513 ** METHOD: sqlite3
@@ -11194,11 +11227,11 @@
11194 #endif
11195
11196 #if 0
11197 } /* End of the 'extern "C"' block */
11198 #endif
11199 #endif /* SQLITE3_H */
11200
11201 /******** Begin file sqlite3rtree.h *********/
11202 /*
11203 ** 2010 August 30
11204 **
@@ -13445,17 +13478,32 @@
13445 ** This is used to access token iToken of phrase hit iIdx within the
13446 ** current row. If iIdx is less than zero or greater than or equal to the
13447 ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13448 ** output variable (*ppToken) is set to point to a buffer containing the
13449 ** matching document token, and (*pnToken) to the size of that buffer in
13450 ** bytes. This API is not available if the specified token matches a
13451 ** prefix query term. In that case both output variables are always set
13452 ** to 0.
13453 **
13454 ** The output text is not a copy of the document text that was tokenized.
13455 ** It is the output of the tokenizer module. For tokendata=1 tables, this
13456 ** includes any embedded 0x00 and trailing data.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13457 **
13458 ** This API can be quite slow if used with an FTS5 table created with the
13459 ** "detail=none" or "detail=column" option.
13460 **
13461 ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
@@ -13886,10 +13934,11 @@
13886 #endif
13887
13888 #endif /* _FTS5_H */
13889
13890 /******** End of fts5.h *********/
 
13891
13892 /************** End of sqlite3.h *********************************************/
13893 /************** Continuing where we left off in sqliteInt.h ******************/
13894
13895 /*
@@ -13931,10 +13980,11 @@
13931 ** to count the size: 2^31-1 or 2147483647.
13932 */
13933 #ifndef SQLITE_MAX_LENGTH
13934 # define SQLITE_MAX_LENGTH 1000000000
13935 #endif
 
13936
13937 /*
13938 ** This is the maximum number of
13939 **
13940 ** * Columns in a table
@@ -13996,13 +14046,17 @@
13996 # define SQLITE_MAX_VDBE_OP 250000000
13997 #endif
13998
13999 /*
14000 ** The maximum number of arguments to an SQL function.
 
 
 
 
14001 */
14002 #ifndef SQLITE_MAX_FUNCTION_ARG
14003 # define SQLITE_MAX_FUNCTION_ARG 127
14004 #endif
14005
14006 /*
14007 ** The suggested maximum number of in-memory pages to use for
14008 ** the main database table and for temporary tables.
@@ -16000,10 +16054,26 @@
16000 #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
16001 #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
16002 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
16003 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
16004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16005 /*
16006 ** Flags that make up the mask passed to sqlite3PagerGet().
16007 */
16008 #define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
16009 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
@@ -17025,11 +17095,11 @@
17025
17026 /*
17027 ** Additional non-public SQLITE_PREPARE_* flags
17028 */
17029 #define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
17030 #define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
17031
17032 /*
17033 ** Prototypes for the VDBE interface. See comments on the implementation
17034 ** for a description of what each of these routines does.
17035 */
@@ -17740,51 +17810,15 @@
17740 struct FuncDefHash {
17741 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
17742 };
17743 #define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
17744
17745 #if defined(SQLITE_USER_AUTHENTICATION)
17746 # warning "The SQLITE_USER_AUTHENTICATION extension is deprecated. \
17747 See ext/userauth/user-auth.txt for details."
17748 #endif
17749 #ifdef SQLITE_USER_AUTHENTICATION
17750 /*
17751 ** Information held in the "sqlite3" database connection object and used
17752 ** to manage user authentication.
17753 */
17754 typedef struct sqlite3_userauth sqlite3_userauth;
17755 struct sqlite3_userauth {
17756 u8 authLevel; /* Current authentication level */
17757 int nAuthPW; /* Size of the zAuthPW in bytes */
17758 char *zAuthPW; /* Password used to authenticate */
17759 char *zAuthUser; /* User name used to authenticate */
17760 };
17761
17762 /* Allowed values for sqlite3_userauth.authLevel */
17763 #define UAUTH_Unknown 0 /* Authentication not yet checked */
17764 #define UAUTH_Fail 1 /* User authentication failed */
17765 #define UAUTH_User 2 /* Authenticated as a normal user */
17766 #define UAUTH_Admin 3 /* Authenticated as an administrator */
17767
17768 /* Functions used only by user authorization logic */
17769 SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
17770 SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
17771 SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
17772 SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
17773
17774 #endif /* SQLITE_USER_AUTHENTICATION */
17775
17776 /*
17777 ** typedef for the authorization callback function.
17778 */
17779 #ifdef SQLITE_USER_AUTHENTICATION
17780 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17781 const char*, const char*);
17782 #else
17783 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
17784 const char*);
17785 #endif
17786
17787 #ifndef SQLITE_OMIT_DEPRECATED
17788 /* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
17789 ** in the style of sqlite3_trace()
17790 */
@@ -17941,13 +17975,10 @@
17941 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
17942 void *pUnlockArg; /* Argument to xUnlockNotify */
17943 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
17944 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
17945 #endif
17946 #ifdef SQLITE_USER_AUTHENTICATION
17947 sqlite3_userauth auth; /* User authentication information */
17948 #endif
17949 };
17950
17951 /*
17952 ** A macro to discover the encoding of a database.
17953 */
@@ -18102,11 +18133,11 @@
18102 **
18103 ** The u.pHash field is used by the global built-ins. The u.pDestructor
18104 ** field is used by per-connection app-def functions.
18105 */
18106 struct FuncDef {
18107 i8 nArg; /* Number of arguments. -1 means unlimited */
18108 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
18109 void *pUserData; /* User data parameter */
18110 FuncDef *pNext; /* Next function with same name */
18111 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
18112 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
@@ -22850,13 +22881,10 @@
22850 "UNLINK_AFTER_CLOSE",
22851 #endif
22852 #ifdef SQLITE_UNTESTABLE
22853 "UNTESTABLE",
22854 #endif
22855 #ifdef SQLITE_USER_AUTHENTICATION
22856 "USER_AUTHENTICATION",
22857 #endif
22858 #ifdef SQLITE_USE_ALLOCA
22859 "USE_ALLOCA",
22860 #endif
22861 #ifdef SQLITE_USE_FCNTL_TRACE
22862 "USE_FCNTL_TRACE",
@@ -23700,11 +23728,11 @@
23700 Vdbe *pVdbe; /* The VM that owns this context */
23701 int iOp; /* Instruction number of OP_Function */
23702 int isError; /* Error code returned by the function. */
23703 u8 enc; /* Encoding to use for results */
23704 u8 skipFlag; /* Skip accumulator loading if true */
23705 u8 argc; /* Number of arguments */
23706 sqlite3_value *argv[1]; /* Argument set */
23707 };
23708
23709 /* A bitfield type for use inside of structures. Always follow with :N where
23710 ** N is the number of bits.
@@ -23847,10 +23875,11 @@
23847 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
23848 int iNewReg; /* Register for new.* values */
23849 int iBlobWrite; /* Value returned by preupdate_blobwrite() */
23850 i64 iKey1; /* First key value passed to hook */
23851 i64 iKey2; /* Second key value passed to hook */
 
23852 Mem *aNew; /* Array of new.* values */
23853 Table *pTab; /* Schema object being updated */
23854 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
23855 sqlite3_value **apDflt; /* Array of default values, if required */
23856 };
@@ -32296,10 +32325,11 @@
32296 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
32297 ){
32298 pExpr = pExpr->pLeft;
32299 }
32300 if( pExpr==0 ) return;
 
32301 db->errByteOffset = pExpr->w.iOfst;
32302 }
32303
32304 /*
32305 ** Enlarge the memory allocation on a StrAccum object so that it is
@@ -33025,11 +33055,11 @@
33025 }
33026 if( pItem->fg.isCte ){
33027 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
33028 }
33029 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
33030 sqlite3_str_appendf(&x, " ON");
33031 }
33032 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc");
33033 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated");
33034 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized");
33035 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine");
@@ -34109,10 +34139,14 @@
34109 **
34110 ** This routines are given external linkage so that they will always be
34111 ** accessible to the debugging, and to avoid warnings about unused
34112 ** functions. But these routines only exist in debugging builds, so they
34113 ** do not contaminate the interface.
 
 
 
 
34114 */
34115 SQLITE_PRIVATE void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); }
34116 SQLITE_PRIVATE void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);}
34117 SQLITE_PRIVATE void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); }
34118 SQLITE_PRIVATE void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); }
@@ -35685,12 +35719,12 @@
35685 int esign = 1; /* sign of exponent */
35686 int e = 0; /* exponent */
35687 int eValid = 1; /* True exponent is either not used or is well-formed */
35688 int nDigit = 0; /* Number of digits processed */
35689 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
 
35690 double rr[2];
35691 u64 s2;
35692
35693 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
35694 *pResult = 0.0; /* Default return value, in case of an error */
35695 if( length==0 ) return 0;
35696
@@ -35789,25 +35823,36 @@
35789
35790 /* adjust exponent by d, and update sign */
35791 e = (e*esign) + d;
35792
35793 /* Try to adjust the exponent to make it smaller */
35794 while( e>0 && s<(LARGEST_UINT64/10) ){
35795 s *= 10;
35796 e--;
35797 }
35798 while( e<0 && (s%10)==0 ){
35799 s /= 10;
35800 e++;
35801 }
35802
35803 rr[0] = (double)s;
35804 s2 = (u64)rr[0];
35805 #if defined(_MSC_VER) && _MSC_VER<1700
35806 if( s2==0x8000000000000000LL ){ s2 = 2*(u64)(0.5*rr[0]); }
 
 
35807 #endif
35808 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
 
 
 
 
 
 
 
 
 
35809 if( e>0 ){
35810 while( e>=100 ){
35811 e -= 100;
35812 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
35813 }
@@ -38674,11 +38719,11 @@
38674 # define F_SETLKW 7
38675 # endif
38676 # endif
38677 #else /* !SQLITE_WASI */
38678 # ifndef HAVE_FCHMOD
38679 # define HAVE_FCHMOD
38680 # endif
38681 #endif /* SQLITE_WASI */
38682
38683 #ifdef SQLITE_WASI
38684 # define osGetpid(X) (pid_t)1
@@ -42448,10 +42493,15 @@
42448 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
42449 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
42450 }
42451 #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
42452
 
 
 
 
 
42453 case SQLITE_FCNTL_LOCKSTATE: {
42454 *(int*)pArg = pFile->eFileLock;
42455 return SQLITE_OK;
42456 }
42457 case SQLITE_FCNTL_LAST_ERRNO: {
@@ -42589,10 +42639,11 @@
42589
42590 /* Set the POWERSAFE_OVERWRITE flag if requested. */
42591 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
42592 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
42593 }
 
42594
42595 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42596 }
42597 }
42598 #else
@@ -50328,10 +50379,15 @@
50328 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
50329 hOldFile, pFile->h));
50330 return SQLITE_OK;
50331 }
50332 #endif
 
 
 
 
 
50333 case SQLITE_FCNTL_TEMPFILENAME: {
50334 char *zTFile = 0;
50335 int rc = winGetTempname(pFile->pVfs, &zTFile);
50336 if( rc==SQLITE_OK ){
50337 *(char**)pArg = zTFile;
@@ -50389,11 +50445,11 @@
50389 /*
50390 ** Return a vector of device characteristics.
50391 */
50392 static int winDeviceCharacteristics(sqlite3_file *id){
50393 winFile *p = (winFile*)id;
50394 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
50395 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
50396 }
50397
50398 /*
50399 ** Windows will only let you create file view mappings
@@ -51777,11 +51833,11 @@
51777 */
51778 char *zTmpname = 0; /* For temporary filename, if necessary. */
51779
51780 int rc = SQLITE_OK; /* Function Return Code */
51781 #if !defined(NDEBUG) || SQLITE_OS_WINCE
51782 int eType = flags&0xFFFFFF00; /* Type of file to open */
51783 #endif
51784
51785 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
51786 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
51787 int isCreate = (flags & SQLITE_OPEN_CREATE);
@@ -57978,43 +58034,37 @@
57978 # define USEFETCH(x) ((x)->bUseFetch)
57979 #else
57980 # define USEFETCH(x) 0
57981 #endif
57982
57983 /*
57984 ** The argument to this macro is a file descriptor (type sqlite3_file*).
57985 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
57986 **
57987 ** This is so that expressions can be written as:
57988 **
57989 ** if( isOpen(pPager->jfd) ){ ...
57990 **
57991 ** instead of
57992 **
57993 ** if( pPager->jfd->pMethods ){ ...
57994 */
57995 #define isOpen(pFd) ((pFd)->pMethods!=0)
57996
57997 #ifdef SQLITE_DIRECT_OVERFLOW_READ
57998 /*
57999 ** Return true if page pgno can be read directly from the database file
58000 ** by the b-tree layer. This is the case if:
58001 **
58002 ** * the database file is open,
58003 ** * there are no dirty pages in the cache, and
58004 ** * the desired page is not currently in the wal file.
 
58005 */
58006 SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
58007 if( pPager->fd->pMethods==0 ) return 0;
58008 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
 
 
58009 #ifndef SQLITE_OMIT_WAL
58010 if( pPager->pWal ){
58011 u32 iRead = 0;
58012 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
58013 return iRead==0;
58014 }
58015 #endif
 
 
 
 
 
58016 return 1;
58017 }
58018 #endif
58019
58020 #ifndef SQLITE_OMIT_WAL
@@ -59269,11 +59319,11 @@
59269 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
59270 }
59271 }
59272 pPager->journalOff = 0;
59273 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
59274 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
59275 ){
59276 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
59277 pPager->journalOff = 0;
59278 }else{
59279 /* This branch may be executed with Pager.journalMode==MEMORY if
@@ -67979,15 +68029,11 @@
67979 ** so it takes care to hold an exclusive lock on the corresponding
67980 ** WAL_READ_LOCK() while changing values.
67981 */
67982 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
67983 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
67984 u32 mxReadMark; /* Largest aReadMark[] value */
67985 int mxI; /* Index of largest aReadMark[] value */
67986 int i; /* Loop counter */
67987 int rc = SQLITE_OK; /* Return code */
67988 u32 mxFrame; /* Wal frame to lock to */
67989 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
67990 int nBlockTmout = 0;
67991 #endif
67992
67993 assert( pWal->readLock<0 ); /* Not currently locked */
@@ -68089,145 +68135,151 @@
68089
68090 assert( pWal->nWiData>0 );
68091 assert( pWal->apWiData[0]!=0 );
68092 pInfo = walCkptInfo(pWal);
68093 SEH_INJECT_FAULT;
68094 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68095 #ifdef SQLITE_ENABLE_SNAPSHOT
68096 && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
68097 #endif
68098 ){
68099 /* The WAL has been completely backfilled (or it is empty).
68100 ** and can be safely ignored.
68101 */
68102 rc = walLockShared(pWal, WAL_READ_LOCK(0));
68103 walShmBarrier(pWal);
68104 if( rc==SQLITE_OK ){
68105 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
68106 /* It is not safe to allow the reader to continue here if frames
68107 ** may have been appended to the log before READ_LOCK(0) was obtained.
68108 ** When holding READ_LOCK(0), the reader ignores the entire log file,
68109 ** which implies that the database file contains a trustworthy
68110 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
68111 ** happening, this is usually correct.
68112 **
68113 ** However, if frames have been appended to the log (or if the log
68114 ** is wrapped and written for that matter) before the READ_LOCK(0)
68115 ** is obtained, that is not necessarily true. A checkpointer may
68116 ** have started to backfill the appended frames but crashed before
68117 ** it finished. Leaving a corrupt image in the database file.
68118 */
68119 walUnlockShared(pWal, WAL_READ_LOCK(0));
68120 return WAL_RETRY;
68121 }
68122 pWal->readLock = 0;
68123 return SQLITE_OK;
68124 }else if( rc!=SQLITE_BUSY ){
68125 return rc;
68126 }
68127 }
68128
68129 /* If we get this far, it means that the reader will want to use
68130 ** the WAL to get at content from recent commits. The job now is
68131 ** to select one of the aReadMark[] entries that is closest to
68132 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
68133 */
68134 mxReadMark = 0;
68135 mxI = 0;
68136 mxFrame = pWal->hdr.mxFrame;
68137 #ifdef SQLITE_ENABLE_SNAPSHOT
68138 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
68139 mxFrame = pWal->pSnapshot->mxFrame;
68140 }
68141 #endif
68142 for(i=1; i<WAL_NREADER; i++){
68143 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68144 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
68145 assert( thisMark!=READMARK_NOT_USED );
68146 mxReadMark = thisMark;
68147 mxI = i;
68148 }
68149 }
68150 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
68151 && (mxReadMark<mxFrame || mxI==0)
68152 ){
68153 for(i=1; i<WAL_NREADER; i++){
68154 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
68155 if( rc==SQLITE_OK ){
68156 AtomicStore(pInfo->aReadMark+i,mxFrame);
68157 mxReadMark = mxFrame;
68158 mxI = i;
68159 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
68160 break;
68161 }else if( rc!=SQLITE_BUSY ){
68162 return rc;
68163 }
68164 }
68165 }
68166 if( mxI==0 ){
68167 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
68168 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
68169 }
68170
68171 (void)walEnableBlockingMs(pWal, nBlockTmout);
68172 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
68173 walDisableBlocking(pWal);
68174 if( rc ){
68175 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68176 if( rc==SQLITE_BUSY_TIMEOUT ){
68177 *pCnt |= WAL_RETRY_BLOCKED_MASK;
68178 }
68179 #else
68180 assert( rc!=SQLITE_BUSY_TIMEOUT );
68181 #endif
68182 assert( (rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT );
68183 return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc;
68184 }
68185 /* Now that the read-lock has been obtained, check that neither the
68186 ** value in the aReadMark[] array or the contents of the wal-index
68187 ** header have changed.
68188 **
68189 ** It is necessary to check that the wal-index header did not change
68190 ** between the time it was read and when the shared-lock was obtained
68191 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
68192 ** that the log file may have been wrapped by a writer, or that frames
68193 ** that occur later in the log than pWal->hdr.mxFrame may have been
68194 ** copied into the database by a checkpointer. If either of these things
68195 ** happened, then reading the database with the current value of
68196 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
68197 ** instead.
68198 **
68199 ** Before checking that the live wal-index header has not changed
68200 ** since it was read, set Wal.minFrame to the first frame in the wal
68201 ** file that has not yet been checkpointed. This client will not need
68202 ** to read any frames earlier than minFrame from the wal file - they
68203 ** can be safely read directly from the database file.
68204 **
68205 ** Because a ShmBarrier() call is made between taking the copy of
68206 ** nBackfill and checking that the wal-header in shared-memory still
68207 ** matches the one cached in pWal->hdr, it is guaranteed that the
68208 ** checkpointer that set nBackfill was not working with a wal-index
68209 ** header newer than that cached in pWal->hdr. If it were, that could
68210 ** cause a problem. The checkpointer could omit to checkpoint
68211 ** a version of page X that lies before pWal->minFrame (call that version
68212 ** A) on the basis that there is a newer version (version B) of the same
68213 ** page later in the wal file. But if version B happens to like past
68214 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
68215 ** that it can read version A from the database file. However, since
68216 ** we can guarantee that the checkpointer that set nBackfill could not
68217 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
68218 */
68219 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
68220 walShmBarrier(pWal);
68221 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
68222 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
68223 ){
68224 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
68225 return WAL_RETRY;
68226 }else{
68227 assert( mxReadMark<=pWal->hdr.mxFrame );
68228 pWal->readLock = (i16)mxI;
 
 
 
 
 
 
68229 }
68230 return rc;
68231 }
68232
68233 #ifdef SQLITE_ENABLE_SNAPSHOT
@@ -87103,10 +87155,11 @@
87103 **
87104 ** All other fields of Mem can safely remain uninitialized for now. They
87105 ** will be initialized before use.
87106 */
87107 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
 
87108 if( N>0 ){
87109 do{
87110 p->flags = flags;
87111 p->db = db;
87112 p->szMalloc = 0;
@@ -87128,10 +87181,11 @@
87128 */
87129 static void releaseMemArray(Mem *p, int N){
87130 if( p && N ){
87131 Mem *pEnd = &p[N];
87132 sqlite3 *db = p->db;
 
87133 if( db->pnBytesFreed ){
87134 do{
87135 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87136 }while( (++p)<pEnd );
87137 return;
@@ -87608,10 +87662,11 @@
87608 assert( p!=0 );
87609 assert( p->nOp>0 );
87610 assert( pParse!=0 );
87611 assert( p->eVdbeState==VDBE_INIT_STATE );
87612 assert( pParse==p->pParse );
 
87613 p->pVList = pParse->pVList;
87614 pParse->pVList = 0;
87615 db = p->db;
87616 assert( db->mallocFailed==0 );
87617 nVar = pParse->nVar;
@@ -90488,10 +90543,11 @@
90488 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
90489 db->pPreUpdate = 0;
90490 sqlite3DbFree(db, preupdate.aRecord);
90491 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
90492 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
 
90493 if( preupdate.aNew ){
90494 int i;
90495 for(i=0; i<pCsr->nField; i++){
90496 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
90497 }
@@ -91844,11 +91900,11 @@
91844 **
91845 ** sqlite3_column_int()
91846 ** sqlite3_column_int64()
91847 ** sqlite3_column_text()
91848 ** sqlite3_column_text16()
91849 ** sqlite3_column_real()
91850 ** sqlite3_column_bytes()
91851 ** sqlite3_column_bytes16()
91852 ** sqlite3_column_blob()
91853 */
91854 static void columnMallocFailure(sqlite3_stmt *pStmt)
@@ -92706,64 +92762,68 @@
92706 if( iIdx>=p->pCsr->nField || iIdx<0 ){
92707 rc = SQLITE_RANGE;
92708 goto preupdate_old_out;
92709 }
92710
92711 /* If the old.* record has not yet been loaded into memory, do so now. */
92712 if( p->pUnpacked==0 ){
92713 u32 nRec;
92714 u8 *aRec;
92715
92716 assert( p->pCsr->eCurType==CURTYPE_BTREE );
92717 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
92718 aRec = sqlite3DbMallocRaw(db, nRec);
92719 if( !aRec ) goto preupdate_old_out;
92720 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
92721 if( rc==SQLITE_OK ){
92722 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
92723 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
92724 }
92725 if( rc!=SQLITE_OK ){
92726 sqlite3DbFree(db, aRec);
92727 goto preupdate_old_out;
92728 }
92729 p->aRecord = aRec;
92730 }
92731
92732 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
92733 if( iIdx==p->pTab->iPKey ){
 
92734 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
92735 }else if( iIdx>=p->pUnpacked->nField ){
92736 /* This occurs when the table has been extended using ALTER TABLE
92737 ** ADD COLUMN. The value to return is the default value of the column. */
92738 Column *pCol = &p->pTab->aCol[iIdx];
92739 if( pCol->iDflt>0 ){
92740 if( p->apDflt==0 ){
92741 int nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
92742 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
92743 if( p->apDflt==0 ) goto preupdate_old_out;
92744 }
92745 if( p->apDflt[iIdx]==0 ){
92746 sqlite3_value *pVal = 0;
92747 Expr *pDflt;
92748 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) );
92749 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
92750 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal);
92751 if( rc==SQLITE_OK && pVal==0 ){
92752 rc = SQLITE_CORRUPT_BKPT;
92753 }
92754 p->apDflt[iIdx] = pVal;
92755 }
92756 *ppValue = p->apDflt[iIdx];
92757 }else{
92758 *ppValue = (sqlite3_value *)columnNullValue();
92759 }
92760 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
92761 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
92762 testcase( pMem->flags & MEM_Int );
92763 testcase( pMem->flags & MEM_IntReal );
92764 sqlite3VdbeMemRealify(pMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92765 }
92766 }
92767
92768 preupdate_old_out:
92769 sqlite3Error(db, rc);
@@ -97913,13 +97973,15 @@
97913 0, pCx->uc.pCursor);
97914 pCx->isTable = 1;
97915 }
97916 }
97917 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
 
97918 if( rc ){
97919 assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
97920 sqlite3BtreeClose(pCx->ub.pBtx);
 
97921 }else{
97922 assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
97923 }
97924 }
97925 }
@@ -109845,11 +109907,11 @@
109845 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
109846 }
109847 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
109848 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
109849 (void*)p4, P4_COLLSEQ);
109850 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
109851 return addr;
109852 }
109853
109854 /*
109855 ** Return true if expression pExpr is a vector, or false otherwise.
@@ -112012,11 +112074,11 @@
112012 **
112013 ** (4) If pSrc is the right operand of a LEFT JOIN, then...
112014 ** (4a) pExpr must come from an ON clause..
112015 ** (4b) and specifically the ON clause associated with the LEFT JOIN.
112016 **
112017 ** (5) If pSrc is not the right operand of a LEFT JOIN or the left
112018 ** operand of a RIGHT JOIN, then pExpr must be from the WHERE
112019 ** clause, not an ON clause.
112020 **
112021 ** (6) Either:
112022 **
@@ -115546,35 +115608,41 @@
115546 **
115547 ** Additionally, if pExpr is a simple SQL value and the value is the
115548 ** same as that currently bound to variable pVar, non-zero is returned.
115549 ** Otherwise, if the values are not the same or if pExpr is not a simple
115550 ** SQL value, zero is returned.
 
 
 
115551 */
115552 static int exprCompareVariable(
115553 const Parse *pParse,
115554 const Expr *pVar,
115555 const Expr *pExpr
115556 ){
115557 int res = 0;
115558 int iVar;
115559 sqlite3_value *pL, *pR = 0;
115560
 
 
 
 
115561 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
115562 if( pR ){
115563 iVar = pVar->iColumn;
115564 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
115565 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
115566 if( pL ){
115567 if( sqlite3_value_type(pL)==SQLITE_TEXT ){
115568 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
115569 }
115570 res = 0==sqlite3MemCompare(pL, pR, 0);
115571 }
115572 sqlite3ValueFree(pR);
115573 sqlite3ValueFree(pL);
115574 }
115575
115576 return res;
115577 }
115578
115579 /*
115580 ** Do a deep comparison of two expression trees. Return 0 if the two
@@ -115596,16 +115664,14 @@
115596 ** can be sure the expressions are the same. In the places where
115597 ** this routine is used, it does not hurt to get an extra 2 - that
115598 ** just might result in some slightly slower code. But returning
115599 ** an incorrect 0 or 1 could lead to a malfunction.
115600 **
115601 ** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
115602 ** pParse->pReprepare can be matched against literals in pB. The
115603 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
115604 ** If pParse is NULL (the normal case) then any TK_VARIABLE term in
115605 ** Argument pParse should normally be NULL. If it is not NULL and pA or
115606 ** pB causes a return value of 2.
115607 */
115608 SQLITE_PRIVATE int sqlite3ExprCompare(
115609 const Parse *pParse,
115610 const Expr *pA,
115611 const Expr *pB,
@@ -115613,12 +115679,12 @@
115613 ){
115614 u32 combinedFlags;
115615 if( pA==0 || pB==0 ){
115616 return pB==pA ? 0 : 2;
115617 }
115618 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
115619 return 0;
115620 }
115621 combinedFlags = pA->flags | pB->flags;
115622 if( combinedFlags & EP_IntValue ){
115623 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
115624 return 0;
@@ -115808,23 +115874,75 @@
115808 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
115809 }
115810 }
115811 return 0;
115812 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115813
115814 /*
115815 ** Return true if we can prove the pE2 will always be true if pE1 is
115816 ** true. Return false if we cannot complete the proof or if pE2 might
115817 ** be false. Examples:
115818 **
115819 ** pE1: x==5 pE2: x==5 Result: true
115820 ** pE1: x>0 pE2: x==5 Result: false
115821 ** pE1: x=21 pE2: x=21 OR y=43 Result: true
115822 ** pE1: x!=123 pE2: x IS NOT NULL Result: true
115823 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true
115824 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false
115825 ** pE1: x IS ?2 pE2: x IS NOT NULL Result: false
 
 
115826 **
115827 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
115828 ** Expr.iTable<0 then assume a table number given by iTab.
115829 **
115830 ** If pParse is not NULL, then the values of bound variables in pE1 are
@@ -115854,10 +115972,13 @@
115854 if( pE2->op==TK_NOTNULL
115855 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
115856 ){
115857 return 1;
115858 }
 
 
 
115859 return 0;
115860 }
115861
115862 /* This is a helper function to impliesNotNullRow(). In this routine,
115863 ** set pWalker->eCode to one only if *both* of the input expressions
@@ -121265,19 +121386,10 @@
121265 rc = sqlite3Init(db, &zErrDyn);
121266 }
121267 sqlite3BtreeLeaveAll(db);
121268 assert( zErrDyn==0 || rc!=SQLITE_OK );
121269 }
121270 #ifdef SQLITE_USER_AUTHENTICATION
121271 if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
121272 u8 newAuth = 0;
121273 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
121274 if( newAuth<db->auth.authLevel ){
121275 rc = SQLITE_AUTH_USER;
121276 }
121277 }
121278 #endif
121279 if( rc ){
121280 if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){
121281 int iDb = db->nDb - 1;
121282 assert( iDb>=2 );
121283 if( db->aDb[iDb].pBt ){
@@ -121771,15 +121883,11 @@
121771 sqlite3 *db = pParse->db; /* Database handle */
121772 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
121773 int rc; /* Auth callback return code */
121774
121775 if( db->init.busy ) return SQLITE_OK;
121776 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
121777 #ifdef SQLITE_USER_AUTHENTICATION
121778 ,db->auth.zAuthUser
121779 #endif
121780 );
121781 if( rc==SQLITE_DENY ){
121782 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
121783 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
121784 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
121785 pParse->rc = SQLITE_AUTH;
@@ -121882,15 +121990,11 @@
121882 testcase( zArg1==0 );
121883 testcase( zArg2==0 );
121884 testcase( zArg3==0 );
121885 testcase( pParse->zAuthContext==0 );
121886
121887 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
121888 #ifdef SQLITE_USER_AUTHENTICATION
121889 ,db->auth.zAuthUser
121890 #endif
121891 );
121892 if( rc==SQLITE_DENY ){
121893 sqlite3ErrorMsg(pParse, "not authorized");
121894 pParse->rc = SQLITE_AUTH;
121895 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
121896 rc = SQLITE_DENY;
@@ -122119,21 +122223,10 @@
122119 sqlite3VdbeJumpHere(v, addrRewind);
122120 }
122121 }
122122 sqlite3VdbeAddOp0(v, OP_Halt);
122123
122124 #if SQLITE_USER_AUTHENTICATION && !defined(SQLITE_OMIT_SHARED_CACHE)
122125 if( pParse->nTableLock>0 && db->init.busy==0 ){
122126 sqlite3UserAuthInit(db);
122127 if( db->auth.authLevel<UAUTH_User ){
122128 sqlite3ErrorMsg(pParse, "user not authenticated");
122129 pParse->rc = SQLITE_AUTH_USER;
122130 return;
122131 }
122132 }
122133 #endif
122134
122135 /* The cookie mask contains one bit for each database file open.
122136 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
122137 ** set for each database that is used. Generate code to start a
122138 ** transaction on each used database and to verify the schema cookie
122139 ** on each used database.
@@ -122258,20 +122351,10 @@
122258 sqlite3DbFree(db, zSql);
122259 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
122260 pParse->nested--;
122261 }
122262
122263 #if SQLITE_USER_AUTHENTICATION
122264 /*
122265 ** Return TRUE if zTable is the name of the system table that stores the
122266 ** list of users and their access credentials.
122267 */
122268 SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
122269 return sqlite3_stricmp(zTable, "sqlite_user")==0;
122270 }
122271 #endif
122272
122273 /*
122274 ** Locate the in-memory structure that describes a particular database
122275 ** table given the name of that table and (optionally) the name of the
122276 ** database containing the table. Return NULL if not found.
122277 **
@@ -122286,17 +122369,10 @@
122286 Table *p = 0;
122287 int i;
122288
122289 /* All mutexes are required for schema access. Make sure we hold them. */
122290 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
122291 #if SQLITE_USER_AUTHENTICATION
122292 /* Only the admin user is allowed to know that the sqlite_user table
122293 ** exists */
122294 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
122295 return 0;
122296 }
122297 #endif
122298 if( zDatabase ){
122299 for(i=0; i<db->nDb; i++){
122300 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
122301 }
122302 if( i>=db->nDb ){
@@ -125951,13 +126027,10 @@
125951
125952 assert( pTab!=0 );
125953 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
125954 && db->init.busy==0
125955 && pTblName!=0
125956 #if SQLITE_USER_AUTHENTICATION
125957 && sqlite3UserAuthTable(pTab->zName)==0
125958 #endif
125959 ){
125960 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
125961 goto exit_create_index;
125962 }
125963 #ifndef SQLITE_OMIT_VIEW
@@ -129661,20 +129734,19 @@
129661 const unsigned char *z;
129662 const unsigned char *z2;
129663 int len;
129664 int p0type;
129665 i64 p1, p2;
129666 int negP2 = 0;
129667
129668 assert( argc==3 || argc==2 );
129669 if( sqlite3_value_type(argv[1])==SQLITE_NULL
129670 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
129671 ){
129672 return;
129673 }
129674 p0type = sqlite3_value_type(argv[0]);
129675 p1 = sqlite3_value_int(argv[1]);
129676 if( p0type==SQLITE_BLOB ){
129677 len = sqlite3_value_bytes(argv[0]);
129678 z = sqlite3_value_blob(argv[0]);
129679 if( z==0 ) return;
129680 assert( len==sqlite3_value_bytes(argv[0]) );
@@ -129695,36 +129767,36 @@
129695 ** from 2009-02-02 for compatibility of applications that exploited the
129696 ** old buggy behavior. */
129697 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
129698 #endif
129699 if( argc==3 ){
129700 p2 = sqlite3_value_int(argv[2]);
129701 if( p2<0 ){
129702 p2 = -p2;
129703 negP2 = 1;
129704 }
129705 }else{
129706 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
129707 }
129708 if( p1<0 ){
129709 p1 += len;
129710 if( p1<0 ){
129711 p2 += p1;
129712 if( p2<0 ) p2 = 0;
 
 
 
129713 p1 = 0;
129714 }
129715 }else if( p1>0 ){
129716 p1--;
129717 }else if( p2>0 ){
129718 p2--;
129719 }
129720 if( negP2 ){
 
 
 
 
 
129721 p1 -= p2;
129722 if( p1<0 ){
129723 p2 += p1;
129724 p1 = 0;
129725 }
129726 }
129727 assert( p1>=0 && p2>=0 );
129728 if( p0type!=SQLITE_BLOB ){
129729 while( *z && p1 ){
129730 SQLITE_SKIP_UTF8(z);
@@ -129734,13 +129806,15 @@
129734 SQLITE_SKIP_UTF8(z2);
129735 }
129736 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
129737 SQLITE_UTF8);
129738 }else{
129739 if( p1+p2>len ){
 
 
129740 p2 = len-p1;
129741 if( p2<0 ) p2 = 0;
129742 }
129743 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
129744 }
129745 }
129746
@@ -129747,17 +129821,17 @@
129747 /*
129748 ** Implementation of the round() function
129749 */
129750 #ifndef SQLITE_OMIT_FLOATING_POINT
129751 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
129752 int n = 0;
129753 double r;
129754 char *zBuf;
129755 assert( argc==1 || argc==2 );
129756 if( argc==2 ){
129757 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
129758 n = sqlite3_value_int(argv[1]);
129759 if( n>30 ) n = 30;
129760 if( n<0 ) n = 0;
129761 }
129762 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
129763 r = sqlite3_value_double(argv[0]);
@@ -129768,11 +129842,11 @@
129768 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
129769 /* The value has no fractional part so there is nothing to round */
129770 }else if( n==0 ){
129771 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
129772 }else{
129773 zBuf = sqlite3_mprintf("%!.*f",n,r);
129774 if( zBuf==0 ){
129775 sqlite3_result_error_nomem(context);
129776 return;
129777 }
129778 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
@@ -131985,13 +132059,10 @@
131985 #endif
131986 #ifndef SQLITE_OMIT_LOAD_EXTENSION
131987 SFUNCTION(load_extension, 1, 0, 0, loadExt ),
131988 SFUNCTION(load_extension, 2, 0, 0, loadExt ),
131989 #endif
131990 #if SQLITE_USER_AUTHENTICATION
131991 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
131992 #endif
131993 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
131994 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
131995 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
131996 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
131997 INLINE_FUNC(unlikely, 1, INLINEFUNC_unlikely, SQLITE_FUNC_UNLIKELY),
@@ -132124,11 +132195,14 @@
132124 MFUNCTION(degrees, 1, radToDeg, math1Func ),
132125 MFUNCTION(pi, 0, 0, piFunc ),
132126 #endif /* SQLITE_ENABLE_MATH_FUNCTIONS */
132127 FUNCTION(sign, 1, 0, 0, signFunc ),
132128 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
 
132129 INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ),
 
 
132130 };
132131 #ifndef SQLITE_OMIT_ALTERTABLE
132132 sqlite3AlterFunctions();
132133 #endif
132134 sqlite3WindowFunctions();
@@ -140637,16 +140711,10 @@
140637 if( db->autoCommit==0 ){
140638 /* Foreign key support may not be enabled or disabled while not
140639 ** in auto-commit mode. */
140640 mask &= ~(SQLITE_ForeignKeys);
140641 }
140642 #if SQLITE_USER_AUTHENTICATION
140643 if( db->auth.authLevel==UAUTH_User ){
140644 /* Do not allow non-admin users to modify the schema arbitrarily */
140645 mask &= ~(SQLITE_WriteSchema);
140646 }
140647 #endif
140648
140649 if( sqlite3GetBoolean(zRight, 0) ){
140650 if( (mask & SQLITE_WriteSchema)==0
140651 || (db->flags & SQLITE_Defensive)==0
140652 ){
@@ -140778,11 +140846,12 @@
140778 pTab = sqliteHashData(k);
140779 if( pTab->nCol==0 ){
140780 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
140781 if( zSql ){
140782 sqlite3_stmt *pDummy = 0;
140783 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
 
140784 (void)sqlite3_finalize(pDummy);
140785 sqlite3DbFree(db, zSql);
140786 }
140787 if( db->mallocFailed ){
140788 sqlite3ErrorMsg(db->pParse, "out of memory");
@@ -141259,11 +141328,11 @@
141259 sqlite3VdbeAddOp3(v, OP_Null, 0, 8, 8+cnt);
141260 sqlite3ClearTempRegCache(pParse);
141261
141262 /* Do the b-tree integrity checks */
141263 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY);
141264 sqlite3VdbeChangeP5(v, (u8)i);
141265 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
141266 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
141267 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
141268 P4_DYNAMIC);
141269 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
@@ -142879,18 +142948,11 @@
142879 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
142880 if( encoding==0 ) encoding = SQLITE_UTF8;
142881 #else
142882 encoding = SQLITE_UTF8;
142883 #endif
142884 if( db->nVdbeActive>0 && encoding!=ENC(db)
142885 && (db->mDbFlags & DBFLAG_Vacuum)==0
142886 ){
142887 rc = SQLITE_LOCKED;
142888 goto initone_error_out;
142889 }else{
142890 sqlite3SetTextEncoding(db, encoding);
142891 }
142892 }else{
142893 /* If opening an attached database, the encoding much match ENC(db) */
142894 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
142895 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
142896 " text encoding as main database");
@@ -147584,36 +147646,36 @@
147584 return pExpr;
147585 }
147586 if( pSubst->isOuterJoin ){
147587 ExprSetProperty(pNew, EP_CanBeNull);
147588 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147589 if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){
147590 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
147591 pExpr->flags & (EP_OuterON|EP_InnerON));
147592 }
147593 sqlite3ExprDelete(db, pExpr);
147594 pExpr = pNew;
147595 if( pExpr->op==TK_TRUEFALSE ){
147596 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
147597 pExpr->op = TK_INTEGER;
147598 ExprSetProperty(pExpr, EP_IntValue);
147599 }
147600
147601 /* Ensure that the expression now has an implicit collation sequence,
147602 ** just as it did when it was a column of a view or sub-query. */
147603 {
147604 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
147605 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
147606 pSubst->pCList->a[iColumn].pExpr
147607 );
147608 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
147609 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
147610 (pColl ? pColl->zName : "BINARY")
147611 );
147612 }
147613 }
147614 ExprClearProperty(pExpr, EP_Collate);
147615 }
147616 }
147617 }else{
147618 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
147619 pExpr->iTable = pSubst->iNewTable;
@@ -148346,20 +148408,20 @@
148346 }
148347
148348 /* Transfer the FROM clause terms from the subquery into the
148349 ** outer query.
148350 */
 
148351 for(i=0; i<nSubSrc; i++){
148352 SrcItem *pItem = &pSrc->a[i+iFrom];
148353 assert( pItem->fg.isTabFunc==0 );
148354 assert( pItem->fg.isSubquery
148355 || pItem->fg.fixedSchema
148356 || pItem->u4.zDatabase==0 );
148357 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
148358 *pItem = pSubSrc->a[i];
148359 pItem->fg.jointype |= ltorj;
148360 iNewParent = pSubSrc->a[i].iCursor;
148361 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
148362 }
148363 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
148364 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
148365
@@ -148395,10 +148457,11 @@
148395 pSub->pOrderBy = 0;
148396 }
148397 pWhere = pSub->pWhere;
148398 pSub->pWhere = 0;
148399 if( isOuterJoin>0 ){
 
148400 sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON);
148401 }
148402 if( pWhere ){
148403 if( pParent->pWhere ){
148404 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
@@ -150498,11 +150561,11 @@
150498 }
150499 sqlite3ReleaseTempReg(pParse, regSubtype);
150500 }
150501 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
150502 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
150503 sqlite3VdbeChangeP5(v, (u8)nArg);
150504 sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v);
150505 sqlite3VdbeJumpHere(v, iTop);
150506 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150507 }
150508 sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i),
@@ -150661,11 +150724,11 @@
150661 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0,
150662 (char *)pColl, P4_COLLSEQ);
150663 }
150664 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i));
150665 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
150666 sqlite3VdbeChangeP5(v, (u8)nArg);
150667 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
150668 }
150669 if( addrNext ){
150670 sqlite3VdbeResolveLabel(v, addrNext);
150671 }
@@ -151494,11 +151557,11 @@
151494 sqlite3TreeViewSelect(0, p, 0);
151495 }
151496 #endif
151497 assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 );
151498 }else{
151499 TREETRACE(0x4000,pParse,p,("WHERE-lcause push-down not possible\n"));
151500 }
151501
151502 /* Convert unused result columns of the subquery into simple NULL
151503 ** expressions, to avoid unneeded searching and computation.
151504 ** tag-select-0440
@@ -154055,11 +154118,11 @@
154055 /* Set the P5 operand of the OP_Program instruction to non-zero if
154056 ** recursive invocation of this trigger program is disallowed. Recursive
154057 ** invocation is disallowed if (a) the sub-program is really a trigger,
154058 ** not a foreign key action, and (b) the flag to enable recursive triggers
154059 ** is clear. */
154060 sqlite3VdbeChangeP5(v, (u8)bRecursive);
154061 }
154062 }
154063
154064 /*
154065 ** This is called to code the required FOR EACH ROW triggers for an operation
@@ -158268,13 +158331,21 @@
158268 SQLITE_PRIVATE int sqlite3WhereExplainBloomFilter(
158269 const Parse *pParse, /* Parse context */
158270 const WhereInfo *pWInfo, /* WHERE clause */
158271 const WhereLevel *pLevel /* Bloom filter on this level */
158272 );
 
 
 
 
 
 
 
158273 #else
158274 # define sqlite3WhereExplainOneScan(u,v,w,x) 0
158275 # define sqlite3WhereExplainBloomFilter(u,v,w) 0
 
158276 #endif /* SQLITE_OMIT_EXPLAIN */
158277 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
158278 SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
158279 Vdbe *v, /* Vdbe to add scanstatus entry to */
158280 SrcList *pSrclist, /* FROM clause pLvl reads data from */
@@ -158472,42 +158543,42 @@
158472 }
158473 sqlite3_str_append(pStr, ")", 1);
158474 }
158475
158476 /*
158477 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
158478 ** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG
158479 ** was defined at compile-time. If it is not a no-op, a single OP_Explain
158480 ** opcode is added to the output to describe the table scan strategy in pLevel.
158481 **
158482 ** If an OP_Explain opcode is added to the VM, its address is returned.
158483 ** Otherwise, if no OP_Explain is coded, zero is returned.
158484 */
158485 SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
158486 Parse *pParse, /* Parse context */
 
158487 SrcList *pTabList, /* Table list this loop refers to */
158488 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
158489 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
158490 ){
158491 int ret = 0;
158492 #if !defined(SQLITE_DEBUG)
158493 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
158494 #endif
158495 {
 
 
158496 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
158497 Vdbe *v = pParse->pVdbe; /* VM being constructed */
158498 sqlite3 *db = pParse->db; /* Database handle */
158499 int isSearch; /* True for a SEARCH. False for SCAN. */
158500 WhereLoop *pLoop; /* The controlling WhereLoop object */
158501 u32 flags; /* Flags that describe this loop */
 
158502 char *zMsg; /* Text to add to EQP output */
 
158503 StrAccum str; /* EQP output string */
158504 char zBuf[100]; /* Initial space for EQP output string */
158505
 
 
158506 pLoop = pLevel->pWLoop;
158507 flags = pLoop->wsFlags;
158508 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
158509
158510 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
158511 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
158512 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
158513
@@ -158527,11 +158598,11 @@
158527 }
158528 }else if( flags & WHERE_PARTIALIDX ){
158529 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
158530 }else if( flags & WHERE_AUTO_INDEX ){
158531 zFmt = "AUTOMATIC COVERING INDEX";
158532 }else if( flags & WHERE_IDX_ONLY ){
158533 zFmt = "COVERING INDEX %s";
158534 }else{
158535 zFmt = "INDEX %s";
158536 }
158537 if( zFmt ){
@@ -158579,15 +158650,54 @@
158579 sqlite3LogEstToInt(pLoop->nOut));
158580 }else{
158581 sqlite3_str_append(&str, " (~1 row)", 9);
158582 }
158583 #endif
 
158584 zMsg = sqlite3StrAccumFinish(&str);
158585 sqlite3ExplainBreakpoint("",zMsg);
158586 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
158587 pParse->addrExplain, pLoop->rRun,
158588 zMsg, P4_DYNAMIC);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158589 }
158590 return ret;
158591 }
158592
158593 /*
@@ -158682,13 +158792,14 @@
158682 if( wsFlags & WHERE_INDEXED ){
158683 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
158684 }
158685 }else{
158686 int addr;
 
158687 assert( pSrclist->a[pLvl->iFrom].fg.isSubquery );
158688 addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
158689 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
158690 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
158691 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
158692 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
158693 }
158694 }
@@ -158937,10 +159048,11 @@
158937 if( pOrigLhs ){
158938 sqlite3ExprListDelete(db, pOrigLhs);
158939 pNew->pLeft->x.pList = pLhs;
158940 }
158941 pSelect->pEList = pRhs;
 
158942 if( pLhs && pLhs->nExpr==1 ){
158943 /* Take care here not to generate a TK_VECTOR containing only a
158944 ** single value. Since the parser never creates such a vector, some
158945 ** of the subroutines do not handle this case. */
158946 Expr *p = pLhs->a[0].pExpr;
@@ -164009,11 +164121,11 @@
164009 || pTerm->pExpr->w.iJoin != pSrc->iCursor
164010 ){
164011 return 0;
164012 }
164013 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
164014 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
164015 ){
164016 return 0;
164017 }
164018 return 1;
164019 }
@@ -165502,11 +165614,11 @@
165502 return rc;
165503 }
165504 #endif /* SQLITE_ENABLE_STAT4 */
165505
165506
165507 #ifdef WHERETRACE_ENABLED
165508 /*
165509 ** Print the content of a WhereTerm object
165510 */
165511 SQLITE_PRIVATE void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){
165512 if( pTerm==0 ){
@@ -165545,10 +165657,13 @@
165545 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
165546 }
165547 sqlite3DebugPrintf("\n");
165548 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
165549 }
 
 
 
165550 }
165551 #endif
165552
165553 #ifdef WHERETRACE_ENABLED
165554 /*
@@ -166731,11 +166846,10 @@
166731 pParse = pWC->pWInfo->pParse;
166732 while( pWhere->op==TK_AND ){
166733 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
166734 pWhere = pWhere->pRight;
166735 }
166736 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
166737 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
166738 Expr *pExpr;
166739 pExpr = pTerm->pExpr;
166740 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
166741 && ((jointype & JT_OUTER)==0 || ExprHasProperty(pExpr, EP_OuterON))
@@ -169392,11 +169506,11 @@
169392 break;
169393 }
169394 }
169395 if( hasRightJoin
169396 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
169397 && pTerm->pExpr->w.iJoin==pItem->iCursor
169398 ){
169399 break; /* restriction (5) */
169400 }
169401 }
169402 if( pTerm<pEnd ) continue;
@@ -170312,10 +170426,11 @@
170312 int pc,
170313 VdbeOp *pOp
170314 ){
170315 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
170316 sqlite3VdbePrintOp(0, pc, pOp);
 
170317 }
170318 #endif
170319
170320 /*
170321 ** Generate the end of the WHERE loop. See comments on
@@ -170611,18 +170726,32 @@
170611 x = sqlite3TableColumnToIndex(pIdx, x);
170612 if( x>=0 ){
170613 pOp->p2 = x;
170614 pOp->p1 = pLevel->iIdxCur;
170615 OpcodeRewriteTrace(db, k, pOp);
170616 }else{
170617 /* Unable to translate the table reference into an index
170618 ** reference. Verify that this is harmless - that the
170619 ** table being referenced really is open.
170620 */
170621 if( pLoop->wsFlags & WHERE_IDX_ONLY ){
 
 
 
170622 sqlite3ErrorMsg(pParse, "internal query planner error");
170623 pParse->rc = SQLITE_INTERNAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170624 }
170625 }
170626 }else if( pOp->opcode==OP_Rowid ){
170627 pOp->p1 = pLevel->iIdxCur;
170628 pOp->opcode = OP_IdxRowid;
@@ -172326,10 +172455,11 @@
172326 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
172327 FuncDef *pFunc = pWin->pWFunc;
172328 int regArg;
172329 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
172330 int i;
 
172331
172332 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
172333
172334 /* All OVER clauses in the same window function aggregate step must
172335 ** be the same. */
@@ -172341,10 +172471,22 @@
172341 }else{
172342 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
172343 }
172344 }
172345 regArg = reg;
 
 
 
 
 
 
 
 
 
 
 
 
172346
172347 if( pMWin->regStartRowid==0
172348 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
172349 && (pWin->eStart!=TK_UNBOUNDED)
172350 ){
@@ -172361,29 +172503,17 @@
172361 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
172362 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
172363 }
172364 sqlite3VdbeJumpHere(v, addrIsNull);
172365 }else if( pWin->regApp ){
 
172366 assert( pFunc->zName==nth_valueName
172367 || pFunc->zName==first_valueName
172368 );
172369 assert( bInverse==0 || bInverse==1 );
172370 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
172371 }else if( pFunc->xSFunc!=noopStepFunc ){
172372 int addrIf = 0;
172373 if( pWin->pFilter ){
172374 int regTmp;
172375 assert( ExprUseXList(pWin->pOwner) );
172376 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
172377 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
172378 regTmp = sqlite3GetTempReg(pParse);
172379 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
172380 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
172381 VdbeCoverage(v);
172382 sqlite3ReleaseTempReg(pParse, regTmp);
172383 }
172384
172385 if( pWin->bExprArgs ){
172386 int iOp = sqlite3VdbeCurrentAddr(v);
172387 int iEnd;
172388
172389 assert( ExprUseXList(pWin->pOwner) );
@@ -172406,16 +172536,17 @@
172406 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
172407 }
172408 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
172409 bInverse, regArg, pWin->regAccum);
172410 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
172411 sqlite3VdbeChangeP5(v, (u8)nArg);
172412 if( pWin->bExprArgs ){
172413 sqlite3ReleaseTempRange(pParse, regArg, nArg);
172414 }
172415 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
172416 }
 
 
172417 }
172418 }
172419
172420 /*
172421 ** Values that may be passed as the second argument to windowCodeOp().
@@ -173837,10 +173968,17 @@
173837 ** Then the "b" IdList records the list "a,b,c".
173838 */
173839 struct TrigEvent { int a; IdList * b; };
173840
173841 struct FrameBound { int eType; Expr *pExpr; };
 
 
 
 
 
 
 
173842
173843 /*
173844 ** Disable lookaside memory allocation for objects that might be
173845 ** shared across database connections.
173846 */
@@ -177730,11 +177868,15 @@
177730 }
177731 break;
177732 case 84: /* cmd ::= select */
177733 {
177734 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0};
177735 sqlite3Select(pParse, yymsp[0].minor.yy555, &dest);
 
 
 
 
177736 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy555);
177737 }
177738 break;
177739 case 85: /* select ::= WITH wqlist selectnowith */
177740 {yymsp[-2].minor.yy555 = attachWithToSelect(pParse,yymsp[0].minor.yy555,yymsp[-1].minor.yy59);}
@@ -178201,11 +178343,11 @@
178201 ** that look like this: #1 #2 ... These terms refer to registers
178202 ** in the virtual machine. #N is the N-th register. */
178203 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
178204 assert( t.n>=2 );
178205 if( pParse->nested==0 ){
178206 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
178207 yymsp[0].minor.yy454 = 0;
178208 }else{
178209 yymsp[0].minor.yy454 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
178210 if( yymsp[0].minor.yy454 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy454->iTable);
178211 }
@@ -179049,11 +179191,11 @@
179049 #define TOKEN yyminor
179050 /************ Begin %syntax_error code ****************************************/
179051
179052 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
179053 if( TOKEN.z[0] ){
179054 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
179055 }else{
179056 sqlite3ErrorMsg(pParse, "incomplete input");
179057 }
179058 /************ End %syntax_error code ******************************************/
179059 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
@@ -180540,11 +180682,13 @@
180540 }
180541 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
180542 if( pParse->zErrMsg==0 ){
180543 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
180544 }
180545 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
 
 
180546 nErr++;
180547 }
180548 pParse->zTail = zSql;
180549 #ifndef SQLITE_OMIT_VIRTUALTABLE
180550 sqlite3_free(pParse->apVtabLock);
@@ -182513,14 +182657,10 @@
182513 #endif
182514
182515 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
182516 sqlite3ValueFree(db->pErr);
182517 sqlite3CloseExtensions(db);
182518 #if SQLITE_USER_AUTHENTICATION
182519 sqlite3_free(db->auth.zAuthUser);
182520 sqlite3_free(db->auth.zAuthPW);
182521 #endif
182522
182523 db->eOpenState = SQLITE_STATE_ERROR;
182524
182525 /* The temp-database schema is allocated differently from the other schema
182526 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
@@ -183951,12 +184091,12 @@
183951 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
183952 #endif
183953 #if SQLITE_MAX_VDBE_OP<40
183954 # error SQLITE_MAX_VDBE_OP must be at least 40
183955 #endif
183956 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
183957 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
183958 #endif
183959 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
183960 # error SQLITE_MAX_ATTACHED must be between 0 and 125
183961 #endif
183962 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
@@ -184019,12 +184159,12 @@
184019 }
184020 oldLimit = db->aLimit[limitId];
184021 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
184022 if( newLimit>aHardLimit[limitId] ){
184023 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
184024 }else if( newLimit<1 && limitId==SQLITE_LIMIT_LENGTH ){
184025 newLimit = 1;
184026 }
184027 db->aLimit[limitId] = newLimit;
184028 }
184029 return oldLimit; /* IMP: R-53341-35419 */
184030 }
@@ -185364,11 +185504,10 @@
185364 rc = x;
185365 #if defined(SQLITE_DEBUG)
185366 /* Invoke these debugging routines so that the compiler does not
185367 ** issue "defined but not used" warnings. */
185368 if( x==9999 ){
185369 sqlite3ShowExpr(0);
185370 sqlite3ShowExpr(0);
185371 sqlite3ShowExprList(0);
185372 sqlite3ShowIdList(0);
185373 sqlite3ShowSrcList(0);
185374 sqlite3ShowWith(0);
@@ -189796,14 +189935,19 @@
189796
189797 assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
189798 if( *p1==POS_COLUMN ){
189799 p1++;
189800 p1 += fts3GetVarint32(p1, &iCol1);
 
 
 
189801 }
189802 if( *p2==POS_COLUMN ){
189803 p2++;
189804 p2 += fts3GetVarint32(p2, &iCol2);
 
 
189805 }
189806
189807 while( 1 ){
189808 if( iCol1==iCol2 ){
189809 char *pSave = p;
@@ -192970,11 +193114,11 @@
192970 for(p=pExpr; p->pLeft; p=p->pLeft){
192971 assert( p->pRight->pPhrase->doclist.nList>0 );
192972 nTmp += p->pRight->pPhrase->doclist.nList;
192973 }
192974 nTmp += p->pPhrase->doclist.nList;
192975 aTmp = sqlite3_malloc64(nTmp*2);
192976 if( !aTmp ){
192977 *pRc = SQLITE_NOMEM;
192978 res = 0;
192979 }else{
192980 char *aPoslist = p->pPhrase->doclist.pList;
@@ -193621,11 +193765,11 @@
193621 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
193622 return SQLITE_CORRUPT_VTAB;
193623 }
193624 #endif
193625
193626 #if !SQLITE_CORE
193627 /*
193628 ** Initialize API pointer table, if required.
193629 */
193630 #ifdef _WIN32
193631 __declspec(dllexport)
@@ -194523,14 +194667,15 @@
194523 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
194524 if( rc==SQLITE_OK ){
194525 Fts3PhraseToken *pToken;
194526
194527 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
194528 if( !p ) goto no_mem;
194529
194530 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
194531 if( !zTemp ) goto no_mem;
 
 
 
194532
194533 assert( nToken==ii );
194534 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
194535 memset(pToken, 0, sizeof(Fts3PhraseToken));
194536
@@ -194541,53 +194686,51 @@
194541 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
194542 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
194543 nToken = ii+1;
194544 }
194545 }
194546
194547 pModule->xClose(pCursor);
194548 pCursor = 0;
194549 }
194550
194551 if( rc==SQLITE_DONE ){
194552 int jj;
194553 char *zBuf = 0;
194554
194555 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
194556 if( !p ) goto no_mem;
 
 
 
194557 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
194558 p->eType = FTSQUERY_PHRASE;
194559 p->pPhrase = (Fts3Phrase *)&p[1];
194560 p->pPhrase->iColumn = pParse->iDefaultCol;
194561 p->pPhrase->nToken = nToken;
194562
194563 zBuf = (char *)&p->pPhrase->aToken[nToken];
 
194564 if( zTemp ){
194565 memcpy(zBuf, zTemp, nTemp);
194566 sqlite3_free(zTemp);
194567 }else{
194568 assert( nTemp==0 );
194569 }
194570
194571 for(jj=0; jj<p->pPhrase->nToken; jj++){
194572 p->pPhrase->aToken[jj].z = zBuf;
194573 zBuf += p->pPhrase->aToken[jj].n;
194574 }
194575 rc = SQLITE_OK;
194576 }
194577
194578 *ppExpr = p;
194579 return rc;
194580 no_mem:
194581
194582 if( pCursor ){
194583 pModule->xClose(pCursor);
194584 }
194585 sqlite3_free(zTemp);
194586 sqlite3_free(p);
194587 *ppExpr = 0;
194588 return SQLITE_NOMEM;
 
 
 
194589 }
194590
194591 /*
194592 ** The output variable *ppExpr is populated with an allocated Fts3Expr
194593 ** structure, or set to 0 if the end of the input buffer is reached.
@@ -215395,12 +215538,12 @@
215395 #endif
215396 }
215397 sqlite3_str_append(pOut, "}", 1);
215398 }
215399 errCode = sqlite3_str_errcode(pOut);
215400 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
215401 sqlite3_result_error_code(ctx, errCode);
 
215402 }
215403
215404 /* This routine implements an SQL function that returns the "depth" parameter
215405 ** from the front of a blob that is an r-tree node. For example:
215406 **
@@ -217912,11 +218055,11 @@
217912 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
217913 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
217914 );
217915 }
217916
217917 #if !SQLITE_CORE
217918 #ifdef _WIN32
217919 __declspec(dllexport)
217920 #endif
217921 SQLITE_API int sqlite3_rtree_init(
217922 sqlite3 *db,
@@ -218503,11 +218646,11 @@
218503 }
218504
218505 return rc;
218506 }
218507
218508 #if !SQLITE_CORE
218509 #ifdef _WIN32
218510 __declspec(dllexport)
218511 #endif
218512 SQLITE_API int sqlite3_icu_init(
218513 sqlite3 *db,
@@ -226177,10 +226320,12 @@
226177 if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK && pData ){
226178 unsigned char *aPage = sqlite3PagerGetData(pDbPage);
226179 memcpy(aPage, pData, szPage);
226180 pTab->pgnoTrunc = 0;
226181 }
 
 
226182 }
226183 sqlite3PagerUnref(pDbPage);
226184 return rc;
226185
226186 update_fail:
@@ -226210,11 +226355,15 @@
226210 static int dbpageSync(sqlite3_vtab *pVtab){
226211 DbpageTable *pTab = (DbpageTable *)pVtab;
226212 if( pTab->pgnoTrunc>0 ){
226213 Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt;
226214 Pager *pPager = sqlite3BtreePager(pBt);
226215 sqlite3PagerTruncateImage(pPager, pTab->pgnoTrunc);
 
 
 
 
226216 }
226217 pTab->pgnoTrunc = 0;
226218 return SQLITE_OK;
226219 }
226220
@@ -232804,20 +232953,46 @@
232804 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
232805
232806 /************** End of sqlite3session.c **************************************/
232807 /************** Begin file fts5.c ********************************************/
232808
232809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232810 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
232811
232812 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
232813 # define NDEBUG 1
232814 #endif
232815 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
232816 # undef NDEBUG
232817 #endif
232818
 
 
 
 
 
 
232819 /*
232820 ** 2014 May 31
232821 **
232822 ** The author disclaims copyright to this source code. In place of
232823 ** a legal notice, here is a blessing:
@@ -233114,17 +233289,32 @@
233114 ** This is used to access token iToken of phrase hit iIdx within the
233115 ** current row. If iIdx is less than zero or greater than or equal to the
233116 ** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
233117 ** output variable (*ppToken) is set to point to a buffer containing the
233118 ** matching document token, and (*pnToken) to the size of that buffer in
233119 ** bytes. This API is not available if the specified token matches a
233120 ** prefix query term. In that case both output variables are always set
233121 ** to 0.
233122 **
233123 ** The output text is not a copy of the document text that was tokenized.
233124 ** It is the output of the tokenizer module. For tokendata=1 tables, this
233125 ** includes any embedded 0x00 and trailing data.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233126 **
233127 ** This API can be quite slow if used with an FTS5 table created with the
233128 ** "detail=none" or "detail=column" option.
233129 **
233130 ** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale)
@@ -233803,11 +233993,12 @@
233803 int nUsermerge; /* 'usermerge' setting */
233804 int nHashSize; /* Bytes of memory for in-memory hash */
233805 char *zRank; /* Name of rank function */
233806 char *zRankArgs; /* Arguments to rank function */
233807 int bSecureDelete; /* 'secure-delete' */
233808 int nDeleteMerge; /* 'deletemerge' */
 
233809
233810 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
233811 char **pzErrmsg;
233812
233813 #ifdef SQLITE_DEBUG
@@ -234060,11 +234251,18 @@
234060 static int sqlite3Fts5StructureTest(Fts5Index*, void*);
234061
234062 /*
234063 ** Used by xInstToken():
234064 */
234065 static int sqlite3Fts5IterToken(Fts5IndexIter*, i64, int, int, const char**, int*);
 
 
 
 
 
 
 
234066
234067 /*
234068 ** Insert or remove data to or from the index. Each time a document is
234069 ** added to or removed from the index, this function is called one or more
234070 ** times.
@@ -238274,10 +238472,23 @@
238274 if( bVal<0 ){
238275 *pbBadkey = 1;
238276 }else{
238277 pConfig->bSecureDelete = (bVal ? 1 : 0);
238278 }
 
 
 
 
 
 
 
 
 
 
 
 
 
238279 }else{
238280 *pbBadkey = 1;
238281 }
238282 return rc;
238283 }
@@ -241409,11 +241620,11 @@
241409 && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
241410 ){
241411 int rc = sqlite3Fts5PoslistWriterAppend(
241412 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
241413 );
241414 if( rc==SQLITE_OK && pExpr->pConfig->bTokendata && !pT->bPrefix ){
241415 int iCol = p->iOff>>32;
241416 int iTokOff = p->iOff & 0x7FFFFFFF;
241417 rc = sqlite3Fts5IndexIterWriteTokendata(
241418 pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
241419 );
@@ -241602,19 +241813,18 @@
241602 pPhrase = pExpr->apExprPhrase[iPhrase];
241603 if( iToken<0 || iToken>=pPhrase->nTerm ){
241604 return SQLITE_RANGE;
241605 }
241606 pTerm = &pPhrase->aTerm[iToken];
241607 if( pTerm->bPrefix==0 ){
241608 if( pExpr->pConfig->bTokendata ){
241609 rc = sqlite3Fts5IterToken(
241610 pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut
241611 );
241612 }else{
241613 *ppOut = pTerm->pTerm;
241614 *pnOut = pTerm->nFullTerm;
241615 }
241616 }
241617 return rc;
241618 }
241619
241620 /*
@@ -248425,10 +248635,387 @@
248425 fts5BufferFree(&tmp);
248426 memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING);
248427 *p1 = out;
248428 }
248429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248430 static void fts5SetupPrefixIter(
248431 Fts5Index *p, /* Index to read from */
248432 int bDesc, /* True for "ORDER BY rowid DESC" */
248433 int iIdx, /* Index to scan for data */
248434 u8 *pToken, /* Buffer containing prefix to match */
@@ -248435,137 +249022,91 @@
248435 int nToken, /* Size of buffer pToken in bytes */
248436 Fts5Colset *pColset, /* Restrict matches to these columns */
248437 Fts5Iter **ppIter /* OUT: New iterator */
248438 ){
248439 Fts5Structure *pStruct;
248440 Fts5Buffer *aBuf;
248441 int nBuf = 32;
248442 int nMerge = 1;
248443
248444 void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*);
248445 void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*);
 
 
 
 
 
 
 
 
 
 
 
 
248446 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
248447 xMerge = fts5MergeRowidLists;
248448 xAppend = fts5AppendRowid;
248449 }else{
248450 nMerge = FTS5_MERGE_NLIST-1;
248451 nBuf = nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */
248452 xMerge = fts5MergePrefixLists;
248453 xAppend = fts5AppendPoslist;
248454 }
248455
248456 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
248457 pStruct = fts5StructureRead(p);
248458 assert( p->rc!=SQLITE_OK || (aBuf && pStruct) );
248459
248460 if( p->rc==SQLITE_OK ){
248461 const int flags = FTS5INDEX_QUERY_SCAN
248462 | FTS5INDEX_QUERY_SKIPEMPTY
248463 | FTS5INDEX_QUERY_NOOUTPUT;
248464 int i;
248465 i64 iLastRowid = 0;
248466 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
248467 Fts5Data *pData;
248468 Fts5Buffer doclist;
248469 int bNewTerm = 1;
248470
248471 memset(&doclist, 0, sizeof(doclist));
248472
248473 /* If iIdx is non-zero, then it is the number of a prefix-index for
248474 ** prefixes 1 character longer than the prefix being queried for. That
248475 ** index contains all the doclists required, except for the one
248476 ** corresponding to the prefix itself. That one is extracted from the
248477 ** main term index here. */
248478 if( iIdx!=0 ){
248479 int dummy = 0;
248480 const int f2 = FTS5INDEX_QUERY_SKIPEMPTY|FTS5INDEX_QUERY_NOOUTPUT;
248481 pToken[0] = FTS5_MAIN_PREFIX;
248482 fts5MultiIterNew(p, pStruct, f2, pColset, pToken, nToken, -1, 0, &p1);
248483 fts5IterSetOutputCb(&p->rc, p1);
248484 for(;