/ Check-in [7e2c4898]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge the latest trunk changes into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1:7e2c4898224f9fabf724a6d4e1ac597845a66f73
User & Date: drh 2011-10-11 14:19:38
Context
2011-10-11
15:03
Fix a locking error introduced when porting the new Apple locking code. check-in: cce1f521 user: drh tags: apple-osx
14:19
Merge the latest trunk changes into the apple-osx branch. check-in: 7e2c4898 user: drh tags: apple-osx
12:39
Fix requirements marks associate with STAT3. check-in: 9325c1a8 user: drh tags: trunk
2011-10-10
23:53
Forward port the Apple-specific changes from [db5b7b778c] in the apple-osx-377 branch. Fix this up so that it will compile and run on Linux. check-in: 6cb43f6c user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   876    876   # If using the amalgamation, use sqlite3.c directly to build the test
   877    877   # fixture.  Otherwise link against libsqlite3.la.  (This distinction is
   878    878   # necessary because the test fixture requires non-API symbols which are
   879    879   # hidden when the library is built via the amalgamation).
   880    880   #
   881    881   TESTFIXTURE_FLAGS  = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   882    882   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE 
          883  +TESTFIXTURE_FLAGS += -DBUILD_sqlite
   883    884   
   884    885   TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlite3.la
   885    886   TESTFIXTURE_SRC1 = sqlite3.c
   886    887   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
   887    888   
   888    889   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
   889    890   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
................................................................................
   895    896   
   896    897   soaktest:	testfixture$(TEXE) sqlite3$(TEXE)
   897    898   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
   898    899   
   899    900   test:	testfixture$(TEXE) sqlite3$(TEXE)
   900    901   	./testfixture$(TEXE) $(TOP)/test/veryquick.test
   901    902   
   902         -sqlite3_analyzer$(TEXE):	$(TESTFIXTURE_SRC) $(TOP)/tool/spaceanal.tcl
   903         -	sed \
   904         -	  -e '/^#/d' \
   905         -	  -e 's,\\,\\\\,g' \
   906         -	  -e 's,",\\",g' \
   907         -	  -e 's,^,",' \
   908         -	  -e 's,$$,\\n",' \
   909         -	  $(TOP)/tool/spaceanal.tcl >spaceanal_tcl.h
   910         -	$(LTLINK) -DTCLSH=2 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 \
   911         -		-DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE \
   912         -		$(TEMP_STORE) -o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
          903  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
          904  +	echo "#define TCLSH 2" > $@
          905  +	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
          906  +	echo "static const char *tclsh_main_loop(void){" >> $@
          907  +	echo "static const char *zMainloop = " >> $@
          908  +	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
          909  +	echo "; return zMainloop; }" >> $@
   913    910   
          911  +sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
          912  +	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
   914    913   
   915    914   # Standard install and cleanup targets
   916    915   #
   917    916   lib_install:	libsqlite3.la
   918    917   	$(INSTALL) -d $(DESTDIR)$(libdir)
   919    918   	$(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir)
   920    919   	
................................................................................
   940    939   	rm -f sqlite3.h opcodes.*
   941    940   	rm -rf .libs .deps
   942    941   	rm -f lemon$(BEXE) lempar.c parse.* sqlite*.tar.gz
   943    942   	rm -f mkkeywordhash$(BEXE) keywordhash.h
   944    943   	rm -f $(PUBLISH)
   945    944   	rm -f *.da *.bb *.bbg gmon.out
   946    945   	rm -rf tsrc .target_source
          946  +	rm -f tclsqlite3$(TEXE)
   947    947   	rm -f testfixture$(TEXE) test.db
   948         -	rm -f sqlite3.dll sqlite3.lib sqlite3.def
          948  +	rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
   949    949   	rm -f sqlite3.c
          950  +	rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c
   950    951   
   951    952   distclean:	clean
   952    953   	rm -f config.log config.status libtool Makefile sqlite3.pc
   953    954   
   954    955   #
   955    956   # Windows section
   956    957   #

Changes to Makefile.msc.

     6      6   # that contains this "Makefile.msc".
     7      7   #
     8      8   TOP = .
     9      9   
    10     10   # Set this non-0 to create and use the SQLite amalgamation file.
    11     11   #
    12     12   USE_AMALGAMATION = 1
           13  +
           14  +# Set this non-0 to use the International Components for Unicode (ICU).
           15  +#
           16  +USE_ICU = 0
    13     17   
    14     18   # Set this to non-0 to create and use PDBs.
    15     19   #
    16     20   SYMBOLS = 1
    17     21   
    18     22   # Set this to one of the following values to enable various debugging
    19     23   # features.  Each level includes the debugging options from the previous
................................................................................
    27     31   # 5 == SQLITE_ENABLE_IOTRACE: Enables output from the IOTRACE() macros.
    28     32   #
    29     33   DEBUG = 0
    30     34   
    31     35   # Version numbers and release number for the SQLite being compiled.
    32     36   #
    33     37   VERSION = 3.7
    34         -VERSION_NUMBER = 3007008
    35         -RELEASE = 3.7.8
           38  +VERSION_NUMBER = 3007009
           39  +RELEASE = 3.7.9
    36     40   
    37     41   # C Compiler and options for use in building executables that
    38     42   # will run on the platform that is doing the build.
    39     43   #
    40     44   BCC = cl.exe
    41     45   
    42     46   # C Compile and options for use in building executables that
................................................................................
   104    108   !if "$(TCLLIBDIR)" == ""
   105    109   TCLLIBDIR = c:\tcl\lib
   106    110   !endif
   107    111   
   108    112   !if "$(LIBTCL)" == ""
   109    113   LIBTCL = tcl85.lib
   110    114   !endif
          115  +
          116  +# The locations of the ICU header and library files.  These variables
          117  +# (ICUINCDIR, ICULIBDIR, and LIBICU) may be overridden via the environment
          118  +# prior to running nmake in order to match the actual installed location on
          119  +# this machine.
          120  +#
          121  +!if "$(ICUINCDIR)" == ""
          122  +ICUINCDIR = c:\icu\include
          123  +!endif
          124  +
          125  +!if "$(ICULIBDIR)" == ""
          126  +ICULIBDIR = c:\icu\lib
          127  +!endif
          128  +
          129  +!if "$(LIBICU)" == ""
          130  +LIBICU = icuuc.lib icuin.lib
          131  +!endif
   111    132   
   112    133   # This is the command to use for tclsh - normally just "tclsh", but we may
   113    134   # know the specific version we want to use.  This variable (TCLSH_CMD) may be
   114    135   # overridden via the environment prior to running nmake in order to select a
   115    136   # specific Tcl shell to use.
   116    137   #
   117    138   !if "$(TCLSH_CMD)" == ""
................................................................................
   132    153   
   133    154   # Do threads override each others locks by default (1), or do we test (-1)
   134    155   #
   135    156   TCC = $(TCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1
   136    157   
   137    158   # Any target libraries which libsqlite must be linked against
   138    159   #
          160  +!if "$(TLIBS)" == ""
   139    161   TLIBS =
          162  +!endif
   140    163   
   141    164   # Flags controlling use of the in memory btree implementation
   142    165   #
   143    166   # SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
   144    167   # default to file, 2 to default to memory, and 3 to force temporary
   145    168   # tables to always be in memory.
   146    169   #
................................................................................
   174    197   !ELSE
   175    198   TCC = $(TCC) -O2
   176    199   !ENDIF
   177    200   
   178    201   !IF $(DEBUG)>0 || $(SYMBOLS)!=0
   179    202   TCC = $(TCC) -Zi
   180    203   !ENDIF
          204  +
          205  +# If ICU support is enabled, add the compiler options for it.
          206  +!IF $(USE_ICU)!=0
          207  +TCC = $(TCC) -DSQLITE_ENABLE_ICU=1
          208  +TCC = $(TCC) -I$(TOP)\ext\icu
          209  +TCC = $(TCC) -I$(ICUINCDIR)
          210  +!ENDIF
   181    211   
   182    212   # libtool compile/link
   183    213   LTCOMPILE = $(TCC) -Fo$@
   184    214   LTLIB = lib.exe
   185    215   LTLINK = $(TCC) -Fe$@
   186    216   
   187    217   # If a platform was set, force the linker to target that.
................................................................................
   193    223   LTLIBOPTS = /MACHINE:$(PLATFORM)
   194    224   !ENDIF
   195    225   
   196    226   # If debugging is enabled, enable PDBs.
   197    227   !IF $(DEBUG)>0 || $(SYMBOLS)!=0
   198    228   LTLINKOPTS = $(LTLINKOPTS) /DEBUG
   199    229   !ENDIF
          230  +
          231  +# Start with the Tcl related linker options.
          232  +LTLIBPATHS = /LIBPATH:$(TCLLIBDIR)
          233  +LTLIBS = $(LIBTCL)
          234  +
          235  +# If ICU support is enabled, add the linker options for it.
          236  +!IF $(USE_ICU)!=0
          237  +LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ICULIBDIR)
          238  +LTLIBS = $(LTLIBS) $(LIBICU)
          239  +!ENDIF
   200    240   
   201    241   # nawk compatible awk.
   202    242   NAWK = gawk.exe
   203    243   
   204    244   # You should not have to change anything below this line
   205    245   ###############################################################################
   206    246   
................................................................................
   524    564   #
   525    565   all:	dll libsqlite3.lib sqlite3.exe libtclsqlite3.lib
   526    566   
   527    567   libsqlite3.lib:	$(LIBOBJ)
   528    568   	$(LTLIB) $(LTLIBOPTS) /OUT:$@ $(LIBOBJ) $(TLIBS)
   529    569   
   530    570   libtclsqlite3.lib:	tclsqlite.lo libsqlite3.lib
   531         -	$(LTLIB) $(LTLIBOPTS) /LIBPATH:$(TCLLIBDIR) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
          571  +	$(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
   532    572   
   533    573   sqlite3.exe:	$(TOP)\src\shell.c libsqlite3.lib sqlite3.h
   534    574   	$(LTLINK) $(READLINE_FLAGS) \
   535    575   		$(TOP)\src\shell.c \
   536         -		/link $(LTLINKOPTS) libsqlite3.lib $(LIBREADLINE) $(TLIBS)
          576  +		/link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LIBREADLINE) $(LTLIBS) $(TLIBS)
   537    577   
   538    578   # This target creates a directory named "tsrc" and fills it with
   539    579   # copies of all of the C source code and header files needed to
   540    580   # build on the target system.  Some of the C source code and header
   541    581   # files are automatically generated.  This target takes care of
   542    582   # all that automatic generation.
   543    583   #
................................................................................
   800    840   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
   801    841   
   802    842   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR)
   803    843   	$(LTCOMPILE) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
   804    844   
   805    845   tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib
   806    846   	$(LTLINK) tclsqlite-shell.lo \
   807         -		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) libsqlite3.lib $(LIBTCL)
          847  +		/link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LTLIBS) $(TLIBS)
   808    848   
   809    849   # Rules to build opcodes.c and opcodes.h
   810    850   #
   811    851   opcodes.c:	opcodes.h $(TOP)\mkopcodec.awk
   812    852   	$(NAWK) -f $(TOP)\mkopcodec.awk opcodes.h > opcodes.c
   813    853   
   814    854   opcodes.h:	parse.h $(TOP)\src\vdbe.c $(TOP)\mkopcodeh.awk
................................................................................
   911    951   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC1)
   912    952   !ENDIF
   913    953   
   914    954   testfixture.exe:	$(TESTFIXTURE_SRC) $(HDR)
   915    955   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \
   916    956   		-DBUILD_sqlite -I$(TCLINCDIR) \
   917    957   		$(TESTFIXTURE_SRC) \
   918         -		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) $(LIBTCL) $(TLIBS)
          958  +		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LTLIBS) $(TLIBS)
   919    959   
   920    960   fulltest:	testfixture.exe sqlite3.exe
   921    961   	.\testfixture.exe $(TOP)\test\all.test
   922    962   
   923    963   soaktest:	testfixture.exe sqlite3.exe
   924    964   	.\testfixture.exe $(TOP)\test\all.test -soak=1
   925    965   
   926    966   test:	testfixture.exe sqlite3.exe
   927    967   	.\testfixture.exe $(TOP)\test\veryquick.test
   928    968   
   929         -spaceanal_tcl.h:	$(TOP)\tool\spaceanal.tcl
   930         -	$(NAWK) -f $(TOP)/tool/tostr.awk \
   931         -		$(TOP)\tool\spaceanal.tcl > spaceanal_tcl.h
          969  +sqlite3_analyzer.c: sqlite3.c $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
          970  +	copy sqlite3.c + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
          971  +	echo static const char *tclsh_main_loop(void){ >> $@
          972  +	echo static const char *zMainloop = >> $@
          973  +	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
          974  +	echo ; return zMainloop; } >> $@
   932    975   
   933         -sqlite3_analyzer.exe:	$(TESTFIXTURE_SRC) spaceanal_tcl.h
   934         -	$(LTLINK) -DTCLSH=2 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 \
   935         -		-DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE \
   936         -		-DBUILD_sqlite -I$(TCLINCDIR) \
   937         -		$(TESTFIXTURE_SRC) \
   938         -		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) $(LIBTCL) $(TLIBS)
          976  +sqlite3_analyzer.exe:	sqlite3_analyzer.c
          977  +	$(LTLINK) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
          978  +		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LTLIBS) $(TLIBS)
   939    979   
   940    980   clean:
   941    981   	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
   942    982   	del /Q sqlite3.h opcodes.c opcodes.h
   943    983   	del /Q lemon.exe lempar.c parse.*
   944    984   	del /Q mkkeywordhash.exe keywordhash.h
   945    985   	-rmdir /Q/S tsrc
   946    986   	del /Q .target_source
          987  +	del /Q tclsqlite3.exe
   947    988   	del /Q testfixture.exe testfixture.exp test.db
   948    989   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
   949    990   	del /Q sqlite3.c
   950         -	del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp spaceanal_tcl.h
          991  +	del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp sqlite3_analyzer.c
   951    992   
   952    993   #
   953    994   # Windows section
   954    995   #
   955    996   dll: sqlite3.dll
   956    997   
   957    998   sqlite3.def: libsqlite3.lib
   958    999   	echo EXPORTS > sqlite3.def
   959   1000   	dumpbin /all libsqlite3.lib \
   960   1001   		| $(NAWK) "/ 1 _?sqlite3_/ { sub(/^.* _?/,\"\");print }" \
   961   1002   		| sort >> sqlite3.def
   962   1003   
   963   1004   sqlite3.dll: $(LIBOBJ) sqlite3.def
   964         -	link $(LTLINKOPTS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ)
         1005  +	link $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) $(LTLIBS) $(TLIBS)

Changes to VERSION.

     1         -3.7.8
            1  +3.7.9

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.65 for sqlite 3.7.8.
            3  +# Generated by GNU Autoconf 2.67 for sqlite 3.7.9.
     4      4   #
     5      5   #
     6      6   # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
     7         -# 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation,
     8         -# Inc.
            7  +# 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
            8  +# Foundation, Inc.
     9      9   #
    10     10   #
    11     11   # This configure script is free software; the Free Software Foundation
    12     12   # gives unlimited permission to copy, distribute and modify it.
    13     13   ## -------------------- ##
    14     14   ## M4sh Initialization. ##
    15     15   ## -------------------- ##
................................................................................
   312    312   	    s//\1/
   313    313   	    q
   314    314   	  }
   315    315   	  s/.*/./; q'`
   316    316         test -d "$as_dir" && break
   317    317       done
   318    318       test -z "$as_dirs" || eval "mkdir $as_dirs"
   319         -  } || test -d "$as_dir" || as_fn_error "cannot create directory $as_dir"
          319  +  } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir"
   320    320   
   321    321   
   322    322   } # as_fn_mkdir_p
   323    323   # as_fn_append VAR VALUE
   324    324   # ----------------------
   325    325   # Append the text in VALUE to the end of the definition contained in VAR. Take
   326    326   # advantage of any shell optimizations that allow amortized linear growth over
................................................................................
   352    352     as_fn_arith ()
   353    353     {
   354    354       as_val=`expr "$@" || test $? -eq 1`
   355    355     }
   356    356   fi # as_fn_arith
   357    357   
   358    358   
   359         -# as_fn_error ERROR [LINENO LOG_FD]
   360         -# ---------------------------------
          359  +# as_fn_error STATUS ERROR [LINENO LOG_FD]
          360  +# ----------------------------------------
   361    361   # Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are
   362    362   # provided, also output the error to LOG_FD, referencing LINENO. Then exit the
   363         -# script with status $?, using 1 if that was 0.
          363  +# script with STATUS, using 1 if that was 0.
   364    364   as_fn_error ()
   365    365   {
   366         -  as_status=$?; test $as_status -eq 0 && as_status=1
   367         -  if test "$3"; then
   368         -    as_lineno=${as_lineno-"$2"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
   369         -    $as_echo "$as_me:${as_lineno-$LINENO}: error: $1" >&$3
          366  +  as_status=$1; test $as_status -eq 0 && as_status=1
          367  +  if test "$4"; then
          368  +    as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
          369  +    $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4
   370    370     fi
   371         -  $as_echo "$as_me: error: $1" >&2
          371  +  $as_echo "$as_me: error: $2" >&2
   372    372     as_fn_exit $as_status
   373    373   } # as_fn_error
   374    374   
   375    375   if expr a : '\(a\)' >/dev/null 2>&1 &&
   376    376      test "X`expr 00001 : '.*\(...\)'`" = X001; then
   377    377     as_expr=expr
   378    378   else
................................................................................
   675    675   
   676    676   
   677    677   
   678    678   test -n "$DJDIR" || exec 7<&0 </dev/null
   679    679   exec 6>&1
   680    680   
   681    681   # Name of the host.
   682         -# hostname on some systems (SVR3.2, Linux) returns a bogus exit status,
          682  +# hostname on some systems (SVR3.2, old GNU/Linux) returns a bogus exit status,
   683    683   # so uname gets run too.
   684    684   ac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q`
   685    685   
   686    686   #
   687    687   # Initializations.
   688    688   #
   689    689   ac_default_prefix=/usr/local
................................................................................
   694    694   subdirs=
   695    695   MFLAGS=
   696    696   MAKEFLAGS=
   697    697   
   698    698   # Identity of this package.
   699    699   PACKAGE_NAME='sqlite'
   700    700   PACKAGE_TARNAME='sqlite'
   701         -PACKAGE_VERSION='3.7.8'
   702         -PACKAGE_STRING='sqlite 3.7.8'
          701  +PACKAGE_VERSION='3.7.9'
          702  +PACKAGE_STRING='sqlite 3.7.9'
   703    703   PACKAGE_BUGREPORT=''
   704    704   PACKAGE_URL=''
   705    705   
   706    706   # Factoring default headers for most tests.
   707    707   ac_includes_default="\
   708    708   #include <stdio.h>
   709    709   #ifdef HAVE_SYS_TYPES_H
................................................................................
   948    948     if test -n "$ac_prev"; then
   949    949       eval $ac_prev=\$ac_option
   950    950       ac_prev=
   951    951       continue
   952    952     fi
   953    953   
   954    954     case $ac_option in
   955         -  *=*)	ac_optarg=`expr "X$ac_option" : '[^=]*=\(.*\)'` ;;
   956         -  *)	ac_optarg=yes ;;
          955  +  *=?*) ac_optarg=`expr "X$ac_option" : '[^=]*=\(.*\)'` ;;
          956  +  *=)   ac_optarg= ;;
          957  +  *)    ac_optarg=yes ;;
   957    958     esac
   958    959   
   959    960     # Accept the important Cygnus configure options, so we can diagnose typos.
   960    961   
   961    962     case $ac_dashdash$ac_option in
   962    963     --)
   963    964       ac_dashdash=yes ;;
................................................................................
   994    995     | --dataroot=* | --dataroo=* | --dataro=* | --datar=*)
   995    996       datarootdir=$ac_optarg ;;
   996    997   
   997    998     -disable-* | --disable-*)
   998    999       ac_useropt=`expr "x$ac_option" : 'x-*disable-\(.*\)'`
   999   1000       # Reject names that are not valid shell variable names.
  1000   1001       expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
  1001         -      as_fn_error "invalid feature name: $ac_useropt"
         1002  +      as_fn_error $? "invalid feature name: $ac_useropt"
  1002   1003       ac_useropt_orig=$ac_useropt
  1003   1004       ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
  1004   1005       case $ac_user_opts in
  1005   1006         *"
  1006   1007   "enable_$ac_useropt"
  1007   1008   "*) ;;
  1008   1009         *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--disable-$ac_useropt_orig"
................................................................................
  1020   1021     -dvidir=* | --dvidir=* | --dvidi=* | --dvid=* | --dvi=* | --dv=*)
  1021   1022       dvidir=$ac_optarg ;;
  1022   1023   
  1023   1024     -enable-* | --enable-*)
  1024   1025       ac_useropt=`expr "x$ac_option" : 'x-*enable-\([^=]*\)'`
  1025   1026       # Reject names that are not valid shell variable names.
  1026   1027       expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
  1027         -      as_fn_error "invalid feature name: $ac_useropt"
         1028  +      as_fn_error $? "invalid feature name: $ac_useropt"
  1028   1029       ac_useropt_orig=$ac_useropt
  1029   1030       ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
  1030   1031       case $ac_user_opts in
  1031   1032         *"
  1032   1033   "enable_$ac_useropt"
  1033   1034   "*) ;;
  1034   1035         *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--enable-$ac_useropt_orig"
................................................................................
  1224   1225     -version | --version | --versio | --versi | --vers | -V)
  1225   1226       ac_init_version=: ;;
  1226   1227   
  1227   1228     -with-* | --with-*)
  1228   1229       ac_useropt=`expr "x$ac_option" : 'x-*with-\([^=]*\)'`
  1229   1230       # Reject names that are not valid shell variable names.
  1230   1231       expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
  1231         -      as_fn_error "invalid package name: $ac_useropt"
         1232  +      as_fn_error $? "invalid package name: $ac_useropt"
  1232   1233       ac_useropt_orig=$ac_useropt
  1233   1234       ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
  1234   1235       case $ac_user_opts in
  1235   1236         *"
  1236   1237   "with_$ac_useropt"
  1237   1238   "*) ;;
  1238   1239         *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--with-$ac_useropt_orig"
................................................................................
  1240   1241       esac
  1241   1242       eval with_$ac_useropt=\$ac_optarg ;;
  1242   1243   
  1243   1244     -without-* | --without-*)
  1244   1245       ac_useropt=`expr "x$ac_option" : 'x-*without-\(.*\)'`
  1245   1246       # Reject names that are not valid shell variable names.
  1246   1247       expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
  1247         -      as_fn_error "invalid package name: $ac_useropt"
         1248  +      as_fn_error $? "invalid package name: $ac_useropt"
  1248   1249       ac_useropt_orig=$ac_useropt
  1249   1250       ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
  1250   1251       case $ac_user_opts in
  1251   1252         *"
  1252   1253   "with_$ac_useropt"
  1253   1254   "*) ;;
  1254   1255         *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--without-$ac_useropt_orig"
................................................................................
  1270   1271     -x-libraries | --x-libraries | --x-librarie | --x-librari \
  1271   1272     | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l)
  1272   1273       ac_prev=x_libraries ;;
  1273   1274     -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \
  1274   1275     | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*)
  1275   1276       x_libraries=$ac_optarg ;;
  1276   1277   
  1277         -  -*) as_fn_error "unrecognized option: \`$ac_option'
  1278         -Try \`$0 --help' for more information."
         1278  +  -*) as_fn_error $? "unrecognized option: \`$ac_option'
         1279  +Try \`$0 --help' for more information"
  1279   1280       ;;
  1280   1281   
  1281   1282     *=*)
  1282   1283       ac_envvar=`expr "x$ac_option" : 'x\([^=]*\)='`
  1283   1284       # Reject names that are not valid shell variable names.
  1284   1285       case $ac_envvar in #(
  1285   1286         '' | [0-9]* | *[!_$as_cr_alnum]* )
  1286         -      as_fn_error "invalid variable name: \`$ac_envvar'" ;;
         1287  +      as_fn_error $? "invalid variable name: \`$ac_envvar'" ;;
  1287   1288       esac
  1288   1289       eval $ac_envvar=\$ac_optarg
  1289   1290       export $ac_envvar ;;
  1290   1291   
  1291   1292     *)
  1292   1293       # FIXME: should be removed in autoconf 3.0.
  1293   1294       $as_echo "$as_me: WARNING: you should use --build, --host, --target" >&2
................................................................................
  1297   1298       ;;
  1298   1299   
  1299   1300     esac
  1300   1301   done
  1301   1302   
  1302   1303   if test -n "$ac_prev"; then
  1303   1304     ac_option=--`echo $ac_prev | sed 's/_/-/g'`
  1304         -  as_fn_error "missing argument to $ac_option"
         1305  +  as_fn_error $? "missing argument to $ac_option"
  1305   1306   fi
  1306   1307   
  1307   1308   if test -n "$ac_unrecognized_opts"; then
  1308   1309     case $enable_option_checking in
  1309   1310       no) ;;
  1310         -    fatal) as_fn_error "unrecognized options: $ac_unrecognized_opts" ;;
         1311  +    fatal) as_fn_error $? "unrecognized options: $ac_unrecognized_opts" ;;
  1311   1312       *)     $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2 ;;
  1312   1313     esac
  1313   1314   fi
  1314   1315   
  1315   1316   # Check all directory arguments for consistency.
  1316   1317   for ac_var in	exec_prefix prefix bindir sbindir libexecdir datarootdir \
  1317   1318   		datadir sysconfdir sharedstatedir localstatedir includedir \
................................................................................
  1326   1327         eval $ac_var=\$ac_val;;
  1327   1328     esac
  1328   1329     # Be sure to have absolute directory names.
  1329   1330     case $ac_val in
  1330   1331       [\\/$]* | ?:[\\/]* )  continue;;
  1331   1332       NONE | '' ) case $ac_var in *prefix ) continue;; esac;;
  1332   1333     esac
  1333         -  as_fn_error "expected an absolute directory name for --$ac_var: $ac_val"
         1334  +  as_fn_error $? "expected an absolute directory name for --$ac_var: $ac_val"
  1334   1335   done
  1335   1336   
  1336   1337   # There might be people who depend on the old broken behavior: `$host'
  1337   1338   # used to hold the argument of --host etc.
  1338   1339   # FIXME: To remove some day.
  1339   1340   build=$build_alias
  1340   1341   host=$host_alias
  1341   1342   target=$target_alias
  1342   1343   
  1343   1344   # FIXME: To remove some day.
  1344   1345   if test "x$host_alias" != x; then
  1345   1346     if test "x$build_alias" = x; then
  1346   1347       cross_compiling=maybe
  1347         -    $as_echo "$as_me: WARNING: If you wanted to set the --build type, don't use --host.
  1348         -    If a cross compiler is detected then cross compile mode will be used." >&2
         1348  +    $as_echo "$as_me: WARNING: if you wanted to set the --build type, don't use --host.
         1349  +    If a cross compiler is detected then cross compile mode will be used" >&2
  1349   1350     elif test "x$build_alias" != "x$host_alias"; then
  1350   1351       cross_compiling=yes
  1351   1352     fi
  1352   1353   fi
  1353   1354   
  1354   1355   ac_tool_prefix=
  1355   1356   test -n "$host_alias" && ac_tool_prefix=$host_alias-
................................................................................
  1356   1357   
  1357   1358   test "$silent" = yes && exec 6>/dev/null
  1358   1359   
  1359   1360   
  1360   1361   ac_pwd=`pwd` && test -n "$ac_pwd" &&
  1361   1362   ac_ls_di=`ls -di .` &&
  1362   1363   ac_pwd_ls_di=`cd "$ac_pwd" && ls -di .` ||
  1363         -  as_fn_error "working directory cannot be determined"
         1364  +  as_fn_error $? "working directory cannot be determined"
  1364   1365   test "X$ac_ls_di" = "X$ac_pwd_ls_di" ||
  1365         -  as_fn_error "pwd does not report name of working directory"
         1366  +  as_fn_error $? "pwd does not report name of working directory"
  1366   1367   
  1367   1368   
  1368   1369   # Find the source files, if location was not specified.
  1369   1370   if test -z "$srcdir"; then
  1370   1371     ac_srcdir_defaulted=yes
  1371   1372     # Try the directory containing this script, then the parent directory.
  1372   1373     ac_confdir=`$as_dirname -- "$as_myself" ||
................................................................................
  1397   1398       srcdir=..
  1398   1399     fi
  1399   1400   else
  1400   1401     ac_srcdir_defaulted=no
  1401   1402   fi
  1402   1403   if test ! -r "$srcdir/$ac_unique_file"; then
  1403   1404     test "$ac_srcdir_defaulted" = yes && srcdir="$ac_confdir or .."
  1404         -  as_fn_error "cannot find sources ($ac_unique_file) in $srcdir"
         1405  +  as_fn_error $? "cannot find sources ($ac_unique_file) in $srcdir"
  1405   1406   fi
  1406   1407   ac_msg="sources are in $srcdir, but \`cd $srcdir' does not work"
  1407   1408   ac_abs_confdir=`(
  1408         -	cd "$srcdir" && test -r "./$ac_unique_file" || as_fn_error "$ac_msg"
         1409  +	cd "$srcdir" && test -r "./$ac_unique_file" || as_fn_error $? "$ac_msg"
  1409   1410   	pwd)`
  1410   1411   # When building in place, set srcdir=.
  1411   1412   if test "$ac_abs_confdir" = "$ac_pwd"; then
  1412   1413     srcdir=.
  1413   1414   fi
  1414   1415   # Remove unnecessary trailing slashes from srcdir.
  1415   1416   # Double slashes in file names in object file debugging info
................................................................................
  1427   1428   #
  1428   1429   # Report the --help message.
  1429   1430   #
  1430   1431   if test "$ac_init_help" = "long"; then
  1431   1432     # Omit some internal or obsolete options to make the list less imposing.
  1432   1433     # This message is too long to be a string in the A/UX 3.1 sh.
  1433   1434     cat <<_ACEOF
  1434         -\`configure' configures sqlite 3.7.8 to adapt to many kinds of systems.
         1435  +\`configure' configures sqlite 3.7.9 to adapt to many kinds of systems.
  1435   1436   
  1436   1437   Usage: $0 [OPTION]... [VAR=VALUE]...
  1437   1438   
  1438   1439   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1439   1440   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1440   1441   
  1441   1442   Defaults for the options are specified in brackets.
  1442   1443   
  1443   1444   Configuration:
  1444   1445     -h, --help              display this help and exit
  1445   1446         --help=short        display options specific to this package
  1446   1447         --help=recursive    display the short help of all the included packages
  1447   1448     -V, --version           display version information and exit
  1448         -  -q, --quiet, --silent   do not print \`checking...' messages
         1449  +  -q, --quiet, --silent   do not print \`checking ...' messages
  1449   1450         --cache-file=FILE   cache test results in FILE [disabled]
  1450   1451     -C, --config-cache      alias for \`--cache-file=config.cache'
  1451   1452     -n, --no-create         do not create output files
  1452   1453         --srcdir=DIR        find the sources in DIR [configure dir or \`..']
  1453   1454   
  1454   1455   Installation directories:
  1455   1456     --prefix=PREFIX         install architecture-independent files in PREFIX
................................................................................
  1492   1493     --build=BUILD     configure for building on BUILD [guessed]
  1493   1494     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1494   1495   _ACEOF
  1495   1496   fi
  1496   1497   
  1497   1498   if test -n "$ac_init_help"; then
  1498   1499     case $ac_init_help in
  1499         -     short | recursive ) echo "Configuration of sqlite 3.7.8:";;
         1500  +     short | recursive ) echo "Configuration of sqlite 3.7.9:";;
  1500   1501      esac
  1501   1502     cat <<\_ACEOF
  1502   1503   
  1503   1504   Optional Features:
  1504   1505     --disable-option-checking  ignore unrecognized --enable/--with options
  1505   1506     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1506   1507     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1609   1610       cd "$ac_pwd" || { ac_status=$?; break; }
  1610   1611     done
  1611   1612   fi
  1612   1613   
  1613   1614   test -n "$ac_init_help" && exit $ac_status
  1614   1615   if $ac_init_version; then
  1615   1616     cat <<\_ACEOF
  1616         -sqlite configure 3.7.8
  1617         -generated by GNU Autoconf 2.65
         1617  +sqlite configure 3.7.9
         1618  +generated by GNU Autoconf 2.67
  1618   1619   
  1619         -Copyright (C) 2009 Free Software Foundation, Inc.
         1620  +Copyright (C) 2010 Free Software Foundation, Inc.
  1620   1621   This configure script is free software; the Free Software Foundation
  1621   1622   gives unlimited permission to copy, distribute and modify it.
  1622   1623   _ACEOF
  1623   1624     exit
  1624   1625   fi
  1625   1626   
  1626   1627   ## ------------------------ ##
................................................................................
  1716   1717   # Tests whether HEADER exists and can be compiled using the include files in
  1717   1718   # INCLUDES, setting the cache variable VAR accordingly.
  1718   1719   ac_fn_c_check_header_compile ()
  1719   1720   {
  1720   1721     as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
  1721   1722     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
  1722   1723   $as_echo_n "checking for $2... " >&6; }
  1723         -if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         1724  +if eval "test \"\${$3+set}\"" = set; then :
  1724   1725     $as_echo_n "(cached) " >&6
  1725   1726   else
  1726   1727     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  1727   1728   /* end confdefs.h.  */
  1728   1729   $4
  1729   1730   #include <$2>
  1730   1731   _ACEOF
................................................................................
  1759   1760     ac_status=$?
  1760   1761     if test -s conftest.err; then
  1761   1762       grep -v '^ *+' conftest.err >conftest.er1
  1762   1763       cat conftest.er1 >&5
  1763   1764       mv -f conftest.er1 conftest.err
  1764   1765     fi
  1765   1766     $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
  1766         -  test $ac_status = 0; } >/dev/null && {
         1767  +  test $ac_status = 0; } > conftest.i && {
  1767   1768   	 test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" ||
  1768   1769   	 test ! -s conftest.err
  1769   1770          }; then :
  1770   1771     ac_retval=0
  1771   1772   else
  1772   1773     $as_echo "$as_me: failed program was:" >&5
  1773   1774   sed 's/^/| /' conftest.$ac_ext >&5
................................................................................
  1825   1826   # ----------------------------------
  1826   1827   # Tests whether FUNC exists, setting the cache variable VAR accordingly
  1827   1828   ac_fn_c_check_func ()
  1828   1829   {
  1829   1830     as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
  1830   1831     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
  1831   1832   $as_echo_n "checking for $2... " >&6; }
  1832         -if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         1833  +if eval "test \"\${$3+set}\"" = set; then :
  1833   1834     $as_echo_n "(cached) " >&6
  1834   1835   else
  1835   1836     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  1836   1837   /* end confdefs.h.  */
  1837   1838   /* Define $2 to an innocuous variant, in case <limits.h> declares $2.
  1838   1839      For example, HP-UX 11i <limits.h> declares gettimeofday.  */
  1839   1840   #define $2 innocuous_$2
................................................................................
  1893   1894   # Tests whether TYPE exists after having included INCLUDES, setting cache
  1894   1895   # variable VAR accordingly.
  1895   1896   ac_fn_c_check_type ()
  1896   1897   {
  1897   1898     as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
  1898   1899     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
  1899   1900   $as_echo_n "checking for $2... " >&6; }
  1900         -if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         1901  +if eval "test \"\${$3+set}\"" = set; then :
  1901   1902     $as_echo_n "(cached) " >&6
  1902   1903   else
  1903   1904     eval "$3=no"
  1904   1905     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  1905   1906   /* end confdefs.h.  */
  1906   1907   $4
  1907   1908   int
................................................................................
  1946   1947   # -------------------------------------------------------
  1947   1948   # Tests whether HEADER exists, giving a warning if it cannot be compiled using
  1948   1949   # the include files in INCLUDES and setting the cache variable VAR
  1949   1950   # accordingly.
  1950   1951   ac_fn_c_check_header_mongrel ()
  1951   1952   {
  1952   1953     as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
  1953         -  if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         1954  +  if eval "test \"\${$3+set}\"" = set; then :
  1954   1955     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
  1955   1956   $as_echo_n "checking for $2... " >&6; }
  1956         -if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         1957  +if eval "test \"\${$3+set}\"" = set; then :
  1957   1958     $as_echo_n "(cached) " >&6
  1958   1959   fi
  1959   1960   eval ac_res=\$$3
  1960   1961   	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
  1961   1962   $as_echo "$ac_res" >&6; }
  1962   1963   else
  1963   1964     # Is the header compilable?
................................................................................
  1985   1986   #include <$2>
  1986   1987   _ACEOF
  1987   1988   if ac_fn_c_try_cpp "$LINENO"; then :
  1988   1989     ac_header_preproc=yes
  1989   1990   else
  1990   1991     ac_header_preproc=no
  1991   1992   fi
  1992         -rm -f conftest.err conftest.$ac_ext
         1993  +rm -f conftest.err conftest.i conftest.$ac_ext
  1993   1994   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_preproc" >&5
  1994   1995   $as_echo "$ac_header_preproc" >&6; }
  1995   1996   
  1996   1997   # So?  What about this header?
  1997   1998   case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in #((
  1998   1999     yes:no: )
  1999   2000       { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&5
................................................................................
  2012   2013   $as_echo "$as_me: WARNING: $2:     section \"Present But Cannot Be Compiled\"" >&2;}
  2013   2014       { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5
  2014   2015   $as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;}
  2015   2016       ;;
  2016   2017   esac
  2017   2018     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
  2018   2019   $as_echo_n "checking for $2... " >&6; }
  2019         -if { as_var=$3; eval "test \"\${$as_var+set}\" = set"; }; then :
         2020  +if eval "test \"\${$3+set}\"" = set; then :
  2020   2021     $as_echo_n "(cached) " >&6
  2021   2022   else
  2022   2023     eval "$3=\$ac_header_compiler"
  2023   2024   fi
  2024   2025   eval ac_res=\$$3
  2025   2026   	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
  2026   2027   $as_echo "$ac_res" >&6; }
................................................................................
  2028   2029     eval $as_lineno_stack; test "x$as_lineno_stack" = x && { as_lineno=; unset as_lineno;}
  2029   2030   
  2030   2031   } # ac_fn_c_check_header_mongrel
  2031   2032   cat >config.log <<_ACEOF
  2032   2033   This file contains any messages produced by compilers while
  2033   2034   running configure, to aid debugging if configure makes a mistake.
  2034   2035   
  2035         -It was created by sqlite $as_me 3.7.8, which was
  2036         -generated by GNU Autoconf 2.65.  Invocation command line was
         2036  +It was created by sqlite $as_me 3.7.9, which was
         2037  +generated by GNU Autoconf 2.67.  Invocation command line was
  2037   2038   
  2038   2039     $ $0 $@
  2039   2040   
  2040   2041   _ACEOF
  2041   2042   exec 5>>config.log
  2042   2043   {
  2043   2044   cat <<_ASUNAME
................................................................................
  2139   2140   # WARNING: Use '\'' to represent an apostrophe within the trap.
  2140   2141   # WARNING: Do not start the trap code with a newline, due to a FreeBSD 4.0 bug.
  2141   2142   trap 'exit_status=$?
  2142   2143     # Save into config.log some information that might help in debugging.
  2143   2144     {
  2144   2145       echo
  2145   2146   
  2146         -    cat <<\_ASBOX
  2147         -## ---------------- ##
         2147  +    $as_echo "## ---------------- ##
  2148   2148   ## Cache variables. ##
  2149         -## ---------------- ##
  2150         -_ASBOX
         2149  +## ---------------- ##"
  2151   2150       echo
  2152   2151       # The following way of writing the cache mishandles newlines in values,
  2153   2152   (
  2154   2153     for ac_var in `(set) 2>&1 | sed -n '\''s/^\([a-zA-Z_][a-zA-Z0-9_]*\)=.*/\1/p'\''`; do
  2155   2154       eval ac_val=\$$ac_var
  2156   2155       case $ac_val in #(
  2157   2156       *${as_nl}*)
................................................................................
  2177   2176         sed -n "/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p"
  2178   2177         ;;
  2179   2178       esac |
  2180   2179       sort
  2181   2180   )
  2182   2181       echo
  2183   2182   
  2184         -    cat <<\_ASBOX
  2185         -## ----------------- ##
         2183  +    $as_echo "## ----------------- ##
  2186   2184   ## Output variables. ##
  2187         -## ----------------- ##
  2188         -_ASBOX
         2185  +## ----------------- ##"
  2189   2186       echo
  2190   2187       for ac_var in $ac_subst_vars
  2191   2188       do
  2192   2189         eval ac_val=\$$ac_var
  2193   2190         case $ac_val in
  2194   2191         *\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;;
  2195   2192         esac
  2196   2193         $as_echo "$ac_var='\''$ac_val'\''"
  2197   2194       done | sort
  2198   2195       echo
  2199   2196   
  2200   2197       if test -n "$ac_subst_files"; then
  2201         -      cat <<\_ASBOX
  2202         -## ------------------- ##
         2198  +      $as_echo "## ------------------- ##
  2203   2199   ## File substitutions. ##
  2204         -## ------------------- ##
  2205         -_ASBOX
         2200  +## ------------------- ##"
  2206   2201         echo
  2207   2202         for ac_var in $ac_subst_files
  2208   2203         do
  2209   2204   	eval ac_val=\$$ac_var
  2210   2205   	case $ac_val in
  2211   2206   	*\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;;
  2212   2207   	esac
  2213   2208   	$as_echo "$ac_var='\''$ac_val'\''"
  2214   2209         done | sort
  2215   2210         echo
  2216   2211       fi
  2217   2212   
  2218   2213       if test -s confdefs.h; then
  2219         -      cat <<\_ASBOX
  2220         -## ----------- ##
         2214  +      $as_echo "## ----------- ##
  2221   2215   ## confdefs.h. ##
  2222         -## ----------- ##
  2223         -_ASBOX
         2216  +## ----------- ##"
  2224   2217         echo
  2225   2218         cat confdefs.h
  2226   2219         echo
  2227   2220       fi
  2228   2221       test "$ac_signal" != 0 &&
  2229   2222         $as_echo "$as_me: caught signal $ac_signal"
  2230   2223       $as_echo "$as_me: exit $exit_status"
................................................................................
  2271   2264   
  2272   2265   
  2273   2266   # Let the site file select an alternate cache file if it wants to.
  2274   2267   # Prefer an explicitly selected file to automatically selected ones.
  2275   2268   ac_site_file1=NONE
  2276   2269   ac_site_file2=NONE
  2277   2270   if test -n "$CONFIG_SITE"; then
  2278         -  ac_site_file1=$CONFIG_SITE
         2271  +  # We do not want a PATH search for config.site.
         2272  +  case $CONFIG_SITE in #((
         2273  +    -*)  ac_site_file1=./$CONFIG_SITE;;
         2274  +    */*) ac_site_file1=$CONFIG_SITE;;
         2275  +    *)   ac_site_file1=./$CONFIG_SITE;;
         2276  +  esac
  2279   2277   elif test "x$prefix" != xNONE; then
  2280   2278     ac_site_file1=$prefix/share/config.site
  2281   2279     ac_site_file2=$prefix/etc/config.site
  2282   2280   else
  2283   2281     ac_site_file1=$ac_default_prefix/share/config.site
  2284   2282     ac_site_file2=$ac_default_prefix/etc/config.site
  2285   2283   fi
................................................................................
  2286   2284   for ac_site_file in "$ac_site_file1" "$ac_site_file2"
  2287   2285   do
  2288   2286     test "x$ac_site_file" = xNONE && continue
  2289   2287     if test /dev/null != "$ac_site_file" && test -r "$ac_site_file"; then
  2290   2288       { $as_echo "$as_me:${as_lineno-$LINENO}: loading site script $ac_site_file" >&5
  2291   2289   $as_echo "$as_me: loading site script $ac_site_file" >&6;}
  2292   2290       sed 's/^/| /' "$ac_site_file" >&5
  2293         -    . "$ac_site_file"
         2291  +    . "$ac_site_file" \
         2292  +      || { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
         2293  +$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
         2294  +as_fn_error $? "failed to load site script $ac_site_file
         2295  +See \`config.log' for more details" "$LINENO" 5 ; }
  2294   2296     fi
  2295   2297   done
  2296   2298   
  2297   2299   if test -r "$cache_file"; then
  2298   2300     # Some versions of bash will fail to source /dev/null (special files
  2299   2301     # actually), so we avoid doing that.  DJGPP emulates it as a regular file.
  2300   2302     if test /dev/null != "$cache_file" && test -f "$cache_file"; then
................................................................................
  2362   2364     fi
  2363   2365   done
  2364   2366   if $ac_cache_corrupted; then
  2365   2367     { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  2366   2368   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  2367   2369     { $as_echo "$as_me:${as_lineno-$LINENO}: error: changes in the environment can compromise the build" >&5
  2368   2370   $as_echo "$as_me: error: changes in the environment can compromise the build" >&2;}
  2369         -  as_fn_error "run \`make distclean' and/or \`rm $cache_file' and start over" "$LINENO" 5
         2371  +  as_fn_error $? "run \`make distclean' and/or \`rm $cache_file' and start over" "$LINENO" 5
  2370   2372   fi
  2371   2373   ## -------------------- ##
  2372   2374   ## Main body of script. ##
  2373   2375   ## -------------------- ##
  2374   2376   
  2375   2377   ac_ext=c
  2376   2378   ac_cpp='$CPP $CPPFLAGS'
................................................................................
  2378   2380   ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
  2379   2381   ac_compiler_gnu=$ac_cv_c_compiler_gnu
  2380   2382   
  2381   2383   
  2382   2384   
  2383   2385   sqlite_version_sanity_check=`cat $srcdir/VERSION | tr -d '\n'`
  2384   2386   if test "$PACKAGE_VERSION" != "$sqlite_version_sanity_check" ; then
  2385         -as_fn_error "configure script is out of date:
         2387  +as_fn_error $? "configure script is out of date:
  2386   2388    configure \$PACKAGE_VERSION = $PACKAGE_VERSION
  2387   2389    top level VERSION file     = $sqlite_version_sanity_check
  2388   2390   please regen with autoconf" "$LINENO" 5
  2389   2391   fi
  2390   2392   
  2391   2393   # The following RCS revision string applies to configure.in
  2392   2394   # $Revision: 1.56 $
................................................................................
  2417   2419   
  2418   2420   
  2419   2421   
  2420   2422   ltmain="$ac_aux_dir/ltmain.sh"
  2421   2423   
  2422   2424   ac_aux_dir=
  2423   2425   for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
  2424         -  for ac_t in install-sh install.sh shtool; do
  2425         -    if test -f "$ac_dir/$ac_t"; then
  2426         -      ac_aux_dir=$ac_dir
  2427         -      ac_install_sh="$ac_aux_dir/$ac_t -c"
  2428         -      break 2
  2429         -    fi
  2430         -  done
         2426  +  if test -f "$ac_dir/install-sh"; then
         2427  +    ac_aux_dir=$ac_dir
         2428  +    ac_install_sh="$ac_aux_dir/install-sh -c"
         2429  +    break
         2430  +  elif test -f "$ac_dir/install.sh"; then
         2431  +    ac_aux_dir=$ac_dir
         2432  +    ac_install_sh="$ac_aux_dir/install.sh -c"
         2433  +    break
         2434  +  elif test -f "$ac_dir/shtool"; then
         2435  +    ac_aux_dir=$ac_dir
         2436  +    ac_install_sh="$ac_aux_dir/shtool install -c"
         2437  +    break
         2438  +  fi
  2431   2439   done
  2432   2440   if test -z "$ac_aux_dir"; then
  2433         -  as_fn_error "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5
         2441  +  as_fn_error $? "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5
  2434   2442   fi
  2435   2443   
  2436   2444   # These three variables are undocumented and unsupported,
  2437   2445   # and are intended to be withdrawn in a future Autoconf release.
  2438   2446   # They can cause serious problems if a builder's source tree is in a directory
  2439   2447   # whose full name contains unusual characters.
  2440   2448   ac_config_guess="$SHELL $ac_aux_dir/config.guess"  # Please don't use this var.
  2441   2449   ac_config_sub="$SHELL $ac_aux_dir/config.sub"  # Please don't use this var.
  2442   2450   ac_configure="$SHELL $ac_aux_dir/configure"  # Please don't use this var.
  2443   2451   
  2444   2452   
  2445   2453   # Make sure we can run config.sub.
  2446   2454   $SHELL "$ac_aux_dir/config.sub" sun4 >/dev/null 2>&1 ||
  2447         -  as_fn_error "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5
         2455  +  as_fn_error $? "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5
  2448   2456   
  2449   2457   { $as_echo "$as_me:${as_lineno-$LINENO}: checking build system type" >&5
  2450   2458   $as_echo_n "checking build system type... " >&6; }
  2451   2459   if test "${ac_cv_build+set}" = set; then :
  2452   2460     $as_echo_n "(cached) " >&6
  2453   2461   else
  2454   2462     ac_build_alias=$build_alias
  2455   2463   test "x$ac_build_alias" = x &&
  2456   2464     ac_build_alias=`$SHELL "$ac_aux_dir/config.guess"`
  2457   2465   test "x$ac_build_alias" = x &&
  2458         -  as_fn_error "cannot guess build type; you must specify one" "$LINENO" 5
         2466  +  as_fn_error $? "cannot guess build type; you must specify one" "$LINENO" 5
  2459   2467   ac_cv_build=`$SHELL "$ac_aux_dir/config.sub" $ac_build_alias` ||
  2460         -  as_fn_error "$SHELL $ac_aux_dir/config.sub $ac_build_alias failed" "$LINENO" 5
         2468  +  as_fn_error $? "$SHELL $ac_aux_dir/config.sub $ac_build_alias failed" "$LINENO" 5
  2461   2469   
  2462   2470   fi
  2463   2471   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_build" >&5
  2464   2472   $as_echo "$ac_cv_build" >&6; }
  2465   2473   case $ac_cv_build in
  2466   2474   *-*-*) ;;
  2467         -*) as_fn_error "invalid value of canonical build" "$LINENO" 5;;
         2475  +*) as_fn_error $? "invalid value of canonical build" "$LINENO" 5 ;;
  2468   2476   esac
  2469   2477   build=$ac_cv_build
  2470   2478   ac_save_IFS=$IFS; IFS='-'
  2471   2479   set x $ac_cv_build
  2472   2480   shift
  2473   2481   build_cpu=$1
  2474   2482   build_vendor=$2
................................................................................
  2485   2493   if test "${ac_cv_host+set}" = set; then :
  2486   2494     $as_echo_n "(cached) " >&6
  2487   2495   else
  2488   2496     if test "x$host_alias" = x; then
  2489   2497     ac_cv_host=$ac_cv_build
  2490   2498   else
  2491   2499     ac_cv_host=`$SHELL "$ac_aux_dir/config.sub" $host_alias` ||
  2492         -    as_fn_error "$SHELL $ac_aux_dir/config.sub $host_alias failed" "$LINENO" 5
         2500  +    as_fn_error $? "$SHELL $ac_aux_dir/config.sub $host_alias failed" "$LINENO" 5
  2493   2501   fi
  2494   2502   
  2495   2503   fi
  2496   2504   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_host" >&5
  2497   2505   $as_echo "$ac_cv_host" >&6; }
  2498   2506   case $ac_cv_host in
  2499   2507   *-*-*) ;;
  2500         -*) as_fn_error "invalid value of canonical host" "$LINENO" 5;;
         2508  +*) as_fn_error $? "invalid value of canonical host" "$LINENO" 5 ;;
  2501   2509   esac
  2502   2510   host=$ac_cv_host
  2503   2511   ac_save_IFS=$IFS; IFS='-'
  2504   2512   set x $ac_cv_host
  2505   2513   shift
  2506   2514   host_cpu=$1
  2507   2515   host_vendor=$2
................................................................................
  2809   2817   fi
  2810   2818   
  2811   2819   fi
  2812   2820   
  2813   2821   
  2814   2822   test -z "$CC" && { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  2815   2823   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  2816         -as_fn_error "no acceptable C compiler found in \$PATH
  2817         -See \`config.log' for more details." "$LINENO" 5; }
         2824  +as_fn_error $? "no acceptable C compiler found in \$PATH
         2825  +See \`config.log' for more details" "$LINENO" 5 ; }
  2818   2826   
  2819   2827   # Provide some information about the compiler.
  2820   2828   $as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler version" >&5
  2821   2829   set X $ac_compile
  2822   2830   ac_compiler=$2
  2823   2831   for ac_option in --version -v -V -qversion; do
  2824   2832     { { ac_try="$ac_compiler $ac_option >&5"
................................................................................
  2924   2932     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
  2925   2933   $as_echo "no" >&6; }
  2926   2934   $as_echo "$as_me: failed program was:" >&5
  2927   2935   sed 's/^/| /' conftest.$ac_ext >&5
  2928   2936   
  2929   2937   { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  2930   2938   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  2931         -{ as_fn_set_status 77
  2932         -as_fn_error "C compiler cannot create executables
  2933         -See \`config.log' for more details." "$LINENO" 5; }; }
         2939  +as_fn_error 77 "C compiler cannot create executables
         2940  +See \`config.log' for more details" "$LINENO" 5 ; }
  2934   2941   else
  2935   2942     { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
  2936   2943   $as_echo "yes" >&6; }
  2937   2944   fi
  2938   2945   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler default output file name" >&5
  2939   2946   $as_echo_n "checking for C compiler default output file name... " >&6; }
  2940   2947   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_file" >&5
................................................................................
  2968   2975   	  break;;
  2969   2976       * ) break;;
  2970   2977     esac
  2971   2978   done
  2972   2979   else
  2973   2980     { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  2974   2981   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  2975         -as_fn_error "cannot compute suffix of executables: cannot compile and link
  2976         -See \`config.log' for more details." "$LINENO" 5; }
         2982  +as_fn_error $? "cannot compute suffix of executables: cannot compile and link
         2983  +See \`config.log' for more details" "$LINENO" 5 ; }
  2977   2984   fi
  2978   2985   rm -f conftest conftest$ac_cv_exeext
  2979   2986   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_exeext" >&5
  2980   2987   $as_echo "$ac_cv_exeext" >&6; }
  2981   2988   
  2982   2989   rm -f conftest.$ac_ext
  2983   2990   EXEEXT=$ac_cv_exeext
................................................................................
  3026   3033       cross_compiling=no
  3027   3034     else
  3028   3035       if test "$cross_compiling" = maybe; then
  3029   3036   	cross_compiling=yes
  3030   3037       else
  3031   3038   	{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  3032   3039   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  3033         -as_fn_error "cannot run C compiled programs.
         3040  +as_fn_error $? "cannot run C compiled programs.
  3034   3041   If you meant to cross compile, use \`--host'.
  3035         -See \`config.log' for more details." "$LINENO" 5; }
         3042  +See \`config.log' for more details" "$LINENO" 5 ; }
  3036   3043       fi
  3037   3044     fi
  3038   3045   fi
  3039   3046   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $cross_compiling" >&5
  3040   3047   $as_echo "$cross_compiling" >&6; }
  3041   3048   
  3042   3049   rm -f conftest.$ac_ext conftest$ac_cv_exeext conftest.out
................................................................................
  3079   3086   done
  3080   3087   else
  3081   3088     $as_echo "$as_me: failed program was:" >&5
  3082   3089   sed 's/^/| /' conftest.$ac_ext >&5
  3083   3090   
  3084   3091   { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  3085   3092   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  3086         -as_fn_error "cannot compute suffix of object files: cannot compile
  3087         -See \`config.log' for more details." "$LINENO" 5; }
         3093  +as_fn_error $? "cannot compute suffix of object files: cannot compile
         3094  +See \`config.log' for more details" "$LINENO" 5 ; }
  3088   3095   fi
  3089   3096   rm -f conftest.$ac_cv_objext conftest.$ac_ext
  3090   3097   fi
  3091   3098   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_objext" >&5
  3092   3099   $as_echo "$ac_cv_objext" >&6; }
  3093   3100   OBJEXT=$ac_cv_objext
  3094   3101   ac_objext=$OBJEXT
................................................................................
  3357   3364   
  3358   3365         $ac_path_SED_found && break 3
  3359   3366       done
  3360   3367     done
  3361   3368     done
  3362   3369   IFS=$as_save_IFS
  3363   3370     if test -z "$ac_cv_path_SED"; then
  3364         -    as_fn_error "no acceptable sed could be found in \$PATH" "$LINENO" 5
         3371  +    as_fn_error $? "no acceptable sed could be found in \$PATH" "$LINENO" 5
  3365   3372     fi
  3366   3373   else
  3367   3374     ac_cv_path_SED=$SED
  3368   3375   fi
  3369   3376   
  3370   3377   fi
  3371   3378   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_SED" >&5
................................................................................
  3433   3440   
  3434   3441         $ac_path_GREP_found && break 3
  3435   3442       done
  3436   3443     done
  3437   3444     done
  3438   3445   IFS=$as_save_IFS
  3439   3446     if test -z "$ac_cv_path_GREP"; then
  3440         -    as_fn_error "no acceptable grep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
         3447  +    as_fn_error $? "no acceptable grep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
  3441   3448     fi
  3442   3449   else
  3443   3450     ac_cv_path_GREP=$GREP
  3444   3451   fi
  3445   3452   
  3446   3453   fi
  3447   3454   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_GREP" >&5
................................................................................
  3499   3506   
  3500   3507         $ac_path_EGREP_found && break 3
  3501   3508       done
  3502   3509     done
  3503   3510     done
  3504   3511   IFS=$as_save_IFS
  3505   3512     if test -z "$ac_cv_path_EGREP"; then
  3506         -    as_fn_error "no acceptable egrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
         3513  +    as_fn_error $? "no acceptable egrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
  3507   3514     fi
  3508   3515   else
  3509   3516     ac_cv_path_EGREP=$EGREP
  3510   3517   fi
  3511   3518   
  3512   3519      fi
  3513   3520   fi
................................................................................
  3566   3573   
  3567   3574         $ac_path_FGREP_found && break 3
  3568   3575       done
  3569   3576     done
  3570   3577     done
  3571   3578   IFS=$as_save_IFS
  3572   3579     if test -z "$ac_cv_path_FGREP"; then
  3573         -    as_fn_error "no acceptable fgrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
         3580  +    as_fn_error $? "no acceptable fgrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
  3574   3581     fi
  3575   3582   else
  3576   3583     ac_cv_path_FGREP=$FGREP
  3577   3584   fi
  3578   3585   
  3579   3586      fi
  3580   3587   fi
................................................................................
  3682   3689   if test -n "$LD"; then
  3683   3690     { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LD" >&5
  3684   3691   $as_echo "$LD" >&6; }
  3685   3692   else
  3686   3693     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
  3687   3694   $as_echo "no" >&6; }
  3688   3695   fi
  3689         -test -z "$LD" && as_fn_error "no acceptable ld found in \$PATH" "$LINENO" 5
         3696  +test -z "$LD" && as_fn_error $? "no acceptable ld found in \$PATH" "$LINENO" 5
  3690   3697   { $as_echo "$as_me:${as_lineno-$LINENO}: checking if the linker ($LD) is GNU ld" >&5
  3691   3698   $as_echo_n "checking if the linker ($LD) is GNU ld... " >&6; }
  3692   3699   if test "${lt_cv_prog_gnu_ld+set}" = set; then :
  3693   3700     $as_echo_n "(cached) " >&6
  3694   3701   else
  3695   3702     # I'd rather use --version here, but apparently some GNU lds only accept -v.
  3696   3703   case `$LD -v 2>&1 </dev/null` in
................................................................................
  3884   3891   { $as_echo "$as_me:${as_lineno-$LINENO}: checking the name lister ($NM) interface" >&5
  3885   3892   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3886   3893   if test "${lt_cv_nm_interface+set}" = set; then :
  3887   3894     $as_echo_n "(cached) " >&6
  3888   3895   else
  3889   3896     lt_cv_nm_interface="BSD nm"
  3890   3897     echo "int some_variable = 0;" > conftest.$ac_ext
  3891         -  (eval echo "\"\$as_me:3891: $ac_compile\"" >&5)
         3898  +  (eval echo "\"\$as_me:3898: $ac_compile\"" >&5)
  3892   3899     (eval "$ac_compile" 2>conftest.err)
  3893   3900     cat conftest.err >&5
  3894         -  (eval echo "\"\$as_me:3894: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3901  +  (eval echo "\"\$as_me:3901: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3895   3902     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3896   3903     cat conftest.err >&5
  3897         -  (eval echo "\"\$as_me:3897: output\"" >&5)
         3904  +  (eval echo "\"\$as_me:3904: output\"" >&5)
  3898   3905     cat conftest.out >&5
  3899   3906     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3900   3907       lt_cv_nm_interface="MS dumpbin"
  3901   3908     fi
  3902   3909     rm -f conftest*
  3903   3910   fi
  3904   3911   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_nm_interface" >&5
................................................................................
  5096   5103   	;;
  5097   5104       esac
  5098   5105     fi
  5099   5106     rm -rf conftest*
  5100   5107     ;;
  5101   5108   *-*-irix6*)
  5102   5109     # Find out which ABI we are using.
  5103         -  echo '#line 5103 "configure"' > conftest.$ac_ext
         5110  +  echo '#line 5110 "configure"' > conftest.$ac_ext
  5104   5111     if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
  5105   5112     (eval $ac_compile) 2>&5
  5106   5113     ac_status=$?
  5107   5114     $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
  5108   5115     test $ac_status = 0; }; then
  5109   5116       if test "$lt_cv_prog_gnu_ld" = yes; then
  5110   5117         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  5885   5892   _ACEOF
  5886   5893   if ac_fn_c_try_cpp "$LINENO"; then :
  5887   5894   
  5888   5895   else
  5889   5896     # Broken: fails on valid input.
  5890   5897   continue
  5891   5898   fi
  5892         -rm -f conftest.err conftest.$ac_ext
         5899  +rm -f conftest.err conftest.i conftest.$ac_ext
  5893   5900   
  5894   5901     # OK, works on sane cases.  Now check whether nonexistent headers
  5895   5902     # can be detected and how.
  5896   5903     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  5897   5904   /* end confdefs.h.  */
  5898   5905   #include <ac_nonexistent.h>
  5899   5906   _ACEOF
................................................................................
  5901   5908     # Broken: success on invalid input.
  5902   5909   continue
  5903   5910   else
  5904   5911     # Passes both tests.
  5905   5912   ac_preproc_ok=:
  5906   5913   break
  5907   5914   fi
  5908         -rm -f conftest.err conftest.$ac_ext
         5915  +rm -f conftest.err conftest.i conftest.$ac_ext
  5909   5916   
  5910   5917   done
  5911   5918   # Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.
  5912         -rm -f conftest.err conftest.$ac_ext
         5919  +rm -f conftest.i conftest.err conftest.$ac_ext
  5913   5920   if $ac_preproc_ok; then :
  5914   5921     break
  5915   5922   fi
  5916   5923   
  5917   5924       done
  5918   5925       ac_cv_prog_CPP=$CPP
  5919   5926   
................................................................................
  5944   5951   _ACEOF
  5945   5952   if ac_fn_c_try_cpp "$LINENO"; then :
  5946   5953   
  5947   5954   else
  5948   5955     # Broken: fails on valid input.
  5949   5956   continue
  5950   5957   fi
  5951         -rm -f conftest.err conftest.$ac_ext
         5958  +rm -f conftest.err conftest.i conftest.$ac_ext
  5952   5959   
  5953   5960     # OK, works on sane cases.  Now check whether nonexistent headers
  5954   5961     # can be detected and how.
  5955   5962     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  5956   5963   /* end confdefs.h.  */
  5957   5964   #include <ac_nonexistent.h>
  5958   5965   _ACEOF
................................................................................
  5960   5967     # Broken: success on invalid input.
  5961   5968   continue
  5962   5969   else
  5963   5970     # Passes both tests.
  5964   5971   ac_preproc_ok=:
  5965   5972   break
  5966   5973   fi
  5967         -rm -f conftest.err conftest.$ac_ext
         5974  +rm -f conftest.err conftest.i conftest.$ac_ext
  5968   5975   
  5969   5976   done
  5970   5977   # Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.
  5971         -rm -f conftest.err conftest.$ac_ext
         5978  +rm -f conftest.i conftest.err conftest.$ac_ext
  5972   5979   if $ac_preproc_ok; then :
  5973   5980   
  5974   5981   else
  5975   5982     { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
  5976   5983   $as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
  5977         -as_fn_error "C preprocessor \"$CPP\" fails sanity check
  5978         -See \`config.log' for more details." "$LINENO" 5; }
         5984  +as_fn_error $? "C preprocessor \"$CPP\" fails sanity check
         5985  +See \`config.log' for more details" "$LINENO" 5 ; }
  5979   5986   fi
  5980   5987   
  5981   5988   ac_ext=c
  5982   5989   ac_cpp='$CPP $CPPFLAGS'
  5983   5990   ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
  5984   5991   ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
  5985   5992   ac_compiler_gnu=$ac_cv_c_compiler_gnu
................................................................................
  6100   6107   # On IRIX 5.3, sys/types and inttypes.h are conflicting.
  6101   6108   for ac_header in sys/types.h sys/stat.h stdlib.h string.h memory.h strings.h \
  6102   6109   		  inttypes.h stdint.h unistd.h
  6103   6110   do :
  6104   6111     as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
  6105   6112   ac_fn_c_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default
  6106   6113   "
  6107         -eval as_val=\$$as_ac_Header
  6108         -   if test "x$as_val" = x""yes; then :
         6114  +if eval test \"x\$"$as_ac_Header"\" = x"yes"; then :
  6109   6115     cat >>confdefs.h <<_ACEOF
  6110   6116   #define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1
  6111   6117   _ACEOF
  6112   6118   
  6113   6119   fi
  6114   6120   
  6115   6121   done
................................................................................
  6622   6628      # Note that $ac_compile itself does not contain backslashes and begins
  6623   6629      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6624   6630      # The option is referenced via a variable to avoid confusing sed.
  6625   6631      lt_compile=`echo "$ac_compile" | $SED \
  6626   6632      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6627   6633      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6628   6634      -e 's:$: $lt_compiler_flag:'`
  6629         -   (eval echo "\"\$as_me:6629: $lt_compile\"" >&5)
         6635  +   (eval echo "\"\$as_me:6635: $lt_compile\"" >&5)
  6630   6636      (eval "$lt_compile" 2>conftest.err)
  6631   6637      ac_status=$?
  6632   6638      cat conftest.err >&5
  6633         -   echo "$as_me:6633: \$? = $ac_status" >&5
         6639  +   echo "$as_me:6639: \$? = $ac_status" >&5
  6634   6640      if (exit $ac_status) && test -s "$ac_outfile"; then
  6635   6641        # The compiler can only warn and ignore the option if not recognized
  6636   6642        # So say no if there are warnings other than the usual output.
  6637   6643        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6638   6644        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6639   6645        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6640   6646          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  6961   6967      # Note that $ac_compile itself does not contain backslashes and begins
  6962   6968      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6963   6969      # The option is referenced via a variable to avoid confusing sed.
  6964   6970      lt_compile=`echo "$ac_compile" | $SED \
  6965   6971      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6966   6972      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6967   6973      -e 's:$: $lt_compiler_flag:'`
  6968         -   (eval echo "\"\$as_me:6968: $lt_compile\"" >&5)
         6974  +   (eval echo "\"\$as_me:6974: $lt_compile\"" >&5)
  6969   6975      (eval "$lt_compile" 2>conftest.err)
  6970   6976      ac_status=$?
  6971   6977      cat conftest.err >&5
  6972         -   echo "$as_me:6972: \$? = $ac_status" >&5
         6978  +   echo "$as_me:6978: \$? = $ac_status" >&5
  6973   6979      if (exit $ac_status) && test -s "$ac_outfile"; then
  6974   6980        # The compiler can only warn and ignore the option if not recognized
  6975   6981        # So say no if there are warnings other than the usual output.
  6976   6982        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6977   6983        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6978   6984        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6979   6985          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7066   7072      # (2) before a word containing "conftest.", or (3) at the end.
  7067   7073      # Note that $ac_compile itself does not contain backslashes and begins
  7068   7074      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7069   7075      lt_compile=`echo "$ac_compile" | $SED \
  7070   7076      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7071   7077      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7072   7078      -e 's:$: $lt_compiler_flag:'`
  7073         -   (eval echo "\"\$as_me:7073: $lt_compile\"" >&5)
         7079  +   (eval echo "\"\$as_me:7079: $lt_compile\"" >&5)
  7074   7080      (eval "$lt_compile" 2>out/conftest.err)
  7075   7081      ac_status=$?
  7076   7082      cat out/conftest.err >&5
  7077         -   echo "$as_me:7077: \$? = $ac_status" >&5
         7083  +   echo "$as_me:7083: \$? = $ac_status" >&5
  7078   7084      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7079   7085      then
  7080   7086        # The compiler can only warn and ignore the option if not recognized
  7081   7087        # So say no if there are warnings
  7082   7088        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7083   7089        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7084   7090        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7121   7127      # (2) before a word containing "conftest.", or (3) at the end.
  7122   7128      # Note that $ac_compile itself does not contain backslashes and begins
  7123   7129      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7124   7130      lt_compile=`echo "$ac_compile" | $SED \
  7125   7131      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7126   7132      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7127   7133      -e 's:$: $lt_compiler_flag:'`
  7128         -   (eval echo "\"\$as_me:7128: $lt_compile\"" >&5)
         7134  +   (eval echo "\"\$as_me:7134: $lt_compile\"" >&5)
  7129   7135      (eval "$lt_compile" 2>out/conftest.err)
  7130   7136      ac_status=$?
  7131   7137      cat out/conftest.err >&5
  7132         -   echo "$as_me:7132: \$? = $ac_status" >&5
         7138  +   echo "$as_me:7138: \$? = $ac_status" >&5
  7133   7139      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7134   7140      then
  7135   7141        # The compiler can only warn and ignore the option if not recognized
  7136   7142        # So say no if there are warnings
  7137   7143        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7138   7144        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7139   7145        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  9501   9507   else
  9502   9508     	  if test "$cross_compiling" = yes; then :
  9503   9509     lt_cv_dlopen_self=cross
  9504   9510   else
  9505   9511     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9506   9512     lt_status=$lt_dlunknown
  9507   9513     cat > conftest.$ac_ext <<_LT_EOF
  9508         -#line 9508 "configure"
         9514  +#line 9514 "configure"
  9509   9515   #include "confdefs.h"
  9510   9516   
  9511   9517   #if HAVE_DLFCN_H
  9512   9518   #include <dlfcn.h>
  9513   9519   #endif
  9514   9520   
  9515   9521   #include <stdio.h>
................................................................................
  9597   9603   else
  9598   9604     	  if test "$cross_compiling" = yes; then :
  9599   9605     lt_cv_dlopen_self_static=cross
  9600   9606   else
  9601   9607     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9602   9608     lt_status=$lt_dlunknown
  9603   9609     cat > conftest.$ac_ext <<_LT_EOF
  9604         -#line 9604 "configure"
         9610  +#line 9610 "configure"
  9605   9611   #include "confdefs.h"
  9606   9612   
  9607   9613   #if HAVE_DLFCN_H
  9608   9614   #include <dlfcn.h>
  9609   9615   #endif
  9610   9616   
  9611   9617   #include <stdio.h>
................................................................................
 10262  10268   
 10263  10269   #########
 10264  10270   # Check for needed/wanted headers
 10265  10271   for ac_header in sys/types.h stdlib.h stdint.h inttypes.h
 10266  10272   do :
 10267  10273     as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
 10268  10274   ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
 10269         -eval as_val=\$$as_ac_Header
 10270         -   if test "x$as_val" = x""yes; then :
        10275  +if eval test \"x\$"$as_ac_Header"\" = x"yes"; then :
 10271  10276     cat >>confdefs.h <<_ACEOF
 10272  10277   #define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1
 10273  10278   _ACEOF
 10274  10279   
 10275  10280   fi
 10276  10281   
 10277  10282   done
................................................................................
 10280  10285   #########
 10281  10286   # Figure out whether or not we have these functions
 10282  10287   #
 10283  10288   for ac_func in usleep fdatasync localtime_r gmtime_r localtime_s utime
 10284  10289   do :
 10285  10290     as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 10286  10291   ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
 10287         -eval as_val=\$$as_ac_var
 10288         -   if test "x$as_val" = x""yes; then :
        10292  +if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
 10289  10293     cat >>confdefs.h <<_ACEOF
 10290  10294   #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
 10291  10295   _ACEOF
 10292  10296   
 10293  10297   fi
 10294  10298   done
 10295  10299   
................................................................................
 10746  10750   else
 10747  10751   
 10748  10752       # First check to see if --with-tcl was specified.
 10749  10753       if test x"${with_tclconfig}" != x ; then
 10750  10754         if test -f "${with_tclconfig}/tclConfig.sh" ; then
 10751  10755           ac_cv_c_tclconfig=`(cd ${with_tclconfig}; pwd)`
 10752  10756         else
 10753         -        as_fn_error "${with_tclconfig} directory doesn't contain tclConfig.sh" "$LINENO" 5
        10757  +        as_fn_error $? "${with_tclconfig} directory doesn't contain tclConfig.sh" "$LINENO" 5
 10754  10758         fi
 10755  10759       fi
 10756  10760   
 10757  10761       # Start autosearch by asking tclsh
 10758  10762       if test x"$cross_compiling" = xno; then
 10759  10763         for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
 10760  10764         do
................................................................................
 11046  11050   			found="no"
 11047  11051   			if test "$cross_compiling" != yes; then
 11048  11052   				for dir in /usr /usr/local /usr/local/readline /usr/contrib /mingw; do
 11049  11053   					for subdir in include include/readline; do
 11050  11054   						as_ac_File=`$as_echo "ac_cv_file_$dir/$subdir/readline.h" | $as_tr_sh`
 11051  11055   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $dir/$subdir/readline.h" >&5
 11052  11056   $as_echo_n "checking for $dir/$subdir/readline.h... " >&6; }
 11053         -if { as_var=$as_ac_File; eval "test \"\${$as_var+set}\" = set"; }; then :
        11057  +if eval "test \"\${$as_ac_File+set}\"" = set; then :
 11054  11058     $as_echo_n "(cached) " >&6
 11055  11059   else
 11056  11060     test "$cross_compiling" = yes &&
 11057         -  as_fn_error "cannot check for file existence when cross compiling" "$LINENO" 5
        11061  +  as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5
 11058  11062   if test -r "$dir/$subdir/readline.h"; then
 11059  11063     eval "$as_ac_File=yes"
 11060  11064   else
 11061  11065     eval "$as_ac_File=no"
 11062  11066   fi
 11063  11067   fi
 11064  11068   eval ac_res=\$$as_ac_File
 11065  11069   	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
 11066  11070   $as_echo "$ac_res" >&6; }
 11067         -eval as_val=\$$as_ac_File
 11068         -   if test "x$as_val" = x""yes; then :
        11071  +if eval test \"x\$"$as_ac_File"\" = x"yes"; then :
 11069  11072     found=yes
 11070  11073   fi
 11071  11074   
 11072  11075   						if test "$found" = "yes"; then
 11073  11076   							TARGET_READLINE_INC="-I$dir/$subdir"
 11074  11077   							break
 11075  11078   						fi
................................................................................
 11418  11421   # Let make expand exec_prefix.
 11419  11422   test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
 11420  11423   
 11421  11424   DEFS=-DHAVE_CONFIG_H
 11422  11425   
 11423  11426   ac_libobjs=
 11424  11427   ac_ltlibobjs=
        11428  +U=
 11425  11429   for ac_i in : $LIBOBJS; do test "x$ac_i" = x: && continue
 11426  11430     # 1. Remove the extension, and $U if already installed.
 11427  11431     ac_script='s/\$U\././;s/\.o$//;s/\.obj$//'
 11428  11432     ac_i=`$as_echo "$ac_i" | sed "$ac_script"`
 11429  11433     # 2. Prepend LIBOBJDIR.  When used with automake>=1.10 LIBOBJDIR
 11430  11434     #    will be set to the directory where LIBOBJS objects are built.
 11431  11435     as_fn_append ac_libobjs " \${LIBOBJDIR}$ac_i\$U.$ac_objext"
................................................................................
 11579  11583   LANGUAGE=C
 11580  11584   export LANGUAGE
 11581  11585   
 11582  11586   # CDPATH.
 11583  11587   (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
 11584  11588   
 11585  11589   
 11586         -# as_fn_error ERROR [LINENO LOG_FD]
 11587         -# ---------------------------------
        11590  +# as_fn_error STATUS ERROR [LINENO LOG_FD]
        11591  +# ----------------------------------------
 11588  11592   # Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are
 11589  11593   # provided, also output the error to LOG_FD, referencing LINENO. Then exit the
 11590         -# script with status $?, using 1 if that was 0.
        11594  +# script with STATUS, using 1 if that was 0.
 11591  11595   as_fn_error ()
 11592  11596   {
 11593         -  as_status=$?; test $as_status -eq 0 && as_status=1
 11594         -  if test "$3"; then
 11595         -    as_lineno=${as_lineno-"$2"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
 11596         -    $as_echo "$as_me:${as_lineno-$LINENO}: error: $1" >&$3
        11597  +  as_status=$1; test $as_status -eq 0 && as_status=1
        11598  +  if test "$4"; then
        11599  +    as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
        11600  +    $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4
 11597  11601     fi
 11598         -  $as_echo "$as_me: error: $1" >&2
        11602  +  $as_echo "$as_me: error: $2" >&2
 11599  11603     as_fn_exit $as_status
 11600  11604   } # as_fn_error
 11601  11605   
 11602  11606   
 11603  11607   # as_fn_set_status STATUS
 11604  11608   # -----------------------
 11605  11609   # Set $? to STATUS, without forking.
................................................................................
 11787  11791   	    s//\1/
 11788  11792   	    q
 11789  11793   	  }
 11790  11794   	  s/.*/./; q'`
 11791  11795         test -d "$as_dir" && break
 11792  11796       done
 11793  11797       test -z "$as_dirs" || eval "mkdir $as_dirs"
 11794         -  } || test -d "$as_dir" || as_fn_error "cannot create directory $as_dir"
        11798  +  } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir"
 11795  11799   
 11796  11800   
 11797  11801   } # as_fn_mkdir_p
 11798  11802   if mkdir -p . 2>/dev/null; then
 11799  11803     as_mkdir_p='mkdir -p "$as_dir"'
 11800  11804   else
 11801  11805     test -d ./-p && rmdir ./-p
................................................................................
 11840  11844   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 11841  11845   
 11842  11846   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 11843  11847   # Save the log message, to keep $0 and so on meaningful, and to
 11844  11848   # report actual input values of CONFIG_FILES etc. instead of their
 11845  11849   # values after options handling.
 11846  11850   ac_log="
 11847         -This file was extended by sqlite $as_me 3.7.8, which was
 11848         -generated by GNU Autoconf 2.65.  Invocation command line was
        11851  +This file was extended by sqlite $as_me 3.7.9, which was
        11852  +generated by GNU Autoconf 2.67.  Invocation command line was
 11849  11853   
 11850  11854     CONFIG_FILES    = $CONFIG_FILES
 11851  11855     CONFIG_HEADERS  = $CONFIG_HEADERS
 11852  11856     CONFIG_LINKS    = $CONFIG_LINKS
 11853  11857     CONFIG_COMMANDS = $CONFIG_COMMANDS
 11854  11858     $ $0 $@
 11855  11859   
................................................................................
 11906  11910   
 11907  11911   Report bugs to the package provider."
 11908  11912   
 11909  11913   _ACEOF
 11910  11914   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 11911  11915   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 11912  11916   ac_cs_version="\\
 11913         -sqlite config.status 3.7.8
 11914         -configured by $0, generated by GNU Autoconf 2.65,
        11917  +sqlite config.status 3.7.9
        11918  +configured by $0, generated by GNU Autoconf 2.67,
 11915  11919     with options \\"\$ac_cs_config\\"
 11916  11920   
 11917         -Copyright (C) 2009 Free Software Foundation, Inc.
        11921  +Copyright (C) 2010 Free Software Foundation, Inc.
 11918  11922   This config.status script is free software; the Free Software Foundation
 11919  11923   gives unlimited permission to copy, distribute and modify it."
 11920  11924   
 11921  11925   ac_pwd='$ac_pwd'
 11922  11926   srcdir='$srcdir'
 11923  11927   INSTALL='$INSTALL'
 11924  11928   AWK='$AWK'
................................................................................
 11927  11931   
 11928  11932   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 11929  11933   # The default lists apply if the user does not specify any file.
 11930  11934   ac_need_defaults=:
 11931  11935   while test $# != 0
 11932  11936   do
 11933  11937     case $1 in
 11934         -  --*=*)
        11938  +  --*=?*)
 11935  11939       ac_option=`expr "X$1" : 'X\([^=]*\)='`
 11936  11940       ac_optarg=`expr "X$1" : 'X[^=]*=\(.*\)'`
 11937  11941       ac_shift=:
 11938  11942       ;;
        11943  +  --*=)
        11944  +    ac_option=`expr "X$1" : 'X\([^=]*\)='`
        11945  +    ac_optarg=
        11946  +    ac_shift=:
        11947  +    ;;
 11939  11948     *)
 11940  11949       ac_option=$1
 11941  11950       ac_optarg=$2
 11942  11951       ac_shift=shift
 11943  11952       ;;
 11944  11953     esac
 11945  11954   
................................................................................
 11953  11962       $as_echo "$ac_cs_config"; exit ;;
 11954  11963     --debug | --debu | --deb | --de | --d | -d )
 11955  11964       debug=: ;;
 11956  11965     --file | --fil | --fi | --f )
 11957  11966       $ac_shift
 11958  11967       case $ac_optarg in
 11959  11968       *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;;
        11969  +    '') as_fn_error $? "missing file argument" ;;
 11960  11970       esac
 11961  11971       as_fn_append CONFIG_FILES " '$ac_optarg'"
 11962  11972       ac_need_defaults=false;;
 11963  11973     --header | --heade | --head | --hea )
 11964  11974       $ac_shift
 11965  11975       case $ac_optarg in
 11966  11976       *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;;
 11967  11977       esac
 11968  11978       as_fn_append CONFIG_HEADERS " '$ac_optarg'"
 11969  11979       ac_need_defaults=false;;
 11970  11980     --he | --h)
 11971  11981       # Conflict between --help and --header
 11972         -    as_fn_error "ambiguous option: \`$1'
        11982  +    as_fn_error $? "ambiguous option: \`$1'
 11973  11983   Try \`$0 --help' for more information.";;
 11974  11984     --help | --hel | -h )
 11975  11985       $as_echo "$ac_cs_usage"; exit ;;
 11976  11986     -q | -quiet | --quiet | --quie | --qui | --qu | --q \
 11977  11987     | -silent | --silent | --silen | --sile | --sil | --si | --s)
 11978  11988       ac_cs_silent=: ;;
 11979  11989   
 11980  11990     # This is an error.
 11981         -  -*) as_fn_error "unrecognized option: \`$1'
        11991  +  -*) as_fn_error $? "unrecognized option: \`$1'
 11982  11992   Try \`$0 --help' for more information." ;;
 11983  11993   
 11984  11994     *) as_fn_append ac_config_targets " $1"
 11985  11995        ac_need_defaults=false ;;
 11986  11996   
 11987  11997     esac
 11988  11998     shift
................................................................................
 12287  12297   do
 12288  12298     case $ac_config_target in
 12289  12299       "libtool") CONFIG_COMMANDS="$CONFIG_COMMANDS libtool" ;;
 12290  12300       "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;;
 12291  12301       "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
 12292  12302       "sqlite3.pc") CONFIG_FILES="$CONFIG_FILES sqlite3.pc" ;;
 12293  12303   
 12294         -  *) as_fn_error "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
        12304  +  *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5 ;;
 12295  12305     esac
 12296  12306   done
 12297  12307   
 12298  12308   
 12299  12309   # If the user did not use the arguments to specify the items to instantiate,
 12300  12310   # then the envvar interface is used.  Set only those that are not.
 12301  12311   # We use the long form for the default assignment because of an extremely
................................................................................
 12325  12335   {
 12326  12336     tmp=`(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null` &&
 12327  12337     test -n "$tmp" && test -d "$tmp"
 12328  12338   }  ||
 12329  12339   {
 12330  12340     tmp=./conf$$-$RANDOM
 12331  12341     (umask 077 && mkdir "$tmp")
 12332         -} || as_fn_error "cannot create a temporary directory in ." "$LINENO" 5
        12342  +} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5
 12333  12343   
 12334  12344   # Set up the scripts for CONFIG_FILES section.
 12335  12345   # No need to generate them if there are no CONFIG_FILES.
 12336  12346   # This happens for instance with `./config.status config.h'.
 12337  12347   if test -n "$CONFIG_FILES"; then
 12338  12348   
 12339  12349   
................................................................................
 12342  12352   # But we know of no other shell where ac_cr would be empty at this
 12343  12353   # point, so we can use a bashism as a fallback.
 12344  12354   if test "x$ac_cr" = x; then
 12345  12355     eval ac_cr=\$\'\\r\'
 12346  12356   fi
 12347  12357   ac_cs_awk_cr=`$AWK 'BEGIN { print "a\rb" }' </dev/null 2>/dev/null`
 12348  12358   if test "$ac_cs_awk_cr" = "a${ac_cr}b"; then
 12349         -  ac_cs_awk_cr='\r'
        12359  +  ac_cs_awk_cr='\\r'
 12350  12360   else
 12351  12361     ac_cs_awk_cr=$ac_cr
 12352  12362   fi
 12353  12363   
 12354  12364   echo 'BEGIN {' >"$tmp/subs1.awk" &&
 12355  12365   _ACEOF
 12356  12366   
 12357  12367   
 12358  12368   {
 12359  12369     echo "cat >conf$$subs.awk <<_ACEOF" &&
 12360  12370     echo "$ac_subst_vars" | sed 's/.*/&!$&$ac_delim/' &&
 12361  12371     echo "_ACEOF"
 12362  12372   } >conf$$subs.sh ||
 12363         -  as_fn_error "could not make $CONFIG_STATUS" "$LINENO" 5
 12364         -ac_delim_num=`echo "$ac_subst_vars" | grep -c '$'`
        12373  +  as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
        12374  +ac_delim_num=`echo "$ac_subst_vars" | grep -c '^'`
 12365  12375   ac_delim='%!_!# '
 12366  12376   for ac_last_try in false false false false false :; do
 12367  12377     . ./conf$$subs.sh ||
 12368         -    as_fn_error "could not make $CONFIG_STATUS" "$LINENO" 5
        12378  +    as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
 12369  12379   
 12370  12380     ac_delim_n=`sed -n "s/.*$ac_delim\$/X/p" conf$$subs.awk | grep -c X`
 12371  12381     if test $ac_delim_n = $ac_delim_num; then
 12372  12382       break
 12373  12383     elif $ac_last_try; then
 12374         -    as_fn_error "could not make $CONFIG_STATUS" "$LINENO" 5
        12384  +    as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
 12375  12385     else
 12376  12386       ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
 12377  12387     fi
 12378  12388   done
 12379  12389   rm -f conf$$subs.sh
 12380  12390   
 12381  12391   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
................................................................................
 12456  12466   _ACEOF
 12457  12467   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12458  12468   if sed "s/$ac_cr//" < /dev/null > /dev/null 2>&1; then
 12459  12469     sed "s/$ac_cr\$//; s/$ac_cr/$ac_cs_awk_cr/g"
 12460  12470   else
 12461  12471     cat
 12462  12472   fi < "$tmp/subs1.awk" > "$tmp/subs.awk" \
 12463         -  || as_fn_error "could not setup config files machinery" "$LINENO" 5
        12473  +  || as_fn_error $? "could not setup config files machinery" "$LINENO" 5
 12464  12474   _ACEOF
 12465  12475   
 12466         -# VPATH may cause trouble with some makes, so we remove $(srcdir),
 12467         -# ${srcdir} and @srcdir@ from VPATH if srcdir is ".", strip leading and
        12476  +# VPATH may cause trouble with some makes, so we remove sole $(srcdir),
        12477  +# ${srcdir} and @srcdir@ entries from VPATH if srcdir is ".", strip leading and
 12468  12478   # trailing colons and then remove the whole line if VPATH becomes empty
 12469  12479   # (actually we leave an empty line to preserve line numbers).
 12470  12480   if test "x$srcdir" = x.; then
 12471         -  ac_vpsub='/^[	 ]*VPATH[	 ]*=/{
 12472         -s/:*\$(srcdir):*/:/
 12473         -s/:*\${srcdir}:*/:/
 12474         -s/:*@srcdir@:*/:/
 12475         -s/^\([^=]*=[	 ]*\):*/\1/
        12481  +  ac_vpsub='/^[	 ]*VPATH[	 ]*=[	 ]*/{
        12482  +h
        12483  +s///
        12484  +s/^/:/
        12485  +s/[	 ]*$/:/
        12486  +s/:\$(srcdir):/:/g
        12487  +s/:\${srcdir}:/:/g
        12488  +s/:@srcdir@:/:/g
        12489  +s/^:*//
 12476  12490   s/:*$//
        12491  +x
        12492  +s/\(=[	 ]*\).*/\1/
        12493  +G
        12494  +s/\n//
 12477  12495   s/^[^=]*=[	 ]*$//
 12478  12496   }'
 12479  12497   fi
 12480  12498   
 12481  12499   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12482  12500   fi # test -n "$CONFIG_FILES"
 12483  12501   
................................................................................
 12497  12515   # handling of long lines.
 12498  12516   ac_delim='%!_!# '
 12499  12517   for ac_last_try in false false :; do
 12500  12518     ac_t=`sed -n "/$ac_delim/p" confdefs.h`
 12501  12519     if test -z "$ac_t"; then
 12502  12520       break
 12503  12521     elif $ac_last_try; then
 12504         -    as_fn_error "could not make $CONFIG_HEADERS" "$LINENO" 5
        12522  +    as_fn_error $? "could not make $CONFIG_HEADERS" "$LINENO" 5
 12505  12523     else
 12506  12524       ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
 12507  12525     fi
 12508  12526   done
 12509  12527   
 12510  12528   # For the awk script, D is an array of macro values keyed by name,
 12511  12529   # likewise P contains macro parameters if any.  Preserve backslash
................................................................................
 12582  12600       }
 12583  12601     }
 12584  12602   }
 12585  12603   { print }
 12586  12604   _ACAWK
 12587  12605   _ACEOF
 12588  12606   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12589         -  as_fn_error "could not setup config headers machinery" "$LINENO" 5
        12607  +  as_fn_error $? "could not setup config headers machinery" "$LINENO" 5
 12590  12608   fi # test -n "$CONFIG_HEADERS"
 12591  12609   
 12592  12610   
 12593  12611   eval set X "  :F $CONFIG_FILES  :H $CONFIG_HEADERS    :C $CONFIG_COMMANDS"
 12594  12612   shift
 12595  12613   for ac_tag
 12596  12614   do
 12597  12615     case $ac_tag in
 12598  12616     :[FHLC]) ac_mode=$ac_tag; continue;;
 12599  12617     esac
 12600  12618     case $ac_mode$ac_tag in
 12601  12619     :[FHL]*:*);;
 12602         -  :L* | :C*:*) as_fn_error "invalid tag \`$ac_tag'" "$LINENO" 5;;
        12620  +  :L* | :C*:*) as_fn_error $? "invalid tag \`$ac_tag'" "$LINENO" 5 ;;
 12603  12621     :[FH]-) ac_tag=-:-;;
 12604  12622     :[FH]*) ac_tag=$ac_tag:$ac_tag.in;;
 12605  12623     esac
 12606  12624     ac_save_IFS=$IFS
 12607  12625     IFS=:
 12608  12626     set x $ac_tag
 12609  12627     IFS=$ac_save_IFS
................................................................................
 12623  12641   	 # (if the path is not absolute).  The absolute path cannot be DOS-style,
 12624  12642   	 # because $ac_f cannot contain `:'.
 12625  12643   	 test -f "$ac_f" ||
 12626  12644   	   case $ac_f in
 12627  12645   	   [\\/$]*) false;;
 12628  12646   	   *) test -f "$srcdir/$ac_f" && ac_f="$srcdir/$ac_f";;
 12629  12647   	   esac ||
 12630         -	   as_fn_error "cannot find input file: \`$ac_f'" "$LINENO" 5;;
        12648  +	   as_fn_error 1 "cannot find input file: \`$ac_f'" "$LINENO" 5 ;;
 12631  12649         esac
 12632  12650         case $ac_f in *\'*) ac_f=`$as_echo "$ac_f" | sed "s/'/'\\\\\\\\''/g"`;; esac
 12633  12651         as_fn_append ac_file_inputs " '$ac_f'"
 12634  12652       done
 12635  12653   
 12636  12654       # Let's still pretend it is `configure' which instantiates (i.e., don't
 12637  12655       # use $as_me), people would be surprised to read:
................................................................................
 12650  12668          ac_sed_conf_input=`$as_echo "$configure_input" |
 12651  12669          sed 's/[\\\\&|]/\\\\&/g'`;; #(
 12652  12670       *) ac_sed_conf_input=$configure_input;;
 12653  12671       esac
 12654  12672   
 12655  12673       case $ac_tag in
 12656  12674       *:-:* | *:-) cat >"$tmp/stdin" \
 12657         -      || as_fn_error "could not create $ac_file" "$LINENO" 5 ;;
        12675  +      || as_fn_error $? "could not create $ac_file" "$LINENO" 5  ;;
 12658  12676       esac
 12659  12677       ;;
 12660  12678     esac
 12661  12679   
 12662  12680     ac_dir=`$as_dirname -- "$ac_file" ||
 12663  12681   $as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
 12664  12682   	 X"$ac_file" : 'X\(//\)[^/]' \| \
................................................................................
 12781  12799   s&@builddir@&$ac_builddir&;t t
 12782  12800   s&@abs_builddir@&$ac_abs_builddir&;t t
 12783  12801   s&@abs_top_builddir@&$ac_abs_top_builddir&;t t
 12784  12802   s&@INSTALL@&$ac_INSTALL&;t t
 12785  12803   $ac_datarootdir_hack
 12786  12804   "
 12787  12805   eval sed \"\$ac_sed_extra\" "$ac_file_inputs" | $AWK -f "$tmp/subs.awk" >$tmp/out \
 12788         -  || as_fn_error "could not create $ac_file" "$LINENO" 5
        12806  +  || as_fn_error $? "could not create $ac_file" "$LINENO" 5
 12789  12807   
 12790  12808   test -z "$ac_datarootdir_hack$ac_datarootdir_seen" &&
 12791  12809     { ac_out=`sed -n '/\${datarootdir}/p' "$tmp/out"`; test -n "$ac_out"; } &&
 12792  12810     { ac_out=`sed -n '/^[	 ]*datarootdir[	 ]*:*=/p' "$tmp/out"`; test -z "$ac_out"; } &&
 12793  12811     { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $ac_file contains a reference to the variable \`datarootdir'
 12794         -which seems to be undefined.  Please make sure it is defined." >&5
        12812  +which seems to be undefined.  Please make sure it is defined" >&5
 12795  12813   $as_echo "$as_me: WARNING: $ac_file contains a reference to the variable \`datarootdir'
 12796         -which seems to be undefined.  Please make sure it is defined." >&2;}
        12814  +which seems to be undefined.  Please make sure it is defined" >&2;}
 12797  12815   
 12798  12816     rm -f "$tmp/stdin"
 12799  12817     case $ac_file in
 12800  12818     -) cat "$tmp/out" && rm -f "$tmp/out";;
 12801  12819     *) rm -f "$ac_file" && mv "$tmp/out" "$ac_file";;
 12802  12820     esac \
 12803         -  || as_fn_error "could not create $ac_file" "$LINENO" 5
        12821  +  || as_fn_error $? "could not create $ac_file" "$LINENO" 5
 12804  12822    ;;
 12805  12823     :H)
 12806  12824     #
 12807  12825     # CONFIG_HEADER
 12808  12826     #
 12809  12827     if test x"$ac_file" != x-; then
 12810  12828       {
 12811  12829         $as_echo "/* $configure_input  */" \
 12812  12830         && eval '$AWK -f "$tmp/defines.awk"' "$ac_file_inputs"
 12813  12831       } >"$tmp/config.h" \
 12814         -      || as_fn_error "could not create $ac_file" "$LINENO" 5
        12832  +      || as_fn_error $? "could not create $ac_file" "$LINENO" 5
 12815  12833       if diff "$ac_file" "$tmp/config.h" >/dev/null 2>&1; then
 12816  12834         { $as_echo "$as_me:${as_lineno-$LINENO}: $ac_file is unchanged" >&5
 12817  12835   $as_echo "$as_me: $ac_file is unchanged" >&6;}
 12818  12836       else
 12819  12837         rm -f "$ac_file"
 12820  12838         mv "$tmp/config.h" "$ac_file" \
 12821         -	|| as_fn_error "could not create $ac_file" "$LINENO" 5
        12839  +	|| as_fn_error $? "could not create $ac_file" "$LINENO" 5
 12822  12840       fi
 12823  12841     else
 12824  12842       $as_echo "/* $configure_input  */" \
 12825  12843         && eval '$AWK -f "$tmp/defines.awk"' "$ac_file_inputs" \
 12826         -      || as_fn_error "could not create -" "$LINENO" 5
        12844  +      || as_fn_error $? "could not create -" "$LINENO" 5
 12827  12845     fi
 12828  12846    ;;
 12829  12847   
 12830  12848     :C)  { $as_echo "$as_me:${as_lineno-$LINENO}: executing $ac_file commands" >&5
 12831  12849   $as_echo "$as_me: executing $ac_file commands" >&6;}
 12832  12850    ;;
 12833  12851     esac
................................................................................
 13475  13493   
 13476  13494   
 13477  13495   as_fn_exit 0
 13478  13496   _ACEOF
 13479  13497   ac_clean_files=$ac_clean_files_save
 13480  13498   
 13481  13499   test $ac_write_fail = 0 ||
 13482         -  as_fn_error "write failure creating $CONFIG_STATUS" "$LINENO" 5
        13500  +  as_fn_error $? "write failure creating $CONFIG_STATUS" "$LINENO" 5
 13483  13501   
 13484  13502   
 13485  13503   # configure is writing to config.log, and then calls config.status.
 13486  13504   # config.status does its own redirection, appending to config.log.
 13487  13505   # Unfortunately, on DOS this fails, as config.log is still kept open
 13488  13506   # by configure, so config.status won't be able to write to it; its
 13489  13507   # output is simply discarded.  So we exec the FD to /dev/null,
................................................................................
 13496  13514     test "$silent" = yes &&
 13497  13515       ac_config_status_args="$ac_config_status_args --quiet"
 13498  13516     exec 5>/dev/null
 13499  13517     $SHELL $CONFIG_STATUS $ac_config_status_args || ac_cs_success=false
 13500  13518     exec 5>>config.log
 13501  13519     # Use ||, not &&, to avoid exiting from the if with $? = 1, which
 13502  13520     # would make configure fail if this is the last instruction.
 13503         -  $ac_cs_success || as_fn_exit $?
        13521  +  $ac_cs_success || as_fn_exit 1
 13504  13522   fi
 13505  13523   if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then
 13506  13524     { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5
 13507  13525   $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
 13508  13526   fi
 13509  13527   

Changes to main.mk.

   518    518   
   519    519   # Rules for building test programs and for running tests
   520    520   #
   521    521   tclsqlite3:	$(TOP)/src/tclsqlite.c libsqlite3.a
   522    522   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \
   523    523   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)
   524    524   
          525  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
          526  +	echo "#define TCLSH 2" > $@
          527  +	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@
          528  +	echo "static const char *tclsh_main_loop(void){" >> $@
          529  +	echo "static const char *zMainloop = " >> $@
          530  +	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl >> $@
          531  +	echo "; return zMainloop; }" >> $@
          532  +
          533  +sqlite3_analyzer$(EXE): sqlite3_analyzer.c
          534  +	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
   525    535   
   526    536   # Rules to build the 'testfixture' application.
   527    537   #
   528    538   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   529    539   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE 
   530    540   
   531    541   testfixture$(EXE): $(TESTSRC2) libsqlite3.a $(TESTSRC) $(TOP)/src/tclsqlite.c
................................................................................
   560    570   threadtest3$(EXE): sqlite3.o $(TOP)/test/threadtest3.c $(TOP)/test/tt3_checkpoint.c
   561    571   	$(TCCX) -O2 sqlite3.o $(TOP)/test/threadtest3.c \
   562    572   		-o threadtest3$(EXE) $(THREADLIB)
   563    573   
   564    574   threadtest: threadtest3$(EXE)
   565    575   	./threadtest3$(EXE)
   566    576   
   567         -sqlite3_analyzer$(EXE):	$(TOP)/src/tclsqlite.c sqlite3.c $(TESTSRC) \
   568         -			$(TOP)/tool/spaceanal.tcl
   569         -	$(NAWK) -f $(TOP)/tool/tostr.awk $(TOP)/tool/spaceanal.tcl \
   570         -		 >spaceanal_tcl.h
   571         -	$(TCCX) $(TCL_FLAGS) -DTCLSH=2 $(TESTFIXTURE_FLAGS)                    \
   572         -		-DSQLITE_TEST=1 -DSQLITE_PRIVATE=""                            \
   573         -		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c                    \
   574         -		-o sqlite3_analyzer$(EXE)                                      \
   575         -		$(LIBTCL) $(THREADLIB)
   576         -
   577    577   TEST_EXTENSION = $(SHPREFIX)testloadext.$(SO)
   578    578   $(TEST_EXTENSION): $(TOP)/src/test_loadext.c
   579    579   	$(MKSHLIB) $(TOP)/src/test_loadext.c -o $(TEST_EXTENSION)
   580    580   
   581    581   extensiontest: testfixture$(EXE) $(TEST_EXTENSION)
   582    582   	./testfixture$(EXE) $(TOP)/test/loadext.test
   583    583   
................................................................................
   593    593   #
   594    594   install:	sqlite3 libsqlite3.a sqlite3.h
   595    595   	mv sqlite3 /usr/bin
   596    596   	mv libsqlite3.a /usr/lib
   597    597   	mv sqlite3.h /usr/include
   598    598   
   599    599   clean:	
   600         -	rm -f *.o sqlite3 libsqlite3.a sqlite3.h opcodes.*
          600  +	rm -f *.o sqlite3 sqlite3.exe libsqlite3.a sqlite3.h opcodes.*
   601    601   	rm -f lemon lempar.c parse.* sqlite*.tar.gz mkkeywordhash keywordhash.h
   602    602   	rm -f $(PUBLISH)
   603    603   	rm -f *.da *.bb *.bbg gmon.out
   604    604   	rm -rf tsrc target_source
   605    605   	rm -f testloadext.dll libtestloadext.so
          606  +	rm -f amalgamation-testfixture amalgamation-testfixture.exe
          607  +	rm -f fts3-testfixture fts3-testfixture.exe
          608  +	rm -f testfixture testfixture.exe
          609  +	rm -f threadtest3 threadtest3.exe
   606    610   	rm -f sqlite3.c fts?amal.c tclsqlite3.c
          611  +	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c

Changes to src/analyze.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
           13  +**
           14  +** The ANALYZE command gather statistics about the content of tables
           15  +** and indices.  These statistics are made available to the query planner
           16  +** to help it make better decisions about how to perform queries.
           17  +**
           18  +** The following system tables are or have been supported:
           19  +**
           20  +**    CREATE TABLE sqlite_stat1(tbl, idx, stat);
           21  +**    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
           22  +**    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
           23  +**
           24  +** Additional tables might be added in future releases of SQLite.
           25  +** The sqlite_stat2 table is not created or used unless the SQLite version
           26  +** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
           27  +** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
           28  +** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
           29  +** created and used by SQLite versions 3.7.9 and later and with
           30  +** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
           31  +** is a superset of sqlite_stat2.  
           32  +**
           33  +** Format of sqlite_stat1:
           34  +**
           35  +** There is normally one row per index, with the index identified by the
           36  +** name in the idx column.  The tbl column is the name of the table to
           37  +** which the index belongs.  In each such row, the stat column will be
           38  +** a string consisting of a list of integers.  The first integer in this
           39  +** list is the number of rows in the index and in the table.  The second
           40  +** integer is the average number of rows in the index that have the same
           41  +** value in the first column of the index.  The third integer is the average
           42  +** number of rows in the index that have the same value for the first two
           43  +** columns.  The N-th integer (for N>1) is the average number of rows in 
           44  +** the index which have the same value for the first N-1 columns.  For
           45  +** a K-column index, there will be K+1 integers in the stat column.  If
           46  +** the index is unique, then the last integer will be 1.
           47  +**
           48  +** The list of integers in the stat column can optionally be followed
           49  +** by the keyword "unordered".  The "unordered" keyword, if it is present,
           50  +** must be separated from the last integer by a single space.  If the
           51  +** "unordered" keyword is present, then the query planner assumes that
           52  +** the index is unordered and will not use the index for a range query.
           53  +** 
           54  +** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
           55  +** column contains a single integer which is the (estimated) number of
           56  +** rows in the table identified by sqlite_stat1.tbl.
           57  +**
           58  +** Format of sqlite_stat2:
           59  +**
           60  +** The sqlite_stat2 is only created and is only used if SQLite is compiled
           61  +** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
           62  +** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
           63  +** about the distribution of keys within an index.  The index is identified by
           64  +** the "idx" column and the "tbl" column is the name of the table to which
           65  +** the index belongs.  There are usually 10 rows in the sqlite_stat2
           66  +** table for each index.
           67  +**
           68  +** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
           69  +** inclusive are samples of the left-most key value in the index taken at
           70  +** evenly spaced points along the index.  Let the number of samples be S
           71  +** (10 in the standard build) and let C be the number of rows in the index.
           72  +** Then the sampled rows are given by:
           73  +**
           74  +**     rownumber = (i*C*2 + C)/(S*2)
           75  +**
           76  +** For i between 0 and S-1.  Conceptually, the index space is divided into
           77  +** S uniform buckets and the samples are the middle row from each bucket.
           78  +**
           79  +** The format for sqlite_stat2 is recorded here for legacy reference.  This
           80  +** version of SQLite does not support sqlite_stat2.  It neither reads nor
           81  +** writes the sqlite_stat2 table.  This version of SQLite only supports
           82  +** sqlite_stat3.
           83  +**
           84  +** Format for sqlite_stat3:
           85  +**
           86  +** The sqlite_stat3 is an enhancement to sqlite_stat2.  A new name is
           87  +** used to avoid compatibility problems.  
           88  +**
           89  +** The format of the sqlite_stat3 table is similar to the format of
           90  +** the sqlite_stat2 table.  There are multiple entries for each index.
           91  +** The idx column names the index and the tbl column is the table of the
           92  +** index.  If the idx and tbl columns are the same, then the sample is
           93  +** of the INTEGER PRIMARY KEY.  The sample column is a value taken from
           94  +** the left-most column of the index.  The nEq column is the approximate
           95  +** number of entires in the index whose left-most column exactly matches
           96  +** the sample.  nLt is the approximate number of entires whose left-most
           97  +** column is less than the sample.  The nDLt column is the approximate
           98  +** number of distinct left-most entries in the index that are less than
           99  +** the sample.
          100  +**
          101  +** Future versions of SQLite might change to store a string containing
          102  +** multiple integers values in the nDLt column of sqlite_stat3.  The first
          103  +** integer will be the number of prior index entires that are distinct in
          104  +** the left-most column.  The second integer will be the number of prior index
          105  +** entries that are distinct in the first two columns.  The third integer
          106  +** will be the number of prior index entries that are distinct in the first
          107  +** three columns.  And so forth.  With that extension, the nDLt field is
          108  +** similar in function to the sqlite_stat1.stat field.
          109  +**
          110  +** There can be an arbitrary number of sqlite_stat3 entries per index.
          111  +** The ANALYZE command will typically generate sqlite_stat3 tables
          112  +** that contain between 10 and 40 samples which are distributed across
          113  +** the key space, though not uniformly, and which include samples with
          114  +** largest possible nEq values.
    13    115   */
    14    116   #ifndef SQLITE_OMIT_ANALYZE
    15    117   #include "sqliteInt.h"
    16    118   
    17    119   /*
    18    120   ** This routine generates code that opens the sqlite_stat1 table for
    19    121   ** writing with cursor iStatCur. If the library was built with the
................................................................................
    38    140     const char *zWhereType  /* Either "tbl" or "idx" */
    39    141   ){
    40    142     static const struct {
    41    143       const char *zName;
    42    144       const char *zCols;
    43    145     } aTable[] = {
    44    146       { "sqlite_stat1", "tbl,idx,stat" },
    45         -#ifdef SQLITE_ENABLE_STAT2
    46         -    { "sqlite_stat2", "tbl,idx,sampleno,sample" },
          147  +#ifdef SQLITE_ENABLE_STAT3
          148  +    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
    47    149   #endif
    48    150     };
    49    151   
    50    152     int aRoot[] = {0, 0};
    51    153     u8 aCreateTbl[] = {0, 0};
    52    154   
    53    155     int i;
................................................................................
    55    157     Db *pDb;
    56    158     Vdbe *v = sqlite3GetVdbe(pParse);
    57    159     if( v==0 ) return;
    58    160     assert( sqlite3BtreeHoldsAllMutexes(db) );
    59    161     assert( sqlite3VdbeDb(v)==db );
    60    162     pDb = &db->aDb[iDb];
    61    163   
          164  +  /* Create new statistic tables if they do not exist, or clear them
          165  +  ** if they do already exist.
          166  +  */
    62    167     for(i=0; i<ArraySize(aTable); i++){
    63    168       const char *zTab = aTable[i].zName;
    64    169       Table *pStat;
    65    170       if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
    66    171         /* The sqlite_stat[12] table does not exist. Create it. Note that a 
    67    172         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
    68    173         ** of the new table in register pParse->regRoot. This is important 
................................................................................
    85    190         }else{
    86    191           /* The sqlite_stat[12] table already exists.  Delete all rows. */
    87    192           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
    88    193         }
    89    194       }
    90    195     }
    91    196   
    92         -  /* Open the sqlite_stat[12] tables for writing. */
          197  +  /* Open the sqlite_stat[13] tables for writing. */
    93    198     for(i=0; i<ArraySize(aTable); i++){
    94    199       sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
    95    200       sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
    96    201       sqlite3VdbeChangeP5(v, aCreateTbl[i]);
    97    202     }
    98    203   }
          204  +
          205  +/*
          206  +** Recommended number of samples for sqlite_stat3
          207  +*/
          208  +#ifndef SQLITE_STAT3_SAMPLES
          209  +# define SQLITE_STAT3_SAMPLES 24
          210  +#endif
          211  +
          212  +/*
          213  +** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
          214  +** share an instance of the following structure to hold their state
          215  +** information.
          216  +*/
          217  +typedef struct Stat3Accum Stat3Accum;
          218  +struct Stat3Accum {
          219  +  tRowcnt nRow;             /* Number of rows in the entire table */
          220  +  tRowcnt nPSample;         /* How often to do a periodic sample */
          221  +  int iMin;                 /* Index of entry with minimum nEq and hash */
          222  +  int mxSample;             /* Maximum number of samples to accumulate */
          223  +  int nSample;              /* Current number of samples */
          224  +  u32 iPrn;                 /* Pseudo-random number used for sampling */
          225  +  struct Stat3Sample {
          226  +    i64 iRowid;                /* Rowid in main table of the key */
          227  +    tRowcnt nEq;               /* sqlite_stat3.nEq */
          228  +    tRowcnt nLt;               /* sqlite_stat3.nLt */
          229  +    tRowcnt nDLt;              /* sqlite_stat3.nDLt */
          230  +    u8 isPSample;              /* True if a periodic sample */
          231  +    u32 iHash;                 /* Tiebreaker hash */
          232  +  } *a;                     /* An array of samples */
          233  +};
          234  +
          235  +#ifdef SQLITE_ENABLE_STAT3
          236  +/*
          237  +** Implementation of the stat3_init(C,S) SQL function.  The two parameters
          238  +** are the number of rows in the table or index (C) and the number of samples
          239  +** to accumulate (S).
          240  +**
          241  +** This routine allocates the Stat3Accum object.
          242  +**
          243  +** The return value is the Stat3Accum object (P).
          244  +*/
          245  +static void stat3Init(
          246  +  sqlite3_context *context,
          247  +  int argc,
          248  +  sqlite3_value **argv
          249  +){
          250  +  Stat3Accum *p;
          251  +  tRowcnt nRow;
          252  +  int mxSample;
          253  +  int n;
          254  +
          255  +  UNUSED_PARAMETER(argc);
          256  +  nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
          257  +  mxSample = sqlite3_value_int(argv[1]);
          258  +  n = sizeof(*p) + sizeof(p->a[0])*mxSample;
          259  +  p = sqlite3_malloc( n );
          260  +  if( p==0 ){
          261  +    sqlite3_result_error_nomem(context);
          262  +    return;
          263  +  }
          264  +  memset(p, 0, n);
          265  +  p->a = (struct Stat3Sample*)&p[1];
          266  +  p->nRow = nRow;
          267  +  p->mxSample = mxSample;
          268  +  p->nPSample = p->nRow/(mxSample/3+1) + 1;
          269  +  sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
          270  +  sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
          271  +}
          272  +static const FuncDef stat3InitFuncdef = {
          273  +  2,                /* nArg */
          274  +  SQLITE_UTF8,      /* iPrefEnc */
          275  +  0,                /* flags */
          276  +  0,                /* pUserData */
          277  +  0,                /* pNext */
          278  +  stat3Init,        /* xFunc */
          279  +  0,                /* xStep */
          280  +  0,                /* xFinalize */
          281  +  "stat3_init",     /* zName */
          282  +  0,                /* pHash */
          283  +  0                 /* pDestructor */
          284  +};
          285  +
          286  +
          287  +/*
          288  +** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function.  The
          289  +** arguments describe a single key instance.  This routine makes the 
          290  +** decision about whether or not to retain this key for the sqlite_stat3
          291  +** table.
          292  +**
          293  +** The return value is NULL.
          294  +*/
          295  +static void stat3Push(
          296  +  sqlite3_context *context,
          297  +  int argc,
          298  +  sqlite3_value **argv
          299  +){
          300  +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
          301  +  tRowcnt nEq = sqlite3_value_int64(argv[0]);
          302  +  tRowcnt nLt = sqlite3_value_int64(argv[1]);
          303  +  tRowcnt nDLt = sqlite3_value_int64(argv[2]);
          304  +  i64 rowid = sqlite3_value_int64(argv[3]);
          305  +  u8 isPSample = 0;
          306  +  u8 doInsert = 0;
          307  +  int iMin = p->iMin;
          308  +  struct Stat3Sample *pSample;
          309  +  int i;
          310  +  u32 h;
          311  +
          312  +  UNUSED_PARAMETER(context);
          313  +  UNUSED_PARAMETER(argc);
          314  +  if( nEq==0 ) return;
          315  +  h = p->iPrn = p->iPrn*1103515245 + 12345;
          316  +  if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
          317  +    doInsert = isPSample = 1;
          318  +  }else if( p->nSample<p->mxSample ){
          319  +    doInsert = 1;
          320  +  }else{
          321  +    if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
          322  +      doInsert = 1;
          323  +    }
          324  +  }
          325  +  if( !doInsert ) return;
          326  +  if( p->nSample==p->mxSample ){
          327  +    assert( p->nSample - iMin - 1 >= 0 );
          328  +    memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
          329  +    pSample = &p->a[p->nSample-1];
          330  +  }else{
          331  +    pSample = &p->a[p->nSample++];
          332  +  }
          333  +  pSample->iRowid = rowid;
          334  +  pSample->nEq = nEq;
          335  +  pSample->nLt = nLt;
          336  +  pSample->nDLt = nDLt;
          337  +  pSample->iHash = h;
          338  +  pSample->isPSample = isPSample;
          339  +
          340  +  /* Find the new minimum */
          341  +  if( p->nSample==p->mxSample ){
          342  +    pSample = p->a;
          343  +    i = 0;
          344  +    while( pSample->isPSample ){
          345  +      i++;
          346  +      pSample++;
          347  +      assert( i<p->nSample );
          348  +    }
          349  +    nEq = pSample->nEq;
          350  +    h = pSample->iHash;
          351  +    iMin = i;
          352  +    for(i++, pSample++; i<p->nSample; i++, pSample++){
          353  +      if( pSample->isPSample ) continue;
          354  +      if( pSample->nEq<nEq
          355  +       || (pSample->nEq==nEq && pSample->iHash<h)
          356  +      ){
          357  +        iMin = i;
          358  +        nEq = pSample->nEq;
          359  +        h = pSample->iHash;
          360  +      }
          361  +    }
          362  +    p->iMin = iMin;
          363  +  }
          364  +}
          365  +static const FuncDef stat3PushFuncdef = {
          366  +  5,                /* nArg */
          367  +  SQLITE_UTF8,      /* iPrefEnc */
          368  +  0,                /* flags */
          369  +  0,                /* pUserData */
          370  +  0,                /* pNext */
          371  +  stat3Push,        /* xFunc */
          372  +  0,                /* xStep */
          373  +  0,                /* xFinalize */
          374  +  "stat3_push",     /* zName */
          375  +  0,                /* pHash */
          376  +  0                 /* pDestructor */
          377  +};
          378  +
          379  +/*
          380  +** Implementation of the stat3_get(P,N,...) SQL function.  This routine is
          381  +** used to query the results.  Content is returned for the Nth sqlite_stat3
          382  +** row where N is between 0 and S-1 and S is the number of samples.  The
          383  +** value returned depends on the number of arguments.
          384  +**
          385  +**   argc==2    result:  rowid
          386  +**   argc==3    result:  nEq
          387  +**   argc==4    result:  nLt
          388  +**   argc==5    result:  nDLt
          389  +*/
          390  +static void stat3Get(
          391  +  sqlite3_context *context,
          392  +  int argc,
          393  +  sqlite3_value **argv
          394  +){
          395  +  int n = sqlite3_value_int(argv[1]);
          396  +  Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
          397  +
          398  +  assert( p!=0 );
          399  +  if( p->nSample<=n ) return;
          400  +  switch( argc ){
          401  +    case 2:  sqlite3_result_int64(context, p->a[n].iRowid); break;
          402  +    case 3:  sqlite3_result_int64(context, p->a[n].nEq);    break;
          403  +    case 4:  sqlite3_result_int64(context, p->a[n].nLt);    break;
          404  +    default: sqlite3_result_int64(context, p->a[n].nDLt);   break;
          405  +  }
          406  +}
          407  +static const FuncDef stat3GetFuncdef = {
          408  +  -1,               /* nArg */
          409  +  SQLITE_UTF8,      /* iPrefEnc */
          410  +  0,                /* flags */
          411  +  0,                /* pUserData */
          412  +  0,                /* pNext */
          413  +  stat3Get,         /* xFunc */
          414  +  0,                /* xStep */
          415  +  0,                /* xFinalize */
          416  +  "stat3_get",     /* zName */
          417  +  0,                /* pHash */
          418  +  0                 /* pDestructor */
          419  +};
          420  +#endif /* SQLITE_ENABLE_STAT3 */
          421  +
          422  +
          423  +
    99    424   
   100    425   /*
   101    426   ** Generate code to do an analysis of all indices associated with
   102    427   ** a single table.
   103    428   */
   104    429   static void analyzeOneTable(
   105    430     Parse *pParse,   /* Parser context */
................................................................................
   115    440     int i;                       /* Loop counter */
   116    441     int topOfLoop;               /* The top of the loop */
   117    442     int endOfLoop;               /* The end of the loop */
   118    443     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   119    444     int iDb;                     /* Index of database containing pTab */
   120    445     int regTabname = iMem++;     /* Register containing table name */
   121    446     int regIdxname = iMem++;     /* Register containing index name */
   122         -  int regSampleno = iMem++;    /* Register containing next sample number */
   123         -  int regCol = iMem++;         /* Content of a column analyzed table */
          447  +  int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
          448  +#ifdef SQLITE_ENABLE_STAT3
          449  +  int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
          450  +  int regNumLt = iMem++;       /* Number of keys less than regSample */
          451  +  int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
          452  +  int regSample = iMem++;      /* The next sample value */
          453  +  int regRowid = regSample;    /* Rowid of a sample */
          454  +  int regAccum = iMem++;       /* Register to hold Stat3Accum object */
          455  +  int regLoop = iMem++;        /* Loop counter */
          456  +  int regCount = iMem++;       /* Number of rows in the table or index */
          457  +  int regTemp1 = iMem++;       /* Intermediate register */
          458  +  int regTemp2 = iMem++;       /* Intermediate register */
          459  +  int once = 1;                /* One-time initialization */
          460  +  int shortJump = 0;           /* Instruction address */
          461  +  int iTabCur = pParse->nTab++; /* Table cursor */
          462  +#endif
          463  +  int regCol = iMem++;         /* Content of a column in analyzed table */
   124    464     int regRec = iMem++;         /* Register holding completed record */
   125    465     int regTemp = iMem++;        /* Temporary use register */
   126         -  int regRowid = iMem++;       /* Rowid for the inserted record */
          466  +  int regNewRowid = iMem++;    /* Rowid for the inserted record */
   127    467   
   128         -#ifdef SQLITE_ENABLE_STAT2
   129         -  int addr = 0;                /* Instruction address */
   130         -  int regTemp2 = iMem++;       /* Temporary use register */
   131         -  int regSamplerecno = iMem++; /* Index of next sample to record */
   132         -  int regRecno = iMem++;       /* Current sample index */
   133         -  int regLast = iMem++;        /* Index of last sample to record */
   134         -  int regFirst = iMem++;       /* Index of first sample to record */
   135         -#endif
   136    468   
   137    469     v = sqlite3GetVdbe(pParse);
   138    470     if( v==0 || NEVER(pTab==0) ){
   139    471       return;
   140    472     }
   141    473     if( pTab->tnum==0 ){
   142    474       /* Do not gather statistics on views or virtual tables */
................................................................................
   161    493     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   162    494   
   163    495     iIdxCur = pParse->nTab++;
   164    496     sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
   165    497     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   166    498       int nCol;
   167    499       KeyInfo *pKey;
          500  +    int addrIfNot = 0;           /* address of OP_IfNot */
          501  +    int *aChngAddr;              /* Array of jump instruction addresses */
   168    502   
   169    503       if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
          504  +    VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
   170    505       nCol = pIdx->nColumn;
          506  +    aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
          507  +    if( aChngAddr==0 ) continue;
   171    508       pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   172    509       if( iMem+1+(nCol*2)>pParse->nMem ){
   173    510         pParse->nMem = iMem+1+(nCol*2);
   174    511       }
   175    512   
   176    513       /* Open a cursor to the index to be analyzed. */
   177    514       assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
................................................................................
   178    515       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
   179    516           (char *)pKey, P4_KEYINFO_HANDOFF);
   180    517       VdbeComment((v, "%s", pIdx->zName));
   181    518   
   182    519       /* Populate the register containing the index name. */
   183    520       sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
   184    521   
   185         -#ifdef SQLITE_ENABLE_STAT2
   186         -
   187         -    /* If this iteration of the loop is generating code to analyze the
   188         -    ** first index in the pTab->pIndex list, then register regLast has
   189         -    ** not been populated. In this case populate it now.  */
   190         -    if( pTab->pIndex==pIdx ){
   191         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
   192         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
   193         -      sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
   194         -
   195         -      sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
   196         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
   197         -      addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
   198         -      sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
   199         -      sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
   200         -      sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
   201         -      sqlite3VdbeAddOp3(v, OP_Divide,  regTemp2, regLast, regLast);
   202         -      sqlite3VdbeJumpHere(v, addr);
          522  +#ifdef SQLITE_ENABLE_STAT3
          523  +    if( once ){
          524  +      once = 0;
          525  +      sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
   203    526       }
   204         -
   205         -    /* Zero the regSampleno and regRecno registers. */
   206         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
   207         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
   208         -    sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
   209         -#endif
          527  +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
          528  +    sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
          529  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
          530  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
          531  +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
          532  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
          533  +                      (char*)&stat3InitFuncdef, P4_FUNCDEF);
          534  +    sqlite3VdbeChangeP5(v, 2);
          535  +#endif /* SQLITE_ENABLE_STAT3 */
   210    536   
   211    537       /* The block of memory cells initialized here is used as follows.
   212    538       **
   213    539       **    iMem:                
   214    540       **        The total number of rows in the table.
   215    541       **
   216    542       **    iMem+1 .. iMem+nCol: 
................................................................................
   232    558       }
   233    559   
   234    560       /* Start the analysis loop. This loop runs through all the entries in
   235    561       ** the index b-tree.  */
   236    562       endOfLoop = sqlite3VdbeMakeLabel(v);
   237    563       sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
   238    564       topOfLoop = sqlite3VdbeCurrentAddr(v);
   239         -    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
          565  +    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);  /* Increment row counter */
   240    566   
   241    567       for(i=0; i<nCol; i++){
   242    568         CollSeq *pColl;
   243    569         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
   244    570         if( i==0 ){
   245         -#ifdef SQLITE_ENABLE_STAT2
   246         -        /* Check if the record that cursor iIdxCur points to contains a
   247         -        ** value that should be stored in the sqlite_stat2 table. If so,
   248         -        ** store it.  */
   249         -        int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
   250         -        assert( regTabname+1==regIdxname 
   251         -             && regTabname+2==regSampleno
   252         -             && regTabname+3==regCol
   253         -        );
   254         -        sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   255         -        sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
   256         -        sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
   257         -        sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
   258         -
   259         -        /* Calculate new values for regSamplerecno and regSampleno.
   260         -        **
   261         -        **   sampleno = sampleno + 1
   262         -        **   samplerecno = samplerecno+(remaining records)/(remaining samples)
   263         -        */
   264         -        sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
   265         -        sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
   266         -        sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
   267         -        sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
   268         -        sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
   269         -        sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
   270         -        sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
   271         -
   272         -        sqlite3VdbeJumpHere(v, ne);
   273         -        sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
   274         -#endif
   275         -
   276    571           /* Always record the very first row */
   277         -        sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
          572  +        addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
   278    573         }
   279    574         assert( pIdx->azColl!=0 );
   280    575         assert( pIdx->azColl[i]!=0 );
   281    576         pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
   282         -      sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
   283         -                       (char*)pColl, P4_COLLSEQ);
          577  +      aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
          578  +                                      (char*)pColl, P4_COLLSEQ);
   284    579         sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   285         -    }
   286         -    if( db->mallocFailed ){
   287         -      /* If a malloc failure has occurred, then the result of the expression 
   288         -      ** passed as the second argument to the call to sqlite3VdbeJumpHere() 
   289         -      ** below may be negative. Which causes an assert() to fail (or an
   290         -      ** out-of-bounds write if SQLITE_DEBUG is not defined).  */
   291         -      return;
          580  +      VdbeComment((v, "jump if column %d changed", i));
          581  +#ifdef SQLITE_ENABLE_STAT3
          582  +      if( i==0 ){
          583  +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
          584  +        VdbeComment((v, "incr repeat count"));
          585  +      }
          586  +#endif
   292    587       }
   293    588       sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
   294    589       for(i=0; i<nCol; i++){
   295         -      int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
          590  +      sqlite3VdbeJumpHere(v, aChngAddr[i]);  /* Set jump dest for the OP_Ne */
   296    591         if( i==0 ){
   297         -        sqlite3VdbeJumpHere(v, addr2-1);  /* Set jump dest for the OP_IfNot */
          592  +        sqlite3VdbeJumpHere(v, addrIfNot);   /* Jump dest for OP_IfNot */
          593  +#ifdef SQLITE_ENABLE_STAT3
          594  +        sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
          595  +                          (char*)&stat3PushFuncdef, P4_FUNCDEF);
          596  +        sqlite3VdbeChangeP5(v, 5);
          597  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
          598  +        sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
          599  +        sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
          600  +        sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
          601  +#endif        
   298    602         }
   299         -      sqlite3VdbeJumpHere(v, addr2);      /* Set jump dest for the OP_Ne */
   300    603         sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   301    604         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
   302    605       }
          606  +    sqlite3DbFree(db, aChngAddr);
   303    607   
   304         -    /* End of the analysis loop. */
          608  +    /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
   305    609       sqlite3VdbeResolveLabel(v, endOfLoop);
          610  +
   306    611       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
   307    612       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
          613  +#ifdef SQLITE_ENABLE_STAT3
          614  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
          615  +                      (char*)&stat3PushFuncdef, P4_FUNCDEF);
          616  +    sqlite3VdbeChangeP5(v, 5);
          617  +    sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
          618  +    shortJump = 
          619  +    sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
          620  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
          621  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
          622  +    sqlite3VdbeChangeP5(v, 2);
          623  +    sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
          624  +    sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
          625  +    sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
          626  +    sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
          627  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
          628  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
          629  +    sqlite3VdbeChangeP5(v, 3);
          630  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
          631  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
          632  +    sqlite3VdbeChangeP5(v, 4);
          633  +    sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
          634  +                      (char*)&stat3GetFuncdef, P4_FUNCDEF);
          635  +    sqlite3VdbeChangeP5(v, 5);
          636  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
          637  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
          638  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
          639  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
          640  +    sqlite3VdbeJumpHere(v, shortJump+2);
          641  +#endif        
   308    642   
   309    643       /* Store the results in sqlite_stat1.
   310    644       **
   311    645       ** The result is a single row of the sqlite_stat1 table.  The first
   312    646       ** two columns are the names of the table and index.  The third column
   313    647       ** is a string composed of a list of integer statistics about the
   314    648       ** index.  The first integer in the list is the total number of entries
................................................................................
   320    654       **
   321    655       **        I = (K+D-1)/D
   322    656       **
   323    657       ** If K==0 then no entry is made into the sqlite_stat1 table.  
   324    658       ** If K>0 then it is always the case the D>0 so division by zero
   325    659       ** is never possible.
   326    660       */
   327         -    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
          661  +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
   328    662       if( jZeroRows<0 ){
   329    663         jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
   330    664       }
   331    665       for(i=0; i<nCol; i++){
   332    666         sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
   333         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
          667  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   334    668         sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
   335    669         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
   336    670         sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
   337    671         sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
   338         -      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
          672  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   339    673       }
   340    674       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   341         -    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
   342         -    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
          675  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
          676  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   343    677       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   344    678     }
   345    679   
   346    680     /* If the table has no indices, create a single sqlite_stat1 entry
   347    681     ** containing NULL as the index name and the row count as the content.
   348    682     */
   349    683     if( pTab->pIndex==0 ){
   350    684       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
   351    685       VdbeComment((v, "%s", pTab->zName));
   352         -    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
          686  +    sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
   353    687       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   354         -    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
          688  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
   355    689     }else{
   356    690       sqlite3VdbeJumpHere(v, jZeroRows);
   357    691       jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
   358    692     }
   359    693     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
   360    694     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   361         -  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
   362         -  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
          695  +  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
          696  +  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   363    697     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   364    698     if( pParse->nMem<regRec ) pParse->nMem = regRec;
   365    699     sqlite3VdbeJumpHere(v, jZeroRows);
   366    700   }
          701  +
   367    702   
   368    703   /*
   369    704   ** Generate code that will cause the most recent index analysis to
   370    705   ** be loaded into internal hash tables where is can be used.
   371    706   */
   372    707   static void loadAnalysis(Parse *pParse, int iDb){
   373    708     Vdbe *v = sqlite3GetVdbe(pParse);
................................................................................
   384    719     Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
   385    720     HashElem *k;
   386    721     int iStatCur;
   387    722     int iMem;
   388    723   
   389    724     sqlite3BeginWriteOperation(pParse, 0, iDb);
   390    725     iStatCur = pParse->nTab;
   391         -  pParse->nTab += 2;
          726  +  pParse->nTab += 3;
   392    727     openStatTable(pParse, iDb, iStatCur, 0, 0);
   393    728     iMem = pParse->nMem+1;
   394    729     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   395    730     for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
   396    731       Table *pTab = (Table*)sqliteHashData(k);
   397    732       analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
   398    733     }
................................................................................
   409    744     int iStatCur;
   410    745   
   411    746     assert( pTab!=0 );
   412    747     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   413    748     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   414    749     sqlite3BeginWriteOperation(pParse, 0, iDb);
   415    750     iStatCur = pParse->nTab;
   416         -  pParse->nTab += 2;
          751  +  pParse->nTab += 3;
   417    752     if( pOnlyIdx ){
   418    753       openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
   419    754     }else{
   420    755       openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
   421    756     }
   422    757     analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
   423    758     loadAnalysis(pParse, iDb);
................................................................................
   514    849   ** the table.
   515    850   */
   516    851   static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
   517    852     analysisInfo *pInfo = (analysisInfo*)pData;
   518    853     Index *pIndex;
   519    854     Table *pTable;
   520    855     int i, c, n;
   521         -  unsigned int v;
          856  +  tRowcnt v;
   522    857     const char *z;
   523    858   
   524    859     assert( argc==3 );
   525    860     UNUSED_PARAMETER2(NotUsed, argc);
   526    861   
   527    862     if( argv==0 || argv[0]==0 || argv[2]==0 ){
   528    863       return 0;
................................................................................
   557    892   }
   558    893   
   559    894   /*
   560    895   ** If the Index.aSample variable is not NULL, delete the aSample[] array
   561    896   ** and its contents.
   562    897   */
   563    898   void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
   564         -#ifdef SQLITE_ENABLE_STAT2
          899  +#ifdef SQLITE_ENABLE_STAT3
   565    900     if( pIdx->aSample ){
   566    901       int j;
   567         -    for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
          902  +    for(j=0; j<pIdx->nSample; j++){
   568    903         IndexSample *p = &pIdx->aSample[j];
   569    904         if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
   570    905           sqlite3DbFree(db, p->u.z);
   571    906         }
   572    907       }
   573    908       sqlite3DbFree(db, pIdx->aSample);
          909  +  }
          910  +  if( db && db->pnBytesFreed==0 ){
          911  +    pIdx->nSample = 0;
          912  +    pIdx->aSample = 0;
   574    913     }
   575    914   #else
   576    915     UNUSED_PARAMETER(db);
   577    916     UNUSED_PARAMETER(pIdx);
   578    917   #endif
   579    918   }
   580    919   
          920  +#ifdef SQLITE_ENABLE_STAT3
          921  +/*
          922  +** Load content from the sqlite_stat3 table into the Index.aSample[]
          923  +** arrays of all indices.
          924  +*/
          925  +static int loadStat3(sqlite3 *db, const char *zDb){
          926  +  int rc;                       /* Result codes from subroutines */
          927  +  sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
          928  +  char *zSql;                   /* Text of the SQL statement */
          929  +  Index *pPrevIdx = 0;          /* Previous index in the loop */
          930  +  int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
          931  +  int eType;                    /* Datatype of a sample */
          932  +  IndexSample *pSample;         /* A slot in pIdx->aSample[] */
          933  +
          934  +  if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
          935  +    return SQLITE_OK;
          936  +  }
          937  +
          938  +  zSql = sqlite3MPrintf(db, 
          939  +      "SELECT idx,count(*) FROM %Q.sqlite_stat3"
          940  +      " GROUP BY idx", zDb);
          941  +  if( !zSql ){
          942  +    return SQLITE_NOMEM;
          943  +  }
          944  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
          945  +  sqlite3DbFree(db, zSql);
          946  +  if( rc ) return rc;
          947  +
          948  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          949  +    char *zIndex;   /* Index name */
          950  +    Index *pIdx;    /* Pointer to the index object */
          951  +    int nSample;    /* Number of samples */
          952  +
          953  +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
          954  +    if( zIndex==0 ) continue;
          955  +    nSample = sqlite3_column_int(pStmt, 1);
          956  +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
          957  +    if( pIdx==0 ) continue;
          958  +    assert( pIdx->nSample==0 );
          959  +    pIdx->nSample = nSample;
          960  +    pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
          961  +    pIdx->avgEq = pIdx->aiRowEst[1];
          962  +    if( pIdx->aSample==0 ){
          963  +      db->mallocFailed = 1;
          964  +      sqlite3_finalize(pStmt);
          965  +      return SQLITE_NOMEM;
          966  +    }
          967  +  }
          968  +  rc = sqlite3_finalize(pStmt);
          969  +  if( rc ) return rc;
          970  +
          971  +  zSql = sqlite3MPrintf(db, 
          972  +      "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
          973  +  if( !zSql ){
          974  +    return SQLITE_NOMEM;
          975  +  }
          976  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
          977  +  sqlite3DbFree(db, zSql);
          978  +  if( rc ) return rc;
          979  +
          980  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          981  +    char *zIndex;   /* Index name */
          982  +    Index *pIdx;    /* Pointer to the index object */
          983  +    int i;          /* Loop counter */
          984  +    tRowcnt sumEq;  /* Sum of the nEq values */
          985  +
          986  +    zIndex = (char *)sqlite3_column_text(pStmt, 0);
          987  +    if( zIndex==0 ) continue;
          988  +    pIdx = sqlite3FindIndex(db, zIndex, zDb);
          989  +    if( pIdx==0 ) continue;
          990  +    if( pIdx==pPrevIdx ){
          991  +      idx++;
          992  +    }else{
          993  +      pPrevIdx = pIdx;
          994  +      idx = 0;
          995  +    }
          996  +    assert( idx<pIdx->nSample );
          997  +    pSample = &pIdx->aSample[idx];
          998  +    pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
          999  +    pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
         1000  +    pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
         1001  +    if( idx==pIdx->nSample-1 ){
         1002  +      if( pSample->nDLt>0 ){
         1003  +        for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
         1004  +        pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
         1005  +      }
         1006  +      if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
         1007  +    }
         1008  +    eType = sqlite3_column_type(pStmt, 4);
         1009  +    pSample->eType = (u8)eType;
         1010  +    switch( eType ){
         1011  +      case SQLITE_INTEGER: {
         1012  +        pSample->u.i = sqlite3_column_int64(pStmt, 4);
         1013  +        break;
         1014  +      }
         1015  +      case SQLITE_FLOAT: {
         1016  +        pSample->u.r = sqlite3_column_double(pStmt, 4);
         1017  +        break;
         1018  +      }
         1019  +      case SQLITE_NULL: {
         1020  +        break;
         1021  +      }
         1022  +      default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
         1023  +        const char *z = (const char *)(
         1024  +              (eType==SQLITE_BLOB) ?
         1025  +              sqlite3_column_blob(pStmt, 4):
         1026  +              sqlite3_column_text(pStmt, 4)
         1027  +           );
         1028  +        int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
         1029  +        pSample->nByte = n;
         1030  +        if( n < 1){
         1031  +          pSample->u.z = 0;
         1032  +        }else{
         1033  +          pSample->u.z = sqlite3Malloc(n);
         1034  +          if( pSample->u.z==0 ){
         1035  +            db->mallocFailed = 1;
         1036  +            sqlite3_finalize(pStmt);
         1037  +            return SQLITE_NOMEM;
         1038  +          }
         1039  +          memcpy(pSample->u.z, z, n);
         1040  +        }
         1041  +      }
         1042  +    }
         1043  +  }
         1044  +  return sqlite3_finalize(pStmt);
         1045  +}
         1046  +#endif /* SQLITE_ENABLE_STAT3 */
         1047  +
   581   1048   /*
   582         -** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
         1049  +** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
   583   1050   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
   584         -** arrays. The contents of sqlite_stat2 are used to populate the
         1051  +** arrays. The contents of sqlite_stat3 are used to populate the
   585   1052   ** Index.aSample[] arrays.
   586   1053   **
   587   1054   ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
   588         -** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined 
   589         -** during compilation and the sqlite_stat2 table is present, no data is 
         1055  +** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 
         1056  +** during compilation and the sqlite_stat3 table is present, no data is 
   590   1057   ** read from it.
   591   1058   **
   592         -** If SQLITE_ENABLE_STAT2 was defined during compilation and the 
   593         -** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
         1059  +** If SQLITE_ENABLE_STAT3 was defined during compilation and the 
         1060  +** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
   594   1061   ** returned. However, in this case, data is read from the sqlite_stat1
   595   1062   ** table (if it is present) before returning.
   596   1063   **
   597   1064   ** If an OOM error occurs, this function always sets db->mallocFailed.
   598   1065   ** This means if the caller does not care about other errors, the return
   599   1066   ** code may be ignored.
   600   1067   */
................................................................................
   608   1075     assert( db->aDb[iDb].pBt!=0 );
   609   1076   
   610   1077     /* Clear any prior statistics */
   611   1078     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   612   1079     for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
   613   1080       Index *pIdx = sqliteHashData(i);
   614   1081       sqlite3DefaultRowEst(pIdx);
         1082  +#ifdef SQLITE_ENABLE_STAT3
   615   1083       sqlite3DeleteIndexSamples(db, pIdx);
   616   1084       pIdx->aSample = 0;
         1085  +#endif
   617   1086     }
   618   1087   
   619   1088     /* Check to make sure the sqlite_stat1 table exists */
   620   1089     sInfo.db = db;
   621   1090     sInfo.zDatabase = db->aDb[iDb].zName;
   622   1091     if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
   623   1092       return SQLITE_ERROR;
   624   1093     }
   625   1094   
   626   1095     /* Load new statistics out of the sqlite_stat1 table */
   627   1096     zSql = sqlite3MPrintf(db, 
   628         -      "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
         1097  +      "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
   629   1098     if( zSql==0 ){
   630   1099       rc = SQLITE_NOMEM;
   631   1100     }else{
   632   1101       rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
   633   1102       sqlite3DbFree(db, zSql);
   634   1103     }
   635   1104   
   636   1105   
   637         -  /* Load the statistics from the sqlite_stat2 table. */
   638         -#ifdef SQLITE_ENABLE_STAT2
   639         -  if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
   640         -    rc = SQLITE_ERROR;
   641         -  }
         1106  +  /* Load the statistics from the sqlite_stat3 table. */
         1107  +#ifdef SQLITE_ENABLE_STAT3
   642   1108     if( rc==SQLITE_OK ){
   643         -    sqlite3_stmt *pStmt = 0;
   644         -
   645         -    zSql = sqlite3MPrintf(db, 
   646         -        "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
   647         -    if( !zSql ){
   648         -      rc = SQLITE_NOMEM;
   649         -    }else{
   650         -      rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   651         -      sqlite3DbFree(db, zSql);
   652         -    }
   653         -
   654         -    if( rc==SQLITE_OK ){
   655         -      while( sqlite3_step(pStmt)==SQLITE_ROW ){
   656         -        char *zIndex;   /* Index name */
   657         -        Index *pIdx;    /* Pointer to the index object */
   658         -
   659         -        zIndex = (char *)sqlite3_column_text(pStmt, 0);
   660         -        pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
   661         -        if( pIdx ){
   662         -          int iSample = sqlite3_column_int(pStmt, 1);
   663         -          if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
   664         -            int eType = sqlite3_column_type(pStmt, 2);
   665         -
   666         -            if( pIdx->aSample==0 ){
   667         -              static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
   668         -              pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
   669         -              if( pIdx->aSample==0 ){
   670         -                db->mallocFailed = 1;
   671         -                break;
   672         -              }
   673         -	      memset(pIdx->aSample, 0, sz);
   674         -            }
   675         -
   676         -            assert( pIdx->aSample );
   677         -            {
   678         -              IndexSample *pSample = &pIdx->aSample[iSample];
   679         -              pSample->eType = (u8)eType;
   680         -              if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
   681         -                pSample->u.r = sqlite3_column_double(pStmt, 2);
   682         -              }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
   683         -                const char *z = (const char *)(
   684         -                    (eType==SQLITE_BLOB) ?
   685         -                    sqlite3_column_blob(pStmt, 2):
   686         -                    sqlite3_column_text(pStmt, 2)
   687         -                );
   688         -                int n = sqlite3_column_bytes(pStmt, 2);
   689         -                if( n>24 ){
   690         -                  n = 24;
   691         -                }
   692         -                pSample->nByte = (u8)n;
   693         -                if( n < 1){
   694         -                  pSample->u.z = 0;
   695         -                }else{
   696         -                  pSample->u.z = sqlite3DbStrNDup(0, z, n);
   697         -                  if( pSample->u.z==0 ){
   698         -                    db->mallocFailed = 1;
   699         -                    break;
   700         -                  }
   701         -                }
   702         -              }
   703         -            }
   704         -          }
   705         -        }
   706         -      }
   707         -      rc = sqlite3_finalize(pStmt);
   708         -    }
         1109  +    rc = loadStat3(db, sInfo.zDatabase);
   709   1110     }
   710   1111   #endif
   711   1112   
   712   1113     if( rc==SQLITE_NOMEM ){
   713   1114       db->mallocFailed = 1;
   714   1115     }
   715   1116     return rc;
   716   1117   }
   717   1118   
   718   1119   
   719   1120   #endif /* SQLITE_OMIT_ANALYZE */

Changes to src/backup.c.

   674    674   **
   675    675   ** The size of file pTo may be reduced by this operation. If anything 
   676    676   ** goes wrong, the transaction on pTo is rolled back. If successful, the 
   677    677   ** transaction is committed before returning.
   678    678   */
   679    679   int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
   680    680     int rc;
          681  +  sqlite3_file *pFd;              /* File descriptor for database pTo */
   681    682     sqlite3_backup b;
   682    683     sqlite3BtreeEnter(pTo);
   683    684     sqlite3BtreeEnter(pFrom);
          685  +
          686  +  assert( sqlite3BtreeIsInTrans(pTo) );
          687  +  pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
          688  +  if( pFd->pMethods ){
          689  +    i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
          690  +    sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
          691  +  }
   684    692   
   685    693     /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
   686    694     ** to 0. This is used by the implementations of sqlite3_backup_step()
   687    695     ** and sqlite3_backup_finish() to detect that they are being called
   688    696     ** from this function, not directly by the user.
   689    697     */
   690    698     memset(&b, 0, sizeof(b));
................................................................................
   703    711     sqlite3_backup_step(&b, 0x7FFFFFFF);
   704    712     assert( b.rc!=SQLITE_OK );
   705    713     rc = sqlite3_backup_finish(&b);
   706    714     if( rc==SQLITE_OK ){
   707    715       pTo->pBt->pageSizeFixed = 0;
   708    716     }
   709    717   
          718  +  assert( sqlite3BtreeIsInTrans(pTo)==0 );
   710    719     sqlite3BtreeLeave(pFrom);
   711    720     sqlite3BtreeLeave(pTo);
   712    721     return rc;
   713    722   }
   714    723   #endif /* SQLITE_OMIT_VACUUM */

Changes to src/btree.c.

  3961   3961   #endif
  3962   3962             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
  3963   3963           offset -= ovflSize;
  3964   3964         }else{
  3965   3965           /* Need to read this page properly. It contains some of the
  3966   3966           ** range of data that is being read (eOp==0) or written (eOp!=0).
  3967   3967           */
  3968         -        DbPage *pDbPage;
         3968  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
         3969  +        sqlite3_file *fd;
         3970  +#endif
  3969   3971           int a = amt;
  3970         -        rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
  3971         -        if( rc==SQLITE_OK ){
  3972         -          aPayload = sqlite3PagerGetData(pDbPage);
  3973         -          nextPage = get4byte(aPayload);
  3974         -          if( a + offset > ovflSize ){
  3975         -            a = ovflSize - offset;
  3976         -          }
  3977         -          rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
  3978         -          sqlite3PagerUnref(pDbPage);
  3979         -          offset = 0;
  3980         -          amt -= a;
  3981         -          pBuf += a;
  3982         -        }
         3972  +        if( a + offset > ovflSize ){
         3973  +          a = ovflSize - offset;
         3974  +        }
         3975  +
         3976  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
         3977  +        /* If all the following are true:
         3978  +        **
         3979  +        **   1) this is a read operation, and 
         3980  +        **   2) data is required from the start of this overflow page, and
         3981  +        **   3) the database is file-backed, and
         3982  +        **   4) there is no open write-transaction, and
         3983  +        **   5) the database is not a WAL database,
         3984  +        **
         3985  +        ** then data can be read directly from the database file into the
         3986  +        ** output buffer, bypassing the page-cache altogether. This speeds
         3987  +        ** up loading large records that span many overflow pages.
         3988  +        */
         3989  +        if( eOp==0                                             /* (1) */
         3990  +         && offset==0                                          /* (2) */
         3991  +         && pBt->inTransaction==TRANS_READ                     /* (4) */
         3992  +         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
         3993  +         && pBt->pPage1->aData[19]==0x01                       /* (5) */
         3994  +        ){
         3995  +          u8 aSave[4];
         3996  +          u8 *aWrite = &pBuf[-4];
         3997  +          memcpy(aSave, aWrite, 4);
         3998  +          rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
         3999  +          nextPage = get4byte(aWrite);
         4000  +          memcpy(aWrite, aSave, 4);
         4001  +        }else
         4002  +#endif
         4003  +
         4004  +        {
         4005  +          DbPage *pDbPage;
         4006  +          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
         4007  +          if( rc==SQLITE_OK ){
         4008  +            aPayload = sqlite3PagerGetData(pDbPage);
         4009  +            nextPage = get4byte(aPayload);
         4010  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
         4011  +            sqlite3PagerUnref(pDbPage);
         4012  +            offset = 0;
         4013  +          }
         4014  +        }
         4015  +        amt -= a;
         4016  +        pBuf += a;
  3983   4017         }
  3984   4018       }
  3985   4019     }
  3986   4020   
  3987   4021     if( rc==SQLITE_OK && amt>0 ){
  3988   4022       return SQLITE_CORRUPT_BKPT;
  3989   4023     }

Changes to src/build.c.

  1986   1986   */
  1987   1987   static void sqlite3ClearStatTables(
  1988   1988     Parse *pParse,         /* The parsing context */
  1989   1989     int iDb,               /* The database number */
  1990   1990     const char *zType,     /* "idx" or "tbl" */
  1991   1991     const char *zName      /* Name of index or table */
  1992   1992   ){
  1993         -  static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
         1993  +  static const char *azStatTab[] = { 
         1994  +    "sqlite_stat1",
         1995  +    "sqlite_stat2",
         1996  +    "sqlite_stat3",
         1997  +  };
  1994   1998     int i;
  1995   1999     const char *zDbName = pParse->db->aDb[iDb].zName;
  1996   2000     for(i=0; i<ArraySize(azStatTab); i++){
  1997   2001       if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
  1998   2002         sqlite3NestedParse(pParse,
  1999   2003           "DELETE FROM %Q.%s WHERE %s=%Q",
  2000   2004           zDbName, azStatTab[i], zType, zName
  2001   2005         );
  2002   2006       }
  2003   2007     }
  2004   2008   }
         2009  +
         2010  +/*
         2011  +** Generate code to drop a table.
         2012  +*/
         2013  +void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
         2014  +  Vdbe *v;
         2015  +  sqlite3 *db = pParse->db;
         2016  +  Trigger *pTrigger;
         2017  +  Db *pDb = &db->aDb[iDb];
         2018  +
         2019  +  v = sqlite3GetVdbe(pParse);
         2020  +  assert( v!=0 );
         2021  +  sqlite3BeginWriteOperation(pParse, 1, iDb);
         2022  +
         2023  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         2024  +  if( IsVirtual(pTab) ){
         2025  +    sqlite3VdbeAddOp0(v, OP_VBegin);
         2026  +  }
         2027  +#endif
         2028  +
         2029  +  /* Drop all triggers associated with the table being dropped. Code
         2030  +  ** is generated to remove entries from sqlite_master and/or
         2031  +  ** sqlite_temp_master if required.
         2032  +  */
         2033  +  pTrigger = sqlite3TriggerList(pParse, pTab);
         2034  +  while( pTrigger ){
         2035  +    assert( pTrigger->pSchema==pTab->pSchema || 
         2036  +        pTrigger->pSchema==db->aDb[1].pSchema );
         2037  +    sqlite3DropTriggerPtr(pParse, pTrigger);
         2038  +    pTrigger = pTrigger->pNext;
         2039  +  }
         2040  +
         2041  +#ifndef SQLITE_OMIT_AUTOINCREMENT
         2042  +  /* Remove any entries of the sqlite_sequence table associated with
         2043  +  ** the table being dropped. This is done before the table is dropped
         2044  +  ** at the btree level, in case the sqlite_sequence table needs to
         2045  +  ** move as a result of the drop (can happen in auto-vacuum mode).
         2046  +  */
         2047  +  if( pTab->tabFlags & TF_Autoincrement ){
         2048  +    sqlite3NestedParse(pParse,
         2049  +      "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
         2050  +      pDb->zName, pTab->zName
         2051  +    );
         2052  +  }
         2053  +#endif
         2054  +
         2055  +  /* Drop all SQLITE_MASTER table and index entries that refer to the
         2056  +  ** table. The program name loops through the master table and deletes
         2057  +  ** every row that refers to a table of the same name as the one being
         2058  +  ** dropped. Triggers are handled seperately because a trigger can be
         2059  +  ** created in the temp database that refers to a table in another
         2060  +  ** database.
         2061  +  */
         2062  +  sqlite3NestedParse(pParse, 
         2063  +      "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
         2064  +      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
         2065  +  if( !isView && !IsVirtual(pTab) ){
         2066  +    destroyTable(pParse, pTab);
         2067  +  }
         2068  +
         2069  +  /* Remove the table entry from SQLite's internal schema and modify
         2070  +  ** the schema cookie.
         2071  +  */
         2072  +  if( IsVirtual(pTab) ){
         2073  +    sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
         2074  +  }
         2075  +  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
         2076  +  sqlite3ChangeCookie(pParse, iDb);
         2077  +  sqliteViewResetAll(db, iDb);
         2078  +}
  2005   2079   
  2006   2080   /*
  2007   2081   ** This routine is called to do the work of a DROP TABLE statement.
  2008   2082   ** pName is the name of the table to be dropped.
  2009   2083   */
  2010   2084   void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
  2011   2085     Table *pTab;
................................................................................
  2067   2141         goto exit_drop_table;
  2068   2142       }
  2069   2143       if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
  2070   2144         goto exit_drop_table;
  2071   2145       }
  2072   2146     }
  2073   2147   #endif
  2074         -  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
         2148  +  if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
         2149  +    && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
  2075   2150       sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
  2076   2151       goto exit_drop_table;
  2077   2152     }
  2078   2153   
  2079   2154   #ifndef SQLITE_OMIT_VIEW
  2080   2155     /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
  2081   2156     ** on a table.
................................................................................
  2091   2166   #endif
  2092   2167   
  2093   2168     /* Generate code to remove the table from the master table
  2094   2169     ** on disk.
  2095   2170     */
  2096   2171     v = sqlite3GetVdbe(pParse);
  2097   2172     if( v ){
  2098         -    Trigger *pTrigger;
  2099         -    Db *pDb = &db->aDb[iDb];
  2100   2173       sqlite3BeginWriteOperation(pParse, 1, iDb);
  2101         -
  2102         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  2103         -    if( IsVirtual(pTab) ){
  2104         -      sqlite3VdbeAddOp0(v, OP_VBegin);
  2105         -    }
  2106         -#endif
         2174  +    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
  2107   2175       sqlite3FkDropTable(pParse, pName, pTab);
  2108         -
  2109         -    /* Drop all triggers associated with the table being dropped. Code
  2110         -    ** is generated to remove entries from sqlite_master and/or
  2111         -    ** sqlite_temp_master if required.
  2112         -    */
  2113         -    pTrigger = sqlite3TriggerList(pParse, pTab);
  2114         -    while( pTrigger ){
  2115         -      assert( pTrigger->pSchema==pTab->pSchema || 
  2116         -          pTrigger->pSchema==db->aDb[1].pSchema );
  2117         -      sqlite3DropTriggerPtr(pParse, pTrigger);
  2118         -      pTrigger = pTrigger->pNext;
  2119         -    }
  2120         -
  2121         -#ifndef SQLITE_OMIT_AUTOINCREMENT
  2122         -    /* Remove any entries of the sqlite_sequence table associated with
  2123         -    ** the table being dropped. This is done before the table is dropped
  2124         -    ** at the btree level, in case the sqlite_sequence table needs to
  2125         -    ** move as a result of the drop (can happen in auto-vacuum mode).
  2126         -    */
  2127         -    if( pTab->tabFlags & TF_Autoincrement ){
  2128         -      sqlite3NestedParse(pParse,
  2129         -        "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
  2130         -        pDb->zName, pTab->zName
  2131         -      );
  2132         -    }
  2133         -#endif
  2134         -
  2135         -    /* Drop all SQLITE_MASTER table and index entries that refer to the
  2136         -    ** table. The program name loops through the master table and deletes
  2137         -    ** every row that refers to a table of the same name as the one being
  2138         -    ** dropped. Triggers are handled seperately because a trigger can be
  2139         -    ** created in the temp database that refers to a table in another
  2140         -    ** database.
  2141         -    */
  2142         -    sqlite3NestedParse(pParse, 
  2143         -        "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
  2144         -        pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
  2145         -    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
  2146         -    if( !isView && !IsVirtual(pTab) ){
  2147         -      destroyTable(pParse, pTab);
  2148         -    }
  2149         -
  2150         -    /* Remove the table entry from SQLite's internal schema and modify
  2151         -    ** the schema cookie.
  2152         -    */
  2153         -    if( IsVirtual(pTab) ){
  2154         -      sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
  2155         -    }
  2156         -    sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
  2157         -    sqlite3ChangeCookie(pParse, iDb);
         2176  +    sqlite3CodeDropTable(pParse, pTab, iDb, isView);
  2158   2177     }
  2159         -  sqliteViewResetAll(db, iDb);
  2160   2178   
  2161   2179   exit_drop_table:
  2162   2180     sqlite3SrcListDelete(db, pName);
  2163   2181   }
  2164   2182   
  2165   2183   /*
  2166   2184   ** This routine is called to create a new foreign key on the table
................................................................................
  2635   2653     /* 
  2636   2654     ** Allocate the index structure. 
  2637   2655     */
  2638   2656     nName = sqlite3Strlen30(zName);
  2639   2657     nCol = pList->nExpr;
  2640   2658     pIndex = sqlite3DbMallocZero(db, 
  2641   2659         sizeof(Index) +              /* Index structure  */
         2660  +      sizeof(tRowcnt)*(nCol+1) +   /* Index.aiRowEst   */
  2642   2661         sizeof(int)*nCol +           /* Index.aiColumn   */
  2643         -      sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
  2644   2662         sizeof(char *)*nCol +        /* Index.azColl     */
  2645   2663         sizeof(u8)*nCol +            /* Index.aSortOrder */
  2646   2664         nName + 1 +                  /* Index.zName      */
  2647   2665         nExtra                       /* Collation sequence names */
  2648   2666     );
  2649   2667     if( db->mallocFailed ){
  2650   2668       goto exit_create_index;
  2651   2669     }
  2652         -  pIndex->azColl = (char**)(&pIndex[1]);
         2670  +  pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
         2671  +  pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
  2653   2672     pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
  2654         -  pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
  2655         -  pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
         2673  +  pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
  2656   2674     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
  2657   2675     zExtra = (char *)(&pIndex->zName[nName+1]);
  2658   2676     memcpy(pIndex->zName, zName, nName+1);
  2659   2677     pIndex->pTable = pTab;
  2660   2678     pIndex->nColumn = pList->nExpr;
  2661   2679     pIndex->onError = (u8)onError;
  2662   2680     pIndex->autoIndex = (u8)(pName==0);
................................................................................
  2925   2943   **           aiRowEst[N]>=1
  2926   2944   **
  2927   2945   ** Apart from that, we have little to go on besides intuition as to
  2928   2946   ** how aiRowEst[] should be initialized.  The numbers generated here
  2929   2947   ** are based on typical values found in actual indices.
  2930   2948   */
  2931   2949   void sqlite3DefaultRowEst(Index *pIdx){
  2932         -  unsigned *a = pIdx->aiRowEst;
         2950  +  tRowcnt *a = pIdx->aiRowEst;
  2933   2951     int i;
  2934         -  unsigned n;
         2952  +  tRowcnt n;
  2935   2953     assert( a!=0 );
  2936   2954     a[0] = pIdx->pTable->nRowEst;
  2937   2955     if( a[0]<10 ) a[0] = 10;
  2938   2956     n = 10;
  2939   2957     for(i=1; i<=pIdx->nColumn; i++){
  2940   2958       a[i] = n;
  2941   2959       if( n>5 ) n--;

Changes to src/ctime.c.

   112    112     "ENABLE_OVERSIZE_CELL_CHECK",
   113    113   #endif
   114    114   #ifdef SQLITE_ENABLE_RTREE
   115    115     "ENABLE_RTREE",
   116    116   #endif
   117    117   #ifdef SQLITE_ENABLE_STAT2
   118    118     "ENABLE_STAT2",
          119  +#endif
          120  +#ifdef SQLITE_ENABLE_STAT3
          121  +  "ENABLE_STAT3",
   119    122   #endif
   120    123   #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   121    124     "ENABLE_UNLOCK_NOTIFY",
   122    125   #endif
   123    126   #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   124    127     "ENABLE_UPDATE_DELETE_LIMIT",
   125    128   #endif
................................................................................
   325    328   #endif
   326    329   #ifdef SQLITE_OMIT_WSD
   327    330     "OMIT_WSD",
   328    331   #endif
   329    332   #ifdef SQLITE_OMIT_XFER_OPT
   330    333     "OMIT_XFER_OPT",
   331    334   #endif
   332         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
   333         -  "PAGECACHE_BLOCKALLOC",
   334         -#endif
   335    335   #ifdef SQLITE_PERFORMANCE_TRACE
   336    336     "PERFORMANCE_TRACE",
   337    337   #endif
   338    338   #ifdef SQLITE_PROXY_DEBUG
   339    339     "PROXY_DEBUG",
   340    340   #endif
   341    341   #ifdef SQLITE_SECURE_DELETE

Changes to src/insert.c.

  1743   1743     ** the extra complication to make this rule less restrictive is probably
  1744   1744     ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
  1745   1745     */
  1746   1746     if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
  1747   1747       return 0;
  1748   1748     }
  1749   1749   #endif
         1750  +  if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
         1751  +    return 0;
         1752  +  }
  1750   1753   
  1751   1754     /* If we get this far, it means either:
  1752   1755     **
  1753   1756     **    *   We can always do the transfer if the table contains an
  1754   1757     **        an integer primary key
  1755   1758     **
  1756   1759     **    *   We can conditionally do the transfer if the destination

Changes to src/os_unix.c.

  5755   5755       **   "<path to db>-journalNN"
  5756   5756       **   "<path to db>-walNN"
  5757   5757       **
  5758   5758       ** where NN is a 4 digit decimal number. The NN naming schemes are 
  5759   5759       ** used by the test_multiplex.c module.
  5760   5760       */
  5761   5761       nDb = sqlite3Strlen30(zPath) - 1; 
  5762         -    while( nDb>0 && zPath[nDb]!='-' ) nDb--;
  5763         -    if( nDb==0 ) return SQLITE_OK;
         5762  +#ifdef SQLITE_ENABLE_8_3_NAMES
         5763  +    while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--;
         5764  +    if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK;
         5765  +#else
         5766  +    while( zPath[nDb]!='-' ){
         5767  +      assert( nDb>0 );
         5768  +      assert( zPath[nDb]!='\n' );
         5769  +      nDb--;
         5770  +    }
         5771  +#endif
  5764   5772       memcpy(zDb, zPath, nDb);
  5765   5773       zDb[nDb] = '\0';
  5766   5774   
  5767   5775       if( 0==osStat(zDb, &sStat) ){
  5768   5776         *pMode = sStat.st_mode & 0777;
  5769   5777         *pUid = sStat.st_uid;
  5770   5778         *pGid = sStat.st_gid;

Changes to src/os_win.c.

  2620   2620              h, zName, dwDesiredAccess, 
  2621   2621              h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
  2622   2622   
  2623   2623     if( h==INVALID_HANDLE_VALUE ){
  2624   2624       pFile->lastErrno = GetLastError();
  2625   2625       winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
  2626   2626       free(zConverted);
  2627         -    if( isReadWrite ){
         2627  +    if( isReadWrite && !isExclusive ){
  2628   2628         return winOpen(pVfs, zName, id, 
  2629   2629                ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
  2630   2630       }else{
  2631   2631         return SQLITE_CANTOPEN_BKPT;
  2632   2632       }
  2633   2633     }
  2634   2634   

Changes to src/pager.c.

   666    666     int pageSize;               /* Number of bytes in a page */
   667    667     Pgno mxPgno;                /* Maximum allowed size of the database */
   668    668     i64 journalSizeLimit;       /* Size limit for persistent journal files */
   669    669     char *zFilename;            /* Name of the database file */
   670    670     char *zJournal;             /* Name of the journal file */
   671    671     int (*xBusyHandler)(void*); /* Function to call when busy */
   672    672     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
          673  +  int nHit, nMiss;            /* Total cache hits and misses */
   673    674   #ifdef SQLITE_TEST
   674         -  int nHit, nMiss;            /* Cache hits and missing */
   675    675     int nRead, nWrite;          /* Database pages read/written */
   676    676   #endif
   677    677     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
   678    678   #ifdef SQLITE_HAS_CODEC
   679    679     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
   680    680     void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
   681    681     void (*xCodecFree)(void*);             /* Destructor for the codec */
................................................................................
  4177   4177     ** pages belonging to the same sector.
  4178   4178     **
  4179   4179     ** The doNotSpill flag inhibits all cache spilling regardless of whether
  4180   4180     ** or not a sync is required.  This is set during a rollback.
  4181   4181     **
  4182   4182     ** Spilling is also prohibited when in an error state since that could
  4183   4183     ** lead to database corruption.   In the current implementaton it 
  4184         -  ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
         4184  +  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
  4185   4185     ** while in the error state, hence it is impossible for this routine to
  4186   4186     ** be called in the error state.  Nevertheless, we include a NEVER()
  4187   4187     ** test for the error state as a safeguard against future changes.
  4188   4188     */
  4189   4189     if( NEVER(pPager->errCode) ) return SQLITE_OK;
  4190   4190     if( pPager->doNotSpill ) return SQLITE_OK;
  4191   4191     if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
................................................................................
  5025   5025     assert( (*ppPage)->pgno==pgno );
  5026   5026     assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
  5027   5027   
  5028   5028     if( (*ppPage)->pPager && !noContent ){
  5029   5029       /* In this case the pcache already contains an initialized copy of
  5030   5030       ** the page. Return without further ado.  */
  5031   5031       assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
  5032         -    PAGER_INCR(pPager->nHit);
         5032  +    pPager->nHit++;
  5033   5033       return SQLITE_OK;
  5034   5034   
  5035   5035     }else{
  5036   5036       /* The pager cache has created a new page. Its content needs to 
  5037   5037       ** be initialized.  */
  5038   5038   
  5039         -    PAGER_INCR(pPager->nMiss);
  5040   5039       pPg = *ppPage;
  5041   5040       pPg->pPager = pPager;
  5042   5041   
  5043   5042       /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
  5044   5043       ** number greater than this, or the unused locking-page, is requested. */
  5045   5044       if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
  5046   5045         rc = SQLITE_CORRUPT_BKPT;
................................................................................
  5068   5067           testcase( rc==SQLITE_NOMEM );
  5069   5068           sqlite3EndBenignMalloc();
  5070   5069         }
  5071   5070         memset(pPg->pData, 0, pPager->pageSize);
  5072   5071         IOTRACE(("ZERO %p %d\n", pPager, pgno));
  5073   5072       }else{
  5074   5073         assert( pPg->pPager==pPager );
         5074  +      pPager->nMiss++;
  5075   5075         rc = readDbPage(pPg);
  5076   5076         if( rc!=SQLITE_OK ){
  5077   5077           goto pager_acquire_err;
  5078   5078         }
  5079   5079       }
  5080   5080       pager_set_pagehash(pPg);
  5081   5081     }
................................................................................
  6104   6104     a[7] = pPager->nMiss;
  6105   6105     a[8] = 0;  /* Used to be pPager->nOvfl */
  6106   6106     a[9] = pPager->nRead;
  6107   6107     a[10] = pPager->nWrite;
  6108   6108     return a;
  6109   6109   }
  6110   6110   #endif
         6111  +
         6112  +/*
         6113  +** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
         6114  +** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
         6115  +** current cache hit or miss count, according to the value of eStat. If the 
         6116  +** reset parameter is non-zero, the cache hit or miss count is zeroed before 
         6117  +** returning.
         6118  +*/
         6119  +void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
         6120  +  int *piStat;
         6121  +
         6122  +  assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
         6123  +       || eStat==SQLITE_DBSTATUS_CACHE_MISS
         6124  +  );
         6125  +  if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
         6126  +    piStat = &pPager->nHit;
         6127  +  }else{
         6128  +    piStat = &pPager->nMiss;
         6129  +  }
         6130  +
         6131  +  *pnVal += *piStat;
         6132  +  if( reset ){
         6133  +    *piStat = 0;
         6134  +  }
         6135  +}
  6111   6136   
  6112   6137   /*
  6113   6138   ** Return true if this is an in-memory pager.
  6114   6139   */
  6115   6140   int sqlite3PagerIsMemdb(Pager *pPager){
  6116   6141     return MEMDB;
  6117   6142   }

Changes to src/pager.h.

   151    151   const char *sqlite3PagerFilename(Pager*);
   152    152   const sqlite3_vfs *sqlite3PagerVfs(Pager*);
   153    153   sqlite3_file *sqlite3PagerFile(Pager*);
   154    154   const char *sqlite3PagerJournalname(Pager*);
   155    155   int sqlite3PagerNosync(Pager*);
   156    156   void *sqlite3PagerTempSpace(Pager*);
   157    157   int sqlite3PagerIsMemdb(Pager*);
          158  +void sqlite3PagerCacheStat(Pager *, int, int, int *);
   158    159   
   159    160   /* Functions used to truncate the database file. */
   160    161   void sqlite3PagerTruncateImage(Pager*,Pgno);
   161    162   
   162    163   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
   163    164   void *sqlite3PagerCodec(DbPage *);
   164    165   #endif

Changes to src/pcache1.c.

    20     20   #include "sqliteInt.h"
    21     21   
    22     22   typedef struct PCache1 PCache1;
    23     23   typedef struct PgHdr1 PgHdr1;
    24     24   typedef struct PgFreeslot PgFreeslot;
    25     25   typedef struct PGroup PGroup;
    26     26   
    27         -typedef struct PGroupBlock PGroupBlock;
    28         -typedef struct PGroupBlockList PGroupBlockList;
    29     27   
    30     28   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
    31     29   ** of one or more PCaches that are able to recycle each others unpinned
    32     30   ** pages when they are under memory pressure.  A PGroup is an instance of
    33     31   ** the following object.
    34     32   **
    35     33   ** This page cache implementation works in one of two modes:
................................................................................
    52     50   struct PGroup {
    53     51     sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
    54     52     int nMaxPage;                  /* Sum of nMax for purgeable caches */
    55     53     int nMinPage;                  /* Sum of nMin for purgeable caches */
    56     54     int mxPinned;                  /* nMaxpage + 10 - nMinPage */
    57     55     int nCurrentPage;              /* Number of purgeable pages allocated */
    58     56     PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
    59         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
    60         -  int isBusy;                    /* Do not run ReleaseMemory() if true */
    61         -  PGroupBlockList *pBlockList;   /* List of block-lists for this group */
    62         -#endif
    63     57   };
    64     58   
    65         -/*
    66         -** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
    67         -** each PGroup structure has a linked list of the the following starting
    68         -** at PGroup.pBlockList. There is one entry for each distinct page-size 
    69         -** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
    70         -** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
    71         -** size requested by each pcache, which is the database page-size plus
    72         -** the various header structures used by the pcache, pager and btree layers.
    73         -** Usually around (pgsz+200) bytes.
    74         -**
    75         -** This size (pgsz+200) bytes is not allocated efficiently by some
    76         -** implementations of malloc. In particular, some implementations are only
    77         -** able to allocate blocks of memory chunks of 2^N bytes, where N is some
    78         -** integer value. Since the page-size is a power of 2, this means we
    79         -** end up wasting (pgsz-200) bytes in each allocation.
    80         -**
    81         -** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
    82         -** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes 
    83         -** are requested from malloc allocator. After a block is returned,
    84         -** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
    85         -** allocations can fit in the space returned by malloc(). This value may
    86         -** be more than M.
    87         -**
    88         -** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
    89         -** contains the number of allocations that will fit in the aData[] space.
    90         -** nEntry is limited to the number of bits in bitmask mUsed. If a slot
    91         -** within aData is in use, the corresponding bit in mUsed is set. Thus
    92         -** when (mUsed+1==(1 << nEntry)) the block is completely full.
    93         -**
    94         -** Each time a slot within a block is freed, the block is moved to the start
    95         -** of the linked-list. And if a block becomes completely full, then it is
    96         -** moved to the end of the list. As a result, when searching for a free
    97         -** slot, only the first block in the list need be examined. If it is full,
    98         -** then it is guaranteed that all blocks are full.
    99         -*/
   100         -struct PGroupBlockList {
   101         -  int nByte;                     /* Size of each allocation in bytes */
   102         -  PGroupBlock *pFirst;           /* First PGroupBlock in list */
   103         -  PGroupBlock *pLast;            /* Last PGroupBlock in list */
   104         -  PGroupBlockList *pNext;        /* Next block-list attached to group */
   105         -};
   106         -
   107         -struct PGroupBlock {
   108         -  Bitmask mUsed;                 /* Mask of used slots */
   109         -  int nEntry;                    /* Maximum number of allocations in aData[] */
   110         -  u8 *aData;                     /* Pointer to data block */
   111         -  PGroupBlock *pNext;            /* Next PGroupBlock in list */
   112         -  PGroupBlock *pPrev;            /* Previous PGroupBlock in list */
   113         -  PGroupBlockList *pList;        /* Owner list */
   114         -};
   115         -
   116         -/* Minimum value for PGroupBlock.nEntry */
   117         -#define PAGECACHE_BLOCKALLOC_MINENTRY 15
   118         -
   119     59   /* Each page cache is an instance of the following object.  Every
   120     60   ** open database file (including each in-memory database and each
   121     61   ** temporary or transient database) has a single page cache which
   122     62   ** is an instance of this object.
   123     63   **
   124     64   ** Pointers to structures of this type are cast and returned as 
   125     65   ** opaque sqlite3_pcache* handles.
................................................................................
   214    154   ** a pointer to a block of szPage bytes of data and the return value is
   215    155   ** a pointer to the associated PgHdr1 structure.
   216    156   **
   217    157   **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
   218    158   */
   219    159   #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
   220    160   #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
   221         -
   222         -/*
   223         -** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve 
   224         -** a PGroupBlock pointer based on a pointer to a page buffer. 
   225         -*/
   226         -#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
   227         -  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
   228         -
   229         -#define PAGE_GET_BLOCKPTR(pCache, pPg) \
   230         -  ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
   231         -
   232    161   
   233    162   /*
   234    163   ** Macros to enter and leave the PCache LRU mutex.
   235    164   */
   236    165   #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
   237    166   #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
   238    167   
................................................................................
   351    280       iSize = sqlite3MallocSize(p);
   352    281       sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
   353    282       return iSize;
   354    283     }
   355    284   }
   356    285   #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
   357    286   
   358         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
   359         -/*
   360         -** The block pBlock belongs to list pList but is not currently linked in.
   361         -** Insert it into the start of the list.
   362         -*/
   363         -static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
   364         -  pBlock->pPrev = 0;
   365         -  pBlock->pNext = pList->pFirst;
   366         -  pList->pFirst = pBlock;
   367         -  if( pBlock->pNext ){
   368         -    pBlock->pNext->pPrev = pBlock;
   369         -  }else{
   370         -    assert( pList->pLast==0 );
   371         -    pList->pLast = pBlock;
   372         -  }
   373         -}
   374         -
   375         -/*
   376         -** If there are no blocks in the list headed by pList, remove pList
   377         -** from the pGroup->pBlockList list and free it with sqlite3_free().
   378         -*/
   379         -static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
   380         -  assert( sqlite3_mutex_held(pGroup->mutex) );
   381         -  if( pList->pFirst==0 ){
   382         -    PGroupBlockList **pp;
   383         -    for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
   384         -    *pp = (*pp)->pNext;
   385         -    sqlite3_free(pList);
   386         -  }
   387         -}
   388         -#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
   389         -
   390    287   /*
   391    288   ** Allocate a new page object initially associated with cache pCache.
   392    289   */
   393    290   static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
   394    291     int nByte = sizeof(PgHdr1) + pCache->szPage;
   395         -  void *pPg = 0;
   396         -  PgHdr1 *p;
          292  +  PgHdr1 *p = 0;
          293  +  void *pPg;
   397    294   
   398         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
   399         -  PGroup *pGroup = pCache->pGroup;
   400         -  PGroupBlockList *pList;
   401         -  PGroupBlock *pBlock;
   402         -  int i;
   403         -
   404         -  nByte += sizeof(PGroupBlockList *);
   405         -  nByte = ROUND8(nByte);
   406         -
   407         -  for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
   408         -    if( pList->nByte==nByte ) break;
   409         -  }
   410         -  if( pList==0 ){
   411         -    PGroupBlockList *pNew;
   412         -    assert( pGroup->isBusy==0 );
   413         -    assert( sqlite3_mutex_held(pGroup->mutex) );
   414         -    pGroup->isBusy = 1;  /* Disable sqlite3PcacheReleaseMemory() */
   415         -    pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
   416         -    pGroup->isBusy = 0;  /* Reenable sqlite3PcacheReleaseMemory() */
   417         -    if( pNew==0 ){
   418         -      /* malloc() failure. Return early. */
   419         -      return 0;
   420         -    }
   421         -#ifdef SQLITE_DEBUG
   422         -    for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
   423         -      assert( pList->nByte!=nByte );
   424         -    }
   425         -#endif
   426         -    pNew->nByte = nByte;
   427         -    pNew->pNext = pGroup->pBlockList;
   428         -    pGroup->pBlockList = pNew;
   429         -    pList = pNew;
   430         -  }
   431         -
   432         -  pBlock = pList->pFirst;
   433         -  if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
   434         -    int sz;
   435         -
   436         -    /* Allocate a new block. Try to allocate enough space for the PGroupBlock
   437         -    ** structure and MINENTRY allocations of nByte bytes each. If the 
   438         -    ** allocator returns more memory than requested, then more than MINENTRY 
   439         -    ** allocations may fit in it. */
   440         -    assert( sqlite3_mutex_held(pGroup->mutex) );
   441         -    pcache1LeaveMutex(pCache->pGroup);
   442         -    sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
   443         -    pBlock = (PGroupBlock *)sqlite3Malloc(sz);
   444         -    pcache1EnterMutex(pCache->pGroup);
   445         -
   446         -    if( !pBlock ){
   447         -      freeListIfEmpty(pGroup, pList);
   448         -      return 0;
   449         -    }
   450         -    pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
   451         -    if( pBlock->nEntry>=BMS ){
   452         -      pBlock->nEntry = BMS-1;
   453         -    }
   454         -    pBlock->pList = pList;
   455         -    pBlock->mUsed = 0;
   456         -    pBlock->aData = (u8 *)&pBlock[1];
   457         -    addBlockToList(pList, pBlock);
   458         -
   459         -    sz = sqlite3MallocSize(pBlock);
   460         -    sqlite3_mutex_enter(pcache1.mutex);
   461         -    sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
   462         -    sqlite3_mutex_leave(pcache1.mutex);
   463         -  }
   464         -
   465         -  for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
   466         -    if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
   467         -      pBlock->mUsed |= ((Bitmask)1<<i);
   468         -      pPg = (void *)&pBlock->aData[pList->nByte * i];
   469         -    }
   470         -  }
   471         -  assert( pPg );
   472         -  PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
   473         -
   474         -  /* If the block is now full, shift it to the end of the list */
   475         -  if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
   476         -    assert( pList->pFirst==pBlock );
   477         -    assert( pBlock->pPrev==0 );
   478         -    assert( pList->pLast->pNext==0 );
   479         -    pList->pFirst = pBlock->pNext;
   480         -    pList->pFirst->pPrev = 0;
   481         -    pBlock->pPrev = pList->pLast;
   482         -    pBlock->pNext = 0;
   483         -    pList->pLast->pNext = pBlock;
   484         -    pList->pLast = pBlock;
   485         -  }
   486         -  p = PAGE_TO_PGHDR1(pCache, pPg);
   487         -  if( pCache->bPurgeable ){
   488         -    pCache->pGroup->nCurrentPage++;
   489         -  }
   490         -#else
   491    295     /* The group mutex must be released before pcache1Alloc() is called. This
   492    296     ** is because it may call sqlite3_release_memory(), which assumes that 
   493    297     ** this mutex is not held. */
   494    298     assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   495    299     pcache1LeaveMutex(pCache->pGroup);
   496    300     pPg = pcache1Alloc(nByte);
   497    301     pcache1EnterMutex(pCache->pGroup);
          302  +
   498    303     if( pPg ){
   499    304       p = PAGE_TO_PGHDR1(pCache, pPg);
   500    305       if( pCache->bPurgeable ){
   501    306         pCache->pGroup->nCurrentPage++;
   502    307       }
   503         -  }else{
   504         -    p = 0;
   505    308     }
   506         -#endif
   507    309     return p;
   508    310   }
   509    311   
   510    312   /*
   511    313   ** Free a page object allocated by pcache1AllocPage().
   512    314   **
   513    315   ** The pointer is allowed to be NULL, which is prudent.  But it turns out
   514    316   ** that the current implementation happens to never call this routine
   515    317   ** with a NULL pointer, so we mark the NULL test with ALWAYS().
   516    318   */
   517    319   static void pcache1FreePage(PgHdr1 *p){
   518    320     if( ALWAYS(p) ){
   519    321       PCache1 *pCache = p->pCache;
   520         -    void *pPg = PGHDR1_TO_PAGE(p);
   521         -
   522         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
   523         -    PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
   524         -    PGroupBlockList *pList = pBlock->pList;
   525         -    int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
   526         -
   527         -    assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
   528         -    assert( pBlock->mUsed & ((Bitmask)1<<i) );
   529         -    pBlock->mUsed &= ~((Bitmask)1<<i);
   530         -
   531         -    /* Remove the block from the list. If it is completely empty, free it.
   532         -    ** Or if it is not completely empty, re-insert it at the start of the
   533         -    ** list. */
   534         -    if( pList->pFirst==pBlock ){
   535         -      pList->pFirst = pBlock->pNext;
   536         -      if( pList->pFirst ) pList->pFirst->pPrev = 0;
   537         -    }else{
   538         -      pBlock->pPrev->pNext = pBlock->pNext;
   539         -    }
   540         -    if( pList->pLast==pBlock ){
   541         -      pList->pLast = pBlock->pPrev;
   542         -      if( pList->pLast ) pList->pLast->pNext = 0;
   543         -    }else{
   544         -      pBlock->pNext->pPrev = pBlock->pPrev;
   545         -    }
   546         -
   547         -    if( pBlock->mUsed==0 ){
   548         -      PGroup *pGroup = p->pCache->pGroup;
   549         -
   550         -      int sz = sqlite3MallocSize(pBlock);
   551         -      sqlite3_mutex_enter(pcache1.mutex);
   552         -      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
   553         -      sqlite3_mutex_leave(pcache1.mutex);
   554         -      freeListIfEmpty(pGroup, pList);
   555         -      sqlite3_free(pBlock);
   556         -    }else{
   557         -      addBlockToList(pList, pBlock);
   558         -    }
   559         -#else
   560    322       assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
   561         -    pcache1Free(pPg);
   562         -#endif
          323  +    pcache1Free(PGHDR1_TO_PAGE(p));
   563    324       if( pCache->bPurgeable ){
   564    325         pCache->pGroup->nCurrentPage--;
   565    326       }
   566    327     }
   567    328   }
   568    329   
   569    330   /*
................................................................................
  1166    927   **
  1167    928   ** nReq is the number of bytes of memory required. Once this much has
  1168    929   ** been released, the function returns. The return value is the total number 
  1169    930   ** of bytes of memory released.
  1170    931   */
  1171    932   int sqlite3PcacheReleaseMemory(int nReq){
  1172    933     int nFree = 0;
  1173         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
  1174         -  if( pcache1.grp.isBusy ) return 0;
  1175         -#endif
  1176    934     assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
  1177    935     assert( sqlite3_mutex_notheld(pcache1.mutex) );
  1178    936     if( pcache1.pStart==0 ){
  1179    937       PgHdr1 *p;
  1180    938       pcache1EnterMutex(&pcache1.grp);
  1181    939       while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
  1182    940         nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));

Changes to src/pragma.c.

   530    530     **  PRAGMA [database.]journal_mode =
   531    531     **                      (delete|persist|off|truncate|memory|wal|off)
   532    532     */
   533    533     if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
   534    534       int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
   535    535       int ii;           /* Loop counter */
   536    536   
   537         -    /* Force the schema to be loaded on all databases.  This cases all
   538         -    ** database files to be opened and the journal_modes set. */
          537  +    /* Force the schema to be loaded on all databases.  This causes all
          538  +    ** database files to be opened and the journal_modes set.  This is
          539  +    ** necessary because subsequent processing must know if the databases
          540  +    ** are in WAL mode. */
   539    541       if( sqlite3ReadSchema(pParse) ){
   540    542         goto pragma_out;
   541    543       }
   542    544   
   543    545       sqlite3VdbeSetNumCols(v, 1);
   544    546       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
   545    547   

Changes to src/shell.c.

  1025   1025       fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
  1026   1026       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
  1027   1027       fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
  1028   1028       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
  1029   1029       fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
  1030   1030       iHiwtr = iCur = -1;
  1031   1031       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
  1032         -    fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur); 
         1032  +    fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n", iCur);    iHiwtr = iCur = -1;
         1033  +    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
         1034  +    fprintf(pArg->out, "Page cache hits:                     %d\n", iCur);
         1035  +    iHiwtr = iCur = -1;
         1036  +    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
         1037  +    fprintf(pArg->out, "Page cache misses:                   %d\n", iCur); 
  1033   1038       iHiwtr = iCur = -1;
  1034   1039       sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
  1035   1040       fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n", iCur); 
  1036   1041       iHiwtr = iCur = -1;
  1037   1042       sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
  1038   1043       fprintf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n", iCur); 
  1039   1044     }
................................................................................
  1669   1674   
  1670   1675       open_db(p);
  1671   1676       nSep = strlen30(p->separator);
  1672   1677       if( nSep==0 ){
  1673   1678         fprintf(stderr, "Error: non-null separator required for import\n");
  1674   1679         return 1;
  1675   1680       }
  1676         -    zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
         1681  +    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
  1677   1682       if( zSql==0 ){
  1678   1683         fprintf(stderr, "Error: out of memory\n");
  1679   1684         return 1;
  1680   1685       }
  1681   1686       nByte = strlen30(zSql);
  1682   1687       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
  1683   1688       sqlite3_free(zSql);
................................................................................
  1691   1696       pStmt = 0;
  1692   1697       if( nCol==0 ) return 0; /* no columns, no error */
  1693   1698       zSql = malloc( nByte + 20 + nCol*2 );
  1694   1699       if( zSql==0 ){
  1695   1700         fprintf(stderr, "Error: out of memory\n");
  1696   1701         return 1;
  1697   1702       }
  1698         -    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
         1703  +    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable);
  1699   1704       j = strlen30(zSql);
  1700   1705       for(i=1; i<nCol; i++){
  1701   1706         zSql[j++] = ',';
  1702   1707         zSql[j++] = '?';
  1703   1708       }
  1704   1709       zSql[j++] = ')';
  1705   1710       zSql[j] = 0;

Changes to src/sqlite.h.in.

   763    763   ** have write permission on the directory containing the database file want
   764    764   ** to read the database file, as the WAL and shared memory files must exist
   765    765   ** in order for the database to be readable.  The fourth parameter to
   766    766   ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
   767    767   ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
   768    768   ** WAL mode.  If the integer is -1, then it is overwritten with the current
   769    769   ** WAL persistence setting.
          770  +**
          771  +** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
          772  +** a write transaction to indicate that, unless it is rolled back for some
          773  +** reason, the entire database file will be overwritten by the current 
          774  +** transaction. This is used by VACUUM operations.
   770    775   */
   771    776   #define SQLITE_FCNTL_LOCKSTATE           1
   772    777   #define SQLITE_FCNTL_GET_LOCKPROXYFILE   2
   773    778   #define SQLITE_FCNTL_SET_LOCKPROXYFILE   3
   774    779   #define SQLITE_FCNTL_LAST_ERRNO          4
   775    780   #define SQLITE_FCNTL_SIZE_HINT           5
   776    781   #define SQLITE_FCNTL_CHUNK_SIZE          6
   777    782   #define SQLITE_FCNTL_FILE_POINTER        7
   778    783   #define SQLITE_FCNTL_SYNC_OMITTED        8
   779    784   #define SQLITE_FCNTL_WIN32_AV_RETRY      9
   780    785   #define SQLITE_FCNTL_PERSIST_WAL        10
          786  +#define SQLITE_FCNTL_OVERWRITE          11
   781    787   
   782    788   /* deprecated names */
   783    789   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
   784    790   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
   785    791   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
   786    792   
   787    793   /*
................................................................................
  2846   2852   ** WHERE clause might influence the choice of query plan for a statement,
  2847   2853   ** then the statement will be automatically recompiled, as if there had been 
  2848   2854   ** a schema change, on the first  [sqlite3_step()] call following any change
  2849   2855   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  2850   2856   ** ^The specific value of WHERE-clause [parameter] might influence the 
  2851   2857   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  2852   2858   ** or [GLOB] operator or if the parameter is compared to an indexed column
  2853         -** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
         2859  +** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  2854   2860   ** the 
  2855   2861   ** </li>
  2856   2862   ** </ol>
  2857   2863   */
  2858   2864   int sqlite3_prepare(
  2859   2865     sqlite3 *db,            /* Database handle */
  2860   2866     const char *zSql,       /* SQL statement, UTF-8 encoded */
................................................................................
  5822   5828   **
  5823   5829   ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  5824   5830   ** <dd>This parameter returns the approximate number of of bytes of heap
  5825   5831   ** and lookaside memory used by all prepared statements associated with
  5826   5832   ** the database connection.)^
  5827   5833   ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  5828   5834   ** </dd>
         5835  +**
         5836  +** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
         5837  +** <dd>This parameter returns the number of pager cache hits that have
         5838  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
         5839  +** is always 0.
         5840  +** </dd>
         5841  +**
         5842  +** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
         5843  +** <dd>This parameter returns the number of pager cache misses that have
         5844  +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
         5845  +** is always 0.
         5846  +** </dd>
  5829   5847   ** </dl>
  5830   5848   */
  5831   5849   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  5832   5850   #define SQLITE_DBSTATUS_CACHE_USED           1
  5833   5851   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  5834   5852   #define SQLITE_DBSTATUS_STMT_USED            3
  5835   5853   #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
  5836   5854   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  5837   5855   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  5838         -#define SQLITE_DBSTATUS_MAX                  6   /* Largest defined DBSTATUS */
         5856  +#define SQLITE_DBSTATUS_CACHE_HIT            7
         5857  +#define SQLITE_DBSTATUS_CACHE_MISS           8
         5858  +#define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
  5839   5859   
  5840   5860   
  5841   5861   /*
  5842   5862   ** CAPI3REF: Prepared Statement Status
  5843   5863   **
  5844   5864   ** ^(Each prepared statement maintains various
  5845   5865   ** [SQLITE_STMTSTATUS counters] that measure the number
................................................................................
  5885   5905   **
  5886   5906   ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  5887   5907   ** <dd>^This is the number of rows inserted into transient indices that
  5888   5908   ** were created automatically in order to help joins run faster.
  5889   5909   ** A non-zero value in this counter may indicate an opportunity to
  5890   5910   ** improvement performance by adding permanent indices that do not
  5891   5911   ** need to be reinitialized each time the statement is run.</dd>
  5892         -**
  5893   5912   ** </dl>
  5894   5913   */
  5895   5914   #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
  5896   5915   #define SQLITE_STMTSTATUS_SORT              2
  5897   5916   #define SQLITE_STMTSTATUS_AUTOINDEX         3
  5898   5917   
  5899   5918   /*

Changes to src/sqliteInt.h.

   152    152   **
   153    153   ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   154    154   ** assert() macro is enabled, each call into the Win32 native heap subsystem
   155    155   ** will cause HeapValidate to be called.  If heap validation should fail, an
   156    156   ** assertion will be triggered.
   157    157   **
   158    158   ** (Historical note:  There used to be several other options, but we've
   159         -** pared it down to just these two.)
          159  +** pared it down to just these three.)
   160    160   **
   161    161   ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   162    162   ** the default.
   163    163   */
   164    164   #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
   165    165   # error "At most one of the following compile-time configuration options\
   166    166    is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
................................................................................
   447    447   ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
   448    448   ** that can be stored in a u32 without loss of data.  The value
   449    449   ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
   450    450   ** have to specify the value in the less intuitive manner shown:
   451    451   */
   452    452   #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
   453    453   
          454  +/*
          455  +** The datatype used to store estimates of the number of rows in a
          456  +** table or index.  This is an unsigned integer type.  For 99.9% of
          457  +** the world, a 32-bit integer is sufficient.  But a 64-bit integer
          458  +** can be used at compile-time if desired.
          459  +*/
          460  +#ifdef SQLITE_64BIT_STATS
          461  + typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
          462  +#else
          463  + typedef u32 tRowcnt;    /* 32-bit is the default */
          464  +#endif
          465  +
   454    466   /*
   455    467   ** Macros to determine whether the machine is big or little endian,
   456    468   ** evaluated at runtime.
   457    469   */
   458    470   #ifdef SQLITE_AMALGAMATION
   459    471   const int sqlite3one = 1;
   460    472   #else
................................................................................
  1290   1302   struct Table {
  1291   1303     char *zName;         /* Name of the table or view */
  1292   1304     int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  1293   1305     int nCol;            /* Number of columns in this table */
  1294   1306     Column *aCol;        /* Information about each column */
  1295   1307     Index *pIndex;       /* List of SQL indexes on this table. */
  1296   1308     int tnum;            /* Root BTree node for this table (see note above) */
  1297         -  unsigned nRowEst;    /* Estimated rows in table - from sqlite_stat1 table */
         1309  +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  1298   1310     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1299   1311     u16 nRef;            /* Number of pointers to this Table */
  1300   1312     u8 tabFlags;         /* Mask of TF_* values */
  1301   1313     u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1302   1314     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1303   1315     char *zColAff;       /* String defining the affinity of each column */
  1304   1316   #ifndef SQLITE_OMIT_CHECK
................................................................................
  1489   1501   ** algorithm to employ whenever an attempt is made to insert a non-unique
  1490   1502   ** element.
  1491   1503   */
  1492   1504   struct Index {
  1493   1505     char *zName;     /* Name of this index */
  1494   1506     int nColumn;     /* Number of columns in the table used by this index */
  1495   1507     int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
  1496         -  unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
         1508  +  tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
  1497   1509     Table *pTable;   /* The SQL table being indexed */
  1498   1510     int tnum;        /* Page containing root of this index in database file */
  1499   1511     u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1500   1512     u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
  1501   1513     u8 bUnordered;   /* Use this index for == or IN queries only */
  1502   1514     char *zColAff;   /* String defining the affinity of each column */
  1503   1515     Index *pNext;    /* The next index associated with the same table */
  1504   1516     Schema *pSchema; /* Schema containing this index */
  1505   1517     u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
  1506   1518     char **azColl;   /* Array of collation sequence names for index */
  1507         -  IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
         1519  +#ifdef SQLITE_ENABLE_STAT3
         1520  +  int nSample;             /* Number of elements in aSample[] */
         1521  +  tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
         1522  +  IndexSample *aSample;    /* Samples of the left-most key */
         1523  +#endif
  1508   1524   };
  1509   1525   
  1510   1526   /*
  1511   1527   ** Each sample stored in the sqlite_stat2 table is represented in memory 
  1512   1528   ** using a structure of this type.
  1513   1529   */
  1514   1530   struct IndexSample {
  1515   1531     union {
  1516   1532       char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
  1517         -    double r;       /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
         1533  +    double r;       /* Value if eType is SQLITE_FLOAT */
         1534  +    i64 i;          /* Value if eType is SQLITE_INTEGER */
  1518   1535     } u;
  1519   1536     u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
  1520         -  u8 nByte;         /* Size in byte of text or blob. */
         1537  +  int nByte;        /* Size in byte of text or blob. */
         1538  +  tRowcnt nEq;      /* Est. number of rows where the key equals this sample */
         1539  +  tRowcnt nLt;      /* Est. number of rows where key is less than this sample */
         1540  +  tRowcnt nDLt;     /* Est. number of distinct keys less than this sample */
  1521   1541   };
  1522   1542   
  1523   1543   /*
  1524   1544   ** Each token coming out of the lexer is an instance of
  1525   1545   ** this structure.  Tokens are also used as part of an expression.
  1526   1546   **
  1527   1547   ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
................................................................................
  1965   1985   ** and the WhereInfo.wctrlFlags member.
  1966   1986   */
  1967   1987   #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
  1968   1988   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
  1969   1989   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
  1970   1990   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
  1971   1991   #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
  1972         -#define WHERE_OMIT_OPEN        0x0010 /* Table cursors are already open */
  1973         -#define WHERE_OMIT_CLOSE       0x0020 /* Omit close of table & index cursors */
  1974         -#define WHERE_FORCE_TABLE      0x0040 /* Do not use an index-only search */
  1975         -#define WHERE_ONETABLE_ONLY    0x0080 /* Only code the 1st table in pTabList */
         1992  +#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
         1993  +#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
         1994  +#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
         1995  +#define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
  1976   1996   
  1977   1997   /*
  1978   1998   ** The WHERE clause processing routine has two halves.  The
  1979   1999   ** first part does the start of the WHERE loop and the second
  1980   2000   ** half does the tail of the WHERE loop.  An instance of
  1981   2001   ** this structure is returned by the first half and passed
  1982   2002   ** into the second half to give some continuity.
................................................................................
  2722   2742   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  2723   2743     int sqlite3ViewGetColumnNames(Parse*,Table*);
  2724   2744   #else
  2725   2745   # define sqlite3ViewGetColumnNames(A,B) 0
  2726   2746   #endif
  2727   2747   
  2728   2748   void sqlite3DropTable(Parse*, SrcList*, int, int);
         2749  +void sqlite3CodeDropTable(Parse*, Table*, int, int);
  2729   2750   void sqlite3DeleteTable(sqlite3*, Table*);
  2730   2751   #ifndef SQLITE_OMIT_AUTOINCREMENT
  2731   2752     void sqlite3AutoincrementBegin(Parse *pParse);
  2732   2753     void sqlite3AutoincrementEnd(Parse *pParse);
  2733   2754   #else
  2734   2755   # define sqlite3AutoincrementBegin(X)
  2735   2756   # define sqlite3AutoincrementEnd(X)
................................................................................
  2978   2999   const void *sqlite3ValueText(sqlite3_value*, u8);
  2979   3000   int sqlite3ValueBytes(sqlite3_value*, u8);
  2980   3001   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
  2981   3002                           void(*)(void*));
  2982   3003   void sqlite3ValueFree(sqlite3_value*);
  2983   3004   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  2984   3005   char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  2985         -#ifdef SQLITE_ENABLE_STAT2
         3006  +#ifdef SQLITE_ENABLE_STAT3
  2986   3007   char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
  2987   3008   #endif
  2988   3009   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2989   3010   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2990   3011   #ifndef SQLITE_AMALGAMATION
  2991   3012   extern const unsigned char sqlite3OpcodeProperty[];
  2992   3013   extern const unsigned char sqlite3UpperToLower[];

Changes to src/status.c.

   213    213         db->pnBytesFreed = 0;
   214    214   
   215    215         *pHighwater = 0;
   216    216         *pCurrent = nByte;
   217    217   
   218    218         break;
   219    219       }
          220  +
          221  +    /*
          222  +    ** Set *pCurrent to the total cache hits or misses encountered by all
          223  +    ** pagers the database handle is connected to. *pHighwater is always set 
          224  +    ** to zero.
          225  +    */
          226  +    case SQLITE_DBSTATUS_CACHE_HIT:
          227  +    case SQLITE_DBSTATUS_CACHE_MISS: {
          228  +      int i;
          229  +      int nRet = 0;
          230  +      assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
          231  +
          232  +      for(i=0; i<db->nDb; i++){
          233  +        if( db->aDb[i].pBt ){
          234  +          Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
          235  +          sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
          236  +        }
          237  +      }
          238  +      *pHighwater = 0;
          239  +      *pCurrent = nRet;
          240  +      break;
          241  +    }
   220    242   
   221    243       default: {
   222    244         rc = SQLITE_ERROR;
   223    245       }
   224    246     }
   225    247     sqlite3_mutex_leave(db->mutex);
   226    248     return rc;
   227    249   }

Changes to src/tclsqlite.c.

  3535   3535   /*
  3536   3536   ** If the macro TCLSH is one, then put in code this for the
  3537   3537   ** "main" routine that will initialize Tcl and take input from
  3538   3538   ** standard input, or if a file is named on the command line
  3539   3539   ** the TCL interpreter reads and evaluates that file.
  3540   3540   */
  3541   3541   #if TCLSH==1
  3542         -static char zMainloop[] =
  3543         -  "set line {}\n"
  3544         -  "while {![eof stdin]} {\n"
  3545         -    "if {$line!=\"\"} {\n"
  3546         -      "puts -nonewline \"> \"\n"
  3547         -    "} else {\n"
  3548         -      "puts -nonewline \"% \"\n"
  3549         -    "}\n"
  3550         -    "flush stdout\n"
  3551         -    "append line [gets stdin]\n"
  3552         -    "if {[info complete $line]} {\n"
  3553         -      "if {[catch {uplevel #0 $line} result]} {\n"
  3554         -        "puts stderr \"Error: $result\"\n"
  3555         -      "} elseif {$result!=\"\"} {\n"
  3556         -        "puts $result\n"
         3542  +static const char *tclsh_main_loop(void){
         3543  +  static const char zMainloop[] =
         3544  +    "set line {}\n"
         3545  +    "while {![eof stdin]} {\n"
         3546  +      "if {$line!=\"\"} {\n"
         3547  +        "puts -nonewline \"> \"\n"
         3548  +      "} else {\n"
         3549  +        "puts -nonewline \"% \"\n"
         3550  +      "}\n"
         3551  +      "flush stdout\n"
         3552  +      "append line [gets stdin]\n"
         3553  +      "if {[info complete $line]} {\n"
         3554  +        "if {[catch {uplevel #0 $line} result]} {\n"
         3555  +          "puts stderr \"Error: $result\"\n"
         3556  +        "} elseif {$result!=\"\"} {\n"
         3557  +          "puts $result\n"
         3558  +        "}\n"
         3559  +        "set line {}\n"
         3560  +      "} else {\n"
         3561  +        "append line \\n\n"
  3557   3562         "}\n"
  3558         -      "set line {}\n"
  3559         -    "} else {\n"
  3560         -      "append line \\n\n"
  3561   3563       "}\n"
  3562         -  "}\n"
  3563         -;
         3564  +  ;
         3565  +  return zMainloop;
         3566  +}
  3564   3567   #endif
  3565   3568   #if TCLSH==2
  3566         -static char zMainloop[] = 
  3567         -#include "spaceanal_tcl.h"
  3568         -;
         3569  +static const char *tclsh_main_loop(void);
  3569   3570   #endif
  3570   3571   
  3571   3572   #ifdef SQLITE_TEST
  3572   3573   static void init_all(Tcl_Interp *);
  3573   3574   static int init_all_cmd(
  3574   3575     ClientData cd,
  3575   3576     Tcl_Interp *interp,
................................................................................
  3644   3645   */
  3645   3646   static void init_all(Tcl_Interp *interp){
  3646   3647     Sqlite3_Init(interp);
  3647   3648   
  3648   3649   #if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5)
  3649   3650     Md5_Init(interp);
  3650   3651   #endif
         3652  +
         3653  +  /* Install the [register_dbstat_vtab] command to access the implementation
         3654  +  ** of virtual table dbstat (source file test_stat.c). This command is
         3655  +  ** required for testfixture and sqlite3_analyzer, but not by the production
         3656  +  ** Tcl extension.  */
         3657  +#if defined(SQLITE_TEST) || TCLSH==2
         3658  +  {
         3659  +    extern int SqlitetestStat_Init(Tcl_Interp*);
         3660  +    SqlitetestStat_Init(interp);
         3661  +  }
         3662  +#endif
  3651   3663   
  3652   3664   #ifdef SQLITE_TEST
  3653   3665     {
  3654   3666       extern int Sqliteconfig_Init(Tcl_Interp*);
  3655   3667       extern int Sqlitetest1_Init(Tcl_Interp*);
  3656   3668       extern int Sqlitetest2_Init(Tcl_Interp*);
  3657   3669       extern int Sqlitetest3_Init(Tcl_Interp*);
................................................................................
  3674   3686       extern int Sqlitetesttclvar_Init(Tcl_Interp*);
  3675   3687       extern int SqlitetestThread_Init(Tcl_Interp*);
  3676   3688       extern int SqlitetestOnefile_Init();
  3677   3689       extern int SqlitetestOsinst_Init(Tcl_Interp*);
  3678   3690       extern int Sqlitetestbackup_Init(Tcl_Interp*);
  3679   3691       extern int Sqlitetestintarray_Init(Tcl_Interp*);
  3680   3692       extern int Sqlitetestvfs_Init(Tcl_Interp *);
  3681         -    extern int SqlitetestStat_Init(Tcl_Interp*);
  3682   3693       extern int Sqlitetestrtree_Init(Tcl_Interp*);
  3683   3694       extern int Sqlitequota_Init(Tcl_Interp*);
  3684   3695       extern int Sqlitemultiplex_Init(Tcl_Interp*);
  3685   3696       extern int SqliteSuperlock_Init(Tcl_Interp*);
  3686   3697       extern int SqlitetestSyscall_Init(Tcl_Interp*);
  3687   3698       extern int Sqlitetestfuzzer_Init(Tcl_Interp*);
  3688   3699       extern int Sqlitetestwholenumber_Init(Tcl_Interp*);
................................................................................
  3718   3729       Sqlitetesttclvar_Init(interp);
  3719   3730       SqlitetestThread_Init(interp);
  3720   3731       SqlitetestOnefile_Init(interp);
  3721   3732       SqlitetestOsinst_Init(interp);
  3722   3733       Sqlitetestbackup_Init(interp);
  3723   3734       Sqlitetestintarray_Init(interp);
  3724   3735       Sqlitetestvfs_Init(interp);
  3725         -    SqlitetestStat_Init(interp);
  3726   3736       Sqlitetestrtree_Init(interp);
  3727   3737       Sqlitequota_Init(interp);
  3728   3738       Sqlitemultiplex_Init(interp);
  3729   3739       SqliteSuperlock_Init(interp);
  3730   3740       SqlitetestSyscall_Init(interp);
  3731   3741       Sqlitetestfuzzer_Init(interp);
  3732   3742       Sqlitetestwholenumber_Init(interp);
................................................................................
  3754   3764     Tcl_Interp *interp;
  3755   3765     
  3756   3766     /* Call sqlite3_shutdown() once before doing anything else. This is to
  3757   3767     ** test that sqlite3_shutdown() can be safely called by a process before
  3758   3768     ** sqlite3_initialize() is. */
  3759   3769     sqlite3_shutdown();
  3760   3770   
         3771  +  Tcl_FindExecutable(argv[0]);
         3772  +  interp = Tcl_CreateInterp();
         3773  +
  3761   3774   #if TCLSH==2
  3762   3775     sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  3763   3776   #endif
  3764         -  Tcl_FindExecutable(argv[0]);
  3765   3777   
  3766         -  interp = Tcl_CreateInterp();
  3767   3778     init_all(interp);
  3768   3779     if( argc>=2 ){
  3769   3780       int i;
  3770   3781       char zArgc[32];
  3771   3782       sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH));
  3772   3783       Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY);
  3773   3784       Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
................................................................................
  3780   3791         const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
  3781   3792         if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp);
  3782   3793         fprintf(stderr,"%s: %s\n", *argv, zInfo);
  3783   3794         return 1;
  3784   3795       }
  3785   3796     }
  3786   3797     if( TCLSH==2 || argc<=1 ){
  3787         -    Tcl_GlobalEval(interp, zMainloop);
         3798  +    Tcl_GlobalEval(interp, tclsh_main_loop());
  3788   3799     }
  3789   3800     return 0;
  3790   3801   }
  3791   3802   #endif /* TCLSH */

Changes to src/test_config.c.

    50     50   #endif
    51     51   
    52     52   #ifdef SQLITE_DEBUG
    53     53     Tcl_SetVar2(interp, "sqlite_options", "debug", "1", TCL_GLOBAL_ONLY);
    54     54   #else
    55     55     Tcl_SetVar2(interp, "sqlite_options", "debug", "0", TCL_GLOBAL_ONLY);
    56     56   #endif
           57  +
           58  +#ifdef SQLITE_DIRECT_OVERFLOW_READ
           59  +  Tcl_SetVar2(interp, "sqlite_options", "direct_read", "1", TCL_GLOBAL_ONLY);
           60  +#else
           61  +  Tcl_SetVar2(interp, "sqlite_options", "direct_read", "0", TCL_GLOBAL_ONLY);
           62  +#endif
    57     63   
    58     64   #ifdef SQLITE_DISABLE_DIRSYNC
    59     65     Tcl_SetVar2(interp, "sqlite_options", "dirsync", "0", TCL_GLOBAL_ONLY);
    60     66   #else
    61     67     Tcl_SetVar2(interp, "sqlite_options", "dirsync", "1", TCL_GLOBAL_ONLY);
    62     68   #endif
    63     69   
................................................................................
   419    425   #endif
   420    426   
   421    427   #ifdef SQLITE_ENABLE_STAT2
   422    428     Tcl_SetVar2(interp, "sqlite_options", "stat2", "1", TCL_GLOBAL_ONLY);
   423    429   #else
   424    430     Tcl_SetVar2(interp, "sqlite_options", "stat2", "0", TCL_GLOBAL_ONLY);
   425    431   #endif
          432  +
          433  +#ifdef SQLITE_ENABLE_STAT3
          434  +  Tcl_SetVar2(interp, "sqlite_options", "stat3", "1", TCL_GLOBAL_ONLY);
          435  +#else
          436  +  Tcl_SetVar2(interp, "sqlite_options", "stat3", "0", TCL_GLOBAL_ONLY);
          437  +#endif
   426    438   
   427    439   #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
   428    440   #  if defined(__APPLE__)
   429    441   #    define SQLITE_ENABLE_LOCKING_STYLE 1
   430    442   #  else
   431    443   #    define SQLITE_ENABLE_LOCKING_STYLE 0
   432    444   #  endif
................................................................................
   560    572   #endif
   561    573   
   562    574   #ifdef YYTRACKMAXSTACKDEPTH
   563    575     Tcl_SetVar2(interp, "sqlite_options", "yytrackmaxstackdepth", "1", TCL_GLOBAL_ONLY);
   564    576   #else
   565    577     Tcl_SetVar2(interp, "sqlite_options", "yytrackmaxstackdepth", "0", TCL_GLOBAL_ONLY);
   566    578   #endif
   567         -
   568         -#ifdef SQLITE_PAGECACHE_BLOCKALLOC
   569         -  Tcl_SetVar2(interp, "sqlite_options", "blockalloc", "1", TCL_GLOBAL_ONLY);
   570         -#else
   571         -  Tcl_SetVar2(interp, "sqlite_options", "blockalloc", "0", TCL_GLOBAL_ONLY);
   572         -#endif
   573    579     
   574    580   #ifdef __APPLE__
   575    581   # if  defined(__ppc__)
   576    582     Tcl_SetVar2(interp, "os_options", "arch", "ppc", TCL_GLOBAL_ONLY);
   577    583   # elif defined(__i386__)
   578    584     Tcl_SetVar2(interp, "os_options", "arch", "i386", TCL_GLOBAL_ONLY);
   579    585   # elif defined(__x86_64__)

Changes to src/test_malloc.c.

  1321   1321     } aOp[] = {
  1322   1322       { "LOOKASIDE_USED",      SQLITE_DBSTATUS_LOOKASIDE_USED      },
  1323   1323       { "CACHE_USED",          SQLITE_DBSTATUS_CACHE_USED          },
  1324   1324       { "SCHEMA_USED",         SQLITE_DBSTATUS_SCHEMA_USED         },
  1325   1325       { "STMT_USED",           SQLITE_DBSTATUS_STMT_USED           },
  1326   1326       { "LOOKASIDE_HIT",       SQLITE_DBSTATUS_LOOKASIDE_HIT       },
  1327   1327       { "LOOKASIDE_MISS_SIZE", SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE },
  1328         -    { "LOOKASIDE_MISS_FULL", SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL }
         1328  +    { "LOOKASIDE_MISS_FULL", SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL },
         1329  +    { "CACHE_HIT",           SQLITE_DBSTATUS_CACHE_HIT           },
         1330  +    { "CACHE_MISS",          SQLITE_DBSTATUS_CACHE_MISS          }
  1329   1331     };
  1330   1332     Tcl_Obj *pResult;
  1331   1333     if( objc!=4 ){
  1332         -    Tcl_WrongNumArgs(interp, 1, objv, "PARAMETER RESETFLAG");
         1334  +    Tcl_WrongNumArgs(interp, 1, objv, "DB PARAMETER RESETFLAG");
  1333   1335       return TCL_ERROR;
  1334   1336     }
  1335   1337     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  1336   1338     zOpName = Tcl_GetString(objv[2]);
  1337   1339     if( memcmp(zOpName, "SQLITE_", 7)==0 ) zOpName += 7;
  1338   1340     if( memcmp(zOpName, "DBSTATUS_", 9)==0 ) zOpName += 9;
  1339   1341     for(i=0; i<ArraySize(aOp); i++){

Changes to src/test_stat.c.

    14     14   **
    15     15   ** The dbstat virtual table is used to extract low-level formatting
    16     16   ** information from an SQLite database in order to implement the
    17     17   ** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
    18     18   ** for an example implementation.
    19     19   */
    20     20   
    21         -#include "sqliteInt.h"
           21  +#ifndef SQLITE_AMALGAMATION
           22  +# include "sqliteInt.h"
           23  +#endif
    22     24   
    23     25   #ifndef SQLITE_OMIT_VIRTUALTABLE
    24     26   
    25     27   /*
    26     28   ** Page paths:
    27     29   ** 
    28     30   **   The value of the 'path' column describes the path taken from the 
................................................................................
    58     60     "  name       STRING,           /* Name of table or index */"             \
    59     61     "  path       INTEGER,          /* Path to page from root */"             \
    60     62     "  pageno     INTEGER,          /* Page number */"                        \
    61     63     "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"   \
    62     64     "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"     \
    63     65     "  payload    INTEGER,          /* Bytes of payload on this page */"      \
    64     66     "  unused     INTEGER,          /* Bytes of unused space on this page */" \
    65         -  "  mx_payload INTEGER           /* Largest payload size of all cells */"  \
           67  +  "  mx_payload INTEGER,          /* Largest payload size of all cells */"  \
           68  +  "  pgoffset   INTEGER,          /* Offset of page in file */"             \
           69  +  "  pgsize     INTEGER           /* Size of the page */"                   \
    66     70     ");"
    67     71   
    68         -#if 0
    69         -#define VTAB_SCHEMA2                                                        \
    70         -  "CREATE TABLE yy( "                                                       \
    71         -  "  pageno   INTEGER,            /* B-tree page number */"                 \
    72         -  "  cellno   INTEGER,            /* Cell number within page */"            \
    73         -  "  local    INTEGER,            /* Bytes of content stored locally */"    \
    74         -  "  payload  INTEGER,            /* Total cell payload size */"            \
    75         -  "  novfl    INTEGER             /* Number of overflow pages */"           \
    76         -  ");"
    77         -#endif
    78         -
    79     72   
    80     73   typedef struct StatTable StatTable;
    81     74   typedef struct StatCursor StatCursor;
    82     75   typedef struct StatPage StatPage;
    83     76   typedef struct StatCell StatCell;
    84     77   
    85     78   struct StatCell {
................................................................................
   120    113     char *zPath;                    /* Value of 'path' column */
   121    114     u32 iPageno;                    /* Value of 'pageno' column */
   122    115     char *zPagetype;                /* Value of 'pagetype' column */
   123    116     int nCell;                      /* Value of 'ncell' column */
   124    117     int nPayload;                   /* Value of 'payload' column */
   125    118     int nUnused;                    /* Value of 'unused' column */
   126    119     int nMxPayload;                 /* Value of 'mx_payload' column */
          120  +  i64 iOffset;                    /* Value of 'pgOffset' column */
          121  +  int szPage;                     /* Value of 'pgSize' column */
   127    122   };
   128    123   
   129    124   struct StatTable {
   130    125     sqlite3_vtab base;
   131    126     sqlite3 *db;
   132    127   };
   133    128   
................................................................................
   277    272   }
   278    273   
   279    274   static int statDecodePage(Btree *pBt, StatPage *p){
   280    275     int nUnused;
   281    276     int iOff;
   282    277     int nHdr;
   283    278     int isLeaf;
          279  +  int szPage;
   284    280   
   285    281     u8 *aData = sqlite3PagerGetData(p->pPg);
   286    282     u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
   287    283   
   288    284     p->flags = aHdr[0];
   289    285     p->nCell = get2byte(&aHdr[3]);
   290    286     p->nMxPayload = 0;
................................................................................
   297    293     iOff = get2byte(&aHdr[1]);
   298    294     while( iOff ){
   299    295       nUnused += get2byte(&aData[iOff+2]);
   300    296       iOff = get2byte(&aData[iOff]);
   301    297     }
   302    298     p->nUnused = nUnused;
   303    299     p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
          300  +  szPage = sqlite3BtreeGetPageSize(pBt);
   304    301   
   305    302     if( p->nCell ){
   306    303       int i;                        /* Used to iterate through cells */
   307         -    int nUsable = sqlite3BtreeGetPageSize(pBt) - sqlite3BtreeGetReserve(pBt);
          304  +    int nUsable = szPage - sqlite3BtreeGetReserve(pBt);
   308    305   
   309    306       p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell));
   310    307       memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
   311    308   
   312    309       for(i=0; i<p->nCell; i++){
   313    310         StatCell *pCell = &p->aCell[i];
   314    311   
................................................................................
   354    351           }
   355    352         }
   356    353       }
   357    354     }
   358    355   
   359    356     return SQLITE_OK;
   360    357   }
          358  +
          359  +/*
          360  +** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
          361  +** the current value of pCsr->iPageno.
          362  +*/
          363  +static void statSizeAndOffset(StatCursor *pCsr){
          364  +  StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
          365  +  Btree *pBt = pTab->db->aDb[0].pBt;
          366  +  Pager *pPager = sqlite3BtreePager(pBt);
          367  +  sqlite3_file *fd;
          368  +  sqlite3_int64 x[2];
          369  +
          370  +  /* The default page size and offset */
          371  +  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
          372  +  pCsr->iOffset = pCsr->szPage * (pCsr->iPageno - 1);
          373  +
          374  +  /* If connected to a ZIPVFS backend, override the page size and
          375  +  ** offset with actual values obtained from ZIPVFS.
          376  +  */
          377  +  fd = sqlite3PagerFile(pPager);
          378  +  x[0] = pCsr->iPageno;
          379  +  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
          380  +    pCsr->iOffset = x[0];
          381  +    pCsr->szPage = x[1];
          382  +  }
          383  +}
   361    384   
   362    385   /*
   363    386   ** Move a statvfs cursor to the next entry in the file.
   364    387   */
   365    388   static int statNext(sqlite3_vtab_cursor *pCursor){
   366    389     int rc;
   367    390     int nPayload;
................................................................................
   413    436             pCsr->nUnused = 0;
   414    437             pCsr->nPayload = nUsable - 4;
   415    438           }else{
   416    439             pCsr->nPayload = pCell->nLastOvfl;
   417    440             pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
   418    441           }
   419    442           pCell->iOvfl++;
          443  +        statSizeAndOffset(pCsr);
   420    444           return SQLITE_OK;
   421    445         }
   422    446         if( p->iRightChildPg ) break;
   423    447         p->iCell++;
   424    448       }
   425    449   
   426    450       while( !p->iRightChildPg || p->iCell>p->nCell ){
................................................................................
   450    474     if( rc==SQLITE_OK ){
   451    475       int i;
   452    476       StatPage *p = &pCsr->aPage[pCsr->iPage];
   453    477       pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
   454    478       pCsr->iPageno = p->iPgno;
   455    479   
   456    480       statDecodePage(pBt, p);
          481  +    statSizeAndOffset(pCsr);
   457    482   
   458    483       switch( p->flags ){
   459    484         case 0x05:             /* table internal */
   460    485         case 0x02:             /* index internal */
   461    486           pCsr->zPagetype = "internal";
   462    487           break;
   463    488         case 0x0D:             /* table leaf */
................................................................................
   525    550         break;
   526    551       case 6:            /* unused */
   527    552         sqlite3_result_int(ctx, pCsr->nUnused);
   528    553         break;
   529    554       case 7:            /* mx_payload */
   530    555         sqlite3_result_int(ctx, pCsr->nMxPayload);
   531    556         break;
          557  +    case 8:            /* pgoffset */
          558  +      sqlite3_result_int64(ctx, pCsr->iOffset);
          559  +      break;
          560  +    case 9:            /* pgsize */
          561  +      sqlite3_result_int(ctx, pCsr->szPage);
          562  +      break;
   532    563     }
   533    564     return SQLITE_OK;
   534    565   }
   535    566   
   536    567   static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
   537    568     StatCursor *pCsr = (StatCursor *)pCursor;
   538    569     *pRowid = pCsr->iPageno;
................................................................................
   564    595     };
   565    596     sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
   566    597     return SQLITE_OK;
   567    598   }
   568    599   
   569    600   #endif
   570    601   
   571         -#ifdef SQLITE_TEST
          602  +#if defined(SQLITE_TEST) || TCLSH==2
   572    603   #include <tcl.h>
   573    604   
   574    605   static int test_dbstat(
   575    606     void *clientData,
   576    607     Tcl_Interp *interp,
   577    608     int objc,
   578    609     Tcl_Obj *CONST objv[]
................................................................................
   600    631   #endif
   601    632   }
   602    633   
   603    634   int SqlitetestStat_Init(Tcl_Interp *interp){
   604    635     Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0);
   605    636     return TCL_OK;
   606    637   }
   607         -#endif
          638  +#endif /* if defined(SQLITE_TEST) || TCLSH==2 */

Changes to src/utf.c.

   460    460   ** is set to the length of the returned string in bytes. The call should
   461    461   ** arrange to call sqlite3DbFree() on the returned pointer when it is
   462    462   ** no longer required.
   463    463   ** 
   464    464   ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
   465    465   ** flag set.
   466    466   */
   467         -#ifdef SQLITE_ENABLE_STAT2
          467  +#ifdef SQLITE_ENABLE_STAT3
   468    468   char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
   469    469     Mem m;
   470    470     memset(&m, 0, sizeof(m));
   471    471     m.db = db;
   472    472     sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
   473    473     if( sqlite3VdbeMemTranslate(&m, enc) ){
   474    474       assert( db->mallocFailed );

Changes to src/vacuum.c.

    41     41       return SQLITE_NOMEM;
    42     42     }
    43     43     if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
    44     44       sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
    45     45       return sqlite3_errcode(db);
    46     46     }
    47     47     VVA_ONLY( rc = ) sqlite3_step(pStmt);
    48         -  assert( rc!=SQLITE_ROW );
           48  +  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
    49     49     return vacuumFinalize(db, pStmt, pzErrMsg);
    50     50   }
    51     51   
    52     52   /*
    53     53   ** Execute zSql on database db. The statement returns exactly
    54     54   ** one column. Execute this as SQL on the same database.
    55     55   */
................................................................................
   259    259         "  SELECT type, name, tbl_name, rootpage, sql"
   260    260         "    FROM main.sqlite_master"
   261    261         "   WHERE type='view' OR type='trigger'"
   262    262         "      OR (type='table' AND rootpage=0)"
   263    263     );
   264    264     if( rc ) goto end_of_vacuum;
   265    265   
   266         -  /* At this point, unless the main db was completely empty, there is now a
   267         -  ** transaction open on the vacuum database, but not on the main database.
   268         -  ** Open a btree level transaction on the main database. This allows a
   269         -  ** call to sqlite3BtreeCopyFile(). The main database btree level
   270         -  ** transaction is then committed, so the SQL level never knows it was
   271         -  ** opened for writing. This way, the SQL transaction used to create the
   272         -  ** temporary database never needs to be committed.
          266  +  /* At this point, there is a write transaction open on both the 
          267  +  ** vacuum database and the main database. Assuming no error occurs,
          268  +  ** both transactions are closed by this block - the main database
          269  +  ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
          270  +  ** call to sqlite3BtreeCommit().
   273    271     */
   274    272     {
   275    273       u32 meta;
   276    274       int i;
   277    275   
   278    276       /* This array determines which meta meta values are preserved in the
   279    277       ** vacuum.  Even entries are the meta value number and odd entries

Changes to src/vdbeaux.c.

   571    571   }
   572    572   
   573    573   /*
   574    574   ** Change the P2 operand of instruction addr so that it points to
   575    575   ** the address of the next instruction to be coded.
   576    576   */
   577    577   void sqlite3VdbeJumpHere(Vdbe *p, int addr){
   578         -  assert( addr>=0 );
   579         -  sqlite3VdbeChangeP2(p, addr, p->nOp);
          578  +  assert( addr>=0 || p->db->mallocFailed );
          579  +  if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
   580    580   }
   581    581   
   582    582   
   583    583   /*
   584    584   ** If the input FuncDef structure is ephemeral, then free it.  If
   585    585   ** the FuncDef is not ephermal, then do nothing.
   586    586   */

Changes to src/vdbemem.c.

  1022   1022   
  1023   1023     if( !pExpr ){
  1024   1024       *ppVal = 0;
  1025   1025       return SQLITE_OK;
  1026   1026     }
  1027   1027     op = pExpr->op;
  1028   1028   
  1029         -  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
         1029  +  /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
  1030   1030     ** The ifdef here is to enable us to achieve 100% branch test coverage even
  1031         -  ** when SQLITE_ENABLE_STAT2 is omitted.
         1031  +  ** when SQLITE_ENABLE_STAT3 is omitted.
  1032   1032     */
  1033         -#ifdef SQLITE_ENABLE_STAT2
         1033  +#ifdef SQLITE_ENABLE_STAT3
  1034   1034     if( op==TK_REGISTER ) op = pExpr->op2;
  1035   1035   #else
  1036   1036     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
  1037   1037   #endif
  1038   1038   
  1039   1039     /* Handle negative integers in a single step.  This is needed in the
  1040   1040     ** case when the value is -9223372036854775808.

Changes to src/where.c.

   114    114   #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
   115    115   #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
   116    116   #define TERM_CODED      0x04   /* This term is already coded */
   117    117   #define TERM_COPIED     0x08   /* Has a child */
   118    118   #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
   119    119   #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
   120    120   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
   121         -#ifdef SQLITE_ENABLE_STAT2
          121  +#ifdef SQLITE_ENABLE_STAT3
   122    122   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
   123    123   #else
   124         -#  define TERM_VNULL    0x00   /* Disabled if not using stat2 */
          124  +#  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
   125    125   #endif
   126    126   
   127    127   /*
   128    128   ** An instance of the following structure holds all information about a
   129    129   ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
          130  +**
          131  +** Explanation of pOuter:  For a WHERE clause of the form
          132  +**
          133  +**           a AND ((b AND c) OR (d AND e)) AND f
          134  +**
          135  +** There are separate WhereClause objects for the whole clause and for
          136  +** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
          137  +** subclauses points to the WhereClause object for the whole clause.
   130    138   */
   131    139   struct WhereClause {
   132    140     Parse *pParse;           /* The parser context */
   133    141     WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
   134    142     Bitmask vmask;           /* Bitmask identifying virtual table cursors */
          143  +  WhereClause *pOuter;     /* Outer conjunction */
   135    144     u8 op;                   /* Split operator.  TK_AND or TK_OR */
          145  +  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
   136    146     int nTerm;               /* Number of terms */
   137    147     int nSlot;               /* Number of entries in a[] */
   138    148     WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
   139    149   #if defined(SQLITE_SMALL_STACK)
   140    150     WhereTerm aStatic[1];    /* Initial static space for a[] */
   141    151   #else
   142    152     WhereTerm aStatic[8];    /* Initial static space for a[] */
................................................................................
   257    267   
   258    268   /*
   259    269   ** Initialize a preallocated WhereClause structure.
   260    270   */
   261    271   static void whereClauseInit(
   262    272     WhereClause *pWC,        /* The WhereClause to be initialized */
   263    273     Parse *pParse,           /* The parsing context */
   264         -  WhereMaskSet *pMaskSet   /* Mapping from table cursor numbers to bitmasks */
          274  +  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
          275  +  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
   265    276   ){
   266    277     pWC->pParse = pParse;
   267    278     pWC->pMaskSet = pMaskSet;
          279  +  pWC->pOuter = 0;
   268    280     pWC->nTerm = 0;
   269    281     pWC->nSlot = ArraySize(pWC->aStatic);
   270    282     pWC->a = pWC->aStatic;
   271    283     pWC->vmask = 0;
          284  +  pWC->wctrlFlags = wctrlFlags;
   272    285   }
   273    286   
   274    287   /* Forward reference */
   275    288   static void whereClauseClear(WhereClause*);
   276    289   
   277    290   /*
   278    291   ** Deallocate all memory associated with a WhereOrInfo object.
................................................................................
   580    593     u32 op,               /* Mask of WO_xx values describing operator */
   581    594     Index *pIdx           /* Must be compatible with this index, if not NULL */
   582    595   ){
   583    596     WhereTerm *pTerm;
   584    597     int k;
   585    598     assert( iCur>=0 );
   586    599     op &= WO_ALL;
   587         -  for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
   588         -    if( pTerm->leftCursor==iCur
   589         -       && (pTerm->prereqRight & notReady)==0
   590         -       && pTerm->u.leftColumn==iColumn
   591         -       && (pTerm->eOperator & op)!=0
   592         -    ){
   593         -      if( pIdx && pTerm->eOperator!=WO_ISNULL ){
   594         -        Expr *pX = pTerm->pExpr;
   595         -        CollSeq *pColl;
   596         -        char idxaff;
   597         -        int j;
   598         -        Parse *pParse = pWC->pParse;
   599         -
   600         -        idxaff = pIdx->pTable->aCol[iColumn].affinity;
   601         -        if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
   602         -
   603         -        /* Figure out the collation sequence required from an index for
   604         -        ** it to be useful for optimising expression pX. Store this
   605         -        ** value in variable pColl.
   606         -        */
   607         -        assert(pX->pLeft);
   608         -        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
   609         -        assert(pColl || pParse->nErr);
   610         -
   611         -        for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
   612         -          if( NEVER(j>=pIdx->nColumn) ) return 0;
   613         -        }
   614         -        if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
   615         -      }
   616         -      return pTerm;
          600  +  for(; pWC; pWC=pWC->pOuter){
          601  +    for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
          602  +      if( pTerm->leftCursor==iCur
          603  +         && (pTerm->prereqRight & notReady)==0
          604  +         && pTerm->u.leftColumn==iColumn
          605  +         && (pTerm->eOperator & op)!=0
          606  +      ){
          607  +        if( pIdx && pTerm->eOperator!=WO_ISNULL ){
          608  +          Expr *pX = pTerm->pExpr;
          609  +          CollSeq *pColl;
          610  +          char idxaff;
          611  +          int j;
          612  +          Parse *pParse = pWC->pParse;
          613  +  
          614  +          idxaff = pIdx->pTable->aCol[iColumn].affinity;
          615  +          if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
          616  +  
          617  +          /* Figure out the collation sequence required from an index for
          618  +          ** it to be useful for optimising expression pX. Store this
          619  +          ** value in variable pColl.
          620  +          */
          621  +          assert(pX->pLeft);
          622  +          pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
          623  +          assert(pColl || pParse->nErr);
          624  +  
          625  +          for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
          626  +            if( NEVER(j>=pIdx->nColumn) ) return 0;
          627  +          }
          628  +          if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
          629  +        }
          630  +        return pTerm;
          631  +      }
   617    632       }
   618    633     }
   619    634     return 0;
   620    635   }
   621    636   
   622    637   /* Forward reference */
   623    638   static void exprAnalyze(SrcList*, WhereClause*, int);
................................................................................
   686    701     if( op==TK_VARIABLE ){
   687    702       Vdbe *pReprepare = pParse->pReprepare;
   688    703       int iCol = pRight->iColumn;
   689    704       pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
   690    705       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
   691    706         z = (char *)sqlite3_value_text(pVal);
   692    707       }
   693         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
          708  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-31526-56213 */
   694    709       assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
   695    710     }else if( op==TK_STRING ){
   696    711       z = pRight->u.zToken;
   697    712     }
   698    713     if( z ){
   699    714       cnt = 0;
   700    715       while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
................................................................................
   704    719         Expr *pPrefix;
   705    720         *pisComplete = c==wc[0] && z[cnt+1]==0;
   706    721         pPrefix = sqlite3Expr(db, TK_STRING, z);
   707    722         if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
   708    723         *ppPrefix = pPrefix;
   709    724         if( op==TK_VARIABLE ){
   710    725           Vdbe *v = pParse->pVdbe;
   711         -        sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
          726  +        sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-31526-56213 */
   712    727           if( *pisComplete && pRight->u.zToken[1] ){
   713    728             /* If the rhs of the LIKE expression is a variable, and the current
   714    729             ** value of the variable means there is no need to invoke the LIKE
   715    730             ** function, then no OP_Variable will be added to the program.
   716    731             ** This causes problems for the sqlite3_bind_parameter_name()
   717    732             ** API. To workaround them, add a dummy OP_Variable here.
   718    733             */ 
................................................................................
   873    888     */
   874    889     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
   875    890     assert( pExpr->op==TK_OR );
   876    891     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
   877    892     if( pOrInfo==0 ) return;
   878    893     pTerm->wtFlags |= TERM_ORINFO;
   879    894     pOrWc = &pOrInfo->wc;
   880         -  whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
          895  +  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
   881    896     whereSplit(pOrWc, pExpr, TK_OR);
   882    897     exprAnalyzeAll(pSrc, pOrWc);
   883    898     if( db->mallocFailed ) return;
   884    899     assert( pOrWc->nTerm>=2 );
   885    900   
   886    901     /*
   887    902     ** Compute the set of tables that might satisfy cases 1 or 2.
................................................................................
   900    915           WhereTerm *pAndTerm;
   901    916           int j;
   902    917           Bitmask b = 0;
   903    918           pOrTerm->u.pAndInfo = pAndInfo;
   904    919           pOrTerm->wtFlags |= TERM_ANDINFO;
   905    920           pOrTerm->eOperator = WO_AND;
   906    921           pAndWC = &pAndInfo->wc;
   907         -        whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
          922  +        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
   908    923           whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
   909    924           exprAnalyzeAll(pSrc, pAndWC);
          925  +        pAndWC->pOuter = pWC;
   910    926           testcase( db->mallocFailed );
   911    927           if( !db->mallocFailed ){
   912    928             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
   913    929               assert( pAndTerm->pExpr );
   914    930               if( allowedOp(pAndTerm->pExpr->op) ){
   915    931                 b |= getMask(pMaskSet, pAndTerm->leftCursor);
   916    932               }
................................................................................
  1336   1352         pTerm->nChild = 1;
  1337   1353         pTerm->wtFlags |= TERM_COPIED;
  1338   1354         pNewTerm->prereqAll = pTerm->prereqAll;
  1339   1355       }
  1340   1356     }
  1341   1357   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1342   1358   
  1343         -#ifdef SQLITE_ENABLE_STAT2
  1344         -  /* When sqlite_stat2 histogram data is available an operator of the
         1359  +#ifdef SQLITE_ENABLE_STAT3
         1360  +  /* When sqlite_stat3 histogram data is available an operator of the
  1345   1361     ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  1346   1362     ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  1347   1363     ** virtual term of that form.
  1348   1364     **
  1349   1365     ** Note that the virtual term must be tagged with TERM_VNULL.  This
  1350   1366     ** TERM_VNULL tag will suppress the not-null check at the beginning
  1351   1367     ** of the loop.  Without the TERM_VNULL flag, the not-null check at
................................................................................
  1375   1391         pNewTerm->iParent = idxTerm;
  1376   1392         pTerm = &pWC->a[idxTerm];
  1377   1393         pTerm->nChild = 1;
  1378   1394         pTerm->wtFlags |= TERM_COPIED;
  1379   1395         pNewTerm->prereqAll = pTerm->prereqAll;
  1380   1396       }
  1381   1397     }
  1382         -#endif /* SQLITE_ENABLE_STAT2 */
         1398  +#endif /* SQLITE_ENABLE_STAT */
  1383   1399   
  1384   1400     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1385   1401     ** an index for tables to the left of the join.
  1386   1402     */
  1387   1403     pTerm->prereqRight |= extraRight;
  1388   1404   }
  1389   1405   
................................................................................
  1797   1813   ){
  1798   1814   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  1799   1815     const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  1800   1816     const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
  1801   1817     WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
  1802   1818     WhereTerm *pTerm;                 /* A single term of the WHERE clause */
  1803   1819   
  1804         -  /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
  1805         -  ** are used */
         1820  +  /* The OR-clause optimization is disallowed if the INDEXED BY or
         1821  +  ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
  1806   1822     if( pSrc->notIndexed || pSrc->pIndex!=0 ){
  1807   1823       return;
         1824  +  }
         1825  +  if( pWC->wctrlFlags & WHERE_AND_ONLY ){
         1826  +    return;
  1808   1827     }
  1809   1828   
  1810   1829     /* Search the WHERE clause terms for a usable WO_OR term. */
  1811   1830     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1812   1831       if( pTerm->eOperator==WO_OR 
  1813   1832        && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
  1814   1833        && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
................................................................................
  1829   1848           if( pOrTerm->eOperator==WO_AND ){
  1830   1849             WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
  1831   1850             bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
  1832   1851           }else if( pOrTerm->leftCursor==iCur ){
  1833   1852             WhereClause tempWC;
  1834   1853             tempWC.pParse = pWC->pParse;
  1835   1854             tempWC.pMaskSet = pWC->pMaskSet;
         1855  +          tempWC.pOuter = pWC;
  1836   1856             tempWC.op = TK_AND;
  1837   1857             tempWC.a = pOrTerm;
  1838   1858             tempWC.nTerm = 1;
  1839   1859             bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
  1840   1860           }else{
  1841   1861             continue;
  1842   1862           }
................................................................................
  2423   2443     /* Try to find a more efficient access pattern by using multiple indexes
  2424   2444     ** to optimize an OR expression within the WHERE clause. 
  2425   2445     */
  2426   2446     bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
  2427   2447   }
  2428   2448   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  2429   2449   
         2450  +#ifdef SQLITE_ENABLE_STAT3
  2430   2451   /*
  2431         -** Argument pIdx is a pointer to an index structure that has an array of
  2432         -** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
  2433         -** stored in Index.aSample. These samples divide the domain of values stored
  2434         -** the index into (SQLITE_INDEX_SAMPLES+1) regions.
  2435         -** Region 0 contains all values less than the first sample value. Region
  2436         -** 1 contains values between the first and second samples.  Region 2 contains
  2437         -** values between samples 2 and 3.  And so on.  Region SQLITE_INDEX_SAMPLES
  2438         -** contains values larger than the last sample.
         2452  +** Estimate the location of a particular key among all keys in an
         2453  +** index.  Store the results in aStat as follows:
  2439   2454   **
  2440         -** If the index contains many duplicates of a single value, then it is
  2441         -** possible that two or more adjacent samples can hold the same value.
  2442         -** When that is the case, the smallest possible region code is returned
  2443         -** when roundUp is false and the largest possible region code is returned
  2444         -** when roundUp is true.
         2455  +**    aStat[0]      Est. number of rows less than pVal
         2456  +**    aStat[1]      Est. number of rows equal to pVal
  2445   2457   **
  2446         -** If successful, this function determines which of the regions value 
  2447         -** pVal lies in, sets *piRegion to the region index (a value between 0
  2448         -** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
  2449         -** Or, if an OOM occurs while converting text values between encodings,
  2450         -** SQLITE_NOMEM is returned and *piRegion is undefined.
         2458  +** Return SQLITE_OK on success.
  2451   2459   */
  2452         -#ifdef SQLITE_ENABLE_STAT2
  2453         -static int whereRangeRegion(
         2460  +static int whereKeyStats(
  2454   2461     Parse *pParse,              /* Database connection */
  2455   2462     Index *pIdx,                /* Index to consider domain of */
  2456   2463     sqlite3_value *pVal,        /* Value to consider */
  2457         -  int roundUp,                /* Return largest valid region if true */
  2458         -  int *piRegion               /* OUT: Region of domain in which value lies */
         2464  +  int roundUp,                /* Round up if true.  Round down if false */
         2465  +  tRowcnt *aStat              /* OUT: stats written here */
  2459   2466   ){
         2467  +  tRowcnt n;
         2468  +  IndexSample *aSample;
         2469  +  int i, eType;
         2470  +  int isEq = 0;
         2471  +  i64 v;
         2472  +  double r, rS;
         2473  +
  2460   2474     assert( roundUp==0 || roundUp==1 );
  2461         -  if( ALWAYS(pVal) ){
  2462         -    IndexSample *aSample = pIdx->aSample;
  2463         -    int i = 0;
  2464         -    int eType = sqlite3_value_type(pVal);
  2465         -
  2466         -    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
  2467         -      double r = sqlite3_value_double(pVal);
  2468         -      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
  2469         -        if( aSample[i].eType==SQLITE_NULL ) continue;
  2470         -        if( aSample[i].eType>=SQLITE_TEXT ) break;
  2471         -        if( roundUp ){
  2472         -          if( aSample[i].u.r>r ) break;
  2473         -        }else{
  2474         -          if( aSample[i].u.r>=r ) break;
  2475         -        }
  2476         -      }
  2477         -    }else if( eType==SQLITE_NULL ){
  2478         -      i = 0;
  2479         -      if( roundUp ){
  2480         -        while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
  2481         -      }
  2482         -    }else{ 
         2475  +  assert( pIdx->nSample>0 );
         2476  +  if( pVal==0 ) return SQLITE_ERROR;
         2477  +  n = pIdx->aiRowEst[0];
         2478  +  aSample = pIdx->aSample;
         2479  +  i = 0;
         2480  +  eType = sqlite3_value_type(pVal);
         2481  +
         2482  +  if( eType==SQLITE_INTEGER ){
         2483  +    v = sqlite3_value_int64(pVal);
         2484  +    r = (i64)v;
         2485  +    for(i=0; i<pIdx->nSample; i++){
         2486  +      if( aSample[i].eType==SQLITE_NULL ) continue;
         2487  +      if( aSample[i].eType>=SQLITE_TEXT ) break;
         2488  +      if( aSample[i].eType==SQLITE_INTEGER ){
         2489  +        if( aSample[i].u.i>=v ){
         2490  +          isEq = aSample[i].u.i==v;
         2491  +          break;
         2492  +        }
         2493  +      }else{
         2494  +        assert( aSample[i].eType==SQLITE_FLOAT );
         2495  +        if( aSample[i].u.r>=r ){
         2496  +          isEq = aSample[i].u.r==r;
         2497  +          break;
         2498  +        }
         2499  +      }
         2500  +    }
         2501  +  }else if( eType==SQLITE_FLOAT ){
         2502  +    r = sqlite3_value_double(pVal);
         2503  +    for(i=0; i<pIdx->nSample; i++){
         2504  +      if( aSample[i].eType==SQLITE_NULL ) continue;
         2505  +      if( aSample[i].eType>=SQLITE_TEXT ) break;
         2506  +      if( aSample[i].eType==SQLITE_FLOAT ){
         2507  +        rS = aSample[i].u.r;
         2508  +      }else{
         2509  +        rS = aSample[i].u.i;
         2510  +      }
         2511  +      if( rS>=r ){
         2512  +        isEq = rS==r;
         2513  +        break;
         2514  +      }
         2515  +    }
         2516  +  }else if( eType==SQLITE_NULL ){
         2517  +    i = 0;
         2518  +    if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
         2519  +  }else{
         2520  +    assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
         2521  +    for(i=0; i<pIdx->nSample; i++){
         2522  +      if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
         2523  +        break;
         2524  +      }
         2525  +    }
         2526  +    if( i<pIdx->nSample ){      
  2483   2527         sqlite3 *db = pParse->db;
  2484   2528         CollSeq *pColl;
  2485   2529         const u8 *z;
  2486         -      int n;
  2487         -
  2488         -      /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
  2489         -      assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
  2490         -
  2491   2530         if( eType==SQLITE_BLOB ){
  2492   2531           z = (const u8 *)sqlite3_value_blob(pVal);
  2493   2532           pColl = db->pDfltColl;
  2494   2533           assert( pColl->enc==SQLITE_UTF8 );
  2495   2534         }else{
  2496   2535           pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
  2497   2536           if( pColl==0 ){
................................................................................
  2502   2541           z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
  2503   2542           if( !z ){
  2504   2543             return SQLITE_NOMEM;
  2505   2544           }
  2506   2545           assert( z && pColl && pColl->xCmp );
  2507   2546         }
  2508   2547         n = sqlite3ValueBytes(pVal, pColl->enc);
  2509         -
  2510         -      for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
         2548  +  
         2549  +      for(; i<pIdx->nSample; i++){
  2511   2550           int c;
  2512   2551           int eSampletype = aSample[i].eType;
  2513         -        if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
  2514         -        if( (eSampletype!=eType) ) break;
         2552  +        if( eSampletype<eType ) continue;
         2553  +        if( eSampletype!=eType ) break;
  2515   2554   #ifndef SQLITE_OMIT_UTF16
  2516   2555           if( pColl->enc!=SQLITE_UTF8 ){
  2517   2556             int nSample;
  2518   2557             char *zSample = sqlite3Utf8to16(
  2519   2558                 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
  2520   2559             );
  2521   2560             if( !zSample ){
................................................................................
  2525   2564             c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
  2526   2565             sqlite3DbFree(db, zSample);
  2527   2566           }else
  2528   2567   #endif
  2529   2568           {
  2530   2569             c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
  2531   2570           }
  2532         -        if( c-roundUp>=0 ) break;
         2571  +        if( c>=0 ){
         2572  +          if( c==0 ) isEq = 1;
         2573  +          break;
         2574  +        }
  2533   2575         }
  2534   2576       }
         2577  +  }
  2535   2578   
  2536         -    assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
  2537         -    *piRegion = i;
         2579  +  /* At this point, aSample[i] is the first sample that is greater than
         2580  +  ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
         2581  +  ** than pVal.  If aSample[i]==pVal, then isEq==1.
         2582  +  */
         2583  +  if( isEq ){
         2584  +    assert( i<pIdx->nSample );
         2585  +    aStat[0] = aSample[i].nLt;
         2586  +    aStat[1] = aSample[i].nEq;
         2587  +  }else{
         2588  +    tRowcnt iLower, iUpper, iGap;
         2589  +    if( i==0 ){
         2590  +      iLower = 0;
         2591  +      iUpper = aSample[0].nLt;
         2592  +    }else{
         2593  +      iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
         2594  +      iLower = aSample[i-1].nEq + aSample[i-1].nLt;
         2595  +    }
         2596  +    aStat[1] = pIdx->avgEq;
         2597  +    if( iLower>=iUpper ){
         2598  +      iGap = 0;
         2599  +    }else{
         2600  +      iGap = iUpper - iLower;
         2601  +    }
         2602  +    if( roundUp ){
         2603  +      iGap = (iGap*2)/3;
         2604  +    }else{
         2605  +      iGap = iGap/3;
         2606  +    }
         2607  +    aStat[0] = iLower + iGap;
  2538   2608     }
  2539   2609     return SQLITE_OK;
  2540   2610   }
  2541         -#endif   /* #ifdef SQLITE_ENABLE_STAT2 */
         2611  +#endif /* SQLITE_ENABLE_STAT3 */
  2542   2612   
  2543   2613   /*
  2544   2614   ** If expression pExpr represents a literal value, set *pp to point to
  2545   2615   ** an sqlite3_value structure containing the same value, with affinity
  2546   2616   ** aff applied to it, before returning. It is the responsibility of the 
  2547   2617   ** caller to eventually release this structure by passing it to 
  2548   2618   ** sqlite3ValueFree().
................................................................................
  2552   2622   ** create an sqlite3_value structure containing this value, again with
  2553   2623   ** affinity aff applied to it, instead.
  2554   2624   **
  2555   2625   ** If neither of the above apply, set *pp to NULL.
  2556   2626   **
  2557   2627   ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
  2558   2628   */
  2559         -#ifdef SQLITE_ENABLE_STAT2
         2629  +#ifdef SQLITE_ENABLE_STAT3
  2560   2630   static int valueFromExpr(
  2561   2631     Parse *pParse, 
  2562   2632     Expr *pExpr, 
  2563   2633     u8 aff, 
  2564   2634     sqlite3_value **pp
  2565   2635   ){
  2566   2636     if( pExpr->op==TK_VARIABLE
  2567   2637      || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
  2568   2638     ){
  2569   2639       int iVar = pExpr->iColumn;
  2570         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
         2640  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-31526-56213 */
  2571   2641       *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
  2572   2642       return SQLITE_OK;
  2573   2643     }
  2574   2644     return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
  2575   2645   }
  2576   2646   #endif
  2577   2647   
................................................................................
  2600   2670   ** then nEq should be passed the value 1 (as the range restricted column,
  2601   2671   ** b, is the second left-most column of the index). Or, if the query is:
  2602   2672   **
  2603   2673   **   ... FROM t1 WHERE a > ? AND a < ? ...
  2604   2674   **
  2605   2675   ** then nEq should be passed 0.
  2606   2676   **
  2607         -** The returned value is an integer between 1 and 100, inclusive. A return
  2608         -** value of 1 indicates that the proposed range scan is expected to visit
  2609         -** approximately 1/100th (1%) of the rows selected by the nEq equality
  2610         -** constraints (if any). A return value of 100 indicates that it is expected
  2611         -** that the range scan will visit every row (100%) selected by the equality
  2612         -** constraints.
         2677  +** The returned value is an integer divisor to reduce the estimated
         2678  +** search space.  A return value of 1 means that range constraints are
         2679  +** no help at all.  A return value of 2 means range constraints are
         2680  +** expected to reduce the search space by half.  And so forth...
  2613   2681   **
  2614         -** In the absence of sqlite_stat2 ANALYZE data, each range inequality
  2615         -** reduces the search space by 3/4ths.  Hence a single constraint (x>?)
  2616         -** results in a return of 25 and a range constraint (x>? AND x<?) results
  2617         -** in a return of 6.
         2682  +** In the absence of sqlite_stat3 ANALYZE data, each range inequality
         2683  +** reduces the search space by a factor of 4.  Hence a single constraint (x>?)
         2684  +** results in a return of 4 and a range constraint (x>? AND x<?) results
         2685  +** in a return of 16.
  2618   2686   */
  2619   2687   static int whereRangeScanEst(
  2620   2688     Parse *pParse,       /* Parsing & code generating context */
  2621   2689     Index *p,            /* The index containing the range-compared column; "x" */
  2622   2690     int nEq,             /* index into p->aCol[] of the range-compared column */
  2623   2691     WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
  2624   2692     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
  2625         -  int *piEst           /* OUT: Return value */
         2693  +  double *pRangeDiv   /* OUT: Reduce search space by this divisor */
  2626   2694   ){
  2627   2695     int rc = SQLITE_OK;
  2628   2696   
  2629         -#ifdef SQLITE_ENABLE_STAT2
         2697  +#ifdef SQLITE_ENABLE_STAT3
  2630   2698   
  2631         -  if( nEq==0 && p->aSample ){
  2632         -    sqlite3_value *pLowerVal = 0;
  2633         -    sqlite3_value *pUpperVal = 0;
  2634         -    int iEst;
  2635         -    int iLower = 0;
  2636         -    int iUpper = SQLITE_INDEX_SAMPLES;
  2637         -    int roundUpUpper = 0;
  2638         -    int roundUpLower = 0;
         2699  +  if( nEq==0 && p->nSample ){
         2700  +    sqlite3_value *pRangeVal;
         2701  +    tRowcnt iLower = 0;
         2702  +    tRowcnt iUpper = p->aiRowEst[0];
         2703  +    tRowcnt a[2];
  2639   2704       u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
  2640   2705   
  2641   2706       if( pLower ){
  2642   2707         Expr *pExpr = pLower->pExpr->pRight;
  2643         -      rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
         2708  +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
  2644   2709         assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
  2645         -      roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
         2710  +      if( rc==SQLITE_OK
         2711  +       && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
         2712  +      ){
         2713  +        iLower = a[0];
         2714  +        if( pLower->eOperator==WO_GT ) iLower += a[1];
         2715  +      }
         2716  +      sqlite3ValueFree(pRangeVal);
  2646   2717       }
  2647   2718       if( rc==SQLITE_OK && pUpper ){
  2648   2719         Expr *pExpr = pUpper->pExpr->pRight;
  2649         -      rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
         2720  +      rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
  2650   2721         assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
  2651         -      roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
  2652         -    }
  2653         -
  2654         -    if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
  2655         -      sqlite3ValueFree(pLowerVal);
  2656         -      sqlite3ValueFree(pUpperVal);
  2657         -      goto range_est_fallback;
  2658         -    }else if( pLowerVal==0 ){
  2659         -      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
  2660         -      if( pLower ) iLower = iUpper/2;
  2661         -    }else if( pUpperVal==0 ){
  2662         -      rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
  2663         -      if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
  2664         -    }else{
  2665         -      rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
  2666         -      if( rc==SQLITE_OK ){
  2667         -        rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
  2668         -      }
  2669         -    }
  2670         -    WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
  2671         -
  2672         -    iEst = iUpper - iLower;
  2673         -    testcase( iEst==SQLITE_INDEX_SAMPLES );
  2674         -    assert( iEst<=SQLITE_INDEX_SAMPLES );
  2675         -    if( iEst<1 ){
  2676         -      *piEst = 50/SQLITE_INDEX_SAMPLES;
  2677         -    }else{
  2678         -      *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
  2679         -    }
  2680         -    sqlite3ValueFree(pLowerVal);
  2681         -    sqlite3ValueFree(pUpperVal);
  2682         -    return rc;
  2683         -  }
  2684         -range_est_fallback:
         2722  +      if( rc==SQLITE_OK
         2723  +       && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
         2724  +      ){
         2725  +        iUpper = a[0];
         2726  +        if( pUpper->eOperator==WO_LE ) iUpper += a[1];
         2727  +      }
         2728  +      sqlite3ValueFree(pRangeVal);
         2729  +    }
         2730  +    if( rc==SQLITE_OK ){
         2731  +      if( iUpper<=iLower ){
         2732  +        *pRangeDiv = (double)p->aiRowEst[0];
         2733  +      }else{
         2734  +        *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
         2735  +      }
         2736  +      WHERETRACE(("range scan regions: %u..%u  div=%g\n",
         2737  +                  (u32)iLower, (u32)iUpper, *pRangeDiv));
         2738  +      return SQLITE_OK;
         2739  +    }
         2740  +  }
  2685   2741   #else
  2686   2742     UNUSED_PARAMETER(pParse);
  2687   2743     UNUSED_PARAMETER(p);
  2688   2744     UNUSED_PARAMETER(nEq);
  2689   2745   #endif
  2690   2746     assert( pLower || pUpper );
  2691         -  *piEst = 100;
  2692         -  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
  2693         -  if( pUpper ) *piEst /= 4;
         2747  +  *pRangeDiv = (double)1;
         2748  +  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
         2749  +  if( pUpper ) *pRangeDiv *= (double)4;
  2694   2750     return rc;
  2695   2751   }
  2696   2752   
  2697         -#ifdef SQLITE_ENABLE_STAT2
         2753  +#ifdef SQLITE_ENABLE_STAT3
  2698   2754   /*
  2699   2755   ** Estimate the number of rows that will be returned based on
  2700   2756   ** an equality constraint x=VALUE and where that VALUE occurs in
  2701   2757   ** the histogram data.  This only works when x is the left-most
  2702         -** column of an index and sqlite_stat2 histogram data is available
         2758  +** column of an index and sqlite_stat3 histogram data is available
  2703   2759   ** for that index.  When pExpr==NULL that means the constraint is
  2704   2760   ** "x IS NULL" instead of "x=VALUE".
  2705   2761   **
  2706   2762   ** Write the estimated row count into *pnRow and return SQLITE_OK. 
  2707   2763   ** If unable to make an estimate, leave *pnRow unchanged and return
  2708   2764   ** non-zero.
  2709   2765   **
................................................................................
  2715   2771   static int whereEqualScanEst(
  2716   2772     Parse *pParse,       /* Parsing & code generating context */
  2717   2773     Index *p,            /* The index whose left-most column is pTerm */
  2718   2774     Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
  2719   2775     double *pnRow        /* Write the revised row estimate here */
  2720   2776   ){
  2721   2777     sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
  2722         -  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
  2723   2778     u8 aff;                   /* Column affinity */
  2724   2779     int rc;                   /* Subfunction return code */
  2725         -  double nRowEst;           /* New estimate of the number of rows */
         2780  +  tRowcnt a[2];             /* Statistics */
  2726   2781   
  2727   2782     assert( p->aSample!=0 );
         2783  +  assert( p->nSample>0 );
  2728   2784     aff = p->pTable->aCol[p->aiColumn[0]].affinity;
  2729   2785     if( pExpr ){
  2730   2786       rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
  2731   2787       if( rc ) goto whereEqualScanEst_cancel;
  2732   2788     }else{
  2733   2789       pRhs = sqlite3ValueNew(pParse->db);
  2734   2790     }
  2735   2791     if( pRhs==0 ) return SQLITE_NOTFOUND;
  2736         -  rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
  2737         -  if( rc ) goto whereEqualScanEst_cancel;
  2738         -  rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
  2739         -  if( rc ) goto whereEqualScanEst_cancel;
  2740         -  WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
  2741         -  if( iLower>=iUpper ){
  2742         -    nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
  2743         -    if( nRowEst<*pnRow ) *pnRow = nRowEst;
  2744         -  }else{
  2745         -    nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
  2746         -    *pnRow = nRowEst;
         2792  +  rc = whereKeyStats(pParse, p, pRhs, 0, a);
         2793  +  if( rc==SQLITE_OK ){
         2794  +    WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
         2795  +    *pnRow = a[1];
  2747   2796     }
  2748         -
  2749   2797   whereEqualScanEst_cancel:
  2750   2798     sqlite3ValueFree(pRhs);
  2751   2799     return rc;
  2752   2800   }
  2753         -#endif /* defined(SQLITE_ENABLE_STAT2) */
         2801  +#endif /* defined(SQLITE_ENABLE_STAT3) */
  2754   2802   
  2755         -#ifdef SQLITE_ENABLE_STAT2
         2803  +#ifdef SQLITE_ENABLE_STAT3
  2756   2804   /*
  2757   2805   ** Estimate the number of rows that will be returned based on
  2758   2806   ** an IN constraint where the right-hand side of the IN operator
  2759   2807   ** is a list of values.  Example:
  2760   2808   **
  2761   2809   **        WHERE x IN (1,2,3,4)
  2762   2810   **
................................................................................
  2771   2819   */
  2772   2820   static int whereInScanEst(
  2773   2821     Parse *pParse,       /* Parsing & code generating context */
  2774   2822     Index *p,            /* The index whose left-most column is pTerm */
  2775   2823     ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
  2776   2824     double *pnRow        /* Write the revised row estimate here */
  2777   2825   ){
  2778         -  sqlite3_value *pVal = 0;  /* One value from list */
  2779         -  int iLower, iUpper;       /* Range of histogram regions containing pRhs */
  2780         -  u8 aff;                   /* Column affinity */
  2781         -  int rc = SQLITE_OK;       /* Subfunction return code */
  2782         -  double nRowEst;           /* New estimate of the number of rows */
  2783         -  int nSpan = 0;            /* Number of histogram regions spanned */
  2784         -  int nSingle = 0;          /* Histogram regions hit by a single value */
  2785         -  int nNotFound = 0;        /* Count of values that are not constants */
  2786         -  int i;                               /* Loop counter */
  2787         -  u8 aSpan[SQLITE_INDEX_SAMPLES+1];    /* Histogram regions that are spanned */
  2788         -  u8 aSingle[SQLITE_INDEX_SAMPLES+1];  /* Histogram regions hit once */
         2826  +  int rc = SQLITE_OK;         /* Subfunction return code */
         2827  +  double nEst;                /* Number of rows for a single term */
         2828  +  double nRowEst = (double)0; /* New estimate of the number of rows */
         2829  +  int i;                      /* Loop counter */
  2789   2830   
  2790   2831     assert( p->aSample!=0 );
  2791         -  aff = p->pTable->aCol[p->aiColumn[0]].affinity;
  2792         -  memset(aSpan, 0, sizeof(aSpan));
  2793         -  memset(aSingle, 0, sizeof(aSingle));
  2794         -  for(i=0; i<pList->nExpr; i++){
  2795         -    sqlite3ValueFree(pVal);
  2796         -    rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
  2797         -    if( rc ) break;
  2798         -    if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
  2799         -      nNotFound++;
  2800         -      continue;
  2801         -    }
  2802         -    rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
  2803         -    if( rc ) break;
  2804         -    rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
  2805         -    if( rc ) break;
  2806         -    if( iLower>=iUpper ){
  2807         -      aSingle[iLower] = 1;
  2808         -    }else{
  2809         -      assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
  2810         -      while( iLower<iUpper ) aSpan[iLower++] = 1;
  2811         -    }
         2832  +  for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
         2833  +    nEst = p->aiRowEst[0];
         2834  +    rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
         2835  +    nRowEst += nEst;
  2812   2836     }
  2813   2837     if( rc==SQLITE_OK ){
  2814         -    for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
  2815         -      if( aSpan[i] ){
  2816         -        nSpan++;
  2817         -      }else if( aSingle[i] ){
  2818         -        nSingle++;
  2819         -      }
  2820         -    }
  2821         -    nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
  2822         -               + nNotFound*p->aiRowEst[1];
  2823   2838       if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
  2824   2839       *pnRow = nRowEst;
  2825         -    WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
  2826         -                 nSpan, nSingle, nNotFound, nRowEst));
         2840  +    WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
  2827   2841     }
  2828         -  sqlite3ValueFree(pVal);
  2829   2842     return rc;
  2830   2843   }
  2831         -#endif /* defined(SQLITE_ENABLE_STAT2) */
         2844  +#endif /* defined(SQLITE_ENABLE_STAT3) */
  2832   2845   
  2833   2846   
  2834   2847   /*
  2835   2848   ** Find the best query plan for accessing a particular table.  Write the
  2836   2849   ** best query plan and its cost into the WhereCost object supplied as the
  2837   2850   ** last parameter.
  2838   2851   **
................................................................................
  2871   2884   ){
  2872   2885     int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  2873   2886     Index *pProbe;              /* An index we are evaluating */
  2874   2887     Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
  2875   2888     int eqTermMask;             /* Current mask of valid equality operators */
  2876   2889     int idxEqTermMask;          /* Index mask of valid equality operators */
  2877   2890     Index sPk;                  /* A fake index object for the primary key */
  2878         -  unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
         2891  +  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
  2879   2892     int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  2880   2893     int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
  2881   2894   
  2882   2895     /* Initialize the cost to a worst-case value */
  2883   2896     memset(pCost, 0, sizeof(*pCost));
  2884   2897     pCost->rCost = SQLITE_BIG_DBL;
  2885   2898   
................................................................................
  2926   2939       eqTermMask = WO_EQ|WO_IN;
  2927   2940       pIdx = 0;
  2928   2941     }
  2929   2942   
  2930   2943     /* Loop over all indices looking for the best one to use
  2931   2944     */
  2932   2945     for(; pProbe; pIdx=pProbe=pProbe->pNext){
  2933         -    const unsigned int * const aiRowEst = pProbe->aiRowEst;
         2946  +    const tRowcnt * const aiRowEst = pProbe->aiRowEst;
  2934   2947       double cost;                /* Cost of using pProbe */
  2935   2948       double nRow;                /* Estimated number of rows in result set */
  2936         -    double log10N;              /* base-10 logarithm of nRow (inexact) */
         2949  +    double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
  2937   2950       int rev;                    /* True to scan in reverse order */
  2938   2951       int wsFlags = 0;
  2939   2952       Bitmask used = 0;
  2940   2953   
  2941   2954       /* The following variables are populated based on the properties of
  2942   2955       ** index being evaluated. They are then used to determine the expected
  2943   2956       ** cost and number of rows returned.
................................................................................
  2969   2982       **
  2970   2983       **  bInEst:  
  2971   2984       **    Set to true if there was at least one "x IN (SELECT ...)" term used 
  2972   2985       **    in determining the value of nInMul.  Note that the RHS of the
  2973   2986       **    IN operator must be a SELECT, not a value list, for this variable
  2974   2987       **    to be true.
  2975   2988       **
  2976         -    **  estBound:
  2977         -    **    An estimate on the amount of the table that must be searched.  A
  2978         -    **    value of 100 means the entire table is searched.  Range constraints
  2979         -    **    might reduce this to a value less than 100 to indicate that only
  2980         -    **    a fraction of the table needs searching.  In the absence of
  2981         -    **    sqlite_stat2 ANALYZE data, a single inequality reduces the search
  2982         -    **    space to 1/4rd its original size.  So an x>? constraint reduces
  2983         -    **    estBound to 25.  Two constraints (x>? AND x<?) reduce estBound to 6.
         2989  +    **  rangeDiv:
         2990  +    **    An estimate of a divisor by which to reduce the search space due
         2991  +    **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
         2992  +    **    data, a single inequality reduces the search space to 1/4rd its
         2993  +    **    original size (rangeDiv==4).  Two inequalities reduce the search
         2994  +    **    space to 1/16th of its original size (rangeDiv==16).
  2984   2995       **
  2985   2996       **  bSort:   
  2986   2997       **    Boolean. True if there is an ORDER BY clause that will require an 
  2987   2998       **    external sort (i.e. scanning the index being evaluated will not 
  2988   2999       **    correctly order records).
  2989   3000       **
  2990   3001       **  bLookup: 
................................................................................
  3001   3012       **
  3002   3013       **             SELECT a, b    FROM tbl WHERE a = 1;
  3003   3014       **             SELECT a, b, c FROM tbl WHERE a = 1;
  3004   3015       */
  3005   3016       int nEq;                      /* Number of == or IN terms matching index */
  3006   3017       int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
  3007   3018       int nInMul = 1;               /* Number of distinct equalities to lookup */
  3008         -    int estBound = 100;           /* Estimated reduction in search space */
         3019  +    double rangeDiv = (double)1;  /* Estimated reduction in search space */
  3009   3020       int nBound = 0;               /* Number of range constraints seen */
  3010   3021       int bSort = !!pOrderBy;       /* True if external sort required */
  3011   3022       int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
  3012   3023       int bLookup = 0;              /* True if not a covering index */
  3013   3024       WhereTerm *pTerm;             /* A single term of the WHERE clause */
  3014         -#ifdef SQLITE_ENABLE_STAT2
         3025  +#ifdef SQLITE_ENABLE_STAT3
  3015   3026       WhereTerm *pFirstTerm = 0;    /* First term matching the index */
  3016   3027   #endif
  3017   3028   
  3018   3029       /* Determine the values of nEq and nInMul */
  3019   3030       for(nEq=0; nEq<pProbe->nColumn; nEq++){
  3020   3031         int j = pProbe->aiColumn[nEq];
  3021   3032         pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
  3022   3033         if( pTerm==0 ) break;
  3023   3034         wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
         3035  +      testcase( pTerm->pWC!=pWC );
  3024   3036         if( pTerm->eOperator & WO_IN ){
  3025   3037           Expr *pExpr = pTerm->pExpr;
  3026   3038           wsFlags |= WHERE_COLUMN_IN;
  3027   3039           if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3028   3040             /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
  3029   3041             nInMul *= 25;
  3030   3042             bInEst = 1;
................................................................................
  3031   3043           }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
  3032   3044             /* "x IN (value, value, ...)" */
  3033   3045             nInMul *= pExpr->x.pList->nExpr;
  3034   3046           }
  3035   3047         }else if( pTerm->eOperator & WO_ISNULL ){
  3036   3048           wsFlags |= WHERE_COLUMN_NULL;
  3037   3049         }
  3038         -#ifdef SQLITE_ENABLE_STAT2
         3050  +#ifdef SQLITE_ENABLE_STAT3
  3039   3051         if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
  3040   3052   #endif
  3041   3053         used |= pTerm->prereqRight;
  3042   3054       }
  3043   3055   
  3044         -    /* Determine the value of estBound. */
         3056  +    /* Determine the value of rangeDiv */
  3045   3057       if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
  3046   3058         int j = pProbe->aiColumn[nEq];
  3047   3059         if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
  3048   3060           WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
  3049   3061           WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
  3050         -        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
         3062  +        whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
  3051   3063           if( pTop ){
  3052   3064             nBound = 1;
  3053   3065             wsFlags |= WHERE_TOP_LIMIT;
  3054   3066             used |= pTop->prereqRight;
         3067  +          testcase( pTop->pWC!=pWC );
  3055   3068           }
  3056   3069           if( pBtm ){
  3057   3070             nBound++;
  3058   3071             wsFlags |= WHERE_BTM_LIMIT;
  3059   3072             used |= pBtm->prereqRight;
         3073  +          testcase( pBtm->pWC!=pWC );
  3060   3074           }
  3061   3075           wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
  3062   3076         }
  3063   3077       }else if( pProbe->onError!=OE_None ){
  3064   3078         testcase( wsFlags & WHERE_COLUMN_IN );
  3065   3079         testcase( wsFlags & WHERE_COLUMN_NULL );
  3066   3080         if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
................................................................................
  3115   3129       */
  3116   3130       nRow = (double)(aiRowEst[nEq] * nInMul);
  3117   3131       if( bInEst && nRow*2>aiRowEst[0] ){
  3118   3132         nRow = aiRowEst[0]/2;
  3119   3133         nInMul = (int)(nRow / aiRowEst[nEq]);
  3120   3134       }
  3121   3135   
  3122         -#ifdef SQLITE_ENABLE_STAT2
         3136  +#ifdef SQLITE_ENABLE_STAT3
  3123   3137       /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
  3124   3138       ** and we do not think that values of x are unique and if histogram
  3125   3139       ** data is available for column x, then it might be possible
  3126   3140       ** to get a better estimate on the number of rows based on
  3127   3141       ** VALUE and how common that value is according to the histogram.
  3128   3142       */
  3129   3143       if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
         3144  +      assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
  3130   3145         if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
  3131   3146           testcase( pFirstTerm->eOperator==WO_EQ );
  3132   3147           testcase( pFirstTerm->eOperator==WO_ISNULL );
  3133   3148           whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
  3134         -      }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
         3149  +      }else if( bInEst==0 ){
         3150  +        assert( pFirstTerm->eOperator==WO_IN );
  3135   3151           whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
  3136   3152         }
  3137   3153       }
  3138         -#endif /* SQLITE_ENABLE_STAT2 */
         3154  +#endif /* SQLITE_ENABLE_STAT3 */
  3139   3155   
  3140   3156       /* Adjust the number of output rows and downward to reflect rows
  3141   3157       ** that are excluded by range constraints.
  3142   3158       */
  3143         -    nRow = (nRow * (double)estBound) / (double)100;
         3159  +    nRow = nRow/rangeDiv;
  3144   3160       if( nRow<1 ) nRow = 1;
  3145   3161   
  3146   3162       /* Experiments run on real SQLite databases show that the time needed
  3147   3163       ** to do a binary search to locate a row in a table or index is roughly
  3148   3164       ** log10(N) times the time to move from one row to the next row within
  3149   3165       ** a table or index.  The actual times can vary, with the size of
  3150   3166       ** records being an important factor.  Both moves and searches are
................................................................................
  3265   3281           }
  3266   3282         }
  3267   3283         if( nRow<2 ) nRow = 2;
  3268   3284       }
  3269   3285   
  3270   3286   
  3271   3287       WHERETRACE((
  3272         -      "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
         3288  +      "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
  3273   3289         "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
  3274   3290         pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
  3275         -      nEq, nInMul, estBound, bSort, bLookup, wsFlags,
         3291  +      nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
  3276   3292         notReady, log10N, nRow, cost, used
  3277   3293       ));
  3278   3294   
  3279   3295       /* If this index is the best we have seen so far, then record this
  3280   3296       ** index and its cost in the pCost structure.
  3281   3297       */
  3282   3298       if( (!pIdx || wsFlags)
................................................................................
  3772   3788   ** Generate code for the start of the iLevel-th loop in the WHERE clause
  3773   3789   ** implementation described by pWInfo.
  3774   3790   */
  3775   3791   static Bitmask codeOneLoopStart(
  3776   3792     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  3777   3793     int iLevel,          /* Which level of pWInfo->a[] should be coded */
  3778   3794     u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
  3779         -  Bitmask notReady     /* Which tables are currently available */
         3795  +  Bitmask notReady,    /* Which tables are currently available */
         3796  +  Expr *pWhere         /* Complete WHERE clause */
  3780   3797   ){
  3781   3798     int j, k;            /* Loop counters */
  3782   3799     int iCur;            /* The VDBE cursor for the table */
  3783   3800     int addrNxt;         /* Where to jump to continue with the next IN case */
  3784   3801     int omitTable;       /* True if we use the index only */
  3785   3802     int bRev;            /* True if we need to scan in reverse order */
  3786   3803     WhereLevel *pLevel;  /* The where level to be coded */
................................................................................
  4254   4271   
  4255   4272       int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  4256   4273       int regRowset = 0;                        /* Register for RowSet object */
  4257   4274       int regRowid = 0;                         /* Register holding rowid */
  4258   4275       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  4259   4276       int iRetInit;                             /* Address of regReturn init */
  4260   4277       int untestedTerms = 0;             /* Some terms not completely tested */
  4261         -    int ii;
         4278  +    int ii;                            /* Loop counter */
         4279  +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  4262   4280      
  4263   4281       pTerm = pLevel->plan.u.pTerm;
  4264   4282       assert( pTerm!=0 );
  4265   4283       assert( pTerm->eOperator==WO_OR );
  4266   4284       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  4267   4285       pOrWc = &pTerm->u.pOrInfo->wc;
  4268   4286       pLevel->op = OP_Return;
................................................................................
  4303   4321       */
  4304   4322       if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4305   4323         regRowset = ++pParse->nMem;
  4306   4324         regRowid = ++pParse->nMem;
  4307   4325         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  4308   4326       }
  4309   4327       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
         4328  +
         4329  +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
         4330  +    ** Then for every term xN, evaluate as the subexpression: xN AND z
         4331  +    ** That way, terms in y that are factored into the disjunction will
         4332  +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
         4333  +    */
         4334  +    if( pWC->nTerm>1 ){
         4335  +      pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
         4336  +      pAndExpr->pRight = pWhere;
         4337  +    }
  4310   4338   
  4311   4339       for(ii=0; ii<pOrWc->nTerm; ii++){
  4312   4340         WhereTerm *pOrTerm = &pOrWc->a[ii];
  4313   4341         if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
  4314   4342           WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
         4343  +        Expr *pOrExpr = pOrTerm->pExpr;
         4344  +        if( pAndExpr ){
         4345  +          pAndExpr->pLeft = pOrExpr;
         4346  +          pOrExpr = pAndExpr;
         4347  +        }
  4315   4348           /* Loop through table entries that match term pOrTerm. */
  4316         -        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0,
  4317         -                        WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
         4349  +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
         4350  +                        WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  4318   4351                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
  4319   4352           if( pSubWInfo ){
  4320   4353             explainOneScan(
  4321   4354                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  4322   4355             );
  4323   4356             if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4324   4357               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
................................................................................
  4338   4371             if( pSubWInfo->untestedTerms ) untestedTerms = 1;
  4339   4372   
  4340   4373             /* Finish the loop through table entries that match term pOrTerm. */
  4341   4374             sqlite3WhereEnd(pSubWInfo);
  4342   4375           }
  4343   4376         }
  4344   4377       }
         4378  +    sqlite3DbFree(pParse->db, pAndExpr);
  4345   4379       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  4346   4380       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  4347   4381       sqlite3VdbeResolveLabel(v, iLoopBody);
  4348   4382   
  4349   4383       if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
  4350   4384       if( !untestedTerms ) disableTerm(pLevel, pTerm);
  4351   4385     }else
................................................................................
  4619   4653     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
  4620   4654     if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
  4621   4655   
  4622   4656     /* Split the WHERE clause into separate subexpressions where each
  4623   4657     ** subexpression is separated by an AND operator.
  4624   4658     */
  4625   4659     initMaskSet(pMaskSet);
  4626         -  whereClauseInit(pWC, pParse, pMaskSet);
         4660  +  whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
  4627   4661     sqlite3ExprCodeConstants(pParse, pWhere);
  4628   4662     whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  4629   4663       
  4630   4664     /* Special case: a WHERE clause that is constant.  Evaluate the
  4631   4665     ** expression and either jump over all of the code or fall thru.
  4632   4666     */
  4633   4667     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
................................................................................
  4947   4981       if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  4948   4982         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
  4949   4983         int iCur = pTabItem->iCursor;
  4950   4984         sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
  4951   4985       }else
  4952   4986   #endif
  4953   4987       if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  4954         -         && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
         4988  +         && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
  4955   4989         int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
  4956   4990         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
  4957   4991         testcase( pTab->nCol==BMS-1 );
  4958   4992         testcase( pTab->nCol==BMS );
  4959   4993         if( !pWInfo->okOnePass && pTab->nCol<BMS ){
  4960   4994           Bitmask b = pTabItem->colUsed;
  4961   4995           int n = 0;
................................................................................
  4992   5026     ** loop below generates code for a single nested loop of the VM
  4993   5027     ** program.
  4994   5028     */
  4995   5029     notReady = ~(Bitmask)0;
  4996   5030     for(i=0; i<nTabList; i++){
  4997   5031       pLevel = &pWInfo->a[i];
  4998   5032       explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
  4999         -    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
         5033  +    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
  5000   5034       pWInfo->iContinue = pLevel->addrCont;
  5001   5035     }
  5002   5036   
  5003   5037   #ifdef SQLITE_TEST  /* For testing and debugging use only */
  5004   5038     /* Record in the query plan information about the current table
  5005   5039     ** and the index used to access it (if any).  If the table itself
  5006   5040     ** is not used, its name is just '{}'.  If no index is used
................................................................................
  5127   5161     assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
  5128   5162     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
  5129   5163       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  5130   5164       Table *pTab = pTabItem->pTab;
  5131   5165       assert( pTab!=0 );
  5132   5166       if( (pTab->tabFlags & TF_Ephemeral)==0
  5133   5167        && pTab->pSelect==0
  5134         -     && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
         5168  +     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
  5135   5169       ){
  5136   5170         int ws = pLevel->plan.wsFlags;
  5137   5171         if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
  5138   5172           sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
  5139   5173         }
  5140   5174         if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
  5141   5175           sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);

Changes to test/alter.test.

   843    843   #-------------------------------------------------------------------------
   844    844   # Test that it is not possible to use ALTER TABLE on any system table.
   845    845   #
   846    846   set system_table_list {1 sqlite_master}
   847    847   catchsql ANALYZE
   848    848   ifcapable analyze { lappend system_table_list 2 sqlite_stat1 }
   849    849   ifcapable stat2   { lappend system_table_list 3 sqlite_stat2 }
          850  +ifcapable stat3   { lappend system_table_list 4 sqlite_stat3 }
   850    851   
   851    852   foreach {tn tbl} $system_table_list {
   852    853     do_test alter-15.$tn.1 {
   853    854       catchsql "ALTER TABLE $tbl RENAME TO xyz"
   854    855     } [list 1 "table $tbl may not be altered"]
   855    856   
   856    857     do_test alter-15.$tn.2 {
   857    858       catchsql "ALTER TABLE $tbl ADD COLUMN xyz"
   858    859     } [list 1 "table $tbl may not be altered"]
   859    860   }
   860    861   
   861    862   
   862    863   finish_test

Changes to test/analyze.test.

   284    284     sqlite3 db test.db
   285    285     execsql {
   286    286       SELECT * FROM t4 WHERE x=1234;
   287    287     }
   288    288   } {}
   289    289   
   290    290   # Verify that DROP TABLE and DROP INDEX remove entries from the 
   291         -# sqlite_stat1 and sqlite_stat2 tables.
          291  +# sqlite_stat1 and sqlite_stat3 tables.
   292    292   #
   293    293   do_test analyze-5.0 {
   294    294     execsql {
   295    295       DELETE FROM t3;
   296    296       DELETE FROM t4;
   297    297       INSERT INTO t3 VALUES(1,2,3,4);
   298    298       INSERT INTO t3 VALUES(5,6,7,8);
................................................................................
   302    302       INSERT INTO t3 SELECT a+64, b+64, c+64, d+64 FROM t3;
   303    303       INSERT INTO t4 SELECT a, b, c FROM t3;
   304    304       ANALYZE;
   305    305       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   306    306       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   307    307     }
   308    308   } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
   309         -ifcapable stat2 {
          309  +ifcapable stat3 {
   310    310     do_test analyze-5.1 {
   311    311       execsql {
   312         -      SELECT DISTINCT idx FROM sqlite_stat2 ORDER BY 1;
   313         -      SELECT DISTINCT tbl FROM sqlite_stat2 ORDER BY 1;
          312  +      SELECT DISTINCT idx FROM sqlite_stat3 ORDER BY 1;
          313  +      SELECT DISTINCT tbl FROM sqlite_stat3 ORDER BY 1;
   314    314       }
   315    315     } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
   316    316   }
   317    317   do_test analyze-5.2 {
   318    318     execsql {
   319    319       DROP INDEX t3i2;
   320    320       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   321    321       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   322    322     }
   323    323   } {t3i1 t3i3 t4i1 t4i2 t3 t4}
   324         -ifcapable stat2 {
          324  +ifcapable stat3 {
   325    325     do_test analyze-5.3 {
   326    326       execsql {
   327         -      SELECT DISTINCT idx FROM sqlite_stat2 ORDER BY 1;
   328         -      SELECT DISTINCT tbl FROM sqlite_stat2 ORDER BY 1;
          327  +      SELECT DISTINCT idx FROM sqlite_stat3 ORDER BY 1;
          328  +      SELECT DISTINCT tbl FROM sqlite_stat3 ORDER BY 1;
   329    329       }
   330    330     } {t3i1 t3i3 t4i1 t4i2 t3 t4}
   331    331   }
   332    332   do_test analyze-5.4 {
   333    333     execsql {
   334    334       DROP TABLE t3;
   335    335       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   336    336       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   337    337     }
   338    338   } {t4i1 t4i2 t4}
   339         -ifcapable stat2 {
          339  +ifcapable stat3 {
   340    340     do_test analyze-5.5 {
   341    341       execsql {
   342         -      SELECT DISTINCT idx FROM sqlite_stat2 ORDER BY 1;
   343         -      SELECT DISTINCT tbl FROM sqlite_stat2 ORDER BY 1;
          342  +      SELECT DISTINCT idx FROM sqlite_stat3 ORDER BY 1;
          343  +      SELECT DISTINCT tbl FROM sqlite_stat3 ORDER BY 1;
   344    344       }
   345    345     } {t4i1 t4i2 t4}
   346    346   }
   347    347   
   348    348   # This test corrupts the database file so it must be the last test
   349    349   # in the series.
   350    350   #

Changes to test/analyze3.test.

    13     13   # implements tests for range and LIKE constraints that use bound variables
    14     14   # instead of literal constant arguments.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable !stat2 {
           20  +ifcapable !stat3 {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   #----------------------------------------------------------------------
    26     26   # Test Organization:
    27     27   #
................................................................................
    93     93       COMMIT;
    94     94       ANALYZE;
    95     95     }
    96     96   } {}
    97     97   
    98     98   do_eqp_test analyze3-1.1.2 {
    99     99     SELECT sum(y) FROM t1 WHERE x>200 AND x<300
   100         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~100 rows)}}
          100  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~179 rows)}}
   101    101   do_eqp_test analyze3-1.1.3 {
   102    102     SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 
   103         -} {0 0 0 {SCAN TABLE t1 (~111 rows)}}
          103  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~959 rows)}}
   104    104   
   105    105   do_test analyze3-1.1.4 {
   106    106     sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
   107    107   } {199 0 14850}
   108    108   do_test analyze3-1.1.5 {
   109    109     set l [string range "200" 0 end]
   110    110     set u [string range "300" 0 end]
................................................................................
   113    113   do_test analyze3-1.1.6 {
   114    114     set l [expr int(200)]
   115    115     set u [expr int(300)]
   116    116     sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
   117    117   } {199 0 14850}
   118    118   do_test analyze3-1.1.7 {
   119    119     sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
   120         -} {999 999 499500}
          120  +} {2000 0 499500}
   121    121   do_test analyze3-1.1.8 {
   122    122     set l [string range "0" 0 end]
   123    123     set u [string range "1100" 0 end]
   124    124     sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
   125         -} {999 999 499500}
          125  +} {2000 0 499500}
   126    126   do_test analyze3-1.1.9 {
   127    127     set l [expr int(0)]
   128    128     set u [expr int(1100)]
   129    129     sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
   130         -} {999 999 499500}
          130  +} {2000 0 499500}
   131    131   
   132    132   
   133    133   # The following tests are similar to the block above. The difference is
   134    134   # that the indexed column has TEXT affinity in this case. In the tests
   135    135   # above the affinity is INTEGER.
   136    136   #
   137    137   do_test analyze3-1.2.1 {
................................................................................
   142    142         CREATE INDEX i2 ON t2(x);
   143    143       COMMIT;
   144    144       ANALYZE;
   145    145     }
   146    146   } {}
   147    147   do_eqp_test analyze3-1.2.2 {
   148    148     SELECT sum(y) FROM t2 WHERE x>1 AND x<2
   149         -} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~200 rows)}}
          149  +} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~196 rows)}}
   150    150   do_eqp_test analyze3-1.2.3 {
   151    151     SELECT sum(y) FROM t2 WHERE x>0 AND x<99
   152         -} {0 0 0 {SCAN TABLE t2 (~111 rows)}}
          152  +} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~968 rows)}}
   153    153   do_test analyze3-1.2.4 {
   154    154     sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
   155    155   } {161 0 4760}
   156    156   do_test analyze3-1.2.5 {
   157    157     set l [string range "12" 0 end]
   158    158     set u [string range "20" 0 end]
   159    159     sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
................................................................................
   161    161   do_test analyze3-1.2.6 {
   162    162     set l [expr int(12)]
   163    163     set u [expr int(20)]
   164    164     sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
   165    165   } {161 0 integer integer 4760}
   166    166   do_test analyze3-1.2.7 {
   167    167     sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
   168         -} {999 999 490555}
          168  +} {1981 0 490555}
   169    169   do_test analyze3-1.2.8 {
   170    170     set l [string range "0" 0 end]
   171    171     set u [string range "99" 0 end]
   172    172     sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
   173         -} {999 999 text text 490555}
          173  +} {1981 0 text text 490555}
   174    174   do_test analyze3-1.2.9 {
   175    175     set l [expr int(0)]
   176    176     set u [expr int(99)]
   177    177     sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
   178         -} {999 999 integer integer 490555}
          178  +} {1981 0 integer integer 490555}
   179    179   
   180    180   # Same tests a third time. This time, column x has INTEGER affinity and
   181    181   # is not the leftmost column of the table. This triggered a bug causing
   182    182   # SQLite to use sub-optimal query plans in 3.6.18 and earlier.
   183    183   #
   184    184   do_test analyze3-1.3.1 {
   185    185     execsql {
................................................................................
   189    189         CREATE INDEX i3 ON t3(x);
   190    190       COMMIT;
   191    191       ANALYZE;
   192    192     }
   193    193   } {}
   194    194   do_eqp_test analyze3-1.3.2 {
   195    195     SELECT sum(y) FROM t3 WHERE x>200 AND x<300
   196         -} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~100 rows)}}
          196  +} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~156 rows)}}
   197    197   do_eqp_test analyze3-1.3.3 {
   198    198     SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
   199         -} {0 0 0 {SCAN TABLE t3 (~111 rows)}}
          199  +} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~989 rows)}}
   200    200   
   201    201   do_test analyze3-1.3.4 {
   202    202     sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
   203    203   } {199 0 14850}
   204    204   do_test analyze3-1.3.5 {
   205    205     set l [string range "200" 0 end]
   206    206     set u [string range "300" 0 end]
................................................................................
   209    209   do_test analyze3-1.3.6 {
   210    210     set l [expr int(200)]
   211    211     set u [expr int(300)]
   212    212     sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
   213    213   } {199 0 14850}
   214    214   do_test analyze3-1.3.7 {
   215    215     sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
   216         -} {999 999 499500}
          216  +} {2000 0 499500}
   217    217   do_test analyze3-1.3.8 {
   218    218     set l [string range "0" 0 end]
   219    219     set u [string range "1100" 0 end]
   220    220     sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
   221         -} {999 999 499500}
          221  +} {2000 0 499500}
   222    222   do_test analyze3-1.3.9 {
   223    223     set l [expr int(0)]
   224    224     set u [expr int(1100)]
   225    225     sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
   226         -} {999 999 499500}
          226  +} {2000 0 499500}
   227    227   
   228    228   #-------------------------------------------------------------------------
   229    229   # Test that the values of bound SQL variables may be used for the LIKE
   230    230   # optimization.
   231    231   #
   232    232   drop_all_tables
   233    233   do_test analyze3-2.1 {
................................................................................
   244    244       append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
   245    245       execsql { INSERT INTO t1 VALUES($i, $t) }
   246    246     }
   247    247     execsql COMMIT
   248    248   } {}
   249    249   do_eqp_test analyze3-2.2 {
   250    250     SELECT count(a) FROM t1 WHERE b LIKE 'a%'
   251         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?) (~30000 rows)}}
          251  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?) (~31250 rows)}}
   252    252   do_eqp_test analyze3-2.3 {
   253    253     SELECT count(a) FROM t1 WHERE b LIKE '%a'
   254    254   } {0 0 0 {SCAN TABLE t1 (~500000 rows)}}
   255    255   
   256    256   do_test analyze3-2.4 {
   257    257     sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
   258    258   } {101 0 100}

Changes to test/analyze5.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # This file implements tests for SQLite library.  The focus of the tests
    13         -# in this file is the use of the sqlite_stat2 histogram data on tables
           13  +# in this file is the use of the sqlite_stat3 histogram data on tables
    14     14   # with many repeated values and only a few distinct values.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable !stat2 {
           20  +ifcapable !stat3 {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   set testprefix analyze5
    26     26   
    27     27   proc eqp {sql {db db}} {
................................................................................
    51     51       CREATE INDEX t1u ON t1(u);  -- text
    52     52       CREATE INDEX t1v ON t1(v);  -- mixed case text
    53     53       CREATE INDEX t1w ON t1(w);  -- integers 0, 1, 2 and a few NULLs
    54     54       CREATE INDEX t1x ON t1(x);  -- integers 1, 2, 3 and many NULLs
    55     55       CREATE INDEX t1y ON t1(y);  -- integers 0 and very few 1s
    56     56       CREATE INDEX t1z ON t1(z);  -- integers 0, 1, 2, and 3
    57     57       ANALYZE;
    58         -    SELECT sample FROM sqlite_stat2 WHERE idx='t1u' ORDER BY sampleno;
           58  +    SELECT sample FROM sqlite_stat3 WHERE idx='t1u' ORDER BY nlt;
    59     59     }
    60         -} {alpha alpha alpha alpha bravo bravo bravo charlie charlie delta}
           60  +} {alpha bravo charlie delta}
           61  +
    61     62   do_test analyze5-1.1 {
    62         -  string tolower \
    63         -   [db eval {SELECT sample from sqlite_stat2 WHERE idx='t1v' ORDER BY sampleno}]
    64         -} {alpha alpha alpha alpha bravo bravo bravo charlie charlie delta}
           63  +  db eval {SELECT DISTINCT lower(sample) FROM sqlite_stat3 WHERE idx='t1v'
           64  +             ORDER BY 1}
           65  +} {alpha bravo charlie delta}
    65     66   do_test analyze5-1.2 {
    66         -  db eval {SELECT sample from sqlite_stat2 WHERE idx='t1w' ORDER BY sampleno}
    67         -} {{} 0 0 0 0 1 1 1 2 2}
    68         -do_test analyze5-1.3 {
    69         -  db eval {SELECT sample from sqlite_stat2 WHERE idx='t1x' ORDER BY sampleno}
    70         -} {{} {} {} {} 1 1 1 2 2 3}
    71         -do_test analyze5-1.4 {
    72         -  db eval {SELECT sample from sqlite_stat2 WHERE idx='t1y' ORDER BY sampleno}
    73         -} {0 0 0 0 0 0 0 0 0 0}
    74         -do_test analyze5-1.5 {
    75         -  db eval {SELECT sample from sqlite_stat2 WHERE idx='t1z' ORDER BY sampleno}
    76         -} {0 0 0 0 1 1 1 2 2 3}
    77         -do_test analyze5-1.6 {
    78         -  db eval {SELECT sample from sqlite_stat2 WHERE idx='t1t' ORDER BY sampleno}
    79         -} {0.5 0.5 0.5 0.5 1.5 1.5 1.5 2.5 2.5 3.5}
    80         -
           67  +  db eval {SELECT idx, count(*) FROM sqlite_stat3 GROUP BY 1 ORDER BY 1}
           68  +} {t1t 4 t1u 4 t1v 4 t1w 4 t1x 4 t1y 2 t1z 4}
    81     69   
    82     70   # Verify that range queries generate the correct row count estimates
    83     71   #
    84     72   foreach {testid where index rows} {
    85     73       1  {z>=0 AND z<=0}       t1z  400
    86     74       2  {z>=1 AND z<=1}       t1z  300
    87         -    3  {z>=2 AND z<=2}       t1z  200
    88         -    4  {z>=3 AND z<=3}       t1z  100
    89         -    5  {z>=4 AND z<=4}       t1z   50
    90         -    6  {z>=-1 AND z<=-1}     t1z   50
    91         -    7  {z>1 AND z<3}         t1z  200
           75  +    3  {z>=2 AND z<=2}       t1z  175
           76  +    4  {z>=3 AND z<=3}       t1z  125
           77  +    5  {z>=4 AND z<=4}       t1z    1
           78  +    6  {z>=-1 AND z<=-1}     t1z    1
           79  +    7  {z>1 AND z<3}         t1z  175
    92     80       8  {z>0 AND z<100}       t1z  600
    93     81       9  {z>=1 AND z<100}      t1z  600
    94     82      10  {z>1 AND z<100}       t1z  300
    95     83      11  {z>=2 AND z<100}      t1z  300
    96         -   12  {z>2 AND z<100}       t1z  100
    97         -   13  {z>=3 AND z<100}      t1z  100
    98         -   14  {z>3 AND z<100}       t1z   50
    99         -   15  {z>=4 AND z<100}      t1z   50
   100         -   16  {z>=-100 AND z<=-1}   t1z   50
           84  +   12  {z>2 AND z<100}       t1z  125
           85  +   13  {z>=3 AND z<100}      t1z  125
           86  +   14  {z>3 AND z<100}       t1z    1
           87  +   15  {z>=4 AND z<100}      t1z    1
           88  +   16  {z>=-100 AND z<=-1}   t1z    1
   101     89      17  {z>=-100 AND z<=0}    t1z  400
   102         -   18  {z>=-100 AND z<0}     t1z   50
           90  +   18  {z>=-100 AND z<0}     t1z    1
   103     91      19  {z>=-100 AND z<=1}    t1z  700
   104     92      20  {z>=-100 AND z<2}     t1z  700
   105         -   21  {z>=-100 AND z<=2}    t1z  900
   106         -   22  {z>=-100 AND z<3}     t1z  900
           93  +   21  {z>=-100 AND z<=2}    t1z  875
           94  +   22  {z>=-100 AND z<3}     t1z  875
   107     95     
   108     96      31  {z>=0.0 AND z<=0.0}   t1z  400
   109     97      32  {z>=1.0 AND z<=1.0}   t1z  300
   110         -   33  {z>=2.0 AND z<=2.0}   t1z  200
   111         -   34  {z>=3.0 AND z<=3.0}   t1z  100
   112         -   35  {z>=4.0 AND z<=4.0}   t1z   50
   113         -   36  {z>=-1.0 AND z<=-1.0} t1z   50
   114         -   37  {z>1.5 AND z<3.0}     t1z  200
   115         -   38  {z>0.5 AND z<100}     t1z  600
           98  +   33  {z>=2.0 AND z<=2.0}   t1z  175
           99  +   34  {z>=3.0 AND z<=3.0}   t1z  125
          100  +   35  {z>=4.0 AND z<=4.0}   t1z    1
          101  +   36  {z>=-1.0 AND z<=-1.0} t1z    1
          102  +   37  {z>1.5 AND z<3.0}     t1z  174
          103  +   38  {z>0.5 AND z<100}     t1z  599
   116    104      39  {z>=1.0 AND z<100}    t1z  600
   117         -   40  {z>1.5 AND z<100}     t1z  300
          105  +   40  {z>1.5 AND z<100}     t1z  299
   118    106      41  {z>=2.0 AND z<100}    t1z  300
   119         -   42  {z>2.1 AND z<100}     t1z  100
   120         -   43  {z>=3.0 AND z<100}    t1z  100
   121         -   44  {z>3.2 AND z<100}     t1z   50
   122         -   45  {z>=4.0 AND z<100}    t1z   50
   123         -   46  {z>=-100 AND z<=-1.0} t1z   50
          107  +   42  {z>2.1 AND z<100}     t1z  124
          108  +   43  {z>=3.0 AND z<100}    t1z  125
          109  +   44  {z>3.2 AND z<100}     t1z    1
          110  +   45  {z>=4.0 AND z<100}    t1z    1
          111  +   46  {z>=-100 AND z<=-1.0} t1z    1
   124    112      47  {z>=-100 AND z<=0.0}  t1z  400
   125         -   48  {z>=-100 AND z<0.0}   t1z   50
          113  +   48  {z>=-100 AND z<0.0}   t1z    1
   126    114      49  {z>=-100 AND z<=1.0}  t1z  700
   127    115      50  {z>=-100 AND z<2.0}   t1z  700
   128         -   51  {z>=-100 AND z<=2.0}  t1z  900
   129         -   52  {z>=-100 AND z<3.0}   t1z  900
          116  +   51  {z>=-100 AND z<=2.0}  t1z  875
          117  +   52  {z>=-100 AND z<3.0}   t1z  875
   130    118     
   131         -  101  {z=-1}                t1z   50
          119  +  101  {z=-1}                t1z    1
   132    120     102  {z=0}                 t1z  400
   133    121     103  {z=1}                 t1z  300
   134         -  104  {z=2}                 t1z  200
   135         -  105  {z=3}                 t1z  100
   136         -  106  {z=4}                 t1z   50
   137         -  107  {z=-10.0}             t1z   50
          122  +  104  {z=2}                 t1z  175
          123  +  105  {z=3}                 t1z  125
          124  +  106  {z=4}                 t1z    1
          125  +  107  {z=-10.0}             t1z    1
   138    126     108  {z=0.0}               t1z  400
   139    127     109  {z=1.0}               t1z  300
   140         -  110  {z=2.0}               t1z  200
   141         -  111  {z=3.0}               t1z  100
   142         -  112  {z=4.0}               t1z   50
   143         -  113  {z=1.5}               t1z   50
   144         -  114  {z=2.5}               t1z   50
          128  +  110  {z=2.0}               t1z  175
          129  +  111  {z=3.0}               t1z  125
          130  +  112  {z=4.0}               t1z    1
          131  +  113  {z=1.5}               t1z    1
          132  +  114  {z=2.5}               t1z    1
   145    133     
   146         -  201  {z IN (-1)}           t1z   50
          134  +  201  {z IN (-1)}           t1z    1
   147    135     202  {z IN (0)}            t1z  400
   148    136     203  {z IN (1)}            t1z  300
   149         -  204  {z IN (2)}            t1z  200
   150         -  205  {z IN (3)}            t1z  100
   151         -  206  {z IN (4)}            t1z   50
   152         -  207  {z IN (0.5)}          t1z   50
          137  +  204  {z IN (2)}            t1z  175
          138  +  205  {z IN (3)}            t1z  125
          139  +  206  {z IN (4)}            t1z    1
          140  +  207  {z IN (0.5)}          t1z    1
   153    141     208  {z IN (0,1)}          t1z  700
   154         -  209  {z IN (0,1,2)}        t1z  900
          142  +  209  {z IN (0,1,2)}        t1z  875
   155    143     210  {z IN (0,1,2,3)}      {}   100
   156    144     211  {z IN (0,1,2,3,4,5)}  {}   100
   157         -  212  {z IN (1,2)}          t1z  500
          145  +  212  {z IN (1,2)}          t1z  475
   158    146     213  {z IN (2,3)}          t1z  300
   159    147     214  {z=3 OR z=2}          t1z  300
   160         -  215  {z IN (-1,3)}         t1z  150
   161         -  216  {z=-1 OR z=3}         t1z  150
          148  +  215  {z IN (-1,3)}         t1z  126
          149  +  216  {z=-1 OR z=3}         t1z  126
   162    150   
   163         -  300  {y=0}                 {}   100
   164         -  301  {y=1}                 t1y   50
   165         -  302  {y=0.1}               t1y   50
          151  +  300  {y=0}                 t1y  974
          152  +  301  {y=1}                 t1y   26
          153  +  302  {y=0.1}               t1y    1
   166    154   
   167    155     400  {x IS NULL}           t1x  400
   168    156   
   169    157   } {
   170    158     # Verify that the expected index is used with the expected row count
   171    159     do_test analyze5-1.${testid}a {
   172    160       set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
................................................................................
   200    188       WHERE rowid IN (SELECT rowid FROM t1 ORDER BY random() LIMIT 5);
   201    189      ANALYZE;
   202    190   }
   203    191   
   204    192   # Verify that range queries generate the correct row count estimates
   205    193   #
   206    194   foreach {testid where index rows} {
   207         -  500  {x IS NULL AND u='charlie'}         t1u  20
   208         -  501  {x=1 AND u='charlie'}               t1x   5
   209         -  502  {x IS NULL}                          {} 100
   210         -  503  {x=1}                               t1x  50
   211         -  504  {x IS NOT NULL}                     t1x  25
          195  +  500  {x IS NULL AND u='charlie'}         t1u  17
          196  +  501  {x=1 AND u='charlie'}               t1x   1
          197  +  502  {x IS NULL}                         t1x 995
          198  +  503  {x=1}                               t1x   1
          199  +  504  {x IS NOT NULL}                     t1x   2
   212    200     505  {+x IS NOT NULL}                     {} 500
   213    201     506  {upper(x) IS NOT NULL}               {} 500
   214    202   
   215    203   } {
   216    204     # Verify that the expected index is used with the expected row count
          205  +if {$testid==50299} {breakpoint; set sqlite_where_trace 1}
   217    206     do_test analyze5-1.${testid}a {
   218    207       set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
   219    208       set idx {}
   220    209       regexp {INDEX (t1.) } $x all idx
   221    210       regexp {~([0-9]+) rows} $x all nrow
   222    211       list $idx $nrow
   223    212     } [list $index $rows]
          213  +if {$testid==50299} exit
   224    214   
   225    215     # Verify that the same result is achieved regardless of whether or not
   226    216     # the index is used
   227    217     do_test analyze5-1.${testid}b {
   228    218       set w2 [string map {y +y z +z} $where]
   229    219       set a1 [db eval "SELECT rowid FROM t1 NOT INDEXED WHERE $w2\
   230    220                        ORDER BY +rowid"]

Changes to test/analyze6.test.

    13     13   # in this file a corner-case query planner optimization involving the
    14     14   # join order of two tables of different sizes.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable !stat2 {
           20  +ifcapable !stat3 {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   set testprefix analyze6
    26     26   
    27     27   proc eqp {sql {db db}} {

Changes to test/analyze7.test.

    78     78   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
    79     79   do_test analyze7-3.1 {
    80     80     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
    81     81   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
    82     82   do_test analyze7-3.2.1 {
    83     83     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
    84     84   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
    85         -ifcapable stat2 {
    86         -  # If ENABLE_STAT2 is defined, SQLite comes up with a different estimated
           85  +ifcapable stat3 {
           86  +  # If ENABLE_STAT3 is defined, SQLite comes up with a different estimated
    87     87     # row count for (c=2) than it does for (c=?).
    88     88     do_test analyze7-3.2.2 {
    89     89       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    90         -  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~51 rows)}}
           90  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~57 rows)}}
    91     91   } else {
    92         -  # If ENABLE_STAT2 is not defined, the expected row count for (c=2) is the
           92  +  # If ENABLE_STAT3 is not defined, the expected row count for (c=2) is the
    93     93     # same as that for (c=?).
    94     94     do_test analyze7-3.2.3 {
    95     95       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    96     96     } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
    97     97   }
    98     98   do_test analyze7-3.3 {
    99     99     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
   100    100   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
   101         -do_test analyze7-3.4 {
   102         -  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
   103         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
   104         -do_test analyze7-3.5 {
   105         -  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
   106         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
          101  +ifcapable {!stat3} {
          102  +  do_test analyze7-3.4 {
          103  +    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
          104  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
          105  +  do_test analyze7-3.5 {
          106  +    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
          107  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
          108  +}
   107    109   do_test analyze7-3.6 {
   108    110     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123}
   109    111   } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?) (~1 rows)}}
   110    112   
   111    113   finish_test

Added test/analyze8.test.

            1  +# 2011 August 13
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file implements tests for SQLite library.  The focus of the tests
           13  +# in this file is testing the capabilities of sqlite_stat3.
           14  +#
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +
           19  +ifcapable !stat3 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +set testprefix analyze8
           25  +
           26  +proc eqp {sql {db db}} {
           27  +  uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
           28  +}
           29  +
           30  +# Scenario:
           31  +#
           32  +#    Two indices.  One has mostly singleton entries, but for a few
           33  +#    values there are hundreds of entries.  The other has 10-20
           34  +#    entries per value.
           35  +#
           36  +# Verify that the query planner chooses the first index for the singleton
           37  +# entries and the second index for the others.
           38  +#
           39  +do_test 1.0 {
           40  +  db eval {
           41  +    CREATE TABLE t1(a,b,c,d);
           42  +    CREATE INDEX t1a ON t1(a);
           43  +    CREATE INDEX t1b ON t1(b);
           44  +    CREATE INDEX t1c ON t1(c);
           45  +  }
           46  +  for {set i 0} {$i<1000} {incr i} {
           47  +    if {$i%2==0} {set a $i} {set a [expr {($i%8)*100}]}
           48  +    set b [expr {$i/10}]
           49  +    set c [expr {$i/8}]
           50  +    set c [expr {$c*$c*$c}]
           51  +    db eval {INSERT INTO t1 VALUES($a,$b,$c,$i)}
           52  +  }
           53  +  db eval {ANALYZE}
           54  +} {}
           55  +
           56  +# The a==100 comparison is expensive because there are many rows
           57  +# with a==100.  And so for those cases, choose the t1b index.
           58  +#
           59  +# Buf ro a==99 and a==101, there are far fewer rows so choose
           60  +# the t1a index.
           61  +#
           62  +do_test 1.1 {
           63  +  eqp {SELECT * FROM t1 WHERE a=100 AND b=55}
           64  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
           65  +do_test 1.2 {
           66  +  eqp {SELECT * FROM t1 WHERE a=99 AND b=55}
           67  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           68  +do_test 1.3 {
           69  +  eqp {SELECT * FROM t1 WHERE a=101 AND b=55}
           70  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           71  +do_test 1.4 {
           72  +  eqp {SELECT * FROM t1 WHERE a=100 AND b=56}
           73  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
           74  +do_test 1.5 {
           75  +  eqp {SELECT * FROM t1 WHERE a=99 AND b=56}
           76  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           77  +do_test 1.6 {
           78  +  eqp {SELECT * FROM t1 WHERE a=101 AND b=56}
           79  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           80  +do_test 2.1 {
           81  +  eqp {SELECT * FROM t1 WHERE a=100 AND b BETWEEN 50 AND 54}
           82  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?) (~2 rows)}}
           83  +
           84  +# There are many more values of c between 0 and 100000 than there are
           85  +# between 800000 and 900000.  So t1c is more selective for the latter
           86  +# range.
           87  +#
           88  +do_test 3.1 {
           89  +  eqp {SELECT * FROM t1 WHERE b BETWEEN 50 AND 54 AND c BETWEEN 0 AND 100000}
           90  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?) (~6 rows)}}
           91  +do_test 3.2 {
           92  +  eqp {SELECT * FROM t1
           93  +       WHERE b BETWEEN 50 AND 54 AND c BETWEEN 800000 AND 900000}
           94  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?) (~4 rows)}}
           95  +do_test 3.3 {
           96  +  eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 0 AND 100000}
           97  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~63 rows)}}
           98  +do_test 3.4 {
           99  +  eqp {SELECT * FROM t1
          100  +       WHERE a=100 AND c BETWEEN 800000 AND 900000}
          101  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?) (~2 rows)}}
          102  +
          103  +finish_test

Changes to test/attach4.test.

    71     71     set L
    72     72   } $files
    73     73   
    74     74   set L [list]
    75     75   set S ""
    76     76   foreach {name f} $files {
    77     77     if {[permutation] == "journaltest"} {
    78         -    lappend L delete
           78  +    set mode delete
    79     79     } else {
    80         -    lappend L wal
           80  +    set mode wal
    81     81     }
           82  +  ifcapable !wal { set mode delete }
           83  +  lappend L $mode
    82     84     append S "
    83     85       PRAGMA $name.journal_mode = WAL;
    84     86       UPDATE $name.tbl SET x = '$name';
    85     87     "
    86     88   }
    87     89   do_execsql_test 1.5 $S $L
    88     90   

Changes to test/auth.test.

  2320   2320           DROP TABLE v1chng;
  2321   2321         }
  2322   2322       }
  2323   2323     }
  2324   2324     ifcapable stat2 {
  2325   2325       set stat2 "sqlite_stat2 "
  2326   2326     } else {
  2327         -    set stat2 ""
         2327  +    ifcapable stat3 {
         2328  +      set stat2 "sqlite_stat3 "
         2329  +    } else {
         2330  +      set stat2 ""
         2331  +    }
  2328   2332     }
  2329   2333     do_test auth-5.2 {
  2330   2334       execsql {
  2331   2335         SELECT name FROM (
  2332   2336           SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master)
  2333   2337         WHERE type='table'
  2334   2338         ORDER BY name

Changes to test/corrupt3.test.

    21     21   # Do not use a codec for tests in this file, as the database file is
    22     22   # manipulated directly using tcl scripts (using the [hexio_write] command).
    23     23   #
    24     24   do_not_use_codec
    25     25   
    26     26   # We must have the page_size pragma for these tests to work.
    27     27   #
    28         -ifcapable !pager_pragmas {
           28  +ifcapable !pager_pragmas||direct_read {
    29     29     finish_test
    30     30     return
    31     31   }
    32     32   
    33     33   # Create a database with an overflow page.
    34     34   #
    35     35   do_test corrupt3-1.1 {

Changes to test/corruptE.test.

    12     12   #
    13     13   # This file implements tests to make sure SQLite does not crash or
    14     14   # segfault if it sees a corrupt database file.  It specifcally
    15     15   # focuses on rowid order corruption.
    16     16   #
    17     17   # $Id: corruptE.test,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
    18     18   
    19         -catch {forcedelete test.db test.db-journal test.bu}
    20         -
    21     19   set testdir [file dirname $argv0]
    22     20   source $testdir/tester.tcl
    23     21   
    24     22   # Do not use a codec for tests in this file, as the database file is
    25     23   # manipulated directly using tcl scripts (using the [hexio_write] command).
    26     24   #
    27     25   do_not_use_codec

Changes to test/ctime.test.

   218    218   # test 1 before array bounds (N=-1)
   219    219   do_test ctime-2.5.$tc {
   220    220     set N -1
   221    221     set ans [ catchsql {
   222    222       SELECT sqlite_compileoption_get($N);
   223    223     } ]
   224    224   } {0 {{}}}
   225         -
   226         -ifcapable blockalloc {
   227         -  do_test ctime-3.1a {
   228         -    db eval {SELECT sqlite_compileoption_used('PAGECACHE_BLOCKALLOC')}
   229         -  } {1}
   230         -} else {
   231         -  do_test ctime-3.1b {
   232         -    db eval {SELECT sqlite_compileoption_used('PAGECACHE_BLOCKALLOC')}
   233         -  } {0}
   234         -}
   235         -
   236    225   
   237    226   
   238    227   finish_test

Changes to test/dbstatus.test.

    51     51   
    52     52   
    53     53   proc lookaside {db} {
    54     54     expr { $::lookaside_buffer_size *
    55     55       [lindex [sqlite3_db_status $db SQLITE_DBSTATUS_LOOKASIDE_USED 0] 1]
    56     56     }
    57     57   }
           58  +
           59  +ifcapable stat3 {
           60  +  set STAT3 1
           61  +} else {
           62  +  set STAT3 0
           63  +}
    58     64   
    59     65   #---------------------------------------------------------------------------
    60     66   # Run the dbstatus-2 and dbstatus-3 tests with several of different
    61     67   # lookaside buffer sizes.
    62     68   #
    63     69   foreach ::lookaside_buffer_size {0 64 120} {
    64     70   
................................................................................
   114    120         END;
   115    121       }
   116    122       5 {
   117    123         CREATE TABLE t1(a, b);
   118    124         CREATE TABLE t2(c, d);
   119    125         CREATE VIEW v1 AS SELECT * FROM t1 UNION SELECT * FROM t2;
   120    126       }
   121         -    6 {
          127  +    6y {
   122    128         CREATE TABLE t1(a, b);
   123    129         CREATE INDEX i1 ON t1(a);
   124    130         CREATE INDEX i2 ON t1(a,b);
   125    131         CREATE INDEX i3 ON t1(b,b);
   126    132         INSERT INTO t1 VALUES(randomblob(20), randomblob(25));
   127    133         INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1;
   128    134         INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1;
................................................................................
   194    200       # for any reason is not counted as "schema memory".
   195    201       #
   196    202       # Additionally, in auto-vacuum mode, dropping tables and indexes causes
   197    203       # the page-cache to shrink. So the amount of memory freed is always
   198    204       # much greater than just that reported by DBSTATUS_SCHEMA_USED in this
   199    205       # case.
   200    206       #
   201         -    if {[string match *x $tn] || $AUTOVACUUM} {
          207  +    # Some of the memory used for sqlite_stat3 is unaccounted for by
          208  +    # dbstatus.
          209  +    #
          210  +    if {[string match *x $tn] || $AUTOVACUUM
          211  +         || ([string match *y $tn] && $STAT3)} {
   202    212         do_test dbstatus-2.$tn.ax { expr {($nSchema1-$nSchema2)<=$nFree} } 1
   203    213       } else {
   204    214         do_test dbstatus-2.$tn.a { expr {$nSchema1-$nSchema2} } $nFree
   205    215       }
   206    216     
   207    217       do_test dbstatus-2.$tn.b { list $nAlloc1 $nSchema1 } "$nAlloc3 $nSchema3"
   208    218       do_test dbstatus-2.$tn.c { list $nAlloc2 $nSchema2 } "$nAlloc4 $nSchema4"

Added test/dbstatus2.test.

            1  +# 2011 September 20
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Tests for the sqlite3_stmt_status() function
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +set ::testprefix dbstatus2
           19  +
           20  +do_execsql_test 1.0 {
           21  +  PRAGMA page_size = 1024;
           22  +  PRAGMA auto_vacuum = 0;
           23  +
           24  +  CREATE TABLE t1(a PRIMARY KEY, b);
           25  +  INSERT INTO t1 VALUES(1, randomblob(600));
           26  +  INSERT INTO t1 VALUES(2, randomblob(600));
           27  +  INSERT INTO t1 VALUES(3, randomblob(600));
           28  +}
           29  +
           30  +proc db_hit_miss {db {reset 0}} {
           31  +  set nHit  [sqlite3_db_status $db CACHE_HIT $reset]
           32  +  set nMiss [sqlite3_db_status $db CACHE_MISS $reset]
           33  +  list $nHit $nMiss
           34  +}
           35  +
           36  +do_test 1.1 {
           37  +  db close
           38  +  sqlite3 db test.db
           39  +  expr {[file size test.db] / 1024}
           40  +} 6
           41  +
           42  +do_test 1.2 {
           43  +  execsql { SELECT b FROM t1 WHERE a=2 }
           44  +  db_hit_miss db
           45  +} {{0 2 0} {0 4 0}}
           46  +
           47  +do_test 1.3 { 
           48  +  execsql { SELECT b FROM t1 WHERE a=2 }
           49  +  db_hit_miss db
           50  +} {{0 6 0} {0 4 0}}
           51  +
           52  +do_test 1.4 { 
           53  +  execsql { SELECT b FROM t1 WHERE a=2 }
           54  +  db_hit_miss db
           55  +} {{0 10 0} {0 4 0}}
           56  +
           57  +do_test 1.5 { 
           58  +  db_hit_miss db 1
           59  +} {{0 10 0} {0 4 0}}
           60  +
           61  +do_test 1.6 { 
           62  +  db_hit_miss db 0
           63  +} {{0 0 0} {0 0 0}}
           64  +
           65  +do_test 1.7 {
           66  +  set fd [db incrblob main t1 b 1]
           67  +  fconfigure $fd -translation binary
           68  +  set len [string length [read $fd]]
           69  +  close $fd
           70  +  set len
           71  +} 600
           72  +do_test 1.8 { sqlite3_db_status db CACHE_HIT  0 } {0 2 0}
           73  +do_test 1.9 { sqlite3_db_status db CACHE_MISS 0 } {0 1 0}
           74  +
           75  + 
           76  +finish_test

Changes to test/malloc5.test.

   215    215   do_test malloc5-4.2 {
   216    216     sqlite3_release_memory
   217    217     sqlite3_soft_heap_limit 100000
   218    218     sqlite3_memory_highwater 1
   219    219     execsql {SELECT * FROM abc}
   220    220     set nMaxBytes [sqlite3_memory_highwater 1]
   221    221     puts -nonewline " (Highwater mark: $nMaxBytes) "
   222         -  expr $nMaxBytes <= 100000
          222  +  expr $nMaxBytes <= 110000
   223    223   } {1}
   224    224   do_test malloc5-4.3 {
   225    225     # Check that the content of table abc is at least roughly as expected.
   226    226     execsql {
   227    227       SELECT count(*), sum(a), sum(b) FROM abc;
   228    228     }
   229    229   } [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]

Changes to test/memdb.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is in-memory database backend.
    13     13   #
    14         -# $Id: memdb.test,v 1.19 2009/05/18 16:04:38 danielk1977 Exp $
    15     14   
    16     15   
    17     16   set testdir [file dirname $argv0]
    18     17   source $testdir/tester.tcl
    19     18   
    20     19   ifcapable memorydb {
    21     20   
................................................................................
   405    404     }
   406    405   } 0
   407    406   
   408    407   # Test that auto-vacuum works with in-memory databases.
   409    408   # 
   410    409   set msize [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
   411    410   if {[lindex $msize 2]!=0} {
   412         -  ifcapable autovacuum&&!blockalloc {
          411  +  ifcapable autovacuum {
   413    412       do_test memdb-9.1 {
   414    413         db close
   415    414         sqlite3 db test.db
   416    415         db cache size 0
   417    416         execsql {
   418    417           PRAGMA auto_vacuum = full;
   419    418           CREATE TABLE t1(a);

Changes to test/memsubsys1.test.

    21     21   # will not work with the "memsubsys1" permutation.
    22     22   #
    23     23   if {[permutation] == "memsubsys1"} {
    24     24     finish_test
    25     25     return
    26     26   }
    27     27   
    28         -# Nor will it work if the pager is allocating memory in blocks.
    29         -#
    30         -ifcapable blockalloc {
    31         -  finish_test
    32         -  return
    33         -}
    34         -
    35     28   # This procedure constructs a new database in test.db.  It fills
    36     29   # this database with many small records (enough to force multiple
    37     30   # rebalance operations in the btree-layer and to require a large
    38     31   # page cache), verifies correct results, then returns.
    39     32   #
    40     33   proc build_test_db {testname pragmas} {
    41     34     catch {db close}

Changes to test/misc3.test.

   266    266           a INTEGER DEFAULT 54321,
   267    267           b TEXT DEFAULT "hello",
   268    268           c REAL DEFAULT 3.1415926
   269    269         );
   270    270         CREATE UNIQUE INDEX ex1i1 ON ex1(a);
   271    271         EXPLAIN REINDEX;
   272    272       }]
   273         -    regexp { SorterCompare \d+ \d+ \d+ } $x
          273  +    ifcapable mergesort {
          274  +      regexp { SorterCompare \d+ \d+ \d+ } $x
          275  +    } else {
          276  +      regexp { IsUnique \d+ \d+ \d+ \d+ } $x
          277  +    }
   274    278     } {1}
   275    279     if {[regexp {16} [db one {PRAGMA encoding}]]} {
   276    280       do_test misc3-6.11-utf16 {
   277    281         set x [execsql {
   278    282           EXPLAIN SELECT a+123456789012, b*4.5678, c FROM ex1 ORDER BY +a, b DESC
   279    283         }]
   280    284         set y [regexp { 123456789012 } $x]

Changes to test/oserror.test.

    93     93   } {1 {unable to open database file}}
    94     94   
    95     95   do_re_test 1.4.2 { lindex $::log 0 } {^os_unix.c:\d*: \(\d+\) open\(.*test.db\) - }
    96     96   
    97     97   #--------------------------------------------------------------------------
    98     98   # Tests oserror-1.* test failures in the unlink() system call.
    99     99   #
   100         -do_test 2.1.1 {
   101         -  set ::log [list]
   102         -  file mkdir test.db-wal
   103         -  forcedelete test.db
   104         -  list [catch {
   105         -    sqlite3 dbh test.db
   106         -    execsql { SELECT * FROM sqlite_master } dbh
   107         -  } msg] $msg
   108         -} {1 {disk I/O error}}
   109         -
   110         -do_re_test 2.1.2 { 
   111         -  lindex $::log 0 
   112         -} {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
   113         -do_test 2.1.3 { 
   114         -  catch { dbh close }
   115         -  forcedelete test.db-wal
   116         -} {}
          100  +ifcapable wal {
          101  +  do_test 2.1.1 {
          102  +    set ::log [list]
          103  +    file mkdir test.db-wal
          104  +    forcedelete test.db
          105  +    list [catch {
          106  +      sqlite3 dbh test.db
          107  +      execsql { SELECT * FROM sqlite_master } dbh
          108  +    } msg] $msg
          109  +  } {1 {disk I/O error}}
          110  +  
          111  +  do_re_test 2.1.2 { 
          112  +    lindex $::log 0 
          113  +  } {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
          114  +  do_test 2.1.3 { 
          115  +    catch { dbh close }
          116  +    forcedelete test.db-wal
          117  +  } {}
          118  +}
   117    119     
   118    120   
   119    121   test_syscall reset
   120    122   sqlite3_shutdown
   121    123   test_sqlite3_log 
   122    124   sqlite3_initialize
   123    125   finish_test

Changes to test/pager1.test.

  1765   1765   # Test the pagers response to the b-tree layer requesting illegal page 
  1766   1766   # numbers:
  1767   1767   #
  1768   1768   #   + The locking page,
  1769   1769   #   + Page 0,
  1770   1770   #   + A page with a page number greater than (2^31-1).
  1771   1771   #
         1772  +# These tests will not work if SQLITE_DIRECT_OVERFLOW_READ is defined. In
         1773  +# that case IO errors are sometimes reported instead of SQLITE_CORRUPT.
         1774  +#
         1775  +ifcapable !direct_read {
  1772   1776   do_test pager1-18.1 {
  1773   1777     faultsim_delete_and_reopen
  1774   1778     db func a_string a_string
  1775   1779     execsql { 
  1776   1780       PRAGMA page_size = 1024;
  1777   1781       CREATE TABLE t1(a, b);
  1778   1782       INSERT INTO t1 VALUES(a_string(500), a_string(200));
................................................................................
  1840   1844     set root [db one "SELECT rootpage FROM sqlite_master"]
  1841   1845     db close
  1842   1846   
  1843   1847     hexio_write test.db [expr ($root-1)*1024 + 8] 00000000
  1844   1848     sqlite3 db test.db
  1845   1849     catchsql { SELECT length(x) FROM t1 }
  1846   1850   } {1 {database disk image is malformed}}
         1851  +}
  1847   1852   
  1848   1853   do_test pager1-19.1 {
  1849   1854     sqlite3 db ""
  1850   1855     db func a_string a_string
  1851   1856     execsql {
  1852   1857       PRAGMA page_size = 512;
  1853   1858       PRAGMA auto_vacuum = 1;

Changes to test/pageropt.test.

    13     13   # pager optimizations implemented in version 3.3.14 work.
    14     14   #
    15     15   # $Id: pageropt.test,v 1.5 2008/08/20 14:49:25 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable {!pager_pragmas||secure_delete} {
           20  +ifcapable {!pager_pragmas||secure_delete||direct_read} {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   # Run the SQL statement supplied by the argument and return
    26     26   # the results.  Prepend four integers to the beginning of the
    27     27   # result which are

Changes to test/pcache2.test.

    12     12   # This file is focused on testing the pcache module.
    13     13   #
    14     14   # $Id: pcache2.test,v 1.5 2009/07/18 14:36:24 danielk1977 Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19         -# If compiled with blockalloc, pagecache memory is not used. Which
    20         -# causes these tests to fail.
    21         -#
    22         -ifcapable blockalloc {
    23         -  finish_test
    24         -  return
    25         -}
    26         -
    27     19   # Set up a pcache memory pool so that we can easily track how many
    28     20   # pages are being used for cache.
    29     21   #
    30     22   do_test pcache2-1.1 {
    31     23     db close
    32     24     sqlite3_reset_auto_extension
    33     25     sqlite3_shutdown

Changes to test/stat.test.

    32     32     SELECT * FROM stat;
    33     33   } {}
    34     34   
    35     35   ifcapable wal {
    36     36     do_execsql_test stat-0.1 {
    37     37       PRAGMA journal_mode = WAL;
    38     38       PRAGMA journal_mode = delete;
    39         -    SELECT * FROM stat;
           39  +    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
           40  +      FROM stat;
    40     41     } {wal delete sqlite_master / 1 leaf 0 0 916 0}
    41     42   }
    42     43   
    43     44   do_test stat-1.0 {
    44     45     execsql {
    45     46       CREATE TABLE t1(a, b);
    46     47       CREATE INDEX i1 ON t1(b);
    47     48       INSERT INTO t1(rowid, a, b) VALUES(2, 2, 3);
    48     49       INSERT INTO t1(rowid, a, b) VALUES(3, 4, 5);
    49     50     }
    50     51   } {}
    51     52   do_test stat-1.1 {
    52     53     execsql {
    53         -    SELECT * FROM stat WHERE name = 't1';
           54  +    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
           55  +      FROM stat WHERE name = 't1';
    54     56     }
    55     57   } {t1 / 2 leaf 2 10 998 5}
    56     58   do_test stat-1.2 {
    57     59     execsql {
    58         -    SELECT * FROM stat WHERE name = 'i1';
           60  +    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
           61  +      FROM stat WHERE name = 'i1';
    59     62     }
    60     63   } {i1 / 3 leaf 2 10 1000 5}
    61     64   do_test stat-1.3 {
    62     65     execsql {
    63         -    SELECT * FROM stat WHERE name = 'sqlite_master';
           66  +    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
           67  +      FROM stat WHERE name = 'sqlite_master';
    64     68     }
    65     69   } {sqlite_master / 1 leaf 2 77 831 40}
    66     70   do_test stat-1.4 {
    67     71     execsql {
    68     72       DROP TABLE t1;
    69     73     }
    70     74   } {}
................................................................................
    73     77     CREATE TABLE t3(a PRIMARY KEY, b);
    74     78     INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
    75     79     INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    76     80     INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    77     81     INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    78     82     INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    79     83     INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    80         -  SELECT * FROM stat WHERE name != 'sqlite_master';
           84  +  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
           85  +    FROM stat WHERE name != 'sqlite_master';
    81     86   } [list \
    82     87     sqlite_autoindex_t3_1 / 3 internal 3 368 623 125       \
    83     88     sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123        \
    84     89     sqlite_autoindex_t3_1 /001/ 9 leaf 8 988 2 131         \
    85     90     sqlite_autoindex_t3_1 /002/ 15 leaf 7 857 137 132      \
    86     91     sqlite_autoindex_t3_1 /003/ 20 leaf 6 739 257 129      \
    87     92     t3 / 2 internal 15 0 907 0                             \
................................................................................
   104    109   ]
   105    110   do_execsql_test stat-2.2 { DROP TABLE t3 } {}
   106    111   
   107    112   do_execsql_test stat-3.1 {
   108    113     CREATE TABLE t4(x);
   109    114     CREATE INDEX i4 ON t4(x);
   110    115     INSERT INTO t4(rowid, x) VALUES(2, a_string(7777));
   111         -  SELECT * FROM stat WHERE name != 'sqlite_master';
          116  +  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
          117  +    FROM stat WHERE name != 'sqlite_master';
   112    118   } [list \
   113    119     i4 / 3 leaf 1 103 905 7782                 \
   114    120     i4 /000+000000 9 overflow 0 1020 0 0       \
   115    121     i4 /000+000001 10 overflow 0 1020 0 0      \
   116    122     i4 /000+000002 11 overflow 0 1020 0 0      \
   117    123     i4 /000+000003 12 overflow 0 1020 0 0      \
   118    124     i4 /000+000004 13 overflow 0 1020 0 0      \
................................................................................
   128    134     t4 /000+000005 18 overflow 0 1020 0 0      \
   129    135     t4 /000+000006 17 overflow 0 1020 0 0      \
   130    136   ]
   131    137   
   132    138   do_execsql_test stat-4.1 {
   133    139     CREATE TABLE t5(x);
   134    140     CREATE INDEX i5 ON t5(x);
   135         -  SELECT * FROM stat WHERE name = 't5' OR name = 'i5';
          141  +  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
          142  +    FROM stat WHERE name = 't5' OR name = 'i5';
   136    143   } [list  \
   137    144     i5 / 5 leaf 0 0 1016 0 \
   138    145     t5 / 4 leaf 0 0 1016 0 \
   139    146   ]
   140    147   
   141    148   db close
   142    149   forcedelete test.db
................................................................................
   145    152   breakpoint
   146    153   do_execsql_test stat-5.1 {
   147    154     PRAGMA auto_vacuum = OFF;
   148    155     CREATE VIRTUAL TABLE temp.stat USING dbstat;
   149    156     CREATE TABLE t1(x);
   150    157     INSERT INTO t1 VALUES(zeroblob(1513));
   151    158     INSERT INTO t1 VALUES(zeroblob(1514));
   152         -  SELECT * FROM stat WHERE name = 't1';
          159  +  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
          160  +    FROM stat WHERE name = 't1';
   153    161   } [list \
   154    162     t1 / 2 leaf 2 993 5 1517                \
   155    163     t1 /000+000000 3 overflow 0 1020 0 0    \
   156    164     t1 /001+000000 4 overflow 0 1020 0 0    \
   157    165   ]
   158    166   
   159    167   finish_test

Changes to test/superlock.test.

    44     44   #
    45     45   
    46     46   do_execsql_test 1.1 {
    47     47     CREATE TABLE t1(a, b);
    48     48     INSERT INTO t1 VALUES(1, 2);
    49     49     PRAGMA journal_mode = DELETE;
    50     50   } {delete}
           51  +
           52  +ifcapable !wal {
           53  +  finish_test
           54  +  return
           55  +}
    51     56   
    52     57   do_test 1.2 { sqlite3demo_superlock unlock test.db } {unlock}
    53     58   do_catchsql_test 1.3 { SELECT * FROM t1 } {1 {database is locked}}
    54     59   do_test 1.4 { unlock } {}
    55     60   
    56     61   ifcapable !wal {finish_test ; return }
    57     62   if { ![wal_is_ok] } {

Changes to test/table.test.

   255    255   } {0 {}}
   256    256   
   257    257   # Try to drop sqlite_master
   258    258   #
   259    259   do_test table-5.2 {
   260    260     catchsql {DROP TABLE IF EXISTS sqlite_master}
   261    261   } {1 {table sqlite_master may not be dropped}}
          262  +
          263  +# Dropping sqlite_statN tables is OK.
          264  +#
          265  +do_test table-5.2.1 {
          266  +  db eval {
          267  +    ANALYZE;
          268  +    DROP TABLE IF EXISTS sqlite_stat1;
          269  +    DROP TABLE IF EXISTS sqlite_stat2;
          270  +    DROP TABLE IF EXISTS sqlite_stat3;
          271  +    SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_stat*';
          272  +  }
          273  +} {}
   262    274   
   263    275   # Make sure an EXPLAIN does not really create a new table
   264    276   #
   265    277   do_test table-5.3 {
   266    278     ifcapable {explain} {
   267    279       execsql {EXPLAIN CREATE TABLE test1(f1 int)}
   268    280     }

Added test/tkt-c48d99d690.test.

            1  +
            2  +set testdir [file dirname $argv0]
            3  +source $testdir/tester.tcl
            4  +
            5  +set ::testprefix tkt-c48d99d690
            6  +
            7  +do_test 1.0 {
            8  +  execsql {
            9  +    CREATE TABLE t1(a, b);
           10  +    CREATE TABLE t2(a, b);
           11  +    INSERT INTO t1 VALUES('one'  , 1);
           12  +    INSERT INTO t1 VALUES('two'  , 5);
           13  +    INSERT INTO t1 VALUES('two'  , 2);
           14  +    INSERT INTO t1 VALUES('three', 3);
           15  +    PRAGMA count_changes = 1;
           16  +  }
           17  +} {}
           18  +
           19  +do_test 1.1 {
           20  +  execsql { INSERT INTO t2 SELECT * FROM t1 }
           21  +} {4}
           22  +
           23  +do_test 1.2 { execsql VACUUM } {}
           24  +
           25  +finish_test
           26  +

Changes to test/tkt-cbd054fa6b.test.

    12     12   # This file implements tests to verify that ticket [cbd054fa6b] has been
    13     13   # fixed.  
    14     14   #
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19         -ifcapable !stat2 {
           19  +ifcapable !stat3 {
    20     20     finish_test
    21     21     return
    22     22   }
    23     23   
    24     24   do_test tkt-cbd05-1.1 {
    25     25     db eval {
    26     26       CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT UNIQUE NOT NULL);
................................................................................
    42     42     db eval {
    43     43       ANALYZE;
    44     44     }
    45     45   } {}
    46     46   do_test tkt-cbd05-1.3 {
    47     47     execsql { 
    48     48       SELECT tbl,idx,group_concat(sample,' ') 
    49         -    FROM sqlite_stat2 
           49  +    FROM sqlite_stat3 
    50     50       WHERE idx = 't1_x' 
    51     51       GROUP BY tbl,idx
    52     52     }
    53     53   } {t1 t1_x { A B C D E F G H I}}
    54     54   
    55     55   do_test tkt-cbd05-2.1 {
    56     56     db eval {
................................................................................
    74     74     db eval {
    75     75       ANALYZE;
    76     76     }
    77     77   } {}
    78     78   do_test tkt-cbd05-2.3 {
    79     79     execsql { 
    80     80       SELECT tbl,idx,group_concat(sample,' ') 
    81         -    FROM sqlite_stat2 
           81  +    FROM sqlite_stat3 
    82     82       WHERE idx = 't1_x' 
    83     83       GROUP BY tbl,idx
    84     84     }
    85     85   } {t1 t1_x { A B C D E F G H I}}
    86     86   
    87     87   finish_test

Changes to test/unordered.test.

    27     27     INSERT INTO t1 SELECT a+16, b FROM t1;
    28     28     INSERT INTO t1 SELECT a+32, b FROM t1;
    29     29     INSERT INTO t1 SELECT a+64, b FROM t1;
    30     30     ANALYZE;
    31     31   } {}
    32     32   
    33     33   foreach idxmode {ordered unordered} {
           34  +  catchsql { DELETE FROM sqlite_stat2 }
           35  +  catchsql { DELETE FROM sqlite_stat3 }
    34     36     if {$idxmode == "unordered"} {
    35     37       execsql { UPDATE sqlite_stat1 SET stat = stat || ' unordered' }
    36         -    db close
    37         -    sqlite3 db test.db
    38     38     }
           39  +  db close
           40  +  sqlite3 db test.db
    39     41     foreach {tn sql r(ordered) r(unordered)} {
    40     42       1   "SELECT * FROM t1 ORDER BY a"
    41     43           {0 0 0 {SCAN TABLE t1 USING INDEX i1 (~128 rows)}}
    42     44           {0 0 0 {SCAN TABLE t1 (~128 rows)} 0 0 0 {USE TEMP B-TREE FOR ORDER BY}}
    43     45       2   "SELECT * FROM t1 WHERE a >?"
    44     46           {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?) (~32 rows)}}
    45     47           {0 0 0 {SCAN TABLE t1 (~42 rows)}}

Changes to test/uri.test.

   215    215   #
   216    216   
   217    217   # This block of code creates two VFS - "tvfs1" and "tvfs2". Each time one
   218    218   # of the above methods is called using "tvfs1", global variable ::T1(X) is
   219    219   # set, where X is the file-name the method is called on. Calls to the above
   220    220   # methods using "tvfs2" set entries in the global T2 array.
   221    221   #
   222         -testvfs tvfs1 
   223         -tvfs1 filter {xOpen xDelete xAccess xFullPathname}
   224         -tvfs1 script tvfs1_callback
   225         -proc tvfs1_callback {method filename args} { 
   226         -  set ::T1([file tail $filename]) 1 
   227         -}
   228         -testvfs tvfs2 
   229         -tvfs2 filter {xOpen xDelete xAccess xFullPathname}
   230         -tvfs2 script tvfs2_callback
   231         -proc tvfs2_callback {method filename args} { 
   232         -  set ::T2([file tail $filename]) 1 
   233         -}
   234         -
   235         -catch {db close}
   236         -eval forcedelete [glob test.db*]
   237         -do_test 5.1.1 {
   238         -  sqlite3 db file:test.db1?vfs=tvfs1
   239         -  execsql {
   240         -    ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
   241         -    PRAGMA main.journal_mode = PERSIST;
   242         -    PRAGMA aux.journal_mode = PERSIST;
   243         -    CREATE TABLE t1(a, b);
   244         -    CREATE TABLE aux.t2(a, b);
   245         -    PRAGMA main.journal_mode = WAL;
   246         -    PRAGMA aux.journal_mode = WAL;
   247         -    INSERT INTO t1 VALUES('x', 'y');
   248         -    INSERT INTO t2 VALUES('x', 'y');
          222  +ifcapable wal {
          223  +  testvfs tvfs1 
          224  +  tvfs1 filter {xOpen xDelete xAccess xFullPathname}
          225  +  tvfs1 script tvfs1_callback
          226  +  proc tvfs1_callback {method filename args} { 
          227  +    set ::T1([file tail $filename]) 1 
          228  +  }
          229  +  testvfs tvfs2 
          230  +  tvfs2 filter {xOpen xDelete xAccess xFullPathname}
          231  +  tvfs2 script tvfs2_callback
          232  +  proc tvfs2_callback {method filename args} { 
          233  +    set ::T2([file tail $filename]) 1 
   249    234     }
   250         -  lsort [array names ::T1]
   251         -} {test.db1 test.db1-journal test.db1-wal}
   252         -
   253         -do_test 5.1.2 {
   254         -  lsort [array names ::T2]
   255         -} {test.db2 test.db2-journal test.db2-wal}
   256         -
   257         -db close
   258         -tvfs1 delete
   259         -tvfs2 delete
          235  +  
          236  +  catch {db close}
          237  +  eval forcedelete [glob test.db*]
          238  +  do_test 5.1.1 {
          239  +    sqlite3 db file:test.db1?vfs=tvfs1
          240  +    execsql {
          241  +      ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
          242  +      PRAGMA main.journal_mode = PERSIST;
          243  +      PRAGMA aux.journal_mode = PERSIST;
          244  +      CREATE TABLE t1(a, b);
          245  +      CREATE TABLE aux.t2(a, b);
          246  +      PRAGMA main.journal_mode = WAL;
          247  +      PRAGMA aux.journal_mode = WAL;
          248  +      INSERT INTO t1 VALUES('x', 'y');
          249  +      INSERT INTO t2 VALUES('x', 'y');
          250  +    }
          251  +    lsort [array names ::T1]
          252  +  } {test.db1 test.db1-journal test.db1-wal}
          253  +  
          254  +  do_test 5.1.2 {
          255  +    lsort [array names ::T2]
          256  +  } {test.db2 test.db2-journal test.db2-wal}
          257  +  db close
          258  +  
          259  +  tvfs1 delete
          260  +  tvfs2 delete
          261  +}
   260    262   
   261    263   #-------------------------------------------------------------------------
   262    264   # Check that only "" and "localhost" are acceptable as authorities.
   263    265   #
   264    266   catch {db close}
   265    267   foreach {tn uri res} {
   266    268     1     "file://localhost/PWD/test.db"   {not an error}

Changes to test/vacuum.test.

   380    380         VACUUM;
   381    381       }
   382    382       cksum
   383    383     } $::cksum
   384    384   }
   385    385   
   386    386   forcedelete {a'z.db}
          387  +
          388  +# Test that "PRAGMA count_changes" does not interfere with VACUUM or cause
          389  +# it to return any rows to the user.
          390  +#
          391  +do_test vacuum-10.1 {
          392  +  db close
          393  +  forcedelete test.db
          394  +  sqlite3 db test.db
          395  +  execsql {
          396  +    CREATE TABLE t8(a, b);
          397  +    INSERT INTO t8 VALUES('a', 'b');
          398  +    INSERT INTO t8 VALUES('c', 'd');
          399  +    PRAGMA count_changes = 1;
          400  +  }
          401  +} {}
          402  +do_test vacuum-10.2 { execsql VACUUM } {}
   387    403   
   388    404   finish_test

Changes to test/walbig.test.

    12     12   # focus of this script testing the ability of SQLite to handle database
    13     13   # files larger than 4GB in WAL mode.
    14     14   #
    15     15   
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
           19  +
           20  +ifcapable !wal {
           21  +  finish_test
           22  +  return
           23  +}
    19     24   
    20     25   # Do not use a codec for this file, as the database is manipulated using
    21     26   # external methods (the [fake_big_file] and [hexio_write] commands).
    22     27   #
    23     28   do_not_use_codec
    24     29   
    25     30   # If SQLITE_DISABLE_LFS is defined, omit this file.

Changes to test/walpersist.test.

    12     12   # This file contains tests for using WAL with persistent WAL file mode.
    13     13   #
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   source $testdir/lock_common.tcl
    18     18   set ::testprefix walpersist
           19  +
           20  +ifcapable !wal {
           21  +  finish_test
           22  +  return
           23  +}
    19     24   
    20     25   do_test walpersist-1.0 {
    21     26     db eval {
    22     27       PRAGMA journal_mode=WAL;
    23     28       CREATE TABLE t1(a);
    24     29       INSERT INTO t1 VALUES(randomblob(5000));
    25     30     }

Changes to test/walro.test.

    26     26   
    27     27   # these tests can't deal with the location of the -shm file under proxy locking
    28     28   #
    29     29   if {[forced_proxy_locking]} {
    30     30     finish_test
    31     31     return
    32     32   }
           33  +
           34  +# And only if the build is WAL-capable.
           35  +#
           36  +ifcapable !wal {
           37  +  finish_test
           38  +  return
           39  +}
    33     40   
    34     41   do_multiclient_test tn {
    35     42     # Do not run tests with the connections in the same process.
    36     43     #
    37     44     if {$tn==2} continue
    38     45     
    39     46     # Close all connections and delete the database.

Changes to test/where7.test.

     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the multi-index OR clause optimizer.
    13         -#
    14         -# $Id: where7.test,v 1.9 2009/06/07 23:45:11 drh Exp $
    15     13   
    16     14   set testdir [file dirname $argv0]
    17     15   source $testdir/tester.tcl
    18     16   
    19     17   ifcapable !or_opt {
    20     18     finish_test
    21     19     return
................................................................................
 23337  23335       FROM t302 JOIN t301 ON t302.c8 = t301.c8
 23338  23336       WHERE t302.c2 = 19571
 23339  23337         AND t302.c3 > 1287603136
 23340  23338         AND (t301.c4 = 1407449685622784
 23341  23339              OR t301.c8 = 1407424651264000)
 23342  23340      ORDER BY t302.c5 LIMIT 200;
 23343  23341   } {
 23344         -  0 0 1 {SEARCH TABLE t301 USING COVERING INDEX t301_c4 (c4=?) (~10 rows)} 
        23342  +  0 0 1 {SEARCH TABLE t301 USING COVERING INDEX t301_c4 (c4=?) (~5 rows)} 
 23345  23343     0 0 1 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} 
 23346  23344     0 1 0 {SEARCH TABLE t302 USING INDEX t302_c8_c3 (c8=? AND c3>?) (~2 rows)} 
 23347  23345     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
 23348  23346   }
 23349  23347   
 23350  23348   finish_test

Changes to test/where9.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing the multi-index OR clause optimizer.
    13     13   #
    14         -# $Id: where9.test,v 1.9 2009/06/05 17:09:12 drh Exp $
    15     14   
    16     15   set testdir [file dirname $argv0]
    17     16   source $testdir/tester.tcl
    18     17   
    19     18   ifcapable !or_opt {
    20     19     finish_test
    21     20     return
................................................................................
   361    360     do_execsql_test where9-3.1 {
   362    361       EXPLAIN QUERY PLAN
   363    362       SELECT t2.a FROM t1, t2
   364    363       WHERE t1.a=80 AND ((t1.c=t2.c AND t1.d=t2.d) OR t1.f=t2.f)
   365    364     } {
   366    365       0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} 
   367    366       0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?) (~2 rows)} 
   368         -    0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~10 rows)}
          367  +    0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~5 rows)}
   369    368     }
   370    369     do_execsql_test where9-3.2 {
   371    370       EXPLAIN QUERY PLAN
   372    371       SELECT coalesce(t2.a,9999)
   373    372       FROM t1 LEFT JOIN t2 ON (t1.c+1=t2.c AND t1.d=t2.d) OR (t1.f||'x')=t2.f
   374    373       WHERE t1.a=80
   375    374     } {
   376    375       0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} 
   377    376       0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?) (~2 rows)} 
   378         -    0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~10 rows)}
          377  +    0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?) (~5 rows)}
   379    378     }
   380    379   } 
   381    380   
   382    381   # Make sure that INDEXED BY and multi-index OR clauses play well with
   383    382   # one another.
   384    383   #
   385    384   do_test where9-4.1 {
................................................................................
   450    449   ifcapable explain {
   451    450     # The (c=31031 OR d IS NULL) clause is preferred over b>1000 because
   452    451     # the former is an equality test which is expected to return fewer rows.
   453    452     #
   454    453     do_execsql_test where9-5.1 {
   455    454       EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>1000 AND (c=31031 OR d IS NULL)
   456    455     } {
   457         -    0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?) (~10 rows)} 
   458         -    0 0 0 {SEARCH TABLE t1 USING INDEX t1d (d=?) (~10 rows)}
          456  +    0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?) (~2 rows)} 
          457  +    0 0 0 {SEARCH TABLE t1 USING INDEX t1d (d=?) (~2 rows)}
   459    458     }
   460    459   
   461    460     # In contrast, b=1000 is preferred over any OR-clause.
   462    461     #
   463    462     do_execsql_test where9-5.2 {
   464    463       EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b=1000 AND (c=31031 OR d IS NULL)
   465    464     } {
................................................................................
   778    777     catchsql {
   779    778       UPDATE t1 INDEXED BY t1b SET a=a+100
   780    779        WHERE (b IS NULL AND c NOT NULL AND d NOT NULL)
   781    780           OR (b NOT NULL AND c IS NULL AND d NOT NULL)
   782    781           OR (b NOT NULL AND c NOT NULL AND d IS NULL)
   783    782     }
   784    783   } {1 {cannot use index: t1b}}
          784  +
          785  +############################################################################
          786  +# Test cases where terms inside an OR series are combined with AND terms
          787  +# external to the OR clause.  In other words, cases where
          788  +#
          789  +#              x AND (y OR z)
          790  +#
          791  +# is able to use indices on x,y and x,z, or indices y,x and z,x.
          792  +#
          793  +do_test where9-7.0 {
          794  +  execsql {
          795  +    CREATE TABLE t5(a, b, c, d, e, f, g, x, y);
          796  +    INSERT INTO t5
          797  +     SELECT a, b, c, e, d, f, g,
          798  +            CASE WHEN (a&1)!=0 THEN 'y' ELSE 'n' END,
          799  +            CASE WHEN (a&2)!=0 THEN 'y' ELSE 'n' END
          800  +       FROM t1;
          801  +    CREATE INDEX t5xb ON t5(x, b);
          802  +    CREATE INDEX t5xc ON t5(x, c);
          803  +    CREATE INDEX t5xd ON t5(x, d);
          804  +    CREATE INDEX t5xe ON t5(x, e);
          805  +    CREATE INDEX t5xf ON t5(x, f);
          806  +    CREATE INDEX t5xg ON t5(x, g);
          807  +    CREATE INDEX t5yb ON t5(y, b);
          808  +    CREATE INDEX t5yc ON t5(y, c);
          809  +    CREATE INDEX t5yd ON t5(y, d);
          810  +    CREATE INDEX t5ye ON t5(y, e);
          811  +    CREATE INDEX t5yf ON t5(y, f);
          812  +    CREATE INDEX t5yg ON t5(y, g);
          813  +    CREATE TABLE t6(a, b, c, e, d, f, g, x, y);
          814  +    INSERT INTO t6 SELECT * FROM t5;
          815  +    ANALYZE t5;
          816  +  }
          817  +} {}
          818  +do_test where9-7.1.1 {
          819  +  count_steps {
          820  +    SELECT a FROM t5 WHERE x='y' AND (b=913 OR c=27027) ORDER BY a;
          821  +  }
          822  +} {79 81 83 scan 0 sort 1}
          823  +do_test where9-7.1.2 {
          824  +  execsql {
          825  +    SELECT a FROM t6 WHERE x='y' AND (b=913 OR c=27027) ORDER BY a;
          826  +  }
          827  +} {79 81 83}
          828  +do_test where9-7.1.3 {
          829  +  count_steps {
          830  +    SELECT a FROM t5 WHERE x='n' AND (b=913 OR c=27027) ORDER BY a;
          831  +  }
          832  +} {80 scan 0 sort 1}
          833  +do_test where9-7.1.4 {
          834  +  execsql {
          835  +    SELECT a FROM t6 WHERE x='n' AND (b=913 OR c=27027) ORDER BY a;
          836  +  }
          837  +} {80}
          838  +do_test where9-7.2.1 {
          839  +  count_steps {
          840  +    SELECT a FROM t5 WHERE (x='y' OR y='y') AND b=913 ORDER BY a;
          841  +  }
          842  +} {83 scan 0 sort 1}
          843  +do_test where9-7.2.2 {
          844  +  execsql {
          845  +    SELECT a FROM t6 WHERE (x='y' OR y='y') AND b=913 ORDER BY a;
          846  +  }
          847  +} {83}
          848  +do_test where9-7.3.1 {
          849  +  count_steps {
          850  +    SELECT a FROM t5 WHERE (x='y' OR y='y') AND c=27027 ORDER BY a;
          851  +  }
          852  +} {79 81 scan 0 sort 1}
          853  +do_test where9-7.3.2 {
          854  +  execsql {
          855  +    SELECT a FROM t6 WHERE (x='y' OR y='y') AND c=27027 ORDER BY a;
          856  +  }
          857  +} {79 81}
          858  +
   785    859   
   786    860   finish_test

Changes to tool/spaceanal.tcl.

     1      1   # Run this TCL script using "testfixture" in order get a report that shows
     2      2   # how much disk space is used by a particular data to actually store data
     3      3   # versus how much space is unused.
     4      4   #
     5      5   
     6      6   if {[catch {
     7         -if {![info exists argv0]} {
     8         -  set argv0 [file rootname [file tail [info nameofexecutable]]]
     9         -}
    10         -
    11      7   # Get the name of the database to analyze
    12      8   #
    13         -#set argv $argv0
    14         -if {![info exists argv] || [llength $argv]!=1} {
            9  +proc usage {} {
           10  +  set argv0 [file rootname [file tail [info nameofexecutable]]]
    15     11     puts stderr "Usage: $argv0 database-name"
    16     12     exit 1
    17     13   }
    18         -set file_to_analyze [lindex $argv 0]
           14  +set file_to_analyze {}
           15  +set flags(-pageinfo) 0
           16  +set flags(-stats) 0
           17  +append argv {}
           18  +foreach arg $argv {
           19  +  if {[regexp {^-+pageinfo$} $arg]} {
           20  +    set flags(-pageinfo) 1
           21  +  } elseif {[regexp {^-+stats$} $arg]} {
           22  +    set flags(-stats) 1
           23  +  } elseif {[regexp {^-} $arg]} {
           24  +    puts stderr "Unknown option: $arg"
           25  +    usage
           26  +  } elseif {$file_to_analyze!=""} {
           27  +    usage
           28  +  } else {
           29  +    set file_to_analyze $arg
           30  +  }
           31  +}
           32  +if {$file_to_analyze==""} usage
    19     33   if {![file exists $file_to_analyze]} {
    20     34     puts stderr "No such file: $file_to_analyze"
    21     35     exit 1
    22     36   }
    23     37   if {![file readable $file_to_analyze]} {
    24     38     puts stderr "File is not readable: $file_to_analyze"
    25     39     exit 1
    26     40   }
    27         -if {[file size $file_to_analyze]<512} {
           41  +set true_file_size [file size $file_to_analyze]
           42  +if {$true_file_size<512} {
    28     43     puts stderr "Empty or malformed database: $file_to_analyze"
    29     44     exit 1
    30     45   }
           46  +
           47  +# Compute the total file size assuming test_multiplexor is being used.
           48  +# Assume that SQLITE_ENABLE_8_3_NAMES might be enabled
           49  +#
           50  +set extension [file extension $file_to_analyze]
           51  +set pattern $file_to_analyze
           52  +append pattern {[0-9][0-9]}
           53  +foreach f [glob -nocomplain $pattern] {
           54  +  incr true_file_size [file size $f]
           55  +  set extension {}
           56  +}
           57  +if {[string length $extension]>=2 && [string length $extension]<=4} {
           58  +  set pattern [file rootname $file_to_analyze]
           59  +  append pattern [string range $extension 0 1]
           60  +  append pattern {[0-9][0-9]}
           61  +  foreach f [glob -nocomplain $pattern] {
           62  +    incr true_file_size [file size $f]
           63  +  }
           64  +}
    31     65   
    32     66   # Open the database
    33     67   #
    34     68   sqlite3 db $file_to_analyze
    35     69   register_dbstat_vtab db
    36     70   
    37         -set pageSize [db one {PRAGMA page_size}]
           71  +db eval {SELECT count(*) FROM sqlite_master}
           72  +set pageSize [expr {wide([db one {PRAGMA page_size}])}]
    38     73   
    39         -#set DB [btree_open $file_to_analyze 1000 0]
           74  +if {$flags(-pageinfo)} {
           75  +  db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
           76  +  db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} {
           77  +    puts "$pageno $name $path"
           78  +  }
           79  +  exit 0
           80  +}
           81  +if {$flags(-stats)} {
           82  +  db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
           83  +  puts "BEGIN;"
           84  +  puts "CREATE TABLE stats("
           85  +  puts "  name       STRING,           /* Name of table or index */"
           86  +  puts "  path       INTEGER,          /* Path to page from root */"
           87  +  puts "  pageno     INTEGER,          /* Page number */"
           88  +  puts "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"
           89  +  puts "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"
           90  +  puts "  payload    INTEGER,          /* Bytes of payload on this page */"
           91  +  puts "  unused     INTEGER,          /* Bytes of unused space on this page */"
           92  +  puts "  mx_payload INTEGER,          /* Largest payload size of all cells */"
           93  +  puts "  pgoffset   INTEGER,          /* Offset of page in file */"
           94  +  puts "  pgsize     INTEGER           /* Size of the page */"
           95  +  puts ");"
           96  +  db eval {SELECT quote(name) || ',' ||
           97  +                  quote(path) || ',' ||
           98  +                  quote(pageno) || ',' ||
           99  +                  quote(pagetype) || ',' ||
          100  +                  quote(ncell) || ',' ||
          101  +                  quote(payload) || ',' ||
          102  +                  quote(unused) || ',' ||
          103  +                  quote(mx_payload) || ',' ||
          104  +                  quote(pgoffset) || ',' ||
          105  +                  quote(pgsize) AS x FROM stat} {
          106  +    puts "INSERT INTO stats VALUES($x);"
          107  +  }
          108  +  puts "COMMIT;"
          109  +  exit 0
          110  +}
    40    111   
    41    112   # In-memory database for collecting statistics. This script loops through
    42    113   # the tables and indices in the database being analyzed, adding a row for each
    43    114   # to an in-memory database (for which the schema is shown below). It then
    44    115   # queries the in-memory db to produce the space-analysis report.
    45    116   #
    46    117   sqlite3 mem :memory:
................................................................................
    56    127      mx_payload int,   -- Maximum payload size
    57    128      int_pages int,    -- Number of interior pages used
    58    129      leaf_pages int,   -- Number of leaf pages used
    59    130      ovfl_pages int,   -- Number of overflow pages used
    60    131      int_unused int,   -- Number of unused bytes on interior pages
    61    132      leaf_unused int,  -- Number of unused bytes on primary pages
    62    133      ovfl_unused int,  -- Number of unused bytes on overflow pages
    63         -   gap_cnt int       -- Number of gaps in the page layout
          134  +   gap_cnt int,      -- Number of gaps in the page layout
          135  +   compressed_size int  -- Total bytes stored on disk
    64    136   );}
    65    137   mem eval $tabledef
    66    138   
    67    139   # Create a temporary "dbstat" virtual table.
    68    140   #
    69         -db eval { 
    70         -  CREATE VIRTUAL TABLE temp.stat USING dbstat;
    71         -  CREATE TEMP TABLE dbstat AS SELECT * FROM temp.stat ORDER BY name, path;
    72         -  DROP TABLE temp.stat;
    73         -}
          141  +db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
          142  +db eval {CREATE TEMP TABLE dbstat AS SELECT * FROM temp.stat
          143  +         ORDER BY name, path}
          144  +db eval {DROP TABLE temp.stat}
    74    145   
    75    146   proc isleaf {pagetype is_index} {
    76    147     return [expr {$pagetype == "leaf" || ($pagetype == "internal" && $is_index)}]
    77    148   }
    78    149   proc isoverflow {pagetype is_index} {
    79    150     return [expr {$pagetype == "overflow"}]
    80    151   }
................................................................................
    82    153     return [expr {$pagetype == "internal" && $is_index==0}]
    83    154   }
    84    155   
    85    156   db func isleaf isleaf
    86    157   db func isinternal isinternal
    87    158   db func isoverflow isoverflow
    88    159   
          160  +set isCompressed 0
          161  +set compressOverhead 0
    89    162   set sql { SELECT name, tbl_name FROM sqlite_master WHERE rootpage>0 }
    90    163   foreach {name tblname} [concat sqlite_master sqlite_master [db eval $sql]] {
    91    164   
    92    165     set is_index [expr {$name!=$tblname}]
    93    166     db eval {
    94    167       SELECT 
    95    168         sum(ncell) AS nentry,
................................................................................
    99    172         sum(path LIKE '%+000000') AS ovfl_cnt,
   100    173         max(mx_payload) AS mx_payload,
   101    174         sum(isinternal(pagetype, $is_index)) AS int_pages,
   102    175         sum(isleaf(pagetype, $is_index)) AS leaf_pages,
   103    176         sum(isoverflow(pagetype, $is_index)) AS ovfl_pages,
   104    177         sum(isinternal(pagetype, $is_index) * unused) AS int_unused,
   105    178         sum(isleaf(pagetype, $is_index) * unused) AS leaf_unused,
   106         -      sum(isoverflow(pagetype, $is_index) * unused) AS ovfl_unused
          179  +      sum(isoverflow(pagetype, $is_index) * unused) AS ovfl_unused,
          180  +      sum(pgsize) AS compressed_size
   107    181       FROM temp.dbstat WHERE name = $name
   108    182     } break
          183  +
          184  +  set total_pages [expr {$leaf_pages+$int_pages+$ovfl_pages}]
          185  +  set storage [expr {$total_pages*$pageSize}]
          186  +  if {!$isCompressed && $storage>$compressed_size} {
          187  +    set isCompressed 1
          188  +    set compressOverhead 14
          189  +  }
   109    190   
   110    191     # Column 'gap_cnt' is set to the number of non-contiguous entries in the
   111    192     # list of pages visited if the b-tree structure is traversed in a top-down
   112    193     # fashion (each node visited before its child-tree is passed). Any overflow
   113    194     # chains present are traversed from start to finish before any child-tree
   114    195     # is.
   115    196     #
................................................................................
   136    217         $mx_payload,
   137    218         $int_pages,
   138    219         $leaf_pages,  
   139    220         $ovfl_pages, 
   140    221         $int_unused, 
   141    222         $leaf_unused,
   142    223         $ovfl_unused,
   143         -      $gap_cnt
          224  +      $gap_cnt,
          225  +      $compressed_size
   144    226       );
   145    227     }
   146    228   }
   147    229   
   148    230   proc integerify {real} {
   149    231     if {[string is double -strict $real]} {
   150         -    return [expr {int($real)}]
          232  +    return [expr {wide($real)}]
   151    233     } else {
   152    234       return 0
   153    235     }
   154    236   }
   155    237   mem function int integerify
   156    238   
   157    239   # Quote a string for use in an SQL query. Examples:
................................................................................
   198    280     return [format %.2f [expr double($num)/double($denom)]]
   199    281   }
   200    282   
   201    283   # Generate a subreport that covers some subset of the database.
   202    284   # the $where clause determines which subset to analyze.
   203    285   #
   204    286   proc subreport {title where} {
   205         -  global pageSize file_pgcnt
          287  +  global pageSize file_pgcnt compressOverhead
   206    288   
   207    289     # Query the in-memory database for the sum of various statistics 
   208    290     # for the subset of tables/indices identified by the WHERE clause in
   209    291     # $where. Note that even if the WHERE clause matches no rows, the
   210    292     # following query returns exactly one row (because it is an aggregate).
   211    293     #
   212    294     # The results of the query are stored directly by SQLite into local 
................................................................................
   222    304         int(sum(ovfl_cnt)) as ovfl_cnt,
   223    305         int(sum(leaf_pages)) AS leaf_pages,
   224    306         int(sum(int_pages)) AS int_pages,
   225    307         int(sum(ovfl_pages)) AS ovfl_pages,
   226    308         int(sum(leaf_unused)) AS leaf_unused,
   227    309         int(sum(int_unused)) AS int_unused,
   228    310         int(sum(ovfl_unused)) AS ovfl_unused,
   229         -      int(sum(gap_cnt)) AS gap_cnt
          311  +      int(sum(gap_cnt)) AS gap_cnt,
          312  +      int(sum(compressed_size)) AS compressed_size
   230    313       FROM space_used WHERE $where" {} {}
   231    314   
   232    315     # Output the sub-report title, nicely decorated with * characters.
   233    316     #
   234    317     puts ""
   235    318     set len [string length $title]
   236    319     set stars [string repeat * [expr 65-$len]]
................................................................................
   272    355     set ovfl_cnt_percent [percent $ovfl_cnt $nleaf {of all entries}]
   273    356   
   274    357     # Print out the sub-report statistics.
   275    358     #
   276    359     statline {Percentage of total database} $total_pages_percent
   277    360     statline {Number of entries} $nleaf
   278    361     statline {Bytes of storage consumed} $storage
          362  +  if {$compressed_size!=$storage} {
          363  +    set compressed_size [expr {$compressed_size+$compressOverhead*$total_pages}]
          364  +    set pct [expr {$compressed_size*100.0/$storage}]
          365  +    set pct [format {%5.1f%%} $pct]
          366  +    statline {Bytes used after compression} $compressed_size $pct
          367  +  }
   279    368     statline {Bytes of payload} $payload $payload_percent
   280    369     statline {Average payload per entry} $avg_payload
   281    370     statline {Average unused bytes per entry} $avg_unused
   282    371     if {[info exists avg_fanout]} {
   283    372       statline {Average fanout} $avg_fanout
   284    373     }
   285    374     if {$total_pages>1} {
................................................................................
   328    417     # The number of entries on each pointer map page. The layout of the
   329    418     # database file is one pointer-map page, followed by $ptrsPerPage other
   330    419     # pages, followed by a pointer-map page etc. The first pointer-map page
   331    420     # is the second page of the file overall.
   332    421     set ptrsPerPage [expr double($pageSize/5)]
   333    422   
   334    423     # Return the number of pointer map pages in the database.
   335         -  return [expr int(ceil( ($filePages-1.0)/($ptrsPerPage+1.0) ))]
          424  +  return [expr wide(ceil( ($filePages-1.0)/($ptrsPerPage+1.0) ))]
   336    425   }
   337    426   
   338    427   
   339    428   # Calculate the summary statistics for the database and store the results
   340    429   # in TCL variables. They are output below. Variables are as follows:
   341    430   #
   342    431   # pageSize:      Size of each page in bytes.
................................................................................
   355    444   # nindex:        Number of indices in the db.
   356    445   # nautoindex:    Number of indices created automatically.
   357    446   # nmanindex:     Number of indices created manually.
   358    447   # user_payload:  Number of bytes of payload in table btrees 
   359    448   #                (not including sqlite_master)
   360    449   # user_percent:  $user_payload as a percentage of total file size.
   361    450   
   362         -set file_bytes  [file size $file_to_analyze]
   363         -set file_pgcnt  [expr {$file_bytes/$pageSize}]
          451  +### The following, setting $file_bytes based on the actual size of the file
          452  +### on disk, causes this tool to choke on zipvfs databases. So set it based
          453  +### on the return of [PRAGMA page_count] instead.
          454  +if 0 {
          455  +  set file_bytes  [file size $file_to_analyze]
          456  +  set file_pgcnt  [expr {$file_bytes/$pageSize}]
          457  +}
          458  +set file_pgcnt  [db one {PRAGMA page_count}]
          459  +set file_bytes  [expr {$file_pgcnt * $pageSize}]
   364    460   
   365    461   set av_pgcnt    [autovacuum_overhead $file_pgcnt $pageSize]
   366    462   set av_percent  [percent $av_pgcnt $file_pgcnt]
   367    463   
   368    464   set sql {SELECT sum(leaf_pages+int_pages+ovfl_pages) FROM space_used}
   369         -set inuse_pgcnt   [expr int([mem eval $sql])]
          465  +set inuse_pgcnt   [expr wide([mem eval $sql])]
   370    466   set inuse_percent [percent $inuse_pgcnt $file_pgcnt]
   371    467   
   372         -set free_pgcnt    [expr $file_pgcnt-$inuse_pgcnt-$av_pgcnt]
          468  +set free_pgcnt    [expr {$file_pgcnt-$inuse_pgcnt-$av_pgcnt}]
   373    469   set free_percent  [percent $free_pgcnt $file_pgcnt]
   374    470   set free_pgcnt2   [db one {PRAGMA freelist_count}]
   375    471   set free_percent2 [percent $free_pgcnt2 $file_pgcnt]
   376    472   
   377    473   set file_pgcnt2 [expr {$inuse_pgcnt+$free_pgcnt2+$av_pgcnt}]
   378    474   
   379    475   set ntable [db eval {SELECT count(*)+1 FROM sqlite_master WHERE type='table'}]
................................................................................
   401    497   statline {Pages on the freelist (per header)} $free_pgcnt2 $free_percent2
   402    498   statline {Pages on the freelist (calculated)} $free_pgcnt $free_percent
   403    499   statline {Pages of auto-vacuum overhead} $av_pgcnt $av_percent
   404    500   statline {Number of tables in the database} $ntable
   405    501   statline {Number of indices} $nindex
   406    502   statline {Number of named indices} $nmanindex
   407    503   statline {Automatically generated indices} $nautoindex
   408         -statline {Size of the file in bytes} $file_bytes
          504  +if {$isCompressed} {
          505  +  statline {Size of uncompressed content in bytes} $file_bytes
          506  +  set efficiency [percent $true_file_size $file_bytes]
          507  +  statline {Size of compressed file on disk} $true_file_size $efficiency
          508  +} else {
          509  +  statline {Size of the file in bytes} $file_bytes
          510  +}
   409    511   statline {Bytes of user payload stored} $user_payload $user_percent
   410    512   
   411    513   # Output table rankings
   412    514   #
   413    515   puts ""
   414    516   puts "*** Page counts for all tables with their indices ********************"
   415    517   puts ""
   416    518   mem eval {SELECT tblname, count(*) AS cnt, 
   417    519                 int(sum(int_pages+leaf_pages+ovfl_pages)) AS size
   418    520             FROM space_used GROUP BY tblname ORDER BY size+0 DESC, tblname} {} {
   419    521     statline [string toupper $tblname] $size [percent $size $file_pgcnt]
          522  +}
          523  +if {$isCompressed} {
          524  +  puts ""
          525  +  puts "*** Bytes of disk space used after compression ***********************"
          526  +  puts ""
          527  +  set csum 0
          528  +  mem eval {SELECT tblname,
          529  +                  int(sum(compressed_size)) +
          530  +                         $compressOverhead*sum(int_pages+leaf_pages+ovfl_pages)
          531  +                        AS csize
          532  +          FROM space_used GROUP BY tblname ORDER BY csize+0 DESC, tblname} {} {
          533  +    incr csum $csize
          534  +    statline [string toupper $tblname] $csize [percent $csize $true_file_size]
          535  +  }
          536  +  set overhead [expr {$true_file_size - $csum}]
          537  +  if {$overhead>0} {
          538  +    statline {Header and free space} $overhead [percent $overhead $true_file_size]
          539  +  }
   420    540   }
   421    541   
   422    542   # Output subreports
   423    543   #
   424    544   if {$nindex>0} {
   425    545     subreport {All tables and indices} 1
   426    546   }

Changes to tool/warnings.sh.

     5      5   #
     6      6   rm -f sqlite3.c
     7      7   make sqlite3.c-debug
     8      8   echo '********** No optimizations.  Includes FTS4 and RTREE *********'
     9      9   gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
    10     10         -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
    11     11         sqlite3.c
    12         -echo '********** No optimizations. ENABLE_STAT2. THREADSAFE=0 *******'
           12  +echo '********** No optimizations. ENABLE_STAT3. THREADSAFE=0 *******'
    13     13   gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
    14         -      -ansi -DSQLITE_ENABLE_STAT2 -DSQLITE_THREADSAFE=0 \
           14  +      -ansi -DSQLITE_ENABLE_STAT3 -DSQLITE_THREADSAFE=0 \
    15     15         sqlite3.c
    16     16   echo '********** Optimized -O3.  Includes FTS4 and RTREE ************'
    17     17   gcc -O3 -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
    18     18         -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
    19     19         sqlite3.c