/ Check-in [d9f8918c]
Login

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

Overview
Comment:Merge version 3.14 plus some subsequent patches (including the page-cache performance patch) from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA1: d9f8918c5b7b6c8540b3f433142e1b4aa4433885
User & Date: drh 2016-08-11 19:12:25
Wiki:begin-concurrent
Context
2017-01-09
06:33
Upgrade this branch to 3.16 plus the various fixes that appeared after its release. check-in: d0e212d0 user: dan tags: begin-concurrent
2016-08-11
19:12
Merge version 3.14 plus some subsequent patches (including the page-cache performance patch) from trunk. check-in: d9f8918c user: drh tags: begin-concurrent
18:05
Add the "modeof=<filename>" URI parameter to os_unix.c - used to specify a file to copy permissions from when a new database is created. Also allow passing NULL as the second parameter to sqlite3rbu_vacuum(). check-in: ed406d31 user: dan tags: trunk
2016-07-06
08:32
Fix a typo in test program bc_test1.c. check-in: 2c61b7ab user: dan tags: begin-concurrent
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

    78     78   TEMP_STORE = -DSQLITE_TEMP_STORE=@TEMP_STORE@
    79     79   
    80     80   # Enable/disable loadable extensions, and other optional features
    81     81   # based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*).  
    82     82   # The same set of OMIT and ENABLE flags should be passed to the 
    83     83   # LEMON parser generator and the mkkeywordhash tool as well.
    84     84   OPT_FEATURE_FLAGS = @OPT_FEATURE_FLAGS@
    85         -OPT_FEATURE_FLAGS += -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK
    86     85   
    87     86   TCC += $(OPT_FEATURE_FLAGS)
    88     87   
    89     88   # Add in any optional parameters specified on the make commane line
    90     89   # ie.  make "OPTS=-DSQLITE_ENABLE_FOO=1 -DSQLITE_OMIT_FOO=1".
    91     90   TCC += $(OPTS)
    92     91   
................................................................................
   413    412     $(TOP)/ext/session/test_session.c \
   414    413     $(TOP)/ext/rbu/test_rbu.c 
   415    414   
   416    415   # Statically linked extensions
   417    416   #
   418    417   TESTSRC += \
   419    418     $(TOP)/ext/misc/amatch.c \
          419  +  $(TOP)/ext/misc/carray.c \
   420    420     $(TOP)/ext/misc/closure.c \
          421  +  $(TOP)/ext/misc/csv.c \
   421    422     $(TOP)/ext/misc/eval.c \
   422    423     $(TOP)/ext/misc/fileio.c \
   423    424     $(TOP)/ext/misc/fuzzer.c \
   424    425     $(TOP)/ext/fts5/fts5_tcl.c \
   425    426     $(TOP)/ext/fts5/fts5_test_mi.c \
   426    427     $(TOP)/ext/fts5/fts5_test_tok.c \
   427    428     $(TOP)/ext/misc/ieee754.c \
................................................................................
   535    536   
   536    537   # executables needed for testing
   537    538   #
   538    539   TESTPROGS = \
   539    540     testfixture$(TEXE) \
   540    541     sqlite3$(TEXE) \
   541    542     sqlite3_analyzer$(TEXE) \
   542         -  sqldiff$(TEXE)
          543  +  sqldiff$(TEXE) \
          544  +  dbhash$(TEXE)
   543    545   
   544    546   # Databases containing fuzzer test cases
   545    547   #
   546    548   FUZZDATA = \
   547    549     $(TOP)/test/fuzzdata1.db \
   548    550     $(TOP)/test/fuzzdata2.db \
   549    551     $(TOP)/test/fuzzdata3.db \
................................................................................
   552    554   # Standard options to testfixture
   553    555   #
   554    556   TESTOPTS = --verbose=file --output=test-out.txt
   555    557   
   556    558   # Extra compiler options for various shell tools
   557    559   #
   558    560   SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4
          561  +# SHELL_OPT += -DSQLITE_ENABLE_FTS5
   559    562   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
          563  +SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   560    564   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   561    565   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
   562    566   
   563    567   # This is the default Makefile target.  The objects listed here
   564    568   # are what get build when you type just "make" with no arguments.
   565    569   #
   566    570   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
................................................................................
   583    587   		-avoid-version
   584    588   
   585    589   sqlite3$(TEXE):	$(TOP)/src/shell.c sqlite3.c
   586    590   	$(LTLINK) $(READLINE_FLAGS) $(SHELL_OPT) -o $@ \
   587    591   		$(TOP)/src/shell.c sqlite3.c \
   588    592   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   589    593   
   590         -sqldiff$(TEXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
   591         -	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS)
          594  +sqldiff$(TEXE):	$(TOP)/tool/sqldiff.c sqlite3.lo sqlite3.h
          595  +	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.lo $(TLIBS)
          596  +
          597  +dbhash$(TEXE):	$(TOP)/tool/dbhash.c sqlite3.lo sqlite3.h
          598  +	$(LTLINK) -o $@ $(TOP)/tool/dbhash.c sqlite3.lo $(TLIBS)
          599  +
          600  +scrub$(TEXE):	$(TOP)/ext/misc/scrub.c sqlite3.lo
          601  +	$(LTLINK) -o $@ -I. -DSCRUB_STANDALONE \
          602  +		$(TOP)/ext/misc/scrub.c sqlite3.lo $(TLIBS)
   592    603   
   593    604   srcck1$(BEXE):	$(TOP)/tool/srcck1.c
   594    605   	$(BCC) -o srcck1$(BEXE) $(TOP)/tool/srcck1.c
   595    606   
   596    607   sourcetest:	srcck1$(BEXE) sqlite3.c
   597    608   	./srcck1 sqlite3.c
   598    609   
................................................................................
   599    610   fuzzershell$(TEXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h
   600    611   	$(LTLINK) -o $@ $(FUZZERSHELL_OPT) \
   601    612   	  $(TOP)/tool/fuzzershell.c sqlite3.c $(TLIBS)
   602    613   
   603    614   fuzzcheck$(TEXE):	$(TOP)/test/fuzzcheck.c sqlite3.c sqlite3.h
   604    615   	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/fuzzcheck.c sqlite3.c $(TLIBS)
   605    616   
   606         -mptester$(TEXE):	sqlite3.c $(TOP)/mptest/mptest.c
   607         -	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \
          617  +mptester$(TEXE):	sqlite3.lo $(TOP)/mptest/mptest.c
          618  +	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.lo \
   608    619   		$(TLIBS) -rpath "$(libdir)"
   609    620   
   610    621   MPTEST1=./mptester$(TEXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20
   611    622   MPTEST2=./mptester$(TEXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20
   612    623   mptest:	mptester$(TEXE)
   613    624   	rm -f mptest.db
   614    625   	$(MPTEST1) --journalmode DELETE
................................................................................
  1144   1155   
  1145   1156   rollback-test$(TEXE):	$(TOP)/tool/rollback-test.c sqlite3.lo
  1146   1157   	$(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS)
  1147   1158   
  1148   1159   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h
  1149   1160   	$(LTLINK) -I. -o $@ $(TOP)/tool/logest.c
  1150   1161   
  1151         -wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.c
  1152         -	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.c $(TLIBS)
         1162  +wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.lo
         1163  +	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.lo $(TLIBS)
  1153   1164   
  1154   1165   speedtest1$(TEXE):	$(TOP)/test/speedtest1.c sqlite3.lo
  1155   1166   	$(LTLINK) -o $@ $(TOP)/test/speedtest1.c sqlite3.lo $(TLIBS)
  1156   1167   
  1157   1168   rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.lo 
  1158   1169   	$(LTLINK) -I. -o $@ $(TOP)/ext/rbu/rbu.c sqlite3.lo $(TLIBS)
  1159   1170   
................................................................................
  1162   1173   
  1163   1174   # This target will fail if the SQLite amalgamation contains any exported
  1164   1175   # symbols that do not begin with "sqlite3_". It is run as part of the
  1165   1176   # releasetest.tcl script.
  1166   1177   #
  1167   1178   VALIDIDS=' sqlite3(changeset|changegroup|session)?_'
  1168   1179   checksymbols: sqlite3.lo
  1169         -	nm -g --defined-only sqlite3.o | egrep -v $(VALIDIDS); test $$? -ne 0
         1180  +	nm -g --defined-only sqlite3.lo | egrep -v $(VALIDIDS); test $$? -ne 0
  1170   1181   	echo '0 errors out of 1 tests'
  1171   1182   
  1172   1183   # Build the amalgamation-autoconf package.  The amalamgation-tarball target builds
  1173   1184   # a tarball named for the version number.  Ex:  sqlite-autoconf-3110000.tar.gz.
  1174   1185   # The snapshot-tarball target builds a tarball named by the SHA1 hash
  1175   1186   #
  1176   1187   amalgamation-tarball: sqlite3.c
................................................................................
  1243   1254   	rm -f sqlite-*-output.vsix
  1244   1255   	rm -f mptester mptester.exe
  1245   1256   	rm -f rbu rbu.exe
  1246   1257   	rm -f srcck1 srcck1.exe
  1247   1258   	rm -f fuzzershell fuzzershell.exe
  1248   1259   	rm -f fuzzcheck fuzzcheck.exe
  1249   1260   	rm -f sqldiff sqldiff.exe
         1261  +	rm -f dbhash dbhash.exe
  1250   1262   	rm -f fts5.* fts5parse.*
  1251   1263   
  1252   1264   distclean:	clean
  1253   1265   	rm -f config.h config.log config.status libtool Makefile sqlite3.pc
  1254   1266   
  1255   1267   #
  1256   1268   # Windows section

Changes to Makefile.msc.

   193    193   # Enable use of available compiler optimizations?  Normally, this should be
   194    194   # non-zero.  Setting this to zero, thus disabling all compiler optimizations,
   195    195   # can be useful for testing.
   196    196   #
   197    197   !IFNDEF OPTIMIZATIONS
   198    198   OPTIMIZATIONS = 2
   199    199   !ENDIF
          200  +
          201  +# Set this to non-0 to enable support for the session extension.
          202  +#
          203  +!IFNDEF SESSION
          204  +SESSION = 0
          205  +!ENDIF
   200    206   
   201    207   # Set the source code file to be used by executables and libraries when
   202    208   # they need the amalgamation.
   203    209   #
   204    210   !IFNDEF SQLITE3C
   205    211   !IF $(SPLIT_AMALGAMATION)!=0
   206    212   SQLITE3C = sqlite3-all.c
................................................................................
   260    266   # These are the "standard" SQLite compilation options used when compiling for
   261    267   # the Windows platform.
   262    268   #
   263    269   !IFNDEF OPT_FEATURE_FLAGS
   264    270   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1
   265    271   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1
   266    272   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1
          273  +!ENDIF
          274  +
          275  +# Should the session extension be enabled?  If so, add compilation options
          276  +# to enable it.
          277  +#
          278  +!IF $(SESSION)!=0
   267    279   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION=1
   268    280   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK=1
   269    281   !ENDIF
   270    282   
   271    283   # These are the "extended" SQLite compilation options used when compiling for
   272    284   # the Windows 10 platform.
   273    285   #
................................................................................
  1276   1288     $(TOP)\ext\rbu\test_rbu.c \
  1277   1289     $(TOP)\ext\session\test_session.c
  1278   1290   
  1279   1291   # Statically linked extensions.
  1280   1292   #
  1281   1293   TESTEXT = \
  1282   1294     $(TOP)\ext\misc\amatch.c \
         1295  +  $(TOP)\ext\misc\carray.c \
  1283   1296     $(TOP)\ext\misc\closure.c \
         1297  +  $(TOP)\ext\misc\csv.c \
  1284   1298     $(TOP)\ext\misc\eval.c \
  1285   1299     $(TOP)\ext\misc\fileio.c \
  1286   1300     $(TOP)\ext\misc\fuzzer.c \
  1287   1301     $(TOP)\ext\fts5\fts5_tcl.c \
  1288   1302     $(TOP)\ext\fts5\fts5_test_mi.c \
  1289   1303     $(TOP)\ext\fts5\fts5_test_tok.c \
  1290   1304     $(TOP)\ext\misc\ieee754.c \
................................................................................
  1361   1375   
  1362   1376   # executables needed for testing
  1363   1377   #
  1364   1378   TESTPROGS = \
  1365   1379     testfixture.exe \
  1366   1380     $(SQLITE3EXE) \
  1367   1381     sqlite3_analyzer.exe \
  1368         -  sqldiff.exe
         1382  +  sqldiff.exe \
         1383  +  dbhash.exe
  1369   1384   
  1370   1385   # Databases containing fuzzer test cases
  1371   1386   #
  1372   1387   FUZZDATA = \
  1373   1388     $(TOP)\test\fuzzdata1.db \
  1374   1389     $(TOP)\test\fuzzdata2.db \
  1375   1390     $(TOP)\test\fuzzdata3.db \
................................................................................
  1439   1454   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c $(SHELL_CORE_SRC) \
  1440   1455   		/link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1441   1456   
  1442   1457   # <<mark>>
  1443   1458   sqldiff.exe:	$(TOP)\tool\sqldiff.c $(SQLITE3C) $(SQLITE3H)
  1444   1459   	$(LTLINK) $(NO_WARN) $(TOP)\tool\sqldiff.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1445   1460   
         1461  +dbhash.exe:	$(TOP)\tool\dbhash.c $(SQLITE3C) $(SQLITE3H)
         1462  +	$(LTLINK) $(NO_WARN) $(TOP)\tool\dbhash.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         1463  +
         1464  +scrub.exe:	$(TOP)\ext\misc\scrub.c $(SQLITE3C) $(SQLITE3H)
         1465  +	$(LTLINK) $(NO_WARN) $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         1466  +
  1446   1467   srcck1.exe:	$(TOP)\tool\srcck1.c
  1447   1468   	$(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\srcck1.c
  1448   1469   
  1449   1470   sourcetest:	srcck1.exe sqlite3.c
  1450   1471   	srcck1.exe sqlite3.c
  1451   1472   
  1452   1473   fuzzershell.exe:	$(TOP)\tool\fuzzershell.c $(SQLITE3C) $(SQLITE3H)
................................................................................
  2085   2106   	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL
  2086   2107   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2087   2108   	del /Q sqlite3.c sqlite3-*.c 2>NUL
  2088   2109   	del /Q sqlite3rc.h 2>NUL
  2089   2110   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2090   2111   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2091   2112   	del /Q sqlite-*-output.vsix 2>NUL
  2092         -	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe 2>NUL
         2113  +	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2093   2114   	del /Q fts5.* fts5parse.* 2>NUL
  2094   2115   # <</mark>>

Changes to VERSION.

     1         -3.13.0
            1  +3.15.0

Changes to autoconf/Makefile.am.

     1      1   
     2         -AM_CFLAGS = @THREADSAFE_FLAGS@ @DYNAMIC_EXTENSION_FLAGS@ @FTS5_FLAGS@ @JSON1_FLAGS@ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE
            2  +AM_CFLAGS = @THREADSAFE_FLAGS@ @DYNAMIC_EXTENSION_FLAGS@ @FTS5_FLAGS@ @JSON1_FLAGS@ @SESSION_FLAGS@ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE
     3      3   
     4      4   lib_LTLIBRARIES = libsqlite3.la
     5      5   libsqlite3_la_SOURCES = sqlite3.c
     6      6   libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8
     7      7   
     8      8   bin_PROGRAMS = sqlite3
     9      9   sqlite3_SOURCES = shell.c sqlite3.h

Changes to autoconf/Makefile.msc.

    19     19   
    20     20   
    21     21   # Set this non-0 to enable full warnings (-W4, etc) when compiling.
    22     22   #
    23     23   !IFNDEF USE_FULLWARN
    24     24   USE_FULLWARN = 0
    25     25   !ENDIF
           26  +
           27  +# Set this non-0 to enable full runtime error checks (-RTC1, etc).  This
           28  +# has no effect if (any) optimizations are enabled.
           29  +#
           30  +!IFNDEF USE_RUNTIME_CHECKS
           31  +USE_RUNTIME_CHECKS = 0
           32  +!ENDIF
    26     33   
    27     34   # Set this non-0 to use "stdcall" calling convention for the core library
    28     35   # and shell executable.
    29     36   #
    30     37   !IFNDEF USE_STDCALL
    31     38   USE_STDCALL = 0
    32     39   !ENDIF
................................................................................
   178    185   # Enable use of available compiler optimizations?  Normally, this should be
   179    186   # non-zero.  Setting this to zero, thus disabling all compiler optimizations,
   180    187   # can be useful for testing.
   181    188   #
   182    189   !IFNDEF OPTIMIZATIONS
   183    190   OPTIMIZATIONS = 2
   184    191   !ENDIF
          192  +
          193  +# Set this to non-0 to enable support for the session extension.
          194  +#
          195  +!IFNDEF SESSION
          196  +SESSION = 0
          197  +!ENDIF
   185    198   
   186    199   # Set the source code file to be used by executables and libraries when
   187    200   # they need the amalgamation.
   188    201   #
   189    202   !IFNDEF SQLITE3C
   190    203   !IF $(SPLIT_AMALGAMATION)!=0
   191    204   SQLITE3C = sqlite3-all.c
................................................................................
   245    258   # These are the "standard" SQLite compilation options used when compiling for
   246    259   # the Windows platform.
   247    260   #
   248    261   !IFNDEF OPT_FEATURE_FLAGS
   249    262   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1
   250    263   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1
   251    264   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1
          265  +!ENDIF
          266  +
          267  +# Should the session extension be enabled?  If so, add compilation options
          268  +# to enable it.
          269  +#
          270  +!IF $(SESSION)!=0
   252    271   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION=1
   253    272   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK=1
   254    273   !ENDIF
   255    274   
   256    275   # These are the "extended" SQLite compilation options used when compiling for
   257    276   # the Windows 10 platform.
   258    277   #
................................................................................
   433    452   # also be noted here that building any target with these "stdcall" options
   434    453   # will most likely fail if the Tcl library is also required.  This is due
   435    454   # to how the Tcl library functions are declared and exported (i.e. without
   436    455   # an explicit calling convention, which results in "cdecl").
   437    456   #
   438    457   !IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0
   439    458   !IF "$(PLATFORM)"=="x86"
   440         -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
   441         -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
          459  +CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall
          460  +SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall
   442    461   !ELSE
   443    462   !IFNDEF PLATFORM
   444         -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
   445         -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall
          463  +CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall
          464  +SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall
   446    465   !ELSE
   447    466   CORE_CCONV_OPTS =
   448    467   SHELL_CCONV_OPTS =
   449    468   !ENDIF
   450    469   !ENDIF
   451    470   !ELSE
   452    471   CORE_CCONV_OPTS =
................................................................................
   724    743   
   725    744   # If optimizations are enabled or disabled (either implicitly or
   726    745   # explicitly), add the necessary flags.
   727    746   #
   728    747   !IF $(DEBUG)>1 || $(OPTIMIZATIONS)==0
   729    748   TCC = $(TCC) -Od
   730    749   BCC = $(BCC) -Od
          750  +!IF $(USE_RUNTIME_CHECKS)!=0
          751  +TCC = $(TCC) -RTC1
          752  +BCC = $(BCC) -RTC1
          753  +!ENDIF
   731    754   !ELSEIF $(OPTIMIZATIONS)>=3
   732    755   TCC = $(TCC) -Ox
   733    756   BCC = $(BCC) -Ox
   734    757   !ELSEIF $(OPTIMIZATIONS)==2
   735    758   TCC = $(TCC) -O2
   736    759   BCC = $(BCC) -O2
   737    760   !ELSEIF $(OPTIMIZATIONS)==1

Changes to autoconf/configure.ac.

    26     26   # Check for library functions that SQLite can optionally use.
    27     27   AC_CHECK_FUNCS([fdatasync usleep fullfsync localtime_r gmtime_r])
    28     28   AC_FUNC_STRERROR_R
    29     29   
    30     30   AC_CONFIG_FILES([Makefile sqlite3.pc])
    31     31   AC_SUBST(BUILD_CFLAGS)
    32     32   
    33         -#-----------------------------------------------------------------------
           33  +#-------------------------------------------------------------------------
           34  +# Two options to enable readline compatible libraries: 
           35  +#
    34     36   #   --enable-editline
    35     37   #   --enable-readline
    36     38   #
    37         -AC_ARG_ENABLE(editline, [AS_HELP_STRING(
    38         -  [--enable-editline], 
    39         -  [use BSD libedit])], 
    40         -  [], [enable_editline=yes])
    41         -AC_ARG_ENABLE(readline, [AS_HELP_STRING(
    42         -  [--enable-readline], 
    43         -  [use readline])], 
    44         -  [], [enable_readline=no])
    45         -if test x"$enable_editline" != xno ; then
    46         -  sLIBS=$LIBS
    47         -  LIBS=""
    48         -  AC_SEARCH_LIBS([readline],[edit],[enable_readline=no],[enable_editline=no])
    49         -  READLINE_LIBS=$LIBS
    50         -  if test x"$LIBS" != "x"; then
    51         -     AC_DEFINE([HAVE_EDITLINE],1,Define to use BSD editline)
    52         -  else
    53         -    unset ac_cv_search_readline
    54         -  fi
    55         -  LIBS=$sLIBS
    56         -fi
    57         -if test x"$enable_readline" != xno ; then
    58         -  sLIBS=$LIBS
    59         -  LIBS=""
    60         -  AC_SEARCH_LIBS(tgetent, curses ncurses ncursesw, [], [])
    61         -  AC_SEARCH_LIBS(readline, readline, [], [enable_readline=no])
    62         -  AC_CHECK_FUNCS(readline, [], [])
    63         -  READLINE_LIBS=$LIBS
    64         -  LIBS=$sLIBS
    65         -fi
           39  +# Both are enabled by default. If, after command line processing both are
           40  +# still enabled, the script searches for editline first and automatically
           41  +# disables readline if it is found. So, to use readline explicitly, the
           42  +# user must pass "--disable-editline". To disable command line editing
           43  +# support altogether, "--disable-editline --disable-readline".
           44  +#
           45  +# When searching for either library, check for headers before libraries 
           46  +# as some distros supply packages that contain libraries but not header
           47  +# files, which come as a separate development package.
           48  +#
           49  +AC_ARG_ENABLE(editline, [AS_HELP_STRING([--enable-editline],[use BSD libedit])])
           50  +AC_ARG_ENABLE(readline, [AS_HELP_STRING([--enable-readline],[use readline])])
           51  +
           52  +AS_IF([ test x"$enable_editline" != xno ],[
           53  +  AC_CHECK_HEADERS([editline/readline.h],[
           54  +    sLIBS=$LIBS
           55  +    LIBS=""
           56  +    AC_SEARCH_LIBS([readline],[edit],[
           57  +      AC_DEFINE([HAVE_EDITLINE],1,Define to use BSD editline)
           58  +      READLINE_LIBS=$LIBS
           59  +      enable_readline=no
           60  +    ])
           61  +    AS_UNSET(ac_cv_search_readline)
           62  +    LIBS=$sLIBS
           63  +  ])
           64  +])
           65  +
           66  +AS_IF([ test x"$enable_readline" != xno ],[
           67  +  AC_CHECK_HEADERS([readline/readline.h],[
           68  +    sLIBS=$LIBS
           69  +    LIBS=""
           70  +    AC_SEARCH_LIBS(tgetent, termcap curses ncurses ncursesw, [], [])
           71  +    AC_SEARCH_LIBS(readline,[readline edit], [
           72  +      AC_DEFINE([HAVE_READLINE],1,Define to use readline or wrapper)
           73  +      READLINE_LIBS=$LIBS
           74  +    ])
           75  +    LIBS=$sLIBS
           76  +  ])
           77  +])
           78  +
    66     79   AC_SUBST(READLINE_LIBS)
    67     80   #-----------------------------------------------------------------------
    68     81   
    69     82   #-----------------------------------------------------------------------
    70     83   #   --enable-threadsafe
    71     84   #
    72     85   AC_ARG_ENABLE(threadsafe, [AS_HELP_STRING(
................................................................................
    99    112   
   100    113   #-----------------------------------------------------------------------
   101    114   #   --enable-fts5
   102    115   #
   103    116   AC_ARG_ENABLE(fts5, [AS_HELP_STRING(
   104    117     [--enable-fts5], [include fts5 support [default=no]])], 
   105    118     [], [enable_fts5=no])
   106         -if test x"$enable_fts5" == "xyes"; then
          119  +if test x"$enable_fts5" = "xyes"; then
   107    120     AC_SEARCH_LIBS(log, m)
   108    121     FTS5_FLAGS=-DSQLITE_ENABLE_FTS5
   109    122   fi
   110    123   AC_SUBST(FTS5_FLAGS)
   111    124   #-----------------------------------------------------------------------
   112    125   
   113    126   #-----------------------------------------------------------------------
   114    127   #   --enable-json1
   115    128   #
   116    129   AC_ARG_ENABLE(json1, [AS_HELP_STRING(
   117    130     [--enable-json1], [include json1 support [default=no]])], 
   118    131     [], [enable_json1=no])
   119         -if test x"$enable_json1" == "xyes"; then
          132  +if test x"$enable_json1" = "xyes"; then
   120    133     JSON1_FLAGS=-DSQLITE_ENABLE_JSON1
   121    134   fi
   122    135   AC_SUBST(JSON1_FLAGS)
   123    136   #-----------------------------------------------------------------------
          137  +
          138  +#-----------------------------------------------------------------------
          139  +#   --enable-session
          140  +#
          141  +AC_ARG_ENABLE(session, [AS_HELP_STRING(
          142  +  [--enable-session], [enable the session extension [default=no]])], 
          143  +  [], [enable_session=no])
          144  +if test x"$enable_session" = "xyes"; then
          145  +  SESSION_FLAGS="-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK"
          146  +fi
          147  +AC_SUBST(SESSION_FLAGS)
          148  +#-----------------------------------------------------------------------
   124    149   
   125    150   #-----------------------------------------------------------------------
   126    151   #   --enable-static-shell
   127    152   #
   128    153   AC_ARG_ENABLE(static-shell, [AS_HELP_STRING(
   129    154     [--enable-static-shell], 
   130    155     [statically link libsqlite3 into shell tool [default=yes]])], 
   131    156     [], [enable_static_shell=yes])
   132         -if test x"$enable_static_shell" == "xyes"; then
          157  +if test x"$enable_static_shell" = "xyes"; then
   133    158     EXTRA_SHELL_OBJ=sqlite3-sqlite3.$OBJEXT
   134    159   else
   135    160     EXTRA_SHELL_OBJ=libsqlite3.la
   136    161   fi
   137    162   AC_SUBST(EXTRA_SHELL_OBJ)
   138    163   #-----------------------------------------------------------------------
   139    164   

Changes to autoconf/tea/configure.ac.

    74     74   TEA_ADD_SOURCES([tclsqlite3.c])
    75     75   TEA_ADD_HEADERS([])
    76     76   TEA_ADD_INCLUDES([-I\"`\${CYGPATH} \${srcdir}/generic`\"])
    77     77   TEA_ADD_LIBS([])
    78     78   TEA_ADD_CFLAGS([-DSQLITE_ENABLE_FTS3=1])
    79     79   TEA_ADD_CFLAGS([-DSQLITE_3_SUFFIX_ONLY=1])
    80     80   TEA_ADD_CFLAGS([-DSQLITE_ENABLE_RTREE=1])
    81         -TEA_ADD_CFLAGS([-DSQLITE_OMIT_DEPRECATED=1])
    82     81   TEA_ADD_STUB_SOURCES([])
    83     82   TEA_ADD_TCL_SOURCES([])
    84     83   
    85     84   #--------------------------------------------------------------------
    86     85   # The --with-system-sqlite causes the TCL bindings to SQLite to use
    87     86   # the system shared library for SQLite rather than statically linking
    88     87   # against its own private copy.  This is dangerous and leads to

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.69 for sqlite 3.13.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.15.0.
     4      4   #
     5      5   #
     6      6   # Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
     7      7   #
     8      8   #
     9      9   # This configure script is free software; the Free Software Foundation
    10     10   # gives unlimited permission to copy, distribute and modify it.
................................................................................
   722    722   subdirs=
   723    723   MFLAGS=
   724    724   MAKEFLAGS=
   725    725   
   726    726   # Identity of this package.
   727    727   PACKAGE_NAME='sqlite'
   728    728   PACKAGE_TARNAME='sqlite'
   729         -PACKAGE_VERSION='3.13.0'
   730         -PACKAGE_STRING='sqlite 3.13.0'
          729  +PACKAGE_VERSION='3.15.0'
          730  +PACKAGE_STRING='sqlite 3.15.0'
   731    731   PACKAGE_BUGREPORT=''
   732    732   PACKAGE_URL=''
   733    733   
   734    734   # Factoring default headers for most tests.
   735    735   ac_includes_default="\
   736    736   #include <stdio.h>
   737    737   #ifdef HAVE_SYS_TYPES_H
................................................................................
   899    899   enable_editline
   900    900   enable_readline
   901    901   with_readline_lib
   902    902   with_readline_inc
   903    903   enable_debug
   904    904   enable_amalgamation
   905    905   enable_load_extension
          906  +enable_memsys5
          907  +enable_memsys3
   906    908   enable_fts3
   907    909   enable_fts4
   908    910   enable_fts5
   909    911   enable_json1
   910    912   enable_rtree
          913  +enable_session
   911    914   enable_gcov
   912    915   '
   913    916         ac_precious_vars='build_alias
   914    917   host_alias
   915    918   target_alias
   916    919   CC
   917    920   CFLAGS
................................................................................
  1456   1459   #
  1457   1460   # Report the --help message.
  1458   1461   #
  1459   1462   if test "$ac_init_help" = "long"; then
  1460   1463     # Omit some internal or obsolete options to make the list less imposing.
  1461   1464     # This message is too long to be a string in the A/UX 3.1 sh.
  1462   1465     cat <<_ACEOF
  1463         -\`configure' configures sqlite 3.13.0 to adapt to many kinds of systems.
         1466  +\`configure' configures sqlite 3.15.0 to adapt to many kinds of systems.
  1464   1467   
  1465   1468   Usage: $0 [OPTION]... [VAR=VALUE]...
  1466   1469   
  1467   1470   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1468   1471   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1469   1472   
  1470   1473   Defaults for the options are specified in brackets.
................................................................................
  1521   1524     --build=BUILD     configure for building on BUILD [guessed]
  1522   1525     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1523   1526   _ACEOF
  1524   1527   fi
  1525   1528   
  1526   1529   if test -n "$ac_init_help"; then
  1527   1530     case $ac_init_help in
  1528         -     short | recursive ) echo "Configuration of sqlite 3.13.0:";;
         1531  +     short | recursive ) echo "Configuration of sqlite 3.15.0:";;
  1529   1532      esac
  1530   1533     cat <<\_ACEOF
  1531   1534   
  1532   1535   Optional Features:
  1533   1536     --disable-option-checking  ignore unrecognized --enable/--with options
  1534   1537     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1535   1538     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1547   1550     --enable-editline       enable BSD editline support
  1548   1551     --disable-readline      disable readline support
  1549   1552     --enable-debug          enable debugging & verbose explain
  1550   1553     --disable-amalgamation  Disable the amalgamation and instead build all files
  1551   1554                             separately
  1552   1555     --disable-load-extension
  1553   1556                             Disable loading of external extensions
         1557  +  --enable-memsys5        Enable MEMSYS5
         1558  +  --enable-memsys3        Enable MEMSYS3
  1554   1559     --enable-fts3           Enable the FTS3 extension
  1555   1560     --enable-fts4           Enable the FTS4 extension
  1556   1561     --enable-fts5           Enable the FTS5 extension
  1557   1562     --enable-json1          Enable the JSON1 extension
  1558   1563     --enable-rtree          Enable the RTREE extension
         1564  +  --enable-session        Enable the SESSION extension
  1559   1565     --enable-gcov           Enable coverage testing using gcov
  1560   1566   
  1561   1567   Optional Packages:
  1562   1568     --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  1563   1569     --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  1564   1570     --with-pic              try to use only PIC/non-PIC objects [default=use
  1565   1571                             both]
................................................................................
  1642   1648       cd "$ac_pwd" || { ac_status=$?; break; }
  1643   1649     done
  1644   1650   fi
  1645   1651   
  1646   1652   test -n "$ac_init_help" && exit $ac_status
  1647   1653   if $ac_init_version; then
  1648   1654     cat <<\_ACEOF
  1649         -sqlite configure 3.13.0
         1655  +sqlite configure 3.15.0
  1650   1656   generated by GNU Autoconf 2.69
  1651   1657   
  1652   1658   Copyright (C) 2012 Free Software Foundation, Inc.
  1653   1659   This configure script is free software; the Free Software Foundation
  1654   1660   gives unlimited permission to copy, distribute and modify it.
  1655   1661   _ACEOF
  1656   1662     exit
................................................................................
  2061   2067     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2062   2068   
  2063   2069   } # ac_fn_c_check_header_mongrel
  2064   2070   cat >config.log <<_ACEOF
  2065   2071   This file contains any messages produced by compilers while
  2066   2072   running configure, to aid debugging if configure makes a mistake.
  2067   2073   
  2068         -It was created by sqlite $as_me 3.13.0, which was
         2074  +It was created by sqlite $as_me 3.15.0, which was
  2069   2075   generated by GNU Autoconf 2.69.  Invocation command line was
  2070   2076   
  2071   2077     $ $0 $@
  2072   2078   
  2073   2079   _ACEOF
  2074   2080   exec 5>>config.log
  2075   2081   {
................................................................................
  3919   3925   { $as_echo "$as_me:${as_lineno-$LINENO}: checking the name lister ($NM) interface" >&5
  3920   3926   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3921   3927   if ${lt_cv_nm_interface+:} false; then :
  3922   3928     $as_echo_n "(cached) " >&6
  3923   3929   else
  3924   3930     lt_cv_nm_interface="BSD nm"
  3925   3931     echo "int some_variable = 0;" > conftest.$ac_ext
  3926         -  (eval echo "\"\$as_me:3926: $ac_compile\"" >&5)
         3932  +  (eval echo "\"\$as_me:3932: $ac_compile\"" >&5)
  3927   3933     (eval "$ac_compile" 2>conftest.err)
  3928   3934     cat conftest.err >&5
  3929         -  (eval echo "\"\$as_me:3929: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3935  +  (eval echo "\"\$as_me:3935: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3930   3936     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3931   3937     cat conftest.err >&5
  3932         -  (eval echo "\"\$as_me:3932: output\"" >&5)
         3938  +  (eval echo "\"\$as_me:3938: output\"" >&5)
  3933   3939     cat conftest.out >&5
  3934   3940     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3935   3941       lt_cv_nm_interface="MS dumpbin"
  3936   3942     fi
  3937   3943     rm -f conftest*
  3938   3944   fi
  3939   3945   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_nm_interface" >&5
................................................................................
  5131   5137   	;;
  5132   5138       esac
  5133   5139     fi
  5134   5140     rm -rf conftest*
  5135   5141     ;;
  5136   5142   *-*-irix6*)
  5137   5143     # Find out which ABI we are using.
  5138         -  echo '#line 5138 "configure"' > conftest.$ac_ext
         5144  +  echo '#line 5144 "configure"' > conftest.$ac_ext
  5139   5145     if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
  5140   5146     (eval $ac_compile) 2>&5
  5141   5147     ac_status=$?
  5142   5148     $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
  5143   5149     test $ac_status = 0; }; then
  5144   5150       if test "$lt_cv_prog_gnu_ld" = yes; then
  5145   5151         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6656   6662      # Note that $ac_compile itself does not contain backslashes and begins
  6657   6663      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6658   6664      # The option is referenced via a variable to avoid confusing sed.
  6659   6665      lt_compile=`echo "$ac_compile" | $SED \
  6660   6666      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6661   6667      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6662   6668      -e 's:$: $lt_compiler_flag:'`
  6663         -   (eval echo "\"\$as_me:6663: $lt_compile\"" >&5)
         6669  +   (eval echo "\"\$as_me:6669: $lt_compile\"" >&5)
  6664   6670      (eval "$lt_compile" 2>conftest.err)
  6665   6671      ac_status=$?
  6666   6672      cat conftest.err >&5
  6667         -   echo "$as_me:6667: \$? = $ac_status" >&5
         6673  +   echo "$as_me:6673: \$? = $ac_status" >&5
  6668   6674      if (exit $ac_status) && test -s "$ac_outfile"; then
  6669   6675        # The compiler can only warn and ignore the option if not recognized
  6670   6676        # So say no if there are warnings other than the usual output.
  6671   6677        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6672   6678        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6673   6679        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6674   6680          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  6995   7001      # Note that $ac_compile itself does not contain backslashes and begins
  6996   7002      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6997   7003      # The option is referenced via a variable to avoid confusing sed.
  6998   7004      lt_compile=`echo "$ac_compile" | $SED \
  6999   7005      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7000   7006      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7001   7007      -e 's:$: $lt_compiler_flag:'`
  7002         -   (eval echo "\"\$as_me:7002: $lt_compile\"" >&5)
         7008  +   (eval echo "\"\$as_me:7008: $lt_compile\"" >&5)
  7003   7009      (eval "$lt_compile" 2>conftest.err)
  7004   7010      ac_status=$?
  7005   7011      cat conftest.err >&5
  7006         -   echo "$as_me:7006: \$? = $ac_status" >&5
         7012  +   echo "$as_me:7012: \$? = $ac_status" >&5
  7007   7013      if (exit $ac_status) && test -s "$ac_outfile"; then
  7008   7014        # The compiler can only warn and ignore the option if not recognized
  7009   7015        # So say no if there are warnings other than the usual output.
  7010   7016        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7011   7017        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7012   7018        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7013   7019          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7100   7106      # (2) before a word containing "conftest.", or (3) at the end.
  7101   7107      # Note that $ac_compile itself does not contain backslashes and begins
  7102   7108      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7103   7109      lt_compile=`echo "$ac_compile" | $SED \
  7104   7110      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7105   7111      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7106   7112      -e 's:$: $lt_compiler_flag:'`
  7107         -   (eval echo "\"\$as_me:7107: $lt_compile\"" >&5)
         7113  +   (eval echo "\"\$as_me:7113: $lt_compile\"" >&5)
  7108   7114      (eval "$lt_compile" 2>out/conftest.err)
  7109   7115      ac_status=$?
  7110   7116      cat out/conftest.err >&5
  7111         -   echo "$as_me:7111: \$? = $ac_status" >&5
         7117  +   echo "$as_me:7117: \$? = $ac_status" >&5
  7112   7118      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7113   7119      then
  7114   7120        # The compiler can only warn and ignore the option if not recognized
  7115   7121        # So say no if there are warnings
  7116   7122        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7117   7123        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7118   7124        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7155   7161      # (2) before a word containing "conftest.", or (3) at the end.
  7156   7162      # Note that $ac_compile itself does not contain backslashes and begins
  7157   7163      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7158   7164      lt_compile=`echo "$ac_compile" | $SED \
  7159   7165      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7160   7166      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7161   7167      -e 's:$: $lt_compiler_flag:'`
  7162         -   (eval echo "\"\$as_me:7162: $lt_compile\"" >&5)
         7168  +   (eval echo "\"\$as_me:7168: $lt_compile\"" >&5)
  7163   7169      (eval "$lt_compile" 2>out/conftest.err)
  7164   7170      ac_status=$?
  7165   7171      cat out/conftest.err >&5
  7166         -   echo "$as_me:7166: \$? = $ac_status" >&5
         7172  +   echo "$as_me:7172: \$? = $ac_status" >&5
  7167   7173      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7168   7174      then
  7169   7175        # The compiler can only warn and ignore the option if not recognized
  7170   7176        # So say no if there are warnings
  7171   7177        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7172   7178        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7173   7179        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  9535   9541   else
  9536   9542     	  if test "$cross_compiling" = yes; then :
  9537   9543     lt_cv_dlopen_self=cross
  9538   9544   else
  9539   9545     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9540   9546     lt_status=$lt_dlunknown
  9541   9547     cat > conftest.$ac_ext <<_LT_EOF
  9542         -#line 9542 "configure"
         9548  +#line 9548 "configure"
  9543   9549   #include "confdefs.h"
  9544   9550   
  9545   9551   #if HAVE_DLFCN_H
  9546   9552   #include <dlfcn.h>
  9547   9553   #endif
  9548   9554   
  9549   9555   #include <stdio.h>
................................................................................
  9631   9637   else
  9632   9638     	  if test "$cross_compiling" = yes; then :
  9633   9639     lt_cv_dlopen_self_static=cross
  9634   9640   else
  9635   9641     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9636   9642     lt_status=$lt_dlunknown
  9637   9643     cat > conftest.$ac_ext <<_LT_EOF
  9638         -#line 9638 "configure"
         9644  +#line 9644 "configure"
  9639   9645   #include "confdefs.h"
  9640   9646   
  9641   9647   #if HAVE_DLFCN_H
  9642   9648   #include <dlfcn.h>
  9643   9649   #endif
  9644   9650   
  9645   9651   #include <stdio.h>
................................................................................
 10750  10756             if test -f "$i/tclConfig.sh" ; then
 10751  10757               ac_cv_c_tclconfig="$i"
 10752  10758               break
 10753  10759             fi
 10754  10760           done
 10755  10761         fi
 10756  10762       fi
        10763  +
        10764  +    # Recent versions of Xcode on Macs hid the tclConfig.sh file
        10765  +    # in a strange place.
        10766  +    if test x"${ac_cv_c_tclconfig}" = x ; then
        10767  +      if test x"$cross_compiling" = xno; then
        10768  +        for i in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX*.sdk/usr/lib
        10769  +        do
        10770  +          if test -f "$i/tclConfig.sh" ; then
        10771  +            ac_cv_c_tclconfig="$i"
        10772  +            break
        10773  +          fi
        10774  +        done
        10775  +      fi
        10776  +    fi
 10757  10777   
 10758  10778       # then check for a private Tcl installation
 10759  10779       if test x"${ac_cv_c_tclconfig}" = x ; then
 10760  10780         for i in \
 10761  10781               ../tcl \
 10762  10782               `ls -dr ../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \
 10763  10783               `ls -dr ../tcl[8-9].[0-9] 2>/dev/null` \
................................................................................
 11318  11338     test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
 11319  11339   
 11320  11340   fi
 11321  11341   
 11322  11342   else
 11323  11343     OPT_FEATURE_FLAGS="-DSQLITE_OMIT_LOAD_EXTENSION=1"
 11324  11344   fi
        11345  +
        11346  +##########
        11347  +# Do we want to support memsys3 and/or memsys5
        11348  +#
        11349  +# Check whether --enable-memsys5 was given.
        11350  +if test "${enable_memsys5+set}" = set; then :
        11351  +  enableval=$enable_memsys5; enable_memsys5=yes
        11352  +else
        11353  +  enable_memsys5=no
        11354  +fi
        11355  +
        11356  +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS5" >&5
        11357  +$as_echo_n "checking whether to support MEMSYS5... " >&6; }
        11358  +if test "${enable_memsys5}" = "yes"; then
        11359  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS5"
        11360  +  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
        11361  +$as_echo "yes" >&6; }
        11362  +else
        11363  +  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
        11364  +$as_echo "no" >&6; }
        11365  +fi
        11366  +# Check whether --enable-memsys3 was given.
        11367  +if test "${enable_memsys3+set}" = set; then :
        11368  +  enableval=$enable_memsys3; enable_memsys3=yes
        11369  +else
        11370  +  enable_memsys3=no
        11371  +fi
        11372  +
        11373  +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS3" >&5
        11374  +$as_echo_n "checking whether to support MEMSYS3... " >&6; }
        11375  +if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
        11376  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS3"
        11377  +  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
        11378  +$as_echo "yes" >&6; }
        11379  +else
        11380  +  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
        11381  +$as_echo "no" >&6; }
        11382  +fi
 11325  11383   
 11326  11384   #########
 11327  11385   # See whether we should enable Full Text Search extensions
 11328  11386   # Check whether --enable-fts3 was given.
 11329  11387   if test "${enable_fts3+set}" = set; then :
 11330  11388     enableval=$enable_fts3; enable_fts3=yes
 11331  11389   else
................................................................................
 11489  11547   else
 11490  11548     enable_rtree=no
 11491  11549   fi
 11492  11550   
 11493  11551   if test "${enable_rtree}" = "yes" ; then
 11494  11552     OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE"
 11495  11553   fi
        11554  +
        11555  +#########
        11556  +# See whether we should enable the SESSION extension
        11557  +# Check whether --enable-session was given.
        11558  +if test "${enable_session+set}" = set; then :
        11559  +  enableval=$enable_session; enable_session=yes
        11560  +else
        11561  +  enable_session=no
        11562  +fi
        11563  +
        11564  +if test "${enable_session}" = "yes" ; then
        11565  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_SESSION"
        11566  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_PREUPDATE_HOOK"
        11567  +fi
 11496  11568   
 11497  11569   #########
 11498  11570   # attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter
 11499  11571   for option in $CFLAGS $CPPFLAGS
 11500  11572   do
 11501  11573     case $option in
 11502  11574       -DSQLITE_OMIT*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";;
................................................................................
 12075  12147   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12076  12148   
 12077  12149   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12078  12150   # Save the log message, to keep $0 and so on meaningful, and to
 12079  12151   # report actual input values of CONFIG_FILES etc. instead of their
 12080  12152   # values after options handling.
 12081  12153   ac_log="
 12082         -This file was extended by sqlite $as_me 3.13.0, which was
        12154  +This file was extended by sqlite $as_me 3.15.0, which was
 12083  12155   generated by GNU Autoconf 2.69.  Invocation command line was
 12084  12156   
 12085  12157     CONFIG_FILES    = $CONFIG_FILES
 12086  12158     CONFIG_HEADERS  = $CONFIG_HEADERS
 12087  12159     CONFIG_LINKS    = $CONFIG_LINKS
 12088  12160     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12089  12161     $ $0 $@
................................................................................
 12141  12213   
 12142  12214   Report bugs to the package provider."
 12143  12215   
 12144  12216   _ACEOF
 12145  12217   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12146  12218   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12147  12219   ac_cs_version="\\
 12148         -sqlite config.status 3.13.0
        12220  +sqlite config.status 3.15.0
 12149  12221   configured by $0, generated by GNU Autoconf 2.69,
 12150  12222     with options \\"\$ac_cs_config\\"
 12151  12223   
 12152  12224   Copyright (C) 2012 Free Software Foundation, Inc.
 12153  12225   This config.status script is free software; the Free Software Foundation
 12154  12226   gives unlimited permission to copy, distribute and modify it."
 12155  12227   

Changes to configure.ac.

   329    329             if test -f "$i/tclConfig.sh" ; then
   330    330               ac_cv_c_tclconfig="$i"
   331    331               break
   332    332             fi
   333    333           done
   334    334         fi
   335    335       fi
          336  +
          337  +    # Recent versions of Xcode on Macs hid the tclConfig.sh file
          338  +    # in a strange place.
          339  +    if test x"${ac_cv_c_tclconfig}" = x ; then
          340  +      if test x"$cross_compiling" = xno; then
          341  +        for i in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX*.sdk/usr/lib
          342  +        do
          343  +          if test -f "$i/tclConfig.sh" ; then
          344  +            ac_cv_c_tclconfig="$i"
          345  +            break
          346  +          fi
          347  +        done
          348  +      fi
          349  +    fi
   336    350   
   337    351       # then check for a private Tcl installation
   338    352       if test x"${ac_cv_c_tclconfig}" = x ; then
   339    353         for i in \
   340    354               ../tcl \
   341    355               `ls -dr ../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \
   342    356               `ls -dr ../tcl[[8-9]].[[0-9]] 2>/dev/null` \
................................................................................
   569    583         [use_loadextension=$enableval],[use_loadextension=yes])
   570    584   if test "${use_loadextension}" = "yes" ; then
   571    585     OPT_FEATURE_FLAGS=""
   572    586     AC_SEARCH_LIBS(dlopen, dl)
   573    587   else
   574    588     OPT_FEATURE_FLAGS="-DSQLITE_OMIT_LOAD_EXTENSION=1"
   575    589   fi
          590  +
          591  +##########
          592  +# Do we want to support memsys3 and/or memsys5
          593  +#
          594  +AC_ARG_ENABLE(memsys5, 
          595  +  AC_HELP_STRING([--enable-memsys5],[Enable MEMSYS5]),
          596  +  [enable_memsys5=yes],[enable_memsys5=no])
          597  +AC_MSG_CHECKING([whether to support MEMSYS5])
          598  +if test "${enable_memsys5}" = "yes"; then
          599  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS5"
          600  +  AC_MSG_RESULT([yes])
          601  +else
          602  +  AC_MSG_RESULT([no])
          603  +fi
          604  +AC_ARG_ENABLE(memsys3, 
          605  +  AC_HELP_STRING([--enable-memsys3],[Enable MEMSYS3]),
          606  +  [enable_memsys3=yes],[enable_memsys3=no])
          607  +AC_MSG_CHECKING([whether to support MEMSYS3])
          608  +if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
          609  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_MEMSYS3"
          610  +  AC_MSG_RESULT([yes])
          611  +else
          612  +  AC_MSG_RESULT([no])
          613  +fi
   576    614   
   577    615   #########
   578    616   # See whether we should enable Full Text Search extensions
   579    617   AC_ARG_ENABLE(fts3, AC_HELP_STRING([--enable-fts3],
   580    618         [Enable the FTS3 extension]),
   581    619         [enable_fts3=yes],[enable_fts3=no])
   582    620   if test "${enable_fts3}" = "yes" ; then
................................................................................
   610    648   # See whether we should enable RTREE
   611    649   AC_ARG_ENABLE(rtree, AC_HELP_STRING([--enable-rtree],
   612    650         [Enable the RTREE extension]),
   613    651         [enable_rtree=yes],[enable_rtree=no])
   614    652   if test "${enable_rtree}" = "yes" ; then
   615    653     OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE"
   616    654   fi
          655  +
          656  +#########
          657  +# See whether we should enable the SESSION extension
          658  +AC_ARG_ENABLE(session, AC_HELP_STRING([--enable-session],
          659  +      [Enable the SESSION extension]),
          660  +      [enable_session=yes],[enable_session=no])
          661  +if test "${enable_session}" = "yes" ; then
          662  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_SESSION"
          663  +  OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_PREUPDATE_HOOK"
          664  +fi
   617    665   
   618    666   #########
   619    667   # attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter
   620    668   for option in $CFLAGS $CPPFLAGS
   621    669   do
   622    670     case $option in
   623    671       -DSQLITE_OMIT*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";;

Changes to ext/fts2/fts2_tokenizer.c.

    95     95     }
    96     96   
    97     97     sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
    98     98   }
    99     99   
   100    100   #ifdef SQLITE_TEST
   101    101   
   102         -#include <tcl.h>
          102  +#if defined(INCLUDE_SQLITE_TCL_H)
          103  +#  include "sqlite_tcl.h"
          104  +#else
          105  +#  include "tcl.h"
          106  +#endif
   103    107   #include <string.h>
   104    108   
   105    109   /*
   106    110   ** Implementation of a special SQL scalar function for testing tokenizers 
   107    111   ** designed to be used in concert with the Tcl testing framework. This
   108    112   ** function must be called with two arguments:
   109    113   **

Changes to ext/fts3/fts3_test.c.

    14     14   ** testing. It contains a Tcl command that can be used to test if a document
    15     15   ** matches an FTS NEAR expression.
    16     16   **
    17     17   ** As of March 2012, it also contains a version 1 tokenizer used for testing
    18     18   ** that the sqlite3_tokenizer_module.xLanguage() method is invoked correctly.
    19     19   */
    20     20   
    21         -#include <tcl.h>
           21  +#if defined(INCLUDE_SQLITE_TCL_H)
           22  +#  include "sqlite_tcl.h"
           23  +#else
           24  +#  include "tcl.h"
           25  +#  ifndef SQLITE_TCLAPI
           26  +#    define SQLITE_TCLAPI
           27  +#  endif
           28  +#endif
    22     29   #include <string.h>
    23     30   #include <assert.h>
    24     31   
    25     32   #if defined(SQLITE_TEST)
    26     33   #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
    27     34   
    28     35   /* Required so that the "ifdef SQLITE_ENABLE_FTS3" below works */
................................................................................
   139    146   
   140    147     return nOcc;
   141    148   }
   142    149   
   143    150   /*
   144    151   ** Tclcmd: fts3_near_match DOCUMENT EXPR ?OPTIONS?
   145    152   */
   146         -static int fts3_near_match_cmd(
          153  +static int SQLITE_TCLAPI fts3_near_match_cmd(
   147    154     ClientData clientData,
   148    155     Tcl_Interp *interp,
   149    156     int objc,
   150    157     Tcl_Obj *CONST objv[]
   151    158   ){
   152    159     int nTotal = 0;
   153    160     int rc;
................................................................................
   274    281   **    set cfg [fts3_configure_incr_load $new_chunksize $new_threshold]
   275    282   **
   276    283   **    .... run tests ....
   277    284   **
   278    285   **    # Restore initial incr-load settings:
   279    286   **    eval fts3_configure_incr_load $cfg
   280    287   */
   281         -static int fts3_configure_incr_load_cmd(
          288  +static int SQLITE_TCLAPI fts3_configure_incr_load_cmd(
   282    289     ClientData clientData,
   283    290     Tcl_Interp *interp,
   284    291     int objc,
   285    292     Tcl_Obj *CONST objv[]
   286    293   ){
   287    294   #ifdef SQLITE_ENABLE_FTS3
   288    295     extern int test_fts3_node_chunksize;
................................................................................
   484    491     if( pCsr->iLangid>=100 ){
   485    492       rc = SQLITE_ERROR;
   486    493     }
   487    494     return rc;
   488    495   }
   489    496   #endif
   490    497   
   491         -static int fts3_test_tokenizer_cmd(
          498  +static int SQLITE_TCLAPI fts3_test_tokenizer_cmd(
   492    499     ClientData clientData,
   493    500     Tcl_Interp *interp,
   494    501     int objc,
   495    502     Tcl_Obj *CONST objv[]
   496    503   ){
   497    504   #ifdef SQLITE_ENABLE_FTS3
   498    505     static const sqlite3_tokenizer_module testTokenizerModule = {
................................................................................
   513    520       (const unsigned char *)&pPtr, sizeof(sqlite3_tokenizer_module *)
   514    521     ));
   515    522   #endif
   516    523     UNUSED_PARAMETER(clientData);
   517    524     return TCL_OK;
   518    525   }
   519    526   
   520         -static int fts3_test_varint_cmd(
          527  +static int SQLITE_TCLAPI fts3_test_varint_cmd(
   521    528     ClientData clientData,
   522    529     Tcl_Interp *interp,
   523    530     int objc,
   524    531     Tcl_Obj *CONST objv[]
   525    532   ){
   526    533   #ifdef SQLITE_ENABLE_FTS3
   527    534     char aBuf[24];

Changes to ext/fts3/fts3_tokenizer.c.

   220    220     sqlite3_free(zCopy);
   221    221     return rc;
   222    222   }
   223    223   
   224    224   
   225    225   #ifdef SQLITE_TEST
   226    226   
   227         -#include <tcl.h>
          227  +#if defined(INCLUDE_SQLITE_TCL_H)
          228  +#  include "sqlite_tcl.h"
          229  +#else
          230  +#  include "tcl.h"
          231  +#endif
   228    232   #include <string.h>
   229    233   
   230    234   /*
   231    235   ** Implementation of a special SQL scalar function for testing tokenizers 
   232    236   ** designed to be used in concert with the Tcl testing framework. This
   233    237   ** function must be called with two or more arguments:
   234    238   **

Changes to ext/fts5/fts5.h.

   139    139   ** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
   140    140   **   This API function is used to query the FTS table for phrase iPhrase
   141    141   **   of the current query. Specifically, a query equivalent to:
   142    142   **
   143    143   **       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
   144    144   **
   145    145   **   with $p set to a phrase equivalent to the phrase iPhrase of the
   146         -**   current query is executed. For each row visited, the callback function
   147         -**   passed as the fourth argument is invoked. The context and API objects 
   148         -**   passed to the callback function may be used to access the properties of
   149         -**   each matched row. Invoking Api.xUserData() returns a copy of the pointer
   150         -**   passed as the third argument to pUserData.
          146  +**   current query is executed. Any column filter that applies to
          147  +**   phrase iPhrase of the current query is included in $p. For each 
          148  +**   row visited, the callback function passed as the fourth argument 
          149  +**   is invoked. The context and API objects passed to the callback 
          150  +**   function may be used to access the properties of each matched row.
          151  +**   Invoking Api.xUserData() returns a copy of the pointer passed as 
          152  +**   the third argument to pUserData.
   151    153   **
   152    154   **   If the callback function returns any value other than SQLITE_OK, the
   153    155   **   query is abandoned and the xQueryPhrase function returns immediately.
   154    156   **   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
   155    157   **   Otherwise, the error code is propagated upwards.
   156    158   **
   157    159   **   If the query runs to completion without incident, SQLITE_OK is returned.
................................................................................
   312    314   ** Applications may also register custom tokenizer types. A tokenizer 
   313    315   ** is registered by providing fts5 with a populated instance of the 
   314    316   ** following structure. All structure methods must be defined, setting
   315    317   ** any member of the fts5_tokenizer struct to NULL leads to undefined
   316    318   ** behaviour. The structure methods are expected to function as follows:
   317    319   **
   318    320   ** xCreate:
   319         -**   This function is used to allocate and inititalize a tokenizer instance.
          321  +**   This function is used to allocate and initialize a tokenizer instance.
   320    322   **   A tokenizer instance is required to actually tokenize text.
   321    323   **
   322    324   **   The first argument passed to this function is a copy of the (void*)
   323    325   **   pointer provided by the application when the fts5_tokenizer object
   324    326   **   was registered with FTS5 (the third argument to xCreateTokenizer()). 
   325    327   **   The second and third arguments are an array of nul-terminated strings
   326    328   **   containing the tokenizer arguments, if any, specified following the
................................................................................
   571    573   *************************************************************************/
   572    574   
   573    575   #ifdef __cplusplus
   574    576   }  /* end of the 'extern "C"' block */
   575    577   #endif
   576    578   
   577    579   #endif /* _FTS5_H */
   578         -

Changes to ext/fts5/fts5Int.h.

   682    682   
   683    683   typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
   684    684   Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
   685    685   int sqlite3Fts5ExprPopulatePoslists(
   686    686       Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
   687    687   );
   688    688   void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
   689         -void sqlite3Fts5ExprClearEof(Fts5Expr*);
   690    689   
   691    690   int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
   692    691   
   693    692   int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
   694    693   
   695    694   /*******************************************
   696    695   ** The fts5_expr.c API above this point is used by the other hand-written
................................................................................
   734    733   
   735    734   void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
   736    735   void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
   737    736   void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
   738    737   
   739    738   void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
   740    739   void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNearset*, Fts5Colset*);
          740  +Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
   741    741   void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
   742    742   void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
   743    743   
   744    744   /*
   745    745   ** End of interface to code in fts5_expr.c.
   746    746   **************************************************************************/
   747    747   

Changes to ext/fts5/fts5_expr.c.

   163    163       case ')':  tok = FTS5_RP;    break;
   164    164       case '{':  tok = FTS5_LCP;   break;
   165    165       case '}':  tok = FTS5_RCP;   break;
   166    166       case ':':  tok = FTS5_COLON; break;
   167    167       case ',':  tok = FTS5_COMMA; break;
   168    168       case '+':  tok = FTS5_PLUS;  break;
   169    169       case '*':  tok = FTS5_STAR;  break;
          170  +    case '-':  tok = FTS5_MINUS; break;
   170    171       case '\0': tok = FTS5_EOF;   break;
   171    172   
   172    173       case '"': {
   173    174         const char *z2;
   174    175         tok = FTS5_STRING;
   175    176   
   176    177         for(z2=&z[1]; 1; z2++){
................................................................................
  1651   1652       pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, 
  1652   1653           sizeof(Fts5ExprNode));
  1653   1654     }
  1654   1655     if( rc==SQLITE_OK ){
  1655   1656       pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, 
  1656   1657           sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
  1657   1658     }
         1659  +  if( rc==SQLITE_OK ){
         1660  +    Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
         1661  +    if( pColsetOrig ){
         1662  +      int nByte = sizeof(Fts5Colset) + pColsetOrig->nCol * sizeof(int);
         1663  +      Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
         1664  +      if( pColset ){ 
         1665  +        memcpy(pColset, pColsetOrig, nByte);
         1666  +      }
         1667  +      pNew->pRoot->pNear->pColset = pColset;
         1668  +    }
         1669  +  }
  1658   1670   
  1659   1671     for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
  1660   1672       int tflags = 0;
  1661   1673       Fts5ExprTerm *p;
  1662   1674       for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
  1663   1675         const char *zTerm = p->zTerm;
  1664   1676         rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
................................................................................
  1777   1789       /* Check that the array is in order and contains no duplicate entries. */
  1778   1790       for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
  1779   1791   #endif
  1780   1792     }
  1781   1793   
  1782   1794     return pNew;
  1783   1795   }
         1796  +
         1797  +/*
         1798  +** Allocate and return an Fts5Colset object specifying the inverse of
         1799  +** the colset passed as the second argument. Free the colset passed
         1800  +** as the second argument before returning.
         1801  +*/
         1802  +Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
         1803  +  Fts5Colset *pRet;
         1804  +  int nCol = pParse->pConfig->nCol;
         1805  +
         1806  +  pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc, 
         1807  +      sizeof(Fts5Colset) + sizeof(int)*nCol
         1808  +  );
         1809  +  if( pRet ){
         1810  +    int i;
         1811  +    int iOld = 0;
         1812  +    for(i=0; i<nCol; i++){
         1813  +      if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
         1814  +        pRet->aiCol[pRet->nCol++] = i;
         1815  +      }else{
         1816  +        iOld++;
         1817  +      }
         1818  +    }
         1819  +  }
         1820  +
         1821  +  sqlite3_free(p);
         1822  +  return pRet;
         1823  +}
  1784   1824   
  1785   1825   Fts5Colset *sqlite3Fts5ParseColset(
  1786   1826     Fts5Parse *pParse,              /* Store SQLITE_NOMEM here if required */
  1787   1827     Fts5Colset *pColset,            /* Existing colset object */
  1788   1828     Fts5Token *p
  1789   1829   ){
  1790   1830     Fts5Colset *pRet = 0;
................................................................................
  2602   2642     return 1;
  2603   2643   }
  2604   2644   
  2605   2645   void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
  2606   2646     fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
  2607   2647   }
  2608   2648   
  2609         -static void fts5ExprClearEof(Fts5ExprNode *pNode){
  2610         -  int i;
  2611         -  for(i=0; i<pNode->nChild; i++){
  2612         -    fts5ExprClearEof(pNode->apChild[i]);
  2613         -  }
  2614         -  pNode->bEof = 0;
  2615         -}
  2616         -void sqlite3Fts5ExprClearEof(Fts5Expr *pExpr){
  2617         -  fts5ExprClearEof(pExpr->pRoot);
  2618         -}
  2619         -
  2620   2649   /*
  2621   2650   ** This function is only called for detail=columns tables. 
  2622   2651   */
  2623   2652   int sqlite3Fts5ExprPhraseCollist(
  2624   2653     Fts5Expr *pExpr, 
  2625   2654     int iPhrase, 
  2626   2655     const u8 **ppCollist, 

Changes to ext/fts5/fts5_index.c.

  3190   3190       ** Fts5Iter.poslist buffer and then set the output pointer to point
  3191   3191       ** to this buffer.  */
  3192   3192       fts5BufferZero(&pIter->poslist);
  3193   3193       fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
  3194   3194       pIter->base.pData = pIter->poslist.p;
  3195   3195     }
  3196   3196   }
         3197  +
         3198  +/*
         3199  +** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
         3200  +** against no columns at all).
         3201  +*/
         3202  +static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
         3203  +  UNUSED_PARAM(pSeg);
         3204  +  pIter->base.nData = 0;
         3205  +}
  3197   3206   
  3198   3207   /*
  3199   3208   ** xSetOutputs callback used by detail=col when there is a column filter
  3200   3209   ** and there are 100 or more columns. Also called as a fallback from
  3201   3210   ** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
  3202   3211   */
  3203   3212   static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
................................................................................
  3295   3304       if( pConfig->eDetail==FTS5_DETAIL_NONE ){
  3296   3305         pIter->xSetOutputs = fts5IterSetOutputs_None;
  3297   3306       }
  3298   3307   
  3299   3308       else if( pIter->pColset==0 ){
  3300   3309         pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
  3301   3310       }
         3311  +
         3312  +    else if( pIter->pColset->nCol==0 ){
         3313  +      pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
         3314  +    }
  3302   3315   
  3303   3316       else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
  3304   3317         pIter->xSetOutputs = fts5IterSetOutputs_Full;
  3305   3318       }
  3306   3319   
  3307   3320       else{
  3308   3321         assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );

Changes to ext/fts5/fts5_main.c.

  1182   1182       assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
  1183   1183       assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
  1184   1184       assert( pCsr->iLastRowid==LARGEST_INT64 );
  1185   1185       assert( pCsr->iFirstRowid==SMALLEST_INT64 );
  1186   1186       pCsr->ePlan = FTS5_PLAN_SOURCE;
  1187   1187       pCsr->pExpr = pTab->pSortCsr->pExpr;
  1188   1188       rc = fts5CursorFirst(pTab, pCsr, bDesc);
  1189         -    sqlite3Fts5ExprClearEof(pCsr->pExpr);
  1190   1189     }else if( pMatch ){
  1191   1190       const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
  1192   1191       if( zExpr==0 ) zExpr = "";
  1193   1192   
  1194   1193       rc = fts5CursorParseRank(pConfig, pCsr, pRank);
  1195   1194       if( rc==SQLITE_OK ){
  1196   1195         if( zExpr[0]=='*' ){

Changes to ext/fts5/fts5_storage.c.

   243    243     int bWithout,                   /* True for without rowid */
   244    244     char **pzErr                    /* OUT: Error message */
   245    245   ){
   246    246     int rc;
   247    247     char *zErr = 0;
   248    248   
   249    249     rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
   250         -      pConfig->zDb, pConfig->zName, zPost, zDefn, bWithout?" WITHOUT ROWID":""
          250  +      pConfig->zDb, pConfig->zName, zPost, zDefn, 
          251  +#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
          252  +      bWithout?" WITHOUT ROWID":
          253  +#endif
          254  +      ""
   251    255     );
   252    256     if( zErr ){
   253    257       *pzErr = sqlite3_mprintf(
   254    258           "fts5: error creating shadow table %q_%s: %s", 
   255    259           pConfig->zName, zPost, zErr
   256    260       );
   257    261       sqlite3_free(zErr);

Changes to ext/fts5/fts5_tcl.c.

    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   */
    14     14   
    15     15   
    16     16   #ifdef SQLITE_TEST
    17         -#include <tcl.h>
           17  +#if defined(INCLUDE_SQLITE_TCL_H)
           18  +#  include "sqlite_tcl.h"
           19  +#else
           20  +#  include "tcl.h"
           21  +#  ifndef SQLITE_TCLAPI
           22  +#    define SQLITE_TCLAPI
           23  +#  endif
           24  +#endif
    18     25   
    19     26   #ifdef SQLITE_ENABLE_FTS5
    20     27   
    21     28   #include "fts5.h"
    22     29   #include <string.h>
    23     30   #include <assert.h>
    24     31   
................................................................................
    74     81         return aErr[i].rc;
    75     82       }
    76     83     }
    77     84   
    78     85     return SQLITE_ERROR;
    79     86   }
    80     87   
    81         -static int f5tDbAndApi(
           88  +static int SQLITE_TCLAPI f5tDbAndApi(
    82     89     Tcl_Interp *interp, 
    83     90     Tcl_Obj *pObj, 
    84     91     sqlite3 **ppDb, 
    85     92     fts5_api **ppApi
    86     93   ){
    87     94     sqlite3 *db = 0;
    88     95     int rc = f5tDbPointer(interp, pObj, &db);
................................................................................
   160    167     if( rc==TCL_OK ){
   161    168       rc = f5tResultToErrorCode(Tcl_GetStringResult(p->interp));
   162    169     }
   163    170   
   164    171     return rc;
   165    172   }
   166    173   
   167         -static int xF5tApi(void*, Tcl_Interp*, int, Tcl_Obj *CONST []);
          174  +static int SQLITE_TCLAPI xF5tApi(void*, Tcl_Interp*, int, Tcl_Obj *CONST []);
   168    175   
   169    176   static int xQueryPhraseCb(
   170    177     const Fts5ExtensionApi *pApi, 
   171    178     Fts5Context *pFts, 
   172    179     void *pCtx
   173    180   ){
   174    181     F5tFunction *p = (F5tFunction*)pCtx;
................................................................................
   205    212   }
   206    213   
   207    214   /*
   208    215   **      api sub-command...
   209    216   **
   210    217   ** Description...
   211    218   */
   212         -static int xF5tApi(
          219  +static int SQLITE_TCLAPI xF5tApi(
   213    220     void * clientData,
   214    221     Tcl_Interp *interp,
   215    222     int objc,
   216    223     Tcl_Obj *CONST objv[]
   217    224   ){
   218    225     struct Sub {
   219    226       const char *zName;
................................................................................
   598    605   }
   599    606   
   600    607   /*
   601    608   **      sqlite3_fts5_create_function DB NAME SCRIPT
   602    609   **
   603    610   ** Description...
   604    611   */
   605         -static int f5tCreateFunction(
          612  +static int SQLITE_TCLAPI f5tCreateFunction(
   606    613     void * clientData,
   607    614     Tcl_Interp *interp,
   608    615     int objc,
   609    616     Tcl_Obj *CONST objv[]
   610    617   ){
   611    618     char *zName;
   612    619     Tcl_Obj *pScript;
................................................................................
   668    675   
   669    676   
   670    677   /*
   671    678   **      sqlite3_fts5_tokenize DB TOKENIZER TEXT
   672    679   **
   673    680   ** Description...
   674    681   */
   675         -static int f5tTokenize(
          682  +static int SQLITE_TCLAPI f5tTokenize(
   676    683     void * clientData,
   677    684     Tcl_Interp *interp,
   678    685     int objc,
   679    686     Tcl_Obj *CONST objv[]
   680    687   ){
   681    688     char *zText;
   682    689     int nText;
................................................................................
   874    881     pInst->pContext->xToken = xOldToken;
   875    882     return rc;
   876    883   }
   877    884   
   878    885   /*
   879    886   ** sqlite3_fts5_token ?-colocated? TEXT START END
   880    887   */
   881         -static int f5tTokenizerReturn(
          888  +static int SQLITE_TCLAPI f5tTokenizerReturn(
   882    889     void * clientData,
   883    890     Tcl_Interp *interp,
   884    891     int objc,
   885    892     Tcl_Obj *CONST objv[]
   886    893   ){
   887    894     F5tTokenizerContext *p = (F5tTokenizerContext*)clientData;
   888    895     int iStart;
................................................................................
   945    952   ** tokenizer instance "returned" by SCRIPT. Specifically, to tokenize
   946    953   ** text SCRIPT2 is invoked with a single argument appended to it - the
   947    954   ** text to tokenize.
   948    955   **
   949    956   ** SCRIPT2 should invoke the [sqlite3_fts5_token] command once for each
   950    957   ** token within the tokenized text.
   951    958   */
   952         -static int f5tCreateTokenizer(
          959  +static int SQLITE_TCLAPI f5tCreateTokenizer(
   953    960     ClientData clientData,
   954    961     Tcl_Interp *interp,
   955    962     int objc,
   956    963     Tcl_Obj *CONST objv[]
   957    964   ){
   958    965     F5tTokenizerContext *pContext = (F5tTokenizerContext*)clientData;
   959    966     sqlite3 *db;
................................................................................
   988    995       Tcl_AppendResult(interp, "error in fts5_api.xCreateTokenizer()", 0);
   989    996       return TCL_ERROR;
   990    997     }
   991    998   
   992    999     return TCL_OK;
   993   1000   }
   994   1001   
   995         -static void xF5tFree(ClientData clientData){
         1002  +static void SQLITE_TCLAPI xF5tFree(ClientData clientData){
   996   1003     ckfree(clientData);
   997   1004   }
   998   1005   
   999   1006   /*
  1000   1007   **      sqlite3_fts5_may_be_corrupt BOOLEAN
  1001   1008   **
  1002   1009   ** Set or clear the global "may-be-corrupt" flag. Return the old value.
  1003   1010   */
  1004         -static int f5tMayBeCorrupt(
         1011  +static int SQLITE_TCLAPI f5tMayBeCorrupt(
  1005   1012     void * clientData,
  1006   1013     Tcl_Interp *interp,
  1007   1014     int objc,
  1008   1015     Tcl_Obj *CONST objv[]
  1009   1016   ){
  1010   1017     int bOld = sqlite3_fts5_may_be_corrupt;
  1011   1018   
................................................................................
  1029   1036     unsigned int h = 13;
  1030   1037     for(i=n-1; i>=0; i--){
  1031   1038       h = (h << 3) ^ h ^ p[i];
  1032   1039     }
  1033   1040     return (h % nSlot);
  1034   1041   }
  1035   1042   
  1036         -static int f5tTokenHash(
         1043  +static int SQLITE_TCLAPI f5tTokenHash(
  1037   1044     void * clientData,
  1038   1045     Tcl_Interp *interp,
  1039   1046     int objc,
  1040   1047     Tcl_Obj *CONST objv[]
  1041   1048   ){
  1042   1049     char *z;
  1043   1050     int n;
................................................................................
  1054   1061     z = Tcl_GetStringFromObj(objv[2], &n);
  1055   1062   
  1056   1063     iVal = f5t_fts5HashKey(nSlot, z, n);
  1057   1064     Tcl_SetObjResult(interp, Tcl_NewIntObj(iVal));
  1058   1065     return TCL_OK;
  1059   1066   }
  1060   1067   
  1061         -static int f5tRegisterMatchinfo(
         1068  +static int SQLITE_TCLAPI f5tRegisterMatchinfo(
  1062   1069     void * clientData,
  1063   1070     Tcl_Interp *interp,
  1064   1071     int objc,
  1065   1072     Tcl_Obj *CONST objv[]
  1066   1073   ){
  1067   1074     int rc;
  1068   1075     sqlite3 *db = 0;
................................................................................
  1079   1086     if( rc!=SQLITE_OK ){
  1080   1087       Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE);
  1081   1088       return TCL_ERROR;
  1082   1089     }
  1083   1090     return TCL_OK;
  1084   1091   }
  1085   1092   
  1086         -static int f5tRegisterTok(
         1093  +static int SQLITE_TCLAPI f5tRegisterTok(
  1087   1094     void * clientData,
  1088   1095     Tcl_Interp *interp,
  1089   1096     int objc,
  1090   1097     Tcl_Obj *CONST objv[]
  1091   1098   ){
  1092   1099     int rc;
  1093   1100     sqlite3 *db = 0;

Changes to ext/fts5/fts5parse.y.

   116    116   }
   117    117   
   118    118   %type colset {Fts5Colset*}
   119    119   %destructor colset { sqlite3_free($$); }
   120    120   %type colsetlist {Fts5Colset*}
   121    121   %destructor colsetlist { sqlite3_free($$); }
   122    122   
          123  +colset(A) ::= MINUS LCP colsetlist(X) RCP. { 
          124  +    A = sqlite3Fts5ParseColsetInvert(pParse, X);
          125  +}
   123    126   colset(A) ::= LCP colsetlist(X) RCP. { A = X; }
   124    127   colset(A) ::= STRING(X). {
   125    128     A = sqlite3Fts5ParseColset(pParse, 0, &X);
   126    129   }
          130  +colset(A) ::= MINUS STRING(X). {
          131  +  A = sqlite3Fts5ParseColset(pParse, 0, &X);
          132  +  A = sqlite3Fts5ParseColsetInvert(pParse, A);
          133  +}
   127    134   
   128    135   colsetlist(A) ::= colsetlist(Y) STRING(X). { 
   129    136     A = sqlite3Fts5ParseColset(pParse, Y, &X); }
   130    137   colsetlist(A) ::= STRING(X). { 
   131    138     A = sqlite3Fts5ParseColset(pParse, 0, &X); 
   132    139   }
   133         -
   134    140   
   135    141   %type nearset     {Fts5ExprNearset*}
   136    142   %type nearphrases {Fts5ExprNearset*}
   137    143   %destructor nearset { sqlite3Fts5ParseNearsetFree($$); }
   138    144   %destructor nearphrases { sqlite3Fts5ParseNearsetFree($$); }
   139    145   
   140    146   nearset(A) ::= phrase(X). { A = sqlite3Fts5ParseNearset(pParse, 0, X); }

Changes to ext/fts5/test/fts5aux.test.

   241    241     execsql { DELETE FROM x1 }
   242    242     foreach row $lRow { execsql { INSERT INTO x1 VALUES($row) } }
   243    243     breakpoint
   244    244     do_execsql_test 8.$tn {
   245    245       SELECT highlight(x1, 0, '[', ']') FROM x1 WHERE x1 MATCH 'a OR (b AND d)';
   246    246     } $res
   247    247   }
          248  +
          249  +#-------------------------------------------------------------------------
          250  +# Test the built-in bm25() demo.
          251  +#
          252  +reset_db
          253  +do_execsql_test 9.1 {
          254  +  CREATE VIRTUAL TABLE t1 USING fts5(a, b);
          255  +  INSERT INTO t1 VALUES('a',   NULL);           -- 1
          256  +  INSERT INTO t1 VALUES('a',   NULL);           -- 2
          257  +  INSERT INTO t1 VALUES('a',   NULL);           -- 3
          258  +  INSERT INTO t1 VALUES('a',   NULL);           -- 4
          259  +  INSERT INTO t1 VALUES('a',   NULL);           -- 5
          260  +  INSERT INTO t1 VALUES('a',   NULL);           -- 6
          261  +  INSERT INTO t1 VALUES('a',   NULL);           -- 7
          262  +  INSERT INTO t1 VALUES('a',   NULL);           -- 8
          263  +  INSERT INTO t1 VALUES(NULL,  'a a b');        -- 9
          264  +  INSERT INTO t1 VALUES(NULL,  'b b a');        -- 10
          265  +}
          266  +
          267  +do_execsql_test 9.2 {
          268  +  SELECT rowid FROM t1('a AND b') ORDER BY rank;
          269  +} {
          270  +  10 9
          271  +}
          272  +
          273  +do_execsql_test 9.3 {
          274  +  SELECT rowid FROM t1('b:a AND b:b') ORDER BY rank;
          275  +} {
          276  +  9 10
          277  +}
          278  +
          279  +
   248    280   
   249    281   finish_test
   250    282   

Added ext/fts5/test/fts5colset.test.

            1  +# 2016 August 10
            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  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this script is testing the FTS5 module.
           13  +#
           14  +
           15  +source [file join [file dirname [info script]] fts5_common.tcl]
           16  +set testprefix fts5colset
           17  +
           18  +# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
           19  +ifcapable !fts5 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +foreach_detail_mode $::testprefix {
           25  +  if {[detail_is_none]} continue
           26  +
           27  +  do_execsql_test 1.0 {
           28  +    CREATE VIRTUAL TABLE t1 USING fts5(a, b, c, d, detail=%DETAIL%);
           29  +    INSERT INTO t1 VALUES('a', 'b', 'c', 'd');  -- 1
           30  +    INSERT INTO t1 VALUES('d', 'a', 'b', 'c');  -- 2
           31  +    INSERT INTO t1 VALUES('c', 'd', 'a', 'b');  -- 3
           32  +    INSERT INTO t1 VALUES('b', 'c', 'd', 'a');  -- 4
           33  +  }
           34  +
           35  +  foreach {tn q res} {
           36  +    1 "a"          {1 2 3 4}
           37  +    2 "{a}   : a"  {1}
           38  +    3 "-{a}   : a" {2 3 4}
           39  +    4 "- {a c} : a" {2 4}
           40  +    5 " - {d d c} : a" {1 2}
           41  +    6 "- {d c b a} : a" {}
           42  +    7 "-{\"a\"} : b" {1 2 3}
           43  +    8 "- c : a" {1 2 4}
           44  +    9 "-c : a"  {1 2 4}
           45  +    10 "-\"c\" : a"  {1 2 4}
           46  +  } {
           47  +  breakpoint
           48  +    do_execsql_test 1.$tn {
           49  +      SELECT rowid FROM t1($q)
           50  +    } $res
           51  +  }
           52  +
           53  +
           54  +}
           55  +
           56  +
           57  +finish_test
           58  +
           59  +

Changes to ext/fts5/test/fts5rank.test.

    87     87   } {1 3 2}
    88     88   
    89     89   do_test 2.7 {
    90     90     execsql { SELECT rowid FROM tt('a') ORDER BY rank; } db
    91     91   } {1 3 2}
    92     92   
    93     93   
           94  +#--------------------------------------------------------------------------
           95  +# At one point there was a problem with queries such as:
           96  +#
           97  +#   ... MATCH 'x OR y' ORDER BY rank;
           98  +#
           99  +# if there were zero occurrences of token 'y' in the dataset. The
          100  +# following tests verify that that problem has been addressed.
          101  +#
          102  +foreach_detail_mode $::testprefix {
          103  +  do_execsql_test 3.1.0 {
          104  +    CREATE VIRTUAL TABLE y1 USING fts5(z, detail=%DETAIL%);
          105  +    INSERT INTO y1 VALUES('test xyz');
          106  +    INSERT INTO y1 VALUES('test test xyz test');
          107  +    INSERT INTO y1 VALUES('test test xyz');
          108  +  }
          109  +
          110  +  do_execsql_test 3.1.1 {
          111  +    SELECT rowid FROM y1('test OR tset');
          112  +  } {1 2 3}
          113  +
          114  +  do_execsql_test 3.1.2 {
          115  +    SELECT rowid FROM y1('test OR tset') ORDER BY bm25(y1)
          116  +  } {2 3 1}
          117  +
          118  +  do_execsql_test 3.1.3 {
          119  +    SELECT rowid FROM y1('test OR tset') ORDER BY +rank
          120  +  } {2 3 1}
          121  +
          122  +  do_execsql_test 3.1.4 {
          123  +    SELECT rowid FROM y1('test OR tset') ORDER BY rank
          124  +  } {2 3 1}
          125  +
          126  +  do_execsql_test 3.1.5 {
          127  +    SELECT rowid FROM y1('test OR xyz') ORDER BY rank
          128  +  } {3 2 1}
          129  +
          130  +
          131  +  do_execsql_test 3.2.1 {
          132  +    CREATE VIRTUAL TABLE z1 USING fts5(a, detail=%DETAIL%);
          133  +    INSERT INTO z1 VALUES('wrinkle in time');
          134  +    SELECT * FROM z1 WHERE z1 MATCH 'wrinkle in time OR a wrinkle in time';
          135  +  } {{wrinkle in time}}
          136  +}
          137  +
          138  +do_execsql_test 4.1 {
          139  +  DROP TABLE IF EXISTS VTest;
          140  +  CREATE virtual TABLE VTest USING FTS5(
          141  +    Title, AUthor, tokenize ='porter unicode61 remove_diacritics 1', 
          142  +    columnsize='1', detail=full
          143  +  );
          144  +  INSERT INTO VTest (Title, Author) VALUES ('wrinkle in time', 'Bill Smith');
    94    145   
          146  +  SELECT * FROM VTest WHERE 
          147  +  VTest MATCH 'wrinkle in time OR a wrinkle in time' ORDER BY rank;
          148  +} {{wrinkle in time} {Bill Smith}}
    95    149   
    96    150   
    97    151   
    98    152   
    99    153   finish_test
   100    154   

Changes to ext/fts5/test/fts5simple.test.

   261    261   # Test that character 0x1A is allowed in fts5 barewords.
   262    262   #
   263    263   do_test 11.0 {
   264    264     execsql "CREATE VIRTUAL TABLE t4 USING fts5(x, tokenize=\"ascii tokenchars '\x1A'\")"
   265    265     execsql "
   266    266       INSERT INTO t4 VALUES('a b c \x1A');
   267    267       INSERT INTO t4 VALUES('a b c d\x1A');
   268         -    INSERT INTO t4 VALUES('a b c \x1Ad');
          268  +    INSERT INTO t4 VALUES('a b c \x1Ag');
   269    269       INSERT INTO t4 VALUES('a b c d');
   270    270     "
   271    271   } {}
   272    272   
   273    273   do_test 11.1 {
   274    274     execsql "SELECT rowid FROM t4('\x1A')"
   275    275   } {1}

Added ext/misc/carray.c.

            1  +/*
            2  +** 2016-06-29
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file demonstrates how to create a table-valued-function that
           14  +** returns the values in a C-language array.
           15  +** Examples:
           16  +**
           17  +**      SELECT * FROM carray($ptr,5)
           18  +**
           19  +** The query above returns 5 integers contained in a C-language array
           20  +** at the address $ptr.  $ptr is a pointer to the array of integers that
           21  +** has been cast to an integer.
           22  +**
           23  +** There is an optional third parameter to determine the datatype of
           24  +** the C-language array.  Allowed values of the third parameter are
           25  +** 'int32', 'int64', 'double', 'char*'.  Example:
           26  +**
           27  +**      SELECT * FROM carray($ptr,10,'char*');
           28  +**
           29  +** HOW IT WORKS
           30  +**
           31  +** The carray "function" is really a virtual table with the
           32  +** following schema:
           33  +**
           34  +**     CREATE TABLE carray(
           35  +**       value,
           36  +**       pointer HIDDEN,
           37  +**       count HIDDEN,
           38  +**       ctype TEXT HIDDEN
           39  +**     );
           40  +**
           41  +** If the hidden columns "pointer" and "count" are unconstrained, then 
           42  +** the virtual table has no rows.  Otherwise, the virtual table interprets
           43  +** the integer value of "pointer" as a pointer to the array and "count"
           44  +** as the number of elements in the array.  The virtual table steps through
           45  +** the array, element by element.
           46  +*/
           47  +#include "sqlite3ext.h"
           48  +SQLITE_EXTENSION_INIT1
           49  +#include <assert.h>
           50  +#include <string.h>
           51  +
           52  +#ifndef SQLITE_OMIT_VIRTUALTABLE
           53  +
           54  +/*
           55  +** Allowed datatypes
           56  +*/
           57  +#define CARRAY_INT32    0
           58  +#define CARRAY_INT64    1
           59  +#define CARRAY_DOUBLE   2
           60  +#define CARRAY_TEXT     3
           61  +
           62  +/*
           63  +** Names of types
           64  +*/
           65  +static const char *azType[] = { "int32", "int64", "double", "char*" };
           66  +
           67  +
           68  +/* carray_cursor is a subclass of sqlite3_vtab_cursor which will
           69  +** serve as the underlying representation of a cursor that scans
           70  +** over rows of the result
           71  +*/
           72  +typedef struct carray_cursor carray_cursor;
           73  +struct carray_cursor {
           74  +  sqlite3_vtab_cursor base;  /* Base class - must be first */
           75  +  sqlite3_int64 iRowid;      /* The rowid */
           76  +  sqlite3_int64 iPtr;        /* Pointer to array of values */
           77  +  sqlite3_int64 iCnt;        /* Number of integers in the array */
           78  +  unsigned char eType;       /* One of the CARRAY_type values */
           79  +};
           80  +
           81  +/*
           82  +** The carrayConnect() method is invoked to create a new
           83  +** carray_vtab that describes the carray virtual table.
           84  +**
           85  +** Think of this routine as the constructor for carray_vtab objects.
           86  +**
           87  +** All this routine needs to do is:
           88  +**
           89  +**    (1) Allocate the carray_vtab object and initialize all fields.
           90  +**
           91  +**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
           92  +**        result set of queries against carray will look like.
           93  +*/
           94  +static int carrayConnect(
           95  +  sqlite3 *db,
           96  +  void *pAux,
           97  +  int argc, const char *const*argv,
           98  +  sqlite3_vtab **ppVtab,
           99  +  char **pzErr
          100  +){
          101  +  sqlite3_vtab *pNew;
          102  +  int rc;
          103  +
          104  +/* Column numbers */
          105  +#define CARRAY_COLUMN_VALUE   0
          106  +#define CARRAY_COLUMN_POINTER 1
          107  +#define CARRAY_COLUMN_COUNT   2
          108  +#define CARRAY_COLUMN_CTYPE   3
          109  +
          110  +  rc = sqlite3_declare_vtab(db,
          111  +     "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)");
          112  +  if( rc==SQLITE_OK ){
          113  +    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
          114  +    if( pNew==0 ) return SQLITE_NOMEM;
          115  +    memset(pNew, 0, sizeof(*pNew));
          116  +  }
          117  +  return rc;
          118  +}
          119  +
          120  +/*
          121  +** This method is the destructor for carray_cursor objects.
          122  +*/
          123  +static int carrayDisconnect(sqlite3_vtab *pVtab){
          124  +  sqlite3_free(pVtab);
          125  +  return SQLITE_OK;
          126  +}
          127  +
          128  +/*
          129  +** Constructor for a new carray_cursor object.
          130  +*/
          131  +static int carrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          132  +  carray_cursor *pCur;
          133  +  pCur = sqlite3_malloc( sizeof(*pCur) );
          134  +  if( pCur==0 ) return SQLITE_NOMEM;
          135  +  memset(pCur, 0, sizeof(*pCur));
          136  +  *ppCursor = &pCur->base;
          137  +  return SQLITE_OK;
          138  +}
          139  +
          140  +/*
          141  +** Destructor for a carray_cursor.
          142  +*/
          143  +static int carrayClose(sqlite3_vtab_cursor *cur){
          144  +  sqlite3_free(cur);
          145  +  return SQLITE_OK;
          146  +}
          147  +
          148  +
          149  +/*
          150  +** Advance a carray_cursor to its next row of output.
          151  +*/
          152  +static int carrayNext(sqlite3_vtab_cursor *cur){
          153  +  carray_cursor *pCur = (carray_cursor*)cur;
          154  +  pCur->iRowid++;
          155  +  return SQLITE_OK;
          156  +}
          157  +
          158  +/*
          159  +** Return values of columns for the row at which the carray_cursor
          160  +** is currently pointing.
          161  +*/
          162  +static int carrayColumn(
          163  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          164  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          165  +  int i                       /* Which column to return */
          166  +){
          167  +  carray_cursor *pCur = (carray_cursor*)cur;
          168  +  sqlite3_int64 x = 0;
          169  +  switch( i ){
          170  +    case CARRAY_COLUMN_POINTER:   x = pCur->iPtr;   break;
          171  +    case CARRAY_COLUMN_COUNT:     x = pCur->iCnt;   break;
          172  +    case CARRAY_COLUMN_CTYPE: {
          173  +      sqlite3_result_text(ctx, azType[pCur->eType], -1, SQLITE_STATIC);
          174  +      return SQLITE_OK;
          175  +    }
          176  +    default: {
          177  +      switch( pCur->eType ){
          178  +        case CARRAY_INT32: {
          179  +          int *p = (int*)pCur->iPtr;
          180  +          sqlite3_result_int(ctx, p[pCur->iRowid-1]);
          181  +          return SQLITE_OK;
          182  +        }
          183  +        case CARRAY_INT64: {
          184  +          sqlite3_int64 *p = (sqlite3_int64*)pCur->iPtr;
          185  +          sqlite3_result_int64(ctx, p[pCur->iRowid-1]);
          186  +          return SQLITE_OK;
          187  +        }
          188  +        case CARRAY_DOUBLE: {
          189  +          double *p = (double*)pCur->iPtr;
          190  +          sqlite3_result_double(ctx, p[pCur->iRowid-1]);
          191  +          return SQLITE_OK;
          192  +        }
          193  +        case CARRAY_TEXT: {
          194  +          const char **p = (const char**)pCur->iPtr;
          195  +          sqlite3_result_text(ctx, p[pCur->iRowid-1], -1, SQLITE_TRANSIENT);
          196  +          return SQLITE_OK;
          197  +        }
          198  +      }
          199  +    }
          200  +  }
          201  +  sqlite3_result_int64(ctx, x);
          202  +  return SQLITE_OK;
          203  +}
          204  +
          205  +/*
          206  +** Return the rowid for the current row.  In this implementation, the
          207  +** rowid is the same as the output value.
          208  +*/
          209  +static int carrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          210  +  carray_cursor *pCur = (carray_cursor*)cur;
          211  +  *pRowid = pCur->iRowid;
          212  +  return SQLITE_OK;
          213  +}
          214  +
          215  +/*
          216  +** Return TRUE if the cursor has been moved off of the last
          217  +** row of output.
          218  +*/
          219  +static int carrayEof(sqlite3_vtab_cursor *cur){
          220  +  carray_cursor *pCur = (carray_cursor*)cur;
          221  +  return pCur->iRowid>pCur->iCnt;
          222  +}
          223  +
          224  +/*
          225  +** This method is called to "rewind" the carray_cursor object back
          226  +** to the first row of output.
          227  +*/
          228  +static int carrayFilter(
          229  +  sqlite3_vtab_cursor *pVtabCursor, 
          230  +  int idxNum, const char *idxStr,
          231  +  int argc, sqlite3_value **argv
          232  +){
          233  +  carray_cursor *pCur = (carray_cursor *)pVtabCursor;
          234  +  if( idxNum ){
          235  +    pCur->iPtr = sqlite3_value_int64(argv[0]);
          236  +    pCur->iCnt = sqlite3_value_int64(argv[1]);
          237  +    if( idxNum<3 ){
          238  +      pCur->eType = CARRAY_INT32;
          239  +    }else{
          240  +      unsigned char i;
          241  +      const char *zType = (const char*)sqlite3_value_text(argv[2]);
          242  +      for(i=0; i<sizeof(azType)/sizeof(azType[0]); i++){
          243  +        if( sqlite3_stricmp(zType, azType[i])==0 ) break;
          244  +      }
          245  +      if( i>=sizeof(azType)/sizeof(azType[0]) ){
          246  +        pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf(
          247  +          "unknown datatype: %Q", zType);
          248  +        return SQLITE_ERROR;
          249  +      }else{
          250  +        pCur->eType = i;
          251  +      }
          252  +    }
          253  +  }else{
          254  +    pCur->iPtr = 0;
          255  +    pCur->iCnt = 0;
          256  +  }
          257  +  pCur->iRowid = 1;
          258  +  return SQLITE_OK;
          259  +}
          260  +
          261  +/*
          262  +** SQLite will invoke this method one or more times while planning a query
          263  +** that uses the carray virtual table.  This routine needs to create
          264  +** a query plan for each invocation and compute an estimated cost for that
          265  +** plan.
          266  +**
          267  +** In this implementation idxNum is used to represent the
          268  +** query plan.  idxStr is unused.
          269  +**
          270  +** idxNum is 2 if the pointer= and count= constraints exist,
          271  +** 3 if the ctype= constraint also exists, and is 0 otherwise.
          272  +** If idxNum is 0, then carray becomes an empty table.
          273  +*/
          274  +static int carrayBestIndex(
          275  +  sqlite3_vtab *tab,
          276  +  sqlite3_index_info *pIdxInfo
          277  +){
          278  +  int i;                 /* Loop over constraints */
          279  +  int ptrIdx = -1;       /* Index of the pointer= constraint, or -1 if none */
          280  +  int cntIdx = -1;       /* Index of the count= constraint, or -1 if none */
          281  +  int ctypeIdx = -1;     /* Index of the ctype= constraint, or -1 if none */
          282  +
          283  +  const struct sqlite3_index_constraint *pConstraint;
          284  +  pConstraint = pIdxInfo->aConstraint;
          285  +  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
          286  +    if( pConstraint->usable==0 ) continue;
          287  +    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
          288  +    switch( pConstraint->iColumn ){
          289  +      case CARRAY_COLUMN_POINTER:
          290  +        ptrIdx = i;
          291  +        break;
          292  +      case CARRAY_COLUMN_COUNT:
          293  +        cntIdx = i;
          294  +        break;
          295  +      case CARRAY_COLUMN_CTYPE:
          296  +        ctypeIdx = i;
          297  +        break;
          298  +    }
          299  +  }
          300  +  if( ptrIdx>=0 && cntIdx>=0 ){
          301  +    pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1;
          302  +    pIdxInfo->aConstraintUsage[ptrIdx].omit = 1;
          303  +    pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2;
          304  +    pIdxInfo->aConstraintUsage[cntIdx].omit = 1;
          305  +    pIdxInfo->estimatedCost = (double)1;
          306  +    pIdxInfo->estimatedRows = 100;
          307  +    pIdxInfo->idxNum = 2;
          308  +    if( ctypeIdx>=0 ){
          309  +      pIdxInfo->aConstraintUsage[ctypeIdx].argvIndex = 3;
          310  +      pIdxInfo->aConstraintUsage[ctypeIdx].omit = 1;
          311  +      pIdxInfo->idxNum = 3;
          312  +    }
          313  +  }else{
          314  +    pIdxInfo->estimatedCost = (double)2147483647;
          315  +    pIdxInfo->estimatedRows = 2147483647;
          316  +    pIdxInfo->idxNum = 0;
          317  +  }
          318  +  return SQLITE_OK;
          319  +}
          320  +
          321  +/*
          322  +** This following structure defines all the methods for the 
          323  +** carray virtual table.
          324  +*/
          325  +static sqlite3_module carrayModule = {
          326  +  0,                         /* iVersion */
          327  +  0,                         /* xCreate */
          328  +  carrayConnect,             /* xConnect */
          329  +  carrayBestIndex,           /* xBestIndex */
          330  +  carrayDisconnect,          /* xDisconnect */
          331  +  0,                         /* xDestroy */
          332  +  carrayOpen,                /* xOpen - open a cursor */
          333  +  carrayClose,               /* xClose - close a cursor */
          334  +  carrayFilter,              /* xFilter - configure scan constraints */
          335  +  carrayNext,                /* xNext - advance a cursor */
          336  +  carrayEof,                 /* xEof - check for end of scan */
          337  +  carrayColumn,              /* xColumn - read data */
          338  +  carrayRowid,               /* xRowid - read data */
          339  +  0,                         /* xUpdate */
          340  +  0,                         /* xBegin */
          341  +  0,                         /* xSync */
          342  +  0,                         /* xCommit */
          343  +  0,                         /* xRollback */
          344  +  0,                         /* xFindMethod */
          345  +  0,                         /* xRename */
          346  +};
          347  +
          348  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          349  +
          350  +#ifdef _WIN32
          351  +__declspec(dllexport)
          352  +#endif
          353  +int sqlite3_carray_init(
          354  +  sqlite3 *db, 
          355  +  char **pzErrMsg, 
          356  +  const sqlite3_api_routines *pApi
          357  +){
          358  +  int rc = SQLITE_OK;
          359  +  SQLITE_EXTENSION_INIT2(pApi);
          360  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          361  +  rc = sqlite3_create_module(db, "carray", &carrayModule, 0);
          362  +#endif
          363  +  return rc;
          364  +}

Added ext/misc/csv.c.

            1  +/*
            2  +** 2016-05-28
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains the implementation of an SQLite virtual table for
           14  +** reading CSV files.
           15  +**
           16  +** Usage:
           17  +**
           18  +**    .load ./csv
           19  +**    CREATE VIRTUAL TABLE temp.csv USING csv(filename=FILENAME);
           20  +**    SELECT * FROM csv;
           21  +**
           22  +** The columns are named "c1", "c2", "c3", ... by default.  But the
           23  +** application can define its own CREATE TABLE statement as an additional
           24  +** parameter.  For example:
           25  +**
           26  +**    CREATE VIRTUAL TABLE temp.csv2 USING csv(
           27  +**       filename = "../http.log",
           28  +**       schema = "CREATE TABLE x(date,ipaddr,url,referrer,userAgent)"
           29  +**    );
           30  +**
           31  +** Instead of specifying a file, the text of the CSV can be loaded using
           32  +** the data= parameter.
           33  +**
           34  +** If the columns=N parameter is supplied, then the CSV file is assumed to have
           35  +** N columns.  If the columns parameter is omitted, the CSV file is opened
           36  +** as soon as the virtual table is constructed and the first row of the CSV
           37  +** is read in order to count the tables.
           38  +**
           39  +** Some extra debugging features (used for testing virtual tables) are available
           40  +** if this module is compiled with -DSQLITE_TEST.
           41  +*/
           42  +#include <sqlite3ext.h>
           43  +SQLITE_EXTENSION_INIT1
           44  +#include <string.h>
           45  +#include <stdlib.h>
           46  +#include <assert.h>
           47  +#include <stdarg.h>
           48  +#include <ctype.h>
           49  +#include <stdio.h>
           50  +
           51  +#ifndef SQLITE_OMIT_VIRTUALTABLE
           52  +
           53  +/*
           54  +** A macro to hint to the compiler that a function should not be
           55  +** inlined.
           56  +*/
           57  +#if defined(__GNUC__)
           58  +#  define CSV_NOINLINE  __attribute__((noinline))
           59  +#elif defined(_MSC_VER) && _MSC_VER>=1310
           60  +#  define CSV_NOINLINE  __declspec(noinline)
           61  +#else
           62  +#  define CSV_NOINLINE
           63  +#endif
           64  +
           65  +
           66  +/* Max size of the error message in a CsvReader */
           67  +#define CSV_MXERR 200
           68  +
           69  +/* Size of the CsvReader input buffer */
           70  +#define CSV_INBUFSZ 1024
           71  +
           72  +/* A context object used when read a CSV file. */
           73  +typedef struct CsvReader CsvReader;
           74  +struct CsvReader {
           75  +  FILE *in;              /* Read the CSV text from this input stream */
           76  +  char *z;               /* Accumulated text for a field */
           77  +  int n;                 /* Number of bytes in z */
           78  +  int nAlloc;            /* Space allocated for z[] */
           79  +  int nLine;             /* Current line number */
           80  +  char cTerm;            /* Character that terminated the most recent field */
           81  +  size_t iIn;            /* Next unread character in the input buffer */
           82  +  size_t nIn;            /* Number of characters in the input buffer */
           83  +  char *zIn;             /* The input buffer */
           84  +  char zErr[CSV_MXERR];  /* Error message */
           85  +};
           86  +
           87  +/* Initialize a CsvReader object */
           88  +static void csv_reader_init(CsvReader *p){
           89  +  p->in = 0;
           90  +  p->z = 0;
           91  +  p->n = 0;
           92  +  p->nAlloc = 0;
           93  +  p->nLine = 0;
           94  +  p->nIn = 0;
           95  +  p->zIn = 0;
           96  +  p->zErr[0] = 0;
           97  +}
           98  +
           99  +/* Close and reset a CsvReader object */
          100  +static void csv_reader_reset(CsvReader *p){
          101  +  if( p->in ){
          102  +    fclose(p->in);
          103  +    sqlite3_free(p->zIn);
          104  +  }
          105  +  sqlite3_free(p->z);
          106  +  csv_reader_init(p);
          107  +}
          108  +
          109  +/* Report an error on a CsvReader */
          110  +static void csv_errmsg(CsvReader *p, const char *zFormat, ...){
          111  +  va_list ap;
          112  +  va_start(ap, zFormat);
          113  +  sqlite3_vsnprintf(CSV_MXERR, p->zErr, zFormat, ap);
          114  +  va_end(ap);
          115  +}
          116  +
          117  +/* Open the file associated with a CsvReader
          118  +** Return the number of errors.
          119  +*/
          120  +static int csv_reader_open(
          121  +  CsvReader *p,               /* The reader to open */
          122  +  const char *zFilename,      /* Read from this filename */
          123  +  const char *zData           /*  ... or use this data */
          124  +){
          125  +  if( zFilename ){
          126  +    p->zIn = sqlite3_malloc( CSV_INBUFSZ );
          127  +    if( p->zIn==0 ){
          128  +      csv_errmsg(p, "out of memory");
          129  +      return 1;
          130  +    }
          131  +    p->in = fopen(zFilename, "rb");
          132  +    if( p->in==0 ){
          133  +      csv_reader_reset(p);
          134  +      csv_errmsg(p, "cannot open '%s' for reading", zFilename);
          135  +      return 1;
          136  +    }
          137  +  }else{
          138  +    assert( p->in==0 );
          139  +    p->zIn = (char*)zData;
          140  +    p->nIn = strlen(zData);
          141  +  }
          142  +  return 0;
          143  +}
          144  +
          145  +/* The input buffer has overflowed.  Refill the input buffer, then
          146  +** return the next character
          147  +*/
          148  +static CSV_NOINLINE int csv_getc_refill(CsvReader *p){
          149  +  size_t got;
          150  +
          151  +  assert( p->iIn>=p->nIn );  /* Only called on an empty input buffer */
          152  +  assert( p->in!=0 );        /* Only called if reading froma file */
          153  +
          154  +  got = fread(p->zIn, 1, CSV_INBUFSZ, p->in);
          155  +  if( got==0 ) return EOF;
          156  +  p->nIn = got;
          157  +  p->iIn = 1;
          158  +  return p->zIn[0];
          159  +}
          160  +
          161  +/* Return the next character of input.  Return EOF at end of input. */
          162  +static int csv_getc(CsvReader *p){
          163  +  if( p->iIn >= p->nIn ){
          164  +    if( p->in!=0 ) return csv_getc_refill(p);
          165  +    return EOF;
          166  +  }
          167  +  return p->zIn[p->iIn++];
          168  +}
          169  +
          170  +/* Increase the size of p->z and append character c to the end. 
          171  +** Return 0 on success and non-zero if there is an OOM error */
          172  +static CSV_NOINLINE int csv_resize_and_append(CsvReader *p, char c){
          173  +  char *zNew;
          174  +  int nNew = p->nAlloc*2 + 100;
          175  +  zNew = sqlite3_realloc64(p->z, nNew);
          176  +  if( zNew ){
          177  +    p->z = zNew;
          178  +    p->nAlloc = nNew;
          179  +    p->z[p->n++] = c;
          180  +    return 0;
          181  +  }else{
          182  +    csv_errmsg(p, "out of memory");
          183  +    return 1;
          184  +  }
          185  +}
          186  +
          187  +/* Append a single character to the CsvReader.z[] array.
          188  +** Return 0 on success and non-zero if there is an OOM error */
          189  +static int csv_append(CsvReader *p, char c){
          190  +  if( p->n>=p->nAlloc-1 ) return csv_resize_and_append(p, c);
          191  +  p->z[p->n++] = c;
          192  +  return 0;
          193  +}
          194  +
          195  +/* Read a single field of CSV text.  Compatible with rfc4180 and extended
          196  +** with the option of having a separator other than ",".
          197  +**
          198  +**   +  Input comes from p->in.
          199  +**   +  Store results in p->z of length p->n.  Space to hold p->z comes
          200  +**      from sqlite3_malloc64().
          201  +**   +  Keep track of the line number in p->nLine.
          202  +**   +  Store the character that terminates the field in p->cTerm.  Store
          203  +**      EOF on end-of-file.
          204  +**
          205  +** Return "" at EOF.  Return 0 on an OOM error.
          206  +*/
          207  +static char *csv_read_one_field(CsvReader *p){
          208  +  int c;
          209  +  p->n = 0;
          210  +  c = csv_getc(p);
          211  +  if( c==EOF ){
          212  +    p->cTerm = EOF;
          213  +    return "";
          214  +  }
          215  +  if( c=='"' ){
          216  +    int pc, ppc;
          217  +    int startLine = p->nLine;
          218  +    pc = ppc = 0;
          219  +    while( 1 ){
          220  +      c = csv_getc(p);
          221  +      if( c<='"' || pc=='"' ){
          222  +        if( c=='\n' ) p->nLine++;
          223  +        if( c=='"' ){
          224  +          if( pc=='"' ){
          225  +            pc = 0;
          226  +            continue;
          227  +          }
          228  +        }
          229  +        if( (c==',' && pc=='"')
          230  +         || (c=='\n' && pc=='"')
          231  +         || (c=='\n' && pc=='\r' && ppc=='"')
          232  +         || (c==EOF && pc=='"')
          233  +        ){
          234  +          do{ p->n--; }while( p->z[p->n]!='"' );
          235  +          p->cTerm = (char)c;
          236  +          break;
          237  +        }
          238  +        if( pc=='"' && c!='\r' ){
          239  +          csv_errmsg(p, "line %d: unescaped %c character", p->nLine, '"');
          240  +          break;
          241  +        }
          242  +        if( c==EOF ){
          243  +          csv_errmsg(p, "line %d: unterminated %c-quoted field\n",
          244  +                     startLine, '"');
          245  +          p->cTerm = (char)c;
          246  +          break;
          247  +        }
          248  +      }
          249  +      if( csv_append(p, (char)c) ) return 0;
          250  +      ppc = pc;
          251  +      pc = c;
          252  +    }
          253  +  }else{
          254  +    while( c>',' || (c!=EOF && c!=',' && c!='\n') ){
          255  +      if( csv_append(p, (char)c) ) return 0;
          256  +      c = csv_getc(p);
          257  +    }
          258  +    if( c=='\n' ){
          259  +      p->nLine++;
          260  +      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
          261  +    }
          262  +    p->cTerm = (char)c;
          263  +  }
          264  +  if( p->z ) p->z[p->n] = 0;
          265  +  return p->z;
          266  +}
          267  +
          268  +
          269  +/* Forward references to the various virtual table methods implemented
          270  +** in this file. */
          271  +static int csvtabCreate(sqlite3*, void*, int, const char*const*, 
          272  +                           sqlite3_vtab**,char**);
          273  +static int csvtabConnect(sqlite3*, void*, int, const char*const*, 
          274  +                           sqlite3_vtab**,char**);
          275  +static int csvtabBestIndex(sqlite3_vtab*,sqlite3_index_info*);
          276  +static int csvtabDisconnect(sqlite3_vtab*);
          277  +static int csvtabOpen(sqlite3_vtab*, sqlite3_vtab_cursor**);
          278  +static int csvtabClose(sqlite3_vtab_cursor*);
          279  +static int csvtabFilter(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
          280  +                          int argc, sqlite3_value **argv);
          281  +static int csvtabNext(sqlite3_vtab_cursor*);
          282  +static int csvtabEof(sqlite3_vtab_cursor*);
          283  +static int csvtabColumn(sqlite3_vtab_cursor*,sqlite3_context*,int);
          284  +static int csvtabRowid(sqlite3_vtab_cursor*,sqlite3_int64*);
          285  +
          286  +/* An instance of the CSV virtual table */
          287  +typedef struct CsvTable {
          288  +  sqlite3_vtab base;              /* Base class.  Must be first */
          289  +  char *zFilename;                /* Name of the CSV file */
          290  +  char *zData;                    /* Raw CSV data in lieu of zFilename */
          291  +  long iStart;                    /* Offset to start of data in zFilename */
          292  +  int nCol;                       /* Number of columns in the CSV file */
          293  +  unsigned int tstFlags;          /* Bit values used for testing */
          294  +} CsvTable;
          295  +
          296  +/* Allowed values for tstFlags */
          297  +#define CSVTEST_FIDX  0x0001      /* Pretend that constrained searchs cost less*/
          298  +
          299  +/* A cursor for the CSV virtual table */
          300  +typedef struct CsvCursor {
          301  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
          302  +  CsvReader rdr;                  /* The CsvReader object */
          303  +  char **azVal;                   /* Value of the current row */
          304  +  int *aLen;                      /* Length of each entry */
          305  +  sqlite3_int64 iRowid;           /* The current rowid.  Negative for EOF */
          306  +} CsvCursor;
          307  +
          308  +/* Transfer error message text from a reader into a CsvTable */
          309  +static void csv_xfer_error(CsvTable *pTab, CsvReader *pRdr){
          310  +  sqlite3_free(pTab->base.zErrMsg);
          311  +  pTab->base.zErrMsg = sqlite3_mprintf("%s", pRdr->zErr);
          312  +}
          313  +
          314  +/*
          315  +** This method is the destructor fo a CsvTable object.
          316  +*/
          317  +static int csvtabDisconnect(sqlite3_vtab *pVtab){
          318  +  CsvTable *p = (CsvTable*)pVtab;
          319  +  sqlite3_free(p->zFilename);
          320  +  sqlite3_free(p->zData);
          321  +  sqlite3_free(p);
          322  +  return SQLITE_OK;
          323  +}
          324  +
          325  +/* Skip leading whitespace.  Return a pointer to the first non-whitespace
          326  +** character, or to the zero terminator if the string has only whitespace */
          327  +static const char *csv_skip_whitespace(const char *z){
          328  +  while( isspace((unsigned char)z[0]) ) z++;
          329  +  return z;
          330  +}
          331  +
          332  +/* Remove trailing whitespace from the end of string z[] */
          333  +static void csv_trim_whitespace(char *z){
          334  +  size_t n = strlen(z);
          335  +  while( n>0 && isspace((unsigned char)z[n]) ) n--;
          336  +  z[n] = 0;
          337  +}
          338  +
          339  +/* Dequote the string */
          340  +static void csv_dequote(char *z){
          341  +  int j;
          342  +  char cQuote = z[0];
          343  +  size_t i, n;
          344  +
          345  +  if( cQuote!='\'' && cQuote!='"' ) return;
          346  +  n = strlen(z);
          347  +  if( n<2 || z[n-1]!=z[0] ) return;
          348  +  for(i=1, j=0; i<n-1; i++){
          349  +    if( z[i]==cQuote && z[i+1]==cQuote ) i++;
          350  +    z[j++] = z[i];
          351  +  }
          352  +  z[j] = 0;
          353  +}
          354  +
          355  +/* Check to see if the string is of the form:  "TAG = VALUE" with optional
          356  +** whitespace before and around tokens.  If it is, return a pointer to the
          357  +** first character of VALUE.  If it is not, return NULL.
          358  +*/
          359  +static const char *csv_parameter(const char *zTag, int nTag, const char *z){
          360  +  z = csv_skip_whitespace(z);
          361  +  if( strncmp(zTag, z, nTag)!=0 ) return 0;
          362  +  z = csv_skip_whitespace(z+nTag);
          363  +  if( z[0]!='=' ) return 0;
          364  +  return csv_skip_whitespace(z+1);
          365  +}
          366  +
          367  +/* Decode a parameter that requires a dequoted string.
          368  +**
          369  +** Return 1 if the parameter is seen, or 0 if not.  1 is returned
          370  +** even if there is an error.  If an error occurs, then an error message
          371  +** is left in p->zErr.  If there are no errors, p->zErr[0]==0.
          372  +*/
          373  +static int csv_string_parameter(
          374  +  CsvReader *p,            /* Leave the error message here, if there is one */
          375  +  const char *zParam,      /* Parameter we are checking for */
          376  +  const char *zArg,        /* Raw text of the virtual table argment */
          377  +  char **pzVal             /* Write the dequoted string value here */
          378  +){
          379  +  const char *zValue;
          380  +  zValue = csv_parameter(zParam,(int)strlen(zParam),zArg);
          381  +  if( zValue==0 ) return 0;
          382  +  p->zErr[0] = 0;
          383  +  if( *pzVal ){
          384  +    csv_errmsg(p, "more than one '%s' parameter", zParam);
          385  +    return 1;
          386  +  }
          387  +  *pzVal = sqlite3_mprintf("%s", zValue);
          388  +  if( *pzVal==0 ){
          389  +    csv_errmsg(p, "out of memory");
          390  +    return 1;
          391  +  }
          392  +  csv_trim_whitespace(*pzVal);
          393  +  csv_dequote(*pzVal);
          394  +  return 1;
          395  +}
          396  +
          397  +
          398  +/* Return 0 if the argument is false and 1 if it is true.  Return -1 if
          399  +** we cannot really tell.
          400  +*/
          401  +static int csv_boolean(const char *z){
          402  +  if( sqlite3_stricmp("yes",z)==0
          403  +   || sqlite3_stricmp("on",z)==0
          404  +   || sqlite3_stricmp("true",z)==0
          405  +   || (z[0]=='1' && z[0]==0)
          406  +  ){
          407  +    return 1;
          408  +  }
          409  +  if( sqlite3_stricmp("no",z)==0
          410  +   || sqlite3_stricmp("off",z)==0
          411  +   || sqlite3_stricmp("false",z)==0
          412  +   || (z[0]=='0' && z[1]==0)
          413  +  ){
          414  +    return 0;
          415  +  }
          416  +  return -1;
          417  +}
          418  +
          419  +
          420  +/*
          421  +** Parameters:
          422  +**    filename=FILENAME          Name of file containing CSV content
          423  +**    data=TEXT                  Direct CSV content.
          424  +**    schema=SCHEMA              Alternative CSV schema.
          425  +**    header=YES|NO              First row of CSV defines the names of
          426  +**                               columns if "yes".  Default "no".
          427  +**    columns=N                  Assume the CSV file contains N columns.
          428  +**
          429  +** Only available if compiled with SQLITE_TEST:
          430  +**    
          431  +**    testflags=N                Bitmask of test flags.  Optional
          432  +**
          433  +** If schema= is omitted, then the columns are named "c0", "c1", "c2",
          434  +** and so forth.  If columns=N is omitted, then the file is opened and
          435  +** the number of columns in the first row is counted to determine the
          436  +** column count.  If header=YES, then the first row is skipped.
          437  +*/
          438  +static int csvtabConnect(
          439  +  sqlite3 *db,
          440  +  void *pAux,
          441  +  int argc, const char *const*argv,
          442  +  sqlite3_vtab **ppVtab,
          443  +  char **pzErr
          444  +){
          445  +  CsvTable *pNew = 0;        /* The CsvTable object to construct */
          446  +  int bHeader = -1;          /* header= flags.  -1 means not seen yet */
          447  +  int rc = SQLITE_OK;        /* Result code from this routine */
          448  +  int i, j;                  /* Loop counters */
          449  +#ifdef SQLITE_TEST
          450  +  int tstFlags = 0;          /* Value for testflags=N parameter */
          451  +#endif
          452  +  int nCol = -99;            /* Value of the columns= parameter */
          453  +  CsvReader sRdr;            /* A CSV file reader used to store an error
          454  +                             ** message and/or to count the number of columns */
          455  +  static const char *azParam[] = {
          456  +     "filename", "data", "schema", 
          457  +  };
          458  +  char *azPValue[3];         /* Parameter values */
          459  +# define CSV_FILENAME (azPValue[0])
          460  +# define CSV_DATA     (azPValue[1])
          461  +# define CSV_SCHEMA   (azPValue[2])
          462  +
          463  +
          464  +  assert( sizeof(azPValue)==sizeof(azParam) );
          465  +  memset(&sRdr, 0, sizeof(sRdr));
          466  +  memset(azPValue, 0, sizeof(azPValue));
          467  +  for(i=3; i<argc; i++){
          468  +    const char *z = argv[i];
          469  +    const char *zValue;
          470  +    for(j=0; j<sizeof(azParam)/sizeof(azParam[0]); j++){
          471  +      if( csv_string_parameter(&sRdr, azParam[j], z, &azPValue[j]) ) break;
          472  +    }
          473  +    if( j<sizeof(azParam)/sizeof(azParam[0]) ){
          474  +      if( sRdr.zErr[0] ) goto csvtab_connect_error;
          475  +    }else
          476  +    if( (zValue = csv_parameter("header",6,z))!=0 ){
          477  +      int x;
          478  +      if( bHeader>=0 ){
          479  +        csv_errmsg(&sRdr, "more than one 'header' parameter");
          480  +        goto csvtab_connect_error;
          481  +      }
          482  +      x = csv_boolean(zValue);
          483  +      if( x==1 ){
          484  +        bHeader = 1;
          485  +      }else if( x==0 ){
          486  +        bHeader = 0;
          487  +      }else{
          488  +        csv_errmsg(&sRdr, "unrecognized argument to 'header': %s", zValue);
          489  +        goto csvtab_connect_error;
          490  +      }
          491  +    }else
          492  +#ifdef SQLITE_TEST
          493  +    if( (zValue = csv_parameter("testflags",9,z))!=0 ){
          494  +      tstFlags = (unsigned int)atoi(zValue);
          495  +    }else
          496  +#endif
          497  +    if( (zValue = csv_parameter("columns",7,z))!=0 ){
          498  +      if( nCol>0 ){
          499  +        csv_errmsg(&sRdr, "more than one 'columns' parameter");
          500  +        goto csvtab_connect_error;
          501  +      }
          502  +      nCol = atoi(zValue);
          503  +      if( nCol<=0 ){
          504  +        csv_errmsg(&sRdr, "must have at least one column");
          505  +        goto csvtab_connect_error;
          506  +      }
          507  +    }else
          508  +    {
          509  +      csv_errmsg(&sRdr, "unrecognized parameter '%s'", z);
          510  +      goto csvtab_connect_error;
          511  +    }
          512  +  }
          513  +  if( (CSV_FILENAME==0)==(CSV_DATA==0) ){
          514  +    csv_errmsg(&sRdr, "must either filename= or data= but not both");
          515  +    goto csvtab_connect_error;
          516  +  }
          517  +  if( nCol<=0 && csv_reader_open(&sRdr, CSV_FILENAME, CSV_DATA) ){
          518  +    goto csvtab_connect_error;
          519  +  }
          520  +  pNew = sqlite3_malloc( sizeof(*pNew) );
          521  +  *ppVtab = (sqlite3_vtab*)pNew;
          522  +  if( pNew==0 ) goto csvtab_connect_oom;
          523  +  memset(pNew, 0, sizeof(*pNew));
          524  +  if( nCol>0 ){
          525  +    pNew->nCol = nCol;
          526  +  }else{
          527  +    do{
          528  +      const char *z = csv_read_one_field(&sRdr);
          529  +      if( z==0 ) goto csvtab_connect_oom;
          530  +      pNew->nCol++;
          531  +    }while( sRdr.cTerm==',' );
          532  +  }
          533  +  pNew->zFilename = CSV_FILENAME;  CSV_FILENAME = 0;
          534  +  pNew->zData = CSV_DATA;          CSV_DATA = 0;
          535  +#ifdef SQLITE_TEST
          536  +  pNew->tstFlags = tstFlags;
          537  +#endif
          538  +  pNew->iStart = bHeader==1 ? ftell(sRdr.in) : 0;
          539  +  csv_reader_reset(&sRdr);
          540  +  if( CSV_SCHEMA==0 ){
          541  +    char *zSep = "";
          542  +    CSV_SCHEMA = sqlite3_mprintf("CREATE TABLE x(");
          543  +    if( CSV_SCHEMA==0 ) goto csvtab_connect_oom;
          544  +    for(i=0; i<pNew->nCol; i++){
          545  +      CSV_SCHEMA = sqlite3_mprintf("%z%sc%d TEXT",CSV_SCHEMA, zSep, i);
          546  +      zSep = ",";
          547  +    }
          548  +    CSV_SCHEMA = sqlite3_mprintf("%z);", CSV_SCHEMA);
          549  +  }
          550  +  rc = sqlite3_declare_vtab(db, CSV_SCHEMA);
          551  +  if( rc ) goto csvtab_connect_error;
          552  +  for(i=0; i<sizeof(azPValue)/sizeof(azPValue[0]); i++){
          553  +    sqlite3_free(azPValue[i]);
          554  +  }
          555  +  return SQLITE_OK;
          556  +
          557  +csvtab_connect_oom:
          558  +  rc = SQLITE_NOMEM;
          559  +  csv_errmsg(&sRdr, "out of memory");
          560  +
          561  +csvtab_connect_error:
          562  +  if( pNew ) csvtabDisconnect(&pNew->base);
          563  +  for(i=0; i<sizeof(azPValue)/sizeof(azPValue[0]); i++){
          564  +    sqlite3_free(azPValue[i]);
          565  +  }
          566  +  if( sRdr.zErr[0] ){
          567  +    sqlite3_free(*pzErr);
          568  +    *pzErr = sqlite3_mprintf("%s", sRdr.zErr);
          569  +  }
          570  +  csv_reader_reset(&sRdr);
          571  +  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
          572  +  return rc;
          573  +}
          574  +
          575  +/*
          576  +** Reset the current row content held by a CsvCursor.
          577  +*/
          578  +static void csvtabCursorRowReset(CsvCursor *pCur){
          579  +  CsvTable *pTab = (CsvTable*)pCur->base.pVtab;
          580  +  int i;
          581  +  for(i=0; i<pTab->nCol; i++){
          582  +    sqlite3_free(pCur->azVal[i]);
          583  +    pCur->azVal[i] = 0;
          584  +    pCur->aLen[i] = 0;
          585  +  }
          586  +}
          587  +
          588  +/*
          589  +** The xConnect and xCreate methods do the same thing, but they must be
          590  +** different so that the virtual table is not an eponymous virtual table.
          591  +*/
          592  +static int csvtabCreate(
          593  +  sqlite3 *db,
          594  +  void *pAux,
          595  +  int argc, const char *const*argv,
          596  +  sqlite3_vtab **ppVtab,
          597  +  char **pzErr
          598  +){
          599  + return csvtabConnect(db, pAux, argc, argv, ppVtab, pzErr);
          600  +}
          601  +
          602  +/*
          603  +** Destructor for a CsvCursor.
          604  +*/
          605  +static int csvtabClose(sqlite3_vtab_cursor *cur){
          606  +  CsvCursor *pCur = (CsvCursor*)cur;
          607  +  csvtabCursorRowReset(pCur);
          608  +  csv_reader_reset(&pCur->rdr);
          609  +  sqlite3_free(cur);
          610  +  return SQLITE_OK;
          611  +}
          612  +
          613  +/*
          614  +** Constructor for a new CsvTable cursor object.
          615  +*/
          616  +static int csvtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          617  +  CsvTable *pTab = (CsvTable*)p;
          618  +  CsvCursor *pCur;
          619  +  size_t nByte;
          620  +  nByte = sizeof(*pCur) + (sizeof(char*)+sizeof(int))*pTab->nCol;
          621  +  pCur = sqlite3_malloc64( nByte );
          622  +  if( pCur==0 ) return SQLITE_NOMEM;
          623  +  memset(pCur, 0, nByte);
          624  +  pCur->azVal = (char**)&pCur[1];
          625  +  pCur->aLen = (int*)&pCur->azVal[pTab->nCol];
          626  +  *ppCursor = &pCur->base;
          627  +  if( csv_reader_open(&pCur->rdr, pTab->zFilename, pTab->zData) ){
          628  +    csv_xfer_error(pTab, &pCur->rdr);
          629  +    return SQLITE_ERROR;
          630  +  }
          631  +  return SQLITE_OK;
          632  +}
          633  +
          634  +
          635  +/*
          636  +** Advance a CsvCursor to its next row of input.
          637  +** Set the EOF marker if we reach the end of input.
          638  +*/
          639  +static int csvtabNext(sqlite3_vtab_cursor *cur){
          640  +  CsvCursor *pCur = (CsvCursor*)cur;
          641  +  CsvTable *pTab = (CsvTable*)cur->pVtab;
          642  +  int i = 0;
          643  +  char *z;
          644  +  do{
          645  +    z = csv_read_one_field(&pCur->rdr);
          646  +    if( z==0 ){
          647  +      csv_xfer_error(pTab, &pCur->rdr);
          648  +      break;
          649  +    }
          650  +    if( i<pTab->nCol ){
          651  +      if( pCur->aLen[i] < pCur->rdr.n+1 ){
          652  +        char *zNew = sqlite3_realloc64(pCur->azVal[i], pCur->rdr.n+1);
          653  +        if( zNew==0 ){
          654  +          csv_errmsg(&pCur->rdr, "out of memory");
          655  +          csv_xfer_error(pTab, &pCur->rdr);
          656  +          break;
          657  +        }
          658  +        pCur->azVal[i] = zNew;
          659  +        pCur->aLen[i] = pCur->rdr.n+1;
          660  +      }
          661  +      memcpy(pCur->azVal[i], z, pCur->rdr.n+1);
          662  +      i++;
          663  +    }
          664  +  }while( pCur->rdr.cTerm==',' );
          665  +  while( i<pTab->nCol ){
          666  +    sqlite3_free(pCur->azVal[i]);
          667  +    pCur->azVal[i] = 0;
          668  +    pCur->aLen[i] = 0;
          669  +    i++;
          670  +  }
          671  +  if( z==0 || pCur->rdr.cTerm==EOF ){
          672  +    pCur->iRowid = -1;
          673  +  }else{
          674  +    pCur->iRowid++;
          675  +  }
          676  +  return SQLITE_OK;
          677  +}
          678  +
          679  +/*
          680  +** Return values of columns for the row at which the CsvCursor
          681  +** is currently pointing.
          682  +*/
          683  +static int csvtabColumn(
          684  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          685  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          686  +  int i                       /* Which column to return */
          687  +){
          688  +  CsvCursor *pCur = (CsvCursor*)cur;
          689  +  CsvTable *pTab = (CsvTable*)cur->pVtab;
          690  +  if( i>=0 && i<pTab->nCol && pCur->azVal[i]!=0 ){
          691  +    sqlite3_result_text(ctx, pCur->azVal[i], -1, SQLITE_STATIC);
          692  +  }
          693  +  return SQLITE_OK;
          694  +}
          695  +
          696  +/*
          697  +** Return the rowid for the current row.
          698  +*/
          699  +static int csvtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          700  +  CsvCursor *pCur = (CsvCursor*)cur;
          701  +  *pRowid = pCur->iRowid;
          702  +  return SQLITE_OK;
          703  +}
          704  +
          705  +/*
          706  +** Return TRUE if the cursor has been moved off of the last
          707  +** row of output.
          708  +*/
          709  +static int csvtabEof(sqlite3_vtab_cursor *cur){
          710  +  CsvCursor *pCur = (CsvCursor*)cur;
          711  +  return pCur->iRowid<0;
          712  +}
          713  +
          714  +/*
          715  +** Only a full table scan is supported.  So xFilter simply rewinds to
          716  +** the beginning.
          717  +*/
          718  +static int csvtabFilter(
          719  +  sqlite3_vtab_cursor *pVtabCursor, 
          720  +  int idxNum, const char *idxStr,
          721  +  int argc, sqlite3_value **argv
          722  +){
          723  +  CsvCursor *pCur = (CsvCursor*)pVtabCursor;
          724  +  CsvTable *pTab = (CsvTable*)pVtabCursor->pVtab;
          725  +  pCur->iRowid = 0;
          726  +  if( pCur->rdr.in==0 ){
          727  +    assert( pCur->rdr.zIn==pTab->zData );
          728  +    assert( pTab->iStart>=0 );
          729  +    assert( (size_t)pTab->iStart<=pCur->rdr.nIn );
          730  +    pCur->rdr.iIn = pTab->iStart;
          731  +  }else{
          732  +    fseek(pCur->rdr.in, pTab->iStart, SEEK_SET);
          733  +    pCur->rdr.iIn = 0;
          734  +    pCur->rdr.nIn = 0;
          735  +  }
          736  +  return csvtabNext(pVtabCursor);
          737  +}
          738  +
          739  +/*
          740  +** Only a forward full table scan is supported.  xBestIndex is mostly
          741  +** a no-op.  If CSVTEST_FIDX is set, then the presence of equality
          742  +** constraints lowers the estimated cost, which is fiction, but is useful
          743  +** for testing certain kinds of virtual table behavior.
          744  +*/
          745  +static int csvtabBestIndex(
          746  +  sqlite3_vtab *tab,
          747  +  sqlite3_index_info *pIdxInfo
          748  +){
          749  +  pIdxInfo->estimatedCost = 1000000;
          750  +#ifdef SQLITE_TEST
          751  +  if( (((CsvTable*)tab)->tstFlags & CSVTEST_FIDX)!=0 ){
          752  +    /* The usual (and sensible) case is to always do a full table scan.
          753  +    ** The code in this branch only runs when testflags=1.  This code
          754  +    ** generates an artifical and unrealistic plan which is useful
          755  +    ** for testing virtual table logic but is not helpful to real applications.
          756  +    **
          757  +    ** Any ==, LIKE, or GLOB constraint is marked as usable by the virtual
          758  +    ** table (even though it is not) and the cost of running the virtual table
          759  +    ** is reduced from 1 million to just 10.  The constraints are *not* marked
          760  +    ** as omittable, however, so the query planner should still generate a
          761  +    ** plan that gives a correct answer, even if they plan is not optimal.
          762  +    */
          763  +    int i;
          764  +    int nConst = 0;
          765  +    for(i=0; i<pIdxInfo->nConstraint; i++){
          766  +      unsigned char op;
          767  +      if( pIdxInfo->aConstraint[i].usable==0 ) continue;
          768  +      op = pIdxInfo->aConstraint[i].op;
          769  +      if( op==SQLITE_INDEX_CONSTRAINT_EQ 
          770  +       || op==SQLITE_INDEX_CONSTRAINT_LIKE
          771  +       || op==SQLITE_INDEX_CONSTRAINT_GLOB
          772  +      ){
          773  +        pIdxInfo->estimatedCost = 10;
          774  +        pIdxInfo->aConstraintUsage[nConst].argvIndex = nConst+1;
          775  +        nConst++;
          776  +      }
          777  +    }
          778  +  }
          779  +#endif
          780  +  return SQLITE_OK;
          781  +}
          782  +
          783  +
          784  +static sqlite3_module CsvModule = {
          785  +  0,                       /* iVersion */
          786  +  csvtabCreate,            /* xCreate */
          787  +  csvtabConnect,           /* xConnect */
          788  +  csvtabBestIndex,         /* xBestIndex */
          789  +  csvtabDisconnect,        /* xDisconnect */
          790  +  csvtabDisconnect,        /* xDestroy */
          791  +  csvtabOpen,              /* xOpen - open a cursor */
          792  +  csvtabClose,             /* xClose - close a cursor */
          793  +  csvtabFilter,            /* xFilter - configure scan constraints */
          794  +  csvtabNext,              /* xNext - advance a cursor */
          795  +  csvtabEof,               /* xEof - check for end of scan */
          796  +  csvtabColumn,            /* xColumn - read data */
          797  +  csvtabRowid,             /* xRowid - read data */
          798  +  0,                       /* xUpdate */
          799  +  0,                       /* xBegin */
          800  +  0,                       /* xSync */
          801  +  0,                       /* xCommit */
          802  +  0,                       /* xRollback */
          803  +  0,                       /* xFindMethod */
          804  +  0,                       /* xRename */
          805  +};
          806  +
          807  +#ifdef SQLITE_TEST
          808  +/*
          809  +** For virtual table testing, make a version of the CSV virtual table
          810  +** available that has an xUpdate function.  But the xUpdate always returns
          811  +** SQLITE_READONLY since the CSV file is not really writable.
          812  +*/
          813  +static int csvtabUpdate(sqlite3_vtab *p,int n,sqlite3_value**v,sqlite3_int64*x){
          814  +  return SQLITE_READONLY;
          815  +}
          816  +static sqlite3_module CsvModuleFauxWrite = {
          817  +  0,                       /* iVersion */
          818  +  csvtabCreate,            /* xCreate */
          819  +  csvtabConnect,           /* xConnect */
          820  +  csvtabBestIndex,         /* xBestIndex */
          821  +  csvtabDisconnect,        /* xDisconnect */
          822  +  csvtabDisconnect,        /* xDestroy */
          823  +  csvtabOpen,              /* xOpen - open a cursor */
          824  +  csvtabClose,             /* xClose - close a cursor */
          825  +  csvtabFilter,            /* xFilter - configure scan constraints */
          826  +  csvtabNext,              /* xNext - advance a cursor */
          827  +  csvtabEof,               /* xEof - check for end of scan */
          828  +  csvtabColumn,            /* xColumn - read data */
          829  +  csvtabRowid,             /* xRowid - read data */
          830  +  csvtabUpdate,            /* xUpdate */
          831  +  0,                       /* xBegin */
          832  +  0,                       /* xSync */
          833  +  0,                       /* xCommit */
          834  +  0,                       /* xRollback */
          835  +  0,                       /* xFindMethod */
          836  +  0,                       /* xRename */
          837  +};
          838  +#endif /* SQLITE_TEST */
          839  +
          840  +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
          841  +
          842  +
          843  +#ifdef _WIN32
          844  +__declspec(dllexport)
          845  +#endif
          846  +/* 
          847  +** This routine is called when the extension is loaded.  The new
          848  +** CSV virtual table module is registered with the calling database
          849  +** connection.
          850  +*/
          851  +int sqlite3_csv_init(
          852  +  sqlite3 *db, 
          853  +  char **pzErrMsg, 
          854  +  const sqlite3_api_routines *pApi
          855  +){
          856  +#ifndef SQLITE_OMIT_VIRTUALTABLE	
          857  +  int rc;
          858  +  SQLITE_EXTENSION_INIT2(pApi);
          859  +  rc = sqlite3_create_module(db, "csv", &CsvModule, 0);
          860  +#ifdef SQLITE_TEST
          861  +  if( rc==SQLITE_OK ){
          862  +    rc = sqlite3_create_module(db, "csv_wr", &CsvModuleFauxWrite, 0);
          863  +  }
          864  +#endif
          865  +  return rc;
          866  +#else
          867  +  return SQLITE_OK;
          868  +#endif
          869  +}

Changes to ext/misc/json1.c.

  1206   1206     sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
  1207   1207   }
  1208   1208   #endif /* SQLITE_DEBUG */
  1209   1209   
  1210   1210   /****************************************************************************
  1211   1211   ** Scalar SQL function implementations
  1212   1212   ****************************************************************************/
         1213  +
         1214  +/*
         1215  +** Implementation of the json_QUOTE(VALUE) function.  Return a JSON value
         1216  +** corresponding to the SQL value input.  Mostly this means putting 
         1217  +** double-quotes around strings and returning the unquoted string "null"
         1218  +** when given a NULL input.
         1219  +*/
         1220  +static void jsonQuoteFunc(
         1221  +  sqlite3_context *ctx,
         1222  +  int argc,
         1223  +  sqlite3_value **argv
         1224  +){
         1225  +  JsonString jx;
         1226  +  UNUSED_PARAM(argc);
         1227  +
         1228  +  jsonInit(&jx, ctx);
         1229  +  jsonAppendValue(&jx, argv[0]);
         1230  +  jsonResult(&jx);
         1231  +  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
         1232  +}
  1213   1233   
  1214   1234   /*
  1215   1235   ** Implementation of the json_array(VALUE,...) function.  Return a JSON
  1216   1236   ** array that contains all values given in arguments.  Or if any argument
  1217   1237   ** is a BLOB, throw an error.
  1218   1238   */
  1219   1239   static void jsonArrayFunc(
................................................................................
  2120   2140       { "json",                 1, 0,   jsonRemoveFunc        },
  2121   2141       { "json_array",          -1, 0,   jsonArrayFunc         },
  2122   2142       { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
  2123   2143       { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
  2124   2144       { "json_extract",        -1, 0,   jsonExtractFunc       },
  2125   2145       { "json_insert",         -1, 0,   jsonSetFunc           },
  2126   2146       { "json_object",         -1, 0,   jsonObjectFunc        },
         2147  +    { "json_quote",           1, 0,   jsonQuoteFunc         },
  2127   2148       { "json_remove",         -1, 0,   jsonRemoveFunc        },
  2128   2149       { "json_replace",        -1, 0,   jsonReplaceFunc       },
  2129   2150       { "json_set",            -1, 1,   jsonSetFunc           },
  2130   2151       { "json_type",            1, 0,   jsonTypeFunc          },
  2131   2152       { "json_type",            2, 0,   jsonTypeFunc          },
  2132   2153       { "json_valid",           1, 0,   jsonValidFunc         },
  2133   2154   

Changes to ext/misc/percentile.c.

   163    163     }
   164    164     p->a[p->nUsed++] = y;
   165    165   }
   166    166   
   167    167   /*
   168    168   ** Compare to doubles for sorting using qsort()
   169    169   */
   170         -static int doubleCmp(const void *pA, const void *pB){
          170  +static int SQLITE_CDECL doubleCmp(const void *pA, const void *pB){
   171    171     double a = *(double*)pA;
   172    172     double b = *(double*)pB;
   173    173     if( a==b ) return 0;
   174    174     if( a<b ) return -1;
   175    175     return +1;
   176    176   }
   177    177   

Added ext/misc/scrub.c.

            1  +/*
            2  +** 2016-05-05
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file implements a utility function (and a utility program) that
           14  +** makes a copy of an SQLite database while simultaneously zeroing out all
           15  +** deleted content.
           16  +**
           17  +** Normally (when PRAGMA secure_delete=OFF, which is the default) when SQLite
           18  +** deletes content, it does not overwrite the deleted content but rather marks
           19  +** the region of the file that held that content as being reusable.  This can
           20  +** cause deleted content to recoverable from the database file.  This stale
           21  +** content is removed by the VACUUM command, but VACUUM can be expensive for
           22  +** large databases.  When in PRAGMA secure_delete=ON mode, the deleted content
           23  +** is zeroed, but secure_delete=ON has overhead as well.
           24  +**
           25  +** This utility attempts to make a copy of a complete SQLite database where
           26  +** all of the deleted content is zeroed out in the copy, and it attempts to
           27  +** do so while being faster than running VACUUM.
           28  +**
           29  +** Usage:
           30  +**
           31  +**   int sqlite3_scrub_backup(
           32  +**       const char *zSourceFile,   // Source database filename
           33  +**       const char *zDestFile,     // Destination database filename
           34  +**       char **pzErrMsg            // Write error message here
           35  +**   );
           36  +**
           37  +** Simply call the API above specifying the filename of the source database
           38  +** and the name of the backup copy.  The source database must already exist
           39  +** and can be in active use. (A read lock is held during the backup.)  The
           40  +** destination file should not previously exist.  If the pzErrMsg parameter
           41  +** is non-NULL and if an error occurs, then an error message might be written
           42  +** into memory obtained from sqlite3_malloc() and *pzErrMsg made to point to
           43  +** that error message.  But if the error is an OOM, the error might not be
           44  +** reported.  The routine always returns non-zero if there is an error.
           45  +**
           46  +** If compiled with -DSCRUB_STANDALONE then a main() procedure is added and
           47  +** this file becomes a standalone program that can be run as follows:
           48  +**
           49  +**      ./sqlite3scrub SOURCE DEST
           50  +*/
           51  +#include "sqlite3.h"
           52  +#include <assert.h>
           53  +#include <stdio.h>
           54  +#include <stdlib.h>
           55  +#include <stdarg.h>
           56  +#include <string.h>
           57  +
           58  +typedef struct ScrubState ScrubState;
           59  +typedef unsigned char u8;
           60  +typedef unsigned short u16;
           61  +typedef unsigned int u32;
           62  +
           63  +
           64  +/* State information for a scrub-and-backup operation */
           65  +struct ScrubState {
           66  +  const char *zSrcFile;    /* Name of the source file */
           67  +  const char *zDestFile;   /* Name of the destination file */
           68  +  int rcErr;               /* Error code */
           69  +  char *zErr;              /* Error message text */
           70  +  sqlite3 *dbSrc;          /* Source database connection */
           71  +  sqlite3_file *pSrc;      /* Source file handle */
           72  +  sqlite3 *dbDest;         /* Destination database connection */
           73  +  sqlite3_file *pDest;     /* Destination file handle */
           74  +  u32 szPage;              /* Page size */
           75  +  u32 szUsable;            /* Usable bytes on each page */
           76  +  u32 nPage;               /* Number of pages */
           77  +  u32 iLastPage;           /* Page number of last page written so far*/
           78  +  u8 *page1;               /* Content of page 1 */
           79  +};
           80  +
           81  +/* Store an error message */
           82  +static void scrubBackupErr(ScrubState *p, const char *zFormat, ...){
           83  +  va_list ap;
           84  +  sqlite3_free(p->zErr);
           85  +  va_start(ap, zFormat);
           86  +  p->zErr = sqlite3_vmprintf(zFormat, ap);
           87  +  va_end(ap);
           88  +  if( p->rcErr==0 ) p->rcErr = SQLITE_ERROR;
           89  +}
           90  +
           91  +/* Allocate memory to hold a single page of content */
           92  +static u8 *scrubBackupAllocPage(ScrubState *p){
           93  +  u8 *pPage;
           94  +  if( p->rcErr ) return 0;
           95  +  pPage = sqlite3_malloc( p->szPage );
           96  +  if( pPage==0 ) p->rcErr = SQLITE_NOMEM;
           97  +  return pPage;
           98  +}
           99  +
          100  +/* Read a page from the source database into memory.  Use the memory
          101  +** provided by pBuf if not NULL or allocate a new page if pBuf==NULL.
          102  +*/
          103  +static u8 *scrubBackupRead(ScrubState *p, int pgno, u8 *pBuf){
          104  +  int rc;
          105  +  sqlite3_int64 iOff;
          106  +  u8 *pOut = pBuf;
          107  +  if( p->rcErr ) return 0;
          108  +  if( pOut==0 ){
          109  +    pOut = scrubBackupAllocPage(p);
          110  +    if( pOut==0 ) return 0;
          111  +  }
          112  +  iOff = (pgno-1)*(sqlite3_int64)p->szPage;
          113  +  rc = p->pSrc->pMethods->xRead(p->pSrc, pOut, p->szPage, iOff);
          114  +  if( rc!=SQLITE_OK ){
          115  +    if( pBuf==0 ) sqlite3_free(pOut);
          116  +    pOut = 0;
          117  +    scrubBackupErr(p, "read failed for page %d", pgno);
          118  +    p->rcErr = SQLITE_IOERR;
          119  +  }
          120  +  return pOut;  
          121  +}
          122  +
          123  +/* Write a page to the destination database */
          124  +static void scrubBackupWrite(ScrubState *p, int pgno, const u8 *pData){
          125  +  int rc;
          126  +  sqlite3_int64 iOff;
          127  +  if( p->rcErr ) return;
          128  +  iOff = (pgno-1)*(sqlite3_int64)p->szPage;
          129  +  rc = p->pDest->pMethods->xWrite(p->pDest, pData, p->szPage, iOff);
          130  +  if( rc!=SQLITE_OK ){
          131  +    scrubBackupErr(p, "write failed for page %d", pgno);
          132  +    p->rcErr = SQLITE_IOERR;
          133  +  }
          134  +  if( pgno>p->iLastPage ) p->iLastPage = pgno;
          135  +}
          136  +
          137  +/* Prepare a statement against the "db" database. */
          138  +static sqlite3_stmt *scrubBackupPrepare(
          139  +  ScrubState *p,      /* Backup context */
          140  +  sqlite3 *db,        /* Database to prepare against */
          141  +  const char *zSql    /* SQL statement */
          142  +){
          143  +  sqlite3_stmt *pStmt;
          144  +  if( p->rcErr ) return 0;
          145  +  p->rcErr = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
          146  +  if( p->rcErr ){
          147  +    scrubBackupErr(p, "SQL error \"%s\" on \"%s\"",
          148  +                   sqlite3_errmsg(db), zSql);
          149  +    sqlite3_finalize(pStmt);
          150  +    return 0;
          151  +  }
          152  +  return pStmt;
          153  +}
          154  +
          155  +
          156  +/* Open the source database file */
          157  +static void scrubBackupOpenSrc(ScrubState *p){
          158  +  sqlite3_stmt *pStmt;
          159  +  int rc;
          160  +  /* Open the source database file */
          161  +  p->rcErr = sqlite3_open_v2(p->zSrcFile, &p->dbSrc,
          162  +                 SQLITE_OPEN_READWRITE |
          163  +                 SQLITE_OPEN_URI | SQLITE_OPEN_PRIVATECACHE, 0);
          164  +  if( p->rcErr ){
          165  +    scrubBackupErr(p, "cannot open source database: %s",
          166  +                      sqlite3_errmsg(p->dbSrc));
          167  +    return;
          168  +  }
          169  +  p->rcErr = sqlite3_exec(p->dbSrc, "SELECT 1 FROM sqlite_master; BEGIN;",
          170  +                          0, 0, 0);
          171  +  if( p->rcErr ){
          172  +    scrubBackupErr(p,
          173  +       "cannot start a read transaction on the source database: %s",
          174  +       sqlite3_errmsg(p->dbSrc));
          175  +    return;
          176  +  }
          177  +  rc = sqlite3_wal_checkpoint_v2(p->dbSrc, "main", SQLITE_CHECKPOINT_FULL,
          178  +                                 0, 0);
          179  +  if( rc ){
          180  +    scrubBackupErr(p, "cannot checkpoint the source database");
          181  +    return;
          182  +  }
          183  +  pStmt = scrubBackupPrepare(p, p->dbSrc, "PRAGMA page_size");
          184  +  if( pStmt==0 ) return;
          185  +  rc = sqlite3_step(pStmt);
          186  +  if( rc==SQLITE_ROW ){
          187  +    p->szPage = sqlite3_column_int(pStmt, 0);
          188  +  }else{
          189  +    scrubBackupErr(p, "unable to determine the page size");
          190  +  }
          191  +  sqlite3_finalize(pStmt);
          192  +  if( p->rcErr ) return;
          193  +  pStmt = scrubBackupPrepare(p, p->dbSrc, "PRAGMA page_count");
          194  +  if( pStmt==0 ) return;
          195  +  rc = sqlite3_step(pStmt);
          196  +  if( rc==SQLITE_ROW ){
          197  +    p->nPage = sqlite3_column_int(pStmt, 0);
          198  +  }else{
          199  +    scrubBackupErr(p, "unable to determine the size of the source database");
          200  +  }
          201  +  sqlite3_finalize(pStmt);
          202  +  sqlite3_file_control(p->dbSrc, "main", SQLITE_FCNTL_FILE_POINTER, &p->pSrc);
          203  +  if( p->pSrc==0 || p->pSrc->pMethods==0 ){
          204  +    scrubBackupErr(p, "cannot get the source file handle");
          205  +    p->rcErr = SQLITE_ERROR;
          206  +  }
          207  +}
          208  +
          209  +/* Create and open the destination file */
          210  +static void scrubBackupOpenDest(ScrubState *p){
          211  +  sqlite3_stmt *pStmt;
          212  +  int rc;
          213  +  char *zSql;
          214  +  if( p->rcErr ) return;
          215  +  p->rcErr = sqlite3_open_v2(p->zDestFile, &p->dbDest,
          216  +                 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
          217  +                 SQLITE_OPEN_URI | SQLITE_OPEN_PRIVATECACHE, 0);
          218  +  if( p->rcErr ){
          219  +    scrubBackupErr(p, "cannot open destination database: %s",
          220  +                      sqlite3_errmsg(p->dbDest));
          221  +    return;
          222  +  }
          223  +  zSql = sqlite3_mprintf("PRAGMA page_size(%u);", p->szPage);
          224  +  if( zSql==0 ){
          225  +    p->rcErr = SQLITE_NOMEM;
          226  +    return;
          227  +  }
          228  +  p->rcErr = sqlite3_exec(p->dbDest, zSql, 0, 0, 0);
          229  +  sqlite3_free(zSql);
          230  +  if( p->rcErr ){
          231  +    scrubBackupErr(p,
          232  +       "cannot set the page size on the destination database: %s",
          233  +       sqlite3_errmsg(p->dbDest));
          234  +    return;
          235  +  }
          236  +  sqlite3_exec(p->dbDest, "PRAGMA journal_mode=OFF;", 0, 0, 0);
          237  +  p->rcErr = sqlite3_exec(p->dbDest, "BEGIN EXCLUSIVE;", 0, 0, 0);
          238  +  if( p->rcErr ){
          239  +    scrubBackupErr(p,
          240  +       "cannot start a write transaction on the destination database: %s",
          241  +       sqlite3_errmsg(p->dbDest));
          242  +    return;
          243  +  }
          244  +  pStmt = scrubBackupPrepare(p, p->dbDest, "PRAGMA page_count;");
          245  +  if( pStmt==0 ) return;
          246  +  rc = sqlite3_step(pStmt);
          247  +  if( rc!=SQLITE_ROW ){
          248  +    scrubBackupErr(p, "cannot measure the size of the destination");
          249  +  }else if( sqlite3_column_int(pStmt, 0)>1 ){
          250  +    scrubBackupErr(p, "destination database is not empty - holds %d pages",
          251  +                   sqlite3_column_int(pStmt, 0));
          252  +  }
          253  +  sqlite3_finalize(pStmt);
          254  +  sqlite3_file_control(p->dbDest, "main", SQLITE_FCNTL_FILE_POINTER, &p->pDest);
          255  +  if( p->pDest==0 || p->pDest->pMethods==0 ){
          256  +    scrubBackupErr(p, "cannot get the destination file handle");
          257  +    p->rcErr = SQLITE_ERROR;
          258  +  }
          259  +}
          260  +
          261  +/* Read a 32-bit big-endian integer */
          262  +static u32 scrubBackupInt32(const u8 *a){
          263  +  u32 v = a[3];
          264  +  v += ((u32)a[2])<<8;
          265  +  v += ((u32)a[1])<<16;
          266  +  v += ((u32)a[0])<<24;
          267  +  return v;
          268  +}
          269  +
          270  +/* Read a 16-bit big-endian integer */
          271  +static u32 scrubBackupInt16(const u8 *a){
          272  +  return (a[0]<<8) + a[1];
          273  +}
          274  +
          275  +/*
          276  +** Read a varint.  Put the value in *pVal and return the number of bytes.
          277  +*/
          278  +static int scrubBackupVarint(const u8 *z, sqlite3_int64 *pVal){
          279  +  sqlite3_int64 v = 0;
          280  +  int i;
          281  +  for(i=0; i<8; i++){
          282  +    v = (v<<7) + (z[i]&0x7f);
          283  +    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
          284  +  }
          285  +  v = (v<<8) + (z[i]&0xff);
          286  +  *pVal = v;
          287  +  return 9;
          288  +}
          289  +
          290  +/*
          291  +** Return the number of bytes in a varint.
          292  +*/
          293  +static int scrubBackupVarintSize(const u8 *z){
          294  +  int i;
          295  +  for(i=0; i<8; i++){
          296  +    if( (z[i]&0x80)==0 ){ return i+1; }
          297  +  }
          298  +  return 9;
          299  +}
          300  +
          301  +/*
          302  +** Copy the freelist trunk page given, and all its descendents,
          303  +** zeroing out as much as possible in the process.
          304  +*/
          305  +static void scrubBackupFreelist(ScrubState *p, int pgno, u32 nFree){
          306  +  u8 *a, *aBuf;
          307  +  u32 n, mx;
          308  +
          309  +  if( p->rcErr ) return;
          310  +  aBuf = scrubBackupAllocPage(p);
          311  +  if( aBuf==0 ) return;
          312  + 
          313  +  while( pgno && nFree){
          314  +    a = scrubBackupRead(p, pgno, aBuf);
          315  +    if( a==0 ) break;
          316  +    n = scrubBackupInt32(&a[4]);
          317  +    mx = p->szUsable/4 - 2;
          318  +    if( n<mx ){
          319  +      memset(&a[n*4+8], 0, 4*(mx-n));
          320  +    }
          321  +    scrubBackupWrite(p, pgno, a);
          322  +    pgno = scrubBackupInt32(a);
          323  +#if 0
          324  +    /* There is really no point in copying the freelist leaf pages.
          325  +    ** Simply leave them uninitialized in the destination database.  The
          326  +    ** OS filesystem should zero those pages for us automatically.
          327  +    */
          328  +    for(i=0; i<n && nFree; i++){
          329  +      u32 iLeaf = scrubBackupInt32(&a[i*4+8]);
          330  +      if( aZero==0 ){
          331  +        aZero = scrubBackupAllocPage(p);
          332  +        if( aZero==0 ){ pgno = 0; break; }
          333  +        memset(aZero, 0, p->szPage);
          334  +      }
          335  +      scrubBackupWrite(p, iLeaf, aZero);
          336  +      nFree--;
          337  +    }
          338  +#endif
          339  +  }
          340  +  sqlite3_free(aBuf);
          341  +}
          342  +
          343  +/*
          344  +** Copy an overflow chain from source to destination.  Zero out any
          345  +** unused tail at the end of the overflow chain.
          346  +*/
          347  +static void scrubBackupOverflow(ScrubState *p, int pgno, u32 nByte){
          348  +  u8 *a, *aBuf;
          349  +
          350  +  aBuf = scrubBackupAllocPage(p);
          351  +  if( aBuf==0 ) return;
          352  +  while( nByte>0 && pgno!=0 ){
          353  +    a = scrubBackupRead(p, pgno, aBuf);
          354  +    if( a==0 ) break;
          355  +    if( nByte >= (p->szUsable)-4 ){
          356  +      nByte -= (p->szUsable) - 4;
          357  +    }else{
          358  +      u32 x = (p->szUsable - 4) - nByte;
          359  +      u32 i = p->szUsable - x;
          360  +      memset(&a[i], 0, x);
          361  +      nByte = 0;
          362  +    }
          363  +    scrubBackupWrite(p, pgno, a);
          364  +    pgno = scrubBackupInt32(a);
          365  +  }
          366  +  sqlite3_free(aBuf);      
          367  +}
          368  +   
          369  +
          370  +/*
          371  +** Copy B-Tree page pgno, and all of its children, from source to destination.
          372  +** Zero out deleted content during the copy.
          373  +*/
          374  +static void scrubBackupBtree(ScrubState *p, int pgno, int iDepth){
          375  +  u8 *a;
          376  +  u32 i, n, pc;
          377  +  u32 nCell;
          378  +  u32 nPrefix;
          379  +  u32 szHdr;
          380  +  u32 iChild;
          381  +  u8 *aTop;
          382  +  u8 *aCell;
          383  +  u32 x, y;
          384  +  int ln = 0;
          385  +
          386  +  
          387  +  if( p->rcErr ) return;
          388  +  if( iDepth>50 ){
          389  +    scrubBackupErr(p, "corrupt: b-tree too deep at page %d", pgno);
          390  +    return;
          391  +  }
          392  +  if( pgno==1 ){
          393  +    a = p->page1;
          394  +  }else{
          395  +    a = scrubBackupRead(p, pgno, 0);
          396  +    if( a==0 ) return;
          397  +  }
          398  +  nPrefix = pgno==1 ? 100 : 0;
          399  +  aTop = &a[nPrefix];
          400  +  szHdr = 8 + 4*(aTop[0]==0x02 || aTop[0]==0x05);
          401  +  aCell = aTop + szHdr;
          402  +  nCell = scrubBackupInt16(&aTop[3]);
          403  +
          404  +  /* Zero out the gap between the cell index and the start of the
          405  +  ** cell content area */
          406  +  x = scrubBackupInt16(&aTop[5]);  /* First byte of cell content area */
          407  +  if( x>p->szUsable ){ ln=__LINE__; goto btree_corrupt; }
          408  +  y = szHdr + nPrefix + nCell*2;
          409  +  if( y>x ){ ln=__LINE__; goto btree_corrupt; }
          410  +  if( y<x ) memset(a+y, 0, x-y);  /* Zero the gap */
          411  +
          412  +  /* Zero out all the free blocks */  
          413  +  pc = scrubBackupInt16(&aTop[1]);
          414  +  if( pc>0 && pc<x ){ ln=__LINE__; goto btree_corrupt; }
          415  +  while( pc ){
          416  +    if( pc>(p->szUsable)-4 ){ ln=__LINE__; goto btree_corrupt; }
          417  +    n = scrubBackupInt16(&a[pc+2]);
          418  +    if( pc+n>(p->szUsable) ){ ln=__LINE__; goto btree_corrupt; }
          419  +    if( n>4 ) memset(&a[pc+4], 0, n-4);
          420  +    x = scrubBackupInt16(&a[pc]);
          421  +    if( x<pc+4 && x>0 ){ ln=__LINE__; goto btree_corrupt; }
          422  +    pc = x;
          423  +  }
          424  +
          425  +  /* Write this one page */
          426  +  scrubBackupWrite(p, pgno, a);
          427  +
          428  +  /* Walk the tree and process child pages */
          429  +  for(i=0; i<nCell; i++){
          430  +    u32 X, M, K, nLocal;
          431  +    sqlite3_int64 P;
          432  +    pc = scrubBackupInt16(&aCell[i*2]);
          433  +    if( pc <= szHdr ){ ln=__LINE__; goto btree_corrupt; }
          434  +    if( pc > p->szUsable-3 ){ ln=__LINE__; goto btree_corrupt; }
          435  +    if( aTop[0]==0x05 || aTop[0]==0x02 ){
          436  +      if( pc+4 > p->szUsable ){ ln=__LINE__; goto btree_corrupt; }
          437  +      iChild = scrubBackupInt32(&a[pc]);
          438  +      pc += 4;
          439  +      scrubBackupBtree(p, iChild, iDepth+1);
          440  +      if( aTop[0]==0x05 ) continue;
          441  +    }
          442  +    pc += scrubBackupVarint(&a[pc], &P);
          443  +    if( pc >= p->szUsable ){ ln=__LINE__; goto btree_corrupt; }
          444  +    if( aTop[0]==0x0d ){
          445  +      X = p->szUsable - 35;
          446  +    }else{
          447  +      X = ((p->szUsable - 12)*64/255) - 23;
          448  +    }
          449  +    if( P<=X ){
          450  +      /* All content is local.  No overflow */
          451  +      continue;
          452  +    }
          453  +    M = ((p->szUsable - 12)*32/255)-23;
          454  +    K = M + ((P-M)%(p->szUsable-4));
          455  +    if( aTop[0]==0x0d ){
          456  +      pc += scrubBackupVarintSize(&a[pc]);
          457  +      if( pc > (p->szUsable-4) ){ ln=__LINE__; goto btree_corrupt; }
          458  +    }
          459  +    nLocal = K<=X ? K : M;
          460  +    if( pc+nLocal > p->szUsable-4 ){ ln=__LINE__; goto btree_corrupt; }
          461  +    iChild = scrubBackupInt32(&a[pc+nLocal]);
          462  +    scrubBackupOverflow(p, iChild, P-nLocal);
          463  +  }
          464  +
          465  +  /* Walk the right-most tree */
          466  +  if( aTop[0]==0x05 || aTop[0]==0x02 ){
          467  +    iChild = scrubBackupInt32(&aTop[8]);
          468  +    scrubBackupBtree(p, iChild, iDepth+1);
          469  +  }
          470  +
          471  +  /* All done */
          472  +  if( pgno>1 ) sqlite3_free(a);
          473  +  return;
          474  +
          475  +btree_corrupt:
          476  +  scrubBackupErr(p, "corruption on page %d of source database (errid=%d)",
          477  +                 pgno, ln);
          478  +  if( pgno>1 ) sqlite3_free(a);  
          479  +}
          480  +
          481  +/*
          482  +** Copy all ptrmap pages from source to destination.
          483  +** This routine is only called if the source database is in autovacuum
          484  +** or incremental vacuum mode.
          485  +*/
          486  +static void scrubBackupPtrmap(ScrubState *p){
          487  +  u32 pgno = 2;
          488  +  u32 J = p->szUsable/5;
          489  +  u32 iLock = (1073742335/p->szPage)+1;
          490  +  u8 *a, *pBuf;
          491  +  if( p->rcErr ) return;
          492  +  pBuf = scrubBackupAllocPage(p);
          493  +  if( pBuf==0 ) return;
          494  +  while( pgno<=p->nPage ){
          495  +    a = scrubBackupRead(p, pgno, pBuf);
          496  +    if( a==0 ) break;
          497  +    scrubBackupWrite(p, pgno, a);
          498  +    pgno += J+1;
          499  +    if( pgno==iLock ) pgno++;
          500  +  }
          501  +  sqlite3_free(pBuf);
          502  +}
          503  +
          504  +int sqlite3_scrub_backup(
          505  +  const char *zSrcFile,    /* Source file */
          506  +  const char *zDestFile,   /* Destination file */
          507  +  char **pzErr             /* Write error here if non-NULL */
          508  +){
          509  +  ScrubState s;
          510  +  u32 n, i;
          511  +  sqlite3_stmt *pStmt;
          512  +
          513  +  memset(&s, 0, sizeof(s));
          514  +  s.zSrcFile = zSrcFile;
          515  +  s.zDestFile = zDestFile;
          516  +
          517  +  /* Open both source and destination databases */
          518  +  scrubBackupOpenSrc(&s);
          519  +  scrubBackupOpenDest(&s);
          520  +
          521  +  /* Read in page 1 */
          522  +  s.page1 = scrubBackupRead(&s, 1, 0);
          523  +  if( s.page1==0 ) goto scrub_abort;
          524  +  s.szUsable = s.szPage - s.page1[20];
          525  +
          526  +  /* Copy the freelist */    
          527  +  n = scrubBackupInt32(&s.page1[36]);
          528  +  i = scrubBackupInt32(&s.page1[32]);
          529  +  if( n ) scrubBackupFreelist(&s, i, n);
          530  +
          531  +  /* Copy ptrmap pages */
          532  +  n = scrubBackupInt32(&s.page1[52]);
          533  +  if( n ) scrubBackupPtrmap(&s);
          534  +
          535  +  /* Copy all of the btrees */
          536  +  scrubBackupBtree(&s, 1, 0);
          537  +  pStmt = scrubBackupPrepare(&s, s.dbSrc,
          538  +       "SELECT rootpage FROM sqlite_master WHERE coalesce(rootpage,0)>0");
          539  +  if( pStmt==0 ) goto scrub_abort;
          540  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          541  +    i = (u32)sqlite3_column_int(pStmt, 0);
          542  +    scrubBackupBtree(&s, i, 0);
          543  +  }
          544  +  sqlite3_finalize(pStmt);
          545  +
          546  +  /* If the last page of the input db file is a free-list leaf, then the
          547  +  ** backup file on disk is still smaller than the size indicated within 
          548  +  ** the database header. In this case, write a page of zeroes to the 
          549  +  ** last page of the backup database so that SQLite does not mistakenly
          550  +  ** think the db is corrupt.  */
          551  +  if( s.iLastPage<s.nPage ){
          552  +    u8 *aZero = scrubBackupAllocPage(&s);
          553  +    if( aZero ){
          554  +      memset(aZero, 0, s.szPage);
          555  +      scrubBackupWrite(&s, s.nPage, aZero);
          556  +      sqlite3_free(aZero);
          557  +    }
          558  +  }
          559  +
          560  +scrub_abort:    
          561  +  /* Close the destination database without closing the transaction. If we
          562  +  ** commit, page zero will be overwritten. */
          563  +  sqlite3_close(s.dbDest);
          564  +
          565  +  /* But do close out the read-transaction on the source database */
          566  +  sqlite3_exec(s.dbSrc, "COMMIT;", 0, 0, 0);
          567  +  sqlite3_close(s.dbSrc);
          568  +  sqlite3_free(s.page1);
          569  +  if( pzErr ){
          570  +    *pzErr = s.zErr;
          571  +  }else{
          572  +    sqlite3_free(s.zErr);
          573  +  }
          574  +  return s.rcErr;
          575  +}   
          576  +
          577  +#ifdef SCRUB_STANDALONE
          578  +/* Error and warning log */
          579  +static void errorLogCallback(void *pNotUsed, int iErr, const char *zMsg){
          580  +  const char *zType;
          581  +  switch( iErr&0xff ){
          582  +    case SQLITE_WARNING: zType = "WARNING";  break;
          583  +    case SQLITE_NOTICE:  zType = "NOTICE";   break;
          584  +    default:             zType = "ERROR";    break;
          585  +  }
          586  +  fprintf(stderr, "%s: %s\n", zType, zMsg);
          587  +}
          588  +
          589  +/* The main() routine when this utility is run as a stand-alone program */
          590  +int main(int argc, char **argv){
          591  +  char *zErr = 0;
          592  +  int rc;
          593  +  if( argc!=3 ){
          594  +    fprintf(stderr,"Usage: %s SOURCE DESTINATION\n", argv[0]);
          595  +    exit(1);
          596  +  }
          597  +  sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, 0);
          598  +  rc = sqlite3_scrub_backup(argv[1], argv[2], &zErr);
          599  +  if( rc==SQLITE_NOMEM ){
          600  +    fprintf(stderr, "%s: out of memory\n", argv[0]);
          601  +    exit(1);
          602  +  }
          603  +  if( zErr ){
          604  +    fprintf(stderr, "%s: %s\n", argv[0], zErr);
          605  +    sqlite3_free(zErr);
          606  +    exit(1);
          607  +  }
          608  +  return 0;
          609  +}
          610  +#endif

Changes to ext/misc/spellfix.c.

  2227   2227     return iDistance + 32 - iLog2;
  2228   2228   }
  2229   2229   
  2230   2230   /*
  2231   2231   ** Compare two spellfix1_row objects for sorting purposes in qsort() such
  2232   2232   ** that they sort in order of increasing distance.
  2233   2233   */
  2234         -static int spellfix1RowCompare(const void *A, const void *B){
         2234  +static int SQLITE_CDECL spellfix1RowCompare(const void *A, const void *B){
  2235   2235     const struct spellfix1_row *a = (const struct spellfix1_row*)A;
  2236   2236     const struct spellfix1_row *b = (const struct spellfix1_row*)B;
  2237   2237     return a->iScore - b->iScore;
  2238   2238   }
  2239   2239   
  2240   2240   /*
  2241   2241   ** A structure used to pass information from spellfix1FilterForMatch()

Added ext/misc/vfsstat.c.

            1  +/*
            2  +** 2016-05-27
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains the implementation of an SQLite vfs shim that
           14  +** tracks I/O.  Access to the accumulated status counts is provided using
           15  +** an eponymous virtual table.
           16  +*/
           17  +#include <sqlite3ext.h>
           18  +SQLITE_EXTENSION_INIT1
           19  +
           20  +/*
           21  +** This module contains code for a wrapper VFS that cause stats for
           22  +** most VFS calls to be recorded.
           23  +**
           24  +** To use this module, first compile it as a loadable extension.  See
           25  +** https://www.sqlite.org/loadext.html#build for compilations instructions.
           26  +**
           27  +** After compliing, load this extension, then open database connections to be
           28  +** measured.  Query usages status using the vfsstat virtual table:
           29  +**
           30  +**         SELECT * FROM vfsstat;
           31  +**
           32  +** Reset counters using UPDATE statements against vfsstat:
           33  +**
           34  +**         UPDATE vfsstat SET count=0;
           35  +**
           36  +** EXAMPLE SCRIPT:
           37  +**
           38  +**      .load ./vfsstat
           39  +**      .open test.db
           40  +**      DROP TABLE IF EXISTS t1;
           41  +**      CREATE TABLE t1(x,y);
           42  +**      INSERT INTO t1 VALUES(123, randomblob(5000));
           43  +**      CREATE INDEX t1x ON t1(x);
           44  +**      DROP TABLE t1;
           45  +**      VACUUM;
           46  +**      SELECT * FROM vfsstat WHERE count>0;
           47  +**
           48  +** LIMITATIONS:
           49  +** 
           50  +** This module increments counters without using mutex protection.  So if
           51  +** two or more threads try to use this module at the same time, race conditions
           52  +** may occur which mess up the counts.  This is harmless, other than giving
           53  +** incorrect statistics.
           54  +*/
           55  +#include <string.h>
           56  +#include <stdlib.h>
           57  +#include <assert.h>
           58  +
           59  +/*
           60  +** File types
           61  +*/
           62  +#define VFSSTAT_MAIN         0   /* Main database file */
           63  +#define VFSSTAT_JOURNAL      1   /* Rollback journal */
           64  +#define VFSSTAT_WAL          2   /* Write-ahead log file */
           65  +#define VFSSTAT_MASTERJRNL   3   /* Master journal */
           66  +#define VFSSTAT_SUBJRNL      4   /* Subjournal */
           67  +#define VFSSTAT_TEMPDB       5   /* TEMP database */
           68  +#define VFSSTAT_TEMPJRNL     6   /* Journal for TEMP database */
           69  +#define VFSSTAT_TRANSIENT    7   /* Transient database */
           70  +#define VFSSTAT_ANY          8   /* Unspecified file type */
           71  +#define VFSSTAT_nFile        9   /* This many file types */
           72  +
           73  +/* Names of the file types.  These are allowed values for the
           74  +** first column of the vfsstat virtual table.
           75  +*/
           76  +static const char *azFile[] = {
           77  +  "database", "journal", "wal", "master-journal", "sub-journal",
           78  +  "temp-database", "temp-journal", "transient-db", "*"
           79  +};
           80  +
           81  +/*
           82  +** Stat types
           83  +*/
           84  +#define VFSSTAT_BYTESIN      0   /* Bytes read in */
           85  +#define VFSSTAT_BYTESOUT     1   /* Bytes written out */   
           86  +#define VFSSTAT_READ         2   /* Read requests */
           87  +#define VFSSTAT_WRITE        3   /* Write requests */
           88  +#define VFSSTAT_SYNC         4   /* Syncs */
           89  +#define VFSSTAT_OPEN         5   /* File opens */
           90  +#define VFSSTAT_LOCK         6   /* Lock requests */
           91  +#define VFSSTAT_ACCESS       0   /* xAccess calls.  filetype==ANY only */
           92  +#define VFSSTAT_DELETE       1   /* xDelete calls.  filetype==ANY only */
           93  +#define VFSSTAT_FULLPATH     2   /* xFullPathname calls.  ANY only */
           94  +#define VFSSTAT_RANDOM       3   /* xRandomness calls.    ANY only */
           95  +#define VFSSTAT_SLEEP        4   /* xSleep calls.         ANY only */
           96  +#define VFSSTAT_CURTIME      5   /* xCurrentTime calls.   ANY only */
           97  +#define VFSSTAT_nStat        7   /* This many stat types */
           98  +
           99  +
          100  +/* Names for the second column of the vfsstat virtual table for all
          101  +** cases except when the first column is "*" or VFSSTAT_ANY. */
          102  +static const char *azStat[] = {
          103  +  "bytes-in", "bytes-out", "read", "write", "sync", "open", "lock",
          104  +};
          105  +static const char *azStatAny[] = {
          106  +  "access", "delete", "fullpathname", "randomness", "sleep", "currenttimestamp",
          107  +  "not-used"
          108  +};
          109  +
          110  +/* Total number of counters */
          111  +#define VFSSTAT_MXCNT  (VFSSTAT_nStat*VFSSTAT_nFile)
          112  +
          113  +/*
          114  +** Performance stats are collected in an instance of the following
          115  +** global array.
          116  +*/
          117  +static sqlite3_uint64 aVfsCnt[VFSSTAT_MXCNT];
          118  +
          119  +/*
          120  +** Access to a specific counter
          121  +*/
          122  +#define STATCNT(filetype,stat) (aVfsCnt[(filetype)*VFSSTAT_nStat+(stat)])
          123  +
          124  +/*
          125  +** Forward declaration of objects used by this utility
          126  +*/
          127  +typedef struct VStatVfs VStatVfs;
          128  +typedef struct VStatFile VStatFile;
          129  +
          130  +/* An instance of the VFS */
          131  +struct VStatVfs {
          132  +  sqlite3_vfs base;               /* VFS methods */
          133  +  sqlite3_vfs *pVfs;              /* Parent VFS */
          134  +};
          135  +
          136  +/* An open file */
          137  +struct VStatFile {
          138  +  sqlite3_file base;              /* IO methods */
          139  +  sqlite3_file *pReal;            /* Underlying file handle */
          140  +  unsigned char eFiletype;        /* What type of file is this */
          141  +};
          142  +
          143  +#define REALVFS(p) (((VStatVfs*)(p))->pVfs)
          144  +
          145  +/*
          146  +** Methods for VStatFile
          147  +*/
          148  +static int vstatClose(sqlite3_file*);
          149  +static int vstatRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
          150  +static int vstatWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
          151  +static int vstatTruncate(sqlite3_file*, sqlite3_int64 size);
          152  +static int vstatSync(sqlite3_file*, int flags);
          153  +static int vstatFileSize(sqlite3_file*, sqlite3_int64 *pSize);
          154  +static int vstatLock(sqlite3_file*, int);
          155  +static int vstatUnlock(sqlite3_file*, int);
          156  +static int vstatCheckReservedLock(sqlite3_file*, int *pResOut);
          157  +static int vstatFileControl(sqlite3_file*, int op, void *pArg);
          158  +static int vstatSectorSize(sqlite3_file*);
          159  +static int vstatDeviceCharacteristics(sqlite3_file*);
          160  +static int vstatShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
          161  +static int vstatShmLock(sqlite3_file*, int offset, int n, int flags);
          162  +static void vstatShmBarrier(sqlite3_file*);
          163  +static int vstatShmUnmap(sqlite3_file*, int deleteFlag);
          164  +static int vstatFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
          165  +static int vstatUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
          166  +
          167  +/*
          168  +** Methods for VStatVfs
          169  +*/
          170  +static int vstatOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
          171  +static int vstatDelete(sqlite3_vfs*, const char *zName, int syncDir);
          172  +static int vstatAccess(sqlite3_vfs*, const char *zName, int flags, int *);
          173  +static int vstatFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
          174  +static void *vstatDlOpen(sqlite3_vfs*, const char *zFilename);
          175  +static void vstatDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
          176  +static void (*vstatDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
          177  +static void vstatDlClose(sqlite3_vfs*, void*);
          178  +static int vstatRandomness(sqlite3_vfs*, int nByte, char *zOut);
          179  +static int vstatSleep(sqlite3_vfs*, int microseconds);
          180  +static int vstatCurrentTime(sqlite3_vfs*, double*);
          181  +static int vstatGetLastError(sqlite3_vfs*, int, char *);
          182  +static int vstatCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
          183  +
          184  +static VStatVfs vstat_vfs = {
          185  +  {
          186  +    2,                            /* iVersion */
          187  +    0,                            /* szOsFile (set by register_vstat()) */
          188  +    1024,                         /* mxPathname */
          189  +    0,                            /* pNext */
          190  +    "vfslog",                     /* zName */
          191  +    0,                            /* pAppData */
          192  +    vstatOpen,                     /* xOpen */
          193  +    vstatDelete,                   /* xDelete */
          194  +    vstatAccess,                   /* xAccess */
          195  +    vstatFullPathname,             /* xFullPathname */
          196  +    vstatDlOpen,                   /* xDlOpen */
          197  +    vstatDlError,                  /* xDlError */
          198  +    vstatDlSym,                    /* xDlSym */
          199  +    vstatDlClose,                  /* xDlClose */
          200  +    vstatRandomness,               /* xRandomness */
          201  +    vstatSleep,                    /* xSleep */
          202  +    vstatCurrentTime,              /* xCurrentTime */
          203  +    vstatGetLastError,             /* xGetLastError */
          204  +    vstatCurrentTimeInt64          /* xCurrentTimeInt64 */
          205  +  },
          206  +  0
          207  +};
          208  +
          209  +static const sqlite3_io_methods vstat_io_methods = {
          210  +  3,                              /* iVersion */
          211  +  vstatClose,                      /* xClose */
          212  +  vstatRead,                       /* xRead */
          213  +  vstatWrite,                      /* xWrite */
          214  +  vstatTruncate,                   /* xTruncate */
          215  +  vstatSync,                       /* xSync */
          216  +  vstatFileSize,                   /* xFileSize */
          217  +  vstatLock,                       /* xLock */
          218  +  vstatUnlock,                     /* xUnlock */
          219  +  vstatCheckReservedLock,          /* xCheckReservedLock */
          220  +  vstatFileControl,                /* xFileControl */
          221  +  vstatSectorSize,                 /* xSectorSize */
          222  +  vstatDeviceCharacteristics,      /* xDeviceCharacteristics */
          223  +  vstatShmMap,                     /* xShmMap */
          224  +  vstatShmLock,                    /* xShmLock */
          225  +  vstatShmBarrier,                 /* xShmBarrier */
          226  +  vstatShmUnmap,                   /* xShmUnmap */
          227  +  vstatFetch,                      /* xFetch */
          228  +  vstatUnfetch                     /* xUnfetch */
          229  +};
          230  +
          231  +
          232  +
          233  +/*
          234  +** Close an vstat-file.
          235  +*/
          236  +static int vstatClose(sqlite3_file *pFile){
          237  +  VStatFile *p = (VStatFile *)pFile;
          238  +  int rc = SQLITE_OK;
          239  +
          240  +  if( p->pReal->pMethods ){
          241  +    rc = p->pReal->pMethods->xClose(p->pReal);
          242  +  }
          243  +  return rc;
          244  +}
          245  +
          246  +
          247  +/*
          248  +** Read data from an vstat-file.
          249  +*/
          250  +static int vstatRead(
          251  +  sqlite3_file *pFile, 
          252  +  void *zBuf, 
          253  +  int iAmt, 
          254  +  sqlite_int64 iOfst
          255  +){
          256  +  int rc;
          257  +  VStatFile *p = (VStatFile *)pFile;
          258  +
          259  +  rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
          260  +  STATCNT(p->eFiletype,VFSSTAT_READ)++;
          261  +  if( rc==SQLITE_OK ){
          262  +    STATCNT(p->eFiletype,VFSSTAT_BYTESIN) += iAmt;
          263  +  }
          264  +  return rc;
          265  +}
          266  +
          267  +/*
          268  +** Write data to an vstat-file.
          269  +*/
          270  +static int vstatWrite(
          271  +  sqlite3_file *pFile,
          272  +  const void *z,
          273  +  int iAmt,
          274  +  sqlite_int64 iOfst
          275  +){
          276  +  int rc;
          277  +  VStatFile *p = (VStatFile *)pFile;
          278  +
          279  +  rc = p->pReal->pMethods->xWrite(p->pReal, z, iAmt, iOfst);
          280  +  STATCNT(p->eFiletype,VFSSTAT_WRITE)++;
          281  +  if( rc==SQLITE_OK ){
          282  +    STATCNT(p->eFiletype,VFSSTAT_BYTESOUT) += iAmt;
          283  +  }
          284  +  return rc;
          285  +}
          286  +
          287  +/*
          288  +** Truncate an vstat-file.
          289  +*/
          290  +static int vstatTruncate(sqlite3_file *pFile, sqlite_int64 size){
          291  +  int rc;
          292  +  VStatFile *p = (VStatFile *)pFile;
          293  +  rc = p->pReal->pMethods->xTruncate(p->pReal, size);
          294  +  return rc;
          295  +}
          296  +
          297  +/*
          298  +** Sync an vstat-file.
          299  +*/
          300  +static int vstatSync(sqlite3_file *pFile, int flags){
          301  +  int rc;
          302  +  VStatFile *p = (VStatFile *)pFile;
          303  +  rc = p->pReal->pMethods->xSync(p->pReal, flags);
          304  +  STATCNT(p->eFiletype,VFSSTAT_SYNC)++;
          305  +  return rc;
          306  +}
          307  +
          308  +/*
          309  +** Return the current file-size of an vstat-file.
          310  +*/
          311  +static int vstatFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
          312  +  int rc;
          313  +  VStatFile *p = (VStatFile *)pFile;
          314  +  rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
          315  +  return rc;
          316  +}
          317  +
          318  +/*
          319  +** Lock an vstat-file.
          320  +*/
          321  +static int vstatLock(sqlite3_file *pFile, int eLock){
          322  +  int rc;
          323  +  VStatFile *p = (VStatFile *)pFile;
          324  +  rc = p->pReal->pMethods->xLock(p->pReal, eLock);
          325  +  STATCNT(p->eFiletype,VFSSTAT_LOCK)++;
          326  +  return rc;
          327  +}
          328  +
          329  +/*
          330  +** Unlock an vstat-file.
          331  +*/
          332  +static int vstatUnlock(sqlite3_file *pFile, int eLock){
          333  +  int rc;
          334  +  VStatFile *p = (VStatFile *)pFile;
          335  +  rc = p->pReal->pMethods->xUnlock(p->pReal, eLock);
          336  +  STATCNT(p->eFiletype,VFSSTAT_LOCK)++;
          337  +  return rc;
          338  +}
          339  +
          340  +/*
          341  +** Check if another file-handle holds a RESERVED lock on an vstat-file.
          342  +*/
          343  +static int vstatCheckReservedLock(sqlite3_file *pFile, int *pResOut){
          344  +  int rc;
          345  +  VStatFile *p = (VStatFile *)pFile;
          346  +  rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
          347  +  STATCNT(p->eFiletype,VFSSTAT_LOCK)++;
          348  +  return rc;
          349  +}
          350  +
          351  +/*
          352  +** File control method. For custom operations on an vstat-file.
          353  +*/
          354  +static int vstatFileControl(sqlite3_file *pFile, int op, void *pArg){
          355  +  VStatFile *p = (VStatFile *)pFile;
          356  +  int rc;
          357  +  rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
          358  +  if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
          359  +    *(char**)pArg = sqlite3_mprintf("vstat/%z", *(char**)pArg);
          360  +  }
          361  +  return rc;
          362  +}
          363  +
          364  +/*
          365  +** Return the sector-size in bytes for an vstat-file.
          366  +*/
          367  +static int vstatSectorSize(sqlite3_file *pFile){
          368  +  int rc;
          369  +  VStatFile *p = (VStatFile *)pFile;
          370  +  rc = p->pReal->pMethods->xSectorSize(p->pReal);
          371  +  return rc;
          372  +}
          373  +
          374  +/*
          375  +** Return the device characteristic flags supported by an vstat-file.
          376  +*/
          377  +static int vstatDeviceCharacteristics(sqlite3_file *pFile){
          378  +  int rc;
          379  +  VStatFile *p = (VStatFile *)pFile;
          380  +  rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
          381  +  return rc;
          382  +}
          383  +
          384  +/* Create a shared memory file mapping */
          385  +static int vstatShmMap(
          386  +  sqlite3_file *pFile,
          387  +  int iPg,
          388  +  int pgsz,
          389  +  int bExtend,
          390  +  void volatile **pp
          391  +){
          392  +  VStatFile *p = (VStatFile *)pFile;
          393  +  return p->pReal->pMethods->xShmMap(p->pReal, iPg, pgsz, bExtend, pp);
          394  +}
          395  +
          396  +/* Perform locking on a shared-memory segment */
          397  +static int vstatShmLock(sqlite3_file *pFile, int offset, int n, int flags){
          398  +  VStatFile *p = (VStatFile *)pFile;
          399  +  return p->pReal->pMethods->xShmLock(p->pReal, offset, n, flags);
          400  +}
          401  +
          402  +/* Memory barrier operation on shared memory */
          403  +static void vstatShmBarrier(sqlite3_file *pFile){
          404  +  VStatFile *p = (VStatFile *)pFile;
          405  +  p->pReal->pMethods->xShmBarrier(p->pReal);
          406  +}
          407  +
          408  +/* Unmap a shared memory segment */
          409  +static int vstatShmUnmap(sqlite3_file *pFile, int deleteFlag){
          410  +  VStatFile *p = (VStatFile *)pFile;
          411  +  return p->pReal->pMethods->xShmUnmap(p->pReal, deleteFlag);
          412  +}
          413  +
          414  +/* Fetch a page of a memory-mapped file */
          415  +static int vstatFetch(
          416  +  sqlite3_file *pFile,
          417  +  sqlite3_int64 iOfst,
          418  +  int iAmt,
          419  +  void **pp
          420  +){
          421  +  VStatFile *p = (VStatFile *)pFile;
          422  +  return p->pReal->pMethods->xFetch(p->pReal, iOfst, iAmt, pp);
          423  +}
          424  +
          425  +/* Release a memory-mapped page */
          426  +static int vstatUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
          427  +  VStatFile *p = (VStatFile *)pFile;
          428  +  return p->pReal->pMethods->xUnfetch(p->pReal, iOfst, pPage);
          429  +}
          430  +
          431  +/*
          432  +** Open an vstat file handle.
          433  +*/
          434  +static int vstatOpen(
          435  +  sqlite3_vfs *pVfs,
          436  +  const char *zName,
          437  +  sqlite3_file *pFile,
          438  +  int flags,
          439  +  int *pOutFlags
          440  +){
          441  +  int rc;
          442  +  VStatFile *p = (VStatFile*)pFile;
          443  +
          444  +  p->pReal = (sqlite3_file*)&p[1];
          445  +  rc = REALVFS(pVfs)->xOpen(REALVFS(pVfs), zName, p->pReal, flags, pOutFlags);
          446  +  if( flags & SQLITE_OPEN_MAIN_DB ){
          447  +    p->eFiletype = VFSSTAT_MAIN;
          448  +  }else if( flags & SQLITE_OPEN_MAIN_JOURNAL ){
          449  +    p->eFiletype = VFSSTAT_JOURNAL;
          450  +  }else if( flags & SQLITE_OPEN_WAL ){
          451  +    p->eFiletype = VFSSTAT_WAL;
          452  +  }else if( flags & SQLITE_OPEN_MASTER_JOURNAL ){
          453  +    p->eFiletype = VFSSTAT_MASTERJRNL;
          454  +  }else if( flags & SQLITE_OPEN_SUBJOURNAL ){
          455  +    p->eFiletype = VFSSTAT_SUBJRNL;
          456  +  }else if( flags & SQLITE_OPEN_TEMP_DB ){
          457  +    p->eFiletype = VFSSTAT_TEMPDB;
          458  +  }else if( flags & SQLITE_OPEN_TEMP_JOURNAL ){
          459  +    p->eFiletype = VFSSTAT_TEMPJRNL;
          460  +  }else{
          461  +    p->eFiletype = VFSSTAT_TRANSIENT;
          462  +  }
          463  +  STATCNT(p->eFiletype,VFSSTAT_OPEN)++;
          464  +  pFile->pMethods = rc ? 0 : &vstat_io_methods;
          465  +  return rc;
          466  +}
          467  +
          468  +/*
          469  +** Delete the file located at zPath. If the dirSync argument is true,
          470  +** ensure the file-system modifications are synced to disk before
          471  +** returning.
          472  +*/
          473  +static int vstatDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
          474  +  int rc;
          475  +  rc = REALVFS(pVfs)->xDelete(REALVFS(pVfs), zPath, dirSync);
          476  +  STATCNT(VFSSTAT_ANY,VFSSTAT_DELETE)++;
          477  +  return rc;
          478  +}
          479  +
          480  +/*
          481  +** Test for access permissions. Return true if the requested permission
          482  +** is available, or false otherwise.
          483  +*/
          484  +static int vstatAccess(
          485  +  sqlite3_vfs *pVfs, 
          486  +  const char *zPath, 
          487  +  int flags, 
          488  +  int *pResOut
          489  +){
          490  +  int rc;
          491  +  rc = REALVFS(pVfs)->xAccess(REALVFS(pVfs), zPath, flags, pResOut);
          492  +  STATCNT(VFSSTAT_ANY,VFSSTAT_ACCESS)++;
          493  +  return rc;
          494  +}
          495  +
          496  +/*
          497  +** Populate buffer zOut with the full canonical pathname corresponding
          498  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
          499  +** of at least (INST_MAX_PATHNAME+1) bytes.
          500  +*/
          501  +static int vstatFullPathname(
          502  +  sqlite3_vfs *pVfs, 
          503  +  const char *zPath, 
          504  +  int nOut, 
          505  +  char *zOut
          506  +){
          507  +  STATCNT(VFSSTAT_ANY,VFSSTAT_FULLPATH)++;
          508  +  return REALVFS(pVfs)->xFullPathname(REALVFS(pVfs), zPath, nOut, zOut);
          509  +}
          510  +
          511  +/*
          512  +** Open the dynamic library located at zPath and return a handle.
          513  +*/
          514  +static void *vstatDlOpen(sqlite3_vfs *pVfs, const char *zPath){
          515  +  return REALVFS(pVfs)->xDlOpen(REALVFS(pVfs), zPath);
          516  +}
          517  +
          518  +/*
          519  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
          520  +** utf-8 string describing the most recent error encountered associated 
          521  +** with dynamic libraries.
          522  +*/
          523  +static void vstatDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
          524  +  REALVFS(pVfs)->xDlError(REALVFS(pVfs), nByte, zErrMsg);
          525  +}
          526  +
          527  +/*
          528  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
          529  +*/
          530  +static void (*vstatDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
          531  +  return REALVFS(pVfs)->xDlSym(REALVFS(pVfs), p, zSym);
          532  +}
          533  +
          534  +/*
          535  +** Close the dynamic library handle pHandle.
          536  +*/
          537  +static void vstatDlClose(sqlite3_vfs *pVfs, void *pHandle){
          538  +  REALVFS(pVfs)->xDlClose(REALVFS(pVfs), pHandle);
          539  +}
          540  +
          541  +/*
          542  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
          543  +** random data.
          544  +*/
          545  +static int vstatRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
          546  +  STATCNT(VFSSTAT_ANY,VFSSTAT_RANDOM)++;
          547  +  return REALVFS(pVfs)->xRandomness(REALVFS(pVfs), nByte, zBufOut);
          548  +}
          549  +
          550  +/*
          551  +** Sleep for nMicro microseconds. Return the number of microseconds 
          552  +** actually slept.
          553  +*/
          554  +static int vstatSleep(sqlite3_vfs *pVfs, int nMicro){
          555  +  STATCNT(VFSSTAT_ANY,VFSSTAT_SLEEP)++;
          556  +  return REALVFS(pVfs)->xSleep(REALVFS(pVfs), nMicro);
          557  +}
          558  +
          559  +/*
          560  +** Return the current time as a Julian Day number in *pTimeOut.
          561  +*/
          562  +static int vstatCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
          563  +  STATCNT(VFSSTAT_ANY,VFSSTAT_CURTIME)++;
          564  +  return REALVFS(pVfs)->xCurrentTime(REALVFS(pVfs), pTimeOut);
          565  +}
          566  +
          567  +static int vstatGetLastError(sqlite3_vfs *pVfs, int a, char *b){
          568  +  return REALVFS(pVfs)->xGetLastError(REALVFS(pVfs), a, b);
          569  +}
          570  +static int vstatCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
          571  +  STATCNT(VFSSTAT_ANY,VFSSTAT_CURTIME)++;
          572  +  return REALVFS(pVfs)->xCurrentTimeInt64(REALVFS(pVfs), p);
          573  +}
          574  +
          575  +/*
          576  +** A virtual table for accessing the stats collected by this VFS shim
          577  +*/
          578  +static int vstattabConnect(sqlite3*, void*, int, const char*const*, 
          579  +                           sqlite3_vtab**,char**);
          580  +static int vstattabBestIndex(sqlite3_vtab*,sqlite3_index_info*);
          581  +static int vstattabDisconnect(sqlite3_vtab*);
          582  +static int vstattabOpen(sqlite3_vtab*, sqlite3_vtab_cursor**);
          583  +static int vstattabClose(sqlite3_vtab_cursor*);
          584  +static int vstattabFilter(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
          585  +                          int argc, sqlite3_value **argv);
          586  +static int vstattabNext(sqlite3_vtab_cursor*);
          587  +static int vstattabEof(sqlite3_vtab_cursor*);
          588  +static int vstattabColumn(sqlite3_vtab_cursor*,sqlite3_context*,int);
          589  +static int vstattabRowid(sqlite3_vtab_cursor*,sqlite3_int64*);
          590  +static int vstattabUpdate(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
          591  +
          592  +/* A cursor for the vfsstat virtual table */
          593  +typedef struct VfsStatCursor {
          594  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
          595  +  int i;                          /* Pointing to this aVfsCnt[] value */
          596  +} VfsStatCursor;
          597  +
          598  +
          599  +static int vstattabConnect(
          600  +  sqlite3 *db,
          601  +  void *pAux,
          602  +  int argc, const char *const*argv,
          603  +  sqlite3_vtab **ppVtab,
          604  +  char **pzErr
          605  +){
          606  +  sqlite3_vtab *pNew;
          607  +  int rc;
          608  +
          609  +/* Column numbers */
          610  +#define VSTAT_COLUMN_FILE  0 
          611  +#define VSTAT_COLUMN_STAT  1
          612  +#define VSTAT_COLUMN_COUNT 2
          613  +
          614  +  rc = sqlite3_declare_vtab(db,"CREATE TABLE x(file,stat,count)");
          615  +  if( rc==SQLITE_OK ){
          616  +    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
          617  +    if( pNew==0 ) return SQLITE_NOMEM;
          618  +    memset(pNew, 0, sizeof(*pNew));
          619  +  }
          620  +  return rc;
          621  +}
          622  +
          623  +/*
          624  +** This method is the destructor for vstat table object.
          625  +*/
          626  +static int vstattabDisconnect(sqlite3_vtab *pVtab){
          627  +  sqlite3_free(pVtab);
          628  +  return SQLITE_OK;
          629  +}
          630  +
          631  +/*
          632  +** Constructor for a new vstat table cursor object.
          633  +*/
          634  +static int vstattabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          635  +  VfsStatCursor *pCur;
          636  +  pCur = sqlite3_malloc( sizeof(*pCur) );
          637  +  if( pCur==0 ) return SQLITE_NOMEM;
          638  +  memset(pCur, 0, sizeof(*pCur));
          639  +  *ppCursor = &pCur->base;
          640  +  return SQLITE_OK;
          641  +}
          642  +
          643  +
          644  +/*
          645  +** Destructor for a VfsStatCursor.
          646  +*/
          647  +static int vstattabClose(sqlite3_vtab_cursor *cur){
          648  +  sqlite3_free(cur);
          649  +  return SQLITE_OK;
          650  +}
          651  +
          652  +
          653  +/*
          654  +** Advance a VfsStatCursor to its next row of output.
          655  +*/
          656  +static int vstattabNext(sqlite3_vtab_cursor *cur){
          657  +  ((VfsStatCursor*)cur)->i++;
          658  +  return SQLITE_OK;
          659  +}
          660  +
          661  +/*
          662  +** Return values of columns for the row at which the VfsStatCursor
          663  +** is currently pointing.
          664  +*/
          665  +static int vstattabColumn(
          666  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          667  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          668  +  int i                       /* Which column to return */
          669  +){
          670  +  VfsStatCursor *pCur = (VfsStatCursor*)cur;
          671  +  switch( i ){
          672  +    case VSTAT_COLUMN_FILE: {
          673  +      sqlite3_result_text(ctx, azFile[pCur->i/VFSSTAT_nStat], -1, SQLITE_STATIC);
          674  +      break;
          675  +    }
          676  +    case VSTAT_COLUMN_STAT: {
          677  +      const char **az;
          678  +      az = (pCur->i/VFSSTAT_nStat)==VFSSTAT_ANY ? azStatAny : azStat;
          679  +      sqlite3_result_text(ctx, az[pCur->i%VFSSTAT_nStat], -1, SQLITE_STATIC);
          680  +      break;
          681  +    }
          682  +    case VSTAT_COLUMN_COUNT: {
          683  +      sqlite3_result_int64(ctx, aVfsCnt[pCur->i]);
          684  +      break;
          685  +    }
          686  +  }
          687  +  return SQLITE_OK;
          688  +}
          689  +
          690  +/*
          691  +** Return the rowid for the current row.
          692  +*/
          693  +static int vstattabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          694  +  VfsStatCursor *pCur = (VfsStatCursor*)cur;
          695  +  *pRowid = pCur->i;
          696  +  return SQLITE_OK;
          697  +}
          698  +
          699  +/*
          700  +** Return TRUE if the cursor has been moved off of the last
          701  +** row of output.
          702  +*/
          703  +static int vstattabEof(sqlite3_vtab_cursor *cur){
          704  +  VfsStatCursor *pCur = (VfsStatCursor*)cur;
          705  +  return pCur->i >= VFSSTAT_MXCNT;
          706  +}
          707  +
          708  +/*
          709  +** Only a full table scan is supported.  So xFilter simply rewinds to
          710  +** the beginning.
          711  +*/
          712  +static int vstattabFilter(
          713  +  sqlite3_vtab_cursor *pVtabCursor, 
          714  +  int idxNum, const char *idxStr,
          715  +  int argc, sqlite3_value **argv
          716  +){
          717  +  VfsStatCursor *pCur = (VfsStatCursor*)pVtabCursor;
          718  +  pCur->i = 0;
          719  +  return SQLITE_OK;
          720  +}
          721  +
          722  +/*
          723  +** Only a forwards full table scan is supported.  xBestIndex is a no-op.
          724  +*/
          725  +static int vstattabBestIndex(
          726  +  sqlite3_vtab *tab,
          727  +  sqlite3_index_info *pIdxInfo
          728  +){
          729  +  return SQLITE_OK;
          730  +}
          731  +
          732  +/*
          733  +** Any VSTAT_COLUMN_COUNT can be changed to a positive integer.
          734  +** No deletions or insertions are allowed.  No changes to other
          735  +** columns are allowed.
          736  +*/
          737  +static int vstattabUpdate(
          738  +  sqlite3_vtab *tab,
          739  +  int argc, sqlite3_value **argv,
          740  +  sqlite3_int64 *pRowid
          741  +){
          742  +  sqlite3_int64 iRowid, x;
          743  +  if( argc==1 ) return SQLITE_ERROR;
          744  +  if( sqlite3_value_type(argv[0])!=SQLITE_INTEGER ) return SQLITE_ERROR;
          745  +  iRowid = sqlite3_value_int64(argv[0]);
          746  +  if( iRowid!=sqlite3_value_int64(argv[1]) ) return SQLITE_ERROR;
          747  +  if( iRowid<0 || iRowid>=VFSSTAT_MXCNT ) return SQLITE_ERROR;
          748  +  if( sqlite3_value_type(argv[VSTAT_COLUMN_COUNT+2])!=SQLITE_INTEGER ){
          749  +    return SQLITE_ERROR;
          750  +  }
          751  +  x = sqlite3_value_int64(argv[VSTAT_COLUMN_COUNT+2]);
          752  +  if( x<0 ) return SQLITE_ERROR;
          753  +  aVfsCnt[iRowid] = x;
          754  +  return SQLITE_OK;
          755  +}
          756  +
          757  +static sqlite3_module VfsStatModule = {
          758  +  0,                         /* iVersion */
          759  +  0,                         /* xCreate */
          760  +  vstattabConnect,           /* xConnect */
          761  +  vstattabBestIndex,         /* xBestIndex */
          762  +  vstattabDisconnect,        /* xDisconnect */
          763  +  0,                         /* xDestroy */
          764  +  vstattabOpen,              /* xOpen - open a cursor */
          765  +  vstattabClose,             /* xClose - close a cursor */
          766  +  vstattabFilter,            /* xFilter - configure scan constraints */
          767  +  vstattabNext,              /* xNext - advance a cursor */
          768  +  vstattabEof,               /* xEof - check for end of scan */
          769  +  vstattabColumn,            /* xColumn - read data */
          770  +  vstattabRowid,             /* xRowid - read data */
          771  +  vstattabUpdate,            /* xUpdate */
          772  +  0,                         /* xBegin */
          773  +  0,                         /* xSync */
          774  +  0,                         /* xCommit */
          775  +  0,                         /* xRollback */
          776  +  0,                         /* xFindMethod */
          777  +  0,                         /* xRename */
          778  +};
          779  +
          780  +/*
          781  +** This routine is an sqlite3_auto_extension() callback, invoked to register
          782  +** the vfsstat virtual table for all new database connections.
          783  +*/
          784  +static int vstatRegister(
          785  +  sqlite3 *db,
          786  +  const char **pzErrMsg,
          787  +  const struct sqlite3_api_routines *pThunk
          788  +){
          789  +  return sqlite3_create_module(db, "vfsstat", &VfsStatModule, 0);
          790  +}
          791  +
          792  +#ifdef _WIN32
          793  +__declspec(dllexport)
          794  +#endif
          795  +/* 
          796  +** This routine is called when the extension is loaded.
          797  +**
          798  +** Register the new VFS.  Make arrangement to register the virtual table
          799  +** for each new database connection.
          800  +*/
          801  +int sqlite3_vfsstat_init(
          802  +  sqlite3 *db, 
          803  +  char **pzErrMsg, 
          804  +  const sqlite3_api_routines *pApi
          805  +){
          806  +  int rc = SQLITE_OK;
          807  +  SQLITE_EXTENSION_INIT2(pApi);
          808  +  vstat_vfs.pVfs = sqlite3_vfs_find(0);
          809  +  vstat_vfs.base.szOsFile = sizeof(VStatFile) + vstat_vfs.pVfs->szOsFile;
          810  +  rc = sqlite3_vfs_register(&vstat_vfs.base, 1);
          811  +  if( rc==SQLITE_OK ){
          812  +    rc = sqlite3_auto_extension(vstatRegister);
          813  +  }
          814  +  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
          815  +  return rc;
          816  +}

Changes to ext/rbu/rbu1.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   
    13         -if {![info exists testdir]} {
    14         -  set testdir [file join [file dirname [info script]] .. .. test]
    15         -}
    16         -source $testdir/tester.tcl
           13  +source [file join [file dirname [info script]] rbu_common.tcl]
    17     14   set ::testprefix rbu1
    18     15   
    19     16   db close
    20     17   sqlite3_shutdown
    21     18   sqlite3_config_uri 1
    22     19   
    23     20   # Create a simple RBU database. That expects to write to a table:
................................................................................
    92     89       INSERT INTO data_t1 VALUES(2, NULL, 10, 5, '..xx');    -- SET c=10, d = 5
    93     90       INSERT INTO data_t1 VALUES(3, 11, NULL, NULL, '.x..'); -- SET b=11
    94     91     }
    95     92     rbu5 close
    96     93     return $filename
    97     94   }
    98     95   
    99         -# Run the RBU in file $rbu on target database $target until completion.
   100         -#
   101         -proc run_rbu {target rbu} {
   102         -  sqlite3rbu rbu $target $rbu
   103         -  while 1 {
   104         -    set rc [rbu step]
   105         -    if {$rc!="SQLITE_OK"} break
   106         -  }
   107         -  rbu close
   108         -}
   109         -
   110         -proc step_rbu {target rbu} {
   111         -  while 1 {
   112         -    sqlite3rbu rbu $target $rbu
   113         -    set rc [rbu step]
   114         -    rbu close
   115         -    if {$rc != "SQLITE_OK"} break
   116         -  }
   117         -  set rc
   118         -}
   119     96   
   120     97   # Same as [step_rbu], except using a URI to open the target db.
   121     98   #
   122     99   proc step_rbu_uri {target rbu} {
   123    100     while 1 {
   124    101       sqlite3rbu rbu file:$target?xyz=&abc=123 $rbu
   125    102       set rc [rbu step]
................................................................................
   637    614       }
   638    615     }
   639    616   
   640    617     # Test that an RBU database containing no input tables is handled
   641    618     # correctly.
   642    619     reset_db
   643    620     forcedelete rbu.db
   644         -  do_test $tn3.8 {
          621  +  do_test $tn3.8.1 {
          622  +    list [catch { run_rbu test.db rbu.db } msg] $msg
          623  +  } {0 SQLITE_DONE}
          624  +
          625  +  # Test that an RBU database containing only empty data_xxx tables is
          626  +  # also handled correctly.
          627  +  reset_db
          628  +  forcedelete rbu.db
          629  +  do_execsql_test $tn3.8.2.1 {
          630  +    CREATE TABLE t1(a PRIMARY KEY, b);
          631  +    INSERT INTO t1 VALUES(1, 2);
          632  +    ATTACH 'rbu.db' AS rbu;
          633  +    CREATE TABLE data_t1(a, b, rbu_control);
          634  +    DETACH rbu;
          635  +  }
          636  +  do_test $tn3.8.2.1 {
   645    637       list [catch { run_rbu test.db rbu.db } msg] $msg
   646    638     } {0 SQLITE_DONE}
   647         -  
          639  +
   648    640     # Test that RBU can update indexes containing NULL values.
   649    641     #
   650    642     reset_db
   651    643     forcedelete rbu.db
   652    644     do_execsql_test $tn3.9.1 {
   653    645       CREATE TABLE t1(a PRIMARY KEY, b, c);
   654    646       CREATE INDEX i1 ON t1(b, c);

Changes to ext/rbu/rbu5.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # Test some properties of the pager_rbu_mode and rbu_mode pragmas.
    13     13   #
    14     14   
    15         -if {![info exists testdir]} {
    16         -  set testdir [file join [file dirname [info script]] .. .. test]
    17         -}
    18         -source $testdir/tester.tcl
           15  +source [file join [file dirname [info script]] rbu_common.tcl]
    19     16   set ::testprefix rbu5
    20     17   
    21     18   
    22         -# Run the RBU in file $rbu on target database $target until completion.
    23         -#
    24         -proc run_rbu {target rbu} {
    25         -  sqlite3rbu rbu $target $rbu
    26         -  while { [rbu step]=="SQLITE_OK" } {}
    27         -  rbu close
    28         -}
    29         -
    30         -
    31         -# Run the RBU in file $rbu on target database $target one step at a
    32         -# time until completion.
    33         -#
    34         -proc step_rbu {target rbu} {
    35         -  while 1 {
    36         -    sqlite3rbu rbu $target $rbu
    37         -    set rc [rbu step]
    38         -    rbu close
    39         -    if {$rc != "SQLITE_OK"} break
    40         -  }
    41         -  set rc
    42         -}
    43         -
    44     19   # Return a list of the primary key columns for table $tbl in the database
    45     20   # opened by database handle $db.
    46     21   #
    47     22   proc pkcols {db tbl} {
    48     23     set ret [list]
    49     24     $db eval "PRAGMA table_info = '$tbl'" {
    50     25       if {$pk} { lappend ret $name }

Changes to ext/rbu/rbu_common.tcl.

    10     10   #***********************************************************************
    11     11   #
    12     12   
    13     13   if {![info exists testdir]} {
    14     14     set testdir [file join [file dirname [info script]] .. .. test]
    15     15   }
    16     16   source $testdir/tester.tcl
           17  +
           18  +proc check_prestep_state {target state} {
           19  +  set oal_exists [file exists $target-oal]
           20  +  set wal_exists [file exists $target-wal]
           21  +  set progress [rbu progress]
           22  +
           23  +  if {($progress==0 && $state!="oal" && $state!="done")
           24  +   || ($oal_exists && $wal_exists)
           25  +   || ($progress>0 && $state=="oal" && (!$oal_exists || $wal_exists))
           26  +   || ($state=="move" && (!$oal_exists || $wal_exists))
           27  +   || ($state=="checkpoint" && ($oal_exists || !$wal_exists))
           28  +   || ($state=="done" && ($oal_exists && $progress!=0))
           29  +  } {
           30  +    error "B: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
           31  +  }
           32  +}
           33  +
           34  +proc check_poststep_state {rc target state} {
           35  +  if {$rc=="SQLITE_OK" || $rc=="SQLITE_DONE"} {
           36  +    set oal_exists [file exists $target-oal]
           37  +    set wal_exists [file exists $target-wal]
           38  +    if {$state=="move" && ($oal_exists || !$wal_exists)} {
           39  +      error "A: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
           40  +    }
           41  +  }
           42  +}
    17     43   
    18     44   # Run the RBU in file $rbu on target database $target until completion.
    19     45   #
    20     46   proc run_rbu {target rbu} {
    21     47     sqlite3rbu rbu $target $rbu
    22     48     while 1 {
           49  +    set state [rbu state]
           50  +
           51  +    check_prestep_state $target $state
    23     52       set rc [rbu step]
           53  +    check_poststep_state $rc $target $state
           54  +
    24     55       if {$rc!="SQLITE_OK"} break
    25     56     }
    26     57     rbu close
    27     58   }
    28     59   
    29     60   proc step_rbu {target rbu} {
    30     61     while 1 {
    31     62       sqlite3rbu rbu $target $rbu
           63  +    set state [rbu state]
           64  +    check_prestep_state $target $state
    32     65       set rc [rbu step]
           66  +    check_poststep_state $rc $target $state
    33     67       rbu close
    34     68       if {$rc != "SQLITE_OK"} break
    35     69     }
    36     70     set rc
    37     71   }
           72  +
           73  +proc do_rbu_vacuum_test {tn step} {
           74  +  uplevel [list do_test $tn.1 {
           75  +    if {$step==0} { sqlite3rbu_vacuum rbu test.db state.db }
           76  +    while 1 {
           77  +      if {$step==1} { sqlite3rbu_vacuum rbu test.db state.db }
           78  +      set state [rbu state]
           79  +      check_prestep_state test.db $state
           80  +      set rc [rbu step]
           81  +      check_poststep_state $rc test.db $state
           82  +      if {$rc!="SQLITE_OK"} break
           83  +      if {$step==1} { rbu close }
           84  +    }
           85  +    rbu close
           86  +  } {SQLITE_DONE}]
           87  +
           88  +  uplevel [list do_execsql_test $tn.2 {
           89  +    PRAGMA integrity_check
           90  +  } ok]
           91  +}
    38     92   

Changes to ext/rbu/rbudiff.test.

    20     20   
    21     21   set PROG [test_find_sqldiff]
    22     22   db close
    23     23   
    24     24   proc get_rbudiff_sql {db1 db2} {
    25     25     exec $::PROG --rbu $db1 $db2
    26     26   }
           27  +
           28  +proc get_vtab_rbudiff_sql {db1 db2} {
           29  +  exec $::PROG --vtab --rbu $db1 $db2
           30  +}
    27     31   
    28     32   proc step_rbu {target rbu} {
    29     33     while 1 {
    30     34       sqlite3rbu rbu $target $rbu
    31     35       set rc [rbu step]
    32     36       rbu close
    33     37       if {$rc != "SQLITE_OK"} break
................................................................................
    39     43     test_rbucount $sql
    40     44     forcedelete rbu.db
    41     45     sqlite3 rbudb rbu.db
    42     46     rbudb eval $sql
    43     47     rbudb close
    44     48     step_rbu $target rbu.db
    45     49   }
           50  +
           51  +proc sqlesc {id} {
           52  +  set ret "'[string map {' ''} $id]'"
           53  +  set ret
           54  +}
    46     55   
    47     56   # The only argument is the output of an [sqldiff -rbu] run. This command
    48     57   # tests that the contents of the rbu_count table is correct. An exception
    49     58   # is thrown if it is not.
    50     59   #
    51     60   proc test_rbucount {sql} {
    52     61     sqlite3 tmpdb ""
    53     62     tmpdb eval $sql
    54     63     tmpdb eval {
    55     64       SELECT name FROM sqlite_master WHERE name LIKE 'data%' AND type='table'
    56     65     } {
    57         -    set a [tmpdb eval "SELECT count(*) FROM $name"]
           66  +    set a [tmpdb eval "SELECT count(*) FROM [sqlesc $name]"]
    58     67       set b [tmpdb eval {SELECT cnt FROM rbu_count WHERE tbl = $name}]
    59     68       if {$a != $b} { 
    60     69         tmpdb close
    61     70         error "rbu_count error - tbl = $name" 
    62     71       }
    63     72     }
    64     73     tmpdb close
................................................................................
    67     76   
    68     77   proc rbudiff_cksum {db1} {
    69     78     set txt ""
    70     79   
    71     80     sqlite3 dbtmp $db1
    72     81     foreach tbl [dbtmp eval {SELECT name FROM sqlite_master WHERE type='table'}] {
    73     82       set cols [list]
    74         -    dbtmp eval "PRAGMA table_info = $tbl" { lappend cols "quote( $name )" }
           83  +    dbtmp eval "PRAGMA table_info = [sqlesc $tbl]" { 
           84  +      lappend cols "quote( $name )" 
           85  +    }
    75     86       append txt [dbtmp eval \
    76         -      "SELECT [join $cols {||'.'||}] FROM $tbl ORDER BY 1"
           87  +      "SELECT [join $cols {||'.'||}] FROM [sqlesc $tbl] ORDER BY 1"
    77     88       ]
    78     89     }
    79     90     dbtmp close
    80     91   
    81     92     md5 $txt
    82     93   }
    83     94   
................................................................................
   154    165   
   155    166     do_test 1.$tn.4 {
   156    167       set sql [get_rbudiff_sql test.db test.db2]
   157    168       apply_rbudiff $sql test.db
   158    169     } {SQLITE_DONE}
   159    170     do_test 1.$tn.5 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
   160    171   }
          172  +
          173  +#-------------------------------------------------------------------------
          174  +# Test that if the --vtab switch is present, [sqldiff] handles virtual
          175  +# table types fts[345] and rtree correctly.
          176  +#
          177  +ifcapable fts3&&fts5&&rtree {
          178  +
          179  +foreach {tn init mod} {
          180  +  1 {
          181  +    CREATE VIRTUAL TABLE t1 USING fts5(c);
          182  +    INSERT INTO t1 VALUES('a b c');
          183  +    INSERT INTO t1 VALUES('a b c');
          184  +  } {
          185  +    DELETE FROM t1 WHERE rowid = 1;
          186  +    INSERT INTO t1 VALUES('a b c');
          187  +  }
          188  +
          189  +  2 {
          190  +    CREATE VIRTUAL TABLE "x y" USING 'rtree'(id, x1, x2);
          191  +    INSERT INTO "x y" VALUES(1, 2, 3);
          192  +    INSERT INTO "x y" VALUES(2, 4, 6);
          193  +  } {
          194  +    DELETE FROM "x y" WHERE rowid = 1;
          195  +    INSERT INTO "x y" VALUES(3, 6, 9);
          196  +  }
          197  +
          198  +  3 {
          199  +    CREATE VIRTUAL TABLE 'x''y' USING fts3;
          200  +    INSERT INTO 'x''y' VALUES('one two three');
          201  +    INSERT INTO 'x''y' VALUES('four five six');
          202  +  } {
          203  +    DELETE FROM 'x''y' WHERE rowid = 1;
          204  +    INSERT INTO 'x''y' VALUES('one two three');
          205  +  }
          206  +} {
          207  +
          208  +  forcedelete test.db test.db2
          209  +  sqlite3 db test.db
          210  +  db eval "$init"
          211  +  sqlite3 db test.db2
          212  +  db eval "$init ; $mod"
          213  +  db close
          214  +
          215  +  do_test 2.$tn.1 {
          216  +    set sql [get_vtab_rbudiff_sql test.db test.db2]
          217  +    apply_rbudiff $sql test.db
          218  +  } {SQLITE_DONE}
          219  +  do_test 2.$tn.2 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
          220  +}
          221  +
          222  +}
          223  +
          224  +ifcapable fts5 {
          225  +  foreach {tn init mod} {
          226  +    1 {
          227  +      CREATE VIRTUAL TABLE t1 USING fts5(c);
          228  +      INSERT INTO t1 VALUES('a b c');
          229  +      INSERT INTO t1 VALUES('a b c');
          230  +    } {
          231  +      DELETE FROM t1 WHERE rowid = 1;
          232  +      INSERT INTO t1 VALUES('a b c');
          233  +    }
          234  +
          235  +    2 {
          236  +      CREATE VIRTUAL TABLE t1 USING FTs5(c);
          237  +      INSERT INTO t1 VALUES('a b c');
          238  +      INSERT INTO t1 VALUES('a b c');
          239  +    } {
          240  +      DELETE FROM t1 WHERE rowid = 1;
          241  +      INSERT INTO t1 VALUES('a b c');
          242  +    }
          243  +
          244  +    3 {
          245  +      creAte    virTUal
          246  +tablE t1 USING FTs5(c);
          247  +      INSERT INTO t1 VALUES('a b c');
          248  +      INSERT INTO t1 VALUES('a b c');
          249  +    } {
          250  +      DELETE FROM t1 WHERE rowid = 1;
          251  +      INSERT INTO t1 VALUES('a b c');
          252  +    }
          253  +
          254  +  } {
          255  +    forcedelete test.db test.db2
          256  +    sqlite3 db test.db
          257  +    db eval "$init"
          258  +    sqlite3 db test.db2
          259  +    db eval "$init ; $mod"
          260  +    db eval { INSERT INTO t1(t1) VALUES('optimize') }
          261  +    db close
          262  +
          263  +    do_test 3.$tn.1 {
          264  +      set sql [get_vtab_rbudiff_sql test.db test.db2]
          265  +      apply_rbudiff $sql test.db
          266  +    } {SQLITE_DONE}
          267  +
          268  +    sqlite3 db test.db
          269  +    sqlite3 db2 test.db2
          270  +    do_test 3.$tn.2 { 
          271  +      db2 eval { SELECT * FROM t1 ORDER BY rowid }
          272  +    } [db eval { SELECT * FROM t1 ORDER BY rowid }]
          273  +
          274  +    do_test 3.$tn.3 { 
          275  +      db2 eval { INSERT INTO t1(t1) VALUES('integrity-check') }
          276  +    } {}
          277  +
          278  +    db close
          279  +    db2 close
          280  +  }
          281  +}
   161    282   
   162    283   finish_test
   163    284   

Changes to ext/rbu/rbuprogress.test.

   356    356         CREATE TABLE t1(a, b, c);
   357    357         CREATE INDEX t1c ON t1(c);
   358    358       }
   359    359       vtab { 
   360    360         CREATE VIRTUAL TABLE t1 USING fts5(a, b, c);
   361    361       }
   362    362     } {
          363  +
          364  +    if {$tn=="vtab"} { ifcapable !fts5 break }
   363    365   
   364    366       foreach {tn2 rbusql r1 r2} {
   365    367         1 {
   366    368           CREATE TABLE data0_t1(a, b, c, rbu_rowid, rbu_control);
   367    369           INSERT INTO data0_t1 VALUES(15, 15, 15, 4, 0);
   368    370           INSERT INTO data0_t1 VALUES(20, 20, 20, 5, 0);
   369    371           CREATE TABLE rbu_count(tbl, cnt);

Changes to ext/rbu/rbuvacuum.test.

    13     13   # contains tests to ensure that the sqlite3rbu_vacuum() API works as
    14     14   # expected.
    15     15   #
    16     16   
    17     17   source [file join [file dirname [info script]] rbu_common.tcl]
    18     18   set ::testprefix rbuvacuum
    19     19   
    20         -proc do_rbu_vacuum_test {tn step} {
    21         -  uplevel [list do_test $tn.1 {
    22         -    if {$step==0} { sqlite3rbu_vacuum rbu test.db state.db }
    23         -    while 1 {
    24         -      if {$step==1} { sqlite3rbu_vacuum rbu test.db state.db }
    25         -      set rc [rbu step]
    26         -      if {$rc!="SQLITE_OK"} break
    27         -      if {$step==1} { rbu close }
    28         -    }
    29         -    rbu close
    30         -  } {SQLITE_DONE}]
    31         -
    32         -  uplevel [list do_execsql_test $tn.2 {
    33         -    PRAGMA integrity_check
    34         -  } ok]
    35         -}
    36         -
    37     20   foreach step {0 1} {
    38     21   
    39     22     set ::testprefix rbuvacuum-step=$step
    40     23     reset_db
    41     24   
    42     25     # Simplest possible vacuum.
    43     26     do_execsql_test 1.0 {
................................................................................
   399    382   
   400    383     sqlite3_create_collation_v2 $db1 length length_cmp noop
   401    384     sqlite3_create_collation_v2 $db2 length length_cmp noop
   402    385   
   403    386     while {[rbu step]=="SQLITE_OK"} {}
   404    387     list [catch { rbu close } msg] $msg
   405    388   } {0 SQLITE_DONE}
   406         -
   407    389   
   408    390   catch { db close }
   409    391   finish_test
   410    392   

Added ext/rbu/rbuvacuum2.test.

            1  +# 2016 June 1
            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 contains tests for the RBU module. More specifically, it
           13  +# contains tests to ensure that the sqlite3rbu_vacuum() API works as
           14  +# expected.
           15  +#
           16  +
           17  +source [file join [file dirname [info script]] rbu_common.tcl]
           18  +
           19  +foreach step {0 1} {
           20  +  set ::testprefix rbuvacuum2-$step
           21  +  
           22  +  #-------------------------------------------------------------------------
           23  +  # Test that a database that contains fts3 tables can be vacuumed.
           24  +  #
           25  +  ifcapable fts3 {
           26  +    reset_db
           27  +    do_execsql_test 1.1 {
           28  +      CREATE VIRTUAL TABLE t1 USING fts3(z, y);
           29  +      INSERT INTO t1 VALUES('fix this issue', 'at some point');
           30  +    }
           31  +  
           32  +    do_rbu_vacuum_test 1.2 $step
           33  +  
           34  +    do_execsql_test 1.3 {
           35  +      SELECT * FROM t1;
           36  +    } {{fix this issue} {at some point}}
           37  +  
           38  +    do_execsql_test 1.4 {
           39  +      SELECT rowid FROM t1 WHERE t1 MATCH 'fix';
           40  +    } {1}
           41  +
           42  +    do_execsql_test 1.5 {
           43  +      INSERT INTO t1 VALUES('a b c', 'd e f');
           44  +      INSERT INTO t1 VALUES('l h i', 'd e f');
           45  +      DELETE FROM t1 WHERE docid = 2;
           46  +      INSERT INTO t1 VALUES('a b c', 'x y z');
           47  +    }
           48  +
           49  +    do_rbu_vacuum_test 1.6 $step
           50  +    do_execsql_test 1.7 {
           51  +      INSERT INTO t1(t1) VALUES('integrity-check');
           52  +      SELECT * FROM t1;
           53  +    } {
           54  +      {fix this issue} {at some point}
           55  +      {l h i} {d e f}
           56  +      {a b c} {x y z}
           57  +    }
           58  +  }
           59  +  
           60  +  #-------------------------------------------------------------------------
           61  +  # Test that a database that contains fts5 tables can be vacuumed.
           62  +  #
           63  +  ifcapable fts5 {
           64  +    reset_db
           65  +    do_execsql_test 2.1 {
           66  +      CREATE VIRTUAL TABLE t1 USING fts5(z, y);
           67  +      INSERT INTO t1 VALUES('fix this issue', 'at some point');
           68  +    }
           69  +  
           70  +    do_rbu_vacuum_test 2.2 $step
           71  +  
           72  +    do_execsql_test 2.3 {
           73  +      SELECT * FROM t1;
           74  +    } {{fix this issue} {at some point}}
           75  +  
           76  +    do_execsql_test 2.4 {
           77  +      SELECT rowid FROM t1 ('fix');
           78  +    } {1}
           79  +
           80  +    do_execsql_test 2.5 {
           81  +      INSERT INTO t1 VALUES('a b c', 'd e f');
           82  +      INSERT INTO t1 VALUES('l h i', 'd e f');
           83  +      DELETE FROM t1 WHERE rowid = 2;
           84  +      INSERT INTO t1 VALUES('a b c', 'x y z');
           85  +    }
           86  +
           87  +    do_rbu_vacuum_test 2.6 $step
           88  +    do_execsql_test 2.7 {
           89  +      INSERT INTO t1(t1) VALUES('integrity-check');
           90  +      SELECT * FROM t1;
           91  +    } {
           92  +      {fix this issue} {at some point}
           93  +      {l h i} {d e f}
           94  +      {a b c} {x y z}
           95  +    }
           96  +  }
           97  +
           98  +  #-------------------------------------------------------------------------
           99  +  # Test that a database that contains an rtree table can be vacuumed.
          100  +  #
          101  +  ifcapable rtree {
          102  +    reset_db
          103  +    do_execsql_test 3.1 {
          104  +      CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
          105  +      INSERT INTO rt VALUES(1, 45, 55);
          106  +      INSERT INTO rt VALUES(2, 50, 60);
          107  +      INSERT INTO rt VALUES(3, 55, 65);
          108  +    }
          109  +  
          110  +    do_rbu_vacuum_test 3.2 $step
          111  +  
          112  +    do_execsql_test 3.3 {
          113  +      SELECT * FROM rt;
          114  +    } {1 45.0 55.0 2 50.0 60.0 3 55.0 65.0}
          115  +  
          116  +    do_execsql_test 3.4.1 {
          117  +      SELECT rowid FROM rt WHERE x2>51 AND x1 < 51
          118  +    } {1 2}
          119  +    do_execsql_test 3.4.2 {
          120  +      SELECT rowid FROM rt WHERE x2>59 AND x1 < 59
          121  +    } {2 3}
          122  +
          123  +    do_rbu_vacuum_test 3.5 $step
          124  +
          125  +    do_execsql_test 3.6.1 {
          126  +      SELECT rowid FROM rt WHERE x2>51 AND x1 < 51
          127  +    } {1 2}
          128  +    do_execsql_test 3.6.2 {
          129  +      SELECT rowid FROM rt WHERE x2>59 AND x1 < 59
          130  +    } {2 3}
          131  +  }
          132  +
          133  +  ifcapable trigger {
          134  +    reset_db
          135  +    do_execsql_test 4.1 {
          136  +      CREATE TABLE t1(a, b, c);
          137  +      INSERT INTO t1 VALUES(1, 2, 3);
          138  +      CREATE VIEW v1 AS SELECT * FROM t1;
          139  +      CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
          140  +    }
          141  +
          142  +    do_execsql_test 4.2 {
          143  +      SELECT * FROM sqlite_master;
          144  +    } {
          145  +    table t1 t1 2 {CREATE TABLE t1(a, b, c)}
          146  +    view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t1}
          147  +    trigger tr1 t1 0 {CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END}
          148  +    }
          149  +
          150  +    do_rbu_vacuum_test 4.3 $step
          151  +    do_execsql_test 4.4 {
          152  +      SELECT * FROM sqlite_master;
          153  +    } {
          154  +    table t1 t1 2 {CREATE TABLE t1(a, b, c)}
          155  +    view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t1}
          156  +    trigger tr1 t1 0 {CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END}
          157  +    }
          158  +  }
          159  +}
          160  +  
          161  +#-------------------------------------------------------------------------
          162  +# Test that passing a NULL value as the second argument to 
          163  +# sqlite3rbu_vacuum() causes it to:
          164  +#
          165  +#   * Use <database>-vacuum as the state db, and
          166  +#   * Set the state db permissions to the same as those on the db file.
          167  +#
          168  +db close
          169  +if {$::tcl_platform(platform)=="unix"} {
          170  +  forcedelete test.db
          171  +
          172  +  sqlite3 db test.db
          173  +  do_execsql_test 5.0 {
          174  +    CREATE TABLE t1(a, b);
          175  +    INSERT INTO t1 VALUES(1, 2);
          176  +    INSERT INTO t1 VALUES(3, 4);
          177  +    INSERT INTO t1 VALUES(5, 6);
          178  +    INSERT INTO t1 VALUES(7, 8);
          179  +  }
          180  +  db close
          181  +
          182  +  foreach {tn perm} {
          183  +    1 00755
          184  +    2 00666
          185  +    3 00644
          186  +    4 00444
          187  +  } {
          188  +    forcedelete test.db-vacuum
          189  +
          190  +    do_test 5.$tn.1 {
          191  +      file attributes test.db -permissions $perm
          192  +      sqlite3rbu_vacuum rbu test.db
          193  +      rbu step
          194  +    } {SQLITE_OK}
          195  +
          196  +    do_test 5.$tn.2 { file exists test.db-vacuum } 1
          197  +    do_test 5.$tn.3 { file attributes test.db-vacuum -permissions} $perm
          198  +    rbu close
          199  +  }
          200  +}
          201  +
          202  +
          203  +finish_test
          204  +

Changes to ext/rbu/sqlite3rbu.c.

   917    917   ** left in the RBU handle passed as the first argument. A copy of the 
   918    918   ** error code is returned.
   919    919   */
   920    920   static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
   921    921     int rc;
   922    922     memset(pIter, 0, sizeof(RbuObjIter));
   923    923   
   924         -  rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg, 
          924  +  rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg, 
          925  +    sqlite3_mprintf(
   925    926         "SELECT rbu_target_name(name, type='view') AS target, name "
   926    927         "FROM sqlite_master "
   927    928         "WHERE type IN ('table', 'view') AND target IS NOT NULL "
          929  +      " %s "
   928    930         "ORDER BY name"
   929         -  );
          931  +  , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
   930    932   
   931    933     if( rc==SQLITE_OK ){
   932    934       rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
   933    935           "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
   934    936           "  FROM main.sqlite_master "
   935    937           "  WHERE type='index' AND tbl_name = ?"
   936    938       );
................................................................................
  2328   2330   
  2329   2331   
  2330   2332   /*
  2331   2333   ** Open the database handle and attach the RBU database as "rbu". If an
  2332   2334   ** error occurs, leave an error code and message in the RBU handle.
  2333   2335   */
  2334   2336   static void rbuOpenDatabase(sqlite3rbu *p){
  2335         -  assert( p->rc==SQLITE_OK );
  2336         -  assert( p->dbMain==0 && p->dbRbu==0 );
  2337         -  assert( rbuIsVacuum(p) || p->zTarget!=0 );
         2337  +  assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
         2338  +  assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
  2338   2339   
  2339   2340     /* Open the RBU database */
  2340   2341     p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
  2341   2342   
  2342   2343     if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
  2343   2344       sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
         2345  +    if( p->zState==0 ){
         2346  +      const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
         2347  +      p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
         2348  +    }
  2344   2349     }
  2345   2350   
  2346   2351     /* If using separate RBU and state databases, attach the state database to
  2347   2352     ** the RBU db handle now.  */
  2348   2353     if( p->zState ){
  2349   2354       rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
  2350   2355       memcpy(p->zStateDb, "stat", 4);
................................................................................
  2501   2506   static void rbuFileSuffix3(const char *zBase, char *z){
  2502   2507   #ifdef SQLITE_ENABLE_8_3_NAMES
  2503   2508   #if SQLITE_ENABLE_8_3_NAMES<2
  2504   2509     if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
  2505   2510   #endif
  2506   2511     {
  2507   2512       int i, sz;
  2508         -    sz = sqlite3Strlen30(z);
         2513  +    sz = (int)strlen(z)&0xffffff;
  2509   2514       for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
  2510         -    if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
         2515  +    if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
  2511   2516     }
  2512   2517   #endif
  2513   2518   }
  2514   2519   
  2515   2520   /*
  2516   2521   ** Return the current wal-index header checksum for the target database 
  2517   2522   ** as a 64-bit integer.
................................................................................
  3471   3476     const char *zTarget, 
  3472   3477     const char *zRbu,
  3473   3478     const char *zState
  3474   3479   ){
  3475   3480     sqlite3rbu *p;
  3476   3481     size_t nTarget = zTarget ? strlen(zTarget) : 0;
  3477   3482     size_t nRbu = strlen(zRbu);
  3478         -  size_t nState = zState ? strlen(zState) : 0;
  3479         -  size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1+ nState+1;
         3483  +  size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
  3480   3484   
  3481   3485     p = (sqlite3rbu*)sqlite3_malloc64(nByte);
  3482   3486     if( p ){
  3483   3487       RbuState *pState = 0;
  3484   3488   
  3485   3489       /* Create the custom VFS. */
  3486   3490       memset(p, 0, sizeof(sqlite3rbu));
................................................................................
  3494   3498           memcpy(p->zTarget, zTarget, nTarget+1);
  3495   3499           pCsr += nTarget+1;
  3496   3500         }
  3497   3501         p->zRbu = pCsr;
  3498   3502         memcpy(p->zRbu, zRbu, nRbu+1);
  3499   3503         pCsr += nRbu+1;
  3500   3504         if( zState ){
  3501         -        p->zState = pCsr;
  3502         -        memcpy(p->zState, zState, nState+1);
         3505  +        p->zState = rbuMPrintf(p, "%s", zState);
  3503   3506         }
  3504   3507         rbuOpenDatabase(p);
  3505   3508       }
  3506   3509   
  3507   3510       if( p->rc==SQLITE_OK ){
  3508   3511         pState = rbuLoadState(p);
  3509   3512         assert( pState || p->rc!=SQLITE_OK );
................................................................................
  3549   3552           );
  3550   3553         }
  3551   3554       }
  3552   3555   
  3553   3556       if( p->rc==SQLITE_OK ){
  3554   3557         if( p->eStage==RBU_STAGE_OAL ){
  3555   3558           sqlite3 *db = p->dbMain;
  3556         -
  3557         -        if( pState->eStage==0 && rbuIsVacuum(p) ){
  3558         -          rbuCopyPragma(p, "page_size");
  3559         -          rbuCopyPragma(p, "auto_vacuum");
  3560         -        }
  3561         -
  3562         -        /* Open transactions both databases. The *-oal file is opened or
  3563         -        ** created at this point. */
  3564         -        if( p->rc==SQLITE_OK ){
  3565         -          p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
  3566         -        }
  3567         -        if( p->rc==SQLITE_OK ){
  3568         -          p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
  3569         -        }
  3570         -
  3571         -        /* Check if the main database is a zipvfs db. If it is, set the upper
  3572         -        ** level pager to use "journal_mode=off". This prevents it from 
  3573         -        ** generating a large journal using a temp file.  */
  3574         -        if( p->rc==SQLITE_OK ){
  3575         -          int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
  3576         -          if( frc==SQLITE_OK ){
  3577         -            p->rc = sqlite3_exec(db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
  3578         -          }
  3579         -        }
         3559  +        p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
  3580   3560   
  3581   3561           /* Point the object iterator at the first object */
  3582   3562           if( p->rc==SQLITE_OK ){
  3583   3563             p->rc = rbuObjIterFirst(p, &p->objiter);
  3584   3564           }
  3585   3565   
  3586   3566           /* If the RBU database contains no data_xxx tables, declare the RBU
  3587   3567           ** update finished.  */
  3588   3568           if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
  3589   3569             p->rc = SQLITE_DONE;
         3570  +          p->eStage = RBU_STAGE_DONE;
         3571  +        }else{
         3572  +          if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
         3573  +            rbuCopyPragma(p, "page_size");
         3574  +            rbuCopyPragma(p, "auto_vacuum");
         3575  +          }
         3576  +
         3577  +          /* Open transactions both databases. The *-oal file is opened or
         3578  +          ** created at this point. */
         3579  +          if( p->rc==SQLITE_OK ){
         3580  +            p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
         3581  +          }
         3582  +
         3583  +          /* Check if the main database is a zipvfs db. If it is, set the upper
         3584  +          ** level pager to use "journal_mode=off". This prevents it from 
         3585  +          ** generating a large journal using a temp file.  */
         3586  +          if( p->rc==SQLITE_OK ){
         3587  +            int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
         3588  +            if( frc==SQLITE_OK ){
         3589  +              p->rc = sqlite3_exec(
         3590  +                db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
         3591  +            }
         3592  +          }
         3593  +
         3594  +          if( p->rc==SQLITE_OK ){
         3595  +            rbuSetupOal(p, pState);
         3596  +          }
  3590   3597           }
  3591         -
  3592         -        if( p->rc==SQLITE_OK ){
  3593         -          rbuSetupOal(p, pState);
  3594         -        }
  3595         -
  3596   3598         }else if( p->eStage==RBU_STAGE_MOVE ){
  3597   3599           /* no-op */
  3598   3600         }else if( p->eStage==RBU_STAGE_CKPT ){
  3599   3601           rbuSetupCheckpoint(p, pState);
  3600   3602         }else if( p->eStage==RBU_STAGE_DONE ){
  3601   3603           p->rc = SQLITE_DONE;
  3602   3604         }else{
................................................................................
  3605   3607       }
  3606   3608   
  3607   3609       rbuFreeState(pState);
  3608   3610     }
  3609   3611   
  3610   3612     return p;
  3611   3613   }
         3614  +
         3615  +/*
         3616  +** Allocate and return an RBU handle with all fields zeroed except for the
         3617  +** error code, which is set to SQLITE_MISUSE.
         3618  +*/
         3619  +static sqlite3rbu *rbuMisuseError(void){
         3620  +  sqlite3rbu *pRet;
         3621  +  pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
         3622  +  if( pRet ){
         3623  +    memset(pRet, 0, sizeof(sqlite3rbu));
         3624  +    pRet->rc = SQLITE_MISUSE;
         3625  +  }
         3626  +  return pRet;
         3627  +}
  3612   3628   
  3613   3629   /*
  3614   3630   ** Open and return a new RBU handle. 
  3615   3631   */
  3616   3632   sqlite3rbu *sqlite3rbu_open(
  3617   3633     const char *zTarget, 
  3618   3634     const char *zRbu,
  3619   3635     const char *zState
  3620   3636   ){
         3637  +  if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
  3621   3638     /* TODO: Check that zTarget and zRbu are non-NULL */
  3622   3639     return openRbuHandle(zTarget, zRbu, zState);
  3623   3640   }
  3624   3641   
  3625   3642   /*
  3626   3643   ** Open a handle to begin or resume an RBU VACUUM operation.
  3627   3644   */
  3628   3645   sqlite3rbu *sqlite3rbu_vacuum(
  3629   3646     const char *zTarget, 
  3630   3647     const char *zState
  3631   3648   ){
         3649  +  if( zTarget==0 ){ return rbuMisuseError(); }
  3632   3650     /* TODO: Check that both arguments are non-NULL */
  3633   3651     return openRbuHandle(0, zTarget, zState);
  3634   3652   }
  3635   3653   
  3636   3654   /*
  3637   3655   ** Return the database handle used by pRbu.
  3638   3656   */
................................................................................
  3702   3720       rbuDeleteVfs(p);
  3703   3721       sqlite3_free(p->aBuf);
  3704   3722       sqlite3_free(p->aFrame);
  3705   3723   
  3706   3724       rbuEditErrmsg(p);
  3707   3725       rc = p->rc;
  3708   3726       *pzErrmsg = p->zErrmsg;
         3727  +    sqlite3_free(p->zState);
  3709   3728       sqlite3_free(p);
  3710   3729     }else{
  3711   3730       rc = SQLITE_NOMEM;
  3712   3731       *pzErrmsg = 0;
  3713   3732     }
  3714   3733     return rc;
  3715   3734   }
................................................................................
  3754   3773         *pnTwo = MAX_PROGRESS;
  3755   3774         break;
  3756   3775   
  3757   3776       default:
  3758   3777         assert( 0 );
  3759   3778     }
  3760   3779   }
         3780  +
         3781  +/*
         3782  +** Return the current state of the RBU vacuum or update operation.
         3783  +*/
         3784  +int sqlite3rbu_state(sqlite3rbu *p){
         3785  +  int aRes[] = {
         3786  +    0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
         3787  +    0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
         3788  +  };
         3789  +
         3790  +  assert( RBU_STAGE_OAL==1 );
         3791  +  assert( RBU_STAGE_MOVE==2 );
         3792  +  assert( RBU_STAGE_CKPT==4 );
         3793  +  assert( RBU_STAGE_DONE==5 );
         3794  +  assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
         3795  +  assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
         3796  +  assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
         3797  +  assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
         3798  +
         3799  +  if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
         3800  +    return SQLITE_RBU_STATE_ERROR;
         3801  +  }else{
         3802  +    assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
         3803  +    assert( p->eStage==RBU_STAGE_OAL
         3804  +         || p->eStage==RBU_STAGE_MOVE
         3805  +         || p->eStage==RBU_STAGE_CKPT
         3806  +         || p->eStage==RBU_STAGE_DONE
         3807  +    );
         3808  +    return aRes[p->eStage];
         3809  +  }
         3810  +}
  3761   3811   
  3762   3812   int sqlite3rbu_savestate(sqlite3rbu *p){
  3763   3813     int rc = p->rc;
  3764         -  
  3765   3814     if( rc==SQLITE_DONE ) return SQLITE_OK;
  3766   3815   
  3767   3816     assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
  3768   3817     if( p->eStage==RBU_STAGE_OAL ){
  3769   3818       assert( rc!=SQLITE_DONE );
  3770   3819       if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
  3771   3820     }

Changes to ext/rbu/sqlite3rbu.h.

   315    315   );
   316    316   
   317    317   /*
   318    318   ** Open an RBU handle to perform an RBU vacuum on database file zTarget.
   319    319   ** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
   320    320   ** that it can be suspended and resumed like an RBU update.
   321    321   **
   322         -** The second argument to this function, which may not be NULL, identifies 
   323         -** a database in which to store the state of the RBU vacuum operation if
   324         -** it is suspended. The first time sqlite3rbu_vacuum() is called, to start
   325         -** an RBU vacuum operation, the state database should either not exist or
   326         -** be empty (contain no tables). If an RBU vacuum is suspended by calling
          322  +** The second argument to this function identifies a database in which 
          323  +** to store the state of the RBU vacuum operation if it is suspended. The 
          324  +** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
          325  +** operation, the state database should either not exist or be empty
          326  +** (contain no tables). If an RBU vacuum is suspended by calling 
   327    327   ** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
   328    328   ** returned SQLITE_DONE, the vacuum state is stored in the state database. 
   329    329   ** The vacuum can be resumed by calling this function to open a new RBU
   330    330   ** handle specifying the same target and state databases.
          331  +**
          332  +** If the second argument passed to this function is NULL, then the
          333  +** name of the state database is "<database>-vacuum", where <database>
          334  +** is the name of the target database file. In this case, on UNIX, if the
          335  +** state database is not already present in the file-system, it is created
          336  +** with the same permissions as the target db is made.
   331    337   **
   332    338   ** This function does not delete the state database after an RBU vacuum
   333    339   ** is completed, even if it created it. However, if the call to
   334    340   ** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
   335    341   ** of the state tables within the state database are zeroed. This way,
   336    342   ** the next call to sqlite3rbu_vacuum() opens a handle that starts a 
   337    343   ** new RBU vacuum operation.
................................................................................
   470    476   ** permyriadage progress of the same stage. If the rbu_count table does
   471    477   ** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
   472    478   ** table exists but is not correctly populated, the value of the *pnOne
   473    479   ** output variable during stage 1 is undefined.
   474    480   */
   475    481   void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int *pnTwo);
   476    482   
          483  +/*
          484  +** Obtain an indication as to the current stage of an RBU update or vacuum.
          485  +** This function always returns one of the SQLITE_RBU_STATE_XXX constants
          486  +** defined in this file. Return values should be interpreted as follows:
          487  +**
          488  +** SQLITE_RBU_STATE_OAL:
          489  +**   RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
          490  +**   may either add further data to the *-oal file, or compute data that will
          491  +**   be added by a subsequent call.
          492  +**
          493  +** SQLITE_RBU_STATE_MOVE:
          494  +**   RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
          495  +**   will move the *-oal file to the equivalent *-wal path. If the current
          496  +**   operation is an RBU update, then the updated version of the database
          497  +**   file will become visible to ordinary SQLite clients following the next
          498  +**   call to sqlite3rbu_step().
          499  +**
          500  +** SQLITE_RBU_STATE_CHECKPOINT:
          501  +**   RBU is currently performing an incremental checkpoint. The next call to
          502  +**   sqlite3rbu_step() will copy a page of data from the *-wal file into
          503  +**   the target database file.
          504  +**
          505  +** SQLITE_RBU_STATE_DONE:
          506  +**   The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
          507  +**   will immediately return SQLITE_DONE.
          508  +**
          509  +** SQLITE_RBU_STATE_ERROR:
          510  +**   An error has occurred. Any subsequent calls to sqlite3rbu_step() will
          511  +**   immediately return the SQLite error code associated with the error.
          512  +*/
          513  +#define SQLITE_RBU_STATE_OAL        1
          514  +#define SQLITE_RBU_STATE_MOVE       2
          515  +#define SQLITE_RBU_STATE_CHECKPOINT 3
          516  +#define SQLITE_RBU_STATE_DONE       4
          517  +#define SQLITE_RBU_STATE_ERROR      5
          518  +
          519  +int sqlite3rbu_state(sqlite3rbu *pRbu);
          520  +
   477    521   /*
   478    522   ** Create an RBU VFS named zName that accesses the underlying file-system
   479    523   ** via existing VFS zParent. Or, if the zParent parameter is passed NULL, 
   480    524   ** then the new RBU VFS uses the default system VFS to access the file-system.
   481    525   ** The new object is registered as a non-default VFS with SQLite before 
   482    526   ** returning.
   483    527   **

Changes to ext/rbu/test_rbu.c.

    13     13   
    14     14   #include "sqlite3.h"
    15     15   
    16     16   #if defined(SQLITE_TEST)
    17     17   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
    18     18   
    19     19   #include "sqlite3rbu.h"
    20         -#include <tcl.h>
           20  +#if defined(INCLUDE_SQLITE_TCL_H)
           21  +#  include "sqlite_tcl.h"
           22  +#else
           23  +#  include "tcl.h"
           24  +#  ifndef SQLITE_TCLAPI
           25  +#    define SQLITE_TCLAPI
           26  +#  endif
           27  +#endif
    21     28   #include <assert.h>
    22     29   
    23     30   /* From main.c */ 
    24     31   extern const char *sqlite3ErrName(int);
    25     32   extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*);
    26     33   
    27     34   void test_rbu_delta(sqlite3_context *pCtx, int nArg, sqlite3_value **apVal){
................................................................................
    45     52       Tcl_BackgroundError(interp);
    46     53     }
    47     54   
    48     55     Tcl_DecrRefCount(pScript);
    49     56   }
    50     57   
    51     58   
    52         -static int test_sqlite3rbu_cmd(
           59  +static int SQLITE_TCLAPI test_sqlite3rbu_cmd(
    53     60     ClientData clientData,
    54     61     Tcl_Interp *interp,
    55     62     int objc,
    56     63     Tcl_Obj *CONST objv[]
    57     64   ){
    58     65     int ret = TCL_OK;
    59     66     sqlite3rbu *pRbu = (sqlite3rbu*)clientData;
................................................................................
    65     72       {"step", 2, ""},              /* 0 */
    66     73       {"close", 2, ""},             /* 1 */
    67     74       {"create_rbu_delta", 2, ""},  /* 2 */
    68     75       {"savestate", 2, ""},         /* 3 */
    69     76       {"dbMain_eval", 3, "SQL"},    /* 4 */
    70     77       {"bp_progress", 2, ""},       /* 5 */
    71     78       {"db", 3, "RBU"},             /* 6 */
           79  +    {"state", 2, ""},             /* 7 */
           80  +    {"progress", 2, ""},          /* 8 */
    72     81       {0,0,0}
    73     82     };
    74     83     int iCmd;
    75     84   
    76     85     if( objc<2 ){
    77     86       Tcl_WrongNumArgs(interp, 1, objv, "METHOD");
    78     87       return TCL_ERROR;
................................................................................
   162    171             ret = TCL_ERROR;
   163    172           }else{
   164    173             Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
   165    174           }
   166    175         }
   167    176         break;
   168    177       }
          178  +    case 7: /* state */ {
          179  +      const char *aRes[] = { 0, "oal", "move", "checkpoint", "done", "error" };
          180  +      int eState = sqlite3rbu_state(pRbu);
          181  +      assert( eState>0 && eState<=5 );
          182  +      Tcl_SetResult(interp, (char*)aRes[eState], TCL_STATIC);
          183  +      break;
          184  +    }
          185  +    case 8: /* progress */ {
          186  +      sqlite3_int64 nStep =  sqlite3rbu_progress(pRbu);
          187  +      Tcl_SetObjResult(interp, Tcl_NewWideIntObj(nStep));
          188  +      break;
          189  +    }
   169    190   
   170    191       default: /* seems unlikely */
   171    192         assert( !"cannot happen" );
   172    193         break;
   173    194     }
   174    195   
   175    196     return ret;
   176    197   }
   177    198   
   178    199   /*
   179    200   ** Tclcmd: sqlite3rbu CMD <target-db> <rbu-db> ?<state-db>?
   180    201   */
   181         -static int test_sqlite3rbu(
          202  +static int SQLITE_TCLAPI test_sqlite3rbu(
   182    203     ClientData clientData,
   183    204     Tcl_Interp *interp,
   184    205     int objc,
   185    206     Tcl_Obj *CONST objv[]
   186    207   ){
   187    208     sqlite3rbu *pRbu = 0;
   188    209     const char *zCmd;
................................................................................
   204    225     Tcl_SetObjResult(interp, objv[1]);
   205    226     return TCL_OK;
   206    227   }
   207    228   
   208    229   /*
   209    230   ** Tclcmd: sqlite3rbu_vacuum CMD <target-db> <state-db>
   210    231   */
   211         -static int test_sqlite3rbu_vacuum(
          232  +static int SQLITE_TCLAPI test_sqlite3rbu_vacuum(
   212    233     ClientData clientData,
   213    234     Tcl_Interp *interp,
   214    235     int objc,
   215    236     Tcl_Obj *CONST objv[]
   216    237   ){
   217    238     sqlite3rbu *pRbu = 0;
   218    239     const char *zCmd;
   219    240     const char *zTarget;
   220    241     const char *zStateDb = 0;
   221    242   
   222         -  if( objc!=4 ){
   223         -    Tcl_WrongNumArgs(interp, 1, objv, "NAME TARGET-DB STATE-DB");
          243  +  if( objc!=3 && objc!=4 ){
          244  +    Tcl_WrongNumArgs(interp, 1, objv, "NAME TARGET-DB ?STATE-DB?");
   224    245       return TCL_ERROR;
   225    246     }
   226    247     zCmd = Tcl_GetString(objv[1]);
   227    248     zTarget = Tcl_GetString(objv[2]);
   228         -  zStateDb = Tcl_GetString(objv[3]);
          249  +  if( objc==4 ) zStateDb = Tcl_GetString(objv[3]);
   229    250   
   230    251     pRbu = sqlite3rbu_vacuum(zTarget, zStateDb);
   231    252     Tcl_CreateObjCommand(interp, zCmd, test_sqlite3rbu_cmd, (ClientData)pRbu, 0);
   232    253     Tcl_SetObjResult(interp, objv[1]);
   233    254     return TCL_OK;
   234    255   }
   235    256   
   236    257   /*
   237    258   ** Tclcmd: sqlite3rbu_create_vfs ?-default? NAME PARENT
   238    259   */
   239         -static int test_sqlite3rbu_create_vfs(
          260  +static int SQLITE_TCLAPI test_sqlite3rbu_create_vfs(
   240    261     ClientData clientData,
   241    262     Tcl_Interp *interp,
   242    263     int objc,
   243    264     Tcl_Obj *CONST objv[]
   244    265   ){
   245    266     const char *zName;
   246    267     const char *zParent;
................................................................................
   267    288     Tcl_ResetResult(interp);
   268    289     return TCL_OK;
   269    290   }
   270    291   
   271    292   /*
   272    293   ** Tclcmd: sqlite3rbu_destroy_vfs NAME
   273    294   */
   274         -static int test_sqlite3rbu_destroy_vfs(
          295  +static int SQLITE_TCLAPI test_sqlite3rbu_destroy_vfs(
   275    296     ClientData clientData,
   276    297     Tcl_Interp *interp,
   277    298     int objc,
   278    299     Tcl_Obj *CONST objv[]
   279    300   ){
   280    301     const char *zName;
   281    302   
................................................................................
   288    309     sqlite3rbu_destroy_vfs(zName);
   289    310     return TCL_OK;
   290    311   }
   291    312   
   292    313   /*
   293    314   ** Tclcmd: sqlite3rbu_internal_test
   294    315   */
   295         -static int test_sqlite3rbu_internal_test(
          316  +static int SQLITE_TCLAPI test_sqlite3rbu_internal_test(
   296    317     ClientData clientData,
   297    318     Tcl_Interp *interp,
   298    319     int objc,
   299    320     Tcl_Obj *CONST objv[]
   300    321   ){
   301    322     sqlite3 *db;
   302    323   
................................................................................
   329    350     for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
   330    351       Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
   331    352     }
   332    353     return TCL_OK;
   333    354   }
   334    355   
   335    356   #else
   336         -#include <tcl.h>
          357  +#if defined(INCLUDE_SQLITE_TCL_H)
          358  +#  include "sqlite_tcl.h"
          359  +#else
          360  +#  include "tcl.h"
          361  +#endif
   337    362   int SqliteRbu_Init(Tcl_Interp *interp){ return TCL_OK; }
   338    363   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
   339    364   #endif /* defined(SQLITE_TEST) */

Changes to ext/rtree/rtree.c.

  2796   2796     if( f<d ){
  2797   2797       f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
  2798   2798     }
  2799   2799     return f;
  2800   2800   }
  2801   2801   #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
  2802   2802   
         2803  +/*
         2804  +** A constraint has failed while inserting a row into an rtree table. 
         2805  +** Assuming no OOM error occurs, this function sets the error message 
         2806  +** (at pRtree->base.zErrMsg) to an appropriate value and returns
         2807  +** SQLITE_CONSTRAINT.
         2808  +**
         2809  +** Parameter iCol is the index of the leftmost column involved in the
         2810  +** constraint failure. If it is 0, then the constraint that failed is
         2811  +** the unique constraint on the id column. Otherwise, it is the rtree
         2812  +** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
         2813  +**
         2814  +** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
         2815  +*/
         2816  +static int rtreeConstraintError(Rtree *pRtree, int iCol){
         2817  +  sqlite3_stmt *pStmt = 0;
         2818  +  char *zSql; 
         2819  +  int rc;
         2820  +
         2821  +  assert( iCol==0 || iCol%2 );
         2822  +  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
         2823  +  if( zSql ){
         2824  +    rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
         2825  +  }else{
         2826  +    rc = SQLITE_NOMEM;
         2827  +  }
         2828  +  sqlite3_free(zSql);
         2829  +
         2830  +  if( rc==SQLITE_OK ){
         2831  +    if( iCol==0 ){
         2832  +      const char *zCol = sqlite3_column_name(pStmt, 0);
         2833  +      pRtree->base.zErrMsg = sqlite3_mprintf(
         2834  +          "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
         2835  +      );
         2836  +    }else{
         2837  +      const char *zCol1 = sqlite3_column_name(pStmt, iCol);
         2838  +      const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
         2839  +      pRtree->base.zErrMsg = sqlite3_mprintf(
         2840  +          "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
         2841  +      );
         2842  +    }
         2843  +  }
         2844  +
         2845  +  sqlite3_finalize(pStmt);
         2846  +  return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
         2847  +}
         2848  +
         2849  +
  2803   2850   
  2804   2851   /*
  2805   2852   ** The xUpdate method for rtree module virtual tables.
  2806   2853   */
  2807   2854   static int rtreeUpdate(
  2808   2855     sqlite3_vtab *pVtab, 
  2809   2856     int nData, 
................................................................................
  2846   2893   
  2847   2894   #ifndef SQLITE_RTREE_INT_ONLY
  2848   2895       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  2849   2896         for(ii=0; ii<nData-4; ii+=2){
  2850   2897           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
  2851   2898           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
  2852   2899           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
  2853         -          rc = SQLITE_CONSTRAINT;
         2900  +          rc = rtreeConstraintError(pRtree, ii+1);
  2854   2901             goto constraint;
  2855   2902           }
  2856   2903         }
  2857   2904       }else
  2858   2905   #endif
  2859   2906       {
  2860   2907         for(ii=0; ii<nData-4; ii+=2){
  2861   2908           cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
  2862   2909           cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
  2863   2910           if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
  2864         -          rc = SQLITE_CONSTRAINT;
         2911  +          rc = rtreeConstraintError(pRtree, ii+1);
  2865   2912             goto constraint;
  2866   2913           }
  2867   2914         }
  2868   2915       }
  2869   2916   
  2870   2917       /* If a rowid value was supplied, check if it is already present in 
  2871   2918       ** the table. If so, the constraint has failed. */
................................................................................
  2878   2925           sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
  2879   2926           steprc = sqlite3_step(pRtree->pReadRowid);
  2880   2927           rc = sqlite3_reset(pRtree->pReadRowid);
  2881   2928           if( SQLITE_ROW==steprc ){
  2882   2929             if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
  2883   2930               rc = rtreeDeleteRowid(pRtree, cell.iRowid);
  2884   2931             }else{
  2885         -            rc = SQLITE_CONSTRAINT;
         2932  +            rc = rtreeConstraintError(pRtree, 0);
  2886   2933               goto constraint;
  2887   2934             }
  2888   2935           }
  2889   2936         }
  2890   2937         bHaveRowid = 1;
  2891   2938       }
  2892   2939     }
................................................................................
  2961   3008   static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
  2962   3009     const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
  2963   3010     char *zSql;
  2964   3011     sqlite3_stmt *p;
  2965   3012     int rc;
  2966   3013     i64 nRow = 0;
  2967   3014   
         3015  +  if( sqlite3_table_column_metadata(db,pRtree->zDb,"sqlite_stat1",
         3016  +          0,0,0,0,0,0)==SQLITE_ERROR ){
         3017  +    pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
         3018  +    return SQLITE_OK;
         3019  +  }
  2968   3020     zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
  2969   3021     if( zSql==0 ){
  2970   3022       rc = SQLITE_NOMEM;
  2971   3023     }else{
  2972   3024       rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
  2973   3025       if( rc==SQLITE_OK ){
  2974   3026         if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);

Changes to ext/rtree/rtree1.test.

   190    190   do_test rtree-2.1.3 {
   191    191     execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
   192    192     execsql { SELECT ii FROM t1 ORDER BY ii }
   193    193   } {1 2 3}
   194    194   
   195    195   do_test rtree-2.2.1 {
   196    196     catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
   197         -} {1 {constraint failed}}
          197  +} {1 {UNIQUE constraint failed: t1.ii}}
   198    198   do_test rtree-2.2.2 {
   199    199     catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
   200         -} {1 {constraint failed}}
          200  +} {1 {rtree constraint failed: t1.(y1<=y2)}}
   201    201   do_test rtree-2.2.3 {
   202    202     catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
   203         -} {1 {constraint failed}}
          203  +} {1 {rtree constraint failed: t1.(x1<=x2)}}
   204    204   do_test rtree-2.2.4 {
   205    205     execsql { SELECT ii FROM t1 ORDER BY ii }
   206    206   } {1 2 3}
   207    207   
   208    208   do_test rtree-2.X {
   209    209     execsql { DROP TABLE t1 }
   210    210   } {}
................................................................................
   232    232       SELECT * FROM t1;
   233    233     }
   234    234   } {5 1 3 2 4 6 2 6 4 8}
   235    235   
   236    236   # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
   237    237   do_test rtree-3.2.1 {
   238    238     catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
   239         -} {1 {constraint failed}}
          239  +} {1 {rtree constraint failed: t1.(y1<=y2)}}
   240    240   do_test rtree-3.2.2 {
   241    241     catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
   242    242   } {0 {}}
   243    243   
   244    244   #----------------------------------------------------------------------------
   245    245   # Test cases rtree-5.* test DELETE operations.
   246    246   #
................................................................................
   486    486       ABORT    1 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
   487    487       IGNORE   1 0 {1 1 2 3 4   2 2 3 4 5               4 4 5 6 7   5 3 4 5 6}
   488    488       FAIL     1 1 {1 1 2 3 4   2 2 3 4 5               4 4 5 6 7   5 3 4 5 6}
   489    489       REPLACE  1 0 {1 4 5 6 7   2 2 3 4 5                           5 3 4 5 6}
   490    490     }
   491    491   
   492    492     4    "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
   493         -    ROLLBACK 0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
   494         -    ABORT    0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
          493  +    ROLLBACK 0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6}
          494  +    ABORT    0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
   495    495       IGNORE   0 0 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
   496         -    FAIL     0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
   497         -    REPLACE  0 1 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
          496  +    FAIL     0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
          497  +    REPLACE  0 2 {1 1 2 3 4   2 2 3 4 5   3 3 4 5 6   4 4 5 6 7}
   498    498     }
   499    499   
   500    500   } {
   501    501     foreach {mode uses error data} $testdata {
   502    502       db_restore_and_reopen
   503    503   
   504    504       set sql [string map [list %CONF% "OR $mode"] $sql_template]
................................................................................
   506    506   
   507    507       execsql {
   508    508         BEGIN;
   509    509           INSERT INTO t1 VALUES(4,   4, 5, 6, 7);
   510    510       }
   511    511   
   512    512       set res(0) {0 {}}
   513         -    set res(1) {1 {constraint failed}}
          513  +    set res(1) {1 {UNIQUE constraint failed: t1.idx}}
          514  +    set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
          515  +
   514    516       do_catchsql_test $testname.1 $sql $res($error)
   515    517       do_test $testname.2 [list sql_uses_stmt db $sql] $uses
   516    518       do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
   517    519   
   518    520       do_test $testname.4 { rtree_check db t1 } 0
   519    521       db close
   520    522     }

Changes to ext/rtree/rtree3.test.

    43     43   #
    44     44   #   rtree3-6: Test OOM while deleting all rows of a table, one at a time.
    45     45   #
    46     46   #   rtree3-7: OOM during an ALTER TABLE RENAME TABLE command.
    47     47   #
    48     48   #   rtree3-8: Test OOM while registering the r-tree module with sqlite.
    49     49   #
    50         -
           50  +#   rtree3-11: OOM following a constraint failure
           51  +#
    51     52   do_faultsim_test rtree3-1 -faults oom* -prep {
    52     53     faultsim_delete_and_reopen
    53     54   } -body {
    54     55     execsql {
    55     56       BEGIN TRANSACTION;
    56     57       CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
    57     58       INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
................................................................................
   230    231     execsql { SELECT * FROM rt }
   231    232   } -body {
   232    233     execsql { SELECT ii FROM rt WHERE ii MATCH cube(4.5, 5.5, 6.5, 1, 1, 1) }
   233    234   } -test {
   234    235     faultsim_test_result {0 2}
   235    236   }
   236    237   
          238  +
          239  +do_test rtree3-11.prep {
          240  +  faultsim_delete_and_reopen
          241  +  execsql { 
          242  +    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
          243  +    INSERT INTO rt VALUES(1, 2, 3, 4, 5);
          244  +  }
          245  +  faultsim_save_and_close
          246  +} {}
          247  +do_faultsim_test rtree3-10.1 -faults oom-* -prep {
          248  +  faultsim_restore_and_reopen
          249  +  execsql { SELECT * FROM rt }
          250  +} -body {
          251  +  execsql { INSERT INTO rt VALUES(1, 2, 3, 4, 5) }
          252  +} -test {
          253  +  faultsim_test_result {1 {UNIQUE constraint failed: rt.ii}} \
          254  +                       {1 {constraint failed}}
          255  +}
          256  +do_faultsim_test rtree3-10.2 -faults oom-* -prep {
          257  +  faultsim_restore_and_reopen
          258  +  execsql { SELECT * FROM rt }
          259  +} -body {
          260  +  execsql { INSERT INTO rt VALUES(2, 2, 3, 5, 4) }
          261  +} -test {
          262  +  faultsim_test_result {1 {rtree constraint failed: rt.(y1<=y2)}} \
          263  +                       {1 {constraint failed}}
          264  +}
          265  +
   237    266   finish_test

Changes to ext/rtree/rtreeC.test.

   344    344     WHERE (x1 BETWEEN xmin AND xmax);
   345    345   } {
   346    346     0 0 1 {SCAN TABLE xdir} 
   347    347     0 1 0 {SCAN TABLE rt VIRTUAL TABLE INDEX 2:B0D1}
   348    348     0 2 2 {SCAN TABLE ydir} 
   349    349     2 4
   350    350   }
   351         -
   352         -finish_test
   353         -
   354         -
   355    351   
   356    352   finish_test

Added ext/rtree/rtreeG.test.

            1  +# 2016-05-32
            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  +# This file contains tests for the r-tree module.
           12  +#
           13  +# Verify that no invalid SQL is run during initialization
           14  +
           15  +if {![info exists testdir]} {
           16  +  set testdir [file join [file dirname [info script]] .. .. test]
           17  +} 
           18  +source $testdir/tester.tcl
           19  +ifcapable !rtree { finish_test ; return }
           20  +
           21  +db close
           22  +sqlite3_shutdown
           23  +test_sqlite3_log [list lappend ::log]
           24  +set ::log [list]
           25  +sqlite3 db test.db
           26  +
           27  +
           28  +set ::log {}
           29  +do_execsql_test rtreeG-1.1 {
           30  +  CREATE VIRTUAL TABLE t1 USING rtree(id,x0,x1,y0,y1);
           31  +} {}
           32  +do_test rtreeG-1.1log {
           33  +  set ::log
           34  +} {}
           35  +
           36  +do_execsql_test rtreeG-1.2 {
           37  +  INSERT INTO t1 VALUES(1,10,15,5,23),(2,20,21,5,23),(3,10,15,20,30);
           38  +  SELECT id from t1 WHERE x0>8 AND x1<16 AND y0>2 AND y1<25;
           39  +} {1}
           40  +do_test rtreeG-1.2log {
           41  +  set ::log
           42  +} {}
           43  +
           44  +db close
           45  +sqlite3 db test.db
           46  +do_execsql_test rtreeG-1.3 {
           47  +  SELECT id from t1 WHERE x0>8 AND x1<16 AND y0>2 AND y1<25;
           48  +} {1}
           49  +do_test rtreeG-1.3log {
           50  +  set ::log
           51  +} {}
           52  +
           53  +do_execsql_test rtreeG-1.4 {
           54  +  DROP TABLE t1;
           55  +} {}
           56  +do_test rtreeG-1.4log {
           57  +  set ::log
           58  +} {}
           59  +
           60  +db close
           61  +sqlite3_shutdown
           62  +test_sqlite3_log
           63  +sqlite3_initialize
           64  +sqlite3 db test.db
           65  +
           66  +finish_test

Changes to ext/session/sqlite3session.c.

   274    274   ** Read a varint value from aBuf[] into *piVal. Return the number of 
   275    275   ** bytes read.
   276    276   */
   277    277   static int sessionVarintGet(u8 *aBuf, int *piVal){
   278    278     return getVarint32(aBuf, *piVal);
   279    279   }
   280    280   
          281  +/* Load an unaligned and unsigned 32-bit integer */
          282  +#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
          283  +
   281    284   /*
   282    285   ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
   283    286   ** the value read.
   284    287   */
   285    288   static sqlite3_int64 sessionGetI64(u8 *aRec){
   286         -  return (((sqlite3_int64)aRec[0]) << 56)
   287         -       + (((sqlite3_int64)aRec[1]) << 48)
   288         -       + (((sqlite3_int64)aRec[2]) << 40)
   289         -       + (((sqlite3_int64)aRec[3]) << 32)
   290         -       + (((sqlite3_int64)aRec[4]) << 24)
   291         -       + (((sqlite3_int64)aRec[5]) << 16)
   292         -       + (((sqlite3_int64)aRec[6]) <<  8)
   293         -       + (((sqlite3_int64)aRec[7]) <<  0);
          289  +  u64 x = SESSION_UINT32(aRec);
          290  +  u32 y = SESSION_UINT32(aRec+4);
          291  +  x = (x<<32) + y;
          292  +  return (sqlite3_int64)x;
   294    293   }
   295    294   
   296    295   /*
   297    296   ** Write a 64-bit big-endian integer value to the buffer aBuf[].
   298    297   */
   299    298   static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
   300    299     aBuf[0] = (i>>56) & 0xFF;

Changes to ext/session/sqlite3session.h.

     1      1   
     2         -#ifndef __SQLITESESSION_H_
            2  +#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
     3      3   #define __SQLITESESSION_H_ 1
     4      4   
     5      5   /*
     6      6   ** Make sure we can call this stuff from C++.
     7      7   */
     8      8   #ifdef __cplusplus
     9      9   extern "C" {
................................................................................
  1270   1270   /*
  1271   1271   ** Make sure we can call this stuff from C++.
  1272   1272   */
  1273   1273   #ifdef __cplusplus
  1274   1274   }
  1275   1275   #endif
  1276   1276   
  1277         -#endif  /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
         1277  +#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */

Changes to ext/session/test_session.c.

     1      1   
     2      2   #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_SESSION) \
     3      3    && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
     4      4   
     5      5   #include "sqlite3session.h"
     6      6   #include <assert.h>
     7      7   #include <string.h>
     8         -#include <tcl.h>
            8  +#if defined(INCLUDE_SQLITE_TCL_H)
            9  +#  include "sqlite_tcl.h"
           10  +#else
           11  +#  include "tcl.h"
           12  +#  ifndef SQLITE_TCLAPI
           13  +#    define SQLITE_TCLAPI
           14  +#  endif
           15  +#endif
     9     16   
    10     17   typedef struct TestSession TestSession;
    11     18   struct TestSession {
    12     19     sqlite3_session *pSession;
    13     20     Tcl_Interp *interp;
    14     21     Tcl_Obj *pFilterScript;
    15     22   };
................................................................................
   103    110   **          $session changeset
   104    111   **          $session delete
   105    112   **          $session enable BOOL
   106    113   **          $session indirect INTEGER
   107    114   **          $session patchset
   108    115   **          $session table_filter SCRIPT
   109    116   */
   110         -static int test_session_cmd(
          117  +static int SQLITE_TCLAPI test_session_cmd(
   111    118     void *clientData,
   112    119     Tcl_Interp *interp,
   113    120     int objc,
   114    121     Tcl_Obj *CONST objv[]
   115    122   ){
   116    123     TestSession *p = (TestSession*)clientData;
   117    124     sqlite3_session *pSession = p->pSession;
................................................................................
   236    243         break;
   237    244       }
   238    245     }
   239    246   
   240    247     return TCL_OK;
   241    248   }
   242    249   
   243         -static void test_session_del(void *clientData){
          250  +static void SQLITE_TCLAPI test_session_del(void *clientData){
   244    251     TestSession *p = (TestSession*)clientData;
   245    252     if( p->pFilterScript ) Tcl_DecrRefCount(p->pFilterScript);
   246    253     sqlite3session_delete(p->pSession);
   247    254     ckfree((char*)p);
   248    255   }
   249    256   
   250    257   /*
   251    258   ** Tclcmd:  sqlite3session CMD DB-HANDLE DB-NAME
   252    259   */
   253         -static int test_sqlite3session(
          260  +static int SQLITE_TCLAPI test_sqlite3session(
   254    261     void * clientData,
   255    262     Tcl_Interp *interp,
   256    263     int objc,
   257    264     Tcl_Obj *CONST objv[]
   258    265   ){
   259    266     sqlite3 *db;
   260    267     Tcl_CmdInfo info;
................................................................................
   602    609     return SQLITE_OK;
   603    610   }
   604    611   
   605    612   
   606    613   /*
   607    614   ** sqlite3changeset_apply DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?
   608    615   */
   609         -static int test_sqlite3changeset_apply(
          616  +static int SQLITE_TCLAPI test_sqlite3changeset_apply(
   610    617     void * clientData,
   611    618     Tcl_Interp *interp,
   612    619     int objc,
   613    620     Tcl_Obj *CONST objv[]
   614    621   ){
   615    622     sqlite3 *db;                    /* Database handle */
   616    623     Tcl_CmdInfo info;               /* Database Tcl command (objv[1]) info */
................................................................................
   657    664     Tcl_ResetResult(interp);
   658    665     return TCL_OK;
   659    666   }
   660    667   
   661    668   /*
   662    669   ** sqlite3changeset_apply_replace_all DB CHANGESET 
   663    670   */
   664         -static int test_sqlite3changeset_apply_replace_all(
          671  +static int SQLITE_TCLAPI test_sqlite3changeset_apply_replace_all(
   665    672     void * clientData,
   666    673     Tcl_Interp *interp,
   667    674     int objc,
   668    675     Tcl_Obj *CONST objv[]
   669    676   ){
   670    677     sqlite3 *db;                    /* Database handle */
   671    678     Tcl_CmdInfo info;               /* Database Tcl command (objv[1]) info */
................................................................................
   692    699     return TCL_OK;
   693    700   }
   694    701   
   695    702   
   696    703   /*
   697    704   ** sqlite3changeset_invert CHANGESET
   698    705   */
   699         -static int test_sqlite3changeset_invert(
          706  +static int SQLITE_TCLAPI test_sqlite3changeset_invert(
   700    707     void * clientData,
   701    708     Tcl_Interp *interp,
   702    709     int objc,
   703    710     Tcl_Obj *CONST objv[]
   704    711   ){
   705    712     int rc;                         /* Return code from changeset_invert() */
   706    713     TestStreamInput sIn;            /* Input stream */
................................................................................
   731    738     sqlite3_free(sOut.p);
   732    739     return rc;
   733    740   }
   734    741   
   735    742   /*
   736    743   ** sqlite3changeset_concat LEFT RIGHT
   737    744   */
   738         -static int test_sqlite3changeset_concat(
          745  +static int SQLITE_TCLAPI test_sqlite3changeset_concat(
   739    746     void * clientData,
   740    747     Tcl_Interp *interp,
   741    748     int objc,
   742    749     Tcl_Obj *CONST objv[]
   743    750   ){
   744    751     int rc;                         /* Return code from changeset_invert() */
   745    752   
................................................................................
   779    786     sqlite3_free(sOut.p);
   780    787     return rc;
   781    788   }
   782    789   
   783    790   /*
   784    791   ** sqlite3session_foreach VARNAME CHANGESET SCRIPT
   785    792   */
   786         -static int test_sqlite3session_foreach(
          793  +static int SQLITE_TCLAPI test_sqlite3session_foreach(
   787    794     void * clientData,
   788    795     Tcl_Interp *interp,
   789    796     int objc,
   790    797     Tcl_Obj *CONST objv[]
   791    798   ){
   792    799     void *pChangeset;
   793    800     int nChangeset;

Changes to main.mk.

   320    320     $(TOP)/src/test_windirent.c \
   321    321     $(TOP)/src/test_wsd.c
   322    322   
   323    323   # Extensions to be statically loaded.
   324    324   #
   325    325   TESTSRC += \
   326    326     $(TOP)/ext/misc/amatch.c \
          327  +  $(TOP)/ext/misc/carray.c \
   327    328     $(TOP)/ext/misc/closure.c \
          329  +  $(TOP)/ext/misc/csv.c \
   328    330     $(TOP)/ext/misc/eval.c \
   329    331     $(TOP)/ext/misc/fileio.c \
   330    332     $(TOP)/ext/misc/fuzzer.c \
   331    333     $(TOP)/ext/misc/ieee754.c \
   332    334     $(TOP)/ext/misc/nextchar.c \
   333    335     $(TOP)/ext/misc/percentile.c \
   334    336     $(TOP)/ext/misc/regexp.c \
................................................................................
   384    386     $(TOP)/ext/fts3/fts3.c \
   385    387     $(TOP)/ext/fts3/fts3_aux.c \
   386    388     $(TOP)/ext/fts3/fts3_expr.c \
   387    389     $(TOP)/ext/fts3/fts3_tokenizer.c \
   388    390     $(TOP)/ext/fts3/fts3_write.c \
   389    391     $(TOP)/ext/async/sqlite3async.c \
   390    392     $(TOP)/ext/session/sqlite3session.c \
   391         -  $(TOP)/ext/session/test_session.c \
   392         -  $(FTS5_SRC)
          393  +  $(TOP)/ext/session/test_session.c 
   393    394   
   394    395   # Header files used by all library source files.
   395    396   #
   396    397   HDR = \
   397    398      $(TOP)/src/btree.h \
   398    399      $(TOP)/src/btreeInt.h \
   399    400      $(TOP)/src/hash.h \
................................................................................
   447    448   
   448    449   # executables needed for testing
   449    450   #
   450    451   TESTPROGS = \
   451    452     testfixture$(EXE) \
   452    453     sqlite3$(EXE) \
   453    454     sqlite3_analyzer$(EXE) \
   454         -  sqldiff$(EXE)
          455  +  sqldiff$(EXE) \
          456  +  dbhash$(EXE)
   455    457   
   456    458   # Databases containing fuzzer test cases
   457    459   #
   458    460   FUZZDATA = \
   459    461     $(TOP)/test/fuzzdata1.db \
   460    462     $(TOP)/test/fuzzdata2.db \
   461    463     $(TOP)/test/fuzzdata3.db \
................................................................................
   464    466   # Standard options to testfixture
   465    467   #
   466    468   TESTOPTS = --verbose=file --output=test-out.txt
   467    469   
   468    470   # Extra compiler options for various shell tools
   469    471   #
   470    472   SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
          473  +SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
          474  +SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   471    475   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   472    476   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
   473    477   
   474    478   # This is the default Makefile target.  The objects listed here
   475    479   # are what get build when you type just "make" with no arguments.
   476    480   #
   477    481   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
................................................................................
   484    488   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) $(SHELL_OPT) \
   485    489   		$(TOP)/src/shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
   486    490   
   487    491   sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
   488    492   	$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
   489    493   		$(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) $(THREADLIB)
   490    494   
          495  +dbhash$(EXE):	$(TOP)/tool/dbhash.c sqlite3.c sqlite3.h
          496  +	$(TCCX) -o dbhash$(EXE) -DSQLITE_THREADSAFE=0 \
          497  +		$(TOP)/tool/dbhash.c sqlite3.c $(TLIBS) $(THREADLIB)
          498  +
          499  +scrub$(EXE):	$(TOP)/ext/misc/scrub.c sqlite3.o
          500  +	$(TCC) -I. -DSCRUB_STANDALONE -o scrub$(EXE) $(TOP)/ext/misc/scrub.c sqlite3.o $(THREADLIB)
          501  +
   491    502   srcck1$(EXE):	$(TOP)/tool/srcck1.c
   492    503   	$(BCC) -o srcck1$(EXE) $(TOP)/tool/srcck1.c
   493    504   
   494    505   sourcetest:	srcck1$(EXE) sqlite3.c
   495    506   	./srcck1 sqlite3.c
   496    507   
   497    508   fuzzershell$(EXE):	$(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h

Changes to src/alter.c.

   597    597     const char *zDb;          /* Database name */
   598    598     const char *zTab;         /* Table name */
   599    599     char *zCol;               /* Null-terminated column definition */
   600    600     Column *pCol;             /* The new column */
   601    601     Expr *pDflt;              /* Default value for the new column */
   602    602     sqlite3 *db;              /* The database connection; */
   603    603     Vdbe *v = pParse->pVdbe;  /* The prepared statement under construction */
          604  +  int r1;                   /* Temporary registers */
   604    605   
   605    606     db = pParse->db;
   606    607     if( pParse->nErr || db->mallocFailed ) return;
   607    608     assert( v!=0 );
   608    609     pNew = pParse->pNewTable;
   609    610     assert( pNew );
   610    611   
................................................................................
   691    692         zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
   692    693         zTab
   693    694       );
   694    695       sqlite3DbFree(db, zCol);
   695    696       db->flags = savedDbFlags;
   696    697     }
   697    698   
   698         -  /* If the default value of the new column is NULL, then the file
   699         -  ** format to 2. If the default value of the new column is not NULL,
   700         -  ** the file format be 3.  Back when this feature was first added
   701         -  ** in 2006, we went to the trouble to upgrade the file format to the
   702         -  ** minimum support values.  But 10-years on, we can assume that all
   703         -  ** extent versions of SQLite support file-format 4, so we always and
   704         -  ** unconditionally upgrade to 4.
          699  +  /* Make sure the schema version is at least 3.  But do not upgrade
          700  +  ** from less than 3 to 4, as that will corrupt any preexisting DESC
          701  +  ** index.
   705    702     */
   706         -  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 
   707         -                    SQLITE_MAX_FILE_FORMAT);
          703  +  r1 = sqlite3GetTempReg(pParse);
          704  +  sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
          705  +  sqlite3VdbeUsesBtree(v, iDb);
          706  +  sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
          707  +  sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
          708  +  VdbeCoverage(v);
          709  +  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
          710  +  sqlite3ReleaseTempReg(pParse, r1);
   708    711   
   709    712     /* Reload the schema of the modified table. */
   710    713     reloadTableSchema(pParse, pTab, pTab->zName);
   711    714   }
   712    715   
   713    716   /*
   714    717   ** This function is called by the parser after the table-name in

Changes to src/auth.c.

   107    107     const char *zCol,               /* Column name */
   108    108     int iDb                         /* Index of containing database. */
   109    109   ){
   110    110     sqlite3 *db = pParse->db;       /* Database handle */
   111    111     char *zDb = db->aDb[iDb].zName; /* Name of attached database */
   112    112     int rc;                         /* Auth callback return code */
   113    113   
          114  +  if( db->init.busy ) return SQLITE_OK;
   114    115     rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
   115    116   #ifdef SQLITE_USER_AUTHENTICATION
   116    117                    ,db->auth.zAuthUser
   117    118   #endif
   118    119                   );
   119    120     if( rc==SQLITE_DENY ){
   120    121       if( db->nDb>2 || iDb!=0 ){

Changes to src/backup.c.

   773    773   #endif
   774    774   
   775    775     /* 0x7FFFFFFF is the hard limit for the number of pages in a database
   776    776     ** file. By passing this as the number of pages to copy to
   777    777     ** sqlite3_backup_step(), we can guarantee that the copy finishes 
   778    778     ** within a single call (unless an error occurs). The assert() statement
   779    779     ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
   780         -  ** or an error code.
   781         -  */
          780  +  ** or an error code.  */
   782    781     sqlite3_backup_step(&b, 0x7FFFFFFF);
   783    782     assert( b.rc!=SQLITE_OK );
          783  +
   784    784     rc = sqlite3_backup_finish(&b);
   785    785     if( rc==SQLITE_OK ){
   786    786       pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
   787    787     }else{
   788    788       sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
   789    789     }
   790    790   

Changes to src/btree.c.

   642    642   **
   643    643   ** Verify that the cursor holds the mutex on its BtShared
   644    644   */
   645    645   #ifdef SQLITE_DEBUG
   646    646   static int cursorHoldsMutex(BtCursor *p){
   647    647     return sqlite3_mutex_held(p->pBt->mutex);
   648    648   }
          649  +
          650  +/* Verify that the cursor and the BtShared agree about what is the current
          651  +** database connetion. This is important in shared-cache mode. If the database 
          652  +** connection pointers get out-of-sync, it is possible for routines like
          653  +** btreeInitPage() to reference an stale connection pointer that references a
          654  +** a connection that has already closed.  This routine is used inside assert()
          655  +** statements only and for the purpose of double-checking that the btree code
          656  +** does keep the database connection pointers up-to-date.
          657  +*/
   649    658   static int cursorOwnsBtShared(BtCursor *p){
   650    659     assert( cursorHoldsMutex(p) );
   651    660     return (p->pBtree->db==p->pBt->db);
   652    661   }
   653    662   #endif
   654    663   
   655    664   /*
................................................................................
   801    810   ** If the cursor is open on an intkey table, then the integer key
   802    811   ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
   803    812   ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is 
   804    813   ** set to point to a malloced buffer pCur->nKey bytes in size containing 
   805    814   ** the key.
   806    815   */
   807    816   static int saveCursorKey(BtCursor *pCur){
   808         -  int rc;
          817  +  int rc = SQLITE_OK;
   809    818     assert( CURSOR_VALID==pCur->eState );
   810    819     assert( 0==pCur->pKey );
   811    820     assert( cursorHoldsMutex(pCur) );
   812    821   
   813         -  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
   814         -  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
   815         -
   816         -  /* If this is an intKey table, then the above call to BtreeKeySize()
   817         -  ** stores the integer key in pCur->nKey. In this case this value is
   818         -  ** all that is required. Otherwise, if pCur is not open on an intKey
   819         -  ** table, then malloc space for and store the pCur->nKey bytes of key 
   820         -  ** data.  */
   821         -  if( 0==pCur->curIntKey ){
   822         -    void *pKey = sqlite3Malloc( pCur->nKey );
          822  +  if( pCur->curIntKey ){
          823  +    /* Only the rowid is required for a table btree */
          824  +    pCur->nKey = sqlite3BtreeIntegerKey(pCur);
          825  +  }else{
          826  +    /* For an index btree, save the complete key content */
          827  +    void *pKey;
          828  +    pCur->nKey = sqlite3BtreePayloadSize(pCur);
          829  +    pKey = sqlite3Malloc( pCur->nKey );
   823    830       if( pKey ){
   824    831         rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
   825    832         if( rc==SQLITE_OK ){
   826    833           pCur->pKey = pKey;
   827    834         }else{
   828    835           sqlite3_free(pKey);
   829    836         }
................................................................................
  1875   1882     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1876   1883     pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
  1877   1884     flagByte &= ~PTF_LEAF;
  1878   1885     pPage->childPtrSize = 4-4*pPage->leaf;
  1879   1886     pPage->xCellSize = cellSizePtr;
  1880   1887     pBt = pPage->pBt;
  1881   1888     if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
  1882         -    /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
  1883         -    ** table b-tree page. */
         1889  +    /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
         1890  +    ** interior table b-tree page. */
  1884   1891       assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
  1885         -    /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
  1886         -    ** table b-tree page. */
         1892  +    /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
         1893  +    ** leaf table b-tree page. */
  1887   1894       assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
  1888   1895       pPage->intKey = 1;
  1889   1896       if( pPage->leaf ){
  1890   1897         pPage->intKeyLeaf = 1;
  1891   1898         pPage->xParseCell = btreeParseCellPtr;
  1892   1899       }else{
  1893   1900         pPage->intKeyLeaf = 0;
  1894   1901         pPage->xCellSize = cellSizePtrNoPayload;
  1895   1902         pPage->xParseCell = btreeParseCellPtrNoPayload;
  1896   1903       }
  1897   1904       pPage->maxLocal = pBt->maxLeaf;
  1898   1905       pPage->minLocal = pBt->minLeaf;
  1899   1906     }else if( flagByte==PTF_ZERODATA ){
  1900         -    /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
  1901         -    ** index b-tree page. */
         1907  +    /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
         1908  +    ** interior index b-tree page. */
  1902   1909       assert( (PTF_ZERODATA)==2 );
  1903         -    /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
  1904         -    ** index b-tree page. */
         1910  +    /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
         1911  +    ** leaf index b-tree page. */
  1905   1912       assert( (PTF_ZERODATA|PTF_LEAF)==10 );
  1906   1913       pPage->intKey = 0;
  1907   1914       pPage->intKeyLeaf = 0;
  1908   1915       pPage->xParseCell = btreeParseCellPtrIndex;
  1909   1916       pPage->maxLocal = pBt->maxLocal;
  1910   1917       pPage->minLocal = pBt->minLocal;
  1911   1918     }else{
................................................................................
  2529   2536       if( rc ) goto btree_open_out;
  2530   2537       pBt->usableSize = pBt->pageSize - nReserve;
  2531   2538       assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
  2532   2539      
  2533   2540   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  2534   2541       /* Add the new BtShared object to the linked list sharable BtShareds.
  2535   2542       */
         2543  +    pBt->nRef = 1;
  2536   2544       if( p->sharable ){
  2537   2545         MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
  2538         -      pBt->nRef = 1;
  2539   2546         MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
  2540   2547         if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  2541   2548           pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  2542   2549           if( pBt->mutex==0 ){
  2543   2550             rc = SQLITE_NOMEM_BKPT;
  2544   2551             goto btree_open_out;
  2545   2552           }
................................................................................
  2602   2609         sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
  2603   2610       }
  2604   2611     }
  2605   2612     if( mutexOpen ){
  2606   2613       assert( sqlite3_mutex_held(mutexOpen) );
  2607   2614       sqlite3_mutex_leave(mutexOpen);
  2608   2615     }
         2616  +  assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
  2609   2617     return rc;
  2610   2618   }
  2611   2619   
  2612   2620   /*
  2613   2621   ** Decrement the BtShared.nRef counter.  When it reaches zero,
  2614   2622   ** remove the BtShared structure from the sharing list.  Return
  2615   2623   ** true if the BtShared.nRef counter reaches zero and return
................................................................................
  4654   4662   */
  4655   4663   int sqlite3BtreeCursorIsValid(BtCursor *pCur){
  4656   4664     return pCur && pCur->eState==CURSOR_VALID;
  4657   4665   }
  4658   4666   #endif /* NDEBUG */
  4659   4667   
  4660   4668   /*
  4661         -** Set *pSize to the size of the buffer needed to hold the value of
  4662         -** the key for the current entry.  If the cursor is not pointing
  4663         -** to a valid entry, *pSize is set to 0. 
  4664         -**
  4665         -** For a table with the INTKEY flag set, this routine returns the key
  4666         -** itself, not the number of bytes in the key.
  4667         -**
  4668         -** The caller must position the cursor prior to invoking this routine.
  4669         -** 
  4670         -** This routine cannot fail.  It always returns SQLITE_OK.  
         4669  +** Return the value of the integer key or "rowid" for a table btree.
         4670  +** This routine is only valid for a cursor that is pointing into a
         4671  +** ordinary table btree.  If the cursor points to an index btree or
         4672  +** is invalid, the result of this routine is undefined.
  4671   4673   */
  4672         -int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
         4674  +i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
  4673   4675     assert( cursorHoldsMutex(pCur) );
  4674   4676     assert( pCur->eState==CURSOR_VALID );
         4677  +  assert( pCur->curIntKey );
  4675   4678     getCellInfo(pCur);
  4676         -  *pSize = pCur->info.nKey;
  4677         -  return SQLITE_OK;
         4679  +  return pCur->info.nKey;
  4678   4680   }
  4679   4681   
  4680   4682   /*
  4681         -** Set *pSize to the number of bytes of data in the entry the
  4682         -** cursor currently points to.
         4683  +** Return the number of bytes of payload for the entry that pCur is
         4684  +** currently pointing to.  For table btrees, this will be the amount
         4685  +** of data.  For index btrees, this will be the size of the key.
  4683   4686   **
  4684   4687   ** The caller must guarantee that the cursor is pointing to a non-NULL
  4685   4688   ** valid entry.  In other words, the calling procedure must guarantee
  4686   4689   ** that the cursor has Cursor.eState==CURSOR_VALID.
  4687         -**
  4688         -** Failure is not possible.  This function always returns SQLITE_OK.
  4689         -** It might just as well be a procedure (returning void) but we continue
  4690         -** to return an integer result code for historical reasons.
  4691   4690   */
  4692         -int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
  4693         -  assert( cursorOwnsBtShared(pCur) );
         4691  +u32 sqlite3BtreePayloadSize(BtCursor *pCur){
         4692  +  assert( cursorHoldsMutex(pCur) );
  4694   4693     assert( pCur->eState==CURSOR_VALID );
  4695         -  assert( pCur->iPage>=0 );
  4696         -  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
  4697         -  assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
  4698   4694     getCellInfo(pCur);
  4699         -  *pSize = pCur->info.nPayload;
  4700         -  return SQLITE_OK;
         4695  +  return pCur->info.nPayload;
  4701   4696   }
  4702   4697   
  4703   4698   /*
  4704   4699   ** Given the page number of an overflow page in the database (parameter
  4705   4700   ** ovfl), this function finds the page number of the next page in the 
  4706   4701   ** linked list of overflow pages. If possible, it uses the auto-vacuum
  4707   4702   ** pointer-map data instead of reading the content of page ovfl to do so. 
................................................................................
  5135   5130   ** including calls from other threads against the same cache.
  5136   5131   ** Hence, a mutex on the BtShared should be held prior to calling
  5137   5132   ** this routine.
  5138   5133   **
  5139   5134   ** These routines is used to get quick access to key and data
  5140   5135   ** in the common case where no overflow pages are used.
  5141   5136   */
  5142         -const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
  5143         -  return fetchPayload(pCur, pAmt);
  5144         -}
  5145         -const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
         5137  +const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
  5146   5138     return fetchPayload(pCur, pAmt);
  5147   5139   }
  5148   5140   
  5149   5141   
  5150   5142   /*
  5151   5143   ** Move the cursor down to a new child page.  The newPgno argument is the
  5152   5144   ** page number of the child page to move to.
................................................................................
  5471   5463     int rc;
  5472   5464     RecordCompare xRecordCompare;
  5473   5465   
  5474   5466     assert( cursorOwnsBtShared(pCur) );
  5475   5467     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  5476   5468     assert( pRes );
  5477   5469     assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
         5470  +  assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
  5478   5471   
  5479   5472     /* If the cursor is already positioned at the point we are trying
  5480   5473     ** to move to, then just return without doing any work */
  5481         -  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  5482         -   && pCur->curIntKey 
         5474  +  if( pIdxKey==0
         5475  +   && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  5483   5476     ){
  5484   5477       if( pCur->info.nKey==intKey ){
  5485   5478         *pRes = 0;
  5486   5479         return SQLITE_OK;
  5487   5480       }
  5488   5481       if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
  5489   5482         *pRes = -1;
................................................................................
  6474   6467   ** area.  pCell might point to some temporary storage.  The cell will
  6475   6468   ** be constructed in this temporary area then copied into pPage->aData
  6476   6469   ** later.
  6477   6470   */
  6478   6471   static int fillInCell(
  6479   6472     MemPage *pPage,                /* The page that contains the cell */
  6480   6473     unsigned char *pCell,          /* Complete text of the cell */
  6481         -  const void *pKey, i64 nKey,    /* The key */
  6482         -  const void *pData,int nData,   /* The data */
  6483         -  int nZero,                     /* Extra zero bytes to append to pData */
         6474  +  const BtreePayload *pX,        /* Payload with which to construct the cell */
  6484   6475     int *pnSize                    /* Write cell size here */
  6485   6476   ){
  6486   6477     int nPayload;
  6487   6478     const u8 *pSrc;
  6488   6479     int nSrc, n, rc;
  6489   6480     int spaceLeft;
  6490   6481     MemPage *pOvfl = 0;
................................................................................
  6500   6491     /* pPage is not necessarily writeable since pCell might be auxiliary
  6501   6492     ** buffer space that is separate from the pPage buffer area */
  6502   6493     assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
  6503   6494               || sqlite3PagerIswriteable(pPage->pDbPage) );
  6504   6495   
  6505   6496     /* Fill in the header. */
  6506   6497     nHeader = pPage->childPtrSize;
  6507         -  nPayload = nData + nZero;
  6508         -  if( pPage->intKeyLeaf ){
         6498  +  if( pPage->intKey ){
         6499  +    nPayload = pX->nData + pX->nZero;
         6500  +    pSrc = pX->pData;
         6501  +    nSrc = pX->nData;
         6502  +    assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
         6503  +    nHeader += putVarint32(&pCell[nHeader], nPayload);
         6504  +    nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
         6505  +  }else{
         6506  +    assert( pX->nData==0 );
         6507  +    assert( pX->nZero==0 );
         6508  +    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
         6509  +    nSrc = nPayload = (int)pX->nKey;
         6510  +    pSrc = pX->pKey;
  6509   6511       nHeader += putVarint32(&pCell[nHeader], nPayload);
  6510         -  }else{
  6511         -    assert( nData==0 );
  6512         -    assert( nZero==0 );
  6513   6512     }
  6514         -  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
  6515   6513     
  6516         -  /* Fill in the payload size */
  6517         -  if( pPage->intKey ){
  6518         -    pSrc = pData;
  6519         -    nSrc = nData;
  6520         -    nData = 0;
  6521         -  }else{ 
  6522         -    assert( nKey<=0x7fffffff && pKey!=0 );
  6523         -    nPayload = (int)nKey;
  6524         -    pSrc = pKey;
  6525         -    nSrc = (int)nKey;
  6526         -  }
         6514  +  /* Fill in the payload */
  6527   6515     if( nPayload<=pPage->maxLocal ){
  6528   6516       n = nHeader + nPayload;
  6529   6517       testcase( n==3 );
  6530   6518       testcase( n==4 );
  6531   6519       if( n<4 ) n = 4;
  6532   6520       *pnSize = n;
  6533   6521       spaceLeft = nPayload;
................................................................................
  6557   6545     ** were computed correctly.
  6558   6546     */
  6559   6547   #if SQLITE_DEBUG
  6560   6548     {
  6561   6549       CellInfo info;
  6562   6550       pPage->xParseCell(pPage, pCell, &info);
  6563   6551       assert( nHeader==(int)(info.pPayload - pCell) );
  6564         -    assert( info.nKey==nKey );
         6552  +    assert( info.nKey==pX->nKey );
  6565   6553       assert( *pnSize == info.nSize );
  6566   6554       assert( spaceLeft == info.nLocal );
  6567   6555     }
  6568   6556   #endif
  6569   6557   
  6570   6558     /* Write the payload into the local Cell and any extra into overflow pages */
  6571   6559     while( nPayload>0 ){
................................................................................
  6641   6629         memset(pPayload, 0, n);
  6642   6630       }
  6643   6631       nPayload -= n;
  6644   6632       pPayload += n;
  6645   6633       pSrc += n;
  6646   6634       nSrc -= n;
  6647   6635       spaceLeft -= n;
  6648         -    if( nSrc==0 ){
  6649         -      nSrc = nData;
  6650         -      pSrc = pData;
  6651         -    }
  6652   6636     }
  6653   6637     releasePage(pToRelease);
  6654   6638     return SQLITE_OK;
  6655   6639   }
  6656   6640   
  6657   6641   /*
  6658   6642   ** Remove the i-th cell from pPage.  This routine effects pPage only.
................................................................................
  6711   6695   ** If the cell content will fit on the page, then put it there.  If it
  6712   6696   ** will not fit, then make a copy of the cell content into pTemp if
  6713   6697   ** pTemp is not null.  Regardless of pTemp, allocate a new entry
  6714   6698   ** in pPage->apOvfl[] and make it point to the cell content (either
  6715   6699   ** in pTemp or the original pCell) and also record its index. 
  6716   6700   ** Allocating a new entry in pPage->aCell[] implies that 
  6717   6701   ** pPage->nOverflow is incremented.
         6702  +**
         6703  +** *pRC must be SQLITE_OK when this routine is called.
  6718   6704   */
  6719   6705   static void insertCell(
  6720   6706     MemPage *pPage,   /* Page into which we are copying */
  6721   6707     int i,            /* New cell becomes the i-th cell of the page */
  6722   6708     u8 *pCell,        /* Content of the new cell */
  6723   6709     int sz,           /* Bytes of content in pCell */
  6724   6710     u8 *pTemp,        /* Temp storage space for pCell, if needed */
................................................................................
  6726   6712     int *pRC          /* Read and write return code from here */
  6727   6713   ){
  6728   6714     int idx = 0;      /* Where to write new cell content in data[] */
  6729   6715     int j;            /* Loop counter */
  6730   6716     u8 *data;         /* The content of the whole page */
  6731   6717     u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
  6732   6718   
  6733         -  if( *pRC ) return;
  6734         -
         6719  +  assert( *pRC==SQLITE_OK );
  6735   6720     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  6736   6721     assert( MX_CELL(pPage->pBt)<=10921 );
  6737   6722     assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  6738   6723     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  6739   6724     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  6740   6725     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  6741   6726     /* The cell should normally be sized correctly.  However, when moving a
................................................................................
  6800   6785         ptrmapPutOvflPtr(pPage, pCell, pRC);
  6801   6786       }
  6802   6787     }
  6803   6788   }
  6804   6789   
  6805   6790   /*
  6806   6791   ** A CellArray object contains a cache of pointers and sizes for a
  6807         -** consecutive sequence of cells that might be held multiple pages.
         6792  +** consecutive sequence of cells that might be held on multiple pages.
  6808   6793   */
  6809   6794   typedef struct CellArray CellArray;
  6810   6795   struct CellArray {
  6811   6796     int nCell;              /* Number of cells in apCell[] */
  6812   6797     MemPage *pRef;          /* Reference page */
  6813   6798     u8 **apCell;            /* All cells begin balanced */
  6814   6799     u16 *szCell;            /* Local size of all cells in apCell[] */
................................................................................
  7232   7217       pCell = findCell(pPage, pPage->nCell-1);
  7233   7218       pStop = &pCell[9];
  7234   7219       while( (*(pCell++)&0x80) && pCell<pStop );
  7235   7220       pStop = &pCell[9];
  7236   7221       while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
  7237   7222   
  7238   7223       /* Insert the new divider cell into pParent. */
  7239         -    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
  7240         -               0, pPage->pgno, &rc);
         7224  +    if( rc==SQLITE_OK ){
         7225  +      insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
         7226  +                   0, pPage->pgno, &rc);
         7227  +    }
  7241   7228   
  7242   7229       /* Set the right-child pointer of pParent to point to the new page. */
  7243   7230       put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
  7244   7231     
  7245   7232       /* Release the reference to the new page. */
  7246   7233       releasePage(pNew);
  7247   7234     }
................................................................................
  7753   7740       d = r + 1 - leafData;
  7754   7741       (void)cachedCellSize(&b, d);
  7755   7742       do{
  7756   7743         assert( d<nMaxCells );
  7757   7744         assert( r<nMaxCells );
  7758   7745         (void)cachedCellSize(&b, r);
  7759   7746         if( szRight!=0
  7760         -       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+2)) ){
         7747  +       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
  7761   7748           break;
  7762   7749         }
  7763   7750         szRight += b.szCell[d] + 2;
  7764   7751         szLeft -= b.szCell[r] + 2;
  7765   7752         cntNew[i-1] = r;
  7766   7753         r--;
  7767   7754         d--;
................................................................................
  8325   8312       sqlite3PageFree(pFree);
  8326   8313     }
  8327   8314     return rc;
  8328   8315   }
  8329   8316   
  8330   8317   
  8331   8318   /*
  8332         -** Insert a new record into the BTree.  The key is given by (pKey,nKey)
  8333         -** and the data is given by (pData,nData).  The cursor is used only to
  8334         -** define what table the record should be inserted into.  The cursor
  8335         -** is left pointing at a random location.
         8319  +** Insert a new record into the BTree.  The content of the new record
         8320  +** is described by the pX object.  The pCur cursor is used only to
         8321  +** define what table the record should be inserted into, and is left
         8322  +** pointing at a random location.
  8336   8323   **
  8337         -** For an INTKEY table, only the nKey value of the key is used.  pKey is
  8338         -** ignored.  For a ZERODATA table, the pData and nData are both ignored.
         8324  +** For a table btree (used for rowid tables), only the pX.nKey value of
         8325  +** the key is used. The pX.pKey value must be NULL.  The pX.nKey is the
         8326  +** rowid or INTEGER PRIMARY KEY of the row.  The pX.nData,pData,nZero fields
         8327  +** hold the content of the row.
         8328  +**
         8329  +** For an index btree (used for indexes and WITHOUT ROWID tables), the
         8330  +** key is an arbitrary byte sequence stored in pX.pKey,nKey.  The 
         8331  +** pX.pData,nData,nZero fields must be zero.
  8339   8332   **
  8340   8333   ** If the seekResult parameter is non-zero, then a successful call to
  8341   8334   ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
  8342   8335   ** been performed. seekResult is the search result returned (a negative
  8343   8336   ** number if pCur points at an entry that is smaller than (pKey, nKey), or
  8344   8337   ** a positive value if pCur points at an entry that is larger than 
  8345   8338   ** (pKey, nKey)). 
................................................................................
  8348   8341   ** cursor pCur is pointing at the existing copy of a row that is to be
  8349   8342   ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
  8350   8343   ** point to any entry or to no entry at all and so this function has to seek
  8351   8344   ** the cursor before the new key can be inserted.
  8352   8345   */
  8353   8346   int sqlite3BtreeInsert(
  8354   8347     BtCursor *pCur,                /* Insert data into the table of this cursor */
  8355         -  const void *pKey, i64 nKey,    /* The key of the new record */
  8356         -  const void *pData, int nData,  /* The data of the new record */
  8357         -  int nZero,                     /* Number of extra 0 bytes to append to data */
         8348  +  const BtreePayload *pX,        /* Content of the row to be inserted */
  8358   8349     int appendBias,                /* True if this is likely an append */
  8359   8350     int seekResult                 /* Result of prior MovetoUnpacked() call */
  8360   8351   ){
  8361   8352     int rc;
  8362   8353     int loc = seekResult;          /* -1: before desired location  +1: after */
  8363   8354     int szNew = 0;
  8364   8355     int idx;
................................................................................
  8380   8371     assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
  8381   8372   
  8382   8373     /* Assert that the caller has been consistent. If this cursor was opened
  8383   8374     ** expecting an index b-tree, then the caller should be inserting blob
  8384   8375     ** keys with no associated data. If the cursor was opened expecting an
  8385   8376     ** intkey table, the caller should be inserting integer keys with a
  8386   8377     ** blob of associated data.  */
  8387         -  assert( (pKey==0)==(pCur->pKeyInfo==0) );
         8378  +  assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
  8388   8379   
  8389   8380     /* Save the positions of any other cursors open on this table.
  8390   8381     **
  8391   8382     ** In some cases, the call to btreeMoveto() below is a no-op. For
  8392   8383     ** example, when inserting data into a table with auto-generated integer
  8393   8384     ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
  8394   8385     ** integer key to use. It then calls this function to actually insert the 
................................................................................
  8399   8390     */
  8400   8391     if( pCur->curFlags & BTCF_Multiple ){
  8401   8392       rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
  8402   8393       if( rc ) return rc;
  8403   8394     }
  8404   8395   
  8405   8396     if( pCur->pKeyInfo==0 ){
  8406         -    assert( pKey==0 );
         8397  +    assert( pX->pKey==0 );
  8407   8398       /* If this is an insert into a table b-tree, invalidate any incrblob 
  8408   8399       ** cursors open on the row being replaced */
  8409         -    invalidateIncrblobCursors(p, nKey, 0);
         8400  +    invalidateIncrblobCursors(p, pX->nKey, 0);
  8410   8401   
  8411   8402       /* If the cursor is currently on the last row and we are appending a
  8412   8403       ** new row onto the end, set the "loc" to avoid an unnecessary
  8413   8404       ** btreeMoveto() call */
  8414         -    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
  8415         -      && pCur->info.nKey==nKey-1 ){
         8405  +    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
         8406  +      && pCur->info.nKey==pX->nKey-1 ){
  8416   8407          loc = -1;
  8417   8408       }else if( loc==0 ){
  8418         -      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc);
         8409  +      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
  8419   8410         if( rc ) return rc;
  8420   8411       }
  8421   8412     }else if( loc==0 ){
  8422         -    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
         8413  +    rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
  8423   8414       if( rc ) return rc;
  8424   8415     }
  8425   8416     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
  8426   8417   
  8427   8418     pPage = pCur->apPage[pCur->iPage];
  8428         -  assert( pPage->intKey || nKey>=0 );
         8419  +  assert( pPage->intKey || pX->nKey>=0 );
  8429   8420     assert( pPage->leaf || !pPage->intKey );
  8430   8421   
  8431   8422     TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
  8432         -          pCur->pgnoRoot, nKey, nData, pPage->pgno,
         8423  +          pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
  8433   8424             loc==0 ? "overwrite" : "new entry"));
  8434   8425     assert( pPage->isInit );
  8435   8426     newCell = pBt->pTmpSpace;
  8436   8427     assert( newCell!=0 );
  8437         -  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
         8428  +  rc = fillInCell(pPage, newCell, pX, &szNew);
  8438   8429     if( rc ) goto end_insert;
  8439   8430     assert( szNew==pPage->xCellSize(pPage, newCell) );
  8440   8431     assert( szNew <= MX_CELL_SIZE(pBt) );
  8441   8432     idx = pCur->aiIdx[pCur->iPage];
  8442   8433     if( loc==0 ){
  8443   8434       u16 szOld;
  8444   8435       assert( idx<pPage->nCell );
................................................................................
  8456   8447     }else if( loc<0 && pPage->nCell>0 ){
  8457   8448       assert( pPage->leaf );
  8458   8449       idx = ++pCur->aiIdx[pCur->iPage];
  8459   8450     }else{
  8460   8451       assert( pPage->leaf );
  8461   8452     }
  8462   8453     insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
         8454  +  assert( pPage->nOverflow==0 || rc==SQLITE_OK );
  8463   8455     assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
  8464   8456   
  8465   8457     /* If no error has occurred and pPage has an overflow cell, call balance() 
  8466   8458     ** to redistribute the cells within the tree. Since balance() may move
  8467   8459     ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
  8468   8460     ** variables.
  8469   8461     **
................................................................................
  8479   8471     ** is advantageous to leave the cursor pointing to the last entry in
  8480   8472     ** the b-tree if possible. If the cursor is left pointing to the last
  8481   8473     ** entry in the table, and the next row inserted has an integer key
  8482   8474     ** larger than the largest existing key, it is possible to insert the
  8483   8475     ** row without seeking the cursor. This can be a big performance boost.
  8484   8476     */
  8485   8477     pCur->info.nSize = 0;
  8486         -  if( rc==SQLITE_OK && pPage->nOverflow ){
         8478  +  if( pPage->nOverflow ){
         8479  +    assert( rc==SQLITE_OK );
  8487   8480       pCur->curFlags &= ~(BTCF_ValidNKey);
  8488   8481       rc = balance(pCur);
  8489   8482   
  8490   8483       /* Must make sure nOverflow is reset to zero even if the balance()
  8491   8484       ** fails. Internal data structure corruption will result otherwise. 
  8492   8485       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
  8493   8486       ** from trying to save the current position of the cursor.  */
................................................................................
  8615   8608       pCell = findCell(pLeaf, pLeaf->nCell-1);
  8616   8609       if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
  8617   8610       nCell = pLeaf->xCellSize(pLeaf, pCell);
  8618   8611       assert( MX_CELL_SIZE(pBt) >= nCell );
  8619   8612       pTmp = pBt->pTmpSpace;
  8620   8613       assert( pTmp!=0 );
  8621   8614       rc = sqlite3PagerWrite(pLeaf->pDbPage);
  8622         -    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
         8615  +    if( rc==SQLITE_OK ){
         8616  +      insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
         8617  +    }
  8623   8618       dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
  8624   8619       if( rc ) return rc;
  8625   8620     }
  8626   8621   
  8627   8622     /* Balance the tree. If the entry deleted was located on a leaf page,
  8628   8623     ** then the cursor still points to that page. In this case the first
  8629   8624     ** call to balance() repairs the tree, and the if(...) condition is
................................................................................
 10133  10128   #if !defined(SQLITE_OMIT_SHARED_CACHE)
 10134  10129   /*
 10135  10130   ** Return true if the Btree passed as the only argument is sharable.
 10136  10131   */
 10137  10132   int sqlite3BtreeSharable(Btree *p){
 10138  10133     return p->sharable;
 10139  10134   }
        10135  +
        10136  +/*
        10137  +** Return the number of connections to the BtShared object accessed by
        10138  +** the Btree handle passed as the only argument. For private caches 
        10139  +** this is always 1. For shared caches it may be 1 or greater.
        10140  +*/
        10141  +int sqlite3BtreeConnectionCount(Btree *p){
        10142  +  testcase( p->sharable );
        10143  +  return p->pBt->nRef;
        10144  +}
 10140  10145   #endif

Changes to src/btree.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite B-Tree file
    13     13   ** subsystem.  See comments in the source code for a detailed description
    14     14   ** of what each interface routine does.
    15     15   */
    16         -#ifndef _BTREE_H_
    17         -#define _BTREE_H_
           16  +#ifndef SQLITE_BTREE_H
           17  +#define SQLITE_BTREE_H
    18     18   
    19     19   /* TODO: This definition is just included so other modules compile. It
    20     20   ** needs to be revisited.
    21     21   */
    22     22   #define SQLITE_N_BTREE_META 16
    23     23   
    24     24   /*
................................................................................
    35     35   
    36     36   /*
    37     37   ** Forward declarations of structure
    38     38   */
    39     39   typedef struct Btree Btree;
    40     40   typedef struct BtCursor BtCursor;
    41     41   typedef struct BtShared BtShared;
           42  +typedef struct BtreePayload BtreePayload;
    42     43   
    43     44   
    44     45   int sqlite3BtreeOpen(
    45     46     sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
    46     47     const char *zFilename,   /* Name of database file to open */
    47     48     sqlite3 *db,             /* Associated database connection */
    48     49     Btree **ppBtree,         /* Return open Btree* here */
................................................................................
   246    247   int sqlite3BtreeCursorRestore(BtCursor*, int*);
   247    248   int sqlite3BtreeDelete(BtCursor*, u8 flags);
   248    249   
   249    250   /* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
   250    251   #define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
   251    252   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
   252    253   
   253         -int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
   254         -                                  const void *pData, int nData,
   255         -                                  int nZero, int bias, int seekResult);
          254  +/* An instance of the BtreePayload object describes the content of a single
          255  +** entry in either an index or table btree.
          256  +**
          257  +** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
          258  +** an arbitrary key and no data.  These btrees have pKey,nKey set to their
          259  +** key and pData,nData,nZero set to zero.
          260  +**
          261  +** Table btrees (used for rowid tables) contain an integer rowid used as
          262  +** the key and passed in the nKey field.  The pKey field is zero.  
          263  +** pData,nData hold the content of the new entry.  nZero extra zero bytes
          264  +** are appended to the end of the content when constructing the entry.
          265  +**
          266  +** This object is used to pass information into sqlite3BtreeInsert().  The
          267  +** same information used to be passed as five separate parameters.  But placing
          268  +** the information into this object helps to keep the interface more 
          269  +** organized and understandable, and it also helps the resulting code to
          270  +** run a little faster by using fewer registers for parameter passing.
          271  +*/
          272  +struct BtreePayload {
          273  +  const void *pKey;       /* Key content for indexes.  NULL for tables */
          274  +  sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
          275  +  const void *pData;      /* Data for tables.  NULL for indexes */
          276  +  int nData;              /* Size of pData.  0 if none. */
          277  +  int nZero;              /* Extra zero data appended after pData,nData */
          278  +};
          279  +
          280  +int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
          281  +                       int bias, int seekResult);
   256    282   int sqlite3BtreeFirst(BtCursor*, int *pRes);
   257    283   int sqlite3BtreeLast(BtCursor*, int *pRes);
   258    284   int sqlite3BtreeNext(BtCursor*, int *pRes);
   259    285   int sqlite3BtreeEof(BtCursor*);
   260    286   int sqlite3BtreePrevious(BtCursor*, int *pRes);
   261         -int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
          287  +i64 sqlite3BtreeIntegerKey(BtCursor*);
   262    288   int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
   263         -const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
   264         -const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
   265         -int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
          289  +const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
          290  +u32 sqlite3BtreePayloadSize(BtCursor*);
   266    291   int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
   267    292   
   268    293   char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
   269    294   struct Pager *sqlite3BtreePager(Btree*);
   270    295   
   271    296   int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
   272    297   void sqlite3BtreeIncrblobCursor(BtCursor *);
................................................................................
   301    326   ** Enter and Leave procedures no-ops.
   302    327   */
   303    328   #ifndef SQLITE_OMIT_SHARED_CACHE
   304    329     void sqlite3BtreeEnter(Btree*);
   305    330     void sqlite3BtreeEnterAll(sqlite3*);
   306    331     int sqlite3BtreeSharable(Btree*);
   307    332     void sqlite3BtreeEnterCursor(BtCursor*);
          333  +  int sqlite3BtreeConnectionCount(Btree*);
   308    334   #else
   309    335   # define sqlite3BtreeEnter(X) 
   310    336   # define sqlite3BtreeEnterAll(X)
   311    337   # define sqlite3BtreeSharable(X) 0
   312    338   # define sqlite3BtreeEnterCursor(X)
          339  +# define sqlite3BtreeConnectionCount(X) 1
   313    340   #endif
   314    341   
   315    342   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
   316    343     void sqlite3BtreeLeave(Btree*);
   317    344     void sqlite3BtreeLeaveCursor(BtCursor*);
   318    345     void sqlite3BtreeLeaveAll(sqlite3*);
   319    346   #ifndef NDEBUG
................................................................................
   329    356   # define sqlite3BtreeLeaveAll(X)
   330    357   
   331    358   # define sqlite3BtreeHoldsMutex(X) 1
   332    359   # define sqlite3BtreeHoldsAllMutexes(X) 1
   333    360   # define sqlite3SchemaMutexHeld(X,Y,Z) 1
   334    361   #endif
   335    362   
   336         -#endif /* _BTREE_H_ */
          363  +#endif /* SQLITE_BTREE_H */

Changes to src/build.c.

   334    334   **
   335    335   ** The difference between this routine and sqlite3FindTable() is that this
   336    336   ** routine leaves an error message in pParse->zErrMsg where
   337    337   ** sqlite3FindTable() does not.
   338    338   */
   339    339   Table *sqlite3LocateTable(
   340    340     Parse *pParse,         /* context in which to report errors */
   341         -  int isView,            /* True if looking for a VIEW rather than a TABLE */
          341  +  u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
   342    342     const char *zName,     /* Name of the table we are looking for */
   343    343     const char *zDbase     /* Name of the database.  Might be NULL */
   344    344   ){
   345    345     Table *p;
   346    346   
   347    347     /* Read the database schema. If an error occurs, leave an error message
   348    348     ** and code in pParse and return NULL. */
   349    349     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   350    350       return 0;
   351    351     }
   352    352   
   353    353     p = sqlite3FindTable(pParse->db, zName, zDbase);
   354    354     if( p==0 ){
   355         -    const char *zMsg = isView ? "no such view" : "no such table";
          355  +    const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
   356    356   #ifndef SQLITE_OMIT_VIRTUALTABLE
   357    357       if( sqlite3FindDbName(pParse->db, zDbase)<1 ){
   358    358         /* If zName is the not the name of a table in the schema created using
   359    359         ** CREATE, then check to see if it is the name of an virtual table that
   360    360         ** can be an eponymous virtual table. */
   361    361         Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName);
   362    362         if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
   363    363           return pMod->pEpoTab;
   364    364         }
   365    365       }
   366    366   #endif
   367         -    if( zDbase ){
   368         -      sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
   369         -    }else{
   370         -      sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
          367  +    if( (flags & LOCATE_NOERR)==0 ){
          368  +      if( zDbase ){
          369  +        sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
          370  +      }else{
          371  +        sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
          372  +      }
          373  +      pParse->checkSchema = 1;
   371    374       }
   372         -    pParse->checkSchema = 1;
   373    375     }
   374    376   
   375    377     return p;
   376    378   }
   377    379   
   378    380   /*
   379    381   ** Locate the table identified by *p.
................................................................................
   382    384   ** sqlite3LocateTable() and this function is that this function restricts
   383    385   ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
   384    386   ** non-NULL if it is part of a view or trigger program definition. See
   385    387   ** sqlite3FixSrcList() for details.
   386    388   */
   387    389   Table *sqlite3LocateTableItem(
   388    390     Parse *pParse, 
   389         -  int isView, 
          391  +  u32 flags,
   390    392     struct SrcList_item *p
   391    393   ){
   392    394     const char *zDb;
   393    395     assert( p->pSchema==0 || p->zDatabase==0 );
   394    396     if( p->pSchema ){
   395    397       int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
   396    398       zDb = pParse->db->aDb[iDb].zName;
   397    399     }else{
   398    400       zDb = p->zDatabase;
   399    401     }
   400         -  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
          402  +  return sqlite3LocateTable(pParse, flags, p->zName, zDb);
   401    403   }
   402    404   
   403    405   /*
   404    406   ** Locate the in-memory structure that describes 
   405    407   ** a particular index given the name of that index
   406    408   ** and the name of the database that contains the index.
   407    409   ** Return NULL if not found.
................................................................................
   588    590   **
   589    591   ** The db parameter is optional.  It is needed if the Table object 
   590    592   ** contains lookaside memory.  (Table objects in the schema do not use
   591    593   ** lookaside memory, but some ephemeral Table objects do.)  Or the
   592    594   ** db parameter can be used with db->pnBytesFreed to measure the memory
   593    595   ** used by the Table object.
   594    596   */
   595         -void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
          597  +static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
   596    598     Index *pIndex, *pNext;
   597    599     TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
   598    600   
   599         -  assert( !pTable || pTable->nRef>0 );
   600         -
   601         -  /* Do not delete the table until the reference count reaches zero. */
   602         -  if( !pTable ) return;
   603         -  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
   604         -
   605    601     /* Record the number of outstanding lookaside allocations in schema Tables
   606    602     ** prior to doing any free() operations.  Since schema Tables do not use
   607    603     ** lookaside, this number should not change. */
   608    604     TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
   609    605                            db->lookaside.nOut : 0 );
   610    606   
   611    607     /* Delete all indices associated with this table. */
   612    608     for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
   613    609       pNext = pIndex->pNext;
   614         -    assert( pIndex->pSchema==pTable->pSchema );
   615         -    if( !db || db->pnBytesFreed==0 ){
          610  +    assert( pIndex->pSchema==pTable->pSchema
          611  +         || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
          612  +    if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
   616    613         char *zName = pIndex->zName; 
   617    614         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   618    615            &pIndex->pSchema->idxHash, zName, 0
   619    616         );
   620    617         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   621    618         assert( pOld==pIndex || pOld==0 );
   622    619       }
................................................................................
   637    634     sqlite3VtabClear(db, pTable);
   638    635   #endif
   639    636     sqlite3DbFree(db, pTable);
   640    637   
   641    638     /* Verify that no lookaside memory was used by schema tables */
   642    639     assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
   643    640   }
          641  +void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
          642  +  /* Do not delete the table until the reference count reaches zero. */
          643  +  if( !pTable ) return;
          644  +  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
          645  +  deleteTable(db, pTable);
          646  +}
          647  +
   644    648   
   645    649   /*
   646    650   ** Unlink the given table from the hash tables and the delete the
   647    651   ** table structure with all its indices and foreign keys.
   648    652   */
   649    653   void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
   650    654     Table *p;
................................................................................
  1283   1287     int autoInc,      /* True if the AUTOINCREMENT keyword is present */
  1284   1288     int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
  1285   1289   ){
  1286   1290     Table *pTab = pParse->pNewTable;
  1287   1291     Column *pCol = 0;
  1288   1292     int iCol = -1, i;
  1289   1293     int nTerm;
  1290         -  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
         1294  +  if( pTab==0 ) goto primary_key_exit;
  1291   1295     if( pTab->tabFlags & TF_HasPrimaryKey ){
  1292   1296       sqlite3ErrorMsg(pParse, 
  1293   1297         "table \"%s\" has more than one primary key", pTab->zName);
  1294   1298       goto primary_key_exit;
  1295   1299     }
  1296   1300     pTab->tabFlags |= TF_HasPrimaryKey;
  1297   1301     if( pList==0 ){
................................................................................
  1329   1333       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
  1330   1334     }else if( autoInc ){
  1331   1335   #ifndef SQLITE_OMIT_AUTOINCREMENT
  1332   1336       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
  1333   1337          "INTEGER PRIMARY KEY");
  1334   1338   #endif
  1335   1339     }else{
  1336         -    Index *p;
  1337         -    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
  1338         -                           0, sortOrder, 0);
  1339         -    if( p ){
  1340         -      p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
  1341         -    }
         1340  +    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
         1341  +                           0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
  1342   1342       pList = 0;
  1343   1343     }
  1344   1344   
  1345   1345   primary_key_exit:
  1346   1346     sqlite3ExprListDelete(pParse->db, pList);
  1347   1347     return;
  1348   1348   }
................................................................................
  1651   1651   /*
  1652   1652   ** This routine runs at the end of parsing a CREATE TABLE statement that
  1653   1653   ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
  1654   1654   ** internal schema data structures and the generated VDBE code so that they
  1655   1655   ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
  1656   1656   ** Changes include:
  1657   1657   **
  1658         -**     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
         1658  +**     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
         1659  +**     (2)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
  1659   1660   **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
  1660   1661   **          data storage is a covering index btree.
  1661         -**     (2)  Bypass the creation of the sqlite_master table entry
         1662  +**     (3)  Bypass the creation of the sqlite_master table entry
  1662   1663   **          for the PRIMARY KEY as the primary key index is now
  1663   1664   **          identified by the sqlite_master table entry of the table itself.
  1664         -**     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
         1665  +**     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
  1665   1666   **          schema to the rootpage from the main table.
  1666         -**     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
  1667   1667   **     (5)  Add all table columns to the PRIMARY KEY Index object
  1668   1668   **          so that the PRIMARY KEY is a covering index.  The surplus
  1669   1669   **          columns are part of KeyInfo.nXField and are not used for
  1670   1670   **          sorting or lookup or uniqueness checks.
  1671   1671   **     (6)  Replace the rowid tail on all automatically generated UNIQUE
  1672   1672   **          indices with the PRIMARY KEY columns.
         1673  +**
         1674  +** For virtual tables, only (1) is performed.
  1673   1675   */
  1674   1676   static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
  1675   1677     Index *pIdx;
  1676   1678     Index *pPk;
  1677   1679     int nPk;
  1678   1680     int i, j;
  1679   1681     sqlite3 *db = pParse->db;
  1680   1682     Vdbe *v = pParse->pVdbe;
         1683  +
         1684  +  /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
         1685  +  */
         1686  +  if( !db->init.imposterTable ){
         1687  +    for(i=0; i<pTab->nCol; i++){
         1688  +      if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
         1689  +        pTab->aCol[i].notNull = OE_Abort;
         1690  +      }
         1691  +    }
         1692  +  }
         1693  +
         1694  +  /* The remaining transformations only apply to b-tree tables, not to
         1695  +  ** virtual tables */
         1696  +  if( IN_DECLARE_VTAB ) return;
  1681   1697   
  1682   1698     /* Convert the OP_CreateTable opcode that would normally create the
  1683   1699     ** root-page for the table into an OP_CreateIndex opcode.  The index
  1684   1700     ** created will become the PRIMARY KEY index.
  1685   1701     */
  1686   1702     if( pParse->addrCrTab ){
  1687   1703       assert( v );
................................................................................
  1696   1712       Token ipkToken;
  1697   1713       sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
  1698   1714       pList = sqlite3ExprListAppend(pParse, 0, 
  1699   1715                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
  1700   1716       if( pList==0 ) return;
  1701   1717       pList->a[0].sortOrder = pParse->iPkSortOrder;
  1702   1718       assert( pParse->pNewTable==pTab );
  1703         -    pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
  1704         -    if( pPk==0 ) return;
  1705         -    pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
         1719  +    sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
         1720  +                       SQLITE_IDXTYPE_PRIMARYKEY);
         1721  +    if( db->mallocFailed ) return;
         1722  +    pPk = sqlite3PrimaryKeyIndex(pTab);
  1706   1723       pTab->iPKey = -1;
  1707   1724     }else{
  1708   1725       pPk = sqlite3PrimaryKeyIndex(pTab);
  1709   1726   
  1710   1727       /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
  1711   1728       ** table entry. This is only required if currently generating VDBE
  1712   1729       ** code for a CREATE TABLE (not when parsing one as part of reading
................................................................................
  1726   1743           pPk->nColumn--;
  1727   1744         }else{
  1728   1745           pPk->aiColumn[j++] = pPk->aiColumn[i];
  1729   1746         }
  1730   1747       }
  1731   1748       pPk->nKeyCol = j;
  1732   1749     }
  1733         -  pPk->isCovering = 1;
  1734   1750     assert( pPk!=0 );
         1751  +  pPk->isCovering = 1;
         1752  +  if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
  1735   1753     nPk = pPk->nKeyCol;
  1736   1754   
  1737         -  /* Make sure every column of the PRIMARY KEY is NOT NULL.  (Except,
  1738         -  ** do not enforce this for imposter tables.) */
  1739         -  if( !db->init.imposterTable ){
  1740         -    for(i=0; i<nPk; i++){
  1741         -      pTab->aCol[pPk->aiColumn[i]].notNull = OE_Abort;
  1742         -    }
  1743         -    pPk->uniqNotNull = 1;
  1744         -  }
  1745         -
  1746   1755     /* The root page of the PRIMARY KEY is the table root page */
  1747   1756     pPk->tnum = pTab->tnum;
  1748   1757   
  1749   1758     /* Update the in-memory representation of all UNIQUE indices by converting
  1750   1759     ** the final rowid column into one or more columns of the PRIMARY KEY.
  1751   1760     */
  1752   1761     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
................................................................................
  2217   2226         pSelTab->nCol = 0;
  2218   2227         pSelTab->aCol = 0;
  2219   2228         assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
  2220   2229       }else{
  2221   2230         pTable->nCol = 0;
  2222   2231         nErr++;
  2223   2232       }
  2224         -    if( pSelTab ) sqlite3DeleteTable(db, pSelTab);
         2233  +    sqlite3DeleteTable(db, pSelTab);
  2225   2234       sqlite3SelectDelete(db, pSel);
  2226   2235       db->lookaside.bDisable--;
  2227   2236     } else {
  2228   2237       nErr++;
  2229   2238     }
  2230   2239     pTable->pSchema->schemaFlags |= DB_UnresetViews;
  2231   2240   #endif /* SQLITE_OMIT_VIEW */
................................................................................
  2493   2502     if( db->mallocFailed ){
  2494   2503       goto exit_drop_table;
  2495   2504     }
  2496   2505     assert( pParse->nErr==0 );
  2497   2506     assert( pName->nSrc==1 );
  2498   2507     if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
  2499   2508     if( noErr ) db->suppressErr++;
         2509  +  assert( isView==0 || isView==LOCATE_VIEW );
  2500   2510     pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
  2501   2511     if( noErr ) db->suppressErr--;
  2502   2512   
  2503   2513     if( pTab==0 ){
  2504   2514       if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
  2505   2515       goto exit_drop_table;
  2506   2516     }
................................................................................
  2863   2873   ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
  2864   2874   ** as the table to be indexed.  pParse->pNewTable is a table that is
  2865   2875   ** currently being constructed by a CREATE TABLE statement.
  2866   2876   **
  2867   2877   ** pList is a list of columns to be indexed.  pList will be NULL if this
  2868   2878   ** is a primary key or unique-constraint on the most recent column added
  2869   2879   ** to the table currently under construction.  
  2870         -**
  2871         -** If the index is created successfully, return a pointer to the new Index
  2872         -** structure. This is used by sqlite3AddPrimaryKey() to mark the index
  2873         -** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  2874   2880   */
  2875         -Index *sqlite3CreateIndex(
         2881  +void sqlite3CreateIndex(
  2876   2882     Parse *pParse,     /* All information about this parse */
  2877   2883     Token *pName1,     /* First part of index name. May be NULL */
  2878   2884     Token *pName2,     /* Second part of index name. May be NULL */
  2879   2885     SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
  2880   2886     ExprList *pList,   /* A list of columns to be indexed */
  2881   2887     int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  2882   2888     Token *pStart,     /* The CREATE token that begins this statement */
  2883   2889     Expr *pPIWhere,    /* WHERE clause for partial indices */
  2884   2890     int sortOrder,     /* Sort order of primary key when pList==NULL */
  2885         -  int ifNotExist     /* Omit error if index already exists */
         2891  +  int ifNotExist,    /* Omit error if index already exists */
         2892  +  u8 idxType         /* The index type */
  2886   2893   ){
  2887         -  Index *pRet = 0;     /* Pointer to return */
  2888   2894     Table *pTab = 0;     /* Table to be indexed */
  2889   2895     Index *pIndex = 0;   /* The index to be created */
  2890   2896     char *zName = 0;     /* Name of the index */
  2891   2897     int nName;           /* Number of characters in zName */
  2892   2898     int i, j;
  2893   2899     DbFixer sFix;        /* For assigning database names to pTable */
  2894   2900     int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
................................................................................
  2898   2904     Token *pName = 0;    /* Unqualified name of the index to create */
  2899   2905     struct ExprList_item *pListItem; /* For looping over pList */
  2900   2906     int nExtra = 0;                  /* Space allocated for zExtra[] */
  2901   2907     int nExtraCol;                   /* Number of extra columns needed */
  2902   2908     char *zExtra = 0;                /* Extra space after the Index object */
  2903   2909     Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
  2904   2910   
  2905         -  if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
         2911  +  if( db->mallocFailed || pParse->nErr>0 ){
         2912  +    goto exit_create_index;
         2913  +  }
         2914  +  if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
  2906   2915       goto exit_create_index;
  2907   2916     }
  2908   2917     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  2909   2918       goto exit_create_index;
  2910   2919     }
  2911   2920   
  2912   2921     /*
................................................................................
  3024   3033       int n;
  3025   3034       Index *pLoop;
  3026   3035       for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
  3027   3036       zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
  3028   3037       if( zName==0 ){
  3029   3038         goto exit_create_index;
  3030   3039       }
         3040  +
         3041  +    /* Automatic index names generated from within sqlite3_declare_vtab()
         3042  +    ** must have names that are distinct from normal automatic index names.
         3043  +    ** The following statement converts "sqlite3_autoindex..." into
         3044  +    ** "sqlite3_butoindex..." in order to make the names distinct.
         3045  +    ** The "vtab_err.test" test demonstrates the need of this statement. */
         3046  +    if( IN_DECLARE_VTAB ) zName[7]++;
  3031   3047     }
  3032   3048   
  3033   3049     /* Check for authorization to create an index.
  3034   3050     */
  3035   3051   #ifndef SQLITE_OMIT_AUTHORIZATION
  3036   3052     {
  3037   3053       const char *zDb = pDb->zName;
................................................................................
  3087   3103     assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
  3088   3104     pIndex->zName = zExtra;
  3089   3105     zExtra += nName + 1;
  3090   3106     memcpy(pIndex->zName, zName, nName+1);
  3091   3107     pIndex->pTable = pTab;
  3092   3108     pIndex->onError = (u8)onError;
  3093   3109     pIndex->uniqNotNull = onError!=OE_None;
  3094         -  pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
         3110  +  pIndex->idxType = idxType;
  3095   3111     pIndex->pSchema = db->aDb[iDb].pSchema;
  3096   3112     pIndex->nKeyCol = pList->nExpr;
  3097   3113     if( pPIWhere ){
  3098   3114       sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
  3099   3115       pIndex->pPartIdxWhere = pPIWhere;
  3100   3116       pPIWhere = 0;
  3101   3117     }
................................................................................
  3267   3283               sqlite3ErrorMsg(pParse, 
  3268   3284                   "conflicting ON CONFLICT clauses specified", 0);
  3269   3285             }
  3270   3286             if( pIdx->onError==OE_Default ){
  3271   3287               pIdx->onError = pIndex->onError;
  3272   3288             }
  3273   3289           }
  3274         -        pRet = pIdx;
         3290  +        if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
  3275   3291           goto exit_create_index;
  3276   3292         }
  3277   3293       }
  3278   3294     }
  3279   3295   
  3280   3296     /* Link the new Index structure to its table and to the other
  3281   3297     ** in-memory database structures. 
  3282   3298     */
  3283   3299     assert( pParse->nErr==0 );
  3284   3300     if( db->init.busy ){
  3285   3301       Index *p;
         3302  +    assert( !IN_DECLARE_VTAB );
  3286   3303       assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
  3287   3304       p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
  3288   3305                             pIndex->zName, pIndex);
  3289   3306       if( p ){
  3290   3307         assert( p==pIndex );  /* Malloc must have failed */
  3291   3308         sqlite3OomFault(db);
  3292   3309         goto exit_create_index;
................................................................................
  3360   3377       ** to invalidate all pre-compiled statements.
  3361   3378       */
  3362   3379       if( pTblName ){
  3363   3380         sqlite3RefillIndex(pParse, pIndex, iMem);
  3364   3381         sqlite3ChangeCookie(pParse, iDb);
  3365   3382         sqlite3VdbeAddParseSchemaOp(v, iDb,
  3366   3383            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
  3367         -      sqlite3VdbeAddOp1(v, OP_Expire, 0);
         3384  +      sqlite3VdbeAddOp0(v, OP_Expire);
  3368   3385       }
  3369   3386   
  3370   3387       sqlite3VdbeJumpHere(v, pIndex->tnum);
  3371   3388     }
  3372   3389   
  3373   3390     /* When adding an index to the list of indices for a table, make
  3374   3391     ** sure all indices labeled OE_Replace come after all those labeled
................................................................................
  3385   3402         Index *pOther = pTab->pIndex;
  3386   3403         while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
  3387   3404           pOther = pOther->pNext;
  3388   3405         }
  3389   3406         pIndex->pNext = pOther->pNext;
  3390   3407         pOther->pNext = pIndex;
  3391   3408       }
  3392         -    pRet = pIndex;
  3393   3409       pIndex = 0;
  3394   3410     }
  3395   3411   
  3396   3412     /* Clean up before exiting */
  3397   3413   exit_create_index:
  3398   3414     if( pIndex ) freeIndex(db, pIndex);
  3399   3415     sqlite3ExprDelete(db, pPIWhere);
  3400   3416     sqlite3ExprListDelete(db, pList);
  3401   3417     sqlite3SrcListDelete(db, pTblName);
  3402   3418     sqlite3DbFree(db, zName);
  3403         -  return pRet;
  3404   3419   }
  3405   3420   
  3406   3421   /*
  3407   3422   ** Fill the Index.aiRowEst[] array with default information - information
  3408   3423   ** to be used when we have not run the ANALYZE command.
  3409   3424   **
  3410   3425   ** aiRowEst[0] is supposed to contain the number of elements in the index.
................................................................................
  3425   3440     /*                10,  9,  8,  7,  6 */
  3426   3441     LogEst aVal[] = { 33, 32, 30, 28, 26 };
  3427   3442     LogEst *a = pIdx->aiRowLogEst;
  3428   3443     int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
  3429   3444     int i;
  3430   3445   
  3431   3446     /* Set the first entry (number of rows in the index) to the estimated 
  3432         -  ** number of rows in the table. Or 10, if the estimated number of rows 
  3433         -  ** in the table is less than that.  */
         3447  +  ** number of rows in the table, or half the number of rows in the table
         3448  +  ** for a partial index.   But do not let the estimate drop below 10. */
  3434   3449     a[0] = pIdx->pTable->nRowLogEst;
  3435         -  if( a[0]<33 ) a[0] = 33;        assert( 33==sqlite3LogEst(10) );
         3450  +  if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
         3451  +  if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );
  3436   3452   
  3437   3453     /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
  3438   3454     ** 6 and each subsequent value (if any) is 5.  */
  3439   3455     memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
  3440   3456     for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
  3441   3457       a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
  3442   3458     }
................................................................................
  4310   4326     sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
  4311   4327   }
  4312   4328   #endif
  4313   4329   
  4314   4330   /*
  4315   4331   ** Return a KeyInfo structure that is appropriate for the given Index.
  4316   4332   **
  4317         -** The KeyInfo structure for an index is cached in the Index object.
  4318         -** So there might be multiple references to the returned pointer.  The
  4319         -** caller should not try to modify the KeyInfo object.
  4320         -**
  4321   4333   ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
  4322   4334   ** when it has finished using it.
  4323   4335   */
  4324   4336   KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
  4325   4337     int i;
  4326   4338     int nCol = pIdx->nColumn;
  4327   4339     int nKey = pIdx->nKeyCol;

Changes to src/ctime.c.

    41     41   #endif
    42     42   #if SQLITE_CASE_SENSITIVE_LIKE
    43     43     "CASE_SENSITIVE_LIKE",
    44     44   #endif
    45     45   #if SQLITE_CHECK_PAGES
    46     46     "CHECK_PAGES",
    47     47   #endif
           48  +#if defined(__clang__) && defined(__clang_major__)
           49  +  "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
           50  +                    CTIMEOPT_VAL(__clang_minor__) "."
           51  +                    CTIMEOPT_VAL(__clang_patchlevel__),
           52  +#elif defined(_MSC_VER)
           53  +  "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
           54  +#elif defined(__GNUC__) && defined(__VERSION__)
           55  +  "COMPILER=gcc-" __VERSION__,
           56  +#endif
    48     57   #if SQLITE_COVERAGE_TEST
    49     58     "COVERAGE_TEST",
    50     59   #endif
    51     60   #if SQLITE_DEBUG
    52     61     "DEBUG",
    53     62   #endif
    54     63   #if SQLITE_DEFAULT_LOCKING_MODE
................................................................................
    60     69   #if SQLITE_DISABLE_DIRSYNC
    61     70     "DISABLE_DIRSYNC",
    62     71   #endif
    63     72   #if SQLITE_DISABLE_LFS
    64     73     "DISABLE_LFS",
    65     74   #endif
    66     75   #if SQLITE_ENABLE_8_3_NAMES
    67         -  "ENABLE_8_3_NAMES",
           76  +  "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
    68     77   #endif
    69     78   #if SQLITE_ENABLE_API_ARMOR
    70     79     "ENABLE_API_ARMOR",
    71     80   #endif
    72     81   #if SQLITE_ENABLE_ATOMIC_WRITE
    73     82     "ENABLE_ATOMIC_WRITE",
    74     83   #endif

Changes to src/date.c.

  1108   1108   static void currentTimeFunc(
  1109   1109     sqlite3_context *context,
  1110   1110     int argc,
  1111   1111     sqlite3_value **argv
  1112   1112   ){
  1113   1113     time_t t;
  1114   1114     char *zFormat = (char *)sqlite3_user_data(context);
  1115         -  sqlite3 *db;
  1116   1115     sqlite3_int64 iT;
  1117   1116     struct tm *pTm;
  1118   1117     struct tm sNow;
  1119   1118     char zBuf[20];
  1120   1119   
  1121   1120     UNUSED_PARAMETER(argc);
  1122   1121     UNUSED_PARAMETER(argv);

Changes to src/dbstat.c.

    54     54   **   the overflow pages associated with a cell will appear earlier in the
    55     55   **   sort-order than its child page:
    56     56   **
    57     57   **      '/1c2/000/'               // Left-most child of 451st child of root
    58     58   */
    59     59   #define VTAB_SCHEMA                                                         \
    60     60     "CREATE TABLE xx( "                                                       \
    61         -  "  name       STRING,           /* Name of table or index */"             \
    62         -  "  path       INTEGER,          /* Path to page from root */"             \
           61  +  "  name       TEXT,             /* Name of table or index */"             \
           62  +  "  path       TEXT,             /* Path to page from root */"             \
    63     63     "  pageno     INTEGER,          /* Page number */"                        \
    64         -  "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"   \
           64  +  "  pagetype   TEXT,             /* 'internal', 'leaf' or 'overflow' */"   \
    65     65     "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"     \
    66     66     "  payload    INTEGER,          /* Bytes of payload on this page */"      \
    67     67     "  unused     INTEGER,          /* Bytes of unused space on this page */" \
    68     68     "  mx_payload INTEGER,          /* Largest payload size of all cells */"  \
    69     69     "  pgoffset   INTEGER,          /* Offset of page in file */"             \
    70     70     "  pgsize     INTEGER,          /* Size of the page */"                   \
    71     71     "  schema     TEXT HIDDEN       /* Database schema being analyzed */"     \

Changes to src/delete.c.

   235    235     int iKey;              /* Memory cell holding key of row to be deleted */
   236    236     i16 nKey;              /* Number of memory cells in the row key */
   237    237     int iEphCur = 0;       /* Ephemeral table holding all primary key values */
   238    238     int iRowSet = 0;       /* Register for rowset of rows to delete */
   239    239     int addrBypass = 0;    /* Address of jump over the delete logic */
   240    240     int addrLoop = 0;      /* Top of the delete loop */
   241    241     int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
          242  +  int bComplex;          /* True if there are triggers or FKs or
          243  +                         ** subqueries in the WHERE clause */
   242    244    
   243    245   #ifndef SQLITE_OMIT_TRIGGER
   244    246     int isView;                  /* True if attempting to delete from a view */
   245    247     Trigger *pTrigger;           /* List of table triggers, if required */
   246         -  int bComplex;                /* True if there are either triggers or FKs */
   247    248   #endif
   248    249   
   249    250     memset(&sContext, 0, sizeof(sContext));
   250    251     db = pParse->db;
   251    252     if( pParse->nErr || db->mallocFailed ){
   252    253       goto delete_from_cleanup;
   253    254     }
................................................................................
   267    268   #ifndef SQLITE_OMIT_TRIGGER
   268    269     pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
   269    270     isView = pTab->pSelect!=0;
   270    271     bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
   271    272   #else
   272    273   # define pTrigger 0
   273    274   # define isView 0
   274         -# define bComplex 0
   275    275   #endif
   276    276   #ifdef SQLITE_OMIT_VIEW
   277    277   # undef isView
   278    278   # define isView 0
   279    279   #endif
   280    280   
   281    281     /* If pTab is really a view, make sure it has been initialized.
................................................................................
   369    369       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   370    370         assert( pIdx->pSchema==pTab->pSchema );
   371    371         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
   372    372       }
   373    373     }else
   374    374   #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
   375    375     {
   376         -    u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK;
          376  +    u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
          377  +    if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
   377    378       wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
   378    379       if( HasRowid(pTab) ){
   379    380         /* For a rowid table, initialize the RowSet to an empty set */
   380    381         pPk = 0;
   381    382         nPk = 1;
   382    383         iRowSet = ++pParse->nMem;
   383    384         sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);

Changes to src/expr.c.

  1814   1814             sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
  1815   1815             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1816   1816             VdbeComment((v, "%s", pIdx->zName));
  1817   1817             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
  1818   1818             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
  1819   1819   
  1820   1820             if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
         1821  +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
         1822  +            const i64 sOne = 1;
         1823  +            sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 
         1824  +                iTab, 0, 0, (u8*)&sOne, P4_INT64);
         1825  +#endif
  1821   1826               *prRhsHasNull = ++pParse->nMem;
  1822   1827               sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
  1823   1828             }
  1824   1829             sqlite3VdbeJumpHere(v, iAddr);
  1825   1830           }
  1826   1831         }
  1827   1832       }
  1828   1833     }
  1829   1834   
  1830   1835     /* If no preexisting index is available for the IN clause
  1831   1836     ** and IN_INDEX_NOOP is an allowed reply
  1832   1837     ** and the RHS of the IN operator is a list, not a subquery
  1833         -  ** and the RHS is not contant or has two or fewer terms,
         1838  +  ** and the RHS is not constant or has two or fewer terms,
  1834   1839     ** then it is not worth creating an ephemeral table to evaluate
  1835   1840     ** the IN operator so return IN_INDEX_NOOP.
  1836   1841     */
  1837   1842     if( eType==0
  1838   1843      && (inFlags & IN_INDEX_NOOP_OK)
  1839   1844      && !ExprHasProperty(pX, EP_xIsSelect)
  1840   1845      && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
................................................................................
  2218   2223           sqlite3VdbeJumpHere(v, addr1);
  2219   2224         }
  2220   2225       }
  2221   2226     
  2222   2227       if( eType==IN_INDEX_ROWID ){
  2223   2228         /* In this case, the RHS is the ROWID of table b-tree
  2224   2229         */
  2225         -      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
  2226         -      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
         2230  +      sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1);
  2227   2231         VdbeCoverage(v);
  2228   2232       }else{
  2229   2233         /* In this case, the RHS is an index b-tree.
  2230   2234         */
  2231   2235         sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
  2232   2236     
  2233   2237         /* If the set membership test fails, then the result of the 
................................................................................
  2532   2536     int regOut      /* Extract the value into this register */
  2533   2537   ){
  2534   2538     if( iCol<0 || iCol==pTab->iPKey ){
  2535   2539       sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
  2536   2540     }else{
  2537   2541       int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
  2538   2542       int x = iCol;
  2539         -    if( !HasRowid(pTab) ){
         2543  +    if( !HasRowid(pTab) && !IsVirtual(pTab) ){
  2540   2544         x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
  2541   2545       }
  2542   2546       sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
  2543   2547     }
  2544   2548     if( iCol>=0 ){
  2545   2549       sqlite3ColumnDefault(v, pTab, iCol, regOut);
  2546   2550     }
................................................................................
  2935   2939         }else{
  2936   2940           pFarg = pExpr->x.pList;
  2937   2941         }
  2938   2942         nFarg = pFarg ? pFarg->nExpr : 0;
  2939   2943         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2940   2944         zId = pExpr->u.zToken;
  2941   2945         pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
         2946  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
         2947  +      if( pDef==0 && pParse->explain ){
         2948  +        pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
         2949  +      }
         2950  +#endif
  2942   2951         if( pDef==0 || pDef->xFinalize!=0 ){
  2943   2952           sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
  2944   2953           break;
  2945   2954         }
  2946   2955   
  2947   2956         /* Attempt a direct implementation of the built-in COALESCE() and
  2948   2957         ** IFNULL() functions.  This avoids unnecessary evaluation of
................................................................................
  3956   3965      && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
  3957   3966      && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
  3958   3967     ){
  3959   3968       return 1;
  3960   3969     }
  3961   3970     return 0;
  3962   3971   }
         3972  +
         3973  +/*
         3974  +** An instance of the following structure is used by the tree walker
         3975  +** to determine if an expression can be evaluated by reference to the
         3976  +** index only, without having to do a search for the corresponding
         3977  +** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
         3978  +** is the cursor for the table.
         3979  +*/
         3980  +struct IdxCover {
         3981  +  Index *pIdx;     /* The index to be tested for coverage */
         3982  +  int iCur;        /* Cursor number for the table corresponding to the index */
         3983  +};
         3984  +
         3985  +/*
         3986  +** Check to see if there are references to columns in table 
         3987  +** pWalker->u.pIdxCover->iCur can be satisfied using the index
         3988  +** pWalker->u.pIdxCover->pIdx.
         3989  +*/
         3990  +static int exprIdxCover(Walker *pWalker, Expr *pExpr){
         3991  +  if( pExpr->op==TK_COLUMN
         3992  +   && pExpr->iTable==pWalker->u.pIdxCover->iCur
         3993  +   && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
         3994  +  ){
         3995  +    pWalker->eCode = 1;
         3996  +    return WRC_Abort;
         3997  +  }
         3998  +  return WRC_Continue;
         3999  +}
         4000  +
         4001  +/*
         4002  +** Determine if an index pIdx on table with cursor iCur contains will
         4003  +** the expression pExpr.  Return true if the index does cover the
         4004  +** expression and false if the pExpr expression references table columns
         4005  +** that are not found in the index pIdx.
         4006  +**
         4007  +** An index covering an expression means that the expression can be
         4008  +** evaluated using only the index and without having to lookup the
         4009  +** corresponding table entry.
         4010  +*/
         4011  +int sqlite3ExprCoveredByIndex(
         4012  +  Expr *pExpr,        /* The index to be tested */
         4013  +  int iCur,           /* The cursor number for the corresponding table */
         4014  +  Index *pIdx         /* The index that might be used for coverage */
         4015  +){
         4016  +  Walker w;
         4017  +  struct IdxCover xcov;
         4018  +  memset(&w, 0, sizeof(w));
         4019  +  xcov.iCur = iCur;
         4020  +  xcov.pIdx = pIdx;
         4021  +  w.xExprCallback = exprIdxCover;
         4022  +  w.u.pIdxCover = &xcov;
         4023  +  sqlite3WalkExpr(&w, pExpr);
         4024  +  return !w.eCode;
         4025  +}
         4026  +
  3963   4027   
  3964   4028   /*
  3965   4029   ** An instance of the following structure is used by the tree walker
  3966   4030   ** to count references to table columns in the arguments of an 
  3967   4031   ** aggregate function, in order to implement the
  3968   4032   ** sqlite3FunctionThisSrc() routine.
  3969   4033   */

Changes to src/fkey.c.

  1368   1368   ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
  1369   1369   ** hash table.
  1370   1370   */
  1371   1371   void sqlite3FkDelete(sqlite3 *db, Table *pTab){
  1372   1372     FKey *pFKey;                    /* Iterator variable */
  1373   1373     FKey *pNext;                    /* Copy of pFKey->pNextFrom */
  1374   1374   
  1375         -  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
         1375  +  assert( db==0 || IsVirtual(pTab)
         1376  +         || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
  1376   1377     for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
  1377   1378   
  1378   1379       /* Remove the FK from the fkeyHash hash table. */
  1379   1380       if( !db || db->pnBytesFreed==0 ){
  1380   1381         if( pFKey->pPrevTo ){
  1381   1382           pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
  1382   1383         }else{

Changes to src/func.c.

   736    736             return 0;
   737    737           }
   738    738           continue;
   739    739         }
   740    740       }
   741    741       c2 = Utf8Read(zString);
   742    742       if( c==c2 ) continue;
   743         -    if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
          743  +    if( noCase  && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
   744    744         continue;
   745    745       }
   746    746       if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
   747    747       return 0;
   748    748     }
   749    749     return *zString==0;
   750    750   }
................................................................................
  1311   1311       if( zCharSet ){
  1312   1312         sqlite3_free(azChar);
  1313   1313       }
  1314   1314     }
  1315   1315     sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
  1316   1316   }
  1317   1317   
         1318  +
         1319  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
         1320  +/*
         1321  +** The "unknown" function is automatically substituted in place of
         1322  +** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
         1323  +** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
         1324  +** When the "sqlite3" command-line shell is built using this functionality,
         1325  +** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
         1326  +** involving application-defined functions to be examined in a generic
         1327  +** sqlite3 shell.
         1328  +*/
         1329  +static void unknownFunc(
         1330  +  sqlite3_context *context,
         1331  +  int argc,
         1332  +  sqlite3_value **argv
         1333  +){
         1334  +  /* no-op */
         1335  +}
         1336  +#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
         1337  +
  1318   1338   
  1319   1339   /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
  1320   1340   ** is only available if the SQLITE_SOUNDEX compile-time option is used
  1321   1341   ** when SQLite is built.
  1322   1342   */
  1323   1343   #ifdef SQLITE_SOUNDEX
  1324   1344   /*
................................................................................
  1782   1802       AGGREGATE2(count,            0, 0, 0, countStep,       countFinalize,
  1783   1803                  SQLITE_FUNC_COUNT  ),
  1784   1804       AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
  1785   1805       AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
  1786   1806       AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
  1787   1807     
  1788   1808       LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1789         -  #ifdef SQLITE_CASE_SENSITIVE_LIKE
         1809  +#ifdef SQLITE_CASE_SENSITIVE_LIKE
  1790   1810       LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1791   1811       LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1792         -  #else
         1812  +#else
  1793   1813       LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1794   1814       LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
  1795         -  #endif
         1815  +#endif
         1816  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
         1817  +    FUNCTION(unknown,           -1, 0, 0, unknownFunc      ),
         1818  +#endif
  1796   1819       FUNCTION(coalesce,           1, 0, 0, 0                ),
  1797   1820       FUNCTION(coalesce,           0, 0, 0, 0                ),
  1798   1821       FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
  1799   1822     };
  1800   1823   #ifndef SQLITE_OMIT_ALTERTABLE
  1801   1824     sqlite3AlterFunctions();
  1802   1825   #endif

Changes to src/hash.h.

     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 is the header file for the generic hash-table implementation
    13     13   ** used in SQLite.
    14     14   */
    15         -#ifndef _SQLITE_HASH_H_
    16         -#define _SQLITE_HASH_H_
           15  +#ifndef SQLITE_HASH_H
           16  +#define SQLITE_HASH_H
    17     17   
    18     18   /* Forward declarations of structures. */
    19     19   typedef struct Hash Hash;
    20     20   typedef struct HashElem HashElem;
    21     21   
    22     22   /* A complete hash table is an instance of the following structure.
    23     23   ** The internals of this structure are intended to be opaque -- client
................................................................................
    89     89   /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
    90     90   
    91     91   /*
    92     92   ** Number of entries in a hash table
    93     93   */
    94     94   /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
    95     95   
    96         -#endif /* _SQLITE_HASH_H_ */
           96  +#endif /* SQLITE_HASH_H */

Changes to src/hwtime.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains inline asm code for retrieving "high-performance"
    14     14   ** counters for x86 class CPUs.
    15     15   */
    16         -#ifndef _HWTIME_H_
    17         -#define _HWTIME_H_
           16  +#ifndef SQLITE_HWTIME_H
           17  +#define SQLITE_HWTIME_H
    18     18   
    19     19   /*
    20     20   ** The following routine only works on pentium-class (or newer) processors.
    21     21   ** It uses the RDTSC opcode to read the cycle count value out of the
    22     22   ** processor and returns that value.  This can be used for high-res
    23     23   ** profiling.
    24     24   */
................................................................................
    78     78     ** of the debugging and testing utilities, but it should at
    79     79     ** least compile and run.
    80     80     */
    81     81     sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
    82     82   
    83     83   #endif
    84     84   
    85         -#endif /* !defined(_HWTIME_H_) */
           85  +#endif /* !defined(SQLITE_HWTIME_H) */

Changes to src/loadext.c.

    17     17     #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
    18     18   #endif
    19     19   #include "sqlite3ext.h"
    20     20   #include "sqliteInt.h"
    21     21   #include <string.h>
    22     22   
    23     23   #ifndef SQLITE_OMIT_LOAD_EXTENSION
    24         -
    25     24   /*
    26     25   ** Some API routines are omitted when various features are
    27     26   ** excluded from a build of SQLite.  Substitute a NULL pointer
    28     27   ** for any missing APIs.
    29     28   */
    30     29   #ifndef SQLITE_ENABLE_COLUMN_METADATA
    31     30   # define sqlite3_column_database_name   0
................................................................................
    87     86   # define sqlite3_vtab_on_conflict 0
    88     87   #endif
    89     88   
    90     89   #ifdef SQLITE_OMIT_SHARED_CACHE
    91     90   # define sqlite3_enable_shared_cache 0
    92     91   #endif
    93     92   
    94         -#ifdef SQLITE_OMIT_TRACE
           93  +#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
    95     94   # define sqlite3_profile       0
    96     95   # define sqlite3_trace         0
    97     96   #endif
    98     97   
    99     98   #ifdef SQLITE_OMIT_GET_TABLE
   100     99   # define sqlite3_free_table    0
   101    100   # define sqlite3_get_table     0
................................................................................
   106    105   #define sqlite3_blob_bytes     0
   107    106   #define sqlite3_blob_close     0
   108    107   #define sqlite3_blob_open      0
   109    108   #define sqlite3_blob_read      0
   110    109   #define sqlite3_blob_write     0
   111    110   #define sqlite3_blob_reopen    0
   112    111   #endif
          112  +
          113  +#if defined(SQLITE_OMIT_TRACE)
          114  +# define sqlite3_trace_v2      0
          115  +#endif
   113    116   
   114    117   /*
   115    118   ** The following structure contains pointers to all SQLite API routines.
   116    119   ** A pointer to this structure is passed into extensions when they are
   117    120   ** loaded so that the extension can make calls back into the SQLite
   118    121   ** library.
   119    122   **
................................................................................
   412    415     sqlite3_value_subtype,
   413    416     sqlite3_result_subtype,
   414    417     /* Version 3.10.0 and later */
   415    418     sqlite3_status64,
   416    419     sqlite3_strlike,
   417    420     sqlite3_db_cacheflush,
   418    421     /* Version 3.12.0 and later */
   419         -  sqlite3_system_errno
          422  +  sqlite3_system_errno,
          423  +  /* Version 3.14.0 and later */
          424  +  sqlite3_trace_v2,
          425  +  sqlite3_expanded_sql
   420    426   };
   421    427   
   422    428   /*
   423    429   ** Attempt to load an SQLite extension library contained in the file
   424    430   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   425    431   ** default entry point name (sqlite3_extension_init) is used.  Use
   426    432   ** of the default name is recommended.
................................................................................
   435    441     sqlite3 *db,          /* Load the extension into this database connection */
   436    442     const char *zFile,    /* Name of the shared library containing extension */
   437    443     const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
   438    444     char **pzErrMsg       /* Put error message here if not 0 */
   439    445   ){
   440    446     sqlite3_vfs *pVfs = db->pVfs;
   441    447     void *handle;
   442         -  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
          448  +  sqlite3_loadext_entry xInit;
   443    449     char *zErrmsg = 0;
   444    450     const char *zEntry;
   445    451     char *zAltEntry = 0;
   446    452     void **aHandle;
   447    453     u64 nMsg = 300 + sqlite3Strlen30(zFile);
   448    454     int ii;
          455  +  int rc;
   449    456   
   450    457     /* Shared library endings to try if zFile cannot be loaded as written */
   451    458     static const char *azEndings[] = {
   452    459   #if SQLITE_OS_WIN
   453    460        "dll"   
   454    461   #elif defined(__APPLE__)
   455    462        "dylib"
................................................................................
   493    500           sqlite3_snprintf(nMsg, zErrmsg, 
   494    501               "unable to open shared library [%s]", zFile);
   495    502           sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
   496    503         }
   497    504       }
   498    505       return SQLITE_ERROR;
   499    506     }
   500         -  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   501         -                   sqlite3OsDlSym(pVfs, handle, zEntry);
          507  +  xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
   502    508   
   503    509     /* If no entry point was specified and the default legacy
   504    510     ** entry point name "sqlite3_extension_init" was not found, then
   505    511     ** construct an entry point name "sqlite3_X_init" where the X is
   506    512     ** replaced by the lowercase value of every ASCII alphabetic 
   507    513     ** character in the filename after the last "/" upto the first ".",
   508    514     ** and eliding the first three characters if they are "lib".  
................................................................................
   526    532       for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
   527    533         if( sqlite3Isalpha(c) ){
   528    534           zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
   529    535         }
   530    536       }
   531    537       memcpy(zAltEntry+iEntry, "_init", 6);
   532    538       zEntry = zAltEntry;
   533         -    xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   534         -                     sqlite3OsDlSym(pVfs, handle, zEntry);
          539  +    xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
   535    540     }
   536    541     if( xInit==0 ){
   537    542       if( pzErrMsg ){
   538    543         nMsg += sqlite3Strlen30(zEntry);
   539    544         *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
   540    545         if( zErrmsg ){
   541    546           sqlite3_snprintf(nMsg, zErrmsg,
................................................................................
   544    549         }
   545    550       }
   546    551       sqlite3OsDlClose(pVfs, handle);
   547    552       sqlite3_free(zAltEntry);
   548    553       return SQLITE_ERROR;
   549    554     }
   550    555     sqlite3_free(zAltEntry);
   551         -  if( xInit(db, &zErrmsg, &sqlite3Apis) ){
          556  +  rc = xInit(db, &zErrmsg, &sqlite3Apis);
          557  +  if( rc ){
          558  +    if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
   552    559       if( pzErrMsg ){
   553    560         *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
   554    561       }
   555    562       sqlite3_free(zErrmsg);
   556    563       sqlite3OsDlClose(pVfs, handle);
   557    564       return SQLITE_ERROR;
   558    565     }
................................................................................
   655    662   #endif
   656    663   
   657    664   
   658    665   /*
   659    666   ** Register a statically linked extension that is automatically
   660    667   ** loaded by every new database connection.
   661    668   */
   662         -int sqlite3_auto_extension(void (*xInit)(void)){
          669  +int sqlite3_auto_extension(
          670  +  void (*xInit)(void)
          671  +){
   663    672     int rc = SQLITE_OK;
   664    673   #ifndef SQLITE_OMIT_AUTOINIT
   665    674     rc = sqlite3_initialize();
   666    675     if( rc ){
   667    676       return rc;
   668    677     }else
   669    678   #endif
................................................................................
   700    709   ** set of routines that is invoked for each new database connection, if it
   701    710   ** is currently on the list.  If xInit is not on the list, then this
   702    711   ** routine is a no-op.
   703    712   **
   704    713   ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
   705    714   ** was not on the list.
   706    715   */
   707         -int sqlite3_cancel_auto_extension(void (*xInit)(void)){
          716  +int sqlite3_cancel_auto_extension(
          717  +  void (*xInit)(void)
          718  +){
   708    719   #if SQLITE_THREADSAFE
   709    720     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   710    721   #endif
   711    722     int i;
   712    723     int n = 0;
   713    724     wsdAutoextInit;
   714    725     sqlite3_mutex_enter(mutex);
................................................................................
   749    760   **
   750    761   ** If anything goes wrong, set an error in the database connection.
   751    762   */
   752    763   void sqlite3AutoLoadExtensions(sqlite3 *db){
   753    764     u32 i;
   754    765     int go = 1;
   755    766     int rc;
   756         -  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
          767  +  sqlite3_loadext_entry xInit;
   757    768   
   758    769     wsdAutoextInit;
   759    770     if( wsdAutoext.nExt==0 ){
   760    771       /* Common case: early out without every having to acquire a mutex */
   761    772       return;
   762    773     }
   763    774     for(i=0; go; i++){
................................................................................
   766    777       sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   767    778   #endif
   768    779       sqlite3_mutex_enter(mutex);
   769    780       if( i>=wsdAutoext.nExt ){
   770    781         xInit = 0;
   771    782         go = 0;
   772    783       }else{
   773         -      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   774         -              wsdAutoext.aExt[i];
          784  +      xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
   775    785       }
   776    786       sqlite3_mutex_leave(mutex);
   777    787       zErrmsg = 0;
   778    788       if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
   779    789         sqlite3ErrorWithMsg(db, rc,
   780    790               "automatic extension loading failed: %s", zErrmsg);
   781    791         go = 0;
   782    792       }
   783    793       sqlite3_free(zErrmsg);
   784    794     }
   785    795   }

Changes to src/main.c.

  1029   1029       ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
  1030   1030       return SQLITE_OK;
  1031   1031     }
  1032   1032     if( !sqlite3SafetyCheckSickOrOk(db) ){
  1033   1033       return SQLITE_MISUSE_BKPT;
  1034   1034     }
  1035   1035     sqlite3_mutex_enter(db->mutex);
         1036  +  if( db->mTrace & SQLITE_TRACE_CLOSE ){
         1037  +    db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
         1038  +  }
  1036   1039   
  1037   1040     /* Force xDisconnect calls on all virtual tables */
  1038   1041     disconnectAllVtab(db);
  1039   1042   
  1040   1043     /* If a transaction is open, the disconnectAllVtab() call above
  1041   1044     ** will not have called the xDisconnect() method on any virtual
  1042   1045     ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
................................................................................
  1797   1800   ** Register a trace function.  The pArg from the previously registered trace
  1798   1801   ** is returned.  
  1799   1802   **
  1800   1803   ** A NULL trace function means that no tracing is executes.  A non-NULL
  1801   1804   ** trace is a pointer to a function that is invoked at the start of each
  1802   1805   ** SQL statement.
  1803   1806   */
  1804         -void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
         1807  +#ifndef SQLITE_OMIT_DEPRECATED
         1808  +void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
  1805   1809     void *pOld;
  1806   1810   
  1807   1811   #ifdef SQLITE_ENABLE_API_ARMOR
  1808   1812     if( !sqlite3SafetyCheckOk(db) ){
  1809   1813       (void)SQLITE_MISUSE_BKPT;
  1810   1814       return 0;
  1811   1815     }
  1812   1816   #endif
  1813   1817     sqlite3_mutex_enter(db->mutex);
  1814   1818     pOld = db->pTraceArg;
  1815         -  db->xTrace = xTrace;
         1819  +  db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
         1820  +  db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
  1816   1821     db->pTraceArg = pArg;
  1817   1822     sqlite3_mutex_leave(db->mutex);
  1818   1823     return pOld;
  1819   1824   }
         1825  +#endif /* SQLITE_OMIT_DEPRECATED */
         1826  +
         1827  +/* Register a trace callback using the version-2 interface.
         1828  +*/
         1829  +int sqlite3_trace_v2(
         1830  +  sqlite3 *db,                               /* Trace this connection */
         1831  +  unsigned mTrace,                           /* Mask of events to be traced */
         1832  +  int(*xTrace)(unsigned,void*,void*,void*),  /* Callback to invoke */
         1833  +  void *pArg                                 /* Context */
         1834  +){
         1835  +#ifdef SQLITE_ENABLE_API_ARMOR
         1836  +  if( !sqlite3SafetyCheckOk(db) ){
         1837  +    return SQLITE_MISUSE_BKPT;
         1838  +  }
         1839  +#endif
         1840  +  sqlite3_mutex_enter(db->mutex);
         1841  +  db->mTrace = mTrace;
         1842  +  db->xTrace = xTrace;
         1843  +  db->pTraceArg = pArg;
         1844  +  sqlite3_mutex_leave(db->mutex);
         1845  +  return SQLITE_OK;
         1846  +}
         1847  +
         1848  +#ifndef SQLITE_OMIT_DEPRECATED
  1820   1849   /*
  1821   1850   ** Register a profile function.  The pArg from the previously registered 
  1822   1851   ** profile function is returned.  
  1823   1852   **
  1824   1853   ** A NULL profile function means that no profiling is executes.  A non-NULL
  1825   1854   ** profile is a pointer to a function that is invoked at the conclusion of
  1826   1855   ** each SQL statement that is run.
................................................................................
  1841   1870     sqlite3_mutex_enter(db->mutex);
  1842   1871     pOld = db->pProfileArg;
  1843   1872     db->xProfile = xProfile;
  1844   1873     db->pProfileArg = pArg;
  1845   1874     sqlite3_mutex_leave(db->mutex);
  1846   1875     return pOld;
  1847   1876   }
         1877  +#endif /* SQLITE_OMIT_DEPRECATED */
  1848   1878   #endif /* SQLITE_OMIT_TRACE */
  1849   1879   
  1850   1880   /*
  1851   1881   ** Register a function to be invoked when a transaction commits.
  1852   1882   ** If the invoked function returns non-zero, then the commit becomes a
  1853   1883   ** rollback.
  1854   1884   */

Changes to src/msvc.h.

     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   **
    13     13   ** This file contains code that is specific to MSVC.
    14     14   */
    15         -#ifndef _MSVC_H_
    16         -#define _MSVC_H_
           15  +#ifndef SQLITE_MSVC_H
           16  +#define SQLITE_MSVC_H
    17     17   
    18     18   #if defined(_MSC_VER)
    19     19   #pragma warning(disable : 4054)
    20     20   #pragma warning(disable : 4055)
    21     21   #pragma warning(disable : 4100)
    22     22   #pragma warning(disable : 4127)
    23     23   #pragma warning(disable : 4130)
................................................................................
    29     29   #pragma warning(disable : 4244)
    30     30   #pragma warning(disable : 4305)
    31     31   #pragma warning(disable : 4306)
    32     32   #pragma warning(disable : 4702)
    33     33   #pragma warning(disable : 4706)
    34     34   #endif /* defined(_MSC_VER) */
    35     35   
    36         -#endif /* _MSVC_H_ */
           36  +#endif /* SQLITE_MSVC_H */

Changes to src/os.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains OS interface code that is common to all
    14     14   ** architectures.
    15     15   */
    16         -#define _SQLITE_OS_C_ 1
    17     16   #include "sqliteInt.h"
    18         -#undef _SQLITE_OS_C_
    19     17   
    20     18   /*
    21     19   ** If we compile with the SQLITE_TEST macro set, then the following block
    22     20   ** of code will give us the ability to simulate a disk I/O error.  This
    23     21   ** is used for testing the I/O recovery logic.
    24     22   */
    25     23   #if defined(SQLITE_TEST)

Changes to src/os_setup.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13     13   ** This file contains pre-processor directives related to operating system
    14     14   ** detection and/or setup.
    15     15   */
    16         -#ifndef _OS_SETUP_H_
    17         -#define _OS_SETUP_H_
           16  +#ifndef SQLITE_OS_SETUP_H
           17  +#define SQLITE_OS_SETUP_H
    18     18   
    19     19   /*
    20     20   ** Figure out if we are dealing with Unix, Windows, or some other operating
    21     21   ** system.
    22     22   **
    23     23   ** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
    24     24   ** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0.  One of
................................................................................
    50     50   #  endif
    51     51   #else
    52     52   #  ifndef SQLITE_OS_WIN
    53     53   #    define SQLITE_OS_WIN 0
    54     54   #  endif
    55     55   #endif
    56     56   
    57         -#endif /* _OS_SETUP_H_ */
           57  +#endif /* SQLITE_OS_SETUP_H */

Changes to src/os_unix.c.

  5408   5408        0,
  5409   5409        0,
  5410   5410        "/var/tmp",
  5411   5411        "/usr/tmp",
  5412   5412        "/tmp",
  5413   5413        "."
  5414   5414     };
  5415         -  unsigned int i;
         5415  +  unsigned int i = 0;
  5416   5416     struct stat buf;
  5417   5417     const char *zDir = sqlite3_temp_directory;
  5418   5418   
  5419   5419     if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
  5420   5420     if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
  5421         -  for(i=0; i<=sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
  5422         -    if( zDir==0 ) continue;
  5423         -    if( osStat(zDir, &buf) ) continue;
  5424         -    if( !S_ISDIR(buf.st_mode) ) continue;
  5425         -    if( osAccess(zDir, 03) ) continue;
  5426         -    return zDir;
         5421  +  while(1){
         5422  +    if( zDir!=0
         5423  +     && osStat(zDir, &buf)==0
         5424  +     && S_ISDIR(buf.st_mode)
         5425  +     && osAccess(zDir, 03)==0
         5426  +    ){
         5427  +      return zDir;
         5428  +    }
         5429  +    if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
         5430  +    zDir = azDirs[i++];
  5427   5431     }
  5428   5432     return 0;
  5429   5433   }
  5430   5434   
  5431   5435   /*
  5432   5436   ** Create a temporary file name in zBuf.  zBuf must be allocated
  5433   5437   ** by the calling process and must be big enough to hold at least
................................................................................
  5520   5524         }
  5521   5525       }
  5522   5526       unixLeaveMutex();
  5523   5527     }
  5524   5528   #endif    /* if !OS_VXWORKS */
  5525   5529     return pUnused;
  5526   5530   }
         5531  +
         5532  +/*
         5533  +** Find the mode, uid and gid of file zFile. 
         5534  +*/
         5535  +static int getFileMode(
         5536  +  const char *zFile,              /* File name */
         5537  +  mode_t *pMode,                  /* OUT: Permissions of zFile */
         5538  +  uid_t *pUid,                    /* OUT: uid of zFile. */
         5539  +  gid_t *pGid                     /* OUT: gid of zFile. */
         5540  +){
         5541  +  struct stat sStat;              /* Output of stat() on database file */
         5542  +  int rc = SQLITE_OK;
         5543  +  if( 0==osStat(zFile, &sStat) ){
         5544  +    *pMode = sStat.st_mode & 0777;
         5545  +    *pUid = sStat.st_uid;
         5546  +    *pGid = sStat.st_gid;
         5547  +  }else{
         5548  +    rc = SQLITE_IOERR_FSTAT;
         5549  +  }
         5550  +  return rc;
         5551  +}
  5527   5552   
  5528   5553   /*
  5529   5554   ** This function is called by unixOpen() to determine the unix permissions
  5530   5555   ** to create new files with. If no error occurs, then SQLITE_OK is returned
  5531   5556   ** and a value suitable for passing as the third argument to open(2) is
  5532   5557   ** written to *pMode. If an IO error occurs, an SQLite error code is 
  5533   5558   ** returned and the value of *pMode is not modified.
................................................................................
  5556   5581     int rc = SQLITE_OK;             /* Return Code */
  5557   5582     *pMode = 0;
  5558   5583     *pUid = 0;
  5559   5584     *pGid = 0;
  5560   5585     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
  5561   5586       char zDb[MAX_PATHNAME+1];     /* Database file path */
  5562   5587       int nDb;                      /* Number of valid bytes in zDb */
  5563         -    struct stat sStat;            /* Output of stat() on database file */
  5564   5588   
  5565   5589       /* zPath is a path to a WAL or journal file. The following block derives
  5566   5590       ** the path to the associated database file from zPath. This block handles
  5567   5591       ** the following naming conventions:
  5568   5592       **
  5569   5593       **   "<path to db>-journal"
  5570   5594       **   "<path to db>-wal"
................................................................................
  5587   5611         if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
  5588   5612   #endif
  5589   5613         nDb--;
  5590   5614       }
  5591   5615       memcpy(zDb, zPath, nDb);
  5592   5616       zDb[nDb] = '\0';
  5593   5617   
  5594         -    if( 0==osStat(zDb, &sStat) ){
  5595         -      *pMode = sStat.st_mode & 0777;
  5596         -      *pUid = sStat.st_uid;
  5597         -      *pGid = sStat.st_gid;
  5598         -    }else{
  5599         -      rc = SQLITE_IOERR_FSTAT;
  5600         -    }
         5618  +    rc = getFileMode(zDb, pMode, pUid, pGid);
  5601   5619     }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
  5602   5620       *pMode = 0600;
         5621  +  }else if( flags & SQLITE_OPEN_URI ){
         5622  +    /* If this is a main database file and the file was opened using a URI
         5623  +    ** filename, check for the "modeof" parameter. If present, interpret
         5624  +    ** its value as a filename and try to copy the mode, uid and gid from
         5625  +    ** that file.  */
         5626  +    const char *z = sqlite3_uri_parameter(zPath, "modeof");
         5627  +    if( z ){
         5628  +      rc = getFileMode(z, pMode, pUid, pGid);
         5629  +    }
  5603   5630     }
  5604   5631     return rc;
  5605   5632   }
  5606   5633   
  5607   5634   /*
  5608   5635   ** Open the file zPath.
  5609   5636   ** 

Changes to src/os_win.c.

   286    286     void *pMapRegion;             /* Area memory mapped */
   287    287     sqlite3_int64 mmapSize;       /* Usable size of mapped region */
   288    288     sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
   289    289     sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
   290    290   #endif
   291    291   };
   292    292   
          293  +/*
          294  +** The winVfsAppData structure is used for the pAppData member for all of the
          295  +** Win32 VFS variants.
          296  +*/
          297  +typedef struct winVfsAppData winVfsAppData;
          298  +struct winVfsAppData {
          299  +  const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
          300  +  void *pAppData;                    /* The extra pAppData, if any. */
          301  +  BOOL bNoLock;                      /* Non-zero if locking is disabled. */
          302  +};
          303  +
   293    304   /*
   294    305   ** Allowed values for winFile.ctrlFlags
   295    306   */
   296    307   #define WINFILE_RDONLY          0x02   /* Connection is read only */
   297    308   #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
   298    309   #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
   299    310   
................................................................................
  2607   2618   
  2608   2619     do{
  2609   2620       rc = osCloseHandle(pFile->h);
  2610   2621       /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
  2611   2622     }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
  2612   2623   #if SQLITE_OS_WINCE
  2613   2624   #define WINCE_DELETION_ATTEMPTS 3
  2614         -  winceDestroyLock(pFile);
         2625  +  {
         2626  +    winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
         2627  +    if( pAppData==NULL || !pAppData->bNoLock ){
         2628  +      winceDestroyLock(pFile);
         2629  +    }
         2630  +  }
  2615   2631     if( pFile->zDeleteOnClose ){
  2616   2632       int cnt = 0;
  2617   2633       while(
  2618   2634              osDeleteFileW(pFile->zDeleteOnClose)==0
  2619   2635           && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
  2620   2636           && cnt++ < WINCE_DELETION_ATTEMPTS
  2621   2637       ){
................................................................................
  3339   3355     }
  3340   3356     pFile->locktype = (u8)locktype;
  3341   3357     OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
  3342   3358              pFile->h, pFile->locktype, sqlite3ErrName(rc)));
  3343   3359     return rc;
  3344   3360   }
  3345   3361   
         3362  +/******************************************************************************
         3363  +****************************** No-op Locking **********************************
         3364  +**
         3365  +** Of the various locking implementations available, this is by far the
         3366  +** simplest:  locking is ignored.  No attempt is made to lock the database
         3367  +** file for reading or writing.
         3368  +**
         3369  +** This locking mode is appropriate for use on read-only databases
         3370  +** (ex: databases that are burned into CD-ROM, for example.)  It can
         3371  +** also be used if the application employs some external mechanism to
         3372  +** prevent simultaneous access of the same database by two or more
         3373  +** database connections.  But there is a serious risk of database
         3374  +** corruption if this locking mode is used in situations where multiple
         3375  +** database connections are accessing the same database file at the same
         3376  +** time and one or more of those connections are writing.
         3377  +*/
         3378  +
         3379  +static int winNolockLock(sqlite3_file *id, int locktype){
         3380  +  UNUSED_PARAMETER(id);
         3381  +  UNUSED_PARAMETER(locktype);
         3382  +  return SQLITE_OK;
         3383  +}
         3384  +
         3385  +static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
         3386  +  UNUSED_PARAMETER(id);
         3387  +  UNUSED_PARAMETER(pResOut);
         3388  +  return SQLITE_OK;
         3389  +}
         3390  +
         3391  +static int winNolockUnlock(sqlite3_file *id, int locktype){
         3392  +  UNUSED_PARAMETER(id);
         3393  +  UNUSED_PARAMETER(locktype);
         3394  +  return SQLITE_OK;
         3395  +}
         3396  +
         3397  +/******************* End of the no-op lock implementation *********************
         3398  +******************************************************************************/
         3399  +
  3346   3400   /*
  3347   3401   ** If *pArg is initially negative then this is a query.  Set *pArg to
  3348   3402   ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
  3349   3403   **
  3350   3404   ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
  3351   3405   */
  3352   3406   static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
................................................................................
  3617   3671   */
  3618   3672   #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
  3619   3673   #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
  3620   3674   
  3621   3675   /*
  3622   3676   ** Apply advisory locks for all n bytes beginning at ofst.
  3623   3677   */
  3624         -#define _SHM_UNLCK  1
  3625         -#define _SHM_RDLCK  2
  3626         -#define _SHM_WRLCK  3
         3678  +#define WINSHM_UNLCK  1
         3679  +#define WINSHM_RDLCK  2
         3680  +#define WINSHM_WRLCK  3
  3627   3681   static int winShmSystemLock(
  3628   3682     winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
  3629         -  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
         3683  +  int lockType,         /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
  3630   3684     int ofst,             /* Offset to first byte to be locked/unlocked */
  3631   3685     int nByte             /* Number of bytes to lock or unlock */
  3632   3686   ){
  3633   3687     int rc = 0;           /* Result code form Lock/UnlockFileEx() */
  3634   3688   
  3635   3689     /* Access to the winShmNode object is serialized by the caller */
  3636   3690     assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
  3637   3691   
  3638   3692     OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
  3639   3693              pFile->hFile.h, lockType, ofst, nByte));
  3640   3694   
  3641   3695     /* Release/Acquire the system-level lock */
  3642         -  if( lockType==_SHM_UNLCK ){
         3696  +  if( lockType==WINSHM_UNLCK ){
  3643   3697       rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
  3644   3698     }else{
  3645   3699       /* Initialize the locking parameters */
  3646   3700       DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
  3647         -    if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
         3701  +    if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
  3648   3702       rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
  3649   3703     }
  3650   3704   
  3651   3705     if( rc!= 0 ){
  3652   3706       rc = SQLITE_OK;
  3653   3707     }else{
  3654   3708       pFile->lastErrno =  osGetLastError();
  3655   3709       rc = SQLITE_BUSY;
  3656   3710     }
  3657   3711   
  3658   3712     OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
  3659         -           pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
         3713  +           pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
  3660   3714              "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
  3661   3715   
  3662   3716     return rc;
  3663   3717   }
  3664   3718   
  3665   3719   /* Forward references to VFS methods */
  3666   3720   static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
................................................................................
  3780   3834       if( SQLITE_OK!=rc ){
  3781   3835         goto shm_open_err;
  3782   3836       }
  3783   3837   
  3784   3838       /* Check to see if another process is holding the dead-man switch.
  3785   3839       ** If not, truncate the file to zero length.
  3786   3840       */
  3787         -    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
         3841  +    if( winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
  3788   3842         rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
  3789   3843         if( rc!=SQLITE_OK ){
  3790   3844           rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
  3791   3845                            "winOpenShm", pDbFd->zPath);
  3792   3846         }
  3793   3847       }
  3794   3848       if( rc==SQLITE_OK ){
  3795         -      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
  3796         -      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
         3849  +      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
         3850  +      rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
  3797   3851       }
  3798   3852       if( rc ) goto shm_open_err;
  3799   3853     }
  3800   3854   
  3801   3855     /* Make the new connection a child of the winShmNode */
  3802   3856     p->pShmNode = pShmNode;
  3803   3857   #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
................................................................................
  3818   3872     p->pNext = pShmNode->pFirst;
  3819   3873     pShmNode->pFirst = p;
  3820   3874     sqlite3_mutex_leave(pShmNode->mutex);
  3821   3875     return SQLITE_OK;
  3822   3876   
  3823   3877     /* Jump here on any error */
  3824   3878   shm_open_err:
  3825         -  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
         3879  +  winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
  3826   3880     winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
  3827   3881     sqlite3_free(p);
  3828   3882     sqlite3_free(pNew);
  3829   3883     winShmLeaveMutex();
  3830   3884     return rc;
  3831   3885   }
  3832   3886   
................................................................................
  3907   3961         if( pX==p ) continue;
  3908   3962         assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
  3909   3963         allMask |= pX->sharedMask;
  3910   3964       }
  3911   3965   
  3912   3966       /* Unlock the system-level locks */
  3913   3967       if( (mask & allMask)==0 ){
  3914         -      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
         3968  +      rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
  3915   3969       }else{
  3916   3970         rc = SQLITE_OK;
  3917   3971       }
  3918   3972   
  3919   3973       /* Undo the local locks */
  3920   3974       if( rc==SQLITE_OK ){
  3921   3975         p->exclMask &= ~mask;
................................................................................
  3935   3989         }
  3936   3990         allShared |= pX->sharedMask;
  3937   3991       }
  3938   3992   
  3939   3993       /* Get shared locks at the system level, if necessary */
  3940   3994       if( rc==SQLITE_OK ){
  3941   3995         if( (allShared & mask)==0 ){
  3942         -        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
         3996  +        rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
  3943   3997         }else{
  3944   3998           rc = SQLITE_OK;
  3945   3999         }
  3946   4000       }
  3947   4001   
  3948   4002       /* Get the local shared locks */
  3949   4003       if( rc==SQLITE_OK ){
................................................................................
  3960   4014         }
  3961   4015       }
  3962   4016   
  3963   4017       /* Get the exclusive locks at the system level.  Then if successful
  3964   4018       ** also mark the local connection as being locked.
  3965   4019       */
  3966   4020       if( rc==SQLITE_OK ){
  3967         -      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
         4021  +      rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
  3968   4022         if( rc==SQLITE_OK ){
  3969   4023           assert( (p->sharedMask & mask)==0 );
  3970   4024           p->exclMask |= mask;
  3971   4025         }
  3972   4026       }
  3973   4027     }
  3974   4028     sqlite3_mutex_leave(pShmNode->mutex);
................................................................................
  4402   4456     winShmMap,                      /* xShmMap */
  4403   4457     winShmLock,                     /* xShmLock */
  4404   4458     winShmBarrier,                  /* xShmBarrier */
  4405   4459     winShmUnmap,                    /* xShmUnmap */
  4406   4460     winFetch,                       /* xFetch */
  4407   4461     winUnfetch                      /* xUnfetch */
  4408   4462   };
         4463  +
         4464  +/*
         4465  +** This vector defines all the methods that can operate on an
         4466  +** sqlite3_file for win32 without performing any locking.
         4467  +*/
         4468  +static const sqlite3_io_methods winIoNolockMethod = {
         4469  +  3,                              /* iVersion */
         4470  +  winClose,                       /* xClose */
         4471  +  winRead,                        /* xRead */
         4472  +  winWrite,                       /* xWrite */
         4473  +  winTruncate,                    /* xTruncate */
         4474  +  winSync,                        /* xSync */
         4475  +  winFileSize,                    /* xFileSize */
         4476  +  winNolockLock,                  /* xLock */
         4477  +  winNolockUnlock,                /* xUnlock */
         4478  +  winNolockCheckReservedLock,     /* xCheckReservedLock */
         4479  +  winFileControl,                 /* xFileControl */
         4480  +  winSectorSize,                  /* xSectorSize */
         4481  +  winDeviceCharacteristics,       /* xDeviceCharacteristics */
         4482  +  winShmMap,                      /* xShmMap */
         4483  +  winShmLock,                     /* xShmLock */
         4484  +  winShmBarrier,                  /* xShmBarrier */
         4485  +  winShmUnmap,                    /* xShmUnmap */
         4486  +  winFetch,                       /* xFetch */
         4487  +  winUnfetch                      /* xUnfetch */
         4488  +};
         4489  +
         4490  +static winVfsAppData winAppData = {
         4491  +  &winIoMethod,       /* pMethod */
         4492  +  0,                  /* pAppData */
         4493  +  0                   /* bNoLock */
         4494  +};
         4495  +
         4496  +static winVfsAppData winNolockAppData = {
         4497  +  &winIoNolockMethod, /* pMethod */
         4498  +  0,                  /* pAppData */
         4499  +  1                   /* bNoLock */
         4500  +};
  4409   4501   
  4410   4502   /****************************************************************************
  4411   4503   **************************** sqlite3_vfs methods ****************************
  4412   4504   **
  4413   4505   ** This division contains the implementation of methods on the
  4414   4506   ** sqlite3_vfs object.
  4415   4507   */
................................................................................
  4735   4827     return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
  4736   4828   }
  4737   4829   
  4738   4830   /*
  4739   4831   ** Open a file.
  4740   4832   */
  4741   4833   static int winOpen(
  4742         -  sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
         4834  +  sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
  4743   4835     const char *zName,        /* Name of the file (UTF-8) */
  4744   4836     sqlite3_file *id,         /* Write the SQLite file handle here */
  4745   4837     int flags,                /* Open mode flags */
  4746   4838     int *pOutFlags            /* Status return flags */
  4747   4839   ){
  4748   4840     HANDLE h;
  4749   4841     DWORD lastErrno = 0;
................................................................................
  4750   4842     DWORD dwDesiredAccess;
  4751   4843     DWORD dwShareMode;
  4752   4844     DWORD dwCreationDisposition;
  4753   4845     DWORD dwFlagsAndAttributes = 0;
  4754   4846   #if SQLITE_OS_WINCE
  4755   4847     int isTemp = 0;
  4756   4848   #endif
         4849  +  winVfsAppData *pAppData;
  4757   4850     winFile *pFile = (winFile*)id;
  4758   4851     void *zConverted;              /* Filename in OS encoding */
  4759   4852     const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
  4760   4853     int cnt = 0;
  4761   4854   
  4762   4855     /* If argument zPath is a NULL pointer, this function is required to open
  4763   4856     ** a temporary file. Use this buffer to store the file name in.
................................................................................
  4971   5064       }
  4972   5065     }
  4973   5066   
  4974   5067     OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
  4975   5068              "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
  4976   5069              *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
  4977   5070   
         5071  +  pAppData = (winVfsAppData*)pVfs->pAppData;
         5072  +
  4978   5073   #if SQLITE_OS_WINCE
  4979         -  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
  4980         -       && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
  4981         -  ){
  4982         -    osCloseHandle(h);
  4983         -    sqlite3_free(zConverted);
  4984         -    sqlite3_free(zTmpname);
  4985         -    OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
  4986         -    return rc;
         5074  +  {
         5075  +    if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
         5076  +         && ((pAppData==NULL) || !pAppData->bNoLock)
         5077  +         && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
         5078  +    ){
         5079  +      osCloseHandle(h);
         5080  +      sqlite3_free(zConverted);
         5081  +      sqlite3_free(zTmpname);
         5082  +      OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
         5083  +      return rc;
         5084  +    }
  4987   5085     }
  4988   5086     if( isTemp ){
  4989   5087       pFile->zDeleteOnClose = zConverted;
  4990   5088     }else
  4991   5089   #endif
  4992   5090     {
  4993   5091       sqlite3_free(zConverted);
  4994   5092     }
  4995   5093   
  4996   5094     sqlite3_free(zTmpname);
  4997         -  pFile->pMethod = &winIoMethod;
         5095  +  pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
  4998   5096     pFile->pVfs = pVfs;
  4999   5097     pFile->h = h;
  5000   5098     if( isReadonly ){
  5001   5099       pFile->ctrlFlags |= WINFILE_RDONLY;
  5002   5100     }
  5003   5101     if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  5004   5102       pFile->ctrlFlags |= WINFILE_PSOW;
................................................................................
  5265   5363   */
  5266   5364   static int winFullPathname(
  5267   5365     sqlite3_vfs *pVfs,            /* Pointer to vfs object */
  5268   5366     const char *zRelative,        /* Possibly relative input path */
  5269   5367     int nFull,                    /* Size of output buffer in bytes */
  5270   5368     char *zFull                   /* Output buffer */
  5271   5369   ){
         5370  +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
         5371  +  DWORD nByte;
         5372  +  void *zConverted;
         5373  +  char *zOut;
         5374  +#endif
         5375  +
         5376  +  /* If this path name begins with "/X:", where "X" is any alphabetic
         5377  +  ** character, discard the initial "/" from the pathname.
         5378  +  */
         5379  +  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
         5380  +    zRelative++;
         5381  +  }
  5272   5382   
  5273   5383   #if defined(__CYGWIN__)
  5274   5384     SimulateIOError( return SQLITE_ERROR );
  5275   5385     UNUSED_PARAMETER(nFull);
  5276   5386     assert( nFull>=pVfs->mxPathname );
  5277   5387     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  5278   5388       /*
................................................................................
  5343   5453     }else{
  5344   5454       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
  5345   5455     }
  5346   5456     return SQLITE_OK;
  5347   5457   #endif
  5348   5458   
  5349   5459   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  5350         -  DWORD nByte;
  5351         -  void *zConverted;
  5352         -  char *zOut;
  5353         -
  5354         -  /* If this path name begins with "/X:", where "X" is any alphabetic
  5355         -  ** character, discard the initial "/" from the pathname.
  5356         -  */
  5357         -  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
  5358         -    zRelative++;
  5359         -  }
  5360         -
  5361   5460     /* It's odd to simulate an io-error here, but really this is just
  5362   5461     ** using the io-error infrastructure to test that SQLite handles this
  5363   5462     ** function failing. This function could fail if, for example, the
  5364   5463     ** current working directory has been unlinked.
  5365   5464     */
  5366   5465     SimulateIOError( return SQLITE_ERROR );
  5367   5466     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
................................................................................
  5712   5811   }
  5713   5812   
  5714   5813   /*
  5715   5814   ** Initialize and deinitialize the operating system interface.
  5716   5815   */
  5717   5816   int sqlite3_os_init(void){
  5718   5817     static sqlite3_vfs winVfs = {
  5719         -    3,                   /* iVersion */
  5720         -    sizeof(winFile),     /* szOsFile */
         5818  +    3,                     /* iVersion */
         5819  +    sizeof(winFile),       /* szOsFile */
  5721   5820       SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
  5722         -    0,                   /* pNext */
  5723         -    "win32",             /* zName */
  5724         -    0,                   /* pAppData */
  5725         -    winOpen,             /* xOpen */
  5726         -    winDelete,           /* xDelete */
  5727         -    winAccess,           /* xAccess */
  5728         -    winFullPathname,     /* xFullPathname */
  5729         -    winDlOpen,           /* xDlOpen */
  5730         -    winDlError,          /* xDlError */
  5731         -    winDlSym,            /* xDlSym */
  5732         -    winDlClose,          /* xDlClose */
  5733         -    winRandomness,       /* xRandomness */
  5734         -    winSleep,            /* xSleep */
  5735         -    winCurrentTime,      /* xCurrentTime */
  5736         -    winGetLastError,     /* xGetLastError */
  5737         -    winCurrentTimeInt64, /* xCurrentTimeInt64 */
  5738         -    winSetSystemCall,    /* xSetSystemCall */
  5739         -    winGetSystemCall,    /* xGetSystemCall */
  5740         -    winNextSystemCall,   /* xNextSystemCall */
         5821  +    0,                     /* pNext */
         5822  +    "win32",               /* zName */
         5823  +    &winAppData,           /* pAppData */
         5824  +    winOpen,               /* xOpen */
         5825  +    winDelete,             /* xDelete */
         5826  +    winAccess,             /* xAccess */
         5827  +    winFullPathname,       /* xFullPathname */
         5828  +    winDlOpen,             /* xDlOpen */
         5829  +    winDlError,            /* xDlError */
         5830  +    winDlSym,              /* xDlSym */
         5831  +    winDlClose,            /* xDlClose */
         5832  +    winRandomness,         /* xRandomness */
         5833  +    winSleep,              /* xSleep */
         5834  +    winCurrentTime,        /* xCurrentTime */
         5835  +    winGetLastError,       /* xGetLastError */
         5836  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
         5837  +    winSetSystemCall,      /* xSetSystemCall */
         5838  +    winGetSystemCall,      /* xGetSystemCall */
         5839  +    winNextSystemCall,     /* xNextSystemCall */
  5741   5840     };
  5742   5841   #if defined(SQLITE_WIN32_HAS_WIDE)
  5743   5842     static sqlite3_vfs winLongPathVfs = {
  5744         -    3,                   /* iVersion */
  5745         -    sizeof(winFile),     /* szOsFile */
         5843  +    3,                     /* iVersion */
         5844  +    sizeof(winFile),       /* szOsFile */
         5845  +    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
         5846  +    0,                     /* pNext */
         5847  +    "win32-longpath",      /* zName */
         5848  +    &winAppData,           /* pAppData */
         5849  +    winOpen,               /* xOpen */
         5850  +    winDelete,             /* xDelete */
         5851  +    winAccess,             /* xAccess */
         5852  +    winFullPathname,       /* xFullPathname */
         5853  +    winDlOpen,             /* xDlOpen */
         5854  +    winDlError,            /* xDlError */
         5855  +    winDlSym,              /* xDlSym */
         5856  +    winDlClose,            /* xDlClose */
         5857  +    winRandomness,         /* xRandomness */
         5858  +    winSleep,              /* xSleep */
         5859  +    winCurrentTime,        /* xCurrentTime */
         5860  +    winGetLastError,       /* xGetLastError */
         5861  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
         5862  +    winSetSystemCall,      /* xSetSystemCall */
         5863  +    winGetSystemCall,      /* xGetSystemCall */
         5864  +    winNextSystemCall,     /* xNextSystemCall */
         5865  +  };
         5866  +#endif
         5867  +  static sqlite3_vfs winNolockVfs = {
         5868  +    3,                     /* iVersion */
         5869  +    sizeof(winFile),       /* szOsFile */
         5870  +    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
         5871  +    0,                     /* pNext */
         5872  +    "win32-none",          /* zName */
         5873  +    &winNolockAppData,     /* pAppData */
         5874  +    winOpen,               /* xOpen */
         5875  +    winDelete,             /* xDelete */
         5876  +    winAccess,             /* xAccess */
         5877  +    winFullPathname,       /* xFullPathname */
         5878  +    winDlOpen,             /* xDlOpen */
         5879  +    winDlError,            /* xDlError */
         5880  +    winDlSym,              /* xDlSym */
         5881  +    winDlClose,            /* xDlClose */
         5882  +    winRandomness,         /* xRandomness */
         5883  +    winSleep,              /* xSleep */
         5884  +    winCurrentTime,        /* xCurrentTime */
         5885  +    winGetLastError,       /* xGetLastError */
         5886  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
         5887  +    winSetSystemCall,      /* xSetSystemCall */
         5888  +    winGetSystemCall,      /* xGetSystemCall */
         5889  +    winNextSystemCall,     /* xNextSystemCall */
         5890  +  };
         5891  +#if defined(SQLITE_WIN32_HAS_WIDE)
         5892  +  static sqlite3_vfs winLongPathNolockVfs = {
         5893  +    3,                     /* iVersion */
         5894  +    sizeof(winFile),       /* szOsFile */
  5746   5895       SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
  5747         -    0,                   /* pNext */
  5748         -    "win32-longpath",    /* zName */
  5749         -    0,                   /* pAppData */
  5750         -    winOpen,             /* xOpen */
  5751         -    winDelete,           /* xDelete */
  5752         -    winAccess,           /* xAccess */
  5753         -    winFullPathname,     /* xFullPathname */
  5754         -    winDlOpen,           /* xDlOpen */
  5755         -    winDlError,          /* xDlError */
  5756         -    winDlSym,            /* xDlSym */
  5757         -    winDlClose,          /* xDlClose */
  5758         -    winRandomness,       /* xRandomness */
  5759         -    winSleep,            /* xSleep */
  5760         -    winCurrentTime,      /* xCurrentTime */
  5761         -    winGetLastError,     /* xGetLastError */
  5762         -    winCurrentTimeInt64, /* xCurrentTimeInt64 */
  5763         -    winSetSystemCall,    /* xSetSystemCall */
  5764         -    winGetSystemCall,    /* xGetSystemCall */
  5765         -    winNextSystemCall,   /* xNextSystemCall */
         5896  +    0,                     /* pNext */
         5897  +    "win32-longpath-none", /* zName */
         5898  +    &winNolockAppData,     /* pAppData */
         5899  +    winOpen,               /* xOpen */
         5900  +    winDelete,             /* xDelete */
         5901  +    winAccess,             /* xAccess */
         5902  +    winFullPathname,       /* xFullPathname */
         5903  +    winDlOpen,             /* xDlOpen */
         5904  +    winDlError,            /* xDlError */
         5905  +    winDlSym,              /* xDlSym */
         5906  +    winDlClose,            /* xDlClose */
         5907  +    winRandomness,         /* xRandomness */
         5908  +    winSleep,              /* xSleep */
         5909  +    winCurrentTime,        /* xCurrentTime */
         5910  +    winGetLastError,       /* xGetLastError */
         5911  +    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
         5912  +    winSetSystemCall,      /* xSetSystemCall */
         5913  +    winGetSystemCall,      /* xGetSystemCall */
         5914  +    winNextSystemCall,     /* xNextSystemCall */
  5766   5915     };
  5767   5916   #endif
  5768   5917   
  5769   5918     /* Double-check that the aSyscall[] array has been constructed
  5770   5919     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  5771   5920     assert( ArraySize(aSyscall)==80 );
  5772   5921   
................................................................................
  5781   5930     assert( winSysInfo.dwPageSize>0 );
  5782   5931   
  5783   5932     sqlite3_vfs_register(&winVfs, 1);
  5784   5933   
  5785   5934   #if defined(SQLITE_WIN32_HAS_WIDE)
  5786   5935     sqlite3_vfs_register(&winLongPathVfs, 0);
  5787   5936   #endif
         5937  +
         5938  +  sqlite3_vfs_register(&winNolockVfs, 0);
         5939  +
         5940  +#if defined(SQLITE_WIN32_HAS_WIDE)
         5941  +  sqlite3_vfs_register(&winLongPathNolockVfs, 0);
         5942  +#endif
  5788   5943   
  5789   5944     return SQLITE_OK;
  5790   5945   }
  5791   5946   
  5792   5947   int sqlite3_os_end(void){
  5793   5948   #if SQLITE_OS_WINRT
  5794   5949     if( sleepObj!=NULL ){

Changes to src/os_win.h.

     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   **
    13     13   ** This file contains code that is specific to Windows.
    14     14   */
    15         -#ifndef _OS_WIN_H_
    16         -#define _OS_WIN_H_
           15  +#ifndef SQLITE_OS_WIN_H
           16  +#define SQLITE_OS_WIN_H
    17     17   
    18     18   /*
    19     19   ** Include the primary Windows SDK header file.
    20     20   */
    21     21   #include "windows.h"
    22     22   
    23     23   #ifdef __CYGWIN__
................................................................................
    81     81   #if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
    82     82       SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
    83     83   # define SQLITE_OS_WIN_THREADS 1
    84     84   #else
    85     85   # define SQLITE_OS_WIN_THREADS 0
    86     86   #endif
    87     87   
    88         -#endif /* _OS_WIN_H_ */
           88  +#endif /* SQLITE_OS_WIN_H */

Changes to src/pager.c.

  1922   1922     }
  1923   1923     return rc;
  1924   1924   }
  1925   1925   
  1926   1926   static int pager_truncate(Pager *pPager, Pgno nPage);
  1927   1927   
  1928   1928   /*
  1929         -** The write transaction open on the pager passed as the only argument is
  1930         -** being committed. This function returns true if all dirty pages should
  1931         -** be flushed to disk, or false otherwise. Pages should be flushed to disk
  1932         -** unless one of the following is true:
         1929  +** The write transaction open on pPager is being committed (bCommit==1)
         1930  +** or rolled back (bCommit==0).
  1933   1931   **
  1934         -**   * The db is an in-memory database.
         1932  +** Return TRUE if and only if all dirty pages should be flushed to disk.
  1935   1933   **
  1936         -**   * The db is a temporary database and the db file has not been opened.
         1934  +** Rules:
  1937   1935   **
  1938         -**   * The db is a temporary database and the cache contains less than
  1939         -**     C/4 dirty pages, where C is the configured cache-size.
         1936  +**   *  For non-TEMP databases, always sync to disk.  This is necessary
         1937  +**      for transactions to be durable.
         1938  +**
         1939  +**   *  Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
         1940  +**      file has been created already (via a spill on pagerStress()) and
         1941  +**      when the number of dirty pages in memory exceeds 25% of the total
         1942  +**      cache size.
  1940   1943   */
  1941         -static int pagerFlushOnCommit(Pager *pPager){
         1944  +static int pagerFlushOnCommit(Pager *pPager, int bCommit){
  1942   1945     if( pPager->tempFile==0 ) return 1;
         1946  +  if( !bCommit ) return 0;
  1943   1947     if( !isOpen(pPager->fd) ) return 0;
  1944   1948     return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
  1945   1949   }
  1946   1950   
  1947   1951   /*
  1948   1952   ** This routine ends a transaction. A transaction is usually ended by 
  1949   1953   ** either a COMMIT or a ROLLBACK operation. This routine may be called 
................................................................................
  2043   2047             rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
  2044   2048           }
  2045   2049         }
  2046   2050         pPager->journalOff = 0;
  2047   2051       }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
  2048   2052         || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
  2049   2053       ){
  2050         -      rc = zeroJournalHdr(pPager, hasMaster);
         2054  +      rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
  2051   2055         pPager->journalOff = 0;
  2052   2056       }else{
  2053   2057         /* This branch may be executed with Pager.journalMode==MEMORY if
  2054   2058         ** a hot-journal was just rolled back. In this case the journal
  2055   2059         ** file should be closed and deleted. If this connection writes to
  2056   2060         ** the database file, it will do so using an in-memory journal.
  2057   2061         */
................................................................................
  2077   2081         sqlite3PagerUnrefNotNull(p);
  2078   2082       }
  2079   2083     }
  2080   2084   #endif
  2081   2085   
  2082   2086     pagerFreeBitvecs(pPager);
  2083   2087     pPager->nRec = 0;
  2084         -  if( MEMDB || pagerFlushOnCommit(pPager) ){
  2085         -    sqlite3PcacheCleanAll(pPager->pPCache);
  2086         -  }else{
  2087         -    sqlite3PcacheClearWritable(pPager->pPCache);
         2088  +  if( rc==SQLITE_OK ){
         2089  +    if( pagerFlushOnCommit(pPager, bCommit) ){
         2090  +      sqlite3PcacheCleanAll(pPager->pPCache);
         2091  +    }else{
         2092  +      sqlite3PcacheClearWritable(pPager->pPCache);
         2093  +    }
         2094  +    sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  2088   2095     }
  2089         -  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  2090   2096   
  2091   2097     if( pagerUseWal(pPager) ){
  2092   2098       /* Drop the WAL write-lock, if any. Also, if the connection was in 
  2093   2099       ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
  2094   2100       ** lock held on the database file.
  2095   2101       */
  2096   2102       rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
................................................................................
  2416   2422       assert( isSavepnt );
  2417   2423       assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
  2418   2424       pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
  2419   2425       rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
  2420   2426       assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
  2421   2427       pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
  2422   2428       if( rc!=SQLITE_OK ) return rc;
  2423         -    pPg->flags &= ~PGHDR_NEED_READ;
  2424   2429       sqlite3PcacheMakeDirty(pPg);
  2425   2430     }
  2426   2431     if( pPg ){
  2427   2432       /* No page should ever be explicitly rolled back that is in use, except
  2428   2433       ** for page 1 which is held in use in order to keep the lock on the
  2429   2434       ** database active. However such a page may be rolled back as a result
  2430   2435       ** of an internal error resulting in an automatic call to
  2431   2436       ** sqlite3PagerRollback().
  2432   2437       */
  2433   2438       void *pData;
  2434   2439       pData = pPg->pData;
  2435   2440       memcpy(pData, (u8*)aData, pPager->pageSize);
  2436   2441       pPager->xReiniter(pPg);
  2437         -    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
  2438         -      /* If the contents of this page were just restored from the main 
  2439         -      ** journal file, then its content must be as they were when the 
  2440         -      ** transaction was first opened. In this case we can mark the page
  2441         -      ** as clean, since there will be no need to write it out to the
  2442         -      ** database.
  2443         -      **
  2444         -      ** There is one exception to this rule. If the page is being rolled
  2445         -      ** back as part of a savepoint (or statement) rollback from an 
  2446         -      ** unsynced portion of the main journal file, then it is not safe
  2447         -      ** to mark the page as clean. This is because marking the page as
  2448         -      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
  2449         -      ** already in the journal file (recorded in Pager.pInJournal) and
  2450         -      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
  2451         -      ** again within this transaction, it will be marked as dirty but
  2452         -      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
  2453         -      ** be written out into the database file before its journal file
  2454         -      ** segment is synced. If a crash occurs during or following this,
  2455         -      ** database corruption may ensue.
  2456         -      **
  2457         -      ** Update: Another exception is for temp files that are not 
  2458         -      ** in-memory databases. In this case the page may have been dirty
  2459         -      ** at the start of the transaction.
  2460         -      */
  2461         -      assert( !pagerUseWal(pPager) );
  2462         -      if( pPager->tempFile==0 ) sqlite3PcacheMakeClean(pPg);
  2463         -    }
         2442  +    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
         2443  +    ** that call was dangerous and had no detectable benefit since the cache
         2444  +    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
         2445  +    ** has been removed. */
  2464   2446       pager_set_pagehash(pPg);
  2465   2447   
  2466   2448       /* If this was page 1, then restore the value of Pager.dbFileVers.
  2467   2449       ** Do this before any decoding. */
  2468   2450       if( pgno==1 ){
  2469   2451         memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
  2470   2452       }
................................................................................
  6082   6064   **
  6083   6065   ** The overlying software layer calls this routine when all of the data
  6084   6066   ** on the given page is unused. The pager marks the page as clean so
  6085   6067   ** that it does not get written to disk.
  6086   6068   **
  6087   6069   ** Tests show that this optimization can quadruple the speed of large 
  6088   6070   ** DELETE operations.
         6071  +**
         6072  +** This optimization cannot be used with a temp-file, as the page may
         6073  +** have been dirty at the start of the transaction. In that case, if
         6074  +** memory pressure forces page pPg out of the cache, the data does need 
         6075  +** to be written out to disk so that it may be read back in if the 
         6076  +** current transaction is rolled back.
  6089   6077   */
  6090   6078   void sqlite3PagerDontWrite(PgHdr *pPg){
  6091   6079     Pager *pPager = pPg->pPager;
  6092         -  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
         6080  +  if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
  6093   6081       PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
  6094   6082       IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
  6095   6083       pPg->flags |= PGHDR_DONT_WRITE;
  6096   6084       pPg->flags &= ~PGHDR_WRITEABLE;
         6085  +    testcase( pPg->flags & PGHDR_NEED_SYNC );
  6097   6086       pager_set_pagehash(pPg);
  6098   6087     }
  6099   6088   }
  6100   6089   
  6101   6090   /*
  6102   6091   ** This routine is called to increment the value of the database file 
  6103   6092   ** change-counter, stored as a 4-byte big-endian integer starting at 
................................................................................
  6359   6348     );
  6360   6349     assert( assert_pager_state(pPager) );
  6361   6350   
  6362   6351     /* If a prior error occurred, report that error again. */
  6363   6352     if( NEVER(pPager->errCode) ) return pPager->errCode;
  6364   6353   
  6365   6354     /* Provide the ability to easily simulate an I/O error during testing */
  6366         -  if( (rc = sqlite3FaultSim(400))!=SQLITE_OK ) return rc;
         6355  +  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
  6367   6356   
  6368   6357     PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
  6369   6358         pPager->zFilename, zMaster, pPager->dbSize));
  6370   6359   
  6371   6360     /* If no database changes have been made, return early. */
  6372   6361     if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
  6373   6362   
  6374   6363     assert( MEMDB==0 || pPager->tempFile );
  6375   6364     assert( isOpen(pPager->fd) || pPager->tempFile );
  6376         -  if( 0==pagerFlushOnCommit(pPager) ){
         6365  +  if( 0==pagerFlushOnCommit(pPager, 1) ){
  6377   6366       /* If this is an in-memory db, or no pages have been written to, or this
  6378   6367       ** function has already been called, it is mostly a no-op.  However, any
  6379   6368       ** backup in progress needs to be restarted.  */
  6380   6369       sqlite3BackupRestart(pPager->pBackup);
  6381   6370     }else{
  6382   6371       if( pagerUseWal(pPager) ){
  6383   6372         PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
................................................................................
  6994   6983          || pPager->eState==PAGER_WRITER_DBMOD
  6995   6984     );
  6996   6985     assert( assert_pager_state(pPager) );
  6997   6986   
  6998   6987     /* In order to be able to rollback, an in-memory database must journal
  6999   6988     ** the page we are moving from.
  7000   6989     */
         6990  +  assert( pPager->tempFile || !MEMDB );
  7001   6991     if( pPager->tempFile ){
  7002   6992       rc = sqlite3PagerWrite(pPg);
  7003   6993       if( rc ) return rc;
  7004   6994     }
  7005   6995   
  7006   6996     /* If the page being moved is dirty and has not been saved by the latest
  7007   6997     ** savepoint, then save the current contents of the page into the 
................................................................................
  7068   7058     sqlite3PcacheMove(pPg, pgno);
  7069   7059     sqlite3PcacheMakeDirty(pPg);
  7070   7060   
  7071   7061     /* For an in-memory database, make sure the original page continues
  7072   7062     ** to exist, in case the transaction needs to roll back.  Use pPgOld
  7073   7063     ** as the original page since it has already been allocated.
  7074   7064     */
  7075         -  if( pPager->tempFile ){
  7076         -    assert( pPgOld );
         7065  +  if( pPager->tempFile && pPgOld ){
  7077   7066       sqlite3PcacheMove(pPgOld, origPgno);
  7078   7067       sqlite3PagerUnrefNotNull(pPgOld);
  7079   7068     }
  7080   7069   
  7081   7070     if( needSyncPgno ){
  7082   7071       /* If needSyncPgno is non-zero, then the journal file needs to be 
  7083   7072       ** sync()ed before any data is written to database file page needSyncPgno.
................................................................................
  7325   7314   ** Unless this is an in-memory or temporary database, clear the pager cache.
  7326   7315   */
  7327   7316   void sqlite3PagerClearCache(Pager *pPager){
  7328   7317     assert( MEMDB==0 || pPager->tempFile );
  7329   7318     if( pPager->tempFile==0 ) pager_reset(pPager);
  7330   7319   }
  7331   7320   #endif
         7321  +
  7332   7322   
  7333   7323   #ifndef SQLITE_OMIT_WAL
  7334   7324   /*
  7335   7325   ** This function is called when the user invokes "PRAGMA wal_checkpoint",
  7336   7326   ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
  7337   7327   ** or wal_blocking_checkpoint() API functions.
  7338   7328   **
................................................................................
  7550   7540   ** is empty, return 0.
  7551   7541   */
  7552   7542   int sqlite3PagerWalFramesize(Pager *pPager){
  7553   7543     assert( pPager->eState>=PAGER_READER );
  7554   7544     return sqlite3WalFramesize(pPager->pWal);
  7555   7545   }
  7556   7546   #endif
  7557         -
  7558   7547   
  7559   7548   #endif /* SQLITE_OMIT_DISKIO */

Changes to src/pager.h.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite page cache
    13     13   ** subsystem.  The page cache subsystem reads and writes a file a page
    14     14   ** at a time and provides a journal for rollback.
    15     15   */
    16     16   
    17         -#ifndef _PAGER_H_
    18         -#define _PAGER_H_
           17  +#ifndef SQLITE_PAGER_H
           18  +#define SQLITE_PAGER_H
    19     19   
    20     20   /*
    21     21   ** Default maximum size for persistent journal files. A negative 
    22     22   ** value means no limit. This value may be overridden using the 
    23     23   ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
    24     24   */
    25     25   #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
................................................................................
   200    200   sqlite3_vfs *sqlite3PagerVfs(Pager*);
   201    201   sqlite3_file *sqlite3PagerFile(Pager*);
   202    202   sqlite3_file *sqlite3PagerJrnlFile(Pager*);
   203    203   const char *sqlite3PagerJournalname(Pager*);
   204    204   void *sqlite3PagerTempSpace(Pager*);
   205    205   int sqlite3PagerIsMemdb(Pager*);
   206    206   void sqlite3PagerCacheStat(Pager *, int, int, int *);
   207         -void sqlite3PagerClearCache(Pager *);
          207  +void sqlite3PagerClearCache(Pager*);
   208    208   int sqlite3SectorSize(sqlite3_file *);
   209    209   
   210    210   /* Functions used to truncate the database file. */
   211    211   void sqlite3PagerTruncateImage(Pager*,Pgno);
   212    212   
   213    213   void sqlite3PagerRekey(DbPage*, Pgno, u16);
   214    214   
................................................................................
   239    239     void disable_simulated_io_errors(void);
   240    240     void enable_simulated_io_errors(void);
   241    241   #else
   242    242   # define disable_simulated_io_errors()
   243    243   # define enable_simulated_io_errors()
   244    244   #endif
   245    245   
   246         -#endif /* _PAGER_H_ */
          246  +#endif /* SQLITE_PAGER_H */

Changes to src/parse.y.

   312    312   // In addition to the type name, we also care about the primary key and
   313    313   // UNIQUE constraints.
   314    314   //
   315    315   ccons ::= NULL onconf.
   316    316   ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
   317    317   ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
   318    318                                    {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
   319         -ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
          319  +ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0,
          320  +                                   SQLITE_IDXTYPE_UNIQUE);}
   320    321   ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
   321    322   ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R).
   322    323                                    {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
   323    324   ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
   324    325   ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
   325    326   
   326    327   // The optional AUTOINCREMENT keyword
................................................................................
   361    362   conslist ::= tcons.
   362    363   tconscomma ::= COMMA.            {pParse->constraintName.n = 0;}
   363    364   tconscomma ::= .
   364    365   tcons ::= CONSTRAINT nm(X).      {pParse->constraintName = X;}
   365    366   tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R).
   366    367                                    {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
   367    368   tcons ::= UNIQUE LP sortlist(X) RP onconf(R).
   368         -                                 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
          369  +                                 {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0,
          370  +                                       SQLITE_IDXTYPE_UNIQUE);}
   369    371   tcons ::= CHECK LP expr(E) RP onconf.
   370    372                                    {sqlite3AddCheckConstraint(pParse,E.pExpr);}
   371    373   tcons ::= FOREIGN KEY LP eidlist(FA) RP
   372    374             REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). {
   373    375       sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
   374    376       sqlite3DeferForeignKey(pParse, D);
   375    377   }
................................................................................
  1144   1146     }
  1145   1147     expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
  1146   1148       A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
  1147   1149       sqlite3PExprAddSelect(pParse, A.pExpr, Y);
  1148   1150       exprNot(pParse, N, &A);
  1149   1151       A.zEnd = &E.z[E.n];
  1150   1152     }
  1151         -  expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z). [IN] {
         1153  +  expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
  1152   1154       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
  1153   1155       Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
         1156  +    if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
  1154   1157       A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
  1155   1158       sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
  1156   1159       exprNot(pParse, N, &A);
  1157   1160       A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
  1158   1161     }
  1159   1162     expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
  1160   1163       Expr *p;
................................................................................
  1203   1206   exprlist(A) ::= nexprlist(A).
  1204   1207   exprlist(A) ::= .                            {A = 0;}
  1205   1208   nexprlist(A) ::= nexprlist(A) COMMA expr(Y).
  1206   1209       {A = sqlite3ExprListAppend(pParse,A,Y.pExpr);}
  1207   1210   nexprlist(A) ::= expr(Y).
  1208   1211       {A = sqlite3ExprListAppend(pParse,0,Y.pExpr); /*A-overwrites-Y*/}
  1209   1212   
         1213  +%ifndef SQLITE_OMIT_SUBQUERY
         1214  +/* A paren_exprlist is an optional expression list contained inside
         1215  +** of parenthesis */
         1216  +%type paren_exprlist {ExprList*}
         1217  +%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
         1218  +paren_exprlist(A) ::= .   {A = 0;}
         1219  +paren_exprlist(A) ::= LP exprlist(X) RP.  {A = X;}
         1220  +%endif SQLITE_OMIT_SUBQUERY
         1221  +
  1210   1222   
  1211   1223   ///////////////////////////// The CREATE INDEX command ///////////////////////
  1212   1224   //
  1213   1225   cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
  1214   1226           ON nm(Y) LP sortlist(Z) RP where_opt(W). {
  1215   1227     sqlite3CreateIndex(pParse, &X, &D, 
  1216   1228                        sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
  1217         -                      &S, W, SQLITE_SO_ASC, NE);
         1229  +                      &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF);
  1218   1230   }
  1219   1231   
  1220   1232   %type uniqueflag {int}
  1221   1233   uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
  1222   1234   uniqueflag(A) ::= .        {A = OE_None;}
  1223   1235   
  1224   1236   

Changes to src/pcache.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file implements that page cache.
    13     13   */
    14     14   #include "sqliteInt.h"
    15     15   
    16     16   /*
    17         -** A complete page cache is an instance of this structure.
           17  +** A complete page cache is an instance of this structure.  Every
           18  +** entry in the cache holds a single page of the database file.  The
           19  +** btree layer only operates on the cached copy of the database pages.
           20  +**
           21  +** A page cache entry is "clean" if it exactly matches what is currently
           22  +** on disk.  A page is "dirty" if it has been modified and needs to be
           23  +** persisted to disk.
           24  +**
           25  +** pDirty, pDirtyTail, pSynced:
           26  +**   All dirty pages are linked into the doubly linked list using
           27  +**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
           28  +**   such that p was added to the list more recently than p->pDirtyNext.
           29  +**   PCache.pDirty points to the first (newest) element in the list and
           30  +**   pDirtyTail to the last (oldest).
           31  +**
           32  +**   The PCache.pSynced variable is used to optimize searching for a dirty
           33  +**   page to eject from the cache mid-transaction. It is better to eject
           34  +**   a page that does not require a journal sync than one that does. 
           35  +**   Therefore, pSynced is maintained to that it *almost* always points
           36  +**   to either the oldest page in the pDirty/pDirtyTail list that has a
           37  +**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
           38  +**   (so that the right page to eject can be found by following pDirtyPrev
           39  +**   pointers).
    18     40   */
    19     41   struct PCache {
    20     42     PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
    21     43     PgHdr *pSynced;                     /* Last synced page in dirty page list */
    22     44     int nRefSum;                        /* Sum of ref counts over all pages */
    23     45     int szCache;                        /* Configured cache size */
    24     46     int szSpill;                        /* Size before spilling occurs */
................................................................................
    26     48     int szExtra;                        /* Size of extra space for each page */
    27     49     u8 bPurgeable;                      /* True if pages are on backing store */
    28     50     u8 eCreate;                         /* eCreate value for for xFetch() */
    29     51     int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
    30     52     void *pStress;                      /* Argument to xStress */
    31     53     sqlite3_pcache *pCache;             /* Pluggable cache module */
    32     54   };
           55  +
           56  +/********************************** Test and Debug Logic **********************/
           57  +/*
           58  +** Debug tracing macros.  Enable by by changing the "0" to "1" and
           59  +** recompiling.
           60  +**
           61  +** When sqlite3PcacheTrace is 1, single line trace messages are issued.
           62  +** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
           63  +** is displayed for many operations, resulting in a lot of output.
           64  +*/
           65  +#if defined(SQLITE_DEBUG) && 0
           66  +  int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
           67  +  int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
           68  +# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
           69  +  void pcacheDump(PCache *pCache){
           70  +    int N;
           71  +    int i, j;
           72  +    sqlite3_pcache_page *pLower;
           73  +    PgHdr *pPg;
           74  +    unsigned char *a;
           75  +  
           76  +    if( sqlite3PcacheTrace<2 ) return;
           77  +    if( pCache->pCache==0 ) return;
           78  +    N = sqlite3PcachePagecount(pCache);
           79  +    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
           80  +    for(i=1; i<=N; i++){
           81  +       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
           82  +       if( pLower==0 ) continue;
           83  +       pPg = (PgHdr*)pLower->pExtra;
           84  +       printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
           85  +       a = (unsigned char *)pLower->pBuf;
           86  +       for(j=0; j<12; j++) printf("%02x", a[j]);
           87  +       printf("\n");
           88  +       if( pPg->pPage==0 ){
           89  +         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
           90  +       }
           91  +    }
           92  +  }
           93  +  #else
           94  +# define pcacheTrace(X)
           95  +# define pcacheDump(X)
           96  +#endif
           97  +
           98  +/*
           99  +** Check invariants on a PgHdr entry.  Return true if everything is OK.
          100  +** Return false if any invariant is violated.
          101  +**
          102  +** This routine is for use inside of assert() statements only.  For
          103  +** example:
          104  +**
          105  +**          assert( sqlite3PcachePageSanity(pPg) );
          106  +*/
          107  +#if SQLITE_DEBUG
          108  +int sqlite3PcachePageSanity(PgHdr *pPg){
          109  +  PCache *pCache;
          110  +  assert( pPg!=0 );
          111  +  assert( pPg->pgno>0 );    /* Page number is 1 or more */
          112  +  pCache = pPg->pCache;
          113  +  assert( pCache!=0 );      /* Every page has an associated PCache */
          114  +  if( pPg->flags & PGHDR_CLEAN ){
          115  +    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
          116  +    assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
          117  +    assert( pCache->pDirtyTail!=pPg );
          118  +  }
          119  +  /* WRITEABLE pages must also be DIRTY */
          120  +  if( pPg->flags & PGHDR_WRITEABLE ){
          121  +    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
          122  +  }
          123  +  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
          124  +  ** for example, when using the sqlite3PagerDontWrite() optimization:
          125  +  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
          126  +  **    (2)  Page X moved to freelist, WRITEABLE is cleared
          127  +  **    (3)  Page X reused, WRITEABLE is set again
          128  +  ** If NEED_SYNC had been cleared in step 2, then it would not be reset
          129  +  ** in step 3, and page might be written into the database without first
          130  +  ** syncing the rollback journal, which might cause corruption on a power
          131  +  ** loss.
          132  +  **
          133  +  ** Another example is when the database page size is smaller than the
          134  +  ** disk sector size.  When any page of a sector is journalled, all pages
          135  +  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
          136  +  ** in case they are later modified, since all pages in the same sector
          137  +  ** must be journalled and synced before any of those pages can be safely
          138  +  ** written.
          139  +  */
          140  +  return 1;
          141  +}
          142  +#endif /* SQLITE_DEBUG */
          143  +
    33    144   
    34    145   /********************************** Linked List Management ********************/
    35    146   
    36    147   /* Allowed values for second argument to pcacheManageDirtyList() */
    37    148   #define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
    38    149   #define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
    39    150   #define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
................................................................................
    43    154   ** argument determines what operation to do.  The 0x01 bit means first
    44    155   ** remove pPage from the dirty list.  The 0x02 means add pPage back to
    45    156   ** the dirty list.  Doing both moves pPage to the front of the dirty list.
    46    157   */
    47    158   static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
    48    159     PCache *p = pPage->pCache;
    49    160   
          161  +  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
          162  +                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
          163  +                pPage->pgno));
    50    164     if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
    51    165       assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
    52    166       assert( pPage->pDirtyPrev || pPage==p->pDirty );
    53    167     
    54    168       /* Update the PCache1.pSynced variable if necessary. */
    55    169       if( p->pSynced==pPage ){
    56         -      PgHdr *pSynced = pPage->pDirtyPrev;
    57         -      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
    58         -        pSynced = pSynced->pDirtyPrev;
    59         -      }
    60         -      p->pSynced = pSynced;
          170  +      p->pSynced = pPage->pDirtyPrev;
    61    171       }
    62    172     
    63    173       if( pPage->pDirtyNext ){
    64    174         pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
    65    175       }else{
    66    176         assert( pPage==p->pDirtyTail );
    67    177         p->pDirtyTail = pPage->pDirtyPrev;
    68    178       }
    69    179       if( pPage->pDirtyPrev ){
    70    180         pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
    71    181       }else{
          182  +      /* If there are now no dirty pages in the cache, set eCreate to 2. 
          183  +      ** This is an optimization that allows sqlite3PcacheFetch() to skip
          184  +      ** searching for a dirty page to eject from the cache when it might
          185  +      ** otherwise have to.  */
    72    186         assert( pPage==p->pDirty );
    73    187         p->pDirty = pPage->pDirtyNext;
    74         -      if( p->pDirty==0 && p->bPurgeable ){
    75         -        assert( p->eCreate==1 );
          188  +      assert( p->bPurgeable || p->eCreate==2 );
          189  +      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
          190  +        assert( p->bPurgeable==0 || p->eCreate==1 );
    76    191           p->eCreate = 2;
    77    192         }
    78    193       }
    79    194       pPage->pDirtyNext = 0;
    80    195       pPage->pDirtyPrev = 0;
    81    196     }
    82    197     if( addRemove & PCACHE_DIRTYLIST_ADD ){
................................................................................
    90    205         p->pDirtyTail = pPage;
    91    206         if( p->bPurgeable ){
    92    207           assert( p->eCreate==2 );
    93    208           p->eCreate = 1;
    94    209         }
    95    210       }
    96    211       p->pDirty = pPage;
    97         -    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
          212  +
          213  +    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
          214  +    ** pSynced to point to it. Checking the NEED_SYNC flag is an 
          215  +    ** optimization, as if pSynced points to a page with the NEED_SYNC
          216  +    ** flag set sqlite3PcacheFetchStress() searches through all newer 
          217  +    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
          218  +    if( !p->pSynced 
          219  +     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
          220  +    ){
    98    221         p->pSynced = pPage;
    99    222       }
   100    223     }
          224  +  pcacheDump(p);
   101    225   }
   102    226   
   103    227   /*
   104    228   ** Wrapper around the pluggable caches xUnpin method. If the cache is
   105    229   ** being used for an in-memory database, this function is a no-op.
   106    230   */
   107    231   static void pcacheUnpin(PgHdr *p){
   108    232     if( p->pCache->bPurgeable ){
          233  +    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
   109    234       sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
          235  +    pcacheDump(p->pCache);
   110    236     }
   111    237   }
   112    238   
   113    239   /*
   114    240   ** Compute the number of pages of cache requested.   p->szCache is the
   115    241   ** cache size requested by the "PRAGMA cache_size" statement.
   116    242   */
................................................................................
   172    298     p->szExtra = szExtra;
   173    299     p->bPurgeable = bPurgeable;
   174    300     p->eCreate = 2;
   175    301     p->xStress = xStress;
   176    302     p->pStress = pStress;
   177    303     p->szCache = 100;
   178    304     p->szSpill = 1;
          305  +  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
   179    306     return sqlite3PcacheSetPageSize(p, szPage);
   180    307   }
   181    308   
   182    309   /*
   183    310   ** Change the page size for PCache object. The caller must ensure that there
   184    311   ** are no outstanding page references when this function is called.
   185    312   */
................................................................................
   194    321       if( pNew==0 ) return SQLITE_NOMEM_BKPT;
   195    322       sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
   196    323       if( pCache->pCache ){
   197    324         sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   198    325       }
   199    326       pCache->pCache = pNew;
   200    327       pCache->szPage = szPage;
          328  +    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
   201    329     }
   202    330     return SQLITE_OK;
   203    331   }
   204    332   
   205    333   /*
   206    334   ** Try to obtain a page from the cache.
   207    335   **
................................................................................
   228    356   */
   229    357   sqlite3_pcache_page *sqlite3PcacheFetch(
   230    358     PCache *pCache,       /* Obtain the page from this cache */
   231    359     Pgno pgno,            /* Page number to obtain */
   232    360     int createFlag        /* If true, create page if it does not exist already */
   233    361   ){
   234    362     int eCreate;
          363  +  sqlite3_pcache_page *pRes;
   235    364   
   236    365     assert( pCache!=0 );
   237    366     assert( pCache->pCache!=0 );
   238    367     assert( createFlag==3 || createFlag==0 );
   239    368     assert( pgno>0 );
          369  +  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
   240    370   
   241    371     /* eCreate defines what to do if the page does not exist.
   242    372     **    0     Do not allocate a new page.  (createFlag==0)
   243    373     **    1     Allocate a new page if doing so is inexpensive.
   244    374     **          (createFlag==1 AND bPurgeable AND pDirty)
   245    375     **    2     Allocate a new page even it doing so is difficult.
   246    376     **          (createFlag==1 AND !(bPurgeable AND pDirty)
   247    377     */
   248    378     eCreate = createFlag & pCache->eCreate;
   249    379     assert( eCreate==0 || eCreate==1 || eCreate==2 );
   250    380     assert( createFlag==0 || pCache->eCreate==eCreate );
   251    381     assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
   252         -  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
          382  +  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
          383  +  pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
          384  +               createFlag?" create":"",pRes));
          385  +  return pRes;
   253    386   }
   254    387   
   255    388   /*
   256    389   ** If the sqlite3PcacheFetch() routine is unable to allocate a new
   257    390   ** page because no clean pages are available for reuse and the cache
   258    391   ** size limit has been reached, then this routine can be invoked to 
   259    392   ** try harder to allocate a page.  This routine might invoke the stress
................................................................................
   272    405     if( pCache->eCreate==2 ) return 0;
   273    406   
   274    407     if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
   275    408       /* Find a dirty page to write-out and recycle. First try to find a 
   276    409       ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   277    410       ** cleared), but if that is not possible settle for any other 
   278    411       ** unreferenced dirty page.
   279         -    */
          412  +    **
          413  +    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
          414  +    ** flag is currently referenced, then the following may leave pSynced
          415  +    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
          416  +    ** cleared). This is Ok, as pSynced is just an optimization.  */
   280    417       for(pPg=pCache->pSynced; 
   281    418           pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   282    419           pPg=pPg->pDirtyPrev
   283    420       );
   284    421       pCache->pSynced = pPg;
   285    422       if( !pPg ){
   286    423         for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
................................................................................
   290    427   #ifdef SQLITE_LOG_CACHE_SPILL
   291    428         sqlite3_log(SQLITE_FULL, 
   292    429                     "spill page %d making room for %d - cache used: %d/%d",
   293    430                     pPg->pgno, pgno,
   294    431                     sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
   295    432                   numberOfCachePages(pCache));
   296    433   #endif
          434  +      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
   297    435         rc = pCache->xStress(pCache->pStress, pPg);
          436  +      pcacheDump(pCache);
   298    437         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   299    438           return rc;
   300    439         }
   301    440       }
   302    441     }
   303    442     *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
   304    443     return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
................................................................................
   350    489     pPgHdr = (PgHdr *)pPage->pExtra;
   351    490   
   352    491     if( !pPgHdr->pPage ){
   353    492       return pcacheFetchFinishWithInit(pCache, pgno, pPage);
   354    493     }
   355    494     pCache->nRefSum++;
   356    495     pPgHdr->nRef++;
          496  +  assert( sqlite3PcachePageSanity(pPgHdr) );
   357    497     return pPgHdr;
   358    498   }
   359    499   
   360    500   /*
   361    501   ** Decrement the reference count on a page. If the page is clean and the
   362    502   ** reference count drops to 0, then it is made eligible for recycling.
   363    503   */
   364    504   void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
   365    505     assert( p->nRef>0 );
   366    506     p->pCache->nRefSum--;
   367    507     if( (--p->nRef)==0 ){
   368    508       if( p->flags&PGHDR_CLEAN ){
   369    509         pcacheUnpin(p);
   370         -    }else if( p->pDirtyPrev!=0 ){
   371         -      /* Move the page to the head of the dirty list. */
          510  +    }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
          511  +      /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
          512  +      ** then page p is already at the head of the dirty list and the
          513  +      ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
          514  +      ** tag above.  */
   372    515         pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   373    516       }
   374    517     }
   375    518   }
   376    519   
   377    520   /*
   378    521   ** Increase the reference count of a supplied page by 1.
   379    522   */
   380    523   void sqlite3PcacheRef(PgHdr *p){
   381    524     assert(p->nRef>0);
          525  +  assert( sqlite3PcachePageSanity(p) );
   382    526     p->nRef++;
   383    527     p->pCache->nRefSum++;
   384    528   }
   385    529   
   386    530   /*
   387    531   ** Drop a page from the cache. There must be exactly one reference to the
   388    532   ** page. This function deletes that reference, so after it returns the
   389    533   ** page pointed to by p is invalid.
   390    534   */
   391    535   void sqlite3PcacheDrop(PgHdr *p){
   392    536     assert( p->nRef==1 );
          537  +  assert( sqlite3PcachePageSanity(p) );
   393    538     if( p->flags&PGHDR_DIRTY ){
   394    539       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   395    540     }
   396    541     p->pCache->nRefSum--;
   397    542     sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
   398    543   }
   399    544   
   400    545   /*
   401    546   ** Make sure the page is marked as dirty. If it isn't dirty already,
   402    547   ** make it so.
   403    548   */
   404    549   void sqlite3PcacheMakeDirty(PgHdr *p){
   405    550     assert( p->nRef>0 );
   406         -  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){
          551  +  assert( sqlite3PcachePageSanity(p) );
          552  +  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
   407    553       p->flags &= ~PGHDR_DONT_WRITE;
   408    554       if( p->flags & PGHDR_CLEAN ){
   409    555         p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
          556  +      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
   410    557         assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
   411    558         pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
   412    559       }
          560  +    assert( sqlite3PcachePageSanity(p) );
   413    561     }
   414    562   }
   415    563   
   416    564   /*
   417    565   ** Make sure the page is marked as clean. If it isn't clean already,
   418    566   ** make it so.
   419    567   */
   420    568   void sqlite3PcacheMakeClean(PgHdr *p){
   421         -  if( (p->flags & PGHDR_DIRTY) ){
          569  +  assert( sqlite3PcachePageSanity(p) );
          570  +  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
   422    571       assert( (p->flags & PGHDR_CLEAN)==0 );
   423    572       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   424    573       p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
   425    574       p->flags |= PGHDR_CLEAN;
          575  +    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
          576  +    assert( sqlite3PcachePageSanity(p) );
   426    577       if( p->nRef==0 ){
   427    578         pcacheUnpin(p);
   428    579       }
   429    580     }
   430    581   }
   431    582   
   432    583   /*
   433    584   ** Make every page in the cache clean.
   434    585   */
   435    586   void sqlite3PcacheCleanAll(PCache *pCache){
   436    587     PgHdr *p;
          588  +  pcacheTrace(("%p.CLEAN-ALL\n",pCache));
   437    589     while( (p = pCache->pDirty)!=0 ){
   438    590       sqlite3PcacheMakeClean(p);
   439    591     }
   440    592   }
   441    593   
   442    594   /*
   443    595   ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
   444    596   */
   445    597   void sqlite3PcacheClearWritable(PCache *pCache){
   446    598     PgHdr *p;
          599  +  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
   447    600     for(p=pCache->pDirty; p; p=p->pDirtyNext){
   448    601       p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
   449    602     }
   450    603     pCache->pSynced = pCache->pDirtyTail;
   451    604   }
   452    605   
   453    606   /*
................................................................................
   464    617   /*
   465    618   ** Change the page number of page p to newPgno. 
   466    619   */
   467    620   void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
   468    621     PCache *pCache = p->pCache;
   469    622     assert( p->nRef>0 );
   470    623     assert( newPgno>0 );
          624  +  assert( sqlite3PcachePageSanity(p) );
          625  +  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
   471    626     sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
   472    627     p->pgno = newPgno;
   473    628     if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
   474    629       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   475    630     }
   476    631   }
   477    632   
................................................................................
   484    639   ** function is 0, then the data area associated with page 1 is zeroed, but
   485    640   ** the page object is not dropped.
   486    641   */
   487    642   void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
   488    643     if( pCache->pCache ){
   489    644       PgHdr *p;
   490    645       PgHdr *pNext;
          646  +    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
   491    647       for(p=pCache->pDirty; p; p=pNext){
   492    648         pNext = p->pDirtyNext;
   493    649         /* This routine never gets call with a positive pgno except right
   494    650         ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
   495    651         ** it must be that pgno==0.
   496    652         */
   497    653         assert( p->pgno>0 );
................................................................................
   514    670   }
   515    671   
   516    672   /*
   517    673   ** Close a cache.
   518    674   */
   519    675   void sqlite3PcacheClose(PCache *pCache){
   520    676     assert( pCache->pCache!=0 );
          677  +  pcacheTrace(("%p.CLOSE\n",pCache));
   521    678     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   522    679   }
   523    680   
   524    681   /* 
   525    682   ** Discard the contents of the cache.
   526    683   */
   527    684   void sqlite3PcacheClear(PCache *pCache){
   528    685     sqlite3PcacheTruncate(pCache, 0);
   529    686   }
   530    687   
   531    688   /*
   532    689   ** Merge two lists of pages connected by pDirty and in pgno order.
   533         -** Do not both fixing the pDirtyPrev pointers.
          690  +** Do not bother fixing the pDirtyPrev pointers.
   534    691   */
   535    692   static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
   536    693     PgHdr result, *pTail;
   537    694     pTail = &result;
   538         -  while( pA && pB ){
          695  +  assert( pA!=0 && pB!=0 );
          696  +  for(;;){
   539    697       if( pA->pgno<pB->pgno ){
   540    698         pTail->pDirty = pA;
   541    699         pTail = pA;
   542    700         pA = pA->pDirty;
          701  +      if( pA==0 ){
          702  +        pTail->pDirty = pB;
          703  +        break;
          704  +      }
   543    705       }else{
   544    706         pTail->pDirty = pB;
   545    707         pTail = pB;
   546    708         pB = pB->pDirty;
          709  +      if( pB==0 ){
          710  +        pTail->pDirty = pA;
          711  +        break;
          712  +      }
   547    713       }
   548    714     }
   549         -  if( pA ){
   550         -    pTail->pDirty = pA;
   551         -  }else if( pB ){
   552         -    pTail->pDirty = pB;
   553         -  }else{
   554         -    pTail->pDirty = 0;
   555         -  }
   556    715     return result.pDirty;
   557    716   }
   558    717   
   559    718   /*
   560    719   ** Sort the list of pages in accending order by pgno.  Pages are
   561    720   ** connected by pDirty pointers.  The pDirtyPrev pointers are
   562    721   ** corrupted by this sort.
................................................................................
   589    748         ** the input list.  But that is impossible.
   590    749         */
   591    750         a[i] = pcacheMergeDirtyList(a[i], p);
   592    751       }
   593    752     }
   594    753     p = a[0];
   595    754     for(i=1; i<N_SORT_BUCKET; i++){
   596         -    p = pcacheMergeDirtyList(p, a[i]);
          755  +    if( a[i]==0 ) continue;
          756  +    p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
   597    757     }
   598    758     return p;
   599    759   }
   600    760   
   601    761   /*
   602    762   ** Return a list of all dirty pages in the cache, sorted by page number.
   603    763   */

Changes to src/pcache.h.

    22     22   ** Every page in the cache is controlled by an instance of the following
    23     23   ** structure.
    24     24   */
    25     25   struct PgHdr {
    26     26     sqlite3_pcache_page *pPage;    /* Pcache object page handle */
    27     27     void *pData;                   /* Page data */
    28     28     void *pExtra;                  /* Extra content */
    29         -  PgHdr *pDirty;                 /* Transient list of dirty pages */
           29  +  PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
    30     30     Pager *pPager;                 /* The pager this page is part of */
    31     31     Pgno pgno;                     /* Page number for this page */
    32     32   #ifdef SQLITE_CHECK_PAGES
    33     33     u32 pageHash;                  /* Hash of page content */
    34     34   #endif
    35     35     u16 flags;                     /* PGHDR flags defined below */
    36     36   
................................................................................
    47     47   
    48     48   /* Bit values for PgHdr.flags */
    49     49   #define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
    50     50   #define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
    51     51   #define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
    52     52   #define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
    53     53                                        ** writing this page to the database */
    54         -#define PGHDR_NEED_READ       0x010  /* Content is unread */
    55         -#define PGHDR_DONT_WRITE      0x020  /* Do not write content to disk */
    56         -#define PGHDR_MMAP            0x040  /* This is an mmap page object */
           54  +#define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
           55  +#define PGHDR_MMAP            0x020  /* This is an mmap page object */
    57     56   
    58         -#define PGHDR_WAL_APPEND      0x080  /* Appended to wal file */
           57  +#define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
    59     58   
    60     59   /* Initialize and shutdown the page cache subsystem */
    61     60   int sqlite3PcacheInitialize(void);
    62     61   void sqlite3PcacheShutdown(void);
    63     62   
    64     63   /* Page cache buffer management:
    65     64   ** These routines implement SQLITE_CONFIG_PAGECACHE.
................................................................................
   133    132   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   134    133   /* Iterate through all dirty pages currently stored in the cache. This
   135    134   ** interface is only available if SQLITE_CHECK_PAGES is defined when the 
   136    135   ** library is built.
   137    136   */
   138    137   void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
   139    138   #endif
          139  +
          140  +#if defined(SQLITE_DEBUG)
          141  +/* Check invariants on a PgHdr object */
          142  +int sqlite3PcachePageSanity(PgHdr*);
          143  +#endif
   140    144   
   141    145   /* Set and get the suggested cache-size for the specified pager-cache.
   142    146   **
   143    147   ** If no global maximum is configured, then the system attempts to limit
   144    148   ** the total number of pages cached by purgeable pager-caches to the sum
   145    149   ** of the suggested cache-sizes.
   146    150   */

Changes to src/pcache1.c.

   628    628   **
   629    629   ** The PCache mutex must be held when this function is called.
   630    630   */
   631    631   static void pcache1TruncateUnsafe(
   632    632     PCache1 *pCache,             /* The cache to truncate */
   633    633     unsigned int iLimit          /* Drop pages with this pgno or larger */
   634    634   ){
   635         -  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
   636         -  unsigned int h;
          635  +  TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
          636  +  unsigned int h, iStop;
   637    637     assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   638         -  for(h=0; h<pCache->nHash; h++){
   639         -    PgHdr1 **pp = &pCache->apHash[h]; 
          638  +  assert( pCache->iMaxKey >= iLimit );
          639  +  assert( pCache->nHash > 0 );
          640  +  if( pCache->iMaxKey - iLimit < pCache->nHash ){
          641  +    /* If we are just shaving the last few pages off the end of the
          642  +    ** cache, then there is no point in scanning the entire hash table.
          643  +    ** Only scan those hash slots that might contain pages that need to
          644  +    ** be removed. */
          645  +    h = iLimit % pCache->nHash;
          646  +    iStop = pCache->iMaxKey % pCache->nHash;
          647  +    TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
          648  +  }else{
          649  +    /* This is the general case where many pages are being removed.
          650  +    ** It is necessary to scan the entire hash table */
          651  +    h = pCache->nHash/2;
          652  +    iStop = h - 1;
          653  +  }
          654  +  for(;;){
          655  +    PgHdr1 **pp;
   640    656       PgHdr1 *pPage;
          657  +    assert( h<pCache->nHash );
          658  +    pp = &pCache->apHash[h]; 
   641    659       while( (pPage = *pp)!=0 ){
   642    660         if( pPage->iKey>=iLimit ){
   643    661           pCache->nPage--;
   644    662           *pp = pPage->pNext;
   645    663           if( !pPage->isPinned ) pcache1PinPage(pPage);
   646    664           pcache1FreePage(pPage);
   647    665         }else{
   648    666           pp = &pPage->pNext;
   649         -        TESTONLY( nPage++; )
          667  +        TESTONLY( if( nPage>=0 ) nPage++; )
   650    668         }
   651    669       }
          670  +    if( h==iStop ) break;
          671  +    h = (h+1) % pCache->nHash;
   652    672     }
   653         -  assert( pCache->nPage==nPage );
          673  +  assert( nPage<0 || pCache->nPage==(unsigned)nPage );
   654    674   }
   655    675   
   656    676   /******************************************************************************/
   657    677   /******** sqlite3_pcache Methods **********************************************/
   658    678   
   659    679   /*
   660    680   ** Implementation of the sqlite3_pcache.xInit method.
................................................................................
  1123   1143   ** Destroy a cache allocated using pcache1Create().
  1124   1144   */
  1125   1145   static void pcache1Destroy(sqlite3_pcache *p){
  1126   1146     PCache1 *pCache = (PCache1 *)p;
  1127   1147     PGroup *pGroup = pCache->pGroup;
  1128   1148     assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
  1129   1149     pcache1EnterMutex(pGroup);
  1130         -  pcache1TruncateUnsafe(pCache, 0);
         1150  +  if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
  1131   1151     assert( pGroup->nMaxPage >= pCache->nMax );
  1132   1152     pGroup->nMaxPage -= pCache->nMax;
  1133   1153     assert( pGroup->nMinPage >= pCache->nMin );
  1134   1154     pGroup->nMinPage -= pCache->nMin;
  1135   1155     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
  1136   1156     pcache1EnforceMaxPage(pCache);
  1137   1157     pcache1LeaveMutex(pGroup);

Changes to src/pragma.c.

  1026   1026           if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
  1027   1027         }
  1028   1028   
  1029   1029         /* Many of the flag-pragmas modify the code generated by the SQL 
  1030   1030         ** compiler (eg. count_changes). So add an opcode to expire all
  1031   1031         ** compiled SQL statements after modifying a pragma value.
  1032   1032         */
  1033         -      sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
         1033  +      sqlite3VdbeAddOp0(v, OP_Expire);
  1034   1034         setAllPagerFlags(db);
  1035   1035       }
  1036   1036       break;
  1037   1037     }
  1038   1038   #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
  1039   1039   
  1040   1040   #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
................................................................................
  1048   1048     ** name:       Column name
  1049   1049     ** type:       Column declaration type.
  1050   1050     ** notnull:    True if 'NOT NULL' is part of column declaration
  1051   1051     ** dflt_value: The default value for the column, if any.
  1052   1052     */
  1053   1053     case PragTyp_TABLE_INFO: if( zRight ){
  1054   1054       Table *pTab;
  1055         -    pTab = sqlite3FindTable(db, zRight, zDb);
         1055  +    pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
  1056   1056       if( pTab ){
  1057   1057         static const char *azCol[] = {
  1058   1058            "cid", "name", "type", "notnull", "dflt_value", "pk"
  1059   1059         };
  1060   1060         int i, k;
  1061   1061         int nHidden = 0;
  1062   1062         Column *pCol;
................................................................................
  1330   1330           if( pParent && pIdx==0 ){
  1331   1331             int iKey = pFK->aCol[0].iFrom;
  1332   1332             assert( iKey>=0 && iKey<pTab->nCol );
  1333   1333             if( iKey!=pTab->iPKey ){
  1334   1334               sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
  1335   1335               sqlite3ColumnDefault(v, pTab, iKey, regRow);
  1336   1336               sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
  1337         -            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 
  1338         -               sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
  1339   1337             }else{
  1340   1338               sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
  1341   1339             }
  1342         -          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
         1340  +          sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v);
  1343   1341             sqlite3VdbeGoto(v, addrOk);
  1344   1342             sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
  1345   1343           }else{
  1346   1344             for(j=0; j<pFK->nCol; j++){
  1347   1345               sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
  1348   1346                               aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
  1349   1347               sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);

Changes to src/printf.c.

    11     11   */
    12     12   #include "sqliteInt.h"
    13     13   
    14     14   /*
    15     15   ** Conversion types fall into various categories as defined by the
    16     16   ** following enumeration.
    17     17   */
    18         -#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
    19         -#define etFLOAT       2 /* Floating point.  %f */
    20         -#define etEXP         3 /* Exponentional notation. %e and %E */
    21         -#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
    22         -#define etSIZE        5 /* Return number of characters processed so far. %n */
    23         -#define etSTRING      6 /* Strings. %s */
    24         -#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
    25         -#define etPERCENT     8 /* Percent symbol. %% */
    26         -#define etCHARX       9 /* Characters. %c */
           18  +#define etRADIX       0 /* Integer types.  %d, %x, %o, and so forth */
           19  +#define etFLOAT       1 /* Floating point.  %f */
           20  +#define etEXP         2 /* Exponentional notation. %e and %E */
           21  +#define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
           22  +#define etSIZE        4 /* Return number of characters processed so far. %n */
           23  +#define etSTRING      5 /* Strings. %s */
           24  +#define etDYNSTRING   6 /* Dynamically allocated strings. %z */
           25  +#define etPERCENT     7 /* Percent symbol. %% */
           26  +#define etCHARX       8 /* Characters. %c */
    27     27   /* The rest are extensions, not normally found in printf() */
    28         -#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
    29         -#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
           28  +#define etSQLESCAPE   9 /* Strings with '\'' doubled.  %q */
           29  +#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
    30     30                             NULL pointers replaced by SQL NULL.  %Q */
    31         -#define etTOKEN      12 /* a pointer to a Token structure */
    32         -#define etSRCLIST    13 /* a pointer to a SrcList */
    33         -#define etPOINTER    14 /* The %p conversion */
    34         -#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
    35         -#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
           31  +#define etTOKEN      11 /* a pointer to a Token structure */
           32  +#define etSRCLIST    12 /* a pointer to a SrcList */
           33  +#define etPOINTER    13 /* The %p conversion */
           34  +#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
           35  +#define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
    36     36   
    37         -#define etINVALID     0 /* Any unrecognized conversion type */
           37  +#define etINVALID    16 /* Any unrecognized conversion type */
    38     38   
    39     39   
    40     40   /*
    41     41   ** An "etByte" is an 8-bit unsigned value.
    42     42   */
    43     43   typedef unsigned char etByte;
    44     44   
................................................................................
   185    185     etByte flag_blanksign;     /* True if " " flag is present */
   186    186     etByte flag_alternateform; /* True if "#" flag is present */
   187    187     etByte flag_altform2;      /* True if "!" flag is present */
   188    188     etByte flag_zeropad;       /* True if field width constant starts with zero */
   189    189     etByte flag_long;          /* True if "l" flag is present */
   190    190     etByte flag_longlong;      /* True if the "ll" flag is present */
   191    191     etByte done;               /* Loop termination flag */
   192         -  etByte xtype = 0;          /* Conversion paradigm */
          192  +  etByte xtype = etINVALID;  /* Conversion paradigm */
   193    193     u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
   194    194     u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
   195    195     char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
   196    196     sqlite_uint64 longvalue;   /* Value for integer types */
   197    197     LONGDOUBLE_TYPE realvalue; /* Value for real types */
   198    198     const et_info *infop;      /* Pointer to the appropriate info structure */
   199    199     char *zOut;                /* Rendering buffer */

Changes to src/resolve.c.

   714    714             notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr);
   715    715           }
   716    716         }
   717    717         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
   718    718           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
   719    719           pNC->nErr++;
   720    720           is_agg = 0;
   721         -      }else if( no_such_func && pParse->db->init.busy==0 ){
          721  +      }else if( no_such_func && pParse->db->init.busy==0
          722  +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
          723  +                && pParse->explain==0
          724  +#endif
          725  +      ){
   722    726           sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   723    727           pNC->nErr++;
   724    728         }else if( wrong_num_args ){
   725    729           sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
   726    730                nId, zId);
   727    731           pNC->nErr++;
   728    732         }
................................................................................
   759    763         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   760    764           int nRef = pNC->nRef;
   761    765           notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
   762    766           sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
   763    767           assert( pNC->nRef>=nRef );
   764    768           if( nRef!=pNC->nRef ){
   765    769             ExprSetProperty(pExpr, EP_VarSelect);
          770  +          pNC->ncFlags |= NC_VarSelect;
   766    771           }
   767    772         }
   768    773         break;
   769    774       }
   770    775       case TK_VARIABLE: {
   771    776         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
   772    777         break;

Changes to src/rowset.c.

   238    238     struct RowSetEntry *pA,    /* First sorted list to be merged */
   239    239     struct RowSetEntry *pB     /* Second sorted list to be merged */
   240    240   ){
   241    241     struct RowSetEntry head;
   242    242     struct RowSetEntry *pTail;
   243    243   
   244    244     pTail = &head;
   245         -  while( pA && pB ){
          245  +  assert( pA!=0 && pB!=0 );
          246  +  for(;;){
   246    247       assert( pA->pRight==0 || pA->v<=pA->pRight->v );
   247    248       assert( pB->pRight==0 || pB->v<=pB->pRight->v );
   248         -    if( pA->v<pB->v ){
   249         -      pTail->pRight = pA;
          249  +    if( pA->v<=pB->v ){
          250  +      if( pA->v<pB->v ) pTail = pTail->pRight = pA;
   250    251         pA = pA->pRight;
   251         -      pTail = pTail->pRight;
   252         -    }else if( pB->v<pA->v ){
   253         -      pTail->pRight = pB;
   254         -      pB = pB->pRight;
   255         -      pTail = pTail->pRight;
          252  +      if( pA==0 ){
          253  +        pTail->pRight = pB;
          254  +        break;
          255  +      }
   256    256       }else{
   257         -      pA = pA->pRight;
          257  +      pTail = pTail->pRight = pB;
          258  +      pB = pB->pRight;
          259  +      if( pB==0 ){
          260  +        pTail->pRight = pA;
          261  +        break;
          262  +      }
   258    263       }
   259    264     }
   260         -  if( pA ){
   261         -    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
   262         -    pTail->pRight = pA;
   263         -  }else{
   264         -    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
   265         -    pTail->pRight = pB;
   266         -  }
   267    265     return head.pRight;
   268    266   }
   269    267   
   270    268   /*
   271    269   ** Sort all elements on the list of RowSetEntry objects into order of
   272    270   ** increasing v.
   273    271   */ 
................................................................................
   282    280       for(i=0; aBucket[i]; i++){
   283    281         pIn = rowSetEntryMerge(aBucket[i], pIn);
   284    282         aBucket[i] = 0;
   285    283       }
   286    284       aBucket[i] = pIn;
   287    285       pIn = pNext;
   288    286     }
   289         -  pIn = 0;
   290         -  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
   291         -    pIn = rowSetEntryMerge(pIn, aBucket[i]);
          287  +  pIn = aBucket[0];
          288  +  for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
          289  +    if( aBucket[i]==0 ) continue;
          290  +    pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
   292    291     }
   293    292     return pIn;
   294    293   }
   295    294   
   296    295   
   297    296   /*
   298    297   ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.

Changes to src/select.c.

    52     52     int nOBSat;           /* Number of ORDER BY terms satisfied by indices */
    53     53     int iECursor;         /* Cursor number for the sorter */
    54     54     int regReturn;        /* Register holding block-output return address */
    55     55     int labelBkOut;       /* Start label for the block-output subroutine */
    56     56     int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
    57     57     int labelDone;        /* Jump here when done, ex: LIMIT reached */
    58     58     u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
           59  +  u8 bOrderedInnerLoop; /* ORDER BY correctly sorts the inner loop */
    59     60   };
    60     61   #define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
    61     62   
    62     63   /*
    63     64   ** Delete all the content of a Select structure.  Deallocate the structure
    64     65   ** itself only if bFree is true.
    65     66   */
................................................................................
   585    586       op = OP_SorterInsert;
   586    587     }else{
   587    588       op = OP_IdxInsert;
   588    589     }
   589    590     sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
   590    591     if( iLimit ){
   591    592       int addr;
          593  +    int r1 = 0;
          594  +    /* Fill the sorter until it contains LIMIT+OFFSET entries.  (The iLimit
          595  +    ** register is initialized with value of LIMIT+OFFSET.)  After the sorter
          596  +    ** fills up, delete the least entry in the sorter after each insert.
          597  +    ** Thus we never hold more than the LIMIT+OFFSET rows in memory at once */
   592    598       addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, 1); VdbeCoverage(v);
   593    599       sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
          600  +    if( pSort->bOrderedInnerLoop ){
          601  +      r1 = ++pParse->nMem;
          602  +      sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1);
          603