Fossil SCM

Update to the latest pre-release SQLite version (as part of the SQLite testing process). Update the makefiles to use SQLITE_ENABLE_STAT2.

drh 2011-02-16 23:47 trunk
Commit c616cef3f787d6b3f4c853a40949e375b74ac24a
+1 -1
--- src/main.mk
+++ src/main.mk
@@ -849,11 +849,11 @@
849849
$(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
850850
$(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
851851
852852
$(OBJDIR)/zip.h: $(OBJDIR)/headers
853853
$(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
854
- $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
854
+ $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
855855
856856
$(OBJDIR)/shell.o: $(SRCDIR)/shell.c
857857
$(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
858858
859859
$(OBJDIR)/th.o: $(SRCDIR)/th.c
860860
--- src/main.mk
+++ src/main.mk
@@ -849,11 +849,11 @@
849 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
850 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
851
852 $(OBJDIR)/zip.h: $(OBJDIR)/headers
853 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
854 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
855
856 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c
857 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
858
859 $(OBJDIR)/th.o: $(SRCDIR)/th.c
860
--- src/main.mk
+++ src/main.mk
@@ -849,11 +849,11 @@
849 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
850 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
851
852 $(OBJDIR)/zip.h: $(OBJDIR)/headers
853 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
854 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
855
856 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c
857 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
858
859 $(OBJDIR)/th.o: $(SRCDIR)/th.c
860
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -237,12 +237,14 @@
237237
238238
writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
239239
set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
240240
append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
241241
#append opt " -DSQLITE_ENABLE_FTS3=1"
242
+append opt " -DSQLITE_ENABLE_STAT2"
242243
append opt " -Dlocaltime=fossil_localtime"
243244
append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
245
+set SQLITE_OPTIONS $opt
244246
writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
245247
246248
writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
247249
set opt {-Dmain=sqlite3_shell}
248250
append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -440,15 +442,11 @@
440442
writeln "$s.h:\t\$(OBJDIR)/headers"
441443
}
442444
443445
444446
writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
445
-set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
446
-append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
447
-#append opt " -DSQLITE_ENABLE_FTS3=1"
448
-append opt " -Dlocaltime=fossil_localtime"
449
-append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
447
+set opt $SQLITE_OPTIONS
450448
writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
451449
452450
writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
453451
set opt {-Dmain=sqlite3_shell}
454452
append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -497,10 +495,11 @@
497495
CFLAGS = -o
498496
BCC = $(DMDIR)\bin\dmc $(CFLAGS)
499497
TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
500498
LIBS = $(DMDIR)\extra\lib\ zlib wsock32
501499
}
500
+writeln "SQLITE_OPTIONS = $SQLITE_OPTIONS\n"
502501
writeln -nonewline "SRC = "
503502
foreach s [lsort $src] {
504503
writeln -nonewline "${s}_.c "
505504
}
506505
writeln "\n"
@@ -549,17 +548,17 @@
549548
550549
version$E: $B\win\version.c
551550
$(BCC) -o$@ $**
552551
553552
$(OBJDIR)\shell$O : $(SRCDIR)\shell.c
554
- $(TCC) -o$@ -c -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
553
+ $(TCC) -o$@ -c -Dmain=sqlite3_shell $(SQLITE_OPTIONS) $**
555554
556555
$(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
557
- $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
556
+ $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
558557
559558
$(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
560
- $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
559
+ $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
561560
562561
$(OBJDIR)\th$O : $(SRCDIR)\th.c
563562
$(TCC) -o$@ -c $**
564563
565564
$(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
@@ -642,10 +641,11 @@
642641
BCC = $(CC) $(CFLAGS)
643642
TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
644643
LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
645644
LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
646645
}
646
+writeln "SQLITE_OPTIONS = $SQLITE_OPTIONS\n"
647647
writeln -nonewline "SRC = "
648648
foreach s [lsort $src] {
649649
writeln -nonewline "${s}_.c "
650650
}
651651
writeln "\n"
@@ -688,11 +688,11 @@
688688
689689
version$E: $B\win\version.c
690690
$(BCC) $**
691691
692692
$(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
693
- $(TCC) /Fo$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
693
+ $(TCC) /Fo$@ -c $(SQLITE_OPTIONS) $**
694694
695695
$(OBJDIR)\th$O : $(SRCDIR)\th.c
696696
$(TCC) /Fo$@ -c $**
697697
698698
$(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
699699
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -237,12 +237,14 @@
237
238 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
239 set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
240 append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
241 #append opt " -DSQLITE_ENABLE_FTS3=1"
 
242 append opt " -Dlocaltime=fossil_localtime"
243 append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
 
244 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
245
246 writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
247 set opt {-Dmain=sqlite3_shell}
248 append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -440,15 +442,11 @@
440 writeln "$s.h:\t\$(OBJDIR)/headers"
441 }
442
443
444 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
445 set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
446 append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
447 #append opt " -DSQLITE_ENABLE_FTS3=1"
448 append opt " -Dlocaltime=fossil_localtime"
449 append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
450 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
451
452 writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
453 set opt {-Dmain=sqlite3_shell}
454 append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -497,10 +495,11 @@
497 CFLAGS = -o
498 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
499 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
500 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
501 }
 
502 writeln -nonewline "SRC = "
503 foreach s [lsort $src] {
504 writeln -nonewline "${s}_.c "
505 }
506 writeln "\n"
@@ -549,17 +548,17 @@
549
550 version$E: $B\win\version.c
551 $(BCC) -o$@ $**
552
553 $(OBJDIR)\shell$O : $(SRCDIR)\shell.c
554 $(TCC) -o$@ -c -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
555
556 $(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
557 $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
558
559 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
560 $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
561
562 $(OBJDIR)\th$O : $(SRCDIR)\th.c
563 $(TCC) -o$@ -c $**
564
565 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
@@ -642,10 +641,11 @@
642 BCC = $(CC) $(CFLAGS)
643 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
644 LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
645 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
646 }
 
647 writeln -nonewline "SRC = "
648 foreach s [lsort $src] {
649 writeln -nonewline "${s}_.c "
650 }
651 writeln "\n"
@@ -688,11 +688,11 @@
688
689 version$E: $B\win\version.c
690 $(BCC) $**
691
692 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
693 $(TCC) /Fo$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
694
695 $(OBJDIR)\th$O : $(SRCDIR)\th.c
696 $(TCC) /Fo$@ -c $**
697
698 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
699
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -237,12 +237,14 @@
237
238 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
239 set opt {-DSQLITE_OMIT_LOAD_EXTENSION=1}
240 append opt " -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4"
241 #append opt " -DSQLITE_ENABLE_FTS3=1"
242 append opt " -DSQLITE_ENABLE_STAT2"
243 append opt " -Dlocaltime=fossil_localtime"
244 append opt " -DSQLITE_ENABLE_LOCKING_STYLE=0"
245 set SQLITE_OPTIONS $opt
246 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
247
248 writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
249 set opt {-Dmain=sqlite3_shell}
250 append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -440,15 +442,11 @@
442 writeln "$s.h:\t\$(OBJDIR)/headers"
443 }
444
445
446 writeln "\$(OBJDIR)/sqlite3.o:\t\$(SRCDIR)/sqlite3.c"
447 set opt $SQLITE_OPTIONS
 
 
 
 
448 writeln "\t\$(XTCC) $opt -c \$(SRCDIR)/sqlite3.c -o \$(OBJDIR)/sqlite3.o\n"
449
450 writeln "\$(OBJDIR)/shell.o:\t\$(SRCDIR)/shell.c"
451 set opt {-Dmain=sqlite3_shell}
452 append opt " -DSQLITE_OMIT_LOAD_EXTENSION=1"
@@ -497,10 +495,11 @@
495 CFLAGS = -o
496 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
497 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
498 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
499 }
500 writeln "SQLITE_OPTIONS = $SQLITE_OPTIONS\n"
501 writeln -nonewline "SRC = "
502 foreach s [lsort $src] {
503 writeln -nonewline "${s}_.c "
504 }
505 writeln "\n"
@@ -549,17 +548,17 @@
548
549 version$E: $B\win\version.c
550 $(BCC) -o$@ $**
551
552 $(OBJDIR)\shell$O : $(SRCDIR)\shell.c
553 $(TCC) -o$@ -c -Dmain=sqlite3_shell $(SQLITE_OPTIONS) $**
554
555 $(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
556 $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
557
558 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
559 $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
560
561 $(OBJDIR)\th$O : $(SRCDIR)\th.c
562 $(TCC) -o$@ -c $**
563
564 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
@@ -642,10 +641,11 @@
641 BCC = $(CC) $(CFLAGS)
642 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
643 LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
644 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
645 }
646 writeln "SQLITE_OPTIONS = $SQLITE_OPTIONS\n"
647 writeln -nonewline "SRC = "
648 foreach s [lsort $src] {
649 writeln -nonewline "${s}_.c "
650 }
651 writeln "\n"
@@ -688,11 +688,11 @@
688
689 version$E: $B\win\version.c
690 $(BCC) $**
691
692 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
693 $(TCC) /Fo$@ -c $(SQLITE_OPTIONS) $**
694
695 $(OBJDIR)\th$O : $(SRCDIR)\th.c
696 $(TCC) /Fo$@ -c $**
697
698 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
699
+101
--- src/shell.c
+++ src/shell.c
@@ -2168,10 +2168,111 @@
21682168
printf("\n");
21692169
}
21702170
}
21712171
sqlite3_free_table(azResult);
21722172
}else
2173
+
2174
+ if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
2175
+ int testctrl = -1;
2176
+ int rc = 0;
2177
+ open_db(p);
2178
+
2179
+ /* convert testctrl text option to value. allow only the first
2180
+ ** three characters of the option to be used or the numerical
2181
+ ** value. */
2182
+ if( strncmp( azArg[1], "prng_save", 6 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_SAVE;
2183
+ else if( strncmp( azArg[1], "prng_restore", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESTORE;
2184
+ else if( strncmp( azArg[1], "prng_reset", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESET;
2185
+ else if( strncmp( azArg[1], "bitvec_test", 6 )==3 ) testctrl = SQLITE_TESTCTRL_BITVEC_TEST;
2186
+ else if( strncmp( azArg[1], "fault_install", 6 )==3 ) testctrl = SQLITE_TESTCTRL_FAULT_INSTALL;
2187
+ else if( strncmp( azArg[1], "benign_malloc_hooks", 3 )==0 ) testctrl = SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS;
2188
+ else if( strncmp( azArg[1], "pending_byte", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PENDING_BYTE;
2189
+ else if( strncmp( azArg[1], "assert", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ASSERT;
2190
+ else if( strncmp( azArg[1], "always", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ALWAYS;
2191
+ else if( strncmp( azArg[1], "reserve", 3 )==0 ) testctrl = SQLITE_TESTCTRL_RESERVE;
2192
+ else if( strncmp( azArg[1], "optimizations", 3 )==0 ) testctrl = SQLITE_TESTCTRL_OPTIMIZATIONS;
2193
+ else if( strncmp( azArg[1], "iskeyword", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ISKEYWORD;
2194
+ else if( strncmp( azArg[1], "pghdrsz", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PGHDRSZ;
2195
+ else if( strncmp( azArg[1], "scratchmalloc", 3 )==0 ) testctrl = SQLITE_TESTCTRL_SCRATCHMALLOC;
2196
+ else testctrl = atoi(azArg[1]);
2197
+
2198
+ if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
2199
+ fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
2200
+ }else{
2201
+ switch(testctrl){
2202
+
2203
+ /* sqlite3_test_control(int, db, int) */
2204
+ case SQLITE_TESTCTRL_OPTIMIZATIONS:
2205
+ case SQLITE_TESTCTRL_RESERVE:
2206
+ if( nArg==3 ){
2207
+ int opt = (int)strtol(azArg[2], 0, 0);
2208
+ rc = sqlite3_test_control(testctrl, p->db, opt);
2209
+ printf("%d (0x%08x)\n", rc, rc);
2210
+ } else {
2211
+ fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]);
2212
+ }
2213
+ break;
2214
+
2215
+ /* sqlite3_test_control(int) */
2216
+ case SQLITE_TESTCTRL_PRNG_SAVE:
2217
+ case SQLITE_TESTCTRL_PRNG_RESTORE:
2218
+ case SQLITE_TESTCTRL_PRNG_RESET:
2219
+ case SQLITE_TESTCTRL_PGHDRSZ:
2220
+ if( nArg==2 ){
2221
+ rc = sqlite3_test_control(testctrl);
2222
+ printf("%d (0x%08x)\n", rc, rc);
2223
+ } else {
2224
+ fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
2225
+ }
2226
+ break;
2227
+
2228
+ /* sqlite3_test_control(int, uint) */
2229
+ case SQLITE_TESTCTRL_PENDING_BYTE:
2230
+ if( nArg==3 ){
2231
+ unsigned int opt = (unsigned int)atoi(azArg[2]);
2232
+ rc = sqlite3_test_control(testctrl, opt);
2233
+ printf("%d (0x%08x)\n", rc, rc);
2234
+ } else {
2235
+ fprintf(stderr,"Error: testctrl %s takes a single unsigned int option\n", azArg[1]);
2236
+ }
2237
+ break;
2238
+
2239
+ /* sqlite3_test_control(int, int) */
2240
+ case SQLITE_TESTCTRL_ASSERT:
2241
+ case SQLITE_TESTCTRL_ALWAYS:
2242
+ if( nArg==3 ){
2243
+ int opt = atoi(azArg[2]);
2244
+ rc = sqlite3_test_control(testctrl, opt);
2245
+ printf("%d (0x%08x)\n", rc, rc);
2246
+ } else {
2247
+ fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]);
2248
+ }
2249
+ break;
2250
+
2251
+ /* sqlite3_test_control(int, char *) */
2252
+#ifdef SQLITE_N_KEYWORD
2253
+ case SQLITE_TESTCTRL_ISKEYWORD:
2254
+ if( nArg==3 ){
2255
+ const char *opt = azArg[2];
2256
+ rc = sqlite3_test_control(testctrl, opt);
2257
+ printf("%d (0x%08x)\n", rc, rc);
2258
+ } else {
2259
+ fprintf(stderr,"Error: testctrl %s takes a single char * option\n", azArg[1]);
2260
+ }
2261
+ break;
2262
+#endif
2263
+
2264
+ case SQLITE_TESTCTRL_BITVEC_TEST:
2265
+ case SQLITE_TESTCTRL_FAULT_INSTALL:
2266
+ case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
2267
+ case SQLITE_TESTCTRL_SCRATCHMALLOC:
2268
+ default:
2269
+ fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n", azArg[1]);
2270
+ break;
2271
+ }
2272
+ }
2273
+ }else
21732274
21742275
if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
21752276
open_db(p);
21762277
sqlite3_busy_timeout(p->db, atoi(azArg[1]));
21772278
}else
21782279
--- src/shell.c
+++ src/shell.c
@@ -2168,10 +2168,111 @@
2168 printf("\n");
2169 }
2170 }
2171 sqlite3_free_table(azResult);
2172 }else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2173
2174 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
2175 open_db(p);
2176 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
2177 }else
2178
--- src/shell.c
+++ src/shell.c
@@ -2168,10 +2168,111 @@
2168 printf("\n");
2169 }
2170 }
2171 sqlite3_free_table(azResult);
2172 }else
2173
2174 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
2175 int testctrl = -1;
2176 int rc = 0;
2177 open_db(p);
2178
2179 /* convert testctrl text option to value. allow only the first
2180 ** three characters of the option to be used or the numerical
2181 ** value. */
2182 if( strncmp( azArg[1], "prng_save", 6 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_SAVE;
2183 else if( strncmp( azArg[1], "prng_restore", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESTORE;
2184 else if( strncmp( azArg[1], "prng_reset", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESET;
2185 else if( strncmp( azArg[1], "bitvec_test", 6 )==3 ) testctrl = SQLITE_TESTCTRL_BITVEC_TEST;
2186 else if( strncmp( azArg[1], "fault_install", 6 )==3 ) testctrl = SQLITE_TESTCTRL_FAULT_INSTALL;
2187 else if( strncmp( azArg[1], "benign_malloc_hooks", 3 )==0 ) testctrl = SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS;
2188 else if( strncmp( azArg[1], "pending_byte", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PENDING_BYTE;
2189 else if( strncmp( azArg[1], "assert", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ASSERT;
2190 else if( strncmp( azArg[1], "always", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ALWAYS;
2191 else if( strncmp( azArg[1], "reserve", 3 )==0 ) testctrl = SQLITE_TESTCTRL_RESERVE;
2192 else if( strncmp( azArg[1], "optimizations", 3 )==0 ) testctrl = SQLITE_TESTCTRL_OPTIMIZATIONS;
2193 else if( strncmp( azArg[1], "iskeyword", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ISKEYWORD;
2194 else if( strncmp( azArg[1], "pghdrsz", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PGHDRSZ;
2195 else if( strncmp( azArg[1], "scratchmalloc", 3 )==0 ) testctrl = SQLITE_TESTCTRL_SCRATCHMALLOC;
2196 else testctrl = atoi(azArg[1]);
2197
2198 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
2199 fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
2200 }else{
2201 switch(testctrl){
2202
2203 /* sqlite3_test_control(int, db, int) */
2204 case SQLITE_TESTCTRL_OPTIMIZATIONS:
2205 case SQLITE_TESTCTRL_RESERVE:
2206 if( nArg==3 ){
2207 int opt = (int)strtol(azArg[2], 0, 0);
2208 rc = sqlite3_test_control(testctrl, p->db, opt);
2209 printf("%d (0x%08x)\n", rc, rc);
2210 } else {
2211 fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]);
2212 }
2213 break;
2214
2215 /* sqlite3_test_control(int) */
2216 case SQLITE_TESTCTRL_PRNG_SAVE:
2217 case SQLITE_TESTCTRL_PRNG_RESTORE:
2218 case SQLITE_TESTCTRL_PRNG_RESET:
2219 case SQLITE_TESTCTRL_PGHDRSZ:
2220 if( nArg==2 ){
2221 rc = sqlite3_test_control(testctrl);
2222 printf("%d (0x%08x)\n", rc, rc);
2223 } else {
2224 fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
2225 }
2226 break;
2227
2228 /* sqlite3_test_control(int, uint) */
2229 case SQLITE_TESTCTRL_PENDING_BYTE:
2230 if( nArg==3 ){
2231 unsigned int opt = (unsigned int)atoi(azArg[2]);
2232 rc = sqlite3_test_control(testctrl, opt);
2233 printf("%d (0x%08x)\n", rc, rc);
2234 } else {
2235 fprintf(stderr,"Error: testctrl %s takes a single unsigned int option\n", azArg[1]);
2236 }
2237 break;
2238
2239 /* sqlite3_test_control(int, int) */
2240 case SQLITE_TESTCTRL_ASSERT:
2241 case SQLITE_TESTCTRL_ALWAYS:
2242 if( nArg==3 ){
2243 int opt = atoi(azArg[2]);
2244 rc = sqlite3_test_control(testctrl, opt);
2245 printf("%d (0x%08x)\n", rc, rc);
2246 } else {
2247 fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]);
2248 }
2249 break;
2250
2251 /* sqlite3_test_control(int, char *) */
2252 #ifdef SQLITE_N_KEYWORD
2253 case SQLITE_TESTCTRL_ISKEYWORD:
2254 if( nArg==3 ){
2255 const char *opt = azArg[2];
2256 rc = sqlite3_test_control(testctrl, opt);
2257 printf("%d (0x%08x)\n", rc, rc);
2258 } else {
2259 fprintf(stderr,"Error: testctrl %s takes a single char * option\n", azArg[1]);
2260 }
2261 break;
2262 #endif
2263
2264 case SQLITE_TESTCTRL_BITVEC_TEST:
2265 case SQLITE_TESTCTRL_FAULT_INSTALL:
2266 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
2267 case SQLITE_TESTCTRL_SCRATCHMALLOC:
2268 default:
2269 fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n", azArg[1]);
2270 break;
2271 }
2272 }
2273 }else
2274
2275 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
2276 open_db(p);
2277 sqlite3_busy_timeout(p->db, atoi(azArg[1]));
2278 }else
2279
+1969 -867
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.7.5. By combining all the individual C code files into this
3
+** version 3.7.6. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a one 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.
@@ -648,13 +648,13 @@
648648
**
649649
** See also: [sqlite3_libversion()],
650650
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651651
** [sqlite_version()] and [sqlite_source_id()].
652652
*/
653
-#define SQLITE_VERSION "3.7.5"
654
-#define SQLITE_VERSION_NUMBER 3007005
655
-#define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
653
+#define SQLITE_VERSION "3.7.6"
654
+#define SQLITE_VERSION_NUMBER 3007006
655
+#define SQLITE_SOURCE_ID "2011-02-16 23:32:24 31fc4ba66e76876b2e7b6b2b74c07f47571938ce"
656656
657657
/*
658658
** CAPI3REF: Run-Time Library Version Numbers
659659
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660660
**
@@ -931,11 +931,11 @@
931931
#define SQLITE_NOMEM 7 /* A malloc() failed */
932932
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
933933
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
934934
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
935935
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
936
-#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
936
+#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
937937
#define SQLITE_FULL 13 /* Insertion failed because database is full */
938938
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
939939
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
940940
#define SQLITE_EMPTY 16 /* Database is empty */
941941
#define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -1163,11 +1163,13 @@
11631163
** locking strategy (for example to use dot-file locks), to inquire
11641164
** about the status of a lock, or to break stale locks. The SQLite
11651165
** core reserves all opcodes less than 100 for its own use.
11661166
** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
11671167
** Applications that define a custom xFileControl method should use opcodes
1168
-** greater than 100 to avoid conflicts.
1168
+** greater than 100 to avoid conflicts. VFS implementations should
1169
+** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1170
+** recognize.
11691171
**
11701172
** The xSectorSize() method returns the sector size of the
11711173
** device that underlies the file. The sector size is the
11721174
** minimum write that can be performed without disturbing
11731175
** other bytes in the file. The xDeviceCharacteristics()
@@ -3210,11 +3212,11 @@
32103212
SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
32113213
32123214
/*
32133215
** CAPI3REF: Determine If An SQL Statement Writes The Database
32143216
**
3215
-** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3217
+** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
32163218
** and only if the [prepared statement] X makes no direct changes to
32173219
** the content of the database file.
32183220
**
32193221
** Note that [application-defined SQL functions] or
32203222
** [virtual tables] might change the database indirectly as a side effect.
@@ -6078,28 +6080,25 @@
60786080
** checked out.</dd>)^
60796081
**
60806082
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
60816083
** <dd>This parameter returns the number malloc attempts that were
60826084
** satisfied using lookaside memory. Only the high-water value is meaningful;
6083
-** the current value is always zero.
6084
-** checked out.</dd>)^
6085
+** the current value is always zero.)^
60856086
**
60866087
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
60876088
** <dd>This parameter returns the number malloc attempts that might have
60886089
** been satisfied using lookaside memory but failed due to the amount of
60896090
** memory requested being larger than the lookaside slot size.
60906091
** Only the high-water value is meaningful;
6091
-** the current value is always zero.
6092
-** checked out.</dd>)^
6092
+** the current value is always zero.)^
60936093
**
60946094
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
60956095
** <dd>This parameter returns the number malloc attempts that might have
60966096
** been satisfied using lookaside memory but failed due to all lookaside
60976097
** memory already being in use.
60986098
** Only the high-water value is meaningful;
6099
-** the current value is always zero.
6100
-** checked out.</dd>)^
6099
+** the current value is always zero.)^
61016100
**
61026101
** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
61036102
** <dd>This parameter returns the approximate number of of bytes of heap
61046103
** memory used by all pager caches associated with the database connection.)^
61056104
** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6794,12 +6793,105 @@
67946793
**
67956794
** ^The [wal_checkpoint pragma] can be used to invoke this interface
67966795
** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
67976796
** [wal_autocheckpoint pragma] can be used to cause this interface to be
67986797
** run whenever the WAL reaches a certain size threshold.
6798
+**
6799
+** See also: [sqlite3_wal_checkpoint_v2()]
67996800
*/
68006801
SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6802
+
6803
+/*
6804
+** CAPI3REF: Checkpoint a database
6805
+**
6806
+** Run a checkpoint operation on WAL database zDb attached to database
6807
+** handle db. The specific operation is determined by the value of the
6808
+** eMode parameter:
6809
+**
6810
+** <dl>
6811
+** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
6812
+** Checkpoint as many frames as possible without waiting for any database
6813
+** readers or writers to finish. Sync the db file if all frames in the log
6814
+** are checkpointed. This mode is the same as calling
6815
+** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
6816
+**
6817
+** <dt>SQLITE_CHECKPOINT_FULL<dd>
6818
+** This mode blocks (calls the busy-handler callback) until there is no
6819
+** database writer and all readers are reading from the most recent database
6820
+** snapshot. It then checkpoints all frames in the log file and syncs the
6821
+** database file. This call blocks database writers while it is running,
6822
+** but not database readers.
6823
+**
6824
+** <dt>SQLITE_CHECKPOINT_RESTART<dd>
6825
+** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
6826
+** checkpointing the log file it blocks (calls the busy-handler callback)
6827
+** until all readers are reading from the database file only. This ensures
6828
+** that the next client to write to the database file restarts the log file
6829
+** from the beginning. This call blocks database writers while it is running,
6830
+** but not database readers.
6831
+** </dl>
6832
+**
6833
+** If pnLog is not NULL, then *pnLog is set to the total number of frames in
6834
+** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
6835
+** the total number of checkpointed frames (including any that were already
6836
+** checkpointed when this function is called). *pnLog and *pnCkpt may be
6837
+** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
6838
+** If no values are available because of an error, they are both set to -1
6839
+** before returning to communicate this to the caller.
6840
+**
6841
+** All calls obtain an exclusive "checkpoint" lock on the database file. If
6842
+** any other process is running a checkpoint operation at the same time, the
6843
+** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
6844
+** busy-handler configured, it will not be invoked in this case.
6845
+**
6846
+** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
6847
+** "writer" lock on the database file. If the writer lock cannot be obtained
6848
+** immediately, and a busy-handler is configured, it is invoked and the writer
6849
+** lock retried until either the busy-handler returns 0 or the lock is
6850
+** successfully obtained. The busy-handler is also invoked while waiting for
6851
+** database readers as described above. If the busy-handler returns 0 before
6852
+** the writer lock is obtained or while waiting for database readers, the
6853
+** checkpoint operation proceeds from that point in the same way as
6854
+** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
6855
+** without blocking any further. SQLITE_BUSY is returned in this case.
6856
+**
6857
+** If parameter zDb is NULL or points to a zero length string, then the
6858
+** specified operation is attempted on all WAL databases. In this case the
6859
+** values written to output parameters *pnLog and *pnCkpt are undefined. If
6860
+** an SQLITE_BUSY error is encountered when processing one or more of the
6861
+** attached WAL databases, the operation is still attempted on any remaining
6862
+** attached databases and SQLITE_BUSY is returned to the caller. If any other
6863
+** error occurs while processing an attached database, processing is abandoned
6864
+** and the error code returned to the caller immediately. If no error
6865
+** (SQLITE_BUSY or otherwise) is encountered while processing the attached
6866
+** databases, SQLITE_OK is returned.
6867
+**
6868
+** If database zDb is the name of an attached database that is not in WAL
6869
+** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
6870
+** zDb is not NULL (or a zero length string) and is not the name of any
6871
+** attached database, SQLITE_ERROR is returned to the caller.
6872
+*/
6873
+SQLITE_API int sqlite3_wal_checkpoint_v2(
6874
+ sqlite3 *db, /* Database handle */
6875
+ const char *zDb, /* Name of attached database (or NULL) */
6876
+ int eMode, /* SQLITE_CHECKPOINT_* value */
6877
+ int *pnLog, /* OUT: Size of WAL log in frames */
6878
+ int *pnCkpt /* OUT: Total number of frames checkpointed */
6879
+);
6880
+
6881
+/*
6882
+** CAPI3REF: Checkpoint operation parameters
6883
+**
6884
+** These constants can be used as the 3rd parameter to
6885
+** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
6886
+** documentation for additional information about the meaning and use of
6887
+** each of these values.
6888
+*/
6889
+#define SQLITE_CHECKPOINT_PASSIVE 0
6890
+#define SQLITE_CHECKPOINT_FULL 1
6891
+#define SQLITE_CHECKPOINT_RESTART 2
6892
+
68016893
68026894
/*
68036895
** Undo the hack that converts floating point types to integer for
68046896
** builds on processors without floating point support.
68056897
*/
@@ -7683,11 +7775,11 @@
76837775
SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
76847776
SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
76857777
#endif
76867778
76877779
#ifndef SQLITE_OMIT_WAL
7688
-SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*);
7780
+SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
76897781
#endif
76907782
76917783
/*
76927784
** If we are not using shared cache, then there is no need to
76937785
** use mutexes to access the BtShared structures. So make the
@@ -8293,11 +8385,11 @@
82938385
SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
82948386
SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
82958387
SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
82968388
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
82978389
8298
-SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager);
8390
+SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
82998391
SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
83008392
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
83018393
SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
83028394
SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
83038395
@@ -11250,11 +11342,11 @@
1125011342
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
1125111343
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
1125211344
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
1125311345
SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
1125411346
SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11255
-SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int);
11347
+SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
1125611348
SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
1125711349
1125811350
/* Declarations for functions in fkey.c. All of these are replaced by
1125911351
** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
1126011352
** key functionality is available. If OMIT_TRIGGER is defined but
@@ -16933,11 +17025,11 @@
1693317025
** <li> SQLITE_MUTEX_STATIC_MASTER
1693417026
** <li> SQLITE_MUTEX_STATIC_MEM
1693517027
** <li> SQLITE_MUTEX_STATIC_MEM2
1693617028
** <li> SQLITE_MUTEX_STATIC_PRNG
1693717029
** <li> SQLITE_MUTEX_STATIC_LRU
16938
-** <li> SQLITE_MUTEX_STATIC_LRU2
17030
+** <li> SQLITE_MUTEX_STATIC_PMEM
1693917031
** </ul>
1694017032
**
1694117033
** The first two constants cause sqlite3_mutex_alloc() to create
1694217034
** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
1694317035
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -17343,11 +17435,11 @@
1734317435
** <li> SQLITE_MUTEX_STATIC_MASTER
1734417436
** <li> SQLITE_MUTEX_STATIC_MEM
1734517437
** <li> SQLITE_MUTEX_STATIC_MEM2
1734617438
** <li> SQLITE_MUTEX_STATIC_PRNG
1734717439
** <li> SQLITE_MUTEX_STATIC_LRU
17348
-** <li> SQLITE_MUTEX_STATIC_LRU2
17440
+** <li> SQLITE_MUTEX_STATIC_PMEM
1734917441
** </ul>
1735017442
**
1735117443
** The first two constants cause sqlite3_mutex_alloc() to create
1735217444
** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
1735317445
** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -22268,11 +22360,11 @@
2226822360
OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
2226922361
((os2File*)id)->h, ((os2File*)id)->locktype ));
2227022362
return SQLITE_OK;
2227122363
}
2227222364
}
22273
- return SQLITE_ERROR;
22365
+ return SQLITE_NOTFOUND;
2227422366
}
2227522367
2227622368
/*
2227722369
** Return the sector size in bytes of the underlying block device for
2227822370
** the specified file. This is almost always 512 bytes, but may be
@@ -26204,12 +26296,15 @@
2620426296
case SQLITE_SET_LOCKPROXYFILE:
2620526297
case SQLITE_GET_LOCKPROXYFILE: {
2620626298
return proxyFileControl(id,op,pArg);
2620726299
}
2620826300
#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
26301
+ case SQLITE_FCNTL_SYNC_OMITTED: {
26302
+ return SQLITE_OK; /* A no-op */
26303
+ }
2620926304
}
26210
- return SQLITE_ERROR;
26305
+ return SQLITE_NOTFOUND;
2621126306
}
2621226307
2621326308
/*
2621426309
** Return the sector size in bytes of the underlying block device for
2621526310
** the specified file. This is almost always 512 bytes, but may be
@@ -30792,12 +30887,15 @@
3079230887
SimulateIOErrorBenign(1);
3079330888
winTruncate(id, sz);
3079430889
SimulateIOErrorBenign(0);
3079530890
return SQLITE_OK;
3079630891
}
30892
+ case SQLITE_FCNTL_SYNC_OMITTED: {
30893
+ return SQLITE_OK;
30894
+ }
3079730895
}
30798
- return SQLITE_ERROR;
30896
+ return SQLITE_NOTFOUND;
3079930897
}
3080030898
3080130899
/*
3080230900
** Return the sector size in bytes of the underlying block device for
3080330901
** the specified file. This is almost always 512 bytes, but may be
@@ -33465,11 +33563,11 @@
3346533563
PGroup *pGroup; /* PGroup this cache belongs to */
3346633564
int szPage; /* Size of allocated pages in bytes */
3346733565
int bPurgeable; /* True if cache is purgeable */
3346833566
unsigned int nMin; /* Minimum number of pages reserved */
3346933567
unsigned int nMax; /* Configured "cache_size" value */
33470
- unsigned int mxPinned; /* nMax*9/10 */
33568
+ unsigned int n90pct; /* nMax*9/10 */
3347133569
3347233570
/* Hash table of all pages. The following variables may only be accessed
3347333571
** when the accessor is holding the PGroup mutex.
3347433572
*/
3347533573
unsigned int nRecyclable; /* Number of pages in the LRU list */
@@ -33619,11 +33717,13 @@
3361933717
** it from sqlite3Malloc instead.
3362033718
*/
3362133719
p = sqlite3Malloc(nByte);
3362233720
if( p ){
3362333721
int sz = sqlite3MallocSize(p);
33722
+ sqlite3_mutex_enter(pcache1.mutex);
3362433723
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
33724
+ sqlite3_mutex_leave(pcache1.mutex);
3362533725
}
3362633726
sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
3362733727
}
3362833728
return p;
3362933729
}
@@ -33647,11 +33747,13 @@
3364733747
}else{
3364833748
int iSize;
3364933749
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
3365033750
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
3365133751
iSize = sqlite3MallocSize(p);
33752
+ sqlite3_mutex_enter(pcache1.mutex);
3365233753
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
33754
+ sqlite3_mutex_leave(pcache1.mutex);
3365333755
sqlite3_free(p);
3365433756
}
3365533757
}
3365633758
3365733759
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -33990,11 +34092,11 @@
3399034092
PGroup *pGroup = pCache->pGroup;
3399134093
pcache1EnterMutex(pGroup);
3399234094
pGroup->nMaxPage += (nMax - pCache->nMax);
3399334095
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
3399434096
pCache->nMax = nMax;
33995
- pCache->mxPinned = nMax*9/10;
34097
+ pCache->n90pct = pCache->nMax*9/10;
3399634098
pcache1EnforceMaxPage(pGroup);
3399734099
pcache1LeaveMutex(pGroup);
3399834100
}
3399934101
}
3400034102
@@ -34100,15 +34202,16 @@
3410034202
#endif
3410134203
3410234204
3410334205
/* Step 3: Abort if createFlag is 1 but the cache is nearly full */
3410434206
nPinned = pCache->nPage - pCache->nRecyclable;
34207
+ assert( nPinned>=0 );
3410534208
assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
34106
- assert( pCache->mxPinned == pCache->nMax*9/10 );
34209
+ assert( pCache->n90pct == pCache->nMax*9/10 );
3410734210
if( createFlag==1 && (
3410834211
nPinned>=pGroup->mxPinned
34109
- || nPinned>=(int)pCache->mxPinned
34212
+ || nPinned>=(int)pCache->n90pct
3411034213
|| pcache1UnderMemoryPressure(pCache)
3411134214
)){
3411234215
goto fetch_out;
3411334216
}
3411434217
@@ -34820,26 +34923,26 @@
3482034923
#ifndef _WAL_H_
3482134924
#define _WAL_H_
3482234925
3482334926
3482434927
#ifdef SQLITE_OMIT_WAL
34825
-# define sqlite3WalOpen(x,y,z) 0
34826
-# define sqlite3WalClose(w,x,y,z) 0
34827
-# define sqlite3WalBeginReadTransaction(y,z) 0
34928
+# define sqlite3WalOpen(x,y,z) 0
34929
+# define sqlite3WalClose(w,x,y,z) 0
34930
+# define sqlite3WalBeginReadTransaction(y,z) 0
3482834931
# define sqlite3WalEndReadTransaction(z)
34829
-# define sqlite3WalRead(v,w,x,y,z) 0
34830
-# define sqlite3WalDbsize(y) 0
34831
-# define sqlite3WalBeginWriteTransaction(y) 0
34832
-# define sqlite3WalEndWriteTransaction(x) 0
34833
-# define sqlite3WalUndo(x,y,z) 0
34932
+# define sqlite3WalRead(v,w,x,y,z) 0
34933
+# define sqlite3WalDbsize(y) 0
34934
+# define sqlite3WalBeginWriteTransaction(y) 0
34935
+# define sqlite3WalEndWriteTransaction(x) 0
34936
+# define sqlite3WalUndo(x,y,z) 0
3483434937
# define sqlite3WalSavepoint(y,z)
34835
-# define sqlite3WalSavepointUndo(y,z) 0
34836
-# define sqlite3WalFrames(u,v,w,x,y,z) 0
34837
-# define sqlite3WalCheckpoint(u,v,w,x) 0
34838
-# define sqlite3WalCallback(z) 0
34839
-# define sqlite3WalExclusiveMode(y,z) 0
34840
-# define sqlite3WalHeapMemory(z) 0
34938
+# define sqlite3WalSavepointUndo(y,z) 0
34939
+# define sqlite3WalFrames(u,v,w,x,y,z) 0
34940
+# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
34941
+# define sqlite3WalCallback(z) 0
34942
+# define sqlite3WalExclusiveMode(y,z) 0
34943
+# define sqlite3WalHeapMemory(z) 0
3484134944
#else
3484234945
3484334946
#define WAL_SAVEPOINT_NDATA 4
3484434947
3484534948
/* Connection to a write-ahead log (WAL) file.
@@ -34886,13 +34989,18 @@
3488634989
SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
3488734990
3488834991
/* Copy pages from the log to the database file */
3488934992
SQLITE_PRIVATE int sqlite3WalCheckpoint(
3489034993
Wal *pWal, /* Write-ahead log connection */
34994
+ int eMode, /* One of PASSIVE, FULL and RESTART */
34995
+ int (*xBusy)(void*), /* Function to call when busy */
34996
+ void *pBusyArg, /* Context argument for xBusyHandler */
3489134997
int sync_flags, /* Flags to sync db file with (or 0) */
3489234998
int nBuf, /* Size of buffer nBuf */
34893
- u8 *zBuf /* Temporary buffer to use */
34999
+ u8 *zBuf, /* Temporary buffer to use */
35000
+ int *pnLog, /* OUT: Number of frames in WAL */
35001
+ int *pnCkpt /* OUT: Number of backfilled frames in WAL */
3489435002
);
3489535003
3489635004
/* Return the value to pass to a sqlite3_wal_hook callback, the
3489735005
** number of frames in the WAL at the point of the last commit since
3489835006
** sqlite3WalCallback() was called. If no commits have occurred since
@@ -37370,19 +37478,25 @@
3737037478
3737137479
if( isOpen(pPager->fd)
3737237480
&& (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
3737337481
){
3737437482
i64 currentSize, newSize;
37483
+ int szPage = pPager->pageSize;
3737537484
assert( pPager->eLock==EXCLUSIVE_LOCK );
3737637485
/* TODO: Is it safe to use Pager.dbFileSize here? */
3737737486
rc = sqlite3OsFileSize(pPager->fd, &currentSize);
37378
- newSize = pPager->pageSize*(i64)nPage;
37487
+ newSize = szPage*(i64)nPage;
3737937488
if( rc==SQLITE_OK && currentSize!=newSize ){
3738037489
if( currentSize>newSize ){
3738137490
rc = sqlite3OsTruncate(pPager->fd, newSize);
3738237491
}else{
37383
- rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
37492
+ char *pTmp = pPager->pTmpSpace;
37493
+ memset(pTmp, 0, szPage);
37494
+ testcase( (newSize-szPage) < currentSize );
37495
+ testcase( (newSize-szPage) == currentSize );
37496
+ testcase( (newSize-szPage) > currentSize );
37497
+ rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
3738437498
}
3738537499
if( rc==SQLITE_OK ){
3738637500
pPager->dbFileSize = nPage;
3738737501
}
3738837502
}
@@ -37737,10 +37851,32 @@
3773737851
PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
3773837852
PAGERID(pPager), pgno, pager_pagehash(pPg)));
3773937853
3774037854
return rc;
3774137855
}
37856
+
37857
+/*
37858
+** Update the value of the change-counter at offsets 24 and 92 in
37859
+** the header and the sqlite version number at offset 96.
37860
+**
37861
+** This is an unconditional update. See also the pager_incr_changecounter()
37862
+** routine which only updates the change-counter if the update is actually
37863
+** needed, as determined by the pPager->changeCountDone state variable.
37864
+*/
37865
+static void pager_write_changecounter(PgHdr *pPg){
37866
+ u32 change_counter;
37867
+
37868
+ /* Increment the value just read and write it back to byte 24. */
37869
+ change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
37870
+ put32bits(((char*)pPg->pData)+24, change_counter);
37871
+
37872
+ /* Also store the SQLite version number in bytes 96..99 and in
37873
+ ** bytes 92..95 store the change counter for which the version number
37874
+ ** is valid. */
37875
+ put32bits(((char*)pPg->pData)+92, change_counter);
37876
+ put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
37877
+}
3774237878
3774337879
#ifndef SQLITE_OMIT_WAL
3774437880
/*
3774537881
** This function is invoked once for each page that has already been
3774637882
** written into the log file when a WAL transaction is rolled back.
@@ -37808,38 +37944,15 @@
3780837944
}
3780937945
3781037946
return rc;
3781137947
}
3781237948
37813
-
37814
-/*
37815
-** Update the value of the change-counter at offsets 24 and 92 in
37816
-** the header and the sqlite version number at offset 96.
37817
-**
37818
-** This is an unconditional update. See also the pager_incr_changecounter()
37819
-** routine which only updates the change-counter if the update is actually
37820
-** needed, as determined by the pPager->changeCountDone state variable.
37821
-*/
37822
-static void pager_write_changecounter(PgHdr *pPg){
37823
- u32 change_counter;
37824
-
37825
- /* Increment the value just read and write it back to byte 24. */
37826
- change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
37827
- put32bits(((char*)pPg->pData)+24, change_counter);
37828
-
37829
- /* Also store the SQLite version number in bytes 96..99 and in
37830
- ** bytes 92..95 store the change counter for which the version number
37831
- ** is valid. */
37832
- put32bits(((char*)pPg->pData)+92, change_counter);
37833
- put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
37834
-}
37835
-
3783637949
/*
3783737950
** This function is a wrapper around sqlite3WalFrames(). As well as logging
3783837951
** the contents of the list of pages headed by pList (connected by pDirty),
3783937952
** this function notifies any active backup processes that the pages have
37840
-** changed.
37953
+** changed.
3784137954
**
3784237955
** The list of pages passed into this routine is always sorted by page number.
3784337956
** Hence, if page 1 appears anywhere on the list, it will be the first page.
3784437957
*/
3784537958
static int pagerWalFrames(
@@ -41487,18 +41600,24 @@
4148741600
return &pPager->pBackup;
4148841601
}
4148941602
4149041603
#ifndef SQLITE_OMIT_WAL
4149141604
/*
41492
-** This function is called when the user invokes "PRAGMA checkpoint".
41605
+** This function is called when the user invokes "PRAGMA wal_checkpoint",
41606
+** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
41607
+** or wal_blocking_checkpoint() API functions.
41608
+**
41609
+** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
4149341610
*/
41494
-SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager){
41611
+SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
4149541612
int rc = SQLITE_OK;
4149641613
if( pPager->pWal ){
41497
- u8 *zBuf = (u8 *)pPager->pTmpSpace;
41498
- rc = sqlite3WalCheckpoint(pPager->pWal, pPager->ckptSyncFlags,
41499
- pPager->pageSize, zBuf);
41614
+ rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
41615
+ pPager->xBusyHandler, pPager->pBusyHandlerArg,
41616
+ pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
41617
+ pnLog, pnCkpt
41618
+ );
4150041619
}
4150141620
return rc;
4150241621
}
4150341622
4150441623
SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -43233,10 +43352,38 @@
4323343352
walIteratorFree(p);
4323443353
}
4323543354
*pp = p;
4323643355
return rc;
4323743356
}
43357
+
43358
+/*
43359
+** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
43360
+** n. If the attempt fails and parameter xBusy is not NULL, then it is a
43361
+** busy-handler function. Invoke it and retry the lock until either the
43362
+** lock is successfully obtained or the busy-handler returns 0.
43363
+*/
43364
+static int walBusyLock(
43365
+ Wal *pWal, /* WAL connection */
43366
+ int (*xBusy)(void*), /* Function to call when busy */
43367
+ void *pBusyArg, /* Context argument for xBusyHandler */
43368
+ int lockIdx, /* Offset of first byte to lock */
43369
+ int n /* Number of bytes to lock */
43370
+){
43371
+ int rc;
43372
+ do {
43373
+ rc = walLockExclusive(pWal, lockIdx, n);
43374
+ }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
43375
+ return rc;
43376
+}
43377
+
43378
+/*
43379
+** The cache of the wal-index header must be valid to call this function.
43380
+** Return the page-size in bytes used by the database.
43381
+*/
43382
+static int walPagesize(Wal *pWal){
43383
+ return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
43384
+}
4323843385
4323943386
/*
4324043387
** Copy as much content as we can from the WAL back into the database file
4324143388
** in response to an sqlite3_wal_checkpoint() request or the equivalent.
4324243389
**
@@ -43267,12 +43414,14 @@
4326743414
** checkpoint is running (in any other thread or process) at the same
4326843415
** time.
4326943416
*/
4327043417
static int walCheckpoint(
4327143418
Wal *pWal, /* Wal connection */
43419
+ int eMode, /* One of PASSIVE, FULL or RESTART */
43420
+ int (*xBusyCall)(void*), /* Function to call when busy */
43421
+ void *pBusyArg, /* Context argument for xBusyHandler */
4327243422
int sync_flags, /* Flags for OsSync() (or 0) */
43273
- int nBuf, /* Size of zBuf in bytes */
4327443423
u8 *zBuf /* Temporary buffer to use */
4327543424
){
4327643425
int rc; /* Return code */
4327743426
int szPage; /* Database page-size */
4327843427
WalIterator *pIter = 0; /* Wal iterator context */
@@ -43280,12 +43429,13 @@
4328043429
u32 iFrame = 0; /* Wal frame containing data for iDbpage */
4328143430
u32 mxSafeFrame; /* Max frame that can be backfilled */
4328243431
u32 mxPage; /* Max database page to write */
4328343432
int i; /* Loop counter */
4328443433
volatile WalCkptInfo *pInfo; /* The checkpoint status information */
43434
+ int (*xBusy)(void*) = 0; /* Function to call when waiting for locks */
4328543435
43286
- szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
43436
+ szPage = walPagesize(pWal);
4328743437
testcase( szPage<=32768 );
4328843438
testcase( szPage>=65536 );
4328943439
pInfo = walCkptInfo(pWal);
4329043440
if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
4329143441
@@ -43294,15 +43444,12 @@
4329443444
if( rc!=SQLITE_OK ){
4329543445
return rc;
4329643446
}
4329743447
assert( pIter );
4329843448
43299
- /*** TODO: Move this test out to the caller. Make it an assert() here ***/
43300
- if( szPage!=nBuf ){
43301
- rc = SQLITE_CORRUPT_BKPT;
43302
- goto walcheckpoint_out;
43303
- }
43449
+ mxPage = pWal->hdr.nPage;
43450
+ if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
4330443451
4330543452
/* Compute in mxSafeFrame the index of the last frame of the WAL that is
4330643453
** safe to write into the database. Frames beyond mxSafeFrame might
4330743454
** overwrite database pages that are in use by active readers and thus
4330843455
** cannot be backfilled from the WAL.
@@ -43309,26 +43456,27 @@
4330943456
*/
4331043457
mxSafeFrame = pWal->hdr.mxFrame;
4331143458
mxPage = pWal->hdr.nPage;
4331243459
for(i=1; i<WAL_NREADER; i++){
4331343460
u32 y = pInfo->aReadMark[i];
43314
- if( mxSafeFrame>=y ){
43461
+ if( mxSafeFrame>y ){
4331543462
assert( y<=pWal->hdr.mxFrame );
43316
- rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
43463
+ rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
4331743464
if( rc==SQLITE_OK ){
4331843465
pInfo->aReadMark[i] = READMARK_NOT_USED;
4331943466
walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
4332043467
}else if( rc==SQLITE_BUSY ){
4332143468
mxSafeFrame = y;
43469
+ xBusy = 0;
4332243470
}else{
4332343471
goto walcheckpoint_out;
4332443472
}
4332543473
}
4332643474
}
4332743475
4332843476
if( pInfo->nBackfill<mxSafeFrame
43329
- && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
43477
+ && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
4333043478
){
4333143479
i64 nSize; /* Current size of database file */
4333243480
u32 nBackfill = pInfo->nBackfill;
4333343481
4333443482
/* Sync the WAL to disk */
@@ -43377,16 +43525,35 @@
4337743525
}
4337843526
}
4337943527
4338043528
/* Release the reader lock held while backfilling */
4338143529
walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
43382
- }else if( rc==SQLITE_BUSY ){
43530
+ }
43531
+
43532
+ if( rc==SQLITE_BUSY ){
4338343533
/* Reset the return code so as not to report a checkpoint failure
43384
- ** just because active readers prevent any backfill.
43385
- */
43534
+ ** just because there are active readers. */
4338643535
rc = SQLITE_OK;
4338743536
}
43537
+
43538
+ /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
43539
+ ** file has been copied into the database file, then block until all
43540
+ ** readers have finished using the wal file. This ensures that the next
43541
+ ** process to write to the database restarts the wal file.
43542
+ */
43543
+ if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
43544
+ assert( pWal->writeLock );
43545
+ if( pInfo->nBackfill<pWal->hdr.mxFrame ){
43546
+ rc = SQLITE_BUSY;
43547
+ }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
43548
+ assert( mxSafeFrame==pWal->hdr.mxFrame );
43549
+ rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
43550
+ if( rc==SQLITE_OK ){
43551
+ walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
43552
+ }
43553
+ }
43554
+ }
4338843555
4338943556
walcheckpoint_out:
4339043557
walIteratorFree(pIter);
4339143558
return rc;
4339243559
}
@@ -43415,11 +43582,13 @@
4341543582
rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
4341643583
if( rc==SQLITE_OK ){
4341743584
if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
4341843585
pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
4341943586
}
43420
- rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf);
43587
+ rc = sqlite3WalCheckpoint(
43588
+ pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
43589
+ );
4342143590
if( rc==SQLITE_OK ){
4342243591
isDelete = 1;
4342343592
}
4342443593
}
4342543594
@@ -44330,21 +44499,31 @@
4433044499
** This routine is called to implement sqlite3_wal_checkpoint() and
4433144500
** related interfaces.
4433244501
**
4433344502
** Obtain a CHECKPOINT lock and then backfill as much information as
4433444503
** we can from WAL into the database.
44504
+**
44505
+** If parameter xBusy is not NULL, it is a pointer to a busy-handler
44506
+** callback. In this case this function runs a blocking checkpoint.
4433544507
*/
4433644508
SQLITE_PRIVATE int sqlite3WalCheckpoint(
4433744509
Wal *pWal, /* Wal connection */
44510
+ int eMode, /* PASSIVE, FULL or RESTART */
44511
+ int (*xBusy)(void*), /* Function to call when busy */
44512
+ void *pBusyArg, /* Context argument for xBusyHandler */
4433844513
int sync_flags, /* Flags to sync db file with (or 0) */
4433944514
int nBuf, /* Size of temporary buffer */
44340
- u8 *zBuf /* Temporary buffer to use */
44515
+ u8 *zBuf, /* Temporary buffer to use */
44516
+ int *pnLog, /* OUT: Number of frames in WAL */
44517
+ int *pnCkpt /* OUT: Number of backfilled frames in WAL */
4434144518
){
4434244519
int rc; /* Return code */
4434344520
int isChanged = 0; /* True if a new wal-index header is loaded */
44521
+ int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
4434444522
4434544523
assert( pWal->ckptLock==0 );
44524
+ assert( pWal->writeLock==0 );
4434644525
4434744526
WALTRACE(("WAL%p: checkpoint begins\n", pWal));
4434844527
rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
4434944528
if( rc ){
4435044529
/* Usually this is SQLITE_BUSY meaning that another thread or process
@@ -44351,16 +44530,50 @@
4435144530
** is already running a checkpoint, or maybe a recovery. But it might
4435244531
** also be SQLITE_IOERR. */
4435344532
return rc;
4435444533
}
4435544534
pWal->ckptLock = 1;
44535
+
44536
+ /* If this is a blocking-checkpoint, then obtain the write-lock as well
44537
+ ** to prevent any writers from running while the checkpoint is underway.
44538
+ ** This has to be done before the call to walIndexReadHdr() below.
44539
+ **
44540
+ ** If the writer lock cannot be obtained, then a passive checkpoint is
44541
+ ** run instead. Since the checkpointer is not holding the writer lock,
44542
+ ** there is no point in blocking waiting for any readers. Assuming no
44543
+ ** other error occurs, this function will return SQLITE_BUSY to the caller.
44544
+ */
44545
+ if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
44546
+ rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
44547
+ if( rc==SQLITE_OK ){
44548
+ pWal->writeLock = 1;
44549
+ }else if( rc==SQLITE_BUSY ){
44550
+ eMode2 = SQLITE_CHECKPOINT_PASSIVE;
44551
+ rc = SQLITE_OK;
44552
+ }
44553
+ }
44554
+
44555
+ /* Read the wal-index header. */
44556
+ if( rc==SQLITE_OK ){
44557
+ rc = walIndexReadHdr(pWal, &isChanged);
44558
+ }
4435644559
4435744560
/* Copy data from the log to the database file. */
44358
- rc = walIndexReadHdr(pWal, &isChanged);
4435944561
if( rc==SQLITE_OK ){
44360
- rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf);
44562
+ if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
44563
+ rc = SQLITE_CORRUPT_BKPT;
44564
+ }else{
44565
+ rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
44566
+ }
44567
+
44568
+ /* If no error occurred, set the output variables. */
44569
+ if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
44570
+ if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
44571
+ if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
44572
+ }
4436144573
}
44574
+
4436244575
if( isChanged ){
4436344576
/* If a new wal-index header was loaded before the checkpoint was
4436444577
** performed, then the pager-cache associated with pWal is now
4436544578
** out of date. So zero the cached wal-index header to ensure that
4436644579
** next time the pager opens a snapshot on this database it knows that
@@ -44368,14 +44581,15 @@
4436844581
*/
4436944582
memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
4437044583
}
4437144584
4437244585
/* Release the locks. */
44586
+ sqlite3WalEndWriteTransaction(pWal);
4437344587
walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
4437444588
pWal->ckptLock = 0;
4437544589
WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
44376
- return rc;
44590
+ return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
4437744591
}
4437844592
4437944593
/* Return the value to pass to a sqlite3_wal_hook callback, the
4438044594
** number of frames in the WAL at the point of the last commit since
4438144595
** sqlite3WalCallback() was called. If no commits have occurred since
@@ -47851,11 +48065,11 @@
4785148065
freeTempSpace(pBt);
4785248066
rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
4785348067
pageSize-usableSize);
4785448068
return rc;
4785548069
}
47856
- if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPageHeader>nPageFile ){
48070
+ if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
4785748071
rc = SQLITE_CORRUPT_BKPT;
4785848072
goto page1_init_failed;
4785948073
}
4786048074
if( usableSize<480 ){
4786148075
goto page1_init_failed;
@@ -53402,20 +53616,22 @@
5340253616
/*
5340353617
** Run a checkpoint on the Btree passed as the first argument.
5340453618
**
5340553619
** Return SQLITE_LOCKED if this or any other connection has an open
5340653620
** transaction on the shared-cache the argument Btree is connected to.
53621
+**
53622
+** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
5340753623
*/
53408
-SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p){
53624
+SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
5340953625
int rc = SQLITE_OK;
5341053626
if( p ){
5341153627
BtShared *pBt = p->pBt;
5341253628
sqlite3BtreeEnter(p);
5341353629
if( pBt->inTransaction!=TRANS_NONE ){
5341453630
rc = SQLITE_LOCKED;
5341553631
}else{
53416
- rc = sqlite3PagerCheckpoint(pBt->pPager);
53632
+ rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
5341753633
}
5341853634
sqlite3BtreeLeave(p);
5341953635
}
5342053636
return rc;
5342153637
}
@@ -55371,10 +55587,12 @@
5537155587
pVal->u.i = -1 * pVal->u.i;
5537255588
/* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
5537355589
pVal->r = (double)-1 * pVal->r;
5537455590
sqlite3ValueApplyAffinity(pVal, affinity, enc);
5537555591
}
55592
+ }else if( op==TK_NULL ){
55593
+ pVal = sqlite3ValueNew(db);
5537655594
}
5537755595
#ifndef SQLITE_OMIT_BLOB_LITERAL
5537855596
else if( op==TK_BLOB ){
5537955597
int nVal;
5538055598
assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
@@ -57549,20 +57767,25 @@
5754957767
if( !sqlite3VtabInSync(db)
5755057768
&& db->autoCommit
5755157769
&& db->writeVdbeCnt==(p->readOnly==0)
5755257770
){
5755357771
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
57554
- if( sqlite3VdbeCheckFk(p, 1) ){
57555
- sqlite3BtreeMutexArrayLeave(&p->aMutex);
57556
- return SQLITE_ERROR;
57557
- }
57558
- /* The auto-commit flag is true, the vdbe program was successful
57559
- ** or hit an 'OR FAIL' constraint and there are no deferred foreign
57560
- ** key constraints to hold up the transaction. This means a commit
57561
- ** is required. */
57562
- rc = vdbeCommit(db, p);
57563
- if( rc==SQLITE_BUSY ){
57772
+ rc = sqlite3VdbeCheckFk(p, 1);
57773
+ if( rc!=SQLITE_OK ){
57774
+ if( NEVER(p->readOnly) ){
57775
+ sqlite3BtreeMutexArrayLeave(&p->aMutex);
57776
+ return SQLITE_ERROR;
57777
+ }
57778
+ rc = SQLITE_CONSTRAINT;
57779
+ }else{
57780
+ /* The auto-commit flag is true, the vdbe program was successful
57781
+ ** or hit an 'OR FAIL' constraint and there are no deferred foreign
57782
+ ** key constraints to hold up the transaction. This means a commit
57783
+ ** is required. */
57784
+ rc = vdbeCommit(db, p);
57785
+ }
57786
+ if( rc==SQLITE_BUSY && p->readOnly ){
5756457787
sqlite3BtreeMutexArrayLeave(&p->aMutex);
5756557788
return SQLITE_BUSY;
5756657789
}else if( rc!=SQLITE_OK ){
5756757790
p->rc = rc;
5756857791
sqlite3RollbackAll(db);
@@ -57657,11 +57880,11 @@
5765757880
if( db->autoCommit ){
5765857881
sqlite3ConnectionUnlocked(db);
5765957882
}
5766057883
5766157884
assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
57662
- return SQLITE_OK;
57885
+ return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
5766357886
}
5766457887
5766557888
5766657889
/*
5766757890
** Each VDBE holds the result of the most recent sqlite3_step() call
@@ -61075,29 +61298,34 @@
6107561298
sqlite3_value **apVal;
6107661299
} cb;
6107761300
struct OP_AggFinal_stack_vars {
6107861301
Mem *pMem;
6107961302
} cc;
61303
+ struct OP_Checkpoint_stack_vars {
61304
+ int i; /* Loop counter */
61305
+ int aRes[3]; /* Results */
61306
+ Mem *pMem; /* Write results here */
61307
+ } cd;
6108061308
struct OP_JournalMode_stack_vars {
6108161309
Btree *pBt; /* Btree to change journal mode of */
6108261310
Pager *pPager; /* Pager associated with pBt */
6108361311
int eNew; /* New journal mode */
6108461312
int eOld; /* The old journal mode */
6108561313
const char *zFilename; /* Name of database file for pPager */
61086
- } cd;
61314
+ } ce;
6108761315
struct OP_IncrVacuum_stack_vars {
6108861316
Btree *pBt;
61089
- } ce;
61317
+ } cf;
6109061318
struct OP_VBegin_stack_vars {
6109161319
VTable *pVTab;
61092
- } cf;
61320
+ } cg;
6109361321
struct OP_VOpen_stack_vars {
6109461322
VdbeCursor *pCur;
6109561323
sqlite3_vtab_cursor *pVtabCursor;
6109661324
sqlite3_vtab *pVtab;
6109761325
sqlite3_module *pModule;
61098
- } cg;
61326
+ } ch;
6109961327
struct OP_VFilter_stack_vars {
6110061328
int nArg;
6110161329
int iQuery;
6110261330
const sqlite3_module *pModule;
6110361331
Mem *pQuery;
@@ -61106,39 +61334,39 @@
6110661334
sqlite3_vtab *pVtab;
6110761335
VdbeCursor *pCur;
6110861336
int res;
6110961337
int i;
6111061338
Mem **apArg;
61111
- } ch;
61339
+ } ci;
6111261340
struct OP_VColumn_stack_vars {
6111361341
sqlite3_vtab *pVtab;
6111461342
const sqlite3_module *pModule;
6111561343
Mem *pDest;
6111661344
sqlite3_context sContext;
61117
- } ci;
61345
+ } cj;
6111861346
struct OP_VNext_stack_vars {
6111961347
sqlite3_vtab *pVtab;
6112061348
const sqlite3_module *pModule;
6112161349
int res;
6112261350
VdbeCursor *pCur;
61123
- } cj;
61351
+ } ck;
6112461352
struct OP_VRename_stack_vars {
6112561353
sqlite3_vtab *pVtab;
6112661354
Mem *pName;
61127
- } ck;
61355
+ } cl;
6112861356
struct OP_VUpdate_stack_vars {
6112961357
sqlite3_vtab *pVtab;
6113061358
sqlite3_module *pModule;
6113161359
int nArg;
6113261360
int i;
6113361361
sqlite_int64 rowid;
6113461362
Mem **apArg;
6113561363
Mem *pX;
61136
- } cl;
61364
+ } cm;
6113761365
struct OP_Trace_stack_vars {
6113861366
char *zTrace;
61139
- } cm;
61367
+ } cn;
6114061368
} u;
6114161369
/* End automatically generated code
6114261370
********************************************************************/
6114361371
6114461372
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -65894,17 +66122,42 @@
6589466122
}
6589566123
break;
6589666124
}
6589766125
6589866126
#ifndef SQLITE_OMIT_WAL
65899
-/* Opcode: Checkpoint P1 * * * *
66127
+/* Opcode: Checkpoint P1 P2 P3 * *
6590066128
**
6590166129
** Checkpoint database P1. This is a no-op if P1 is not currently in
65902
-** WAL mode.
66130
+** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
66131
+** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
66132
+** SQLITE_BUSY or not, respectively. Write the number of pages in the
66133
+** WAL after the checkpoint into mem[P3+1] and the number of pages
66134
+** in the WAL that have been checkpointed after the checkpoint
66135
+** completes into mem[P3+2]. However on an error, mem[P3+1] and
66136
+** mem[P3+2] are initialized to -1.
6590366137
*/
6590466138
case OP_Checkpoint: {
65905
- rc = sqlite3Checkpoint(db, pOp->p1);
66139
+#if 0 /* local variables moved into u.cd */
66140
+ int i; /* Loop counter */
66141
+ int aRes[3]; /* Results */
66142
+ Mem *pMem; /* Write results here */
66143
+#endif /* local variables moved into u.cd */
66144
+
66145
+ u.cd.aRes[0] = 0;
66146
+ u.cd.aRes[1] = u.cd.aRes[2] = -1;
66147
+ assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
66148
+ || pOp->p2==SQLITE_CHECKPOINT_FULL
66149
+ || pOp->p2==SQLITE_CHECKPOINT_RESTART
66150
+ );
66151
+ rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
66152
+ if( rc==SQLITE_BUSY ){
66153
+ rc = SQLITE_OK;
66154
+ u.cd.aRes[0] = 1;
66155
+ }
66156
+ for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
66157
+ sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
66158
+ }
6590666159
break;
6590766160
};
6590866161
#endif
6590966162
6591066163
#ifndef SQLITE_OMIT_PRAGMA
@@ -65918,26 +66171,26 @@
6591866171
** If changing into or out of WAL mode the procedure is more complicated.
6591966172
**
6592066173
** Write a string containing the final journal-mode to register P2.
6592166174
*/
6592266175
case OP_JournalMode: { /* out2-prerelease */
65923
-#if 0 /* local variables moved into u.cd */
66176
+#if 0 /* local variables moved into u.ce */
6592466177
Btree *pBt; /* Btree to change journal mode of */
6592566178
Pager *pPager; /* Pager associated with pBt */
6592666179
int eNew; /* New journal mode */
6592766180
int eOld; /* The old journal mode */
6592866181
const char *zFilename; /* Name of database file for pPager */
65929
-#endif /* local variables moved into u.cd */
65930
-
65931
- u.cd.eNew = pOp->p3;
65932
- assert( u.cd.eNew==PAGER_JOURNALMODE_DELETE
65933
- || u.cd.eNew==PAGER_JOURNALMODE_TRUNCATE
65934
- || u.cd.eNew==PAGER_JOURNALMODE_PERSIST
65935
- || u.cd.eNew==PAGER_JOURNALMODE_OFF
65936
- || u.cd.eNew==PAGER_JOURNALMODE_MEMORY
65937
- || u.cd.eNew==PAGER_JOURNALMODE_WAL
65938
- || u.cd.eNew==PAGER_JOURNALMODE_QUERY
66182
+#endif /* local variables moved into u.ce */
66183
+
66184
+ u.ce.eNew = pOp->p3;
66185
+ assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
66186
+ || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
66187
+ || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
66188
+ || u.ce.eNew==PAGER_JOURNALMODE_OFF
66189
+ || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
66190
+ || u.ce.eNew==PAGER_JOURNALMODE_WAL
66191
+ || u.ce.eNew==PAGER_JOURNALMODE_QUERY
6593966192
);
6594066193
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6594166194
6594266195
/* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
6594366196
** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
@@ -65956,76 +66209,76 @@
6595666209
** database. */
6595766210
sqlite3VdbeUsesBtree(p, pOp->p1);
6595866211
sqlite3VdbeMutexArrayEnter(p);
6595966212
}
6596066213
65961
- u.cd.pBt = db->aDb[pOp->p1].pBt;
65962
- u.cd.pPager = sqlite3BtreePager(u.cd.pBt);
65963
- u.cd.eOld = sqlite3PagerGetJournalMode(u.cd.pPager);
65964
- if( u.cd.eNew==PAGER_JOURNALMODE_QUERY ) u.cd.eNew = u.cd.eOld;
65965
- if( !sqlite3PagerOkToChangeJournalMode(u.cd.pPager) ) u.cd.eNew = u.cd.eOld;
66214
+ u.ce.pBt = db->aDb[pOp->p1].pBt;
66215
+ u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
66216
+ u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
66217
+ if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
66218
+ if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
6596666219
6596766220
#ifndef SQLITE_OMIT_WAL
65968
- u.cd.zFilename = sqlite3PagerFilename(u.cd.pPager);
66221
+ u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
6596966222
6597066223
/* Do not allow a transition to journal_mode=WAL for a database
6597166224
** in temporary storage or if the VFS does not support shared memory
6597266225
*/
65973
- if( u.cd.eNew==PAGER_JOURNALMODE_WAL
65974
- && (u.cd.zFilename[0]==0 /* Temp file */
65975
- || !sqlite3PagerWalSupported(u.cd.pPager)) /* No shared-memory support */
66226
+ if( u.ce.eNew==PAGER_JOURNALMODE_WAL
66227
+ && (u.ce.zFilename[0]==0 /* Temp file */
66228
+ || !sqlite3PagerWalSupported(u.ce.pPager)) /* No shared-memory support */
6597666229
){
65977
- u.cd.eNew = u.cd.eOld;
66230
+ u.ce.eNew = u.ce.eOld;
6597866231
}
6597966232
65980
- if( (u.cd.eNew!=u.cd.eOld)
65981
- && (u.cd.eOld==PAGER_JOURNALMODE_WAL || u.cd.eNew==PAGER_JOURNALMODE_WAL)
66233
+ if( (u.ce.eNew!=u.ce.eOld)
66234
+ && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
6598266235
){
6598366236
if( !db->autoCommit || db->activeVdbeCnt>1 ){
6598466237
rc = SQLITE_ERROR;
6598566238
sqlite3SetString(&p->zErrMsg, db,
6598666239
"cannot change %s wal mode from within a transaction",
65987
- (u.cd.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
66240
+ (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6598866241
);
6598966242
break;
6599066243
}else{
6599166244
65992
- if( u.cd.eOld==PAGER_JOURNALMODE_WAL ){
66245
+ if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
6599366246
/* If leaving WAL mode, close the log file. If successful, the call
6599466247
** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6599566248
** file. An EXCLUSIVE lock may still be held on the database file
6599666249
** after a successful return.
6599766250
*/
65998
- rc = sqlite3PagerCloseWal(u.cd.pPager);
66251
+ rc = sqlite3PagerCloseWal(u.ce.pPager);
6599966252
if( rc==SQLITE_OK ){
66000
- sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
66253
+ sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
6600166254
}
66002
- }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){
66255
+ }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
6600366256
/* Cannot transition directly from MEMORY to WAL. Use mode OFF
6600466257
** as an intermediate */
66005
- sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF);
66258
+ sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
6600666259
}
6600766260
6600866261
/* Open a transaction on the database file. Regardless of the journal
6600966262
** mode, this transaction always uses a rollback journal.
6601066263
*/
66011
- assert( sqlite3BtreeIsInTrans(u.cd.pBt)==0 );
66264
+ assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
6601266265
if( rc==SQLITE_OK ){
66013
- rc = sqlite3BtreeSetVersion(u.cd.pBt, (u.cd.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
66266
+ rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6601466267
}
6601566268
}
6601666269
}
6601766270
#endif /* ifndef SQLITE_OMIT_WAL */
6601866271
6601966272
if( rc ){
66020
- u.cd.eNew = u.cd.eOld;
66273
+ u.ce.eNew = u.ce.eOld;
6602166274
}
66022
- u.cd.eNew = sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
66275
+ u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
6602366276
6602466277
pOut = &aMem[pOp->p2];
6602566278
pOut->flags = MEM_Str|MEM_Static|MEM_Term;
66026
- pOut->z = (char *)sqlite3JournalModename(u.cd.eNew);
66279
+ pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
6602766280
pOut->n = sqlite3Strlen30(pOut->z);
6602866281
pOut->enc = SQLITE_UTF8;
6602966282
sqlite3VdbeChangeEncoding(pOut, encoding);
6603066283
break;
6603166284
};
@@ -66050,18 +66303,18 @@
6605066303
** Perform a single step of the incremental vacuum procedure on
6605166304
** the P1 database. If the vacuum has finished, jump to instruction
6605266305
** P2. Otherwise, fall through to the next instruction.
6605366306
*/
6605466307
case OP_IncrVacuum: { /* jump */
66055
-#if 0 /* local variables moved into u.ce */
66308
+#if 0 /* local variables moved into u.cf */
6605666309
Btree *pBt;
66057
-#endif /* local variables moved into u.ce */
66310
+#endif /* local variables moved into u.cf */
6605866311
6605966312
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6606066313
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66061
- u.ce.pBt = db->aDb[pOp->p1].pBt;
66062
- rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
66314
+ u.cf.pBt = db->aDb[pOp->p1].pBt;
66315
+ rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
6606366316
if( rc==SQLITE_DONE ){
6606466317
pc = pOp->p2 - 1;
6606566318
rc = SQLITE_OK;
6606666319
}
6606766320
break;
@@ -66127,16 +66380,16 @@
6612766380
** Also, whether or not P4 is set, check that this is not being called from
6612866381
** within a callback to a virtual table xSync() method. If it is, the error
6612966382
** code will be set to SQLITE_LOCKED.
6613066383
*/
6613166384
case OP_VBegin: {
66132
-#if 0 /* local variables moved into u.cf */
66385
+#if 0 /* local variables moved into u.cg */
6613366386
VTable *pVTab;
66134
-#endif /* local variables moved into u.cf */
66135
- u.cf.pVTab = pOp->p4.pVtab;
66136
- rc = sqlite3VtabBegin(db, u.cf.pVTab);
66137
- if( u.cf.pVTab ) importVtabErrMsg(p, u.cf.pVTab->pVtab);
66387
+#endif /* local variables moved into u.cg */
66388
+ u.cg.pVTab = pOp->p4.pVtab;
66389
+ rc = sqlite3VtabBegin(db, u.cg.pVTab);
66390
+ if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
6613866391
break;
6613966392
}
6614066393
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6614166394
6614266395
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -66171,36 +66424,36 @@
6617166424
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6617266425
** P1 is a cursor number. This opcode opens a cursor to the virtual
6617366426
** table and stores that cursor in P1.
6617466427
*/
6617566428
case OP_VOpen: {
66176
-#if 0 /* local variables moved into u.cg */
66429
+#if 0 /* local variables moved into u.ch */
6617766430
VdbeCursor *pCur;
6617866431
sqlite3_vtab_cursor *pVtabCursor;
6617966432
sqlite3_vtab *pVtab;
6618066433
sqlite3_module *pModule;
66181
-#endif /* local variables moved into u.cg */
66182
-
66183
- u.cg.pCur = 0;
66184
- u.cg.pVtabCursor = 0;
66185
- u.cg.pVtab = pOp->p4.pVtab->pVtab;
66186
- u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
66187
- assert(u.cg.pVtab && u.cg.pModule);
66188
- rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
66189
- importVtabErrMsg(p, u.cg.pVtab);
66434
+#endif /* local variables moved into u.ch */
66435
+
66436
+ u.ch.pCur = 0;
66437
+ u.ch.pVtabCursor = 0;
66438
+ u.ch.pVtab = pOp->p4.pVtab->pVtab;
66439
+ u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
66440
+ assert(u.ch.pVtab && u.ch.pModule);
66441
+ rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
66442
+ importVtabErrMsg(p, u.ch.pVtab);
6619066443
if( SQLITE_OK==rc ){
6619166444
/* Initialize sqlite3_vtab_cursor base class */
66192
- u.cg.pVtabCursor->pVtab = u.cg.pVtab;
66445
+ u.ch.pVtabCursor->pVtab = u.ch.pVtab;
6619366446
6619466447
/* Initialise vdbe cursor object */
66195
- u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
66196
- if( u.cg.pCur ){
66197
- u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
66198
- u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
66448
+ u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
66449
+ if( u.ch.pCur ){
66450
+ u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
66451
+ u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
6619966452
}else{
6620066453
db->mallocFailed = 1;
66201
- u.cg.pModule->xClose(u.cg.pVtabCursor);
66454
+ u.ch.pModule->xClose(u.ch.pVtabCursor);
6620266455
}
6620366456
}
6620466457
break;
6620566458
}
6620666459
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66223,11 +66476,11 @@
6622366476
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
6622466477
**
6622566478
** A jump is made to P2 if the result set after filtering would be empty.
6622666479
*/
6622766480
case OP_VFilter: { /* jump */
66228
-#if 0 /* local variables moved into u.ch */
66481
+#if 0 /* local variables moved into u.ci */
6622966482
int nArg;
6623066483
int iQuery;
6623166484
const sqlite3_module *pModule;
6623266485
Mem *pQuery;
6623366486
Mem *pArgc;
@@ -66235,49 +66488,49 @@
6623566488
sqlite3_vtab *pVtab;
6623666489
VdbeCursor *pCur;
6623766490
int res;
6623866491
int i;
6623966492
Mem **apArg;
66240
-#endif /* local variables moved into u.ch */
66241
-
66242
- u.ch.pQuery = &aMem[pOp->p3];
66243
- u.ch.pArgc = &u.ch.pQuery[1];
66244
- u.ch.pCur = p->apCsr[pOp->p1];
66245
- assert( memIsValid(u.ch.pQuery) );
66246
- REGISTER_TRACE(pOp->p3, u.ch.pQuery);
66247
- assert( u.ch.pCur->pVtabCursor );
66248
- u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
66249
- u.ch.pVtab = u.ch.pVtabCursor->pVtab;
66250
- u.ch.pModule = u.ch.pVtab->pModule;
66493
+#endif /* local variables moved into u.ci */
66494
+
66495
+ u.ci.pQuery = &aMem[pOp->p3];
66496
+ u.ci.pArgc = &u.ci.pQuery[1];
66497
+ u.ci.pCur = p->apCsr[pOp->p1];
66498
+ assert( memIsValid(u.ci.pQuery) );
66499
+ REGISTER_TRACE(pOp->p3, u.ci.pQuery);
66500
+ assert( u.ci.pCur->pVtabCursor );
66501
+ u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
66502
+ u.ci.pVtab = u.ci.pVtabCursor->pVtab;
66503
+ u.ci.pModule = u.ci.pVtab->pModule;
6625166504
6625266505
/* Grab the index number and argc parameters */
66253
- assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
66254
- u.ch.nArg = (int)u.ch.pArgc->u.i;
66255
- u.ch.iQuery = (int)u.ch.pQuery->u.i;
66506
+ assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
66507
+ u.ci.nArg = (int)u.ci.pArgc->u.i;
66508
+ u.ci.iQuery = (int)u.ci.pQuery->u.i;
6625666509
6625766510
/* Invoke the xFilter method */
6625866511
{
66259
- u.ch.res = 0;
66260
- u.ch.apArg = p->apArg;
66261
- for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
66262
- u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
66263
- sqlite3VdbeMemStoreType(u.ch.apArg[u.ch.i]);
66512
+ u.ci.res = 0;
66513
+ u.ci.apArg = p->apArg;
66514
+ for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
66515
+ u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
66516
+ sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
6626466517
}
6626566518
6626666519
p->inVtabMethod = 1;
66267
- rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
66520
+ rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
6626866521
p->inVtabMethod = 0;
66269
- importVtabErrMsg(p, u.ch.pVtab);
66522
+ importVtabErrMsg(p, u.ci.pVtab);
6627066523
if( rc==SQLITE_OK ){
66271
- u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
66524
+ u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
6627266525
}
6627366526
66274
- if( u.ch.res ){
66527
+ if( u.ci.res ){
6627566528
pc = pOp->p2 - 1;
6627666529
}
6627766530
}
66278
- u.ch.pCur->nullRow = 0;
66531
+ u.ci.pCur->nullRow = 0;
6627966532
6628066533
break;
6628166534
}
6628266535
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6628366536
@@ -66287,55 +66540,55 @@
6628766540
** Store the value of the P2-th column of
6628866541
** the row of the virtual-table that the
6628966542
** P1 cursor is pointing to into register P3.
6629066543
*/
6629166544
case OP_VColumn: {
66292
-#if 0 /* local variables moved into u.ci */
66545
+#if 0 /* local variables moved into u.cj */
6629366546
sqlite3_vtab *pVtab;
6629466547
const sqlite3_module *pModule;
6629566548
Mem *pDest;
6629666549
sqlite3_context sContext;
66297
-#endif /* local variables moved into u.ci */
66550
+#endif /* local variables moved into u.cj */
6629866551
6629966552
VdbeCursor *pCur = p->apCsr[pOp->p1];
6630066553
assert( pCur->pVtabCursor );
6630166554
assert( pOp->p3>0 && pOp->p3<=p->nMem );
66302
- u.ci.pDest = &aMem[pOp->p3];
66303
- memAboutToChange(p, u.ci.pDest);
66555
+ u.cj.pDest = &aMem[pOp->p3];
66556
+ memAboutToChange(p, u.cj.pDest);
6630466557
if( pCur->nullRow ){
66305
- sqlite3VdbeMemSetNull(u.ci.pDest);
66558
+ sqlite3VdbeMemSetNull(u.cj.pDest);
6630666559
break;
6630766560
}
66308
- u.ci.pVtab = pCur->pVtabCursor->pVtab;
66309
- u.ci.pModule = u.ci.pVtab->pModule;
66310
- assert( u.ci.pModule->xColumn );
66311
- memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
66561
+ u.cj.pVtab = pCur->pVtabCursor->pVtab;
66562
+ u.cj.pModule = u.cj.pVtab->pModule;
66563
+ assert( u.cj.pModule->xColumn );
66564
+ memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
6631266565
6631366566
/* The output cell may already have a buffer allocated. Move
66314
- ** the current contents to u.ci.sContext.s so in case the user-function
66567
+ ** the current contents to u.cj.sContext.s so in case the user-function
6631566568
** can use the already allocated buffer instead of allocating a
6631666569
** new one.
6631766570
*/
66318
- sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
66319
- MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
66571
+ sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
66572
+ MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
6632066573
66321
- rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
66322
- importVtabErrMsg(p, u.ci.pVtab);
66323
- if( u.ci.sContext.isError ){
66324
- rc = u.ci.sContext.isError;
66574
+ rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
66575
+ importVtabErrMsg(p, u.cj.pVtab);
66576
+ if( u.cj.sContext.isError ){
66577
+ rc = u.cj.sContext.isError;
6632566578
}
6632666579
6632766580
/* Copy the result of the function to the P3 register. We
6632866581
** do this regardless of whether or not an error occurred to ensure any
66329
- ** dynamic allocation in u.ci.sContext.s (a Mem struct) is released.
66582
+ ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
6633066583
*/
66331
- sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
66332
- sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
66333
- REGISTER_TRACE(pOp->p3, u.ci.pDest);
66334
- UPDATE_MAX_BLOBSIZE(u.ci.pDest);
66584
+ sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
66585
+ sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
66586
+ REGISTER_TRACE(pOp->p3, u.cj.pDest);
66587
+ UPDATE_MAX_BLOBSIZE(u.cj.pDest);
6633566588
66336
- if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
66589
+ if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
6633766590
goto too_big;
6633866591
}
6633966592
break;
6634066593
}
6634166594
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66346,42 +66599,42 @@
6634666599
** Advance virtual table P1 to the next row in its result set and
6634766600
** jump to instruction P2. Or, if the virtual table has reached
6634866601
** the end of its result set, then fall through to the next instruction.
6634966602
*/
6635066603
case OP_VNext: { /* jump */
66351
-#if 0 /* local variables moved into u.cj */
66604
+#if 0 /* local variables moved into u.ck */
6635266605
sqlite3_vtab *pVtab;
6635366606
const sqlite3_module *pModule;
6635466607
int res;
6635566608
VdbeCursor *pCur;
66356
-#endif /* local variables moved into u.cj */
66609
+#endif /* local variables moved into u.ck */
6635766610
66358
- u.cj.res = 0;
66359
- u.cj.pCur = p->apCsr[pOp->p1];
66360
- assert( u.cj.pCur->pVtabCursor );
66361
- if( u.cj.pCur->nullRow ){
66611
+ u.ck.res = 0;
66612
+ u.ck.pCur = p->apCsr[pOp->p1];
66613
+ assert( u.ck.pCur->pVtabCursor );
66614
+ if( u.ck.pCur->nullRow ){
6636266615
break;
6636366616
}
66364
- u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
66365
- u.cj.pModule = u.cj.pVtab->pModule;
66366
- assert( u.cj.pModule->xNext );
66617
+ u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
66618
+ u.ck.pModule = u.ck.pVtab->pModule;
66619
+ assert( u.ck.pModule->xNext );
6636766620
6636866621
/* Invoke the xNext() method of the module. There is no way for the
6636966622
** underlying implementation to return an error if one occurs during
6637066623
** xNext(). Instead, if an error occurs, true is returned (indicating that
6637166624
** data is available) and the error code returned when xColumn or
6637266625
** some other method is next invoked on the save virtual table cursor.
6637366626
*/
6637466627
p->inVtabMethod = 1;
66375
- rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
66628
+ rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
6637666629
p->inVtabMethod = 0;
66377
- importVtabErrMsg(p, u.cj.pVtab);
66630
+ importVtabErrMsg(p, u.ck.pVtab);
6637866631
if( rc==SQLITE_OK ){
66379
- u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
66632
+ u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
6638066633
}
6638166634
66382
- if( !u.cj.res ){
66635
+ if( !u.ck.res ){
6638366636
/* If there is data, jump to P2 */
6638466637
pc = pOp->p2 - 1;
6638566638
}
6638666639
break;
6638766640
}
@@ -66393,23 +66646,23 @@
6639366646
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6639466647
** This opcode invokes the corresponding xRename method. The value
6639566648
** in register P1 is passed as the zName argument to the xRename method.
6639666649
*/
6639766650
case OP_VRename: {
66398
-#if 0 /* local variables moved into u.ck */
66651
+#if 0 /* local variables moved into u.cl */
6639966652
sqlite3_vtab *pVtab;
6640066653
Mem *pName;
66401
-#endif /* local variables moved into u.ck */
66402
-
66403
- u.ck.pVtab = pOp->p4.pVtab->pVtab;
66404
- u.ck.pName = &aMem[pOp->p1];
66405
- assert( u.ck.pVtab->pModule->xRename );
66406
- assert( memIsValid(u.ck.pName) );
66407
- REGISTER_TRACE(pOp->p1, u.ck.pName);
66408
- assert( u.ck.pName->flags & MEM_Str );
66409
- rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
66410
- importVtabErrMsg(p, u.ck.pVtab);
66654
+#endif /* local variables moved into u.cl */
66655
+
66656
+ u.cl.pVtab = pOp->p4.pVtab->pVtab;
66657
+ u.cl.pName = &aMem[pOp->p1];
66658
+ assert( u.cl.pVtab->pModule->xRename );
66659
+ assert( memIsValid(u.cl.pName) );
66660
+ REGISTER_TRACE(pOp->p1, u.cl.pName);
66661
+ assert( u.cl.pName->flags & MEM_Str );
66662
+ rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
66663
+ importVtabErrMsg(p, u.cl.pVtab);
6641166664
p->expired = 0;
6641266665
6641366666
break;
6641466667
}
6641566668
#endif
@@ -66437,39 +66690,39 @@
6643766690
** P1 is a boolean flag. If it is set to true and the xUpdate call
6643866691
** is successful, then the value returned by sqlite3_last_insert_rowid()
6643966692
** is set to the value of the rowid for the row just inserted.
6644066693
*/
6644166694
case OP_VUpdate: {
66442
-#if 0 /* local variables moved into u.cl */
66695
+#if 0 /* local variables moved into u.cm */
6644366696
sqlite3_vtab *pVtab;
6644466697
sqlite3_module *pModule;
6644566698
int nArg;
6644666699
int i;
6644766700
sqlite_int64 rowid;
6644866701
Mem **apArg;
6644966702
Mem *pX;
66450
-#endif /* local variables moved into u.cl */
66703
+#endif /* local variables moved into u.cm */
6645166704
66452
- u.cl.pVtab = pOp->p4.pVtab->pVtab;
66453
- u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
66454
- u.cl.nArg = pOp->p2;
66705
+ u.cm.pVtab = pOp->p4.pVtab->pVtab;
66706
+ u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
66707
+ u.cm.nArg = pOp->p2;
6645566708
assert( pOp->p4type==P4_VTAB );
66456
- if( ALWAYS(u.cl.pModule->xUpdate) ){
66457
- u.cl.apArg = p->apArg;
66458
- u.cl.pX = &aMem[pOp->p3];
66459
- for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
66460
- assert( memIsValid(u.cl.pX) );
66461
- memAboutToChange(p, u.cl.pX);
66462
- sqlite3VdbeMemStoreType(u.cl.pX);
66463
- u.cl.apArg[u.cl.i] = u.cl.pX;
66464
- u.cl.pX++;
66465
- }
66466
- rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
66467
- importVtabErrMsg(p, u.cl.pVtab);
66709
+ if( ALWAYS(u.cm.pModule->xUpdate) ){
66710
+ u.cm.apArg = p->apArg;
66711
+ u.cm.pX = &aMem[pOp->p3];
66712
+ for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
66713
+ assert( memIsValid(u.cm.pX) );
66714
+ memAboutToChange(p, u.cm.pX);
66715
+ sqlite3VdbeMemStoreType(u.cm.pX);
66716
+ u.cm.apArg[u.cm.i] = u.cm.pX;
66717
+ u.cm.pX++;
66718
+ }
66719
+ rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
66720
+ importVtabErrMsg(p, u.cm.pVtab);
6646866721
if( rc==SQLITE_OK && pOp->p1 ){
66469
- assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
66470
- db->lastRowid = u.cl.rowid;
66722
+ assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
66723
+ db->lastRowid = u.cm.rowid;
6647166724
}
6647266725
p->nChange++;
6647366726
}
6647466727
break;
6647566728
}
@@ -66517,24 +66770,24 @@
6651766770
**
6651866771
** If tracing is enabled (by the sqlite3_trace()) interface, then
6651966772
** the UTF-8 string contained in P4 is emitted on the trace callback.
6652066773
*/
6652166774
case OP_Trace: {
66522
-#if 0 /* local variables moved into u.cm */
66775
+#if 0 /* local variables moved into u.cn */
6652366776
char *zTrace;
66524
-#endif /* local variables moved into u.cm */
66777
+#endif /* local variables moved into u.cn */
6652566778
66526
- u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
66527
- if( u.cm.zTrace ){
66779
+ u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
66780
+ if( u.cn.zTrace ){
6652866781
if( db->xTrace ){
66529
- char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace);
66782
+ char *z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
6653066783
db->xTrace(db->pTraceArg, z);
6653166784
sqlite3DbFree(db, z);
6653266785
}
6653366786
#ifdef SQLITE_DEBUG
6653466787
if( (db->flags & SQLITE_SqlTrace)!=0 ){
66535
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace);
66788
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
6653666789
}
6653766790
#endif /* SQLITE_DEBUG */
6653866791
}
6653966792
break;
6654066793
}
@@ -69058,11 +69311,11 @@
6905869311
** there is no default collation type, return 0.
6905969312
*/
6906069313
SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
6906169314
CollSeq *pColl = 0;
6906269315
Expr *p = pExpr;
69063
- while( ALWAYS(p) ){
69316
+ while( p ){
6906469317
int op;
6906569318
pColl = p->pColl;
6906669319
if( pColl ) break;
6906769320
op = p->op;
6906869321
if( p->pTab!=0 && (
@@ -72217,18 +72470,20 @@
7221772470
case TK_BETWEEN: {
7221872471
testcase( jumpIfNull==0 );
7221972472
exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
7222072473
break;
7222172474
}
72475
+#ifndef SQLITE_OMIT_SUBQUERY
7222272476
case TK_IN: {
7222372477
int destIfFalse = sqlite3VdbeMakeLabel(v);
7222472478
int destIfNull = jumpIfNull ? dest : destIfFalse;
7222572479
sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
7222672480
sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
7222772481
sqlite3VdbeResolveLabel(v, destIfFalse);
7222872482
break;
7222972483
}
72484
+#endif
7223072485
default: {
7223172486
r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
7223272487
sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
7223372488
testcase( regFree1==0 );
7223472489
testcase( jumpIfNull==0 );
@@ -72358,10 +72613,11 @@
7235872613
case TK_BETWEEN: {
7235972614
testcase( jumpIfNull==0 );
7236072615
exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
7236172616
break;
7236272617
}
72618
+#ifndef SQLITE_OMIT_SUBQUERY
7236372619
case TK_IN: {
7236472620
if( jumpIfNull ){
7236572621
sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
7236672622
}else{
7236772623
int destIfNull = sqlite3VdbeMakeLabel(v);
@@ -72368,10 +72624,11 @@
7236872624
sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
7236972625
sqlite3VdbeResolveLabel(v, destIfNull);
7237072626
}
7237172627
break;
7237272628
}
72629
+#endif
7237372630
default: {
7237472631
r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
7237572632
sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
7237672633
testcase( regFree1==0 );
7237772634
testcase( jumpIfNull==0 );
@@ -74549,13 +74806,15 @@
7454974806
goto attach_end;
7455074807
}
7455174808
7455274809
#ifndef SQLITE_OMIT_AUTHORIZATION
7455374810
if( pAuthArg ){
74554
- char *zAuthArg = pAuthArg->u.zToken;
74555
- if( NEVER(zAuthArg==0) ){
74556
- goto attach_end;
74811
+ char *zAuthArg;
74812
+ if( pAuthArg->op==TK_STRING ){
74813
+ zAuthArg = pAuthArg->u.zToken;
74814
+ }else{
74815
+ zAuthArg = 0;
7455774816
}
7455874817
rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
7455974818
if(rc!=SQLITE_OK ){
7456074819
goto attach_end;
7456174820
}
@@ -87034,17 +87293,33 @@
8703487293
}else
8703587294
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
8703687295
8703787296
#ifndef SQLITE_OMIT_WAL
8703887297
/*
87039
- ** PRAGMA [database.]wal_checkpoint
87298
+ ** PRAGMA [database.]wal_checkpoint = passive|full|restart
8704087299
**
8704187300
** Checkpoint the database.
8704287301
*/
8704387302
if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
87303
+ int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
87304
+ int eMode = SQLITE_CHECKPOINT_PASSIVE;
87305
+ if( zRight ){
87306
+ if( sqlite3StrICmp(zRight, "full")==0 ){
87307
+ eMode = SQLITE_CHECKPOINT_FULL;
87308
+ }else if( sqlite3StrICmp(zRight, "restart")==0 ){
87309
+ eMode = SQLITE_CHECKPOINT_RESTART;
87310
+ }
87311
+ }
8704487312
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
87045
- sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
87313
+ sqlite3VdbeSetNumCols(v, 3);
87314
+ pParse->nMem = 3;
87315
+ sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
87316
+ sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
87317
+ sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
87318
+
87319
+ sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
87320
+ sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
8704687321
}else
8704787322
8704887323
/*
8704987324
** PRAGMA wal_autocheckpoint
8705087325
** PRAGMA wal_autocheckpoint = N
@@ -90670,10 +90945,13 @@
9067090945
** (20) If the sub-query is a compound select, then it must not use
9067190946
** an ORDER BY clause. Ticket #3773. We could relax this constraint
9067290947
** somewhat by saying that the terms of the ORDER BY clause must
9067390948
** appear as unmodified result columns in the outer query. But
9067490949
** have other optimizations in mind to deal with that case.
90950
+**
90951
+** (21) The subquery does not use LIMIT or the outer query is not
90952
+** DISTINCT. (See ticket [752e1646fc]).
9067590953
**
9067690954
** In this routine, the "p" parameter is a pointer to the outer query.
9067790955
** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
9067890956
** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
9067990957
**
@@ -90739,10 +91017,13 @@
9073991017
if( p->pOrderBy && pSub->pOrderBy ){
9074091018
return 0; /* Restriction (11) */
9074191019
}
9074291020
if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
9074391021
if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
91022
+ if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
91023
+ return 0; /* Restriction (21) */
91024
+ }
9074491025
9074591026
/* OBSOLETE COMMENT 1:
9074691027
** Restriction 3: If the subquery is a join, make sure the subquery is
9074791028
** not used as the right operand of an outer join. Examples of why this
9074891029
** is not allowed:
@@ -95714,10 +95995,11 @@
9571495995
** generating the code that loops through a table looking for applicable
9571595996
** rows. Indices are selected and used to speed the search when doing
9571695997
** so is applicable. Because this module is responsible for selecting
9571795998
** indices, you might also think of this module as the "query optimizer".
9571895999
*/
96000
+
9571996001
9572096002
/*
9572196003
** Trace output macros
9572296004
*/
9572396005
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
@@ -95814,10 +96096,15 @@
9581496096
#define TERM_CODED 0x04 /* This term is already coded */
9581596097
#define TERM_COPIED 0x08 /* Has a child */
9581696098
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
9581796099
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
9581896100
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
96101
+#ifdef SQLITE_ENABLE_STAT2
96102
+# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
96103
+#else
96104
+# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
96105
+#endif
9581996106
9582096107
/*
9582196108
** An instance of the following structure holds all information about a
9582296109
** WHERE clause. Mostly this is a container for one or more WhereTerms.
9582396110
*/
@@ -95907,10 +96194,11 @@
9590796194
#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
9590896195
#define WO_MATCH 0x040
9590996196
#define WO_ISNULL 0x080
9591096197
#define WO_OR 0x100 /* Two or more OR-connected terms */
9591196198
#define WO_AND 0x200 /* Two or more AND-connected terms */
96199
+#define WO_NOOP 0x800 /* This term does not restrict search space */
9591296200
9591396201
#define WO_ALL 0xfff /* Mask of all possible WO_* values */
9591496202
#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
9591596203
9591696204
/*
@@ -96757,11 +97045,11 @@
9675797045
pWC->a[idxNew].iParent = idxTerm;
9675897046
pTerm->nChild = 1;
9675997047
}else{
9676097048
sqlite3ExprListDelete(db, pList);
9676197049
}
96762
- pTerm->eOperator = 0; /* case 1 trumps case 2 */
97050
+ pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
9676397051
}
9676497052
}
9676597053
}
9676697054
#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
9676797055
@@ -97021,10 +97309,48 @@
9702197309
pNewTerm->prereqAll = pTerm->prereqAll;
9702297310
}
9702397311
}
9702497312
#endif /* SQLITE_OMIT_VIRTUALTABLE */
9702597313
97314
+#ifdef SQLITE_ENABLE_STAT2
97315
+ /* When sqlite_stat2 histogram data is available an operator of the
97316
+ ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
97317
+ ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
97318
+ ** virtual term of that form.
97319
+ **
97320
+ ** Note that the virtual term must be tagged with TERM_VNULL. This
97321
+ ** TERM_VNULL tag will suppress the not-null check at the beginning
97322
+ ** of the loop. Without the TERM_VNULL flag, the not-null check at
97323
+ ** the start of the loop will prevent any results from being returned.
97324
+ */
97325
+ if( pExpr->op==TK_NOTNULL && pExpr->pLeft->iColumn>=0 ){
97326
+ Expr *pNewExpr;
97327
+ Expr *pLeft = pExpr->pLeft;
97328
+ int idxNew;
97329
+ WhereTerm *pNewTerm;
97330
+
97331
+ pNewExpr = sqlite3PExpr(pParse, TK_GT,
97332
+ sqlite3ExprDup(db, pLeft, 0),
97333
+ sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
97334
+
97335
+ idxNew = whereClauseInsert(pWC, pNewExpr,
97336
+ TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
97337
+ if( idxNew ){
97338
+ pNewTerm = &pWC->a[idxNew];
97339
+ pNewTerm->prereqRight = 0;
97340
+ pNewTerm->leftCursor = pLeft->iTable;
97341
+ pNewTerm->u.leftColumn = pLeft->iColumn;
97342
+ pNewTerm->eOperator = WO_GT;
97343
+ pNewTerm->iParent = idxTerm;
97344
+ pTerm = &pWC->a[idxTerm];
97345
+ pTerm->nChild = 1;
97346
+ pTerm->wtFlags |= TERM_COPIED;
97347
+ pNewTerm->prereqAll = pTerm->prereqAll;
97348
+ }
97349
+ }
97350
+#endif /* SQLITE_ENABLE_STAT2 */
97351
+
9702697352
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
9702797353
** an index for tables to the left of the join.
9702897354
*/
9702997355
pTerm->prereqRight |= extraRight;
9703097356
}
@@ -97073,10 +97399,11 @@
9707397399
WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
9707497400
Index *pIdx, /* The index we are testing */
9707597401
int base, /* Cursor number for the table to be sorted */
9707697402
ExprList *pOrderBy, /* The ORDER BY clause */
9707797403
int nEqCol, /* Number of index columns with == constraints */
97404
+ int wsFlags, /* Index usages flags */
9707897405
int *pbRev /* Set to 1 if ORDER BY is DESC */
9707997406
){
9708097407
int i, j; /* Loop counters */
9708197408
int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
9708297409
int nTerm; /* Number of ORDER BY terms */
@@ -97178,15 +97505,18 @@
9717897505
/* All terms of the ORDER BY clause are covered by this index so
9717997506
** this index can be used for sorting. */
9718097507
return 1;
9718197508
}
9718297509
if( pIdx->onError!=OE_None && i==pIdx->nColumn
97510
+ && (wsFlags & WHERE_COLUMN_NULL)==0
9718397511
&& !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
9718497512
/* All terms of this index match some prefix of the ORDER BY clause
9718597513
** and the index is UNIQUE and no terms on the tail of the ORDER BY
9718697514
** clause reference other tables in a join. If this is all true then
97187
- ** the order by clause is superfluous. */
97515
+ ** the order by clause is superfluous. Not that if the matching
97516
+ ** condition is IS NULL then the result is not necessarily unique
97517
+ ** even on a UNIQUE index, so disallow those cases. */
9718897518
return 1;
9718997519
}
9719097520
return 0;
9719197521
}
9719297522
@@ -97419,11 +97749,11 @@
9741997749
9742097750
/* Search for any equality comparison term */
9742197751
pWCEnd = &pWC->a[pWC->nTerm];
9742297752
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
9742397753
if( termCanDriveIndex(pTerm, pSrc, notReady) ){
97424
- WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
97754
+ WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
9742597755
pCost->rCost, costTempIdx));
9742697756
pCost->rCost = costTempIdx;
9742797757
pCost->plan.nRow = logN + 1;
9742897758
pCost->plan.wsFlags = WHERE_TEMP_INDEX;
9742997759
pCost->used = pTerm->prereqRight;
@@ -97540,11 +97870,12 @@
9754097870
if( (idxCols & cMask)==0 ){
9754197871
Expr *pX = pTerm->pExpr;
9754297872
idxCols |= cMask;
9754397873
pIdx->aiColumn[n] = pTerm->u.leftColumn;
9754497874
pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
97545
- pIdx->azColl[n] = pColl->zName;
97875
+ assert( pColl!=0 || pParse->nErr>0 );
97876
+ pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
9754697877
n++;
9754797878
}
9754897879
}
9754997880
}
9755097881
assert( (u32)n==pLevel->plan.nEq );
@@ -97898,15 +98229,22 @@
9789898229
#endif /* SQLITE_OMIT_VIRTUALTABLE */
9789998230
9790098231
/*
9790198232
** Argument pIdx is a pointer to an index structure that has an array of
9790298233
** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
97903
-** stored in Index.aSample. The domain of values stored in said column
97904
-** may be thought of as divided into (SQLITE_INDEX_SAMPLES+1) regions.
97905
-** Region 0 contains all values smaller than the first sample value. Region
97906
-** 1 contains values larger than or equal to the value of the first sample,
97907
-** but smaller than the value of the second. And so on.
98234
+** stored in Index.aSample. These samples divide the domain of values stored
98235
+** the index into (SQLITE_INDEX_SAMPLES+1) regions.
98236
+** Region 0 contains all values less than the first sample value. Region
98237
+** 1 contains values between the first and second samples. Region 2 contains
98238
+** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
98239
+** contains values larger than the last sample.
98240
+**
98241
+** If the index contains many duplicates of a single value, then it is
98242
+** possible that two or more adjacent samples can hold the same value.
98243
+** When that is the case, the smallest possible region code is returned
98244
+** when roundUp is false and the largest possible region code is returned
98245
+** when roundUp is true.
9790898246
**
9790998247
** If successful, this function determines which of the regions value
9791098248
** pVal lies in, sets *piRegion to the region index (a value between 0
9791198249
** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
9791298250
** Or, if an OOM occurs while converting text values between encodings,
@@ -97915,22 +98253,34 @@
9791598253
#ifdef SQLITE_ENABLE_STAT2
9791698254
static int whereRangeRegion(
9791798255
Parse *pParse, /* Database connection */
9791898256
Index *pIdx, /* Index to consider domain of */
9791998257
sqlite3_value *pVal, /* Value to consider */
98258
+ int roundUp, /* Return largest valid region if true */
9792098259
int *piRegion /* OUT: Region of domain in which value lies */
9792198260
){
98261
+ assert( roundUp==0 || roundUp==1 );
9792298262
if( ALWAYS(pVal) ){
9792398263
IndexSample *aSample = pIdx->aSample;
9792498264
int i = 0;
9792598265
int eType = sqlite3_value_type(pVal);
9792698266
9792798267
if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
9792898268
double r = sqlite3_value_double(pVal);
9792998269
for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
9793098270
if( aSample[i].eType==SQLITE_NULL ) continue;
97931
- if( aSample[i].eType>=SQLITE_TEXT || aSample[i].u.r>r ) break;
98271
+ if( aSample[i].eType>=SQLITE_TEXT ) break;
98272
+ if( roundUp ){
98273
+ if( aSample[i].u.r>r ) break;
98274
+ }else{
98275
+ if( aSample[i].u.r>=r ) break;
98276
+ }
98277
+ }
98278
+ }else if( eType==SQLITE_NULL ){
98279
+ i = 0;
98280
+ if( roundUp ){
98281
+ while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
9793298282
}
9793398283
}else{
9793498284
sqlite3 *db = pParse->db;
9793598285
CollSeq *pColl;
9793698286
const u8 *z;
@@ -97957,11 +98307,11 @@
9795798307
assert( z && pColl && pColl->xCmp );
9795898308
}
9795998309
n = sqlite3ValueBytes(pVal, pColl->enc);
9796098310
9796198311
for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
97962
- int r;
98312
+ int c;
9796398313
int eSampletype = aSample[i].eType;
9796498314
if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
9796598315
if( (eSampletype!=eType) ) break;
9796698316
#ifndef SQLITE_OMIT_UTF16
9796798317
if( pColl->enc!=SQLITE_UTF8 ){
@@ -97971,18 +98321,18 @@
9797198321
);
9797298322
if( !zSample ){
9797398323
assert( db->mallocFailed );
9797498324
return SQLITE_NOMEM;
9797598325
}
97976
- r = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
98326
+ c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
9797798327
sqlite3DbFree(db, zSample);
9797898328
}else
9797998329
#endif
9798098330
{
97981
- r = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
98331
+ c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
9798298332
}
97983
- if( r>0 ) break;
98333
+ if( c-roundUp>=0 ) break;
9798498334
}
9798598335
}
9798698336
9798798337
assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
9798898338
*piRegion = i;
@@ -98061,13 +98411,13 @@
9806198411
** constraints (if any). A return value of 100 indicates that it is expected
9806298412
** that the range scan will visit every row (100%) selected by the equality
9806398413
** constraints.
9806498414
**
9806598415
** In the absence of sqlite_stat2 ANALYZE data, each range inequality
98066
-** reduces the search space by 2/3rds. Hence a single constraint (x>?)
98067
-** results in a return of 33 and a range constraint (x>? AND x<?) results
98068
-** in a return of 11.
98416
+** reduces the search space by 3/4ths. Hence a single constraint (x>?)
98417
+** results in a return of 25 and a range constraint (x>? AND x<?) results
98418
+** in a return of 6.
9806998419
*/
9807098420
static int whereRangeScanEst(
9807198421
Parse *pParse, /* Parsing & code generating context */
9807298422
Index *p, /* The index containing the range-compared column; "x" */
9807398423
int nEq, /* index into p->aCol[] of the range-compared column */
@@ -98083,73 +98433,209 @@
9808398433
sqlite3_value *pLowerVal = 0;
9808498434
sqlite3_value *pUpperVal = 0;
9808598435
int iEst;
9808698436
int iLower = 0;
9808798437
int iUpper = SQLITE_INDEX_SAMPLES;
98438
+ int roundUpUpper = 0;
98439
+ int roundUpLower = 0;
9808898440
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
9808998441
9809098442
if( pLower ){
9809198443
Expr *pExpr = pLower->pExpr->pRight;
9809298444
rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
98445
+ assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
98446
+ roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
9809398447
}
9809498448
if( rc==SQLITE_OK && pUpper ){
9809598449
Expr *pExpr = pUpper->pExpr->pRight;
9809698450
rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
98451
+ assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
98452
+ roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
9809798453
}
9809898454
9809998455
if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
9810098456
sqlite3ValueFree(pLowerVal);
9810198457
sqlite3ValueFree(pUpperVal);
9810298458
goto range_est_fallback;
9810398459
}else if( pLowerVal==0 ){
98104
- rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
98460
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
9810598461
if( pLower ) iLower = iUpper/2;
9810698462
}else if( pUpperVal==0 ){
98107
- rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
98463
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
9810898464
if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
9810998465
}else{
98110
- rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
98466
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
9811198467
if( rc==SQLITE_OK ){
98112
- rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
98468
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
9811398469
}
9811498470
}
98471
+ WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
9811598472
9811698473
iEst = iUpper - iLower;
9811798474
testcase( iEst==SQLITE_INDEX_SAMPLES );
9811898475
assert( iEst<=SQLITE_INDEX_SAMPLES );
9811998476
if( iEst<1 ){
98120
- iEst = 1;
98477
+ *piEst = 50/SQLITE_INDEX_SAMPLES;
98478
+ }else{
98479
+ *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
9812198480
}
98122
-
9812398481
sqlite3ValueFree(pLowerVal);
9812498482
sqlite3ValueFree(pUpperVal);
98125
- *piEst = (iEst * 100)/SQLITE_INDEX_SAMPLES;
9812698483
return rc;
9812798484
}
9812898485
range_est_fallback:
9812998486
#else
9813098487
UNUSED_PARAMETER(pParse);
9813198488
UNUSED_PARAMETER(p);
9813298489
UNUSED_PARAMETER(nEq);
9813398490
#endif
9813498491
assert( pLower || pUpper );
98135
- if( pLower && pUpper ){
98136
- *piEst = 11;
98137
- }else{
98138
- *piEst = 33;
98139
- }
98492
+ *piEst = 100;
98493
+ if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
98494
+ if( pUpper ) *piEst /= 4;
9814098495
return rc;
9814198496
}
9814298497
98498
+#ifdef SQLITE_ENABLE_STAT2
98499
+/*
98500
+** Estimate the number of rows that will be returned based on
98501
+** an equality constraint x=VALUE and where that VALUE occurs in
98502
+** the histogram data. This only works when x is the left-most
98503
+** column of an index and sqlite_stat2 histogram data is available
98504
+** for that index.
98505
+**
98506
+** Write the estimated row count into *pnRow and return SQLITE_OK.
98507
+** If unable to make an estimate, leave *pnRow unchanged and return
98508
+** non-zero.
98509
+**
98510
+** This routine can fail if it is unable to load a collating sequence
98511
+** required for string comparison, or if unable to allocate memory
98512
+** for a UTF conversion required for comparison. The error is stored
98513
+** in the pParse structure.
98514
+*/
98515
+int whereEqualScanEst(
98516
+ Parse *pParse, /* Parsing & code generating context */
98517
+ Index *p, /* The index whose left-most column is pTerm */
98518
+ Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
98519
+ double *pnRow /* Write the revised row estimate here */
98520
+){
98521
+ sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
98522
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
98523
+ u8 aff; /* Column affinity */
98524
+ int rc; /* Subfunction return code */
98525
+ double nRowEst; /* New estimate of the number of rows */
98526
+
98527
+ assert( p->aSample!=0 );
98528
+ aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98529
+ rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
98530
+ if( rc ) goto whereEqualScanEst_cancel;
98531
+ if( pRhs==0 ) return SQLITE_NOTFOUND;
98532
+ rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
98533
+ if( rc ) goto whereEqualScanEst_cancel;
98534
+ rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
98535
+ if( rc ) goto whereEqualScanEst_cancel;
98536
+ WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
98537
+ if( iLower>=iUpper ){
98538
+ nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
98539
+ if( nRowEst<*pnRow ) *pnRow = nRowEst;
98540
+ }else{
98541
+ nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
98542
+ *pnRow = nRowEst;
98543
+ }
98544
+
98545
+whereEqualScanEst_cancel:
98546
+ sqlite3ValueFree(pRhs);
98547
+ return rc;
98548
+}
98549
+#endif /* defined(SQLITE_ENABLE_STAT2) */
98550
+
98551
+#ifdef SQLITE_ENABLE_STAT2
98552
+/*
98553
+** Estimate the number of rows that will be returned based on
98554
+** an IN constraint where the right-hand side of the IN operator
98555
+** is a list of values. Example:
98556
+**
98557
+** WHERE x IN (1,2,3,4)
98558
+**
98559
+** Write the estimated row count into *pnRow and return SQLITE_OK.
98560
+** If unable to make an estimate, leave *pnRow unchanged and return
98561
+** non-zero.
98562
+**
98563
+** This routine can fail if it is unable to load a collating sequence
98564
+** required for string comparison, or if unable to allocate memory
98565
+** for a UTF conversion required for comparison. The error is stored
98566
+** in the pParse structure.
98567
+*/
98568
+int whereInScanEst(
98569
+ Parse *pParse, /* Parsing & code generating context */
98570
+ Index *p, /* The index whose left-most column is pTerm */
98571
+ ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
98572
+ double *pnRow /* Write the revised row estimate here */
98573
+){
98574
+ sqlite3_value *pVal = 0; /* One value from list */
98575
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
98576
+ u8 aff; /* Column affinity */
98577
+ int rc = SQLITE_OK; /* Subfunction return code */
98578
+ double nRowEst; /* New estimate of the number of rows */
98579
+ int nSpan = 0; /* Number of histogram regions spanned */
98580
+ int nSingle = 0; /* Histogram regions hit by a single value */
98581
+ int nNotFound = 0; /* Count of values that are not constants */
98582
+ int i; /* Loop counter */
98583
+ u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
98584
+ u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
98585
+
98586
+ assert( p->aSample!=0 );
98587
+ aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98588
+ memset(aSpan, 0, sizeof(aSpan));
98589
+ memset(aSingle, 0, sizeof(aSingle));
98590
+ for(i=0; i<pList->nExpr; i++){
98591
+ sqlite3ValueFree(pVal);
98592
+ rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
98593
+ if( rc ) break;
98594
+ if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
98595
+ nNotFound++;
98596
+ continue;
98597
+ }
98598
+ rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
98599
+ if( rc ) break;
98600
+ rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
98601
+ if( rc ) break;
98602
+ if( iLower>=iUpper ){
98603
+ aSingle[iLower] = 1;
98604
+ }else{
98605
+ assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
98606
+ while( iLower<iUpper ) aSpan[iLower++] = 1;
98607
+ }
98608
+ }
98609
+ if( rc==SQLITE_OK ){
98610
+ for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
98611
+ if( aSpan[i] ){
98612
+ nSpan++;
98613
+ }else if( aSingle[i] ){
98614
+ nSingle++;
98615
+ }
98616
+ }
98617
+ nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
98618
+ + nNotFound*p->aiRowEst[1];
98619
+ if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
98620
+ *pnRow = nRowEst;
98621
+ WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
98622
+ nSpan, nSingle, nNotFound, nRowEst));
98623
+ }
98624
+ sqlite3ValueFree(pVal);
98625
+ return rc;
98626
+}
98627
+#endif /* defined(SQLITE_ENABLE_STAT2) */
98628
+
9814398629
9814498630
/*
98145
-** Find the query plan for accessing a particular table. Write the
98631
+** Find the best query plan for accessing a particular table. Write the
9814698632
** best query plan and its cost into the WhereCost object supplied as the
9814798633
** last parameter.
9814898634
**
9814998635
** The lowest cost plan wins. The cost is an estimate of the amount of
98150
-** CPU and disk I/O need to process the request using the selected plan.
98636
+** CPU and disk I/O needed to process the requested result.
9815198637
** Factors that influence cost include:
9815298638
**
9815398639
** * The estimated number of rows that will be retrieved. (The
9815498640
** fewer the better.)
9815598641
**
@@ -98164,11 +98650,11 @@
9816498650
** SQLITE_BIG_DBL. If a plan is found that uses the named index,
9816598651
** then the cost is calculated in the usual way.
9816698652
**
9816798653
** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
9816898654
** in the SELECT statement, then no indexes are considered. However, the
98169
-** selected plan may still take advantage of the tables built-in rowid
98655
+** selected plan may still take advantage of the built-in rowid primary key
9817098656
** index.
9817198657
*/
9817298658
static void bestBtreeIndex(
9817398659
Parse *pParse, /* The parsing context */
9817498660
WhereClause *pWC, /* The WHERE clause */
@@ -98207,13 +98693,15 @@
9820798693
/* An INDEXED BY clause specifies a particular index to use */
9820898694
pIdx = pProbe = pSrc->pIndex;
9820998695
wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
9821098696
eqTermMask = idxEqTermMask;
9821198697
}else{
98212
- /* There is no INDEXED BY clause. Create a fake Index object to
98213
- ** represent the primary key */
98214
- Index *pFirst; /* Any other index on the table */
98698
+ /* There is no INDEXED BY clause. Create a fake Index object in local
98699
+ ** variable sPk to represent the rowid primary key index. Make this
98700
+ ** fake index the first in a chain of Index objects with all of the real
98701
+ ** indices to follow */
98702
+ Index *pFirst; /* First of real indices on the table */
9821598703
memset(&sPk, 0, sizeof(Index));
9821698704
sPk.nColumn = 1;
9821798705
sPk.aiColumn = &aiColumnPk;
9821898706
sPk.aiRowEst = aiRowEstPk;
9821998707
sPk.onError = OE_Replace;
@@ -98220,10 +98708,12 @@
9822098708
sPk.pTable = pSrc->pTab;
9822198709
aiRowEstPk[0] = pSrc->pTab->nRowEst;
9822298710
aiRowEstPk[1] = 1;
9822398711
pFirst = pSrc->pTab->pIndex;
9822498712
if( pSrc->notIndexed==0 ){
98713
+ /* The real indices of the table are only considered if the
98714
+ ** NOT INDEXED qualifier is omitted from the FROM clause */
9822598715
sPk.pNext = pFirst;
9822698716
}
9822798717
pProbe = &sPk;
9822898718
wsFlagMask = ~(
9822998719
WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
@@ -98236,20 +98726,23 @@
9823698726
*/
9823798727
for(; pProbe; pIdx=pProbe=pProbe->pNext){
9823898728
const unsigned int * const aiRowEst = pProbe->aiRowEst;
9823998729
double cost; /* Cost of using pProbe */
9824098730
double nRow; /* Estimated number of rows in result set */
98731
+ double log10N; /* base-10 logarithm of nRow (inexact) */
9824198732
int rev; /* True to scan in reverse order */
9824298733
int wsFlags = 0;
9824398734
Bitmask used = 0;
9824498735
9824598736
/* The following variables are populated based on the properties of
98246
- ** scan being evaluated. They are then used to determine the expected
98737
+ ** index being evaluated. They are then used to determine the expected
9824798738
** cost and number of rows returned.
9824898739
**
9824998740
** nEq:
9825098741
** Number of equality terms that can be implemented using the index.
98742
+ ** In other words, the number of initial fields in the index that
98743
+ ** are used in == or IN or NOT NULL constraints of the WHERE clause.
9825198744
**
9825298745
** nInMul:
9825398746
** The "in-multiplier". This is an estimate of how many seek operations
9825498747
** SQLite must perform on the index in question. For example, if the
9825598748
** WHERE clause is:
@@ -98269,46 +98762,54 @@
9826998762
** the sub-select is assumed to return 25 rows for the purposes of
9827098763
** determining nInMul.
9827198764
**
9827298765
** bInEst:
9827398766
** Set to true if there was at least one "x IN (SELECT ...)" term used
98274
- ** in determining the value of nInMul.
98767
+ ** in determining the value of nInMul. Note that the RHS of the
98768
+ ** IN operator must be a SELECT, not a value list, for this variable
98769
+ ** to be true.
9827598770
**
9827698771
** estBound:
9827798772
** An estimate on the amount of the table that must be searched. A
9827898773
** value of 100 means the entire table is searched. Range constraints
9827998774
** might reduce this to a value less than 100 to indicate that only
9828098775
** a fraction of the table needs searching. In the absence of
9828198776
** sqlite_stat2 ANALYZE data, a single inequality reduces the search
98282
- ** space to 1/3rd its original size. So an x>? constraint reduces
98283
- ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11.
98777
+ ** space to 1/4rd its original size. So an x>? constraint reduces
98778
+ ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
9828498779
**
9828598780
** bSort:
9828698781
** Boolean. True if there is an ORDER BY clause that will require an
9828798782
** external sort (i.e. scanning the index being evaluated will not
9828898783
** correctly order records).
9828998784
**
9829098785
** bLookup:
98291
- ** Boolean. True if for each index entry visited a lookup on the
98292
- ** corresponding table b-tree is required. This is always false
98293
- ** for the rowid index. For other indexes, it is true unless all the
98294
- ** columns of the table used by the SELECT statement are present in
98295
- ** the index (such an index is sometimes described as a covering index).
98786
+ ** Boolean. True if a table lookup is required for each index entry
98787
+ ** visited. In other words, true if this is not a covering index.
98788
+ ** This is always false for the rowid primary key index of a table.
98789
+ ** For other indexes, it is true unless all the columns of the table
98790
+ ** used by the SELECT statement are present in the index (such an
98791
+ ** index is sometimes described as a covering index).
9829698792
** For example, given the index on (a, b), the second of the following
98297
- ** two queries requires table b-tree lookups, but the first does not.
98793
+ ** two queries requires table b-tree lookups in order to find the value
98794
+ ** of column c, but the first does not because columns a and b are
98795
+ ** both available in the index.
9829898796
**
9829998797
** SELECT a, b FROM tbl WHERE a = 1;
9830098798
** SELECT a, b, c FROM tbl WHERE a = 1;
9830198799
*/
98302
- int nEq;
98303
- int bInEst = 0;
98304
- int nInMul = 1;
98305
- int estBound = 100;
98306
- int nBound = 0; /* Number of range constraints seen */
98307
- int bSort = 0;
98308
- int bLookup = 0;
98309
- WhereTerm *pTerm; /* A single term of the WHERE clause */
98800
+ int nEq; /* Number of == or IN terms matching index */
98801
+ int bInEst = 0; /* True if "x IN (SELECT...)" seen */
98802
+ int nInMul = 1; /* Number of distinct equalities to lookup */
98803
+ int estBound = 100; /* Estimated reduction in search space */
98804
+ int nBound = 0; /* Number of range constraints seen */
98805
+ int bSort = 0; /* True if external sort required */
98806
+ int bLookup = 0; /* True if not a covering index */
98807
+ WhereTerm *pTerm; /* A single term of the WHERE clause */
98808
+#ifdef SQLITE_ENABLE_STAT2
98809
+ WhereTerm *pFirstTerm = 0; /* First term matching the index */
98810
+#endif
9831098811
9831198812
/* Determine the values of nEq and nInMul */
9831298813
for(nEq=0; nEq<pProbe->nColumn; nEq++){
9831398814
int j = pProbe->aiColumn[nEq];
9831498815
pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
@@ -98316,18 +98817,23 @@
9831698817
wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
9831798818
if( pTerm->eOperator & WO_IN ){
9831898819
Expr *pExpr = pTerm->pExpr;
9831998820
wsFlags |= WHERE_COLUMN_IN;
9832098821
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98822
+ /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
9832198823
nInMul *= 25;
9832298824
bInEst = 1;
98323
- }else if( ALWAYS(pExpr->x.pList) ){
98324
- nInMul *= pExpr->x.pList->nExpr + 1;
98825
+ }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
98826
+ /* "x IN (value, value, ...)" */
98827
+ nInMul *= pExpr->x.pList->nExpr;
9832598828
}
9832698829
}else if( pTerm->eOperator & WO_ISNULL ){
9832798830
wsFlags |= WHERE_COLUMN_NULL;
9832898831
}
98832
+#ifdef SQLITE_ENABLE_STAT2
98833
+ if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
98834
+#endif
9832998835
used |= pTerm->prereqRight;
9833098836
}
9833198837
9833298838
/* Determine the value of estBound. */
9833398839
if( nEq<pProbe->nColumn ){
@@ -98359,12 +98865,13 @@
9835998865
/* If there is an ORDER BY clause and the index being considered will
9836098866
** naturally scan rows in the required order, set the appropriate flags
9836198867
** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
9836298868
** will scan rows in a different order, set the bSort variable. */
9836398869
if( pOrderBy ){
98364
- if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0
98365
- && isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev)
98870
+ if( (wsFlags & WHERE_COLUMN_IN)==0
98871
+ && isSortingIndex(pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy,
98872
+ nEq, wsFlags, &rev)
9836698873
){
9836798874
wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
9836898875
wsFlags |= (rev ? WHERE_REVERSE : 0);
9836998876
}else{
9837098877
bSort = 1;
@@ -98391,44 +98898,101 @@
9839198898
bLookup = 1;
9839298899
}
9839398900
}
9839498901
9839598902
/*
98396
- ** Estimate the number of rows of output. For an IN operator,
98397
- ** do not let the estimate exceed half the rows in the table.
98903
+ ** Estimate the number of rows of output. For an "x IN (SELECT...)"
98904
+ ** constraint, do not let the estimate exceed half the rows in the table.
9839898905
*/
9839998906
nRow = (double)(aiRowEst[nEq] * nInMul);
9840098907
if( bInEst && nRow*2>aiRowEst[0] ){
9840198908
nRow = aiRowEst[0]/2;
9840298909
nInMul = (int)(nRow / aiRowEst[nEq]);
9840398910
}
9840498911
98405
- /* Assume constant cost to access a row and logarithmic cost to
98406
- ** do a binary search. Hence, the initial cost is the number of output
98407
- ** rows plus log2(table-size) times the number of binary searches.
98912
+#ifdef SQLITE_ENABLE_STAT2
98913
+ /* If the constraint is of the form x=VALUE and histogram
98914
+ ** data is available for column x, then it might be possible
98915
+ ** to get a better estimate on the number of rows based on
98916
+ ** VALUE and how common that value is according to the histogram.
9840898917
*/
98409
- cost = nRow + nInMul*estLog(aiRowEst[0]);
98918
+ if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
98919
+ if( pFirstTerm->eOperator==WO_EQ ){
98920
+ whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
98921
+ }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
98922
+ whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
98923
+ }
98924
+ }
98925
+#endif /* SQLITE_ENABLE_STAT2 */
9841098926
98411
- /* Adjust the number of rows and the cost downward to reflect rows
98927
+ /* Adjust the number of output rows and downward to reflect rows
9841298928
** that are excluded by range constraints.
9841398929
*/
9841498930
nRow = (nRow * (double)estBound) / (double)100;
98415
- cost = (cost * (double)estBound) / (double)100;
98931
+ if( nRow<1 ) nRow = 1;
9841698932
98417
- /* Add in the estimated cost of sorting the result
98933
+ /* Experiments run on real SQLite databases show that the time needed
98934
+ ** to do a binary search to locate a row in a table or index is roughly
98935
+ ** log10(N) times the time to move from one row to the next row within
98936
+ ** a table or index. The actual times can vary, with the size of
98937
+ ** records being an important factor. Both moves and searches are
98938
+ ** slower with larger records, presumably because fewer records fit
98939
+ ** on one page and hence more pages have to be fetched.
98940
+ **
98941
+ ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
98942
+ ** not give us data on the relative sizes of table and index records.
98943
+ ** So this computation assumes table records are about twice as big
98944
+ ** as index records
98945
+ */
98946
+ if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
98947
+ /* The cost of a full table scan is a number of move operations equal
98948
+ ** to the number of rows in the table.
98949
+ **
98950
+ ** We add an additional 4x penalty to full table scans. This causes
98951
+ ** the cost function to err on the side of choosing an index over
98952
+ ** choosing a full scan. This 4x full-scan penalty is an arguable
98953
+ ** decision and one which we expect to revisit in the future. But
98954
+ ** it seems to be working well enough at the moment.
98955
+ */
98956
+ cost = aiRowEst[0]*4;
98957
+ }else{
98958
+ log10N = estLog(aiRowEst[0]);
98959
+ cost = nRow;
98960
+ if( pIdx ){
98961
+ if( bLookup ){
98962
+ /* For an index lookup followed by a table lookup:
98963
+ ** nInMul index searches to find the start of each index range
98964
+ ** + nRow steps through the index
98965
+ ** + nRow table searches to lookup the table entry using the rowid
98966
+ */
98967
+ cost += (nInMul + nRow)*log10N;
98968
+ }else{
98969
+ /* For a covering index:
98970
+ ** nInMul index searches to find the initial entry
98971
+ ** + nRow steps through the index
98972
+ */
98973
+ cost += nInMul*log10N;
98974
+ }
98975
+ }else{
98976
+ /* For a rowid primary key lookup:
98977
+ ** nInMult table searches to find the initial entry for each range
98978
+ ** + nRow steps through the table
98979
+ */
98980
+ cost += nInMul*log10N;
98981
+ }
98982
+ }
98983
+
98984
+ /* Add in the estimated cost of sorting the result. Actual experimental
98985
+ ** measurements of sorting performance in SQLite show that sorting time
98986
+ ** adds C*N*log10(N) to the cost, where N is the number of rows to be
98987
+ ** sorted and C is a factor between 1.95 and 4.3. We will split the
98988
+ ** difference and select C of 3.0.
9841898989
*/
9841998990
if( bSort ){
98420
- cost += cost*estLog(cost);
98991
+ cost += nRow*estLog(nRow)*3;
9842198992
}
9842298993
98423
- /* If all information can be taken directly from the index, we avoid
98424
- ** doing table lookups. This reduces the cost by half. (Not really -
98425
- ** this needs to be fixed.)
98426
- */
98427
- if( pIdx && bLookup==0 ){
98428
- cost /= (double)2;
98429
- }
9843098994
/**** Cost of using this index has now been computed ****/
9843198995
9843298996
/* If there are additional constraints on this table that cannot
9843398997
** be used with the current index, but which might lower the number
9843498998
** of output rows, adjust the nRow value accordingly. This only
@@ -98465,19 +99029,23 @@
9846599029
** set size by a factor of 10 */
9846699030
nRow /= 10;
9846799031
}
9846899032
}else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
9846999033
if( nSkipRange ){
98470
- /* Ignore the first nBound range constraints since the index
99034
+ /* Ignore the first nSkipRange range constraints since the index
9847199035
** has already accounted for these */
9847299036
nSkipRange--;
9847399037
}else{
9847499038
/* Assume each additional range constraint reduces the result
98475
- ** set size by a factor of 3 */
99039
+ ** set size by a factor of 3. Indexed range constraints reduce
99040
+ ** the search space by a larger factor: 4. We make indexed range
99041
+ ** more selective intentionally because of the subjective
99042
+ ** observation that indexed range constraints really are more
99043
+ ** selective in practice, on average. */
9847699044
nRow /= 3;
9847799045
}
98478
- }else{
99046
+ }else if( pTerm->eOperator!=WO_NOOP ){
9847999047
/* Any other expression lowers the output row count by half */
9848099048
nRow /= 2;
9848199049
}
9848299050
}
9848399051
if( nRow<2 ) nRow = 2;
@@ -98484,14 +99052,14 @@
9848499052
}
9848599053
9848699054
9848799055
WHERETRACE((
9848899056
"%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
98489
- " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
99057
+ " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
9849099058
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
9849199059
nEq, nInMul, estBound, bSort, bLookup, wsFlags,
98492
- notReady, nRow, cost, used
99060
+ notReady, log10N, nRow, cost, used
9849399061
));
9849499062
9849599063
/* If this index is the best we have seen so far, then record this
9849699064
** index and its cost in the pCost structure.
9849799065
*/
@@ -99311,11 +99879,13 @@
9931199879
/* Seek the index cursor to the start of the range. */
9931299880
nConstraint = nEq;
9931399881
if( pRangeStart ){
9931499882
Expr *pRight = pRangeStart->pExpr->pRight;
9931599883
sqlite3ExprCode(pParse, pRight, regBase+nEq);
99316
- sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99884
+ if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
99885
+ sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99886
+ }
9931799887
if( zStartAff ){
9931899888
if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
9931999889
/* Since the comparison is to be performed with no conversions
9932099890
** applied to the operands, set the affinity to apply to pRight to
9932199891
** SQLITE_AFF_NONE. */
@@ -99350,11 +99920,13 @@
9935099920
nConstraint = nEq;
9935199921
if( pRangeEnd ){
9935299922
Expr *pRight = pRangeEnd->pExpr->pRight;
9935399923
sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
9935499924
sqlite3ExprCode(pParse, pRight, regBase+nEq);
99355
- sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99925
+ if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
99926
+ sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99927
+ }
9935699928
if( zEndAff ){
9935799929
if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
9935899930
/* Since the comparison is to be performed with no conversions
9935999931
** applied to the operands, set the affinity to apply to pRight to
9936099932
** SQLITE_AFF_NONE. */
@@ -105802,11 +106374,11 @@
105802106374
/* SQLITE_NOMEM */ "out of memory",
105803106375
/* SQLITE_READONLY */ "attempt to write a readonly database",
105804106376
/* SQLITE_INTERRUPT */ "interrupted",
105805106377
/* SQLITE_IOERR */ "disk I/O error",
105806106378
/* SQLITE_CORRUPT */ "database disk image is malformed",
105807
- /* SQLITE_NOTFOUND */ 0,
106379
+ /* SQLITE_NOTFOUND */ "unknown operation",
105808106380
/* SQLITE_FULL */ "database or disk is full",
105809106381
/* SQLITE_CANTOPEN */ "unable to open database file",
105810106382
/* SQLITE_PROTOCOL */ "locking protocol",
105811106383
/* SQLITE_EMPTY */ "table contains no data",
105812106384
/* SQLITE_SCHEMA */ "database schema has changed",
@@ -106326,40 +106898,64 @@
106326106898
#else
106327106899
return 0;
106328106900
#endif
106329106901
}
106330106902
106331
-
106332106903
/*
106333
-** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
106334
-** to contains a zero-length string, all attached databases are
106335
-** checkpointed.
106904
+** Checkpoint database zDb.
106336106905
*/
106337
-SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
106906
+SQLITE_API int sqlite3_wal_checkpoint_v2(
106907
+ sqlite3 *db, /* Database handle */
106908
+ const char *zDb, /* Name of attached database (or NULL) */
106909
+ int eMode, /* SQLITE_CHECKPOINT_* value */
106910
+ int *pnLog, /* OUT: Size of WAL log in frames */
106911
+ int *pnCkpt /* OUT: Total number of frames checkpointed */
106912
+){
106338106913
#ifdef SQLITE_OMIT_WAL
106339106914
return SQLITE_OK;
106340106915
#else
106341106916
int rc; /* Return code */
106342106917
int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
106918
+
106919
+ /* Initialize the output variables to -1 in case an error occurs. */
106920
+ if( pnLog ) *pnLog = -1;
106921
+ if( pnCkpt ) *pnCkpt = -1;
106922
+
106923
+ assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
106924
+ assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
106925
+ assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
106926
+ if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
106927
+ return SQLITE_MISUSE;
106928
+ }
106343106929
106344106930
sqlite3_mutex_enter(db->mutex);
106345106931
if( zDb && zDb[0] ){
106346106932
iDb = sqlite3FindDbName(db, zDb);
106347106933
}
106348106934
if( iDb<0 ){
106349106935
rc = SQLITE_ERROR;
106350106936
sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
106351106937
}else{
106352
- rc = sqlite3Checkpoint(db, iDb);
106938
+ rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
106353106939
sqlite3Error(db, rc, 0);
106354106940
}
106355106941
rc = sqlite3ApiExit(db, rc);
106356106942
sqlite3_mutex_leave(db->mutex);
106357106943
return rc;
106358106944
#endif
106359106945
}
106360106946
106947
+
106948
+/*
106949
+** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
106950
+** to contains a zero-length string, all attached databases are
106951
+** checkpointed.
106952
+*/
106953
+SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
106954
+ return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
106955
+}
106956
+
106361106957
#ifndef SQLITE_OMIT_WAL
106362106958
/*
106363106959
** Run a checkpoint on database iDb. This is a no-op if database iDb is
106364106960
** not currently open in WAL mode.
106365106961
**
@@ -106373,24 +106969,35 @@
106373106969
** this function while the checkpoint is running.
106374106970
**
106375106971
** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
106376106972
** checkpointed. If an error is encountered it is returned immediately -
106377106973
** no attempt is made to checkpoint any remaining databases.
106974
+**
106975
+** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
106378106976
*/
106379
-SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb){
106977
+SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
106380106978
int rc = SQLITE_OK; /* Return code */
106381106979
int i; /* Used to iterate through attached dbs */
106980
+ int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
106382106981
106383106982
assert( sqlite3_mutex_held(db->mutex) );
106983
+ assert( !pnLog || *pnLog==-1 );
106984
+ assert( !pnCkpt || *pnCkpt==-1 );
106384106985
106385106986
for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
106386106987
if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
106387
- rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt);
106988
+ rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
106989
+ pnLog = 0;
106990
+ pnCkpt = 0;
106991
+ if( rc==SQLITE_BUSY ){
106992
+ bBusy = 1;
106993
+ rc = SQLITE_OK;
106994
+ }
106388106995
}
106389106996
}
106390106997
106391
- return rc;
106998
+ return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
106392106999
}
106393107000
#endif /* SQLITE_OMIT_WAL */
106394107001
106395107002
/*
106396107003
** This function returns true if main-memory should be used instead of
@@ -107350,10 +107957,12 @@
107350107957
if( op==SQLITE_FCNTL_FILE_POINTER ){
107351107958
*(sqlite3_file**)pArg = fd;
107352107959
rc = SQLITE_OK;
107353107960
}else if( fd->pMethods ){
107354107961
rc = sqlite3OsFileControl(fd, op, pArg);
107962
+ }else{
107963
+ rc = SQLITE_NOTFOUND;
107355107964
}
107356107965
sqlite3BtreeLeave(pBtree);
107357107966
}
107358107967
}
107359107968
sqlite3_mutex_leave(db->mutex);
@@ -108597,11 +109206,11 @@
108597109206
typedef struct Fts3PhraseToken Fts3PhraseToken;
108598109207
108599109208
typedef struct Fts3SegFilter Fts3SegFilter;
108600109209
typedef struct Fts3DeferredToken Fts3DeferredToken;
108601109210
typedef struct Fts3SegReader Fts3SegReader;
108602
-typedef struct Fts3SegReaderArray Fts3SegReaderArray;
109211
+typedef struct Fts3SegReaderCursor Fts3SegReaderCursor;
108603109212
108604109213
/*
108605109214
** A connection to a fulltext index is an instance of the following
108606109215
** structure. The xCreate and xConnect methods create an instance
108607109216
** of this structure and xDestroy and xDisconnect free that instance.
@@ -108620,10 +109229,13 @@
108620109229
/* Precompiled statements used by the implementation. Each of these
108621109230
** statements is run and reset within a single virtual table API call.
108622109231
*/
108623109232
sqlite3_stmt *aStmt[24];
108624109233
109234
+ char *zReadExprlist;
109235
+ char *zWriteExprlist;
109236
+
108625109237
int nNodeSize; /* Soft limit for node size */
108626109238
u8 bHasStat; /* True if %_stat table exists */
108627109239
u8 bHasDocsize; /* True if %_docsize table exists */
108628109240
int nPgsz; /* Page size for host database */
108629109241
char *zSegmentsTbl; /* Name of %_segments table */
@@ -108707,11 +109319,11 @@
108707109319
struct Fts3PhraseToken {
108708109320
char *z; /* Text of the token */
108709109321
int n; /* Number of bytes in buffer z */
108710109322
int isPrefix; /* True if token ends with a "*" character */
108711109323
int bFulltext; /* True if full-text index was used */
108712
- Fts3SegReaderArray *pArray; /* Segment-reader for this token */
109324
+ Fts3SegReaderCursor *pSegcsr; /* Segment-reader for this token */
108713109325
Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
108714109326
};
108715109327
108716109328
struct Fts3Phrase {
108717109329
/* Variables populated by fts3_expr.c when parsing a MATCH expression */
@@ -108775,16 +109387,12 @@
108775109387
SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
108776109388
SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
108777109389
sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
108778109390
SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**);
108779109391
SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
108780
-SQLITE_PRIVATE int sqlite3Fts3SegReaderIterate(
108781
- Fts3Table *, Fts3SegReader **, int, Fts3SegFilter *,
108782
- int (*)(Fts3Table *, void *, char *, int, char *, int), void *
108783
-);
108784109392
SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *);
108785
-SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **);
109393
+SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, sqlite3_stmt **);
108786109394
SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
108787109395
SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*);
108788109396
108789109397
SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
108790109398
SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
@@ -108792,26 +109400,54 @@
108792109400
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
108793109401
SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
108794109402
SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
108795109403
SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
108796109404
SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *);
108797
-
108798109405
SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
108799109406
109407
+#define FTS3_SEGCURSOR_PENDING -1
109408
+#define FTS3_SEGCURSOR_ALL -2
109409
+
109410
+SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3SegReaderCursor*, Fts3SegFilter*);
109411
+SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3SegReaderCursor *);
109412
+SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3SegReaderCursor *);
109413
+SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
109414
+ Fts3Table *, int, const char *, int, int, int, Fts3SegReaderCursor *);
109415
+
108800109416
/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
108801109417
#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
108802109418
#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
108803109419
#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
108804109420
#define FTS3_SEGMENT_PREFIX 0x00000008
109421
+#define FTS3_SEGMENT_SCAN 0x00000010
108805109422
108806109423
/* Type passed as 4th argument to SegmentReaderIterate() */
108807109424
struct Fts3SegFilter {
108808109425
const char *zTerm;
108809109426
int nTerm;
108810109427
int iCol;
108811109428
int flags;
108812109429
};
109430
+
109431
+struct Fts3SegReaderCursor {
109432
+ /* Used internally by sqlite3Fts3SegReaderXXX() calls */
109433
+ Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
109434
+ int nSegment; /* Size of apSegment array */
109435
+ int nAdvance; /* How many seg-readers to advance */
109436
+ Fts3SegFilter *pFilter; /* Pointer to filter object */
109437
+ char *aBuffer; /* Buffer to merge doclists in */
109438
+ int nBuffer; /* Allocated size of aBuffer[] in bytes */
109439
+
109440
+ /* Cost of running this iterator. Used by fts3.c only. */
109441
+ int nCost;
109442
+
109443
+ /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
109444
+ char *zTerm; /* Pointer to term buffer */
109445
+ int nTerm; /* Size of zTerm in bytes */
109446
+ char *aDoclist; /* Pointer to doclist buffer */
109447
+ int nDoclist; /* Size of aDoclist[] in bytes */
109448
+};
108813109449
108814109450
/* fts3.c */
108815109451
SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
108816109452
SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
108817109453
SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
@@ -108845,10 +109481,13 @@
108845109481
SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
108846109482
#ifdef SQLITE_TEST
108847109483
SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
108848109484
#endif
108849109485
109486
+/* fts3_aux.c */
109487
+SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
109488
+
108850109489
#endif /* _FTSINT_H */
108851109490
108852109491
/************** End of fts3Int.h *********************************************/
108853109492
/************** Continuing where we left off in fts3.c ***********************/
108854109493
@@ -108990,10 +109629,12 @@
108990109629
/* Free any prepared statements held */
108991109630
for(i=0; i<SizeofArray(p->aStmt); i++){
108992109631
sqlite3_finalize(p->aStmt[i]);
108993109632
}
108994109633
sqlite3_free(p->zSegmentsTbl);
109634
+ sqlite3_free(p->zReadExprlist);
109635
+ sqlite3_free(p->zWriteExprlist);
108995109636
108996109637
/* Invoke the tokenizer destructor to free the tokenizer. */
108997109638
p->pTokenizer->pModule->xDestroy(p->pTokenizer);
108998109639
108999109640
sqlite3_free(p);
@@ -109206,10 +109847,145 @@
109206109847
sqlite3Fts3Dequote(zValue);
109207109848
}
109208109849
*pzValue = zValue;
109209109850
return 1;
109210109851
}
109852
+
109853
+/*
109854
+** Append the output of a printf() style formatting to an existing string.
109855
+*/
109856
+static void fts3Appendf(
109857
+ int *pRc, /* IN/OUT: Error code */
109858
+ char **pz, /* IN/OUT: Pointer to string buffer */
109859
+ const char *zFormat, /* Printf format string to append */
109860
+ ... /* Arguments for printf format string */
109861
+){
109862
+ if( *pRc==SQLITE_OK ){
109863
+ va_list ap;
109864
+ char *z;
109865
+ va_start(ap, zFormat);
109866
+ z = sqlite3_vmprintf(zFormat, ap);
109867
+ if( z && *pz ){
109868
+ char *z2 = sqlite3_mprintf("%s%s", *pz, z);
109869
+ sqlite3_free(z);
109870
+ z = z2;
109871
+ }
109872
+ if( z==0 ) *pRc = SQLITE_NOMEM;
109873
+ sqlite3_free(*pz);
109874
+ *pz = z;
109875
+ }
109876
+}
109877
+
109878
+/*
109879
+** Return a copy of input string zInput enclosed in double-quotes (") and
109880
+** with all double quote characters escaped. For example:
109881
+**
109882
+** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
109883
+**
109884
+** The pointer returned points to memory obtained from sqlite3_malloc(). It
109885
+** is the callers responsibility to call sqlite3_free() to release this
109886
+** memory.
109887
+*/
109888
+static char *fts3QuoteId(char const *zInput){
109889
+ int nRet;
109890
+ char *zRet;
109891
+ nRet = 2 + strlen(zInput)*2 + 1;
109892
+ zRet = sqlite3_malloc(nRet);
109893
+ if( zRet ){
109894
+ int i;
109895
+ char *z = zRet;
109896
+ *(z++) = '"';
109897
+ for(i=0; zInput[i]; i++){
109898
+ if( zInput[i]=='"' ) *(z++) = '"';
109899
+ *(z++) = zInput[i];
109900
+ }
109901
+ *(z++) = '"';
109902
+ *(z++) = '\0';
109903
+ }
109904
+ return zRet;
109905
+}
109906
+
109907
+/*
109908
+** Return a list of comma separated SQL expressions that could be used
109909
+** in a SELECT statement such as the following:
109910
+**
109911
+** SELECT <list of expressions> FROM %_content AS x ...
109912
+**
109913
+** to return the docid, followed by each column of text data in order
109914
+** from left to write. If parameter zFunc is not NULL, then instead of
109915
+** being returned directly each column of text data is passed to an SQL
109916
+** function named zFunc first. For example, if zFunc is "unzip" and the
109917
+** table has the three user-defined columns "a", "b", and "c", the following
109918
+** string is returned:
109919
+**
109920
+** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
109921
+**
109922
+** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
109923
+** is the responsibility of the caller to eventually free it.
109924
+**
109925
+** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
109926
+** a NULL pointer is returned). Otherwise, if an OOM error is encountered
109927
+** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
109928
+** no error occurs, *pRc is left unmodified.
109929
+*/
109930
+static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
109931
+ char *zRet = 0;
109932
+ char *zFree = 0;
109933
+ char *zFunction;
109934
+ int i;
109935
+
109936
+ if( !zFunc ){
109937
+ zFunction = "";
109938
+ }else{
109939
+ zFree = zFunction = fts3QuoteId(zFunc);
109940
+ }
109941
+ fts3Appendf(pRc, &zRet, "docid");
109942
+ for(i=0; i<p->nColumn; i++){
109943
+ fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
109944
+ }
109945
+ sqlite3_free(zFree);
109946
+ return zRet;
109947
+}
109948
+
109949
+/*
109950
+** Return a list of N comma separated question marks, where N is the number
109951
+** of columns in the %_content table (one for the docid plus one for each
109952
+** user-defined text column).
109953
+**
109954
+** If argument zFunc is not NULL, then all but the first question mark
109955
+** is preceded by zFunc and an open bracket, and followed by a closed
109956
+** bracket. For example, if zFunc is "zip" and the FTS3 table has three
109957
+** user-defined text columns, the following string is returned:
109958
+**
109959
+** "?, zip(?), zip(?), zip(?)"
109960
+**
109961
+** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
109962
+** is the responsibility of the caller to eventually free it.
109963
+**
109964
+** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
109965
+** a NULL pointer is returned). Otherwise, if an OOM error is encountered
109966
+** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
109967
+** no error occurs, *pRc is left unmodified.
109968
+*/
109969
+static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
109970
+ char *zRet = 0;
109971
+ char *zFree = 0;
109972
+ char *zFunction;
109973
+ int i;
109974
+
109975
+ if( !zFunc ){
109976
+ zFunction = "";
109977
+ }else{
109978
+ zFree = zFunction = fts3QuoteId(zFunc);
109979
+ }
109980
+ fts3Appendf(pRc, &zRet, "?");
109981
+ for(i=0; i<p->nColumn; i++){
109982
+ fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
109983
+ }
109984
+ sqlite3_free(zFree);
109985
+ return zRet;
109986
+}
109211109987
109212109988
/*
109213109989
** This function is the implementation of both the xConnect and xCreate
109214109990
** methods of the FTS3 virtual table.
109215109991
**
@@ -109243,10 +110019,13 @@
109243110019
int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
109244110020
int bNoDocsize = 0; /* True to omit %_docsize table */
109245110021
const char **aCol; /* Array of column names */
109246110022
sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
109247110023
110024
+ char *zCompress = 0;
110025
+ char *zUncompress = 0;
110026
+
109248110027
assert( strlen(argv[0])==4 );
109249110028
assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
109250110029
|| (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
109251110030
);
109252110031
@@ -109293,10 +110072,16 @@
109293110072
bNoDocsize = 1;
109294110073
}else{
109295110074
*pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
109296110075
rc = SQLITE_ERROR;
109297110076
}
110077
+ }else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){
110078
+ zCompress = zVal;
110079
+ zVal = 0;
110080
+ }else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){
110081
+ zUncompress = zVal;
110082
+ zVal = 0;
109298110083
}else{
109299110084
*pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
109300110085
rc = SQLITE_ERROR;
109301110086
}
109302110087
sqlite3_free(zVal);
@@ -109366,10 +110151,19 @@
109366110151
sqlite3Fts3Dequote(zCsr);
109367110152
p->azColumn[iCol] = zCsr;
109368110153
zCsr += n+1;
109369110154
assert( zCsr <= &((char *)p)[nByte] );
109370110155
}
110156
+
110157
+ if( (zCompress==0)!=(zUncompress==0) ){
110158
+ char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
110159
+ rc = SQLITE_ERROR;
110160
+ *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
110161
+ }
110162
+ p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
110163
+ p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
110164
+ if( rc!=SQLITE_OK ) goto fts3_init_out;
109371110165
109372110166
/* If this is an xCreate call, create the underlying tables in the
109373110167
** database. TODO: For xConnect(), it could verify that said tables exist.
109374110168
*/
109375110169
if( isCreate ){
@@ -109384,11 +110178,12 @@
109384110178
109385110179
/* Declare the table schema to SQLite. */
109386110180
fts3DeclareVtab(&rc, p);
109387110181
109388110182
fts3_init_out:
109389
-
110183
+ sqlite3_free(zCompress);
110184
+ sqlite3_free(zUncompress);
109390110185
sqlite3_free((void *)aCol);
109391110186
if( rc!=SQLITE_OK ){
109392110187
if( p ){
109393110188
fts3DisconnectMethod((sqlite3_vtab *)p);
109394110189
}else if( pTokenizer ){
@@ -110477,134 +111272,137 @@
110477111272
}
110478111273
110479111274
return SQLITE_OK;
110480111275
}
110481111276
110482
-/*
110483
-** An Fts3SegReaderArray is used to store an array of Fts3SegReader objects.
110484
-** Elements are added to the array using fts3SegReaderArrayAdd().
110485
-*/
110486
-struct Fts3SegReaderArray {
110487
- int nSegment; /* Number of valid entries in apSegment[] */
110488
- int nAlloc; /* Allocated size of apSegment[] */
110489
- int nCost; /* The cost of executing SegReaderIterate() */
110490
- Fts3SegReader *apSegment[1]; /* Array of seg-reader objects */
110491
-};
110492
-
110493
-
110494
-/*
110495
-** Free an Fts3SegReaderArray object. Also free all seg-readers in the
110496
-** array (using sqlite3Fts3SegReaderFree()).
110497
-*/
110498
-static void fts3SegReaderArrayFree(Fts3SegReaderArray *pArray){
110499
- if( pArray ){
110500
- int i;
110501
- for(i=0; i<pArray->nSegment; i++){
110502
- sqlite3Fts3SegReaderFree(pArray->apSegment[i]);
110503
- }
110504
- sqlite3_free(pArray);
110505
- }
110506
-}
110507
-
110508
-static int fts3SegReaderArrayAdd(
110509
- Fts3SegReaderArray **ppArray,
110510
- Fts3SegReader *pNew
110511
-){
110512
- Fts3SegReaderArray *pArray = *ppArray;
110513
-
110514
- if( !pArray || pArray->nAlloc==pArray->nSegment ){
110515
- int nNew = (pArray ? pArray->nAlloc+16 : 16);
110516
- pArray = (Fts3SegReaderArray *)sqlite3_realloc(pArray,
110517
- sizeof(Fts3SegReaderArray) + (nNew-1) * sizeof(Fts3SegReader*)
110518
- );
110519
- if( !pArray ){
110520
- sqlite3Fts3SegReaderFree(pNew);
110521
- return SQLITE_NOMEM;
110522
- }
110523
- if( nNew==16 ){
110524
- pArray->nSegment = 0;
110525
- pArray->nCost = 0;
110526
- }
110527
- pArray->nAlloc = nNew;
110528
- *ppArray = pArray;
110529
- }
110530
-
110531
- pArray->apSegment[pArray->nSegment++] = pNew;
110532
- return SQLITE_OK;
110533
-}
110534
-
110535
-static int fts3TermSegReaderArray(
111277
+SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
111278
+ Fts3Table *p, /* FTS3 table handle */
111279
+ int iLevel, /* Level of segments to scan */
111280
+ const char *zTerm, /* Term to query for */
111281
+ int nTerm, /* Size of zTerm in bytes */
111282
+ int isPrefix, /* True for a prefix search */
111283
+ int isScan, /* True to scan from zTerm to EOF */
111284
+ Fts3SegReaderCursor *pCsr /* Cursor object to populate */
111285
+){
111286
+ int rc = SQLITE_OK;
111287
+ int rc2;
111288
+ int iAge = 0;
111289
+ sqlite3_stmt *pStmt = 0;
111290
+ Fts3SegReader *pPending = 0;
111291
+
111292
+ assert( iLevel==FTS3_SEGCURSOR_ALL
111293
+ || iLevel==FTS3_SEGCURSOR_PENDING
111294
+ || iLevel>=0
111295
+ );
111296
+ assert( FTS3_SEGCURSOR_PENDING<0 );
111297
+ assert( FTS3_SEGCURSOR_ALL<0 );
111298
+ assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) );
111299
+ assert( isPrefix==0 || isScan==0 );
111300
+
111301
+
111302
+ memset(pCsr, 0, sizeof(Fts3SegReaderCursor));
111303
+
111304
+ /* If iLevel is less than 0, include a seg-reader for the pending-terms. */
111305
+ assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 );
111306
+ if( iLevel<0 && isScan==0 ){
111307
+ rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending);
111308
+ if( rc==SQLITE_OK && pPending ){
111309
+ int nByte = (sizeof(Fts3SegReader *) * 16);
111310
+ pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
111311
+ if( pCsr->apSegment==0 ){
111312
+ rc = SQLITE_NOMEM;
111313
+ }else{
111314
+ pCsr->apSegment[0] = pPending;
111315
+ pCsr->nSegment = 1;
111316
+ pPending = 0;
111317
+ }
111318
+ }
111319
+ }
111320
+
111321
+ if( iLevel!=FTS3_SEGCURSOR_PENDING ){
111322
+ if( rc==SQLITE_OK ){
111323
+ rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt);
111324
+ }
111325
+ while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
111326
+
111327
+ /* Read the values returned by the SELECT into local variables. */
111328
+ sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
111329
+ sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
111330
+ sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
111331
+ int nRoot = sqlite3_column_bytes(pStmt, 4);
111332
+ char const *zRoot = sqlite3_column_blob(pStmt, 4);
111333
+
111334
+ /* If nSegment is a multiple of 16 the array needs to be extended. */
111335
+ if( (pCsr->nSegment%16)==0 ){
111336
+ Fts3SegReader **apNew;
111337
+ int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
111338
+ apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
111339
+ if( !apNew ){
111340
+ rc = SQLITE_NOMEM;
111341
+ goto finished;
111342
+ }
111343
+ pCsr->apSegment = apNew;
111344
+ }
111345
+
111346
+ /* If zTerm is not NULL, and this segment is not stored entirely on its
111347
+ ** root node, the range of leaves scanned can be reduced. Do this. */
111348
+ if( iStartBlock && zTerm ){
111349
+ sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
111350
+ rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
111351
+ if( rc!=SQLITE_OK ) goto finished;
111352
+ if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
111353
+ }
111354
+
111355
+ rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock,
111356
+ iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment]
111357
+ );
111358
+ if( rc!=SQLITE_OK ) goto finished;
111359
+ pCsr->nSegment++;
111360
+ iAge++;
111361
+ }
111362
+ }
111363
+
111364
+ finished:
111365
+ rc2 = sqlite3_reset(pStmt);
111366
+ if( rc==SQLITE_DONE ) rc = rc2;
111367
+ sqlite3Fts3SegReaderFree(pPending);
111368
+
111369
+ return rc;
111370
+}
111371
+
111372
+
111373
+static int fts3TermSegReaderCursor(
110536111374
Fts3Cursor *pCsr, /* Virtual table cursor handle */
110537111375
const char *zTerm, /* Term to query for */
110538111376
int nTerm, /* Size of zTerm in bytes */
110539111377
int isPrefix, /* True for a prefix search */
110540
- Fts3SegReaderArray **ppArray /* OUT: Allocated seg-reader array */
110541
-){
110542
- Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
110543
- int rc; /* Return code */
110544
- Fts3SegReaderArray *pArray = 0; /* Array object to build */
110545
- Fts3SegReader *pReader = 0; /* Seg-reader to add to pArray */
110546
- sqlite3_stmt *pStmt = 0; /* SQL statement to scan %_segdir table */
110547
- int iAge = 0; /* Used to assign ages to segments */
110548
-
110549
- /* Allocate a seg-reader to scan the pending terms, if any. */
110550
- rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pReader);
110551
- if( rc==SQLITE_OK && pReader ) {
110552
- rc = fts3SegReaderArrayAdd(&pArray, pReader);
110553
- }
110554
-
110555
- /* Loop through the entire %_segdir table. For each segment, create a
110556
- ** Fts3SegReader to iterate through the subset of the segment leaves
110557
- ** that may contain a term that matches zTerm/nTerm. For non-prefix
110558
- ** searches, this is always a single leaf. For prefix searches, this
110559
- ** may be a contiguous block of leaves.
110560
- */
110561
- if( rc==SQLITE_OK ){
110562
- rc = sqlite3Fts3AllSegdirs(p, &pStmt);
110563
- }
110564
- while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
110565
- Fts3SegReader *pNew = 0;
110566
- int nRoot = sqlite3_column_bytes(pStmt, 4);
110567
- char const *zRoot = sqlite3_column_blob(pStmt, 4);
110568
- if( sqlite3_column_int64(pStmt, 1)==0 ){
110569
- /* The entire segment is stored on the root node (which must be a
110570
- ** leaf). Do not bother inspecting any data in this case, just
110571
- ** create a Fts3SegReader to scan the single leaf.
110572
- */
110573
- rc = sqlite3Fts3SegReaderNew(iAge, 0, 0, 0, zRoot, nRoot, &pNew);
110574
- }else{
110575
- sqlite3_int64 i1; /* First leaf that may contain zTerm */
110576
- sqlite3_int64 i2; /* Final leaf that may contain zTerm */
110577
- rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &i1, (isPrefix?&i2:0));
110578
- if( isPrefix==0 ) i2 = i1;
110579
- if( rc==SQLITE_OK ){
110580
- rc = sqlite3Fts3SegReaderNew(iAge, i1, i2, 0, 0, 0, &pNew);
110581
- }
110582
- }
110583
- assert( (pNew==0)==(rc!=SQLITE_OK) );
110584
-
110585
- /* If a new Fts3SegReader was allocated, add it to the array. */
110586
- if( rc==SQLITE_OK ){
110587
- rc = fts3SegReaderArrayAdd(&pArray, pNew);
110588
- }
110589
- if( rc==SQLITE_OK ){
110590
- rc = sqlite3Fts3SegReaderCost(pCsr, pNew, &pArray->nCost);
110591
- }
110592
- iAge++;
110593
- }
110594
-
110595
- if( rc==SQLITE_DONE ){
110596
- rc = sqlite3_reset(pStmt);
110597
- }else{
110598
- sqlite3_reset(pStmt);
110599
- }
110600
- if( rc!=SQLITE_OK ){
110601
- fts3SegReaderArrayFree(pArray);
110602
- pArray = 0;
110603
- }
110604
- *ppArray = pArray;
110605
- return rc;
111378
+ Fts3SegReaderCursor **ppSegcsr /* OUT: Allocated seg-reader cursor */
111379
+){
111380
+ Fts3SegReaderCursor *pSegcsr; /* Object to allocate and return */
111381
+ int rc = SQLITE_NOMEM; /* Return code */
111382
+
111383
+ pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor));
111384
+ if( pSegcsr ){
111385
+ Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
111386
+ int i;
111387
+ int nCost = 0;
111388
+ rc = sqlite3Fts3SegReaderCursor(
111389
+ p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr);
111390
+
111391
+ for(i=0; rc==SQLITE_OK && i<pSegcsr->nSegment; i++){
111392
+ rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost);
111393
+ }
111394
+ pSegcsr->nCost = nCost;
111395
+ }
111396
+
111397
+ *ppSegcsr = pSegcsr;
111398
+ return rc;
111399
+}
111400
+
111401
+static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){
111402
+ sqlite3Fts3SegReaderFinish(pSegcsr);
111403
+ sqlite3_free(pSegcsr);
110606111404
}
110607111405
110608111406
/*
110609111407
** This function retreives the doclist for the specified term (or term
110610111408
** prefix) from the database.
@@ -110623,15 +111421,15 @@
110623111421
int isReqPos, /* True to include position lists in output */
110624111422
int *pnOut, /* OUT: Size of buffer at *ppOut */
110625111423
char **ppOut /* OUT: Malloced result buffer */
110626111424
){
110627111425
int rc; /* Return code */
110628
- Fts3SegReaderArray *pArray; /* Seg-reader array for this term */
110629
- TermSelect tsc; /* Context object for fts3TermSelectCb() */
110630
- Fts3SegFilter filter; /* Segment term filter configuration */
111426
+ Fts3SegReaderCursor *pSegcsr; /* Seg-reader cursor for this term */
111427
+ TermSelect tsc; /* Context object for fts3TermSelectCb() */
111428
+ Fts3SegFilter filter; /* Segment term filter configuration */
110631111429
110632
- pArray = pTok->pArray;
111430
+ pSegcsr = pTok->pSegcsr;
110633111431
memset(&tsc, 0, sizeof(TermSelect));
110634111432
tsc.isReqPos = isReqPos;
110635111433
110636111434
filter.flags = FTS3_SEGMENT_IGNORE_EMPTY
110637111435
| (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
@@ -110639,17 +111437,22 @@
110639111437
| (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
110640111438
filter.iCol = iColumn;
110641111439
filter.zTerm = pTok->z;
110642111440
filter.nTerm = pTok->n;
110643111441
110644
- rc = sqlite3Fts3SegReaderIterate(p, pArray->apSegment, pArray->nSegment,
110645
- &filter, fts3TermSelectCb, (void *)&tsc
110646
- );
111442
+ rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
111443
+ while( SQLITE_OK==rc
111444
+ && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
111445
+ ){
111446
+ rc = fts3TermSelectCb(p, (void *)&tsc,
111447
+ pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist
111448
+ );
111449
+ }
111450
+
110647111451
if( rc==SQLITE_OK ){
110648111452
rc = fts3TermSelectMerge(&tsc);
110649111453
}
110650
-
110651111454
if( rc==SQLITE_OK ){
110652111455
*ppOut = tsc.aaOutput[0];
110653111456
*pnOut = tsc.anOutput[0];
110654111457
}else{
110655111458
int i;
@@ -110656,12 +111459,12 @@
110656111459
for(i=0; i<SizeofArray(tsc.aaOutput); i++){
110657111460
sqlite3_free(tsc.aaOutput[i]);
110658111461
}
110659111462
}
110660111463
110661
- fts3SegReaderArrayFree(pArray);
110662
- pTok->pArray = 0;
111464
+ fts3SegReaderCursorFree(pSegcsr);
111465
+ pTok->pSegcsr = 0;
110663111466
return rc;
110664111467
}
110665111468
110666111469
/*
110667111470
** This function counts the total number of docids in the doclist stored
@@ -110780,17 +111583,17 @@
110780111583
** evaluation, only create segment-readers if there are no Fts3DeferredToken
110781111584
** objects attached to the phrase-tokens.
110782111585
*/
110783111586
for(ii=0; ii<pPhrase->nToken; ii++){
110784111587
Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
110785
- if( pTok->pArray==0 ){
111588
+ if( pTok->pSegcsr==0 ){
110786111589
if( (pCsr->eEvalmode==FTS3_EVAL_FILTER)
110787111590
|| (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0)
110788111591
|| (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext)
110789111592
){
110790
- rc = fts3TermSegReaderArray(
110791
- pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pArray
111593
+ rc = fts3TermSegReaderCursor(
111594
+ pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
110792111595
);
110793111596
if( rc!=SQLITE_OK ) return rc;
110794111597
}
110795111598
}
110796111599
}
@@ -110817,14 +111620,14 @@
110817111620
int nMinCost = 0x7FFFFFFF;
110818111621
int jj;
110819111622
110820111623
/* Find the remaining token with the lowest cost. */
110821111624
for(jj=0; jj<pPhrase->nToken; jj++){
110822
- Fts3SegReaderArray *pArray = pPhrase->aToken[jj].pArray;
110823
- if( pArray && pArray->nCost<nMinCost ){
111625
+ Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr;
111626
+ if( pSegcsr && pSegcsr->nCost<nMinCost ){
110824111627
iTok = jj;
110825
- nMinCost = pArray->nCost;
111628
+ nMinCost = pSegcsr->nCost;
110826111629
}
110827111630
}
110828111631
pTok = &pPhrase->aToken[iTok];
110829111632
110830111633
/* This branch is taken if it is determined that loading the doclist
@@ -110839,16 +111642,16 @@
110839111642
}
110840111643
110841111644
if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){
110842111645
rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList);
110843111646
}else{
110844
- if( pTok->pArray ){
111647
+ if( pTok->pSegcsr ){
110845111648
rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList);
110846111649
}
110847111650
pTok->bFulltext = 1;
110848111651
}
110849
- assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pArray==0 );
111652
+ assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 );
110850111653
if( rc!=SQLITE_OK ) break;
110851111654
110852111655
if( isFirst ){
110853111656
pOut = pList;
110854111657
nOut = nList;
@@ -111022,13 +111825,13 @@
111022111825
Fts3Phrase *pPhrase = pExpr->pPhrase;
111023111826
int ii;
111024111827
111025111828
for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
111026111829
Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
111027
- if( pTok->pArray==0 ){
111028
- rc = fts3TermSegReaderArray(
111029
- pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pArray
111830
+ if( pTok->pSegcsr==0 ){
111831
+ rc = fts3TermSegReaderCursor(
111832
+ pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
111030111833
);
111031111834
}
111032111835
}
111033111836
}else{
111034111837
rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr);
@@ -111048,12 +111851,12 @@
111048111851
if( pExpr ){
111049111852
Fts3Phrase *pPhrase = pExpr->pPhrase;
111050111853
if( pPhrase ){
111051111854
int kk;
111052111855
for(kk=0; kk<pPhrase->nToken; kk++){
111053
- fts3SegReaderArrayFree(pPhrase->aToken[kk].pArray);
111054
- pPhrase->aToken[kk].pArray = 0;
111856
+ fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr);
111857
+ pPhrase->aToken[kk].pSegcsr = 0;
111055111858
}
111056111859
}
111057111860
fts3ExprFreeSegReaders(pExpr->pLeft);
111058111861
fts3ExprFreeSegReaders(pExpr->pRight);
111059111862
}
@@ -111069,14 +111872,12 @@
111069111872
if( pExpr->eType==FTSQUERY_PHRASE ){
111070111873
Fts3Phrase *pPhrase = pExpr->pPhrase;
111071111874
int ii;
111072111875
nCost = 0;
111073111876
for(ii=0; ii<pPhrase->nToken; ii++){
111074
- Fts3SegReaderArray *pArray = pPhrase->aToken[ii].pArray;
111075
- if( pArray ){
111076
- nCost += pPhrase->aToken[ii].pArray->nCost;
111077
- }
111877
+ Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr;
111878
+ if( pSegcsr ) nCost += pSegcsr->nCost;
111078111879
}
111079111880
}else{
111080111881
nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight);
111081111882
}
111082111883
return nCost;
@@ -111414,12 +112215,12 @@
111414112215
const char *idxStr, /* Unused */
111415112216
int nVal, /* Number of elements in apVal */
111416112217
sqlite3_value **apVal /* Arguments for the indexing scheme */
111417112218
){
111418112219
const char *azSql[] = {
111419
- "SELECT * FROM %Q.'%q_content' WHERE docid = ?", /* non-full-table-scan */
111420
- "SELECT * FROM %Q.'%q_content'", /* full-table-scan */
112220
+ "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?", /* non-full-scan */
112221
+ "SELECT %s FROM %Q.'%q_content' AS x ", /* full-scan */
111421112222
};
111422112223
int rc; /* Return code */
111423112224
char *zSql; /* SQL statement used to access %_content */
111424112225
Fts3Table *p = (Fts3Table *)pCursor->pVtab;
111425112226
Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
@@ -111470,11 +112271,12 @@
111470112271
/* Compile a SELECT statement for this cursor. For a full-table-scan, the
111471112272
** statement loops through all rows of the %_content table. For a
111472112273
** full-text query or docid lookup, the statement retrieves a single
111473112274
** row by docid.
111474112275
*/
111475
- zSql = sqlite3_mprintf(azSql[idxNum==FTS3_FULLSCAN_SEARCH], p->zDb, p->zName);
112276
+ zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH];
112277
+ zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName);
111476112278
if( !zSql ){
111477112279
rc = SQLITE_NOMEM;
111478112280
}else{
111479112281
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
111480112282
sqlite3_free(zSql);
@@ -111988,10 +112790,13 @@
111988112790
#ifdef SQLITE_ENABLE_ICU
111989112791
const sqlite3_tokenizer_module *pIcu = 0;
111990112792
sqlite3Fts3IcuTokenizerModule(&pIcu);
111991112793
#endif
111992112794
112795
+ rc = sqlite3Fts3InitAux(db);
112796
+ if( rc!=SQLITE_OK ) return rc;
112797
+
111993112798
sqlite3Fts3SimpleTokenizerModule(&pSimple);
111994112799
sqlite3Fts3PorterTokenizerModule(&pPorter);
111995112800
111996112801
/* Allocate and initialise the hash-table used to store tokenizers. */
111997112802
pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -112063,10 +112868,472 @@
112063112868
#endif
112064112869
112065112870
#endif
112066112871
112067112872
/************** End of fts3.c ************************************************/
112873
+/************** Begin file fts3_aux.c ****************************************/
112874
+/*
112875
+** 2011 Jan 27
112876
+**
112877
+** The author disclaims copyright to this source code. In place of
112878
+** a legal notice, here is a blessing:
112879
+**
112880
+** May you do good and not evil.
112881
+** May you find forgiveness for yourself and forgive others.
112882
+** May you share freely, never taking more than you give.
112883
+**
112884
+******************************************************************************
112885
+**
112886
+*/
112887
+
112888
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
112889
+
112890
+
112891
+typedef struct Fts3auxTable Fts3auxTable;
112892
+typedef struct Fts3auxCursor Fts3auxCursor;
112893
+
112894
+struct Fts3auxTable {
112895
+ sqlite3_vtab base; /* Base class used by SQLite core */
112896
+ Fts3Table *pFts3Tab;
112897
+};
112898
+
112899
+struct Fts3auxCursor {
112900
+ sqlite3_vtab_cursor base; /* Base class used by SQLite core */
112901
+ Fts3SegReaderCursor csr; /* Must be right after "base" */
112902
+ Fts3SegFilter filter;
112903
+ char *zStop;
112904
+ int nStop; /* Byte-length of string zStop */
112905
+ int isEof; /* True if cursor is at EOF */
112906
+ sqlite3_int64 iRowid; /* Current rowid */
112907
+
112908
+ int iCol; /* Current value of 'col' column */
112909
+ int nStat; /* Size of aStat[] array */
112910
+ struct Fts3auxColstats {
112911
+ sqlite3_int64 nDoc; /* 'documents' values for current csr row */
112912
+ sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
112913
+ } *aStat;
112914
+};
112915
+
112916
+/*
112917
+** Schema of the terms table.
112918
+*/
112919
+#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
112920
+
112921
+/*
112922
+** This function does all the work for both the xConnect and xCreate methods.
112923
+** These tables have no persistent representation of their own, so xConnect
112924
+** and xCreate are identical operations.
112925
+*/
112926
+static int fts3auxConnectMethod(
112927
+ sqlite3 *db, /* Database connection */
112928
+ void *pUnused, /* Unused */
112929
+ int argc, /* Number of elements in argv array */
112930
+ const char * const *argv, /* xCreate/xConnect argument array */
112931
+ sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
112932
+ char **pzErr /* OUT: sqlite3_malloc'd error message */
112933
+){
112934
+ char const *zDb; /* Name of database (e.g. "main") */
112935
+ char const *zFts3; /* Name of fts3 table */
112936
+ int nDb; /* Result of strlen(zDb) */
112937
+ int nFts3; /* Result of strlen(zFts3) */
112938
+ int nByte; /* Bytes of space to allocate here */
112939
+ int rc; /* value returned by declare_vtab() */
112940
+ Fts3auxTable *p; /* Virtual table object to return */
112941
+
112942
+ /* The user should specify a single argument - the name of an fts3 table. */
112943
+ if( argc!=4 ){
112944
+ *pzErr = sqlite3_mprintf(
112945
+ "wrong number of arguments to fts4aux constructor"
112946
+ );
112947
+ return SQLITE_ERROR;
112948
+ }
112949
+
112950
+ zDb = argv[1];
112951
+ nDb = strlen(zDb);
112952
+ zFts3 = argv[3];
112953
+ nFts3 = strlen(zFts3);
112954
+
112955
+ rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
112956
+ if( rc!=SQLITE_OK ) return rc;
112957
+
112958
+ nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
112959
+ p = (Fts3auxTable *)sqlite3_malloc(nByte);
112960
+ if( !p ) return SQLITE_NOMEM;
112961
+ memset(p, 0, nByte);
112962
+
112963
+ p->pFts3Tab = (Fts3Table *)&p[1];
112964
+ p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
112965
+ p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
112966
+ p->pFts3Tab->db = db;
112967
+
112968
+ memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
112969
+ memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
112970
+ sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
112971
+
112972
+ *ppVtab = (sqlite3_vtab *)p;
112973
+ return SQLITE_OK;
112974
+}
112975
+
112976
+/*
112977
+** This function does the work for both the xDisconnect and xDestroy methods.
112978
+** These tables have no persistent representation of their own, so xDisconnect
112979
+** and xDestroy are identical operations.
112980
+*/
112981
+static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
112982
+ Fts3auxTable *p = (Fts3auxTable *)pVtab;
112983
+ Fts3Table *pFts3 = p->pFts3Tab;
112984
+ int i;
112985
+
112986
+ /* Free any prepared statements held */
112987
+ for(i=0; i<SizeofArray(pFts3->aStmt); i++){
112988
+ sqlite3_finalize(pFts3->aStmt[i]);
112989
+ }
112990
+ sqlite3_free(pFts3->zSegmentsTbl);
112991
+ sqlite3_free(p);
112992
+ return SQLITE_OK;
112993
+}
112994
+
112995
+#define FTS4AUX_EQ_CONSTRAINT 1
112996
+#define FTS4AUX_GE_CONSTRAINT 2
112997
+#define FTS4AUX_LE_CONSTRAINT 4
112998
+
112999
+/*
113000
+** xBestIndex - Analyze a WHERE and ORDER BY clause.
113001
+*/
113002
+static int fts3auxBestIndexMethod(
113003
+ sqlite3_vtab *pVTab,
113004
+ sqlite3_index_info *pInfo
113005
+){
113006
+ int i;
113007
+ int iEq = -1;
113008
+ int iGe = -1;
113009
+ int iLe = -1;
113010
+
113011
+ /* This vtab delivers always results in "ORDER BY term ASC" order. */
113012
+ if( pInfo->nOrderBy==1
113013
+ && pInfo->aOrderBy[0].iColumn==0
113014
+ && pInfo->aOrderBy[0].desc==0
113015
+ ){
113016
+ pInfo->orderByConsumed = 1;
113017
+ }
113018
+
113019
+ /* Search for equality and range constraints on the "term" column. */
113020
+ for(i=0; i<pInfo->nConstraint; i++){
113021
+ if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
113022
+ int op = pInfo->aConstraint[i].op;
113023
+ if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
113024
+ if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
113025
+ if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
113026
+ if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
113027
+ if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
113028
+ }
113029
+ }
113030
+
113031
+ if( iEq>=0 ){
113032
+ pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
113033
+ pInfo->aConstraintUsage[iEq].argvIndex = 1;
113034
+ pInfo->estimatedCost = 5;
113035
+ }else{
113036
+ pInfo->idxNum = 0;
113037
+ pInfo->estimatedCost = 20000;
113038
+ if( iGe>=0 ){
113039
+ pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
113040
+ pInfo->aConstraintUsage[iGe].argvIndex = 1;
113041
+ pInfo->estimatedCost /= 2;
113042
+ }
113043
+ if( iLe>=0 ){
113044
+ pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
113045
+ pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
113046
+ pInfo->estimatedCost /= 2;
113047
+ }
113048
+ }
113049
+
113050
+ return SQLITE_OK;
113051
+}
113052
+
113053
+/*
113054
+** xOpen - Open a cursor.
113055
+*/
113056
+static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
113057
+ Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
113058
+
113059
+ pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
113060
+ if( !pCsr ) return SQLITE_NOMEM;
113061
+ memset(pCsr, 0, sizeof(Fts3auxCursor));
113062
+
113063
+ *ppCsr = (sqlite3_vtab_cursor *)pCsr;
113064
+ return SQLITE_OK;
113065
+}
113066
+
113067
+/*
113068
+** xClose - Close a cursor.
113069
+*/
113070
+static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
113071
+ Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113072
+ Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113073
+
113074
+ sqlite3Fts3SegmentsClose(pFts3);
113075
+ sqlite3Fts3SegReaderFinish(&pCsr->csr);
113076
+ sqlite3_free((void *)pCsr->filter.zTerm);
113077
+ sqlite3_free(pCsr->zStop);
113078
+ sqlite3_free(pCsr->aStat);
113079
+ sqlite3_free(pCsr);
113080
+ return SQLITE_OK;
113081
+}
113082
+
113083
+static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
113084
+ if( nSize>pCsr->nStat ){
113085
+ struct Fts3auxColstats *aNew;
113086
+ aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
113087
+ sizeof(struct Fts3auxColstats) * nSize
113088
+ );
113089
+ if( aNew==0 ) return SQLITE_NOMEM;
113090
+ memset(&aNew[pCsr->nStat], 0,
113091
+ sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
113092
+ );
113093
+ pCsr->aStat = aNew;
113094
+ pCsr->nStat = nSize;
113095
+ }
113096
+ return SQLITE_OK;
113097
+}
113098
+
113099
+/*
113100
+** xNext - Advance the cursor to the next row, if any.
113101
+*/
113102
+static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
113103
+ Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113104
+ Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113105
+ int rc;
113106
+
113107
+ /* Increment our pretend rowid value. */
113108
+ pCsr->iRowid++;
113109
+
113110
+ for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
113111
+ if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
113112
+ }
113113
+
113114
+ rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
113115
+ if( rc==SQLITE_ROW ){
113116
+ int i = 0;
113117
+ int nDoclist = pCsr->csr.nDoclist;
113118
+ char *aDoclist = pCsr->csr.aDoclist;
113119
+ int iCol;
113120
+
113121
+ int eState = 0;
113122
+
113123
+ if( pCsr->zStop ){
113124
+ int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
113125
+ int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
113126
+ if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
113127
+ pCsr->isEof = 1;
113128
+ return SQLITE_OK;
113129
+ }
113130
+ }
113131
+
113132
+ if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
113133
+ memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
113134
+ iCol = 0;
113135
+
113136
+ while( i<nDoclist ){
113137
+ sqlite3_int64 v = 0;
113138
+
113139
+ i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
113140
+ switch( eState ){
113141
+ /* State 0. In this state the integer just read was a docid. */
113142
+ case 0:
113143
+ pCsr->aStat[0].nDoc++;
113144
+ eState = 1;
113145
+ iCol = 0;
113146
+ break;
113147
+
113148
+ /* State 1. In this state we are expecting either a 1, indicating
113149
+ ** that the following integer will be a column number, or the
113150
+ ** start of a position list for column 0.
113151
+ **
113152
+ ** The only difference between state 1 and state 2 is that if the
113153
+ ** integer encountered in state 1 is not 0 or 1, then we need to
113154
+ ** increment the column 0 "nDoc" count for this term.
113155
+ */
113156
+ case 1:
113157
+ assert( iCol==0 );
113158
+ if( v>1 ){
113159
+ pCsr->aStat[1].nDoc++;
113160
+ }
113161
+ eState = 2;
113162
+ /* fall through */
113163
+
113164
+ case 2:
113165
+ if( v==0 ){ /* 0x00. Next integer will be a docid. */
113166
+ eState = 0;
113167
+ }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
113168
+ eState = 3;
113169
+ }else{ /* 2 or greater. A position. */
113170
+ pCsr->aStat[iCol+1].nOcc++;
113171
+ pCsr->aStat[0].nOcc++;
113172
+ }
113173
+ break;
113174
+
113175
+ /* State 3. The integer just read is a column number. */
113176
+ default: assert( eState==3 );
113177
+ iCol = (int)v;
113178
+ if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
113179
+ pCsr->aStat[iCol+1].nDoc++;
113180
+ eState = 2;
113181
+ break;
113182
+ }
113183
+ }
113184
+
113185
+ pCsr->iCol = 0;
113186
+ rc = SQLITE_OK;
113187
+ }else{
113188
+ pCsr->isEof = 1;
113189
+ }
113190
+ return rc;
113191
+}
113192
+
113193
+/*
113194
+** xFilter - Initialize a cursor to point at the start of its data.
113195
+*/
113196
+static int fts3auxFilterMethod(
113197
+ sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
113198
+ int idxNum, /* Strategy index */
113199
+ const char *idxStr, /* Unused */
113200
+ int nVal, /* Number of elements in apVal */
113201
+ sqlite3_value **apVal /* Arguments for the indexing scheme */
113202
+){
113203
+ Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113204
+ Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113205
+ int rc;
113206
+ int isScan;
113207
+
113208
+ assert( idxStr==0 );
113209
+ assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
113210
+ || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
113211
+ || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
113212
+ );
113213
+ isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
113214
+
113215
+ /* In case this cursor is being reused, close and zero it. */
113216
+ testcase(pCsr->filter.zTerm);
113217
+ sqlite3Fts3SegReaderFinish(&pCsr->csr);
113218
+ sqlite3_free((void *)pCsr->filter.zTerm);
113219
+ sqlite3_free(pCsr->aStat);
113220
+ memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
113221
+
113222
+ pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
113223
+ if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
113224
+
113225
+ if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
113226
+ const unsigned char *zStr = sqlite3_value_text(apVal[0]);
113227
+ if( zStr ){
113228
+ pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
113229
+ pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
113230
+ if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
113231
+ }
113232
+ }
113233
+ if( idxNum&FTS4AUX_LE_CONSTRAINT ){
113234
+ int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
113235
+ pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
113236
+ pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
113237
+ if( pCsr->zStop==0 ) return SQLITE_NOMEM;
113238
+ }
113239
+
113240
+ rc = sqlite3Fts3SegReaderCursor(pFts3, FTS3_SEGCURSOR_ALL,
113241
+ pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
113242
+ );
113243
+ if( rc==SQLITE_OK ){
113244
+ rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
113245
+ }
113246
+
113247
+ if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
113248
+ return rc;
113249
+}
113250
+
113251
+/*
113252
+** xEof - Return true if the cursor is at EOF, or false otherwise.
113253
+*/
113254
+static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
113255
+ Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113256
+ return pCsr->isEof;
113257
+}
113258
+
113259
+/*
113260
+** xColumn - Return a column value.
113261
+*/
113262
+static int fts3auxColumnMethod(
113263
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
113264
+ sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */
113265
+ int iCol /* Index of column to read value from */
113266
+){
113267
+ Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
113268
+
113269
+ assert( p->isEof==0 );
113270
+ if( iCol==0 ){ /* Column "term" */
113271
+ sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
113272
+ }else if( iCol==1 ){ /* Column "col" */
113273
+ if( p->iCol ){
113274
+ sqlite3_result_int(pContext, p->iCol-1);
113275
+ }else{
113276
+ sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
113277
+ }
113278
+ }else if( iCol==2 ){ /* Column "documents" */
113279
+ sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
113280
+ }else{ /* Column "occurrences" */
113281
+ sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
113282
+ }
113283
+
113284
+ return SQLITE_OK;
113285
+}
113286
+
113287
+/*
113288
+** xRowid - Return the current rowid for the cursor.
113289
+*/
113290
+static int fts3auxRowidMethod(
113291
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
113292
+ sqlite_int64 *pRowid /* OUT: Rowid value */
113293
+){
113294
+ Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113295
+ *pRowid = pCsr->iRowid;
113296
+ return SQLITE_OK;
113297
+}
113298
+
113299
+/*
113300
+** Register the fts3aux module with database connection db. Return SQLITE_OK
113301
+** if successful or an error code if sqlite3_create_module() fails.
113302
+*/
113303
+SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
113304
+ static const sqlite3_module fts3aux_module = {
113305
+ 0, /* iVersion */
113306
+ fts3auxConnectMethod, /* xCreate */
113307
+ fts3auxConnectMethod, /* xConnect */
113308
+ fts3auxBestIndexMethod, /* xBestIndex */
113309
+ fts3auxDisconnectMethod, /* xDisconnect */
113310
+ fts3auxDisconnectMethod, /* xDestroy */
113311
+ fts3auxOpenMethod, /* xOpen */
113312
+ fts3auxCloseMethod, /* xClose */
113313
+ fts3auxFilterMethod, /* xFilter */
113314
+ fts3auxNextMethod, /* xNext */
113315
+ fts3auxEofMethod, /* xEof */
113316
+ fts3auxColumnMethod, /* xColumn */
113317
+ fts3auxRowidMethod, /* xRowid */
113318
+ 0, /* xUpdate */
113319
+ 0, /* xBegin */
113320
+ 0, /* xSync */
113321
+ 0, /* xCommit */
113322
+ 0, /* xRollback */
113323
+ 0, /* xFindFunction */
113324
+ 0 /* xRename */
113325
+ };
113326
+ int rc; /* Return code */
113327
+
113328
+ rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
113329
+ return rc;
113330
+}
113331
+
113332
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
113333
+
113334
+/************** End of fts3_aux.c ********************************************/
112068113335
/************** Begin file fts3_expr.c ***************************************/
112069113336
/*
112070113337
** 2008 Nov 28
112071113338
**
112072113339
** The author disclaims copyright to this source code. In place of
@@ -114956,11 +116223,11 @@
114956116223
/* 2 */ "DELETE FROM %Q.'%q_content'",
114957116224
/* 3 */ "DELETE FROM %Q.'%q_segments'",
114958116225
/* 4 */ "DELETE FROM %Q.'%q_segdir'",
114959116226
/* 5 */ "DELETE FROM %Q.'%q_docsize'",
114960116227
/* 6 */ "DELETE FROM %Q.'%q_stat'",
114961
-/* 7 */ "SELECT * FROM %Q.'%q_content' WHERE rowid=?",
116228
+/* 7 */ "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
114962116229
/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
114963116230
/* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
114964116231
/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
114965116232
/* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
114966116233
@@ -114973,11 +116240,11 @@
114973116240
/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
114974116241
/* 15 */ "SELECT count(*), max(level) FROM %Q.'%q_segdir'",
114975116242
114976116243
/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
114977116244
/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
114978
-/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%z)",
116245
+/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
114979116246
/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
114980116247
/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
114981116248
/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
114982116249
/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=0",
114983116250
/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
@@ -114990,24 +116257,13 @@
114990116257
114991116258
pStmt = p->aStmt[eStmt];
114992116259
if( !pStmt ){
114993116260
char *zSql;
114994116261
if( eStmt==SQL_CONTENT_INSERT ){
114995
- int i; /* Iterator variable */
114996
- char *zVarlist; /* The "?, ?, ..." string */
114997
- zVarlist = (char *)sqlite3_malloc(2*p->nColumn+2);
114998
- if( !zVarlist ){
114999
- *pp = 0;
115000
- return SQLITE_NOMEM;
115001
- }
115002
- zVarlist[0] = '?';
115003
- zVarlist[p->nColumn*2+1] = '\0';
115004
- for(i=1; i<=p->nColumn; i++){
115005
- zVarlist[i*2-1] = ',';
115006
- zVarlist[i*2] = '?';
115007
- }
115008
- zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, zVarlist);
116262
+ zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
116263
+ }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
116264
+ zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
115009116265
}else{
115010116266
zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
115011116267
}
115012116268
if( !zSql ){
115013116269
rc = SQLITE_NOMEM;
@@ -115044,11 +116300,11 @@
115044116300
if( rc==SQLITE_OK ){
115045116301
if( eStmt==SQL_SELECT_DOCSIZE ){
115046116302
sqlite3_bind_int64(pStmt, 1, iDocid);
115047116303
}
115048116304
rc = sqlite3_step(pStmt);
115049
- if( rc!=SQLITE_ROW ){
116305
+ if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
115050116306
rc = sqlite3_reset(pStmt);
115051116307
if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT;
115052116308
pStmt = 0;
115053116309
}else{
115054116310
rc = SQLITE_OK;
@@ -115145,12 +116401,21 @@
115145116401
** 1: start_block
115146116402
** 2: leaves_end_block
115147116403
** 3: end_block
115148116404
** 4: root
115149116405
*/
115150
-SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table *p, sqlite3_stmt **ppStmt){
115151
- return fts3SqlStmt(p, SQL_SELECT_ALL_LEVEL, ppStmt, 0);
116406
+SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table *p, int iLevel, sqlite3_stmt **ppStmt){
116407
+ int rc;
116408
+ sqlite3_stmt *pStmt = 0;
116409
+ if( iLevel<0 ){
116410
+ rc = fts3SqlStmt(p, SQL_SELECT_ALL_LEVEL, &pStmt, 0);
116411
+ }else{
116412
+ rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
116413
+ if( rc==SQLITE_OK ) sqlite3_bind_int(pStmt, 1, iLevel);
116414
+ }
116415
+ *ppStmt = pStmt;
116416
+ return rc;
115152116417
}
115153116418
115154116419
115155116420
/*
115156116421
** Append a single varint to a PendingList buffer. SQLITE_OK is returned
@@ -115848,20 +117113,22 @@
115848117113
** to right.
115849117114
*/
115850117115
sqlite3_stmt *pStmt;
115851117116
sqlite3_int64 nDoc = 0;
115852117117
sqlite3_int64 nByte = 0;
117118
+ const char *pEnd;
115853117119
const char *a;
117120
+
115854117121
rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
115855
- if( rc ) return rc;
117122
+ if( rc!=SQLITE_OK ) return rc;
115856117123
a = sqlite3_column_blob(pStmt, 0);
115857
- if( a ){
115858
- const char *pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
115859
- a += sqlite3Fts3GetVarint(a, &nDoc);
115860
- while( a<pEnd ){
115861
- a += sqlite3Fts3GetVarint(a, &nByte);
115862
- }
117124
+ assert( a );
117125
+
117126
+ pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
117127
+ a += sqlite3Fts3GetVarint(a, &nDoc);
117128
+ while( a<pEnd ){
117129
+ a += sqlite3Fts3GetVarint(a, &nByte);
115863117130
}
115864117131
if( nDoc==0 || nByte==0 ){
115865117132
sqlite3_reset(pStmt);
115866117133
return SQLITE_CORRUPT;
115867117134
}
@@ -116047,46 +117314,10 @@
116047117314
}
116048117315
*ppReader = pReader;
116049117316
return rc;
116050117317
}
116051117318
116052
-
116053
-/*
116054
-** The second argument to this function is expected to be a statement of
116055
-** the form:
116056
-**
116057
-** SELECT
116058
-** idx, -- col 0
116059
-** start_block, -- col 1
116060
-** leaves_end_block, -- col 2
116061
-** end_block, -- col 3
116062
-** root -- col 4
116063
-** FROM %_segdir ...
116064
-**
116065
-** This function allocates and initializes a Fts3SegReader structure to
116066
-** iterate through the terms stored in the segment identified by the
116067
-** current row that pStmt is pointing to.
116068
-**
116069
-** If successful, the Fts3SegReader is left pointing to the first term
116070
-** in the segment and SQLITE_OK is returned. Otherwise, an SQLite error
116071
-** code is returned.
116072
-*/
116073
-static int fts3SegReaderNew(
116074
- sqlite3_stmt *pStmt, /* See above */
116075
- int iAge, /* Segment "age". */
116076
- Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
116077
-){
116078
- return sqlite3Fts3SegReaderNew(iAge,
116079
- sqlite3_column_int64(pStmt, 1),
116080
- sqlite3_column_int64(pStmt, 2),
116081
- sqlite3_column_int64(pStmt, 3),
116082
- sqlite3_column_blob(pStmt, 4),
116083
- sqlite3_column_bytes(pStmt, 4),
116084
- ppReader
116085
- );
116086
-}
116087
-
116088117319
/*
116089117320
** Compare the entries pointed to by two Fts3SegReader structures.
116090117321
** Comparison is as follows:
116091117322
**
116092117323
** 1) EOF is greater than not EOF.
@@ -116687,29 +117918,10 @@
116687117918
rc = sqlite3_reset(pStmt);
116688117919
}
116689117920
return rc;
116690117921
}
116691117922
116692
-/*
116693
-** Set *pnSegment to the number of segments of level iLevel in the database.
116694
-**
116695
-** Return SQLITE_OK if successful, or an SQLite error code if not.
116696
-*/
116697
-static int fts3SegmentCount(Fts3Table *p, int iLevel, int *pnSegment){
116698
- sqlite3_stmt *pStmt;
116699
- int rc;
116700
-
116701
- assert( iLevel>=0 );
116702
- rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_COUNT, &pStmt, 0);
116703
- if( rc!=SQLITE_OK ) return rc;
116704
- sqlite3_bind_int(pStmt, 1, iLevel);
116705
- if( SQLITE_ROW==sqlite3_step(pStmt) ){
116706
- *pnSegment = sqlite3_column_int(pStmt, 0);
116707
- }
116708
- return sqlite3_reset(pStmt);
116709
-}
116710
-
116711117923
/*
116712117924
** Set *pnSegment to the total number of segments in the database. Set
116713117925
** *pnMax to the largest segment level in the database (segment levels
116714117926
** are stored in the 'level' column of the %_segdir table).
116715117927
**
@@ -116764,19 +117976,22 @@
116764117976
}
116765117977
if( rc!=SQLITE_OK ){
116766117978
return rc;
116767117979
}
116768117980
116769
- if( iLevel>=0 ){
117981
+ if( iLevel==FTS3_SEGCURSOR_ALL ){
117982
+ fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
117983
+ }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
117984
+ sqlite3Fts3PendingTermsClear(p);
117985
+ }else{
117986
+ assert( iLevel>=0 );
116770117987
rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_BY_LEVEL, &pDelete, 0);
116771117988
if( rc==SQLITE_OK ){
116772117989
sqlite3_bind_int(pDelete, 1, iLevel);
116773117990
sqlite3_step(pDelete);
116774117991
rc = sqlite3_reset(pDelete);
116775117992
}
116776
- }else{
116777
- fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
116778117993
}
116779117994
116780117995
return rc;
116781117996
}
116782117997
@@ -116821,148 +118036,117 @@
116821118036
116822118037
*ppList = pList;
116823118038
*pnList = nList;
116824118039
}
116825118040
116826
-/*
116827
-** sqlite3Fts3SegReaderIterate() callback used when merging multiple
116828
-** segments to create a single, larger segment.
116829
-*/
116830
-static int fts3MergeCallback(
116831
- Fts3Table *p, /* FTS3 Virtual table handle */
116832
- void *pContext, /* Pointer to SegmentWriter* to write with */
116833
- char *zTerm, /* Term to write to the db */
116834
- int nTerm, /* Number of bytes in zTerm */
116835
- char *aDoclist, /* Doclist associated with zTerm */
116836
- int nDoclist /* Number of bytes in doclist */
116837
-){
116838
- SegmentWriter **ppW = (SegmentWriter **)pContext;
116839
- return fts3SegWriterAdd(p, ppW, 1, zTerm, nTerm, aDoclist, nDoclist);
116840
-}
116841
-
116842
-/*
116843
-** sqlite3Fts3SegReaderIterate() callback used when flushing the contents
116844
-** of the pending-terms hash table to the database.
116845
-*/
116846
-static int fts3FlushCallback(
116847
- Fts3Table *p, /* FTS3 Virtual table handle */
116848
- void *pContext, /* Pointer to SegmentWriter* to write with */
116849
- char *zTerm, /* Term to write to the db */
116850
- int nTerm, /* Number of bytes in zTerm */
116851
- char *aDoclist, /* Doclist associated with zTerm */
116852
- int nDoclist /* Number of bytes in doclist */
116853
-){
116854
- SegmentWriter **ppW = (SegmentWriter **)pContext;
116855
- return fts3SegWriterAdd(p, ppW, 0, zTerm, nTerm, aDoclist, nDoclist);
116856
-}
116857
-
116858
-/*
116859
-** This function is used to iterate through a contiguous set of terms
116860
-** stored in the full-text index. It merges data contained in one or
116861
-** more segments to support this.
116862
-**
116863
-** The second argument is passed an array of pointers to SegReader objects
116864
-** allocated with sqlite3Fts3SegReaderNew(). This function merges the range
116865
-** of terms selected by each SegReader. If a single term is present in
116866
-** more than one segment, the associated doclists are merged. For each
116867
-** term and (possibly merged) doclist in the merged range, the callback
116868
-** function xFunc is invoked with its arguments set as follows.
116869
-**
116870
-** arg 0: Copy of 'p' parameter passed to this function
116871
-** arg 1: Copy of 'pContext' parameter passed to this function
116872
-** arg 2: Pointer to buffer containing term
116873
-** arg 3: Size of arg 2 buffer in bytes
116874
-** arg 4: Pointer to buffer containing doclist
116875
-** arg 5: Size of arg 2 buffer in bytes
116876
-**
116877
-** The 4th argument to this function is a pointer to a structure of type
116878
-** Fts3SegFilter, defined in fts3Int.h. The contents of this structure
116879
-** further restrict the range of terms that callbacks are made for and
116880
-** modify the behaviour of this function. See comments above structure
116881
-** definition for details.
116882
-*/
116883
-SQLITE_PRIVATE int sqlite3Fts3SegReaderIterate(
118041
+SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
116884118042
Fts3Table *p, /* Virtual table handle */
116885
- Fts3SegReader **apSegment, /* Array of Fts3SegReader objects */
116886
- int nSegment, /* Size of apSegment array */
116887
- Fts3SegFilter *pFilter, /* Restrictions on range of iteration */
116888
- int (*xFunc)(Fts3Table *, void *, char *, int, char *, int), /* Callback */
116889
- void *pContext /* Callback context (2nd argument) */
118043
+ Fts3SegReaderCursor *pCsr, /* Cursor object */
118044
+ Fts3SegFilter *pFilter /* Restrictions on range of iteration */
116890118045
){
116891
- int i; /* Iterator variable */
116892
- char *aBuffer = 0; /* Buffer to merge doclists in */
116893
- int nAlloc = 0; /* Allocated size of aBuffer buffer */
116894
- int rc = SQLITE_OK; /* Return code */
116895
-
116896
- int isIgnoreEmpty = (pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
116897
- int isRequirePos = (pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
116898
- int isColFilter = (pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
116899
- int isPrefix = (pFilter->flags & FTS3_SEGMENT_PREFIX);
116900
-
116901
- /* If there are zero segments, this function is a no-op. This scenario
116902
- ** comes about only when reading from an empty database.
116903
- */
116904
- if( nSegment==0 ) goto finished;
118046
+ int i;
118047
+
118048
+ /* Initialize the cursor object */
118049
+ pCsr->pFilter = pFilter;
116905118050
116906118051
/* If the Fts3SegFilter defines a specific term (or term prefix) to search
116907118052
** for, then advance each segment iterator until it points to a term of
116908118053
** equal or greater value than the specified term. This prevents many
116909118054
** unnecessary merge/sort operations for the case where single segment
116910118055
** b-tree leaf nodes contain more than one term.
116911118056
*/
116912
- for(i=0; i<nSegment; i++){
118057
+ for(i=0; i<pCsr->nSegment; i++){
116913118058
int nTerm = pFilter->nTerm;
116914118059
const char *zTerm = pFilter->zTerm;
116915
- Fts3SegReader *pSeg = apSegment[i];
118060
+ Fts3SegReader *pSeg = pCsr->apSegment[i];
116916118061
do {
116917
- rc = fts3SegReaderNext(p, pSeg);
116918
- if( rc!=SQLITE_OK ) goto finished;
118062
+ int rc = fts3SegReaderNext(p, pSeg);
118063
+ if( rc!=SQLITE_OK ) return rc;
116919118064
}while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
116920118065
}
118066
+ fts3SegReaderSort(
118067
+ pCsr->apSegment, pCsr->nSegment, pCsr->nSegment, fts3SegReaderCmp);
116921118068
116922
- fts3SegReaderSort(apSegment, nSegment, nSegment, fts3SegReaderCmp);
116923
- while( apSegment[0]->aNode ){
116924
- int nTerm = apSegment[0]->nTerm;
116925
- char *zTerm = apSegment[0]->zTerm;
116926
- int nMerge = 1;
118069
+ return SQLITE_OK;
118070
+}
118071
+
118072
+SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
118073
+ Fts3Table *p, /* Virtual table handle */
118074
+ Fts3SegReaderCursor *pCsr /* Cursor object */
118075
+){
118076
+ int rc = SQLITE_OK;
118077
+
118078
+ int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
118079
+ int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
118080
+ int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
118081
+ int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
118082
+ int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
118083
+
118084
+ Fts3SegReader **apSegment = pCsr->apSegment;
118085
+ int nSegment = pCsr->nSegment;
118086
+ Fts3SegFilter *pFilter = pCsr->pFilter;
118087
+
118088
+ if( pCsr->nSegment==0 ) return SQLITE_OK;
118089
+
118090
+ do {
118091
+ int nMerge;
118092
+ int i;
118093
+
118094
+ /* Advance the first pCsr->nAdvance entries in the apSegment[] array
118095
+ ** forward. Then sort the list in order of current term again.
118096
+ */
118097
+ for(i=0; i<pCsr->nAdvance; i++){
118098
+ rc = fts3SegReaderNext(p, apSegment[i]);
118099
+ if( rc!=SQLITE_OK ) return rc;
118100
+ }
118101
+ fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
118102
+ pCsr->nAdvance = 0;
118103
+
118104
+ /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
118105
+ assert( rc==SQLITE_OK );
118106
+ if( apSegment[0]->aNode==0 ) break;
118107
+
118108
+ pCsr->nTerm = apSegment[0]->nTerm;
118109
+ pCsr->zTerm = apSegment[0]->zTerm;
116927118110
116928118111
/* If this is a prefix-search, and if the term that apSegment[0] points
116929118112
** to does not share a suffix with pFilter->zTerm/nTerm, then all
116930118113
** required callbacks have been made. In this case exit early.
116931118114
**
116932118115
** Similarly, if this is a search for an exact match, and the first term
116933118116
** of segment apSegment[0] is not a match, exit early.
116934118117
*/
116935
- if( pFilter->zTerm ){
116936
- if( nTerm<pFilter->nTerm
116937
- || (!isPrefix && nTerm>pFilter->nTerm)
116938
- || memcmp(zTerm, pFilter->zTerm, pFilter->nTerm)
116939
- ){
116940
- goto finished;
118118
+ if( pFilter->zTerm && !isScan ){
118119
+ if( pCsr->nTerm<pFilter->nTerm
118120
+ || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
118121
+ || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
118122
+ ){
118123
+ break;
116941118124
}
116942118125
}
116943118126
118127
+ nMerge = 1;
116944118128
while( nMerge<nSegment
116945118129
&& apSegment[nMerge]->aNode
116946
- && apSegment[nMerge]->nTerm==nTerm
116947
- && 0==memcmp(zTerm, apSegment[nMerge]->zTerm, nTerm)
118130
+ && apSegment[nMerge]->nTerm==pCsr->nTerm
118131
+ && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
116948118132
){
116949118133
nMerge++;
116950118134
}
116951118135
116952118136
assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
116953118137
if( nMerge==1 && !isIgnoreEmpty ){
116954
- Fts3SegReader *p0 = apSegment[0];
116955
- rc = xFunc(p, pContext, zTerm, nTerm, p0->aDoclist, p0->nDoclist);
116956
- if( rc!=SQLITE_OK ) goto finished;
118138
+ pCsr->aDoclist = apSegment[0]->aDoclist;
118139
+ pCsr->nDoclist = apSegment[0]->nDoclist;
118140
+ rc = SQLITE_ROW;
116957118141
}else{
116958118142
int nDoclist = 0; /* Size of doclist */
116959118143
sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
116960118144
116961118145
/* The current term of the first nMerge entries in the array
116962118146
** of Fts3SegReader objects is the same. The doclists must be merged
116963
- ** and a single term added to the new segment.
118147
+ ** and a single term returned with the merged doclist.
116964118148
*/
116965118149
for(i=0; i<nMerge; i++){
116966118150
fts3SegReaderFirstDocid(apSegment[i]);
116967118151
}
116968118152
fts3SegReaderSort(apSegment, nMerge, nMerge, fts3SegReaderDoclistCmp);
@@ -116986,57 +118170,60 @@
116986118170
fts3ColumnFilter(pFilter->iCol, &pList, &nList);
116987118171
}
116988118172
116989118173
if( !isIgnoreEmpty || nList>0 ){
116990118174
nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0);
116991
- if( nDoclist+nByte>nAlloc ){
118175
+ if( nDoclist+nByte>pCsr->nBuffer ){
116992118176
char *aNew;
116993
- nAlloc = (nDoclist+nByte)*2;
116994
- aNew = sqlite3_realloc(aBuffer, nAlloc);
118177
+ pCsr->nBuffer = (nDoclist+nByte)*2;
118178
+ aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
116995118179
if( !aNew ){
116996
- rc = SQLITE_NOMEM;
116997
- goto finished;
118180
+ return SQLITE_NOMEM;
116998118181
}
116999
- aBuffer = aNew;
118182
+ pCsr->aBuffer = aNew;
117000118183
}
117001
- nDoclist += sqlite3Fts3PutVarint(&aBuffer[nDoclist], iDocid-iPrev);
118184
+ nDoclist += sqlite3Fts3PutVarint(
118185
+ &pCsr->aBuffer[nDoclist], iDocid-iPrev
118186
+ );
117002118187
iPrev = iDocid;
117003118188
if( isRequirePos ){
117004
- memcpy(&aBuffer[nDoclist], pList, nList);
118189
+ memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
117005118190
nDoclist += nList;
117006
- aBuffer[nDoclist++] = '\0';
118191
+ pCsr->aBuffer[nDoclist++] = '\0';
117007118192
}
117008118193
}
117009118194
117010118195
fts3SegReaderSort(apSegment, nMerge, j, fts3SegReaderDoclistCmp);
117011118196
}
117012
-
117013118197
if( nDoclist>0 ){
117014
- rc = xFunc(p, pContext, zTerm, nTerm, aBuffer, nDoclist);
117015
- if( rc!=SQLITE_OK ) goto finished;
117016
- }
117017
- }
117018
-
117019
- /* If there is a term specified to filter on, and this is not a prefix
117020
- ** search, return now. The callback that corresponds to the required
117021
- ** term (if such a term exists in the index) has already been made.
117022
- */
117023
- if( pFilter->zTerm && !isPrefix ){
117024
- goto finished;
117025
- }
117026
-
117027
- for(i=0; i<nMerge; i++){
117028
- rc = fts3SegReaderNext(p, apSegment[i]);
117029
- if( rc!=SQLITE_OK ) goto finished;
117030
- }
117031
- fts3SegReaderSort(apSegment, nSegment, nMerge, fts3SegReaderCmp);
117032
- }
117033
-
117034
- finished:
117035
- sqlite3_free(aBuffer);
118198
+ pCsr->aDoclist = pCsr->aBuffer;
118199
+ pCsr->nDoclist = nDoclist;
118200
+ rc = SQLITE_ROW;
118201
+ }
118202
+ }
118203
+ pCsr->nAdvance = nMerge;
118204
+ }while( rc==SQLITE_OK );
118205
+
117036118206
return rc;
117037118207
}
118208
+
118209
+SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
118210
+ Fts3SegReaderCursor *pCsr /* Cursor object */
118211
+){
118212
+ if( pCsr ){
118213
+ int i;
118214
+ for(i=0; i<pCsr->nSegment; i++){
118215
+ sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
118216
+ }
118217
+ sqlite3_free(pCsr->apSegment);
118218
+ sqlite3_free(pCsr->aBuffer);
118219
+
118220
+ pCsr->nSegment = 0;
118221
+ pCsr->apSegment = 0;
118222
+ pCsr->aBuffer = 0;
118223
+ }
118224
+}
117038118225
117039118226
/*
117040118227
** Merge all level iLevel segments in the database into a single
117041118228
** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
117042118229
** single segment with a level equal to the numerically largest level
@@ -117046,161 +118233,73 @@
117046118233
** segment in the database, SQLITE_DONE is returned immediately.
117047118234
** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
117048118235
** an SQLite error code is returned.
117049118236
*/
117050118237
static int fts3SegmentMerge(Fts3Table *p, int iLevel){
117051
- int i; /* Iterator variable */
117052118238
int rc; /* Return code */
117053
- int iIdx; /* Index of new segment */
118239
+ int iIdx = 0; /* Index of new segment */
117054118240
int iNewLevel = 0; /* Level to create new segment at */
117055
- sqlite3_stmt *pStmt = 0;
117056
- SegmentWriter *pWriter = 0;
117057
- int nSegment = 0; /* Number of segments being merged */
117058
- Fts3SegReader **apSegment = 0; /* Array of Segment iterators */
117059
- Fts3SegReader *pPending = 0; /* Iterator for pending-terms */
118241
+ SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
117060118242
Fts3SegFilter filter; /* Segment term filter condition */
118243
+ Fts3SegReaderCursor csr; /* Cursor to iterate through level(s) */
117061118244
117062
- if( iLevel<0 ){
118245
+ rc = sqlite3Fts3SegReaderCursor(p, iLevel, 0, 0, 1, 0, &csr);
118246
+ if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
118247
+
118248
+ if( iLevel==FTS3_SEGCURSOR_ALL ){
117063118249
/* This call is to merge all segments in the database to a single
117064118250
** segment. The level of the new segment is equal to the the numerically
117065118251
** greatest segment level currently present in the database. The index
117066
- ** of the new segment is always 0.
117067
- */
117068
- iIdx = 0;
117069
- rc = sqlite3Fts3SegReaderPending(p, 0, 0, 1, &pPending);
117070
- if( rc!=SQLITE_OK ) goto finished;
117071
- rc = fts3SegmentCountMax(p, &nSegment, &iNewLevel);
117072
- if( rc!=SQLITE_OK ) goto finished;
117073
- nSegment += (pPending!=0);
117074
- if( nSegment<=1 ){
117075
- return SQLITE_DONE;
117076
- }
118252
+ ** of the new segment is always 0. */
118253
+ int nDummy; /* TODO: Remove this */
118254
+ if( csr.nSegment==1 ){
118255
+ rc = SQLITE_DONE;
118256
+ goto finished;
118257
+ }
118258
+ rc = fts3SegmentCountMax(p, &nDummy, &iNewLevel);
117077118259
}else{
117078118260
/* This call is to merge all segments at level iLevel. Find the next
117079118261
** available segment index at level iLevel+1. The call to
117080118262
** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
117081
- ** a single iLevel+2 segment if necessary.
117082
- */
118263
+ ** a single iLevel+2 segment if necessary. */
117083118264
iNewLevel = iLevel+1;
117084118265
rc = fts3AllocateSegdirIdx(p, iNewLevel, &iIdx);
117085
- if( rc!=SQLITE_OK ) goto finished;
117086
- rc = fts3SegmentCount(p, iLevel, &nSegment);
117087
- if( rc!=SQLITE_OK ) goto finished;
117088
- }
117089
- assert( nSegment>0 );
117090
- assert( iNewLevel>=0 );
117091
-
117092
- /* Allocate space for an array of pointers to segment iterators. */
117093
- apSegment = (Fts3SegReader**)sqlite3_malloc(sizeof(Fts3SegReader *)*nSegment);
117094
- if( !apSegment ){
117095
- rc = SQLITE_NOMEM;
117096
- goto finished;
117097
- }
117098
- memset(apSegment, 0, sizeof(Fts3SegReader *)*nSegment);
117099
-
117100
- /* Allocate a Fts3SegReader structure for each segment being merged. A
117101
- ** Fts3SegReader stores the state data required to iterate through all
117102
- ** entries on all leaves of a single segment.
117103
- */
117104
- assert( SQL_SELECT_LEVEL+1==SQL_SELECT_ALL_LEVEL);
117105
- rc = fts3SqlStmt(p, SQL_SELECT_LEVEL+(iLevel<0), &pStmt, 0);
117106
- if( rc!=SQLITE_OK ) goto finished;
117107
- sqlite3_bind_int(pStmt, 1, iLevel);
117108
- for(i=0; SQLITE_ROW==(sqlite3_step(pStmt)); i++){
117109
- rc = fts3SegReaderNew(pStmt, i, &apSegment[i]);
117110
- if( rc!=SQLITE_OK ){
117111
- goto finished;
117112
- }
117113
- }
117114
- rc = sqlite3_reset(pStmt);
117115
- if( pPending ){
117116
- apSegment[i] = pPending;
117117
- pPending = 0;
117118
- }
117119
- pStmt = 0;
117120
- if( rc!=SQLITE_OK ) goto finished;
118266
+ }
118267
+ if( rc!=SQLITE_OK ) goto finished;
118268
+ assert( csr.nSegment>0 );
118269
+ assert( iNewLevel>=0 );
117121118270
117122118271
memset(&filter, 0, sizeof(Fts3SegFilter));
117123118272
filter.flags = FTS3_SEGMENT_REQUIRE_POS;
117124
- filter.flags |= (iLevel<0 ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
117125
- rc = sqlite3Fts3SegReaderIterate(p, apSegment, nSegment,
117126
- &filter, fts3MergeCallback, (void *)&pWriter
117127
- );
118273
+ filter.flags |= (iLevel==FTS3_SEGCURSOR_ALL ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
118274
+
118275
+ rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
118276
+ while( SQLITE_OK==rc ){
118277
+ rc = sqlite3Fts3SegReaderStep(p, &csr);
118278
+ if( rc!=SQLITE_ROW ) break;
118279
+ rc = fts3SegWriterAdd(p, &pWriter, 1,
118280
+ csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
118281
+ }
118282
+ if( rc!=SQLITE_OK ) goto finished;
118283
+ assert( pWriter );
118284
+
118285
+ rc = fts3DeleteSegdir(p, iLevel, csr.apSegment, csr.nSegment);
117128118286
if( rc!=SQLITE_OK ) goto finished;
117129
-
117130
- rc = fts3DeleteSegdir(p, iLevel, apSegment, nSegment);
117131
- if( rc==SQLITE_OK ){
117132
- rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
117133
- }
118287
+ rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
117134118288
117135118289
finished:
117136118290
fts3SegWriterFree(pWriter);
117137
- if( apSegment ){
117138
- for(i=0; i<nSegment; i++){
117139
- sqlite3Fts3SegReaderFree(apSegment[i]);
117140
- }
117141
- sqlite3_free(apSegment);
117142
- }
117143
- sqlite3Fts3SegReaderFree(pPending);
117144
- sqlite3_reset(pStmt);
118291
+ sqlite3Fts3SegReaderFinish(&csr);
117145118292
return rc;
117146118293
}
117147118294
117148118295
117149118296
/*
117150118297
** Flush the contents of pendingTerms to a level 0 segment.
117151118298
*/
117152118299
SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
117153
- int rc; /* Return Code */
117154
- int idx; /* Index of new segment created */
117155
- SegmentWriter *pWriter = 0; /* Used to write the segment */
117156
- Fts3SegReader *pReader = 0; /* Used to iterate through the hash table */
117157
-
117158
- /* Allocate a SegReader object to iterate through the contents of the
117159
- ** pending-terms table. If an error occurs, or if there are no terms
117160
- ** in the pending-terms table, return immediately.
117161
- */
117162
- rc = sqlite3Fts3SegReaderPending(p, 0, 0, 1, &pReader);
117163
- if( rc!=SQLITE_OK || pReader==0 ){
117164
- return rc;
117165
- }
117166
-
117167
- /* Determine the next index at level 0. If level 0 is already full, this
117168
- ** call may merge all existing level 0 segments into a single level 1
117169
- ** segment.
117170
- */
117171
- rc = fts3AllocateSegdirIdx(p, 0, &idx);
117172
-
117173
- /* If no errors have occured, iterate through the contents of the
117174
- ** pending-terms hash table using the Fts3SegReader iterator. The callback
117175
- ** writes each term (along with its doclist) to the database via the
117176
- ** SegmentWriter handle pWriter.
117177
- */
117178
- if( rc==SQLITE_OK ){
117179
- void *c = (void *)&pWriter; /* SegReaderIterate() callback context */
117180
- Fts3SegFilter f; /* SegReaderIterate() parameters */
117181
-
117182
- memset(&f, 0, sizeof(Fts3SegFilter));
117183
- f.flags = FTS3_SEGMENT_REQUIRE_POS;
117184
- rc = sqlite3Fts3SegReaderIterate(p, &pReader, 1, &f, fts3FlushCallback, c);
117185
- }
117186
- assert( pWriter || rc!=SQLITE_OK );
117187
-
117188
- /* If no errors have occured, flush the SegmentWriter object to the
117189
- ** database. Then delete the SegmentWriter and Fts3SegReader objects
117190
- ** allocated by this function.
117191
- */
117192
- if( rc==SQLITE_OK ){
117193
- rc = fts3SegWriterFlush(p, pWriter, 0, idx);
117194
- }
117195
- fts3SegWriterFree(pWriter);
117196
- sqlite3Fts3SegReaderFree(pReader);
117197
-
117198
- if( rc==SQLITE_OK ){
117199
- sqlite3Fts3PendingTermsClear(p);
117200
- }
117201
- return rc;
118300
+ return fts3SegmentMerge(p, FTS3_SEGCURSOR_PENDING);
117202118301
}
117203118302
117204118303
/*
117205118304
** Encode N integers as varints into a blob.
117206118305
*/
@@ -117363,11 +118462,11 @@
117363118462
int nVal = sqlite3_value_bytes(pVal);
117364118463
117365118464
if( !zVal ){
117366118465
return SQLITE_NOMEM;
117367118466
}else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
117368
- rc = fts3SegmentMerge(p, -1);
118467
+ rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
117369118468
if( rc==SQLITE_DONE ){
117370118469
rc = SQLITE_OK;
117371118470
}else{
117372118471
sqlite3Fts3PendingTermsClear(p);
117373118472
}
@@ -117621,11 +118720,11 @@
117621118720
*/
117622118721
SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
117623118722
int rc;
117624118723
rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
117625118724
if( rc==SQLITE_OK ){
117626
- rc = fts3SegmentMerge(p, -1);
118725
+ rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
117627118726
if( rc==SQLITE_OK ){
117628118727
rc = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
117629118728
if( rc==SQLITE_OK ){
117630118729
sqlite3Fts3PendingTermsClear(p);
117631118730
}
@@ -118599,10 +119698,11 @@
118599119698
pStmt = *ppStmt;
118600119699
assert( sqlite3_data_count(pStmt)==1 );
118601119700
118602119701
a = sqlite3_column_blob(pStmt, 0);
118603119702
a += sqlite3Fts3GetVarint(a, &nDoc);
119703
+ if( nDoc==0 ) return SQLITE_CORRUPT;
118604119704
*pnDoc = (u32)nDoc;
118605119705
118606119706
if( paLen ) *paLen = a;
118607119707
return SQLITE_OK;
118608119708
}
@@ -118805,13 +119905,15 @@
118805119905
118806119906
rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
118807119907
if( rc==SQLITE_OK ){
118808119908
int iCol;
118809119909
for(iCol=0; iCol<pInfo->nCol; iCol++){
119910
+ u32 iVal;
118810119911
sqlite3_int64 nToken;
118811119912
a += sqlite3Fts3GetVarint(a, &nToken);
118812
- pInfo->aMatchinfo[iCol] = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
119913
+ iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
119914
+ pInfo->aMatchinfo[iCol] = iVal;
118813119915
}
118814119916
}
118815119917
}
118816119918
break;
118817119919
118818119920
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.5. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one 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.
@@ -648,13 +648,13 @@
648 **
649 ** See also: [sqlite3_libversion()],
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.5"
654 #define SQLITE_VERSION_NUMBER 3007005
655 #define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -931,11 +931,11 @@
931 #define SQLITE_NOMEM 7 /* A malloc() failed */
932 #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
933 #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
934 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
935 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
936 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
937 #define SQLITE_FULL 13 /* Insertion failed because database is full */
938 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
939 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
940 #define SQLITE_EMPTY 16 /* Database is empty */
941 #define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -1163,11 +1163,13 @@
1163 ** locking strategy (for example to use dot-file locks), to inquire
1164 ** about the status of a lock, or to break stale locks. The SQLite
1165 ** core reserves all opcodes less than 100 for its own use.
1166 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
1167 ** Applications that define a custom xFileControl method should use opcodes
1168 ** greater than 100 to avoid conflicts.
 
 
1169 **
1170 ** The xSectorSize() method returns the sector size of the
1171 ** device that underlies the file. The sector size is the
1172 ** minimum write that can be performed without disturbing
1173 ** other bytes in the file. The xDeviceCharacteristics()
@@ -3210,11 +3212,11 @@
3210 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3211
3212 /*
3213 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3214 **
3215 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3216 ** and only if the [prepared statement] X makes no direct changes to
3217 ** the content of the database file.
3218 **
3219 ** Note that [application-defined SQL functions] or
3220 ** [virtual tables] might change the database indirectly as a side effect.
@@ -6078,28 +6080,25 @@
6078 ** checked out.</dd>)^
6079 **
6080 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6081 ** <dd>This parameter returns the number malloc attempts that were
6082 ** satisfied using lookaside memory. Only the high-water value is meaningful;
6083 ** the current value is always zero.
6084 ** checked out.</dd>)^
6085 **
6086 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6087 ** <dd>This parameter returns the number malloc attempts that might have
6088 ** been satisfied using lookaside memory but failed due to the amount of
6089 ** memory requested being larger than the lookaside slot size.
6090 ** Only the high-water value is meaningful;
6091 ** the current value is always zero.
6092 ** checked out.</dd>)^
6093 **
6094 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6095 ** <dd>This parameter returns the number malloc attempts that might have
6096 ** been satisfied using lookaside memory but failed due to all lookaside
6097 ** memory already being in use.
6098 ** Only the high-water value is meaningful;
6099 ** the current value is always zero.
6100 ** checked out.</dd>)^
6101 **
6102 ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6103 ** <dd>This parameter returns the approximate number of of bytes of heap
6104 ** memory used by all pager caches associated with the database connection.)^
6105 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6794,12 +6793,105 @@
6794 **
6795 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
6796 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6797 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
6798 ** run whenever the WAL reaches a certain size threshold.
 
 
6799 */
6800 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6801
6802 /*
6803 ** Undo the hack that converts floating point types to integer for
6804 ** builds on processors without floating point support.
6805 */
@@ -7683,11 +7775,11 @@
7683 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
7684 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
7685 #endif
7686
7687 #ifndef SQLITE_OMIT_WAL
7688 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*);
7689 #endif
7690
7691 /*
7692 ** If we are not using shared cache, then there is no need to
7693 ** use mutexes to access the BtShared structures. So make the
@@ -8293,11 +8385,11 @@
8293 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
8294 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8295 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8296 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8297
8298 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager);
8299 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8300 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8301 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8302 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8303
@@ -11250,11 +11342,11 @@
11250 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11251 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11252 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11253 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11254 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11255 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int);
11256 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11257
11258 /* Declarations for functions in fkey.c. All of these are replaced by
11259 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
11260 ** key functionality is available. If OMIT_TRIGGER is defined but
@@ -16933,11 +17025,11 @@
16933 ** <li> SQLITE_MUTEX_STATIC_MASTER
16934 ** <li> SQLITE_MUTEX_STATIC_MEM
16935 ** <li> SQLITE_MUTEX_STATIC_MEM2
16936 ** <li> SQLITE_MUTEX_STATIC_PRNG
16937 ** <li> SQLITE_MUTEX_STATIC_LRU
16938 ** <li> SQLITE_MUTEX_STATIC_LRU2
16939 ** </ul>
16940 **
16941 ** The first two constants cause sqlite3_mutex_alloc() to create
16942 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
16943 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -17343,11 +17435,11 @@
17343 ** <li> SQLITE_MUTEX_STATIC_MASTER
17344 ** <li> SQLITE_MUTEX_STATIC_MEM
17345 ** <li> SQLITE_MUTEX_STATIC_MEM2
17346 ** <li> SQLITE_MUTEX_STATIC_PRNG
17347 ** <li> SQLITE_MUTEX_STATIC_LRU
17348 ** <li> SQLITE_MUTEX_STATIC_LRU2
17349 ** </ul>
17350 **
17351 ** The first two constants cause sqlite3_mutex_alloc() to create
17352 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17353 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -22268,11 +22360,11 @@
22268 OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
22269 ((os2File*)id)->h, ((os2File*)id)->locktype ));
22270 return SQLITE_OK;
22271 }
22272 }
22273 return SQLITE_ERROR;
22274 }
22275
22276 /*
22277 ** Return the sector size in bytes of the underlying block device for
22278 ** the specified file. This is almost always 512 bytes, but may be
@@ -26204,12 +26296,15 @@
26204 case SQLITE_SET_LOCKPROXYFILE:
26205 case SQLITE_GET_LOCKPROXYFILE: {
26206 return proxyFileControl(id,op,pArg);
26207 }
26208 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 
 
 
26209 }
26210 return SQLITE_ERROR;
26211 }
26212
26213 /*
26214 ** Return the sector size in bytes of the underlying block device for
26215 ** the specified file. This is almost always 512 bytes, but may be
@@ -30792,12 +30887,15 @@
30792 SimulateIOErrorBenign(1);
30793 winTruncate(id, sz);
30794 SimulateIOErrorBenign(0);
30795 return SQLITE_OK;
30796 }
 
 
 
30797 }
30798 return SQLITE_ERROR;
30799 }
30800
30801 /*
30802 ** Return the sector size in bytes of the underlying block device for
30803 ** the specified file. This is almost always 512 bytes, but may be
@@ -33465,11 +33563,11 @@
33465 PGroup *pGroup; /* PGroup this cache belongs to */
33466 int szPage; /* Size of allocated pages in bytes */
33467 int bPurgeable; /* True if cache is purgeable */
33468 unsigned int nMin; /* Minimum number of pages reserved */
33469 unsigned int nMax; /* Configured "cache_size" value */
33470 unsigned int mxPinned; /* nMax*9/10 */
33471
33472 /* Hash table of all pages. The following variables may only be accessed
33473 ** when the accessor is holding the PGroup mutex.
33474 */
33475 unsigned int nRecyclable; /* Number of pages in the LRU list */
@@ -33619,11 +33717,13 @@
33619 ** it from sqlite3Malloc instead.
33620 */
33621 p = sqlite3Malloc(nByte);
33622 if( p ){
33623 int sz = sqlite3MallocSize(p);
 
33624 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 
33625 }
33626 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
33627 }
33628 return p;
33629 }
@@ -33647,11 +33747,13 @@
33647 }else{
33648 int iSize;
33649 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
33650 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
33651 iSize = sqlite3MallocSize(p);
 
33652 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
 
33653 sqlite3_free(p);
33654 }
33655 }
33656
33657 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -33990,11 +34092,11 @@
33990 PGroup *pGroup = pCache->pGroup;
33991 pcache1EnterMutex(pGroup);
33992 pGroup->nMaxPage += (nMax - pCache->nMax);
33993 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
33994 pCache->nMax = nMax;
33995 pCache->mxPinned = nMax*9/10;
33996 pcache1EnforceMaxPage(pGroup);
33997 pcache1LeaveMutex(pGroup);
33998 }
33999 }
34000
@@ -34100,15 +34202,16 @@
34100 #endif
34101
34102
34103 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
34104 nPinned = pCache->nPage - pCache->nRecyclable;
 
34105 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
34106 assert( pCache->mxPinned == pCache->nMax*9/10 );
34107 if( createFlag==1 && (
34108 nPinned>=pGroup->mxPinned
34109 || nPinned>=(int)pCache->mxPinned
34110 || pcache1UnderMemoryPressure(pCache)
34111 )){
34112 goto fetch_out;
34113 }
34114
@@ -34820,26 +34923,26 @@
34820 #ifndef _WAL_H_
34821 #define _WAL_H_
34822
34823
34824 #ifdef SQLITE_OMIT_WAL
34825 # define sqlite3WalOpen(x,y,z) 0
34826 # define sqlite3WalClose(w,x,y,z) 0
34827 # define sqlite3WalBeginReadTransaction(y,z) 0
34828 # define sqlite3WalEndReadTransaction(z)
34829 # define sqlite3WalRead(v,w,x,y,z) 0
34830 # define sqlite3WalDbsize(y) 0
34831 # define sqlite3WalBeginWriteTransaction(y) 0
34832 # define sqlite3WalEndWriteTransaction(x) 0
34833 # define sqlite3WalUndo(x,y,z) 0
34834 # define sqlite3WalSavepoint(y,z)
34835 # define sqlite3WalSavepointUndo(y,z) 0
34836 # define sqlite3WalFrames(u,v,w,x,y,z) 0
34837 # define sqlite3WalCheckpoint(u,v,w,x) 0
34838 # define sqlite3WalCallback(z) 0
34839 # define sqlite3WalExclusiveMode(y,z) 0
34840 # define sqlite3WalHeapMemory(z) 0
34841 #else
34842
34843 #define WAL_SAVEPOINT_NDATA 4
34844
34845 /* Connection to a write-ahead log (WAL) file.
@@ -34886,13 +34989,18 @@
34886 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
34887
34888 /* Copy pages from the log to the database file */
34889 SQLITE_PRIVATE int sqlite3WalCheckpoint(
34890 Wal *pWal, /* Write-ahead log connection */
 
 
 
34891 int sync_flags, /* Flags to sync db file with (or 0) */
34892 int nBuf, /* Size of buffer nBuf */
34893 u8 *zBuf /* Temporary buffer to use */
 
 
34894 );
34895
34896 /* Return the value to pass to a sqlite3_wal_hook callback, the
34897 ** number of frames in the WAL at the point of the last commit since
34898 ** sqlite3WalCallback() was called. If no commits have occurred since
@@ -37370,19 +37478,25 @@
37370
37371 if( isOpen(pPager->fd)
37372 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
37373 ){
37374 i64 currentSize, newSize;
 
37375 assert( pPager->eLock==EXCLUSIVE_LOCK );
37376 /* TODO: Is it safe to use Pager.dbFileSize here? */
37377 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
37378 newSize = pPager->pageSize*(i64)nPage;
37379 if( rc==SQLITE_OK && currentSize!=newSize ){
37380 if( currentSize>newSize ){
37381 rc = sqlite3OsTruncate(pPager->fd, newSize);
37382 }else{
37383 rc = sqlite3OsWrite(pPager->fd, "", 1, newSize-1);
 
 
 
 
 
37384 }
37385 if( rc==SQLITE_OK ){
37386 pPager->dbFileSize = nPage;
37387 }
37388 }
@@ -37737,10 +37851,32 @@
37737 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
37738 PAGERID(pPager), pgno, pager_pagehash(pPg)));
37739
37740 return rc;
37741 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37742
37743 #ifndef SQLITE_OMIT_WAL
37744 /*
37745 ** This function is invoked once for each page that has already been
37746 ** written into the log file when a WAL transaction is rolled back.
@@ -37808,38 +37944,15 @@
37808 }
37809
37810 return rc;
37811 }
37812
37813
37814 /*
37815 ** Update the value of the change-counter at offsets 24 and 92 in
37816 ** the header and the sqlite version number at offset 96.
37817 **
37818 ** This is an unconditional update. See also the pager_incr_changecounter()
37819 ** routine which only updates the change-counter if the update is actually
37820 ** needed, as determined by the pPager->changeCountDone state variable.
37821 */
37822 static void pager_write_changecounter(PgHdr *pPg){
37823 u32 change_counter;
37824
37825 /* Increment the value just read and write it back to byte 24. */
37826 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
37827 put32bits(((char*)pPg->pData)+24, change_counter);
37828
37829 /* Also store the SQLite version number in bytes 96..99 and in
37830 ** bytes 92..95 store the change counter for which the version number
37831 ** is valid. */
37832 put32bits(((char*)pPg->pData)+92, change_counter);
37833 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
37834 }
37835
37836 /*
37837 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
37838 ** the contents of the list of pages headed by pList (connected by pDirty),
37839 ** this function notifies any active backup processes that the pages have
37840 ** changed.
37841 **
37842 ** The list of pages passed into this routine is always sorted by page number.
37843 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
37844 */
37845 static int pagerWalFrames(
@@ -41487,18 +41600,24 @@
41487 return &pPager->pBackup;
41488 }
41489
41490 #ifndef SQLITE_OMIT_WAL
41491 /*
41492 ** This function is called when the user invokes "PRAGMA checkpoint".
 
 
 
 
41493 */
41494 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager){
41495 int rc = SQLITE_OK;
41496 if( pPager->pWal ){
41497 u8 *zBuf = (u8 *)pPager->pTmpSpace;
41498 rc = sqlite3WalCheckpoint(pPager->pWal, pPager->ckptSyncFlags,
41499 pPager->pageSize, zBuf);
 
 
41500 }
41501 return rc;
41502 }
41503
41504 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -43233,10 +43352,38 @@
43233 walIteratorFree(p);
43234 }
43235 *pp = p;
43236 return rc;
43237 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43238
43239 /*
43240 ** Copy as much content as we can from the WAL back into the database file
43241 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
43242 **
@@ -43267,12 +43414,14 @@
43267 ** checkpoint is running (in any other thread or process) at the same
43268 ** time.
43269 */
43270 static int walCheckpoint(
43271 Wal *pWal, /* Wal connection */
 
 
 
43272 int sync_flags, /* Flags for OsSync() (or 0) */
43273 int nBuf, /* Size of zBuf in bytes */
43274 u8 *zBuf /* Temporary buffer to use */
43275 ){
43276 int rc; /* Return code */
43277 int szPage; /* Database page-size */
43278 WalIterator *pIter = 0; /* Wal iterator context */
@@ -43280,12 +43429,13 @@
43280 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
43281 u32 mxSafeFrame; /* Max frame that can be backfilled */
43282 u32 mxPage; /* Max database page to write */
43283 int i; /* Loop counter */
43284 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
 
43285
43286 szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
43287 testcase( szPage<=32768 );
43288 testcase( szPage>=65536 );
43289 pInfo = walCkptInfo(pWal);
43290 if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
43291
@@ -43294,15 +43444,12 @@
43294 if( rc!=SQLITE_OK ){
43295 return rc;
43296 }
43297 assert( pIter );
43298
43299 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
43300 if( szPage!=nBuf ){
43301 rc = SQLITE_CORRUPT_BKPT;
43302 goto walcheckpoint_out;
43303 }
43304
43305 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
43306 ** safe to write into the database. Frames beyond mxSafeFrame might
43307 ** overwrite database pages that are in use by active readers and thus
43308 ** cannot be backfilled from the WAL.
@@ -43309,26 +43456,27 @@
43309 */
43310 mxSafeFrame = pWal->hdr.mxFrame;
43311 mxPage = pWal->hdr.nPage;
43312 for(i=1; i<WAL_NREADER; i++){
43313 u32 y = pInfo->aReadMark[i];
43314 if( mxSafeFrame>=y ){
43315 assert( y<=pWal->hdr.mxFrame );
43316 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
43317 if( rc==SQLITE_OK ){
43318 pInfo->aReadMark[i] = READMARK_NOT_USED;
43319 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
43320 }else if( rc==SQLITE_BUSY ){
43321 mxSafeFrame = y;
 
43322 }else{
43323 goto walcheckpoint_out;
43324 }
43325 }
43326 }
43327
43328 if( pInfo->nBackfill<mxSafeFrame
43329 && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
43330 ){
43331 i64 nSize; /* Current size of database file */
43332 u32 nBackfill = pInfo->nBackfill;
43333
43334 /* Sync the WAL to disk */
@@ -43377,16 +43525,35 @@
43377 }
43378 }
43379
43380 /* Release the reader lock held while backfilling */
43381 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
43382 }else if( rc==SQLITE_BUSY ){
 
 
43383 /* Reset the return code so as not to report a checkpoint failure
43384 ** just because active readers prevent any backfill.
43385 */
43386 rc = SQLITE_OK;
43387 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43388
43389 walcheckpoint_out:
43390 walIteratorFree(pIter);
43391 return rc;
43392 }
@@ -43415,11 +43582,13 @@
43415 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
43416 if( rc==SQLITE_OK ){
43417 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
43418 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
43419 }
43420 rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf);
 
 
43421 if( rc==SQLITE_OK ){
43422 isDelete = 1;
43423 }
43424 }
43425
@@ -44330,21 +44499,31 @@
44330 ** This routine is called to implement sqlite3_wal_checkpoint() and
44331 ** related interfaces.
44332 **
44333 ** Obtain a CHECKPOINT lock and then backfill as much information as
44334 ** we can from WAL into the database.
 
 
 
44335 */
44336 SQLITE_PRIVATE int sqlite3WalCheckpoint(
44337 Wal *pWal, /* Wal connection */
 
 
 
44338 int sync_flags, /* Flags to sync db file with (or 0) */
44339 int nBuf, /* Size of temporary buffer */
44340 u8 *zBuf /* Temporary buffer to use */
 
 
44341 ){
44342 int rc; /* Return code */
44343 int isChanged = 0; /* True if a new wal-index header is loaded */
 
44344
44345 assert( pWal->ckptLock==0 );
 
44346
44347 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
44348 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
44349 if( rc ){
44350 /* Usually this is SQLITE_BUSY meaning that another thread or process
@@ -44351,16 +44530,50 @@
44351 ** is already running a checkpoint, or maybe a recovery. But it might
44352 ** also be SQLITE_IOERR. */
44353 return rc;
44354 }
44355 pWal->ckptLock = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44356
44357 /* Copy data from the log to the database file. */
44358 rc = walIndexReadHdr(pWal, &isChanged);
44359 if( rc==SQLITE_OK ){
44360 rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf);
 
 
 
 
 
 
 
 
 
 
44361 }
 
44362 if( isChanged ){
44363 /* If a new wal-index header was loaded before the checkpoint was
44364 ** performed, then the pager-cache associated with pWal is now
44365 ** out of date. So zero the cached wal-index header to ensure that
44366 ** next time the pager opens a snapshot on this database it knows that
@@ -44368,14 +44581,15 @@
44368 */
44369 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
44370 }
44371
44372 /* Release the locks. */
 
44373 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
44374 pWal->ckptLock = 0;
44375 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
44376 return rc;
44377 }
44378
44379 /* Return the value to pass to a sqlite3_wal_hook callback, the
44380 ** number of frames in the WAL at the point of the last commit since
44381 ** sqlite3WalCallback() was called. If no commits have occurred since
@@ -47851,11 +48065,11 @@
47851 freeTempSpace(pBt);
47852 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
47853 pageSize-usableSize);
47854 return rc;
47855 }
47856 if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPageHeader>nPageFile ){
47857 rc = SQLITE_CORRUPT_BKPT;
47858 goto page1_init_failed;
47859 }
47860 if( usableSize<480 ){
47861 goto page1_init_failed;
@@ -53402,20 +53616,22 @@
53402 /*
53403 ** Run a checkpoint on the Btree passed as the first argument.
53404 **
53405 ** Return SQLITE_LOCKED if this or any other connection has an open
53406 ** transaction on the shared-cache the argument Btree is connected to.
 
 
53407 */
53408 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p){
53409 int rc = SQLITE_OK;
53410 if( p ){
53411 BtShared *pBt = p->pBt;
53412 sqlite3BtreeEnter(p);
53413 if( pBt->inTransaction!=TRANS_NONE ){
53414 rc = SQLITE_LOCKED;
53415 }else{
53416 rc = sqlite3PagerCheckpoint(pBt->pPager);
53417 }
53418 sqlite3BtreeLeave(p);
53419 }
53420 return rc;
53421 }
@@ -55371,10 +55587,12 @@
55371 pVal->u.i = -1 * pVal->u.i;
55372 /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
55373 pVal->r = (double)-1 * pVal->r;
55374 sqlite3ValueApplyAffinity(pVal, affinity, enc);
55375 }
 
 
55376 }
55377 #ifndef SQLITE_OMIT_BLOB_LITERAL
55378 else if( op==TK_BLOB ){
55379 int nVal;
55380 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
@@ -57549,20 +57767,25 @@
57549 if( !sqlite3VtabInSync(db)
57550 && db->autoCommit
57551 && db->writeVdbeCnt==(p->readOnly==0)
57552 ){
57553 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
57554 if( sqlite3VdbeCheckFk(p, 1) ){
57555 sqlite3BtreeMutexArrayLeave(&p->aMutex);
57556 return SQLITE_ERROR;
57557 }
57558 /* The auto-commit flag is true, the vdbe program was successful
57559 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
57560 ** key constraints to hold up the transaction. This means a commit
57561 ** is required. */
57562 rc = vdbeCommit(db, p);
57563 if( rc==SQLITE_BUSY ){
 
 
 
 
 
57564 sqlite3BtreeMutexArrayLeave(&p->aMutex);
57565 return SQLITE_BUSY;
57566 }else if( rc!=SQLITE_OK ){
57567 p->rc = rc;
57568 sqlite3RollbackAll(db);
@@ -57657,11 +57880,11 @@
57657 if( db->autoCommit ){
57658 sqlite3ConnectionUnlocked(db);
57659 }
57660
57661 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
57662 return SQLITE_OK;
57663 }
57664
57665
57666 /*
57667 ** Each VDBE holds the result of the most recent sqlite3_step() call
@@ -61075,29 +61298,34 @@
61075 sqlite3_value **apVal;
61076 } cb;
61077 struct OP_AggFinal_stack_vars {
61078 Mem *pMem;
61079 } cc;
 
 
 
 
 
61080 struct OP_JournalMode_stack_vars {
61081 Btree *pBt; /* Btree to change journal mode of */
61082 Pager *pPager; /* Pager associated with pBt */
61083 int eNew; /* New journal mode */
61084 int eOld; /* The old journal mode */
61085 const char *zFilename; /* Name of database file for pPager */
61086 } cd;
61087 struct OP_IncrVacuum_stack_vars {
61088 Btree *pBt;
61089 } ce;
61090 struct OP_VBegin_stack_vars {
61091 VTable *pVTab;
61092 } cf;
61093 struct OP_VOpen_stack_vars {
61094 VdbeCursor *pCur;
61095 sqlite3_vtab_cursor *pVtabCursor;
61096 sqlite3_vtab *pVtab;
61097 sqlite3_module *pModule;
61098 } cg;
61099 struct OP_VFilter_stack_vars {
61100 int nArg;
61101 int iQuery;
61102 const sqlite3_module *pModule;
61103 Mem *pQuery;
@@ -61106,39 +61334,39 @@
61106 sqlite3_vtab *pVtab;
61107 VdbeCursor *pCur;
61108 int res;
61109 int i;
61110 Mem **apArg;
61111 } ch;
61112 struct OP_VColumn_stack_vars {
61113 sqlite3_vtab *pVtab;
61114 const sqlite3_module *pModule;
61115 Mem *pDest;
61116 sqlite3_context sContext;
61117 } ci;
61118 struct OP_VNext_stack_vars {
61119 sqlite3_vtab *pVtab;
61120 const sqlite3_module *pModule;
61121 int res;
61122 VdbeCursor *pCur;
61123 } cj;
61124 struct OP_VRename_stack_vars {
61125 sqlite3_vtab *pVtab;
61126 Mem *pName;
61127 } ck;
61128 struct OP_VUpdate_stack_vars {
61129 sqlite3_vtab *pVtab;
61130 sqlite3_module *pModule;
61131 int nArg;
61132 int i;
61133 sqlite_int64 rowid;
61134 Mem **apArg;
61135 Mem *pX;
61136 } cl;
61137 struct OP_Trace_stack_vars {
61138 char *zTrace;
61139 } cm;
61140 } u;
61141 /* End automatically generated code
61142 ********************************************************************/
61143
61144 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -65894,17 +66122,42 @@
65894 }
65895 break;
65896 }
65897
65898 #ifndef SQLITE_OMIT_WAL
65899 /* Opcode: Checkpoint P1 * * * *
65900 **
65901 ** Checkpoint database P1. This is a no-op if P1 is not currently in
65902 ** WAL mode.
 
 
 
 
 
 
65903 */
65904 case OP_Checkpoint: {
65905 rc = sqlite3Checkpoint(db, pOp->p1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65906 break;
65907 };
65908 #endif
65909
65910 #ifndef SQLITE_OMIT_PRAGMA
@@ -65918,26 +66171,26 @@
65918 ** If changing into or out of WAL mode the procedure is more complicated.
65919 **
65920 ** Write a string containing the final journal-mode to register P2.
65921 */
65922 case OP_JournalMode: { /* out2-prerelease */
65923 #if 0 /* local variables moved into u.cd */
65924 Btree *pBt; /* Btree to change journal mode of */
65925 Pager *pPager; /* Pager associated with pBt */
65926 int eNew; /* New journal mode */
65927 int eOld; /* The old journal mode */
65928 const char *zFilename; /* Name of database file for pPager */
65929 #endif /* local variables moved into u.cd */
65930
65931 u.cd.eNew = pOp->p3;
65932 assert( u.cd.eNew==PAGER_JOURNALMODE_DELETE
65933 || u.cd.eNew==PAGER_JOURNALMODE_TRUNCATE
65934 || u.cd.eNew==PAGER_JOURNALMODE_PERSIST
65935 || u.cd.eNew==PAGER_JOURNALMODE_OFF
65936 || u.cd.eNew==PAGER_JOURNALMODE_MEMORY
65937 || u.cd.eNew==PAGER_JOURNALMODE_WAL
65938 || u.cd.eNew==PAGER_JOURNALMODE_QUERY
65939 );
65940 assert( pOp->p1>=0 && pOp->p1<db->nDb );
65941
65942 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
65943 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
@@ -65956,76 +66209,76 @@
65956 ** database. */
65957 sqlite3VdbeUsesBtree(p, pOp->p1);
65958 sqlite3VdbeMutexArrayEnter(p);
65959 }
65960
65961 u.cd.pBt = db->aDb[pOp->p1].pBt;
65962 u.cd.pPager = sqlite3BtreePager(u.cd.pBt);
65963 u.cd.eOld = sqlite3PagerGetJournalMode(u.cd.pPager);
65964 if( u.cd.eNew==PAGER_JOURNALMODE_QUERY ) u.cd.eNew = u.cd.eOld;
65965 if( !sqlite3PagerOkToChangeJournalMode(u.cd.pPager) ) u.cd.eNew = u.cd.eOld;
65966
65967 #ifndef SQLITE_OMIT_WAL
65968 u.cd.zFilename = sqlite3PagerFilename(u.cd.pPager);
65969
65970 /* Do not allow a transition to journal_mode=WAL for a database
65971 ** in temporary storage or if the VFS does not support shared memory
65972 */
65973 if( u.cd.eNew==PAGER_JOURNALMODE_WAL
65974 && (u.cd.zFilename[0]==0 /* Temp file */
65975 || !sqlite3PagerWalSupported(u.cd.pPager)) /* No shared-memory support */
65976 ){
65977 u.cd.eNew = u.cd.eOld;
65978 }
65979
65980 if( (u.cd.eNew!=u.cd.eOld)
65981 && (u.cd.eOld==PAGER_JOURNALMODE_WAL || u.cd.eNew==PAGER_JOURNALMODE_WAL)
65982 ){
65983 if( !db->autoCommit || db->activeVdbeCnt>1 ){
65984 rc = SQLITE_ERROR;
65985 sqlite3SetString(&p->zErrMsg, db,
65986 "cannot change %s wal mode from within a transaction",
65987 (u.cd.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
65988 );
65989 break;
65990 }else{
65991
65992 if( u.cd.eOld==PAGER_JOURNALMODE_WAL ){
65993 /* If leaving WAL mode, close the log file. If successful, the call
65994 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
65995 ** file. An EXCLUSIVE lock may still be held on the database file
65996 ** after a successful return.
65997 */
65998 rc = sqlite3PagerCloseWal(u.cd.pPager);
65999 if( rc==SQLITE_OK ){
66000 sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
66001 }
66002 }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){
66003 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
66004 ** as an intermediate */
66005 sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF);
66006 }
66007
66008 /* Open a transaction on the database file. Regardless of the journal
66009 ** mode, this transaction always uses a rollback journal.
66010 */
66011 assert( sqlite3BtreeIsInTrans(u.cd.pBt)==0 );
66012 if( rc==SQLITE_OK ){
66013 rc = sqlite3BtreeSetVersion(u.cd.pBt, (u.cd.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
66014 }
66015 }
66016 }
66017 #endif /* ifndef SQLITE_OMIT_WAL */
66018
66019 if( rc ){
66020 u.cd.eNew = u.cd.eOld;
66021 }
66022 u.cd.eNew = sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
66023
66024 pOut = &aMem[pOp->p2];
66025 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
66026 pOut->z = (char *)sqlite3JournalModename(u.cd.eNew);
66027 pOut->n = sqlite3Strlen30(pOut->z);
66028 pOut->enc = SQLITE_UTF8;
66029 sqlite3VdbeChangeEncoding(pOut, encoding);
66030 break;
66031 };
@@ -66050,18 +66303,18 @@
66050 ** Perform a single step of the incremental vacuum procedure on
66051 ** the P1 database. If the vacuum has finished, jump to instruction
66052 ** P2. Otherwise, fall through to the next instruction.
66053 */
66054 case OP_IncrVacuum: { /* jump */
66055 #if 0 /* local variables moved into u.ce */
66056 Btree *pBt;
66057 #endif /* local variables moved into u.ce */
66058
66059 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66060 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66061 u.ce.pBt = db->aDb[pOp->p1].pBt;
66062 rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
66063 if( rc==SQLITE_DONE ){
66064 pc = pOp->p2 - 1;
66065 rc = SQLITE_OK;
66066 }
66067 break;
@@ -66127,16 +66380,16 @@
66127 ** Also, whether or not P4 is set, check that this is not being called from
66128 ** within a callback to a virtual table xSync() method. If it is, the error
66129 ** code will be set to SQLITE_LOCKED.
66130 */
66131 case OP_VBegin: {
66132 #if 0 /* local variables moved into u.cf */
66133 VTable *pVTab;
66134 #endif /* local variables moved into u.cf */
66135 u.cf.pVTab = pOp->p4.pVtab;
66136 rc = sqlite3VtabBegin(db, u.cf.pVTab);
66137 if( u.cf.pVTab ) importVtabErrMsg(p, u.cf.pVTab->pVtab);
66138 break;
66139 }
66140 #endif /* SQLITE_OMIT_VIRTUALTABLE */
66141
66142 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -66171,36 +66424,36 @@
66171 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
66172 ** P1 is a cursor number. This opcode opens a cursor to the virtual
66173 ** table and stores that cursor in P1.
66174 */
66175 case OP_VOpen: {
66176 #if 0 /* local variables moved into u.cg */
66177 VdbeCursor *pCur;
66178 sqlite3_vtab_cursor *pVtabCursor;
66179 sqlite3_vtab *pVtab;
66180 sqlite3_module *pModule;
66181 #endif /* local variables moved into u.cg */
66182
66183 u.cg.pCur = 0;
66184 u.cg.pVtabCursor = 0;
66185 u.cg.pVtab = pOp->p4.pVtab->pVtab;
66186 u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
66187 assert(u.cg.pVtab && u.cg.pModule);
66188 rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
66189 importVtabErrMsg(p, u.cg.pVtab);
66190 if( SQLITE_OK==rc ){
66191 /* Initialize sqlite3_vtab_cursor base class */
66192 u.cg.pVtabCursor->pVtab = u.cg.pVtab;
66193
66194 /* Initialise vdbe cursor object */
66195 u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
66196 if( u.cg.pCur ){
66197 u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
66198 u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
66199 }else{
66200 db->mallocFailed = 1;
66201 u.cg.pModule->xClose(u.cg.pVtabCursor);
66202 }
66203 }
66204 break;
66205 }
66206 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66223,11 +66476,11 @@
66223 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
66224 **
66225 ** A jump is made to P2 if the result set after filtering would be empty.
66226 */
66227 case OP_VFilter: { /* jump */
66228 #if 0 /* local variables moved into u.ch */
66229 int nArg;
66230 int iQuery;
66231 const sqlite3_module *pModule;
66232 Mem *pQuery;
66233 Mem *pArgc;
@@ -66235,49 +66488,49 @@
66235 sqlite3_vtab *pVtab;
66236 VdbeCursor *pCur;
66237 int res;
66238 int i;
66239 Mem **apArg;
66240 #endif /* local variables moved into u.ch */
66241
66242 u.ch.pQuery = &aMem[pOp->p3];
66243 u.ch.pArgc = &u.ch.pQuery[1];
66244 u.ch.pCur = p->apCsr[pOp->p1];
66245 assert( memIsValid(u.ch.pQuery) );
66246 REGISTER_TRACE(pOp->p3, u.ch.pQuery);
66247 assert( u.ch.pCur->pVtabCursor );
66248 u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
66249 u.ch.pVtab = u.ch.pVtabCursor->pVtab;
66250 u.ch.pModule = u.ch.pVtab->pModule;
66251
66252 /* Grab the index number and argc parameters */
66253 assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
66254 u.ch.nArg = (int)u.ch.pArgc->u.i;
66255 u.ch.iQuery = (int)u.ch.pQuery->u.i;
66256
66257 /* Invoke the xFilter method */
66258 {
66259 u.ch.res = 0;
66260 u.ch.apArg = p->apArg;
66261 for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
66262 u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
66263 sqlite3VdbeMemStoreType(u.ch.apArg[u.ch.i]);
66264 }
66265
66266 p->inVtabMethod = 1;
66267 rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
66268 p->inVtabMethod = 0;
66269 importVtabErrMsg(p, u.ch.pVtab);
66270 if( rc==SQLITE_OK ){
66271 u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
66272 }
66273
66274 if( u.ch.res ){
66275 pc = pOp->p2 - 1;
66276 }
66277 }
66278 u.ch.pCur->nullRow = 0;
66279
66280 break;
66281 }
66282 #endif /* SQLITE_OMIT_VIRTUALTABLE */
66283
@@ -66287,55 +66540,55 @@
66287 ** Store the value of the P2-th column of
66288 ** the row of the virtual-table that the
66289 ** P1 cursor is pointing to into register P3.
66290 */
66291 case OP_VColumn: {
66292 #if 0 /* local variables moved into u.ci */
66293 sqlite3_vtab *pVtab;
66294 const sqlite3_module *pModule;
66295 Mem *pDest;
66296 sqlite3_context sContext;
66297 #endif /* local variables moved into u.ci */
66298
66299 VdbeCursor *pCur = p->apCsr[pOp->p1];
66300 assert( pCur->pVtabCursor );
66301 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66302 u.ci.pDest = &aMem[pOp->p3];
66303 memAboutToChange(p, u.ci.pDest);
66304 if( pCur->nullRow ){
66305 sqlite3VdbeMemSetNull(u.ci.pDest);
66306 break;
66307 }
66308 u.ci.pVtab = pCur->pVtabCursor->pVtab;
66309 u.ci.pModule = u.ci.pVtab->pModule;
66310 assert( u.ci.pModule->xColumn );
66311 memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
66312
66313 /* The output cell may already have a buffer allocated. Move
66314 ** the current contents to u.ci.sContext.s so in case the user-function
66315 ** can use the already allocated buffer instead of allocating a
66316 ** new one.
66317 */
66318 sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
66319 MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
66320
66321 rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
66322 importVtabErrMsg(p, u.ci.pVtab);
66323 if( u.ci.sContext.isError ){
66324 rc = u.ci.sContext.isError;
66325 }
66326
66327 /* Copy the result of the function to the P3 register. We
66328 ** do this regardless of whether or not an error occurred to ensure any
66329 ** dynamic allocation in u.ci.sContext.s (a Mem struct) is released.
66330 */
66331 sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
66332 sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
66333 REGISTER_TRACE(pOp->p3, u.ci.pDest);
66334 UPDATE_MAX_BLOBSIZE(u.ci.pDest);
66335
66336 if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
66337 goto too_big;
66338 }
66339 break;
66340 }
66341 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66346,42 +66599,42 @@
66346 ** Advance virtual table P1 to the next row in its result set and
66347 ** jump to instruction P2. Or, if the virtual table has reached
66348 ** the end of its result set, then fall through to the next instruction.
66349 */
66350 case OP_VNext: { /* jump */
66351 #if 0 /* local variables moved into u.cj */
66352 sqlite3_vtab *pVtab;
66353 const sqlite3_module *pModule;
66354 int res;
66355 VdbeCursor *pCur;
66356 #endif /* local variables moved into u.cj */
66357
66358 u.cj.res = 0;
66359 u.cj.pCur = p->apCsr[pOp->p1];
66360 assert( u.cj.pCur->pVtabCursor );
66361 if( u.cj.pCur->nullRow ){
66362 break;
66363 }
66364 u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
66365 u.cj.pModule = u.cj.pVtab->pModule;
66366 assert( u.cj.pModule->xNext );
66367
66368 /* Invoke the xNext() method of the module. There is no way for the
66369 ** underlying implementation to return an error if one occurs during
66370 ** xNext(). Instead, if an error occurs, true is returned (indicating that
66371 ** data is available) and the error code returned when xColumn or
66372 ** some other method is next invoked on the save virtual table cursor.
66373 */
66374 p->inVtabMethod = 1;
66375 rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
66376 p->inVtabMethod = 0;
66377 importVtabErrMsg(p, u.cj.pVtab);
66378 if( rc==SQLITE_OK ){
66379 u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
66380 }
66381
66382 if( !u.cj.res ){
66383 /* If there is data, jump to P2 */
66384 pc = pOp->p2 - 1;
66385 }
66386 break;
66387 }
@@ -66393,23 +66646,23 @@
66393 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
66394 ** This opcode invokes the corresponding xRename method. The value
66395 ** in register P1 is passed as the zName argument to the xRename method.
66396 */
66397 case OP_VRename: {
66398 #if 0 /* local variables moved into u.ck */
66399 sqlite3_vtab *pVtab;
66400 Mem *pName;
66401 #endif /* local variables moved into u.ck */
66402
66403 u.ck.pVtab = pOp->p4.pVtab->pVtab;
66404 u.ck.pName = &aMem[pOp->p1];
66405 assert( u.ck.pVtab->pModule->xRename );
66406 assert( memIsValid(u.ck.pName) );
66407 REGISTER_TRACE(pOp->p1, u.ck.pName);
66408 assert( u.ck.pName->flags & MEM_Str );
66409 rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
66410 importVtabErrMsg(p, u.ck.pVtab);
66411 p->expired = 0;
66412
66413 break;
66414 }
66415 #endif
@@ -66437,39 +66690,39 @@
66437 ** P1 is a boolean flag. If it is set to true and the xUpdate call
66438 ** is successful, then the value returned by sqlite3_last_insert_rowid()
66439 ** is set to the value of the rowid for the row just inserted.
66440 */
66441 case OP_VUpdate: {
66442 #if 0 /* local variables moved into u.cl */
66443 sqlite3_vtab *pVtab;
66444 sqlite3_module *pModule;
66445 int nArg;
66446 int i;
66447 sqlite_int64 rowid;
66448 Mem **apArg;
66449 Mem *pX;
66450 #endif /* local variables moved into u.cl */
66451
66452 u.cl.pVtab = pOp->p4.pVtab->pVtab;
66453 u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
66454 u.cl.nArg = pOp->p2;
66455 assert( pOp->p4type==P4_VTAB );
66456 if( ALWAYS(u.cl.pModule->xUpdate) ){
66457 u.cl.apArg = p->apArg;
66458 u.cl.pX = &aMem[pOp->p3];
66459 for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
66460 assert( memIsValid(u.cl.pX) );
66461 memAboutToChange(p, u.cl.pX);
66462 sqlite3VdbeMemStoreType(u.cl.pX);
66463 u.cl.apArg[u.cl.i] = u.cl.pX;
66464 u.cl.pX++;
66465 }
66466 rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
66467 importVtabErrMsg(p, u.cl.pVtab);
66468 if( rc==SQLITE_OK && pOp->p1 ){
66469 assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
66470 db->lastRowid = u.cl.rowid;
66471 }
66472 p->nChange++;
66473 }
66474 break;
66475 }
@@ -66517,24 +66770,24 @@
66517 **
66518 ** If tracing is enabled (by the sqlite3_trace()) interface, then
66519 ** the UTF-8 string contained in P4 is emitted on the trace callback.
66520 */
66521 case OP_Trace: {
66522 #if 0 /* local variables moved into u.cm */
66523 char *zTrace;
66524 #endif /* local variables moved into u.cm */
66525
66526 u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
66527 if( u.cm.zTrace ){
66528 if( db->xTrace ){
66529 char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace);
66530 db->xTrace(db->pTraceArg, z);
66531 sqlite3DbFree(db, z);
66532 }
66533 #ifdef SQLITE_DEBUG
66534 if( (db->flags & SQLITE_SqlTrace)!=0 ){
66535 sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace);
66536 }
66537 #endif /* SQLITE_DEBUG */
66538 }
66539 break;
66540 }
@@ -69058,11 +69311,11 @@
69058 ** there is no default collation type, return 0.
69059 */
69060 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
69061 CollSeq *pColl = 0;
69062 Expr *p = pExpr;
69063 while( ALWAYS(p) ){
69064 int op;
69065 pColl = p->pColl;
69066 if( pColl ) break;
69067 op = p->op;
69068 if( p->pTab!=0 && (
@@ -72217,18 +72470,20 @@
72217 case TK_BETWEEN: {
72218 testcase( jumpIfNull==0 );
72219 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
72220 break;
72221 }
 
72222 case TK_IN: {
72223 int destIfFalse = sqlite3VdbeMakeLabel(v);
72224 int destIfNull = jumpIfNull ? dest : destIfFalse;
72225 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
72226 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
72227 sqlite3VdbeResolveLabel(v, destIfFalse);
72228 break;
72229 }
 
72230 default: {
72231 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
72232 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
72233 testcase( regFree1==0 );
72234 testcase( jumpIfNull==0 );
@@ -72358,10 +72613,11 @@
72358 case TK_BETWEEN: {
72359 testcase( jumpIfNull==0 );
72360 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
72361 break;
72362 }
 
72363 case TK_IN: {
72364 if( jumpIfNull ){
72365 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
72366 }else{
72367 int destIfNull = sqlite3VdbeMakeLabel(v);
@@ -72368,10 +72624,11 @@
72368 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
72369 sqlite3VdbeResolveLabel(v, destIfNull);
72370 }
72371 break;
72372 }
 
72373 default: {
72374 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
72375 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
72376 testcase( regFree1==0 );
72377 testcase( jumpIfNull==0 );
@@ -74549,13 +74806,15 @@
74549 goto attach_end;
74550 }
74551
74552 #ifndef SQLITE_OMIT_AUTHORIZATION
74553 if( pAuthArg ){
74554 char *zAuthArg = pAuthArg->u.zToken;
74555 if( NEVER(zAuthArg==0) ){
74556 goto attach_end;
 
 
74557 }
74558 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
74559 if(rc!=SQLITE_OK ){
74560 goto attach_end;
74561 }
@@ -87034,17 +87293,33 @@
87034 }else
87035 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
87036
87037 #ifndef SQLITE_OMIT_WAL
87038 /*
87039 ** PRAGMA [database.]wal_checkpoint
87040 **
87041 ** Checkpoint the database.
87042 */
87043 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
 
 
 
 
 
 
 
 
 
87044 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
87045 sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
 
 
 
 
 
 
 
87046 }else
87047
87048 /*
87049 ** PRAGMA wal_autocheckpoint
87050 ** PRAGMA wal_autocheckpoint = N
@@ -90670,10 +90945,13 @@
90670 ** (20) If the sub-query is a compound select, then it must not use
90671 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
90672 ** somewhat by saying that the terms of the ORDER BY clause must
90673 ** appear as unmodified result columns in the outer query. But
90674 ** have other optimizations in mind to deal with that case.
 
 
 
90675 **
90676 ** In this routine, the "p" parameter is a pointer to the outer query.
90677 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
90678 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
90679 **
@@ -90739,10 +91017,13 @@
90739 if( p->pOrderBy && pSub->pOrderBy ){
90740 return 0; /* Restriction (11) */
90741 }
90742 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
90743 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
 
 
 
90744
90745 /* OBSOLETE COMMENT 1:
90746 ** Restriction 3: If the subquery is a join, make sure the subquery is
90747 ** not used as the right operand of an outer join. Examples of why this
90748 ** is not allowed:
@@ -95714,10 +95995,11 @@
95714 ** generating the code that loops through a table looking for applicable
95715 ** rows. Indices are selected and used to speed the search when doing
95716 ** so is applicable. Because this module is responsible for selecting
95717 ** indices, you might also think of this module as the "query optimizer".
95718 */
 
95719
95720 /*
95721 ** Trace output macros
95722 */
95723 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
@@ -95814,10 +96096,15 @@
95814 #define TERM_CODED 0x04 /* This term is already coded */
95815 #define TERM_COPIED 0x08 /* Has a child */
95816 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
95817 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
95818 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
 
 
 
 
 
95819
95820 /*
95821 ** An instance of the following structure holds all information about a
95822 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
95823 */
@@ -95907,10 +96194,11 @@
95907 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
95908 #define WO_MATCH 0x040
95909 #define WO_ISNULL 0x080
95910 #define WO_OR 0x100 /* Two or more OR-connected terms */
95911 #define WO_AND 0x200 /* Two or more AND-connected terms */
 
95912
95913 #define WO_ALL 0xfff /* Mask of all possible WO_* values */
95914 #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
95915
95916 /*
@@ -96757,11 +97045,11 @@
96757 pWC->a[idxNew].iParent = idxTerm;
96758 pTerm->nChild = 1;
96759 }else{
96760 sqlite3ExprListDelete(db, pList);
96761 }
96762 pTerm->eOperator = 0; /* case 1 trumps case 2 */
96763 }
96764 }
96765 }
96766 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
96767
@@ -97021,10 +97309,48 @@
97021 pNewTerm->prereqAll = pTerm->prereqAll;
97022 }
97023 }
97024 #endif /* SQLITE_OMIT_VIRTUALTABLE */
97025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97026 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
97027 ** an index for tables to the left of the join.
97028 */
97029 pTerm->prereqRight |= extraRight;
97030 }
@@ -97073,10 +97399,11 @@
97073 WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
97074 Index *pIdx, /* The index we are testing */
97075 int base, /* Cursor number for the table to be sorted */
97076 ExprList *pOrderBy, /* The ORDER BY clause */
97077 int nEqCol, /* Number of index columns with == constraints */
 
97078 int *pbRev /* Set to 1 if ORDER BY is DESC */
97079 ){
97080 int i, j; /* Loop counters */
97081 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
97082 int nTerm; /* Number of ORDER BY terms */
@@ -97178,15 +97505,18 @@
97178 /* All terms of the ORDER BY clause are covered by this index so
97179 ** this index can be used for sorting. */
97180 return 1;
97181 }
97182 if( pIdx->onError!=OE_None && i==pIdx->nColumn
 
97183 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
97184 /* All terms of this index match some prefix of the ORDER BY clause
97185 ** and the index is UNIQUE and no terms on the tail of the ORDER BY
97186 ** clause reference other tables in a join. If this is all true then
97187 ** the order by clause is superfluous. */
 
 
97188 return 1;
97189 }
97190 return 0;
97191 }
97192
@@ -97419,11 +97749,11 @@
97419
97420 /* Search for any equality comparison term */
97421 pWCEnd = &pWC->a[pWC->nTerm];
97422 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
97423 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
97424 WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
97425 pCost->rCost, costTempIdx));
97426 pCost->rCost = costTempIdx;
97427 pCost->plan.nRow = logN + 1;
97428 pCost->plan.wsFlags = WHERE_TEMP_INDEX;
97429 pCost->used = pTerm->prereqRight;
@@ -97540,11 +97870,12 @@
97540 if( (idxCols & cMask)==0 ){
97541 Expr *pX = pTerm->pExpr;
97542 idxCols |= cMask;
97543 pIdx->aiColumn[n] = pTerm->u.leftColumn;
97544 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
97545 pIdx->azColl[n] = pColl->zName;
 
97546 n++;
97547 }
97548 }
97549 }
97550 assert( (u32)n==pLevel->plan.nEq );
@@ -97898,15 +98229,22 @@
97898 #endif /* SQLITE_OMIT_VIRTUALTABLE */
97899
97900 /*
97901 ** Argument pIdx is a pointer to an index structure that has an array of
97902 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
97903 ** stored in Index.aSample. The domain of values stored in said column
97904 ** may be thought of as divided into (SQLITE_INDEX_SAMPLES+1) regions.
97905 ** Region 0 contains all values smaller than the first sample value. Region
97906 ** 1 contains values larger than or equal to the value of the first sample,
97907 ** but smaller than the value of the second. And so on.
 
 
 
 
 
 
 
97908 **
97909 ** If successful, this function determines which of the regions value
97910 ** pVal lies in, sets *piRegion to the region index (a value between 0
97911 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
97912 ** Or, if an OOM occurs while converting text values between encodings,
@@ -97915,22 +98253,34 @@
97915 #ifdef SQLITE_ENABLE_STAT2
97916 static int whereRangeRegion(
97917 Parse *pParse, /* Database connection */
97918 Index *pIdx, /* Index to consider domain of */
97919 sqlite3_value *pVal, /* Value to consider */
 
97920 int *piRegion /* OUT: Region of domain in which value lies */
97921 ){
 
97922 if( ALWAYS(pVal) ){
97923 IndexSample *aSample = pIdx->aSample;
97924 int i = 0;
97925 int eType = sqlite3_value_type(pVal);
97926
97927 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
97928 double r = sqlite3_value_double(pVal);
97929 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
97930 if( aSample[i].eType==SQLITE_NULL ) continue;
97931 if( aSample[i].eType>=SQLITE_TEXT || aSample[i].u.r>r ) break;
 
 
 
 
 
 
 
 
 
 
97932 }
97933 }else{
97934 sqlite3 *db = pParse->db;
97935 CollSeq *pColl;
97936 const u8 *z;
@@ -97957,11 +98307,11 @@
97957 assert( z && pColl && pColl->xCmp );
97958 }
97959 n = sqlite3ValueBytes(pVal, pColl->enc);
97960
97961 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
97962 int r;
97963 int eSampletype = aSample[i].eType;
97964 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
97965 if( (eSampletype!=eType) ) break;
97966 #ifndef SQLITE_OMIT_UTF16
97967 if( pColl->enc!=SQLITE_UTF8 ){
@@ -97971,18 +98321,18 @@
97971 );
97972 if( !zSample ){
97973 assert( db->mallocFailed );
97974 return SQLITE_NOMEM;
97975 }
97976 r = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
97977 sqlite3DbFree(db, zSample);
97978 }else
97979 #endif
97980 {
97981 r = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
97982 }
97983 if( r>0 ) break;
97984 }
97985 }
97986
97987 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
97988 *piRegion = i;
@@ -98061,13 +98411,13 @@
98061 ** constraints (if any). A return value of 100 indicates that it is expected
98062 ** that the range scan will visit every row (100%) selected by the equality
98063 ** constraints.
98064 **
98065 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
98066 ** reduces the search space by 2/3rds. Hence a single constraint (x>?)
98067 ** results in a return of 33 and a range constraint (x>? AND x<?) results
98068 ** in a return of 11.
98069 */
98070 static int whereRangeScanEst(
98071 Parse *pParse, /* Parsing & code generating context */
98072 Index *p, /* The index containing the range-compared column; "x" */
98073 int nEq, /* index into p->aCol[] of the range-compared column */
@@ -98083,73 +98433,209 @@
98083 sqlite3_value *pLowerVal = 0;
98084 sqlite3_value *pUpperVal = 0;
98085 int iEst;
98086 int iLower = 0;
98087 int iUpper = SQLITE_INDEX_SAMPLES;
 
 
98088 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98089
98090 if( pLower ){
98091 Expr *pExpr = pLower->pExpr->pRight;
98092 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
 
 
98093 }
98094 if( rc==SQLITE_OK && pUpper ){
98095 Expr *pExpr = pUpper->pExpr->pRight;
98096 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
 
 
98097 }
98098
98099 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
98100 sqlite3ValueFree(pLowerVal);
98101 sqlite3ValueFree(pUpperVal);
98102 goto range_est_fallback;
98103 }else if( pLowerVal==0 ){
98104 rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
98105 if( pLower ) iLower = iUpper/2;
98106 }else if( pUpperVal==0 ){
98107 rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
98108 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
98109 }else{
98110 rc = whereRangeRegion(pParse, p, pUpperVal, &iUpper);
98111 if( rc==SQLITE_OK ){
98112 rc = whereRangeRegion(pParse, p, pLowerVal, &iLower);
98113 }
98114 }
 
98115
98116 iEst = iUpper - iLower;
98117 testcase( iEst==SQLITE_INDEX_SAMPLES );
98118 assert( iEst<=SQLITE_INDEX_SAMPLES );
98119 if( iEst<1 ){
98120 iEst = 1;
 
 
98121 }
98122
98123 sqlite3ValueFree(pLowerVal);
98124 sqlite3ValueFree(pUpperVal);
98125 *piEst = (iEst * 100)/SQLITE_INDEX_SAMPLES;
98126 return rc;
98127 }
98128 range_est_fallback:
98129 #else
98130 UNUSED_PARAMETER(pParse);
98131 UNUSED_PARAMETER(p);
98132 UNUSED_PARAMETER(nEq);
98133 #endif
98134 assert( pLower || pUpper );
98135 if( pLower && pUpper ){
98136 *piEst = 11;
98137 }else{
98138 *piEst = 33;
98139 }
98140 return rc;
98141 }
98142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98143
98144 /*
98145 ** Find the query plan for accessing a particular table. Write the
98146 ** best query plan and its cost into the WhereCost object supplied as the
98147 ** last parameter.
98148 **
98149 ** The lowest cost plan wins. The cost is an estimate of the amount of
98150 ** CPU and disk I/O need to process the request using the selected plan.
98151 ** Factors that influence cost include:
98152 **
98153 ** * The estimated number of rows that will be retrieved. (The
98154 ** fewer the better.)
98155 **
@@ -98164,11 +98650,11 @@
98164 ** SQLITE_BIG_DBL. If a plan is found that uses the named index,
98165 ** then the cost is calculated in the usual way.
98166 **
98167 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
98168 ** in the SELECT statement, then no indexes are considered. However, the
98169 ** selected plan may still take advantage of the tables built-in rowid
98170 ** index.
98171 */
98172 static void bestBtreeIndex(
98173 Parse *pParse, /* The parsing context */
98174 WhereClause *pWC, /* The WHERE clause */
@@ -98207,13 +98693,15 @@
98207 /* An INDEXED BY clause specifies a particular index to use */
98208 pIdx = pProbe = pSrc->pIndex;
98209 wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
98210 eqTermMask = idxEqTermMask;
98211 }else{
98212 /* There is no INDEXED BY clause. Create a fake Index object to
98213 ** represent the primary key */
98214 Index *pFirst; /* Any other index on the table */
 
 
98215 memset(&sPk, 0, sizeof(Index));
98216 sPk.nColumn = 1;
98217 sPk.aiColumn = &aiColumnPk;
98218 sPk.aiRowEst = aiRowEstPk;
98219 sPk.onError = OE_Replace;
@@ -98220,10 +98708,12 @@
98220 sPk.pTable = pSrc->pTab;
98221 aiRowEstPk[0] = pSrc->pTab->nRowEst;
98222 aiRowEstPk[1] = 1;
98223 pFirst = pSrc->pTab->pIndex;
98224 if( pSrc->notIndexed==0 ){
 
 
98225 sPk.pNext = pFirst;
98226 }
98227 pProbe = &sPk;
98228 wsFlagMask = ~(
98229 WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
@@ -98236,20 +98726,23 @@
98236 */
98237 for(; pProbe; pIdx=pProbe=pProbe->pNext){
98238 const unsigned int * const aiRowEst = pProbe->aiRowEst;
98239 double cost; /* Cost of using pProbe */
98240 double nRow; /* Estimated number of rows in result set */
 
98241 int rev; /* True to scan in reverse order */
98242 int wsFlags = 0;
98243 Bitmask used = 0;
98244
98245 /* The following variables are populated based on the properties of
98246 ** scan being evaluated. They are then used to determine the expected
98247 ** cost and number of rows returned.
98248 **
98249 ** nEq:
98250 ** Number of equality terms that can be implemented using the index.
 
 
98251 **
98252 ** nInMul:
98253 ** The "in-multiplier". This is an estimate of how many seek operations
98254 ** SQLite must perform on the index in question. For example, if the
98255 ** WHERE clause is:
@@ -98269,46 +98762,54 @@
98269 ** the sub-select is assumed to return 25 rows for the purposes of
98270 ** determining nInMul.
98271 **
98272 ** bInEst:
98273 ** Set to true if there was at least one "x IN (SELECT ...)" term used
98274 ** in determining the value of nInMul.
 
 
98275 **
98276 ** estBound:
98277 ** An estimate on the amount of the table that must be searched. A
98278 ** value of 100 means the entire table is searched. Range constraints
98279 ** might reduce this to a value less than 100 to indicate that only
98280 ** a fraction of the table needs searching. In the absence of
98281 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
98282 ** space to 1/3rd its original size. So an x>? constraint reduces
98283 ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11.
98284 **
98285 ** bSort:
98286 ** Boolean. True if there is an ORDER BY clause that will require an
98287 ** external sort (i.e. scanning the index being evaluated will not
98288 ** correctly order records).
98289 **
98290 ** bLookup:
98291 ** Boolean. True if for each index entry visited a lookup on the
98292 ** corresponding table b-tree is required. This is always false
98293 ** for the rowid index. For other indexes, it is true unless all the
98294 ** columns of the table used by the SELECT statement are present in
98295 ** the index (such an index is sometimes described as a covering index).
 
98296 ** For example, given the index on (a, b), the second of the following
98297 ** two queries requires table b-tree lookups, but the first does not.
 
 
98298 **
98299 ** SELECT a, b FROM tbl WHERE a = 1;
98300 ** SELECT a, b, c FROM tbl WHERE a = 1;
98301 */
98302 int nEq;
98303 int bInEst = 0;
98304 int nInMul = 1;
98305 int estBound = 100;
98306 int nBound = 0; /* Number of range constraints seen */
98307 int bSort = 0;
98308 int bLookup = 0;
98309 WhereTerm *pTerm; /* A single term of the WHERE clause */
 
 
 
98310
98311 /* Determine the values of nEq and nInMul */
98312 for(nEq=0; nEq<pProbe->nColumn; nEq++){
98313 int j = pProbe->aiColumn[nEq];
98314 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
@@ -98316,18 +98817,23 @@
98316 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
98317 if( pTerm->eOperator & WO_IN ){
98318 Expr *pExpr = pTerm->pExpr;
98319 wsFlags |= WHERE_COLUMN_IN;
98320 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
 
98321 nInMul *= 25;
98322 bInEst = 1;
98323 }else if( ALWAYS(pExpr->x.pList) ){
98324 nInMul *= pExpr->x.pList->nExpr + 1;
 
98325 }
98326 }else if( pTerm->eOperator & WO_ISNULL ){
98327 wsFlags |= WHERE_COLUMN_NULL;
98328 }
 
 
 
98329 used |= pTerm->prereqRight;
98330 }
98331
98332 /* Determine the value of estBound. */
98333 if( nEq<pProbe->nColumn ){
@@ -98359,12 +98865,13 @@
98359 /* If there is an ORDER BY clause and the index being considered will
98360 ** naturally scan rows in the required order, set the appropriate flags
98361 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
98362 ** will scan rows in a different order, set the bSort variable. */
98363 if( pOrderBy ){
98364 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0
98365 && isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev)
 
98366 ){
98367 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
98368 wsFlags |= (rev ? WHERE_REVERSE : 0);
98369 }else{
98370 bSort = 1;
@@ -98391,44 +98898,101 @@
98391 bLookup = 1;
98392 }
98393 }
98394
98395 /*
98396 ** Estimate the number of rows of output. For an IN operator,
98397 ** do not let the estimate exceed half the rows in the table.
98398 */
98399 nRow = (double)(aiRowEst[nEq] * nInMul);
98400 if( bInEst && nRow*2>aiRowEst[0] ){
98401 nRow = aiRowEst[0]/2;
98402 nInMul = (int)(nRow / aiRowEst[nEq]);
98403 }
98404
98405 /* Assume constant cost to access a row and logarithmic cost to
98406 ** do a binary search. Hence, the initial cost is the number of output
98407 ** rows plus log2(table-size) times the number of binary searches.
 
 
98408 */
98409 cost = nRow + nInMul*estLog(aiRowEst[0]);
 
 
 
 
 
 
 
98410
98411 /* Adjust the number of rows and the cost downward to reflect rows
98412 ** that are excluded by range constraints.
98413 */
98414 nRow = (nRow * (double)estBound) / (double)100;
98415 cost = (cost * (double)estBound) / (double)100;
98416
98417 /* Add in the estimated cost of sorting the result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98418 */
98419 if( bSort ){
98420 cost += cost*estLog(cost);
98421 }
98422
98423 /* If all information can be taken directly from the index, we avoid
98424 ** doing table lookups. This reduces the cost by half. (Not really -
98425 ** this needs to be fixed.)
98426 */
98427 if( pIdx && bLookup==0 ){
98428 cost /= (double)2;
98429 }
98430 /**** Cost of using this index has now been computed ****/
98431
98432 /* If there are additional constraints on this table that cannot
98433 ** be used with the current index, but which might lower the number
98434 ** of output rows, adjust the nRow value accordingly. This only
@@ -98465,19 +99029,23 @@
98465 ** set size by a factor of 10 */
98466 nRow /= 10;
98467 }
98468 }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
98469 if( nSkipRange ){
98470 /* Ignore the first nBound range constraints since the index
98471 ** has already accounted for these */
98472 nSkipRange--;
98473 }else{
98474 /* Assume each additional range constraint reduces the result
98475 ** set size by a factor of 3 */
 
 
 
 
98476 nRow /= 3;
98477 }
98478 }else{
98479 /* Any other expression lowers the output row count by half */
98480 nRow /= 2;
98481 }
98482 }
98483 if( nRow<2 ) nRow = 2;
@@ -98484,14 +99052,14 @@
98484 }
98485
98486
98487 WHERETRACE((
98488 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
98489 " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
98490 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
98491 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
98492 notReady, nRow, cost, used
98493 ));
98494
98495 /* If this index is the best we have seen so far, then record this
98496 ** index and its cost in the pCost structure.
98497 */
@@ -99311,11 +99879,13 @@
99311 /* Seek the index cursor to the start of the range. */
99312 nConstraint = nEq;
99313 if( pRangeStart ){
99314 Expr *pRight = pRangeStart->pExpr->pRight;
99315 sqlite3ExprCode(pParse, pRight, regBase+nEq);
99316 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
 
 
99317 if( zStartAff ){
99318 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
99319 /* Since the comparison is to be performed with no conversions
99320 ** applied to the operands, set the affinity to apply to pRight to
99321 ** SQLITE_AFF_NONE. */
@@ -99350,11 +99920,13 @@
99350 nConstraint = nEq;
99351 if( pRangeEnd ){
99352 Expr *pRight = pRangeEnd->pExpr->pRight;
99353 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
99354 sqlite3ExprCode(pParse, pRight, regBase+nEq);
99355 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
 
 
99356 if( zEndAff ){
99357 if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
99358 /* Since the comparison is to be performed with no conversions
99359 ** applied to the operands, set the affinity to apply to pRight to
99360 ** SQLITE_AFF_NONE. */
@@ -105802,11 +106374,11 @@
105802 /* SQLITE_NOMEM */ "out of memory",
105803 /* SQLITE_READONLY */ "attempt to write a readonly database",
105804 /* SQLITE_INTERRUPT */ "interrupted",
105805 /* SQLITE_IOERR */ "disk I/O error",
105806 /* SQLITE_CORRUPT */ "database disk image is malformed",
105807 /* SQLITE_NOTFOUND */ 0,
105808 /* SQLITE_FULL */ "database or disk is full",
105809 /* SQLITE_CANTOPEN */ "unable to open database file",
105810 /* SQLITE_PROTOCOL */ "locking protocol",
105811 /* SQLITE_EMPTY */ "table contains no data",
105812 /* SQLITE_SCHEMA */ "database schema has changed",
@@ -106326,40 +106898,64 @@
106326 #else
106327 return 0;
106328 #endif
106329 }
106330
106331
106332 /*
106333 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
106334 ** to contains a zero-length string, all attached databases are
106335 ** checkpointed.
106336 */
106337 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
 
 
 
 
 
 
106338 #ifdef SQLITE_OMIT_WAL
106339 return SQLITE_OK;
106340 #else
106341 int rc; /* Return code */
106342 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
 
 
 
 
 
 
 
 
 
 
 
106343
106344 sqlite3_mutex_enter(db->mutex);
106345 if( zDb && zDb[0] ){
106346 iDb = sqlite3FindDbName(db, zDb);
106347 }
106348 if( iDb<0 ){
106349 rc = SQLITE_ERROR;
106350 sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
106351 }else{
106352 rc = sqlite3Checkpoint(db, iDb);
106353 sqlite3Error(db, rc, 0);
106354 }
106355 rc = sqlite3ApiExit(db, rc);
106356 sqlite3_mutex_leave(db->mutex);
106357 return rc;
106358 #endif
106359 }
106360
 
 
 
 
 
 
 
 
 
 
106361 #ifndef SQLITE_OMIT_WAL
106362 /*
106363 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
106364 ** not currently open in WAL mode.
106365 **
@@ -106373,24 +106969,35 @@
106373 ** this function while the checkpoint is running.
106374 **
106375 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
106376 ** checkpointed. If an error is encountered it is returned immediately -
106377 ** no attempt is made to checkpoint any remaining databases.
 
 
106378 */
106379 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb){
106380 int rc = SQLITE_OK; /* Return code */
106381 int i; /* Used to iterate through attached dbs */
 
106382
106383 assert( sqlite3_mutex_held(db->mutex) );
 
 
106384
106385 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
106386 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
106387 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt);
 
 
 
 
 
 
106388 }
106389 }
106390
106391 return rc;
106392 }
106393 #endif /* SQLITE_OMIT_WAL */
106394
106395 /*
106396 ** This function returns true if main-memory should be used instead of
@@ -107350,10 +107957,12 @@
107350 if( op==SQLITE_FCNTL_FILE_POINTER ){
107351 *(sqlite3_file**)pArg = fd;
107352 rc = SQLITE_OK;
107353 }else if( fd->pMethods ){
107354 rc = sqlite3OsFileControl(fd, op, pArg);
 
 
107355 }
107356 sqlite3BtreeLeave(pBtree);
107357 }
107358 }
107359 sqlite3_mutex_leave(db->mutex);
@@ -108597,11 +109206,11 @@
108597 typedef struct Fts3PhraseToken Fts3PhraseToken;
108598
108599 typedef struct Fts3SegFilter Fts3SegFilter;
108600 typedef struct Fts3DeferredToken Fts3DeferredToken;
108601 typedef struct Fts3SegReader Fts3SegReader;
108602 typedef struct Fts3SegReaderArray Fts3SegReaderArray;
108603
108604 /*
108605 ** A connection to a fulltext index is an instance of the following
108606 ** structure. The xCreate and xConnect methods create an instance
108607 ** of this structure and xDestroy and xDisconnect free that instance.
@@ -108620,10 +109229,13 @@
108620 /* Precompiled statements used by the implementation. Each of these
108621 ** statements is run and reset within a single virtual table API call.
108622 */
108623 sqlite3_stmt *aStmt[24];
108624
 
 
 
108625 int nNodeSize; /* Soft limit for node size */
108626 u8 bHasStat; /* True if %_stat table exists */
108627 u8 bHasDocsize; /* True if %_docsize table exists */
108628 int nPgsz; /* Page size for host database */
108629 char *zSegmentsTbl; /* Name of %_segments table */
@@ -108707,11 +109319,11 @@
108707 struct Fts3PhraseToken {
108708 char *z; /* Text of the token */
108709 int n; /* Number of bytes in buffer z */
108710 int isPrefix; /* True if token ends with a "*" character */
108711 int bFulltext; /* True if full-text index was used */
108712 Fts3SegReaderArray *pArray; /* Segment-reader for this token */
108713 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
108714 };
108715
108716 struct Fts3Phrase {
108717 /* Variables populated by fts3_expr.c when parsing a MATCH expression */
@@ -108775,16 +109387,12 @@
108775 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
108776 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
108777 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
108778 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**);
108779 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
108780 SQLITE_PRIVATE int sqlite3Fts3SegReaderIterate(
108781 Fts3Table *, Fts3SegReader **, int, Fts3SegFilter *,
108782 int (*)(Fts3Table *, void *, char *, int, char *, int), void *
108783 );
108784 SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *);
108785 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, sqlite3_stmt **);
108786 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
108787 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*);
108788
108789 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
108790 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
@@ -108792,26 +109400,54 @@
108792 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
108793 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
108794 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
108795 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
108796 SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *);
108797
108798 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
108799
 
 
 
 
 
 
 
 
 
108800 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
108801 #define FTS3_SEGMENT_REQUIRE_POS 0x00000001
108802 #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
108803 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
108804 #define FTS3_SEGMENT_PREFIX 0x00000008
 
108805
108806 /* Type passed as 4th argument to SegmentReaderIterate() */
108807 struct Fts3SegFilter {
108808 const char *zTerm;
108809 int nTerm;
108810 int iCol;
108811 int flags;
108812 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108813
108814 /* fts3.c */
108815 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
108816 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
108817 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
@@ -108845,10 +109481,13 @@
108845 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
108846 #ifdef SQLITE_TEST
108847 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
108848 #endif
108849
 
 
 
108850 #endif /* _FTSINT_H */
108851
108852 /************** End of fts3Int.h *********************************************/
108853 /************** Continuing where we left off in fts3.c ***********************/
108854
@@ -108990,10 +109629,12 @@
108990 /* Free any prepared statements held */
108991 for(i=0; i<SizeofArray(p->aStmt); i++){
108992 sqlite3_finalize(p->aStmt[i]);
108993 }
108994 sqlite3_free(p->zSegmentsTbl);
 
 
108995
108996 /* Invoke the tokenizer destructor to free the tokenizer. */
108997 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
108998
108999 sqlite3_free(p);
@@ -109206,10 +109847,145 @@
109206 sqlite3Fts3Dequote(zValue);
109207 }
109208 *pzValue = zValue;
109209 return 1;
109210 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109211
109212 /*
109213 ** This function is the implementation of both the xConnect and xCreate
109214 ** methods of the FTS3 virtual table.
109215 **
@@ -109243,10 +110019,13 @@
109243 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
109244 int bNoDocsize = 0; /* True to omit %_docsize table */
109245 const char **aCol; /* Array of column names */
109246 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
109247
 
 
 
109248 assert( strlen(argv[0])==4 );
109249 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
109250 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
109251 );
109252
@@ -109293,10 +110072,16 @@
109293 bNoDocsize = 1;
109294 }else{
109295 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
109296 rc = SQLITE_ERROR;
109297 }
 
 
 
 
 
 
109298 }else{
109299 *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
109300 rc = SQLITE_ERROR;
109301 }
109302 sqlite3_free(zVal);
@@ -109366,10 +110151,19 @@
109366 sqlite3Fts3Dequote(zCsr);
109367 p->azColumn[iCol] = zCsr;
109368 zCsr += n+1;
109369 assert( zCsr <= &((char *)p)[nByte] );
109370 }
 
 
 
 
 
 
 
 
 
109371
109372 /* If this is an xCreate call, create the underlying tables in the
109373 ** database. TODO: For xConnect(), it could verify that said tables exist.
109374 */
109375 if( isCreate ){
@@ -109384,11 +110178,12 @@
109384
109385 /* Declare the table schema to SQLite. */
109386 fts3DeclareVtab(&rc, p);
109387
109388 fts3_init_out:
109389
 
109390 sqlite3_free((void *)aCol);
109391 if( rc!=SQLITE_OK ){
109392 if( p ){
109393 fts3DisconnectMethod((sqlite3_vtab *)p);
109394 }else if( pTokenizer ){
@@ -110477,134 +111272,137 @@
110477 }
110478
110479 return SQLITE_OK;
110480 }
110481
110482 /*
110483 ** An Fts3SegReaderArray is used to store an array of Fts3SegReader objects.
110484 ** Elements are added to the array using fts3SegReaderArrayAdd().
110485 */
110486 struct Fts3SegReaderArray {
110487 int nSegment; /* Number of valid entries in apSegment[] */
110488 int nAlloc; /* Allocated size of apSegment[] */
110489 int nCost; /* The cost of executing SegReaderIterate() */
110490 Fts3SegReader *apSegment[1]; /* Array of seg-reader objects */
110491 };
110492
110493
110494 /*
110495 ** Free an Fts3SegReaderArray object. Also free all seg-readers in the
110496 ** array (using sqlite3Fts3SegReaderFree()).
110497 */
110498 static void fts3SegReaderArrayFree(Fts3SegReaderArray *pArray){
110499 if( pArray ){
110500 int i;
110501 for(i=0; i<pArray->nSegment; i++){
110502 sqlite3Fts3SegReaderFree(pArray->apSegment[i]);
110503 }
110504 sqlite3_free(pArray);
110505 }
110506 }
110507
110508 static int fts3SegReaderArrayAdd(
110509 Fts3SegReaderArray **ppArray,
110510 Fts3SegReader *pNew
110511 ){
110512 Fts3SegReaderArray *pArray = *ppArray;
110513
110514 if( !pArray || pArray->nAlloc==pArray->nSegment ){
110515 int nNew = (pArray ? pArray->nAlloc+16 : 16);
110516 pArray = (Fts3SegReaderArray *)sqlite3_realloc(pArray,
110517 sizeof(Fts3SegReaderArray) + (nNew-1) * sizeof(Fts3SegReader*)
110518 );
110519 if( !pArray ){
110520 sqlite3Fts3SegReaderFree(pNew);
110521 return SQLITE_NOMEM;
110522 }
110523 if( nNew==16 ){
110524 pArray->nSegment = 0;
110525 pArray->nCost = 0;
110526 }
110527 pArray->nAlloc = nNew;
110528 *ppArray = pArray;
110529 }
110530
110531 pArray->apSegment[pArray->nSegment++] = pNew;
110532 return SQLITE_OK;
110533 }
110534
110535 static int fts3TermSegReaderArray(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110536 Fts3Cursor *pCsr, /* Virtual table cursor handle */
110537 const char *zTerm, /* Term to query for */
110538 int nTerm, /* Size of zTerm in bytes */
110539 int isPrefix, /* True for a prefix search */
110540 Fts3SegReaderArray **ppArray /* OUT: Allocated seg-reader array */
110541 ){
110542 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
110543 int rc; /* Return code */
110544 Fts3SegReaderArray *pArray = 0; /* Array object to build */
110545 Fts3SegReader *pReader = 0; /* Seg-reader to add to pArray */
110546 sqlite3_stmt *pStmt = 0; /* SQL statement to scan %_segdir table */
110547 int iAge = 0; /* Used to assign ages to segments */
110548
110549 /* Allocate a seg-reader to scan the pending terms, if any. */
110550 rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pReader);
110551 if( rc==SQLITE_OK && pReader ) {
110552 rc = fts3SegReaderArrayAdd(&pArray, pReader);
110553 }
110554
110555 /* Loop through the entire %_segdir table. For each segment, create a
110556 ** Fts3SegReader to iterate through the subset of the segment leaves
110557 ** that may contain a term that matches zTerm/nTerm. For non-prefix
110558 ** searches, this is always a single leaf. For prefix searches, this
110559 ** may be a contiguous block of leaves.
110560 */
110561 if( rc==SQLITE_OK ){
110562 rc = sqlite3Fts3AllSegdirs(p, &pStmt);
110563 }
110564 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
110565 Fts3SegReader *pNew = 0;
110566 int nRoot = sqlite3_column_bytes(pStmt, 4);
110567 char const *zRoot = sqlite3_column_blob(pStmt, 4);
110568 if( sqlite3_column_int64(pStmt, 1)==0 ){
110569 /* The entire segment is stored on the root node (which must be a
110570 ** leaf). Do not bother inspecting any data in this case, just
110571 ** create a Fts3SegReader to scan the single leaf.
110572 */
110573 rc = sqlite3Fts3SegReaderNew(iAge, 0, 0, 0, zRoot, nRoot, &pNew);
110574 }else{
110575 sqlite3_int64 i1; /* First leaf that may contain zTerm */
110576 sqlite3_int64 i2; /* Final leaf that may contain zTerm */
110577 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &i1, (isPrefix?&i2:0));
110578 if( isPrefix==0 ) i2 = i1;
110579 if( rc==SQLITE_OK ){
110580 rc = sqlite3Fts3SegReaderNew(iAge, i1, i2, 0, 0, 0, &pNew);
110581 }
110582 }
110583 assert( (pNew==0)==(rc!=SQLITE_OK) );
110584
110585 /* If a new Fts3SegReader was allocated, add it to the array. */
110586 if( rc==SQLITE_OK ){
110587 rc = fts3SegReaderArrayAdd(&pArray, pNew);
110588 }
110589 if( rc==SQLITE_OK ){
110590 rc = sqlite3Fts3SegReaderCost(pCsr, pNew, &pArray->nCost);
110591 }
110592 iAge++;
110593 }
110594
110595 if( rc==SQLITE_DONE ){
110596 rc = sqlite3_reset(pStmt);
110597 }else{
110598 sqlite3_reset(pStmt);
110599 }
110600 if( rc!=SQLITE_OK ){
110601 fts3SegReaderArrayFree(pArray);
110602 pArray = 0;
110603 }
110604 *ppArray = pArray;
110605 return rc;
110606 }
110607
110608 /*
110609 ** This function retreives the doclist for the specified term (or term
110610 ** prefix) from the database.
@@ -110623,15 +111421,15 @@
110623 int isReqPos, /* True to include position lists in output */
110624 int *pnOut, /* OUT: Size of buffer at *ppOut */
110625 char **ppOut /* OUT: Malloced result buffer */
110626 ){
110627 int rc; /* Return code */
110628 Fts3SegReaderArray *pArray; /* Seg-reader array for this term */
110629 TermSelect tsc; /* Context object for fts3TermSelectCb() */
110630 Fts3SegFilter filter; /* Segment term filter configuration */
110631
110632 pArray = pTok->pArray;
110633 memset(&tsc, 0, sizeof(TermSelect));
110634 tsc.isReqPos = isReqPos;
110635
110636 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY
110637 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
@@ -110639,17 +111437,22 @@
110639 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
110640 filter.iCol = iColumn;
110641 filter.zTerm = pTok->z;
110642 filter.nTerm = pTok->n;
110643
110644 rc = sqlite3Fts3SegReaderIterate(p, pArray->apSegment, pArray->nSegment,
110645 &filter, fts3TermSelectCb, (void *)&tsc
110646 );
 
 
 
 
 
 
110647 if( rc==SQLITE_OK ){
110648 rc = fts3TermSelectMerge(&tsc);
110649 }
110650
110651 if( rc==SQLITE_OK ){
110652 *ppOut = tsc.aaOutput[0];
110653 *pnOut = tsc.anOutput[0];
110654 }else{
110655 int i;
@@ -110656,12 +111459,12 @@
110656 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
110657 sqlite3_free(tsc.aaOutput[i]);
110658 }
110659 }
110660
110661 fts3SegReaderArrayFree(pArray);
110662 pTok->pArray = 0;
110663 return rc;
110664 }
110665
110666 /*
110667 ** This function counts the total number of docids in the doclist stored
@@ -110780,17 +111583,17 @@
110780 ** evaluation, only create segment-readers if there are no Fts3DeferredToken
110781 ** objects attached to the phrase-tokens.
110782 */
110783 for(ii=0; ii<pPhrase->nToken; ii++){
110784 Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
110785 if( pTok->pArray==0 ){
110786 if( (pCsr->eEvalmode==FTS3_EVAL_FILTER)
110787 || (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0)
110788 || (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext)
110789 ){
110790 rc = fts3TermSegReaderArray(
110791 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pArray
110792 );
110793 if( rc!=SQLITE_OK ) return rc;
110794 }
110795 }
110796 }
@@ -110817,14 +111620,14 @@
110817 int nMinCost = 0x7FFFFFFF;
110818 int jj;
110819
110820 /* Find the remaining token with the lowest cost. */
110821 for(jj=0; jj<pPhrase->nToken; jj++){
110822 Fts3SegReaderArray *pArray = pPhrase->aToken[jj].pArray;
110823 if( pArray && pArray->nCost<nMinCost ){
110824 iTok = jj;
110825 nMinCost = pArray->nCost;
110826 }
110827 }
110828 pTok = &pPhrase->aToken[iTok];
110829
110830 /* This branch is taken if it is determined that loading the doclist
@@ -110839,16 +111642,16 @@
110839 }
110840
110841 if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){
110842 rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList);
110843 }else{
110844 if( pTok->pArray ){
110845 rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList);
110846 }
110847 pTok->bFulltext = 1;
110848 }
110849 assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pArray==0 );
110850 if( rc!=SQLITE_OK ) break;
110851
110852 if( isFirst ){
110853 pOut = pList;
110854 nOut = nList;
@@ -111022,13 +111825,13 @@
111022 Fts3Phrase *pPhrase = pExpr->pPhrase;
111023 int ii;
111024
111025 for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
111026 Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
111027 if( pTok->pArray==0 ){
111028 rc = fts3TermSegReaderArray(
111029 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pArray
111030 );
111031 }
111032 }
111033 }else{
111034 rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr);
@@ -111048,12 +111851,12 @@
111048 if( pExpr ){
111049 Fts3Phrase *pPhrase = pExpr->pPhrase;
111050 if( pPhrase ){
111051 int kk;
111052 for(kk=0; kk<pPhrase->nToken; kk++){
111053 fts3SegReaderArrayFree(pPhrase->aToken[kk].pArray);
111054 pPhrase->aToken[kk].pArray = 0;
111055 }
111056 }
111057 fts3ExprFreeSegReaders(pExpr->pLeft);
111058 fts3ExprFreeSegReaders(pExpr->pRight);
111059 }
@@ -111069,14 +111872,12 @@
111069 if( pExpr->eType==FTSQUERY_PHRASE ){
111070 Fts3Phrase *pPhrase = pExpr->pPhrase;
111071 int ii;
111072 nCost = 0;
111073 for(ii=0; ii<pPhrase->nToken; ii++){
111074 Fts3SegReaderArray *pArray = pPhrase->aToken[ii].pArray;
111075 if( pArray ){
111076 nCost += pPhrase->aToken[ii].pArray->nCost;
111077 }
111078 }
111079 }else{
111080 nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight);
111081 }
111082 return nCost;
@@ -111414,12 +112215,12 @@
111414 const char *idxStr, /* Unused */
111415 int nVal, /* Number of elements in apVal */
111416 sqlite3_value **apVal /* Arguments for the indexing scheme */
111417 ){
111418 const char *azSql[] = {
111419 "SELECT * FROM %Q.'%q_content' WHERE docid = ?", /* non-full-table-scan */
111420 "SELECT * FROM %Q.'%q_content'", /* full-table-scan */
111421 };
111422 int rc; /* Return code */
111423 char *zSql; /* SQL statement used to access %_content */
111424 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
111425 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
@@ -111470,11 +112271,12 @@
111470 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
111471 ** statement loops through all rows of the %_content table. For a
111472 ** full-text query or docid lookup, the statement retrieves a single
111473 ** row by docid.
111474 */
111475 zSql = sqlite3_mprintf(azSql[idxNum==FTS3_FULLSCAN_SEARCH], p->zDb, p->zName);
 
111476 if( !zSql ){
111477 rc = SQLITE_NOMEM;
111478 }else{
111479 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
111480 sqlite3_free(zSql);
@@ -111988,10 +112790,13 @@
111988 #ifdef SQLITE_ENABLE_ICU
111989 const sqlite3_tokenizer_module *pIcu = 0;
111990 sqlite3Fts3IcuTokenizerModule(&pIcu);
111991 #endif
111992
 
 
 
111993 sqlite3Fts3SimpleTokenizerModule(&pSimple);
111994 sqlite3Fts3PorterTokenizerModule(&pPorter);
111995
111996 /* Allocate and initialise the hash-table used to store tokenizers. */
111997 pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -112063,10 +112868,472 @@
112063 #endif
112064
112065 #endif
112066
112067 /************** End of fts3.c ************************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112068 /************** Begin file fts3_expr.c ***************************************/
112069 /*
112070 ** 2008 Nov 28
112071 **
112072 ** The author disclaims copyright to this source code. In place of
@@ -114956,11 +116223,11 @@
114956 /* 2 */ "DELETE FROM %Q.'%q_content'",
114957 /* 3 */ "DELETE FROM %Q.'%q_segments'",
114958 /* 4 */ "DELETE FROM %Q.'%q_segdir'",
114959 /* 5 */ "DELETE FROM %Q.'%q_docsize'",
114960 /* 6 */ "DELETE FROM %Q.'%q_stat'",
114961 /* 7 */ "SELECT * FROM %Q.'%q_content' WHERE rowid=?",
114962 /* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
114963 /* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
114964 /* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
114965 /* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
114966
@@ -114973,11 +116240,11 @@
114973 /* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
114974 /* 15 */ "SELECT count(*), max(level) FROM %Q.'%q_segdir'",
114975
114976 /* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
114977 /* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
114978 /* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%z)",
114979 /* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
114980 /* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
114981 /* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
114982 /* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=0",
114983 /* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
@@ -114990,24 +116257,13 @@
114990
114991 pStmt = p->aStmt[eStmt];
114992 if( !pStmt ){
114993 char *zSql;
114994 if( eStmt==SQL_CONTENT_INSERT ){
114995 int i; /* Iterator variable */
114996 char *zVarlist; /* The "?, ?, ..." string */
114997 zVarlist = (char *)sqlite3_malloc(2*p->nColumn+2);
114998 if( !zVarlist ){
114999 *pp = 0;
115000 return SQLITE_NOMEM;
115001 }
115002 zVarlist[0] = '?';
115003 zVarlist[p->nColumn*2+1] = '\0';
115004 for(i=1; i<=p->nColumn; i++){
115005 zVarlist[i*2-1] = ',';
115006 zVarlist[i*2] = '?';
115007 }
115008 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, zVarlist);
115009 }else{
115010 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
115011 }
115012 if( !zSql ){
115013 rc = SQLITE_NOMEM;
@@ -115044,11 +116300,11 @@
115044 if( rc==SQLITE_OK ){
115045 if( eStmt==SQL_SELECT_DOCSIZE ){
115046 sqlite3_bind_int64(pStmt, 1, iDocid);
115047 }
115048 rc = sqlite3_step(pStmt);
115049 if( rc!=SQLITE_ROW ){
115050 rc = sqlite3_reset(pStmt);
115051 if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT;
115052 pStmt = 0;
115053 }else{
115054 rc = SQLITE_OK;
@@ -115145,12 +116401,21 @@
115145 ** 1: start_block
115146 ** 2: leaves_end_block
115147 ** 3: end_block
115148 ** 4: root
115149 */
115150 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table *p, sqlite3_stmt **ppStmt){
115151 return fts3SqlStmt(p, SQL_SELECT_ALL_LEVEL, ppStmt, 0);
 
 
 
 
 
 
 
 
 
115152 }
115153
115154
115155 /*
115156 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
@@ -115848,20 +117113,22 @@
115848 ** to right.
115849 */
115850 sqlite3_stmt *pStmt;
115851 sqlite3_int64 nDoc = 0;
115852 sqlite3_int64 nByte = 0;
 
115853 const char *a;
 
115854 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
115855 if( rc ) return rc;
115856 a = sqlite3_column_blob(pStmt, 0);
115857 if( a ){
115858 const char *pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
115859 a += sqlite3Fts3GetVarint(a, &nDoc);
115860 while( a<pEnd ){
115861 a += sqlite3Fts3GetVarint(a, &nByte);
115862 }
115863 }
115864 if( nDoc==0 || nByte==0 ){
115865 sqlite3_reset(pStmt);
115866 return SQLITE_CORRUPT;
115867 }
@@ -116047,46 +117314,10 @@
116047 }
116048 *ppReader = pReader;
116049 return rc;
116050 }
116051
116052
116053 /*
116054 ** The second argument to this function is expected to be a statement of
116055 ** the form:
116056 **
116057 ** SELECT
116058 ** idx, -- col 0
116059 ** start_block, -- col 1
116060 ** leaves_end_block, -- col 2
116061 ** end_block, -- col 3
116062 ** root -- col 4
116063 ** FROM %_segdir ...
116064 **
116065 ** This function allocates and initializes a Fts3SegReader structure to
116066 ** iterate through the terms stored in the segment identified by the
116067 ** current row that pStmt is pointing to.
116068 **
116069 ** If successful, the Fts3SegReader is left pointing to the first term
116070 ** in the segment and SQLITE_OK is returned. Otherwise, an SQLite error
116071 ** code is returned.
116072 */
116073 static int fts3SegReaderNew(
116074 sqlite3_stmt *pStmt, /* See above */
116075 int iAge, /* Segment "age". */
116076 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
116077 ){
116078 return sqlite3Fts3SegReaderNew(iAge,
116079 sqlite3_column_int64(pStmt, 1),
116080 sqlite3_column_int64(pStmt, 2),
116081 sqlite3_column_int64(pStmt, 3),
116082 sqlite3_column_blob(pStmt, 4),
116083 sqlite3_column_bytes(pStmt, 4),
116084 ppReader
116085 );
116086 }
116087
116088 /*
116089 ** Compare the entries pointed to by two Fts3SegReader structures.
116090 ** Comparison is as follows:
116091 **
116092 ** 1) EOF is greater than not EOF.
@@ -116687,29 +117918,10 @@
116687 rc = sqlite3_reset(pStmt);
116688 }
116689 return rc;
116690 }
116691
116692 /*
116693 ** Set *pnSegment to the number of segments of level iLevel in the database.
116694 **
116695 ** Return SQLITE_OK if successful, or an SQLite error code if not.
116696 */
116697 static int fts3SegmentCount(Fts3Table *p, int iLevel, int *pnSegment){
116698 sqlite3_stmt *pStmt;
116699 int rc;
116700
116701 assert( iLevel>=0 );
116702 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_COUNT, &pStmt, 0);
116703 if( rc!=SQLITE_OK ) return rc;
116704 sqlite3_bind_int(pStmt, 1, iLevel);
116705 if( SQLITE_ROW==sqlite3_step(pStmt) ){
116706 *pnSegment = sqlite3_column_int(pStmt, 0);
116707 }
116708 return sqlite3_reset(pStmt);
116709 }
116710
116711 /*
116712 ** Set *pnSegment to the total number of segments in the database. Set
116713 ** *pnMax to the largest segment level in the database (segment levels
116714 ** are stored in the 'level' column of the %_segdir table).
116715 **
@@ -116764,19 +117976,22 @@
116764 }
116765 if( rc!=SQLITE_OK ){
116766 return rc;
116767 }
116768
116769 if( iLevel>=0 ){
 
 
 
 
 
116770 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_BY_LEVEL, &pDelete, 0);
116771 if( rc==SQLITE_OK ){
116772 sqlite3_bind_int(pDelete, 1, iLevel);
116773 sqlite3_step(pDelete);
116774 rc = sqlite3_reset(pDelete);
116775 }
116776 }else{
116777 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
116778 }
116779
116780 return rc;
116781 }
116782
@@ -116821,148 +118036,117 @@
116821
116822 *ppList = pList;
116823 *pnList = nList;
116824 }
116825
116826 /*
116827 ** sqlite3Fts3SegReaderIterate() callback used when merging multiple
116828 ** segments to create a single, larger segment.
116829 */
116830 static int fts3MergeCallback(
116831 Fts3Table *p, /* FTS3 Virtual table handle */
116832 void *pContext, /* Pointer to SegmentWriter* to write with */
116833 char *zTerm, /* Term to write to the db */
116834 int nTerm, /* Number of bytes in zTerm */
116835 char *aDoclist, /* Doclist associated with zTerm */
116836 int nDoclist /* Number of bytes in doclist */
116837 ){
116838 SegmentWriter **ppW = (SegmentWriter **)pContext;
116839 return fts3SegWriterAdd(p, ppW, 1, zTerm, nTerm, aDoclist, nDoclist);
116840 }
116841
116842 /*
116843 ** sqlite3Fts3SegReaderIterate() callback used when flushing the contents
116844 ** of the pending-terms hash table to the database.
116845 */
116846 static int fts3FlushCallback(
116847 Fts3Table *p, /* FTS3 Virtual table handle */
116848 void *pContext, /* Pointer to SegmentWriter* to write with */
116849 char *zTerm, /* Term to write to the db */
116850 int nTerm, /* Number of bytes in zTerm */
116851 char *aDoclist, /* Doclist associated with zTerm */
116852 int nDoclist /* Number of bytes in doclist */
116853 ){
116854 SegmentWriter **ppW = (SegmentWriter **)pContext;
116855 return fts3SegWriterAdd(p, ppW, 0, zTerm, nTerm, aDoclist, nDoclist);
116856 }
116857
116858 /*
116859 ** This function is used to iterate through a contiguous set of terms
116860 ** stored in the full-text index. It merges data contained in one or
116861 ** more segments to support this.
116862 **
116863 ** The second argument is passed an array of pointers to SegReader objects
116864 ** allocated with sqlite3Fts3SegReaderNew(). This function merges the range
116865 ** of terms selected by each SegReader. If a single term is present in
116866 ** more than one segment, the associated doclists are merged. For each
116867 ** term and (possibly merged) doclist in the merged range, the callback
116868 ** function xFunc is invoked with its arguments set as follows.
116869 **
116870 ** arg 0: Copy of 'p' parameter passed to this function
116871 ** arg 1: Copy of 'pContext' parameter passed to this function
116872 ** arg 2: Pointer to buffer containing term
116873 ** arg 3: Size of arg 2 buffer in bytes
116874 ** arg 4: Pointer to buffer containing doclist
116875 ** arg 5: Size of arg 2 buffer in bytes
116876 **
116877 ** The 4th argument to this function is a pointer to a structure of type
116878 ** Fts3SegFilter, defined in fts3Int.h. The contents of this structure
116879 ** further restrict the range of terms that callbacks are made for and
116880 ** modify the behaviour of this function. See comments above structure
116881 ** definition for details.
116882 */
116883 SQLITE_PRIVATE int sqlite3Fts3SegReaderIterate(
116884 Fts3Table *p, /* Virtual table handle */
116885 Fts3SegReader **apSegment, /* Array of Fts3SegReader objects */
116886 int nSegment, /* Size of apSegment array */
116887 Fts3SegFilter *pFilter, /* Restrictions on range of iteration */
116888 int (*xFunc)(Fts3Table *, void *, char *, int, char *, int), /* Callback */
116889 void *pContext /* Callback context (2nd argument) */
116890 ){
116891 int i; /* Iterator variable */
116892 char *aBuffer = 0; /* Buffer to merge doclists in */
116893 int nAlloc = 0; /* Allocated size of aBuffer buffer */
116894 int rc = SQLITE_OK; /* Return code */
116895
116896 int isIgnoreEmpty = (pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
116897 int isRequirePos = (pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
116898 int isColFilter = (pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
116899 int isPrefix = (pFilter->flags & FTS3_SEGMENT_PREFIX);
116900
116901 /* If there are zero segments, this function is a no-op. This scenario
116902 ** comes about only when reading from an empty database.
116903 */
116904 if( nSegment==0 ) goto finished;
116905
116906 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
116907 ** for, then advance each segment iterator until it points to a term of
116908 ** equal or greater value than the specified term. This prevents many
116909 ** unnecessary merge/sort operations for the case where single segment
116910 ** b-tree leaf nodes contain more than one term.
116911 */
116912 for(i=0; i<nSegment; i++){
116913 int nTerm = pFilter->nTerm;
116914 const char *zTerm = pFilter->zTerm;
116915 Fts3SegReader *pSeg = apSegment[i];
116916 do {
116917 rc = fts3SegReaderNext(p, pSeg);
116918 if( rc!=SQLITE_OK ) goto finished;
116919 }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
116920 }
 
 
116921
116922 fts3SegReaderSort(apSegment, nSegment, nSegment, fts3SegReaderCmp);
116923 while( apSegment[0]->aNode ){
116924 int nTerm = apSegment[0]->nTerm;
116925 char *zTerm = apSegment[0]->zTerm;
116926 int nMerge = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116927
116928 /* If this is a prefix-search, and if the term that apSegment[0] points
116929 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
116930 ** required callbacks have been made. In this case exit early.
116931 **
116932 ** Similarly, if this is a search for an exact match, and the first term
116933 ** of segment apSegment[0] is not a match, exit early.
116934 */
116935 if( pFilter->zTerm ){
116936 if( nTerm<pFilter->nTerm
116937 || (!isPrefix && nTerm>pFilter->nTerm)
116938 || memcmp(zTerm, pFilter->zTerm, pFilter->nTerm)
116939 ){
116940 goto finished;
116941 }
116942 }
116943
 
116944 while( nMerge<nSegment
116945 && apSegment[nMerge]->aNode
116946 && apSegment[nMerge]->nTerm==nTerm
116947 && 0==memcmp(zTerm, apSegment[nMerge]->zTerm, nTerm)
116948 ){
116949 nMerge++;
116950 }
116951
116952 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
116953 if( nMerge==1 && !isIgnoreEmpty ){
116954 Fts3SegReader *p0 = apSegment[0];
116955 rc = xFunc(p, pContext, zTerm, nTerm, p0->aDoclist, p0->nDoclist);
116956 if( rc!=SQLITE_OK ) goto finished;
116957 }else{
116958 int nDoclist = 0; /* Size of doclist */
116959 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
116960
116961 /* The current term of the first nMerge entries in the array
116962 ** of Fts3SegReader objects is the same. The doclists must be merged
116963 ** and a single term added to the new segment.
116964 */
116965 for(i=0; i<nMerge; i++){
116966 fts3SegReaderFirstDocid(apSegment[i]);
116967 }
116968 fts3SegReaderSort(apSegment, nMerge, nMerge, fts3SegReaderDoclistCmp);
@@ -116986,57 +118170,60 @@
116986 fts3ColumnFilter(pFilter->iCol, &pList, &nList);
116987 }
116988
116989 if( !isIgnoreEmpty || nList>0 ){
116990 nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0);
116991 if( nDoclist+nByte>nAlloc ){
116992 char *aNew;
116993 nAlloc = (nDoclist+nByte)*2;
116994 aNew = sqlite3_realloc(aBuffer, nAlloc);
116995 if( !aNew ){
116996 rc = SQLITE_NOMEM;
116997 goto finished;
116998 }
116999 aBuffer = aNew;
117000 }
117001 nDoclist += sqlite3Fts3PutVarint(&aBuffer[nDoclist], iDocid-iPrev);
 
 
117002 iPrev = iDocid;
117003 if( isRequirePos ){
117004 memcpy(&aBuffer[nDoclist], pList, nList);
117005 nDoclist += nList;
117006 aBuffer[nDoclist++] = '\0';
117007 }
117008 }
117009
117010 fts3SegReaderSort(apSegment, nMerge, j, fts3SegReaderDoclistCmp);
117011 }
117012
117013 if( nDoclist>0 ){
117014 rc = xFunc(p, pContext, zTerm, nTerm, aBuffer, nDoclist);
117015 if( rc!=SQLITE_OK ) goto finished;
117016 }
117017 }
117018
117019 /* If there is a term specified to filter on, and this is not a prefix
117020 ** search, return now. The callback that corresponds to the required
117021 ** term (if such a term exists in the index) has already been made.
117022 */
117023 if( pFilter->zTerm && !isPrefix ){
117024 goto finished;
117025 }
117026
117027 for(i=0; i<nMerge; i++){
117028 rc = fts3SegReaderNext(p, apSegment[i]);
117029 if( rc!=SQLITE_OK ) goto finished;
117030 }
117031 fts3SegReaderSort(apSegment, nSegment, nMerge, fts3SegReaderCmp);
117032 }
117033
117034 finished:
117035 sqlite3_free(aBuffer);
117036 return rc;
117037 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117038
117039 /*
117040 ** Merge all level iLevel segments in the database into a single
117041 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
117042 ** single segment with a level equal to the numerically largest level
@@ -117046,161 +118233,73 @@
117046 ** segment in the database, SQLITE_DONE is returned immediately.
117047 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
117048 ** an SQLite error code is returned.
117049 */
117050 static int fts3SegmentMerge(Fts3Table *p, int iLevel){
117051 int i; /* Iterator variable */
117052 int rc; /* Return code */
117053 int iIdx; /* Index of new segment */
117054 int iNewLevel = 0; /* Level to create new segment at */
117055 sqlite3_stmt *pStmt = 0;
117056 SegmentWriter *pWriter = 0;
117057 int nSegment = 0; /* Number of segments being merged */
117058 Fts3SegReader **apSegment = 0; /* Array of Segment iterators */
117059 Fts3SegReader *pPending = 0; /* Iterator for pending-terms */
117060 Fts3SegFilter filter; /* Segment term filter condition */
 
117061
117062 if( iLevel<0 ){
 
 
 
117063 /* This call is to merge all segments in the database to a single
117064 ** segment. The level of the new segment is equal to the the numerically
117065 ** greatest segment level currently present in the database. The index
117066 ** of the new segment is always 0.
117067 */
117068 iIdx = 0;
117069 rc = sqlite3Fts3SegReaderPending(p, 0, 0, 1, &pPending);
117070 if( rc!=SQLITE_OK ) goto finished;
117071 rc = fts3SegmentCountMax(p, &nSegment, &iNewLevel);
117072 if( rc!=SQLITE_OK ) goto finished;
117073 nSegment += (pPending!=0);
117074 if( nSegment<=1 ){
117075 return SQLITE_DONE;
117076 }
117077 }else{
117078 /* This call is to merge all segments at level iLevel. Find the next
117079 ** available segment index at level iLevel+1. The call to
117080 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
117081 ** a single iLevel+2 segment if necessary.
117082 */
117083 iNewLevel = iLevel+1;
117084 rc = fts3AllocateSegdirIdx(p, iNewLevel, &iIdx);
117085 if( rc!=SQLITE_OK ) goto finished;
117086 rc = fts3SegmentCount(p, iLevel, &nSegment);
117087 if( rc!=SQLITE_OK ) goto finished;
117088 }
117089 assert( nSegment>0 );
117090 assert( iNewLevel>=0 );
117091
117092 /* Allocate space for an array of pointers to segment iterators. */
117093 apSegment = (Fts3SegReader**)sqlite3_malloc(sizeof(Fts3SegReader *)*nSegment);
117094 if( !apSegment ){
117095 rc = SQLITE_NOMEM;
117096 goto finished;
117097 }
117098 memset(apSegment, 0, sizeof(Fts3SegReader *)*nSegment);
117099
117100 /* Allocate a Fts3SegReader structure for each segment being merged. A
117101 ** Fts3SegReader stores the state data required to iterate through all
117102 ** entries on all leaves of a single segment.
117103 */
117104 assert( SQL_SELECT_LEVEL+1==SQL_SELECT_ALL_LEVEL);
117105 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL+(iLevel<0), &pStmt, 0);
117106 if( rc!=SQLITE_OK ) goto finished;
117107 sqlite3_bind_int(pStmt, 1, iLevel);
117108 for(i=0; SQLITE_ROW==(sqlite3_step(pStmt)); i++){
117109 rc = fts3SegReaderNew(pStmt, i, &apSegment[i]);
117110 if( rc!=SQLITE_OK ){
117111 goto finished;
117112 }
117113 }
117114 rc = sqlite3_reset(pStmt);
117115 if( pPending ){
117116 apSegment[i] = pPending;
117117 pPending = 0;
117118 }
117119 pStmt = 0;
117120 if( rc!=SQLITE_OK ) goto finished;
117121
117122 memset(&filter, 0, sizeof(Fts3SegFilter));
117123 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
117124 filter.flags |= (iLevel<0 ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
117125 rc = sqlite3Fts3SegReaderIterate(p, apSegment, nSegment,
117126 &filter, fts3MergeCallback, (void *)&pWriter
117127 );
 
 
 
 
 
 
 
 
 
117128 if( rc!=SQLITE_OK ) goto finished;
117129
117130 rc = fts3DeleteSegdir(p, iLevel, apSegment, nSegment);
117131 if( rc==SQLITE_OK ){
117132 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
117133 }
117134
117135 finished:
117136 fts3SegWriterFree(pWriter);
117137 if( apSegment ){
117138 for(i=0; i<nSegment; i++){
117139 sqlite3Fts3SegReaderFree(apSegment[i]);
117140 }
117141 sqlite3_free(apSegment);
117142 }
117143 sqlite3Fts3SegReaderFree(pPending);
117144 sqlite3_reset(pStmt);
117145 return rc;
117146 }
117147
117148
117149 /*
117150 ** Flush the contents of pendingTerms to a level 0 segment.
117151 */
117152 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
117153 int rc; /* Return Code */
117154 int idx; /* Index of new segment created */
117155 SegmentWriter *pWriter = 0; /* Used to write the segment */
117156 Fts3SegReader *pReader = 0; /* Used to iterate through the hash table */
117157
117158 /* Allocate a SegReader object to iterate through the contents of the
117159 ** pending-terms table. If an error occurs, or if there are no terms
117160 ** in the pending-terms table, return immediately.
117161 */
117162 rc = sqlite3Fts3SegReaderPending(p, 0, 0, 1, &pReader);
117163 if( rc!=SQLITE_OK || pReader==0 ){
117164 return rc;
117165 }
117166
117167 /* Determine the next index at level 0. If level 0 is already full, this
117168 ** call may merge all existing level 0 segments into a single level 1
117169 ** segment.
117170 */
117171 rc = fts3AllocateSegdirIdx(p, 0, &idx);
117172
117173 /* If no errors have occured, iterate through the contents of the
117174 ** pending-terms hash table using the Fts3SegReader iterator. The callback
117175 ** writes each term (along with its doclist) to the database via the
117176 ** SegmentWriter handle pWriter.
117177 */
117178 if( rc==SQLITE_OK ){
117179 void *c = (void *)&pWriter; /* SegReaderIterate() callback context */
117180 Fts3SegFilter f; /* SegReaderIterate() parameters */
117181
117182 memset(&f, 0, sizeof(Fts3SegFilter));
117183 f.flags = FTS3_SEGMENT_REQUIRE_POS;
117184 rc = sqlite3Fts3SegReaderIterate(p, &pReader, 1, &f, fts3FlushCallback, c);
117185 }
117186 assert( pWriter || rc!=SQLITE_OK );
117187
117188 /* If no errors have occured, flush the SegmentWriter object to the
117189 ** database. Then delete the SegmentWriter and Fts3SegReader objects
117190 ** allocated by this function.
117191 */
117192 if( rc==SQLITE_OK ){
117193 rc = fts3SegWriterFlush(p, pWriter, 0, idx);
117194 }
117195 fts3SegWriterFree(pWriter);
117196 sqlite3Fts3SegReaderFree(pReader);
117197
117198 if( rc==SQLITE_OK ){
117199 sqlite3Fts3PendingTermsClear(p);
117200 }
117201 return rc;
117202 }
117203
117204 /*
117205 ** Encode N integers as varints into a blob.
117206 */
@@ -117363,11 +118462,11 @@
117363 int nVal = sqlite3_value_bytes(pVal);
117364
117365 if( !zVal ){
117366 return SQLITE_NOMEM;
117367 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
117368 rc = fts3SegmentMerge(p, -1);
117369 if( rc==SQLITE_DONE ){
117370 rc = SQLITE_OK;
117371 }else{
117372 sqlite3Fts3PendingTermsClear(p);
117373 }
@@ -117621,11 +118720,11 @@
117621 */
117622 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
117623 int rc;
117624 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
117625 if( rc==SQLITE_OK ){
117626 rc = fts3SegmentMerge(p, -1);
117627 if( rc==SQLITE_OK ){
117628 rc = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
117629 if( rc==SQLITE_OK ){
117630 sqlite3Fts3PendingTermsClear(p);
117631 }
@@ -118599,10 +119698,11 @@
118599 pStmt = *ppStmt;
118600 assert( sqlite3_data_count(pStmt)==1 );
118601
118602 a = sqlite3_column_blob(pStmt, 0);
118603 a += sqlite3Fts3GetVarint(a, &nDoc);
 
118604 *pnDoc = (u32)nDoc;
118605
118606 if( paLen ) *paLen = a;
118607 return SQLITE_OK;
118608 }
@@ -118805,13 +119905,15 @@
118805
118806 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
118807 if( rc==SQLITE_OK ){
118808 int iCol;
118809 for(iCol=0; iCol<pInfo->nCol; iCol++){
 
118810 sqlite3_int64 nToken;
118811 a += sqlite3Fts3GetVarint(a, &nToken);
118812 pInfo->aMatchinfo[iCol] = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
 
118813 }
118814 }
118815 }
118816 break;
118817
118818
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.6. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one 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.
@@ -648,13 +648,13 @@
648 **
649 ** See also: [sqlite3_libversion()],
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.6"
654 #define SQLITE_VERSION_NUMBER 3007006
655 #define SQLITE_SOURCE_ID "2011-02-16 23:32:24 31fc4ba66e76876b2e7b6b2b74c07f47571938ce"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -931,11 +931,11 @@
931 #define SQLITE_NOMEM 7 /* A malloc() failed */
932 #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
933 #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
934 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
935 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
936 #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
937 #define SQLITE_FULL 13 /* Insertion failed because database is full */
938 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
939 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
940 #define SQLITE_EMPTY 16 /* Database is empty */
941 #define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -1163,11 +1163,13 @@
1163 ** locking strategy (for example to use dot-file locks), to inquire
1164 ** about the status of a lock, or to break stale locks. The SQLite
1165 ** core reserves all opcodes less than 100 for its own use.
1166 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
1167 ** Applications that define a custom xFileControl method should use opcodes
1168 ** greater than 100 to avoid conflicts. VFS implementations should
1169 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1170 ** recognize.
1171 **
1172 ** The xSectorSize() method returns the sector size of the
1173 ** device that underlies the file. The sector size is the
1174 ** minimum write that can be performed without disturbing
1175 ** other bytes in the file. The xDeviceCharacteristics()
@@ -3210,11 +3212,11 @@
3212 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3213
3214 /*
3215 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3216 **
3217 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3218 ** and only if the [prepared statement] X makes no direct changes to
3219 ** the content of the database file.
3220 **
3221 ** Note that [application-defined SQL functions] or
3222 ** [virtual tables] might change the database indirectly as a side effect.
@@ -6078,28 +6080,25 @@
6080 ** checked out.</dd>)^
6081 **
6082 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6083 ** <dd>This parameter returns the number malloc attempts that were
6084 ** satisfied using lookaside memory. Only the high-water value is meaningful;
6085 ** the current value is always zero.)^
 
6086 **
6087 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6088 ** <dd>This parameter returns the number malloc attempts that might have
6089 ** been satisfied using lookaside memory but failed due to the amount of
6090 ** memory requested being larger than the lookaside slot size.
6091 ** Only the high-water value is meaningful;
6092 ** the current value is always zero.)^
 
6093 **
6094 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6095 ** <dd>This parameter returns the number malloc attempts that might have
6096 ** been satisfied using lookaside memory but failed due to all lookaside
6097 ** memory already being in use.
6098 ** Only the high-water value is meaningful;
6099 ** the current value is always zero.)^
 
6100 **
6101 ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6102 ** <dd>This parameter returns the approximate number of of bytes of heap
6103 ** memory used by all pager caches associated with the database connection.)^
6104 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6794,12 +6793,105 @@
6793 **
6794 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
6795 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6796 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
6797 ** run whenever the WAL reaches a certain size threshold.
6798 **
6799 ** See also: [sqlite3_wal_checkpoint_v2()]
6800 */
6801 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6802
6803 /*
6804 ** CAPI3REF: Checkpoint a database
6805 **
6806 ** Run a checkpoint operation on WAL database zDb attached to database
6807 ** handle db. The specific operation is determined by the value of the
6808 ** eMode parameter:
6809 **
6810 ** <dl>
6811 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
6812 ** Checkpoint as many frames as possible without waiting for any database
6813 ** readers or writers to finish. Sync the db file if all frames in the log
6814 ** are checkpointed. This mode is the same as calling
6815 ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
6816 **
6817 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
6818 ** This mode blocks (calls the busy-handler callback) until there is no
6819 ** database writer and all readers are reading from the most recent database
6820 ** snapshot. It then checkpoints all frames in the log file and syncs the
6821 ** database file. This call blocks database writers while it is running,
6822 ** but not database readers.
6823 **
6824 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
6825 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
6826 ** checkpointing the log file it blocks (calls the busy-handler callback)
6827 ** until all readers are reading from the database file only. This ensures
6828 ** that the next client to write to the database file restarts the log file
6829 ** from the beginning. This call blocks database writers while it is running,
6830 ** but not database readers.
6831 ** </dl>
6832 **
6833 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
6834 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
6835 ** the total number of checkpointed frames (including any that were already
6836 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
6837 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
6838 ** If no values are available because of an error, they are both set to -1
6839 ** before returning to communicate this to the caller.
6840 **
6841 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
6842 ** any other process is running a checkpoint operation at the same time, the
6843 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
6844 ** busy-handler configured, it will not be invoked in this case.
6845 **
6846 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
6847 ** "writer" lock on the database file. If the writer lock cannot be obtained
6848 ** immediately, and a busy-handler is configured, it is invoked and the writer
6849 ** lock retried until either the busy-handler returns 0 or the lock is
6850 ** successfully obtained. The busy-handler is also invoked while waiting for
6851 ** database readers as described above. If the busy-handler returns 0 before
6852 ** the writer lock is obtained or while waiting for database readers, the
6853 ** checkpoint operation proceeds from that point in the same way as
6854 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
6855 ** without blocking any further. SQLITE_BUSY is returned in this case.
6856 **
6857 ** If parameter zDb is NULL or points to a zero length string, then the
6858 ** specified operation is attempted on all WAL databases. In this case the
6859 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
6860 ** an SQLITE_BUSY error is encountered when processing one or more of the
6861 ** attached WAL databases, the operation is still attempted on any remaining
6862 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
6863 ** error occurs while processing an attached database, processing is abandoned
6864 ** and the error code returned to the caller immediately. If no error
6865 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
6866 ** databases, SQLITE_OK is returned.
6867 **
6868 ** If database zDb is the name of an attached database that is not in WAL
6869 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
6870 ** zDb is not NULL (or a zero length string) and is not the name of any
6871 ** attached database, SQLITE_ERROR is returned to the caller.
6872 */
6873 SQLITE_API int sqlite3_wal_checkpoint_v2(
6874 sqlite3 *db, /* Database handle */
6875 const char *zDb, /* Name of attached database (or NULL) */
6876 int eMode, /* SQLITE_CHECKPOINT_* value */
6877 int *pnLog, /* OUT: Size of WAL log in frames */
6878 int *pnCkpt /* OUT: Total number of frames checkpointed */
6879 );
6880
6881 /*
6882 ** CAPI3REF: Checkpoint operation parameters
6883 **
6884 ** These constants can be used as the 3rd parameter to
6885 ** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
6886 ** documentation for additional information about the meaning and use of
6887 ** each of these values.
6888 */
6889 #define SQLITE_CHECKPOINT_PASSIVE 0
6890 #define SQLITE_CHECKPOINT_FULL 1
6891 #define SQLITE_CHECKPOINT_RESTART 2
6892
6893
6894 /*
6895 ** Undo the hack that converts floating point types to integer for
6896 ** builds on processors without floating point support.
6897 */
@@ -7683,11 +7775,11 @@
7775 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
7776 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
7777 #endif
7778
7779 #ifndef SQLITE_OMIT_WAL
7780 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
7781 #endif
7782
7783 /*
7784 ** If we are not using shared cache, then there is no need to
7785 ** use mutexes to access the BtShared structures. So make the
@@ -8293,11 +8385,11 @@
8385 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
8386 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
8387 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
8388 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
8389
8390 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
8391 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
8392 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
8393 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
8394 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
8395
@@ -11250,11 +11342,11 @@
11342 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11343 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11344 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11345 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11346 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11347 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11348 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11349
11350 /* Declarations for functions in fkey.c. All of these are replaced by
11351 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
11352 ** key functionality is available. If OMIT_TRIGGER is defined but
@@ -16933,11 +17025,11 @@
17025 ** <li> SQLITE_MUTEX_STATIC_MASTER
17026 ** <li> SQLITE_MUTEX_STATIC_MEM
17027 ** <li> SQLITE_MUTEX_STATIC_MEM2
17028 ** <li> SQLITE_MUTEX_STATIC_PRNG
17029 ** <li> SQLITE_MUTEX_STATIC_LRU
17030 ** <li> SQLITE_MUTEX_STATIC_PMEM
17031 ** </ul>
17032 **
17033 ** The first two constants cause sqlite3_mutex_alloc() to create
17034 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17035 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -17343,11 +17435,11 @@
17435 ** <li> SQLITE_MUTEX_STATIC_MASTER
17436 ** <li> SQLITE_MUTEX_STATIC_MEM
17437 ** <li> SQLITE_MUTEX_STATIC_MEM2
17438 ** <li> SQLITE_MUTEX_STATIC_PRNG
17439 ** <li> SQLITE_MUTEX_STATIC_LRU
17440 ** <li> SQLITE_MUTEX_STATIC_PMEM
17441 ** </ul>
17442 **
17443 ** The first two constants cause sqlite3_mutex_alloc() to create
17444 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17445 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
@@ -22268,11 +22360,11 @@
22360 OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
22361 ((os2File*)id)->h, ((os2File*)id)->locktype ));
22362 return SQLITE_OK;
22363 }
22364 }
22365 return SQLITE_NOTFOUND;
22366 }
22367
22368 /*
22369 ** Return the sector size in bytes of the underlying block device for
22370 ** the specified file. This is almost always 512 bytes, but may be
@@ -26204,12 +26296,15 @@
26296 case SQLITE_SET_LOCKPROXYFILE:
26297 case SQLITE_GET_LOCKPROXYFILE: {
26298 return proxyFileControl(id,op,pArg);
26299 }
26300 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
26301 case SQLITE_FCNTL_SYNC_OMITTED: {
26302 return SQLITE_OK; /* A no-op */
26303 }
26304 }
26305 return SQLITE_NOTFOUND;
26306 }
26307
26308 /*
26309 ** Return the sector size in bytes of the underlying block device for
26310 ** the specified file. This is almost always 512 bytes, but may be
@@ -30792,12 +30887,15 @@
30887 SimulateIOErrorBenign(1);
30888 winTruncate(id, sz);
30889 SimulateIOErrorBenign(0);
30890 return SQLITE_OK;
30891 }
30892 case SQLITE_FCNTL_SYNC_OMITTED: {
30893 return SQLITE_OK;
30894 }
30895 }
30896 return SQLITE_NOTFOUND;
30897 }
30898
30899 /*
30900 ** Return the sector size in bytes of the underlying block device for
30901 ** the specified file. This is almost always 512 bytes, but may be
@@ -33465,11 +33563,11 @@
33563 PGroup *pGroup; /* PGroup this cache belongs to */
33564 int szPage; /* Size of allocated pages in bytes */
33565 int bPurgeable; /* True if cache is purgeable */
33566 unsigned int nMin; /* Minimum number of pages reserved */
33567 unsigned int nMax; /* Configured "cache_size" value */
33568 unsigned int n90pct; /* nMax*9/10 */
33569
33570 /* Hash table of all pages. The following variables may only be accessed
33571 ** when the accessor is holding the PGroup mutex.
33572 */
33573 unsigned int nRecyclable; /* Number of pages in the LRU list */
@@ -33619,11 +33717,13 @@
33717 ** it from sqlite3Malloc instead.
33718 */
33719 p = sqlite3Malloc(nByte);
33720 if( p ){
33721 int sz = sqlite3MallocSize(p);
33722 sqlite3_mutex_enter(pcache1.mutex);
33723 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
33724 sqlite3_mutex_leave(pcache1.mutex);
33725 }
33726 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
33727 }
33728 return p;
33729 }
@@ -33647,11 +33747,13 @@
33747 }else{
33748 int iSize;
33749 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
33750 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
33751 iSize = sqlite3MallocSize(p);
33752 sqlite3_mutex_enter(pcache1.mutex);
33753 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
33754 sqlite3_mutex_leave(pcache1.mutex);
33755 sqlite3_free(p);
33756 }
33757 }
33758
33759 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -33990,11 +34092,11 @@
34092 PGroup *pGroup = pCache->pGroup;
34093 pcache1EnterMutex(pGroup);
34094 pGroup->nMaxPage += (nMax - pCache->nMax);
34095 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
34096 pCache->nMax = nMax;
34097 pCache->n90pct = pCache->nMax*9/10;
34098 pcache1EnforceMaxPage(pGroup);
34099 pcache1LeaveMutex(pGroup);
34100 }
34101 }
34102
@@ -34100,15 +34202,16 @@
34202 #endif
34203
34204
34205 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
34206 nPinned = pCache->nPage - pCache->nRecyclable;
34207 assert( nPinned>=0 );
34208 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
34209 assert( pCache->n90pct == pCache->nMax*9/10 );
34210 if( createFlag==1 && (
34211 nPinned>=pGroup->mxPinned
34212 || nPinned>=(int)pCache->n90pct
34213 || pcache1UnderMemoryPressure(pCache)
34214 )){
34215 goto fetch_out;
34216 }
34217
@@ -34820,26 +34923,26 @@
34923 #ifndef _WAL_H_
34924 #define _WAL_H_
34925
34926
34927 #ifdef SQLITE_OMIT_WAL
34928 # define sqlite3WalOpen(x,y,z) 0
34929 # define sqlite3WalClose(w,x,y,z) 0
34930 # define sqlite3WalBeginReadTransaction(y,z) 0
34931 # define sqlite3WalEndReadTransaction(z)
34932 # define sqlite3WalRead(v,w,x,y,z) 0
34933 # define sqlite3WalDbsize(y) 0
34934 # define sqlite3WalBeginWriteTransaction(y) 0
34935 # define sqlite3WalEndWriteTransaction(x) 0
34936 # define sqlite3WalUndo(x,y,z) 0
34937 # define sqlite3WalSavepoint(y,z)
34938 # define sqlite3WalSavepointUndo(y,z) 0
34939 # define sqlite3WalFrames(u,v,w,x,y,z) 0
34940 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
34941 # define sqlite3WalCallback(z) 0
34942 # define sqlite3WalExclusiveMode(y,z) 0
34943 # define sqlite3WalHeapMemory(z) 0
34944 #else
34945
34946 #define WAL_SAVEPOINT_NDATA 4
34947
34948 /* Connection to a write-ahead log (WAL) file.
@@ -34886,13 +34989,18 @@
34989 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
34990
34991 /* Copy pages from the log to the database file */
34992 SQLITE_PRIVATE int sqlite3WalCheckpoint(
34993 Wal *pWal, /* Write-ahead log connection */
34994 int eMode, /* One of PASSIVE, FULL and RESTART */
34995 int (*xBusy)(void*), /* Function to call when busy */
34996 void *pBusyArg, /* Context argument for xBusyHandler */
34997 int sync_flags, /* Flags to sync db file with (or 0) */
34998 int nBuf, /* Size of buffer nBuf */
34999 u8 *zBuf, /* Temporary buffer to use */
35000 int *pnLog, /* OUT: Number of frames in WAL */
35001 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
35002 );
35003
35004 /* Return the value to pass to a sqlite3_wal_hook callback, the
35005 ** number of frames in the WAL at the point of the last commit since
35006 ** sqlite3WalCallback() was called. If no commits have occurred since
@@ -37370,19 +37478,25 @@
37478
37479 if( isOpen(pPager->fd)
37480 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
37481 ){
37482 i64 currentSize, newSize;
37483 int szPage = pPager->pageSize;
37484 assert( pPager->eLock==EXCLUSIVE_LOCK );
37485 /* TODO: Is it safe to use Pager.dbFileSize here? */
37486 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
37487 newSize = szPage*(i64)nPage;
37488 if( rc==SQLITE_OK && currentSize!=newSize ){
37489 if( currentSize>newSize ){
37490 rc = sqlite3OsTruncate(pPager->fd, newSize);
37491 }else{
37492 char *pTmp = pPager->pTmpSpace;
37493 memset(pTmp, 0, szPage);
37494 testcase( (newSize-szPage) < currentSize );
37495 testcase( (newSize-szPage) == currentSize );
37496 testcase( (newSize-szPage) > currentSize );
37497 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
37498 }
37499 if( rc==SQLITE_OK ){
37500 pPager->dbFileSize = nPage;
37501 }
37502 }
@@ -37737,10 +37851,32 @@
37851 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
37852 PAGERID(pPager), pgno, pager_pagehash(pPg)));
37853
37854 return rc;
37855 }
37856
37857 /*
37858 ** Update the value of the change-counter at offsets 24 and 92 in
37859 ** the header and the sqlite version number at offset 96.
37860 **
37861 ** This is an unconditional update. See also the pager_incr_changecounter()
37862 ** routine which only updates the change-counter if the update is actually
37863 ** needed, as determined by the pPager->changeCountDone state variable.
37864 */
37865 static void pager_write_changecounter(PgHdr *pPg){
37866 u32 change_counter;
37867
37868 /* Increment the value just read and write it back to byte 24. */
37869 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
37870 put32bits(((char*)pPg->pData)+24, change_counter);
37871
37872 /* Also store the SQLite version number in bytes 96..99 and in
37873 ** bytes 92..95 store the change counter for which the version number
37874 ** is valid. */
37875 put32bits(((char*)pPg->pData)+92, change_counter);
37876 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
37877 }
37878
37879 #ifndef SQLITE_OMIT_WAL
37880 /*
37881 ** This function is invoked once for each page that has already been
37882 ** written into the log file when a WAL transaction is rolled back.
@@ -37808,38 +37944,15 @@
37944 }
37945
37946 return rc;
37947 }
37948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37949 /*
37950 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
37951 ** the contents of the list of pages headed by pList (connected by pDirty),
37952 ** this function notifies any active backup processes that the pages have
37953 ** changed.
37954 **
37955 ** The list of pages passed into this routine is always sorted by page number.
37956 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
37957 */
37958 static int pagerWalFrames(
@@ -41487,18 +41600,24 @@
41600 return &pPager->pBackup;
41601 }
41602
41603 #ifndef SQLITE_OMIT_WAL
41604 /*
41605 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
41606 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
41607 ** or wal_blocking_checkpoint() API functions.
41608 **
41609 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
41610 */
41611 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
41612 int rc = SQLITE_OK;
41613 if( pPager->pWal ){
41614 rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
41615 pPager->xBusyHandler, pPager->pBusyHandlerArg,
41616 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
41617 pnLog, pnCkpt
41618 );
41619 }
41620 return rc;
41621 }
41622
41623 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
@@ -43233,10 +43352,38 @@
43352 walIteratorFree(p);
43353 }
43354 *pp = p;
43355 return rc;
43356 }
43357
43358 /*
43359 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
43360 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
43361 ** busy-handler function. Invoke it and retry the lock until either the
43362 ** lock is successfully obtained or the busy-handler returns 0.
43363 */
43364 static int walBusyLock(
43365 Wal *pWal, /* WAL connection */
43366 int (*xBusy)(void*), /* Function to call when busy */
43367 void *pBusyArg, /* Context argument for xBusyHandler */
43368 int lockIdx, /* Offset of first byte to lock */
43369 int n /* Number of bytes to lock */
43370 ){
43371 int rc;
43372 do {
43373 rc = walLockExclusive(pWal, lockIdx, n);
43374 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
43375 return rc;
43376 }
43377
43378 /*
43379 ** The cache of the wal-index header must be valid to call this function.
43380 ** Return the page-size in bytes used by the database.
43381 */
43382 static int walPagesize(Wal *pWal){
43383 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
43384 }
43385
43386 /*
43387 ** Copy as much content as we can from the WAL back into the database file
43388 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
43389 **
@@ -43267,12 +43414,14 @@
43414 ** checkpoint is running (in any other thread or process) at the same
43415 ** time.
43416 */
43417 static int walCheckpoint(
43418 Wal *pWal, /* Wal connection */
43419 int eMode, /* One of PASSIVE, FULL or RESTART */
43420 int (*xBusyCall)(void*), /* Function to call when busy */
43421 void *pBusyArg, /* Context argument for xBusyHandler */
43422 int sync_flags, /* Flags for OsSync() (or 0) */
 
43423 u8 *zBuf /* Temporary buffer to use */
43424 ){
43425 int rc; /* Return code */
43426 int szPage; /* Database page-size */
43427 WalIterator *pIter = 0; /* Wal iterator context */
@@ -43280,12 +43429,13 @@
43429 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
43430 u32 mxSafeFrame; /* Max frame that can be backfilled */
43431 u32 mxPage; /* Max database page to write */
43432 int i; /* Loop counter */
43433 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
43434 int (*xBusy)(void*) = 0; /* Function to call when waiting for locks */
43435
43436 szPage = walPagesize(pWal);
43437 testcase( szPage<=32768 );
43438 testcase( szPage>=65536 );
43439 pInfo = walCkptInfo(pWal);
43440 if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
43441
@@ -43294,15 +43444,12 @@
43444 if( rc!=SQLITE_OK ){
43445 return rc;
43446 }
43447 assert( pIter );
43448
43449 mxPage = pWal->hdr.nPage;
43450 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
 
 
 
43451
43452 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
43453 ** safe to write into the database. Frames beyond mxSafeFrame might
43454 ** overwrite database pages that are in use by active readers and thus
43455 ** cannot be backfilled from the WAL.
@@ -43309,26 +43456,27 @@
43456 */
43457 mxSafeFrame = pWal->hdr.mxFrame;
43458 mxPage = pWal->hdr.nPage;
43459 for(i=1; i<WAL_NREADER; i++){
43460 u32 y = pInfo->aReadMark[i];
43461 if( mxSafeFrame>y ){
43462 assert( y<=pWal->hdr.mxFrame );
43463 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
43464 if( rc==SQLITE_OK ){
43465 pInfo->aReadMark[i] = READMARK_NOT_USED;
43466 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
43467 }else if( rc==SQLITE_BUSY ){
43468 mxSafeFrame = y;
43469 xBusy = 0;
43470 }else{
43471 goto walcheckpoint_out;
43472 }
43473 }
43474 }
43475
43476 if( pInfo->nBackfill<mxSafeFrame
43477 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
43478 ){
43479 i64 nSize; /* Current size of database file */
43480 u32 nBackfill = pInfo->nBackfill;
43481
43482 /* Sync the WAL to disk */
@@ -43377,16 +43525,35 @@
43525 }
43526 }
43527
43528 /* Release the reader lock held while backfilling */
43529 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
43530 }
43531
43532 if( rc==SQLITE_BUSY ){
43533 /* Reset the return code so as not to report a checkpoint failure
43534 ** just because there are active readers. */
 
43535 rc = SQLITE_OK;
43536 }
43537
43538 /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
43539 ** file has been copied into the database file, then block until all
43540 ** readers have finished using the wal file. This ensures that the next
43541 ** process to write to the database restarts the wal file.
43542 */
43543 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
43544 assert( pWal->writeLock );
43545 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
43546 rc = SQLITE_BUSY;
43547 }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
43548 assert( mxSafeFrame==pWal->hdr.mxFrame );
43549 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
43550 if( rc==SQLITE_OK ){
43551 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
43552 }
43553 }
43554 }
43555
43556 walcheckpoint_out:
43557 walIteratorFree(pIter);
43558 return rc;
43559 }
@@ -43415,11 +43582,13 @@
43582 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
43583 if( rc==SQLITE_OK ){
43584 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
43585 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
43586 }
43587 rc = sqlite3WalCheckpoint(
43588 pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
43589 );
43590 if( rc==SQLITE_OK ){
43591 isDelete = 1;
43592 }
43593 }
43594
@@ -44330,21 +44499,31 @@
44499 ** This routine is called to implement sqlite3_wal_checkpoint() and
44500 ** related interfaces.
44501 **
44502 ** Obtain a CHECKPOINT lock and then backfill as much information as
44503 ** we can from WAL into the database.
44504 **
44505 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
44506 ** callback. In this case this function runs a blocking checkpoint.
44507 */
44508 SQLITE_PRIVATE int sqlite3WalCheckpoint(
44509 Wal *pWal, /* Wal connection */
44510 int eMode, /* PASSIVE, FULL or RESTART */
44511 int (*xBusy)(void*), /* Function to call when busy */
44512 void *pBusyArg, /* Context argument for xBusyHandler */
44513 int sync_flags, /* Flags to sync db file with (or 0) */
44514 int nBuf, /* Size of temporary buffer */
44515 u8 *zBuf, /* Temporary buffer to use */
44516 int *pnLog, /* OUT: Number of frames in WAL */
44517 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
44518 ){
44519 int rc; /* Return code */
44520 int isChanged = 0; /* True if a new wal-index header is loaded */
44521 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
44522
44523 assert( pWal->ckptLock==0 );
44524 assert( pWal->writeLock==0 );
44525
44526 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
44527 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
44528 if( rc ){
44529 /* Usually this is SQLITE_BUSY meaning that another thread or process
@@ -44351,16 +44530,50 @@
44530 ** is already running a checkpoint, or maybe a recovery. But it might
44531 ** also be SQLITE_IOERR. */
44532 return rc;
44533 }
44534 pWal->ckptLock = 1;
44535
44536 /* If this is a blocking-checkpoint, then obtain the write-lock as well
44537 ** to prevent any writers from running while the checkpoint is underway.
44538 ** This has to be done before the call to walIndexReadHdr() below.
44539 **
44540 ** If the writer lock cannot be obtained, then a passive checkpoint is
44541 ** run instead. Since the checkpointer is not holding the writer lock,
44542 ** there is no point in blocking waiting for any readers. Assuming no
44543 ** other error occurs, this function will return SQLITE_BUSY to the caller.
44544 */
44545 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
44546 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
44547 if( rc==SQLITE_OK ){
44548 pWal->writeLock = 1;
44549 }else if( rc==SQLITE_BUSY ){
44550 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
44551 rc = SQLITE_OK;
44552 }
44553 }
44554
44555 /* Read the wal-index header. */
44556 if( rc==SQLITE_OK ){
44557 rc = walIndexReadHdr(pWal, &isChanged);
44558 }
44559
44560 /* Copy data from the log to the database file. */
 
44561 if( rc==SQLITE_OK ){
44562 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
44563 rc = SQLITE_CORRUPT_BKPT;
44564 }else{
44565 rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
44566 }
44567
44568 /* If no error occurred, set the output variables. */
44569 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
44570 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
44571 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
44572 }
44573 }
44574
44575 if( isChanged ){
44576 /* If a new wal-index header was loaded before the checkpoint was
44577 ** performed, then the pager-cache associated with pWal is now
44578 ** out of date. So zero the cached wal-index header to ensure that
44579 ** next time the pager opens a snapshot on this database it knows that
@@ -44368,14 +44581,15 @@
44581 */
44582 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
44583 }
44584
44585 /* Release the locks. */
44586 sqlite3WalEndWriteTransaction(pWal);
44587 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
44588 pWal->ckptLock = 0;
44589 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
44590 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
44591 }
44592
44593 /* Return the value to pass to a sqlite3_wal_hook callback, the
44594 ** number of frames in the WAL at the point of the last commit since
44595 ** sqlite3WalCallback() was called. If no commits have occurred since
@@ -47851,11 +48065,11 @@
48065 freeTempSpace(pBt);
48066 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
48067 pageSize-usableSize);
48068 return rc;
48069 }
48070 if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
48071 rc = SQLITE_CORRUPT_BKPT;
48072 goto page1_init_failed;
48073 }
48074 if( usableSize<480 ){
48075 goto page1_init_failed;
@@ -53402,20 +53616,22 @@
53616 /*
53617 ** Run a checkpoint on the Btree passed as the first argument.
53618 **
53619 ** Return SQLITE_LOCKED if this or any other connection has an open
53620 ** transaction on the shared-cache the argument Btree is connected to.
53621 **
53622 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
53623 */
53624 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
53625 int rc = SQLITE_OK;
53626 if( p ){
53627 BtShared *pBt = p->pBt;
53628 sqlite3BtreeEnter(p);
53629 if( pBt->inTransaction!=TRANS_NONE ){
53630 rc = SQLITE_LOCKED;
53631 }else{
53632 rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
53633 }
53634 sqlite3BtreeLeave(p);
53635 }
53636 return rc;
53637 }
@@ -55371,10 +55587,12 @@
55587 pVal->u.i = -1 * pVal->u.i;
55588 /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
55589 pVal->r = (double)-1 * pVal->r;
55590 sqlite3ValueApplyAffinity(pVal, affinity, enc);
55591 }
55592 }else if( op==TK_NULL ){
55593 pVal = sqlite3ValueNew(db);
55594 }
55595 #ifndef SQLITE_OMIT_BLOB_LITERAL
55596 else if( op==TK_BLOB ){
55597 int nVal;
55598 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
@@ -57549,20 +57767,25 @@
57767 if( !sqlite3VtabInSync(db)
57768 && db->autoCommit
57769 && db->writeVdbeCnt==(p->readOnly==0)
57770 ){
57771 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
57772 rc = sqlite3VdbeCheckFk(p, 1);
57773 if( rc!=SQLITE_OK ){
57774 if( NEVER(p->readOnly) ){
57775 sqlite3BtreeMutexArrayLeave(&p->aMutex);
57776 return SQLITE_ERROR;
57777 }
57778 rc = SQLITE_CONSTRAINT;
57779 }else{
57780 /* The auto-commit flag is true, the vdbe program was successful
57781 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
57782 ** key constraints to hold up the transaction. This means a commit
57783 ** is required. */
57784 rc = vdbeCommit(db, p);
57785 }
57786 if( rc==SQLITE_BUSY && p->readOnly ){
57787 sqlite3BtreeMutexArrayLeave(&p->aMutex);
57788 return SQLITE_BUSY;
57789 }else if( rc!=SQLITE_OK ){
57790 p->rc = rc;
57791 sqlite3RollbackAll(db);
@@ -57657,11 +57880,11 @@
57880 if( db->autoCommit ){
57881 sqlite3ConnectionUnlocked(db);
57882 }
57883
57884 assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
57885 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
57886 }
57887
57888
57889 /*
57890 ** Each VDBE holds the result of the most recent sqlite3_step() call
@@ -61075,29 +61298,34 @@
61298 sqlite3_value **apVal;
61299 } cb;
61300 struct OP_AggFinal_stack_vars {
61301 Mem *pMem;
61302 } cc;
61303 struct OP_Checkpoint_stack_vars {
61304 int i; /* Loop counter */
61305 int aRes[3]; /* Results */
61306 Mem *pMem; /* Write results here */
61307 } cd;
61308 struct OP_JournalMode_stack_vars {
61309 Btree *pBt; /* Btree to change journal mode of */
61310 Pager *pPager; /* Pager associated with pBt */
61311 int eNew; /* New journal mode */
61312 int eOld; /* The old journal mode */
61313 const char *zFilename; /* Name of database file for pPager */
61314 } ce;
61315 struct OP_IncrVacuum_stack_vars {
61316 Btree *pBt;
61317 } cf;
61318 struct OP_VBegin_stack_vars {
61319 VTable *pVTab;
61320 } cg;
61321 struct OP_VOpen_stack_vars {
61322 VdbeCursor *pCur;
61323 sqlite3_vtab_cursor *pVtabCursor;
61324 sqlite3_vtab *pVtab;
61325 sqlite3_module *pModule;
61326 } ch;
61327 struct OP_VFilter_stack_vars {
61328 int nArg;
61329 int iQuery;
61330 const sqlite3_module *pModule;
61331 Mem *pQuery;
@@ -61106,39 +61334,39 @@
61334 sqlite3_vtab *pVtab;
61335 VdbeCursor *pCur;
61336 int res;
61337 int i;
61338 Mem **apArg;
61339 } ci;
61340 struct OP_VColumn_stack_vars {
61341 sqlite3_vtab *pVtab;
61342 const sqlite3_module *pModule;
61343 Mem *pDest;
61344 sqlite3_context sContext;
61345 } cj;
61346 struct OP_VNext_stack_vars {
61347 sqlite3_vtab *pVtab;
61348 const sqlite3_module *pModule;
61349 int res;
61350 VdbeCursor *pCur;
61351 } ck;
61352 struct OP_VRename_stack_vars {
61353 sqlite3_vtab *pVtab;
61354 Mem *pName;
61355 } cl;
61356 struct OP_VUpdate_stack_vars {
61357 sqlite3_vtab *pVtab;
61358 sqlite3_module *pModule;
61359 int nArg;
61360 int i;
61361 sqlite_int64 rowid;
61362 Mem **apArg;
61363 Mem *pX;
61364 } cm;
61365 struct OP_Trace_stack_vars {
61366 char *zTrace;
61367 } cn;
61368 } u;
61369 /* End automatically generated code
61370 ********************************************************************/
61371
61372 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -65894,17 +66122,42 @@
66122 }
66123 break;
66124 }
66125
66126 #ifndef SQLITE_OMIT_WAL
66127 /* Opcode: Checkpoint P1 P2 P3 * *
66128 **
66129 ** Checkpoint database P1. This is a no-op if P1 is not currently in
66130 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
66131 ** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
66132 ** SQLITE_BUSY or not, respectively. Write the number of pages in the
66133 ** WAL after the checkpoint into mem[P3+1] and the number of pages
66134 ** in the WAL that have been checkpointed after the checkpoint
66135 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
66136 ** mem[P3+2] are initialized to -1.
66137 */
66138 case OP_Checkpoint: {
66139 #if 0 /* local variables moved into u.cd */
66140 int i; /* Loop counter */
66141 int aRes[3]; /* Results */
66142 Mem *pMem; /* Write results here */
66143 #endif /* local variables moved into u.cd */
66144
66145 u.cd.aRes[0] = 0;
66146 u.cd.aRes[1] = u.cd.aRes[2] = -1;
66147 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
66148 || pOp->p2==SQLITE_CHECKPOINT_FULL
66149 || pOp->p2==SQLITE_CHECKPOINT_RESTART
66150 );
66151 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
66152 if( rc==SQLITE_BUSY ){
66153 rc = SQLITE_OK;
66154 u.cd.aRes[0] = 1;
66155 }
66156 for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
66157 sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
66158 }
66159 break;
66160 };
66161 #endif
66162
66163 #ifndef SQLITE_OMIT_PRAGMA
@@ -65918,26 +66171,26 @@
66171 ** If changing into or out of WAL mode the procedure is more complicated.
66172 **
66173 ** Write a string containing the final journal-mode to register P2.
66174 */
66175 case OP_JournalMode: { /* out2-prerelease */
66176 #if 0 /* local variables moved into u.ce */
66177 Btree *pBt; /* Btree to change journal mode of */
66178 Pager *pPager; /* Pager associated with pBt */
66179 int eNew; /* New journal mode */
66180 int eOld; /* The old journal mode */
66181 const char *zFilename; /* Name of database file for pPager */
66182 #endif /* local variables moved into u.ce */
66183
66184 u.ce.eNew = pOp->p3;
66185 assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
66186 || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
66187 || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
66188 || u.ce.eNew==PAGER_JOURNALMODE_OFF
66189 || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
66190 || u.ce.eNew==PAGER_JOURNALMODE_WAL
66191 || u.ce.eNew==PAGER_JOURNALMODE_QUERY
66192 );
66193 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66194
66195 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
66196 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
@@ -65956,76 +66209,76 @@
66209 ** database. */
66210 sqlite3VdbeUsesBtree(p, pOp->p1);
66211 sqlite3VdbeMutexArrayEnter(p);
66212 }
66213
66214 u.ce.pBt = db->aDb[pOp->p1].pBt;
66215 u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
66216 u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
66217 if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
66218 if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
66219
66220 #ifndef SQLITE_OMIT_WAL
66221 u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
66222
66223 /* Do not allow a transition to journal_mode=WAL for a database
66224 ** in temporary storage or if the VFS does not support shared memory
66225 */
66226 if( u.ce.eNew==PAGER_JOURNALMODE_WAL
66227 && (u.ce.zFilename[0]==0 /* Temp file */
66228 || !sqlite3PagerWalSupported(u.ce.pPager)) /* No shared-memory support */
66229 ){
66230 u.ce.eNew = u.ce.eOld;
66231 }
66232
66233 if( (u.ce.eNew!=u.ce.eOld)
66234 && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
66235 ){
66236 if( !db->autoCommit || db->activeVdbeCnt>1 ){
66237 rc = SQLITE_ERROR;
66238 sqlite3SetString(&p->zErrMsg, db,
66239 "cannot change %s wal mode from within a transaction",
66240 (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
66241 );
66242 break;
66243 }else{
66244
66245 if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
66246 /* If leaving WAL mode, close the log file. If successful, the call
66247 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
66248 ** file. An EXCLUSIVE lock may still be held on the database file
66249 ** after a successful return.
66250 */
66251 rc = sqlite3PagerCloseWal(u.ce.pPager);
66252 if( rc==SQLITE_OK ){
66253 sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
66254 }
66255 }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
66256 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
66257 ** as an intermediate */
66258 sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
66259 }
66260
66261 /* Open a transaction on the database file. Regardless of the journal
66262 ** mode, this transaction always uses a rollback journal.
66263 */
66264 assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
66265 if( rc==SQLITE_OK ){
66266 rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
66267 }
66268 }
66269 }
66270 #endif /* ifndef SQLITE_OMIT_WAL */
66271
66272 if( rc ){
66273 u.ce.eNew = u.ce.eOld;
66274 }
66275 u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
66276
66277 pOut = &aMem[pOp->p2];
66278 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
66279 pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
66280 pOut->n = sqlite3Strlen30(pOut->z);
66281 pOut->enc = SQLITE_UTF8;
66282 sqlite3VdbeChangeEncoding(pOut, encoding);
66283 break;
66284 };
@@ -66050,18 +66303,18 @@
66303 ** Perform a single step of the incremental vacuum procedure on
66304 ** the P1 database. If the vacuum has finished, jump to instruction
66305 ** P2. Otherwise, fall through to the next instruction.
66306 */
66307 case OP_IncrVacuum: { /* jump */
66308 #if 0 /* local variables moved into u.cf */
66309 Btree *pBt;
66310 #endif /* local variables moved into u.cf */
66311
66312 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66313 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
66314 u.cf.pBt = db->aDb[pOp->p1].pBt;
66315 rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
66316 if( rc==SQLITE_DONE ){
66317 pc = pOp->p2 - 1;
66318 rc = SQLITE_OK;
66319 }
66320 break;
@@ -66127,16 +66380,16 @@
66380 ** Also, whether or not P4 is set, check that this is not being called from
66381 ** within a callback to a virtual table xSync() method. If it is, the error
66382 ** code will be set to SQLITE_LOCKED.
66383 */
66384 case OP_VBegin: {
66385 #if 0 /* local variables moved into u.cg */
66386 VTable *pVTab;
66387 #endif /* local variables moved into u.cg */
66388 u.cg.pVTab = pOp->p4.pVtab;
66389 rc = sqlite3VtabBegin(db, u.cg.pVTab);
66390 if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
66391 break;
66392 }
66393 #endif /* SQLITE_OMIT_VIRTUALTABLE */
66394
66395 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -66171,36 +66424,36 @@
66424 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
66425 ** P1 is a cursor number. This opcode opens a cursor to the virtual
66426 ** table and stores that cursor in P1.
66427 */
66428 case OP_VOpen: {
66429 #if 0 /* local variables moved into u.ch */
66430 VdbeCursor *pCur;
66431 sqlite3_vtab_cursor *pVtabCursor;
66432 sqlite3_vtab *pVtab;
66433 sqlite3_module *pModule;
66434 #endif /* local variables moved into u.ch */
66435
66436 u.ch.pCur = 0;
66437 u.ch.pVtabCursor = 0;
66438 u.ch.pVtab = pOp->p4.pVtab->pVtab;
66439 u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
66440 assert(u.ch.pVtab && u.ch.pModule);
66441 rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
66442 importVtabErrMsg(p, u.ch.pVtab);
66443 if( SQLITE_OK==rc ){
66444 /* Initialize sqlite3_vtab_cursor base class */
66445 u.ch.pVtabCursor->pVtab = u.ch.pVtab;
66446
66447 /* Initialise vdbe cursor object */
66448 u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
66449 if( u.ch.pCur ){
66450 u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
66451 u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
66452 }else{
66453 db->mallocFailed = 1;
66454 u.ch.pModule->xClose(u.ch.pVtabCursor);
66455 }
66456 }
66457 break;
66458 }
66459 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66223,11 +66476,11 @@
66476 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
66477 **
66478 ** A jump is made to P2 if the result set after filtering would be empty.
66479 */
66480 case OP_VFilter: { /* jump */
66481 #if 0 /* local variables moved into u.ci */
66482 int nArg;
66483 int iQuery;
66484 const sqlite3_module *pModule;
66485 Mem *pQuery;
66486 Mem *pArgc;
@@ -66235,49 +66488,49 @@
66488 sqlite3_vtab *pVtab;
66489 VdbeCursor *pCur;
66490 int res;
66491 int i;
66492 Mem **apArg;
66493 #endif /* local variables moved into u.ci */
66494
66495 u.ci.pQuery = &aMem[pOp->p3];
66496 u.ci.pArgc = &u.ci.pQuery[1];
66497 u.ci.pCur = p->apCsr[pOp->p1];
66498 assert( memIsValid(u.ci.pQuery) );
66499 REGISTER_TRACE(pOp->p3, u.ci.pQuery);
66500 assert( u.ci.pCur->pVtabCursor );
66501 u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
66502 u.ci.pVtab = u.ci.pVtabCursor->pVtab;
66503 u.ci.pModule = u.ci.pVtab->pModule;
66504
66505 /* Grab the index number and argc parameters */
66506 assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
66507 u.ci.nArg = (int)u.ci.pArgc->u.i;
66508 u.ci.iQuery = (int)u.ci.pQuery->u.i;
66509
66510 /* Invoke the xFilter method */
66511 {
66512 u.ci.res = 0;
66513 u.ci.apArg = p->apArg;
66514 for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
66515 u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
66516 sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
66517 }
66518
66519 p->inVtabMethod = 1;
66520 rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
66521 p->inVtabMethod = 0;
66522 importVtabErrMsg(p, u.ci.pVtab);
66523 if( rc==SQLITE_OK ){
66524 u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
66525 }
66526
66527 if( u.ci.res ){
66528 pc = pOp->p2 - 1;
66529 }
66530 }
66531 u.ci.pCur->nullRow = 0;
66532
66533 break;
66534 }
66535 #endif /* SQLITE_OMIT_VIRTUALTABLE */
66536
@@ -66287,55 +66540,55 @@
66540 ** Store the value of the P2-th column of
66541 ** the row of the virtual-table that the
66542 ** P1 cursor is pointing to into register P3.
66543 */
66544 case OP_VColumn: {
66545 #if 0 /* local variables moved into u.cj */
66546 sqlite3_vtab *pVtab;
66547 const sqlite3_module *pModule;
66548 Mem *pDest;
66549 sqlite3_context sContext;
66550 #endif /* local variables moved into u.cj */
66551
66552 VdbeCursor *pCur = p->apCsr[pOp->p1];
66553 assert( pCur->pVtabCursor );
66554 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66555 u.cj.pDest = &aMem[pOp->p3];
66556 memAboutToChange(p, u.cj.pDest);
66557 if( pCur->nullRow ){
66558 sqlite3VdbeMemSetNull(u.cj.pDest);
66559 break;
66560 }
66561 u.cj.pVtab = pCur->pVtabCursor->pVtab;
66562 u.cj.pModule = u.cj.pVtab->pModule;
66563 assert( u.cj.pModule->xColumn );
66564 memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
66565
66566 /* The output cell may already have a buffer allocated. Move
66567 ** the current contents to u.cj.sContext.s so in case the user-function
66568 ** can use the already allocated buffer instead of allocating a
66569 ** new one.
66570 */
66571 sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
66572 MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
66573
66574 rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
66575 importVtabErrMsg(p, u.cj.pVtab);
66576 if( u.cj.sContext.isError ){
66577 rc = u.cj.sContext.isError;
66578 }
66579
66580 /* Copy the result of the function to the P3 register. We
66581 ** do this regardless of whether or not an error occurred to ensure any
66582 ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
66583 */
66584 sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
66585 sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
66586 REGISTER_TRACE(pOp->p3, u.cj.pDest);
66587 UPDATE_MAX_BLOBSIZE(u.cj.pDest);
66588
66589 if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
66590 goto too_big;
66591 }
66592 break;
66593 }
66594 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -66346,42 +66599,42 @@
66599 ** Advance virtual table P1 to the next row in its result set and
66600 ** jump to instruction P2. Or, if the virtual table has reached
66601 ** the end of its result set, then fall through to the next instruction.
66602 */
66603 case OP_VNext: { /* jump */
66604 #if 0 /* local variables moved into u.ck */
66605 sqlite3_vtab *pVtab;
66606 const sqlite3_module *pModule;
66607 int res;
66608 VdbeCursor *pCur;
66609 #endif /* local variables moved into u.ck */
66610
66611 u.ck.res = 0;
66612 u.ck.pCur = p->apCsr[pOp->p1];
66613 assert( u.ck.pCur->pVtabCursor );
66614 if( u.ck.pCur->nullRow ){
66615 break;
66616 }
66617 u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
66618 u.ck.pModule = u.ck.pVtab->pModule;
66619 assert( u.ck.pModule->xNext );
66620
66621 /* Invoke the xNext() method of the module. There is no way for the
66622 ** underlying implementation to return an error if one occurs during
66623 ** xNext(). Instead, if an error occurs, true is returned (indicating that
66624 ** data is available) and the error code returned when xColumn or
66625 ** some other method is next invoked on the save virtual table cursor.
66626 */
66627 p->inVtabMethod = 1;
66628 rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
66629 p->inVtabMethod = 0;
66630 importVtabErrMsg(p, u.ck.pVtab);
66631 if( rc==SQLITE_OK ){
66632 u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
66633 }
66634
66635 if( !u.ck.res ){
66636 /* If there is data, jump to P2 */
66637 pc = pOp->p2 - 1;
66638 }
66639 break;
66640 }
@@ -66393,23 +66646,23 @@
66646 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
66647 ** This opcode invokes the corresponding xRename method. The value
66648 ** in register P1 is passed as the zName argument to the xRename method.
66649 */
66650 case OP_VRename: {
66651 #if 0 /* local variables moved into u.cl */
66652 sqlite3_vtab *pVtab;
66653 Mem *pName;
66654 #endif /* local variables moved into u.cl */
66655
66656 u.cl.pVtab = pOp->p4.pVtab->pVtab;
66657 u.cl.pName = &aMem[pOp->p1];
66658 assert( u.cl.pVtab->pModule->xRename );
66659 assert( memIsValid(u.cl.pName) );
66660 REGISTER_TRACE(pOp->p1, u.cl.pName);
66661 assert( u.cl.pName->flags & MEM_Str );
66662 rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
66663 importVtabErrMsg(p, u.cl.pVtab);
66664 p->expired = 0;
66665
66666 break;
66667 }
66668 #endif
@@ -66437,39 +66690,39 @@
66690 ** P1 is a boolean flag. If it is set to true and the xUpdate call
66691 ** is successful, then the value returned by sqlite3_last_insert_rowid()
66692 ** is set to the value of the rowid for the row just inserted.
66693 */
66694 case OP_VUpdate: {
66695 #if 0 /* local variables moved into u.cm */
66696 sqlite3_vtab *pVtab;
66697 sqlite3_module *pModule;
66698 int nArg;
66699 int i;
66700 sqlite_int64 rowid;
66701 Mem **apArg;
66702 Mem *pX;
66703 #endif /* local variables moved into u.cm */
66704
66705 u.cm.pVtab = pOp->p4.pVtab->pVtab;
66706 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
66707 u.cm.nArg = pOp->p2;
66708 assert( pOp->p4type==P4_VTAB );
66709 if( ALWAYS(u.cm.pModule->xUpdate) ){
66710 u.cm.apArg = p->apArg;
66711 u.cm.pX = &aMem[pOp->p3];
66712 for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
66713 assert( memIsValid(u.cm.pX) );
66714 memAboutToChange(p, u.cm.pX);
66715 sqlite3VdbeMemStoreType(u.cm.pX);
66716 u.cm.apArg[u.cm.i] = u.cm.pX;
66717 u.cm.pX++;
66718 }
66719 rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
66720 importVtabErrMsg(p, u.cm.pVtab);
66721 if( rc==SQLITE_OK && pOp->p1 ){
66722 assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
66723 db->lastRowid = u.cm.rowid;
66724 }
66725 p->nChange++;
66726 }
66727 break;
66728 }
@@ -66517,24 +66770,24 @@
66770 **
66771 ** If tracing is enabled (by the sqlite3_trace()) interface, then
66772 ** the UTF-8 string contained in P4 is emitted on the trace callback.
66773 */
66774 case OP_Trace: {
66775 #if 0 /* local variables moved into u.cn */
66776 char *zTrace;
66777 #endif /* local variables moved into u.cn */
66778
66779 u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
66780 if( u.cn.zTrace ){
66781 if( db->xTrace ){
66782 char *z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
66783 db->xTrace(db->pTraceArg, z);
66784 sqlite3DbFree(db, z);
66785 }
66786 #ifdef SQLITE_DEBUG
66787 if( (db->flags & SQLITE_SqlTrace)!=0 ){
66788 sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
66789 }
66790 #endif /* SQLITE_DEBUG */
66791 }
66792 break;
66793 }
@@ -69058,11 +69311,11 @@
69311 ** there is no default collation type, return 0.
69312 */
69313 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
69314 CollSeq *pColl = 0;
69315 Expr *p = pExpr;
69316 while( p ){
69317 int op;
69318 pColl = p->pColl;
69319 if( pColl ) break;
69320 op = p->op;
69321 if( p->pTab!=0 && (
@@ -72217,18 +72470,20 @@
72470 case TK_BETWEEN: {
72471 testcase( jumpIfNull==0 );
72472 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
72473 break;
72474 }
72475 #ifndef SQLITE_OMIT_SUBQUERY
72476 case TK_IN: {
72477 int destIfFalse = sqlite3VdbeMakeLabel(v);
72478 int destIfNull = jumpIfNull ? dest : destIfFalse;
72479 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
72480 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
72481 sqlite3VdbeResolveLabel(v, destIfFalse);
72482 break;
72483 }
72484 #endif
72485 default: {
72486 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
72487 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
72488 testcase( regFree1==0 );
72489 testcase( jumpIfNull==0 );
@@ -72358,10 +72613,11 @@
72613 case TK_BETWEEN: {
72614 testcase( jumpIfNull==0 );
72615 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
72616 break;
72617 }
72618 #ifndef SQLITE_OMIT_SUBQUERY
72619 case TK_IN: {
72620 if( jumpIfNull ){
72621 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
72622 }else{
72623 int destIfNull = sqlite3VdbeMakeLabel(v);
@@ -72368,10 +72624,11 @@
72624 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
72625 sqlite3VdbeResolveLabel(v, destIfNull);
72626 }
72627 break;
72628 }
72629 #endif
72630 default: {
72631 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
72632 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
72633 testcase( regFree1==0 );
72634 testcase( jumpIfNull==0 );
@@ -74549,13 +74806,15 @@
74806 goto attach_end;
74807 }
74808
74809 #ifndef SQLITE_OMIT_AUTHORIZATION
74810 if( pAuthArg ){
74811 char *zAuthArg;
74812 if( pAuthArg->op==TK_STRING ){
74813 zAuthArg = pAuthArg->u.zToken;
74814 }else{
74815 zAuthArg = 0;
74816 }
74817 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
74818 if(rc!=SQLITE_OK ){
74819 goto attach_end;
74820 }
@@ -87034,17 +87293,33 @@
87293 }else
87294 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
87295
87296 #ifndef SQLITE_OMIT_WAL
87297 /*
87298 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
87299 **
87300 ** Checkpoint the database.
87301 */
87302 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
87303 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
87304 int eMode = SQLITE_CHECKPOINT_PASSIVE;
87305 if( zRight ){
87306 if( sqlite3StrICmp(zRight, "full")==0 ){
87307 eMode = SQLITE_CHECKPOINT_FULL;
87308 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
87309 eMode = SQLITE_CHECKPOINT_RESTART;
87310 }
87311 }
87312 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
87313 sqlite3VdbeSetNumCols(v, 3);
87314 pParse->nMem = 3;
87315 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
87316 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
87317 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
87318
87319 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
87320 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
87321 }else
87322
87323 /*
87324 ** PRAGMA wal_autocheckpoint
87325 ** PRAGMA wal_autocheckpoint = N
@@ -90670,10 +90945,13 @@
90945 ** (20) If the sub-query is a compound select, then it must not use
90946 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
90947 ** somewhat by saying that the terms of the ORDER BY clause must
90948 ** appear as unmodified result columns in the outer query. But
90949 ** have other optimizations in mind to deal with that case.
90950 **
90951 ** (21) The subquery does not use LIMIT or the outer query is not
90952 ** DISTINCT. (See ticket [752e1646fc]).
90953 **
90954 ** In this routine, the "p" parameter is a pointer to the outer query.
90955 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
90956 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
90957 **
@@ -90739,10 +91017,13 @@
91017 if( p->pOrderBy && pSub->pOrderBy ){
91018 return 0; /* Restriction (11) */
91019 }
91020 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
91021 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
91022 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
91023 return 0; /* Restriction (21) */
91024 }
91025
91026 /* OBSOLETE COMMENT 1:
91027 ** Restriction 3: If the subquery is a join, make sure the subquery is
91028 ** not used as the right operand of an outer join. Examples of why this
91029 ** is not allowed:
@@ -95714,10 +95995,11 @@
95995 ** generating the code that loops through a table looking for applicable
95996 ** rows. Indices are selected and used to speed the search when doing
95997 ** so is applicable. Because this module is responsible for selecting
95998 ** indices, you might also think of this module as the "query optimizer".
95999 */
96000
96001
96002 /*
96003 ** Trace output macros
96004 */
96005 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
@@ -95814,10 +96096,15 @@
96096 #define TERM_CODED 0x04 /* This term is already coded */
96097 #define TERM_COPIED 0x08 /* Has a child */
96098 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
96099 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
96100 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
96101 #ifdef SQLITE_ENABLE_STAT2
96102 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
96103 #else
96104 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
96105 #endif
96106
96107 /*
96108 ** An instance of the following structure holds all information about a
96109 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
96110 */
@@ -95907,10 +96194,11 @@
96194 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
96195 #define WO_MATCH 0x040
96196 #define WO_ISNULL 0x080
96197 #define WO_OR 0x100 /* Two or more OR-connected terms */
96198 #define WO_AND 0x200 /* Two or more AND-connected terms */
96199 #define WO_NOOP 0x800 /* This term does not restrict search space */
96200
96201 #define WO_ALL 0xfff /* Mask of all possible WO_* values */
96202 #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
96203
96204 /*
@@ -96757,11 +97045,11 @@
97045 pWC->a[idxNew].iParent = idxTerm;
97046 pTerm->nChild = 1;
97047 }else{
97048 sqlite3ExprListDelete(db, pList);
97049 }
97050 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
97051 }
97052 }
97053 }
97054 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
97055
@@ -97021,10 +97309,48 @@
97309 pNewTerm->prereqAll = pTerm->prereqAll;
97310 }
97311 }
97312 #endif /* SQLITE_OMIT_VIRTUALTABLE */
97313
97314 #ifdef SQLITE_ENABLE_STAT2
97315 /* When sqlite_stat2 histogram data is available an operator of the
97316 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
97317 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
97318 ** virtual term of that form.
97319 **
97320 ** Note that the virtual term must be tagged with TERM_VNULL. This
97321 ** TERM_VNULL tag will suppress the not-null check at the beginning
97322 ** of the loop. Without the TERM_VNULL flag, the not-null check at
97323 ** the start of the loop will prevent any results from being returned.
97324 */
97325 if( pExpr->op==TK_NOTNULL && pExpr->pLeft->iColumn>=0 ){
97326 Expr *pNewExpr;
97327 Expr *pLeft = pExpr->pLeft;
97328 int idxNew;
97329 WhereTerm *pNewTerm;
97330
97331 pNewExpr = sqlite3PExpr(pParse, TK_GT,
97332 sqlite3ExprDup(db, pLeft, 0),
97333 sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
97334
97335 idxNew = whereClauseInsert(pWC, pNewExpr,
97336 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
97337 if( idxNew ){
97338 pNewTerm = &pWC->a[idxNew];
97339 pNewTerm->prereqRight = 0;
97340 pNewTerm->leftCursor = pLeft->iTable;
97341 pNewTerm->u.leftColumn = pLeft->iColumn;
97342 pNewTerm->eOperator = WO_GT;
97343 pNewTerm->iParent = idxTerm;
97344 pTerm = &pWC->a[idxTerm];
97345 pTerm->nChild = 1;
97346 pTerm->wtFlags |= TERM_COPIED;
97347 pNewTerm->prereqAll = pTerm->prereqAll;
97348 }
97349 }
97350 #endif /* SQLITE_ENABLE_STAT2 */
97351
97352 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
97353 ** an index for tables to the left of the join.
97354 */
97355 pTerm->prereqRight |= extraRight;
97356 }
@@ -97073,10 +97399,11 @@
97399 WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
97400 Index *pIdx, /* The index we are testing */
97401 int base, /* Cursor number for the table to be sorted */
97402 ExprList *pOrderBy, /* The ORDER BY clause */
97403 int nEqCol, /* Number of index columns with == constraints */
97404 int wsFlags, /* Index usages flags */
97405 int *pbRev /* Set to 1 if ORDER BY is DESC */
97406 ){
97407 int i, j; /* Loop counters */
97408 int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
97409 int nTerm; /* Number of ORDER BY terms */
@@ -97178,15 +97505,18 @@
97505 /* All terms of the ORDER BY clause are covered by this index so
97506 ** this index can be used for sorting. */
97507 return 1;
97508 }
97509 if( pIdx->onError!=OE_None && i==pIdx->nColumn
97510 && (wsFlags & WHERE_COLUMN_NULL)==0
97511 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
97512 /* All terms of this index match some prefix of the ORDER BY clause
97513 ** and the index is UNIQUE and no terms on the tail of the ORDER BY
97514 ** clause reference other tables in a join. If this is all true then
97515 ** the order by clause is superfluous. Not that if the matching
97516 ** condition is IS NULL then the result is not necessarily unique
97517 ** even on a UNIQUE index, so disallow those cases. */
97518 return 1;
97519 }
97520 return 0;
97521 }
97522
@@ -97419,11 +97749,11 @@
97749
97750 /* Search for any equality comparison term */
97751 pWCEnd = &pWC->a[pWC->nTerm];
97752 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
97753 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
97754 WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
97755 pCost->rCost, costTempIdx));
97756 pCost->rCost = costTempIdx;
97757 pCost->plan.nRow = logN + 1;
97758 pCost->plan.wsFlags = WHERE_TEMP_INDEX;
97759 pCost->used = pTerm->prereqRight;
@@ -97540,11 +97870,12 @@
97870 if( (idxCols & cMask)==0 ){
97871 Expr *pX = pTerm->pExpr;
97872 idxCols |= cMask;
97873 pIdx->aiColumn[n] = pTerm->u.leftColumn;
97874 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
97875 assert( pColl!=0 || pParse->nErr>0 );
97876 pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
97877 n++;
97878 }
97879 }
97880 }
97881 assert( (u32)n==pLevel->plan.nEq );
@@ -97898,15 +98229,22 @@
98229 #endif /* SQLITE_OMIT_VIRTUALTABLE */
98230
98231 /*
98232 ** Argument pIdx is a pointer to an index structure that has an array of
98233 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
98234 ** stored in Index.aSample. These samples divide the domain of values stored
98235 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
98236 ** Region 0 contains all values less than the first sample value. Region
98237 ** 1 contains values between the first and second samples. Region 2 contains
98238 ** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
98239 ** contains values larger than the last sample.
98240 **
98241 ** If the index contains many duplicates of a single value, then it is
98242 ** possible that two or more adjacent samples can hold the same value.
98243 ** When that is the case, the smallest possible region code is returned
98244 ** when roundUp is false and the largest possible region code is returned
98245 ** when roundUp is true.
98246 **
98247 ** If successful, this function determines which of the regions value
98248 ** pVal lies in, sets *piRegion to the region index (a value between 0
98249 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
98250 ** Or, if an OOM occurs while converting text values between encodings,
@@ -97915,22 +98253,34 @@
98253 #ifdef SQLITE_ENABLE_STAT2
98254 static int whereRangeRegion(
98255 Parse *pParse, /* Database connection */
98256 Index *pIdx, /* Index to consider domain of */
98257 sqlite3_value *pVal, /* Value to consider */
98258 int roundUp, /* Return largest valid region if true */
98259 int *piRegion /* OUT: Region of domain in which value lies */
98260 ){
98261 assert( roundUp==0 || roundUp==1 );
98262 if( ALWAYS(pVal) ){
98263 IndexSample *aSample = pIdx->aSample;
98264 int i = 0;
98265 int eType = sqlite3_value_type(pVal);
98266
98267 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
98268 double r = sqlite3_value_double(pVal);
98269 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
98270 if( aSample[i].eType==SQLITE_NULL ) continue;
98271 if( aSample[i].eType>=SQLITE_TEXT ) break;
98272 if( roundUp ){
98273 if( aSample[i].u.r>r ) break;
98274 }else{
98275 if( aSample[i].u.r>=r ) break;
98276 }
98277 }
98278 }else if( eType==SQLITE_NULL ){
98279 i = 0;
98280 if( roundUp ){
98281 while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
98282 }
98283 }else{
98284 sqlite3 *db = pParse->db;
98285 CollSeq *pColl;
98286 const u8 *z;
@@ -97957,11 +98307,11 @@
98307 assert( z && pColl && pColl->xCmp );
98308 }
98309 n = sqlite3ValueBytes(pVal, pColl->enc);
98310
98311 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
98312 int c;
98313 int eSampletype = aSample[i].eType;
98314 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
98315 if( (eSampletype!=eType) ) break;
98316 #ifndef SQLITE_OMIT_UTF16
98317 if( pColl->enc!=SQLITE_UTF8 ){
@@ -97971,18 +98321,18 @@
98321 );
98322 if( !zSample ){
98323 assert( db->mallocFailed );
98324 return SQLITE_NOMEM;
98325 }
98326 c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
98327 sqlite3DbFree(db, zSample);
98328 }else
98329 #endif
98330 {
98331 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
98332 }
98333 if( c-roundUp>=0 ) break;
98334 }
98335 }
98336
98337 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
98338 *piRegion = i;
@@ -98061,13 +98411,13 @@
98411 ** constraints (if any). A return value of 100 indicates that it is expected
98412 ** that the range scan will visit every row (100%) selected by the equality
98413 ** constraints.
98414 **
98415 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
98416 ** reduces the search space by 3/4ths. Hence a single constraint (x>?)
98417 ** results in a return of 25 and a range constraint (x>? AND x<?) results
98418 ** in a return of 6.
98419 */
98420 static int whereRangeScanEst(
98421 Parse *pParse, /* Parsing & code generating context */
98422 Index *p, /* The index containing the range-compared column; "x" */
98423 int nEq, /* index into p->aCol[] of the range-compared column */
@@ -98083,73 +98433,209 @@
98433 sqlite3_value *pLowerVal = 0;
98434 sqlite3_value *pUpperVal = 0;
98435 int iEst;
98436 int iLower = 0;
98437 int iUpper = SQLITE_INDEX_SAMPLES;
98438 int roundUpUpper = 0;
98439 int roundUpLower = 0;
98440 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98441
98442 if( pLower ){
98443 Expr *pExpr = pLower->pExpr->pRight;
98444 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
98445 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
98446 roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
98447 }
98448 if( rc==SQLITE_OK && pUpper ){
98449 Expr *pExpr = pUpper->pExpr->pRight;
98450 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
98451 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
98452 roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
98453 }
98454
98455 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
98456 sqlite3ValueFree(pLowerVal);
98457 sqlite3ValueFree(pUpperVal);
98458 goto range_est_fallback;
98459 }else if( pLowerVal==0 ){
98460 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
98461 if( pLower ) iLower = iUpper/2;
98462 }else if( pUpperVal==0 ){
98463 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
98464 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
98465 }else{
98466 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
98467 if( rc==SQLITE_OK ){
98468 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
98469 }
98470 }
98471 WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
98472
98473 iEst = iUpper - iLower;
98474 testcase( iEst==SQLITE_INDEX_SAMPLES );
98475 assert( iEst<=SQLITE_INDEX_SAMPLES );
98476 if( iEst<1 ){
98477 *piEst = 50/SQLITE_INDEX_SAMPLES;
98478 }else{
98479 *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
98480 }
 
98481 sqlite3ValueFree(pLowerVal);
98482 sqlite3ValueFree(pUpperVal);
 
98483 return rc;
98484 }
98485 range_est_fallback:
98486 #else
98487 UNUSED_PARAMETER(pParse);
98488 UNUSED_PARAMETER(p);
98489 UNUSED_PARAMETER(nEq);
98490 #endif
98491 assert( pLower || pUpper );
98492 *piEst = 100;
98493 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
98494 if( pUpper ) *piEst /= 4;
 
 
98495 return rc;
98496 }
98497
98498 #ifdef SQLITE_ENABLE_STAT2
98499 /*
98500 ** Estimate the number of rows that will be returned based on
98501 ** an equality constraint x=VALUE and where that VALUE occurs in
98502 ** the histogram data. This only works when x is the left-most
98503 ** column of an index and sqlite_stat2 histogram data is available
98504 ** for that index.
98505 **
98506 ** Write the estimated row count into *pnRow and return SQLITE_OK.
98507 ** If unable to make an estimate, leave *pnRow unchanged and return
98508 ** non-zero.
98509 **
98510 ** This routine can fail if it is unable to load a collating sequence
98511 ** required for string comparison, or if unable to allocate memory
98512 ** for a UTF conversion required for comparison. The error is stored
98513 ** in the pParse structure.
98514 */
98515 int whereEqualScanEst(
98516 Parse *pParse, /* Parsing & code generating context */
98517 Index *p, /* The index whose left-most column is pTerm */
98518 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
98519 double *pnRow /* Write the revised row estimate here */
98520 ){
98521 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
98522 int iLower, iUpper; /* Range of histogram regions containing pRhs */
98523 u8 aff; /* Column affinity */
98524 int rc; /* Subfunction return code */
98525 double nRowEst; /* New estimate of the number of rows */
98526
98527 assert( p->aSample!=0 );
98528 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98529 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
98530 if( rc ) goto whereEqualScanEst_cancel;
98531 if( pRhs==0 ) return SQLITE_NOTFOUND;
98532 rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
98533 if( rc ) goto whereEqualScanEst_cancel;
98534 rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
98535 if( rc ) goto whereEqualScanEst_cancel;
98536 WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
98537 if( iLower>=iUpper ){
98538 nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
98539 if( nRowEst<*pnRow ) *pnRow = nRowEst;
98540 }else{
98541 nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
98542 *pnRow = nRowEst;
98543 }
98544
98545 whereEqualScanEst_cancel:
98546 sqlite3ValueFree(pRhs);
98547 return rc;
98548 }
98549 #endif /* defined(SQLITE_ENABLE_STAT2) */
98550
98551 #ifdef SQLITE_ENABLE_STAT2
98552 /*
98553 ** Estimate the number of rows that will be returned based on
98554 ** an IN constraint where the right-hand side of the IN operator
98555 ** is a list of values. Example:
98556 **
98557 ** WHERE x IN (1,2,3,4)
98558 **
98559 ** Write the estimated row count into *pnRow and return SQLITE_OK.
98560 ** If unable to make an estimate, leave *pnRow unchanged and return
98561 ** non-zero.
98562 **
98563 ** This routine can fail if it is unable to load a collating sequence
98564 ** required for string comparison, or if unable to allocate memory
98565 ** for a UTF conversion required for comparison. The error is stored
98566 ** in the pParse structure.
98567 */
98568 int whereInScanEst(
98569 Parse *pParse, /* Parsing & code generating context */
98570 Index *p, /* The index whose left-most column is pTerm */
98571 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
98572 double *pnRow /* Write the revised row estimate here */
98573 ){
98574 sqlite3_value *pVal = 0; /* One value from list */
98575 int iLower, iUpper; /* Range of histogram regions containing pRhs */
98576 u8 aff; /* Column affinity */
98577 int rc = SQLITE_OK; /* Subfunction return code */
98578 double nRowEst; /* New estimate of the number of rows */
98579 int nSpan = 0; /* Number of histogram regions spanned */
98580 int nSingle = 0; /* Histogram regions hit by a single value */
98581 int nNotFound = 0; /* Count of values that are not constants */
98582 int i; /* Loop counter */
98583 u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
98584 u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
98585
98586 assert( p->aSample!=0 );
98587 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
98588 memset(aSpan, 0, sizeof(aSpan));
98589 memset(aSingle, 0, sizeof(aSingle));
98590 for(i=0; i<pList->nExpr; i++){
98591 sqlite3ValueFree(pVal);
98592 rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
98593 if( rc ) break;
98594 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
98595 nNotFound++;
98596 continue;
98597 }
98598 rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
98599 if( rc ) break;
98600 rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
98601 if( rc ) break;
98602 if( iLower>=iUpper ){
98603 aSingle[iLower] = 1;
98604 }else{
98605 assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
98606 while( iLower<iUpper ) aSpan[iLower++] = 1;
98607 }
98608 }
98609 if( rc==SQLITE_OK ){
98610 for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
98611 if( aSpan[i] ){
98612 nSpan++;
98613 }else if( aSingle[i] ){
98614 nSingle++;
98615 }
98616 }
98617 nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
98618 + nNotFound*p->aiRowEst[1];
98619 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
98620 *pnRow = nRowEst;
98621 WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
98622 nSpan, nSingle, nNotFound, nRowEst));
98623 }
98624 sqlite3ValueFree(pVal);
98625 return rc;
98626 }
98627 #endif /* defined(SQLITE_ENABLE_STAT2) */
98628
98629
98630 /*
98631 ** Find the best query plan for accessing a particular table. Write the
98632 ** best query plan and its cost into the WhereCost object supplied as the
98633 ** last parameter.
98634 **
98635 ** The lowest cost plan wins. The cost is an estimate of the amount of
98636 ** CPU and disk I/O needed to process the requested result.
98637 ** Factors that influence cost include:
98638 **
98639 ** * The estimated number of rows that will be retrieved. (The
98640 ** fewer the better.)
98641 **
@@ -98164,11 +98650,11 @@
98650 ** SQLITE_BIG_DBL. If a plan is found that uses the named index,
98651 ** then the cost is calculated in the usual way.
98652 **
98653 ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table
98654 ** in the SELECT statement, then no indexes are considered. However, the
98655 ** selected plan may still take advantage of the built-in rowid primary key
98656 ** index.
98657 */
98658 static void bestBtreeIndex(
98659 Parse *pParse, /* The parsing context */
98660 WhereClause *pWC, /* The WHERE clause */
@@ -98207,13 +98693,15 @@
98693 /* An INDEXED BY clause specifies a particular index to use */
98694 pIdx = pProbe = pSrc->pIndex;
98695 wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
98696 eqTermMask = idxEqTermMask;
98697 }else{
98698 /* There is no INDEXED BY clause. Create a fake Index object in local
98699 ** variable sPk to represent the rowid primary key index. Make this
98700 ** fake index the first in a chain of Index objects with all of the real
98701 ** indices to follow */
98702 Index *pFirst; /* First of real indices on the table */
98703 memset(&sPk, 0, sizeof(Index));
98704 sPk.nColumn = 1;
98705 sPk.aiColumn = &aiColumnPk;
98706 sPk.aiRowEst = aiRowEstPk;
98707 sPk.onError = OE_Replace;
@@ -98220,10 +98708,12 @@
98708 sPk.pTable = pSrc->pTab;
98709 aiRowEstPk[0] = pSrc->pTab->nRowEst;
98710 aiRowEstPk[1] = 1;
98711 pFirst = pSrc->pTab->pIndex;
98712 if( pSrc->notIndexed==0 ){
98713 /* The real indices of the table are only considered if the
98714 ** NOT INDEXED qualifier is omitted from the FROM clause */
98715 sPk.pNext = pFirst;
98716 }
98717 pProbe = &sPk;
98718 wsFlagMask = ~(
98719 WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
@@ -98236,20 +98726,23 @@
98726 */
98727 for(; pProbe; pIdx=pProbe=pProbe->pNext){
98728 const unsigned int * const aiRowEst = pProbe->aiRowEst;
98729 double cost; /* Cost of using pProbe */
98730 double nRow; /* Estimated number of rows in result set */
98731 double log10N; /* base-10 logarithm of nRow (inexact) */
98732 int rev; /* True to scan in reverse order */
98733 int wsFlags = 0;
98734 Bitmask used = 0;
98735
98736 /* The following variables are populated based on the properties of
98737 ** index being evaluated. They are then used to determine the expected
98738 ** cost and number of rows returned.
98739 **
98740 ** nEq:
98741 ** Number of equality terms that can be implemented using the index.
98742 ** In other words, the number of initial fields in the index that
98743 ** are used in == or IN or NOT NULL constraints of the WHERE clause.
98744 **
98745 ** nInMul:
98746 ** The "in-multiplier". This is an estimate of how many seek operations
98747 ** SQLite must perform on the index in question. For example, if the
98748 ** WHERE clause is:
@@ -98269,46 +98762,54 @@
98762 ** the sub-select is assumed to return 25 rows for the purposes of
98763 ** determining nInMul.
98764 **
98765 ** bInEst:
98766 ** Set to true if there was at least one "x IN (SELECT ...)" term used
98767 ** in determining the value of nInMul. Note that the RHS of the
98768 ** IN operator must be a SELECT, not a value list, for this variable
98769 ** to be true.
98770 **
98771 ** estBound:
98772 ** An estimate on the amount of the table that must be searched. A
98773 ** value of 100 means the entire table is searched. Range constraints
98774 ** might reduce this to a value less than 100 to indicate that only
98775 ** a fraction of the table needs searching. In the absence of
98776 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
98777 ** space to 1/4rd its original size. So an x>? constraint reduces
98778 ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
98779 **
98780 ** bSort:
98781 ** Boolean. True if there is an ORDER BY clause that will require an
98782 ** external sort (i.e. scanning the index being evaluated will not
98783 ** correctly order records).
98784 **
98785 ** bLookup:
98786 ** Boolean. True if a table lookup is required for each index entry
98787 ** visited. In other words, true if this is not a covering index.
98788 ** This is always false for the rowid primary key index of a table.
98789 ** For other indexes, it is true unless all the columns of the table
98790 ** used by the SELECT statement are present in the index (such an
98791 ** index is sometimes described as a covering index).
98792 ** For example, given the index on (a, b), the second of the following
98793 ** two queries requires table b-tree lookups in order to find the value
98794 ** of column c, but the first does not because columns a and b are
98795 ** both available in the index.
98796 **
98797 ** SELECT a, b FROM tbl WHERE a = 1;
98798 ** SELECT a, b, c FROM tbl WHERE a = 1;
98799 */
98800 int nEq; /* Number of == or IN terms matching index */
98801 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
98802 int nInMul = 1; /* Number of distinct equalities to lookup */
98803 int estBound = 100; /* Estimated reduction in search space */
98804 int nBound = 0; /* Number of range constraints seen */
98805 int bSort = 0; /* True if external sort required */
98806 int bLookup = 0; /* True if not a covering index */
98807 WhereTerm *pTerm; /* A single term of the WHERE clause */
98808 #ifdef SQLITE_ENABLE_STAT2
98809 WhereTerm *pFirstTerm = 0; /* First term matching the index */
98810 #endif
98811
98812 /* Determine the values of nEq and nInMul */
98813 for(nEq=0; nEq<pProbe->nColumn; nEq++){
98814 int j = pProbe->aiColumn[nEq];
98815 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
@@ -98316,18 +98817,23 @@
98817 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
98818 if( pTerm->eOperator & WO_IN ){
98819 Expr *pExpr = pTerm->pExpr;
98820 wsFlags |= WHERE_COLUMN_IN;
98821 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98822 /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
98823 nInMul *= 25;
98824 bInEst = 1;
98825 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
98826 /* "x IN (value, value, ...)" */
98827 nInMul *= pExpr->x.pList->nExpr;
98828 }
98829 }else if( pTerm->eOperator & WO_ISNULL ){
98830 wsFlags |= WHERE_COLUMN_NULL;
98831 }
98832 #ifdef SQLITE_ENABLE_STAT2
98833 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
98834 #endif
98835 used |= pTerm->prereqRight;
98836 }
98837
98838 /* Determine the value of estBound. */
98839 if( nEq<pProbe->nColumn ){
@@ -98359,12 +98865,13 @@
98865 /* If there is an ORDER BY clause and the index being considered will
98866 ** naturally scan rows in the required order, set the appropriate flags
98867 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
98868 ** will scan rows in a different order, set the bSort variable. */
98869 if( pOrderBy ){
98870 if( (wsFlags & WHERE_COLUMN_IN)==0
98871 && isSortingIndex(pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy,
98872 nEq, wsFlags, &rev)
98873 ){
98874 wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
98875 wsFlags |= (rev ? WHERE_REVERSE : 0);
98876 }else{
98877 bSort = 1;
@@ -98391,44 +98898,101 @@
98898 bLookup = 1;
98899 }
98900 }
98901
98902 /*
98903 ** Estimate the number of rows of output. For an "x IN (SELECT...)"
98904 ** constraint, do not let the estimate exceed half the rows in the table.
98905 */
98906 nRow = (double)(aiRowEst[nEq] * nInMul);
98907 if( bInEst && nRow*2>aiRowEst[0] ){
98908 nRow = aiRowEst[0]/2;
98909 nInMul = (int)(nRow / aiRowEst[nEq]);
98910 }
98911
98912 #ifdef SQLITE_ENABLE_STAT2
98913 /* If the constraint is of the form x=VALUE and histogram
98914 ** data is available for column x, then it might be possible
98915 ** to get a better estimate on the number of rows based on
98916 ** VALUE and how common that value is according to the histogram.
98917 */
98918 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){
98919 if( pFirstTerm->eOperator==WO_EQ ){
98920 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
98921 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
98922 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
98923 }
98924 }
98925 #endif /* SQLITE_ENABLE_STAT2 */
98926
98927 /* Adjust the number of output rows and downward to reflect rows
98928 ** that are excluded by range constraints.
98929 */
98930 nRow = (nRow * (double)estBound) / (double)100;
98931 if( nRow<1 ) nRow = 1;
98932
98933 /* Experiments run on real SQLite databases show that the time needed
98934 ** to do a binary search to locate a row in a table or index is roughly
98935 ** log10(N) times the time to move from one row to the next row within
98936 ** a table or index. The actual times can vary, with the size of
98937 ** records being an important factor. Both moves and searches are
98938 ** slower with larger records, presumably because fewer records fit
98939 ** on one page and hence more pages have to be fetched.
98940 **
98941 ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do
98942 ** not give us data on the relative sizes of table and index records.
98943 ** So this computation assumes table records are about twice as big
98944 ** as index records
98945 */
98946 if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
98947 /* The cost of a full table scan is a number of move operations equal
98948 ** to the number of rows in the table.
98949 **
98950 ** We add an additional 4x penalty to full table scans. This causes
98951 ** the cost function to err on the side of choosing an index over
98952 ** choosing a full scan. This 4x full-scan penalty is an arguable
98953 ** decision and one which we expect to revisit in the future. But
98954 ** it seems to be working well enough at the moment.
98955 */
98956 cost = aiRowEst[0]*4;
98957 }else{
98958 log10N = estLog(aiRowEst[0]);
98959 cost = nRow;
98960 if( pIdx ){
98961 if( bLookup ){
98962 /* For an index lookup followed by a table lookup:
98963 ** nInMul index searches to find the start of each index range
98964 ** + nRow steps through the index
98965 ** + nRow table searches to lookup the table entry using the rowid
98966 */
98967 cost += (nInMul + nRow)*log10N;
98968 }else{
98969 /* For a covering index:
98970 ** nInMul index searches to find the initial entry
98971 ** + nRow steps through the index
98972 */
98973 cost += nInMul*log10N;
98974 }
98975 }else{
98976 /* For a rowid primary key lookup:
98977 ** nInMult table searches to find the initial entry for each range
98978 ** + nRow steps through the table
98979 */
98980 cost += nInMul*log10N;
98981 }
98982 }
98983
98984 /* Add in the estimated cost of sorting the result. Actual experimental
98985 ** measurements of sorting performance in SQLite show that sorting time
98986 ** adds C*N*log10(N) to the cost, where N is the number of rows to be
98987 ** sorted and C is a factor between 1.95 and 4.3. We will split the
98988 ** difference and select C of 3.0.
98989 */
98990 if( bSort ){
98991 cost += nRow*estLog(nRow)*3;
98992 }
98993
 
 
 
 
 
 
 
98994 /**** Cost of using this index has now been computed ****/
98995
98996 /* If there are additional constraints on this table that cannot
98997 ** be used with the current index, but which might lower the number
98998 ** of output rows, adjust the nRow value accordingly. This only
@@ -98465,19 +99029,23 @@
99029 ** set size by a factor of 10 */
99030 nRow /= 10;
99031 }
99032 }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
99033 if( nSkipRange ){
99034 /* Ignore the first nSkipRange range constraints since the index
99035 ** has already accounted for these */
99036 nSkipRange--;
99037 }else{
99038 /* Assume each additional range constraint reduces the result
99039 ** set size by a factor of 3. Indexed range constraints reduce
99040 ** the search space by a larger factor: 4. We make indexed range
99041 ** more selective intentionally because of the subjective
99042 ** observation that indexed range constraints really are more
99043 ** selective in practice, on average. */
99044 nRow /= 3;
99045 }
99046 }else if( pTerm->eOperator!=WO_NOOP ){
99047 /* Any other expression lowers the output row count by half */
99048 nRow /= 2;
99049 }
99050 }
99051 if( nRow<2 ) nRow = 2;
@@ -98484,14 +99052,14 @@
99052 }
99053
99054
99055 WHERETRACE((
99056 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
99057 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
99058 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
99059 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
99060 notReady, log10N, nRow, cost, used
99061 ));
99062
99063 /* If this index is the best we have seen so far, then record this
99064 ** index and its cost in the pCost structure.
99065 */
@@ -99311,11 +99879,13 @@
99879 /* Seek the index cursor to the start of the range. */
99880 nConstraint = nEq;
99881 if( pRangeStart ){
99882 Expr *pRight = pRangeStart->pExpr->pRight;
99883 sqlite3ExprCode(pParse, pRight, regBase+nEq);
99884 if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
99885 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99886 }
99887 if( zStartAff ){
99888 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
99889 /* Since the comparison is to be performed with no conversions
99890 ** applied to the operands, set the affinity to apply to pRight to
99891 ** SQLITE_AFF_NONE. */
@@ -99350,11 +99920,13 @@
99920 nConstraint = nEq;
99921 if( pRangeEnd ){
99922 Expr *pRight = pRangeEnd->pExpr->pRight;
99923 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
99924 sqlite3ExprCode(pParse, pRight, regBase+nEq);
99925 if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
99926 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
99927 }
99928 if( zEndAff ){
99929 if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
99930 /* Since the comparison is to be performed with no conversions
99931 ** applied to the operands, set the affinity to apply to pRight to
99932 ** SQLITE_AFF_NONE. */
@@ -105802,11 +106374,11 @@
106374 /* SQLITE_NOMEM */ "out of memory",
106375 /* SQLITE_READONLY */ "attempt to write a readonly database",
106376 /* SQLITE_INTERRUPT */ "interrupted",
106377 /* SQLITE_IOERR */ "disk I/O error",
106378 /* SQLITE_CORRUPT */ "database disk image is malformed",
106379 /* SQLITE_NOTFOUND */ "unknown operation",
106380 /* SQLITE_FULL */ "database or disk is full",
106381 /* SQLITE_CANTOPEN */ "unable to open database file",
106382 /* SQLITE_PROTOCOL */ "locking protocol",
106383 /* SQLITE_EMPTY */ "table contains no data",
106384 /* SQLITE_SCHEMA */ "database schema has changed",
@@ -106326,40 +106898,64 @@
106898 #else
106899 return 0;
106900 #endif
106901 }
106902
 
106903 /*
106904 ** Checkpoint database zDb.
 
 
106905 */
106906 SQLITE_API int sqlite3_wal_checkpoint_v2(
106907 sqlite3 *db, /* Database handle */
106908 const char *zDb, /* Name of attached database (or NULL) */
106909 int eMode, /* SQLITE_CHECKPOINT_* value */
106910 int *pnLog, /* OUT: Size of WAL log in frames */
106911 int *pnCkpt /* OUT: Total number of frames checkpointed */
106912 ){
106913 #ifdef SQLITE_OMIT_WAL
106914 return SQLITE_OK;
106915 #else
106916 int rc; /* Return code */
106917 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
106918
106919 /* Initialize the output variables to -1 in case an error occurs. */
106920 if( pnLog ) *pnLog = -1;
106921 if( pnCkpt ) *pnCkpt = -1;
106922
106923 assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
106924 assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
106925 assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
106926 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
106927 return SQLITE_MISUSE;
106928 }
106929
106930 sqlite3_mutex_enter(db->mutex);
106931 if( zDb && zDb[0] ){
106932 iDb = sqlite3FindDbName(db, zDb);
106933 }
106934 if( iDb<0 ){
106935 rc = SQLITE_ERROR;
106936 sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
106937 }else{
106938 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
106939 sqlite3Error(db, rc, 0);
106940 }
106941 rc = sqlite3ApiExit(db, rc);
106942 sqlite3_mutex_leave(db->mutex);
106943 return rc;
106944 #endif
106945 }
106946
106947
106948 /*
106949 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
106950 ** to contains a zero-length string, all attached databases are
106951 ** checkpointed.
106952 */
106953 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
106954 return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
106955 }
106956
106957 #ifndef SQLITE_OMIT_WAL
106958 /*
106959 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
106960 ** not currently open in WAL mode.
106961 **
@@ -106373,24 +106969,35 @@
106969 ** this function while the checkpoint is running.
106970 **
106971 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
106972 ** checkpointed. If an error is encountered it is returned immediately -
106973 ** no attempt is made to checkpoint any remaining databases.
106974 **
106975 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
106976 */
106977 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
106978 int rc = SQLITE_OK; /* Return code */
106979 int i; /* Used to iterate through attached dbs */
106980 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
106981
106982 assert( sqlite3_mutex_held(db->mutex) );
106983 assert( !pnLog || *pnLog==-1 );
106984 assert( !pnCkpt || *pnCkpt==-1 );
106985
106986 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
106987 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
106988 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
106989 pnLog = 0;
106990 pnCkpt = 0;
106991 if( rc==SQLITE_BUSY ){
106992 bBusy = 1;
106993 rc = SQLITE_OK;
106994 }
106995 }
106996 }
106997
106998 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
106999 }
107000 #endif /* SQLITE_OMIT_WAL */
107001
107002 /*
107003 ** This function returns true if main-memory should be used instead of
@@ -107350,10 +107957,12 @@
107957 if( op==SQLITE_FCNTL_FILE_POINTER ){
107958 *(sqlite3_file**)pArg = fd;
107959 rc = SQLITE_OK;
107960 }else if( fd->pMethods ){
107961 rc = sqlite3OsFileControl(fd, op, pArg);
107962 }else{
107963 rc = SQLITE_NOTFOUND;
107964 }
107965 sqlite3BtreeLeave(pBtree);
107966 }
107967 }
107968 sqlite3_mutex_leave(db->mutex);
@@ -108597,11 +109206,11 @@
109206 typedef struct Fts3PhraseToken Fts3PhraseToken;
109207
109208 typedef struct Fts3SegFilter Fts3SegFilter;
109209 typedef struct Fts3DeferredToken Fts3DeferredToken;
109210 typedef struct Fts3SegReader Fts3SegReader;
109211 typedef struct Fts3SegReaderCursor Fts3SegReaderCursor;
109212
109213 /*
109214 ** A connection to a fulltext index is an instance of the following
109215 ** structure. The xCreate and xConnect methods create an instance
109216 ** of this structure and xDestroy and xDisconnect free that instance.
@@ -108620,10 +109229,13 @@
109229 /* Precompiled statements used by the implementation. Each of these
109230 ** statements is run and reset within a single virtual table API call.
109231 */
109232 sqlite3_stmt *aStmt[24];
109233
109234 char *zReadExprlist;
109235 char *zWriteExprlist;
109236
109237 int nNodeSize; /* Soft limit for node size */
109238 u8 bHasStat; /* True if %_stat table exists */
109239 u8 bHasDocsize; /* True if %_docsize table exists */
109240 int nPgsz; /* Page size for host database */
109241 char *zSegmentsTbl; /* Name of %_segments table */
@@ -108707,11 +109319,11 @@
109319 struct Fts3PhraseToken {
109320 char *z; /* Text of the token */
109321 int n; /* Number of bytes in buffer z */
109322 int isPrefix; /* True if token ends with a "*" character */
109323 int bFulltext; /* True if full-text index was used */
109324 Fts3SegReaderCursor *pSegcsr; /* Segment-reader for this token */
109325 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
109326 };
109327
109328 struct Fts3Phrase {
109329 /* Variables populated by fts3_expr.c when parsing a MATCH expression */
@@ -108775,16 +109387,12 @@
109387 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
109388 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
109389 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
109390 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**);
109391 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
 
 
 
 
109392 SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *);
109393 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, sqlite3_stmt **);
109394 SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
109395 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*);
109396
109397 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
109398 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
@@ -108792,26 +109400,54 @@
109400 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
109401 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
109402 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
109403 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
109404 SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *);
 
109405 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
109406
109407 #define FTS3_SEGCURSOR_PENDING -1
109408 #define FTS3_SEGCURSOR_ALL -2
109409
109410 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3SegReaderCursor*, Fts3SegFilter*);
109411 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3SegReaderCursor *);
109412 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3SegReaderCursor *);
109413 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
109414 Fts3Table *, int, const char *, int, int, int, Fts3SegReaderCursor *);
109415
109416 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
109417 #define FTS3_SEGMENT_REQUIRE_POS 0x00000001
109418 #define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
109419 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
109420 #define FTS3_SEGMENT_PREFIX 0x00000008
109421 #define FTS3_SEGMENT_SCAN 0x00000010
109422
109423 /* Type passed as 4th argument to SegmentReaderIterate() */
109424 struct Fts3SegFilter {
109425 const char *zTerm;
109426 int nTerm;
109427 int iCol;
109428 int flags;
109429 };
109430
109431 struct Fts3SegReaderCursor {
109432 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
109433 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
109434 int nSegment; /* Size of apSegment array */
109435 int nAdvance; /* How many seg-readers to advance */
109436 Fts3SegFilter *pFilter; /* Pointer to filter object */
109437 char *aBuffer; /* Buffer to merge doclists in */
109438 int nBuffer; /* Allocated size of aBuffer[] in bytes */
109439
109440 /* Cost of running this iterator. Used by fts3.c only. */
109441 int nCost;
109442
109443 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
109444 char *zTerm; /* Pointer to term buffer */
109445 int nTerm; /* Size of zTerm in bytes */
109446 char *aDoclist; /* Pointer to doclist buffer */
109447 int nDoclist; /* Size of aDoclist[] in bytes */
109448 };
109449
109450 /* fts3.c */
109451 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
109452 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
109453 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
@@ -108845,10 +109481,13 @@
109481 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
109482 #ifdef SQLITE_TEST
109483 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
109484 #endif
109485
109486 /* fts3_aux.c */
109487 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
109488
109489 #endif /* _FTSINT_H */
109490
109491 /************** End of fts3Int.h *********************************************/
109492 /************** Continuing where we left off in fts3.c ***********************/
109493
@@ -108990,10 +109629,12 @@
109629 /* Free any prepared statements held */
109630 for(i=0; i<SizeofArray(p->aStmt); i++){
109631 sqlite3_finalize(p->aStmt[i]);
109632 }
109633 sqlite3_free(p->zSegmentsTbl);
109634 sqlite3_free(p->zReadExprlist);
109635 sqlite3_free(p->zWriteExprlist);
109636
109637 /* Invoke the tokenizer destructor to free the tokenizer. */
109638 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
109639
109640 sqlite3_free(p);
@@ -109206,10 +109847,145 @@
109847 sqlite3Fts3Dequote(zValue);
109848 }
109849 *pzValue = zValue;
109850 return 1;
109851 }
109852
109853 /*
109854 ** Append the output of a printf() style formatting to an existing string.
109855 */
109856 static void fts3Appendf(
109857 int *pRc, /* IN/OUT: Error code */
109858 char **pz, /* IN/OUT: Pointer to string buffer */
109859 const char *zFormat, /* Printf format string to append */
109860 ... /* Arguments for printf format string */
109861 ){
109862 if( *pRc==SQLITE_OK ){
109863 va_list ap;
109864 char *z;
109865 va_start(ap, zFormat);
109866 z = sqlite3_vmprintf(zFormat, ap);
109867 if( z && *pz ){
109868 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
109869 sqlite3_free(z);
109870 z = z2;
109871 }
109872 if( z==0 ) *pRc = SQLITE_NOMEM;
109873 sqlite3_free(*pz);
109874 *pz = z;
109875 }
109876 }
109877
109878 /*
109879 ** Return a copy of input string zInput enclosed in double-quotes (") and
109880 ** with all double quote characters escaped. For example:
109881 **
109882 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
109883 **
109884 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
109885 ** is the callers responsibility to call sqlite3_free() to release this
109886 ** memory.
109887 */
109888 static char *fts3QuoteId(char const *zInput){
109889 int nRet;
109890 char *zRet;
109891 nRet = 2 + strlen(zInput)*2 + 1;
109892 zRet = sqlite3_malloc(nRet);
109893 if( zRet ){
109894 int i;
109895 char *z = zRet;
109896 *(z++) = '"';
109897 for(i=0; zInput[i]; i++){
109898 if( zInput[i]=='"' ) *(z++) = '"';
109899 *(z++) = zInput[i];
109900 }
109901 *(z++) = '"';
109902 *(z++) = '\0';
109903 }
109904 return zRet;
109905 }
109906
109907 /*
109908 ** Return a list of comma separated SQL expressions that could be used
109909 ** in a SELECT statement such as the following:
109910 **
109911 ** SELECT <list of expressions> FROM %_content AS x ...
109912 **
109913 ** to return the docid, followed by each column of text data in order
109914 ** from left to write. If parameter zFunc is not NULL, then instead of
109915 ** being returned directly each column of text data is passed to an SQL
109916 ** function named zFunc first. For example, if zFunc is "unzip" and the
109917 ** table has the three user-defined columns "a", "b", and "c", the following
109918 ** string is returned:
109919 **
109920 ** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')"
109921 **
109922 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
109923 ** is the responsibility of the caller to eventually free it.
109924 **
109925 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
109926 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
109927 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
109928 ** no error occurs, *pRc is left unmodified.
109929 */
109930 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
109931 char *zRet = 0;
109932 char *zFree = 0;
109933 char *zFunction;
109934 int i;
109935
109936 if( !zFunc ){
109937 zFunction = "";
109938 }else{
109939 zFree = zFunction = fts3QuoteId(zFunc);
109940 }
109941 fts3Appendf(pRc, &zRet, "docid");
109942 for(i=0; i<p->nColumn; i++){
109943 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
109944 }
109945 sqlite3_free(zFree);
109946 return zRet;
109947 }
109948
109949 /*
109950 ** Return a list of N comma separated question marks, where N is the number
109951 ** of columns in the %_content table (one for the docid plus one for each
109952 ** user-defined text column).
109953 **
109954 ** If argument zFunc is not NULL, then all but the first question mark
109955 ** is preceded by zFunc and an open bracket, and followed by a closed
109956 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
109957 ** user-defined text columns, the following string is returned:
109958 **
109959 ** "?, zip(?), zip(?), zip(?)"
109960 **
109961 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
109962 ** is the responsibility of the caller to eventually free it.
109963 **
109964 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
109965 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
109966 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
109967 ** no error occurs, *pRc is left unmodified.
109968 */
109969 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
109970 char *zRet = 0;
109971 char *zFree = 0;
109972 char *zFunction;
109973 int i;
109974
109975 if( !zFunc ){
109976 zFunction = "";
109977 }else{
109978 zFree = zFunction = fts3QuoteId(zFunc);
109979 }
109980 fts3Appendf(pRc, &zRet, "?");
109981 for(i=0; i<p->nColumn; i++){
109982 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
109983 }
109984 sqlite3_free(zFree);
109985 return zRet;
109986 }
109987
109988 /*
109989 ** This function is the implementation of both the xConnect and xCreate
109990 ** methods of the FTS3 virtual table.
109991 **
@@ -109243,10 +110019,13 @@
110019 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
110020 int bNoDocsize = 0; /* True to omit %_docsize table */
110021 const char **aCol; /* Array of column names */
110022 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
110023
110024 char *zCompress = 0;
110025 char *zUncompress = 0;
110026
110027 assert( strlen(argv[0])==4 );
110028 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
110029 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
110030 );
110031
@@ -109293,10 +110072,16 @@
110072 bNoDocsize = 1;
110073 }else{
110074 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
110075 rc = SQLITE_ERROR;
110076 }
110077 }else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){
110078 zCompress = zVal;
110079 zVal = 0;
110080 }else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){
110081 zUncompress = zVal;
110082 zVal = 0;
110083 }else{
110084 *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
110085 rc = SQLITE_ERROR;
110086 }
110087 sqlite3_free(zVal);
@@ -109366,10 +110151,19 @@
110151 sqlite3Fts3Dequote(zCsr);
110152 p->azColumn[iCol] = zCsr;
110153 zCsr += n+1;
110154 assert( zCsr <= &((char *)p)[nByte] );
110155 }
110156
110157 if( (zCompress==0)!=(zUncompress==0) ){
110158 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
110159 rc = SQLITE_ERROR;
110160 *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
110161 }
110162 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
110163 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
110164 if( rc!=SQLITE_OK ) goto fts3_init_out;
110165
110166 /* If this is an xCreate call, create the underlying tables in the
110167 ** database. TODO: For xConnect(), it could verify that said tables exist.
110168 */
110169 if( isCreate ){
@@ -109384,11 +110178,12 @@
110178
110179 /* Declare the table schema to SQLite. */
110180 fts3DeclareVtab(&rc, p);
110181
110182 fts3_init_out:
110183 sqlite3_free(zCompress);
110184 sqlite3_free(zUncompress);
110185 sqlite3_free((void *)aCol);
110186 if( rc!=SQLITE_OK ){
110187 if( p ){
110188 fts3DisconnectMethod((sqlite3_vtab *)p);
110189 }else if( pTokenizer ){
@@ -110477,134 +111272,137 @@
111272 }
111273
111274 return SQLITE_OK;
111275 }
111276
111277 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
111278 Fts3Table *p, /* FTS3 table handle */
111279 int iLevel, /* Level of segments to scan */
111280 const char *zTerm, /* Term to query for */
111281 int nTerm, /* Size of zTerm in bytes */
111282 int isPrefix, /* True for a prefix search */
111283 int isScan, /* True to scan from zTerm to EOF */
111284 Fts3SegReaderCursor *pCsr /* Cursor object to populate */
111285 ){
111286 int rc = SQLITE_OK;
111287 int rc2;
111288 int iAge = 0;
111289 sqlite3_stmt *pStmt = 0;
111290 Fts3SegReader *pPending = 0;
111291
111292 assert( iLevel==FTS3_SEGCURSOR_ALL
111293 || iLevel==FTS3_SEGCURSOR_PENDING
111294 || iLevel>=0
111295 );
111296 assert( FTS3_SEGCURSOR_PENDING<0 );
111297 assert( FTS3_SEGCURSOR_ALL<0 );
111298 assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) );
111299 assert( isPrefix==0 || isScan==0 );
111300
111301
111302 memset(pCsr, 0, sizeof(Fts3SegReaderCursor));
111303
111304 /* If iLevel is less than 0, include a seg-reader for the pending-terms. */
111305 assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 );
111306 if( iLevel<0 && isScan==0 ){
111307 rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending);
111308 if( rc==SQLITE_OK && pPending ){
111309 int nByte = (sizeof(Fts3SegReader *) * 16);
111310 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
111311 if( pCsr->apSegment==0 ){
111312 rc = SQLITE_NOMEM;
111313 }else{
111314 pCsr->apSegment[0] = pPending;
111315 pCsr->nSegment = 1;
111316 pPending = 0;
111317 }
111318 }
111319 }
111320
111321 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
111322 if( rc==SQLITE_OK ){
111323 rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt);
111324 }
111325 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
111326
111327 /* Read the values returned by the SELECT into local variables. */
111328 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
111329 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
111330 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
111331 int nRoot = sqlite3_column_bytes(pStmt, 4);
111332 char const *zRoot = sqlite3_column_blob(pStmt, 4);
111333
111334 /* If nSegment is a multiple of 16 the array needs to be extended. */
111335 if( (pCsr->nSegment%16)==0 ){
111336 Fts3SegReader **apNew;
111337 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
111338 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
111339 if( !apNew ){
111340 rc = SQLITE_NOMEM;
111341 goto finished;
111342 }
111343 pCsr->apSegment = apNew;
111344 }
111345
111346 /* If zTerm is not NULL, and this segment is not stored entirely on its
111347 ** root node, the range of leaves scanned can be reduced. Do this. */
111348 if( iStartBlock && zTerm ){
111349 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
111350 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
111351 if( rc!=SQLITE_OK ) goto finished;
111352 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
111353 }
111354
111355 rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock,
111356 iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment]
111357 );
111358 if( rc!=SQLITE_OK ) goto finished;
111359 pCsr->nSegment++;
111360 iAge++;
111361 }
111362 }
111363
111364 finished:
111365 rc2 = sqlite3_reset(pStmt);
111366 if( rc==SQLITE_DONE ) rc = rc2;
111367 sqlite3Fts3SegReaderFree(pPending);
111368
111369 return rc;
111370 }
111371
111372
111373 static int fts3TermSegReaderCursor(
111374 Fts3Cursor *pCsr, /* Virtual table cursor handle */
111375 const char *zTerm, /* Term to query for */
111376 int nTerm, /* Size of zTerm in bytes */
111377 int isPrefix, /* True for a prefix search */
111378 Fts3SegReaderCursor **ppSegcsr /* OUT: Allocated seg-reader cursor */
111379 ){
111380 Fts3SegReaderCursor *pSegcsr; /* Object to allocate and return */
111381 int rc = SQLITE_NOMEM; /* Return code */
111382
111383 pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor));
111384 if( pSegcsr ){
111385 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
111386 int i;
111387 int nCost = 0;
111388 rc = sqlite3Fts3SegReaderCursor(
111389 p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr);
111390
111391 for(i=0; rc==SQLITE_OK && i<pSegcsr->nSegment; i++){
111392 rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost);
111393 }
111394 pSegcsr->nCost = nCost;
111395 }
111396
111397 *ppSegcsr = pSegcsr;
111398 return rc;
111399 }
111400
111401 static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){
111402 sqlite3Fts3SegReaderFinish(pSegcsr);
111403 sqlite3_free(pSegcsr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111404 }
111405
111406 /*
111407 ** This function retreives the doclist for the specified term (or term
111408 ** prefix) from the database.
@@ -110623,15 +111421,15 @@
111421 int isReqPos, /* True to include position lists in output */
111422 int *pnOut, /* OUT: Size of buffer at *ppOut */
111423 char **ppOut /* OUT: Malloced result buffer */
111424 ){
111425 int rc; /* Return code */
111426 Fts3SegReaderCursor *pSegcsr; /* Seg-reader cursor for this term */
111427 TermSelect tsc; /* Context object for fts3TermSelectCb() */
111428 Fts3SegFilter filter; /* Segment term filter configuration */
111429
111430 pSegcsr = pTok->pSegcsr;
111431 memset(&tsc, 0, sizeof(TermSelect));
111432 tsc.isReqPos = isReqPos;
111433
111434 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY
111435 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
@@ -110639,17 +111437,22 @@
111437 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
111438 filter.iCol = iColumn;
111439 filter.zTerm = pTok->z;
111440 filter.nTerm = pTok->n;
111441
111442 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
111443 while( SQLITE_OK==rc
111444 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
111445 ){
111446 rc = fts3TermSelectCb(p, (void *)&tsc,
111447 pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist
111448 );
111449 }
111450
111451 if( rc==SQLITE_OK ){
111452 rc = fts3TermSelectMerge(&tsc);
111453 }
 
111454 if( rc==SQLITE_OK ){
111455 *ppOut = tsc.aaOutput[0];
111456 *pnOut = tsc.anOutput[0];
111457 }else{
111458 int i;
@@ -110656,12 +111459,12 @@
111459 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
111460 sqlite3_free(tsc.aaOutput[i]);
111461 }
111462 }
111463
111464 fts3SegReaderCursorFree(pSegcsr);
111465 pTok->pSegcsr = 0;
111466 return rc;
111467 }
111468
111469 /*
111470 ** This function counts the total number of docids in the doclist stored
@@ -110780,17 +111583,17 @@
111583 ** evaluation, only create segment-readers if there are no Fts3DeferredToken
111584 ** objects attached to the phrase-tokens.
111585 */
111586 for(ii=0; ii<pPhrase->nToken; ii++){
111587 Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
111588 if( pTok->pSegcsr==0 ){
111589 if( (pCsr->eEvalmode==FTS3_EVAL_FILTER)
111590 || (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0)
111591 || (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext)
111592 ){
111593 rc = fts3TermSegReaderCursor(
111594 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
111595 );
111596 if( rc!=SQLITE_OK ) return rc;
111597 }
111598 }
111599 }
@@ -110817,14 +111620,14 @@
111620 int nMinCost = 0x7FFFFFFF;
111621 int jj;
111622
111623 /* Find the remaining token with the lowest cost. */
111624 for(jj=0; jj<pPhrase->nToken; jj++){
111625 Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr;
111626 if( pSegcsr && pSegcsr->nCost<nMinCost ){
111627 iTok = jj;
111628 nMinCost = pSegcsr->nCost;
111629 }
111630 }
111631 pTok = &pPhrase->aToken[iTok];
111632
111633 /* This branch is taken if it is determined that loading the doclist
@@ -110839,16 +111642,16 @@
111642 }
111643
111644 if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){
111645 rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList);
111646 }else{
111647 if( pTok->pSegcsr ){
111648 rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList);
111649 }
111650 pTok->bFulltext = 1;
111651 }
111652 assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 );
111653 if( rc!=SQLITE_OK ) break;
111654
111655 if( isFirst ){
111656 pOut = pList;
111657 nOut = nList;
@@ -111022,13 +111825,13 @@
111825 Fts3Phrase *pPhrase = pExpr->pPhrase;
111826 int ii;
111827
111828 for(ii=0; rc==SQLITE_OK && ii<pPhrase->nToken; ii++){
111829 Fts3PhraseToken *pTok = &pPhrase->aToken[ii];
111830 if( pTok->pSegcsr==0 ){
111831 rc = fts3TermSegReaderCursor(
111832 pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr
111833 );
111834 }
111835 }
111836 }else{
111837 rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr);
@@ -111048,12 +111851,12 @@
111851 if( pExpr ){
111852 Fts3Phrase *pPhrase = pExpr->pPhrase;
111853 if( pPhrase ){
111854 int kk;
111855 for(kk=0; kk<pPhrase->nToken; kk++){
111856 fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr);
111857 pPhrase->aToken[kk].pSegcsr = 0;
111858 }
111859 }
111860 fts3ExprFreeSegReaders(pExpr->pLeft);
111861 fts3ExprFreeSegReaders(pExpr->pRight);
111862 }
@@ -111069,14 +111872,12 @@
111872 if( pExpr->eType==FTSQUERY_PHRASE ){
111873 Fts3Phrase *pPhrase = pExpr->pPhrase;
111874 int ii;
111875 nCost = 0;
111876 for(ii=0; ii<pPhrase->nToken; ii++){
111877 Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr;
111878 if( pSegcsr ) nCost += pSegcsr->nCost;
 
 
111879 }
111880 }else{
111881 nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight);
111882 }
111883 return nCost;
@@ -111414,12 +112215,12 @@
112215 const char *idxStr, /* Unused */
112216 int nVal, /* Number of elements in apVal */
112217 sqlite3_value **apVal /* Arguments for the indexing scheme */
112218 ){
112219 const char *azSql[] = {
112220 "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?", /* non-full-scan */
112221 "SELECT %s FROM %Q.'%q_content' AS x ", /* full-scan */
112222 };
112223 int rc; /* Return code */
112224 char *zSql; /* SQL statement used to access %_content */
112225 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
112226 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
@@ -111470,11 +112271,12 @@
112271 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
112272 ** statement loops through all rows of the %_content table. For a
112273 ** full-text query or docid lookup, the statement retrieves a single
112274 ** row by docid.
112275 */
112276 zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH];
112277 zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName);
112278 if( !zSql ){
112279 rc = SQLITE_NOMEM;
112280 }else{
112281 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
112282 sqlite3_free(zSql);
@@ -111988,10 +112790,13 @@
112790 #ifdef SQLITE_ENABLE_ICU
112791 const sqlite3_tokenizer_module *pIcu = 0;
112792 sqlite3Fts3IcuTokenizerModule(&pIcu);
112793 #endif
112794
112795 rc = sqlite3Fts3InitAux(db);
112796 if( rc!=SQLITE_OK ) return rc;
112797
112798 sqlite3Fts3SimpleTokenizerModule(&pSimple);
112799 sqlite3Fts3PorterTokenizerModule(&pPorter);
112800
112801 /* Allocate and initialise the hash-table used to store tokenizers. */
112802 pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -112063,10 +112868,472 @@
112868 #endif
112869
112870 #endif
112871
112872 /************** End of fts3.c ************************************************/
112873 /************** Begin file fts3_aux.c ****************************************/
112874 /*
112875 ** 2011 Jan 27
112876 **
112877 ** The author disclaims copyright to this source code. In place of
112878 ** a legal notice, here is a blessing:
112879 **
112880 ** May you do good and not evil.
112881 ** May you find forgiveness for yourself and forgive others.
112882 ** May you share freely, never taking more than you give.
112883 **
112884 ******************************************************************************
112885 **
112886 */
112887
112888 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
112889
112890
112891 typedef struct Fts3auxTable Fts3auxTable;
112892 typedef struct Fts3auxCursor Fts3auxCursor;
112893
112894 struct Fts3auxTable {
112895 sqlite3_vtab base; /* Base class used by SQLite core */
112896 Fts3Table *pFts3Tab;
112897 };
112898
112899 struct Fts3auxCursor {
112900 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
112901 Fts3SegReaderCursor csr; /* Must be right after "base" */
112902 Fts3SegFilter filter;
112903 char *zStop;
112904 int nStop; /* Byte-length of string zStop */
112905 int isEof; /* True if cursor is at EOF */
112906 sqlite3_int64 iRowid; /* Current rowid */
112907
112908 int iCol; /* Current value of 'col' column */
112909 int nStat; /* Size of aStat[] array */
112910 struct Fts3auxColstats {
112911 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
112912 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
112913 } *aStat;
112914 };
112915
112916 /*
112917 ** Schema of the terms table.
112918 */
112919 #define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
112920
112921 /*
112922 ** This function does all the work for both the xConnect and xCreate methods.
112923 ** These tables have no persistent representation of their own, so xConnect
112924 ** and xCreate are identical operations.
112925 */
112926 static int fts3auxConnectMethod(
112927 sqlite3 *db, /* Database connection */
112928 void *pUnused, /* Unused */
112929 int argc, /* Number of elements in argv array */
112930 const char * const *argv, /* xCreate/xConnect argument array */
112931 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
112932 char **pzErr /* OUT: sqlite3_malloc'd error message */
112933 ){
112934 char const *zDb; /* Name of database (e.g. "main") */
112935 char const *zFts3; /* Name of fts3 table */
112936 int nDb; /* Result of strlen(zDb) */
112937 int nFts3; /* Result of strlen(zFts3) */
112938 int nByte; /* Bytes of space to allocate here */
112939 int rc; /* value returned by declare_vtab() */
112940 Fts3auxTable *p; /* Virtual table object to return */
112941
112942 /* The user should specify a single argument - the name of an fts3 table. */
112943 if( argc!=4 ){
112944 *pzErr = sqlite3_mprintf(
112945 "wrong number of arguments to fts4aux constructor"
112946 );
112947 return SQLITE_ERROR;
112948 }
112949
112950 zDb = argv[1];
112951 nDb = strlen(zDb);
112952 zFts3 = argv[3];
112953 nFts3 = strlen(zFts3);
112954
112955 rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
112956 if( rc!=SQLITE_OK ) return rc;
112957
112958 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
112959 p = (Fts3auxTable *)sqlite3_malloc(nByte);
112960 if( !p ) return SQLITE_NOMEM;
112961 memset(p, 0, nByte);
112962
112963 p->pFts3Tab = (Fts3Table *)&p[1];
112964 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
112965 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
112966 p->pFts3Tab->db = db;
112967
112968 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
112969 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
112970 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
112971
112972 *ppVtab = (sqlite3_vtab *)p;
112973 return SQLITE_OK;
112974 }
112975
112976 /*
112977 ** This function does the work for both the xDisconnect and xDestroy methods.
112978 ** These tables have no persistent representation of their own, so xDisconnect
112979 ** and xDestroy are identical operations.
112980 */
112981 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
112982 Fts3auxTable *p = (Fts3auxTable *)pVtab;
112983 Fts3Table *pFts3 = p->pFts3Tab;
112984 int i;
112985
112986 /* Free any prepared statements held */
112987 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
112988 sqlite3_finalize(pFts3->aStmt[i]);
112989 }
112990 sqlite3_free(pFts3->zSegmentsTbl);
112991 sqlite3_free(p);
112992 return SQLITE_OK;
112993 }
112994
112995 #define FTS4AUX_EQ_CONSTRAINT 1
112996 #define FTS4AUX_GE_CONSTRAINT 2
112997 #define FTS4AUX_LE_CONSTRAINT 4
112998
112999 /*
113000 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
113001 */
113002 static int fts3auxBestIndexMethod(
113003 sqlite3_vtab *pVTab,
113004 sqlite3_index_info *pInfo
113005 ){
113006 int i;
113007 int iEq = -1;
113008 int iGe = -1;
113009 int iLe = -1;
113010
113011 /* This vtab delivers always results in "ORDER BY term ASC" order. */
113012 if( pInfo->nOrderBy==1
113013 && pInfo->aOrderBy[0].iColumn==0
113014 && pInfo->aOrderBy[0].desc==0
113015 ){
113016 pInfo->orderByConsumed = 1;
113017 }
113018
113019 /* Search for equality and range constraints on the "term" column. */
113020 for(i=0; i<pInfo->nConstraint; i++){
113021 if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
113022 int op = pInfo->aConstraint[i].op;
113023 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
113024 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
113025 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
113026 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
113027 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
113028 }
113029 }
113030
113031 if( iEq>=0 ){
113032 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
113033 pInfo->aConstraintUsage[iEq].argvIndex = 1;
113034 pInfo->estimatedCost = 5;
113035 }else{
113036 pInfo->idxNum = 0;
113037 pInfo->estimatedCost = 20000;
113038 if( iGe>=0 ){
113039 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
113040 pInfo->aConstraintUsage[iGe].argvIndex = 1;
113041 pInfo->estimatedCost /= 2;
113042 }
113043 if( iLe>=0 ){
113044 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
113045 pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
113046 pInfo->estimatedCost /= 2;
113047 }
113048 }
113049
113050 return SQLITE_OK;
113051 }
113052
113053 /*
113054 ** xOpen - Open a cursor.
113055 */
113056 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
113057 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
113058
113059 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
113060 if( !pCsr ) return SQLITE_NOMEM;
113061 memset(pCsr, 0, sizeof(Fts3auxCursor));
113062
113063 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
113064 return SQLITE_OK;
113065 }
113066
113067 /*
113068 ** xClose - Close a cursor.
113069 */
113070 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
113071 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113072 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113073
113074 sqlite3Fts3SegmentsClose(pFts3);
113075 sqlite3Fts3SegReaderFinish(&pCsr->csr);
113076 sqlite3_free((void *)pCsr->filter.zTerm);
113077 sqlite3_free(pCsr->zStop);
113078 sqlite3_free(pCsr->aStat);
113079 sqlite3_free(pCsr);
113080 return SQLITE_OK;
113081 }
113082
113083 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
113084 if( nSize>pCsr->nStat ){
113085 struct Fts3auxColstats *aNew;
113086 aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
113087 sizeof(struct Fts3auxColstats) * nSize
113088 );
113089 if( aNew==0 ) return SQLITE_NOMEM;
113090 memset(&aNew[pCsr->nStat], 0,
113091 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
113092 );
113093 pCsr->aStat = aNew;
113094 pCsr->nStat = nSize;
113095 }
113096 return SQLITE_OK;
113097 }
113098
113099 /*
113100 ** xNext - Advance the cursor to the next row, if any.
113101 */
113102 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
113103 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113104 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113105 int rc;
113106
113107 /* Increment our pretend rowid value. */
113108 pCsr->iRowid++;
113109
113110 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
113111 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
113112 }
113113
113114 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
113115 if( rc==SQLITE_ROW ){
113116 int i = 0;
113117 int nDoclist = pCsr->csr.nDoclist;
113118 char *aDoclist = pCsr->csr.aDoclist;
113119 int iCol;
113120
113121 int eState = 0;
113122
113123 if( pCsr->zStop ){
113124 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
113125 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
113126 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
113127 pCsr->isEof = 1;
113128 return SQLITE_OK;
113129 }
113130 }
113131
113132 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
113133 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
113134 iCol = 0;
113135
113136 while( i<nDoclist ){
113137 sqlite3_int64 v = 0;
113138
113139 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
113140 switch( eState ){
113141 /* State 0. In this state the integer just read was a docid. */
113142 case 0:
113143 pCsr->aStat[0].nDoc++;
113144 eState = 1;
113145 iCol = 0;
113146 break;
113147
113148 /* State 1. In this state we are expecting either a 1, indicating
113149 ** that the following integer will be a column number, or the
113150 ** start of a position list for column 0.
113151 **
113152 ** The only difference between state 1 and state 2 is that if the
113153 ** integer encountered in state 1 is not 0 or 1, then we need to
113154 ** increment the column 0 "nDoc" count for this term.
113155 */
113156 case 1:
113157 assert( iCol==0 );
113158 if( v>1 ){
113159 pCsr->aStat[1].nDoc++;
113160 }
113161 eState = 2;
113162 /* fall through */
113163
113164 case 2:
113165 if( v==0 ){ /* 0x00. Next integer will be a docid. */
113166 eState = 0;
113167 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
113168 eState = 3;
113169 }else{ /* 2 or greater. A position. */
113170 pCsr->aStat[iCol+1].nOcc++;
113171 pCsr->aStat[0].nOcc++;
113172 }
113173 break;
113174
113175 /* State 3. The integer just read is a column number. */
113176 default: assert( eState==3 );
113177 iCol = (int)v;
113178 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
113179 pCsr->aStat[iCol+1].nDoc++;
113180 eState = 2;
113181 break;
113182 }
113183 }
113184
113185 pCsr->iCol = 0;
113186 rc = SQLITE_OK;
113187 }else{
113188 pCsr->isEof = 1;
113189 }
113190 return rc;
113191 }
113192
113193 /*
113194 ** xFilter - Initialize a cursor to point at the start of its data.
113195 */
113196 static int fts3auxFilterMethod(
113197 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
113198 int idxNum, /* Strategy index */
113199 const char *idxStr, /* Unused */
113200 int nVal, /* Number of elements in apVal */
113201 sqlite3_value **apVal /* Arguments for the indexing scheme */
113202 ){
113203 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113204 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
113205 int rc;
113206 int isScan;
113207
113208 assert( idxStr==0 );
113209 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
113210 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
113211 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
113212 );
113213 isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
113214
113215 /* In case this cursor is being reused, close and zero it. */
113216 testcase(pCsr->filter.zTerm);
113217 sqlite3Fts3SegReaderFinish(&pCsr->csr);
113218 sqlite3_free((void *)pCsr->filter.zTerm);
113219 sqlite3_free(pCsr->aStat);
113220 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
113221
113222 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
113223 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
113224
113225 if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
113226 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
113227 if( zStr ){
113228 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
113229 pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
113230 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
113231 }
113232 }
113233 if( idxNum&FTS4AUX_LE_CONSTRAINT ){
113234 int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
113235 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
113236 pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
113237 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
113238 }
113239
113240 rc = sqlite3Fts3SegReaderCursor(pFts3, FTS3_SEGCURSOR_ALL,
113241 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
113242 );
113243 if( rc==SQLITE_OK ){
113244 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
113245 }
113246
113247 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
113248 return rc;
113249 }
113250
113251 /*
113252 ** xEof - Return true if the cursor is at EOF, or false otherwise.
113253 */
113254 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
113255 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113256 return pCsr->isEof;
113257 }
113258
113259 /*
113260 ** xColumn - Return a column value.
113261 */
113262 static int fts3auxColumnMethod(
113263 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
113264 sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */
113265 int iCol /* Index of column to read value from */
113266 ){
113267 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
113268
113269 assert( p->isEof==0 );
113270 if( iCol==0 ){ /* Column "term" */
113271 sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
113272 }else if( iCol==1 ){ /* Column "col" */
113273 if( p->iCol ){
113274 sqlite3_result_int(pContext, p->iCol-1);
113275 }else{
113276 sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
113277 }
113278 }else if( iCol==2 ){ /* Column "documents" */
113279 sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
113280 }else{ /* Column "occurrences" */
113281 sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
113282 }
113283
113284 return SQLITE_OK;
113285 }
113286
113287 /*
113288 ** xRowid - Return the current rowid for the cursor.
113289 */
113290 static int fts3auxRowidMethod(
113291 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
113292 sqlite_int64 *pRowid /* OUT: Rowid value */
113293 ){
113294 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
113295 *pRowid = pCsr->iRowid;
113296 return SQLITE_OK;
113297 }
113298
113299 /*
113300 ** Register the fts3aux module with database connection db. Return SQLITE_OK
113301 ** if successful or an error code if sqlite3_create_module() fails.
113302 */
113303 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
113304 static const sqlite3_module fts3aux_module = {
113305 0, /* iVersion */
113306 fts3auxConnectMethod, /* xCreate */
113307 fts3auxConnectMethod, /* xConnect */
113308 fts3auxBestIndexMethod, /* xBestIndex */
113309 fts3auxDisconnectMethod, /* xDisconnect */
113310 fts3auxDisconnectMethod, /* xDestroy */
113311 fts3auxOpenMethod, /* xOpen */
113312 fts3auxCloseMethod, /* xClose */
113313 fts3auxFilterMethod, /* xFilter */
113314 fts3auxNextMethod, /* xNext */
113315 fts3auxEofMethod, /* xEof */
113316 fts3auxColumnMethod, /* xColumn */
113317 fts3auxRowidMethod, /* xRowid */
113318 0, /* xUpdate */
113319 0, /* xBegin */
113320 0, /* xSync */
113321 0, /* xCommit */
113322 0, /* xRollback */
113323 0, /* xFindFunction */
113324 0 /* xRename */
113325 };
113326 int rc; /* Return code */
113327
113328 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
113329 return rc;
113330 }
113331
113332 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
113333
113334 /************** End of fts3_aux.c ********************************************/
113335 /************** Begin file fts3_expr.c ***************************************/
113336 /*
113337 ** 2008 Nov 28
113338 **
113339 ** The author disclaims copyright to this source code. In place of
@@ -114956,11 +116223,11 @@
116223 /* 2 */ "DELETE FROM %Q.'%q_content'",
116224 /* 3 */ "DELETE FROM %Q.'%q_segments'",
116225 /* 4 */ "DELETE FROM %Q.'%q_segdir'",
116226 /* 5 */ "DELETE FROM %Q.'%q_docsize'",
116227 /* 6 */ "DELETE FROM %Q.'%q_stat'",
116228 /* 7 */ "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?",
116229 /* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
116230 /* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
116231 /* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
116232 /* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
116233
@@ -114973,11 +116240,11 @@
116240 /* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
116241 /* 15 */ "SELECT count(*), max(level) FROM %Q.'%q_segdir'",
116242
116243 /* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
116244 /* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
116245 /* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
116246 /* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
116247 /* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
116248 /* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
116249 /* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=0",
116250 /* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
@@ -114990,24 +116257,13 @@
116257
116258 pStmt = p->aStmt[eStmt];
116259 if( !pStmt ){
116260 char *zSql;
116261 if( eStmt==SQL_CONTENT_INSERT ){
116262 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
116263 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
116264 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName);
 
 
 
 
 
 
 
 
 
 
 
116265 }else{
116266 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
116267 }
116268 if( !zSql ){
116269 rc = SQLITE_NOMEM;
@@ -115044,11 +116300,11 @@
116300 if( rc==SQLITE_OK ){
116301 if( eStmt==SQL_SELECT_DOCSIZE ){
116302 sqlite3_bind_int64(pStmt, 1, iDocid);
116303 }
116304 rc = sqlite3_step(pStmt);
116305 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
116306 rc = sqlite3_reset(pStmt);
116307 if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT;
116308 pStmt = 0;
116309 }else{
116310 rc = SQLITE_OK;
@@ -115145,12 +116401,21 @@
116401 ** 1: start_block
116402 ** 2: leaves_end_block
116403 ** 3: end_block
116404 ** 4: root
116405 */
116406 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table *p, int iLevel, sqlite3_stmt **ppStmt){
116407 int rc;
116408 sqlite3_stmt *pStmt = 0;
116409 if( iLevel<0 ){
116410 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LEVEL, &pStmt, 0);
116411 }else{
116412 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
116413 if( rc==SQLITE_OK ) sqlite3_bind_int(pStmt, 1, iLevel);
116414 }
116415 *ppStmt = pStmt;
116416 return rc;
116417 }
116418
116419
116420 /*
116421 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
@@ -115848,20 +117113,22 @@
117113 ** to right.
117114 */
117115 sqlite3_stmt *pStmt;
117116 sqlite3_int64 nDoc = 0;
117117 sqlite3_int64 nByte = 0;
117118 const char *pEnd;
117119 const char *a;
117120
117121 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
117122 if( rc!=SQLITE_OK ) return rc;
117123 a = sqlite3_column_blob(pStmt, 0);
117124 assert( a );
117125
117126 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
117127 a += sqlite3Fts3GetVarint(a, &nDoc);
117128 while( a<pEnd ){
117129 a += sqlite3Fts3GetVarint(a, &nByte);
117130 }
117131 if( nDoc==0 || nByte==0 ){
117132 sqlite3_reset(pStmt);
117133 return SQLITE_CORRUPT;
117134 }
@@ -116047,46 +117314,10 @@
117314 }
117315 *ppReader = pReader;
117316 return rc;
117317 }
117318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117319 /*
117320 ** Compare the entries pointed to by two Fts3SegReader structures.
117321 ** Comparison is as follows:
117322 **
117323 ** 1) EOF is greater than not EOF.
@@ -116687,29 +117918,10 @@
117918 rc = sqlite3_reset(pStmt);
117919 }
117920 return rc;
117921 }
117922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117923 /*
117924 ** Set *pnSegment to the total number of segments in the database. Set
117925 ** *pnMax to the largest segment level in the database (segment levels
117926 ** are stored in the 'level' column of the %_segdir table).
117927 **
@@ -116764,19 +117976,22 @@
117976 }
117977 if( rc!=SQLITE_OK ){
117978 return rc;
117979 }
117980
117981 if( iLevel==FTS3_SEGCURSOR_ALL ){
117982 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
117983 }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
117984 sqlite3Fts3PendingTermsClear(p);
117985 }else{
117986 assert( iLevel>=0 );
117987 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_BY_LEVEL, &pDelete, 0);
117988 if( rc==SQLITE_OK ){
117989 sqlite3_bind_int(pDelete, 1, iLevel);
117990 sqlite3_step(pDelete);
117991 rc = sqlite3_reset(pDelete);
117992 }
 
 
117993 }
117994
117995 return rc;
117996 }
117997
@@ -116821,148 +118036,117 @@
118036
118037 *ppList = pList;
118038 *pnList = nList;
118039 }
118040
118041 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118042 Fts3Table *p, /* Virtual table handle */
118043 Fts3SegReaderCursor *pCsr, /* Cursor object */
118044 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
 
 
 
118045 ){
118046 int i;
118047
118048 /* Initialize the cursor object */
118049 pCsr->pFilter = pFilter;
 
 
 
 
 
 
 
 
 
 
118050
118051 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
118052 ** for, then advance each segment iterator until it points to a term of
118053 ** equal or greater value than the specified term. This prevents many
118054 ** unnecessary merge/sort operations for the case where single segment
118055 ** b-tree leaf nodes contain more than one term.
118056 */
118057 for(i=0; i<pCsr->nSegment; i++){
118058 int nTerm = pFilter->nTerm;
118059 const char *zTerm = pFilter->zTerm;
118060 Fts3SegReader *pSeg = pCsr->apSegment[i];
118061 do {
118062 int rc = fts3SegReaderNext(p, pSeg);
118063 if( rc!=SQLITE_OK ) return rc;
118064 }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
118065 }
118066 fts3SegReaderSort(
118067 pCsr->apSegment, pCsr->nSegment, pCsr->nSegment, fts3SegReaderCmp);
118068
118069 return SQLITE_OK;
118070 }
118071
118072 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
118073 Fts3Table *p, /* Virtual table handle */
118074 Fts3SegReaderCursor *pCsr /* Cursor object */
118075 ){
118076 int rc = SQLITE_OK;
118077
118078 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
118079 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
118080 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
118081 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
118082 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
118083
118084 Fts3SegReader **apSegment = pCsr->apSegment;
118085 int nSegment = pCsr->nSegment;
118086 Fts3SegFilter *pFilter = pCsr->pFilter;
118087
118088 if( pCsr->nSegment==0 ) return SQLITE_OK;
118089
118090 do {
118091 int nMerge;
118092 int i;
118093
118094 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
118095 ** forward. Then sort the list in order of current term again.
118096 */
118097 for(i=0; i<pCsr->nAdvance; i++){
118098 rc = fts3SegReaderNext(p, apSegment[i]);
118099 if( rc!=SQLITE_OK ) return rc;
118100 }
118101 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
118102 pCsr->nAdvance = 0;
118103
118104 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
118105 assert( rc==SQLITE_OK );
118106 if( apSegment[0]->aNode==0 ) break;
118107
118108 pCsr->nTerm = apSegment[0]->nTerm;
118109 pCsr->zTerm = apSegment[0]->zTerm;
118110
118111 /* If this is a prefix-search, and if the term that apSegment[0] points
118112 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
118113 ** required callbacks have been made. In this case exit early.
118114 **
118115 ** Similarly, if this is a search for an exact match, and the first term
118116 ** of segment apSegment[0] is not a match, exit early.
118117 */
118118 if( pFilter->zTerm && !isScan ){
118119 if( pCsr->nTerm<pFilter->nTerm
118120 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
118121 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
118122 ){
118123 break;
118124 }
118125 }
118126
118127 nMerge = 1;
118128 while( nMerge<nSegment
118129 && apSegment[nMerge]->aNode
118130 && apSegment[nMerge]->nTerm==pCsr->nTerm
118131 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
118132 ){
118133 nMerge++;
118134 }
118135
118136 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
118137 if( nMerge==1 && !isIgnoreEmpty ){
118138 pCsr->aDoclist = apSegment[0]->aDoclist;
118139 pCsr->nDoclist = apSegment[0]->nDoclist;
118140 rc = SQLITE_ROW;
118141 }else{
118142 int nDoclist = 0; /* Size of doclist */
118143 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
118144
118145 /* The current term of the first nMerge entries in the array
118146 ** of Fts3SegReader objects is the same. The doclists must be merged
118147 ** and a single term returned with the merged doclist.
118148 */
118149 for(i=0; i<nMerge; i++){
118150 fts3SegReaderFirstDocid(apSegment[i]);
118151 }
118152 fts3SegReaderSort(apSegment, nMerge, nMerge, fts3SegReaderDoclistCmp);
@@ -116986,57 +118170,60 @@
118170 fts3ColumnFilter(pFilter->iCol, &pList, &nList);
118171 }
118172
118173 if( !isIgnoreEmpty || nList>0 ){
118174 nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0);
118175 if( nDoclist+nByte>pCsr->nBuffer ){
118176 char *aNew;
118177 pCsr->nBuffer = (nDoclist+nByte)*2;
118178 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
118179 if( !aNew ){
118180 return SQLITE_NOMEM;
 
118181 }
118182 pCsr->aBuffer = aNew;
118183 }
118184 nDoclist += sqlite3Fts3PutVarint(
118185 &pCsr->aBuffer[nDoclist], iDocid-iPrev
118186 );
118187 iPrev = iDocid;
118188 if( isRequirePos ){
118189 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
118190 nDoclist += nList;
118191 pCsr->aBuffer[nDoclist++] = '\0';
118192 }
118193 }
118194
118195 fts3SegReaderSort(apSegment, nMerge, j, fts3SegReaderDoclistCmp);
118196 }
 
118197 if( nDoclist>0 ){
118198 pCsr->aDoclist = pCsr->aBuffer;
118199 pCsr->nDoclist = nDoclist;
118200 rc = SQLITE_ROW;
118201 }
118202 }
118203 pCsr->nAdvance = nMerge;
118204 }while( rc==SQLITE_OK );
118205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118206 return rc;
118207 }
118208
118209 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
118210 Fts3SegReaderCursor *pCsr /* Cursor object */
118211 ){
118212 if( pCsr ){
118213 int i;
118214 for(i=0; i<pCsr->nSegment; i++){
118215 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
118216 }
118217 sqlite3_free(pCsr->apSegment);
118218 sqlite3_free(pCsr->aBuffer);
118219
118220 pCsr->nSegment = 0;
118221 pCsr->apSegment = 0;
118222 pCsr->aBuffer = 0;
118223 }
118224 }
118225
118226 /*
118227 ** Merge all level iLevel segments in the database into a single
118228 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
118229 ** single segment with a level equal to the numerically largest level
@@ -117046,161 +118233,73 @@
118233 ** segment in the database, SQLITE_DONE is returned immediately.
118234 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
118235 ** an SQLite error code is returned.
118236 */
118237 static int fts3SegmentMerge(Fts3Table *p, int iLevel){
 
118238 int rc; /* Return code */
118239 int iIdx = 0; /* Index of new segment */
118240 int iNewLevel = 0; /* Level to create new segment at */
118241 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
 
 
 
 
118242 Fts3SegFilter filter; /* Segment term filter condition */
118243 Fts3SegReaderCursor csr; /* Cursor to iterate through level(s) */
118244
118245 rc = sqlite3Fts3SegReaderCursor(p, iLevel, 0, 0, 1, 0, &csr);
118246 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
118247
118248 if( iLevel==FTS3_SEGCURSOR_ALL ){
118249 /* This call is to merge all segments in the database to a single
118250 ** segment. The level of the new segment is equal to the the numerically
118251 ** greatest segment level currently present in the database. The index
118252 ** of the new segment is always 0. */
118253 int nDummy; /* TODO: Remove this */
118254 if( csr.nSegment==1 ){
118255 rc = SQLITE_DONE;
118256 goto finished;
118257 }
118258 rc = fts3SegmentCountMax(p, &nDummy, &iNewLevel);
 
 
 
 
118259 }else{
118260 /* This call is to merge all segments at level iLevel. Find the next
118261 ** available segment index at level iLevel+1. The call to
118262 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
118263 ** a single iLevel+2 segment if necessary. */
 
118264 iNewLevel = iLevel+1;
118265 rc = fts3AllocateSegdirIdx(p, iNewLevel, &iIdx);
118266 }
118267 if( rc!=SQLITE_OK ) goto finished;
118268 assert( csr.nSegment>0 );
118269 assert( iNewLevel>=0 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118270
118271 memset(&filter, 0, sizeof(Fts3SegFilter));
118272 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
118273 filter.flags |= (iLevel==FTS3_SEGCURSOR_ALL ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
118274
118275 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
118276 while( SQLITE_OK==rc ){
118277 rc = sqlite3Fts3SegReaderStep(p, &csr);
118278 if( rc!=SQLITE_ROW ) break;
118279 rc = fts3SegWriterAdd(p, &pWriter, 1,
118280 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
118281 }
118282 if( rc!=SQLITE_OK ) goto finished;
118283 assert( pWriter );
118284
118285 rc = fts3DeleteSegdir(p, iLevel, csr.apSegment, csr.nSegment);
118286 if( rc!=SQLITE_OK ) goto finished;
118287 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
 
 
 
 
118288
118289 finished:
118290 fts3SegWriterFree(pWriter);
118291 sqlite3Fts3SegReaderFinish(&csr);
 
 
 
 
 
 
 
118292 return rc;
118293 }
118294
118295
118296 /*
118297 ** Flush the contents of pendingTerms to a level 0 segment.
118298 */
118299 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
118300 return fts3SegmentMerge(p, FTS3_SEGCURSOR_PENDING);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118301 }
118302
118303 /*
118304 ** Encode N integers as varints into a blob.
118305 */
@@ -117363,11 +118462,11 @@
118462 int nVal = sqlite3_value_bytes(pVal);
118463
118464 if( !zVal ){
118465 return SQLITE_NOMEM;
118466 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
118467 rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
118468 if( rc==SQLITE_DONE ){
118469 rc = SQLITE_OK;
118470 }else{
118471 sqlite3Fts3PendingTermsClear(p);
118472 }
@@ -117621,11 +118720,11 @@
118720 */
118721 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
118722 int rc;
118723 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
118724 if( rc==SQLITE_OK ){
118725 rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL);
118726 if( rc==SQLITE_OK ){
118727 rc = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
118728 if( rc==SQLITE_OK ){
118729 sqlite3Fts3PendingTermsClear(p);
118730 }
@@ -118599,10 +119698,11 @@
119698 pStmt = *ppStmt;
119699 assert( sqlite3_data_count(pStmt)==1 );
119700
119701 a = sqlite3_column_blob(pStmt, 0);
119702 a += sqlite3Fts3GetVarint(a, &nDoc);
119703 if( nDoc==0 ) return SQLITE_CORRUPT;
119704 *pnDoc = (u32)nDoc;
119705
119706 if( paLen ) *paLen = a;
119707 return SQLITE_OK;
119708 }
@@ -118805,13 +119905,15 @@
119905
119906 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
119907 if( rc==SQLITE_OK ){
119908 int iCol;
119909 for(iCol=0; iCol<pInfo->nCol; iCol++){
119910 u32 iVal;
119911 sqlite3_int64 nToken;
119912 a += sqlite3Fts3GetVarint(a, &nToken);
119913 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
119914 pInfo->aMatchinfo[iCol] = iVal;
119915 }
119916 }
119917 }
119918 break;
119919
119920
+104 -12
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.7.5"
111
-#define SQLITE_VERSION_NUMBER 3007005
112
-#define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
110
+#define SQLITE_VERSION "3.7.6"
111
+#define SQLITE_VERSION_NUMBER 3007006
112
+#define SQLITE_SOURCE_ID "2011-02-16 23:32:24 31fc4ba66e76876b2e7b6b2b74c07f47571938ce"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -388,11 +388,11 @@
388388
#define SQLITE_NOMEM 7 /* A malloc() failed */
389389
#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
390390
#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
391391
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392392
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393
-#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
393
+#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
394394
#define SQLITE_FULL 13 /* Insertion failed because database is full */
395395
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396396
#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
397397
#define SQLITE_EMPTY 16 /* Database is empty */
398398
#define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -620,11 +620,13 @@
620620
** locking strategy (for example to use dot-file locks), to inquire
621621
** about the status of a lock, or to break stale locks. The SQLite
622622
** core reserves all opcodes less than 100 for its own use.
623623
** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
624624
** Applications that define a custom xFileControl method should use opcodes
625
-** greater than 100 to avoid conflicts.
625
+** greater than 100 to avoid conflicts. VFS implementations should
626
+** return [SQLITE_NOTFOUND] for file control opcodes that they do not
627
+** recognize.
626628
**
627629
** The xSectorSize() method returns the sector size of the
628630
** device that underlies the file. The sector size is the
629631
** minimum write that can be performed without disturbing
630632
** other bytes in the file. The xDeviceCharacteristics()
@@ -2667,11 +2669,11 @@
26672669
SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
26682670
26692671
/*
26702672
** CAPI3REF: Determine If An SQL Statement Writes The Database
26712673
**
2672
-** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2674
+** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
26732675
** and only if the [prepared statement] X makes no direct changes to
26742676
** the content of the database file.
26752677
**
26762678
** Note that [application-defined SQL functions] or
26772679
** [virtual tables] might change the database indirectly as a side effect.
@@ -5535,28 +5537,25 @@
55355537
** checked out.</dd>)^
55365538
**
55375539
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
55385540
** <dd>This parameter returns the number malloc attempts that were
55395541
** satisfied using lookaside memory. Only the high-water value is meaningful;
5540
-** the current value is always zero.
5541
-** checked out.</dd>)^
5542
+** the current value is always zero.)^
55425543
**
55435544
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
55445545
** <dd>This parameter returns the number malloc attempts that might have
55455546
** been satisfied using lookaside memory but failed due to the amount of
55465547
** memory requested being larger than the lookaside slot size.
55475548
** Only the high-water value is meaningful;
5548
-** the current value is always zero.
5549
-** checked out.</dd>)^
5549
+** the current value is always zero.)^
55505550
**
55515551
** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
55525552
** <dd>This parameter returns the number malloc attempts that might have
55535553
** been satisfied using lookaside memory but failed due to all lookaside
55545554
** memory already being in use.
55555555
** Only the high-water value is meaningful;
5556
-** the current value is always zero.
5557
-** checked out.</dd>)^
5556
+** the current value is always zero.)^
55585557
**
55595558
** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
55605559
** <dd>This parameter returns the approximate number of of bytes of heap
55615560
** memory used by all pager caches associated with the database connection.)^
55625561
** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6251,12 +6250,105 @@
62516250
**
62526251
** ^The [wal_checkpoint pragma] can be used to invoke this interface
62536252
** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
62546253
** [wal_autocheckpoint pragma] can be used to cause this interface to be
62556254
** run whenever the WAL reaches a certain size threshold.
6255
+**
6256
+** See also: [sqlite3_wal_checkpoint_v2()]
62566257
*/
62576258
SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6259
+
6260
+/*
6261
+** CAPI3REF: Checkpoint a database
6262
+**
6263
+** Run a checkpoint operation on WAL database zDb attached to database
6264
+** handle db. The specific operation is determined by the value of the
6265
+** eMode parameter:
6266
+**
6267
+** <dl>
6268
+** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
6269
+** Checkpoint as many frames as possible without waiting for any database
6270
+** readers or writers to finish. Sync the db file if all frames in the log
6271
+** are checkpointed. This mode is the same as calling
6272
+** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
6273
+**
6274
+** <dt>SQLITE_CHECKPOINT_FULL<dd>
6275
+** This mode blocks (calls the busy-handler callback) until there is no
6276
+** database writer and all readers are reading from the most recent database
6277
+** snapshot. It then checkpoints all frames in the log file and syncs the
6278
+** database file. This call blocks database writers while it is running,
6279
+** but not database readers.
6280
+**
6281
+** <dt>SQLITE_CHECKPOINT_RESTART<dd>
6282
+** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
6283
+** checkpointing the log file it blocks (calls the busy-handler callback)
6284
+** until all readers are reading from the database file only. This ensures
6285
+** that the next client to write to the database file restarts the log file
6286
+** from the beginning. This call blocks database writers while it is running,
6287
+** but not database readers.
6288
+** </dl>
6289
+**
6290
+** If pnLog is not NULL, then *pnLog is set to the total number of frames in
6291
+** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
6292
+** the total number of checkpointed frames (including any that were already
6293
+** checkpointed when this function is called). *pnLog and *pnCkpt may be
6294
+** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
6295
+** If no values are available because of an error, they are both set to -1
6296
+** before returning to communicate this to the caller.
6297
+**
6298
+** All calls obtain an exclusive "checkpoint" lock on the database file. If
6299
+** any other process is running a checkpoint operation at the same time, the
6300
+** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
6301
+** busy-handler configured, it will not be invoked in this case.
6302
+**
6303
+** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
6304
+** "writer" lock on the database file. If the writer lock cannot be obtained
6305
+** immediately, and a busy-handler is configured, it is invoked and the writer
6306
+** lock retried until either the busy-handler returns 0 or the lock is
6307
+** successfully obtained. The busy-handler is also invoked while waiting for
6308
+** database readers as described above. If the busy-handler returns 0 before
6309
+** the writer lock is obtained or while waiting for database readers, the
6310
+** checkpoint operation proceeds from that point in the same way as
6311
+** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
6312
+** without blocking any further. SQLITE_BUSY is returned in this case.
6313
+**
6314
+** If parameter zDb is NULL or points to a zero length string, then the
6315
+** specified operation is attempted on all WAL databases. In this case the
6316
+** values written to output parameters *pnLog and *pnCkpt are undefined. If
6317
+** an SQLITE_BUSY error is encountered when processing one or more of the
6318
+** attached WAL databases, the operation is still attempted on any remaining
6319
+** attached databases and SQLITE_BUSY is returned to the caller. If any other
6320
+** error occurs while processing an attached database, processing is abandoned
6321
+** and the error code returned to the caller immediately. If no error
6322
+** (SQLITE_BUSY or otherwise) is encountered while processing the attached
6323
+** databases, SQLITE_OK is returned.
6324
+**
6325
+** If database zDb is the name of an attached database that is not in WAL
6326
+** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
6327
+** zDb is not NULL (or a zero length string) and is not the name of any
6328
+** attached database, SQLITE_ERROR is returned to the caller.
6329
+*/
6330
+SQLITE_API int sqlite3_wal_checkpoint_v2(
6331
+ sqlite3 *db, /* Database handle */
6332
+ const char *zDb, /* Name of attached database (or NULL) */
6333
+ int eMode, /* SQLITE_CHECKPOINT_* value */
6334
+ int *pnLog, /* OUT: Size of WAL log in frames */
6335
+ int *pnCkpt /* OUT: Total number of frames checkpointed */
6336
+);
6337
+
6338
+/*
6339
+** CAPI3REF: Checkpoint operation parameters
6340
+**
6341
+** These constants can be used as the 3rd parameter to
6342
+** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
6343
+** documentation for additional information about the meaning and use of
6344
+** each of these values.
6345
+*/
6346
+#define SQLITE_CHECKPOINT_PASSIVE 0
6347
+#define SQLITE_CHECKPOINT_FULL 1
6348
+#define SQLITE_CHECKPOINT_RESTART 2
6349
+
62586350
62596351
/*
62606352
** Undo the hack that converts floating point types to integer for
62616353
** builds on processors without floating point support.
62626354
*/
62636355
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.5"
111 #define SQLITE_VERSION_NUMBER 3007005
112 #define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -388,11 +388,11 @@
388 #define SQLITE_NOMEM 7 /* A malloc() failed */
389 #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
390 #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
391 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
394 #define SQLITE_FULL 13 /* Insertion failed because database is full */
395 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
397 #define SQLITE_EMPTY 16 /* Database is empty */
398 #define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -620,11 +620,13 @@
620 ** locking strategy (for example to use dot-file locks), to inquire
621 ** about the status of a lock, or to break stale locks. The SQLite
622 ** core reserves all opcodes less than 100 for its own use.
623 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
624 ** Applications that define a custom xFileControl method should use opcodes
625 ** greater than 100 to avoid conflicts.
 
 
626 **
627 ** The xSectorSize() method returns the sector size of the
628 ** device that underlies the file. The sector size is the
629 ** minimum write that can be performed without disturbing
630 ** other bytes in the file. The xDeviceCharacteristics()
@@ -2667,11 +2669,11 @@
2667 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
2668
2669 /*
2670 ** CAPI3REF: Determine If An SQL Statement Writes The Database
2671 **
2672 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2673 ** and only if the [prepared statement] X makes no direct changes to
2674 ** the content of the database file.
2675 **
2676 ** Note that [application-defined SQL functions] or
2677 ** [virtual tables] might change the database indirectly as a side effect.
@@ -5535,28 +5537,25 @@
5535 ** checked out.</dd>)^
5536 **
5537 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
5538 ** <dd>This parameter returns the number malloc attempts that were
5539 ** satisfied using lookaside memory. Only the high-water value is meaningful;
5540 ** the current value is always zero.
5541 ** checked out.</dd>)^
5542 **
5543 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
5544 ** <dd>This parameter returns the number malloc attempts that might have
5545 ** been satisfied using lookaside memory but failed due to the amount of
5546 ** memory requested being larger than the lookaside slot size.
5547 ** Only the high-water value is meaningful;
5548 ** the current value is always zero.
5549 ** checked out.</dd>)^
5550 **
5551 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
5552 ** <dd>This parameter returns the number malloc attempts that might have
5553 ** been satisfied using lookaside memory but failed due to all lookaside
5554 ** memory already being in use.
5555 ** Only the high-water value is meaningful;
5556 ** the current value is always zero.
5557 ** checked out.</dd>)^
5558 **
5559 ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
5560 ** <dd>This parameter returns the approximate number of of bytes of heap
5561 ** memory used by all pager caches associated with the database connection.)^
5562 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6251,12 +6250,105 @@
6251 **
6252 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
6253 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6254 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
6255 ** run whenever the WAL reaches a certain size threshold.
 
 
6256 */
6257 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6258
6259 /*
6260 ** Undo the hack that converts floating point types to integer for
6261 ** builds on processors without floating point support.
6262 */
6263
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.6"
111 #define SQLITE_VERSION_NUMBER 3007006
112 #define SQLITE_SOURCE_ID "2011-02-16 23:32:24 31fc4ba66e76876b2e7b6b2b74c07f47571938ce"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -388,11 +388,11 @@
388 #define SQLITE_NOMEM 7 /* A malloc() failed */
389 #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
390 #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
391 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393 #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
394 #define SQLITE_FULL 13 /* Insertion failed because database is full */
395 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
397 #define SQLITE_EMPTY 16 /* Database is empty */
398 #define SQLITE_SCHEMA 17 /* The database schema changed */
@@ -620,11 +620,13 @@
620 ** locking strategy (for example to use dot-file locks), to inquire
621 ** about the status of a lock, or to break stale locks. The SQLite
622 ** core reserves all opcodes less than 100 for its own use.
623 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
624 ** Applications that define a custom xFileControl method should use opcodes
625 ** greater than 100 to avoid conflicts. VFS implementations should
626 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
627 ** recognize.
628 **
629 ** The xSectorSize() method returns the sector size of the
630 ** device that underlies the file. The sector size is the
631 ** minimum write that can be performed without disturbing
632 ** other bytes in the file. The xDeviceCharacteristics()
@@ -2667,11 +2669,11 @@
2669 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
2670
2671 /*
2672 ** CAPI3REF: Determine If An SQL Statement Writes The Database
2673 **
2674 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2675 ** and only if the [prepared statement] X makes no direct changes to
2676 ** the content of the database file.
2677 **
2678 ** Note that [application-defined SQL functions] or
2679 ** [virtual tables] might change the database indirectly as a side effect.
@@ -5535,28 +5537,25 @@
5537 ** checked out.</dd>)^
5538 **
5539 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
5540 ** <dd>This parameter returns the number malloc attempts that were
5541 ** satisfied using lookaside memory. Only the high-water value is meaningful;
5542 ** the current value is always zero.)^
 
5543 **
5544 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
5545 ** <dd>This parameter returns the number malloc attempts that might have
5546 ** been satisfied using lookaside memory but failed due to the amount of
5547 ** memory requested being larger than the lookaside slot size.
5548 ** Only the high-water value is meaningful;
5549 ** the current value is always zero.)^
 
5550 **
5551 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
5552 ** <dd>This parameter returns the number malloc attempts that might have
5553 ** been satisfied using lookaside memory but failed due to all lookaside
5554 ** memory already being in use.
5555 ** Only the high-water value is meaningful;
5556 ** the current value is always zero.)^
 
5557 **
5558 ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
5559 ** <dd>This parameter returns the approximate number of of bytes of heap
5560 ** memory used by all pager caches associated with the database connection.)^
5561 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
@@ -6251,12 +6250,105 @@
6250 **
6251 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
6252 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6253 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
6254 ** run whenever the WAL reaches a certain size threshold.
6255 **
6256 ** See also: [sqlite3_wal_checkpoint_v2()]
6257 */
6258 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6259
6260 /*
6261 ** CAPI3REF: Checkpoint a database
6262 **
6263 ** Run a checkpoint operation on WAL database zDb attached to database
6264 ** handle db. The specific operation is determined by the value of the
6265 ** eMode parameter:
6266 **
6267 ** <dl>
6268 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
6269 ** Checkpoint as many frames as possible without waiting for any database
6270 ** readers or writers to finish. Sync the db file if all frames in the log
6271 ** are checkpointed. This mode is the same as calling
6272 ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
6273 **
6274 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
6275 ** This mode blocks (calls the busy-handler callback) until there is no
6276 ** database writer and all readers are reading from the most recent database
6277 ** snapshot. It then checkpoints all frames in the log file and syncs the
6278 ** database file. This call blocks database writers while it is running,
6279 ** but not database readers.
6280 **
6281 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
6282 ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
6283 ** checkpointing the log file it blocks (calls the busy-handler callback)
6284 ** until all readers are reading from the database file only. This ensures
6285 ** that the next client to write to the database file restarts the log file
6286 ** from the beginning. This call blocks database writers while it is running,
6287 ** but not database readers.
6288 ** </dl>
6289 **
6290 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
6291 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
6292 ** the total number of checkpointed frames (including any that were already
6293 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
6294 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
6295 ** If no values are available because of an error, they are both set to -1
6296 ** before returning to communicate this to the caller.
6297 **
6298 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
6299 ** any other process is running a checkpoint operation at the same time, the
6300 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
6301 ** busy-handler configured, it will not be invoked in this case.
6302 **
6303 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
6304 ** "writer" lock on the database file. If the writer lock cannot be obtained
6305 ** immediately, and a busy-handler is configured, it is invoked and the writer
6306 ** lock retried until either the busy-handler returns 0 or the lock is
6307 ** successfully obtained. The busy-handler is also invoked while waiting for
6308 ** database readers as described above. If the busy-handler returns 0 before
6309 ** the writer lock is obtained or while waiting for database readers, the
6310 ** checkpoint operation proceeds from that point in the same way as
6311 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
6312 ** without blocking any further. SQLITE_BUSY is returned in this case.
6313 **
6314 ** If parameter zDb is NULL or points to a zero length string, then the
6315 ** specified operation is attempted on all WAL databases. In this case the
6316 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
6317 ** an SQLITE_BUSY error is encountered when processing one or more of the
6318 ** attached WAL databases, the operation is still attempted on any remaining
6319 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
6320 ** error occurs while processing an attached database, processing is abandoned
6321 ** and the error code returned to the caller immediately. If no error
6322 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
6323 ** databases, SQLITE_OK is returned.
6324 **
6325 ** If database zDb is the name of an attached database that is not in WAL
6326 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
6327 ** zDb is not NULL (or a zero length string) and is not the name of any
6328 ** attached database, SQLITE_ERROR is returned to the caller.
6329 */
6330 SQLITE_API int sqlite3_wal_checkpoint_v2(
6331 sqlite3 *db, /* Database handle */
6332 const char *zDb, /* Name of attached database (or NULL) */
6333 int eMode, /* SQLITE_CHECKPOINT_* value */
6334 int *pnLog, /* OUT: Size of WAL log in frames */
6335 int *pnCkpt /* OUT: Total number of frames checkpointed */
6336 );
6337
6338 /*
6339 ** CAPI3REF: Checkpoint operation parameters
6340 **
6341 ** These constants can be used as the 3rd parameter to
6342 ** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
6343 ** documentation for additional information about the meaning and use of
6344 ** each of these values.
6345 */
6346 #define SQLITE_CHECKPOINT_PASSIVE 0
6347 #define SQLITE_CHECKPOINT_FULL 1
6348 #define SQLITE_CHECKPOINT_RESTART 2
6349
6350
6351 /*
6352 ** Undo the hack that converts floating point types to integer for
6353 ** builds on processors without floating point support.
6354 */
6355
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -21,10 +21,12 @@
2121
2222
CFLAGS = -o
2323
BCC = $(DMDIR)\bin\dmc $(CFLAGS)
2424
TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
2525
LIBS = $(DMDIR)\extra\lib\ zlib wsock32
26
+
27
+SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
2628
2729
SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
2830
2931
OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
3032
@@ -62,17 +64,17 @@
6264
6365
version$E: $B\win\version.c
6466
$(BCC) -o$@ $**
6567
6668
$(OBJDIR)\shell$O : $(SRCDIR)\shell.c
67
- $(TCC) -o$@ -c -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
69
+ $(TCC) -o$@ -c -Dmain=sqlite3_shell $(SQLITE_OPTIONS) $**
6870
6971
$(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
70
- $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
72
+ $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
7173
7274
$(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
73
- $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
75
+ $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
7476
7577
$(OBJDIR)\th$O : $(SRCDIR)\th.c
7678
$(TCC) -o$@ -c $**
7779
7880
$(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
7981
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -21,10 +21,12 @@
21
22 CFLAGS = -o
23 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
24 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
25 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
 
 
26
27 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
28
29 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
30
@@ -62,17 +64,17 @@
62
63 version$E: $B\win\version.c
64 $(BCC) -o$@ $**
65
66 $(OBJDIR)\shell$O : $(SRCDIR)\shell.c
67 $(TCC) -o$@ -c -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
68
69 $(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
70 $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
71
72 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
73 $(TCC) -o$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
74
75 $(OBJDIR)\th$O : $(SRCDIR)\th.c
76 $(TCC) -o$@ -c $**
77
78 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
79
--- win/Makefile.dmc
+++ win/Makefile.dmc
@@ -21,10 +21,12 @@
21
22 CFLAGS = -o
23 BCC = $(DMDIR)\bin\dmc $(CFLAGS)
24 TCC = $(DMDIR)\bin\dmc $(CFLAGS) $(DMCDEF) $(I18N) $(SSL) $(INCL)
25 LIBS = $(DMDIR)\extra\lib\ zlib wsock32
26
27 SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
28
29 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
30
31 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\shell$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
32
@@ -62,17 +64,17 @@
64
65 version$E: $B\win\version.c
66 $(BCC) -o$@ $**
67
68 $(OBJDIR)\shell$O : $(SRCDIR)\shell.c
69 $(TCC) -o$@ -c -Dmain=sqlite3_shell $(SQLITE_OPTIONS) $**
70
71 $(OBJDIR)\sqlcmd$O : $(SRCDIR)\sqlcmd.c
72 $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
73
74 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
75 $(TCC) -o$@ -c $(SQLITE_OPTIONS) $**
76
77 $(OBJDIR)\th$O : $(SRCDIR)\th.c
78 $(TCC) -o$@ -c $**
79
80 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
81
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -918,11 +918,11 @@
918918
$(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
919919
$(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
920920
921921
zip.h: $(OBJDIR)/headers
922922
$(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
923
- $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
923
+ $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
924924
925925
$(OBJDIR)/shell.o: $(SRCDIR)/shell.c
926926
$(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
927927
928928
$(OBJDIR)/th.o: $(SRCDIR)/th.c
929929
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -918,11 +918,11 @@
918 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
919 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
920
921 zip.h: $(OBJDIR)/headers
922 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
923 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
924
925 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c
926 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
927
928 $(OBJDIR)/th.o: $(SRCDIR)/th.c
929
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -918,11 +918,11 @@
918 $(OBJDIR)/zip.o: $(OBJDIR)/zip_.c $(OBJDIR)/zip.h $(SRCDIR)/config.h
919 $(XTCC) -o $(OBJDIR)/zip.o -c $(OBJDIR)/zip_.c
920
921 zip.h: $(OBJDIR)/headers
922 $(OBJDIR)/sqlite3.o: $(SRCDIR)/sqlite3.c
923 $(XTCC) -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 -c $(SRCDIR)/sqlite3.c -o $(OBJDIR)/sqlite3.o
924
925 $(OBJDIR)/shell.o: $(SRCDIR)/shell.c
926 $(XTCC) -Dmain=sqlite3_shell -DSQLITE_OMIT_LOAD_EXTENSION=1 -c $(SRCDIR)/shell.c -o $(OBJDIR)/shell.o
927
928 $(OBJDIR)/th.o: $(SRCDIR)/th.c
929
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,10 +34,12 @@
3434
CFLAGS = -nologo -MT -O2
3535
BCC = $(CC) $(CFLAGS)
3636
TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
3737
LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
3838
LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
39
+
40
+SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
3941
4042
SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
4143
4244
OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
4345
@@ -71,11 +73,11 @@
7173
7274
version$E: $B\win\version.c
7375
$(BCC) $**
7476
7577
$(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
76
- $(TCC) /Fo$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
78
+ $(TCC) /Fo$@ -c $(SQLITE_OPTIONS) $**
7779
7880
$(OBJDIR)\th$O : $(SRCDIR)\th.c
7981
$(TCC) /Fo$@ -c $**
8082
8183
$(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
8284
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,10 +34,12 @@
34 CFLAGS = -nologo -MT -O2
35 BCC = $(CC) $(CFLAGS)
36 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
37 LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
38 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
 
 
39
40 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
41
42 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
43
@@ -71,11 +73,11 @@
71
72 version$E: $B\win\version.c
73 $(BCC) $**
74
75 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
76 $(TCC) /Fo$@ -c -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0 $**
77
78 $(OBJDIR)\th$O : $(SRCDIR)\th.c
79 $(TCC) /Fo$@ -c $**
80
81 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
82
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -34,10 +34,12 @@
34 CFLAGS = -nologo -MT -O2
35 BCC = $(CC) $(CFLAGS)
36 TCC = $(CC) -c $(CFLAGS) $(MSCDEF) $(I18N) $(SSL) $(INCL)
37 LIBS = $(ZLIB) ws2_32.lib $(SSLLIB)
38 LIBDIR = -LIBPATH:$(MSCDIR)\extra\lib -LIBPATH:$(ZLIBDIR)
39
40 SQLITE_OPTIONS = -DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_THREADSAFE=0 -DSQLITE_DEFAULT_FILE_FORMAT=4 -DSQLITE_ENABLE_STAT2 -Dlocaltime=fossil_localtime -DSQLITE_ENABLE_LOCKING_STYLE=0
41
42 SRC = add_.c allrepo_.c attach_.c bag_.c bisect_.c blob_.c branch_.c browse_.c captcha_.c cgi_.c checkin_.c checkout_.c clearsign_.c clone_.c comformat_.c configure_.c content_.c db_.c delta_.c deltacmd_.c descendants_.c diff_.c diffcmd_.c doc_.c encode_.c event_.c export_.c file_.c finfo_.c graph_.c http_.c http_socket_.c http_ssl_.c http_transport_.c import_.c info_.c leaf_.c login_.c main_.c manifest_.c md5_.c merge_.c merge3_.c name_.c pivot_.c popen_.c pqueue_.c printf_.c rebuild_.c report_.c rss_.c schema_.c search_.c setup_.c sha1_.c shun_.c skins_.c sqlcmd_.c stash_.c stat_.c style_.c sync_.c tag_.c th_main_.c timeline_.c tkt_.c tktsetup_.c undo_.c update_.c url_.c user_.c verify_.c vfile_.c wiki_.c wikiformat_.c winhttp_.c xfer_.c zip_.c
43
44 OBJ = $(OBJDIR)\add$O $(OBJDIR)\allrepo$O $(OBJDIR)\attach$O $(OBJDIR)\bag$O $(OBJDIR)\bisect$O $(OBJDIR)\blob$O $(OBJDIR)\branch$O $(OBJDIR)\browse$O $(OBJDIR)\captcha$O $(OBJDIR)\cgi$O $(OBJDIR)\checkin$O $(OBJDIR)\checkout$O $(OBJDIR)\clearsign$O $(OBJDIR)\clone$O $(OBJDIR)\comformat$O $(OBJDIR)\configure$O $(OBJDIR)\content$O $(OBJDIR)\db$O $(OBJDIR)\delta$O $(OBJDIR)\deltacmd$O $(OBJDIR)\descendants$O $(OBJDIR)\diff$O $(OBJDIR)\diffcmd$O $(OBJDIR)\doc$O $(OBJDIR)\encode$O $(OBJDIR)\event$O $(OBJDIR)\export$O $(OBJDIR)\file$O $(OBJDIR)\finfo$O $(OBJDIR)\graph$O $(OBJDIR)\http$O $(OBJDIR)\http_socket$O $(OBJDIR)\http_ssl$O $(OBJDIR)\http_transport$O $(OBJDIR)\import$O $(OBJDIR)\info$O $(OBJDIR)\leaf$O $(OBJDIR)\login$O $(OBJDIR)\main$O $(OBJDIR)\manifest$O $(OBJDIR)\md5$O $(OBJDIR)\merge$O $(OBJDIR)\merge3$O $(OBJDIR)\name$O $(OBJDIR)\pivot$O $(OBJDIR)\popen$O $(OBJDIR)\pqueue$O $(OBJDIR)\printf$O $(OBJDIR)\rebuild$O $(OBJDIR)\report$O $(OBJDIR)\rss$O $(OBJDIR)\schema$O $(OBJDIR)\search$O $(OBJDIR)\setup$O $(OBJDIR)\sha1$O $(OBJDIR)\shun$O $(OBJDIR)\skins$O $(OBJDIR)\sqlcmd$O $(OBJDIR)\stash$O $(OBJDIR)\stat$O $(OBJDIR)\style$O $(OBJDIR)\sync$O $(OBJDIR)\tag$O $(OBJDIR)\th_main$O $(OBJDIR)\timeline$O $(OBJDIR)\tkt$O $(OBJDIR)\tktsetup$O $(OBJDIR)\undo$O $(OBJDIR)\update$O $(OBJDIR)\url$O $(OBJDIR)\user$O $(OBJDIR)\verify$O $(OBJDIR)\vfile$O $(OBJDIR)\wiki$O $(OBJDIR)\wikiformat$O $(OBJDIR)\winhttp$O $(OBJDIR)\xfer$O $(OBJDIR)\zip$O $(OBJDIR)\sqlite3$O $(OBJDIR)\th$O $(OBJDIR)\th_lang$O
45
@@ -71,11 +73,11 @@
73
74 version$E: $B\win\version.c
75 $(BCC) $**
76
77 $(OBJDIR)\sqlite3$O : $(SRCDIR)\sqlite3.c
78 $(TCC) /Fo$@ -c $(SQLITE_OPTIONS) $**
79
80 $(OBJDIR)\th$O : $(SRCDIR)\th.c
81 $(TCC) /Fo$@ -c $**
82
83 $(OBJDIR)\th_lang$O : $(SRCDIR)\th_lang.c
84

Keyboard Shortcuts

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