/ Check-in [fd5676fe]
Login

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

Overview
Comment:Merge all recent enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA3-256: fd5676fe7f55b3d4fa15ca119af7c064c6f9d053affdfd5e748785e300affbeb
User & Date: drh 2017-03-24 15:09:47
Wiki:begin-concurrent
Context
2017-03-30
20:35
Merge changes from the 3.18.0 release. check-in: 785c37d9 user: drh tags: begin-concurrent
2017-03-24
15:09
Merge all recent enhancements from trunk. check-in: fd5676fe user: drh tags: begin-concurrent
13:31
Add the RFC-7396 Appendix A test cases for json_patch(). check-in: c5441d2d user: drh tags: trunk
2017-01-09
07:00
Merge the "changebatch" functionality into this branch. check-in: 50fb1eb3 user: dan tags: begin-concurrent
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   562    562   #
   563    563   SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4
   564    564   # SHELL_OPT += -DSQLITE_ENABLE_FTS5
   565    565   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   566    566   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   567    567   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   568    568   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
          569  +FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   569    570   FUZZCHECK_SRC = $(TOP)/test/fuzzcheck.c $(TOP)/test/ossfuzz.c
   570    571   DBFUZZ_OPT = 
   571    572   
   572    573   # This is the default Makefile target.  The objects listed here
   573    574   # are what get build when you type just "make" with no arguments.
   574    575   #
   575    576   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
................................................................................
   616    617   	$(LTLINK) -o $@ $(FUZZERSHELL_OPT) \
   617    618   	  $(TOP)/tool/fuzzershell.c sqlite3.c $(TLIBS)
   618    619   
   619    620   fuzzcheck$(TEXE):	$(FUZZCHECK_SRC) sqlite3.c sqlite3.h
   620    621   	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) sqlite3.c $(TLIBS)
   621    622   
   622    623   ossshell$(TEXE):	$(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c sqlite3.h
   623         -	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c sqlite3.c sqlite3.h $(TLIBS)
          624  +	$(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c \
          625  +             $(TOP)/test/ossfuzz.c sqlite3.c $(TLIBS)
   624    626   
   625    627   dbfuzz$(TEXE):	$(TOP)/test/dbfuzz.c sqlite3.c sqlite3.h
   626    628   	$(LTLINK) -o $@ $(DBFUZZ_OPT) $(TOP)/test/dbfuzz.c sqlite3.c $(TLIBS)
   627    629   
   628    630   mptester$(TEXE):	sqlite3.lo $(TOP)/mptest/mptest.c
   629    631   	$(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.lo \
   630    632   		$(TLIBS) -rpath "$(libdir)"
................................................................................
  1149   1151   	echo "static const char *zMainloop = " >> $@
  1150   1152   	$(TCLSH_CMD) $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
  1151   1153   	echo "; return zMainloop; }" >> $@
  1152   1154   
  1153   1155   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1154   1156   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
  1155   1157   
         1158  +dbdump$(TEXE): $(TOP)/ext/misc/dbdump.c sqlite3.lo
         1159  +	$(LTLINK) -DDBDUMP_STANDALONE -o $@ \
         1160  +           $(TOP)/ext/misc/dbdump.c sqlite3.lo $(TLIBS)
         1161  +
  1156   1162   showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.lo
  1157   1163   	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.lo $(TLIBS)
  1158   1164   
  1159   1165   showstat4$(TEXE):	$(TOP)/tool/showstat4.c sqlite3.lo
  1160   1166   	$(LTLINK) -o $@ $(TOP)/tool/showstat4.c sqlite3.lo $(TLIBS)
  1161   1167   
  1162   1168   showjournal$(TEXE):	$(TOP)/tool/showjournal.c sqlite3.lo
................................................................................
  1173   1179   
  1174   1180   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h
  1175   1181   	$(LTLINK) -I. -o $@ $(TOP)/tool/logest.c
  1176   1182   
  1177   1183   wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.lo
  1178   1184   	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.lo $(TLIBS)
  1179   1185   
  1180         -speedtest1$(TEXE):	$(TOP)/test/speedtest1.c sqlite3.lo
  1181         -	$(LTLINK) -o $@ $(TOP)/test/speedtest1.c sqlite3.lo $(TLIBS)
         1186  +speedtest1$(TEXE):	$(TOP)/test/speedtest1.c sqlite3.c
         1187  +	$(LTLINK) $(ST_OPT) -o $@ $(TOP)/test/speedtest1.c sqlite3.c $(TLIBS)
         1188  +
         1189  +KV_OPT += -DSQLITE_DIRECT_OVERFLOW_READ
         1190  +
         1191  +kvtest$(TEXE):	$(TOP)/test/kvtest.c sqlite3.c
         1192  +	$(LTLINK) $(KV_OPT) -o $@ $(TOP)/test/kvtest.c sqlite3.c $(TLIBS)
  1182   1193   
  1183   1194   rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.lo 
  1184   1195   	$(LTLINK) -I. -o $@ $(TOP)/ext/rbu/rbu.c sqlite3.lo $(TLIBS)
  1185   1196   
  1186   1197   loadfts$(EXE): $(TOP)/tool/loadfts.c libsqlite3.la
  1187   1198   	$(LTLINK) $(TOP)/tool/loadfts.c libsqlite3.la -o $@ $(TLIBS)
  1188   1199   

Changes to Makefile.msc.

    19     19   # <</mark>>
    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 treating warnings as errors (-WX, etc) when
           28  +# compiling.
           29  +#
           30  +!IFNDEF USE_FATAL_WARN
           31  +USE_FATAL_WARN = 0
           32  +!ENDIF
    26     33   
    27     34   # Set this non-0 to enable full runtime error checks (-RTC1, etc).  This
    28     35   # has no effect if (any) optimizations are enabled.
    29     36   #
    30     37   !IFNDEF USE_RUNTIME_CHECKS
    31     38   USE_RUNTIME_CHECKS = 0
    32     39   !ENDIF
................................................................................
   488    495   # same unless your are cross-compiling.)
   489    496   #
   490    497   !IF $(USE_FULLWARN)!=0
   491    498   TCC = $(CC) -nologo -W4 -DINCLUDE_MSVC_H=1 $(CCOPTS) $(TCCOPTS)
   492    499   !ELSE
   493    500   TCC = $(CC) -nologo -W3 $(CCOPTS) $(TCCOPTS)
   494    501   !ENDIF
          502  +
          503  +# Check if warnings should be treated as errors when compiling.
          504  +#
          505  +!IF $(USE_FATAL_WARN)!=0
          506  +TCC = $(TCC) -WX
          507  +!ENDIF
   495    508   
   496    509   TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src -fp:precise
   497    510   RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src $(RCOPTS) $(RCCOPTS)
   498    511   
   499    512   # Check if we want to use the "stdcall" calling convention when compiling.
   500    513   # This is not supported by the compilers for non-x86 platforms.  It should
   501    514   # also be noted here that building any target with these "stdcall" options
................................................................................
   728    741   !IF $(DEBUG)>0 || $(API_ARMOR)!=0 || $(FOR_WIN10)!=0
   729    742   TCC = $(TCC) -DSQLITE_ENABLE_API_ARMOR=1
   730    743   RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1
   731    744   !ENDIF
   732    745   
   733    746   !IF $(DEBUG)>2
   734    747   TCC = $(TCC) -DSQLITE_DEBUG=1
          748  +TCC = $(TCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE
   735    749   RCC = $(RCC) -DSQLITE_DEBUG=1
   736    750   !ENDIF
   737    751   
   738    752   !IF $(DEBUG)>4 || $(OSTRACE)!=0
   739    753   TCC = $(TCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   740    754   RCC = $(RCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   741    755   !ENDIF
................................................................................
  1492   1506   !ENDIF
  1493   1507   
  1494   1508   # <<mark>>
  1495   1509   # Extra compiler options for various test tools.
  1496   1510   #
  1497   1511   MPTESTER_COMPILE_OPTS = -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS5
  1498   1512   FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1
  1499         -FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
         1513  +FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ -DSQLITE_MAX_MEMORY=50000000
  1500   1514   FUZZCHECK_SRC = $(TOP)\test\fuzzcheck.c $(TOP)\test\ossfuzz.c
  1501   1515   OSSSHELL_SRC = $(TOP)\test\ossshell.c $(TOP)\test\ossfuzz.c
         1516  +DBFUZZ_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION
         1517  +KV_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
         1518  +DBSELFTEST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
         1519  +ST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0
  1502   1520   
  1503   1521   # Standard options to testfixture.
  1504   1522   #
  1505   1523   TESTOPTS = --verbose=file --output=test-out.txt
  1506   1524   
  1507   1525   # Extra targets for the "all" target that require Tcl.
  1508   1526   #
................................................................................
  1537   1555   $(SQLITE3DLL):	$(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP)
  1538   1556   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL $(CORE_LINK_OPTS) /OUT:$@ $(LIBOBJ) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1539   1557   
  1540   1558   # <<block2>>
  1541   1559   sqlite3.def:	libsqlite3.lib
  1542   1560   	echo EXPORTS > sqlite3.def
  1543   1561   	dumpbin /all libsqlite3.lib \
  1544         -		| $(TCLSH_CMD) $(TOP)\tool\replace.tcl include "^\s+1 _?(sqlite3_[^@]*)(?:@\d+)?$$" \1 \
         1562  +		| $(TCLSH_CMD) $(TOP)\tool\replace.tcl include "^\s+1 _?(sqlite3(?:session|changeset)?_[^@]*)(?:@\d+)?$$" \1 \
  1545   1563   		| sort >> sqlite3.def
  1546   1564   # <</block2>>
  1547   1565   
  1548   1566   $(SQLITE3EXE):	$(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
  1549   1567   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c $(SHELL_CORE_SRC) \
  1550   1568   		/link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1551   1569   
................................................................................
  1564   1582   
  1565   1583   sourcetest:	srcck1.exe sqlite3.c
  1566   1584   	srcck1.exe sqlite3.c
  1567   1585   
  1568   1586   fuzzershell.exe:	$(TOP)\tool\fuzzershell.c $(SQLITE3C) $(SQLITE3H)
  1569   1587   	$(LTLINK) $(NO_WARN) $(FUZZERSHELL_COMPILE_OPTS) $(TOP)\tool\fuzzershell.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1570   1588   
         1589  +dbfuzz.exe:	$(TOP)\test\dbfuzz.c $(SQLITE3C) $(SQLITE3H)
         1590  +	$(LTLINK) $(NO_WARN) $(DBFUZZ_COMPILE_OPTS) $(TOP)\test\dbfuzz.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         1591  +
  1571   1592   fuzzcheck.exe:	$(FUZZCHECK_SRC) $(SQLITE3C) $(SQLITE3H)
  1572   1593   	$(LTLINK) $(NO_WARN) $(FUZZCHECK_COMPILE_OPTS) $(FUZZCHECK_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1573   1594   
  1574   1595   ossshell.exe:	$(OSSSHELL_SRC) $(SQLITE3C) $(SQLITE3H)
  1575   1596   	$(LTLINK) $(NO_WARN) $(FUZZCHECK_COMPILE_OPTS) $(OSSSHELL_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1576   1597   
  1577   1598   mptester.exe:	$(TOP)\mptest\mptest.c $(SQLITE3C) $(SQLITE3H)
................................................................................
  2153   2174   	$(TCLSH_CMD) $(TOP)\tool\tostr.tcl $(TOP)\tool\spaceanal.tcl >> $@
  2154   2175   	echo ; return zMainloop; } >> $@
  2155   2176   
  2156   2177   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  2157   2178   	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_analyzer.c \
  2158   2179   		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  2159   2180   
         2181  +dbdump.exe:	$(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H)
         2182  +	$(LTLINK) $(NO_WARN) -DDBDUMP_STANDALONE $(TOP)\ext\misc\dbdump.c $(SQLITE3C) \
         2183  +		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS)
         2184  +
  2160   2185   testloadext.lo:	$(TOP)\src\test_loadext.c
  2161   2186   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  2162   2187   
  2163   2188   testloadext.dll:	testloadext.lo
  2164   2189   	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo
  2165   2190   
  2166   2191   showdb.exe:	$(TOP)\tool\showdb.c $(SQLITE3C) $(SQLITE3H)
  2167         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2192  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2168   2193   		$(TOP)\tool\showdb.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2169   2194   
  2170   2195   showstat4.exe:	$(TOP)\tool\showstat4.c $(SQLITE3C) $(SQLITE3H)
  2171         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2196  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2172   2197   		$(TOP)\tool\showstat4.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2173   2198   
  2174   2199   showjournal.exe:	$(TOP)\tool\showjournal.c $(SQLITE3C) $(SQLITE3H)
  2175         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2200  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2176   2201   		$(TOP)\tool\showjournal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2177   2202   
  2178   2203   showwal.exe:	$(TOP)\tool\showwal.c $(SQLITE3C) $(SQLITE3H)
  2179         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2204  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2180   2205   		$(TOP)\tool\showwal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2181   2206   
  2182         -changeset.exe:	$(TOP)\ext\session\changeset.c $(SQLITE3C)
  2183         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2207  +changeset.exe:	$(TOP)\ext\session\changeset.c $(SQLITE3C) $(SQLITE3H)
         2208  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
         2209  +		-DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
  2184   2210   		$(TOP)\ext\session\changeset.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2185   2211   
  2186   2212   fts3view.exe:	$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) $(SQLITE3H)
  2187         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2213  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2188   2214   		$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2189   2215   
  2190   2216   rollback-test.exe:	$(TOP)\tool\rollback-test.c $(SQLITE3C) $(SQLITE3H)
  2191         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2217  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2192   2218   		$(TOP)\tool\rollback-test.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2193   2219   
  2194   2220   LogEst.exe:	$(TOP)\tool\logest.c $(SQLITE3H)
  2195         -	$(LTLINK) $(NO_WARN) -Fe$@ $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS)
         2221  +	$(LTLINK) $(NO_WARN) $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS)
  2196   2222   
  2197   2223   wordcount.exe:	$(TOP)\test\wordcount.c $(SQLITE3C) $(SQLITE3H)
  2198         -	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2224  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2199   2225   		$(TOP)\test\wordcount.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2200   2226   
  2201   2227   speedtest1.exe:	$(TOP)\test\speedtest1.c $(SQLITE3C) $(SQLITE3H)
  2202         -	$(LTLINK) $(NO_WARN) -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \
         2228  +	$(LTLINK) $(NO_WARN) $(ST_COMPILE_OPTS) -DSQLITE_OMIT_LOAD_EXTENSION \
  2203   2229   		$(TOP)\test\speedtest1.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2204   2230   
         2231  +kvtest.exe:	$(TOP)\test\kvtest.c $(SQLITE3C) $(SQLITE3H)
         2232  +	$(LTLINK) $(NO_WARN) $(KV_COMPILE_OPTS) \
         2233  +		$(TOP)\test\kvtest.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         2234  +
         2235  +dbselftest.exe:	$(TOP)\test\dbselftest.c $(SQLITE3C) $(SQLITE3H)
         2236  +	$(LTLINK) $(NO_WARN) $(DBSELFTEST_COMPILE_OPTS) $(TOP)\test\dbselftest.c $(SQLITE3C)
         2237  +
  2205   2238   rbu.exe:	$(TOP)\ext\rbu\rbu.c $(TOP)\ext\rbu\sqlite3rbu.c $(SQLITE3C) $(SQLITE3H)
  2206         -	$(LTLINK) $(NO_WARN) -DSQLITE_ENABLE_RBU -Fe$@ \
         2239  +	$(LTLINK) $(NO_WARN) -DSQLITE_ENABLE_RBU \
  2207   2240   		$(TOP)\ext\rbu\rbu.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2208   2241   
  2209   2242   moreclean:	clean
  2210   2243   	del /Q $(SQLITE3C) $(SQLITE3H) 2>NUL
  2211   2244   # <</mark>>
  2212   2245   
  2213   2246   clean:
................................................................................
  2223   2256   	-rmdir /Q/S .deps 2>NUL
  2224   2257   	-rmdir /Q/S .libs 2>NUL
  2225   2258   	-rmdir /Q/S tsrc 2>NUL
  2226   2259   	del /Q .target_source 2>NUL
  2227   2260   	del /Q tclsqlite3.exe $(SQLITETCLH) $(SQLITETCLDECLSH) 2>NUL
  2228   2261   	del /Q testloadext.dll 2>NUL
  2229   2262   	del /Q testfixture.exe test.db 2>NUL
  2230         -	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe 2>NUL
         2263  +	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe dbdump.exe 2>NUL
  2231   2264   	del /Q changeset.exe 2>NUL
  2232   2265   	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL
  2233   2266   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2234   2267   	del /Q sqlite3.c sqlite3-*.c 2>NUL
  2235   2268   	del /Q sqlite3rc.h 2>NUL
  2236   2269   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2237   2270   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2238   2271   	del /Q sqlite-*-output.vsix 2>NUL
  2239   2272   	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2240   2273   	del /Q fts5.* fts5parse.* 2>NUL
  2241   2274   # <</mark>>

Changes to README.md.

     4      4   engine.  Some test scripts are also include.  However, many other test scripts
     5      5   and most of the documentation are managed separately.
     6      6   
     7      7   If you are reading this on a Git mirror someplace, you are doing it wrong.
     8      8   The [official repository](https://www.sqlite.org/src/) is better.  Go there
     9      9   now.
    10     10   
           11  +## Obtaining The Code
           12  +
           13  +SQLite sources are managed using the
           14  +[Fossil](https://www.fossil-scm.org/), a distributed version control system
           15  +that was specifically designed to support SQLite development.
           16  +If you do not want to use Fossil, you can download tarballs or ZIP
           17  +archives as follows:
           18  +
           19  +  *  Lastest trunk check-in:
           20  +     <https://www.sqlite.org/src/tarball/sqlite.tar.gz> or
           21  +     <https://www.sqlite.org/src/zip/sqlite.zip>.
           22  +
           23  +  *  Latest release:
           24  +     <https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release> or
           25  +     <https://www.sqlite.org/src/zip/sqlite.zip?r=release>.
           26  +
           27  +  *  For other check-ins, substitute an appropriate branch name or
           28  +     tag or hash prefix for "release" in the URLs of the previous
           29  +     bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
           30  +     to locate the check-in desired, click on its information page link,
           31  +     then click on the "Tarball" or "ZIP Archive" links on the information
           32  +     page.
           33  +
           34  +If you do want to use Fossil to check out the source tree, 
           35  +first install Fossil version 2.0 or later.
           36  +(Source tarballs and precompiled binaries available
           37  +[here](https://www.fossil-scm.org/fossil/uv/download.html).)
           38  +Then run commands like this:
           39  +
           40  +        mkdir ~/sqlite
           41  +        cd ~/sqlite
           42  +        fossil clone https://www.sqlite.org/src sqlite.fossil
           43  +        fossil open sqlite.fossil
           44  +    
           45  +After setting up a repository using the steps above, you can always
           46  +update to the lastest version using:
           47  +
           48  +        fossil update trunk   ;# latest trunk check-in
           49  +        fossil update release ;# latest official release
           50  +
           51  +Or type "fossil ui" to get a web-based user interface.
           52  +
    11     53   ## Compiling
    12     54   
    13     55   First create a directory in which to place
    14     56   the build products.  It is recommended, but not required, that the
    15     57   build directory be separate from the source directory.  Cd into the
    16     58   build directory and then from the build directory run the configure
    17     59   script found at the root of the source tree.  Then run "make".
    18     60   
    19     61   For example:
    20     62   
    21         -    tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
    22         -    mkdir bld                ;#  Build will occur in a sibling directory
    23         -    cd bld                   ;#  Change to the build directory
    24         -    ../sqlite/configure      ;#  Run the configure script
    25         -    make                     ;#  Run the makefile.
    26         -    make sqlite3.c           ;#  Build the "amalgamation" source file
    27         -    make test                ;#  Run some tests (requires Tcl)
           63  +        tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
           64  +        mkdir bld                ;#  Build will occur in a sibling directory
           65  +        cd bld                   ;#  Change to the build directory
           66  +        ../sqlite/configure      ;#  Run the configure script
           67  +        make                     ;#  Run the makefile.
           68  +        make sqlite3.c           ;#  Build the "amalgamation" source file
           69  +        make test                ;#  Run some tests (requires Tcl)
    28     70   
    29     71   See the makefile for additional targets.
    30     72   
    31     73   The configure script uses autoconf 2.61 and libtool.  If the configure
    32     74   script does not work out for you, there is a generic makefile named
    33     75   "Makefile.linux-gcc" in the top directory of the source tree that you
    34     76   can copy and edit to suit your needs.  Comments on the generic makefile
................................................................................
    39     81   On Windows, all applicable build products can be compiled with MSVC.
    40     82   First open the command prompt window associated with the desired compiler
    41     83   version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
    42     84   with the provided "Makefile.msc" to build one of the supported targets.
    43     85   
    44     86   For example:
    45     87   
    46         -    mkdir bld
    47         -    cd bld
    48         -    nmake /f Makefile.msc TOP=..\sqlite
    49         -    nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
    50         -    nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
    51         -    nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
    52         -    nmake /f Makefile.msc test TOP=..\sqlite
           88  +        mkdir bld
           89  +        cd bld
           90  +        nmake /f Makefile.msc TOP=..\sqlite
           91  +        nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
           92  +        nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
           93  +        nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
           94  +        nmake /f Makefile.msc test TOP=..\sqlite
    53     95   
    54     96   There are several build options that can be set via the NMAKE command
    55     97   line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
    56     98   to the "sqlite3.dll" command line above.  When debugging into the SQLite
    57     99   code, adding the "DEBUG=1" argument to one of the above command lines is
    58    100   recommended.
    59    101   

Changes to VERSION.

     1         -3.17.0
            1  +3.18.0

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 treating warnings as errors (-WX, etc) when
           28  +# compiling.
           29  +#
           30  +!IFNDEF USE_FATAL_WARN
           31  +USE_FATAL_WARN = 0
           32  +!ENDIF
    26     33   
    27     34   # Set this non-0 to enable full runtime error checks (-RTC1, etc).  This
    28     35   # has no effect if (any) optimizations are enabled.
    29     36   #
    30     37   !IFNDEF USE_RUNTIME_CHECKS
    31     38   USE_RUNTIME_CHECKS = 0
    32     39   !ENDIF
................................................................................
   449    456   # same unless your are cross-compiling.)
   450    457   #
   451    458   !IF $(USE_FULLWARN)!=0
   452    459   TCC = $(CC) -nologo -W4 -DINCLUDE_MSVC_H=1 $(CCOPTS) $(TCCOPTS)
   453    460   !ELSE
   454    461   TCC = $(CC) -nologo -W3 $(CCOPTS) $(TCCOPTS)
   455    462   !ENDIF
          463  +
          464  +# Check if warnings should be treated as errors when compiling.
          465  +#
          466  +!IF $(USE_FATAL_WARN)!=0
          467  +TCC = $(TCC) -WX
          468  +!ENDIF
   456    469   
   457    470   TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -fp:precise
   458    471   RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) $(RCOPTS) $(RCCOPTS)
   459    472   
   460    473   # Check if we want to use the "stdcall" calling convention when compiling.
   461    474   # This is not supported by the compilers for non-x86 platforms.  It should
   462    475   # also be noted here that building any target with these "stdcall" options
................................................................................
   627    640   !IF $(DEBUG)>0 || $(API_ARMOR)!=0 || $(FOR_WIN10)!=0
   628    641   TCC = $(TCC) -DSQLITE_ENABLE_API_ARMOR=1
   629    642   RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1
   630    643   !ENDIF
   631    644   
   632    645   !IF $(DEBUG)>2
   633    646   TCC = $(TCC) -DSQLITE_DEBUG=1
          647  +TCC = $(TCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE
   634    648   RCC = $(RCC) -DSQLITE_DEBUG=1
   635    649   !ENDIF
   636    650   
   637    651   !IF $(DEBUG)>4 || $(OSTRACE)!=0
   638    652   TCC = $(TCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   639    653   RCC = $(RCC) -DSQLITE_FORCE_OS_TRACE=1 -DSQLITE_DEBUG_OS_TRACE=1
   640    654   !ENDIF
................................................................................
   933    947   
   934    948   Replace.exe:
   935    949   	$(CSC) /target:exe $(TOP)\Replace.cs
   936    950   
   937    951   sqlite3.def:	Replace.exe $(LIBOBJ)
   938    952   	echo EXPORTS > sqlite3.def
   939    953   	dumpbin /all $(LIBOBJ) \
   940         -		| .\Replace.exe "^\s+/EXPORT:_?(sqlite3_[^@,]*)(?:@\d+|,DATA)?$$" $$1 true \
          954  +		| .\Replace.exe "^\s+/EXPORT:_?(sqlite3(?:session|changeset)?_[^@,]*)(?:@\d+|,DATA)?$$" $$1 true \
   941    955   		| sort >> sqlite3.def
   942    956   
   943    957   $(SQLITE3EXE):	$(TOP)\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
   944    958   	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\shell.c $(SHELL_CORE_SRC) \
   945    959   		/link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
   946    960   
   947    961   

Changes to autoconf/configure.ac.

    51     51   
    52     52   AS_IF([ test x"$enable_editline" != xno ],[
    53     53     AC_CHECK_HEADERS([editline/readline.h],[
    54     54       sLIBS=$LIBS
    55     55       LIBS=""
    56     56       AC_SEARCH_LIBS([readline],[edit],[
    57     57         AC_DEFINE([HAVE_EDITLINE],1,Define to use BSD editline)
    58         -      READLINE_LIBS=$LIBS
           58  +      READLINE_LIBS="$LIBS -ltinfo"
    59     59         enable_readline=no
    60         -    ])
           60  +    ],[],[-ltinfo])
    61     61       AS_UNSET(ac_cv_search_readline)
    62     62       LIBS=$sLIBS
    63     63     ])
    64     64   ])
    65     65   
    66     66   AS_IF([ test x"$enable_readline" != xno ],[
    67     67     AC_CHECK_HEADERS([readline/readline.h],[

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.17.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.18.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.17.0'
   730         -PACKAGE_STRING='sqlite 3.17.0'
          729  +PACKAGE_VERSION='3.18.0'
          730  +PACKAGE_STRING='sqlite 3.18.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
................................................................................
  1459   1459   #
  1460   1460   # Report the --help message.
  1461   1461   #
  1462   1462   if test "$ac_init_help" = "long"; then
  1463   1463     # Omit some internal or obsolete options to make the list less imposing.
  1464   1464     # This message is too long to be a string in the A/UX 3.1 sh.
  1465   1465     cat <<_ACEOF
  1466         -\`configure' configures sqlite 3.17.0 to adapt to many kinds of systems.
         1466  +\`configure' configures sqlite 3.18.0 to adapt to many kinds of systems.
  1467   1467   
  1468   1468   Usage: $0 [OPTION]... [VAR=VALUE]...
  1469   1469   
  1470   1470   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1471   1471   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1472   1472   
  1473   1473   Defaults for the options are specified in brackets.
................................................................................
  1524   1524     --build=BUILD     configure for building on BUILD [guessed]
  1525   1525     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1526   1526   _ACEOF
  1527   1527   fi
  1528   1528   
  1529   1529   if test -n "$ac_init_help"; then
  1530   1530     case $ac_init_help in
  1531         -     short | recursive ) echo "Configuration of sqlite 3.17.0:";;
         1531  +     short | recursive ) echo "Configuration of sqlite 3.18.0:";;
  1532   1532      esac
  1533   1533     cat <<\_ACEOF
  1534   1534   
  1535   1535   Optional Features:
  1536   1536     --disable-option-checking  ignore unrecognized --enable/--with options
  1537   1537     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1538   1538     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1648   1648       cd "$ac_pwd" || { ac_status=$?; break; }
  1649   1649     done
  1650   1650   fi
  1651   1651   
  1652   1652   test -n "$ac_init_help" && exit $ac_status
  1653   1653   if $ac_init_version; then
  1654   1654     cat <<\_ACEOF
  1655         -sqlite configure 3.17.0
         1655  +sqlite configure 3.18.0
  1656   1656   generated by GNU Autoconf 2.69
  1657   1657   
  1658   1658   Copyright (C) 2012 Free Software Foundation, Inc.
  1659   1659   This configure script is free software; the Free Software Foundation
  1660   1660   gives unlimited permission to copy, distribute and modify it.
  1661   1661   _ACEOF
  1662   1662     exit
................................................................................
  2067   2067     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2068   2068   
  2069   2069   } # ac_fn_c_check_header_mongrel
  2070   2070   cat >config.log <<_ACEOF
  2071   2071   This file contains any messages produced by compilers while
  2072   2072   running configure, to aid debugging if configure makes a mistake.
  2073   2073   
  2074         -It was created by sqlite $as_me 3.17.0, which was
         2074  +It was created by sqlite $as_me 3.18.0, which was
  2075   2075   generated by GNU Autoconf 2.69.  Invocation command line was
  2076   2076   
  2077   2077     $ $0 $@
  2078   2078   
  2079   2079   _ACEOF
  2080   2080   exec 5>>config.log
  2081   2081   {
................................................................................
 12147  12147   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12148  12148   
 12149  12149   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12150  12150   # Save the log message, to keep $0 and so on meaningful, and to
 12151  12151   # report actual input values of CONFIG_FILES etc. instead of their
 12152  12152   # values after options handling.
 12153  12153   ac_log="
 12154         -This file was extended by sqlite $as_me 3.17.0, which was
        12154  +This file was extended by sqlite $as_me 3.18.0, which was
 12155  12155   generated by GNU Autoconf 2.69.  Invocation command line was
 12156  12156   
 12157  12157     CONFIG_FILES    = $CONFIG_FILES
 12158  12158     CONFIG_HEADERS  = $CONFIG_HEADERS
 12159  12159     CONFIG_LINKS    = $CONFIG_LINKS
 12160  12160     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12161  12161     $ $0 $@
................................................................................
 12213  12213   
 12214  12214   Report bugs to the package provider."
 12215  12215   
 12216  12216   _ACEOF
 12217  12217   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12218  12218   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12219  12219   ac_cs_version="\\
 12220         -sqlite config.status 3.17.0
        12220  +sqlite config.status 3.18.0
 12221  12221   configured by $0, generated by GNU Autoconf 2.69,
 12222  12222     with options \\"\$ac_cs_config\\"
 12223  12223   
 12224  12224   Copyright (C) 2012 Free Software Foundation, Inc.
 12225  12225   This config.status script is free software; the Free Software Foundation
 12226  12226   gives unlimited permission to copy, distribute and modify it."
 12227  12227   

Name change from ext/README.txt to ext/README.md.

     1         -Version loadable extensions to SQLite are found in subfolders
     2         -of this folder.
            1  +## Loadable Extensions
            2  +
            3  +Various [loadable extensions](https://www.sqlite.org/loadext.html) for
            4  +SQLite are found in subfolders.
            5  +
            6  +Most subfolders are dedicated to a single loadable extension (for
            7  +example FTS5, or RTREE).  But the misc/ subfolder contains a collection
            8  +of smaller single-file extensions.

Changes to ext/fts3/fts3.c.

   345    345     if( (v & mask2)==0 ){ var = v; return ret; }
   346    346   
   347    347   /* 
   348    348   ** Read a 64-bit variable-length integer from memory starting at p[0].
   349    349   ** Return the number of bytes read, or 0 on error.
   350    350   ** The value is stored in *v.
   351    351   */
   352         -int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
   353         -  const char *pStart = p;
          352  +int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
          353  +  const unsigned char *p = (const unsigned char*)pBuf;
          354  +  const unsigned char *pStart = p;
   354    355     u32 a;
   355    356     u64 b;
   356    357     int shift;
   357    358   
   358    359     GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
   359    360     GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
   360    361     GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
................................................................................
   488    489     Fts3Table *p = (Fts3Table *)pVtab;
   489    490     int i;
   490    491   
   491    492     assert( p->nPendingData==0 );
   492    493     assert( p->pSegments==0 );
   493    494   
   494    495     /* Free any prepared statements held */
          496  +  sqlite3_finalize(p->pSeekStmt);
   495    497     for(i=0; i<SizeofArray(p->aStmt); i++){
   496    498       sqlite3_finalize(p->aStmt[i]);
   497    499     }
   498    500     sqlite3_free(p->zSegmentsTbl);
   499    501     sqlite3_free(p->zReadExprlist);
   500    502     sqlite3_free(p->zWriteExprlist);
   501    503     sqlite3_free(p->zContentTbl);
................................................................................
  1359   1361     p->db = db;
  1360   1362     p->nColumn = nCol;
  1361   1363     p->nPendingData = 0;
  1362   1364     p->azColumn = (char **)&p[1];
  1363   1365     p->pTokenizer = pTokenizer;
  1364   1366     p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
  1365   1367     p->bHasDocsize = (isFts4 && bNoDocsize==0);
  1366         -  p->bHasStat = isFts4;
  1367         -  p->bFts4 = isFts4;
  1368         -  p->bDescIdx = bDescIdx;
         1368  +  p->bHasStat = (u8)isFts4;
         1369  +  p->bFts4 = (u8)isFts4;
         1370  +  p->bDescIdx = (u8)bDescIdx;
  1369   1371     p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
  1370   1372     p->zContentTbl = zContent;
  1371   1373     p->zLanguageid = zLanguageid;
  1372   1374     zContent = 0;
  1373   1375     zLanguageid = 0;
  1374   1376     TESTONLY( p->inTransaction = -1 );
  1375   1377     TESTONLY( p->mxSavepoint = -1 );
................................................................................
  1392   1394     zCsr += nDb;
  1393   1395   
  1394   1396     /* Fill in the azColumn array */
  1395   1397     for(iCol=0; iCol<nCol; iCol++){
  1396   1398       char *z; 
  1397   1399       int n = 0;
  1398   1400       z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
  1399         -    memcpy(zCsr, z, n);
         1401  +    if( n>0 ){
         1402  +      memcpy(zCsr, z, n);
         1403  +    }
  1400   1404       zCsr[n] = '\0';
  1401   1405       sqlite3Fts3Dequote(zCsr);
  1402   1406       p->azColumn[iCol] = zCsr;
  1403   1407       zCsr += n+1;
  1404   1408       assert( zCsr <= &((char *)p)[nByte] );
  1405   1409     }
  1406   1410   
................................................................................
  1675   1679     *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
  1676   1680     if( !pCsr ){
  1677   1681       return SQLITE_NOMEM;
  1678   1682     }
  1679   1683     memset(pCsr, 0, sizeof(Fts3Cursor));
  1680   1684     return SQLITE_OK;
  1681   1685   }
         1686  +
         1687  +/*
         1688  +** Finalize the statement handle at pCsr->pStmt.
         1689  +**
         1690  +** Or, if that statement handle is one created by fts3CursorSeekStmt(),
         1691  +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
         1692  +** pointer there instead of finalizing it.
         1693  +*/
         1694  +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
         1695  +  if( pCsr->bSeekStmt ){
         1696  +    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
         1697  +    if( p->pSeekStmt==0 ){
         1698  +      p->pSeekStmt = pCsr->pStmt;
         1699  +      sqlite3_reset(pCsr->pStmt);
         1700  +      pCsr->pStmt = 0;
         1701  +    }
         1702  +    pCsr->bSeekStmt = 0;
         1703  +  }
         1704  +  sqlite3_finalize(pCsr->pStmt);
         1705  +}
  1682   1706   
  1683   1707   /*
  1684   1708   ** Close the cursor.  For additional information see the documentation
  1685   1709   ** on the xClose method of the virtual table interface.
  1686   1710   */
  1687   1711   static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
  1688   1712     Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  1689   1713     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1690         -  sqlite3_finalize(pCsr->pStmt);
         1714  +  fts3CursorFinalizeStmt(pCsr);
  1691   1715     sqlite3Fts3ExprFree(pCsr->pExpr);
  1692   1716     sqlite3Fts3FreeDeferredTokens(pCsr);
  1693   1717     sqlite3_free(pCsr->aDoclist);
  1694   1718     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
  1695   1719     assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1696   1720     sqlite3_free(pCsr);
  1697   1721     return SQLITE_OK;
................................................................................
  1701   1725   ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
  1702   1726   ** compose and prepare an SQL statement of the form:
  1703   1727   **
  1704   1728   **    "SELECT <columns> FROM %_content WHERE rowid = ?"
  1705   1729   **
  1706   1730   ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
  1707   1731   ** it. If an error occurs, return an SQLite error code.
  1708         -**
  1709         -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
  1710   1732   */
  1711         -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
         1733  +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
  1712   1734     int rc = SQLITE_OK;
  1713   1735     if( pCsr->pStmt==0 ){
  1714   1736       Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  1715   1737       char *zSql;
  1716         -    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
  1717         -    if( !zSql ) return SQLITE_NOMEM;
  1718         -    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
  1719         -    sqlite3_free(zSql);
         1738  +    if( p->pSeekStmt ){
         1739  +      pCsr->pStmt = p->pSeekStmt;
         1740  +      p->pSeekStmt = 0;
         1741  +    }else{
         1742  +      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
         1743  +      if( !zSql ) return SQLITE_NOMEM;
         1744  +      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
         1745  +      sqlite3_free(zSql);
         1746  +    }
         1747  +    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
  1720   1748     }
  1721         -  *ppStmt = pCsr->pStmt;
  1722   1749     return rc;
  1723   1750   }
  1724   1751   
  1725   1752   /*
  1726   1753   ** Position the pCsr->pStmt statement so that it is on the row
  1727   1754   ** of the %_content table that contains the last match.  Return
  1728   1755   ** SQLITE_OK on success.  
  1729   1756   */
  1730   1757   static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
  1731   1758     int rc = SQLITE_OK;
  1732   1759     if( pCsr->isRequireSeek ){
  1733         -    sqlite3_stmt *pStmt = 0;
  1734         -
  1735         -    rc = fts3CursorSeekStmt(pCsr, &pStmt);
         1760  +    rc = fts3CursorSeekStmt(pCsr);
  1736   1761       if( rc==SQLITE_OK ){
  1737   1762         sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
  1738   1763         pCsr->isRequireSeek = 0;
  1739   1764         if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
  1740   1765           return SQLITE_OK;
  1741   1766         }else{
  1742   1767           rc = sqlite3_reset(pCsr->pStmt);
................................................................................
  3186   3211     if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
  3187   3212     if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
  3188   3213     if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
  3189   3214     if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
  3190   3215     assert( iIdx==nVal );
  3191   3216   
  3192   3217     /* In case the cursor has been used before, clear it now. */
  3193         -  sqlite3_finalize(pCsr->pStmt);
         3218  +  fts3CursorFinalizeStmt(pCsr);
  3194   3219     sqlite3_free(pCsr->aDoclist);
  3195   3220     sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
  3196   3221     sqlite3Fts3ExprFree(pCsr->pExpr);
  3197   3222     memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
  3198   3223   
  3199   3224     /* Set the lower and upper bounds on docids to return */
  3200   3225     pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
................................................................................
  3254   3279       if( zSql ){
  3255   3280         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
  3256   3281         sqlite3_free(zSql);
  3257   3282       }else{
  3258   3283         rc = SQLITE_NOMEM;
  3259   3284       }
  3260   3285     }else if( eSearch==FTS3_DOCID_SEARCH ){
  3261         -    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
         3286  +    rc = fts3CursorSeekStmt(pCsr);
  3262   3287       if( rc==SQLITE_OK ){
  3263   3288         rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
  3264   3289       }
  3265   3290     }
  3266   3291     if( rc!=SQLITE_OK ) return rc;
  3267   3292   
  3268   3293     return fts3NextMethod(pCursor);
................................................................................
  3382   3407     ** of blocks from the segments table. But this is not considered overhead
  3383   3408     ** as it would also be required by a crisis-merge that used the same input 
  3384   3409     ** segments.
  3385   3410     */
  3386   3411     const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
  3387   3412   
  3388   3413     Fts3Table *p = (Fts3Table*)pVtab;
  3389         -  int rc = sqlite3Fts3PendingTermsFlush(p);
         3414  +  int rc;
         3415  +  i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
  3390   3416   
         3417  +  rc = sqlite3Fts3PendingTermsFlush(p);
  3391   3418     if( rc==SQLITE_OK 
  3392   3419      && p->nLeafAdd>(nMinMerge/16) 
  3393   3420      && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
  3394   3421     ){
  3395   3422       int mxLevel = 0;              /* Maximum relative level value in db */
  3396   3423       int A;                        /* Incr-merge parameter A */
  3397   3424   
................................................................................
  3398   3425       rc = sqlite3Fts3MaxLevel(p, &mxLevel);
  3399   3426       assert( rc==SQLITE_OK || mxLevel==0 );
  3400   3427       A = p->nLeafAdd * mxLevel;
  3401   3428       A += (A/2);
  3402   3429       if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
  3403   3430     }
  3404   3431     sqlite3Fts3SegmentsClose(p);
         3432  +  sqlite3_set_last_insert_rowid(p->db, iLastRowid);
  3405   3433     return rc;
  3406   3434   }
  3407   3435   
  3408   3436   /*
  3409   3437   ** If it is currently unknown whether or not the FTS table has an %_stat
  3410   3438   ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
  3411   3439   ** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
................................................................................
  3418   3446       char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
  3419   3447       if( zSql ){
  3420   3448         sqlite3_stmt *pStmt = 0;
  3421   3449         rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  3422   3450         if( rc==SQLITE_OK ){
  3423   3451           int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
  3424   3452           rc = sqlite3_finalize(pStmt);
  3425         -        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
         3453  +        if( rc==SQLITE_OK ) p->bHasStat = (u8)bHasStat;
  3426   3454         }
  3427   3455         sqlite3_free(zSql);
  3428   3456       }else{
  3429   3457         rc = SQLITE_NOMEM;
  3430   3458       }
  3431   3459     }
  3432   3460     return rc;

Changes to ext/fts3/fts3Int.h.

   226    226     int nAutoincrmerge;             /* Value configured by 'automerge' */
   227    227     u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
   228    228   
   229    229     /* Precompiled statements used by the implementation. Each of these 
   230    230     ** statements is run and reset within a single virtual table API call. 
   231    231     */
   232    232     sqlite3_stmt *aStmt[40];
          233  +  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
   233    234   
   234    235     char *zReadExprlist;
   235    236     char *zWriteExprlist;
   236    237   
   237    238     int nNodeSize;                  /* Soft limit for node size */
   238    239     u8 bFts4;                       /* True for FTS4, false for FTS3 */
   239    240     u8 bHasStat;                    /* True if %_stat table exists (2==unknown) */
................................................................................
   295    296   ** the xOpen method. Cursors are destroyed using the xClose method.
   296    297   */
   297    298   struct Fts3Cursor {
   298    299     sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
   299    300     i16 eSearch;                    /* Search strategy (see below) */
   300    301     u8 isEof;                       /* True if at End Of Results */
   301    302     u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
          303  +  u8 bSeekStmt;                   /* True if pStmt is a seek */
   302    304     sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
   303    305     Fts3Expr *pExpr;                /* Parsed MATCH query string */
   304    306     int iLangid;                    /* Language being queried for */
   305    307     int nPhrase;                    /* Number of matchable phrases in query */
   306    308     Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
   307    309     sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
   308    310     char *pNextId;                  /* Pointer into the body of aDoclist */

Changes to ext/fts3/fts3_unicode.c.

   132    132     unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
   133    133     int bAlnum,                     /* Replace Isalnum() return value with this */
   134    134     const char *zIn,                /* Array of characters to make exceptions */
   135    135     int nIn                         /* Length of z in bytes */
   136    136   ){
   137    137     const unsigned char *z = (const unsigned char *)zIn;
   138    138     const unsigned char *zTerm = &z[nIn];
   139         -  int iCode;
          139  +  unsigned int iCode;
   140    140     int nEntry = 0;
   141    141   
   142    142     assert( bAlnum==0 || bAlnum==1 );
   143    143   
   144    144     while( z<zTerm ){
   145    145       READ_UTF8(z, zTerm, iCode);
   146         -    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
   147         -    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
   148         -     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
          146  +    assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
          147  +    if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
          148  +     && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0 
   149    149       ){
   150    150         nEntry++;
   151    151       }
   152    152     }
   153    153   
   154    154     if( nEntry ){
   155    155       int *aNew;                    /* New aiException[] array */
................................................................................
   158    158       aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
   159    159       if( aNew==0 ) return SQLITE_NOMEM;
   160    160       nNew = p->nException;
   161    161   
   162    162       z = (const unsigned char *)zIn;
   163    163       while( z<zTerm ){
   164    164         READ_UTF8(z, zTerm, iCode);
   165         -      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
   166         -       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
          165  +      if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
          166  +       && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
   167    167         ){
   168    168           int i, j;
   169         -        for(i=0; i<nNew && aNew[i]<iCode; i++);
          169  +        for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
   170    170           for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
   171         -        aNew[i] = iCode;
          171  +        aNew[i] = (int)iCode;
   172    172           nNew++;
   173    173         }
   174    174       }
   175    175       p->aiException = aNew;
   176    176       p->nException = nNew;
   177    177     }
   178    178   
................................................................................
   314    314     int *pnToken,                   /* OUT: Number of bytes at *paToken */
   315    315     int *piStart,                   /* OUT: Starting offset of token */
   316    316     int *piEnd,                     /* OUT: Ending offset of token */
   317    317     int *piPos                      /* OUT: Position integer of token */
   318    318   ){
   319    319     unicode_cursor *pCsr = (unicode_cursor *)pC;
   320    320     unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
   321         -  int iCode = 0;
          321  +  unsigned int iCode = 0;
   322    322     char *zOut;
   323    323     const unsigned char *z = &pCsr->aInput[pCsr->iOff];
   324    324     const unsigned char *zStart = z;
   325    325     const unsigned char *zEnd;
   326    326     const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
   327    327   
   328    328     /* Scan past any delimiter characters before the start of the next token.
   329    329     ** Return SQLITE_DONE early if this takes us all the way to the end of 
   330    330     ** the input.  */
   331    331     while( z<zTerm ){
   332    332       READ_UTF8(z, zTerm, iCode);
   333         -    if( unicodeIsAlnum(p, iCode) ) break;
          333  +    if( unicodeIsAlnum(p, (int)iCode) ) break;
   334    334       zStart = z;
   335    335     }
   336    336     if( zStart>=zTerm ) return SQLITE_DONE;
   337    337   
   338    338     zOut = pCsr->zToken;
   339    339     do {
   340    340       int iOut;
................................................................................
   346    346         zOut = &zNew[zOut - pCsr->zToken];
   347    347         pCsr->zToken = zNew;
   348    348         pCsr->nAlloc += 64;
   349    349       }
   350    350   
   351    351       /* Write the folded case of the last character read to the output */
   352    352       zEnd = z;
   353         -    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
          353  +    iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
   354    354       if( iOut ){
   355    355         WRITE_UTF8(zOut, iOut);
   356    356       }
   357    357   
   358    358       /* If the cursor is not at EOF, read the next character */
   359    359       if( z>=zTerm ) break;
   360    360       READ_UTF8(z, zTerm, iCode);
   361         -  }while( unicodeIsAlnum(p, iCode) 
   362         -       || sqlite3FtsUnicodeIsdiacritic(iCode)
          361  +  }while( unicodeIsAlnum(p, (int)iCode) 
          362  +       || sqlite3FtsUnicodeIsdiacritic((int)iCode)
   363    363     );
   364    364   
   365    365     /* Set the output variables and return. */
   366    366     pCsr->iOff = (int)(z - pCsr->aInput);
   367    367     *paToken = pCsr->zToken;
   368    368     *pnToken = (int)(zOut - pCsr->zToken);
   369    369     *piStart = (int)(zStart - pCsr->aInput);

Changes to ext/fts3/fts3_unicode2.c.

   123    123       0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
   124    124       0x380400F0,
   125    125     };
   126    126     static const unsigned int aAscii[4] = {
   127    127       0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
   128    128     };
   129    129   
   130         -  if( c<128 ){
   131         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
   132         -  }else if( c<(1<<22) ){
          130  +  if( (unsigned int)c<128 ){
          131  +    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
          132  +  }else if( (unsigned int)c<(1<<22) ){
   133    133       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   134    134       int iRes = 0;
   135    135       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   136    136       int iLo = 0;
   137    137       while( iHi>=iLo ){
   138    138         int iTest = (iHi + iLo) / 2;
   139    139         if( key >= aEntry[iTest] ){
................................................................................
   318    318      65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
   319    319      65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
   320    320      65514, 65521, 65527, 65528, 65529, 
   321    321     };
   322    322   
   323    323     int ret = c;
   324    324   
   325         -  assert( c>=0 );
   326    325     assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
   327    326   
   328    327     if( c<128 ){
   329    328       if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
   330    329     }else if( c<65536 ){
          330  +    const struct TableEntry *p;
   331    331       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   332    332       int iLo = 0;
   333    333       int iRes = -1;
   334    334   
          335  +    assert( c>aEntry[0].iCode );
   335    336       while( iHi>=iLo ){
   336    337         int iTest = (iHi + iLo) / 2;
   337    338         int cmp = (c - aEntry[iTest].iCode);
   338    339         if( cmp>=0 ){
   339    340           iRes = iTest;
   340    341           iLo = iTest+1;
   341    342         }else{
   342    343           iHi = iTest-1;
   343    344         }
   344    345       }
   345         -    assert( iRes<0 || c>=aEntry[iRes].iCode );
   346    346   
   347         -    if( iRes>=0 ){
   348         -      const struct TableEntry *p = &aEntry[iRes];
   349         -      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
   350         -        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
   351         -        assert( ret>0 );
   352         -      }
          347  +    assert( iRes>=0 && c>=aEntry[iRes].iCode );
          348  +    p = &aEntry[iRes];
          349  +    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
          350  +      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
          351  +      assert( ret>0 );
   353    352       }
   354    353   
   355    354       if( bRemoveDiacritic ) ret = remove_diacritic(ret);
   356    355     }
   357    356     
   358    357     else if( c>=66560 && c<66600 ){
   359    358       ret = c + 40;
   360    359     }
   361    360   
   362    361     return ret;
   363    362   }
   364    363   #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
   365    364   #endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */

Changes to ext/fts3/fts3_write.c.

  4952   4952     return rc;
  4953   4953   }
  4954   4954   
  4955   4955   /*
  4956   4956   ** Convert the text beginning at *pz into an integer and return
  4957   4957   ** its value.  Advance *pz to point to the first character past
  4958   4958   ** the integer.
         4959  +**
         4960  +** This function used for parameters to merge= and incrmerge=
         4961  +** commands. 
  4959   4962   */
  4960   4963   static int fts3Getint(const char **pz){
  4961   4964     const char *z = *pz;
  4962   4965     int i = 0;
  4963         -  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
         4966  +  while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
  4964   4967     *pz = z;
  4965   4968     return i;
  4966   4969   }
  4967   4970   
  4968   4971   /*
  4969   4972   ** Process statements of the form:
  4970   4973   **

Added ext/fts3/tool/fts3cov.sh.

            1  +#!/bin/sh
            2  +
            3  +set -e
            4  +
            5  +srcdir=`dirname $(dirname $(dirname $(dirname $0)))`
            6  +./testfixture $srcdir/test/fts3.test --output=fts3cov-out.txt
            7  +
            8  +echo ""
            9  +
           10  +for f in `ls $srcdir/ext/fts3/*.c` 
           11  +do
           12  +  f=`basename $f`
           13  +  echo -ne "$f: "
           14  +  gcov -b $f | grep Taken | sed 's/Taken at least once://'
           15  +done
           16  +

Changes to ext/fts3/unicode/mkunicode.tcl.

   223    223     puts "** is less than zero."
   224    224     puts "*/"
   225    225     puts "int ${zFunc}\(int c)\{"
   226    226     an_print_range_array $lRange
   227    227     an_print_ascii_bitmap $lRange
   228    228     puts {
   229    229     if( (unsigned int)c<128 ){
   230         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
          230  +    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
   231    231     }else if( (unsigned int)c<(1<<22) ){
   232    232       unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   233    233       int iRes = 0;
   234    234       int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   235    235       int iLo = 0;
   236    236       while( iHi>=iLo ){
   237    237         int iTest = (iHi + iLo) / 2;

Changes to ext/fts5/fts5Int.h.

    26     26   typedef unsigned char  u8;
    27     27   typedef unsigned int   u32;
    28     28   typedef unsigned short u16;
    29     29   typedef short i16;
    30     30   typedef sqlite3_int64 i64;
    31     31   typedef sqlite3_uint64 u64;
    32     32   
    33         -#define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
           33  +#ifndef ArraySize
           34  +# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
           35  +#endif
    34     36   
    35     37   #define testcase(x)
    36     38   #define ALWAYS(x) 1
    37     39   #define NEVER(x) 0
    38     40   
    39     41   #define MIN(x,y) (((x) < (y)) ? (x) : (y))
    40     42   #define MAX(x,y) (((x) > (y)) ? (x) : (y))

Changes to ext/fts5/fts5_expr.c.

  1106   1106       Fts5ExprNode *p1 = pNode->apChild[i];
  1107   1107       assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
  1108   1108       if( p1->bEof==0 ){
  1109   1109         if( (p1->iRowid==iLast) 
  1110   1110          || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
  1111   1111         ){
  1112   1112           int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
  1113         -        if( rc!=SQLITE_OK ) return rc;
         1113  +        if( rc!=SQLITE_OK ){
         1114  +          pNode->bNomatch = 0;
         1115  +          return rc;
         1116  +        }
  1114   1117         }
  1115   1118       }
  1116   1119     }
  1117   1120   
  1118   1121     fts5ExprNodeTest_OR(pExpr, pNode);
  1119   1122     return SQLITE_OK;
  1120   1123   }
................................................................................
  1137   1140       bMatch = 1;
  1138   1141       for(iChild=0; iChild<pAnd->nChild; iChild++){
  1139   1142         Fts5ExprNode *pChild = pAnd->apChild[iChild];
  1140   1143         int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
  1141   1144         if( cmp>0 ){
  1142   1145           /* Advance pChild until it points to iLast or laster */
  1143   1146           rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
  1144         -        if( rc!=SQLITE_OK ) return rc;
         1147  +        if( rc!=SQLITE_OK ){
         1148  +          pAnd->bNomatch = 0;
         1149  +          return rc;
         1150  +        }
  1145   1151         }
  1146   1152   
  1147   1153         /* If the child node is now at EOF, so is the parent AND node. Otherwise,
  1148   1154         ** the child node is guaranteed to have advanced at least as far as
  1149   1155         ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
  1150   1156         ** new lastest rowid seen so far.  */
  1151   1157         assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
................................................................................
  1176   1182     Fts5ExprNode *pNode,
  1177   1183     int bFromValid,
  1178   1184     i64 iFrom
  1179   1185   ){
  1180   1186     int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
  1181   1187     if( rc==SQLITE_OK ){
  1182   1188       rc = fts5ExprNodeTest_AND(pExpr, pNode);
         1189  +  }else{
         1190  +    pNode->bNomatch = 0;
  1183   1191     }
  1184   1192     return rc;
  1185   1193   }
  1186   1194   
  1187   1195   static int fts5ExprNodeTest_NOT(
  1188   1196     Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
  1189   1197     Fts5ExprNode *pNode             /* FTS5_NOT node to advance */
................................................................................
  1218   1226     int bFromValid,
  1219   1227     i64 iFrom
  1220   1228   ){
  1221   1229     int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
  1222   1230     if( rc==SQLITE_OK ){
  1223   1231       rc = fts5ExprNodeTest_NOT(pExpr, pNode);
  1224   1232     }
         1233  +  if( rc!=SQLITE_OK ){
         1234  +    pNode->bNomatch = 0;
         1235  +  }
  1225   1236     return rc;
  1226   1237   }
  1227   1238   
  1228   1239   /*
  1229   1240   ** If pNode currently points to a match, this function returns SQLITE_OK
  1230   1241   ** without modifying it. Otherwise, pNode is advanced until it does point
  1231   1242   ** to a match or EOF is reached.
................................................................................
  1597   1608     char *z = 0;
  1598   1609   
  1599   1610     memset(&sCtx, 0, sizeof(TokenCtx));
  1600   1611     sCtx.pPhrase = pAppend;
  1601   1612   
  1602   1613     rc = fts5ParseStringFromToken(pToken, &z);
  1603   1614     if( rc==SQLITE_OK ){
  1604         -    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
         1615  +    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
  1605   1616       int n;
  1606   1617       sqlite3Fts5Dequote(z);
  1607   1618       n = (int)strlen(z);
  1608   1619       rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
  1609   1620     }
  1610   1621     sqlite3_free(z);
  1611   1622     if( rc || (rc = sCtx.rc) ){

Changes to ext/fts5/fts5_storage.c.

   541    541       if( rc==SQLITE_OK ){
   542    542         sqlite3_bind_int64(pDel, 1, iDel);
   543    543         sqlite3_step(pDel);
   544    544         rc = sqlite3_reset(pDel);
   545    545       }
   546    546     }
   547    547   
   548         -  /* Write the averages record */
   549         -  if( rc==SQLITE_OK ){
   550         -    rc = fts5StorageSaveTotals(p);
   551         -  }
   552         -
   553    548     return rc;
   554    549   }
   555    550   
   556    551   /*
   557    552   ** Delete all entries in the FTS5 index.
   558    553   */
   559    554   int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
................................................................................
   749    744   
   750    745     /* Write the %_docsize record */
   751    746     if( rc==SQLITE_OK ){
   752    747       rc = fts5StorageInsertDocsize(p, iRowid, &buf);
   753    748     }
   754    749     sqlite3_free(buf.p);
   755    750   
   756         -  /* Write the averages record */
   757         -  if( rc==SQLITE_OK ){
   758         -    rc = fts5StorageSaveTotals(p);
   759         -  }
   760         -
   761    751     return rc;
   762    752   }
   763    753   
   764    754   static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
   765    755     Fts5Config *pConfig = p->pConfig;
   766    756     char *zSql;
   767    757     int rc;
................................................................................
  1088   1078     return rc;
  1089   1079   }
  1090   1080   
  1091   1081   /*
  1092   1082   ** Flush any data currently held in-memory to disk.
  1093   1083   */
  1094   1084   int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
  1095         -  if( bCommit && p->bTotalsValid ){
  1096         -    int rc = fts5StorageSaveTotals(p);
  1097         -    p->bTotalsValid = 0;
  1098         -    if( rc!=SQLITE_OK ) return rc;
         1085  +  int rc = SQLITE_OK;
         1086  +  i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
         1087  +  if( p->bTotalsValid ){
         1088  +    rc = fts5StorageSaveTotals(p);
         1089  +    if( bCommit ) p->bTotalsValid = 0;
         1090  +  }
         1091  +  if( rc==SQLITE_OK ){
         1092  +    rc = sqlite3Fts5IndexSync(p->pIndex, bCommit);
  1099   1093     }
  1100         -  return sqlite3Fts5IndexSync(p->pIndex, bCommit);
         1094  +  sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
         1095  +  return rc;
  1101   1096   }
  1102   1097   
  1103   1098   int sqlite3Fts5StorageRollback(Fts5Storage *p){
  1104   1099     p->bTotalsValid = 0;
  1105   1100     return sqlite3Fts5IndexRollback(p->pIndex);
  1106   1101   }
  1107   1102   

Added ext/fts5/test/fts5faultD.test.

            1  +# 2016 February 2
            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 is focused on OOM errors.
           13  +#
           14  +
           15  +source [file join [file dirname [info script]] fts5_common.tcl]
           16  +source $testdir/malloc_common.tcl
           17  +set testprefix fts5faultA
           18  +
           19  +# If SQLITE_ENABLE_FTS3 is defined, omit this file.
           20  +ifcapable !fts5 {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +foreach_detail_mode $testprefix {
           26  +  if {"%DETAIL%"=="none"} continue
           27  +
           28  +  do_execsql_test 1.0 {
           29  +    CREATE VIRTUAL TABLE o1 USING fts5(a, b, c, detail=%DETAIL%);
           30  +    INSERT INTO o1(o1, rank) VALUES('pgsz', 32);
           31  +
           32  +    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 )
           33  +    INSERT INTO o1 SELECT 'A', 'B', 'C' FROM s;
           34  +
           35  +    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 )
           36  +    INSERT INTO o1 SELECT 'C', 'A', 'B' FROM s;
           37  +
           38  +    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 )
           39  +    INSERT INTO o1 SELECT 'B', 'C', 'A' FROM s;
           40  +  }
           41  +  
           42  +  do_faultsim_test 1 -faults int* -prep {
           43  +    sqlite3 db test.db
           44  +  } -body {
           45  +    execsql { SELECT count(*) FROM o1('a') }
           46  +  } -test {
           47  +    faultsim_test_result {0 180} {1 {vtable constructor failed: o1}}
           48  +  }
           49  +
           50  +  do_faultsim_test 2 -faults int* -prep {
           51  +    sqlite3 db test.db
           52  +  } -body {
           53  +    execsql { SELECT * FROM o1('a:a AND {b c}:b') ORDER BY rank }
           54  +    expr 1
           55  +  } -test {
           56  +    faultsim_test_result {0 1} {1 {vtable constructor failed: o1}}
           57  +  }
           58  +
           59  +  do_faultsim_test 3 -faults int* -prep {
           60  +    sqlite3 db test.db
           61  +  } -body {
           62  +    execsql { SELECT * FROM o1('{b c}:b NOT a:a') ORDER BY rank }
           63  +    expr 1
           64  +  } -test {
           65  +    faultsim_test_result {0 1} {1 {vtable constructor failed: o1}}
           66  +  }
           67  +
           68  +  do_faultsim_test 4 -faults int* -prep {
           69  +    sqlite3 db test.db
           70  +  } -body {
           71  +    execsql { SELECT * FROM o1('b:b OR a:a') }
           72  +    expr 1
           73  +  } -test {
           74  +    faultsim_test_result {0 1} {1 {vtable constructor failed: o1}}
           75  +  }
           76  +
           77  +  do_faultsim_test 5 -faults int* -prep {
           78  +    sqlite3 db test.db
           79  +  } -body {
           80  +    execsql { SELECT count(*) FROM o1('c:b') }
           81  +    expr 1
           82  +  } -test {
           83  +    faultsim_test_result {0 1} {1 {vtable constructor failed: o1}}
           84  +  }
           85  +}
           86  +
           87  +finish_test

Added ext/fts5/test/fts5lastrowid.test.

            1  +# 2017 Feb 27
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Tests of the last_insert_rowid functionality with fts5.
           13  +#
           14  +
           15  +source [file join [file dirname [info script]] fts5_common.tcl]
           16  +set testprefix fts5lastrowid
           17  +
           18  +# If SQLITE_ENABLE_FTS5 is defined, omit this file.
           19  +ifcapable !fts5 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.0 {
           25  +  CREATE VIRTUAL TABLE t1 USING fts5(str);
           26  +}
           27  +
           28  +do_execsql_test 1.1 {
           29  +  INSERT INTO t1 VALUES('one string');
           30  +  INSERT INTO t1 VALUES('two string');
           31  +  INSERT INTO t1 VALUES('three string');
           32  +  SELECT last_insert_rowid();
           33  +} {3}
           34  +
           35  +do_execsql_test 1.2 {
           36  +  BEGIN;
           37  +    INSERT INTO t1 VALUES('one string');
           38  +    INSERT INTO t1 VALUES('two string');
           39  +    INSERT INTO t1 VALUES('three string');
           40  +  COMMIT;
           41  +  SELECT last_insert_rowid();
           42  +} {6}
           43  +
           44  +do_execsql_test 1.3 {
           45  +  INSERT INTO t1(rowid, str) VALUES(-22, 'some more text');
           46  +  SELECT last_insert_rowid();
           47  +} {-22}
           48  +
           49  +do_execsql_test 1.4 {
           50  +  BEGIN;
           51  +    INSERT INTO t1(rowid, str) VALUES(45, 'some more text');
           52  +    INSERT INTO t1(rowid, str) VALUES(46, 'some more text');
           53  +    INSERT INTO t1(rowid, str) VALUES(222, 'some more text');
           54  +    SELECT last_insert_rowid();
           55  +  COMMIT;
           56  +  SELECT last_insert_rowid();
           57  +} {222 222}
           58  +
           59  +do_execsql_test 1.5 {
           60  +  CREATE TABLE x1(x);
           61  +  INSERT INTO x1 VALUES('john'), ('paul'), ('george'), ('ringo');
           62  +  INSERT INTO t1 SELECT x FROM x1;
           63  +  SELECT last_insert_rowid();
           64  +} {226}
           65  +
           66  +do_execsql_test 1.6 {
           67  +  INSERT INTO t1(rowid, str) SELECT rowid+10, x FROM x1;
           68  +  SELECT last_insert_rowid();
           69  +} {14}
           70  +
           71  +
           72  +finish_test
           73  +

Changes to ext/fts5/test/fts5synonym.test.

   148    148   reset_db
   149    149   fts5_tclnum_register db
   150    150   
   151    151   foreach {tn expr res} {
   152    152     1  {abc}                           {"abc"}
   153    153     2  {one}                           {"one"|"i"|"1"}
   154    154     3  {3}                             {"3"|"iii"|"three"}
   155         -  4  {3*}                            {"3"|"iii"|"three" *}
          155  +  4  {3*}                            {"3" *}
   156    156   } {
   157    157     do_execsql_test 4.1.$tn {
   158    158       SELECT fts5_expr($expr, 'tokenize=tclnum')
   159    159     } [list $res]
   160    160   }
   161    161   
   162    162   do_execsql_test 4.2.1 {

Changes to ext/fts5/test/fts5tokenizer.test.

   257    257     INSERT INTO e6 VALUES('theAquickBbrownCfoxDjumpedWoverXtheYlazyZdog');
   258    258     CREATE VIRTUAL TABLE e7 USING fts5vocab(e6, 'row');
   259    259     SELECT term FROM e7;
   260    260     ROLLBACK;
   261    261   } {
   262    262     brown dog fox jump lazi over quick the
   263    263   }
          264  +
          265  +#-------------------------------------------------------------------------
          266  +# Check that the FTS5_TOKENIZE_PREFIX flag is passed to the tokenizer
          267  +# implementation.
          268  +#
          269  +reset_db
          270  +proc tcl_create {args} { return "tcl_tokenize" }
          271  +sqlite3_fts5_create_tokenizer db tcl tcl_create
          272  +set ::flags [list]
          273  +proc tcl_tokenize {tflags text} {
          274  +  lappend ::flags $tflags
          275  +  foreach {w iStart iEnd} [fts5_tokenize_split $text] {
          276  +    sqlite3_fts5_token $w $iStart $iEnd
          277  +  }
          278  +}
          279  +
          280  +do_execsql_test 9.1.1 {
          281  +  CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=tcl);
          282  +  INSERT INTO t1 VALUES('abc');
          283  +  INSERT INTO t1 VALUES('xyz');
          284  +} {}
          285  +do_test 9.1.2 { set ::flags } {document document}
          286  +
          287  +set ::flags [list]
          288  +do_execsql_test 9.2.1 { SELECT * FROM t1('abc'); } {abc}
          289  +do_test 9.2.2 { set ::flags } {query}
          290  +
          291  +set ::flags [list]
          292  +do_execsql_test 9.3.1 { SELECT * FROM t1('ab*'); } {abc}
          293  +do_test 9.3.2 { set ::flags } {prefixquery}
          294  +
          295  +set ::flags [list]
          296  +do_execsql_test 9.4.1 { SELECT * FROM t1('"abc xyz" *'); } {}
          297  +do_test 9.4.2 { set ::flags } {prefixquery}
          298  +
          299  +set ::flags [list]
          300  +do_execsql_test 9.5.1 { SELECT * FROM t1('"abc xyz*"'); } {}
          301  +do_test 9.5.2 { set ::flags } {query}
          302  +
   264    303   
   265    304   finish_test
   266    305   

Changes to ext/icu/icu.c.

   489    489     }
   490    490   }
   491    491   
   492    492   /*
   493    493   ** Register the ICU extension functions with database db.
   494    494   */
   495    495   int sqlite3IcuInit(sqlite3 *db){
   496         -  struct IcuScalar {
          496  +  static const struct IcuScalar {
   497    497       const char *zName;                        /* Function name */
   498         -    int nArg;                                 /* Number of arguments */
   499         -    int enc;                                  /* Optimal text encoding */
   500         -    void *pContext;                           /* sqlite3_user_data() context */
          498  +    unsigned char nArg;                       /* Number of arguments */
          499  +    unsigned short enc;                       /* Optimal text encoding */
          500  +    unsigned char iContext;                   /* sqlite3_user_data() context */
   501    501       void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
   502    502     } scalars[] = {
   503         -    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,          0, icuRegexpFunc},
   504         -
   505         -    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
   506         -    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
   507         -    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
   508         -    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC, (void*)1, icuCaseFunc16},
   509         -
   510         -    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
   511         -    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuCaseFunc16},
   512         -    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
   513         -    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,  (void*)1, icuCaseFunc16},
   514         -
   515         -    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
   516         -    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,         0, icuLikeFunc},
   517         -
   518         -    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
          503  +    {"icu_load_collation",  2, SQLITE_UTF8,                1, icuLoadCollation},
          504  +    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,         0, icuRegexpFunc},
          505  +    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
          506  +    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
          507  +    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
          508  +    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
          509  +    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
          510  +    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
          511  +    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
          512  +    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
          513  +    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
          514  +    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
   519    515     };
   520         -
   521    516     int rc = SQLITE_OK;
   522    517     int i;
   523    518   
          519  +  
   524    520     for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
   525         -    struct IcuScalar *p = &scalars[i];
          521  +    const struct IcuScalar *p = &scalars[i];
   526    522       rc = sqlite3_create_function(
   527         -        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
          523  +        db, p->zName, p->nArg, p->enc, 
          524  +        p->iContext ? (void*)db : (void*)0,
          525  +        p->xFunc, 0, 0
   528    526       );
   529    527     }
   530    528   
   531    529     return rc;
   532    530   }
   533    531   
   534    532   #if !SQLITE_CORE

Added ext/misc/README.md.

            1  +## Miscellaneous Extensions
            2  +
            3  +This folder contains a collection of smaller loadable extensions.
            4  +See <https://www.sqlite.org/loadext.html> for instructions on how
            5  +to compile and use loadable extensions.
            6  +Each extension in this folder is implemented in a single file of C code.
            7  +
            8  +Each source file contains a description in its header comment.  See the
            9  +header comments for details about each extension.  Additional notes are
           10  +as follows:
           11  +
           12  +  *  **carray.c** &mdash;  This module implements the
           13  +     [carray](https://www.sqlite.org/carray.html) table-valued function.
           14  +     It is a good example of how to go about implementing a custom
           15  +     [table-valued function](https://www.sqlite.org/vtab.html#tabfunc2).
           16  +
           17  +  *  **dbdump.c** &mdash;  This is not actually a loadable extension, but
           18  +     rather a library that implements an approximate equivalent to the
           19  +     ".dump" command of the
           20  +     [command-line shell](https://www.sqlite.org/cli.html).
           21  +
           22  +  *  **memvfs.c** &mdash;  This file implements a custom
           23  +     [VFS](https://www.sqlite.org/vfs.html) that stores an entire database
           24  +     file in a single block of RAM.  It serves as a good example of how
           25  +     to implement a simple custom VFS.
           26  +
           27  +  *  **rot13.c** &mdash;  This file implements the very simple rot13()
           28  +     substitution function.  This file makes a good template for implementing
           29  +     new custom SQL functions for SQLite.
           30  +
           31  +  *  **series.c** &mdash;  This is an implementation of the
           32  +     "generate_series" [virtual table](https://www.sqlite.org/vtab.html).
           33  +     It can make a good template for new custom virtual table implementations.
           34  +
           35  +  *  **shathree.c** &mdash;  An implementation of the sha3() and
           36  +     sha3_query() SQL functions.  The file is named "shathree.c" instead
           37  +     of "sha3.c" because the default entry point names in SQLite are based
           38  +     on the source filename with digits removed, so if we used the name
           39  +     "sha3.c" then the entry point would conflict with the prior "sha1.c"
           40  +     extension.

Added ext/misc/dbdump.c.

            1  +/*
            2  +** 2016-03-13
            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 C-language subroutine that converts the content
           14  +** of an SQLite database into UTF-8 text SQL statements that can be used
           15  +** to exactly recreate the original database.  ROWID values are preserved.
           16  +**
           17  +** A prototype of the implemented subroutine is this:
           18  +**
           19  +**   int sqlite3_db_dump(
           20  +**          sqlite3 *db,
           21  +**          const char *zSchema,
           22  +**          const char *zTable,
           23  +**          void (*xCallback)(void*, const char*),
           24  +**          void *pArg
           25  +**   );
           26  +**
           27  +** The db parameter is the database connection.  zSchema is the schema within
           28  +** that database which is to be dumped.  Usually the zSchema is "main" but
           29  +** can also be "temp" or any ATTACH-ed database.  If zTable is not NULL, then
           30  +** only the content of that one table is dumped.  If zTable is NULL, then all
           31  +** tables are dumped.
           32  +**
           33  +** The generate text is passed to xCallback() in multiple calls.  The second
           34  +** argument to xCallback() is a copy of the pArg parameter.  The first
           35  +** argument is some of the output text that this routine generates.  The
           36  +** signature to xCallback() is designed to make it compatible with fputs().
           37  +**
           38  +** The sqlite3_db_dump() subroutine returns SQLITE_OK on success or some error
           39  +** code if it encounters a problem.
           40  +**
           41  +** If this file is compiled with -DDBDUMP_STANDALONE then a "main()" routine
           42  +** is included so that this routine becomes a command-line utility.  The
           43  +** command-line utility takes two or three arguments which are the name
           44  +** of the database file, the schema, and optionally the table, forming the
           45  +** first three arguments of a single call to the library routine.
           46  +*/
           47  +#include "sqlite3.h"
           48  +#include <stdarg.h>
           49  +#include <string.h>
           50  +#include <ctype.h>
           51  +
           52  +/*
           53  +** The state of the dump process.
           54  +*/
           55  +typedef struct DState DState;
           56  +struct DState {
           57  +  sqlite3 *db;                /* The database connection */
           58  +  int nErr;                   /* Number of errors seen so far */
           59  +  int rc;                     /* Error code */
           60  +  int writableSchema;                    /* True if in writable_schema mode */
           61  +  int (*xCallback)(const char*,void*);   /* Send output here */
           62  +  void *pArg;                            /* Argument to xCallback() */
           63  +};
           64  +
           65  +/*
           66  +** A variable length string to which one can append text.
           67  +*/
           68  +typedef struct DText DText;
           69  +struct DText {
           70  +  char *z;           /* The text */
           71  +  int n;             /* Number of bytes of content in z[] */
           72  +  int nAlloc;        /* Number of bytes allocated to z[] */
           73  +};
           74  +
           75  +/*
           76  +** Initialize and destroy a DText object
           77  +*/
           78  +static void initText(DText *p){
           79  +  memset(p, 0, sizeof(*p));
           80  +}
           81  +static void freeText(DText *p){
           82  +  sqlite3_free(p->z);
           83  +  initText(p);
           84  +}
           85  +
           86  +/* zIn is either a pointer to a NULL-terminated string in memory obtained
           87  +** from malloc(), or a NULL pointer. The string pointed to by zAppend is
           88  +** added to zIn, and the result returned in memory obtained from malloc().
           89  +** zIn, if it was not NULL, is freed.
           90  +**
           91  +** If the third argument, quote, is not '\0', then it is used as a
           92  +** quote character for zAppend.
           93  +*/
           94  +static void appendText(DText *p, char const *zAppend, char quote){
           95  +  int len;
           96  +  int i;
           97  +  int nAppend = (int)(strlen(zAppend) & 0x3fffffff);
           98  +
           99  +  len = nAppend+p->n+1;
          100  +  if( quote ){
          101  +    len += 2;
          102  +    for(i=0; i<nAppend; i++){
          103  +      if( zAppend[i]==quote ) len++;
          104  +    }
          105  +  }
          106  +
          107  +  if( p->n+len>=p->nAlloc ){
          108  +    char *zNew;
          109  +    p->nAlloc = p->nAlloc*2 + len + 20;
          110  +    zNew = sqlite3_realloc(p->z, p->nAlloc);
          111  +    if( zNew==0 ){
          112  +      freeText(p);
          113  +      return;
          114  +    }
          115  +    p->z = zNew;
          116  +  }
          117  +
          118  +  if( quote ){
          119  +    char *zCsr = p->z+p->n;
          120  +    *zCsr++ = quote;
          121  +    for(i=0; i<nAppend; i++){
          122  +      *zCsr++ = zAppend[i];
          123  +      if( zAppend[i]==quote ) *zCsr++ = quote;
          124  +    }
          125  +    *zCsr++ = quote;
          126  +    p->n = (int)(zCsr - p->z);
          127  +    *zCsr = '\0';
          128  +  }else{
          129  +    memcpy(p->z+p->n, zAppend, nAppend);
          130  +    p->n += nAppend;
          131  +    p->z[p->n] = '\0';
          132  +  }
          133  +}
          134  +
          135  +/*
          136  +** Attempt to determine if identifier zName needs to be quoted, either
          137  +** because it contains non-alphanumeric characters, or because it is an
          138  +** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
          139  +** that quoting is required.
          140  +**
          141  +** Return '"' if quoting is required.  Return 0 if no quoting is required.
          142  +*/
          143  +static char quoteChar(const char *zName){
          144  +  /* All SQLite keywords, in alphabetical order */
          145  +  static const char *azKeywords[] = {
          146  +    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
          147  +    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
          148  +    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
          149  +    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
          150  +    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
          151  +    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
          152  +    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
          153  +    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
          154  +    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
          155  +    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
          156  +    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
          157  +    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
          158  +    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
          159  +    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
          160  +    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
          161  +    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
          162  +    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
          163  +    "WITH", "WITHOUT",
          164  +  };
          165  +  int i, lwr, upr, mid, c;
          166  +  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
          167  +  for(i=0; zName[i]; i++){
          168  +    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
          169  +  }
          170  +  lwr = 0;
          171  +  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
          172  +  while( lwr<=upr ){
          173  +    mid = (lwr+upr)/2;
          174  +    c = sqlite3_stricmp(azKeywords[mid], zName);
          175  +    if( c==0 ) return '"';
          176  +    if( c<0 ){
          177  +      lwr = mid+1;
          178  +    }else{
          179  +      upr = mid-1;
          180  +    }
          181  +  }
          182  +  return 0;
          183  +}
          184  +
          185  +
          186  +/*
          187  +** Release memory previously allocated by tableColumnList().
          188  +*/
          189  +static void freeColumnList(char **azCol){
          190  +  int i;
          191  +  for(i=1; azCol[i]; i++){
          192  +    sqlite3_free(azCol[i]);
          193  +  }
          194  +  /* azCol[0] is a static string */
          195  +  sqlite3_free(azCol);
          196  +}
          197  +
          198  +/*
          199  +** Return a list of pointers to strings which are the names of all
          200  +** columns in table zTab.   The memory to hold the names is dynamically
          201  +** allocated and must be released by the caller using a subsequent call
          202  +** to freeColumnList().
          203  +**
          204  +** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
          205  +** value that needs to be preserved, then azCol[0] is filled in with the
          206  +** name of the rowid column.
          207  +**
          208  +** The first regular column in the table is azCol[1].  The list is terminated
          209  +** by an entry with azCol[i]==0.
          210  +*/
          211  +static char **tableColumnList(DState *p, const char *zTab){
          212  +  char **azCol = 0;
          213  +  sqlite3_stmt *pStmt = 0;
          214  +  char *zSql;
          215  +  int nCol = 0;
          216  +  int nAlloc = 0;
          217  +  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
          218  +  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
          219  +  int preserveRowid = 1;
          220  +  int rc;
          221  +
          222  +  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
          223  +  if( zSql==0 ) return 0;
          224  +  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
          225  +  sqlite3_free(zSql);
          226  +  if( rc ) return 0;
          227  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          228  +    if( nCol>=nAlloc-2 ){
          229  +      char **azNew;
          230  +      nAlloc = nAlloc*2 + nCol + 10;
          231  +      azNew = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
          232  +      if( azNew==0 ) goto col_oom;
          233  +      azCol = azNew;
          234  +      azCol[0] = 0;
          235  +    }
          236  +    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
          237  +    if( azCol[nCol]==0 ) goto col_oom;
          238  +    if( sqlite3_column_int(pStmt, 5) ){
          239  +      nPK++;
          240  +      if( nPK==1
          241  +       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
          242  +                          "INTEGER")==0 
          243  +      ){
          244  +        isIPK = 1;
          245  +      }else{
          246  +        isIPK = 0;
          247  +      }
          248  +    }
          249  +  }
          250  +  sqlite3_finalize(pStmt);
          251  +  pStmt = 0;
          252  +  azCol[nCol+1] = 0;
          253  +
          254  +  /* The decision of whether or not a rowid really needs to be preserved
          255  +  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
          256  +  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
          257  +  ** rowids on tables where the rowid is inaccessible because there are other
          258  +  ** columns in the table named "rowid", "_rowid_", and "oid".
          259  +  */
          260  +  if( isIPK ){
          261  +    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
          262  +    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
          263  +    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
          264  +    ** ROWID aliases.  To distinguish these cases, check to see if
          265  +    ** there is a "pk" entry in "PRAGMA index_list".  There will be
          266  +    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
          267  +    */
          268  +    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
          269  +                           " WHERE origin='pk'", zTab);
          270  +    if( zSql==0 ) goto col_oom;
          271  +    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
          272  +    sqlite3_free(zSql);
          273  +    if( rc ){
          274  +      freeColumnList(azCol);
          275  +      return 0;
          276  +    }
          277  +    rc = sqlite3_step(pStmt);
          278  +    sqlite3_finalize(pStmt);
          279  +    pStmt = 0;
          280  +    preserveRowid = rc==SQLITE_ROW;
          281  +  }
          282  +  if( preserveRowid ){
          283  +    /* Only preserve the rowid if we can find a name to use for the
          284  +    ** rowid */
          285  +    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
          286  +    int i, j;
          287  +    for(j=0; j<3; j++){
          288  +      for(i=1; i<=nCol; i++){
          289  +        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
          290  +      }
          291  +      if( i>nCol ){
          292  +        /* At this point, we know that azRowid[j] is not the name of any
          293  +        ** ordinary column in the table.  Verify that azRowid[j] is a valid
          294  +        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
          295  +        ** tables will fail this last check */
          296  +        int rc;
          297  +        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
          298  +        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
          299  +        break;
          300  +      }
          301  +    }
          302  +  }
          303  +  return azCol;
          304  +
          305  +col_oom:
          306  +  sqlite3_finalize(pStmt);
          307  +  freeColumnList(azCol);
          308  +  p->nErr++;
          309  +  p->rc = SQLITE_NOMEM;
          310  +  return 0;
          311  +}
          312  +
          313  +/*
          314  +** Send mprintf-formatted content to the output callback.
          315  +*/
          316  +static void output_formatted(DState *p, const char *zFormat, ...){
          317  +  va_list ap;
          318  +  char *z;
          319  +  va_start(ap, zFormat);
          320  +  z = sqlite3_vmprintf(zFormat, ap);
          321  +  va_end(ap);
          322  +  p->xCallback(z, p->pArg);
          323  +  sqlite3_free(z);
          324  +}
          325  +
          326  +/*
          327  +** Output the given string as a quoted string using SQL quoting conventions.
          328  +**
          329  +** The "\n" and "\r" characters are converted to char(10) and char(13)
          330  +** to prevent them from being transformed by end-of-line translators.
          331  +*/
          332  +static void output_quoted_string(DState *p, const unsigned char *z){
          333  +  int i;
          334  +  char c;
          335  +  int inQuote = 0;
          336  +  int bStarted = 0;
          337  +
          338  +  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
          339  +  if( c==0 ){
          340  +    output_formatted(p, "'%s'", z);
          341  +    return;
          342  +  }
          343  +  while( *z ){
          344  +    for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
          345  +    if( c=='\'' ) i++;
          346  +    if( i ){
          347  +      if( !inQuote ){
          348  +        if( bStarted ) p->xCallback("||", p->pArg);
          349  +        p->xCallback("'", p->pArg);
          350  +        inQuote = 1;
          351  +      }
          352  +      output_formatted(p, "%.*s", i, z);
          353  +      z += i;
          354  +      bStarted = 1;
          355  +    }
          356  +    if( c=='\'' ){
          357  +      p->xCallback("'", p->pArg);
          358  +      continue;
          359  +    }
          360  +    if( inQuote ){
          361  +      p->xCallback("'", p->pArg);
          362  +      inQuote = 0;
          363  +    }
          364  +    if( c==0 ){
          365  +      break;
          366  +    }
          367  +    for(i=0; (c = z[i])=='\r' || c=='\n'; i++){
          368  +      if( bStarted ) p->xCallback("||", p->pArg);
          369  +      output_formatted(p, "char(%d)", c);
          370  +      bStarted = 1;
          371  +    }
          372  +    z += i;
          373  +  }
          374  +  if( inQuote ) p->xCallback("'", p->pArg);
          375  +}
          376  +
          377  +/*
          378  +** This is an sqlite3_exec callback routine used for dumping the database.
          379  +** Each row received by this callback consists of a table name,
          380  +** the table type ("index" or "table") and SQL to create the table.
          381  +** This routine should print text sufficient to recreate the table.
          382  +*/
          383  +static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
          384  +  int rc;
          385  +  const char *zTable;
          386  +  const char *zType;
          387  +  const char *zSql;
          388  +  DState *p = (DState*)pArg;
          389  +  sqlite3_stmt *pStmt;
          390  +
          391  +  (void)azCol;
          392  +  if( nArg!=3 ) return 1;
          393  +  zTable = azArg[0];
          394  +  zType = azArg[1];
          395  +  zSql = azArg[2];
          396  +
          397  +  if( strcmp(zTable, "sqlite_sequence")==0 ){
          398  +    p->xCallback("DELETE FROM sqlite_sequence;\n", p->pArg);
          399  +  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
          400  +    p->xCallback("ANALYZE sqlite_master;\n", p->pArg);
          401  +  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
          402  +    return 0;
          403  +  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
          404  +    if( !p->writableSchema ){
          405  +      p->xCallback("PRAGMA writable_schema=ON;\n", p->pArg);
          406  +      p->writableSchema = 1;
          407  +    }
          408  +    output_formatted(p,
          409  +       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
          410  +       "VALUES('table','%q','%q',0,'%q');",
          411  +       zTable, zTable, zSql);
          412  +    return 0;
          413  +  }else{
          414  +    if( sqlite3_strglob("CREATE TABLE ['\"]*", zSql)==0 ){
          415  +      p->xCallback("CREATE TABLE IF NOT EXISTS ", p->pArg);
          416  +      p->xCallback(zSql+13, p->pArg);
          417  +    }else{
          418  +      p->xCallback(zSql, p->pArg);
          419  +    }
          420  +    p->xCallback(";\n", p->pArg);
          421  +  }
          422  +
          423  +  if( strcmp(zType, "table")==0 ){
          424  +    DText sSelect;
          425  +    DText sTable;
          426  +    char **azCol;
          427  +    int i;
          428  +    int nCol;
          429  +
          430  +    azCol = tableColumnList(p, zTable);
          431  +    if( azCol==0 ) return 0;
          432  +
          433  +    initText(&sTable);
          434  +    appendText(&sTable, "INSERT INTO ", 0);
          435  +
          436  +    /* Always quote the table name, even if it appears to be pure ascii,
          437  +    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
          438  +    appendText(&sTable, zTable, quoteChar(zTable));
          439  +
          440  +    /* If preserving the rowid, add a column list after the table name.
          441  +    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
          442  +    ** instead of the usual "INSERT INTO tab VALUES(...)".
          443  +    */
          444  +    if( azCol[0] ){
          445  +      appendText(&sTable, "(", 0);
          446  +      appendText(&sTable, azCol[0], 0);
          447  +      for(i=1; azCol[i]; i++){
          448  +        appendText(&sTable, ",", 0);
          449  +        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
          450  +      }
          451  +      appendText(&sTable, ")", 0);
          452  +    }
          453  +    appendText(&sTable, " VALUES(", 0);
          454  +
          455  +    /* Build an appropriate SELECT statement */
          456  +    initText(&sSelect);
          457  +    appendText(&sSelect, "SELECT ", 0);
          458  +    if( azCol[0] ){
          459  +      appendText(&sSelect, azCol[0], 0);
          460  +      appendText(&sSelect, ",", 0);
          461  +    }
          462  +    for(i=1; azCol[i]; i++){
          463  +      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
          464  +      if( azCol[i+1] ){
          465  +        appendText(&sSelect, ",", 0);
          466  +      }
          467  +    }
          468  +    nCol = i;
          469  +    if( azCol[0]==0 ) nCol--;
          470  +    freeColumnList(azCol);
          471  +    appendText(&sSelect, " FROM ", 0);
          472  +    appendText(&sSelect, zTable, quoteChar(zTable));
          473  +
          474  +    rc = sqlite3_prepare_v2(p->db, sSelect.z, -1, &pStmt, 0);
          475  +    if( rc!=SQLITE_OK ){
          476  +      p->nErr++;
          477  +      if( p->rc==SQLITE_OK ) p->rc = rc;
          478  +    }else{
          479  +      while( SQLITE_ROW==sqlite3_step(pStmt) ){
          480  +        p->xCallback(sTable.z, p->pArg);
          481  +        for(i=0; i<nCol; i++){
          482  +          if( i ) p->xCallback(",", p->pArg);
          483  +          switch( sqlite3_column_type(pStmt,i) ){
          484  +            case SQLITE_INTEGER: {
          485  +              output_formatted(p, "%lld", sqlite3_column_int64(pStmt,i));
          486  +              break;
          487  +            }
          488  +            case SQLITE_FLOAT: {
          489  +              double r = sqlite3_column_double(pStmt,i);
          490  +              output_formatted(p, "%!.20g", r);
          491  +              break;
          492  +            }
          493  +            case SQLITE_NULL: {
          494  +              p->xCallback("NULL", p->pArg);
          495  +              break;
          496  +            }
          497  +            case SQLITE_TEXT: {
          498  +              output_quoted_string(p, sqlite3_column_text(pStmt,i));
          499  +              break;
          500  +            }
          501  +            case SQLITE_BLOB: {
          502  +              int nByte = sqlite3_column_bytes(pStmt,i);
          503  +              unsigned char *a = (unsigned char*)sqlite3_column_blob(pStmt,i);
          504  +              int j;
          505  +              p->xCallback("x'", p->pArg);
          506  +              for(j=0; j<nByte; j++){
          507  +                char zWord[3];
          508  +                zWord[0] = "0123456789abcdef"[(a[j]>>4)&15];
          509  +                zWord[1] = "0123456789abcdef"[a[j]&15];
          510  +                zWord[2] = 0;
          511  +                p->xCallback(zWord, p->pArg);
          512  +              }
          513  +              p->xCallback("'", p->pArg);
          514  +              break;
          515  +            }
          516  +          }
          517  +        }
          518  +        p->xCallback(");\n", p->pArg);
          519  +      }
          520  +    }
          521  +    sqlite3_finalize(pStmt);
          522  +    freeText(&sTable);
          523  +    freeText(&sSelect);
          524  +  }
          525  +  return 0;
          526  +}
          527  +
          528  +
          529  +/*
          530  +** Execute a query statement that will generate SQL output.  Print
          531  +** the result columns, comma-separated, on a line and then add a
          532  +** semicolon terminator to the end of that line.
          533  +**
          534  +** If the number of columns is 1 and that column contains text "--"
          535  +** then write the semicolon on a separate line.  That way, if a
          536  +** "--" comment occurs at the end of the statement, the comment
          537  +** won't consume the semicolon terminator.
          538  +*/
          539  +static void output_sql_from_query(
          540  +  DState *p,               /* Query context */
          541  +  const char *zSelect,     /* SELECT statement to extract content */
          542  +  ...
          543  +){
          544  +  sqlite3_stmt *pSelect;
          545  +  int rc;
          546  +  int nResult;
          547  +  int i;
          548  +  const char *z;
          549  +  char *zSql;
          550  +  va_list ap;
          551  +  va_start(ap, zSelect);
          552  +  zSql = sqlite3_vmprintf(zSelect, ap);
          553  +  va_end(ap);
          554  +  if( zSql==0 ){
          555  +    p->rc = SQLITE_NOMEM;
          556  +    p->nErr++;
          557  +    return;
          558  +  }
          559  +  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pSelect, 0);
          560  +  sqlite3_free(zSql);
          561  +  if( rc!=SQLITE_OK || !pSelect ){
          562  +    output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc,
          563  +                sqlite3_errmsg(p->db));
          564  +    p->nErr++;
          565  +    return;
          566  +  }
          567  +  rc = sqlite3_step(pSelect);
          568  +  nResult = sqlite3_column_count(pSelect);
          569  +  while( rc==SQLITE_ROW ){
          570  +    z = (const char*)sqlite3_column_text(pSelect, 0);
          571  +    p->xCallback(z, p->pArg);
          572  +    for(i=1; i<nResult; i++){
          573  +      p->xCallback(",", p->pArg);
          574  +      p->xCallback((const char*)sqlite3_column_text(pSelect,i), p->pArg);
          575  +    }
          576  +    if( z==0 ) z = "";
          577  +    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
          578  +    if( z[0] ){
          579  +      p->xCallback("\n;\n", p->pArg);
          580  +    }else{
          581  +      p->xCallback(";\n", p->pArg);
          582  +    }
          583  +    rc = sqlite3_step(pSelect);
          584  +  }
          585  +  rc = sqlite3_finalize(pSelect);
          586  +  if( rc!=SQLITE_OK ){
          587  +    output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc,
          588  +                     sqlite3_errmsg(p->db));
          589  +    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
          590  +  }
          591  +}
          592  +
          593  +/*
          594  +** Run zQuery.  Use dump_callback() as the callback routine so that
          595  +** the contents of the query are output as SQL statements.
          596  +**
          597  +** If we get a SQLITE_CORRUPT error, rerun the query after appending
          598  +** "ORDER BY rowid DESC" to the end.
          599  +*/
          600  +static void run_schema_dump_query(
          601  +  DState *p,
          602  +  const char *zQuery,
          603  +  ...
          604  +){
          605  +  char *zErr = 0;
          606  +  char *z;
          607  +  va_list ap;
          608  +  va_start(ap, zQuery);
          609  +  z = sqlite3_vmprintf(zQuery, ap);
          610  +  va_end(ap); 
          611  +  sqlite3_exec(p->db, z, dump_callback, p, &zErr);
          612  +  sqlite3_free(z);
          613  +  if( zErr ){
          614  +    output_formatted(p, "/****** %s ******/\n", zErr);
          615  +    sqlite3_free(zErr);
          616  +    p->nErr++;
          617  +    zErr = 0;
          618  +  }
          619  +}
          620  +
          621  +/*
          622  +** Convert an SQLite database into SQL statements that will recreate that
          623  +** database.
          624  +*/
          625  +int sqlite3_db_dump(
          626  +  sqlite3 *db,               /* The database connection */
          627  +  const char *zSchema,       /* Which schema to dump.  Usually "main". */
          628  +  const char *zTable,        /* Which table to dump.  NULL means everything. */
          629  +  int (*xCallback)(const char*,void*),   /* Output sent to this callback */
          630  +  void *pArg                             /* Second argument of the callback */
          631  +){
          632  +  DState x;
          633  +  memset(&x, 0, sizeof(x));
          634  +  x.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
          635  +  if( x.rc ) return x.rc;
          636  +  x.db = db;
          637  +  x.xCallback = xCallback;
          638  +  x.pArg = pArg;
          639  +  xCallback("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n", pArg);
          640  +  if( zTable==0 ){
          641  +    run_schema_dump_query(&x,
          642  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          643  +      "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'",
          644  +      zSchema
          645  +    );
          646  +    run_schema_dump_query(&x,
          647  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          648  +      "WHERE name=='sqlite_sequence'", zSchema
          649  +    );
          650  +    output_sql_from_query(&x,
          651  +      "SELECT sql FROM sqlite_master "
          652  +      "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
          653  +    );
          654  +  }else{
          655  +    run_schema_dump_query(&x,
          656  +      "SELECT name, type, sql FROM \"%w\".sqlite_master "
          657  +      "WHERE tbl_name=%Q COLLATE nocase AND type=='table'"
          658  +      "  AND sql NOT NULL",
          659  +      zSchema, zTable
          660  +    );
          661  +    output_sql_from_query(&x,
          662  +      "SELECT sql FROM \"%w\".sqlite_master "
          663  +      "WHERE sql NOT NULL"
          664  +      "  AND type IN ('index','trigger','view')"
          665  +      "  AND tbl_name=%Q COLLATE nocase",
          666  +      zSchema, zTable
          667  +    ); 
          668  +  }
          669  +  if( x.writableSchema ){
          670  +    xCallback("PRAGMA writable_schema=OFF;\n", pArg);
          671  +  }
          672  +  xCallback(x.nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n", pArg);
          673  +  sqlite3_exec(db, "COMMIT", 0, 0, 0);
          674  +  return x.rc;
          675  +}
          676  +
          677  +
          678  +
          679  +/* The generic subroutine is above.  The code the follows implements
          680  +** the command-line interface.
          681  +*/
          682  +#ifdef DBDUMP_STANDALONE
          683  +#include <stdio.h>
          684  +
          685  +/*
          686  +** Command-line interface
          687  +*/
          688  +int main(int argc, char **argv){
          689  +  sqlite3 *db;
          690  +  const char *zDb;
          691  +  const char *zSchema;
          692  +  const char *zTable = 0;
          693  +  int rc;
          694  +
          695  +  if( argc<2 || argc>4 ){
          696  +    fprintf(stderr, "Usage: %s DATABASE ?SCHEMA? ?TABLE?\n", argv[0]);
          697  +    return 1;
          698  +  }
          699  +  zDb = argv[1];
          700  +  zSchema = argc>=3 ? argv[2] : "main";
          701  +  zTable = argc==4 ? argv[3] : 0;
          702  +
          703  +  rc = sqlite3_open(zDb, &db);
          704  +  if( rc ){
          705  +    fprintf(stderr, "Cannot open \"%s\": %s\n", zDb, sqlite3_errmsg(db));
          706  +    sqlite3_close(db);
          707  +    return 1;
          708  +  }
          709  +  rc = sqlite3_db_dump(db, zSchema, zTable, 
          710  +          (int(*)(const char*,void*))fputs, (void*)stdout);
          711  +  if( rc ){
          712  +    fprintf(stderr, "Error: sqlite3_db_dump() returns %d\n", rc);
          713  +  }
          714  +  sqlite3_close(db);
          715  +  return rc!=SQLITE_OK;  
          716  +}
          717  +#endif /* DBDUMP_STANDALONE */

Changes to ext/misc/json1.c.

    18     18   ** For the time being, all JSON is stored as pure text.  (We might add
    19     19   ** a JSONB type in the future which stores a binary encoding of JSON in
    20     20   ** a BLOB, but there is no support for JSONB in the current implementation.
    21     21   ** This implementation parses JSON text at 250 MB/s, so it is hard to see
    22     22   ** how JSONB might improve on that.)
    23     23   */
    24     24   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
    25         -#if !defined(_SQLITEINT_H_)
           25  +#if !defined(SQLITEINT_H)
    26     26   #include "sqlite3ext.h"
    27     27   #endif
    28     28   SQLITE_EXTENSION_INIT1
    29     29   #include <assert.h>
    30     30   #include <string.h>
    31     31   #include <stdlib.h>
    32     32   #include <stdarg.h>
................................................................................
   134    134   };
   135    135   
   136    136   /* Bit values for the JsonNode.jnFlag field
   137    137   */
   138    138   #define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
   139    139   #define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
   140    140   #define JNODE_REMOVE  0x04         /* Do not output */
   141         -#define JNODE_REPLACE 0x08         /* Replace with JsonNode.iVal */
   142         -#define JNODE_APPEND  0x10         /* More ARRAY/OBJECT entries at u.iAppend */
   143         -#define JNODE_LABEL   0x20         /* Is a label of an object */
          141  +#define JNODE_REPLACE 0x08         /* Replace with JsonNode.u.iReplace */
          142  +#define JNODE_PATCH   0x10         /* Patch with JsonNode.u.pPatch */
          143  +#define JNODE_APPEND  0x20         /* More ARRAY/OBJECT entries at u.iAppend */
          144  +#define JNODE_LABEL   0x40         /* Is a label of an object */
   144    145   
   145    146   
   146    147   /* A single node of parsed JSON
   147    148   */
   148    149   struct JsonNode {
   149    150     u8 eType;              /* One of the JSON_ type values */
   150    151     u8 jnFlags;            /* JNODE flags */
   151         -  u8 iVal;               /* Replacement value when JNODE_REPLACE */
   152    152     u32 n;                 /* Bytes of content, or number of sub-nodes */
   153    153     union {
   154    154       const char *zJContent; /* Content for INT, REAL, and STRING */
   155    155       u32 iAppend;           /* More terms for ARRAY and OBJECT */
   156    156       u32 iKey;              /* Key for ARRAY objects in json_tree() */
          157  +    u32 iReplace;          /* Replacement content for JNODE_REPLACE */
          158  +    JsonNode *pPatch;      /* Node chain of patch for JNODE_PATCH */
   157    159     } u;
   158    160   };
   159    161   
   160    162   /* A completely parsed JSON string
   161    163   */
   162    164   struct JsonParse {
   163    165     u32 nNode;         /* Number of slots of aNode[] used */
................................................................................
   406    408   ** the number of JsonNode objects that are encoded.
   407    409   */
   408    410   static void jsonRenderNode(
   409    411     JsonNode *pNode,               /* The node to render */
   410    412     JsonString *pOut,              /* Write JSON here */
   411    413     sqlite3_value **aReplace       /* Replacement values */
   412    414   ){
          415  +  if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
          416  +    if( pNode->jnFlags & JNODE_REPLACE ){
          417  +      jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
          418  +      return;
          419  +    }
          420  +    pNode = pNode->u.pPatch;
          421  +  }
   413    422     switch( pNode->eType ){
   414    423       default: {
   415    424         assert( pNode->eType==JSON_NULL );
   416    425         jsonAppendRaw(pOut, "null", 4);
   417    426         break;
   418    427       }
   419    428       case JSON_TRUE: {
................................................................................
   437    446         break;
   438    447       }
   439    448       case JSON_ARRAY: {
   440    449         u32 j = 1;
   441    450         jsonAppendChar(pOut, '[');
   442    451         for(;;){
   443    452           while( j<=pNode->n ){
   444         -          if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
   445         -            if( pNode[j].jnFlags & JNODE_REPLACE ){
   446         -              jsonAppendSeparator(pOut);
   447         -              jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
   448         -            }
   449         -          }else{
          453  +          if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
   450    454               jsonAppendSeparator(pOut);
   451    455               jsonRenderNode(&pNode[j], pOut, aReplace);
   452    456             }
   453    457             j += jsonNodeSize(&pNode[j]);
   454    458           }
   455    459           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
   456    460           pNode = &pNode[pNode->u.iAppend];
................................................................................
   464    468         jsonAppendChar(pOut, '{');
   465    469         for(;;){
   466    470           while( j<=pNode->n ){
   467    471             if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
   468    472               jsonAppendSeparator(pOut);
   469    473               jsonRenderNode(&pNode[j], pOut, aReplace);
   470    474               jsonAppendChar(pOut, ':');
   471         -            if( pNode[j+1].jnFlags & JNODE_REPLACE ){
   472         -              jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
   473         -            }else{
   474         -              jsonRenderNode(&pNode[j+1], pOut, aReplace);
   475         -            }
          475  +            jsonRenderNode(&pNode[j+1], pOut, aReplace);
   476    476             }
   477    477             j += 1 + jsonNodeSize(&pNode[j+1]);
   478    478           }
   479    479           if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
   480    480           pNode = &pNode[pNode->u.iAppend];
   481    481           j = 1;
   482    482         }
................................................................................
   695    695     JsonNode *p;
   696    696     if( pParse->nNode>=pParse->nAlloc ){
   697    697       return jsonParseAddNodeExpand(pParse, eType, n, zContent);
   698    698     }
   699    699     p = &pParse->aNode[pParse->nNode];
   700    700     p->eType = (u8)eType;
   701    701     p->jnFlags = 0;
   702         -  p->iVal = 0;
   703    702     p->n = n;
   704    703     p->u.zJContent = zContent;
   705    704     return pParse->nNode++;
   706    705   }
   707    706   
   708    707   /*
   709    708   ** Return true if z[] begins with 4 (or more) hexadecimal digits
................................................................................
  1161   1160   ){
  1162   1161     char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
  1163   1162                                  zFuncName);
  1164   1163     sqlite3_result_error(pCtx, zMsg, -1);
  1165   1164     sqlite3_free(zMsg);     
  1166   1165   }
  1167   1166   
         1167  +/*
         1168  +** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
         1169  +*/
         1170  +static void jsonRemoveAllNulls(JsonNode *pNode){
         1171  +  int i, n;
         1172  +  assert( pNode->eType==JSON_OBJECT );
         1173  +  n = pNode->n;
         1174  +  for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
         1175  +    switch( pNode[i].eType ){
         1176  +      case JSON_NULL:
         1177  +        pNode[i].jnFlags |= JNODE_REMOVE;
         1178  +        break;
         1179  +      case JSON_OBJECT:
         1180  +        jsonRemoveAllNulls(&pNode[i]);
         1181  +        break;
         1182  +    }
         1183  +  }
         1184  +}
         1185  +
  1168   1186   
  1169   1187   /****************************************************************************
  1170   1188   ** SQL functions used for testing and debugging
  1171   1189   ****************************************************************************/
  1172   1190   
  1173   1191   #ifdef SQLITE_DEBUG
  1174   1192   /*
................................................................................
  1352   1370       jsonAppendChar(&jx, ']');
  1353   1371       jsonResult(&jx);
  1354   1372       sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1355   1373     }
  1356   1374     jsonReset(&jx);
  1357   1375     jsonParseReset(&x);
  1358   1376   }
         1377  +
         1378  +/* This is the RFC 7396 MergePatch algorithm.
         1379  +*/
         1380  +static JsonNode *jsonMergePatch(
         1381  +  JsonParse *pParse,   /* The JSON parser that contains the TARGET */
         1382  +  int iTarget,         /* Node of the TARGET in pParse */
         1383  +  JsonNode *pPatch     /* The PATCH */
         1384  +){
         1385  +  u32 i, j;
         1386  +  u32 iRoot;
         1387  +  JsonNode *pTarget;
         1388  +  if( pPatch->eType!=JSON_OBJECT ){
         1389  +    return pPatch;
         1390  +  }
         1391  +  assert( iTarget>=0 && iTarget<pParse->nNode );
         1392  +  pTarget = &pParse->aNode[iTarget];
         1393  +  assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
         1394  +  if( pTarget->eType!=JSON_OBJECT ){
         1395  +    jsonRemoveAllNulls(pPatch);
         1396  +    return pPatch;
         1397  +  }
         1398  +  iRoot = iTarget;
         1399  +  for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
         1400  +    int nKey;
         1401  +    const char *zKey;
         1402  +    assert( pPatch[i].eType==JSON_STRING );
         1403  +    assert( pPatch[i].jnFlags & JNODE_LABEL );
         1404  +    nKey = pPatch[i].n;
         1405  +    zKey = pPatch[i].u.zJContent;
         1406  +    assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
         1407  +    for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
         1408  +      assert( pTarget[j].eType==JSON_STRING );
         1409  +      assert( pTarget[j].jnFlags & JNODE_LABEL );
         1410  +      assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
         1411  +      if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
         1412  +        if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
         1413  +        if( pPatch[i+1].eType==JSON_NULL ){
         1414  +          pTarget[j+1].jnFlags |= JNODE_REMOVE;
         1415  +        }else{
         1416  +          JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
         1417  +          if( pNew==0 ) return 0;
         1418  +          pTarget = &pParse->aNode[iTarget];
         1419  +          if( pNew!=&pTarget[j+1] ){
         1420  +            pTarget[j+1].u.pPatch = pNew;
         1421  +            pTarget[j+1].jnFlags |= JNODE_PATCH;
         1422  +          }
         1423  +        }
         1424  +        break;
         1425  +      }
         1426  +    }
         1427  +    if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
         1428  +      int iStart, iPatch;
         1429  +      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
         1430  +      jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
         1431  +      iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
         1432  +      if( pParse->oom ) return 0;
         1433  +      jsonRemoveAllNulls(pPatch);
         1434  +      pTarget = &pParse->aNode[iTarget];
         1435  +      pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
         1436  +      pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
         1437  +      iRoot = iStart;
         1438  +      pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
         1439  +      pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
         1440  +    }
         1441  +  }
         1442  +  return pTarget;
         1443  +}
         1444  +
         1445  +/*
         1446  +** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
         1447  +** object that is the result of running the RFC 7396 MergePatch() algorithm
         1448  +** on the two arguments.
         1449  +*/
         1450  +static void jsonPatchFunc(
         1451  +  sqlite3_context *ctx,
         1452  +  int argc,
         1453  +  sqlite3_value **argv
         1454  +){
         1455  +  JsonParse x;     /* The JSON that is being patched */
         1456  +  JsonParse y;     /* The patch */
         1457  +  JsonNode *pResult;   /* The result of the merge */
         1458  +
         1459  +  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
         1460  +  if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
         1461  +    jsonParseReset(&x);
         1462  +    return;
         1463  +  }
         1464  +  pResult = jsonMergePatch(&x, 0, y.aNode);
         1465  +  assert( pResult!=0 || x.oom );
         1466  +  if( pResult ){
         1467  +    jsonReturnJson(pResult, ctx, 0);
         1468  +  }else{
         1469  +    sqlite3_result_error_nomem(ctx);
         1470  +  }
         1471  +  jsonParseReset(&x);
         1472  +  jsonParseReset(&y);
         1473  +}
         1474  +
  1359   1475   
  1360   1476   /*
  1361   1477   ** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
  1362   1478   ** object that contains all name/value given in arguments.  Or if any name
  1363   1479   ** is not a string or if any value is a BLOB, throw an error.
  1364   1480   */
  1365   1481   static void jsonObjectFunc(
................................................................................
  1456   1572     assert( x.nNode );
  1457   1573     for(i=1; i<(u32)argc; i+=2){
  1458   1574       zPath = (const char*)sqlite3_value_text(argv[i]);
  1459   1575       pNode = jsonLookup(&x, zPath, 0, ctx);
  1460   1576       if( x.nErr ) goto replace_err;
  1461   1577       if( pNode ){
  1462   1578         pNode->jnFlags |= (u8)JNODE_REPLACE;
  1463         -      pNode->iVal = (u8)(i+1);
         1579  +      pNode->u.iReplace = i + 1;
  1464   1580       }
  1465   1581     }
  1466   1582     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
  1467         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
         1583  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
  1468   1584     }else{
  1469   1585       jsonReturnJson(x.aNode, ctx, argv);
  1470   1586     }
  1471   1587   replace_err:
  1472   1588     jsonParseReset(&x);
  1473   1589   }
  1474   1590   
................................................................................
  1510   1626       if( x.oom ){
  1511   1627         sqlite3_result_error_nomem(ctx);
  1512   1628         goto jsonSetDone;
  1513   1629       }else if( x.nErr ){
  1514   1630         goto jsonSetDone;
  1515   1631       }else if( pNode && (bApnd || bIsSet) ){
  1516   1632         pNode->jnFlags |= (u8)JNODE_REPLACE;
  1517         -      pNode->iVal = (u8)(i+1);
         1633  +      pNode->u.iReplace = i + 1;
  1518   1634       }
  1519   1635     }
  1520   1636     if( x.aNode[0].jnFlags & JNODE_REPLACE ){
  1521         -    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
         1637  +    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
  1522   1638     }else{
  1523   1639       jsonReturnJson(x.aNode, ctx, argv);
  1524   1640     }
  1525   1641   jsonSetDone:
  1526   1642     jsonParseReset(&x);
  1527   1643   }
  1528   1644   
................................................................................
  2157   2273       { "json",                 1, 0,   jsonRemoveFunc        },
  2158   2274       { "json_array",          -1, 0,   jsonArrayFunc         },
  2159   2275       { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
  2160   2276       { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
  2161   2277       { "json_extract",        -1, 0,   jsonExtractFunc       },
  2162   2278       { "json_insert",         -1, 0,   jsonSetFunc           },
  2163   2279       { "json_object",         -1, 0,   jsonObjectFunc        },
         2280  +    { "json_patch",           2, 0,   jsonPatchFunc         },
  2164   2281       { "json_quote",           1, 0,   jsonQuoteFunc         },
  2165   2282       { "json_remove",         -1, 0,   jsonRemoveFunc        },
  2166   2283       { "json_replace",        -1, 0,   jsonReplaceFunc       },
  2167   2284       { "json_set",            -1, 1,   jsonSetFunc           },
  2168   2285       { "json_type",            1, 0,   jsonTypeFunc          },
  2169   2286       { "json_type",            2, 0,   jsonTypeFunc          },
  2170   2287       { "json_valid",           1, 0,   jsonValidFunc         },

Added ext/misc/sha1.c.

            1  +/*
            2  +** 2017-01-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 SQLite extension implements a functions that compute SHA1 hashes.
           14  +** Two SQL functions are implemented:
           15  +**
           16  +**     sha1(X)
           17  +**     sha1_query(Y)
           18  +**
           19  +** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
           20  +** X is NULL.
           21  +**
           22  +** The sha1_query(Y) function evalutes all queries in the SQL statements of Y
           23  +** and returns a hash of their results.
           24  +*/
           25  +#include "sqlite3ext.h"
           26  +SQLITE_EXTENSION_INIT1
           27  +#include <assert.h>
           28  +#include <string.h>
           29  +#include <stdarg.h>
           30  +
           31  +/******************************************************************************
           32  +** The Hash Engine
           33  +*/
           34  +/* Context for the SHA1 hash */
           35  +typedef struct SHA1Context SHA1Context;
           36  +struct SHA1Context {
           37  +  unsigned int state[5];
           38  +  unsigned int count[2];
           39  +  unsigned char buffer[64];
           40  +};
           41  +
           42  +
           43  +#if __GNUC__ && (defined(__i386__) || defined(__x86_64__))
           44  +/*
           45  + * GCC by itself only generates left rotates.  Use right rotates if
           46  + * possible to be kinder to dinky implementations with iterative rotate
           47  + * instructions.
           48  + */
           49  +#define SHA_ROT(op, x, k) \
           50  +        ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; })
           51  +#define rol(x,k) SHA_ROT("roll", x, k)
           52  +#define ror(x,k) SHA_ROT("rorl", x, k)
           53  +
           54  +#else
           55  +/* Generic C equivalent */
           56  +#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
           57  +#define rol(x,k) SHA_ROT(x,k,32-(k))
           58  +#define ror(x,k) SHA_ROT(x,32-(k),k)
           59  +#endif
           60  +
           61  +
           62  +#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
           63  +    |(rol(block[i],8)&0x00FF00FF))
           64  +#define blk0be(i) block[i]
           65  +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
           66  +    ^block[(i+2)&15]^block[i&15],1))
           67  +
           68  +/*
           69  + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
           70  + *
           71  + * Rl0() for little-endian and Rb0() for big-endian.  Endianness is
           72  + * determined at run-time.
           73  + */
           74  +#define Rl0(v,w,x,y,z,i) \
           75  +    z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
           76  +#define Rb0(v,w,x,y,z,i) \
           77  +    z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
           78  +#define R1(v,w,x,y,z,i) \
           79  +    z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
           80  +#define R2(v,w,x,y,z,i) \
           81  +    z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
           82  +#define R3(v,w,x,y,z,i) \
           83  +    z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
           84  +#define R4(v,w,x,y,z,i) \
           85  +    z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
           86  +
           87  +/*
           88  + * Hash a single 512-bit block. This is the core of the algorithm.
           89  + */
           90  +void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){
           91  +  unsigned int qq[5]; /* a, b, c, d, e; */
           92  +  static int one = 1;
           93  +  unsigned int block[16];
           94  +  memcpy(block, buffer, 64);
           95  +  memcpy(qq,state,5*sizeof(unsigned int));
           96  +
           97  +#define a qq[0]
           98  +#define b qq[1]
           99  +#define c qq[2]
          100  +#define d qq[3]
          101  +#define e qq[4]
          102  +
          103  +  /* Copy p->state[] to working vars */
          104  +  /*
          105  +  a = state[0];
          106  +  b = state[1];
          107  +  c = state[2];
          108  +  d = state[3];
          109  +  e = state[4];
          110  +  */
          111  +
          112  +  /* 4 rounds of 20 operations each. Loop unrolled. */
          113  +  if( 1 == *(unsigned char*)&one ){
          114  +    Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
          115  +    Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
          116  +    Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
          117  +    Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
          118  +  }else{
          119  +    Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
          120  +    Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
          121  +    Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
          122  +    Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
          123  +  }
          124  +  R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
          125  +  R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
          126  +  R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
          127  +  R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
          128  +  R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
          129  +  R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
          130  +  R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
          131  +  R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
          132  +  R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
          133  +  R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
          134  +  R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
          135  +  R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
          136  +  R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
          137  +  R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
          138  +  R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
          139  +  R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
          140  +
          141  +  /* Add the working vars back into context.state[] */
          142  +  state[0] += a;
          143  +  state[1] += b;
          144  +  state[2] += c;
          145  +  state[3] += d;
          146  +  state[4] += e;
          147  +
          148  +#undef a
          149  +#undef b
          150  +#undef c
          151  +#undef d
          152  +#undef e
          153  +}
          154  +
          155  +
          156  +/* Initialize a SHA1 context */
          157  +static void hash_init(SHA1Context *p){
          158  +  /* SHA1 initialization constants */
          159  +  p->state[0] = 0x67452301;
          160  +  p->state[1] = 0xEFCDAB89;
          161  +  p->state[2] = 0x98BADCFE;
          162  +  p->state[3] = 0x10325476;
          163  +  p->state[4] = 0xC3D2E1F0;
          164  +  p->count[0] = p->count[1] = 0;
          165  +}
          166  +
          167  +/* Add new content to the SHA1 hash */
          168  +static void hash_step(
          169  +  SHA1Context *p,                 /* Add content to this context */
          170  +  const unsigned char *data,      /* Data to be added */
          171  +  unsigned int len                /* Number of bytes in data */
          172  +){
          173  +  unsigned int i, j;
          174  +
          175  +  j = p->count[0];
          176  +  if( (p->count[0] += len << 3) < j ){
          177  +    p->count[1] += (len>>29)+1;
          178  +  }
          179  +  j = (j >> 3) & 63;
          180  +  if( (j + len) > 63 ){
          181  +    (void)memcpy(&p->buffer[j], data, (i = 64-j));
          182  +    SHA1Transform(p->state, p->buffer);
          183  +    for(; i + 63 < len; i += 64){
          184  +      SHA1Transform(p->state, &data[i]);
          185  +    }
          186  +    j = 0;
          187  +  }else{
          188  +    i = 0;
          189  +  }
          190  +  (void)memcpy(&p->buffer[j], &data[i], len - i);
          191  +}
          192  +
          193  +/* Compute a string using sqlite3_vsnprintf() and hash it */
          194  +static void hash_step_vformat(
          195  +  SHA1Context *p,                 /* Add content to this context */
          196  +  const char *zFormat,
          197  +  ...
          198  +){
          199  +  va_list ap;
          200  +  int n;
          201  +  char zBuf[50];
          202  +  va_start(ap, zFormat);
          203  +  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
          204  +  va_end(ap);
          205  +  n = (int)strlen(zBuf);
          206  +  hash_step(p, (unsigned char*)zBuf, n);
          207  +}
          208  +
          209  +
          210  +/* Add padding and compute the message digest.  Render the
          211  +** message digest as lower-case hexadecimal and put it into
          212  +** zOut[].  zOut[] must be at least 41 bytes long. */
          213  +static void hash_finish(
          214  +  SHA1Context *p,           /* The SHA1 context to finish and render */
          215  +  char *zOut                /* Store hexadecimal hash here */
          216  +){
          217  +  unsigned int i;
          218  +  unsigned char finalcount[8];
          219  +  unsigned char digest[20];
          220  +  static const char zEncode[] = "0123456789abcdef";
          221  +
          222  +  for (i = 0; i < 8; i++){
          223  +    finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)]
          224  +       >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
          225  +  }
          226  +  hash_step(p, (const unsigned char *)"\200", 1);
          227  +  while ((p->count[0] & 504) != 448){
          228  +    hash_step(p, (const unsigned char *)"\0", 1);
          229  +  }
          230  +  hash_step(p, finalcount, 8);  /* Should cause a SHA1Transform() */
          231  +  for (i = 0; i < 20; i++){
          232  +    digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
          233  +  }
          234  +  for(i=0; i<20; i++){
          235  +    zOut[i*2] = zEncode[(digest[i]>>4)&0xf];
          236  +    zOut[i*2+1] = zEncode[digest[i] & 0xf];
          237  +  }
          238  +  zOut[i*2]= 0;
          239  +}
          240  +/* End of the hashing logic
          241  +*****************************************************************************/
          242  +
          243  +/*
          244  +** Implementation of the sha1(X) function.
          245  +**
          246  +** Return a lower-case hexadecimal rendering of the SHA1 hash of the
          247  +** argument X.  If X is a BLOB, it is hashed as is.  For all other
          248  +** types of input, X is converted into a UTF-8 string and the string
          249  +** is hash without the trailing 0x00 terminator.  The hash of a NULL
          250  +** value is NULL.
          251  +*/
          252  +static void sha1Func(
          253  +  sqlite3_context *context,
          254  +  int argc,
          255  +  sqlite3_value **argv
          256  +){
          257  +  SHA1Context cx;
          258  +  int eType = sqlite3_value_type(argv[0]);
          259  +  int nByte = sqlite3_value_bytes(argv[0]);
          260  +  char zOut[44];
          261  +
          262  +  assert( argc==1 );
          263  +  if( eType==SQLITE_NULL ) return;
          264  +  hash_init(&cx);
          265  +  if( eType==SQLITE_BLOB ){
          266  +    hash_step(&cx, sqlite3_value_blob(argv[0]), nByte);
          267  +  }else{
          268  +    hash_step(&cx, sqlite3_value_text(argv[0]), nByte);
          269  +  }
          270  +  hash_finish(&cx, zOut);
          271  +  sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
          272  +}
          273  +
          274  +/*
          275  +** Implementation of the sha1_query(SQL) function.
          276  +**
          277  +** This function compiles and runs the SQL statement(s) given in the
          278  +** argument. The results are hashed using SHA1 and that hash is returned.
          279  +**
          280  +** The original SQL text is included as part of the hash.
          281  +**
          282  +** The hash is not just a concatenation of the outputs.  Each query
          283  +** is delimited and each row and value within the query is delimited,
          284  +** with all values being marked with their datatypes.
          285  +*/
          286  +static void sha1QueryFunc(
          287  +  sqlite3_context *context,
          288  +  int argc,
          289  +  sqlite3_value **argv
          290  +){
          291  +  sqlite3 *db = sqlite3_context_db_handle(context);
          292  +  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
          293  +  sqlite3_stmt *pStmt = 0;
          294  +  int nCol;                   /* Number of columns in the result set */
          295  +  int i;                      /* Loop counter */
          296  +  int rc;
          297  +  int n;
          298  +  const char *z;
          299  +  SHA1Context cx;
          300  +  char zOut[44];
          301  +
          302  +  assert( argc==1 );
          303  +  if( zSql==0 ) return;
          304  +  hash_init(&cx);
          305  +  while( zSql[0] ){
          306  +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
          307  +    if( rc ){
          308  +      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
          309  +                                   zSql, sqlite3_errmsg(db));
          310  +      sqlite3_finalize(pStmt);
          311  +      sqlite3_result_error(context, zMsg, -1);
          312  +      sqlite3_free(zMsg);
          313  +      return;
          314  +    }
          315  +    if( !sqlite3_stmt_readonly(pStmt) ){
          316  +      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
          317  +      sqlite3_finalize(pStmt);
          318  +      sqlite3_result_error(context, zMsg, -1);
          319  +      sqlite3_free(zMsg);
          320  +      return;
          321  +    }
          322  +    nCol = sqlite3_column_count(pStmt);
          323  +    z = sqlite3_sql(pStmt);
          324  +    n = (int)strlen(z);
          325  +    hash_step_vformat(&cx,"S%d:",n);
          326  +    hash_step(&cx,(unsigned char*)z,n);
          327  +
          328  +    /* Compute a hash over the result of the query */
          329  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          330  +      hash_step(&cx,(const unsigned char*)"R",1);
          331  +      for(i=0; i<nCol; i++){
          332  +        switch( sqlite3_column_type(pStmt,i) ){
          333  +          case SQLITE_NULL: {
          334  +            hash_step(&cx, (const unsigned char*)"N",1);
          335  +            break;
          336  +          }
          337  +          case SQLITE_INTEGER: {
          338  +            sqlite3_uint64 u;
          339  +            int j;
          340  +            unsigned char x[9];
          341  +            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
          342  +            memcpy(&u, &v, 8);
          343  +            for(j=8; j>=1; j--){
          344  +              x[j] = u & 0xff;
          345  +              u >>= 8;
          346  +            }
          347  +            x[0] = 'I';
          348  +            hash_step(&cx, x, 9);
          349  +            break;
          350  +          }
          351  +          case SQLITE_FLOAT: {
          352  +            sqlite3_uint64 u;
          353  +            int j;
          354  +            unsigned char x[9];
          355  +            double r = sqlite3_column_double(pStmt,i);
          356  +            memcpy(&u, &r, 8);
          357  +            for(j=8; j>=1; j--){
          358  +              x[j] = u & 0xff;
          359  +              u >>= 8;
          360  +            }
          361  +            x[0] = 'F';
          362  +            hash_step(&cx,x,9);
          363  +            break;
          364  +          }
          365  +          case SQLITE_TEXT: {
          366  +            int n2 = sqlite3_column_bytes(pStmt, i);
          367  +            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
          368  +            hash_step_vformat(&cx,"T%d:",n2);
          369  +            hash_step(&cx, z2, n2);
          370  +            break;
          371  +          }
          372  +          case SQLITE_BLOB: {
          373  +            int n2 = sqlite3_column_bytes(pStmt, i);
          374  +            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
          375  +            hash_step_vformat(&cx,"B%d:",n2);
          376  +            hash_step(&cx, z2, n2);
          377  +            break;
          378  +          }
          379  +        }
          380  +      }
          381  +    }
          382  +    sqlite3_finalize(pStmt);
          383  +  }
          384  +  hash_finish(&cx, zOut);
          385  +  sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
          386  +}
          387  +
          388  +
          389  +#ifdef _WIN32
          390  +__declspec(dllexport)
          391  +#endif
          392  +int sqlite3_sha_init(
          393  +  sqlite3 *db,
          394  +  char **pzErrMsg,
          395  +  const sqlite3_api_routines *pApi
          396  +){
          397  +  int rc = SQLITE_OK;
          398  +  SQLITE_EXTENSION_INIT2(pApi);
          399  +  (void)pzErrMsg;  /* Unused parameter */
          400  +  rc = sqlite3_create_function(db, "sha1", 1, SQLITE_UTF8, 0,
          401  +                               sha1Func, 0, 0);
          402  +  if( rc==SQLITE_OK ){
          403  +    rc = sqlite3_create_function(db, "sha1_query", 1, SQLITE_UTF8, 0,
          404  +                                 sha1QueryFunc, 0, 0);
          405  +  }
          406  +  return rc;
          407  +}

Added ext/misc/shathree.c.

            1  +/*
            2  +** 2017-03-08
            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 SQLite extension implements a functions that compute SHA1 hashes.
           14  +** Two SQL functions are implemented:
           15  +**
           16  +**     sha3(X,SIZE)
           17  +**     sha3_query(Y,SIZE)
           18  +**
           19  +** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
           20  +** X is NULL.
           21  +**
           22  +** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
           23  +** and returns a hash of their results.
           24  +**
           25  +** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
           26  +** is used.  If SIZE is included it must be one of the integers 224, 256,
           27  +** 384, or 512, to determine SHA3 hash variant that is computed.
           28  +*/
           29  +#include "sqlite3ext.h"
           30  +SQLITE_EXTENSION_INIT1
           31  +#include <assert.h>
           32  +#include <string.h>
           33  +#include <stdarg.h>
           34  +typedef sqlite3_uint64 u64;
           35  +
           36  +/******************************************************************************
           37  +** The Hash Engine
           38  +*/
           39  +/*
           40  +** Macros to determine whether the machine is big or little endian,
           41  +** and whether or not that determination is run-time or compile-time.
           42  +**
           43  +** For best performance, an attempt is made to guess at the byte-order
           44  +** using C-preprocessor macros.  If that is unsuccessful, or if
           45  +** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
           46  +** at run-time.
           47  +*/
           48  +#ifndef SHA3_BYTEORDER
           49  +# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
           50  +     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
           51  +     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
           52  +     defined(__arm__)
           53  +#   define SHA3_BYTEORDER    1234
           54  +# elif defined(sparc)    || defined(__ppc__)
           55  +#   define SHA3_BYTEORDER    4321
           56  +# else
           57  +#   define SHA3_BYTEORDER 0
           58  +# endif
           59  +#endif
           60  +
           61  +
           62  +/*
           63  +** State structure for a SHA3 hash in progress
           64  +*/
           65  +typedef struct SHA3Context SHA3Context;
           66  +struct SHA3Context {
           67  +  union {
           68  +    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
           69  +    unsigned char x[1600];    /* ... or 1600 bytes */
           70  +  } u;
           71  +  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
           72  +  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
           73  +  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
           74  +};
           75  +
           76  +/*
           77  +** A single step of the Keccak mixing function for a 1600-bit state
           78  +*/
           79  +static void KeccakF1600Step(SHA3Context *p){
           80  +  int i;
           81  +  u64 B0, B1, B2, B3, B4;
           82  +  u64 C0, C1, C2, C3, C4;
           83  +  u64 D0, D1, D2, D3, D4;
           84  +  static const u64 RC[] = {
           85  +    0x0000000000000001ULL,  0x0000000000008082ULL,
           86  +    0x800000000000808aULL,  0x8000000080008000ULL,
           87  +    0x000000000000808bULL,  0x0000000080000001ULL,
           88  +    0x8000000080008081ULL,  0x8000000000008009ULL,
           89  +    0x000000000000008aULL,  0x0000000000000088ULL,
           90  +    0x0000000080008009ULL,  0x000000008000000aULL,
           91  +    0x000000008000808bULL,  0x800000000000008bULL,
           92  +    0x8000000000008089ULL,  0x8000000000008003ULL,
           93  +    0x8000000000008002ULL,  0x8000000000000080ULL,
           94  +    0x000000000000800aULL,  0x800000008000000aULL,
           95  +    0x8000000080008081ULL,  0x8000000000008080ULL,
           96  +    0x0000000080000001ULL,  0x8000000080008008ULL
           97  +  };
           98  +# define A00 (p->u.s[0])
           99  +# define A01 (p->u.s[1])
          100  +# define A02 (p->u.s[2])
          101  +# define A03 (p->u.s[3])
          102  +# define A04 (p->u.s[4])
          103  +# define A10 (p->u.s[5])
          104  +# define A11 (p->u.s[6])
          105  +# define A12 (p->u.s[7])
          106  +# define A13 (p->u.s[8])
          107  +# define A14 (p->u.s[9])
          108  +# define A20 (p->u.s[10])
          109  +# define A21 (p->u.s[11])
          110  +# define A22 (p->u.s[12])
          111  +# define A23 (p->u.s[13])
          112  +# define A24 (p->u.s[14])
          113  +# define A30 (p->u.s[15])
          114  +# define A31 (p->u.s[16])
          115  +# define A32 (p->u.s[17])
          116  +# define A33 (p->u.s[18])
          117  +# define A34 (p->u.s[19])
          118  +# define A40 (p->u.s[20])
          119  +# define A41 (p->u.s[21])
          120  +# define A42 (p->u.s[22])
          121  +# define A43 (p->u.s[23])
          122  +# define A44 (p->u.s[24])
          123  +# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
          124  +
          125  +  for(i=0; i<24; i+=4){
          126  +    C0 = A00^A10^A20^A30^A40;
          127  +    C1 = A01^A11^A21^A31^A41;
          128  +    C2 = A02^A12^A22^A32^A42;
          129  +    C3 = A03^A13^A23^A33^A43;
          130  +    C4 = A04^A14^A24^A34^A44;
          131  +    D0 = C4^ROL64(C1, 1);
          132  +    D1 = C0^ROL64(C2, 1);
          133  +    D2 = C1^ROL64(C3, 1);
          134  +    D3 = C2^ROL64(C4, 1);
          135  +    D4 = C3^ROL64(C0, 1);
          136  +
          137  +    B0 = (A00^D0);
          138  +    B1 = ROL64((A11^D1), 44);
          139  +    B2 = ROL64((A22^D2), 43);
          140  +    B3 = ROL64((A33^D3), 21);
          141  +    B4 = ROL64((A44^D4), 14);
          142  +    A00 =   B0 ^((~B1)&  B2 );
          143  +    A00 ^= RC[i];
          144  +    A11 =   B1 ^((~B2)&  B3 );
          145  +    A22 =   B2 ^((~B3)&  B4 );
          146  +    A33 =   B3 ^((~B4)&  B0 );
          147  +    A44 =   B4 ^((~B0)&  B1 );
          148  +
          149  +    B2 = ROL64((A20^D0), 3);
          150  +    B3 = ROL64((A31^D1), 45);
          151  +    B4 = ROL64((A42^D2), 61);
          152  +    B0 = ROL64((A03^D3), 28);
          153  +    B1 = ROL64((A14^D4), 20);
          154  +    A20 =   B0 ^((~B1)&  B2 );
          155  +    A31 =   B1 ^((~B2)&  B3 );
          156  +    A42 =   B2 ^((~B3)&  B4 );
          157  +    A03 =   B3 ^((~B4)&  B0 );
          158  +    A14 =   B4 ^((~B0)&  B1 );
          159  +
          160  +    B4 = ROL64((A40^D0), 18);
          161  +    B0 = ROL64((A01^D1), 1);
          162  +    B1 = ROL64((A12^D2), 6);
          163  +    B2 = ROL64((A23^D3), 25);
          164  +    B3 = ROL64((A34^D4), 8);
          165  +    A40 =   B0 ^((~B1)&  B2 );
          166  +    A01 =   B1 ^((~B2)&  B3 );
          167  +    A12 =   B2 ^((~B3)&  B4 );
          168  +    A23 =   B3 ^((~B4)&  B0 );
          169  +    A34 =   B4 ^((~B0)&  B1 );
          170  +
          171  +    B1 = ROL64((A10^D0), 36);
          172  +    B2 = ROL64((A21^D1), 10);
          173  +    B3 = ROL64((A32^D2), 15);
          174  +    B4 = ROL64((A43^D3), 56);
          175  +    B0 = ROL64((A04^D4), 27);
          176  +    A10 =   B0 ^((~B1)&  B2 );
          177  +    A21 =   B1 ^((~B2)&  B3 );
          178  +    A32 =   B2 ^((~B3)&  B4 );
          179  +    A43 =   B3 ^((~B4)&  B0 );
          180  +    A04 =   B4 ^((~B0)&  B1 );
          181  +
          182  +    B3 = ROL64((A30^D0), 41);
          183  +    B4 = ROL64((A41^D1), 2);
          184  +    B0 = ROL64((A02^D2), 62);
          185  +    B1 = ROL64((A13^D3), 55);
          186  +    B2 = ROL64((A24^D4), 39);
          187  +    A30 =   B0 ^((~B1)&  B2 );
          188  +    A41 =   B1 ^((~B2)&  B3 );
          189  +    A02 =   B2 ^((~B3)&  B4 );
          190  +    A13 =   B3 ^((~B4)&  B0 );
          191  +    A24 =   B4 ^((~B0)&  B1 );
          192  +
          193  +    C0 = A00^A20^A40^A10^A30;
          194  +    C1 = A11^A31^A01^A21^A41;
          195  +    C2 = A22^A42^A12^A32^A02;
          196  +    C3 = A33^A03^A23^A43^A13;
          197  +    C4 = A44^A14^A34^A04^A24;
          198  +    D0 = C4^ROL64(C1, 1);
          199  +    D1 = C0^ROL64(C2, 1);
          200  +    D2 = C1^ROL64(C3, 1);
          201  +    D3 = C2^ROL64(C4, 1);
          202  +    D4 = C3^ROL64(C0, 1);
          203  +
          204  +    B0 = (A00^D0);
          205  +    B1 = ROL64((A31^D1), 44);
          206  +    B2 = ROL64((A12^D2), 43);
          207  +    B3 = ROL64((A43^D3), 21);
          208  +    B4 = ROL64((A24^D4), 14);
          209  +    A00 =   B0 ^((~B1)&  B2 );
          210  +    A00 ^= RC[i+1];
          211  +    A31 =   B1 ^((~B2)&  B3 );
          212  +    A12 =   B2 ^((~B3)&  B4 );
          213  +    A43 =   B3 ^((~B4)&  B0 );
          214  +    A24 =   B4 ^((~B0)&  B1 );
          215  +
          216  +    B2 = ROL64((A40^D0), 3);
          217  +    B3 = ROL64((A21^D1), 45);
          218  +    B4 = ROL64((A02^D2), 61);
          219  +    B0 = ROL64((A33^D3), 28);
          220  +    B1 = ROL64((A14^D4), 20);
          221  +    A40 =   B0 ^((~B1)&  B2 );
          222  +    A21 =   B1 ^((~B2)&  B3 );
          223  +    A02 =   B2 ^((~B3)&  B4 );
          224  +    A33 =   B3 ^((~B4)&  B0 );
          225  +    A14 =   B4 ^((~B0)&  B1 );
          226  +
          227  +    B4 = ROL64((A30^D0), 18);
          228  +    B0 = ROL64((A11^D1), 1);
          229  +    B1 = ROL64((A42^D2), 6);
          230  +    B2 = ROL64((A23^D3), 25);
          231  +    B3 = ROL64((A04^D4), 8);
          232  +    A30 =   B0 ^((~B1)&  B2 );
          233  +    A11 =   B1 ^((~B2)&  B3 );
          234  +    A42 =   B2 ^((~B3)&  B4 );
          235  +    A23 =   B3 ^((~B4)&  B0 );
          236  +    A04 =   B4 ^((~B0)&  B1 );
          237  +
          238  +    B1 = ROL64((A20^D0), 36);
          239  +    B2 = ROL64((A01^D1), 10);
          240  +    B3 = ROL64((A32^D2), 15);
          241  +    B4 = ROL64((A13^D3), 56);
          242  +    B0 = ROL64((A44^D4), 27);
          243  +    A20 =   B0 ^((~B1)&  B2 );
          244  +    A01 =   B1 ^((~B2)&  B3 );
          245  +    A32 =   B2 ^((~B3)&  B4 );
          246  +    A13 =   B3 ^((~B4)&  B0 );
          247  +    A44 =   B4 ^((~B0)&  B1 );
          248  +
          249  +    B3 = ROL64((A10^D0), 41);
          250  +    B4 = ROL64((A41^D1), 2);
          251  +    B0 = ROL64((A22^D2), 62);
          252  +    B1 = ROL64((A03^D3), 55);
          253  +    B2 = ROL64((A34^D4), 39);
          254  +    A10 =   B0 ^((~B1)&  B2 );
          255  +    A41 =   B1 ^((~B2)&  B3 );
          256  +    A22 =   B2 ^((~B3)&  B4 );
          257  +    A03 =   B3 ^((~B4)&  B0 );
          258  +    A34 =   B4 ^((~B0)&  B1 );
          259  +
          260  +    C0 = A00^A40^A30^A20^A10;
          261  +    C1 = A31^A21^A11^A01^A41;
          262  +    C2 = A12^A02^A42^A32^A22;
          263  +    C3 = A43^A33^A23^A13^A03;
          264  +    C4 = A24^A14^A04^A44^A34;
          265  +    D0 = C4^ROL64(C1, 1);
          266  +    D1 = C0^ROL64(C2, 1);
          267  +    D2 = C1^ROL64(C3, 1);
          268  +    D3 = C2^ROL64(C4, 1);
          269  +    D4 = C3^ROL64(C0, 1);
          270  +
          271  +    B0 = (A00^D0);
          272  +    B1 = ROL64((A21^D1), 44);
          273  +    B2 = ROL64((A42^D2), 43);
          274  +    B3 = ROL64((A13^D3), 21);
          275  +    B4 = ROL64((A34^D4), 14);
          276  +    A00 =   B0 ^((~B1)&  B2 );
          277  +    A00 ^= RC[i+2];
          278  +    A21 =   B1 ^((~B2)&  B3 );
          279  +    A42 =   B2 ^((~B3)&  B4 );
          280  +    A13 =   B3 ^((~B4)&  B0 );
          281  +    A34 =   B4 ^((~B0)&  B1 );
          282  +
          283  +    B2 = ROL64((A30^D0), 3);
          284  +    B3 = ROL64((A01^D1), 45);
          285  +    B4 = ROL64((A22^D2), 61);
          286  +    B0 = ROL64((A43^D3), 28);
          287  +    B1 = ROL64((A14^D4), 20);
          288  +    A30 =   B0 ^((~B1)&  B2 );
          289  +    A01 =   B1 ^((~B2)&  B3 );
          290  +    A22 =   B2 ^((~B3)&  B4 );
          291  +    A43 =   B3 ^((~B4)&  B0 );
          292  +    A14 =   B4 ^((~B0)&  B1 );
          293  +
          294  +    B4 = ROL64((A10^D0), 18);
          295  +    B0 = ROL64((A31^D1), 1);
          296  +    B1 = ROL64((A02^D2), 6);
          297  +    B2 = ROL64((A23^D3), 25);
          298  +    B3 = ROL64((A44^D4), 8);
          299  +    A10 =   B0 ^((~B1)&  B2 );
          300  +    A31 =   B1 ^((~B2)&  B3 );
          301  +    A02 =   B2 ^((~B3)&  B4 );
          302  +    A23 =   B3 ^((~B4)&  B0 );
          303  +    A44 =   B4 ^((~B0)&  B1 );
          304  +
          305  +    B1 = ROL64((A40^D0), 36);
          306  +    B2 = ROL64((A11^D1), 10);
          307  +    B3 = ROL64((A32^D2), 15);
          308  +    B4 = ROL64((A03^D3), 56);
          309  +    B0 = ROL64((A24^D4), 27);
          310  +    A40 =   B0 ^((~B1)&  B2 );
          311  +    A11 =   B1 ^((~B2)&  B3 );
          312  +    A32 =   B2 ^((~B3)&  B4 );
          313  +    A03 =   B3 ^((~B4)&  B0 );
          314  +    A24 =   B4 ^((~B0)&  B1 );
          315  +
          316  +    B3 = ROL64((A20^D0), 41);
          317  +    B4 = ROL64((A41^D1), 2);
          318  +    B0 = ROL64((A12^D2), 62);
          319  +    B1 = ROL64((A33^D3), 55);
          320  +    B2 = ROL64((A04^D4), 39);
          321  +    A20 =   B0 ^((~B1)&  B2 );
          322  +    A41 =   B1 ^((~B2)&  B3 );
          323  +    A12 =   B2 ^((~B3)&  B4 );
          324  +    A33 =   B3 ^((~B4)&  B0 );
          325  +    A04 =   B4 ^((~B0)&  B1 );
          326  +
          327  +    C0 = A00^A30^A10^A40^A20;
          328  +    C1 = A21^A01^A31^A11^A41;
          329  +    C2 = A42^A22^A02^A32^A12;
          330  +    C3 = A13^A43^A23^A03^A33;
          331  +    C4 = A34^A14^A44^A24^A04;
          332  +    D0 = C4^ROL64(C1, 1);
          333  +    D1 = C0^ROL64(C2, 1);
          334  +    D2 = C1^ROL64(C3, 1);
          335  +    D3 = C2^ROL64(C4, 1);
          336  +    D4 = C3^ROL64(C0, 1);
          337  +
          338  +    B0 = (A00^D0);
          339  +    B1 = ROL64((A01^D1), 44);
          340  +    B2 = ROL64((A02^D2), 43);
          341  +    B3 = ROL64((A03^D3), 21);
          342  +    B4 = ROL64((A04^D4), 14);
          343  +    A00 =   B0 ^((~B1)&  B2 );
          344  +    A00 ^= RC[i+3];
          345  +    A01 =   B1 ^((~B2)&  B3 );
          346  +    A02 =   B2 ^((~B3)&  B4 );
          347  +    A03 =   B3 ^((~B4)&  B0 );
          348  +    A04 =   B4 ^((~B0)&  B1 );
          349  +
          350  +    B2 = ROL64((A10^D0), 3);
          351  +    B3 = ROL64((A11^D1), 45);
          352  +    B4 = ROL64((A12^D2), 61);
          353  +    B0 = ROL64((A13^D3), 28);
          354  +    B1 = ROL64((A14^D4), 20);
          355  +    A10 =   B0 ^((~B1)&  B2 );
          356  +    A11 =   B1 ^((~B2)&  B3 );
          357  +    A12 =   B2 ^((~B3)&  B4 );
          358  +    A13 =   B3 ^((~B4)&  B0 );
          359  +    A14 =   B4 ^((~B0)&  B1 );
          360  +
          361  +    B4 = ROL64((A20^D0), 18);
          362  +    B0 = ROL64((A21^D1), 1);
          363  +    B1 = ROL64((A22^D2), 6);
          364  +    B2 = ROL64((A23^D3), 25);
          365  +    B3 = ROL64((A24^D4), 8);
          366  +    A20 =   B0 ^((~B1)&  B2 );
          367  +    A21 =   B1 ^((~B2)&  B3 );
          368  +    A22 =   B2 ^((~B3)&  B4 );
          369  +    A23 =   B3 ^((~B4)&  B0 );
          370  +    A24 =   B4 ^((~B0)&  B1 );
          371  +
          372  +    B1 = ROL64((A30^D0), 36);
          373  +    B2 = ROL64((A31^D1), 10);
          374  +    B3 = ROL64((A32^D2), 15);
          375  +    B4 = ROL64((A33^D3), 56);
          376  +    B0 = ROL64((A34^D4), 27);
          377  +    A30 =   B0 ^((~B1)&  B2 );
          378  +    A31 =   B1 ^((~B2)&  B3 );
          379  +    A32 =   B2 ^((~B3)&  B4 );
          380  +    A33 =   B3 ^((~B4)&  B0 );
          381  +    A34 =   B4 ^((~B0)&  B1 );
          382  +
          383  +    B3 = ROL64((A40^D0), 41);
          384  +    B4 = ROL64((A41^D1), 2);
          385  +    B0 = ROL64((A42^D2), 62);
          386  +    B1 = ROL64((A43^D3), 55);
          387  +    B2 = ROL64((A44^D4), 39);
          388  +    A40 =   B0 ^((~B1)&  B2 );
          389  +    A41 =   B1 ^((~B2)&  B3 );
          390  +    A42 =   B2 ^((~B3)&  B4 );
          391  +    A43 =   B3 ^((~B4)&  B0 );
          392  +    A44 =   B4 ^((~B0)&  B1 );
          393  +  }
          394  +}
          395  +
          396  +/*
          397  +** Initialize a new hash.  iSize determines the size of the hash
          398  +** in bits and should be one of 224, 256, 384, or 512.  Or iSize
          399  +** can be zero to use the default hash size of 256 bits.
          400  +*/
          401  +static void SHA3Init(SHA3Context *p, int iSize){
          402  +  memset(p, 0, sizeof(*p));
          403  +  if( iSize>=128 && iSize<=512 ){
          404  +    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
          405  +  }else{
          406  +    p->nRate = (1600 - 2*256)/8;
          407  +  }
          408  +#if SHA3_BYTEORDER==1234
          409  +  /* Known to be little-endian at compile-time. No-op */
          410  +#elif SHA3_BYTEORDER==4321
          411  +  p->ixMask = 7;  /* Big-endian */
          412  +#else
          413  +  {
          414  +    static unsigned int one = 1;
          415  +    if( 1==*(unsigned char*)&one ){
          416  +      /* Little endian.  No byte swapping. */
          417  +      p->ixMask = 0;
          418  +    }else{
          419  +      /* Big endian.  Byte swap. */
          420  +      p->ixMask = 7;
          421  +    }
          422  +  }
          423  +#endif
          424  +}
          425  +
          426  +/*
          427  +** Make consecutive calls to the SHA3Update function to add new content
          428  +** to the hash
          429  +*/
          430  +static void SHA3Update(
          431  +  SHA3Context *p,
          432  +  const unsigned char *aData,
          433  +  unsigned int nData
          434  +){
          435  +  unsigned int i = 0;
          436  +#if SHA3_BYTEORDER==1234
          437  +  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
          438  +    for(; i+7<nData; i+=8){
          439  +      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
          440  +      p->nLoaded += 8;
          441  +      if( p->nLoaded>=p->nRate ){
          442  +        KeccakF1600Step(p);
          443  +        p->nLoaded = 0;
          444  +      }
          445  +    }
          446  +  }
          447  +#endif
          448  +  for(; i<nData; i++){
          449  +#if SHA3_BYTEORDER==1234
          450  +    p->u.x[p->nLoaded] ^= aData[i];
          451  +#elif SHA3_BYTEORDER==4321
          452  +    p->u.x[p->nLoaded^0x07] ^= aData[i];
          453  +#else
          454  +    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
          455  +#endif
          456  +    p->nLoaded++;
          457  +    if( p->nLoaded==p->nRate ){
          458  +      KeccakF1600Step(p);
          459  +      p->nLoaded = 0;
          460  +    }
          461  +  }
          462  +}
          463  +
          464  +/*
          465  +** After all content has been added, invoke SHA3Final() to compute
          466  +** the final hash.  The function returns a pointer to the binary
          467  +** hash value.
          468  +*/
          469  +static unsigned char *SHA3Final(SHA3Context *p){
          470  +  unsigned int i;
          471  +  if( p->nLoaded==p->nRate-1 ){
          472  +    const unsigned char c1 = 0x86;
          473  +    SHA3Update(p, &c1, 1);
          474  +  }else{
          475  +    const unsigned char c2 = 0x06;
          476  +    const unsigned char c3 = 0x80;
          477  +    SHA3Update(p, &c2, 1);
          478  +    p->nLoaded = p->nRate - 1;
          479  +    SHA3Update(p, &c3, 1);
          480  +  }
          481  +  for(i=0; i<p->nRate; i++){
          482  +    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
          483  +  }
          484  +  return &p->u.x[p->nRate];
          485  +}
          486  +/* End of the hashing logic
          487  +*****************************************************************************/
          488  +
          489  +/*
          490  +** Implementation of the sha3(X,SIZE) function.
          491  +**
          492  +** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
          493  +** size is 256.  If X is a BLOB, it is hashed as is.  
          494  +** For all other non-NULL types of input, X is converted into a UTF-8 string
          495  +** and the string is hashed without the trailing 0x00 terminator.  The hash
          496  +** of a NULL value is NULL.
          497  +*/
          498  +static void sha3Func(
          499  +  sqlite3_context *context,
          500  +  int argc,
          501  +  sqlite3_value **argv
          502  +){
          503  +  SHA3Context cx;
          504  +  int eType = sqlite3_value_type(argv[0]);
          505  +  int nByte = sqlite3_value_bytes(argv[0]);
          506  +  int iSize;
          507  +  if( argc==1 ){
          508  +    iSize = 256;
          509  +  }else{
          510  +    iSize = sqlite3_value_int(argv[1]);
          511  +    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
          512  +      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
          513  +                                    "384 512", -1);
          514  +      return;
          515  +    }
          516  +  }
          517  +  if( eType==SQLITE_NULL ) return;
          518  +  SHA3Init(&cx, iSize);
          519  +  if( eType==SQLITE_BLOB ){
          520  +    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
          521  +  }else{
          522  +    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
          523  +  }
          524  +  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
          525  +}
          526  +
          527  +/* Compute a string using sqlite3_vsnprintf() with a maximum length
          528  +** of 50 bytes and add it to the hash.
          529  +*/
          530  +static void hash_step_vformat(
          531  +  SHA3Context *p,                 /* Add content to this context */
          532  +  const char *zFormat,
          533  +  ...
          534  +){
          535  +  va_list ap;
          536  +  int n;
          537  +  char zBuf[50];
          538  +  va_start(ap, zFormat);
          539  +  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
          540  +  va_end(ap);
          541  +  n = (int)strlen(zBuf);
          542  +  SHA3Update(p, (unsigned char*)zBuf, n);
          543  +}
          544  +
          545  +/*
          546  +** Implementation of the sha3_query(SQL,SIZE) function.
          547  +**
          548  +** This function compiles and runs the SQL statement(s) given in the
          549  +** argument. The results are hashed using a SIZE-bit SHA3.  The default
          550  +** size is 256.
          551  +**
          552  +** The format of the byte stream that is hashed is summarized as follows:
          553  +**
          554  +**       S<n>:<sql>
          555  +**       R
          556  +**       N
          557  +**       I<int>
          558  +**       F<ieee-float>
          559  +**       B<size>:<bytes>
          560  +**       T<size>:<text>
          561  +**
          562  +** <sql> is the original SQL text for each statement run and <n> is
          563  +** the size of that text.  The SQL text is UTF-8.  A single R character
          564  +** occurs before the start of each row.  N means a NULL value.
          565  +** I mean an 8-byte little-endian integer <int>.  F is a floating point
          566  +** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
          567  +** B means blobs of <size> bytes.  T means text rendered as <size>
          568  +** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
          569  +** text integers.
          570  +**
          571  +** For each SQL statement in the X input, there is one S segment.  Each
          572  +** S segment is followed by zero or more R segments, one for each row in the
          573  +** result set.  After each R, there are one or more N, I, F, B, or T segments,
          574  +** one for each column in the result set.  Segments are concatentated directly
          575  +** with no delimiters of any kind.
          576  +*/
          577  +static void sha3QueryFunc(
          578  +  sqlite3_context *context,
          579  +  int argc,
          580  +  sqlite3_value **argv
          581  +){
          582  +  sqlite3 *db = sqlite3_context_db_handle(context);
          583  +  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
          584  +  sqlite3_stmt *pStmt = 0;
          585  +  int nCol;                   /* Number of columns in the result set */
          586  +  int i;                      /* Loop counter */
          587  +  int rc;
          588  +  int n;
          589  +  const char *z;
          590  +  SHA3Context cx;
          591  +  int iSize;
          592  +
          593  +  if( argc==1 ){
          594  +    iSize = 256;
          595  +  }else{
          596  +    iSize = sqlite3_value_int(argv[1]);
          597  +    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
          598  +      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
          599  +                                    "384 512", -1);
          600  +      return;
          601  +    }
          602  +  }
          603  +  if( zSql==0 ) return;
          604  +  SHA3Init(&cx, iSize);
          605  +  while( zSql[0] ){
          606  +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
          607  +    if( rc ){
          608  +      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
          609  +                                   zSql, sqlite3_errmsg(db));
          610  +      sqlite3_finalize(pStmt);
          611  +      sqlite3_result_error(context, zMsg, -1);
          612  +      sqlite3_free(zMsg);
          613  +      return;
          614  +    }
          615  +    if( !sqlite3_stmt_readonly(pStmt) ){
          616  +      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
          617  +      sqlite3_finalize(pStmt);
          618  +      sqlite3_result_error(context, zMsg, -1);
          619  +      sqlite3_free(zMsg);
          620  +      return;
          621  +    }
          622  +    nCol = sqlite3_column_count(pStmt);
          623  +    z = sqlite3_sql(pStmt);
          624  +    n = (int)strlen(z);
          625  +    hash_step_vformat(&cx,"S%d:",n);
          626  +    SHA3Update(&cx,(unsigned char*)z,n);
          627  +
          628  +    /* Compute a hash over the result of the query */
          629  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
          630  +      SHA3Update(&cx,(const unsigned char*)"R",1);
          631  +      for(i=0; i<nCol; i++){
          632  +        switch( sqlite3_column_type(pStmt,i) ){
          633  +          case SQLITE_NULL: {
          634  +            SHA3Update(&cx, (const unsigned char*)"N",1);
          635  +            break;
          636  +          }
          637  +          case SQLITE_INTEGER: {
          638  +            sqlite3_uint64 u;
          639  +            int j;
          640  +            unsigned char x[9];
          641  +            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
          642  +            memcpy(&u, &v, 8);
          643  +            for(j=8; j>=1; j--){
          644  +              x[j] = u & 0xff;
          645  +              u >>= 8;
          646  +            }
          647  +            x[0] = 'I';
          648  +            SHA3Update(&cx, x, 9);
          649  +            break;
          650  +          }
          651  +          case SQLITE_FLOAT: {
          652  +            sqlite3_uint64 u;
          653  +            int j;
          654  +            unsigned char x[9];
          655  +            double r = sqlite3_column_double(pStmt,i);
          656  +            memcpy(&u, &r, 8);
          657  +            for(j=8; j>=1; j--){
          658  +              x[j] = u & 0xff;
          659  +              u >>= 8;
          660  +            }
          661  +            x[0] = 'F';
          662  +            SHA3Update(&cx,x,9);
          663  +            break;
          664  +          }
          665  +          case SQLITE_TEXT: {
          666  +            int n2 = sqlite3_column_bytes(pStmt, i);
          667  +            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
          668  +            hash_step_vformat(&cx,"T%d:",n2);
          669  +            SHA3Update(&cx, z2, n2);
          670  +            break;
          671  +          }
          672  +          case SQLITE_BLOB: {
          673  +            int n2 = sqlite3_column_bytes(pStmt, i);
          674  +            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
          675  +            hash_step_vformat(&cx,"B%d:",n2);
          676  +            SHA3Update(&cx, z2, n2);
          677  +            break;
          678  +          }
          679  +        }
          680  +      }
          681  +    }
          682  +    sqlite3_finalize(pStmt);
          683  +  }
          684  +  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
          685  +}
          686  +
          687  +
          688  +#ifdef _WIN32
          689  +__declspec(dllexport)
          690  +#endif
          691  +int sqlite3_shathree_init(
          692  +  sqlite3 *db,
          693  +  char **pzErrMsg,
          694  +  const sqlite3_api_routines *pApi
          695  +){
          696  +  int rc = SQLITE_OK;
          697  +  SQLITE_EXTENSION_INIT2(pApi);
          698  +  (void)pzErrMsg;  /* Unused parameter */
          699  +  rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
          700  +                               sha3Func, 0, 0);
          701  +  if( rc==SQLITE_OK ){
          702  +    rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
          703  +                                 sha3Func, 0, 0);
          704  +  }
          705  +  if( rc==SQLITE_OK ){
          706  +    rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
          707  +                                 sha3QueryFunc, 0, 0);
          708  +  }
          709  +  if( rc==SQLITE_OK ){
          710  +    rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
          711  +                                 sha3QueryFunc, 0, 0);
          712  +  }
          713  +  return rc;
          714  +}

Changes to ext/misc/vtshim.c.

    91     91     if( pNew==0 ) return SQLITE_NOMEM;
    92     92     memset(pNew, 0, sizeof(*pNew));
    93     93     rc = pAux->pMod->xCreate(db, pAux->pChildAux, argc, argv,
    94     94                              &pNew->pChild, pzErr);
    95     95     if( rc ){
    96     96       sqlite3_free(pNew);
    97     97       *ppVtab = 0;
           98  +    return rc;
    98     99     }
    99    100     pNew->pAux = pAux;
   100    101     pNew->ppPrev = &pAux->pAllVtab;
   101    102     pNew->pNext = pAux->pAllVtab;
   102    103     if( pAux->pAllVtab ) pAux->pAllVtab->ppPrev = &pNew->pNext;
   103    104     pAux->pAllVtab = pNew;
   104    105     return rc;
................................................................................
   129    130     if( pNew==0 ) return SQLITE_NOMEM;
   130    131     memset(pNew, 0, sizeof(*pNew));
   131    132     rc = pAux->pMod->xConnect(db, pAux->pChildAux, argc, argv,
   132    133                               &pNew->pChild, pzErr);
   133    134     if( rc ){
   134    135       sqlite3_free(pNew);
   135    136       *ppVtab = 0;
          137  +    return rc;
   136    138     }
   137    139     pNew->pAux = pAux;
   138    140     pNew->ppPrev = &pAux->pAllVtab;
   139    141     pNew->pNext = pAux->pAllVtab;
   140    142     if( pAux->pAllVtab ) pAux->pAllVtab->ppPrev = &pNew->pNext;
   141    143     pAux->pAllVtab = pNew;
   142    144     return rc;

Added ext/rbu/rbucrash2.test.

            1  +# 2017 March 02
            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  +
           13  +if {![info exists testdir]} {
           14  +  set testdir [file join [file dirname [info script]] .. .. test]
           15  +}
           16  +source $testdir/tester.tcl
           17  +set ::testprefix rbucrash2
           18  +
           19  +db close
           20  +forcedelete test.db-oal rbu.db
           21  +sqlite3_shutdown
           22  +sqlite3_config_uri 1
           23  +reset_db
           24  +
           25  +# Set up a target database and an rbu update database. The target
           26  +# db is the usual "test.db", the rbu db is "test.db2".
           27  +#
           28  +forcedelete test.db2
           29  +do_execsql_test 1.0 {
           30  +  CREATE TABLE t1(a, b, c, PRIMARY KEY(a), UNIQUE(b));
           31  +  INSERT INTO t1 VALUES(1, 2, 3);
           32  +  INSERT INTO t1 VALUES(4, 5, 6);
           33  +  INSERT INTO t1 VALUES(7, 8, 9);
           34  +
           35  +  ATTACH 'test.db2' AS rbu;
           36  +  CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
           37  +  INSERT INTO data_t1 VALUES('one', randomblob(3500), NULL, 0);
           38  +  INSERT INTO data_t1 VALUES('two', randomblob(3500), NULL, 0);
           39  +  INSERT INTO data_t1 VALUES('three', randomblob(3500), NULL, 0);
           40  +  INSERT INTO data_t1 VALUES('four', randomblob(3500), NULL, 0);
           41  +  INSERT INTO data_t1 VALUES('five', randomblob(3500), NULL, 0);
           42  +  INSERT INTO data_t1 VALUES('six', randomblob(3500), NULL, 0);
           43  +}
           44  +db_save_and_close
           45  +
           46  +proc do_rbu_crash_test2 {tn script} {
           47  +
           48  +  foreach {f blksz} {
           49  +    test.db   512
           50  +    test.db2  512
           51  +    test.db   4096
           52  +    test.db2  4096
           53  +  } {
           54  +    set bDone 0
           55  +    for {set iDelay 1} {$bDone==0} {incr iDelay} {
           56  +      forcedelete test.db2 test.db2-journal test.db test.db-oal test.db-wal
           57  +      db_restore
           58  +  
           59  +      set res [
           60  +        crashsql -file $f -delay $iDelay -tclbody $script -dflt 1 -opendb {} \
           61  +            -blocksize $blksz {}
           62  +      ]
           63  +  
           64  +      set bDone 1
           65  +      if {$res == "1 {child process exited abnormally}"} {
           66  +        set bDone 0
           67  +      } elseif {$res != "0 {}"} {
           68  +        error "unexected catchsql result: $res"
           69  +      }
           70  +  
           71  +      sqlite3rbu rbu test.db test.db2
           72  +      while {[rbu step]=="SQLITE_OK"} {}
           73  +      rbu close
           74  +  
           75  +      sqlite3 db test.db
           76  +      do_execsql_test $tn.delay=$iDelay.f=$f.blksz=$blksz {
           77  +        PRAGMA integrity_check;
           78  +      } {ok}
           79  +      db close
           80  +    }
           81  +  }
           82  +}
           83  +
           84  +for {set x 1} {$x < 10} {incr x} {
           85  +  do_rbu_crash_test2 1.$x {
           86  +    sqlite3rbu rbu test.db test.db2
           87  +    while {[rbu step]=="SQLITE_OK"} {
           88  +      rbu savestate
           89  +    }
           90  +    rbu close
           91  +  }
           92  +}
           93  +
           94  +for {set x 1} {$x < 2} {incr x} {
           95  +  do_rbu_crash_test2 2.$x {
           96  +    sqlite3rbu rbu test.db test.db2
           97  +    while {[rbu step]=="SQLITE_OK"} {
           98  +      rbu close
           99  +      sqlite3rbu rbu test.db test.db2
          100  +    }
          101  +    rbu close
          102  +  }
          103  +}
          104  +
          105  +finish_test
          106  +

Added ext/rbu/rbufault4.test.

            1  +# 2014 October 22
            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  +
           13  +if {![info exists testdir]} {
           14  +  set testdir [file join [file dirname [info script]] .. .. test]
           15  +}
           16  +source $testdir/tester.tcl
           17  +source $testdir/malloc_common.tcl
           18  +set ::testprefix rbufault4
           19  +
           20  +for {set tn 1} {1} {incr tn} {
           21  +  reset_db
           22  +  do_execsql_test 1.0 {
           23  +    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
           24  +    CREATE INDEX i1b ON t1(b);
           25  +    CREATE INDEX i1c ON t1(c);
           26  +    INSERT INTO t1 VALUES(1, 2, 3);
           27  +    INSERT INTO t1 VALUES(4, 5, 6);
           28  +  }
           29  +
           30  +  forcedelete test.db2
           31  +  sqlite3rbu_vacuum rbu test.db test.db2
           32  +  for {set i 0} {$i < $tn} {incr i} { rbu step }
           33  +  set rc [rbu close]
           34  +  if {$rc!="SQLITE_OK"} { 
           35  +    if {$rc!="SQLITE_DONE"} {error $rc}
           36  +    break
           37  +  }
           38  +  faultsim_save
           39  +
           40  +  do_faultsim_test $tn -faults oom-t* -prep {
           41  +    faultsim_restore
           42  +  } -body {
           43  +    sqlite3rbu_vacuum rbu test.db test.db2
           44  +    while 1 {
           45  +      set rc [rbu step]
           46  +      if {$rc=="SQLITE_DONE"} break
           47  +      if {$rc!="SQLITE_OK"} { error $rc }
           48  +    }
           49  +  } -test {
           50  +    catch {rbu close}
           51  +    faultsim_test_result {0 {}} {1 SQLITE_NOMEM} {1 SQLITE_IOERR_NOMEM}
           52  +
           53  +    sqlite3rbu_vacuum rbu test.db test.db2
           54  +    while {[rbu step]=="SQLITE_OK"} {}
           55  +    set trc [rbu close]
           56  +    if {$trc!="SQLITE_DONE"} { error "Got $trc instead of SQLITE_DONE!" }
           57  +
           58  +    set rc [db one {PRAGMA integrity_check}]
           59  +    if {$rc!="ok"} { error "Got $rc instead of ok!" }
           60  +  }
           61  +}
           62  +
           63  +
           64  +
           65  +finish_test
           66  +

Changes to ext/rbu/rbuprogress.test.

    36     36       INSERT INTO rbu_count VALUES('data_t1', 3);
    37     37     }
    38     38     return $filename
    39     39   }
    40     40   
    41     41   
    42     42   do_execsql_test 1.0 {
           43  +  PRAGMA page_size = 4096;
    43     44     CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
    44     45   }
    45     46   
    46     47   do_test 1.1 {
    47     48     create_rbu1 rbu.db
    48     49     sqlite3rbu rbu test.db rbu.db
    49     50     rbu bp_progress
................................................................................
   262    263     }] {SQLITE_DONE}]
   263    264   }
   264    265   
   265    266   foreach bReopen {0 1} {
   266    267     do_test 3.$bReopen.1.0 {
   267    268       reset_db
   268    269       execsql {
          270  +      PRAGMA page_size = 4096;
   269    271         CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
   270    272         CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
   271    273         CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
   272    274         CREATE TABLE t4(a INTEGER PRIMARY KEY, b);
   273    275       }
   274    276       create_db_file rbu.db {
   275    277         CREATE TABLE data_t1(a, b, rbu_control);

Added ext/rbu/rburesume.test.

            1  +# 2017 January 13
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains tests for resumption of RBU operations in the
           13  +# case where the previous RBU process crashed.
           14  +#
           15  +
           16  +source [file join [file dirname [info script]] rbu_common.tcl]
           17  +set ::testprefix rburesume
           18  +
           19  +forcedelete test.db-shm test.db-oal
           20  +do_execsql_test 1.0 {
           21  +  CREATE TABLE t1(a PRIMARY KEY, b, c);
           22  +  CREATE INDEX t1a ON t1(a);
           23  +  CREATE INDEX t1b ON t1(b);
           24  +  CREATE INDEX t1c ON t1(c);
           25  +  WITH s(i) AS (
           26  +    VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<50
           27  +  )
           28  +  INSERT INTO t1 SELECT randomblob(50), randomblob(75), randomblob(100) FROM s;
           29  +}
           30  +db_save_and_close
           31  +
           32  +do_test 1.1 {
           33  +  list [file exists test.db] \
           34  +       [file exists test.db-wal] \
           35  +       [file exists test.db-shm] \
           36  +       [file exists test.db-oal]
           37  +} {1 0 0 0}
           38  +
           39  +# Each iteration of the following loop:
           40  +#
           41  +#   1. Restores the db to the state it was in following test case 1.0
           42  +#   2. Opens an RBU vacuum and steps it $n times.
           43  +#   3. Closes the RBU vacuum handled opened in (2).
           44  +#   4. Opens a second RBU vacuum handle, resumes and completes the vacuum op. 
           45  +#
           46  +# The loop runs until $n is large enough that step (2) vacuums the entire
           47  +# database.
           48  +#
           49  +for {set n 1} {$n < 5000} {incr n} {
           50  +  db_restore
           51  +  forcedelete state.db
           52  +  sqlite3rbu_vacuum rbu test.db state.db
           53  +  for {set i 0} {$i<$n} {incr i} {
           54  +    set rc [rbu step]
           55  +    if {$rc == "SQLITE_DONE"} break
           56  +  }
           57  +  rbu close
           58  +  if {$rc == "SQLITE_DONE"} break
           59  +
           60  +  do_test 1.2.$n.1 {
           61  +    sqlite3rbu_vacuum rbu test.db state.db
           62  +    while {[rbu step]=="SQLITE_OK"} {}
           63  +    rbu close
           64  +  } {SQLITE_DONE}
           65  +
           66  +  do_test 1.2.$n.2 {
           67  +    sqlite3 db2 test.db
           68  +    db2 eval { 
           69  +      SELECT count(*) FROM t1;
           70  +      PRAGMA integrity_check;
           71  +    }
           72  +  } {50 ok}
           73  +  db2 close
           74  +}
           75  +
           76  +# Each iteration of this loop:
           77  +#
           78  +#   1. Restores the db to the state it was in following test case 1.0
           79  +#   2. Opens an RBU vacuum and steps it $n times.
           80  +#   3. Takes a copy of all database files and the state db.
           81  +#   4. Opens a second RBU vacuum handle on the copy, resumes and completes the
           82  +#      vacuum op. 
           83  +#
           84  +# The loop runs until $n is large enough that step (2) vacuums the entire
           85  +# database.
           86  +#
           87  +for {set n 1} {$n < 5000} {incr n} {
           88  +  db_restore
           89  +  forcedelete state.db state.db-shm state.db-oal state.db-wal
           90  +  sqlite3rbu_vacuum rbu test.db state.db
           91  +  for {set i 0} {$i<$n} {incr i} {
           92  +    set rc [rbu step]
           93  +    if {$rc == "SQLITE_DONE"} break
           94  +  }
           95  +  if {$rc == "SQLITE_DONE"} {
           96  +    rbu close
           97  +    break
           98  +  }
           99  +
          100  +  foreach f {test.db test.db-oal test.db-wal test.db-shm test.db-vacuum} {
          101  +    set f2 [string map [list test.db test.db2] $f]
          102  +    if {[file exists $f]} {
          103  +      forcecopy $f $f2
          104  +    } else {
          105  +      forcedelete $f2
          106  +    }
          107  +  }
          108  +  forcecopy state.db state.db2
          109  +  rbu close
          110  +
          111  +  do_test 1.3.$n.1 {
          112  +    sqlite3rbu_vacuum rbu test.db2 state.db2
          113  +    while {[rbu step]=="SQLITE_OK"} {}
          114  +    rbu close
          115  +  } {SQLITE_DONE}
          116  +
          117  +  do_test 1.3.$n.2 {
          118  +    sqlite3 db2 test.db2
          119  +    db2 eval { 
          120  +      SELECT count(*) FROM t1;
          121  +      PRAGMA integrity_check;
          122  +    }
          123  +  } {50 ok}
          124  +  db2 close
          125  +}
          126  +
          127  +# Each iteration of this loop:
          128  +#
          129  +#   1. Restores the db to the state it was in following test case 1.0
          130  +#   2. Opens an RBU vacuum and steps it 10 times. Then closes it.
          131  +#   2. Opens an RBU vacuum and steps it $n times.
          132  +#   3. Takes a copy of all database files and the state db.
          133  +#   4. Opens a second RBU vacuum handle on the copy, resumes and completes the
          134  +#      vacuum op. 
          135  +#
          136  +# The loop runs until $n is large enough that step (3) vacuums the entire
          137  +# database.
          138  +#
          139  +for {set n 1} {$n < 5000} {incr n} {
          140  +  db_restore
          141  +  forcedelete state.db state.db-shm state.db-oal state.db-wal
          142  +
          143  +  sqlite3rbu_vacuum rbu test.db state.db
          144  +  for {set i 0} {$i<10} {incr i} {
          145  +    rbu step
          146  +  }
          147  +  rbu close
          148  +
          149  +  sqlite3rbu_vacuum rbu test.db state.db
          150  +  for {set i 0} {$i<$n} {incr i} {
          151  +    set rc [rbu step]
          152  +    if {$rc == "SQLITE_DONE"} break
          153  +  }
          154  +  if {$rc == "SQLITE_DONE"} {
          155  +    rbu close
          156  +    break
          157  +  }
          158  +
          159  +  foreach f {test.db test.db-oal test.db-wal test.db-shm test.db-vacuum} {
          160  +    set f2 [string map [list test.db test.db2] $f]
          161  +    if {[file exists $f]} {
          162  +      forcecopy $f $f2
          163  +    } else {
          164  +      forcedelete $f2
          165  +    }
          166  +  }
          167  +  forcecopy state.db state.db2
          168  +  rbu close
          169  +
          170  +  do_test 1.4.$n.1 {
          171  +    sqlite3rbu_vacuum rbu test.db2 state.db2
          172  +    while {[rbu step]=="SQLITE_OK"} {}
          173  +    rbu close
          174  +  } {SQLITE_DONE}
          175  +
          176  +  do_test 1.4.$n.2 {
          177  +    sqlite3 db2 test.db2
          178  +    db2 eval { 
          179  +      SELECT count(*) FROM t1;
          180  +      PRAGMA integrity_check;
          181  +    }
          182  +  } {50 ok}
          183  +  db2 close
          184  +}
          185  +
          186  +forcedelete rbu.db
          187  +do_test 2.0 {
          188  +  sqlite3 db2 rbu.db
          189  +  db2 eval {
          190  +    CREATE TABLE data_t1(a, b, c, rbu_control);
          191  +    WITH s(i) AS (
          192  +        VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10
          193  +    )
          194  +    INSERT INTO data_t1 
          195  +      SELECT randomblob(50), randomblob(75), randomblob(100), 0 FROM s;
          196  +  }
          197  +  db2 close
          198  +} {}
          199  +
          200  +# Each iteration of this loop:
          201  +#
          202  +#   1. Restores the db to the state it was in following test case 1.0
          203  +#   2. Opens an RBU handle to apply the RBU update created in test case 2.0.
          204  +#   3. Steps the RBU handle $n times.
          205  +#   4. Takes a copy of all database files and the state db.
          206  +#   5. Opens a second RBU handle on the copy, resumes and completes the
          207  +#      RBU op. Checks it worked as expected.
          208  +#
          209  +# The loop runs until $n is large enough that step (3) applies the entire
          210  +# update.
          211  +#
          212  +for {set n 1} {$n < 5000} {incr n} {
          213  +  db_restore
          214  +  forcedelete state.db state.db-shm state.db-oal state.db-wal
          215  +  sqlite3rbu rbu test.db rbu.db state.db
          216  +
          217  +  for {set i 0} {$i<$n} {incr i} {
          218  +    set rc [rbu step]
          219  +    if {$rc == "SQLITE_DONE"} break
          220  +  }
          221  +  if {$rc == "SQLITE_DONE"} {
          222  +    rbu close
          223  +    break
          224  +  }
          225  +
          226  +  foreach f {test.db test.db-oal test.db-wal test.db-shm test.db-vacuum} {
          227  +    set f2 [string map [list test.db test.db2] $f]
          228  +    if {[file exists $f]} {
          229  +      forcecopy $f $f2
          230  +    } else {
          231  +      forcedelete $f2
          232  +    }
          233  +  }
          234  +  forcecopy state.db state.db2
          235  +  rbu close
          236  +
          237  +  do_test 2.$n.1 {
          238  +    sqlite3rbu rbu test.db2 rbu.db state.db2
          239  +    while {[rbu step]=="SQLITE_OK"} {}
          240  +    rbu close
          241  +  } {SQLITE_DONE}
          242  +
          243  +  do_test 2.$n.2 {
          244  +    sqlite3 db2 test.db2
          245  +    db2 eval { 
          246  +      SELECT count(*) FROM t1;
          247  +      PRAGMA integrity_check;
          248  +    }
          249  +  } {60 ok}
          250  +  db2 close
          251  +}
          252  +
          253  +finish_test
          254  +

Changes to ext/rbu/rbuvacuum2.test.

   195    195   
   196    196       do_test 5.$tn.2 { file exists test.db-vacuum } 1
   197    197       do_test 5.$tn.3 { file attributes test.db-vacuum -permissions} $perm
   198    198       rbu close
   199    199     }
   200    200   }
   201    201   
          202  +#-------------------------------------------------------------------------
          203  +# Test the outcome of some other connection running a checkpoint while
          204  +# the incremental checkpoint is suspended.
          205  +#
          206  +reset_db
          207  +do_execsql_test 6.0 {
          208  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
          209  +  CREATE INDEX i1b ON t1(b);
          210  +  CREATE INDEX i1c ON t1(c);
          211  +  INSERT INTO t1 VALUES(1, 2, 3);
          212  +  INSERT INTO t1 VALUES(4, 5, 6);
          213  +}
          214  +forcedelete test.db2
          215  +
          216  +do_test 6.1 {
          217  +  sqlite3rbu_vacuum rbu test.db test.db2
          218  +  while {[rbu state]!="checkpoint"} { rbu step }
          219  +  rbu close
          220  +} {SQLITE_OK}
          221  +
          222  +do_execsql_test 6.2 {
          223  +  SELECT 1 FROM sqlite_master LIMIT 1;
          224  +  PRAGMA wal_checkpoint;
          225  +} {1 0 4 4}
          226  +
          227  +do_test 6.3 {
          228  +  sqlite3rbu_vacuum rbu test.db test.db2
          229  +  while {[rbu step]!="SQLITE_DONE"} { rbu step }
          230  +  rbu close
          231  +  execsql { PRAGMA integrity_check }
          232  +} {ok}
   202    233   
   203    234   finish_test
   204    235   

Changes to ext/rbu/sqlite3rbu.c.

   352    352     int rc;                         /* Value returned by last rbu_step() call */
   353    353     char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
   354    354     int nStep;                      /* Rows processed for current object */
   355    355     int nProgress;                  /* Rows processed for all objects */
   356    356     RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
   357    357     const char *zVfsName;           /* Name of automatically created rbu vfs */
   358    358     rbu_file *pTargetFd;            /* File handle open on target db */
          359  +  int nPagePerSector;             /* Pages per sector for pTargetFd */
   359    360     i64 iOalSz;
   360    361     i64 nPhaseOneStep;
   361    362   
   362    363     /* The following state variables are used as part of the incremental
   363    364     ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
   364    365     ** function rbuSetupCheckpoint() for details.  */
   365    366     u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
................................................................................
  2329   2330   }
  2330   2331   
  2331   2332   
  2332   2333   /*
  2333   2334   ** Open the database handle and attach the RBU database as "rbu". If an
  2334   2335   ** error occurs, leave an error code and message in the RBU handle.
  2335   2336   */
  2336         -static void rbuOpenDatabase(sqlite3rbu *p){
         2337  +static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
  2337   2338     assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
  2338   2339     assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
  2339   2340   
  2340   2341     /* Open the RBU database */
  2341   2342     p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
  2342   2343   
  2343   2344     if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
................................................................................
  2404   2405       rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
  2405   2406       if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
  2406   2407       if( p->eStage>=RBU_STAGE_MOVE ){
  2407   2408         bOpen = 1;
  2408   2409       }else{
  2409   2410         RbuState *pState = rbuLoadState(p);
  2410   2411         if( pState ){
  2411         -        bOpen = (pState->eStage>RBU_STAGE_MOVE);
         2412  +        bOpen = (pState->eStage>=RBU_STAGE_MOVE);
  2412   2413           rbuFreeState(pState);
  2413   2414         }
  2414   2415       }
  2415   2416       if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
  2416   2417     }
  2417   2418   
  2418   2419     p->eStage = 0;
  2419   2420     if( p->rc==SQLITE_OK && p->dbMain==0 ){
  2420   2421       if( !rbuIsVacuum(p) ){
  2421   2422         p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
  2422   2423       }else if( p->pRbuFd->pWalFd ){
         2424  +      if( pbRetry ){
         2425  +        p->pRbuFd->bNolock = 0;
         2426  +        sqlite3_close(p->dbRbu);
         2427  +        sqlite3_close(p->dbMain);
         2428  +        p->dbMain = 0;
         2429  +        p->dbRbu = 0;
         2430  +        *pbRetry = 1;
         2431  +        return;
         2432  +      }
  2423   2433         p->rc = SQLITE_ERROR;
  2424   2434         p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
  2425   2435       }else{
  2426   2436         char *zTarget;
  2427   2437         char *zExtra = 0;
  2428   2438         if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
  2429   2439           zExtra = &p->zRbu[5];
................................................................................
  2596   2606     if( p->rc==SQLITE_OK ){
  2597   2607       int rc2;
  2598   2608       p->eStage = RBU_STAGE_CAPTURE;
  2599   2609       rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
  2600   2610       if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
  2601   2611     }
  2602   2612   
  2603         -  if( p->rc==SQLITE_OK ){
         2613  +  if( p->rc==SQLITE_OK && p->nFrame>0 ){
  2604   2614       p->eStage = RBU_STAGE_CKPT;
  2605   2615       p->nStep = (pState ? pState->nRow : 0);
  2606   2616       p->aBuf = rbuMalloc(p, p->pgsz);
  2607   2617       p->iWalCksum = rbuShmChecksum(p);
  2608   2618     }
  2609   2619   
  2610         -  if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
  2611         -    p->rc = SQLITE_DONE;
  2612         -    p->eStage = RBU_STAGE_DONE;
         2620  +  if( p->rc==SQLITE_OK ){
         2621  +    if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
         2622  +      p->rc = SQLITE_DONE;
         2623  +      p->eStage = RBU_STAGE_DONE;
         2624  +    }else{
         2625  +      int nSectorSize;
         2626  +      sqlite3_file *pDb = p->pTargetFd->pReal;
         2627  +      sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
         2628  +      assert( p->nPagePerSector==0 );
         2629  +      nSectorSize = pDb->pMethods->xSectorSize(pDb);
         2630  +      if( nSectorSize>p->pgsz ){
         2631  +        p->nPagePerSector = nSectorSize / p->pgsz;
         2632  +      }else{
         2633  +        p->nPagePerSector = 1;
         2634  +      }
         2635  +
         2636  +      /* Call xSync() on the wal file. This causes SQLite to sync the 
         2637  +      ** directory in which the target database and the wal file reside, in 
         2638  +      ** case it has not been synced since the rename() call in 
         2639  +      ** rbuMoveOalFile(). */
         2640  +      p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
         2641  +    }
  2613   2642     }
  2614   2643   }
  2615   2644   
  2616   2645   /*
  2617   2646   ** Called when iAmt bytes are read from offset iOff of the wal file while
  2618   2647   ** the rbu object is in capture mode. Record the frame number of the frame
  2619   2648   ** being read in the aFrame[] array.
................................................................................
  2778   2807           }
  2779   2808         }
  2780   2809   #else
  2781   2810         p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
  2782   2811   #endif
  2783   2812   
  2784   2813         if( p->rc==SQLITE_OK ){
  2785         -        rbuOpenDatabase(p);
         2814  +        rbuOpenDatabase(p, 0);
  2786   2815           rbuSetupCheckpoint(p, 0);
  2787   2816         }
  2788   2817       }
  2789   2818     }
  2790   2819   
  2791   2820     sqlite3_free(zWal);
  2792   2821     sqlite3_free(zOal);
................................................................................
  3260   3289               }
  3261   3290     
  3262   3291               if( p->rc==SQLITE_OK ){
  3263   3292                 p->eStage = RBU_STAGE_DONE;
  3264   3293                 p->rc = SQLITE_DONE;
  3265   3294               }
  3266   3295             }else{
  3267         -            RbuFrame *pFrame = &p->aFrame[p->nStep];
  3268         -            rbuCheckpointFrame(p, pFrame);
  3269         -            p->nStep++;
         3296  +            /* At one point the following block copied a single frame from the
         3297  +            ** wal file to the database file. So that one call to sqlite3rbu_step()
         3298  +            ** checkpointed a single frame. 
         3299  +            **
         3300  +            ** However, if the sector-size is larger than the page-size, and the
         3301  +            ** application calls sqlite3rbu_savestate() or close() immediately
         3302  +            ** after this step, then rbu_step() again, then a power failure occurs,
         3303  +            ** then the database page written here may be damaged. Work around
         3304  +            ** this by checkpointing frames until the next page in the aFrame[]
         3305  +            ** lies on a different disk sector to the current one. */
         3306  +            u32 iSector;
         3307  +            do{
         3308  +              RbuFrame *pFrame = &p->aFrame[p->nStep];
         3309  +              iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
         3310  +              rbuCheckpointFrame(p, pFrame);
         3311  +              p->nStep++;
         3312  +            }while( p->nStep<p->nFrame 
         3313  +                 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
         3314  +                 && p->rc==SQLITE_OK
         3315  +            );
  3270   3316             }
  3271   3317             p->nProgress++;
  3272   3318           }
  3273   3319           break;
  3274   3320         }
  3275   3321   
  3276   3322         default:
................................................................................
  3489   3535       /* Create the custom VFS. */
  3490   3536       memset(p, 0, sizeof(sqlite3rbu));
  3491   3537       rbuCreateVfs(p);
  3492   3538   
  3493   3539       /* Open the target, RBU and state databases */
  3494   3540       if( p->rc==SQLITE_OK ){
  3495   3541         char *pCsr = (char*)&p[1];
         3542  +      int bRetry = 0;
  3496   3543         if( zTarget ){
  3497   3544           p->zTarget = pCsr;
  3498   3545           memcpy(p->zTarget, zTarget, nTarget+1);
  3499   3546           pCsr += nTarget+1;
  3500   3547         }
  3501   3548         p->zRbu = pCsr;
  3502   3549         memcpy(p->zRbu, zRbu, nRbu+1);
  3503   3550         pCsr += nRbu+1;
  3504   3551         if( zState ){
  3505   3552           p->zState = rbuMPrintf(p, "%s", zState);
  3506   3553         }
  3507         -      rbuOpenDatabase(p);
         3554  +
         3555  +      /* If the first attempt to open the database file fails and the bRetry
         3556  +      ** flag it set, this means that the db was not opened because it seemed
         3557  +      ** to be a wal-mode db. But, this may have happened due to an earlier
         3558  +      ** RBU vacuum operation leaving an old wal file in the directory.
         3559  +      ** If this is the case, it will have been checkpointed and deleted
         3560  +      ** when the handle was closed and a second attempt to open the 
         3561  +      ** database may succeed.  */
         3562  +      rbuOpenDatabase(p, &bRetry);
         3563  +      if( bRetry ){
         3564  +        rbuOpenDatabase(p, 0);
         3565  +      }
  3508   3566       }
  3509   3567   
  3510   3568       if( p->rc==SQLITE_OK ){
  3511   3569         pState = rbuLoadState(p);
  3512   3570         assert( pState || p->rc!=SQLITE_OK );
  3513   3571         if( p->rc==SQLITE_OK ){
  3514   3572   
................................................................................
  3690   3748     int rc;
  3691   3749     if( p ){
  3692   3750   
  3693   3751       /* Commit the transaction to the *-oal file. */
  3694   3752       if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
  3695   3753         p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
  3696   3754       }
         3755  +
         3756  +    /* Sync the db file if currently doing an incremental checkpoint */
         3757  +    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
         3758  +      sqlite3_file *pDb = p->pTargetFd->pReal;
         3759  +      p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
         3760  +    }
  3697   3761   
  3698   3762       rbuSaveState(p, p->eStage);
  3699   3763   
  3700   3764       if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
  3701   3765         p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
  3702   3766       }
  3703   3767   
................................................................................
  3814   3878     if( rc==SQLITE_DONE ) return SQLITE_OK;
  3815   3879   
  3816   3880     assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
  3817   3881     if( p->eStage==RBU_STAGE_OAL ){
  3818   3882       assert( rc!=SQLITE_DONE );
  3819   3883       if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
  3820   3884     }
         3885  +
         3886  +  /* Sync the db file */
         3887  +  if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
         3888  +    sqlite3_file *pDb = p->pTargetFd->pReal;
         3889  +    rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
         3890  +  }
  3821   3891   
  3822   3892     p->rc = rc;
  3823   3893     rbuSaveState(p, p->eStage);
  3824   3894     rc = p->rc;
  3825   3895   
  3826   3896     if( p->eStage==RBU_STAGE_OAL ){
  3827   3897       assert( rc!=SQLITE_DONE );

Changes to ext/rtree/rtree.c.

    64     64   #include <string.h>
    65     65   #include <assert.h>
    66     66   #include <stdio.h>
    67     67   
    68     68   #ifndef SQLITE_AMALGAMATION
    69     69   #include "sqlite3rtree.h"
    70     70   typedef sqlite3_int64 i64;
           71  +typedef sqlite3_uint64 u64;
    71     72   typedef unsigned char u8;
    72     73   typedef unsigned short u16;
    73     74   typedef unsigned int u32;
    74     75   #endif
    75     76   
    76     77   /*  The following macro is used to suppress compiler warnings.
    77     78   */
................................................................................
   112    113   ** An rtree virtual-table object.
   113    114   */
   114    115   struct Rtree {
   115    116     sqlite3_vtab base;          /* Base class.  Must be first */
   116    117     sqlite3 *db;                /* Host database connection */
   117    118     int iNodeSize;              /* Size in bytes of each node in the node table */
   118    119     u8 nDim;                    /* Number of dimensions */
          120  +  u8 nDim2;                   /* Twice the number of dimensions */
   119    121     u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
   120    122     u8 nBytesPerCell;           /* Bytes consumed per cell */
          123  +  u8 inWrTrans;               /* True if inside write transaction */
   121    124     int iDepth;                 /* Current depth of the r-tree structure */
   122    125     char *zDb;                  /* Name of database containing r-tree table */
   123    126     char *zName;                /* Name of r-tree table */ 
   124         -  int nBusy;                  /* Current number of users of this structure */
          127  +  u32 nBusy;                  /* Current number of users of this structure */
   125    128     i64 nRowEst;                /* Estimated number of rows in this table */
          129  +  u32 nCursor;                /* Number of open cursors */
   126    130   
   127    131     /* List of nodes removed during a CondenseTree operation. List is
   128    132     ** linked together via the pointer normally used for hash chains -
   129    133     ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
   130    134     ** headed by the node (leaf nodes have RtreeNode.iNode==0).
   131    135     */
   132    136     RtreeNode *pDeleted;
   133    137     int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
          138  +
          139  +  /* Blob I/O on xxx_node */
          140  +  sqlite3_blob *pNodeBlob;
   134    141   
   135    142     /* Statements to read/write/delete a record from xxx_node */
   136         -  sqlite3_stmt *pReadNode;
   137    143     sqlite3_stmt *pWriteNode;
   138    144     sqlite3_stmt *pDeleteNode;
   139    145   
   140    146     /* Statements to read/write/delete a record from xxx_rowid */
   141    147     sqlite3_stmt *pReadRowid;
   142    148     sqlite3_stmt *pWriteRowid;
   143    149     sqlite3_stmt *pDeleteRowid;
................................................................................
   358    364   #ifndef MAX
   359    365   # define MAX(x,y) ((x) < (y) ? (y) : (x))
   360    366   #endif
   361    367   #ifndef MIN
   362    368   # define MIN(x,y) ((x) > (y) ? (y) : (x))
   363    369   #endif
   364    370   
          371  +/* What version of GCC is being used.  0 means GCC is not being used .
          372  +** Note that the GCC_VERSION macro will also be set correctly when using
          373  +** clang, since clang works hard to be gcc compatible.  So the gcc
          374  +** optimizations will also work when compiling with clang.
          375  +*/
          376  +#ifndef GCC_VERSION
          377  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
          378  +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
          379  +#else
          380  +# define GCC_VERSION 0
          381  +#endif
          382  +#endif
          383  +
          384  +/* The testcase() macro should already be defined in the amalgamation.  If
          385  +** it is not, make it a no-op.
          386  +*/
          387  +#ifndef SQLITE_AMALGAMATION
          388  +# define testcase(X)
          389  +#endif
          390  +
          391  +/*
          392  +** Macros to determine whether the machine is big or little endian,
          393  +** and whether or not that determination is run-time or compile-time.
          394  +**
          395  +** For best performance, an attempt is made to guess at the byte-order
          396  +** using C-preprocessor macros.  If that is unsuccessful, or if
          397  +** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
          398  +** at run-time.
          399  +*/
          400  +#ifndef SQLITE_BYTEORDER
          401  +#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
          402  +    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
          403  +    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
          404  +    defined(__arm__)
          405  +# define SQLITE_BYTEORDER    1234
          406  +#elif defined(sparc)    || defined(__ppc__)
          407  +# define SQLITE_BYTEORDER    4321
          408  +#else
          409  +# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
          410  +#endif
          411  +#endif
          412  +
          413  +
          414  +/* What version of MSVC is being used.  0 means MSVC is not being used */
          415  +#ifndef MSVC_VERSION
          416  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
          417  +# define MSVC_VERSION _MSC_VER
          418  +#else
          419  +# define MSVC_VERSION 0
          420  +#endif
          421  +#endif
          422  +
   365    423   /*
   366    424   ** Functions to deserialize a 16 bit integer, 32 bit real number and
   367    425   ** 64 bit integer. The deserialized value is returned.
   368    426   */
   369    427   static int readInt16(u8 *p){
   370    428     return (p[0]<<8) + p[1];
   371    429   }
   372    430   static void readCoord(u8 *p, RtreeCoord *pCoord){
          431  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
          432  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
          433  +  pCoord->u = _byteswap_ulong(*(u32*)p);
          434  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
          435  +  pCoord->u = __builtin_bswap32(*(u32*)p);
          436  +#elif SQLITE_BYTEORDER==4321
          437  +  pCoord->u = *(u32*)p;
          438  +#else
   373    439     pCoord->u = (
   374    440       (((u32)p[0]) << 24) + 
   375    441       (((u32)p[1]) << 16) + 
   376    442       (((u32)p[2]) <<  8) + 
   377    443       (((u32)p[3]) <<  0)
   378    444     );
          445  +#endif
   379    446   }
   380    447   static i64 readInt64(u8 *p){
   381         -  return (
   382         -    (((i64)p[0]) << 56) + 
   383         -    (((i64)p[1]) << 48) + 
   384         -    (((i64)p[2]) << 40) + 
   385         -    (((i64)p[3]) << 32) + 
   386         -    (((i64)p[4]) << 24) + 
   387         -    (((i64)p[5]) << 16) + 
   388         -    (((i64)p[6]) <<  8) + 
   389         -    (((i64)p[7]) <<  0)
          448  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
          449  +  u64 x;
          450  +  memcpy(&x, p, 8);
          451  +  return (i64)_byteswap_uint64(x);
          452  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
          453  +  u64 x;
          454  +  memcpy(&x, p, 8);
          455  +  return (i64)__builtin_bswap64(x);
          456  +#elif SQLITE_BYTEORDER==4321
          457  +  i64 x;
          458  +  memcpy(&x, p, 8);
          459  +  return x;
          460  +#else
          461  +  return (i64)(
          462  +    (((u64)p[0]) << 56) + 
          463  +    (((u64)p[1]) << 48) + 
          464  +    (((u64)p[2]) << 40) + 
          465  +    (((u64)p[3]) << 32) + 
          466  +    (((u64)p[4]) << 24) + 
          467  +    (((u64)p[5]) << 16) + 
          468  +    (((u64)p[6]) <<  8) + 
          469  +    (((u64)p[7]) <<  0)
   390    470     );
          471  +#endif
   391    472   }
   392    473   
   393    474   /*
   394    475   ** Functions to serialize a 16 bit integer, 32 bit real number and
   395    476   ** 64 bit integer. The value returned is the number of bytes written
   396    477   ** to the argument buffer (always 2, 4 and 8 respectively).
   397    478   */
   398         -static int writeInt16(u8 *p, int i){
          479  +static void writeInt16(u8 *p, int i){
   399    480     p[0] = (i>> 8)&0xFF;
   400    481     p[1] = (i>> 0)&0xFF;
   401         -  return 2;
   402    482   }
   403    483   static int writeCoord(u8 *p, RtreeCoord *pCoord){
   404    484     u32 i;
          485  +  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
   405    486     assert( sizeof(RtreeCoord)==4 );
   406    487     assert( sizeof(u32)==4 );
          488  +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
          489  +  i = __builtin_bswap32(pCoord->u);
          490  +  memcpy(p, &i, 4);
          491  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
          492  +  i = _byteswap_ulong(pCoord->u);
          493  +  memcpy(p, &i, 4);
          494  +#elif SQLITE_BYTEORDER==4321
          495  +  i = pCoord->u;
          496  +  memcpy(p, &i, 4);
          497  +#else
   407    498     i = pCoord->u;
   408    499     p[0] = (i>>24)&0xFF;
   409    500     p[1] = (i>>16)&0xFF;
   410    501     p[2] = (i>> 8)&0xFF;
   411    502     p[3] = (i>> 0)&0xFF;
          503  +#endif
   412    504     return 4;
   413    505   }
   414    506   static int writeInt64(u8 *p, i64 i){
          507  +#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
          508  +  i = (i64)__builtin_bswap64((u64)i);
          509  +  memcpy(p, &i, 8);
          510  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
          511  +  i = (i64)_byteswap_uint64((u64)i);
          512  +  memcpy(p, &i, 8);
          513  +#elif SQLITE_BYTEORDER==4321
          514  +  memcpy(p, &i, 8);
          515  +#else
   415    516     p[0] = (i>>56)&0xFF;
   416    517     p[1] = (i>>48)&0xFF;
   417    518     p[2] = (i>>40)&0xFF;
   418    519     p[3] = (i>>32)&0xFF;
   419    520     p[4] = (i>>24)&0xFF;
   420    521     p[5] = (i>>16)&0xFF;
   421    522     p[6] = (i>> 8)&0xFF;
   422    523     p[7] = (i>> 0)&0xFF;
          524  +#endif
   423    525     return 8;
   424    526   }
   425    527   
   426    528   /*
   427    529   ** Increment the reference count of node p.
   428    530   */
   429    531   static void nodeReference(RtreeNode *p){
................................................................................
   497    599       pNode->nRef = 1;
   498    600       pNode->pParent = pParent;
   499    601       pNode->isDirty = 1;
   500    602       nodeReference(pParent);
   501    603     }
   502    604     return pNode;
   503    605   }
          606  +
          607  +/*
          608  +** Clear the Rtree.pNodeBlob object
          609  +*/
          610  +static void nodeBlobReset(Rtree *pRtree){
          611  +  if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
          612  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
          613  +    pRtree->pNodeBlob = 0;
          614  +    sqlite3_blob_close(pBlob);
          615  +  }
          616  +}
   504    617   
   505    618   /*
   506    619   ** Obtain a reference to an r-tree node.
   507    620   */
   508    621   static int nodeAcquire(
   509    622     Rtree *pRtree,             /* R-tree structure */
   510    623     i64 iNode,                 /* Node number to load */
   511    624     RtreeNode *pParent,        /* Either the parent node or NULL */
   512    625     RtreeNode **ppNode         /* OUT: Acquired node */
   513    626   ){
   514         -  int rc;
   515         -  int rc2 = SQLITE_OK;
   516         -  RtreeNode *pNode;
          627  +  int rc = SQLITE_OK;
          628  +  RtreeNode *pNode = 0;
   517    629   
   518    630     /* Check if the requested node is already in the hash table. If so,
   519    631     ** increase its reference count and return it.
   520    632     */
   521    633     if( (pNode = nodeHashLookup(pRtree, iNode)) ){
   522    634       assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
   523    635       if( pParent && !pNode->pParent ){
................................................................................
   525    637         pNode->pParent = pParent;
   526    638       }
   527    639       pNode->nRef++;
   528    640       *ppNode = pNode;
   529    641       return SQLITE_OK;
   530    642     }
   531    643   
   532         -  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
   533         -  rc = sqlite3_step(pRtree->pReadNode);
   534         -  if( rc==SQLITE_ROW ){
   535         -    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
   536         -    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
   537         -      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
   538         -      if( !pNode ){
   539         -        rc2 = SQLITE_NOMEM;
   540         -      }else{
   541         -        pNode->pParent = pParent;
   542         -        pNode->zData = (u8 *)&pNode[1];
   543         -        pNode->nRef = 1;
   544         -        pNode->iNode = iNode;
   545         -        pNode->isDirty = 0;
   546         -        pNode->pNext = 0;
   547         -        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
   548         -        nodeReference(pParent);
   549         -      }
   550         -    }
   551         -  }
   552         -  rc = sqlite3_reset(pRtree->pReadNode);
   553         -  if( rc==SQLITE_OK ) rc = rc2;
          644  +  if( pRtree->pNodeBlob ){
          645  +    sqlite3_blob *pBlob = pRtree->pNodeBlob;
          646  +    pRtree->pNodeBlob = 0;
          647  +    rc = sqlite3_blob_reopen(pBlob, iNode);
          648  +    pRtree->pNodeBlob = pBlob;
          649  +    if( rc ){
          650  +      nodeBlobReset(pRtree);
          651  +      if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
          652  +    }
          653  +  }
          654  +  if( pRtree->pNodeBlob==0 ){
          655  +    char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
          656  +    if( zTab==0 ) return SQLITE_NOMEM;
          657  +    rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
          658  +                           &pRtree->pNodeBlob);
          659  +    sqlite3_free(zTab);
          660  +  }
          661  +  if( rc ){
          662  +    nodeBlobReset(pRtree);
          663  +    *ppNode = 0;
          664  +    /* If unable to open an sqlite3_blob on the desired row, that can only
          665  +    ** be because the shadow tables hold erroneous data. */
          666  +    if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
          667  +  }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
          668  +    pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
          669  +    if( !pNode ){
          670  +      rc = SQLITE_NOMEM;
          671  +    }else{
          672  +      pNode->pParent = pParent;
          673  +      pNode->zData = (u8 *)&pNode[1];
          674  +      pNode->nRef = 1;
          675  +      pNode->iNode = iNode;
          676  +      pNode->isDirty = 0;
          677  +      pNode->pNext = 0;
          678  +      rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
          679  +                             pRtree->iNodeSize, 0);
          680  +      nodeReference(pParent);
          681  +    }
          682  +  }
   554    683   
   555    684     /* If the root node was just loaded, set pRtree->iDepth to the height
   556    685     ** of the r-tree structure. A height of zero means all data is stored on
   557    686     ** the root node. A height of one means the children of the root node
   558    687     ** are the leaves, and so on. If the depth as specified on the root node
   559    688     ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
   560    689     */
................................................................................
   598    727     RtreeNode *pNode,          /* The node into which the cell is to be written */
   599    728     RtreeCell *pCell,          /* The cell to write */
   600    729     int iCell                  /* Index into pNode into which pCell is written */
   601    730   ){
   602    731     int ii;
   603    732     u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
   604    733     p += writeInt64(p, pCell->iRowid);
   605         -  for(ii=0; ii<(pRtree->nDim*2); ii++){
          734  +  for(ii=0; ii<pRtree->nDim2; ii++){
   606    735       p += writeCoord(p, &pCell->aCoord[ii]);
   607    736     }
   608    737     pNode->isDirty = 1;
   609    738   }
   610    739   
   611    740   /*
   612    741   ** Remove the cell with index iCell from node pNode.
................................................................................
   732    861     Rtree *pRtree,               /* The overall R-Tree */
   733    862     RtreeNode *pNode,            /* The node containing the cell to be read */
   734    863     int iCell,                   /* Index of the cell within the node */
   735    864     RtreeCell *pCell             /* OUT: Write the cell contents here */
   736    865   ){
   737    866     u8 *pData;
   738    867     RtreeCoord *pCoord;
   739         -  int ii;
          868  +  int ii = 0;
   740    869     pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
   741    870     pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
   742    871     pCoord = pCell->aCoord;
   743         -  for(ii=0; ii<pRtree->nDim*2; ii++){
   744         -    readCoord(&pData[ii*4], &pCoord[ii]);
   745         -  }
          872  +  do{
          873  +    readCoord(pData, &pCoord[ii]);
          874  +    readCoord(pData+4, &pCoord[ii+1]);
          875  +    pData += 8;
          876  +    ii += 2;
          877  +  }while( ii<pRtree->nDim2 );
   746    878   }
   747    879   
   748    880   
   749    881   /* Forward declaration for the function that does the work of
   750    882   ** the virtual table module xCreate() and xConnect() methods.
   751    883   */
   752    884   static int rtreeInit(
................................................................................
   789    921   /*
   790    922   ** Decrement the r-tree reference count. When the reference count reaches
   791    923   ** zero the structure is deleted.
   792    924   */
   793    925   static void rtreeRelease(Rtree *pRtree){
   794    926     pRtree->nBusy--;
   795    927     if( pRtree->nBusy==0 ){
   796         -    sqlite3_finalize(pRtree->pReadNode);
          928  +    pRtree->inWrTrans = 0;
          929  +    pRtree->nCursor = 0;
          930  +    nodeBlobReset(pRtree);
   797    931       sqlite3_finalize(pRtree->pWriteNode);
   798    932       sqlite3_finalize(pRtree->pDeleteNode);
   799    933       sqlite3_finalize(pRtree->pReadRowid);
   800    934       sqlite3_finalize(pRtree->pWriteRowid);
   801    935       sqlite3_finalize(pRtree->pDeleteRowid);
   802    936       sqlite3_finalize(pRtree->pReadParent);
   803    937       sqlite3_finalize(pRtree->pWriteParent);
................................................................................
   827    961       pRtree->zDb, pRtree->zName, 
   828    962       pRtree->zDb, pRtree->zName,
   829    963       pRtree->zDb, pRtree->zName
   830    964     );
   831    965     if( !zCreate ){
   832    966       rc = SQLITE_NOMEM;
   833    967     }else{
          968  +    nodeBlobReset(pRtree);
   834    969       rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
   835    970       sqlite3_free(zCreate);
   836    971     }
   837    972     if( rc==SQLITE_OK ){
   838    973       rtreeRelease(pRtree);
   839    974     }
   840    975   
................................................................................
   842    977   }
   843    978   
   844    979   /* 
   845    980   ** Rtree virtual table module xOpen method.
   846    981   */
   847    982   static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
   848    983     int rc = SQLITE_NOMEM;
          984  +  Rtree *pRtree = (Rtree *)pVTab;
   849    985     RtreeCursor *pCsr;
   850    986   
   851    987     pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
   852    988     if( pCsr ){
   853    989       memset(pCsr, 0, sizeof(RtreeCursor));
   854    990       pCsr->base.pVtab = pVTab;
   855    991       rc = SQLITE_OK;
          992  +    pRtree->nCursor++;
   856    993     }
   857    994     *ppCursor = (sqlite3_vtab_cursor *)pCsr;
   858    995   
   859    996     return rc;
   860    997   }
   861    998   
   862    999   
................................................................................
   881   1018   /* 
   882   1019   ** Rtree virtual table module xClose method.
   883   1020   */
   884   1021   static int rtreeClose(sqlite3_vtab_cursor *cur){
   885   1022     Rtree *pRtree = (Rtree *)(cur->pVtab);
   886   1023     int ii;
   887   1024     RtreeCursor *pCsr = (RtreeCursor *)cur;
         1025  +  assert( pRtree->nCursor>0 );
   888   1026     freeCursorConstraints(pCsr);
   889   1027     sqlite3_free(pCsr->aPoint);
   890   1028     for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
   891   1029     sqlite3_free(pCsr);
         1030  +  pRtree->nCursor--;
         1031  +  nodeBlobReset(pRtree);
   892   1032     return SQLITE_OK;
   893   1033   }
   894   1034   
   895   1035   /*
   896   1036   ** Rtree virtual table module xEof method.
   897   1037   **
   898   1038   ** Return non-zero if the cursor does not currently point to a valid 
................................................................................
   907   1047   ** Convert raw bits from the on-disk RTree record into a coordinate value.
   908   1048   ** The on-disk format is big-endian and needs to be converted for little-
   909   1049   ** endian platforms.  The on-disk record stores integer coordinates if
   910   1050   ** eInt is true and it stores 32-bit floating point records if eInt is
   911   1051   ** false.  a[] is the four bytes of the on-disk record to be decoded.
   912   1052   ** Store the results in "r".
   913   1053   **
   914         -** There are three versions of this macro, one each for little-endian and
   915         -** big-endian processors and a third generic implementation.  The endian-
   916         -** specific implementations are much faster and are preferred if the
   917         -** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
   918         -** macro is part of sqliteInt.h and hence the endian-specific
   919         -** implementation will only be used if this module is compiled as part
   920         -** of the amalgamation.
         1054  +** There are five versions of this macro.  The last one is generic.  The
         1055  +** other four are various architectures-specific optimizations.
   921   1056   */
   922         -#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
         1057  +#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
         1058  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
         1059  +    RtreeCoord c;    /* Coordinate decoded */                   \
         1060  +    c.u = _byteswap_ulong(*(u32*)a);                            \
         1061  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
         1062  +}
         1063  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
         1064  +#define RTREE_DECODE_COORD(eInt, a, r) {                        \
         1065  +    RtreeCoord c;    /* Coordinate decoded */                   \
         1066  +    c.u = __builtin_bswap32(*(u32*)a);                          \
         1067  +    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
         1068  +}
         1069  +#elif SQLITE_BYTEORDER==1234
   923   1070   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
   924   1071       RtreeCoord c;    /* Coordinate decoded */                   \
   925   1072       memcpy(&c.u,a,4);                                           \
   926   1073       c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)|                   \
   927   1074             ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
   928   1075       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
   929   1076   }
   930         -#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
         1077  +#elif SQLITE_BYTEORDER==4321
   931   1078   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
   932   1079       RtreeCoord c;    /* Coordinate decoded */                   \
   933   1080       memcpy(&c.u,a,4);                                           \
   934   1081       r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
   935   1082   }
   936   1083   #else
   937   1084   #define RTREE_DECODE_COORD(eInt, a, r) {                        \
................................................................................
   950   1097     RtreeConstraint *pConstraint,  /* The constraint to test */
   951   1098     int eInt,                      /* True if RTree holding integer coordinates */
   952   1099     u8 *pCellData,                 /* Raw cell content */
   953   1100     RtreeSearchPoint *pSearch,     /* Container of this cell */
   954   1101     sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
   955   1102     int *peWithin                  /* OUT: visibility of the cell */
   956   1103   ){
   957         -  int i;                                                /* Loop counter */
   958   1104     sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
   959   1105     int nCoord = pInfo->nCoord;                           /* No. of coordinates */
   960   1106     int rc;                                             /* Callback return code */
         1107  +  RtreeCoord c;                                       /* Translator union */
   961   1108     sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
   962   1109   
   963   1110     assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
   964   1111     assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
   965   1112   
   966   1113     if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
   967   1114       pInfo->iRowid = readInt64(pCellData);
   968   1115     }
   969   1116     pCellData += 8;
   970         -  for(i=0; i<nCoord; i++, pCellData += 4){
   971         -    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
         1117  +#ifndef SQLITE_RTREE_INT_ONLY
         1118  +  if( eInt==0 ){
         1119  +    switch( nCoord ){
         1120  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.f;
         1121  +                readCoord(pCellData+32, &c); aCoord[8] = c.f;
         1122  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.f;
         1123  +                readCoord(pCellData+24, &c); aCoord[6] = c.f;
         1124  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.f;
         1125  +                readCoord(pCellData+16, &c); aCoord[4] = c.f;
         1126  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.f;
         1127  +                readCoord(pCellData+8,  &c); aCoord[2] = c.f;
         1128  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.f;
         1129  +                readCoord(pCellData,    &c); aCoord[0] = c.f;
         1130  +    }
         1131  +  }else
         1132  +#endif
         1133  +  {
         1134  +    switch( nCoord ){
         1135  +      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.i;
         1136  +                readCoord(pCellData+32, &c); aCoord[8] = c.i;
         1137  +      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.i;
         1138  +                readCoord(pCellData+24, &c); aCoord[6] = c.i;
         1139  +      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.i;
         1140  +                readCoord(pCellData+16, &c); aCoord[4] = c.i;
         1141  +      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.i;
         1142  +                readCoord(pCellData+8,  &c); aCoord[2] = c.i;
         1143  +      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.i;
         1144  +                readCoord(pCellData,    &c); aCoord[0] = c.i;
         1145  +    }
   972   1146     }
   973   1147     if( pConstraint->op==RTREE_MATCH ){
         1148  +    int eWithin = 0;
   974   1149       rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
   975         -                              nCoord, aCoord, &i);
   976         -    if( i==0 ) *peWithin = NOT_WITHIN;
         1150  +                              nCoord, aCoord, &eWithin);
         1151  +    if( eWithin==0 ) *peWithin = NOT_WITHIN;
   977   1152       *prScore = RTREE_ZERO;
   978   1153     }else{
   979   1154       pInfo->aCoord = aCoord;
   980   1155       pInfo->iLevel = pSearch->iLevel - 1;
   981   1156       pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
   982   1157       pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
   983   1158       rc = pConstraint->u.xQueryFunc(pInfo);
................................................................................
  1005   1180     /* p->iCoord might point to either a lower or upper bound coordinate
  1006   1181     ** in a coordinate pair.  But make pCellData point to the lower bound.
  1007   1182     */
  1008   1183     pCellData += 8 + 4*(p->iCoord&0xfe);
  1009   1184   
  1010   1185     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
  1011   1186         || p->op==RTREE_GT || p->op==RTREE_EQ );
         1187  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
  1012   1188     switch( p->op ){
  1013   1189       case RTREE_LE:
  1014   1190       case RTREE_LT:
  1015   1191       case RTREE_EQ:
  1016   1192         RTREE_DECODE_COORD(eInt, pCellData, val);
  1017   1193         /* val now holds the lower bound of the coordinate pair */
  1018   1194         if( p->u.rValue>=val ) return;
................................................................................
  1045   1221     int *peWithin              /* Adjust downward, as appropriate */
  1046   1222   ){
  1047   1223     RtreeDValue xN;      /* Coordinate value converted to a double */
  1048   1224   
  1049   1225     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
  1050   1226         || p->op==RTREE_GT || p->op==RTREE_EQ );
  1051   1227     pCellData += 8 + p->iCoord*4;
         1228  +  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
  1052   1229     RTREE_DECODE_COORD(eInt, pCellData, xN);
  1053   1230     switch( p->op ){
  1054   1231       case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
  1055   1232       case RTREE_LT: if( xN <  p->u.rValue ) return;  break;
  1056   1233       case RTREE_GE: if( xN >= p->u.rValue ) return;  break;
  1057   1234       case RTREE_GT: if( xN >  p->u.rValue ) return;  break;
  1058   1235       default:       if( xN == p->u.rValue ) return;  break;
................................................................................
  1113   1290     if( pA->rScore>pB->rScore ) return +1;
  1114   1291     if( pA->iLevel<pB->iLevel ) return -1;
  1115   1292     if( pA->iLevel>pB->iLevel ) return +1;
  1116   1293     return 0;
  1117   1294   }
  1118   1295   
  1119   1296   /*
  1120         -** Interchange to search points in a cursor.
         1297  +** Interchange two search points in a cursor.
  1121   1298   */
  1122   1299   static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
  1123   1300     RtreeSearchPoint t = p->aPoint[i];
  1124   1301     assert( i<j );
  1125   1302     p->aPoint[i] = p->aPoint[j];
  1126   1303     p->aPoint[j] = t;
  1127   1304     i++; j++;
................................................................................
  1361   1538         if( p->iCell>=nCell ){
  1362   1539           RTREE_QUEUE_TRACE(pCur, "POP-S:");
  1363   1540           rtreeSearchPointPop(pCur);
  1364   1541         }
  1365   1542         if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
  1366   1543         p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
  1367   1544         if( p==0 ) return SQLITE_NOMEM;
  1368         -      p->eWithin = eWithin;
         1545  +      p->eWithin = (u8)eWithin;
  1369   1546         p->id = x.id;
  1370   1547         p->iCell = x.iCell;
  1371   1548         RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
  1372   1549         break;
  1373   1550       }
  1374   1551       if( p->iCell>=nCell ){
  1375   1552         RTREE_QUEUE_TRACE(pCur, "POP-Se:");
................................................................................
  1420   1597     RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
  1421   1598   
  1422   1599     if( rc ) return rc;
  1423   1600     if( p==0 ) return SQLITE_OK;
  1424   1601     if( i==0 ){
  1425   1602       sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
  1426   1603     }else{
  1427         -    if( rc ) return rc;
  1428   1604       nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
  1429   1605   #ifndef SQLITE_RTREE_INT_ONLY
  1430   1606       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  1431   1607         sqlite3_result_double(ctx, c.f);
  1432   1608       }else
  1433   1609   #endif
  1434   1610       {
................................................................................
  1549   1725       if( rc==SQLITE_OK && pLeaf!=0 ){
  1550   1726         p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
  1551   1727         assert( p!=0 );  /* Always returns pCsr->sPoint */
  1552   1728         pCsr->aNode[0] = pLeaf;
  1553   1729         p->id = iNode;
  1554   1730         p->eWithin = PARTLY_WITHIN;
  1555   1731         rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
  1556         -      p->iCell = iCell;
         1732  +      p->iCell = (u8)iCell;
  1557   1733         RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
  1558   1734       }else{
  1559   1735         pCsr->atEOF = 1;
  1560   1736       }
  1561   1737     }else{
  1562   1738       /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
  1563   1739       ** with the configured constraints. 
................................................................................
  1582   1758               ** can be cast into an RtreeMatchArg object. One created using
  1583   1759               ** an sqlite3_rtree_geometry_callback() SQL user function.
  1584   1760               */
  1585   1761               rc = deserializeGeometry(argv[ii], p);
  1586   1762               if( rc!=SQLITE_OK ){
  1587   1763                 break;
  1588   1764               }
  1589         -            p->pInfo->nCoord = pRtree->nDim*2;
         1765  +            p->pInfo->nCoord = pRtree->nDim2;
  1590   1766               p->pInfo->anQueue = pCsr->anQueue;
  1591   1767               p->pInfo->mxLevel = pRtree->iDepth + 1;
  1592   1768             }else{
  1593   1769   #ifdef SQLITE_RTREE_INT_ONLY
  1594   1770               p->u.rValue = sqlite3_value_int64(argv[ii]);
  1595   1771   #else
  1596   1772               p->u.rValue = sqlite3_value_double(argv[ii]);
................................................................................
  1597   1773   #endif
  1598   1774             }
  1599   1775           }
  1600   1776         }
  1601   1777       }
  1602   1778       if( rc==SQLITE_OK ){
  1603   1779         RtreeSearchPoint *pNew;
  1604         -      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
         1780  +      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
  1605   1781         if( pNew==0 ) return SQLITE_NOMEM;
  1606   1782         pNew->id = 1;
  1607   1783         pNew->iCell = 0;
  1608   1784         pNew->eWithin = PARTLY_WITHIN;
  1609   1785         assert( pCsr->bPoint==1 );
  1610   1786         pCsr->aNode[0] = pRoot;
  1611   1787         pRoot = 0;
................................................................................
  1615   1791     }
  1616   1792   
  1617   1793     nodeRelease(pRtree, pRoot);
  1618   1794     rtreeRelease(pRtree);
  1619   1795     return rc;
  1620   1796   }
  1621   1797   
  1622         -/*
  1623         -** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
  1624         -** extension is currently being used by a version of SQLite too old to
  1625         -** support estimatedRows. In that case this function is a no-op.
  1626         -*/
  1627         -static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
  1628         -#if SQLITE_VERSION_NUMBER>=3008002
  1629         -  if( sqlite3_libversion_number()>=3008002 ){
  1630         -    pIdxInfo->estimatedRows = nRow;
  1631         -  }
  1632         -#endif
  1633         -}
  1634         -
  1635   1798   /*
  1636   1799   ** Rtree virtual table module xBestIndex method. There are three
  1637   1800   ** table scan strategies to choose from (in order from most to 
  1638   1801   ** least desirable):
  1639   1802   **
  1640   1803   **   idxNum     idxStr        Strategy
  1641   1804   **   ------------------------------------------------
................................................................................
  1707   1870         /* This strategy involves a two rowid lookups on an B-Tree structures
  1708   1871         ** and then a linear search of an R-Tree node. This should be 
  1709   1872         ** considered almost as quick as a direct rowid lookup (for which 
  1710   1873         ** sqlite uses an internal cost of 0.0). It is expected to return
  1711   1874         ** a single row.
  1712   1875         */ 
  1713   1876         pIdxInfo->estimatedCost = 30.0;
  1714         -      setEstimatedRows(pIdxInfo, 1);
         1877  +      pIdxInfo->estimatedRows = 1;
  1715   1878         return SQLITE_OK;
  1716   1879       }
  1717   1880   
  1718   1881       if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
  1719   1882         u8 op;
  1720   1883         switch( p->op ){
  1721   1884           case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
................................................................................
  1725   1888           case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
  1726   1889           default:
  1727   1890             assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
  1728   1891             op = RTREE_MATCH; 
  1729   1892             break;
  1730   1893         }
  1731   1894         zIdxStr[iIdx++] = op;
  1732         -      zIdxStr[iIdx++] = p->iColumn - 1 + '0';
         1895  +      zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
  1733   1896         pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
  1734   1897         pIdxInfo->aConstraintUsage[ii].omit = 1;
  1735   1898       }
  1736   1899     }
  1737   1900   
  1738   1901     pIdxInfo->idxNum = 2;
  1739   1902     pIdxInfo->needToFreeIdxStr = 1;
  1740   1903     if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
  1741   1904       return SQLITE_NOMEM;
  1742   1905     }
  1743   1906   
  1744   1907     nRow = pRtree->nRowEst >> (iIdx/2);
  1745   1908     pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
  1746         -  setEstimatedRows(pIdxInfo, nRow);
         1909  +  pIdxInfo->estimatedRows = nRow;
  1747   1910   
  1748   1911     return rc;
  1749   1912   }
  1750   1913   
  1751   1914   /*
  1752   1915   ** Return the N-dimensional volumn of the cell stored in *p.
  1753   1916   */
  1754   1917   static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
  1755   1918     RtreeDValue area = (RtreeDValue)1;
  1756         -  int ii;
  1757         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  1758         -    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
         1919  +  assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
         1920  +#ifndef SQLITE_RTREE_INT_ONLY
         1921  +  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
         1922  +    switch( pRtree->nDim ){
         1923  +      case 5:  area  = p->aCoord[9].f - p->aCoord[8].f;
         1924  +      case 4:  area *= p->aCoord[7].f - p->aCoord[6].f;
         1925  +      case 3:  area *= p->aCoord[5].f - p->aCoord[4].f;
         1926  +      case 2:  area *= p->aCoord[3].f - p->aCoord[2].f;
         1927  +      default: area *= p->aCoord[1].f - p->aCoord[0].f;
         1928  +    }
         1929  +  }else
         1930  +#endif
         1931  +  {
         1932  +    switch( pRtree->nDim ){
         1933  +      case 5:  area  = p->aCoord[9].i - p->aCoord[8].i;
         1934  +      case 4:  area *= p->aCoord[7].i - p->aCoord[6].i;
         1935  +      case 3:  area *= p->aCoord[5].i - p->aCoord[4].i;
         1936  +      case 2:  area *= p->aCoord[3].i - p->aCoord[2].i;
         1937  +      default: area *= p->aCoord[1].i - p->aCoord[0].i;
         1938  +    }
  1759   1939     }
  1760   1940     return area;
  1761   1941   }
  1762   1942   
  1763   1943   /*
  1764   1944   ** Return the margin length of cell p. The margin length is the sum
  1765   1945   ** of the objects size in each dimension.
  1766   1946   */
  1767   1947   static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
  1768         -  RtreeDValue margin = (RtreeDValue)0;
  1769         -  int ii;
  1770         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         1948  +  RtreeDValue margin = 0;
         1949  +  int ii = pRtree->nDim2 - 2;
         1950  +  do{
  1771   1951       margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
  1772         -  }
         1952  +    ii -= 2;
         1953  +  }while( ii>=0 );
  1773   1954     return margin;
  1774   1955   }
  1775   1956   
  1776   1957   /*
  1777   1958   ** Store the union of cells p1 and p2 in p1.
  1778   1959   */
  1779   1960   static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
  1780         -  int ii;
         1961  +  int ii = 0;
  1781   1962     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  1782         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         1963  +    do{
  1783   1964         p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
  1784   1965         p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
  1785         -    }
         1966  +      ii += 2;
         1967  +    }while( ii<pRtree->nDim2 );
  1786   1968     }else{
  1787         -    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         1969  +    do{
  1788   1970         p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
  1789   1971         p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
  1790         -    }
         1972  +      ii += 2;
         1973  +    }while( ii<pRtree->nDim2 );
  1791   1974     }
  1792   1975   }
  1793   1976   
  1794   1977   /*
  1795   1978   ** Return true if the area covered by p2 is a subset of the area covered
  1796   1979   ** by p1. False otherwise.
  1797   1980   */
  1798   1981   static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
  1799   1982     int ii;
  1800   1983     int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
  1801         -  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
         1984  +  for(ii=0; ii<pRtree->nDim2; ii+=2){
  1802   1985       RtreeCoord *a1 = &p1->aCoord[ii];
  1803   1986       RtreeCoord *a2 = &p2->aCoord[ii];
  1804   1987       if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
  1805   1988        || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
  1806   1989       ){
  1807   1990         return 0;
  1808   1991       }
................................................................................
  1829   2012     int nCell
  1830   2013   ){
  1831   2014     int ii;
  1832   2015     RtreeDValue overlap = RTREE_ZERO;
  1833   2016     for(ii=0; ii<nCell; ii++){
  1834   2017       int jj;
  1835   2018       RtreeDValue o = (RtreeDValue)1;
  1836         -    for(jj=0; jj<(pRtree->nDim*2); jj+=2){
         2019  +    for(jj=0; jj<pRtree->nDim2; jj+=2){
  1837   2020         RtreeDValue x1, x2;
  1838   2021         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
  1839   2022         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
  1840   2023         if( x2<x1 ){
  1841   2024           o = (RtreeDValue)0;
  1842   2025           break;
  1843   2026         }else{
................................................................................
  2885   3068       **
  2886   3069       ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
  2887   3070       ** with "column" that are interpreted as table constraints.
  2888   3071       ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
  2889   3072       ** This problem was discovered after years of use, so we silently ignore
  2890   3073       ** these kinds of misdeclared tables to avoid breaking any legacy.
  2891   3074       */
  2892         -    assert( nData<=(pRtree->nDim*2 + 3) );
         3075  +    assert( nData<=(pRtree->nDim2 + 3) );
  2893   3076   
  2894   3077   #ifndef SQLITE_RTREE_INT_ONLY
  2895   3078       if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  2896   3079         for(ii=0; ii<nData-4; ii+=2){
  2897   3080           cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
  2898   3081           cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
  2899   3082           if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
................................................................................
  2974   3157       }
  2975   3158     }
  2976   3159   
  2977   3160   constraint:
  2978   3161     rtreeRelease(pRtree);
  2979   3162     return rc;
  2980   3163   }
         3164  +
         3165  +/*
         3166  +** Called when a transaction starts.
         3167  +*/
         3168  +static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
         3169  +  Rtree *pRtree = (Rtree *)pVtab;
         3170  +  assert( pRtree->inWrTrans==0 );
         3171  +  pRtree->inWrTrans++;
         3172  +  return SQLITE_OK;
         3173  +}
         3174  +
         3175  +/*
         3176  +** Called when a transaction completes (either by COMMIT or ROLLBACK).
         3177  +** The sqlite3_blob object should be released at this point.
         3178  +*/
         3179  +static int rtreeEndTransaction(sqlite3_vtab *pVtab){
         3180  +  Rtree *pRtree = (Rtree *)pVtab;
         3181  +  pRtree->inWrTrans = 0;
         3182  +  nodeBlobReset(pRtree);
         3183  +  return SQLITE_OK;
         3184  +}
  2981   3185   
  2982   3186   /*
  2983   3187   ** The xRename method for rtree module virtual tables.
  2984   3188   */
  2985   3189   static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
  2986   3190     Rtree *pRtree = (Rtree *)pVtab;
  2987   3191     int rc = SQLITE_NOMEM;
................................................................................
  2995   3199     );
  2996   3200     if( zSql ){
  2997   3201       rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
  2998   3202       sqlite3_free(zSql);
  2999   3203     }
  3000   3204     return rc;
  3001   3205   }
         3206  +
  3002   3207   
  3003   3208   /*
  3004   3209   ** This function populates the pRtree->nRowEst variable with an estimate
  3005   3210   ** of the number of rows in the virtual table. If possible, this is based
  3006   3211   ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
  3007   3212   */
  3008   3213   static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
................................................................................
  3055   3260     rtreeClose,                 /* xClose - close a cursor */
  3056   3261     rtreeFilter,                /* xFilter - configure scan constraints */
  3057   3262     rtreeNext,                  /* xNext - advance a cursor */
  3058   3263     rtreeEof,                   /* xEof */
  3059   3264     rtreeColumn,                /* xColumn - read data */
  3060   3265     rtreeRowid,                 /* xRowid - read data */
  3061   3266     rtreeUpdate,                /* xUpdate - write data */
  3062         -  0,                          /* xBegin - begin transaction */
  3063         -  0,                          /* xSync - sync transaction */
  3064         -  0,                          /* xCommit - commit transaction */
  3065         -  0,                          /* xRollback - rollback transaction */
         3267  +  rtreeBeginTransaction,      /* xBegin - begin transaction */
         3268  +  rtreeEndTransaction,        /* xSync - sync transaction */
         3269  +  rtreeEndTransaction,        /* xCommit - commit transaction */
         3270  +  rtreeEndTransaction,        /* xRollback - rollback transaction */
  3066   3271     0,                          /* xFindFunction - function overloading */
  3067   3272     rtreeRename,                /* xRename - rename the table */
  3068   3273     0,                          /* xSavepoint */
  3069   3274     0,                          /* xRelease */
  3070         -  0                           /* xRollbackTo */
         3275  +  0,                          /* xRollbackTo */
  3071   3276   };
  3072   3277   
  3073   3278   static int rtreeSqlInit(
  3074   3279     Rtree *pRtree, 
  3075   3280     sqlite3 *db, 
  3076   3281     const char *zDb, 
  3077   3282     const char *zPrefix, 
  3078   3283     int isCreate
  3079   3284   ){
  3080   3285     int rc = SQLITE_OK;
  3081   3286   
  3082         -  #define N_STATEMENT 9
         3287  +  #define N_STATEMENT 8
  3083   3288     static const char *azSql[N_STATEMENT] = {
  3084         -    /* Read and write the xxx_node table */
  3085         -    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
         3289  +    /* Write the xxx_node table */
  3086   3290       "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
  3087   3291       "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
  3088   3292   
  3089   3293       /* Read and write the xxx_rowid table */
  3090   3294       "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
  3091   3295       "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
  3092   3296       "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
................................................................................
  3116   3320       rc = sqlite3_exec(db, zCreate, 0, 0, 0);
  3117   3321       sqlite3_free(zCreate);
  3118   3322       if( rc!=SQLITE_OK ){
  3119   3323         return rc;
  3120   3324       }
  3121   3325     }
  3122   3326   
  3123         -  appStmt[0] = &pRtree->pReadNode;
  3124         -  appStmt[1] = &pRtree->pWriteNode;
  3125         -  appStmt[2] = &pRtree->pDeleteNode;
  3126         -  appStmt[3] = &pRtree->pReadRowid;
  3127         -  appStmt[4] = &pRtree->pWriteRowid;
  3128         -  appStmt[5] = &pRtree->pDeleteRowid;
  3129         -  appStmt[6] = &pRtree->pReadParent;
  3130         -  appStmt[7] = &pRtree->pWriteParent;
  3131         -  appStmt[8] = &pRtree->pDeleteParent;
         3327  +  appStmt[0] = &pRtree->pWriteNode;
         3328  +  appStmt[1] = &pRtree->pDeleteNode;
         3329  +  appStmt[2] = &pRtree->pReadRowid;
         3330  +  appStmt[3] = &pRtree->pWriteRowid;
         3331  +  appStmt[4] = &pRtree->pDeleteRowid;
         3332  +  appStmt[5] = &pRtree->pReadParent;
         3333  +  appStmt[6] = &pRtree->pWriteParent;
         3334  +  appStmt[7] = &pRtree->pDeleteParent;
  3132   3335   
  3133   3336     rc = rtreeQueryStat1(db, pRtree);
  3134   3337     for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
  3135   3338       char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
  3136   3339       if( zSql ){
  3137   3340         rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
  3138   3341       }else{
................................................................................
  3262   3465       return SQLITE_NOMEM;
  3263   3466     }
  3264   3467     memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
  3265   3468     pRtree->nBusy = 1;
  3266   3469     pRtree->base.pModule = &rtreeModule;
  3267   3470     pRtree->zDb = (char *)&pRtree[1];
  3268   3471     pRtree->zName = &pRtree->zDb[nDb+1];
  3269         -  pRtree->nDim = (argc-4)/2;
  3270         -  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
  3271         -  pRtree->eCoordType = eCoordType;
         3472  +  pRtree->nDim = (u8)((argc-4)/2);
         3473  +  pRtree->nDim2 = pRtree->nDim*2;
         3474  +  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
         3475  +  pRtree->eCoordType = (u8)eCoordType;
  3272   3476     memcpy(pRtree->zDb, argv[1], nDb);
  3273   3477     memcpy(pRtree->zName, argv[2], nName);
  3274   3478   
  3275   3479     /* Figure out the node size to use. */
  3276   3480     rc = getNodeSize(db, pRtree, isCreate, pzErr);
  3277   3481   
  3278   3482     /* Create/Connect to the underlying relational database schema. If
................................................................................
  3337   3541     RtreeNode node;
  3338   3542     Rtree tree;
  3339   3543     int ii;
  3340   3544   
  3341   3545     UNUSED_PARAMETER(nArg);
  3342   3546     memset(&node, 0, sizeof(RtreeNode));
  3343   3547     memset(&tree, 0, sizeof(Rtree));
  3344         -  tree.nDim = sqlite3_value_int(apArg[0]);
         3548  +  tree.nDim = (u8)sqlite3_value_int(apArg[0]);
         3549  +  tree.nDim2 = tree.nDim*2;
  3345   3550     tree.nBytesPerCell = 8 + 8 * tree.nDim;
  3346   3551     node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
  3347   3552   
  3348   3553     for(ii=0; ii<NCELL(&node); ii++){
  3349   3554       char zCell[512];
  3350   3555       int nCell = 0;
  3351   3556       RtreeCell cell;
  3352   3557       int jj;
  3353   3558   
  3354   3559       nodeGetCell(&tree, &node, ii, &cell);
  3355   3560       sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
  3356   3561       nCell = (int)strlen(zCell);
  3357         -    for(jj=0; jj<tree.nDim*2; jj++){
         3562  +    for(jj=0; jj<tree.nDim2; jj++){
  3358   3563   #ifndef SQLITE_RTREE_INT_ONLY
  3359   3564         sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
  3360   3565                          (double)cell.aCoord[jj].f);
  3361   3566   #else
  3362   3567         sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
  3363   3568                          cell.aCoord[jj].i);
  3364   3569   #endif

Changes to ext/rtree/rtreeA.test.

   105    105     1   "SELECT * FROM t1"
   106    106     2   "SELECT * FROM t1 WHERE rowid=5"
   107    107     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   108    108     4   "SELECT * FROM t1 WHERE x1<10 AND x2>12"
   109    109   }
   110    110   
   111    111   do_execsql_test  rtreeA-1.2.0 { DROP TABLE t1_node } {}
   112         -do_corruption_tests rtreeA-1.2 -error "SQL logic error or missing database" {
          112  +do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" {
   113    113     1   "SELECT * FROM t1"
   114    114     2   "SELECT * FROM t1 WHERE rowid=5"
   115    115     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   116    116     4   "SELECT * FROM t1 WHERE x1<10 AND x2>12"
   117    117   }
   118    118   
   119    119   #-------------------------------------------------------------------------

Changes to ext/session/session1.test.

    16     16   } 
    17     17   source [file join [file dirname [info script]] session_common.tcl]
    18     18   source $testdir/tester.tcl
    19     19   ifcapable !session {finish_test; return}
    20     20   
    21     21   set testprefix session1
    22     22   
    23         -do_execsql_test 1.0 {
    24         -  CREATE TABLE t1(x PRIMARY KEY, y);
           23  +# Run all tests in this file twice. Once with "WITHOUT ROWID", and once
           24  +# with regular rowid tables.
           25  +#
           26  +foreach {tn trailing} {
           27  +  1 ""
           28  +  2 " WITHOUT ROWID "
           29  +} {
           30  +eval [string map [list %WR% $trailing] {
           31  +
           32  +db close
           33  +forcedelete test.db test.db2
           34  +reset_db
           35  +
           36  +do_execsql_test $tn.1.0 {
           37  +  CREATE TABLE t1(x PRIMARY KEY, y) %WR%;
    25     38     INSERT INTO t1 VALUES('abc', 'def');
    26     39   }
    27     40   
    28     41   #-------------------------------------------------------------------------
    29     42   # Test creating, attaching tables to and deleting session objects.
    30     43   #
    31         -do_test 1.1 { sqlite3session S db main } {S}
    32         -do_test 1.2 { S delete } {}
    33         -do_test 1.3 { sqlite3session S db main } {S}
    34         -do_test 1.4 { S attach t1 } {}
    35         -do_test 1.5 { S delete } {}
    36         -do_test 1.6 { sqlite3session S db main } {S}
    37         -do_test 1.7 { S attach t1 ; S attach t2 ; S attach t3 } {}
    38         -do_test 1.8 { S attach t1 ; S attach t2 ; S attach t3 } {}
    39         -do_test 1.9 { S delete } {}
    40         -do_test 1.10 {
           44  +do_test $tn.1.1 { sqlite3session S db main } {S}
           45  +do_test $tn.1.2 { S delete } {}
           46  +do_test $tn.1.3 { sqlite3session S db main } {S}
           47  +do_test $tn.1.4 { S attach t1 } {}
           48  +do_test $tn.1.5 { S delete } {}
           49  +do_test $tn.1.6 { sqlite3session S db main } {S}
           50  +do_test $tn.1.7 { S attach t1 ; S attach t2 ; S attach t3 } {}
           51  +do_test $tn.1.8 { S attach t1 ; S attach t2 ; S attach t3 } {}
           52  +do_test $tn.1.9 { S delete } {}
           53  +do_test $tn.1.10 {
    41     54     sqlite3session S db main
    42     55     S attach t1
    43     56     execsql { INSERT INTO t1 VALUES('ghi', 'jkl') }
    44     57   } {}
    45         -do_test 1.11 { S delete } {}
    46         -do_test 1.12 {
    47         -  sqlite3session S db main
    48         -  S attach t1
    49         -  execsql { INSERT INTO t1 VALUES('mno', 'pqr') }
    50         -  execsql { UPDATE t1 SET x = 111 WHERE rowid = 1 }
    51         -  execsql { DELETE FROM t1 WHERE rowid = 2 }
    52         -} {}
    53         -do_test 1.13 {
    54         -  S changeset
    55         -  S delete
    56         -} {}
           58  +do_test $tn.1.11 { S delete } {}
           59  +if {$tn==1} {
           60  +  do_test $tn.1.12 {
           61  +    sqlite3session S db main
           62  +    S attach t1
           63  +    execsql { INSERT INTO t1 VALUES('mno', 'pqr') }
           64  +    execsql { UPDATE t1 SET x = 111 WHERE rowid = 1 }
           65  +    execsql { DELETE FROM t1 WHERE rowid = 2 }
           66  +  } {}
           67  +  do_test $tn.1.13 {
           68  +    S changeset
           69  +    S delete
           70  +  } {}
           71  +}
    57     72   
    58     73   #-------------------------------------------------------------------------
    59     74   # Simple changeset tests. Also test the sqlite3changeset_invert() 
    60     75   # function.
    61     76   #
    62         -do_test 2.1.1 {
           77  +do_test $tn.2.1.1 {
    63     78     execsql { DELETE FROM t1 }
    64     79     sqlite3session S db main
    65     80     S attach t1
    66     81     execsql { INSERT INTO t1 VALUES(1, 'Sukhothai') }
    67     82     execsql { INSERT INTO t1 VALUES(2, 'Ayutthaya') }
    68     83     execsql { INSERT INTO t1 VALUES(3, 'Thonburi') }
    69     84   } {}
    70         -do_changeset_test 2.1.2 S {
           85  +do_changeset_test $tn.2.1.2 S {
    71     86     {INSERT t1 0 X. {} {i 1 t Sukhothai}}
    72     87     {INSERT t1 0 X. {} {i 2 t Ayutthaya}}
    73     88     {INSERT t1 0 X. {} {i 3 t Thonburi}}
    74     89   }
    75         -do_changeset_invert_test 2.1.3 S {
           90  +do_changeset_invert_test $tn.2.1.3 S {
    76     91     {DELETE t1 0 X. {i 1 t Sukhothai} {}}
    77     92     {DELETE t1 0 X. {i 2 t Ayutthaya} {}}
    78     93     {DELETE t1 0 X. {i 3 t Thonburi} {}}
    79     94   }
    80         -do_test 2.1.4 { S delete } {}
           95  +do_test $tn.2.1.4 { S delete } {}
    81     96   
    82         -do_test 2.2.1 {
           97  +do_test $tn.2.2.1 {
    83     98     sqlite3session S db main
    84     99     S attach t1
    85    100     execsql { DELETE FROM t1 WHERE 1 }
    86    101   } {}
    87         -do_changeset_test 2.2.2 S {
          102  +do_changeset_test $tn.2.2.2 S {
    88    103     {DELETE t1 0 X. {i 1 t Sukhothai} {}}
    89    104     {DELETE t1 0 X. {i 2 t Ayutthaya} {}}
    90    105     {DELETE t1 0 X. {i 3 t Thonburi} {}}
    91    106   }
    92         -do_changeset_invert_test 2.2.3 S {
          107  +do_changeset_invert_test $tn.2.2.3 S {
    93    108     {INSERT t1 0 X. {} {i 1 t Sukhothai}}
    94    109     {INSERT t1 0 X. {} {i 2 t Ayutthaya}}
    95    110     {INSERT t1 0 X. {} {i 3 t Thonburi}}
    96    111   }
    97         -do_test 2.2.4 { S delete } {}
          112  +do_test $tn.2.2.4 { S delete } {}
    98    113   
    99         -do_test 2.3.1 {
          114  +do_test $tn.2.3.1 {
   100    115     execsql { DELETE FROM t1 }
   101    116     sqlite3session S db main
   102    117     execsql { INSERT INTO t1 VALUES(1, 'Sukhothai') }
   103    118     execsql { INSERT INTO t1 VALUES(2, 'Ayutthaya') }
   104    119     execsql { INSERT INTO t1 VALUES(3, 'Thonburi') }
   105    120     S attach t1
   106    121     execsql { 
   107    122       UPDATE t1 SET x = 10 WHERE x = 1;
   108    123       UPDATE t1 SET y = 'Surin' WHERE x = 2;
   109    124       UPDATE t1 SET x = 20, y = 'Thapae' WHERE x = 3;
   110    125     }
   111    126   } {}
   112    127   
   113         -do_changeset_test 2.3.2 S {
          128  +do_changeset_test $tn.2.3.2 S {
   114    129     {INSERT t1 0 X. {} {i 10 t Sukhothai}} 
   115    130     {DELETE t1 0 X. {i 1 t Sukhothai} {}} 
   116    131     {UPDATE t1 0 X. {i 2 t Ayutthaya} {{} {} t Surin}} 
   117    132     {DELETE t1 0 X. {i 3 t Thonburi} {}} 
   118    133     {INSERT t1 0 X. {} {i 20 t Thapae}} 
   119    134   }
   120    135   
   121         -do_changeset_invert_test 2.3.3 S {
          136  +do_changeset_invert_test $tn.2.3.3 S {
   122    137     {DELETE t1 0 X. {i 10 t Sukhothai} {}} 
   123    138     {INSERT t1 0 X. {} {i 1 t Sukhothai}} 
   124    139     {UPDATE t1 0 X. {i 2 t Surin} {{} {} t Ayutthaya}} 
   125    140     {INSERT t1 0 X. {} {i 3 t Thonburi}} 
   126    141     {DELETE t1 0 X. {i 20 t Thapae} {}}
   127    142   }
   128         -do_test 2.3.4 { S delete } {}
          143  +do_test $tn.2.3.4 { S delete } {}
   129    144   
   130         -do_test 2.4.1 {
          145  +do_test $tn.2.4.1 {
   131    146     sqlite3session S db main
   132    147     S attach t1
   133    148     execsql { INSERT INTO t1 VALUES(100, 'Bangkok') }
   134    149     execsql { DELETE FROM t1 WHERE x = 100 }
   135    150   } {}
   136         -do_changeset_test 2.4.2 S {}
   137         -do_changeset_invert_test 2.4.3 S {}
   138         -do_test 2.4.4 { S delete } {}
          151  +do_changeset_test $tn.2.4.2 S {}
          152  +do_changeset_invert_test $tn.2.4.3 S {}
          153  +do_test $tn.2.4.4 { S delete } {}
   139    154   
   140    155   #-------------------------------------------------------------------------
   141    156   # Test the application of simple changesets. These tests also test that
   142    157   # the conflict callback is invoked correctly. For these tests, the 
   143    158   # conflict callback always returns OMIT.
   144    159   #
   145    160   db close
................................................................................
   185    200   
   186    201   proc do_db2_test {testname sql {result {}}} {
   187    202     uplevel do_test $testname [list "execsql {$sql} db2"] [list [list {*}$result]]
   188    203   }
   189    204   
   190    205   # Test INSERT changesets.
   191    206   #
   192         -do_test 3.1.0 {
   193         -  execsql { CREATE TABLE t1(a PRIMARY KEY, b NOT NULL) } db2
          207  +do_test $tn.3.1.0 {
          208  +  execsql { CREATE TABLE t1(a PRIMARY KEY, b NOT NULL) %WR% } db2
   194    209     execsql { 
   195         -    CREATE TABLE t1(a PRIMARY KEY, b);
          210  +    CREATE TABLE t1(a PRIMARY KEY, b) %WR%;
   196    211       INSERT INTO t1 VALUES(1, 'one');
   197    212       INSERT INTO t1 VALUES(2, 'two');
   198    213     } db 
   199    214   } {}
   200         -do_db2_test 3.1.1 "INSERT INTO t1 VALUES(6, 'VI')"
   201         -do_conflict_test 3.1.2 -tables t1 -sql {
          215  +do_db2_test $tn.3.1.1 "INSERT INTO t1 VALUES(6, 'VI')"
          216  +do_conflict_test $tn.3.1.2 -tables t1 -sql {
   202    217     INSERT INTO t1 VALUES(3, 'three');
   203    218     INSERT INTO t1 VALUES(4, 'four');
   204    219     INSERT INTO t1 VALUES(5, 'five');
   205    220     INSERT INTO t1 VALUES(6, 'six');
   206    221     INSERT INTO t1 VALUES(7, 'seven');
   207    222     INSERT INTO t1 VALUES(8, NULL);
   208    223   } -conflicts {
   209    224     {INSERT t1 CONFLICT {i 6 t six} {i 6 t VI}}
   210    225     {INSERT t1 CONSTRAINT {i 8 n {}}}
   211    226   }
   212    227   
   213         -do_db2_test 3.1.3 "SELECT * FROM t1" {
   214         -  6 VI 3 three 4 four 5 five 7 seven
          228  +do_db2_test $tn.3.1.3 "SELECT * FROM t1 ORDER BY a" {
          229  +  3 three 4 four 5 five 6 VI 7 seven
   215    230   }
   216         -do_execsql_test 3.1.4 "SELECT * FROM t1" {
          231  +do_execsql_test $tn.3.1.4 "SELECT * FROM t1" {
   217    232     1 one 2 two 3 three 4 four 5 five 6 six 7 seven 8 {}
   218    233   }
   219    234   
   220    235   # Test DELETE changesets.
   221    236   #
   222         -do_execsql_test 3.2.1 {
          237  +do_execsql_test $tn.3.2.1 {
   223    238     PRAGMA foreign_keys = on;
   224         -  CREATE TABLE t2(a PRIMARY KEY, b);
          239  +  CREATE TABLE t2(a PRIMARY KEY, b)%WR%;
   225    240     CREATE TABLE t3(c, d REFERENCES t2);
   226    241     INSERT INTO t2 VALUES(1, 'one');
   227    242     INSERT INTO t2 VALUES(2, 'two');
   228    243     INSERT INTO t2 VALUES(3, 'three');
   229    244     INSERT INTO t2 VALUES(4, 'four');
   230    245   }
   231         -do_db2_test 3.2.2 {
          246  +do_db2_test $tn.3.2.2 {
   232    247     PRAGMA foreign_keys = on;
   233         -  CREATE TABLE t2(a PRIMARY KEY, b);
          248  +  CREATE TABLE t2(a PRIMARY KEY, b)%WR%;
   234    249     CREATE TABLE t3(c, d REFERENCES t2);
   235    250     INSERT INTO t2 VALUES(1, 'one');
   236    251     INSERT INTO t2 VALUES(2, 'two');
   237    252     INSERT INTO t2 VALUES(4, 'five');
   238    253     INSERT INTO t3 VALUES('i', 1);
   239    254   }
   240         -do_conflict_test 3.2.3 -tables t2 -sql {
          255  +do_conflict_test $tn.3.2.3 -tables t2 -sql {
   241    256     DELETE FROM t2 WHERE a = 1;
   242    257     DELETE FROM t2 WHERE a = 2;
   243    258     DELETE FROM t2 WHERE a = 3;
   244    259     DELETE FROM t2 WHERE a = 4;
   245    260   } -conflicts {
   246    261     {DELETE t2 NOTFOUND {i 3 t three}}
   247    262     {DELETE t2 DATA {i 4 t four} {i 4 t five}}
   248    263     {FOREIGN_KEY 1}
   249    264   }
   250         -do_execsql_test 3.2.4 "SELECT * FROM t2" {}
   251         -do_db2_test     3.2.5 "SELECT * FROM t2" {4 five}
          265  +do_execsql_test $tn.3.2.4 "SELECT * FROM t2" {}
          266  +do_db2_test $tn.3.2.5 "SELECT * FROM t2" {4 five}
   252    267   
   253    268   # Test UPDATE changesets.
   254    269   #
   255         -do_execsql_test 3.3.1 {
   256         -  CREATE TABLE t4(a, b, c, PRIMARY KEY(b, c));
          270  +do_execsql_test $tn.3.3.1 {
          271  +  CREATE TABLE t4(a, b, c, PRIMARY KEY(b, c))%WR%;
   257    272     INSERT INTO t4 VALUES(1, 2, 3);
   258    273     INSERT INTO t4 VALUES(4, 5, 6);
   259    274     INSERT INTO t4 VALUES(7, 8, 9);
   260    275     INSERT INTO t4 VALUES(10, 11, 12);
   261    276   }
   262         -do_db2_test 3.3.2 {
   263         -  CREATE TABLE t4(a NOT NULL, b, c, PRIMARY KEY(b, c));
          277  +do_db2_test $tn.3.3.2 {
          278  +  CREATE TABLE t4(a NOT NULL, b, c, PRIMARY KEY(b, c))%WR%;
   264    279     INSERT INTO t4 VALUES(0, 2, 3);
   265    280     INSERT INTO t4 VALUES(4, 5, 7);
   266    281     INSERT INTO t4 VALUES(7, 8, 9);
   267    282     INSERT INTO t4 VALUES(10, 11, 12);
   268    283   }
   269         -do_conflict_test 3.3.3 -tables t4 -sql {
          284  +do_conflict_test $tn.3.3.3 -tables t4 -sql {
   270    285     UPDATE t4 SET a = -1 WHERE b = 2;
   271    286     UPDATE t4 SET a = -1 WHERE b = 5;
   272    287     UPDATE t4 SET a = NULL WHERE c = 9;
   273    288     UPDATE t4 SET a = 'x' WHERE b = 11;
   274    289   } -conflicts {
   275    290     {UPDATE t4 DATA {i 1 i 2 i 3} {i -1 {} {} {} {}} {i 0 i 2 i 3}}
   276    291     {UPDATE t4 NOTFOUND {i 4 i 5 i 6} {i -1 {} {} {} {}}}
   277    292     {UPDATE t4 CONSTRAINT {i 7 i 8 i 9} {n {} {} {} {} {}}}
   278    293   }
   279         -do_db2_test     3.3.4 { SELECT * FROM t4 } {0 2 3 4 5 7 7 8 9 x 11 12}
   280         -do_execsql_test 3.3.5 { SELECT * FROM t4 } {-1 2 3 -1 5 6 {} 8 9 x 11 12}
          294  +do_db2_test $tn.3.3.4 { SELECT * FROM t4 } {0 2 3 4 5 7 7 8 9 x 11 12}
          295  +do_execsql_test $tn.3.3.5 { SELECT * FROM t4 } {-1 2 3 -1 5 6 {} 8 9 x 11 12}
   281    296   
   282    297   #-------------------------------------------------------------------------
   283    298   # This next block of tests verifies that values returned by the conflict
   284    299   # handler are intepreted correctly.
   285    300   #
   286    301   
   287    302   proc test_reset {} {
................................................................................
   293    308   }
   294    309   
   295    310   proc xConflict {args} {
   296    311     lappend ::xConflict $args
   297    312     return $::conflict_return
   298    313   }
   299    314   
   300         -foreach {tn conflict_return after} {
          315  +foreach {tn2 conflict_return after} {
   301    316     1 OMIT      {1 2 value1   4 5 7       10 x x}
   302    317     2 REPLACE   {1 2 value1   4 5 value2  10 8 9}
   303    318   } {
   304    319     test_reset
   305    320   
   306         -  do_test 4.$tn.1 {
          321  +  do_test $tn.4.$tn2.1 {
   307    322       foreach db {db db2} {
   308    323         execsql { 
   309         -        CREATE TABLE t1(a, b, c, PRIMARY KEY(a));
          324  +        CREATE TABLE t1(a, b, c, PRIMARY KEY(a))%WR%;
   310    325           INSERT INTO t1 VALUES(1, 2, 3);
   311    326           INSERT INTO t1 VALUES(4, 5, 6);
   312    327           INSERT INTO t1 VALUES(7, 8, 9);
   313    328         } $db
   314    329       }
   315    330       execsql { 
   316    331         REPLACE INTO t1 VALUES(4, 5, 7);
   317    332         REPLACE INTO t1 VALUES(10, 'x', 'x');
   318    333       } db2
   319    334     } {}
   320    335   
   321         -  do_conflict_test 4.$tn.2 -tables t1 -sql {
          336  +  do_conflict_test $tn.4.$tn2.2 -tables t1 -sql {
   322    337       UPDATE t1 SET c = 'value1' WHERE a = 1;       -- no conflict
   323    338       UPDATE t1 SET c = 'value2' WHERE a = 4;       -- DATA conflict
   324    339       UPDATE t1 SET a = 10 WHERE a = 7;             -- CONFLICT conflict
   325    340     } -conflicts {
   326    341       {INSERT t1 CONFLICT {i 10 i 8 i 9} {i 10 t x t x}}
   327    342       {UPDATE t1 DATA {i 4 {} {} i 6} {{} {} {} {} t value2} {i 4 i 5 i 7}}
   328    343     }
   329    344   
   330         -  do_db2_test 4.$tn.3 "SELECT * FROM t1 ORDER BY a" $after
          345  +  do_db2_test $tn.4.$tn2.3 "SELECT * FROM t1 ORDER BY a" $after
   331    346   }
   332    347   
   333         -foreach {tn conflict_return} {
          348  +foreach {tn2 conflict_return} {
   334    349     1 OMIT
   335    350     2 REPLACE
   336    351   } {
   337    352     test_reset
   338    353   
   339         -  do_test 5.$tn.1 {
          354  +  do_test $tn.5.$tn2.1 {
   340    355       # Create an identical schema in both databases.
   341    356       set schema {
   342         -      CREATE TABLE "'foolish name'"(x, y, z, PRIMARY KEY(x, y));
          357  +      CREATE TABLE "'foolish name'"(x, y, z, PRIMARY KEY(x, y))%WR%;
   343    358       }
   344    359       execsql $schema db
   345    360       execsql $schema db2
   346    361   
   347    362       # Add some rows to [db2]. These rows will cause conflicts later
   348    363       # on when the changeset from [db] is applied to it.
   349    364       execsql { 
................................................................................
   350    365         INSERT INTO "'foolish name'" VALUES('one', 'one', 'ii');
   351    366         INSERT INTO "'foolish name'" VALUES('one', 'two', 'i');
   352    367         INSERT INTO "'foolish name'" VALUES('two', 'two', 'ii');
   353    368       } db2
   354    369   
   355    370     } {}
   356    371   
   357         -  do_conflict_test 5.$tn.2 -tables {{'foolish name'}} -sql {
          372  +  do_conflict_test $tn.5.$tn2.2 -tables {{'foolish name'}} -sql {
   358    373       INSERT INTO "'foolish name'" VALUES('one', 'two', 2);
   359    374     } -conflicts {
   360    375       {INSERT {'foolish name'} CONFLICT {t one t two i 2} {t one t two t i}}
   361    376     }
   362    377   
   363    378     set res(REPLACE) {one one ii one two 2 two two ii}
   364    379     set res(OMIT)    {one one ii one two i two two ii}
   365         -  do_db2_test 5.$tn.3 {
          380  +  do_db2_test $tn.5.$tn2.3 {
   366    381       SELECT * FROM "'foolish name'" ORDER BY x, y
   367    382     } $res($conflict_return)
   368    383   
   369    384   
   370         -  do_test 5.$tn.1 {
          385  +  do_test $tn.5.$tn2.1 {
   371    386       set schema {
   372         -      CREATE TABLE d1("z""z" PRIMARY KEY, y);
          387  +      CREATE TABLE d1("z""z" PRIMARY KEY, y)%WR%;
   373    388         INSERT INTO d1 VALUES(1, 'one');
   374    389         INSERT INTO d1 VALUES(2, 'two');
   375    390       }
   376    391       execsql $schema db
   377    392       execsql $schema db2
   378    393   
   379    394       execsql { 
   380    395         UPDATE d1 SET y = 'TWO' WHERE "z""z" = 2;
   381    396       } db2
   382    397   
   383    398     } {}
   384    399   
   385         -  do_conflict_test 5.$tn.2 -tables d1 -sql {
          400  +  do_conflict_test $tn.5.$tn2.2 -tables d1 -sql {
   386    401       DELETE FROM d1 WHERE "z""z" = 2;
   387    402     } -conflicts {
   388    403       {DELETE d1 DATA {i 2 t two} {i 2 t TWO}}
   389    404     }
   390    405   
   391    406     set res(REPLACE) {1 one}
   392    407     set res(OMIT)    {1 one 2 TWO}
   393         -  do_db2_test 5.$tn.3 "SELECT * FROM d1" $res($conflict_return)
          408  +  do_db2_test $tn.5.$tn2.3 "SELECT * FROM d1" $res($conflict_return)
   394    409   }
   395    410   
   396    411   #-------------------------------------------------------------------------
   397    412   # Test that two tables can be monitored by a single session object.
   398    413   #
   399    414   test_reset
   400    415   set schema {
   401         -  CREATE TABLE t1(a COLLATE nocase PRIMARY KEY, b);
   402         -  CREATE TABLE t2(a, b PRIMARY KEY);
          416  +  CREATE TABLE t1(a COLLATE nocase PRIMARY KEY, b)%WR%;
          417  +  CREATE TABLE t2(a, b PRIMARY KEY)%WR%;
   403    418   }
   404         -do_test 6.0 {
          419  +do_test $tn.6.0 {
   405    420     execsql $schema db
   406    421     execsql $schema db2
   407    422     execsql {
   408    423       INSERT INTO t1 VALUES('a', 'b');
   409    424       INSERT INTO t2 VALUES('a', 'b');
   410    425     } db2
   411    426   } {}
   412    427   
   413    428   set conflict_return ""
   414         -do_conflict_test 6.1 -tables {t1 t2} -sql {
          429  +do_conflict_test $tn.6.1 -tables {t1 t2} -sql {
   415    430     INSERT INTO t1 VALUES('1', '2');
   416    431     INSERT INTO t1 VALUES('A', 'B');
   417    432     INSERT INTO t2 VALUES('A', 'B');
   418    433   } -conflicts {
   419    434     {INSERT t1 CONFLICT {t A t B} {t a t b}}
   420    435   }
   421    436   
   422         -do_db2_test 6.2 "SELECT * FROM t1" {a b 1 2}
   423         -do_db2_test 6.3 "SELECT * FROM t2" {a b A B}
          437  +do_db2_test $tn.6.2 "SELECT * FROM t1 ORDER BY a" {1 2 a b}
          438  +do_db2_test $tn.6.3 "SELECT * FROM t2 ORDER BY a" {A B a b}
   424    439   
   425    440   #-------------------------------------------------------------------------
   426    441   # Test that session objects are not confused by changes to table in
   427    442   # other databases.
   428    443   #
   429    444   catch { db2 close }
   430    445   drop_all_tables
   431    446   forcedelete test.db2
   432         -do_iterator_test 7.1 * {
          447  +do_iterator_test $tn.7.1 * {
   433    448     ATTACH 'test.db2' AS aux;
   434         -  CREATE TABLE main.t1(x PRIMARY KEY, y);
   435         -  CREATE TABLE aux.t1(x PRIMARY KEY, y);
          449  +  CREATE TABLE main.t1(x PRIMARY KEY, y)%WR%;
          450  +  CREATE TABLE aux.t1(x PRIMARY KEY, y)%WR%;
   436    451   
   437    452     INSERT INTO main.t1 VALUES('one', 1);
   438    453     INSERT INTO main.t1 VALUES('two', 2);
   439    454     INSERT INTO aux.t1 VALUES('three', 3);
   440    455     INSERT INTO aux.t1 VALUES('four', 4);
   441    456   } {
   442    457     {INSERT t1 0 X. {} {t two i 2}} 
   443    458     {INSERT t1 0 X. {} {t one i 1}}
   444    459   }
   445    460   
   446    461   #-------------------------------------------------------------------------
   447    462   # Test the sqlite3session_isempty() function.
   448    463   #
   449         -do_test 8.1 {
          464  +do_test $tn.8.1 {
   450    465     execsql {
   451         -    CREATE TABLE t5(x PRIMARY KEY, y);
   452         -    CREATE TABLE t6(x PRIMARY KEY, y);
          466  +    CREATE TABLE t5(x PRIMARY KEY, y)%WR%;
          467  +    CREATE TABLE t6(x PRIMARY KEY, y)%WR%;
   453    468       INSERT INTO t5 VALUES('a', 'b');
   454    469       INSERT INTO t6 VALUES('a', 'b');
   455    470     }
   456    471     sqlite3session S db main
   457    472     S attach *
   458    473   
   459    474     S isempty
   460    475   } {1}
   461         -do_test 8.2 {
          476  +do_test $tn.8.2 {
   462    477     execsql { DELETE FROM t5 }
   463    478     S isempty
   464    479   } {0}
   465         -do_test 8.3 {
          480  +do_test $tn.8.3 {
   466    481     S delete
   467    482     sqlite3session S db main
   468    483     S attach t5
   469    484     execsql { DELETE FROM t5 }
   470    485     S isempty
   471    486   } {1}
   472         -do_test 8.4 { S delete } {}
          487  +do_test $tn.8.4 { S delete } {}
   473    488   
   474         -do_test 8.5 {
          489  +do_test $tn.8.5 {
   475    490     sqlite3session S db main
   476    491     S attach t5
   477    492     S attach t6
   478    493     execsql { INSERT INTO t5 VALUES(1, 2) }
   479    494     S isempty
   480    495   } {0}
   481    496   
   482         -do_test 8.6 {
          497  +do_test $tn.8.6 {
   483    498     S delete
   484    499     sqlite3session S db main
   485    500     S attach t5
   486    501     S attach t6
   487    502     execsql { INSERT INTO t6 VALUES(1, 2) }
   488    503     S isempty
   489    504   } {0}
   490         -do_test 8.7 { S delete } {}
          505  +do_test $tn.8.7 { S delete } {}
   491    506   
   492    507   #-------------------------------------------------------------------------
   493    508   #
   494         -do_execsql_test 9.1 {
   495         -  CREATE TABLE t7(a, b, c, d, e PRIMARY KEY, f, g);
          509  +do_execsql_test $tn.9.1 {
          510  +  CREATE TABLE t7(a, b, c, d, e PRIMARY KEY, f, g)%WR%;
   496    511     INSERT INTO t7 VALUES(1, 1, 1, 1, 1, 1, 1);
   497    512   }
   498         -do_test 9.2 { 
          513  +do_test $tn.9.2 { 
   499    514     sqlite3session S db main 
   500    515     S attach *
   501    516     execsql { UPDATE t7 SET b=2, d=2 }
   502    517   } {}
   503         -do_changeset_test 9.2 S {{UPDATE t7 0 ....X.. {{} {} i 1 {} {} i 1 i 1 {} {} {} {}} {{} {} i 2 {} {} i 2 {} {} {} {} {} {}}}}
          518  +do_changeset_test $tn.9.2 S {{UPDATE t7 0 ....X.. {{} {} i 1 {} {} i 1 i 1 {} {} {} {}} {{} {} i 2 {} {} i 2 {} {} {} {} {} {}}}}
   504    519   S delete
   505    520   catch { db2 close }
   506    521    
   507    522   #-------------------------------------------------------------------------
   508    523   # Test a really long table name.
   509    524   #
   510    525   reset_db
   511    526   set tblname [string repeat tblname123 100]
   512         -do_test 10.1.1 {
          527  +do_test $tn.10.1.1 {
   513    528     execsql "
   514         -    CREATE TABLE $tblname (a PRIMARY KEY, b);
          529  +    CREATE TABLE $tblname (a PRIMARY KEY, b)%WR%;
   515    530       INSERT INTO $tblname VALUES('xyz', 'def');
   516    531     "
   517    532     sqlite3session S db main
   518    533     S attach $tblname
   519    534     execsql " 
   520    535       INSERT INTO $tblname VALUES('uvw', 'abc');
   521    536       DELETE FROM $tblname WHERE a = 'xyz';
   522    537     "
   523    538   } {}
   524    539   breakpoint
   525         -do_changeset_test 10.1.2 S "
          540  +do_changeset_test $tn.10.1.2 S "
   526    541     {INSERT $tblname 0 X. {} {t uvw t abc}}
   527    542     {DELETE $tblname 0 X. {t xyz t def} {}}
   528    543   "
   529         -do_test 10.1.4 { S delete } {}
          544  +do_test $tn.10.1.4 { S delete } {}
   530    545   
   531    546   #---------------------------------------------------------------
   532    547   reset_db
   533         -do_execsql_test 11.1 {
          548  +do_execsql_test $tn.11.1 {
   534    549     CREATE TABLE t1(a, b);
   535    550   }
   536         -do_test 11.2 {
          551  +do_test $tn.11.2 {
   537    552     sqlite3session S db main
   538    553     S attach t1
   539    554     execsql {
   540    555       INSERT INTO t1 VALUES(1, 2);
   541    556     }
   542    557     S changeset
   543    558   } {}
................................................................................
   546    561   
   547    562   
   548    563   #-------------------------------------------------------------------------
   549    564   # Test a really long table name.
   550    565   #
   551    566   reset_db
   552    567   set tblname [string repeat tblname123 100]
   553         -do_test 10.1.1 {
          568  +do_test $tn.10.1.1 {
   554    569     execsql "
   555         -    CREATE TABLE $tblname (a PRIMARY KEY, b);
          570  +    CREATE TABLE $tblname (a PRIMARY KEY, b)%WR%;
   556    571       INSERT INTO $tblname VALUES('xyz', 'def');
   557    572     "
   558    573     sqlite3session S db main
   559    574     S attach $tblname
   560    575     execsql " 
   561    576       INSERT INTO $tblname VALUES('uvw', 'abc');
   562    577       DELETE FROM $tblname WHERE a = 'xyz';
   563    578     "
   564    579   } {}
   565    580   breakpoint
   566         -do_changeset_test 10.1.2 S "
          581  +do_changeset_test $tn.10.1.2 S "
   567    582     {INSERT $tblname 0 X. {} {t uvw t abc}}
   568    583     {DELETE $tblname 0 X. {t xyz t def} {}}
   569    584   "
   570         -do_test 10.1.4 { S delete } {}
          585  +do_test $tn.10.1.4 { S delete } {}
   571    586   
   572    587   #-------------------------------------------------------------------------
   573    588   # Test the effect of updating a column from 0.0 to 0.0.
   574    589   #
   575    590   reset_db
   576         -do_execsql_test 11.1 {
   577         -  CREATE TABLE t1(a INTEGER PRIMARY KEY, b REAL);
          591  +do_execsql_test $tn.11.1 {
          592  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b REAL)%WR%;
   578    593     INSERT INTO t1 VALUES(1, 0.0);
   579    594   }
   580         -do_iterator_test 11.2 * {
          595  +do_iterator_test $tn.11.2 * {
   581    596     UPDATE t1 SET b = 0.0;
   582    597   } {
   583    598   }
          599  +
          600  +reset_db
          601  +do_execsql_test $tn.12.1 {
          602  +  CREATE TABLE t1(r INTEGER PRIMARY KEY, a, b)%WR%;
          603  +  CREATE INDEX i1 ON t1(a);
          604  +  INSERT INTO t1 VALUES(1, 1, 1);
          605  +  INSERT INTO t1 VALUES(2, 1, 2);
          606  +  INSERT INTO t1 VALUES(3, 1, 3);
          607  +}
          608  +
          609  +do_iterator_test $tn.12.2 * {
          610  +  UPDATE t1 SET b='one' WHERE a=1;
          611  +} {
          612  +  {UPDATE t1 0 X.. {i 1 {} {} i 1} {{} {} {} {} t one}}
          613  +  {UPDATE t1 0 X.. {i 2 {} {} i 2} {{} {} {} {} t one}}
          614  +  {UPDATE t1 0 X.. {i 3 {} {} i 3} {{} {} {} {} t one}}
          615  +}
          616  +
          617  +}]
          618  +}
          619  +
   584    620   
   585    621   finish_test

Changes to ext/session/session3.test.

    59     59   do_test 1.2.1 {
    60     60     set ::log {}
    61     61     do_then_apply_sql {
    62     62       INSERT INTO t1 VALUES(5, 6);
    63     63       INSERT INTO t1 VALUES(7, 8);
    64     64     }
    65     65     set ::log
    66         -} {SQLITE_SCHEMA {sqlite3changeset_apply(): table t1 has 3 columns, expected 2}}
           66  +} {}
           67  +do_test 1.2.2 {
           68  +  db2 eval { SELECT * FROM t1 }
           69  +} {5 6 {} 7 8 {}}
    67     70   
    68     71   do_test 1.3.0 {
    69     72     execsql { 
    70     73       DROP TABLE t1;
    71     74       CREATE TABLE t1(a, b PRIMARY KEY);
    72     75     } db2
    73     76   } {}

Changes to ext/session/session_common.tcl.

    29     29       sqlite3session_foreach c [set changeset] { lappend x [set c] }
    30     30       set x
    31     31     }]] [list $r]
    32     32   }
    33     33   
    34     34   
    35     35   proc do_conflict_test {tn args} {
    36         -  proc xConflict {args} { 
    37         -    lappend ::xConflict $args
    38         -    return "" 
    39         -  }
    40         -  proc bgerror {args} { set ::background_error $args }
    41         -
    42     36   
    43     37     set O(-tables)    [list]
    44     38     set O(-sql)       [list]
    45     39     set O(-conflicts) [list]
           40  +  set O(-policy)    "OMIT"
    46     41   
    47     42     array set V $args
    48     43     foreach key [array names V] {
    49     44       if {![info exists O($key)]} {error "no such option: $key"}
    50     45     }
    51     46     array set O $args
    52     47   
           48  +  proc xConflict {args} [subst -nocommands { 
           49  +    lappend ::xConflict [set args]
           50  +    return $O(-policy) 
           51  +  }]
           52  +  proc bgerror {args} { set ::background_error $args }
           53  +
    53     54     sqlite3session S db main
    54     55     foreach t $O(-tables) { S attach $t }
    55     56     execsql $O(-sql)
    56     57   
    57     58     set ::xConflict [list]
    58     59     sqlite3changeset_apply db2 [S changeset] xConflict
    59     60   

Added ext/session/session_speed_test.c.

            1  +/*
            2  +** 2017 January 31
            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  +** This file contains the source code for a standalone program used to
           13  +** test the performance of the sessions module. Compile and run:
           14  +**
           15  +**   ./session_speed_test -help
           16  +**
           17  +** for details.
           18  +*/
           19  +
           20  +#include "sqlite3.h"
           21  +#include <stdio.h>
           22  +#include <stdlib.h>
           23  +#include <string.h>
           24  +#include <stddef.h>
           25  +#include <unistd.h>
           26  +
           27  +/*************************************************************************
           28  +** Start of generic command line parser.
           29  +*/
           30  +#define CMDLINE_BARE       0
           31  +#define CMDLINE_INTEGER    1
           32  +#define CMDLINE_STRING     2
           33  +#define CMDLINE_BOOLEAN    3
           34  +
           35  +typedef struct CmdLineOption CmdLineOption;
           36  +struct CmdLineOption {
           37  +  const char *zText;              /* Name of command line option */
           38  +  const char *zHelp;              /* Help text for option */
           39  +  int eType;                      /* One of the CMDLINE_* values */
           40  +  int iOff;                       /* Offset of output variable */
           41  +};
           42  +
           43  +#define CMDLINE_INT32(x,y,z) {x, y, CMDLINE_INTEGER, z}
           44  +#define CMDLINE_BOOL(x,y,z)  {x, y, CMDLINE_BOOLEAN, z}
           45  +#define CMDLINE_TEXT(x,y,z)  {x, y, CMDLINE_STRING, z}
           46  +#define CMDLINE_NONE(x,y,z)  {x, y, CMDLINE_BARE, z}
           47  +
           48  +static void option_requires_argument_error(CmdLineOption *pOpt){
           49  +  fprintf(stderr, "Option requires a%s argument: %s\n", 
           50  +      pOpt->eType==CMDLINE_INTEGER ? "n integer" :
           51  +      pOpt->eType==CMDLINE_STRING ? " string" : " boolean",
           52  +      pOpt->zText
           53  +  );
           54  +  exit(1);
           55  +}
           56  +
           57  +static void ambiguous_option_error(const char *zArg){
           58  +  fprintf(stderr, "Option is ambiguous: %s\n", zArg);
           59  +  exit(1);
           60  +}
           61  +
           62  +static void unknown_option_error(
           63  +  const char *zArg, 
           64  +  CmdLineOption *aOpt,
           65  +  const char *zHelp
           66  +){
           67  +  int i;
           68  +  fprintf(stderr, "Unknown option: %s\n", zArg);
           69  +  fprintf(stderr, "\nOptions are:\n");
           70  +  fprintf(stderr, "  % -30sEcho command line options\n", "-cmdline:verbose");
           71  +  for(i=0; aOpt[i].zText; i++){
           72  +    int eType = aOpt[i].eType;
           73  +    char *zOpt = sqlite3_mprintf("%s %s", aOpt[i].zText,
           74  +        eType==CMDLINE_BARE ? "" :
           75  +        eType==CMDLINE_INTEGER ? "N" :
           76  +        eType==CMDLINE_BOOLEAN ? "BOOLEAN" : "TEXT"
           77  +    );
           78  +    fprintf(stderr, "  % -30s%s\n", zOpt, aOpt[i].zHelp);
           79  +    sqlite3_free(zOpt);
           80  +  }
           81  +  if( zHelp ){
           82  +    fprintf(stderr, "\n%s\n", zHelp);
           83  +  }
           84  +  exit(1);
           85  +}
           86  +
           87  +static int get_integer_option(CmdLineOption *pOpt, const char *zArg){
           88  +  int i = 0;
           89  +  int iRet = 0;
           90  +  int bSign = 1;
           91  +  if( zArg[0]=='-' ){
           92  +    bSign = -1;
           93  +    i = 1;
           94  +  }
           95  +  while( zArg[i] ){
           96  +    if( zArg[i]<'0' || zArg[i]>'9' ) option_requires_argument_error(pOpt);
           97  +    iRet = iRet*10 + (zArg[i] - '0');
           98  +    i++;
           99  +  }
          100  +  return (iRet*bSign);
          101  +}
          102  +
          103  +static int get_boolean_option(CmdLineOption *pOpt, const char *zArg){
          104  +  if( 0==sqlite3_stricmp(zArg, "true") ) return 1;
          105  +  if( 0==sqlite3_stricmp(zArg, "1") ) return 1;
          106  +  if( 0==sqlite3_stricmp(zArg, "0") ) return 0;
          107  +  if( 0==sqlite3_stricmp(zArg, "false") ) return 0;
          108  +  option_requires_argument_error(pOpt);
          109  +  return 0;
          110  +}
          111  +
          112  +static void parse_command_line(
          113  +  int argc, 
          114  +  char **argv, 
          115  +  int iStart,
          116  +  CmdLineOption *aOpt,
          117  +  void *pStruct,
          118  +  const char *zHelp
          119  +){
          120  +  char *pOut = (char*)pStruct;
          121  +  int bVerbose = 0;
          122  +  int iArg;
          123  +
          124  +  for(iArg=iStart; iArg<argc; iArg++){
          125  +    const char *zArg = argv[iArg];
          126  +    int nArg = strlen(zArg);
          127  +    int nMatch = 0;
          128  +    int iOpt;
          129  +
          130  +    for(iOpt=0; aOpt[iOpt].zText; iOpt++){
          131  +      CmdLineOption *pOpt = &aOpt[iOpt];
          132  +      if( 0==sqlite3_strnicmp(pOpt->zText, zArg, nArg) ){
          133  +        if( nMatch ){
          134  +          ambiguous_option_error(zArg);
          135  +        }
          136  +        nMatch++;
          137  +        if( pOpt->eType==CMDLINE_BARE ){
          138  +          *(int*)(&pOut[pOpt->iOff]) = 1;
          139  +        }else{
          140  +          iArg++;
          141  +          if( iArg==argc ){
          142  +            option_requires_argument_error(pOpt);
          143  +          }
          144  +          switch( pOpt->eType ){
          145  +            case CMDLINE_INTEGER:
          146  +              *(int*)(&pOut[pOpt->iOff]) = get_integer_option(pOpt, argv[iArg]);
          147  +              break;
          148  +            case CMDLINE_STRING:
          149  +              *(const char**)(&pOut[pOpt->iOff]) = argv[iArg];
          150  +              break;
          151  +            case CMDLINE_BOOLEAN:
          152  +              *(int*)(&pOut[pOpt->iOff]) = get_boolean_option(pOpt, argv[iArg]);
          153  +              break;
          154  +          }
          155  +        }
          156  +      }
          157  +    }
          158  +
          159  +    if( nMatch==0 && 0==sqlite3_strnicmp("-cmdline:verbose", zArg, nArg) ){
          160  +      bVerbose = 1;
          161  +      nMatch = 1;
          162  +    }
          163  +
          164  +    if( nMatch==0 ){
          165  +      unknown_option_error(zArg, aOpt, zHelp);
          166  +    }
          167  +  }
          168  +
          169  +  if( bVerbose ){
          170  +    int iOpt;
          171  +    fprintf(stdout, "Options are: ");
          172  +    for(iOpt=0; aOpt[iOpt].zText; iOpt++){
          173  +      CmdLineOption *pOpt = &aOpt[iOpt];
          174  +      if( pOpt->eType!=CMDLINE_BARE || *(int*)(&pOut[pOpt->iOff]) ){
          175  +        fprintf(stdout, "%s ", pOpt->zText);
          176  +      }
          177  +      switch( pOpt->eType ){
          178  +        case CMDLINE_INTEGER:
          179  +          fprintf(stdout, "%d ", *(int*)(&pOut[pOpt->iOff]));
          180  +          break;
          181  +        case CMDLINE_BOOLEAN:
          182  +          fprintf(stdout, "%d ", *(int*)(&pOut[pOpt->iOff]));
          183  +          break;
          184  +        case CMDLINE_STRING:
          185  +          fprintf(stdout, "%s ", *(const char**)(&pOut[pOpt->iOff]));
          186  +          break;
          187  +      }
          188  +    }
          189  +    fprintf(stdout, "\n");
          190  +  }
          191  +}
          192  +/* 
          193  +** End of generic command line parser.
          194  +*************************************************************************/
          195  +
          196  +static void abort_due_to_error(int rc){
          197  +  fprintf(stderr, "Error: %d\n");
          198  +  exit(-1);
          199  +}
          200  +
          201  +static void execsql(sqlite3 *db, const char *zSql){
          202  +  int rc = sqlite3_exec(db, zSql, 0, 0, 0);
          203  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          204  +}
          205  +
          206  +static int xConflict(void *pCtx, int eConflict, sqlite3_changeset_iter *p){
          207  +  return SQLITE_CHANGESET_ABORT;
          208  +}
          209  +
          210  +static void run_test(
          211  +  sqlite3 *db, 
          212  +  sqlite3 *db2, 
          213  +  int nRow, 
          214  +  const char *zSql
          215  +){
          216  +  sqlite3_session *pSession = 0;
          217  +  sqlite3_stmt *pStmt = 0;
          218  +  int rc;
          219  +  int i;
          220  +  int nChangeset;
          221  +  void *pChangeset;
          222  +
          223  +  /* Attach a session object to database db */
          224  +  rc = sqlite3session_create(db, "main", &pSession);
          225  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          226  +
          227  +  /* Configure the session to capture changes on all tables */
          228  +  rc = sqlite3session_attach(pSession, 0);
          229  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          230  +
          231  +  /* Prepare the SQL statement */
          232  +  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
          233  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          234  +
          235  +  /* Open a transaction */
          236  +  execsql(db, "BEGIN");
          237  +
          238  +  /* Execute the SQL statement nRow times */
          239  +  for(i=0; i<nRow; i++){
          240  +    sqlite3_bind_int(pStmt, 1, i);
          241  +    sqlite3_step(pStmt);
          242  +    rc = sqlite3_reset(pStmt);
          243  +    if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          244  +  }
          245  +  sqlite3_finalize(pStmt);
          246  +
          247  +  /* Extract a changeset from the sessions object */
          248  +  rc = sqlite3session_changeset(pSession, &nChangeset, &pChangeset);
          249  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          250  +  execsql(db, "COMMIT");
          251  +
          252  +  /* Apply the changeset to the second db */
          253  +  rc = sqlite3changeset_apply(db2, nChangeset, pChangeset, 0, xConflict, 0);
          254  +  if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          255  +
          256  +  /* Cleanup */
          257  +  sqlite3_free(pChangeset);
          258  +  sqlite3session_delete(pSession);
          259  +}
          260  +
          261  +int main(int argc, char **argv){
          262  +  struct Options {
          263  +    int nRow;
          264  +    int bWithoutRowid;
          265  +    int bInteger;
          266  +    int bAll;
          267  +    const char *zDb;
          268  +  };
          269  +  struct Options o = { 2500, 0, 0, 0, "session_speed_test.db" };
          270  +
          271  +  CmdLineOption aOpt[] = {
          272  +    CMDLINE_INT32( "-rows", "number of rows in test",
          273  +      offsetof(struct Options, nRow) ),
          274  +    CMDLINE_BOOL("-without-rowid", "use WITHOUT ROWID tables", 
          275  +      offsetof(struct Options, bWithoutRowid) ),
          276  +    CMDLINE_BOOL("-integer", "use integer data (instead of text/blobs)",
          277  +      offsetof(struct Options, bInteger) ),
          278  +    CMDLINE_NONE("-all", "Run all 4 combos of -without-rowid and -integer",
          279  +      offsetof(struct Options, bAll) ),
          280  +    CMDLINE_TEXT("-database", "prefix for database files to use",
          281  +      offsetof(struct Options, zDb) ),
          282  +    {0, 0, 0, 0}
          283  +  };
          284  +
          285  +  const char *azCreate[] = {
          286  +    "CREATE TABLE t1(a PRIMARY KEY, b, c, d)",
          287  +    "CREATE TABLE t1(a PRIMARY KEY, b, c, d) WITHOUT ROWID",
          288  +  };
          289  +
          290  +  const char *azInsert[] = {
          291  +    "INSERT INTO t1 VALUES("
          292  +    "printf('%.8d',?), randomblob(50), randomblob(50), randomblob(50))",
          293  +    "INSERT INTO t1 VALUES(?, random(), random(), random())"
          294  +  };
          295  +
          296  +  const char *azUpdate[] = {
          297  +    "UPDATE t1 SET d = randomblob(50) WHERE a = printf('%.8d',?)",
          298  +    "UPDATE t1 SET d = random() WHERE a = ?"
          299  +  };
          300  +
          301  +  const char *azDelete[] = {
          302  +    "DELETE FROM t1 WHERE a = printf('%.8d',?)",
          303  +    "DELETE FROM t1 WHERE a = ?"
          304  +  };
          305  +
          306  +  int rc;
          307  +  sqlite3 *db;
          308  +  sqlite3 *db2;
          309  +  char *zDb2;
          310  +  int bWithoutRowid;
          311  +  int bInteger;
          312  +
          313  +  parse_command_line(argc, argv, 1, aOpt, (void*)&o,
          314  +    "This program creates two new, empty, databases each containing a single\n"
          315  +    "table. It then does the following:\n\n"
          316  +    "  1. Inserts -rows rows into the first database\n"
          317  +    "  2. Updates each row in the first db\n"
          318  +    "  3. Delete each row from the first db\n\n"
          319  +    "The modifications made by each step are captured in a changeset and\n"
          320  +    "applied to the second database.\n"
          321  +  );
          322  +  zDb2 = sqlite3_mprintf("%s2", o.zDb);
          323  +
          324  +  for(bWithoutRowid=0; bWithoutRowid<2; bWithoutRowid++){
          325  +    for(bInteger=0; bInteger<2; bInteger++){
          326  +      if( o.bAll || (o.bWithoutRowid==bWithoutRowid && o.bInteger==bInteger) ){
          327  +        fprintf(stdout, "Testing %s data with %s table\n",
          328  +            bInteger ? "integer" : "blob/text",
          329  +            bWithoutRowid ? "WITHOUT ROWID" : "rowid"
          330  +        );
          331  +
          332  +        /* Open new database handles on two empty databases */
          333  +        unlink(o.zDb);
          334  +        rc = sqlite3_open(o.zDb, &db);
          335  +        if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          336  +        unlink(zDb2);
          337  +        rc = sqlite3_open(zDb2, &db2);
          338  +        if( rc!=SQLITE_OK ) abort_due_to_error(rc);
          339  +
          340  +        /* Create the schema in both databases. */
          341  +        execsql(db, azCreate[o.bWithoutRowid]);
          342  +        execsql(db2, azCreate[o.bWithoutRowid]);
          343  +
          344  +        /* Run the three tests */
          345  +        run_test(db, db2, o.nRow, azInsert[o.bInteger]);
          346  +        run_test(db, db2, o.nRow, azUpdate[o.bInteger]);
          347  +        run_test(db, db2, o.nRow, azDelete[o.bInteger]);
          348  +
          349  +        /* Close the db handles */
          350  +        sqlite3_close(db);
          351  +        sqlite3_close(db2);
          352  +      }
          353  +    }
          354  +  }
          355  +
          356  +
          357  +  return 0;
          358  +}
          359  +
          360  +

Added ext/session/sessionat.test.

            1  +# 2017 February 04
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Tests for the sessions module. Specifically, that a changeset can
           13  +# be applied after ALTER TABLE ADD COLUMN has been used to add 
           14  +# columns to tables.
           15  +#
           16  +
           17  +if {![info exists testdir]} {
           18  +  set testdir [file join [file dirname [info script]] .. .. test]
           19  +} 
           20  +source [file join [file dirname [info script]] session_common.tcl]
           21  +source $testdir/tester.tcl
           22  +ifcapable !session {finish_test; return}
           23  +
           24  +set testprefix sessionat
           25  +
           26  +db close
           27  +sqlite3_shutdown
           28  +test_sqlite3_log log
           29  +proc log {code msg} { lappend ::log $code $msg }
           30  +
           31  +proc reset_test {} {
           32  +  catch { db  close }
           33  +  catch { db2 close }
           34  +  forcedelete test.db test.db2
           35  +  sqlite3 db test.db
           36  +  sqlite3 db2 test.db2
           37  +}
           38  +
           39  +
           40  +# Run all tests in this file twice. Once with "WITHOUT ROWID", and once
           41  +# with regular rowid tables.
           42  +#
           43  +# ?.1.*: Test that PK inconsistencies are detected if one or more of the PK
           44  +#        columns are not present in the changeset.
           45  +#
           46  +# ?.2.*: Test that it is not possible to apply a changeset with N columns
           47  +#        to a db with fewer than N columns.
           48  +#
           49  +# ?.3.*: Test some INSERT, UPDATE and DELETE operations that do not
           50  +#        require conflict handling.
           51  +#
           52  +# ?.4.*: Test some INSERT, UPDATE and DELETE operations that do require 
           53  +#        conflict handling.
           54  +#
           55  +# ?.5.*: Test that attempting to concat two changesets with different
           56  +#        numbers of columns for the same table is an error.
           57  +#
           58  +foreach {tn trailing} {
           59  +  sessionat-ipk ""
           60  +  sessionat-wor " WITHOUT ROWID "
           61  +} {
           62  +eval [string map [list %WR% $trailing] {
           63  +  reset_test
           64  +
           65  +  #-----------------------------------------------------------------------
           66  +  do_execsql_test $tn.1.0 {
           67  +    CREATE TABLE t1(a, b, PRIMARY KEY(a)) %WR%;
           68  +  }
           69  +  do_execsql_test -db db2 $tn.1.1 {
           70  +    CREATE TABLE t1(a, b, c, PRIMARY KEY(a, c)) %WR%;
           71  +  }
           72  +  do_test $tn.1.2 {
           73  +    set ::log {}
           74  +    do_then_apply_sql { INSERT INTO t1 VALUES('one', 'two') }
           75  +    set ::log
           76  +  } [list \
           77  +    SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}
           78  +  ]
           79  +  do_execsql_test $tn.1.3 { SELECT * FROM t1 } {one two}
           80  +  do_execsql_test -db db2 $tn.1.4 { SELECT * FROM t1 } {}
           81  +
           82  +  #-----------------------------------------------------------------------
           83  +  do_execsql_test $tn.2.0 {
           84  +    CREATE TABLE t2(x, y, z, PRIMARY KEY(x)) %WR%;
           85  +  }
           86  +  do_execsql_test -db db2 $tn.2.1 {
           87  +    CREATE TABLE t2(x, y, PRIMARY KEY(x)) %WR%;
           88  +  }
           89  +  do_test $tn.2.2 {
           90  +    db cache flush
           91  +    set ::log {}
           92  +    do_then_apply_sql { INSERT INTO t2 VALUES(1, 2, 3) }
           93  +    set ::log
           94  +  } [list SQLITE_SCHEMA \
           95  +    {sqlite3changeset_apply(): table t2 has 2 columns, expected 3 or more}
           96  +  ]
           97  +  do_execsql_test $tn.2.3 { SELECT * FROM t2 } {1 2 3}
           98  +  do_execsql_test -db db2 $tn.2.4 { SELECT * FROM t2 } {}
           99  +
          100  +  #-----------------------------------------------------------------------
          101  +  do_execsql_test $tn.3.0 {
          102  +    CREATE TABLE t3(a, b, PRIMARY KEY(b)) %WR%;
          103  +  }
          104  +  do_execsql_test -db db2 $tn.3.1 {
          105  +    CREATE TABLE t3(a, b, c DEFAULT 'D', PRIMARY KEY(b)) %WR%;
          106  +  }
          107  +  do_test $tn.3.2 {
          108  +    do_then_apply_sql {
          109  +      INSERT INTO t3 VALUES(1, 2);
          110  +      INSERT INTO t3 VALUES(3, 4);
          111  +      INSERT INTO t3 VALUES(5, 6);
          112  +    };
          113  +    db2 eval {SELECT * FROM t3}
          114  +  } {1 2 D 3 4 D 5 6 D}
          115  +  do_test $tn.3.3 {
          116  +    do_then_apply_sql {
          117  +      UPDATE t3 SET a=45 WHERE b=4;
          118  +      DELETE FROM t3 WHERE a=5;
          119  +    };
          120  +    db2 eval {SELECT * FROM t3}
          121  +  } {1 2 D 45 4 D}
          122  +
          123  +  #-----------------------------------------------------------------------
          124  +  # 4.1: INSERT statements
          125  +  # 4.2: DELETE statements
          126  +  # 4.3: UPDATE statements
          127  +  #  
          128  +  do_execsql_test $tn.4.1.0 {
          129  +    CREATE TABLE t4(x INTEGER PRIMARY KEY, y) %WR%;
          130  +  }
          131  +  do_execsql_test -db db2 $tn.4.1.1 {
          132  +    CREATE TABLE t4(x INTEGER PRIMARY KEY, y, z) %WR%;
          133  +    INSERT INTO t4 VALUES(1, 2, 3);
          134  +    INSERT INTO t4 VALUES(4, 5, 6);
          135  +  }
          136  +  do_conflict_test $tn.4.1.2 -tables t4 -sql {
          137  +    INSERT INTO t4 VALUES(10, 20);
          138  +    INSERT INTO t4 VALUES(4, 11);
          139  +  } -conflicts {
          140  +    {INSERT t4 CONFLICT {i 4 i 11} {i 4 i 5}}
          141  +  }
          142  +  do_execsql_test -db db2 $tn.4.1.3 {
          143  +    SELECT * FROM t4 ORDER BY x
          144  +  } {1 2 3 4 5 6 10 20 {}}
          145  +  do_conflict_test $tn.4.1.4 -policy REPLACE -tables t4 -sql {
          146  +    INSERT INTO t4 VALUES(1, 11);
          147  +  } -conflicts {
          148  +    {INSERT t4 CONFLICT {i 1 i 11} {i 1 i 2}}
          149  +  }
          150  +  do_execsql_test -db db2 $tn.4.1.5 {
          151  +    SELECT * FROM t4 ORDER BY x
          152  +  } {1 11 {} 4 5 6 10 20 {}}
          153  +
          154  +  do_execsql_test $tn.4.2.0 {
          155  +    DELETE FROM t4;
          156  +    INSERT INTO t4 VALUES(1, 'A');
          157  +    INSERT INTO t4 VALUES(2, 'B');
          158  +    INSERT INTO t4 VALUES(3, 'C');
          159  +    INSERT INTO t4 VALUES(4, 'D');
          160  +  }
          161  +  do_execsql_test -db db2 $tn.4.2.1 {
          162  +    DELETE FROM t4;
          163  +    INSERT INTO t4 VALUES(1, 'A', 'a');
          164  +    INSERT INTO t4 VALUES(3, 'C', 'c');
          165  +    INSERT INTO t4 VALUES(4, 'E', 'd');
          166  +  }
          167  +  do_conflict_test $tn.4.2.2 -tables t4 -sql {
          168  +    DELETE FROM t4 WHERE x=2;
          169  +    DELETE FROM t4 WHERE x=4;
          170  +  } -conflicts {
          171  +    {DELETE t4 NOTFOUND {i 2 t B}}
          172  +    {DELETE t4 DATA {i 4 t D} {i 4 t E}}
          173  +  }
          174  +
          175  +  do_execsql_test $tn.4.3.0 {
          176  +    CREATE TABLE t5(a, b, c PRIMARY KEY) %WR%;
          177  +    INSERT INTO t5 VALUES(1,1,1), (2,2,2), (3,3,3), (4,4,4);
          178  +  }
          179  +  do_execsql_test -db db2 $tn.4.3.1 {
          180  +    CREATE TABLE t5(a, b, c PRIMARY KEY, d CHECK(b!=10)) %WR%;
          181  +    INSERT INTO t5 VALUES (2,2,2,2), (3,8,3,3), (4,4,4,4);
          182  +  }
          183  +  do_conflict_test $tn.4.3.2 -tables t5 -sql {
          184  +    UPDATE t5 SET a=4 WHERE c=1;
          185  +    UPDATE t5 SET b=9 WHERE c=3;
          186  +    UPDATE t5 SET b=10 WHERE c=2;
          187  +  } -conflicts {
          188  +    {UPDATE t5 NOTFOUND {i 1 {} {} i 1} {i 4 {} {} {} {}}}
          189  +    {UPDATE t5 DATA {{} {} i 3 i 3} {{} {} i 9 {} {}} {i 3 i 8 i 3}}
          190  +    {UPDATE t5 CONSTRAINT {{} {} i 2 i 2} {{} {} i 10 {} {}}}
          191  +  }
          192  +  
          193  +  #-----------------------------------------------------------------------
          194  +  do_execsql_test $tn.5.0 {
          195  +    CREATE TABLE t6(a, b, c, PRIMARY KEY(a, b)) %WR%;
          196  +  }
          197  +  do_execsql_test -db db2 $tn.5.1 {
          198  +    CREATE TABLE t6(a, b, c, d, e, PRIMARY KEY(a, b)) %WR%;
          199  +  }
          200  +  do_test $tn.5.2 {
          201  +    set c1 [sql_exec_changeset db {
          202  +      INSERT INTO t6 VALUES(1, 1, 1);
          203  +      INSERT INTO t6 VALUES(2, 2, 2);
          204  +    }]
          205  +    set c2 [sql_exec_changeset db2 {
          206  +      INSERT INTO t6 VALUES(3, 3, 3, 3, 3);
          207  +      INSERT INTO t6 VALUES(4, 4, 4, 4, 4);
          208  +    }]
          209  +    list [catch { sqlite3changeset_concat $c1 $c2} msg] $msg
          210  +  } {1 SQLITE_SCHEMA}
          211  +
          212  +}]
          213  +}
          214  +
          215  +
          216  +finish_test

Added ext/session/sessionwor.test.

            1  +# 2017 Jan 31
            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  +# The focus of this file is testing the session module. Specifically,
           13  +# testing support for WITHOUT ROWID tables.
           14  +#
           15  +
           16  +if {![info exists testdir]} {
           17  +  set testdir [file join [file dirname [info script]] .. .. test]
           18  +} 
           19  +source [file join [file dirname [info script]] session_common.tcl]
           20  +source $testdir/tester.tcl
           21  +ifcapable !session {finish_test; return}
           22  +
           23  +set testprefix sessionwor
           24  +
           25  +proc test_reset {} {
           26  +  catch { db close }
           27  +  catch { db2 close }
           28  +  forcedelete test.db test.db2
           29  +  sqlite3 db test.db
           30  +  sqlite3 db2 test.db2
           31  +}
           32  +
           33  +
           34  +do_execsql_test 1.0 {
           35  +  CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT ROWID;
           36  +}
           37  +
           38  +do_iterator_test 1.1 t1 {
           39  +  INSERT INTO t1 VALUES('one', 'two');
           40  +} {
           41  +  {INSERT t1 0 X. {} {t one t two}}
           42  +}
           43  +
           44  +do_iterator_test 1.2 t1 {
           45  +  UPDATE t1 SET b='three'
           46  +} {
           47  +  {UPDATE t1 0 X. {t one t two} {{} {} t three}}
           48  +}
           49  +
           50  +do_iterator_test 1.3 t1 {
           51  +  DELETE FROM t1;
           52  +} {
           53  +  {DELETE t1 0 X. {t one t three} {}}
           54  +}
           55  +
           56  +finish_test
           57  +

Changes to ext/session/sqlite3changebatch.c.

   156    156     if( rc==SQLITE_OK ){
   157    157       while( SQLITE_ROW==sqlite3_step(pIndexInfo) ){ nCol++; }
   158    158       rc = sqlite3_reset(pIndexInfo);
   159    159     }
   160    160   
   161    161     pNew = (BatchIndex*)cbMalloc(&rc, sizeof(BatchIndex) + sizeof(int) * nCol);
   162    162     if( rc==SQLITE_OK ){
   163         -    int rc2;
   164    163       pNew->nCol = nCol;
   165    164       pNew->bPk = bPk;
   166    165       pNew->aiCol = (int*)&pNew[1];
   167    166       pNew->iId = p->iNextIdxId++;
   168    167       while( SQLITE_ROW==sqlite3_step(pIndexInfo) ){ 
   169    168         int i = sqlite3_column_int(pIndexInfo, 0);
   170    169         int j = sqlite3_column_int(pIndexInfo, 1);

Changes to ext/session/sqlite3session.c.

  3045   3045     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
  3046   3046     int iVal,                       /* Index of conflict record value to fetch */
  3047   3047     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
  3048   3048   ){
  3049   3049     if( !pIter->pConflict ){
  3050   3050       return SQLITE_MISUSE;
  3051   3051     }
  3052         -  if( iVal<0 || iVal>=sqlite3_column_count(pIter->pConflict) ){
         3052  +  if( iVal<0 || iVal>=pIter->nCol ){
  3053   3053       return SQLITE_RANGE;
  3054   3054     }
  3055   3055     *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
  3056   3056     return SQLITE_OK;
  3057   3057   }
  3058   3058   
  3059   3059   /*
................................................................................
  3512   3512   ){
  3513   3513     int rc = SQLITE_OK;
  3514   3514     int i;
  3515   3515     SessionBuffer buf = {0, 0, 0};
  3516   3516   
  3517   3517     sessionAppendStr(&buf, "INSERT INTO main.", &rc);
  3518   3518     sessionAppendIdent(&buf, zTab, &rc);
  3519         -  sessionAppendStr(&buf, " VALUES(?", &rc);
         3519  +  sessionAppendStr(&buf, "(", &rc);
         3520  +  for(i=0; i<p->nCol; i++){
         3521  +    if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
         3522  +    sessionAppendIdent(&buf, p->azCol[i], &rc);
         3523  +  }
         3524  +
         3525  +  sessionAppendStr(&buf, ") VALUES(?", &rc);
  3520   3526     for(i=1; i<p->nCol; i++){
  3521   3527       sessionAppendStr(&buf, ", ?", &rc);
  3522   3528     }
  3523   3529     sessionAppendStr(&buf, ")", &rc);
  3524   3530   
  3525   3531     if( rc==SQLITE_OK ){
  3526   3532       rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
................................................................................
  4058   4064           if( zTab==0 ){
  4059   4065             rc = SQLITE_NOMEM;
  4060   4066             break;
  4061   4067           }
  4062   4068           nTab = (int)strlen(zTab);
  4063   4069           sApply.azCol = (const char **)zTab;
  4064   4070         }else{
         4071  +        int nMinCol = 0;
         4072  +        int i;
         4073  +
  4065   4074           sqlite3changeset_pk(pIter, &abPK, 0);
  4066   4075           rc = sessionTableInfo(
  4067   4076               db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
  4068   4077           );
  4069   4078           if( rc!=SQLITE_OK ) break;
         4079  +        for(i=0; i<sApply.nCol; i++){
         4080  +          if( sApply.abPK[i] ) nMinCol = i+1;
         4081  +        }
  4070   4082     
  4071   4083           if( sApply.nCol==0 ){
  4072   4084             schemaMismatch = 1;
  4073   4085             sqlite3_log(SQLITE_SCHEMA, 
  4074   4086                 "sqlite3changeset_apply(): no such table: %s", zTab
  4075   4087             );
  4076   4088           }
  4077         -        else if( sApply.nCol!=nCol ){
         4089  +        else if( sApply.nCol<nCol ){
  4078   4090             schemaMismatch = 1;
  4079   4091             sqlite3_log(SQLITE_SCHEMA, 
  4080         -              "sqlite3changeset_apply(): table %s has %d columns, expected %d", 
         4092  +              "sqlite3changeset_apply(): table %s has %d columns, "
         4093  +              "expected %d or more", 
  4081   4094                 zTab, sApply.nCol, nCol
  4082   4095             );
  4083   4096           }
  4084         -        else if( memcmp(sApply.abPK, abPK, nCol)!=0 ){
         4097  +        else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
  4085   4098             schemaMismatch = 1;
  4086   4099             sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
  4087   4100                 "primary key mismatch for table %s", zTab
  4088   4101             );
  4089   4102           }
  4090         -        else if( 
  4091         -            (rc = sessionSelectRow(db, zTab, &sApply))
  4092         -         || (rc = sessionUpdateRow(db, zTab, &sApply))
  4093         -         || (rc = sessionDeleteRow(db, zTab, &sApply))
  4094         -         || (rc = sessionInsertRow(db, zTab, &sApply))
  4095         -        ){
  4096         -          break;
         4103  +        else{
         4104  +          sApply.nCol = nCol;
         4105  +          if((rc = sessionSelectRow(db, zTab, &sApply))
         4106  +          || (rc = sessionUpdateRow(db, zTab, &sApply))
         4107  +          || (rc = sessionDeleteRow(db, zTab, &sApply))
         4108  +          || (rc = sessionInsertRow(db, zTab, &sApply))
         4109  +          ){
         4110  +            break;
         4111  +          }
  4097   4112           }
  4098   4113           nTab = sqlite3Strlen30(zTab);
  4099   4114         }
  4100   4115       }
  4101   4116   
  4102   4117       /* If there is a schema mismatch on the current table, proceed to the
  4103   4118       ** next change. A log message has already been issued. */

Changes to ext/session/sqlite3session.h.

   328    328   **   <li> For each row (primary key) that exists in the to-table but not in 
   329    329   **     the from-table, an INSERT record is added to the session object.
   330    330   **
   331    331   **   <li> For each row (primary key) that exists in the to-table but not in 
   332    332   **     the from-table, a DELETE record is added to the session object.
   333    333   **
   334    334   **   <li> For each row (primary key) that exists in both tables, but features 
   335         -**     different in each, an UPDATE record is added to the session.
          335  +**     different non-PK values in each, an UPDATE record is added to the
          336  +**     session.  
   336    337   ** </ul>
   337    338   **
   338    339   ** To clarify, if this function is called and then a changeset constructed
   339    340   ** using [sqlite3session_changeset()], then after applying that changeset to 
   340    341   ** database zFrom the contents of the two compatible tables would be 
   341    342   ** identical.
   342    343   **
................................................................................
   913    914   ** For each table that is not excluded by the filter callback, this function 
   914    915   ** tests that the target database contains a compatible table. A table is 
   915    916   ** considered compatible if all of the following are true:
   916    917   **
   917    918   ** <ul>
   918    919   **   <li> The table has the same name as the name recorded in the 
   919    920   **        changeset, and
   920         -**   <li> The table has the same number of columns as recorded in the 
          921  +**   <li> The table has at least as many columns as recorded in the 
   921    922   **        changeset, and
   922    923   **   <li> The table has primary key columns in the same position as 
   923    924   **        recorded in the changeset.
   924    925   ** </ul>
   925    926   **
   926    927   ** If there is no compatible table, it is not an error, but none of the
   927    928   ** changes associated with the table are applied. A warning message is issued
................................................................................
   958    959   **   original row values stored in the changeset. If it does, and the values 
   959    960   **   stored in all non-primary key columns also match the values stored in 
   960    961   **   the changeset the row is deleted from the target database.
   961    962   **
   962    963   **   If a row with matching primary key values is found, but one or more of
   963    964   **   the non-primary key fields contains a value different from the original
   964    965   **   row value stored in the changeset, the conflict-handler function is
   965         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument.
          966  +**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
          967  +**   database table has more columns than are recorded in the changeset,
          968  +**   only the values of those non-primary key fields are compared against
          969  +**   the current database contents - any trailing database table columns
          970  +**   are ignored.
   966    971   **
   967    972   **   If no row with matching primary key values is found in the database,
   968    973   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
   969    974   **   passed as the second argument.
   970    975   **
   971    976   **   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
   972    977   **   (which can only happen if a foreign key constraint is violated), the
................................................................................
   973    978   **   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
   974    979   **   passed as the second argument. This includes the case where the DELETE
   975    980   **   operation is attempted because an earlier call to the conflict handler
   976    981   **   function returned [SQLITE_CHANGESET_REPLACE].
   977    982   **
   978    983   ** <dt>INSERT Changes<dd>
   979    984   **   For each INSERT change, an attempt is made to insert the new row into
   980         -**   the database.
          985  +**   the database. If the changeset row contains fewer fields than the
          986  +**   database table, the trailing fields are populated with their default
          987  +**   values.
   981    988   **
   982    989   **   If the attempt to insert the row fails because the database already 
   983    990   **   contains a row with the same primary key values, the conflict handler
   984    991   **   function is invoked with the second argument set to 
   985    992   **   [SQLITE_CHANGESET_CONFLICT].
   986    993   **
   987    994   **   If the attempt to insert the row fails because of some other constraint
................................................................................
   991    998   **   an earlier call to the conflict handler function returned 
   992    999   **   [SQLITE_CHANGESET_REPLACE].
   993   1000   **
   994   1001   ** <dt>UPDATE Changes<dd>
   995   1002   **   For each UPDATE change, this function checks if the target database 
   996   1003   **   contains a row with the same primary key value (or values) as the 
   997   1004   **   original row values stored in the changeset. If it does, and the values 
   998         -**   stored in all non-primary key columns also match the values stored in 
   999         -**   the changeset the row is updated within the target database.
         1005  +**   stored in all modified non-primary key columns also match the values
         1006  +**   stored in the changeset the row is updated within the target database.
  1000   1007   **
  1001   1008   **   If a row with matching primary key values is found, but one or more of
  1002         -**   the non-primary key fields contains a value different from an original
  1003         -**   row value stored in the changeset, the conflict-handler function is
  1004         -**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
         1009  +**   the modified non-primary key fields contains a value different from an
         1010  +**   original row value stored in the changeset, the conflict-handler function
         1011  +**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
  1005   1012   **   UPDATE changes only contain values for non-primary key fields that are
  1006   1013   **   to be modified, only those fields need to match the original values to
  1007   1014   **   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
  1008   1015   **
  1009   1016   **   If no row with matching primary key values is found in the database,
  1010   1017   **   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
  1011   1018   **   passed as the second argument.

Changes to ext/userauth/sqlite3userauth.h.

    17     17   ** end of an SQLite amalgamation header file ("sqlite3.h"), then add
    18     18   ** the SQLITE_USER_AUTHENTICATION compile-time option.  See the
    19     19   ** user-auth.txt file in the same source directory as this file for
    20     20   ** additional information.
    21     21   */
    22     22   #ifdef SQLITE_USER_AUTHENTICATION
    23     23   
           24  +#ifdef __cplusplus
           25  +extern "C" {
           26  +#endif
           27  +
    24     28   /*
    25     29   ** If a database contains the SQLITE_USER table, then the
    26     30   ** sqlite3_user_authenticate() interface must be invoked with an
    27     31   ** appropriate username and password prior to enable read and write
    28     32   ** access to the database.
    29     33   **
    30     34   ** Return SQLITE_OK on success or SQLITE_ERROR if the username/password
................................................................................
    80     84   ** the database cannot be converted into a no-authentication-required
    81     85   ** database.
    82     86   */
    83     87   int sqlite3_user_delete(
    84     88     sqlite3 *db,           /* Database connection */
    85     89     const char *zUsername  /* Username to remove */
    86     90   );
           91  +
           92  +#ifdef __cplusplus
           93  +}  /* end of the 'extern "C"' block */
           94  +#endif
    87     95   
    88     96   #endif /* SQLITE_USER_AUTHENTICATION */

Changes to ext/userauth/userauth.c.

    18     18   **
    19     19   ** To compile with the user-authentication feature, append this file to
    20     20   ** end of an SQLite amalgamation, then add the SQLITE_USER_AUTHENTICATION
    21     21   ** compile-time option.  See the user-auth.txt file in the same source
    22     22   ** directory as this file for additional information.
    23     23   */
    24     24   #ifdef SQLITE_USER_AUTHENTICATION
    25         -#ifndef _SQLITEINT_H_
           25  +#ifndef SQLITEINT_H
    26     26   # include "sqliteInt.h"
    27     27   #endif
    28     28   
    29     29   /*
    30     30   ** Prepare an SQL statement for use by the user authentication logic.
    31     31   ** Return a pointer to the prepared statement on success.  Return a
    32     32   ** NULL pointer if there is an error of any kind.

Changes to main.mk.

   474    474   # Extra compiler options for various shell tools
   475    475   #
   476    476   SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
   477    477   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   478    478   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   479    479   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   480    480   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
          481  +FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   481    482   DBFUZZ_OPT =
          483  +KV_OPT = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
          484  +ST_OPT = -DSQLITE_THREADSAFE=0
   482    485   
   483    486   # This is the default Makefile target.  The objects listed here
   484    487   # are what get build when you type just "make" with no arguments.
   485    488   #
   486    489   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   487    490   
   488    491   libsqlite3.a:	$(LIBOBJ)
................................................................................
   755    758   	echo "static const char *zMainloop = " >> $@
   756    759   	tclsh $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
   757    760   	echo "; return zMainloop; }" >> $@
   758    761   
   759    762   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   760    763   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
   761    764   
          765  +dbdump$(EXE):	$(TOP)/ext/misc/dbdump.c sqlite3.o
          766  +	$(TCCX) -DDBDUMP_STANDALONE -o dbdump$(EXE) \
          767  +            $(TOP)/ext/misc/dbdump.c sqlite3.o $(THREADLIB)
          768  +
   762    769   # Rules to build the 'testfixture' application.
   763    770   #
   764    771   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   765    772   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
   766    773   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
   767    774   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
   768    775   
................................................................................
   892    899   LogEst$(EXE):	$(TOP)/tool/logest.c sqlite3.h
   893    900   	$(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c
   894    901   
   895    902   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
   896    903   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
   897    904   		$(TOP)/test/wordcount.c sqlite3.c
   898    905   
   899         -speedtest1$(EXE):	$(TOP)/test/speedtest1.c sqlite3.o
   900         -	$(TCC) -I. $(OTAFLAGS) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.o $(THREADLIB) 
          906  +speedtest1$(EXE):	$(TOP)/test/speedtest1.c sqlite3.c
          907  +	$(TCCX) -I. $(ST_OPT) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.c $(THREADLIB) 
          908  +
          909  +kvtest$(EXE):	$(TOP)/test/kvtest.c sqlite3.c
          910  +	$(TCCX) -I. $(KV_OPT) -o kvtest$(EXE) $(TOP)/test/kvtest.c sqlite3.c $(THREADLIB) 
   901    911   
   902    912   rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.o 
   903    913   	$(TCC) -I. -o rbu$(EXE) $(TOP)/ext/rbu/rbu.c sqlite3.o \
   904    914   	  $(THREADLIB)
   905    915   
   906    916   loadfts: $(TOP)/tool/loadfts.c libsqlite3.a
   907    917   	$(TCC) $(TOP)/tool/loadfts.c libsqlite3.a -o loadfts $(THREADLIB)

Changes to src/analyze.c.

   286    286     int nKeyCol;              /* Number of index columns w/o the pk/rowid */
   287    287     int mxSample;             /* Maximum number of samples to accumulate */
   288    288     Stat4Sample current;      /* Current row as a Stat4Sample */
   289    289     u32 iPrn;                 /* Pseudo-random number used for sampling */
   290    290     Stat4Sample *aBest;       /* Array of nCol best samples */
   291    291     int iMin;                 /* Index in a[] of entry with minimum score */
   292    292     int nSample;              /* Current number of samples */
          293  +  int nMaxEqZero;           /* Max leading 0 in anEq[] for any a[] entry */
   293    294     int iGet;                 /* Index of current sample accessed by stat_get() */
   294    295     Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
   295    296     sqlite3 *db;              /* Database connection, for malloc() */
   296    297   };
   297    298   
   298    299   /* Reclaim memory used by a Stat4Sample
   299    300   */
................................................................................
   550    551   static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
   551    552     Stat4Sample *pSample = 0;
   552    553     int i;
   553    554   
   554    555     assert( IsStat4 || nEqZero==0 );
   555    556   
   556    557   #ifdef SQLITE_ENABLE_STAT4
          558  +  /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
          559  +  ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
          560  +  ** other words, if nMaxEqZero is n, then it is guaranteed that there
          561  +  ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
          562  +  if( nEqZero>p->nMaxEqZero ){
          563  +    p->nMaxEqZero = nEqZero;
          564  +  }
   557    565     if( pNew->isPSample==0 ){
   558    566       Stat4Sample *pUpgrade = 0;
   559    567       assert( pNew->anEq[pNew->iCol]>0 );
   560    568   
   561    569       /* This sample is being added because the prefix that ends in column 
   562    570       ** iCol occurs many times in the table. However, if we have already
   563    571       ** added a sample that shares this prefix, there is no need to add
................................................................................
   647    655       Stat4Sample *pBest = &p->aBest[i];
   648    656       pBest->anEq[i] = p->current.anEq[i];
   649    657       if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
   650    658         sampleInsert(p, pBest, i);
   651    659       }
   652    660     }
   653    661   
   654         -  /* Update the anEq[] fields of any samples already collected. */
          662  +  /* Check that no sample contains an anEq[] entry with an index of
          663  +  ** p->nMaxEqZero or greater set to zero. */
   655    664     for(i=p->nSample-1; i>=0; i--){
   656    665       int j;
   657         -    for(j=iChng; j<p->nCol; j++){
   658         -      if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
          666  +    for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
          667  +  }
          668  +
          669  +  /* Update the anEq[] fields of any samples already collected. */
          670  +  if( iChng<p->nMaxEqZero ){
          671  +    for(i=p->nSample-1; i>=0; i--){
          672  +      int j;
          673  +      for(j=iChng; j<p->nCol; j++){
          674  +        if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
          675  +      }
   659    676       }
          677  +    p->nMaxEqZero = iChng;
   660    678     }
   661    679   #endif
   662    680   
   663    681   #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
   664    682     if( iChng==0 ){
   665    683       tRowcnt nLt = p->current.anLt[0];
   666    684       tRowcnt nEq = p->current.anEq[0];
................................................................................
   792    810   /*
   793    811   ** Implementation of the stat_get(P,J) SQL function.  This routine is
   794    812   ** used to query statistical information that has been gathered into
   795    813   ** the Stat4Accum object by prior calls to stat_push().  The P parameter
   796    814   ** has type BLOB but it is really just a pointer to the Stat4Accum object.
   797    815   ** The content to returned is determined by the parameter J
   798    816   ** which is one of the STAT_GET_xxxx values defined above.
          817  +**
          818  +** The stat_get(P,J) function is not available to generic SQL.  It is
          819  +** inserted as part of a manually constructed bytecode program.  (See
          820  +** the callStatGet() routine below.)  It is guaranteed that the P
          821  +** parameter will always be a poiner to a Stat4Accum object, never a
          822  +** NULL.
   799    823   **
   800    824   ** If neither STAT3 nor STAT4 are enabled, then J is always
   801    825   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
   802    826   ** a one-parameter function, stat_get(P), that always returns the
   803    827   ** stat1 table entry information.
   804    828   */
   805    829   static void statGet(
................................................................................
  1176   1200         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1177   1201       }else{
  1178   1202         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  1179   1203         int j, k, regKey;
  1180   1204         regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
  1181   1205         for(j=0; j<pPk->nKeyCol; j++){
  1182   1206           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
  1183         -        assert( k>=0 && k<pTab->nCol );
         1207  +        assert( k>=0 && k<pIdx->nColumn );
  1184   1208           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
  1185   1209           VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
  1186   1210         }
  1187   1211         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
  1188   1212         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1189   1213       }
  1190   1214   #endif
................................................................................
  1360   1384     assert( pName2!=0 || pName1==0 );
  1361   1385     if( pName1==0 ){
  1362   1386       /* Form 1:  Analyze everything */
  1363   1387       for(i=0; i<db->nDb; i++){
  1364   1388         if( i==1 ) continue;  /* Do not analyze the TEMP database */
  1365   1389         analyzeDatabase(pParse, i);
  1366   1390       }
  1367         -  }else if( pName2->n==0 ){
  1368         -    /* Form 2:  Analyze the database or table named */
  1369         -    iDb = sqlite3FindDb(db, pName1);
  1370         -    if( iDb>=0 ){
  1371         -      analyzeDatabase(pParse, iDb);
  1372         -    }else{
  1373         -      z = sqlite3NameFromToken(db, pName1);
  1374         -      if( z ){
  1375         -        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
  1376         -          analyzeTable(pParse, pIdx->pTable, pIdx);
  1377         -        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
  1378         -          analyzeTable(pParse, pTab, 0);
  1379         -        }
  1380         -        sqlite3DbFree(db, z);
  1381         -      }
  1382         -    }
         1391  +  }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
         1392  +    /* Analyze the schema named as the argument */
         1393  +    analyzeDatabase(pParse, iDb);
  1383   1394     }else{
  1384         -    /* Form 3: Analyze the fully qualified table name */
         1395  +    /* Form 3: Analyze the table or index named as an argument */
  1385   1396       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
  1386   1397       if( iDb>=0 ){
  1387         -      zDb = db->aDb[iDb].zDbSName;
         1398  +      zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
  1388   1399         z = sqlite3NameFromToken(db, pTableName);
  1389   1400         if( z ){
  1390   1401           if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
  1391   1402             analyzeTable(pParse, pIdx->pTable, pIdx);
  1392   1403           }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
  1393   1404             analyzeTable(pParse, pTab, 0);
  1394   1405           }
  1395   1406           sqlite3DbFree(db, z);
  1396   1407         }
  1397         -    }   
         1408  +    }
  1398   1409     }
  1399         -  v = sqlite3GetVdbe(pParse);
  1400         -  if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
         1410  +  if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
         1411  +    sqlite3VdbeAddOp0(v, OP_Expire);
         1412  +  }
  1401   1413   }
  1402   1414   
  1403   1415   /*
  1404   1416   ** Used to pass information from the analyzer reader through to the
  1405   1417   ** callback routine.
  1406   1418   */
  1407   1419   typedef struct analysisInfo analysisInfo;
................................................................................
  1522   1534         pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
  1523   1535         if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
  1524   1536       }
  1525   1537       aiRowEst = pIndex->aiRowEst;
  1526   1538   #endif
  1527   1539       pIndex->bUnordered = 0;
  1528   1540       decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
  1529         -    if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
         1541  +    pIndex->hasStat1 = 1;
         1542  +    if( pIndex->pPartIdxWhere==0 ){
         1543  +      pTable->nRowLogEst = pIndex->aiRowLogEst[0];
         1544  +      pTable->tabFlags |= TF_HasStat1;
         1545  +    }
  1530   1546     }else{
  1531   1547       Index fakeIdx;
  1532   1548       fakeIdx.szIdxRow = pTable->szTabRow;
  1533   1549   #ifdef SQLITE_ENABLE_COSTMULT
  1534   1550       fakeIdx.pTable = pTable;
  1535   1551   #endif
  1536   1552       decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
  1537   1553       pTable->szTabRow = fakeIdx.szIdxRow;
         1554  +    pTable->tabFlags |= TF_HasStat1;
  1538   1555     }
  1539   1556   
  1540   1557     return 0;
  1541   1558   }
  1542   1559   
  1543   1560   /*
  1544   1561   ** If the Index.aSample variable is not NULL, delete the aSample[] array
................................................................................
  1611   1628            || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] 
  1612   1629           ){
  1613   1630             sumEq += aSample[i].anEq[iCol];
  1614   1631             nSum100 += 100;
  1615   1632           }
  1616   1633         }
  1617   1634   
  1618         -      if( nDist100>nSum100 ){
         1635  +      if( nDist100>nSum100 && sumEq<nRow ){
  1619   1636           avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
  1620   1637         }
  1621   1638         if( avgEq==0 ) avgEq = 1;
  1622   1639         pIdx->aAvgEq[iCol] = avgEq;
  1623   1640       }
  1624   1641     }
  1625   1642   }
................................................................................
  1825   1842   ** code may be ignored.
  1826   1843   */
  1827   1844   int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
  1828   1845     analysisInfo sInfo;
  1829   1846     HashElem *i;
  1830   1847     char *zSql;
  1831   1848     int rc = SQLITE_OK;
         1849  +  Schema *pSchema = db->aDb[iDb].pSchema;
  1832   1850   
  1833   1851     assert( iDb>=0 && iDb<db->nDb );
  1834   1852     assert( db->aDb[iDb].pBt!=0 );
  1835   1853   
  1836   1854     /* Clear any prior statistics */
  1837   1855     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1838         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
         1856  +  for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
         1857  +    Table *pTab = sqliteHashData(i);
         1858  +    pTab->tabFlags &= ~TF_HasStat1;
         1859  +  }
         1860  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1839   1861       Index *pIdx = sqliteHashData(i);
  1840         -    pIdx->aiRowLogEst[0] = 0;
         1862  +    pIdx->hasStat1 = 0;
  1841   1863   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1842   1864       sqlite3DeleteIndexSamples(db, pIdx);
  1843   1865       pIdx->aSample = 0;
  1844   1866   #endif
  1845   1867     }
  1846   1868   
  1847   1869     /* Load new statistics out of the sqlite_stat1 table */
................................................................................
  1856   1878         rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
  1857   1879         sqlite3DbFree(db, zSql);
  1858   1880       }
  1859   1881     }
  1860   1882   
  1861   1883     /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
  1862   1884     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1863         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
         1885  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1864   1886       Index *pIdx = sqliteHashData(i);
  1865         -    if( pIdx->aiRowLogEst[0]==0 ) sqlite3DefaultRowEst(pIdx);
         1887  +    if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
  1866   1888     }
  1867   1889   
  1868   1890     /* Load the statistics from the sqlite_stat4 table. */
  1869   1891   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1870   1892     if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
  1871   1893       db->lookaside.bDisable++;
  1872   1894       rc = loadStat4(db, sInfo.zDatabase);
  1873   1895       db->lookaside.bDisable--;
  1874   1896     }
  1875         -  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
         1897  +  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1876   1898       Index *pIdx = sqliteHashData(i);
  1877   1899       sqlite3_free(pIdx->aiRowEst);
  1878   1900       pIdx->aiRowEst = 0;
  1879   1901     }
  1880   1902   #endif
  1881   1903   
  1882   1904     if( rc==SQLITE_NOMEM ){

Changes to src/btree.c.

  1516   1516       ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
  1517   1517     }
  1518   1518   }
  1519   1519   #endif
  1520   1520   
  1521   1521   
  1522   1522   /*
  1523         -** Defragment the page given.  All Cells are moved to the
  1524         -** end of the page and all free space is collected into one
  1525         -** big FreeBlk that occurs in between the header and cell
  1526         -** pointer array and the cell content area.
         1523  +** Defragment the page given. This routine reorganizes cells within the
         1524  +** page so that there are no free-blocks on the free-block list.
         1525  +**
         1526  +** Parameter nMaxFrag is the maximum amount of fragmented space that may be
         1527  +** present in the page after this routine returns.
  1527   1528   **
  1528   1529   ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
  1529   1530   ** b-tree page so that there are no freeblocks or fragment bytes, all
  1530   1531   ** unused bytes are contained in the unallocated space region, and all
  1531   1532   ** cells are packed tightly at the end of the page.
  1532   1533   */
  1533         -static int defragmentPage(MemPage *pPage){
         1534  +static int defragmentPage(MemPage *pPage, int nMaxFrag){
  1534   1535     int i;                     /* Loop counter */
  1535   1536     int pc;                    /* Address of the i-th cell */
  1536   1537     int hdr;                   /* Offset to the page header */
  1537   1538     int size;                  /* Size of a cell */
  1538   1539     int usableSize;            /* Number of usable bytes on a page */
  1539   1540     int cellOffset;            /* Offset to the cell pointer array */
  1540   1541     int cbrk;                  /* Offset to the cell content area */
................................................................................
  1541   1542     int nCell;                 /* Number of cells on the page */
  1542   1543     unsigned char *data;       /* The page data */
  1543   1544     unsigned char *temp;       /* Temp area for cell content */
  1544   1545     unsigned char *src;        /* Source of content */
  1545   1546     int iCellFirst;            /* First allowable cell index */
  1546   1547     int iCellLast;             /* Last possible cell index */
  1547   1548   
  1548         -
  1549   1549     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1550   1550     assert( pPage->pBt!=0 );
  1551   1551     assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
  1552   1552     assert( pPage->nOverflow==0 );
  1553   1553     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1554   1554     temp = 0;
  1555   1555     src = data = pPage->aData;
  1556   1556     hdr = pPage->hdrOffset;
  1557   1557     cellOffset = pPage->cellOffset;
  1558   1558     nCell = pPage->nCell;
  1559   1559     assert( nCell==get2byte(&data[hdr+3]) );
         1560  +  iCellFirst = cellOffset + 2*nCell;
  1560   1561     usableSize = pPage->pBt->usableSize;
         1562  +
         1563  +  /* This block handles pages with two or fewer free blocks and nMaxFrag
         1564  +  ** or fewer fragmented bytes. In this case it is faster to move the
         1565  +  ** two (or one) blocks of cells using memmove() and add the required
         1566  +  ** offsets to each pointer in the cell-pointer array than it is to 
         1567  +  ** reconstruct the entire page.  */
         1568  +  if( (int)data[hdr+7]<=nMaxFrag ){
         1569  +    int iFree = get2byte(&data[hdr+1]);
         1570  +    if( iFree ){
         1571  +      int iFree2 = get2byte(&data[iFree]);
         1572  +
         1573  +      /* pageFindSlot() has already verified that free blocks are sorted
         1574  +      ** in order of offset within the page, and that no block extends
         1575  +      ** past the end of the page. Provided the two free slots do not 
         1576  +      ** overlap, this guarantees that the memmove() calls below will not
         1577  +      ** overwrite the usableSize byte buffer, even if the database page
         1578  +      ** is corrupt.  */
         1579  +      assert( iFree2==0 || iFree2>iFree );
         1580  +      assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
         1581  +      assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
         1582  +
         1583  +      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
         1584  +        u8 *pEnd = &data[cellOffset + nCell*2];
         1585  +        u8 *pAddr;
         1586  +        int sz2 = 0;
         1587  +        int sz = get2byte(&data[iFree+2]);
         1588  +        int top = get2byte(&data[hdr+5]);
         1589  +        if( iFree2 ){
         1590  +          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_BKPT;
         1591  +          sz2 = get2byte(&data[iFree2+2]);
         1592  +          assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
         1593  +          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
         1594  +          sz += sz2;
         1595  +        }
         1596  +        cbrk = top+sz;
         1597  +        assert( cbrk+(iFree-top) <= usableSize );
         1598  +        memmove(&data[cbrk], &data[top], iFree-top);
         1599  +        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
         1600  +          pc = get2byte(pAddr);
         1601  +          if( pc<iFree ){ put2byte(pAddr, pc+sz); }
         1602  +          else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
         1603  +        }
         1604  +        goto defragment_out;
         1605  +      }
         1606  +    }
         1607  +  }
         1608  +
  1561   1609     cbrk = usableSize;
  1562         -  iCellFirst = cellOffset + 2*nCell;
  1563   1610     iCellLast = usableSize - 4;
  1564   1611     for(i=0; i<nCell; i++){
  1565   1612       u8 *pAddr;     /* The i-th cell pointer */
  1566   1613       pAddr = &data[cellOffset + i*2];
  1567   1614       pc = get2byte(pAddr);
  1568   1615       testcase( pc==iCellFirst );
  1569   1616       testcase( pc==iCellLast );
................................................................................
  1589   1636         temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
  1590   1637         x = get2byte(&data[hdr+5]);
  1591   1638         memcpy(&temp[x], &data[x], (cbrk+size) - x);
  1592   1639         src = temp;
  1593   1640       }
  1594   1641       memcpy(&data[cbrk], &src[pc], size);
  1595   1642     }
         1643  +  data[hdr+7] = 0;
         1644  +
         1645  + defragment_out:
         1646  +  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
         1647  +    return SQLITE_CORRUPT_BKPT;
         1648  +  }
  1596   1649     assert( cbrk>=iCellFirst );
  1597   1650     put2byte(&data[hdr+5], cbrk);
  1598   1651     data[hdr+1] = 0;
  1599   1652     data[hdr+2] = 0;
  1600         -  data[hdr+7] = 0;
  1601   1653     memset(&data[iCellFirst], 0, cbrk-iCellFirst);
  1602   1654     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  1603         -  if( cbrk-iCellFirst!=pPage->nFree ){
  1604         -    return SQLITE_CORRUPT_BKPT;
  1605         -  }
  1606   1655     return SQLITE_OK;
  1607   1656   }
  1608   1657   
  1609   1658   /*
  1610   1659   ** Search the free-list on page pPg for space to store a cell nByte bytes in
  1611   1660   ** size. If one can be found, return a pointer to the space and remove it
  1612   1661   ** from the free-list.
................................................................................
  1736   1785   
  1737   1786     /* The request could not be fulfilled using a freelist slot.  Check
  1738   1787     ** to see if defragmentation is necessary.
  1739   1788     */
  1740   1789     testcase( gap+2+nByte==top );
  1741   1790     if( gap+2+nByte>top ){
  1742   1791       assert( pPage->nCell>0 || CORRUPT_DB );
  1743         -    rc = defragmentPage(pPage);
         1792  +    rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
  1744   1793       if( rc ) return rc;
  1745   1794       top = get2byteNotZero(&data[hdr+5]);
  1746         -    assert( gap+nByte<=top );
         1795  +    assert( gap+2+nByte<=top );
  1747   1796     }
  1748   1797   
  1749   1798   
  1750   1799     /* Allocate memory from the gap in between the cell pointer array
  1751   1800     ** and the cell content area.  The btreeInitPage() call has already
  1752   1801     ** validated the freelist.  Given that the freelist is valid, there
  1753   1802     ** is no way that the allocation can extend off the end of the page.
................................................................................
  3011   3060       BTREE_AUTOVACUUM_INCR
  3012   3061     );
  3013   3062     sqlite3BtreeLeave(p);
  3014   3063     return rc;
  3015   3064   #endif
  3016   3065   }
  3017   3066   
         3067  +/*
         3068  +** If the user has not set the safety-level for this database connection
         3069  +** using "PRAGMA synchronous", and if the safety-level is not already
         3070  +** set to the value passed to this function as the second parameter,
         3071  +** set it so.
         3072  +*/
         3073  +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
         3074  +static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
         3075  +  sqlite3 *db;
         3076  +  Db *pDb;
         3077  +  if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
         3078  +    while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
         3079  +    if( pDb->bSyncSet==0 
         3080  +     && pDb->safety_level!=safety_level 
         3081  +     && pDb!=&db->aDb[1] 
         3082  +    ){
         3083  +      pDb->safety_level = safety_level;
         3084  +      sqlite3PagerSetFlags(pBt->pPager,
         3085  +          pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
         3086  +    }
         3087  +  }
         3088  +}
         3089  +#else
         3090  +# define setDefaultSyncFlag(pBt,safety_level)
         3091  +#endif
  3018   3092   
  3019   3093   /*
  3020   3094   ** Get a reference to pPage1 of the database file.  This will
  3021   3095   ** also acquire a readlock on that file.
  3022   3096   **
  3023   3097   ** SQLITE_OK is returned on success.  If the file is not a
  3024   3098   ** well-formed database file, then SQLITE_CORRUPT is returned.
................................................................................
  3084   3158       */
  3085   3159       if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
  3086   3160         int isOpen = 0;
  3087   3161         rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
  3088   3162         if( rc!=SQLITE_OK ){
  3089   3163           goto page1_init_failed;
  3090   3164         }else{
  3091         -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
  3092         -        sqlite3 *db;
  3093         -        Db *pDb;
  3094         -        if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
  3095         -          while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
  3096         -          if( pDb->bSyncSet==0
  3097         -           && pDb->safety_level==SQLITE_DEFAULT_SYNCHRONOUS+1
  3098         -          ){
  3099         -            pDb->safety_level = SQLITE_DEFAULT_WAL_SYNCHRONOUS+1;
  3100         -            sqlite3PagerSetFlags(pBt->pPager,
  3101         -               pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
  3102         -          }
  3103         -        }
  3104         -#endif
         3165  +        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
  3105   3166           if( isOpen==0 ){
  3106   3167             releasePage(pPage1);
  3107   3168             return SQLITE_OK;
  3108   3169           }
  3109   3170         }
  3110   3171         rc = SQLITE_NOTADB;
         3172  +    }else{
         3173  +      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
  3111   3174       }
  3112   3175   #endif
  3113   3176   
  3114   3177       /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
  3115   3178       ** fractions and the leaf payload fraction values must be 64, 32, and 32.
  3116   3179       **
  3117   3180       ** The original design allowed these amounts to vary, but as of
................................................................................
  3570   3633       nCell = pPage->nCell;
  3571   3634   
  3572   3635       for(i=0; i<nCell; i++){
  3573   3636         u8 *pCell = findCell(pPage, i);
  3574   3637         if( eType==PTRMAP_OVERFLOW1 ){
  3575   3638           CellInfo info;
  3576   3639           pPage->xParseCell(pPage, pCell, &info);
  3577         -        if( info.nLocal<info.nPayload
  3578         -         && pCell+info.nSize-1<=pPage->aData+pPage->maskPage
  3579         -         && iFrom==get4byte(pCell+info.nSize-4)
  3580         -        ){
  3581         -          put4byte(pCell+info.nSize-4, iTo);
  3582         -          break;
         3640  +        if( info.nLocal<info.nPayload ){
         3641  +          if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
         3642  +            return SQLITE_CORRUPT_BKPT;
         3643  +          }
         3644  +          if( iFrom==get4byte(pCell+info.nSize-4) ){
         3645  +            put4byte(pCell+info.nSize-4, iTo);
         3646  +            break;
         3647  +          }
  3583   3648           }
  3584   3649         }else{
  3585   3650           if( get4byte(pCell)==iFrom ){
  3586   3651             put4byte(pCell, iTo);
  3587   3652             break;
  3588   3653           }
  3589   3654         }
................................................................................
  4428   4493     int rc = SQLITE_OK;
  4429   4494     if( p && p->inTrans==TRANS_WRITE ){
  4430   4495       BtShared *pBt = p->pBt;
  4431   4496       assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
  4432   4497       assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
  4433   4498       sqlite3BtreeEnter(p);
  4434   4499       btreePtrmapEnd(pBt, op, iSavepoint);
  4435         -    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
         4500  +    if( op==SAVEPOINT_ROLLBACK ){
         4501  +      rc = saveAllCursors(pBt, 0, 0);
         4502  +    }
         4503  +    if( rc==SQLITE_OK ){
         4504  +      rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
         4505  +    }
  4436   4506       if( rc==SQLITE_OK ){
  4437   4507         if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
  4438   4508           pBt->nPage = 0;
  4439   4509         }
  4440   4510         rc = newDatabase(pBt);
  4441   4511         pBt->nPage = get4byte(28 + pBt->pPage1->aData);
  4442   4512   
................................................................................
  4814   4884   /*
  4815   4885   ** This function is used to read or overwrite payload information
  4816   4886   ** for the entry that the pCur cursor is pointing to. The eOp
  4817   4887   ** argument is interpreted as follows:
  4818   4888   **
  4819   4889   **   0: The operation is a read. Populate the overflow cache.
  4820   4890   **   1: The operation is a write. Populate the overflow cache.
  4821         -**   2: The operation is a read. Do not populate the overflow cache.
  4822   4891   **
  4823   4892   ** A total of "amt" bytes are read or written beginning at "offset".
  4824   4893   ** Data is read to or from the buffer pBuf.
  4825   4894   **
  4826   4895   ** The content being read or written might appear on the main page
  4827   4896   ** or be scattered out on multiple overflow pages.
  4828   4897   **
  4829         -** If the current cursor entry uses one or more overflow pages and the
  4830         -** eOp argument is not 2, this function may allocate space for and lazily 
  4831         -** populates the overflow page-list cache array (BtCursor.aOverflow). 
         4898  +** If the current cursor entry uses one or more overflow pages
         4899  +** this function may allocate space for and lazily populate
         4900  +** the overflow page-list cache array (BtCursor.aOverflow). 
  4832   4901   ** Subsequent calls use this cache to make seeking to the supplied offset 
  4833   4902   ** more efficient.
  4834   4903   **
  4835         -** Once an overflow page-list cache has been allocated, it may be
         4904  +** Once an overflow page-list cache has been allocated, it must be
  4836   4905   ** invalidated if some other cursor writes to the same table, or if
  4837   4906   ** the cursor is moved to a different row. Additionally, in auto-vacuum
  4838   4907   ** mode, the following events may invalidate an overflow page-list cache.
  4839   4908   **
  4840   4909   **   * An incremental vacuum,
  4841   4910   **   * A commit in auto_vacuum="full" mode,
  4842   4911   **   * Creating a table (may require moving an overflow page).
................................................................................
  4850   4919   ){
  4851   4920     unsigned char *aPayload;
  4852   4921     int rc = SQLITE_OK;
  4853   4922     int iIdx = 0;
  4854   4923     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  4855   4924     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
  4856   4925   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4857         -  unsigned char * const pBufStart = pBuf;
  4858         -  int bEnd;                                 /* True if reading to end of data */
         4926  +  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
  4859   4927   #endif
  4860   4928   
  4861   4929     assert( pPage );
         4930  +  assert( eOp==0 || eOp==1 );
  4862   4931     assert( pCur->eState==CURSOR_VALID );
  4863   4932     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  4864   4933     assert( cursorHoldsMutex(pCur) );
  4865         -  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
  4866   4934   
  4867   4935     getCellInfo(pCur);
  4868   4936     aPayload = pCur->info.pPayload;
  4869         -#ifdef SQLITE_DIRECT_OVERFLOW_READ
  4870         -  bEnd = offset+amt==pCur->info.nPayload;
  4871         -#endif
  4872   4937     assert( offset+amt <= pCur->info.nPayload );
  4873   4938   
  4874   4939     assert( aPayload > pPage->aData );
  4875   4940     if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
  4876   4941       /* Trying to read or write past the end of the data is an error.  The
  4877   4942       ** conditional above is really:
  4878   4943       **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
................................................................................
  4883   4948   
  4884   4949     /* Check if data must be read/written to/from the btree page itself. */
  4885   4950     if( offset<pCur->info.nLocal ){
  4886   4951       int a = amt;
  4887   4952       if( a+offset>pCur->info.nLocal ){
  4888   4953         a = pCur->info.nLocal - offset;
  4889   4954       }
  4890         -    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
         4955  +    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
  4891   4956       offset = 0;
  4892   4957       pBuf += a;
  4893   4958       amt -= a;
  4894   4959     }else{
  4895   4960       offset -= pCur->info.nLocal;
  4896   4961     }
  4897   4962   
................................................................................
  4899   4964     if( rc==SQLITE_OK && amt>0 ){
  4900   4965       const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
  4901   4966       Pgno nextPage;
  4902   4967   
  4903   4968       nextPage = get4byte(&aPayload[pCur->info.nLocal]);
  4904   4969   
  4905   4970       /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
  4906         -    ** Except, do not allocate aOverflow[] for eOp==2.
  4907   4971       **
  4908   4972       ** The aOverflow[] array is sized at one entry for each overflow page
  4909   4973       ** in the overflow chain. The page number of the first overflow page is
  4910   4974       ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
  4911   4975       ** means "not yet known" (the cache is lazily populated).
  4912   4976       */
  4913         -    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
         4977  +    if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
  4914   4978         int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
  4915   4979         if( nOvfl>pCur->nOvflAlloc ){
  4916   4980           Pgno *aNew = (Pgno*)sqlite3Realloc(
  4917   4981               pCur->aOverflow, nOvfl*2*sizeof(Pgno)
  4918   4982           );
  4919   4983           if( aNew==0 ){
  4920         -          rc = SQLITE_NOMEM_BKPT;
         4984  +          return SQLITE_NOMEM_BKPT;
  4921   4985           }else{
  4922   4986             pCur->nOvflAlloc = nOvfl*2;
  4923   4987             pCur->aOverflow = aNew;
  4924   4988           }
  4925   4989         }
  4926         -      if( rc==SQLITE_OK ){
  4927         -        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
  4928         -        pCur->curFlags |= BTCF_ValidOvfl;
         4990  +      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
         4991  +      pCur->curFlags |= BTCF_ValidOvfl;
         4992  +    }else{
         4993  +      /* If the overflow page-list cache has been allocated and the
         4994  +      ** entry for the first required overflow page is valid, skip
         4995  +      ** directly to it.
         4996  +      */
         4997  +      if( pCur->aOverflow[offset/ovflSize] ){
         4998  +        iIdx = (offset/ovflSize);
         4999  +        nextPage = pCur->aOverflow[iIdx];
         5000  +        offset = (offset%ovflSize);
  4929   5001         }
  4930   5002       }
  4931   5003   
  4932         -    /* If the overflow page-list cache has been allocated and the
  4933         -    ** entry for the first required overflow page is valid, skip
  4934         -    ** directly to it.
  4935         -    */
  4936         -    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
  4937         -     && pCur->aOverflow[offset/ovflSize]
  4938         -    ){
  4939         -      iIdx = (offset/ovflSize);
  4940         -      nextPage = pCur->aOverflow[iIdx];
  4941         -      offset = (offset%ovflSize);
  4942         -    }
  4943         -
  4944         -    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
  4945         -
         5004  +    assert( rc==SQLITE_OK && amt>0 );
         5005  +    while( nextPage ){
  4946   5006         /* If required, populate the overflow page-list cache. */
  4947         -      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
  4948         -        assert( pCur->aOverflow[iIdx]==0
  4949         -                || pCur->aOverflow[iIdx]==nextPage
  4950         -                || CORRUPT_DB );
  4951         -        pCur->aOverflow[iIdx] = nextPage;
  4952         -      }
         5007  +      assert( pCur->aOverflow[iIdx]==0
         5008  +              || pCur->aOverflow[iIdx]==nextPage
         5009  +              || CORRUPT_DB );
         5010  +      pCur->aOverflow[iIdx] = nextPage;
  4953   5011   
  4954   5012         if( offset>=ovflSize ){
  4955   5013           /* The only reason to read this page is to obtain the page
  4956   5014           ** number for the next page in the overflow chain. The page
  4957   5015           ** data is not required. So first try to lookup the overflow
  4958   5016           ** page-list cache, if any, then fall back to the getOverflowPage()
  4959   5017           ** function.
  4960         -        **
  4961         -        ** Note that the aOverflow[] array must be allocated because eOp!=2
  4962         -        ** here.  If eOp==2, then offset==0 and this branch is never taken.
  4963   5018           */
  4964         -        assert( eOp!=2 );
  4965   5019           assert( pCur->curFlags & BTCF_ValidOvfl );
  4966   5020           assert( pCur->pBtree->db==pBt->db );
  4967   5021           if( pCur->aOverflow[iIdx+1] ){
  4968   5022             nextPage = pCur->aOverflow[iIdx+1];
  4969   5023           }else{
  4970   5024             rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
  4971   5025           }
  4972   5026           offset -= ovflSize;
  4973   5027         }else{
  4974   5028           /* Need to read this page properly. It contains some of the
  4975   5029           ** range of data that is being read (eOp==0) or written (eOp!=0).
  4976   5030           */
  4977   5031   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4978         -        sqlite3_file *fd;
         5032  +        sqlite3_file *fd;      /* File from which to do direct overflow read */
  4979   5033   #endif
  4980   5034           int a = amt;
  4981   5035           if( a + offset > ovflSize ){
  4982   5036             a = ovflSize - offset;
  4983   5037           }
  4984   5038   
  4985   5039   #ifdef SQLITE_DIRECT_OVERFLOW_READ
  4986   5040           /* If all the following are true:
  4987   5041           **
  4988   5042           **   1) this is a read operation, and 
  4989   5043           **   2) data is required from the start of this overflow page, and
  4990         -        **   3) the database is file-backed, and
  4991         -        **   4) there is no open write-transaction, and
  4992         -        **   5) the database is not a WAL database,
  4993         -        **   6) all data from the page is being read.
  4994         -        **   7) at least 4 bytes have already been read into the output buffer 
         5044  +        **   3) there is no open write-transaction, and
         5045  +        **   4) the database is file-backed, and
         5046  +        **   5) the page is not in the WAL file
         5047  +        **   6) at least 4 bytes have already been read into the output buffer 
  4995   5048           **
  4996   5049           ** then data can be read directly from the database file into the
  4997   5050           ** output buffer, bypassing the page-cache altogether. This speeds
  4998   5051           ** up loading large records that span many overflow pages.
  4999   5052           */
  5000         -        if( (eOp&0x01)==0                                      /* (1) */
         5053  +        if( eOp==0                                             /* (1) */
  5001   5054            && offset==0                                          /* (2) */
  5002         -         && (bEnd || a==ovflSize)                              /* (6) */
  5003         -         && pBt->inTransaction==TRANS_READ                     /* (4) */
  5004         -         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
  5005         -         && 0==sqlite3PagerUseWal(pBt->pPager)                 /* (5) */
  5006         -         && &pBuf[-4]>=pBufStart                               /* (7) */
         5055  +         && pBt->inTransaction==TRANS_READ                     /* (3) */
         5056  +         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (4) */
         5057  +         && 0==sqlite3PagerUseWal(pBt->pPager, nextPage)       /* (5) */
         5058  +         && &pBuf[-4]>=pBufStart                               /* (6) */
  5007   5059           ){
  5008   5060             u8 aSave[4];
  5009   5061             u8 *aWrite = &pBuf[-4];
  5010         -          assert( aWrite>=pBufStart );                         /* hence (7) */
         5062  +          assert( aWrite>=pBufStart );                         /* due to (6) */
  5011   5063             memcpy(aSave, aWrite, 4);
  5012   5064             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
  5013   5065             nextPage = get4byte(aWrite);
  5014   5066             memcpy(aWrite, aSave, 4);
  5015   5067           }else
  5016   5068   #endif
  5017   5069   
  5018   5070           {
  5019   5071             DbPage *pDbPage;
  5020   5072             rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
  5021         -              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
         5073  +              (eOp==0 ? PAGER_GET_READONLY : 0)
  5022   5074             );
  5023   5075             if( rc==SQLITE_OK ){
  5024   5076               aPayload = sqlite3PagerGetData(pDbPage);
  5025   5077               nextPage = get4byte(aPayload);
  5026         -            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
         5078  +            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
  5027   5079               sqlite3PagerUnref(pDbPage);
  5028   5080               offset = 0;
  5029   5081             }
  5030   5082           }
  5031   5083           amt -= a;
         5084  +        if( amt==0 ) return rc;
  5032   5085           pBuf += a;
  5033   5086         }
         5087  +      if( rc ) break;
         5088  +      iIdx++;
  5034   5089       }
  5035   5090     }
  5036   5091   
  5037   5092     if( rc==SQLITE_OK && amt>0 ){
  5038         -    return SQLITE_CORRUPT_BKPT;
         5093  +    return SQLITE_CORRUPT_BKPT; /* Overflow chain ends prematurely */
  5039   5094     }
  5040   5095     return rc;
  5041   5096   }
  5042   5097   
  5043   5098   /*
  5044   5099   ** Read part of the payload for the row at which that cursor pCur is currently
  5045   5100   ** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
................................................................................
  5060   5115   int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
  5061   5116     assert( cursorHoldsMutex(pCur) );
  5062   5117     assert( pCur->eState==CURSOR_VALID );
  5063   5118     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
  5064   5119     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  5065   5120     return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
  5066   5121   }
         5122  +
         5123  +/*
         5124  +** This variant of sqlite3BtreePayload() works even if the cursor has not
         5125  +** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
         5126  +** interface.
         5127  +*/
  5067   5128   #ifndef SQLITE_OMIT_INCRBLOB
  5068         -int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
         5129  +static SQLITE_NOINLINE int accessPayloadChecked(
         5130  +  BtCursor *pCur,
         5131  +  u32 offset,
         5132  +  u32 amt,
         5133  +  void *pBuf
         5134  +){
  5069   5135     int rc;
  5070   5136     if ( pCur->eState==CURSOR_INVALID ){
  5071   5137       return SQLITE_ABORT;
  5072   5138     }
  5073   5139     assert( cursorOwnsBtShared(pCur) );
  5074         -  rc = restoreCursorPosition(pCur);
  5075         -  if( rc==SQLITE_OK ){
  5076         -    assert( pCur->eState==CURSOR_VALID );
  5077         -    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
  5078         -    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
  5079         -    rc = accessPayload(pCur, offset, amt, pBuf, 0);
         5140  +  rc = btreeRestoreCursorPosition(pCur);
         5141  +  return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
         5142  +}
         5143  +int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
         5144  +  if( pCur->eState==CURSOR_VALID ){
         5145  +    assert( cursorOwnsBtShared(pCur) );
         5146  +    return accessPayload(pCur, offset, amt, pBuf, 0);
         5147  +  }else{
         5148  +    return accessPayloadChecked(pCur, offset, amt, pBuf);
  5080   5149     }
  5081         -  return rc;
  5082   5150   }
  5083   5151   #endif /* SQLITE_OMIT_INCRBLOB */
  5084   5152   
  5085   5153   /*
  5086   5154   ** Return a pointer to payload information from the entry that the 
  5087   5155   ** pCur cursor is pointing to.  The pointer is to the beginning of
  5088   5156   ** the key if index btrees (pPage->intKey==0) and is the data for
................................................................................
  5163   5231     pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
  5164   5232     pCur->iPage++;
  5165   5233     pCur->aiIdx[pCur->iPage] = 0;
  5166   5234     return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage],
  5167   5235                           pCur, pCur->curPagerFlags);
  5168   5236   }
  5169   5237   
  5170         -#if SQLITE_DEBUG
         5238  +#ifdef SQLITE_DEBUG
  5171   5239   /*
  5172   5240   ** Page pParent is an internal (non-leaf) tree page. This function 
  5173   5241   ** asserts that page number iChild is the left-child if the iIdx'th
  5174   5242   ** cell in page pParent. Or, if iIdx is equal to the total number of
  5175   5243   ** cells in pParent, that page number iChild is the right-child of
  5176   5244   ** the page.
  5177   5245   */
................................................................................
  5480   5548     if( pIdxKey==0
  5481   5549      && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  5482   5550     ){
  5483   5551       if( pCur->info.nKey==intKey ){
  5484   5552         *pRes = 0;
  5485   5553         return SQLITE_OK;
  5486   5554       }
  5487         -    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
  5488         -      *pRes = -1;
  5489         -      return SQLITE_OK;
         5555  +    if( pCur->info.nKey<intKey ){
         5556  +      if( (pCur->curFlags & BTCF_AtLast)!=0 ){
         5557  +        *pRes = -1;
         5558  +        return SQLITE_OK;
         5559  +      }
         5560  +      /* If the requested key is one more than the previous key, then
         5561  +      ** try to get there using sqlite3BtreeNext() rather than a full
         5562  +      ** binary search.  This is an optimization only.  The correct answer
         5563  +      ** is still obtained without this ase, only a little more slowely */
         5564  +      if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
         5565  +        *pRes = 0;
         5566  +        rc = sqlite3BtreeNext(pCur, pRes);
         5567  +        if( rc ) return rc;
         5568  +        if( *pRes==0 ){
         5569  +          getCellInfo(pCur);
         5570  +          if( pCur->info.nKey==intKey ){
         5571  +            return SQLITE_OK;
         5572  +          }
         5573  +        }
         5574  +      }
  5490   5575       }
  5491   5576     }
  5492   5577   
  5493   5578     if( pIdxKey ){
  5494   5579       xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
  5495   5580       pIdxKey->errCode = 0;
  5496   5581       assert( pIdxKey->default_rc==1 
................................................................................
  5618   5703             }
  5619   5704             pCellKey = sqlite3Malloc( nCell+18 );
  5620   5705             if( pCellKey==0 ){
  5621   5706               rc = SQLITE_NOMEM_BKPT;
  5622   5707               goto moveto_finish;
  5623   5708             }
  5624   5709             pCur->aiIdx[pCur->iPage] = (u16)idx;
  5625         -          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
         5710  +          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
         5711  +          pCur->curFlags &= ~BTCF_ValidOvfl;
  5626   5712             if( rc ){
  5627   5713               sqlite3_free(pCellKey);
  5628   5714               goto moveto_finish;
  5629   5715             }
  5630   5716             c = xRecordCompare(nCell, pCellKey, pIdxKey);
  5631   5717             sqlite3_free(pCellKey);
  5632   5718           }
................................................................................
  5687   5773   int sqlite3BtreeEof(BtCursor *pCur){
  5688   5774     /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
  5689   5775     ** have been deleted? This API will need to change to return an error code
  5690   5776     ** as well as the boolean result value.
  5691   5777     */
  5692   5778     return (CURSOR_VALID!=pCur->eState);
  5693   5779   }
         5780  +
         5781  +/*
         5782  +** Return an estimate for the number of rows in the table that pCur is
         5783  +** pointing to.  Return a negative number if no estimate is currently 
         5784  +** available.
         5785  +*/
         5786  +i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
         5787  +  i64 n;
         5788  +  u8 i;
         5789  +
         5790  +  assert( cursorOwnsBtShared(pCur) );
         5791  +  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
         5792  +
         5793  +  /* Currently this interface is only called by the OP_IfSmaller
         5794  +  ** opcode, and it that case the cursor will always be valid and
         5795  +  ** will always point to a leaf node. */
         5796  +  if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
         5797  +  if( NEVER(pCur->apPage[pCur->iPage]->leaf==0) ) return -1;
         5798  +
         5799  +  for(n=1, i=0; i<=pCur->iPage; i++){
         5800  +    n *= pCur->apPage[i]->nCell;
         5801  +  }
         5802  +  return n;
         5803  +}
  5694   5804   
  5695   5805   /*
  5696   5806   ** Advance the cursor to the next entry in the database.  If
  5697   5807   ** successful then set *pRes=0.  If the cursor
  5698   5808   ** was already pointing to the last entry in the database before
  5699   5809   ** this routine was called, then set *pRes=1.
  5700   5810   **
................................................................................
  6542   6652     **                      that means content must spill into overflow pages.
  6543   6653     **   *pnSize            Size of the local cell (not counting overflow pages)
  6544   6654     **   pPrior             Where to write the pgno of the first overflow page
  6545   6655     **
  6546   6656     ** Use a call to btreeParseCellPtr() to verify that the values above
  6547   6657     ** were computed correctly.
  6548   6658     */
  6549         -#if SQLITE_DEBUG
         6659  +#ifdef SQLITE_DEBUG
  6550   6660     {
  6551   6661       CellInfo info;
  6552   6662       pPage->xParseCell(pPage, pCell, &info);
  6553   6663       assert( nHeader==(int)(info.pPayload - pCell) );
  6554   6664       assert( info.nKey==pX->nKey );
  6555   6665       assert( *pnSize == info.nSize );
  6556   6666       assert( spaceLeft == info.nLocal );
................................................................................
  7669   7779     ** usableSpace: Number of bytes of space available on each sibling.
  7670   7780     ** 
  7671   7781     */
  7672   7782     usableSpace = pBt->usableSize - 12 + leafCorrection;
  7673   7783     for(i=0; i<nOld; i++){
  7674   7784       MemPage *p = apOld[i];
  7675   7785       szNew[i] = usableSpace - p->nFree;
  7676         -    if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
  7677   7786       for(j=0; j<p->nOverflow; j++){
  7678   7787         szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
  7679   7788       }
  7680   7789       cntNew[i] = cntOld[i];
  7681   7790     }
  7682   7791     k = nOld;
  7683   7792     for(i=0; i<k; i++){
................................................................................
  8067   8176       **
  8068   8177       ** It is critical that the child page be defragmented before being
  8069   8178       ** copied into the parent, because if the parent is page 1 then it will
  8070   8179       ** by smaller than the child due to the database header, and so all the
  8071   8180       ** free space needs to be up front.
  8072   8181       */
  8073   8182       assert( nNew==1 || CORRUPT_DB );
  8074         -    rc = defragmentPage(apNew[0]);
         8183  +    rc = defragmentPage(apNew[0], -1);
  8075   8184       testcase( rc!=SQLITE_OK );
  8076   8185       assert( apNew[0]->nFree == 
  8077   8186           (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
  8078   8187         || rc!=SQLITE_OK
  8079   8188       );
  8080   8189       copyNodeContent(apNew[0], pParent, &rc);
  8081   8190       freePage(apNew[0], &rc);
................................................................................
  8348   8457   ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
  8349   8458   ** key values and pX->aMem can be used instead of pX->pKey to avoid having
  8350   8459   ** to decode the key.
  8351   8460   */
  8352   8461   int sqlite3BtreeInsert(
  8353   8462     BtCursor *pCur,                /* Insert data into the table of this cursor */
  8354   8463     const BtreePayload *pX,        /* Content of the row to be inserted */
  8355         -  int appendBias,                /* True if this is likely an append */
         8464  +  int flags,                     /* True if this is likely an append */
  8356   8465     int seekResult                 /* Result of prior MovetoUnpacked() call */
  8357   8466   ){
  8358   8467     int rc;
  8359   8468     int loc = seekResult;          /* -1: before desired location  +1: after */
  8360   8469     int szNew = 0;
  8361   8470     int idx;
  8362   8471     MemPage *pPage;
  8363   8472     Btree *p = pCur->pBtree;
  8364   8473     BtShared *pBt = p->pBt;
  8365   8474     unsigned char *oldCell;
  8366   8475     unsigned char *newCell = 0;
         8476  +
         8477  +  assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
  8367   8478   
  8368   8479     if( pCur->eState==CURSOR_FAULT ){
  8369   8480       assert( pCur->skipNext!=SQLITE_OK );
  8370   8481       return pCur->skipNext;
  8371   8482     }
  8372   8483   
  8373   8484     assert( cursorOwnsBtShared(pCur) );
................................................................................
  8401   8512   
  8402   8513     if( pCur->pKeyInfo==0 ){
  8403   8514       assert( pX->pKey==0 );
  8404   8515       /* If this is an insert into a table b-tree, invalidate any incrblob 
  8405   8516       ** cursors open on the row being replaced */
  8406   8517       invalidateIncrblobCursors(p, pX->nKey, 0);
  8407   8518   
         8519  +    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
         8520  +    ** to a row with the same key as the new entry being inserted.  */
         8521  +    assert( (flags & BTREE_SAVEPOSITION)==0 || 
         8522  +            ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
         8523  +
  8408   8524       /* If the cursor is currently on the last row and we are appending a
  8409   8525       ** new row onto the end, set the "loc" to avoid an unnecessary
  8410   8526       ** btreeMoveto() call */
  8411   8527       if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
  8412   8528         loc = 0;
  8413   8529       }else if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey>0
  8414   8530                  && pCur->info.nKey==pX->nKey-1 ){
  8415   8531         loc = -1;
  8416   8532       }else if( loc==0 ){
  8417         -      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
         8533  +      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
  8418   8534         if( rc ) return rc;
  8419   8535       }
  8420         -  }else if( loc==0 ){
         8536  +  }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
  8421   8537       if( pX->nMem ){
  8422   8538         UnpackedRecord r;
  8423   8539         r.pKeyInfo = pCur->pKeyInfo;
  8424   8540         r.aMem = pX->aMem;
  8425   8541         r.nField = pX->nMem;
  8426   8542         r.default_rc = 0;
  8427   8543         r.errCode = 0;
  8428   8544         r.r1 = 0;
  8429   8545         r.r2 = 0;
  8430   8546         r.eqSeen = 0;
  8431         -      rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
         8547  +      rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
  8432   8548       }else{
  8433         -      rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
         8549  +      rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
  8434   8550       }
  8435   8551       if( rc ) return rc;
  8436   8552     }
  8437   8553     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
  8438   8554   
  8439   8555     pPage = pCur->apPage[pCur->iPage];
  8440   8556     assert( pPage->intKey || pX->nKey>=0 );
................................................................................
  8514   8630   
  8515   8631       /* Must make sure nOverflow is reset to zero even if the balance()
  8516   8632       ** fails. Internal data structure corruption will result otherwise. 
  8517   8633       ** Also, set the cursor state to invalid. This stops saveCursorPosition()
  8518   8634       ** from trying to save the current position of the cursor.  */
  8519   8635       pCur->apPage[pCur->iPage]->nOverflow = 0;
  8520   8636       pCur->eState = CURSOR_INVALID;
         8637  +    if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
         8638  +      rc = moveToRoot(pCur);
         8639  +      if( pCur->pKeyInfo ){
         8640  +        assert( pCur->pKey==0 );
         8641  +        pCur->pKey = sqlite3Malloc( pX->nKey );
         8642  +        if( pCur->pKey==0 ){
         8643  +          rc = SQLITE_NOMEM;
         8644  +        }else{
         8645  +          memcpy(pCur->pKey, pX->pKey, pX->nKey);
         8646  +        }
         8647  +      }
         8648  +      pCur->eState = CURSOR_REQUIRESEEK;
         8649  +      pCur->nKey = pX->nKey;
         8650  +    }
  8521   8651     }
  8522   8652     assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
  8523   8653   
  8524   8654   end_insert:
  8525   8655     return rc;
  8526   8656   }
  8527   8657   

Changes to src/btree.h.

   245    245     int bias,
   246    246     int *pRes
   247    247   );
   248    248   int sqlite3BtreeCursorHasMoved(BtCursor*);
   249    249   int sqlite3BtreeCursorRestore(BtCursor*, int*);
   250    250   int sqlite3BtreeDelete(BtCursor*, u8 flags);
   251    251   
   252         -/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
          252  +/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
   253    253   #define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
   254    254   #define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
          255  +#define BTREE_APPEND       0x08  /* Insert is likely an append */
   255    256   
   256    257   /* An instance of the BtreePayload object describes the content of a single
   257    258   ** entry in either an index or table btree.
   258    259   **
   259    260   ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
   260    261   ** an arbitrary key and no data.  These btrees have pKey,nKey set to their
   261    262   ** key and pData,nData,nZero set to zero.
................................................................................
   278    279     struct Mem *aMem;       /* First of nMem value in the unpacked pKey */
   279    280     u16 nMem;               /* Number of aMem[] value.  Might be zero */
   280    281     int nData;              /* Size of pData.  0 if none. */
   281    282     int nZero;              /* Extra zero data appended after pData,nData */
   282    283   };
   283    284   
   284    285   int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
   285         -                       int bias, int seekResult);
          286  +                       int flags, int seekResult);
   286    287   int sqlite3BtreeFirst(BtCursor*, int *pRes);
   287    288   int sqlite3BtreeLast(BtCursor*, int *pRes);
   288    289   int sqlite3BtreeNext(BtCursor*, int *pRes);
   289    290   int sqlite3BtreeEof(BtCursor*);
   290    291   int sqlite3BtreePrevious(BtCursor*, int *pRes);
   291    292   i64 sqlite3BtreeIntegerKey(BtCursor*);
   292    293   int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
   293    294   const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
   294    295   u32 sqlite3BtreePayloadSize(BtCursor*);
   295    296   
   296    297   char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
   297    298   struct Pager *sqlite3BtreePager(Btree*);
          299  +i64 sqlite3BtreeRowCountEst(BtCursor*);
   298    300   
   299    301   #ifndef SQLITE_OMIT_INCRBLOB
   300    302   int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
   301    303   int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
   302    304   void sqlite3BtreeIncrblobCursor(BtCursor *);
   303    305   #endif
   304    306   void sqlite3BtreeClearCursor(BtCursor *);

Changes to src/btreeInt.h.

   699    699   /*
   700    700   ** get2byteAligned(), unlike get2byte(), requires that its argument point to a
   701    701   ** two-byte aligned address.  get2bytea() is only used for accessing the
   702    702   ** cell addresses in a btree header.
   703    703   */
   704    704   #if SQLITE_BYTEORDER==4321
   705    705   # define get2byteAligned(x)  (*(u16*)(x))
   706         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
   707         -    && GCC_VERSION>=4008000
          706  +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
   708    707   # define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
   709         -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \
   710         -    && defined(_MSC_VER) && _MSC_VER>=1300
          708  +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
   711    709   # define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
   712    710   #else
   713    711   # define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
   714    712   #endif

Changes to src/build.c.

  1113   1113   ** the column currently under construction.
  1114   1114   */
  1115   1115   void sqlite3AddNotNull(Parse *pParse, int onError){
  1116   1116     Table *p;
  1117   1117     p = pParse->pNewTable;
  1118   1118     if( p==0 || NEVER(p->nCol<1) ) return;
  1119   1119     p->aCol[p->nCol-1].notNull = (u8)onError;
         1120  +  p->tabFlags |= TF_HasNotNull;
  1120   1121   }
  1121   1122   
  1122   1123   /*
  1123   1124   ** Scan the column type name zType (length nType) and return the
  1124   1125   ** associated affinity type.
  1125   1126   **
  1126   1127   ** This routine does a case-independent search of zType for the 
................................................................................
  3451   3452   */
  3452   3453   void sqlite3DefaultRowEst(Index *pIdx){
  3453   3454     /*                10,  9,  8,  7,  6 */
  3454   3455     LogEst aVal[] = { 33, 32, 30, 28, 26 };
  3455   3456     LogEst *a = pIdx->aiRowLogEst;
  3456   3457     int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
  3457   3458     int i;
         3459  +
         3460  +  /* Indexes with default row estimates should not have stat1 data */
         3461  +  assert( !pIdx->hasStat1 );
  3458   3462   
  3459   3463     /* Set the first entry (number of rows in the index) to the estimated 
  3460   3464     ** number of rows in the table, or half the number of rows in the table
  3461   3465     ** for a partial index.   But do not let the estimate drop below 10. */
  3462   3466     a[0] = pIdx->pTable->nRowLogEst;
  3463   3467     if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
  3464   3468     if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );

Changes to src/ctime.c.

    53     53     "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
    54     54   #elif defined(__GNUC__) && defined(__VERSION__)
    55     55     "COMPILER=gcc-" __VERSION__,
    56     56   #endif
    57     57   #if SQLITE_COVERAGE_TEST
    58     58     "COVERAGE_TEST",
    59     59   #endif
    60         -#if SQLITE_DEBUG
           60  +#ifdef SQLITE_DEBUG
    61     61     "DEBUG",
    62     62   #endif
    63     63   #if SQLITE_DEFAULT_LOCKING_MODE
    64     64     "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
    65     65   #endif
    66     66   #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
    67     67     "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
    68     68   #endif
           69  +#if SQLITE_DEFAULT_SYNCHRONOUS
           70  +  "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
           71  +#endif
           72  +#if SQLITE_DEFAULT_WAL_SYNCHRONOUS
           73  +  "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
           74  +#endif
    69     75   #if SQLITE_DIRECT_OVERFLOW_READ
    70     76     "DIRECT_OVERFLOW_READ",
    71     77   #endif
    72     78   #if SQLITE_DISABLE_DIRSYNC
    73     79     "DISABLE_DIRSYNC",
    74     80   #endif
    75     81   #if SQLITE_DISABLE_LFS

Changes to src/date.c.

   739    739         /*
   740    740         **    start of TTTTT
   741    741         **
   742    742         ** Move the date backwards to the beginning of the current day,
   743    743         ** or month or year.
   744    744         */
   745    745         if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
          746  +      if( !p->validJD && !p->validYMD && !p->validHMS ) break;
   746    747         z += 9;
   747    748         computeYMD(p);
   748    749         p->validHMS = 1;
   749    750         p->h = p->m = 0;
   750    751         p->s = 0.0;
          752  +      p->rawS = 0;
   751    753         p->validTZ = 0;
   752    754         p->validJD = 0;
   753    755         if( sqlite3_stricmp(z,"month")==0 ){
   754    756           p->D = 1;
   755    757           rc = 0;
   756    758         }else if( sqlite3_stricmp(z,"year")==0 ){
   757         -        computeYMD(p);
   758    759           p->M = 1;
   759    760           p->D = 1;
   760    761           rc = 0;
   761    762         }else if( sqlite3_stricmp(z,"day")==0 ){
   762    763           rc = 0;
   763    764         }
   764    765         break;

Changes to src/delete.c.

   515    515         if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){
   516    516           pParse->isMultiWrite = 0;
   517    517         }
   518    518       }else
   519    519   #endif
   520    520       {
   521    521         int count = (pParse->nested==0);    /* True to count changes */
   522         -      int iIdxNoSeek = -1;
   523         -      if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){
   524         -        iIdxNoSeek = aiCurOnePass[1];
   525         -      }
   526    522         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
   527         -          iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek);
          523  +          iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
   528    524       }
   529    525     
   530    526       /* End of the loop over all rowids/primary-keys. */
   531    527       if( eOnePass!=ONEPASS_OFF ){
   532    528         sqlite3VdbeResolveLabel(v, addrBypass);
   533    529         sqlite3WhereEnd(pWInfo);
   534    530       }else if( pPk ){
................................................................................
   600    596   **   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
   601    597   **   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
   602    598   **   then this function must seek iDataCur to the entry identified by iPk
   603    599   **   and nPk before reading from it.
   604    600   **
   605    601   **   If eMode is ONEPASS_MULTI, then this call is being made as part
   606    602   **   of a ONEPASS delete that affects multiple rows. In this case, if 
   607         -**   iIdxNoSeek is a valid cursor number (>=0), then its position should
   608         -**   be preserved following the delete operation. Or, if iIdxNoSeek is not
   609         -**   a valid cursor number, the position of iDataCur should be preserved
   610         -**   instead.
          603  +**   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
          604  +**   iDataCur, then its position should be preserved following the delete
          605  +**   operation. Or, if iIdxNoSeek is not a valid cursor number, the
          606  +**   position of iDataCur should be preserved instead.
   611    607   **
   612    608   ** iIdxNoSeek:
   613         -**   If iIdxNoSeek is a valid cursor number (>=0), then it identifies an
   614         -**   index cursor (from within array of cursors starting at iIdxCur) that
   615         -**   already points to the index entry to be deleted.
          609  +**   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
          610  +**   then it identifies an index cursor (from within array of cursors
          611  +**   starting at iIdxCur) that already points to the index entry to be deleted.
          612  +**   Except, this optimization is disabled if there are BEFORE triggers since
          613  +**   the trigger body might have moved the cursor.
   616    614   */
   617    615   void sqlite3GenerateRowDelete(
   618    616     Parse *pParse,     /* Parsing context */
   619    617     Table *pTab,       /* Table containing the row to be deleted */
   620    618     Trigger *pTrigger, /* List of triggers to (potentially) fire */
   621    619     int iDataCur,      /* Cursor from which column data is extracted */
   622    620     int iIdxCur,       /* First index cursor */
................................................................................
   679    677       addrStart = sqlite3VdbeCurrentAddr(v);
   680    678       sqlite3CodeRowTrigger(pParse, pTrigger, 
   681    679           TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
   682    680       );
   683    681   
   684    682       /* If any BEFORE triggers were coded, then seek the cursor to the 
   685    683       ** row to be deleted again. It may be that the BEFORE triggers moved
   686         -    ** the cursor or of already deleted the row that the cursor was
          684  +    ** the cursor or already deleted the row that the cursor was
   687    685       ** pointing to.
          686  +    **
          687  +    ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
          688  +    ** may have moved that cursor.
   688    689       */
   689    690       if( addrStart<sqlite3VdbeCurrentAddr(v) ){
   690    691         sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
   691    692         VdbeCoverageIf(v, opSeek==OP_NotExists);
   692    693         VdbeCoverageIf(v, opSeek==OP_NotFound);
          694  +      testcase( iIdxNoSeek>=0 );
          695  +      iIdxNoSeek = -1;
   693    696       }
   694    697   
   695    698       /* Do FK processing. This call checks that any FK constraints that
   696    699       ** refer to this table (i.e. constraints attached to other tables) 
   697    700       ** are not violated by deleting this row.  */
   698    701       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
   699    702     }
................................................................................
   708    711     ** the update-hook is not invoked for rows removed by REPLACE, but the 
   709    712     ** pre-update-hook is.
   710    713     */ 
   711    714     if( pTab->pSelect==0 ){
   712    715       u8 p5 = 0;
   713    716       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
   714    717       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
   715         -    sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
          718  +    if( pParse->nested==0 ){
          719  +      sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
          720  +    }
   716    721       if( eMode!=ONEPASS_OFF ){
   717    722         sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
   718    723       }
   719         -    if( iIdxNoSeek>=0 ){
          724  +    if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
   720    725         sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
   721    726       }
   722    727       if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
   723    728       sqlite3VdbeChangeP5(v, p5);
   724    729     }
   725    730   
   726    731     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
................................................................................
   866    871       ** But we are getting ready to store this value back into an index, where
   867    872       ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
   868    873       ** opcode if it is present */
   869    874       sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
   870    875     }
   871    876     if( regOut ){
   872    877       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
          878  +    if( pIdx->pTable->pSelect ){
          879  +      const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
          880  +      sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
          881  +    }
   873    882     }
   874    883     sqlite3ReleaseTempRange(pParse, regBase, nCol);
   875    884     return regBase;
   876    885   }
   877    886   
   878    887   /*
   879    888   ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label

Changes to src/expr.c.

   227    227             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
   228    228     assert( pExpr->pLeft );
   229    229     aff = sqlite3ExprAffinity(pExpr->pLeft);
   230    230     if( pExpr->pRight ){
   231    231       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
   232    232     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   233    233       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
   234         -  }else if( NEVER(aff==0) ){
          234  +  }else if( aff==0 ){
   235    235       aff = SQLITE_AFF_BLOB;
   236    236     }
   237    237     return aff;
   238    238   }
   239    239   
   240    240   /*
   241    241   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
................................................................................
   963    963       x = (ynVar)(++pParse->nVar);
   964    964     }else{
   965    965       int doAdd = 0;
   966    966       if( z[0]=='?' ){
   967    967         /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
   968    968         ** use it as the variable number */
   969    969         i64 i;
   970         -      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
   971         -      x = (ynVar)i;
          970  +      int bOk;
          971  +      if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
          972  +        i = z[1]-'0';  /* The common case of ?N for a single digit N */
          973  +        bOk = 1;
          974  +      }else{
          975  +        bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
          976  +      }
   972    977         testcase( i==0 );
   973    978         testcase( i==1 );
   974    979         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
   975    980         testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
   976    981         if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   977    982           sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
   978    983               db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
   979    984           return;
   980    985         }
          986  +      x = (ynVar)i;
   981    987         if( x>pParse->nVar ){
   982    988           pParse->nVar = (int)x;
   983    989           doAdd = 1;
   984    990         }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
   985    991           doAdd = 1;
   986    992         }
   987    993       }else{
................................................................................
  1408   1414       struct IdList_item *pNewItem = &pNew->a[i];
  1409   1415       struct IdList_item *pOldItem = &p->a[i];
  1410   1416       pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  1411   1417       pNewItem->idx = pOldItem->idx;
  1412   1418     }
  1413   1419     return pNew;
  1414   1420   }
  1415         -Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1416         -  Select *pNew, *pPrior;
         1421  +Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
         1422  +  Select *pRet = 0;
         1423  +  Select *pNext = 0;
         1424  +  Select **pp = &pRet;
         1425  +  Select *p;
         1426  +
  1417   1427     assert( db!=0 );
  1418         -  if( p==0 ) return 0;
  1419         -  pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
  1420         -  if( pNew==0 ) return 0;
  1421         -  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
  1422         -  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
  1423         -  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
  1424         -  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
  1425         -  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
  1426         -  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
  1427         -  pNew->op = p->op;
  1428         -  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
  1429         -  if( pPrior ) pPrior->pNext = pNew;
  1430         -  pNew->pNext = 0;
  1431         -  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
  1432         -  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
  1433         -  pNew->iLimit = 0;
  1434         -  pNew->iOffset = 0;
  1435         -  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1436         -  pNew->addrOpenEphm[0] = -1;
  1437         -  pNew->addrOpenEphm[1] = -1;
  1438         -  pNew->nSelectRow = p->nSelectRow;
  1439         -  pNew->pWith = withDup(db, p->pWith);
  1440         -  sqlite3SelectSetName(pNew, p->zSelName);
  1441         -  return pNew;
         1428  +  for(p=pDup; p; p=p->pPrior){
         1429  +    Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
         1430  +    if( pNew==0 ) break;
         1431  +    pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
         1432  +    pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
         1433  +    pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
         1434  +    pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
         1435  +    pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
         1436  +    pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
         1437  +    pNew->op = p->op;
         1438  +    pNew->pNext = pNext;
         1439  +    pNew->pPrior = 0;
         1440  +    pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
         1441  +    pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
         1442  +    pNew->iLimit = 0;
         1443  +    pNew->iOffset = 0;
         1444  +    pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
         1445  +    pNew->addrOpenEphm[0] = -1;
         1446  +    pNew->addrOpenEphm[1] = -1;
         1447  +    pNew->nSelectRow = p->nSelectRow;
         1448  +    pNew->pWith = withDup(db, p->pWith);
         1449  +    sqlite3SelectSetName(pNew, p->zSelName);
         1450  +    *pp = pNew;
         1451  +    pp = &pNew->pPrior;
         1452  +    pNext = pNew;
         1453  +  }
         1454  +
         1455  +  return pRet;
  1442   1456   }
  1443   1457   #else
  1444   1458   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1445   1459     assert( p==0 );
  1446   1460     return 0;
  1447   1461   }
  1448   1462   #endif
................................................................................
  1842   1856   ** If the expression p codes a constant integer that is small enough
  1843   1857   ** to fit in a 32-bit integer, return 1 and put the value of the integer
  1844   1858   ** in *pValue.  If the expression is not an integer or if it is too big
  1845   1859   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
  1846   1860   */
  1847   1861   int sqlite3ExprIsInteger(Expr *p, int *pValue){
  1848   1862     int rc = 0;
         1863  +  if( p==0 ) return 0;  /* Can only happen following on OOM */
  1849   1864   
  1850   1865     /* If an expression is an integer literal that fits in a signed 32-bit
  1851   1866     ** integer, then the EP_IntValue flag will have already been set */
  1852   1867     assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
  1853   1868              || sqlite3GetInt32(p->u.zToken, &rc)==0 );
  1854   1869   
  1855   1870     if( p->flags & EP_IntValue ){
................................................................................
  2349   2364   static char *exprINAffinity(Parse *pParse, Expr *pExpr){
  2350   2365     Expr *pLeft = pExpr->pLeft;
  2351   2366     int nVal = sqlite3ExprVectorSize(pLeft);
  2352   2367     Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
  2353   2368     char *zRet;
  2354   2369   
  2355   2370     assert( pExpr->op==TK_IN );
  2356         -  zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
         2371  +  zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
  2357   2372     if( zRet ){
  2358   2373       int i;
  2359   2374       for(i=0; i<nVal; i++){
  2360   2375         Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
  2361   2376         char a = sqlite3ExprAffinity(pA);
  2362   2377         if( pSelect ){
  2363   2378           zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
................................................................................
  2514   2529           /* If the LHS and RHS of the IN operator do not match, that
  2515   2530           ** error will have been caught long before we reach this point. */
  2516   2531           if( ALWAYS(pEList->nExpr==nVal) ){
  2517   2532             SelectDest dest;
  2518   2533             int i;
  2519   2534             sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
  2520   2535             dest.zAffSdst = exprINAffinity(pParse, pExpr);
  2521         -          assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  2522   2536             pSelect->iLimit = 0;
  2523   2537             testcase( pSelect->selFlags & SF_Distinct );
  2524   2538             testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
  2525   2539             if( sqlite3Select(pParse, pSelect, &dest) ){
  2526   2540               sqlite3DbFree(pParse->db, dest.zAffSdst);
  2527   2541               sqlite3KeyInfoUnref(pKeyInfo);
  2528   2542               return 0;
................................................................................
  3254   3268   
  3255   3269   /*
  3256   3270   ** Clear all column cache entries.
  3257   3271   */
  3258   3272   void sqlite3ExprCacheClear(Parse *pParse){
  3259   3273     int i;
  3260   3274   
  3261         -#if SQLITE_DEBUG
         3275  +#ifdef SQLITE_DEBUG
  3262   3276     if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3263   3277       printf("CLEAR\n");
  3264   3278     }
  3265   3279   #endif
  3266   3280     for(i=0; i<pParse->nColCache; i++){
  3267   3281       if( pParse->aColCache[i].tempReg
  3268   3282        && pParse->nTempReg<ArraySize(pParse->aTempReg)
................................................................................
  4661   4675       Expr *pExprA = pA->a[i].pExpr;
  4662   4676       Expr *pExprB = pB->a[i].pExpr;
  4663   4677       if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
  4664   4678       if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
  4665   4679     }
  4666   4680     return 0;
  4667   4681   }
         4682  +
         4683  +/*
         4684  +** Like sqlite3ExprCompare() except COLLATE operators at the top-level
         4685  +** are ignored.
         4686  +*/
         4687  +int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
         4688  +  return sqlite3ExprCompare(
         4689  +             sqlite3ExprSkipCollate(pA),
         4690  +             sqlite3ExprSkipCollate(pB),
         4691  +             iTab);
         4692  +}
  4668   4693   
  4669   4694   /*
  4670   4695   ** Return true if we can prove the pE2 will always be true if pE1 is
  4671   4696   ** true.  Return false if we cannot complete the proof or if pE2 might
  4672   4697   ** be false.  Examples:
  4673   4698   **
  4674   4699   **     pE1: x==5       pE2: x==5             Result: true

Changes to src/func.c.

   200    200     if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
   201    201     nHaystack = sqlite3_value_bytes(argv[0]);
   202    202     nNeedle = sqlite3_value_bytes(argv[1]);
   203    203     if( nNeedle>0 ){
   204    204       if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
   205    205         zHaystack = sqlite3_value_blob(argv[0]);
   206    206         zNeedle = sqlite3_value_blob(argv[1]);
   207         -      assert( zNeedle!=0 );
   208         -      assert( zHaystack!=0 || nHaystack==0 );
   209    207         isText = 0;
   210    208       }else{
   211    209         zHaystack = sqlite3_value_text(argv[0]);
   212    210         zNeedle = sqlite3_value_text(argv[1]);
   213    211         isText = 1;
   214         -      if( zHaystack==0 || zNeedle==0 ) return;
   215    212       }
          213  +    if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
   216    214       while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
   217    215         N++;
   218    216         do{
   219    217           nHaystack--;
   220    218           zHaystack++;
   221    219         }while( isText && (zHaystack[0]&0xc0)==0x80 );
   222    220       }

Changes to src/insert.c.

  1523   1523           }
  1524   1524           sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
  1525   1525           VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
  1526   1526         }
  1527   1527       }
  1528   1528       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
  1529   1529       VdbeComment((v, "for %s", pIdx->zName));
         1530  +#ifdef SQLITE_ENABLE_NULL_TRIM
         1531  +    if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
         1532  +#endif
  1530   1533   
  1531   1534       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
  1532   1535       ** of a WITHOUT ROWID table and there has been no change the
  1533   1536       ** primary key, then no collision is possible.  The collision detection
  1534   1537       ** logic below can all be skipped. */
  1535   1538       if( isUpdate && pPk==pIdx && pkChng==0 ){
  1536   1539         sqlite3VdbeResolveLabel(v, addrUniqueOk);
................................................................................
  1647   1650           assert( onError==OE_Replace );
  1648   1651           sqlite3MultiWrite(pParse);
  1649   1652           if( db->flags&SQLITE_RecTriggers ){
  1650   1653             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1651   1654           }
  1652   1655           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
  1653   1656               regR, nPkField, 0, OE_Replace,
  1654         -            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1);
         1657  +            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
  1655   1658           seenReplace = 1;
  1656   1659           break;
  1657   1660         }
  1658   1661       }
  1659   1662       sqlite3VdbeResolveLabel(v, addrUniqueOk);
  1660   1663       if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
  1661   1664     }
................................................................................
  1663   1666       sqlite3VdbeGoto(v, ipkTop+1);
  1664   1667       sqlite3VdbeJumpHere(v, ipkBottom);
  1665   1668     }
  1666   1669     
  1667   1670     *pbMayReplace = seenReplace;
  1668   1671     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
  1669   1672   }
         1673  +
         1674  +#ifdef SQLITE_ENABLE_NULL_TRIM
         1675  +/*
         1676  +** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
         1677  +** to be the number of columns in table pTab that must not be NULL-trimmed.
         1678  +**
         1679  +** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
         1680  +*/
         1681  +void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
         1682  +  u16 i;
         1683  +
         1684  +  /* Records with omitted columns are only allowed for schema format
         1685  +  ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
         1686  +  if( pTab->pSchema->file_format<2 ) return;
         1687  +
         1688  +  for(i=pTab->nCol-1; i>0; i--){
         1689  +    if( pTab->aCol[i].pDflt!=0 ) break;
         1690  +    if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
         1691  +  }
         1692  +  sqlite3VdbeChangeP5(v, i+1);
         1693  +}
         1694  +#endif
  1670   1695   
  1671   1696   /*
  1672   1697   ** This routine generates code to finish the INSERT or UPDATE operation
  1673   1698   ** that was started by a prior call to sqlite3GenerateConstraintChecks.
  1674   1699   ** A consecutive range of registers starting at regNewData contains the
  1675   1700   ** rowid and the content to be inserted.
  1676   1701   **
................................................................................
  1680   1705   void sqlite3CompleteInsertion(
  1681   1706     Parse *pParse,      /* The parser context */
  1682   1707     Table *pTab,        /* the table into which we are inserting */
  1683   1708     int iDataCur,       /* Cursor of the canonical data source */
  1684   1709     int iIdxCur,        /* First index cursor */
  1685   1710     int regNewData,     /* Range of content */
  1686   1711     int *aRegIdx,       /* Register used by each index.  0 for unused indices */
  1687         -  int isUpdate,       /* True for UPDATE, False for INSERT */
         1712  +  int update_flags,   /* True for UPDATE, False for INSERT */
  1688   1713     int appendBias,     /* True if this is likely to be an append */
  1689   1714     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1690   1715   ){
  1691   1716     Vdbe *v;            /* Prepared statements under construction */
  1692   1717     Index *pIdx;        /* An index being inserted or updated */
  1693   1718     u8 pik_flags;       /* flag values passed to the btree insert */
  1694   1719     int regData;        /* Content registers (after the rowid) */
  1695   1720     int regRec;         /* Register holding assembled record for the table */
  1696   1721     int i;              /* Loop counter */
  1697   1722     u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
         1723  +
         1724  +  assert( update_flags==0
         1725  +       || update_flags==OPFLAG_ISUPDATE
         1726  +       || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
         1727  +  );
  1698   1728   
  1699   1729     v = sqlite3GetVdbe(pParse);
  1700   1730     assert( v!=0 );
  1701   1731     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1702   1732     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1703   1733       if( aRegIdx[i]==0 ) continue;
  1704   1734       bAffinityDone = 1;
  1705   1735       if( pIdx->pPartIdxWhere ){
  1706   1736         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
  1707   1737         VdbeCoverage(v);
  1708   1738       }
  1709         -    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
  1710         -                         aRegIdx[i]+1,
  1711         -                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
  1712         -    pik_flags = 0;
  1713         -    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
         1739  +    pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
  1714   1740       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
  1715   1741         assert( pParse->nested==0 );
  1716   1742         pik_flags |= OPFLAG_NCHANGE;
         1743  +      pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
         1744  +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
         1745  +      if( update_flags==0 ){
         1746  +        sqlite3VdbeAddOp4(v, OP_InsertInt, 
         1747  +            iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
         1748  +        );
         1749  +        sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
         1750  +      }
         1751  +#endif
  1717   1752       }
         1753  +    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
         1754  +                         aRegIdx[i]+1,
         1755  +                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
  1718   1756       sqlite3VdbeChangeP5(v, pik_flags);
  1719   1757     }
  1720   1758     if( !HasRowid(pTab) ) return;
  1721   1759     regData = regNewData + 1;
  1722   1760     regRec = sqlite3GetTempReg(pParse);
  1723   1761     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
         1762  +  sqlite3SetMakeRecordP5(v, pTab);
  1724   1763     if( !bAffinityDone ){
  1725   1764       sqlite3TableAffinity(v, pTab, 0);
  1726   1765       sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
  1727   1766     }
  1728   1767     if( pParse->nested ){
  1729   1768       pik_flags = 0;
  1730   1769     }else{
  1731   1770       pik_flags = OPFLAG_NCHANGE;
  1732         -    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
         1771  +    pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
  1733   1772     }
  1734   1773     if( appendBias ){
  1735   1774       pik_flags |= OPFLAG_APPEND;
  1736   1775     }
  1737   1776     if( useSeekResult ){
  1738   1777       pik_flags |= OPFLAG_USESEEKRESULT;
  1739   1778     }
................................................................................
  1936   1975       ** error if pSelect reads from a CTE named "xxx".  */
  1937   1976       return 0;
  1938   1977     }
  1939   1978     if( sqlite3TriggerList(pParse, pDest) ){
  1940   1979       return 0;   /* tab1 must not have triggers */
  1941   1980     }
  1942   1981   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1943         -  if( pDest->tabFlags & TF_Virtual ){
         1982  +  if( IsVirtual(pDest) ){
  1944   1983       return 0;   /* tab1 must not be a virtual table */
  1945   1984     }
  1946   1985   #endif
  1947   1986     if( onError==OE_Default ){
  1948   1987       if( pDest->iPKey>=0 ) onError = pDest->keyConf;
  1949   1988       if( onError==OE_Default ) onError = OE_Abort;
  1950   1989     }
................................................................................
  1998   2037     if( pSrc==pDest ){
  1999   2038       return 0;   /* tab1 and tab2 may not be the same table */
  2000   2039     }
  2001   2040     if( HasRowid(pDest)!=HasRowid(pSrc) ){
  2002   2041       return 0;   /* source and destination must both be WITHOUT ROWID or not */
  2003   2042     }
  2004   2043   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2005         -  if( pSrc->tabFlags & TF_Virtual ){
         2044  +  if( IsVirtual(pSrc) ){
  2006   2045       return 0;   /* tab2 must not be a virtual table */
  2007   2046     }
  2008   2047   #endif
  2009   2048     if( pSrc->pSelect ){
  2010   2049       return 0;   /* tab2 may not be a view */
  2011   2050     }
  2012   2051     if( pDest->nCol!=pSrc->nCol ){
................................................................................
  2134   2173         autoIncStep(pParse, regAutoinc, regRowid);
  2135   2174       }else if( pDest->pIndex==0 ){
  2136   2175         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
  2137   2176       }else{
  2138   2177         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  2139   2178         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
  2140   2179       }
  2141         -    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
         2180  +    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
  2142   2181       if( db->flags & SQLITE_Vacuum ){
  2143   2182         sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
  2144   2183         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
  2145   2184                              OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
  2146   2185       }else{
  2147   2186         insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
  2148   2187       }
................................................................................
  2166   2205       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
  2167   2206       VdbeComment((v, "%s", pSrcIdx->zName));
  2168   2207       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
  2169   2208       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
  2170   2209       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
  2171   2210       VdbeComment((v, "%s", pDestIdx->zName));
  2172   2211       addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
  2173         -    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
         2212  +    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
  2174   2213       if( db->flags & SQLITE_Vacuum ){
  2175   2214         /* This INSERT command is part of a VACUUM operation, which guarantees
  2176   2215         ** that the destination table is empty. If all indexed columns use
  2177   2216         ** collation sequence BINARY, then it can also be assumed that the
  2178   2217         ** index will be populated by inserting keys in strictly sorted 
  2179   2218         ** order. In this case, instead of seeking within the b-tree as part
  2180   2219         ** of every OP_IdxInsert opcode, an OP_Last is added before the
................................................................................
  2184   2223         ** If any of the indexed columns use a collation sequence other than
  2185   2224         ** BINARY, this optimization is disabled. This is because the user 
  2186   2225         ** might change the definition of a collation sequence and then run
  2187   2226         ** a VACUUM command. In that case keys may not be written in strictly
  2188   2227         ** sorted order.  */
  2189   2228         for(i=0; i<pSrcIdx->nColumn; i++){
  2190   2229           const char *zColl = pSrcIdx->azColl[i];
  2191         -        assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0
  2192         -                    || sqlite3StrBINARY==zColl );
  2193   2230           if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
  2194   2231         }
  2195   2232         if( i==pSrcIdx->nColumn ){
  2196   2233           idxInsFlags = OPFLAG_USESEEKRESULT;
  2197   2234           sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
  2198   2235         }
  2199   2236       }

Changes to src/legacy.c.

    69     69         rc = sqlite3_step(pStmt);
    70     70   
    71     71         /* Invoke the callback function if required */
    72     72         if( xCallback && (SQLITE_ROW==rc || 
    73     73             (SQLITE_DONE==rc && !callbackIsInit
    74     74                              && db->flags&SQLITE_NullCallback)) ){
    75     75           if( !callbackIsInit ){
    76         -          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
           76  +          azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
    77     77             if( azCols==0 ){
    78     78               goto exec_out;
    79     79             }
    80     80             for(i=0; i<nCol; i++){
    81     81               azCols[i] = (char *)sqlite3_column_name(pStmt, i);
    82     82               /* sqlite3VdbeSetColName() installs column names as UTF8
    83     83               ** strings so there is no way for sqlite3_column_name() to fail. */
................................................................................
    90     90             for(i=0; i<nCol; i++){
    91     91               azVals[i] = (char *)sqlite3_column_text(pStmt, i);
    92     92               if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
    93     93                 sqlite3OomFault(db);
    94     94                 goto exec_out;
    95     95               }
    96     96             }
           97  +          azVals[i] = 0;
    97     98           }
    98     99           if( xCallback(pArg, nCol, azVals, azCols) ){
    99    100             /* EVIDENCE-OF: R-38229-40159 If the callback function to
   100    101             ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
   101    102             ** return SQLITE_ABORT. */
   102    103             rc = SQLITE_ABORT;
   103    104             sqlite3VdbeFinalize((Vdbe *)pStmt);

Changes to src/loadext.c.

    14     14   */
    15     15   
    16     16   #ifndef SQLITE_CORE
    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         -#include <string.h>
    22     21   
    23     22   #ifndef SQLITE_OMIT_LOAD_EXTENSION
    24     23   /*
    25     24   ** Some API routines are omitted when various features are
    26     25   ** excluded from a build of SQLite.  Substitute a NULL pointer
    27     26   ** for any missing APIs.
    28     27   */

Changes to src/main.c.

   916    916     if( !sqlite3SafetyCheckOk(db) ){
   917    917       (void)SQLITE_MISUSE_BKPT;
   918    918       return 0;
   919    919     }
   920    920   #endif
   921    921     return db->lastRowid;
   922    922   }
          923  +
          924  +/*
          925  +** Set the value returned by the sqlite3_last_insert_rowid() API function.
          926  +*/
          927  +void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
          928  +#ifdef SQLITE_ENABLE_API_ARMOR
          929  +  if( !sqlite3SafetyCheckOk(db) ){
          930  +    (void)SQLITE_MISUSE_BKPT;
          931  +    return;
          932  +  }
          933  +#endif
          934  +  sqlite3_mutex_enter(db->mutex);
          935  +  db->lastRowid = iRowid;
          936  +  sqlite3_mutex_leave(db->mutex);
          937  +}
   923    938   
   924    939   /*
   925    940   ** Return the number of changes in the most recent call to sqlite3_exec().
   926    941   */
   927    942   int sqlite3_changes(sqlite3 *db){
   928    943   #ifdef SQLITE_ENABLE_API_ARMOR
   929    944     if( !sqlite3SafetyCheckOk(db) ){

Changes to src/malloc.c.

   213    213     sqlite3_mutex_enter(mem0.mutex);
   214    214   }
   215    215   
   216    216   /*
   217    217   ** Do a memory allocation with statistics and alarms.  Assume the
   218    218   ** lock is already held.
   219    219   */
   220         -static int mallocWithAlarm(int n, void **pp){
   221         -  int nFull;
          220  +static void mallocWithAlarm(int n, void **pp){
   222    221     void *p;
          222  +  int nFull;
   223    223     assert( sqlite3_mutex_held(mem0.mutex) );
          224  +  assert( n>0 );
          225  +
          226  +  /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
          227  +  ** implementation of malloc_good_size(), which must be called in debug
          228  +  ** mode and specifically when the DMD "Dark Matter Detector" is enabled
          229  +  ** or else a crash results.  Hence, do not attempt to optimize out the
          230  +  ** following xRoundup() call. */
   224    231     nFull = sqlite3GlobalConfig.m.xRoundup(n);
          232  +
          233  +#ifdef SQLITE_MAX_MEMORY
          234  +  if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
          235  +    *pp = 0;
          236  +    return;
          237  +  }
          238  +#endif
          239  +
   225    240     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
   226    241     if( mem0.alarmThreshold>0 ){
   227    242       sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   228    243       if( nUsed >= mem0.alarmThreshold - nFull ){
   229    244         mem0.nearlyFull = 1;
   230    245         sqlite3MallocAlarm(nFull);
   231    246       }else{
................................................................................
   241    256   #endif
   242    257     if( p ){
   243    258       nFull = sqlite3MallocSize(p);
   244    259       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
   245    260       sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
   246    261     }
   247    262     *pp = p;
   248         -  return nFull;
   249    263   }
   250    264   
   251    265   /*
   252    266   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   253    267   ** assumes the memory subsystem has already been initialized.
   254    268   */
   255    269   void *sqlite3Malloc(u64 n){
................................................................................
   407    421   int sqlite3MallocSize(void *p){
   408    422     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   409    423     return sqlite3GlobalConfig.m.xSize(p);
   410    424   }
   411    425   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   412    426     assert( p!=0 );
   413    427     if( db==0 || !isLookaside(db,p) ){
   414         -#if SQLITE_DEBUG
          428  +#ifdef SQLITE_DEBUG
   415    429       if( db==0 ){
   416    430         assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
   417    431         assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   418    432       }else{
   419    433         assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   420    434         assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   421    435       }
................................................................................
   468    482     if( db ){
   469    483       if( db->pnBytesFreed ){
   470    484         measureAllocationSize(db, p);
   471    485         return;
   472    486       }
   473    487       if( isLookaside(db, p) ){
   474    488         LookasideSlot *pBuf = (LookasideSlot*)p;
   475         -#if SQLITE_DEBUG
          489  +#ifdef SQLITE_DEBUG
   476    490         /* Trash all content in the buffer being freed */
   477    491         memset(p, 0xaa, db->lookaside.sz);
   478    492   #endif
   479    493         pBuf->pNext = db->lookaside.pFree;
   480    494         db->lookaside.pFree = pBuf;
   481    495         db->lookaside.nOut--;
   482    496         return;

Changes to src/mem1.c.

    53     53   
    54     54   /*
    55     55   ** Use the zone allocator available on apple products unless the
    56     56   ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
    57     57   */
    58     58   #include <sys/sysctl.h>
    59     59   #include <malloc/malloc.h>
           60  +#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
    60     61   #include <libkern/OSAtomic.h>
           62  +#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
    61     63   static malloc_zone_t* _sqliteZone_;
    62     64   #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
    63     65   #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
    64     66   #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
    65     67   #define SQLITE_MALLOCSIZE(x) \
    66     68           (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
    67     69   
................................................................................
   121    123   **
   122    124   ** For this low-level routine, we are guaranteed that nByte>0 because
   123    125   ** cases of nByte<=0 will be intercepted and dealt with by higher level
   124    126   ** routines.
   125    127   */
   126    128   static void *sqlite3MemMalloc(int nByte){
   127    129   #ifdef SQLITE_MALLOCSIZE
   128         -  void *p = SQLITE_MALLOC( nByte );
          130  +  void *p;
          131  +  testcase( ROUND8(nByte)==nByte );
          132  +  p = SQLITE_MALLOC( nByte );
   129    133     if( p==0 ){
   130    134       testcase( sqlite3GlobalConfig.xLog!=0 );
   131    135       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   132    136     }
   133    137     return p;
   134    138   #else
   135    139     sqlite3_int64 *p;
   136    140     assert( nByte>0 );
   137         -  nByte = ROUND8(nByte);
          141  +  testcase( ROUND8(nByte)!=nByte );
   138    142     p = SQLITE_MALLOC( nByte+8 );
   139    143     if( p ){
   140    144       p[0] = nByte;
   141    145       p++;
   142    146     }else{
   143    147       testcase( sqlite3GlobalConfig.xLog!=0 );
   144    148       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
................................................................................
   244    248     sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
   245    249     if( cpuCount>1 ){
   246    250       /* defer MT decisions to system malloc */
   247    251       _sqliteZone_ = malloc_default_zone();
   248    252     }else{
   249    253       /* only 1 core, use our own zone to contention over global locks, 
   250    254       ** e.g. we have our own dedicated locks */
   251         -    bool success;
   252         -    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
   253         -    malloc_set_zone_name(newzone, "Sqlite_Heap");
   254         -    do{
   255         -      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
   256         -                                 (void * volatile *)&_sqliteZone_);
   257         -    }while(!_sqliteZone_);
   258         -    if( !success ){
   259         -      /* somebody registered a zone first */
   260         -      malloc_destroy_zone(newzone);
   261         -    }
          255  +    _sqliteZone_ = malloc_create_zone(4096, 0);
          256  +    malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
   262    257     }
   263         -#endif
          258  +#endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
   264    259     UNUSED_PARAMETER(NotUsed);
   265    260     return SQLITE_OK;
   266    261   }
   267    262   
   268    263   /*
   269    264   ** Deinitialize this module.
   270    265   */

Changes to src/mutex_w32.c.

    83     83   ** compiled without mutexes (SQLITE_THREADSAFE=0).
    84     84   */
    85     85   void sqlite3MemoryBarrier(void){
    86     86   #if defined(SQLITE_MEMORY_BARRIER)
    87     87     SQLITE_MEMORY_BARRIER;
    88     88   #elif defined(__GNUC__)
    89     89     __sync_synchronize();
    90         -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \
    91         -      defined(_MSC_VER) && _MSC_VER>=1300
           90  +#elif MSVC_VERSION>=1300
    92     91     _ReadWriteBarrier();
    93     92   #elif defined(MemoryBarrier)
    94     93     MemoryBarrier();
    95     94   #endif
    96     95   }
    97     96   
    98     97   /*
................................................................................
   295    294     assert( winMutex_isInit==1 );
   296    295     EnterCriticalSection(&p->mutex);
   297    296   #ifdef SQLITE_DEBUG
   298    297     assert( p->nRef>0 || p->owner==0 );
   299    298     p->owner = tid;
   300    299     p->nRef++;
   301    300     if( p->trace ){
   302         -    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
   303         -             tid, p, p->trace, p->nRef));
          301  +    OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
          302  +             tid, p->id, p, p->trace, p->nRef));
   304    303     }
   305    304   #endif
   306    305   }
   307    306   
   308    307   static int winMutexTry(sqlite3_mutex *p){
   309    308   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
   310    309     DWORD tid = GetCurrentThreadId();
................................................................................
   338    337       rc = SQLITE_OK;
   339    338     }
   340    339   #else
   341    340     UNUSED_PARAMETER(p);
   342    341   #endif
   343    342   #ifdef SQLITE_DEBUG
   344    343     if( p->trace ){
   345         -    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
   346         -             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
          344  +    OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
          345  +             tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
   347    346     }
   348    347   #endif
   349    348     return rc;
   350    349   }
   351    350   
   352    351   /*
   353    352   ** The sqlite3_mutex_leave() routine exits a mutex that was
................................................................................
   367    366     if( p->nRef==0 ) p->owner = 0;
   368    367     assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   369    368   #endif
   370    369     assert( winMutex_isInit==1 );
   371    370     LeaveCriticalSection(&p->mutex);
   372    371   #ifdef SQLITE_DEBUG
   373    372     if( p->trace ){
   374         -    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
   375         -             tid, p, p->trace, p->nRef));
          373  +    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
          374  +             tid, p->id, p, p->trace, p->nRef));
   376    375     }
   377    376   #endif
   378    377   }
   379    378   
   380    379   sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   381    380     static const sqlite3_mutex_methods sMutex = {
   382    381       winMutexInit,

Changes to src/os_win.c.

   348    348    *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
   349    349    *          function), all data that was allocated using the isolated heap will
   350    350    *          be freed immediately and any attempt to access any of that freed
   351    351    *          data will almost certainly result in an immediate access violation.
   352    352    ******************************************************************************
   353    353    */
   354    354   #ifndef SQLITE_WIN32_HEAP_CREATE
   355         -#  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
          355  +#  define SQLITE_WIN32_HEAP_CREATE        (TRUE)
          356  +#endif
          357  +
          358  +/*
          359  + * This is the maximum possible initial size of the Win32-specific heap, in
          360  + * bytes.
          361  + */
          362  +#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
          363  +#  define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
          364  +#endif
          365  +
          366  +/*
          367  + * This is the extra space for the initial size of the Win32-specific heap,
          368  + * in bytes.  This value may be zero.
          369  + */
          370  +#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
          371  +#  define SQLITE_WIN32_HEAP_INIT_EXTRA  (4194304)
          372  +#endif
          373  +
          374  +/*
          375  + * Calculate the maximum legal cache size, in pages, based on the maximum
          376  + * possible initial heap size and the default page size, setting aside the
          377  + * needed extra space.
          378  + */
          379  +#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
          380  +#  define SQLITE_WIN32_MAX_CACHE_SIZE   (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
          381  +                                          (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
          382  +                                         (SQLITE_DEFAULT_PAGE_SIZE))
   356    383   #endif
   357    384   
   358    385   /*
   359    386    * This is cache size used in the calculation of the initial size of the
   360    387    * Win32-specific heap.  It cannot be negative.
   361    388    */
   362    389   #ifndef SQLITE_WIN32_CACHE_SIZE
   363    390   #  if SQLITE_DEFAULT_CACHE_SIZE>=0
   364         -#    define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
          391  +#    define SQLITE_WIN32_CACHE_SIZE     (SQLITE_DEFAULT_CACHE_SIZE)
   365    392   #  else
   366         -#    define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
          393  +#    define SQLITE_WIN32_CACHE_SIZE     (-(SQLITE_DEFAULT_CACHE_SIZE))
   367    394   #  endif
   368    395   #endif
   369    396   
          397  +/*
          398  + * Make sure that the calculated cache size, in pages, cannot cause the
          399  + * initial size of the Win32-specific heap to exceed the maximum amount
          400  + * of memory that can be specified in the call to HeapCreate.
          401  + */
          402  +#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
          403  +#  undef SQLITE_WIN32_CACHE_SIZE
          404  +#  define SQLITE_WIN32_CACHE_SIZE       (2000)
          405  +#endif
          406  +
   370    407   /*
   371    408    * The initial size of the Win32-specific heap.  This value may be zero.
   372    409    */
   373    410   #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
   374         -#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
   375         -                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
          411  +#  define SQLITE_WIN32_HEAP_INIT_SIZE   ((SQLITE_WIN32_CACHE_SIZE) * \
          412  +                                         (SQLITE_DEFAULT_PAGE_SIZE) + \
          413  +                                         (SQLITE_WIN32_HEAP_INIT_EXTRA))
   376    414   #endif
   377    415   
   378    416   /*
   379    417    * The maximum size of the Win32-specific heap.  This value may be zero.
   380    418    */
   381    419   #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
   382         -#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
          420  +#  define SQLITE_WIN32_HEAP_MAX_SIZE    (0)
   383    421   #endif
   384    422   
   385    423   /*
   386    424    * The extra flags to use in calls to the Win32 heap APIs.  This value may be
   387    425    * zero for the default behavior.
   388    426    */
   389    427   #ifndef SQLITE_WIN32_HEAP_FLAGS
   390         -#  define SQLITE_WIN32_HEAP_FLAGS     (0)
          428  +#  define SQLITE_WIN32_HEAP_FLAGS       (0)
   391    429   #endif
   392    430   
   393    431   
   394    432   /*
   395    433   ** The winMemData structure stores information required by the Win32-specific
   396    434   ** sqlite3_mem_methods implementation.
   397    435   */

Changes to src/pager.c.

   813    813   ** instead of
   814    814   **
   815    815   **   if( pPager->jfd->pMethods ){ ...
   816    816   */
   817    817   #define isOpen(pFd) ((pFd)->pMethods!=0)
   818    818   
   819    819   /*
   820         -** Return true if this pager uses a write-ahead log instead of the usual
   821         -** rollback journal. Otherwise false.
          820  +** Return true if this pager uses a write-ahead log to read page pgno.
          821  +** Return false if the pager reads pgno directly from the database.
   822    822   */
          823  +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
          824  +int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
          825  +  u32 iRead = 0;
          826  +  int rc;
          827  +  if( pPager->pWal==0 ) return 0;
          828  +  rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
          829  +  return rc || iRead;
          830  +}
          831  +#endif
   823    832   #ifndef SQLITE_OMIT_WAL
   824         -int sqlite3PagerUseWal(Pager *pPager){
   825         -  return (pPager->pWal!=0);
   826         -}
   827         -# define pagerUseWal(x) sqlite3PagerUseWal(x)
          833  +# define pagerUseWal(x) ((x)->pWal!=0)
   828    834   #else
   829    835   # define pagerUseWal(x) 0
   830    836   # define pagerRollbackWal(x) 0
   831    837   # define pagerWalFrames(v,w,x,y) 0
   832    838   # define pagerOpenWalIfPresent(z) SQLITE_OK
   833    839   # define pagerBeginReadTransaction(z) SQLITE_OK
   834    840   #endif

Changes to src/pager.h.

   175    175   
   176    176   #ifndef SQLITE_OMIT_WAL
   177    177     int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
   178    178     int sqlite3PagerWalSupported(Pager *pPager);
   179    179     int sqlite3PagerWalCallback(Pager *pPager);
   180    180     int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
   181    181     int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
   182         -  int sqlite3PagerUseWal(Pager *pPager);
          182  +# ifdef SQLITE_DIRECT_OVERFLOW_READ
          183  +  int sqlite3PagerUseWal(Pager *pPager, Pgno);
          184  +# endif
   183    185   # ifdef SQLITE_ENABLE_SNAPSHOT
   184    186     int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
   185    187     int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
   186    188     int sqlite3PagerSnapshotRecover(Pager *pPager);
   187    189   # endif
   188    190   #else
   189         -# define sqlite3PagerUseWal(x) 0
          191  +# define sqlite3PagerUseWal(x,y) 0
   190    192   #endif
   191    193   
   192    194   #ifdef SQLITE_ENABLE_ZIPVFS
   193    195     int sqlite3PagerWalFramesize(Pager *pPager);
   194    196   #endif
   195    197   
   196    198   /* Functions used to query pager state and configuration. */

Changes to src/parse.y.

    61     61   
    62     62   /*
    63     63   ** Indicate that sqlite3ParserFree() will never be called with a null
    64     64   ** pointer.
    65     65   */
    66     66   #define YYPARSEFREENEVERNULL 1
    67     67   
           68  +/*
           69  +** In the amalgamation, the parse.c file generated by lemon and the
           70  +** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
           71  +** has access to the the size of the yyParser object and so the parser
           72  +** engine can be allocated from stack.  In that case, only the
           73  +** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
           74  +** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
           75  +** omitted.
           76  +*/
           77  +#ifdef SQLITE_AMALGAMATION
           78  +# define sqlite3Parser_ENGINEALWAYSONSTACK 1
           79  +#endif
           80  +
    68     81   /*
    69     82   ** Alternative datatype for the argument to the malloc() routine passed
    70     83   ** into sqlite3ParserAlloc().  The default is size_t.
    71     84   */
    72     85   #define YYMALLOCARGTYPE  u64
    73     86   
    74     87   /*
................................................................................
   898    911   }
   899    912   term(A) ::= FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   900    913   term(A) ::= STRING(X).     {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   901    914   term(A) ::= INTEGER(X). {
   902    915     A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
   903    916     A.zStart = X.z;
   904    917     A.zEnd = X.z + X.n;
   905         -  if( A.pExpr ) A.pExpr->flags |= EP_Leaf;
          918  +  if( A.pExpr ) A.pExpr->flags |= EP_Leaf|EP_Resolved;
   906    919   }
   907    920   expr(A) ::= VARIABLE(X).     {
   908    921     if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
   909    922       u32 n = X.n;
   910    923       spanExpr(&A, pParse, TK_VARIABLE, X);
   911    924       sqlite3ExprAssignVarNumber(pParse, A.pExpr, n);
   912    925     }else{
................................................................................
   999   1012                                           {spanBinaryExpr(pParse,@OP,&A,&Y);}
  1000   1013   expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).
  1001   1014                                           {spanBinaryExpr(pParse,@OP,&A,&Y);}
  1002   1015   expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y).
  1003   1016                                           {spanBinaryExpr(pParse,@OP,&A,&Y);}
  1004   1017   expr(A) ::= expr(A) CONCAT(OP) expr(Y). {spanBinaryExpr(pParse,@OP,&A,&Y);}
  1005   1018   %type likeop {Token}
  1006         -likeop(A) ::= LIKE_KW|MATCH(X).     {A=X;/*A-overwrites-X*/}
         1019  +likeop(A) ::= LIKE_KW|MATCH(A).
  1007   1020   likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/}
  1008   1021   expr(A) ::= expr(A) likeop(OP) expr(Y).  [LIKE_KW]  {
  1009   1022     ExprList *pList;
  1010   1023     int bNot = OP.n & 0x80000000;
  1011   1024     OP.n &= 0x7fffffff;
  1012   1025     pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
  1013   1026     pList = sqlite3ExprListAppend(pParse,pList, A.pExpr);

Changes to src/pcache.c.

   100    100   ** Return false if any invariant is violated.
   101    101   **
   102    102   ** This routine is for use inside of assert() statements only.  For
   103    103   ** example:
   104    104   **
   105    105   **          assert( sqlite3PcachePageSanity(pPg) );
   106    106   */
   107         -#if SQLITE_DEBUG
          107  +#ifdef SQLITE_DEBUG
   108    108   int sqlite3PcachePageSanity(PgHdr *pPg){
   109    109     PCache *pCache;
   110    110     assert( pPg!=0 );
   111    111     assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */
   112    112     pCache = pPg->pCache;
   113    113     assert( pCache!=0 );      /* Every page has an associated PCache */
   114    114     if( pPg->flags & PGHDR_CLEAN ){

Changes to src/pragma.c.

   275    275     return azModeName[eMode];
   276    276   }
   277    277   
   278    278   /*
   279    279   ** Locate a pragma in the aPragmaName[] array.
   280    280   */
   281    281   static const PragmaName *pragmaLocate(const char *zName){
   282         -  int upr, lwr, mid, rc;
          282  +  int upr, lwr, mid = 0, rc;
   283    283     lwr = 0;
   284    284     upr = ArraySize(aPragmaName)-1;
   285    285     while( lwr<=upr ){
   286    286       mid = (lwr+upr)/2;
   287    287       rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
   288    288       if( rc==0 ) break;
   289    289       if( rc<0 ){
................................................................................
   290    290         upr = mid - 1;
   291    291       }else{
   292    292         lwr = mid + 1;
   293    293       }
   294    294     }
   295    295     return lwr>upr ? 0 : &aPragmaName[mid];
   296    296   }
          297  +
          298  +/*
          299  +** Helper subroutine for PRAGMA integrity_check:
          300  +**
          301  +** Generate code to output a single-column result row with the result
          302  +** held in register regResult.  Decrement the result count and halt if
          303  +** the maximum number of result rows have been issued.
          304  +*/
          305  +static int integrityCheckResultRow(Vdbe *v, int regResult){
          306  +  int addr;
          307  +  sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
          308  +  addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
          309  +  VdbeCoverage(v);
          310  +  sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
          311  +  return addr;
          312  +}
   297    313   
   298    314   /*
   299    315   ** Process a pragma statement.  
   300    316   **
   301    317   ** Pragmas are of this form:
   302    318   **
   303    319   **      PRAGMA [schema.]id [= value]
................................................................................
   995   1011     case PragTyp_SYNCHRONOUS: {
   996   1012       if( !zRight ){
   997   1013         returnSingleInt(v, pDb->safety_level-1);
   998   1014       }else{
   999   1015         if( !db->autoCommit ){
  1000   1016           sqlite3ErrorMsg(pParse, 
  1001   1017               "Safety level may not be changed inside a transaction");
  1002         -      }else{
         1018  +      }else if( iDb!=1 ){
  1003   1019           int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
  1004   1020           if( iLevel==0 ) iLevel = 1;
  1005   1021           pDb->safety_level = iLevel;
  1006   1022           pDb->bSyncSet = 1;
  1007   1023           setAllPagerFlags(db);
  1008   1024         }
  1009   1025       }
................................................................................
  1094   1110                  k);
  1095   1111           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
  1096   1112         }
  1097   1113       }
  1098   1114     }
  1099   1115     break;
  1100   1116   
         1117  +#ifdef SQLITE_DEBUG
  1101   1118     case PragTyp_STATS: {
  1102   1119       Index *pIdx;
  1103   1120       HashElem *i;
  1104         -    pParse->nMem = 4;
         1121  +    pParse->nMem = 5;
  1105   1122       sqlite3CodeVerifySchema(pParse, iDb);
  1106   1123       for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
  1107   1124         Table *pTab = sqliteHashData(i);
  1108         -      sqlite3VdbeMultiLoad(v, 1, "ssii",
         1125  +      sqlite3VdbeMultiLoad(v, 1, "ssiii",
  1109   1126              pTab->zName,
  1110   1127              0,
  1111   1128              pTab->szTabRow,
  1112         -           pTab->nRowLogEst);
  1113         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
         1129  +           pTab->nRowLogEst,
         1130  +           pTab->tabFlags);
         1131  +      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
  1114   1132         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1115         -        sqlite3VdbeMultiLoad(v, 2, "sii",
         1133  +        sqlite3VdbeMultiLoad(v, 2, "siii",
  1116   1134              pIdx->zName,
  1117   1135              pIdx->szIdxRow,
  1118         -           pIdx->aiRowLogEst[0]);
  1119         -        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
         1136  +           pIdx->aiRowLogEst[0],
         1137  +           pIdx->hasStat1);
         1138  +        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
  1120   1139         }
  1121   1140       }
  1122   1141     }
  1123   1142     break;
         1143  +#endif
  1124   1144   
  1125   1145     case PragTyp_INDEX_INFO: if( zRight ){
  1126   1146       Index *pIdx;
  1127   1147       Table *pTab;
  1128   1148       pIdx = sqlite3FindIndex(db, zRight, zDb);
  1129   1149       if( pIdx ){
  1130   1150         int i;
................................................................................
  1373   1393     break;
  1374   1394   
  1375   1395   #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
  1376   1396   # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
  1377   1397   #endif
  1378   1398   
  1379   1399   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  1380         -  /* Pragma "quick_check" is reduced version of 
         1400  +  /*    PRAGMA integrity_check
         1401  +  **    PRAGMA integrity_check(N)
         1402  +  **    PRAGMA quick_check
         1403  +  **    PRAGMA quick_check(N)
         1404  +  **
         1405  +  ** Verify the integrity of the database.
         1406  +  **
         1407  +  ** The "quick_check" is reduced version of 
  1381   1408     ** integrity_check designed to detect most database corruption
  1382         -  ** without most of the overhead of a full integrity-check.
         1409  +  ** without the overhead of cross-checking indexes.  Quick_check
         1410  +  ** is linear time wherease integrity_check is O(NlogN).
  1383   1411     */
  1384   1412     case PragTyp_INTEGRITY_CHECK: {
  1385   1413       int i, j, addr, mxErr;
  1386   1414   
  1387   1415       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
  1388   1416   
  1389   1417       /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
................................................................................
  1406   1434       mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  1407   1435       if( zRight ){
  1408   1436         sqlite3GetInt32(zRight, &mxErr);
  1409   1437         if( mxErr<=0 ){
  1410   1438           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  1411   1439         }
  1412   1440       }
  1413         -    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
         1441  +    sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
  1414   1442   
  1415   1443       /* Do an integrity check on each database file */
  1416   1444       for(i=0; i<db->nDb; i++){
  1417   1445         HashElem *x;
  1418   1446         Hash *pTbls;
  1419   1447         int *aRoot;
  1420   1448         int cnt = 0;
................................................................................
  1421   1449         int mxIdx = 0;
  1422   1450         int nIdx;
  1423   1451   
  1424   1452         if( OMIT_TEMPDB && i==1 ) continue;
  1425   1453         if( iDb>=0 && i!=iDb ) continue;
  1426   1454   
  1427   1455         sqlite3CodeVerifySchema(pParse, i);
  1428         -      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
  1429         -      VdbeCoverage(v);
  1430         -      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1431         -      sqlite3VdbeJumpHere(v, addr);
  1432   1456   
  1433   1457         /* Do an integrity check of the B-Tree
  1434   1458         **
  1435   1459         ** Begin by finding the root pages numbers
  1436   1460         ** for all tables and indices in the database.
  1437   1461         */
  1438   1462         assert( sqlite3SchemaMutexHeld(db, i, 0) );
................................................................................
  1464   1488         sqlite3VdbeChangeP5(v, (u8)i);
  1465   1489         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
  1466   1490         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1467   1491            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
  1468   1492            P4_DYNAMIC);
  1469   1493         sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
  1470   1494         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1471         -      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
         1495  +      integrityCheckResultRow(v, 2);
  1472   1496         sqlite3VdbeJumpHere(v, addr);
  1473   1497   
  1474   1498         /* Make sure all the indices are constructed correctly.
  1475   1499         */
  1476         -      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
         1500  +      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1477   1501           Table *pTab = sqliteHashData(x);
  1478   1502           Index *pIdx, *pPk;
  1479   1503           Index *pPrior = 0;
  1480   1504           int loopTop;
  1481   1505           int iDataCur, iIdxCur;
  1482   1506           int r1 = -1;
  1483   1507   
  1484         -        if( pTab->pIndex==0 ) continue;
         1508  +        if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
         1509  +        if( pTab->pCheck==0
         1510  +         && (pTab->tabFlags & TF_HasNotNull)==0
         1511  +         && (pTab->pIndex==0 || isQuick)
         1512  +        ){
         1513  +          continue;  /* No additional checks needed for this table */
         1514  +        }
  1485   1515           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1486         -        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1487         -        VdbeCoverage(v);
  1488         -        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1489         -        sqlite3VdbeJumpHere(v, addr);
  1490   1516           sqlite3ExprCacheClear(pParse);
  1491   1517           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
  1492   1518                                      1, 0, &iDataCur, &iIdxCur);
  1493   1519           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1494   1520           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1495   1521             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1496   1522           }
................................................................................
  1497   1523           assert( pParse->nMem>=8+j );
  1498   1524           assert( sqlite3NoTempsInRange(pParse,1,7+j) );
  1499   1525           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
  1500   1526           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
  1501   1527           /* Verify that all NOT NULL columns really are NOT NULL */
  1502   1528           for(j=0; j<pTab->nCol; j++){
  1503   1529             char *zErr;
  1504         -          int jmp2, jmp3;
         1530  +          int jmp2;
  1505   1531             if( j==pTab->iPKey ) continue;
  1506   1532             if( pTab->aCol[j].notNull==0 ) continue;
  1507   1533             sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
  1508   1534             sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
  1509   1535             jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
  1510         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1511   1536             zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
  1512   1537                                 pTab->aCol[j].zName);
  1513   1538             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
  1514         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
  1515         -          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
  1516         -          sqlite3VdbeAddOp0(v, OP_Halt);
         1539  +          integrityCheckResultRow(v, 3);
  1517   1540             sqlite3VdbeJumpHere(v, jmp2);
  1518         -          sqlite3VdbeJumpHere(v, jmp3);
         1541  +        }
         1542  +        /* Verify CHECK constraints */
         1543  +        if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
         1544  +          int addrCkFault = sqlite3VdbeMakeLabel(v);
         1545  +          int addrCkOk = sqlite3VdbeMakeLabel(v);
         1546  +          ExprList *pCheck = pTab->pCheck;
         1547  +          char *zErr;
         1548  +          int k;
         1549  +          pParse->iSelfTab = iDataCur;
         1550  +          sqlite3ExprCachePush(pParse);
         1551  +          for(k=pCheck->nExpr-1; k>0; k--){
         1552  +            sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
         1553  +          }
         1554  +          sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
         1555  +                            SQLITE_JUMPIFNULL);
         1556  +          sqlite3VdbeResolveLabel(v, addrCkFault);
         1557  +          zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
         1558  +                                pTab->zName);
         1559  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
         1560  +          integrityCheckResultRow(v, 3);
         1561  +          sqlite3VdbeResolveLabel(v, addrCkOk);
         1562  +          sqlite3ExprCachePop(pParse);
  1519   1563           }
  1520   1564           /* Validate index entries for the current row */
  1521         -        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
         1565  +        for(j=0, pIdx=pTab->pIndex; pIdx && !isQuick; pIdx=pIdx->pNext, j++){
  1522   1566             int jmp2, jmp3, jmp4, jmp5;
  1523   1567             int ckUniq = sqlite3VdbeMakeLabel(v);
  1524   1568             if( pPk==pIdx ) continue;
  1525   1569             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
  1526   1570                                          pPrior, r1);
  1527   1571             pPrior = pIdx;
  1528   1572             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1529   1573             /* Verify that an index entry exists for the current table row */
  1530   1574             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
  1531   1575                                         pIdx->nColumn); VdbeCoverage(v);
  1532         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1533   1576             sqlite3VdbeLoadString(v, 3, "row ");
  1534   1577             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1535   1578             sqlite3VdbeLoadString(v, 4, " missing from index ");
  1536   1579             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1537   1580             jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
  1538   1581             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1539         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
  1540         -          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
  1541         -          sqlite3VdbeAddOp0(v, OP_Halt);
         1582  +          jmp4 = integrityCheckResultRow(v, 3);
  1542   1583             sqlite3VdbeJumpHere(v, jmp2);
  1543   1584             /* For UNIQUE indexes, verify that only one entry exists with the
  1544   1585             ** current key.  The entry is unique if (1) any column is NULL
  1545   1586             ** or (2) the next entry has a different key */
  1546   1587             if( IsUniqueIndex(pIdx) ){
  1547   1588               int uniqOk = sqlite3VdbeMakeLabel(v);
  1548   1589               int jmp6;
................................................................................
  1555   1596                 VdbeCoverage(v);
  1556   1597               }
  1557   1598               jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
  1558   1599               sqlite3VdbeGoto(v, uniqOk);
  1559   1600               sqlite3VdbeJumpHere(v, jmp6);
  1560   1601               sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
  1561   1602                                    pIdx->nKeyCol); VdbeCoverage(v);
  1562         -            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1563   1603               sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
  1564   1604               sqlite3VdbeGoto(v, jmp5);
  1565   1605               sqlite3VdbeResolveLabel(v, uniqOk);
  1566   1606             }
  1567   1607             sqlite3VdbeJumpHere(v, jmp4);
  1568   1608             sqlite3ResolvePartIdxLabel(pParse, jmp3);
  1569   1609           }
  1570   1610           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
  1571   1611           sqlite3VdbeJumpHere(v, loopTop-1);
  1572   1612   #ifndef SQLITE_OMIT_BTREECOUNT
  1573         -        sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
  1574         -        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1575         -          if( pPk==pIdx ) continue;
  1576         -          addr = sqlite3VdbeCurrentAddr(v);
  1577         -          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
  1578         -          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1579         -          sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
  1580         -          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
  1581         -          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
  1582         -          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1583         -          sqlite3VdbeLoadString(v, 3, pIdx->zName);
  1584         -          sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1585         -          sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
         1613  +        if( !isQuick ){
         1614  +          sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
         1615  +          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
         1616  +            if( pPk==pIdx ) continue;
         1617  +            sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
         1618  +            addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
         1619  +            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
         1620  +            sqlite3VdbeLoadString(v, 3, pIdx->zName);
         1621  +            sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
         1622  +            integrityCheckResultRow(v, 7);
         1623  +            sqlite3VdbeJumpHere(v, addr);
         1624  +          }
  1586   1625           }
  1587   1626   #endif /* SQLITE_OMIT_BTREECOUNT */
  1588   1627         } 
  1589   1628       }
  1590   1629       {
  1591   1630         static const int iLn = VDBE_OFFSET_LINENO(2);
  1592   1631         static const VdbeOpList endCode[] = {
  1593   1632           { OP_AddImm,      1, 0,        0},    /* 0 */
  1594         -        { OP_If,          1, 4,        0},    /* 1 */
         1633  +        { OP_IfNotZero,   1, 4,        0},    /* 1 */
  1595   1634           { OP_String8,     0, 3,        0},    /* 2 */
  1596   1635           { OP_ResultRow,   3, 1,        0},    /* 3 */
  1597   1636         };
  1598   1637         VdbeOp *aOp;
  1599   1638   
  1600   1639         aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
  1601   1640         if( aOp ){
  1602         -        aOp[0].p2 = -mxErr;
         1641  +        aOp[0].p2 = 1-mxErr;
  1603   1642           aOp[2].p4type = P4_STATIC;
  1604   1643           aOp[2].p4.z = "ok";
  1605   1644         }
  1606   1645       }
  1607   1646     }
  1608   1647     break;
  1609   1648   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
................................................................................
  1819   1858     ** connection on which it is invoked to free up as much memory as it
  1820   1859     ** can, by calling sqlite3_db_release_memory().
  1821   1860     */
  1822   1861     case PragTyp_SHRINK_MEMORY: {
  1823   1862       sqlite3_db_release_memory(db);
  1824   1863       break;
  1825   1864     }
         1865  +
         1866  +  /*
         1867  +  **  PRAGMA optimize
         1868  +  **  PRAGMA optimize(MASK)
         1869  +  **  PRAGMA schema.optimize
         1870  +  **  PRAGMA schema.optimize(MASK)
         1871  +  **
         1872  +  ** Attempt to optimize the database.  All schemas are optimized in the first
         1873  +  ** two forms, and only the specified schema is optimized in the latter two.
         1874  +  **
         1875  +  ** The details of optimizations performed by this pragma are expected
         1876  +  ** to change and improve over time.  Applications should anticipate that
         1877  +  ** this pragma will perform new optimizations in future releases.
         1878  +  **
         1879  +  ** The optional argument is a bitmask of optimizations to perform:
         1880  +  **
         1881  +  **    0x0001    Debugging mode.  Do not actually perform any optimizations
         1882  +  **              but instead return one line of text for each optimization
         1883  +  **              that would have been done.  Off by default.
         1884  +  **
         1885  +  **    0x0002    Run ANALYZE on tables that might benefit.  On by default.
         1886  +  **              See below for additional information.
         1887  +  **
         1888  +  **    0x0004    (Not yet implemented) Record usage and performance 
         1889  +  **              information from the current session in the
         1890  +  **              database file so that it will be available to "optimize"
         1891  +  **              pragmas run by future database connections.
         1892  +  **
         1893  +  **    0x0008    (Not yet implemented) Create indexes that might have
         1894  +  **              been helpful to recent queries
         1895  +  **
         1896  +  ** The default MASK is and always shall be 0xfffe.  0xfffe means perform all    ** of the optimizations listed above except Debug Mode, including new
         1897  +  ** optimizations that have not yet been invented.  If new optimizations are
         1898  +  ** ever added that should be off by default, those off-by-default 
         1899  +  ** optimizations will have bitmasks of 0x10000 or larger.
         1900  +  **
         1901  +  ** DETERMINATION OF WHEN TO RUN ANALYZE
         1902  +  **
         1903  +  ** In the current implementation, a table is analyzed if only if all of
         1904  +  ** the following are true:
         1905  +  **
         1906  +  ** (1) MASK bit 0x02 is set.
         1907  +  **
         1908  +  ** (2) The query planner used sqlite_stat1-style statistics for one or
         1909  +  **     more indexes of the table at some point during the lifetime of
         1910  +  **     the current connection.
         1911  +  **
         1912  +  ** (3) One or more indexes of the table are currently unanalyzed OR
         1913  +  **     the number of rows in the table has increased by 25 times or more
         1914  +  **     since the last time ANALYZE was run.
         1915  +  **
         1916  +  ** The rules for when tables are analyzed are likely to change in
         1917  +  ** future releases.
         1918  +  */
         1919  +  case PragTyp_OPTIMIZE: {
         1920  +    int iDbLast;           /* Loop termination point for the schema loop */
         1921  +    int iTabCur;           /* Cursor for a table whose size needs checking */
         1922  +    HashElem *k;           /* Loop over tables of a schema */
         1923  +    Schema *pSchema;       /* The current schema */
         1924  +    Table *pTab;           /* A table in the schema */
         1925  +    Index *pIdx;           /* An index of the table */
         1926  +    LogEst szThreshold;    /* Size threshold above which reanalysis is needd */
         1927  +    char *zSubSql;         /* SQL statement for the OP_SqlExec opcode */
         1928  +    u32 opMask;            /* Mask of operations to perform */
         1929  +
         1930  +    if( zRight ){
         1931  +      opMask = (u32)sqlite3Atoi(zRight);
         1932  +      if( (opMask & 0x02)==0 ) break;
         1933  +    }else{
         1934  +      opMask = 0xfffe;
         1935  +    }
         1936  +    iTabCur = pParse->nTab++;
         1937  +    for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
         1938  +      if( iDb==1 ) continue;
         1939  +      sqlite3CodeVerifySchema(pParse, iDb);
         1940  +      pSchema = db->aDb[iDb].pSchema;
         1941  +      for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
         1942  +        pTab = (Table*)sqliteHashData(k);
         1943  +
         1944  +        /* If table pTab has not been used in a way that would benefit from
         1945  +        ** having analysis statistics during the current session, then skip it.
         1946  +        ** This also has the effect of skipping virtual tables and views */
         1947  +        if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
         1948  +
         1949  +        /* Reanalyze if the table is 25 times larger than the last analysis */
         1950  +        szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
         1951  +        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
         1952  +          if( !pIdx->hasStat1 ){
         1953  +            szThreshold = 0; /* Always analyze if any index lacks statistics */
         1954  +            break;
         1955  +          }
         1956  +        }
         1957  +        if( szThreshold ){
         1958  +          sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
         1959  +          sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 
         1960  +                         sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
         1961  +          VdbeCoverage(v);
         1962  +        }
         1963  +        zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
         1964  +                                 db->aDb[iDb].zDbSName, pTab->zName);
         1965  +        if( opMask & 0x01 ){
         1966  +          int r1 = sqlite3GetTempReg(pParse);
         1967  +          sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
         1968  +          sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
         1969  +        }else{
         1970  +          sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
         1971  +        }
         1972  +      }
         1973  +    }
         1974  +    sqlite3VdbeAddOp0(v, OP_Expire);
         1975  +    break;
         1976  +  }
  1826   1977   
  1827   1978     /*
  1828   1979     **   PRAGMA busy_timeout
  1829   1980     **   PRAGMA busy_timeout = N
  1830   1981     **
  1831   1982     ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
  1832   1983     ** if one is set.  If no busy handler or a different busy handler is set

Changes to src/pragma.h.

    26     26   #define PragTyp_INTEGRITY_CHECK               18
    27     27   #define PragTyp_JOURNAL_MODE                  19
    28     28   #define PragTyp_JOURNAL_SIZE_LIMIT            20
    29     29   #define PragTyp_LOCK_PROXY_FILE               21
    30     30   #define PragTyp_LOCKING_MODE                  22
    31     31   #define PragTyp_PAGE_COUNT                    23
    32     32   #define PragTyp_MMAP_SIZE                     24
    33         -#define PragTyp_PAGE_SIZE                     25
    34         -#define PragTyp_SECURE_DELETE                 26
    35         -#define PragTyp_SHRINK_MEMORY                 27
    36         -#define PragTyp_SOFT_HEAP_LIMIT               28
    37         -#define PragTyp_STATS                         29
           33  +#define PragTyp_OPTIMIZE                      25
           34  +#define PragTyp_PAGE_SIZE                     26
           35  +#define PragTyp_SECURE_DELETE                 27
           36  +#define PragTyp_SHRINK_MEMORY                 28
           37  +#define PragTyp_SOFT_HEAP_LIMIT               29
    38     38   #define PragTyp_SYNCHRONOUS                   30
    39     39   #define PragTyp_TABLE_INFO                    31
    40     40   #define PragTyp_TEMP_STORE                    32
    41     41   #define PragTyp_TEMP_STORE_DIRECTORY          33
    42     42   #define PragTyp_THREADS                       34
    43     43   #define PragTyp_WAL_AUTOCHECKPOINT            35
    44     44   #define PragTyp_WAL_CHECKPOINT                36
    45     45   #define PragTyp_ACTIVATE_EXTENSIONS           37
    46     46   #define PragTyp_HEXKEY                        38
    47     47   #define PragTyp_KEY                           39
    48     48   #define PragTyp_REKEY                         40
    49     49   #define PragTyp_LOCK_STATUS                   41
    50     50   #define PragTyp_PARSER_TRACE                  42
           51  +#define PragTyp_STATS                         43
    51     52   
    52     53   /* Property flags associated with various pragma. */
    53     54   #define PragFlg_NeedSchema 0x01 /* Force schema load before running */
    54     55   #define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
    55     56   #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
    56     57   #define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
    57     58   #define PragFlg_Result0    0x10 /* Acts as query when no argument */
................................................................................
    67     68     /*   0 */ "cache_size",  /* Used by: default_cache_size */
    68     69     /*   1 */ "cid",         /* Used by: table_info */
    69     70     /*   2 */ "name",       
    70     71     /*   3 */ "type",       
    71     72     /*   4 */ "notnull",    
    72     73     /*   5 */ "dflt_value", 
    73     74     /*   6 */ "pk",         
    74         -  /*   7 */ "table",       /* Used by: stats */
    75         -  /*   8 */ "index",      
    76         -  /*   9 */ "width",      
    77         -  /*  10 */ "height",     
    78         -  /*  11 */ "seqno",       /* Used by: index_info */
    79         -  /*  12 */ "cid",        
    80         -  /*  13 */ "name",       
    81         -  /*  14 */ "seqno",       /* Used by: index_xinfo */
    82         -  /*  15 */ "cid",        
    83         -  /*  16 */ "name",       
    84         -  /*  17 */ "desc",       
    85         -  /*  18 */ "coll",       
    86         -  /*  19 */ "key",        
    87         -  /*  20 */ "seq",         /* Used by: index_list */
    88         -  /*  21 */ "name",       
    89         -  /*  22 */ "unique",     
    90         -  /*  23 */ "origin",     
    91         -  /*  24 */ "partial",    
    92         -  /*  25 */ "seq",         /* Used by: database_list */
    93         -  /*  26 */ "name",       
    94         -  /*  27 */ "file",       
    95         -  /*  28 */ "seq",         /* Used by: collation_list */
    96         -  /*  29 */ "name",       
    97         -  /*  30 */ "id",          /* Used by: foreign_key_list */
    98         -  /*  31 */ "seq",        
    99         -  /*  32 */ "table",      
   100         -  /*  33 */ "from",       
   101         -  /*  34 */ "to",         
   102         -  /*  35 */ "on_update",  
   103         -  /*  36 */ "on_delete",  
   104         -  /*  37 */ "match",      
   105         -  /*  38 */ "table",       /* Used by: foreign_key_check */
   106         -  /*  39 */ "rowid",      
   107         -  /*  40 */ "parent",     
   108         -  /*  41 */ "fkid",       
   109         -  /*  42 */ "busy",        /* Used by: wal_checkpoint */
   110         -  /*  43 */ "log",        
   111         -  /*  44 */ "checkpointed",
   112         -  /*  45 */ "timeout",     /* Used by: busy_timeout */
   113         -  /*  46 */ "database",    /* Used by: lock_status */
   114         -  /*  47 */ "status",     
           75  +  /*   7 */ "tbl",         /* Used by: stats */
           76  +  /*   8 */ "idx",        
           77  +  /*   9 */ "wdth",       
           78  +  /*  10 */ "hght",       
           79  +  /*  11 */ "flgs",       
           80  +  /*  12 */ "seqno",       /* Used by: index_info */
           81  +  /*  13 */ "cid",        
           82  +  /*  14 */ "name",       
           83  +  /*  15 */ "seqno",       /* Used by: index_xinfo */
           84  +  /*  16 */ "cid",        
           85  +  /*  17 */ "name",       
           86  +  /*  18 */ "desc",       
           87  +  /*  19 */ "coll",       
           88  +  /*  20 */ "key",        
           89  +  /*  21 */ "seq",         /* Used by: index_list */
           90  +  /*  22 */ "name",       
           91  +  /*  23 */ "unique",     
           92  +  /*  24 */ "origin",     
           93  +  /*  25 */ "partial",    
           94  +  /*  26 */ "seq",         /* Used by: database_list */
           95  +  /*  27 */ "name",       
           96  +  /*  28 */ "file",       
           97  +  /*  29 */ "seq",         /* Used by: collation_list */
           98  +  /*  30 */ "name",       
           99  +  /*  31 */ "id",          /* Used by: foreign_key_list */
          100  +  /*  32 */ "seq",        
          101  +  /*  33 */ "table",      
          102  +  /*  34 */ "from",       
          103  +  /*  35 */ "to",         
          104  +  /*  36 */ "on_update",  
          105  +  /*  37 */ "on_delete",  
          106  +  /*  38 */ "match",      
          107  +  /*  39 */ "table",       /* Used by: foreign_key_check */
          108  +  /*  40 */ "rowid",      
          109  +  /*  41 */ "parent",     
          110  +  /*  42 */ "fkid",       
          111  +  /*  43 */ "busy",        /* Used by: wal_checkpoint */
          112  +  /*  44 */ "log",        
          113  +  /*  45 */ "checkpointed",
          114  +  /*  46 */ "timeout",     /* Used by: busy_timeout */
          115  +  /*  47 */ "database",    /* Used by: lock_status */
          116  +  /*  48 */ "status",     
   115    117   };
   116    118   
   117    119   /* Definitions of all built-in pragmas */
   118    120   typedef struct PragmaName {
   119    121     const char *const zName; /* Name of pragma */
   120    122     u8 ePragTyp;             /* PragTyp_XXX value */
   121    123     u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
................................................................................
   153    155     /* ColNames:  */ 0, 0,
   154    156     /* iArg:      */ SQLITE_AutoIndex },
   155    157   #endif
   156    158   #endif
   157    159    {/* zName:     */ "busy_timeout",
   158    160     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
   159    161     /* ePragFlg:  */ PragFlg_Result0,
   160         -  /* ColNames:  */ 45, 1,
          162  +  /* ColNames:  */ 46, 1,
   161    163     /* iArg:      */ 0 },
   162    164   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   163    165    {/* zName:     */ "cache_size",
   164    166     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
   165    167     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
   166    168     /* ColNames:  */ 0, 0,
   167    169     /* iArg:      */ 0 },
................................................................................
   190    192     /* ColNames:  */ 0, 0,
   191    193     /* iArg:      */ SQLITE_CkptFullFSync },
   192    194   #endif
   193    195   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
   194    196    {/* zName:     */ "collation_list",
   195    197     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
   196    198     /* ePragFlg:  */ PragFlg_Result0,
   197         -  /* ColNames:  */ 28, 2,
          199  +  /* ColNames:  */ 29, 2,
   198    200     /* iArg:      */ 0 },
   199    201   #endif
   200    202   #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
   201    203    {/* zName:     */ "compile_options",
   202    204     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
   203    205     /* ePragFlg:  */ PragFlg_Result0,
   204    206     /* ColNames:  */ 0, 0,
................................................................................
   225    227     /* ColNames:  */ 0, 0,
   226    228     /* iArg:      */ BTREE_DATA_VERSION },
   227    229   #endif
   228    230   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
   229    231    {/* zName:     */ "database_list",
   230    232     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
   231    233     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
   232         -  /* ColNames:  */ 25, 3,
          234  +  /* ColNames:  */ 26, 3,
   233    235     /* iArg:      */ 0 },
   234    236   #endif
   235    237   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
   236    238    {/* zName:     */ "default_cache_size",
   237    239     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
   238    240     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
   239    241     /* ColNames:  */ 0, 1,
................................................................................
   262    264     /* ColNames:  */ 0, 0,
   263    265     /* iArg:      */ 0 },
   264    266   #endif
   265    267   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   266    268    {/* zName:     */ "foreign_key_check",
   267    269     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
   268    270     /* ePragFlg:  */ PragFlg_NeedSchema,
   269         -  /* ColNames:  */ 38, 4,
          271  +  /* ColNames:  */ 39, 4,
   270    272     /* iArg:      */ 0 },
   271    273   #endif
   272    274   #if !defined(SQLITE_OMIT_FOREIGN_KEY)
   273    275    {/* zName:     */ "foreign_key_list",
   274    276     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
   275    277     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
   276         -  /* ColNames:  */ 30, 8,
          278  +  /* ColNames:  */ 31, 8,
   277    279     /* iArg:      */ 0 },
   278    280   #endif
   279    281   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   280    282   #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   281    283    {/* zName:     */ "foreign_keys",
   282    284     /* ePragTyp:  */ PragTyp_FLAG,
   283    285     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
................................................................................
   332    334     /* ColNames:  */ 0, 0,
   333    335     /* iArg:      */ 0 },
   334    336   #endif
   335    337   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
   336    338    {/* zName:     */ "index_info",
   337    339     /* ePragTyp:  */ PragTyp_INDEX_INFO,
   338    340     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
   339         -  /* ColNames:  */ 11, 3,
          341  +  /* ColNames:  */ 12, 3,
   340    342     /* iArg:      */ 0 },
   341    343    {/* zName:     */ "index_list",
   342    344     /* ePragTyp:  */ PragTyp_INDEX_LIST,
   343    345     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
   344         -  /* ColNames:  */ 20, 5,
          346  +  /* ColNames:  */ 21, 5,
   345    347     /* iArg:      */ 0 },
   346    348    {/* zName:     */ "index_xinfo",
   347    349     /* ePragTyp:  */ PragTyp_INDEX_INFO,
   348    350     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
   349         -  /* ColNames:  */ 14, 6,
          351  +  /* ColNames:  */ 15, 6,
   350    352     /* iArg:      */ 1 },
   351    353   #endif
   352    354   #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
   353    355    {/* zName:     */ "integrity_check",
   354    356     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
   355    357     /* ePragFlg:  */ PragFlg_NeedSchema,
   356    358     /* ColNames:  */ 0, 0,
................................................................................
   389    391     /* ColNames:  */ 0, 0,
   390    392     /* iArg:      */ 0 },
   391    393   #endif
   392    394   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
   393    395    {/* zName:     */ "lock_status",
   394    396     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
   395    397     /* ePragFlg:  */ PragFlg_Result0,
   396         -  /* ColNames:  */ 46, 2,
          398  +  /* ColNames:  */ 47, 2,
   397    399     /* iArg:      */ 0 },
   398    400   #endif
   399    401   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   400    402    {/* zName:     */ "locking_mode",
   401    403     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
   402    404     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
   403    405     /* ColNames:  */ 0, 0,
................................................................................
   408    410     /* ColNames:  */ 0, 0,
   409    411     /* iArg:      */ 0 },
   410    412    {/* zName:     */ "mmap_size",
   411    413     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
   412    414     /* ePragFlg:  */ 0,
   413    415     /* ColNames:  */ 0, 0,
   414    416     /* iArg:      */ 0 },
          417  +#endif
          418  + {/* zName:     */ "optimize",
          419  +  /* ePragTyp:  */ PragTyp_OPTIMIZE,
          420  +  /* ePragFlg:  */ PragFlg_Result1,
          421  +  /* ColNames:  */ 0, 0,
          422  +  /* iArg:      */ 0 },
          423  +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   415    424    {/* zName:     */ "page_count",
   416    425     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
   417    426     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
   418    427     /* ColNames:  */ 0, 0,
   419    428     /* iArg:      */ 0 },
   420    429    {/* zName:     */ "page_size",
   421    430     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
................................................................................
   506    515    {/* zName:     */ "sql_trace",
   507    516     /* ePragTyp:  */ PragTyp_FLAG,
   508    517     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   509    518     /* ColNames:  */ 0, 0,
   510    519     /* iArg:      */ SQLITE_SqlTrace },
   511    520   #endif
   512    521   #endif
   513         -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
          522  +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
   514    523    {/* zName:     */ "stats",
   515    524     /* ePragTyp:  */ PragTyp_STATS,
   516    525     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
   517         -  /* ColNames:  */ 7, 4,
          526  +  /* ColNames:  */ 7, 5,
   518    527     /* iArg:      */ 0 },
   519    528   #endif
   520    529   #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   521    530    {/* zName:     */ "synchronous",
   522    531     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
   523    532     /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
   524    533     /* ColNames:  */ 0, 0,
................................................................................
   589    598     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
   590    599     /* ePragFlg:  */ 0,
   591    600     /* ColNames:  */ 0, 0,
   592    601     /* iArg:      */ 0 },
   593    602    {/* zName:     */ "wal_checkpoint",
   594    603     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
   595    604     /* ePragFlg:  */ PragFlg_NeedSchema,
   596         -  /* ColNames:  */ 42, 3,
          605  +  /* ColNames:  */ 43, 3,
   597    606     /* iArg:      */ 0 },
   598    607   #endif
   599    608   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   600    609    {/* zName:     */ "writable_schema",
   601    610     /* ePragTyp:  */ PragTyp_FLAG,
   602    611     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   603    612     /* ColNames:  */ 0, 0,
   604    613     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
   605    614   #endif
   606    615   };
   607         -/* Number of pragmas: 60 on by default, 73 total. */
          616  +/* Number of pragmas: 60 on by default, 74 total. */

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       0 /* Integer types.  %d, %x, %o, and so forth */
           18  +#define etRADIX       0 /* non-decimal integer types.  %x %o */
    19     19   #define etFLOAT       1 /* Floating point.  %f */
    20     20   #define etEXP         2 /* Exponentional notation. %e and %E */
    21     21   #define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
    22     22   #define etSIZE        4 /* Return number of characters processed so far. %n */
    23     23   #define etSTRING      5 /* Strings. %s */
    24     24   #define etDYNSTRING   6 /* Dynamically allocated strings. %z */
    25     25   #define etPERCENT     7 /* Percent symbol. %% */
................................................................................
    29     29   #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
    30     30                             NULL pointers replaced by SQL NULL.  %Q */
    31     31   #define etTOKEN      11 /* a pointer to a Token structure */
    32     32   #define etSRCLIST    12 /* a pointer to a SrcList */
    33     33   #define etPOINTER    13 /* The %p conversion */
    34     34   #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
    35     35   #define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
           36  +#define etDECIMAL    16 /* %d or %u, but not %x, %o */
    36     37   
    37         -#define etINVALID    16 /* Any unrecognized conversion type */
           38  +#define etINVALID    17 /* Any unrecognized conversion type */
    38     39   
    39     40   
    40     41   /*
    41     42   ** An "etByte" is an 8-bit unsigned value.
    42     43   */
    43     44   typedef unsigned char etByte;
    44     45   
................................................................................
    54     55     etByte charset;          /* Offset into aDigits[] of the digits string */
    55     56     etByte prefix;           /* Offset into aPrefix[] of the prefix string */
    56     57   } et_info;
    57     58   
    58     59   /*
    59     60   ** Allowed values for et_info.flags
    60     61   */
    61         -#define FLAG_SIGNED  1     /* True if the value to convert is signed */
    62         -#define FLAG_STRING  4     /* Allow infinity precision */
           62  +#define FLAG_SIGNED    1     /* True if the value to convert is signed */
           63  +#define FLAG_STRING    4     /* Allow infinite precision */
    63     64   
    64     65   
    65     66   /*
    66     67   ** The following table is searched linearly, so it is good to put the
    67     68   ** most frequently used conversion types first.
    68     69   */
    69     70   static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
    70     71   static const char aPrefix[] = "-x0\000X0";
    71     72   static const et_info fmtinfo[] = {
    72         -  {  'd',