/ Check-in [7f217eda]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Merge latest trunk changes into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA3-256: 7f217edab4575554f657d38e2a1bc6b3f577998fdbecb04eb200aeb8b8406581
User & Date: dan 2017-11-06 20:02:08
Wiki:begin-concurrent
Context
2017-11-14
20:06
Merge changes from trunk. This fixes the SQLITE_ENABLE_UPDATE_DELETE_LIMIT functionality so that it works with views and WITHOUT ROWID tables. check-in: d90e5f34 user: dan tags: begin-concurrent
2017-11-06
20:02
Merge latest trunk changes into this branch. check-in: 7f217eda user: dan tags: begin-concurrent
10:04
Allow "BEGIN CONCURRENT" transactions to modify the temp schema. check-in: 0fb6d91c user: dan tags: begin-concurrent
09:34
Fix a harmless compiler warning from Xcode 9.1. check-in: 66d98310 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   162    162   
   163    163   USE_AMALGAMATION = @USE_AMALGAMATION@
   164    164   
   165    165   # Object files for the SQLite library (non-amalgamation).
   166    166   #
   167    167   LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
   168    168            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   169         -         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
          169  +         callback.lo complete.lo ctime.lo \
          170  +         date.lo dbpage.lo dbstat.lo delete.lo \
   170    171            expr.lo fault.lo fkey.lo \
   171    172            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   172    173            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   173    174            fts3_tokenize_vtab.lo \
   174    175            fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   175    176   	 fts5.lo \
   176    177            func.lo global.lo hash.lo \
................................................................................
   211    212     $(TOP)/src/btree.h \
   212    213     $(TOP)/src/btreeInt.h \
   213    214     $(TOP)/src/build.c \
   214    215     $(TOP)/src/callback.c \
   215    216     $(TOP)/src/complete.c \
   216    217     $(TOP)/src/ctime.c \
   217    218     $(TOP)/src/date.c \
          219  +  $(TOP)/src/dbpage.c \
   218    220     $(TOP)/src/dbstat.c \
   219    221     $(TOP)/src/delete.c \
   220    222     $(TOP)/src/expr.c \
   221    223     $(TOP)/src/fault.c \
   222    224     $(TOP)/src/fkey.c \
   223    225     $(TOP)/src/func.c \
   224    226     $(TOP)/src/global.c \
................................................................................
   261    263     $(TOP)/src/prepare.c \
   262    264     $(TOP)/src/printf.c \
   263    265     $(TOP)/src/random.c \
   264    266     $(TOP)/src/resolve.c \
   265    267     $(TOP)/src/rowset.c \
   266    268     $(TOP)/src/select.c \
   267    269     $(TOP)/src/status.c \
   268         -  $(TOP)/src/shell.c \
          270  +  $(TOP)/src/shell.c.in \
   269    271     $(TOP)/src/sqlite.h.in \
   270    272     $(TOP)/src/sqlite3ext.h \
   271    273     $(TOP)/src/sqliteInt.h \
   272    274     $(TOP)/src/sqliteLimit.h \
   273    275     $(TOP)/src/table.c \
   274    276     $(TOP)/src/tclsqlite.c \
   275    277     $(TOP)/src/threads.c \
................................................................................
   358    360   SRC += \
   359    361     keywordhash.h \
   360    362     opcodes.c \
   361    363     opcodes.h \
   362    364     parse.c \
   363    365     parse.h \
   364    366     config.h \
          367  +  shell.c \
   365    368     sqlite3.h
   366    369   
   367    370   # Source code to the test files.
   368    371   #
   369    372   TESTSRC = \
   370    373     $(TOP)/src/test1.c \
   371    374     $(TOP)/src/test2.c \
................................................................................
   389    392     $(TOP)/src/test_fs.c \
   390    393     $(TOP)/src/test_func.c \
   391    394     $(TOP)/src/test_hexio.c \
   392    395     $(TOP)/src/test_init.c \
   393    396     $(TOP)/src/test_intarray.c \
   394    397     $(TOP)/src/test_journal.c \
   395    398     $(TOP)/src/test_malloc.c \
          399  +  $(TOP)/src/test_md5.c \
   396    400     $(TOP)/src/test_multiplex.c \
   397    401     $(TOP)/src/test_mutex.c \
   398    402     $(TOP)/src/test_onefile.c \
   399    403     $(TOP)/src/test_osinst.c \
   400    404     $(TOP)/src/test_pcache.c \
   401    405     $(TOP)/src/test_quota.c \
   402    406     $(TOP)/src/test_rtree.c \
   403    407     $(TOP)/src/test_schema.c \
   404    408     $(TOP)/src/test_server.c \
   405    409     $(TOP)/src/test_superlock.c \
   406    410     $(TOP)/src/test_syscall.c \
          411  +  $(TOP)/src/test_tclsh.c \
   407    412     $(TOP)/src/test_tclvar.c \
   408    413     $(TOP)/src/test_thread.c \
   409    414     $(TOP)/src/test_vfs.c \
   410    415     $(TOP)/src/test_windirent.c \
   411    416     $(TOP)/src/test_wsd.c       \
   412    417     $(TOP)/ext/fts3/fts3_term.c \
   413    418     $(TOP)/ext/fts3/fts3_test.c  \
................................................................................
   445    450     $(TOP)/src/attach.c \
   446    451     $(TOP)/src/backup.c \
   447    452     $(TOP)/src/bitvec.c \
   448    453     $(TOP)/src/btree.c \
   449    454     $(TOP)/src/build.c \
   450    455     $(TOP)/src/ctime.c \
   451    456     $(TOP)/src/date.c \
          457  +  $(TOP)/src/dbpage.c \
   452    458     $(TOP)/src/dbstat.c \
   453    459     $(TOP)/src/expr.c \
   454    460     $(TOP)/src/func.c \
   455    461     $(TOP)/src/insert.c \
   456    462     $(TOP)/src/wal.c \
   457    463     $(TOP)/src/main.c \
   458    464     $(TOP)/src/mem5.c \
................................................................................
   565    571   # Extra compiler options for various shell tools
   566    572   #
   567    573   SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4
   568    574   # SHELL_OPT += -DSQLITE_ENABLE_FTS5
   569    575   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   570    576   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   571    577   SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB
          578  +SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
          579  +SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   572    580   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   573    581   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
   574    582   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   575    583   FUZZCHECK_SRC = $(TOP)/test/fuzzcheck.c $(TOP)/test/ossfuzz.c
   576    584   DBFUZZ_OPT = 
   577    585   
   578    586   # This is the default Makefile target.  The objects listed here
................................................................................
   593    601   libtclsqlite3.la:	tclsqlite.lo libsqlite3.la
   594    602   	$(LTLINK) -no-undefined -o $@ tclsqlite.lo \
   595    603   		libsqlite3.la @TCL_STUB_LIB_SPEC@ $(TLIBS) \
   596    604   		-rpath "$(TCLLIBDIR)" \
   597    605   		-version-info "8:6:8" \
   598    606   		-avoid-version
   599    607   
   600         -sqlite3$(TEXE):	$(TOP)/src/shell.c sqlite3.c
          608  +sqlite3$(TEXE):	shell.c sqlite3.c
   601    609   	$(LTLINK) $(READLINE_FLAGS) $(SHELL_OPT) -o $@ \
   602         -		$(TOP)/src/shell.c sqlite3.c \
          610  +		shell.c sqlite3.c \
   603    611   		$(LIBREADLINE) $(TLIBS) -rpath "$(libdir)"
   604    612   
   605    613   sqldiff$(TEXE):	$(TOP)/tool/sqldiff.c sqlite3.lo sqlite3.h
   606    614   	$(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.lo $(TLIBS)
   607    615   
   608    616   dbhash$(TEXE):	$(TOP)/tool/dbhash.c sqlite3.lo sqlite3.h
   609    617   	$(LTLINK) -o $@ $(TOP)/tool/dbhash.c sqlite3.lo $(TLIBS)
................................................................................
   664    672   	$(TCLSH_CMD) $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   665    673   	mv vdbe.new tsrc/vdbe.c
   666    674   	cp fts5.c fts5.h tsrc
   667    675   	touch .target_source
   668    676   
   669    677   sqlite3.c:	.target_source $(TOP)/tool/mksqlite3c.tcl
   670    678   	$(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl
   671         -	cp tsrc/shell.c tsrc/sqlite3ext.h .
          679  +	cp tsrc/sqlite3ext.h .
   672    680   	cp $(TOP)/ext/session/sqlite3session.h .
   673    681   
   674    682   sqlite3ext.h:	.target_source
   675    683   	cp tsrc/sqlite3ext.h .
   676    684   
   677    685   tclsqlite3.c:	sqlite3.c
   678    686   	echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
................................................................................
   748    756   
   749    757   ctime.lo:	$(TOP)/src/ctime.c $(HDR)
   750    758   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/ctime.c
   751    759   
   752    760   date.lo:	$(TOP)/src/date.c $(HDR)
   753    761   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/date.c
   754    762   
          763  +dbpage.lo:	$(TOP)/src/dbpage.c $(HDR)
          764  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/dbpage.c
          765  +
   755    766   dbstat.lo:	$(TOP)/src/dbstat.c $(HDR)
   756    767   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/dbstat.c
   757    768   
   758    769   delete.lo:	$(TOP)/src/delete.c $(HDR)
   759    770   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/delete.c
   760    771   
   761    772   expr.lo:	$(TOP)/src/expr.c $(HDR)
................................................................................
   932    943   whereexpr.lo:	$(TOP)/src/whereexpr.c $(HDR)
   933    944   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/whereexpr.c
   934    945   
   935    946   tclsqlite.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   936    947   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -c $(TOP)/src/tclsqlite.c
   937    948   
   938    949   tclsqlite-shell.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   939         -	$(LTCOMPILE) -DTCLSH=1 -o $@ -c $(TOP)/src/tclsqlite.c
          950  +	$(LTCOMPILE) -DTCLSH -o $@ -c $(TOP)/src/tclsqlite.c
   940    951   
   941    952   tclsqlite-stubs.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   942    953   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -o $@ -c $(TOP)/src/tclsqlite.c
   943    954   
   944    955   tclsqlite3$(TEXE):	tclsqlite-shell.lo libsqlite3.la
   945    956   	$(LTLINK) -o $@ tclsqlite-shell.lo \
   946    957   		 libsqlite3.la $(LIBTCL)
................................................................................
   966    977   
   967    978   sqlite3.h:	$(TOP)/src/sqlite.h.in $(TOP)/manifest mksourceid$(BEXE) $(TOP)/VERSION
   968    979   	$(TCLSH_CMD) $(TOP)/tool/mksqlite3h.tcl $(TOP) >sqlite3.h
   969    980   
   970    981   keywordhash.h:	$(TOP)/tool/mkkeywordhash.c
   971    982   	$(BCC) -o mkkeywordhash$(BEXE) $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)/tool/mkkeywordhash.c
   972    983   	./mkkeywordhash$(BEXE) >keywordhash.h
          984  +
          985  +# Source files that go into making shell.c
          986  +SHELL_SRC = \
          987  +	$(TOP)/src/shell.c.in \
          988  +	$(TOP)/ext/misc/shathree.c \
          989  +	$(TOP)/ext/misc/fileio.c \
          990  +	$(TOP)/ext/misc/completion.c
          991  +
          992  +shell.c:	$(SHELL_SRC) $(TOP)/tool/mkshellc.tcl
          993  +	$(TCLSH_CMD) $(TOP)/tool/mkshellc.tcl >shell.c
          994  +
   973    995   
   974    996   
   975    997   
   976    998   # Rules to build the extension objects.
   977    999   #
   978   1000   icu.lo:	$(TOP)/ext/icu/icu.c $(HDR) $(EXTHDR)
   979   1001   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/icu/icu.c
................................................................................
  1087   1109   # Rules to build the 'testfixture' application.
  1088   1110   #
  1089   1111   # If using the amalgamation, use sqlite3.c directly to build the test
  1090   1112   # fixture.  Otherwise link against libsqlite3.la.  (This distinction is
  1091   1113   # necessary because the test fixture requires non-API symbols which are
  1092   1114   # hidden when the library is built via the amalgamation).
  1093   1115   #
  1094         -TESTFIXTURE_FLAGS  = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
         1116  +TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
         1117  +TESTFIXTURE_FLAGS += -DTCLSH_INIT_PROC=sqlite3TestInit
  1095   1118   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE 
  1096   1119   TESTFIXTURE_FLAGS += -DBUILD_sqlite
  1097   1120   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  1098   1121   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
  1099   1122   TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_STMTVTAB
         1123  +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_DBPAGE_VTAB
  1100   1124   
  1101   1125   TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlite3.la
  1102   1126   TESTFIXTURE_SRC1 = sqlite3.c
  1103   1127   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c
  1104   1128   TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
  1105   1129   
  1106   1130   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
................................................................................
  1153   1177   # A very fast test that checks basic sanity.  The name comes from
  1154   1178   # the 60s-era electronics testing:  "Turn it on and see if smoke
  1155   1179   # comes out."
  1156   1180   #
  1157   1181   smoketest:	$(TESTPROGS) fuzzcheck$(TEXE)
  1158   1182   	./testfixture$(TEXE) $(TOP)/test/main.test $(TESTOPTS)
  1159   1183   
  1160         -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
  1161         -	echo "#define TCLSH 2" > $@
  1162         -	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
  1163         -	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
  1164         -	echo "static const char *tclsh_main_loop(void){" >> $@
  1165         -	echo "static const char *zMainloop = " >> $@
  1166         -	$(TCLSH_CMD) $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
  1167         -	echo "; return zMainloop; }" >> $@
         1184  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in
         1185  +	$(TCLSH_CMD) $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in >sqlite3_analyzer.c
  1168   1186   
  1169   1187   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1170   1188   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
  1171   1189   
         1190  +CHECKER_DEPS =\
         1191  +  $(TOP)/tool/mkccode.tcl \
         1192  +  sqlite3.c \
         1193  +  $(TOP)/src/tclsqlite.c \
         1194  +  $(TOP)/ext/repair/sqlite3_checker.tcl \
         1195  +  $(TOP)/ext/repair/checkindex.c \
         1196  +  $(TOP)/ext/repair/checkfreelist.c \
         1197  +  $(TOP)/ext/misc/btreeinfo.c \
         1198  +  $(TOP)/ext/repair/sqlite3_checker.c.in
         1199  +
         1200  +sqlite3_checker.c:	$(CHECKER_DEPS)
         1201  +	$(TCLSH_CMD) $(TOP)/tool/mkccode.tcl $(TOP)/ext/repair/sqlite3_checker.c.in >$@
         1202  +
         1203  +sqlite3_checker$(TEXE):	sqlite3_checker.c
         1204  +	$(LTLINK) sqlite3_checker.c -o $@ $(LIBTCL) $(TLIBS)
         1205  +
  1172   1206   dbdump$(TEXE): $(TOP)/ext/misc/dbdump.c sqlite3.lo
  1173   1207   	$(LTLINK) -DDBDUMP_STANDALONE -o $@ \
  1174   1208              $(TOP)/ext/misc/dbdump.c sqlite3.lo $(TLIBS)
  1175   1209   
  1176   1210   showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.lo
  1177   1211   	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.lo $(TLIBS)
  1178   1212   
................................................................................
  1181   1215   
  1182   1216   showjournal$(TEXE):	$(TOP)/tool/showjournal.c sqlite3.lo
  1183   1217   	$(LTLINK) -o $@ $(TOP)/tool/showjournal.c sqlite3.lo $(TLIBS)
  1184   1218   
  1185   1219   showwal$(TEXE):	$(TOP)/tool/showwal.c sqlite3.lo
  1186   1220   	$(LTLINK) -o $@ $(TOP)/tool/showwal.c sqlite3.lo $(TLIBS)
  1187   1221   
         1222  +showshm$(TEXE):	$(TOP)/tool/showshm.c
         1223  +	$(LTLINK) -o $@ $(TOP)/tool/showshm.c
         1224  +
  1188   1225   changeset$(TEXE):	$(TOP)/ext/session/changeset.c sqlite3.lo
  1189   1226   	$(LTLINK) -o $@ $(TOP)/ext/session/changeset.c sqlite3.lo $(TLIBS)
  1190   1227   
  1191   1228   rollback-test$(TEXE):	$(TOP)/tool/rollback-test.c sqlite3.lo
  1192   1229   	$(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS)
  1193   1230   
  1194   1231   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h

Changes to Makefile.msc.

  1087   1087   ###############################################################################
  1088   1088   
  1089   1089   # <<mark>>
  1090   1090   # Object files for the SQLite library (non-amalgamation).
  1091   1091   #
  1092   1092   LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \
  1093   1093            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
  1094         -         callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \
         1094  +         callback.lo complete.lo ctime.lo \
         1095  +         date.lo dbpage.lo dbstat.lo delete.lo \
  1095   1096            expr.lo fault.lo fkey.lo \
  1096   1097            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
  1097   1098            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
  1098   1099            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
  1099   1100            fts5.lo \
  1100   1101            func.lo global.lo hash.lo \
  1101   1102            icu.lo insert.lo legacy.lo loadext.lo \
................................................................................
  1150   1151     $(TOP)\src\btmutex.c \
  1151   1152     $(TOP)\src\btree.c \
  1152   1153     $(TOP)\src\build.c \
  1153   1154     $(TOP)\src\callback.c \
  1154   1155     $(TOP)\src\complete.c \
  1155   1156     $(TOP)\src\ctime.c \
  1156   1157     $(TOP)\src\date.c \
         1158  +  $(TOP)\src\dbpage.c \
  1157   1159     $(TOP)\src\dbstat.c \
  1158   1160     $(TOP)\src\delete.c \
  1159   1161     $(TOP)\src\expr.c \
  1160   1162     $(TOP)\src\fault.c \
  1161   1163     $(TOP)\src\fkey.c \
  1162   1164     $(TOP)\src\func.c \
  1163   1165     $(TOP)\src\global.c \
................................................................................
  1216   1218     $(TOP)\src\vtab.c \
  1217   1219     $(TOP)\src\wal.c \
  1218   1220     $(TOP)\src\walker.c \
  1219   1221     $(TOP)\src\where.c \
  1220   1222     $(TOP)\src\wherecode.c \
  1221   1223     $(TOP)\src\whereexpr.c
  1222   1224   
  1223         -# Shell source code files.
  1224         -#
  1225         -SRC02 = \
  1226         -  $(TOP)\src\shell.c
  1227         -
  1228   1225   # Core miscellaneous files.
  1229   1226   #
  1230   1227   SRC03 = \
  1231   1228     $(TOP)\src\parse.y
  1232   1229   
  1233   1230   # Core header files, part 1.
  1234   1231   #
................................................................................
  1327   1324   
  1328   1325   # Generated header files
  1329   1326   #
  1330   1327   SRC11 = \
  1331   1328     keywordhash.h \
  1332   1329     opcodes.h \
  1333   1330     parse.h \
         1331  +  shell.c \
  1334   1332     $(SQLITE3H)
  1335   1333   
  1336   1334   # Generated Tcl header files
  1337   1335   #
  1338   1336   !IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0
  1339   1337   SRC12 = \
  1340   1338     $(SQLITETCLH) \
................................................................................
  1341   1339     $(SQLITETCLDECLSH)
  1342   1340   !ELSE
  1343   1341   SRC12 =
  1344   1342   !ENDIF
  1345   1343   
  1346   1344   # All source code files.
  1347   1345   #
  1348         -SRC = $(SRC00) $(SRC01) $(SRC02) $(SRC03) $(SRC04) $(SRC05) $(SRC06) $(SRC07) $(SRC08) $(SRC09) $(SRC10) $(SRC11)
         1346  +SRC = $(SRC00) $(SRC01) $(SRC03) $(SRC04) $(SRC05) $(SRC06) $(SRC07) $(SRC08) $(SRC09) $(SRC10) $(SRC11)
  1349   1347   
  1350   1348   # Source code to the test files.
  1351   1349   #
  1352   1350   TESTSRC = \
  1353   1351     $(TOP)\src\test1.c \
  1354   1352     $(TOP)\src\test2.c \
  1355   1353     $(TOP)\src\test3.c \
................................................................................
  1372   1370     $(TOP)\src\test_fs.c \
  1373   1371     $(TOP)\src\test_func.c \
  1374   1372     $(TOP)\src\test_hexio.c \
  1375   1373     $(TOP)\src\test_init.c \
  1376   1374     $(TOP)\src\test_intarray.c \
  1377   1375     $(TOP)\src\test_journal.c \
  1378   1376     $(TOP)\src\test_malloc.c \
         1377  +  $(TOP)\src\test_md5.c \
  1379   1378     $(TOP)\src\test_multiplex.c \
  1380   1379     $(TOP)\src\test_mutex.c \
  1381   1380     $(TOP)\src\test_onefile.c \
  1382   1381     $(TOP)\src\test_osinst.c \
  1383   1382     $(TOP)\src\test_pcache.c \
  1384   1383     $(TOP)\src\test_quota.c \
  1385   1384     $(TOP)\src\test_rtree.c \
  1386   1385     $(TOP)\src\test_schema.c \
  1387   1386     $(TOP)\src\test_server.c \
  1388   1387     $(TOP)\src\test_superlock.c \
  1389   1388     $(TOP)\src\test_syscall.c \
         1389  +  $(TOP)\src\test_tclsh.c \
  1390   1390     $(TOP)\src\test_tclvar.c \
  1391   1391     $(TOP)\src\test_thread.c \
  1392   1392     $(TOP)\src\test_vfs.c \
  1393   1393     $(TOP)\src\test_windirent.c \
  1394   1394     $(TOP)\src\test_wsd.c \
  1395   1395     $(TOP)\ext\fts3\fts3_term.c \
  1396   1396     $(TOP)\ext\fts3\fts3_test.c \
................................................................................
  1487   1487   
  1488   1488   # executables needed for testing
  1489   1489   #
  1490   1490   TESTPROGS = \
  1491   1491     testfixture.exe \
  1492   1492     $(SQLITE3EXE) \
  1493   1493     sqlite3_analyzer.exe \
         1494  +  sqlite3_checker.exe \
  1494   1495     sqldiff.exe \
  1495   1496     dbhash.exe
  1496   1497   
  1497   1498   # Databases containing fuzzer test cases
  1498   1499   #
  1499   1500   FUZZDATA = \
  1500   1501     $(TOP)\test\fuzzdata1.db \
................................................................................
  1504   1505     $(TOP)\test\fuzzdata5.db
  1505   1506   # <</mark>>
  1506   1507   
  1507   1508   # Additional compiler options for the shell.  These are only effective
  1508   1509   # when the shell is not being dynamically linked.
  1509   1510   #
  1510   1511   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
  1511         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
         1512  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
         1513  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
  1512   1514   !ENDIF
  1513   1515   
  1514   1516   # <<mark>>
  1515   1517   # Extra compiler options for various test tools.
  1516   1518   #
  1517         -MPTESTER_COMPILE_OPTS = -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS5
         1519  +MPTESTER_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5
  1518   1520   FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1
  1519   1521   FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ -DSQLITE_MAX_MEMORY=50000000
  1520   1522   FUZZCHECK_SRC = $(TOP)\test\fuzzcheck.c $(TOP)\test\ossfuzz.c
  1521   1523   OSSSHELL_SRC = $(TOP)\test\ossshell.c $(TOP)\test\ossfuzz.c
  1522   1524   DBFUZZ_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION
  1523   1525   KV_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
  1524   1526   DBSELFTEST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
................................................................................
  1565   1567   sqlite3.def:	libsqlite3.lib
  1566   1568   	echo EXPORTS > sqlite3.def
  1567   1569   	dumpbin /all libsqlite3.lib \
  1568   1570   		| $(TCLSH_CMD) $(TOP)\tool\replace.tcl include "^\s+1 _?(sqlite3(?:session|changeset|changegroup)?_[^@]*)(?:@\d+)?$$" \1 \
  1569   1571   		| sort >> sqlite3.def
  1570   1572   # <</block2>>
  1571   1573   
  1572         -$(SQLITE3EXE):	$(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
  1573         -	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c $(SHELL_CORE_SRC) \
         1574  +$(SQLITE3EXE):	shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
         1575  +	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) shell.c $(SHELL_CORE_SRC) \
  1574   1576   		/link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
  1575   1577   
  1576   1578   # <<mark>>
  1577   1579   sqldiff.exe:	$(TOP)\tool\sqldiff.c $(SQLITE3C) $(SQLITE3H)
  1578   1580   	$(LTLINK) $(NO_WARN) $(TOP)\tool\sqldiff.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1579   1581   
  1580   1582   dbhash.exe:	$(TOP)\tool\dbhash.c $(SQLITE3C) $(SQLITE3H)
................................................................................
  1625   1627   # all that automatic generation.
  1626   1628   #
  1627   1629   .target_source:	$(SRC) $(TOP)\tool\vdbe-compress.tcl fts5.c $(SQLITE_TCL_DEP)
  1628   1630   	-rmdir /Q/S tsrc 2>NUL
  1629   1631   	-mkdir tsrc
  1630   1632   	for %i in ($(SRC00)) do copy /Y %i tsrc
  1631   1633   	for %i in ($(SRC01)) do copy /Y %i tsrc
  1632         -	for %i in ($(SRC02)) do copy /Y %i tsrc
  1633   1634   	for %i in ($(SRC03)) do copy /Y %i tsrc
  1634   1635   	for %i in ($(SRC04)) do copy /Y %i tsrc
  1635   1636   	for %i in ($(SRC05)) do copy /Y %i tsrc
  1636   1637   	for %i in ($(SRC06)) do copy /Y %i tsrc
  1637   1638   	for %i in ($(SRC07)) do copy /Y %i tsrc
  1638   1639   	for %i in ($(SRC08)) do copy /Y %i tsrc
  1639   1640   	for %i in ($(SRC09)) do copy /Y %i tsrc
................................................................................
  1645   1646   	del /Q tsrc\sqlite.h.in tsrc\parse.y 2>NUL
  1646   1647   	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl $(OPTS) < tsrc\vdbe.c > vdbe.new
  1647   1648   	move vdbe.new tsrc\vdbe.c
  1648   1649   	echo > .target_source
  1649   1650   
  1650   1651   sqlite3.c:	.target_source sqlite3ext.h $(MKSQLITE3C_TOOL)
  1651   1652   	$(TCLSH_CMD) $(MKSQLITE3C_TOOL) $(MKSQLITE3C_ARGS)
  1652         -	copy tsrc\shell.c .
  1653   1653   	copy $(TOP)\ext\session\sqlite3session.h .
  1654   1654   
  1655   1655   sqlite3-all.c:	sqlite3.c $(TOP)\tool\split-sqlite3c.tcl
  1656   1656   	$(TCLSH_CMD) $(TOP)\tool\split-sqlite3c.tcl
  1657   1657   # <</mark>>
  1658   1658   
  1659   1659   # Rule to build the amalgamation
................................................................................
  1743   1743   
  1744   1744   ctime.lo:	$(TOP)\src\ctime.c $(HDR)
  1745   1745   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\ctime.c
  1746   1746   
  1747   1747   date.lo:	$(TOP)\src\date.c $(HDR)
  1748   1748   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\date.c
  1749   1749   
  1750         -dbstat.lo:	$(TOP)\src\date.c $(HDR)
         1750  +dbpage.lo:	$(TOP)\src\dbpage.c $(HDR)
         1751  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbpage.c
         1752  +
         1753  +dbstat.lo:	$(TOP)\src\dbstat.c $(HDR)
  1751   1754   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbstat.c
  1752   1755   
  1753   1756   delete.lo:	$(TOP)\src\delete.c $(HDR)
  1754   1757   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\delete.c
  1755   1758   
  1756   1759   expr.lo:	$(TOP)\src\expr.c $(HDR)
  1757   1760   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\expr.c
................................................................................
  1927   1930   whereexpr.lo:	$(TOP)\src\whereexpr.c $(HDR)
  1928   1931   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\whereexpr.c
  1929   1932   
  1930   1933   tclsqlite.lo:	$(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP)
  1931   1934   	$(LTCOMPILE) $(NO_WARN) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1932   1935   
  1933   1936   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP)
  1934         -	$(LTCOMPILE) $(NO_WARN) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
         1937  +	$(LTCOMPILE) $(NO_WARN) -DTCLSH -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1935   1938   
  1936   1939   tclsqlite3.exe:	tclsqlite-shell.lo $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS)
  1937   1940   	$(LTLINK) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite-shell.lo $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1938   1941   
  1939   1942   # Rules to build opcodes.c and opcodes.h
  1940   1943   #
  1941   1944   opcodes.c:	opcodes.h $(TOP)\tool\mkopcodec.tcl
................................................................................
  1970   1973   mkkeywordhash.exe:	$(TOP)\tool\mkkeywordhash.c
  1971   1974   	$(BCC) $(NO_WARN) -Fe$@ $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) \
  1972   1975   		$(TOP)\tool\mkkeywordhash.c /link $(LDFLAGS) $(NLTLINKOPTS) $(NLTLIBPATHS)
  1973   1976   
  1974   1977   keywordhash.h:	$(TOP)\tool\mkkeywordhash.c mkkeywordhash.exe
  1975   1978   	.\mkkeywordhash.exe > keywordhash.h
  1976   1979   
         1980  +# Source files that go into making shell.c
         1981  +SHELL_SRC = \
         1982  +	$(TOP)\src\shell.c.in \
         1983  +	$(TOP)\ext\misc\shathree.c \
         1984  +	$(TOP)\ext\misc\fileio.c \
         1985  +	$(TOP)\ext\misc\completion.c
         1986  +
         1987  +shell.c:	$(SHELL_SRC) $(TOP)\tool\mkshellc.tcl
         1988  +	$(TCLSH_CMD) $(TOP)\tool\mkshellc.tcl > shell.c
  1977   1989   
  1978   1990   
  1979   1991   # Rules to build the extension objects.
  1980   1992   #
  1981   1993   icu.lo:	$(TOP)\ext\icu\icu.c $(HDR) $(EXTHDR)
  1982   1994   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\icu\icu.c
  1983   1995   
................................................................................
  2089   2101   # Rules to build the 'testfixture' application.
  2090   2102   #
  2091   2103   # If using the amalgamation, use sqlite3.c directly to build the test
  2092   2104   # fixture.  Otherwise link against libsqlite3.lib.  (This distinction is
  2093   2105   # necessary because the test fixture requires non-API symbols which are
  2094   2106   # hidden when the library is built via the amalgamation).
  2095   2107   #
  2096         -TESTFIXTURE_FLAGS = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
         2108  +TESTFIXTURE_FLAGS = -DTCLSH_INIT_PROC=sqlite3TestInit -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
  2097   2109   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE=""
  2098   2110   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CORE $(NO_WARN)
  2099   2111   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  2100   2112   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_DEFAULT_PAGE_SIZE=1024
  2101   2113   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB
         2114  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB
  2102   2115   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) $(TEST_CCONV_OPTS)
  2103   2116   
  2104   2117   TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2)
  2105   2118   TESTFIXTURE_SRC1 = $(TESTEXT) $(SQLITE3C)
  2106   2119   !IF $(USE_AMALGAMATION)==0
  2107   2120   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
  2108   2121   !ELSE
................................................................................
  2174   2187   	@set PATH=$(LIBTCLPATH);$(PATH)
  2175   2188   	.\testfixture.exe $(TOP)\test\veryquick.test $(TESTOPTS)
  2176   2189   
  2177   2190   smoketest:	$(TESTPROGS)
  2178   2191   	@set PATH=$(LIBTCLPATH);$(PATH)
  2179   2192   	.\testfixture.exe $(TOP)\test\main.test $(TESTOPTS)
  2180   2193   
  2181         -sqlite3_analyzer.c:	$(SQLITE3C) $(SQLITE3H) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl $(SQLITE_TCL_DEP)
  2182         -	echo #define TCLSH 2 > $@
  2183         -	echo #define SQLITE_ENABLE_DBSTAT_VTAB 1 >> $@
  2184         -	copy $@ + $(SQLITE3C) + $(TOP)\src\tclsqlite.c $@
  2185         -	echo static const char *tclsh_main_loop(void){ >> $@
  2186         -	echo static const char *zMainloop = >> $@
  2187         -	$(TCLSH_CMD) $(TOP)\tool\tostr.tcl $(TOP)\tool\spaceanal.tcl >> $@
  2188         -	echo ; return zMainloop; } >> $@
         2194  +sqlite3_analyzer.c:	$(SQLITE3C) $(SQLITE3H) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl $(TOP)\tool\mkccode.tcl $(TOP)\tool\sqlite3_analyzer.c.in $(SQLITE_TCL_DEP)
         2195  +	$(TCLSH_CMD) $(TOP)\tool\mkccode.tcl $(TOP)\tool\sqlite3_analyzer.c.in > $@
  2189   2196   
  2190   2197   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  2191   2198   	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_analyzer.c \
  2192   2199   		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  2193   2200   
         2201  +CHECKER_DEPS =\
         2202  +  $(TOP)/tool/mkccode.tcl \
         2203  +  sqlite3.c \
         2204  +  $(TOP)/src/tclsqlite.c \
         2205  +  $(TOP)/ext/repair/sqlite3_checker.tcl \
         2206  +  $(TOP)/ext/repair/checkindex.c \
         2207  +  $(TOP)/ext/repair/checkfreelist.c \
         2208  +  $(TOP)/ext/misc/btreeinfo.c \
         2209  +  $(TOP)/ext/repair/sqlite3_checker.c.in
         2210  +
         2211  +sqlite3_checker.c:	$(CHECKER_DEPS)
         2212  +	$(TCLSH_CMD) $(TOP)\tool\mkccode.tcl $(TOP)\ext\repair\sqlite3_checker.c.in > $@
         2213  +
         2214  +sqlite3_checker.exe:	sqlite3_checker.c $(LIBRESOBJS)
         2215  +	$(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_checker.c \
         2216  +		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
         2217  +
  2194   2218   dbdump.exe:	$(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H)
  2195   2219   	$(LTLINK) $(NO_WARN) -DDBDUMP_STANDALONE $(TOP)\ext\misc\dbdump.c $(SQLITE3C) \
  2196   2220   		/link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS)
  2197   2221   
  2198   2222   testloadext.lo:	$(TOP)\src\test_loadext.c
  2199   2223   	$(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c
  2200   2224   
................................................................................
  2213   2237   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2214   2238   		$(TOP)\tool\showjournal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2215   2239   
  2216   2240   showwal.exe:	$(TOP)\tool\showwal.c $(SQLITE3C) $(SQLITE3H)
  2217   2241   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2218   2242   		$(TOP)\tool\showwal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2219   2243   
         2244  +showshm.exe:	$(TOP)\tool\showshm.c
         2245  +	$(LTLINK) $(NO_WARN)	$(TOP)\tool\showshm.c /link $(LDFLAGS) $(LTLINKOPTS)
         2246  +
  2220   2247   changeset.exe:	$(TOP)\ext\session\changeset.c $(SQLITE3C) $(SQLITE3H)
  2221   2248   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2222   2249   		-DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
  2223   2250   		$(TOP)\ext\session\changeset.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2224   2251   
  2225   2252   fts3view.exe:	$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) $(SQLITE3H)
  2226   2253   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \

Changes to VERSION.

     1         -3.21.0
            1  +3.22.0

Changes to autoconf/Makefile.am.

     6      6   libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8
     7      7   
     8      8   bin_PROGRAMS = sqlite3
     9      9   sqlite3_SOURCES = shell.c sqlite3.h
    10     10   EXTRA_sqlite3_SOURCES = sqlite3.c
    11     11   sqlite3_LDADD = @EXTRA_SHELL_OBJ@ @READLINE_LIBS@
    12     12   sqlite3_DEPENDENCIES = @EXTRA_SHELL_OBJ@
    13         -sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS
           13  +sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB
    14     14   
    15     15   include_HEADERS = sqlite3.h sqlite3ext.h
    16     16   
    17     17   EXTRA_DIST = sqlite3.1 tea Makefile.msc sqlite3.rc README.txt Replace.cs
    18     18   pkgconfigdir = ${libdir}/pkgconfig
    19     19   pkgconfig_DATA = sqlite3.pc
    20     20   
    21     21   man_MANS = sqlite3.1

Changes to autoconf/Makefile.msc.

   923    923   !ENDIF
   924    924   
   925    925   
   926    926   # Additional compiler options for the shell.  These are only effective
   927    927   # when the shell is not being dynamically linked.
   928    928   #
   929    929   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
   930         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
          930  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
          931  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
   931    932   !ENDIF
   932    933   
   933    934   
   934    935   # This is the default Makefile target.  The objects listed here
   935    936   # are what get build when you type just "make" with no arguments.
   936    937   #
   937    938   all:	dll shell
................................................................................
   953    954   
   954    955   sqlite3.def:	Replace.exe $(LIBOBJ)
   955    956   	echo EXPORTS > sqlite3.def
   956    957   	dumpbin /all $(LIBOBJ) \
   957    958   		| .\Replace.exe "^\s+/EXPORT:_?(sqlite3(?:session|changeset|changegroup)?_[^@,]*)(?:@\d+|,DATA)?$$" $$1 true \
   958    959   		| sort >> sqlite3.def
   959    960   
   960         -$(SQLITE3EXE):	$(TOP)\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
   961         -	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\shell.c $(SHELL_CORE_SRC) \
          961  +$(SQLITE3EXE):	shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H)
          962  +	$(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) shell.c $(SHELL_CORE_SRC) \
   962    963   		/link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS)
   963    964   
   964    965   
   965    966   # Rule to build the amalgamation
   966    967   #
   967    968   sqlite3.lo:	$(SQLITE3C)
   968    969   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(SQLITE3C)
................................................................................
   969    970   
   970    971   
   971    972   # Rule to build the Win32 resources object file.
   972    973   #
   973    974   !IF $(USE_RC)!=0
   974    975   _HASHCHAR=^#
   975    976   !IF ![echo !IFNDEF VERSION > rcver.vc] && \
   976         -    ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| find "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \
          977  +    ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| "%SystemRoot%\System32\find.exe" "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \
   977    978       ![echo !ENDIF >> rcver.vc]
   978    979   !INCLUDE rcver.vc
   979    980   !ENDIF
   980    981   
   981    982   RESOURCE_VERSION = $(VERSION:^#=)
   982    983   RESOURCE_VERSION = $(RESOURCE_VERSION:define=)
   983    984   RESOURCE_VERSION = $(RESOURCE_VERSION:SQLITE_VERSION=)

Changes to autoconf/configure.ac.

     8      8   #   --enable-static-shell
     9      9   #   --enable-dynamic-extensions
    10     10   #
    11     11   
    12     12   AC_PREREQ(2.61)
    13     13   AC_INIT(sqlite, --SQLITE-VERSION--, http://www.sqlite.org)
    14     14   AC_CONFIG_SRCDIR([sqlite3.c])
           15  +AC_CONFIG_AUX_DIR([.])
    15     16   
    16     17   # Use automake.
    17     18   AM_INIT_AUTOMAKE([foreign])
    18     19   
    19     20   AC_SYS_LARGEFILE
    20     21   
    21     22   # Check for required programs.

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.21.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.22.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.21.0'
   730         -PACKAGE_STRING='sqlite 3.21.0'
          729  +PACKAGE_VERSION='3.22.0'
          730  +PACKAGE_STRING='sqlite 3.22.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
................................................................................
  1460   1460   #
  1461   1461   # Report the --help message.
  1462   1462   #
  1463   1463   if test "$ac_init_help" = "long"; then
  1464   1464     # Omit some internal or obsolete options to make the list less imposing.
  1465   1465     # This message is too long to be a string in the A/UX 3.1 sh.
  1466   1466     cat <<_ACEOF
  1467         -\`configure' configures sqlite 3.21.0 to adapt to many kinds of systems.
         1467  +\`configure' configures sqlite 3.22.0 to adapt to many kinds of systems.
  1468   1468   
  1469   1469   Usage: $0 [OPTION]... [VAR=VALUE]...
  1470   1470   
  1471   1471   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1472   1472   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1473   1473   
  1474   1474   Defaults for the options are specified in brackets.
................................................................................
  1525   1525     --build=BUILD     configure for building on BUILD [guessed]
  1526   1526     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1527   1527   _ACEOF
  1528   1528   fi
  1529   1529   
  1530   1530   if test -n "$ac_init_help"; then
  1531   1531     case $ac_init_help in
  1532         -     short | recursive ) echo "Configuration of sqlite 3.21.0:";;
         1532  +     short | recursive ) echo "Configuration of sqlite 3.22.0:";;
  1533   1533      esac
  1534   1534     cat <<\_ACEOF
  1535   1535   
  1536   1536   Optional Features:
  1537   1537     --disable-option-checking  ignore unrecognized --enable/--with options
  1538   1538     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1539   1539     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1650   1650       cd "$ac_pwd" || { ac_status=$?; break; }
  1651   1651     done
  1652   1652   fi
  1653   1653   
  1654   1654   test -n "$ac_init_help" && exit $ac_status
  1655   1655   if $ac_init_version; then
  1656   1656     cat <<\_ACEOF
  1657         -sqlite configure 3.21.0
         1657  +sqlite configure 3.22.0
  1658   1658   generated by GNU Autoconf 2.69
  1659   1659   
  1660   1660   Copyright (C) 2012 Free Software Foundation, Inc.
  1661   1661   This configure script is free software; the Free Software Foundation
  1662   1662   gives unlimited permission to copy, distribute and modify it.
  1663   1663   _ACEOF
  1664   1664     exit
................................................................................
  2069   2069     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2070   2070   
  2071   2071   } # ac_fn_c_check_header_mongrel
  2072   2072   cat >config.log <<_ACEOF
  2073   2073   This file contains any messages produced by compilers while
  2074   2074   running configure, to aid debugging if configure makes a mistake.
  2075   2075   
  2076         -It was created by sqlite $as_me 3.21.0, which was
         2076  +It was created by sqlite $as_me 3.22.0, which was
  2077   2077   generated by GNU Autoconf 2.69.  Invocation command line was
  2078   2078   
  2079   2079     $ $0 $@
  2080   2080   
  2081   2081   _ACEOF
  2082   2082   exec 5>>config.log
  2083   2083   {
................................................................................
 12163  12163   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12164  12164   
 12165  12165   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12166  12166   # Save the log message, to keep $0 and so on meaningful, and to
 12167  12167   # report actual input values of CONFIG_FILES etc. instead of their
 12168  12168   # values after options handling.
 12169  12169   ac_log="
 12170         -This file was extended by sqlite $as_me 3.21.0, which was
        12170  +This file was extended by sqlite $as_me 3.22.0, which was
 12171  12171   generated by GNU Autoconf 2.69.  Invocation command line was
 12172  12172   
 12173  12173     CONFIG_FILES    = $CONFIG_FILES
 12174  12174     CONFIG_HEADERS  = $CONFIG_HEADERS
 12175  12175     CONFIG_LINKS    = $CONFIG_LINKS
 12176  12176     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12177  12177     $ $0 $@
................................................................................
 12229  12229   
 12230  12230   Report bugs to the package provider."
 12231  12231   
 12232  12232   _ACEOF
 12233  12233   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12234  12234   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12235  12235   ac_cs_version="\\
 12236         -sqlite config.status 3.21.0
        12236  +sqlite config.status 3.22.0
 12237  12237   configured by $0, generated by GNU Autoconf 2.69,
 12238  12238     with options \\"\$ac_cs_config\\"
 12239  12239   
 12240  12240   Copyright (C) 2012 Free Software Foundation, Inc.
 12241  12241   This config.status script is free software; the Free Software Foundation
 12242  12242   gives unlimited permission to copy, distribute and modify it."
 12243  12243   

Changes to ext/fts5/fts5_vocab.c.

   679    679       assert( iCol==1 || iCol==2 );
   680    680       if( iCol==1 ){
   681    681         iVal = pCsr->aDoc[0];
   682    682       }else{
   683    683         iVal = pCsr->aCnt[0];
   684    684       }
   685    685     }else{
   686         -    int eDetail = pCsr->pConfig->eDetail;
   687    686       assert( eType==FTS5_VOCAB_INSTANCE );
   688    687       switch( iCol ){
   689    688         case 1:
   690    689           sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
   691    690           break;
   692    691         case 2: {
   693    692           int ii = -1;
   694    693           if( eDetail==FTS5_DETAIL_FULL ){
   695    694             ii = FTS5_POS2COLUMN(pCsr->iInstPos);
   696    695           }else if( eDetail==FTS5_DETAIL_COLUMNS ){
   697         -          ii = pCsr->iInstPos;
          696  +          ii = (int)pCsr->iInstPos;
   698    697           }
   699    698           if( ii>=0 && ii<pCsr->pConfig->nCol ){
   700    699             const char *z = pCsr->pConfig->azCol[ii];
   701    700             sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
   702    701           }
   703    702           break;
   704    703         }

Changes to ext/lsm1/lsm_shared.c.

   336    336     const int nUsMax = 100000;      /* Max value for nUs */
   337    337     int nUs = 1000;                 /* us to wait between DMS1 attempts */
   338    338     int rc;
   339    339   
   340    340     /* Obtain a pointer to the shared-memory header */
   341    341     assert( pDb->pShmhdr==0 );
   342    342     assert( pDb->bReadonly==0 );
   343         -  rc = lsmShmCacheChunks(pDb, 1);
   344         -  if( rc!=LSM_OK ) return rc;
   345         -  pDb->pShmhdr = (ShmHeader *)pDb->apShm[0];
   346    343   
   347    344     /* Block for an exclusive lock on DMS1. This lock serializes all calls
   348    345     ** to doDbConnect() and doDbDisconnect() across all processes.  */
   349    346     while( 1 ){
   350    347       rc = lsmShmLock(pDb, LSM_LOCK_DMS1, LSM_LOCK_EXCL, 1);
   351    348       if( rc!=LSM_BUSY ) break;
   352    349       lsmEnvSleep(pDb->pEnv, nUs);
   353    350       nUs = nUs * 2;
   354    351       if( nUs>nUsMax ) nUs = nUsMax;
   355    352     }
   356         -  if( rc!=LSM_OK ){
   357         -    pDb->pShmhdr = 0;
   358         -    return rc;
          353  +  if( rc==LSM_OK ){
          354  +    rc = lsmShmCacheChunks(pDb, 1);
   359    355     }
          356  +  if( rc!=LSM_OK ) return rc;
          357  +  pDb->pShmhdr = (ShmHeader *)pDb->apShm[0];
   360    358   
   361    359     /* Try an exclusive lock on DMS2/DMS3. If successful, this is the first 
   362    360     ** and only connection to the database. In this case initialize the 
   363    361     ** shared-memory and run log file recovery.  */
   364    362     assert( LSM_LOCK_DMS3==1+LSM_LOCK_DMS2 );
   365    363     rc = lsmShmTestLock(pDb, LSM_LOCK_DMS2, 2, LSM_LOCK_EXCL);
   366    364     if( rc==LSM_OK ){
................................................................................
   518    516       rc = lsmFsOpen(pDb, zName, p->bReadonly);
   519    517     }
   520    518   
   521    519     /* If the db handle is read-write, then connect to the system now. Run
   522    520     ** recovery as necessary. Or, if this is a read-only database handle,
   523    521     ** defer attempting to connect to the system until a read-transaction
   524    522     ** is opened.  */
   525         -  if( pDb->bReadonly==0 ){
   526         -    if( rc==LSM_OK ){
   527         -      rc = lsmFsConfigure(pDb);
   528         -    }
   529         -    if( rc==LSM_OK ){
   530         -      rc = doDbConnect(pDb);
   531         -    }
          523  +  if( rc==LSM_OK ){
          524  +    rc = lsmFsConfigure(pDb);
          525  +  }
          526  +  if( rc==LSM_OK && pDb->bReadonly==0 ){
          527  +    rc = doDbConnect(pDb);
   532    528     }
   533    529   
   534    530     return rc;
   535    531   }
   536    532   
   537    533   static void dbDeferClose(lsm_db *pDb){
   538    534     if( pDb->pFS ){

Added ext/misc/btreeinfo.c.

            1  +/*
            2  +** 2017-10-24
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains an implementation of the "sqlite_btreeinfo" virtual table.
           14  +**
           15  +** The sqlite_btreeinfo virtual table is a read-only eponymous-only virtual
           16  +** table that shows information about all btrees in an SQLite database file.
           17  +** The schema is like this:
           18  +**
           19  +** CREATE TABLE sqlite_btreeinfo(
           20  +**    type TEXT,                   -- "table" or "index"
           21  +**    name TEXT,                   -- Name of table or index for this btree.
           22  +**    tbl_name TEXT,               -- Associated table
           23  +**    rootpage INT,                -- The root page of the btree
           24  +**    sql TEXT,                    -- SQL for this btree - from sqlite_master
           25  +**    hasRowid BOOLEAN,            -- True if the btree has a rowid
           26  +**    nEntry INT,                  -- Estimated number of enteries
           27  +**    nPage INT,                   -- Estimated number of pages
           28  +**    depth INT,                   -- Depth of the btree
           29  +**    szPage INT,                  -- Size of each page in bytes
           30  +**    zSchema TEXT HIDDEN          -- The schema to which this btree belongs
           31  +** );
           32  +**
           33  +** The first 5 fields are taken directly from the sqlite_master table.
           34  +** Considering only the first 5 fields, the only difference between 
           35  +** this virtual table and the sqlite_master table is that this virtual
           36  +** table omits all entries that have a 0 or NULL rowid - in other words
           37  +** it omits triggers and views.
           38  +**
           39  +** The value added by this table comes in the next 5 fields.
           40  +**
           41  +** Note that nEntry and nPage are *estimated*.  They are computed doing
           42  +** a single search from the root to a leaf, counting the number of cells
           43  +** at each level, and assuming that unvisited pages have a similar number
           44  +** of cells.
           45  +**
           46  +** The sqlite_dbpage virtual table must be available for this virtual table
           47  +** to operate.
           48  +**
           49  +** USAGE EXAMPLES:
           50  +**
           51  +** Show the table btrees in a schema order with the tables with the most
           52  +** rows occuring first:
           53  +**
           54  +**      SELECT name, nEntry
           55  +**        FROM sqlite_btreeinfo
           56  +**       WHERE type='table'
           57  +**       ORDER BY nEntry DESC, name;
           58  +**
           59  +** Show the names of all WITHOUT ROWID tables: 
           60  +**
           61  +**      SELECT name FROM sqlite_btreeinfo
           62  +**       WHERE type='table' AND NOT hasRowid;
           63  +*/
           64  +#if !defined(SQLITEINT_H)
           65  +#include "sqlite3ext.h"
           66  +#endif
           67  +SQLITE_EXTENSION_INIT1
           68  +#include <string.h>
           69  +#include <assert.h>
           70  +
           71  +/* Columns available in this virtual table */
           72  +#define BINFO_COLUMN_TYPE         0
           73  +#define BINFO_COLUMN_NAME         1
           74  +#define BINFO_COLUMN_TBL_NAME     2
           75  +#define BINFO_COLUMN_ROOTPAGE     3
           76  +#define BINFO_COLUMN_SQL          4
           77  +#define BINFO_COLUMN_HASROWID     5
           78  +#define BINFO_COLUMN_NENTRY       6
           79  +#define BINFO_COLUMN_NPAGE        7
           80  +#define BINFO_COLUMN_DEPTH        8
           81  +#define BINFO_COLUMN_SZPAGE       9
           82  +#define BINFO_COLUMN_SCHEMA      10
           83  +
           84  +/* Forward declarations */
           85  +typedef struct BinfoTable BinfoTable;
           86  +typedef struct BinfoCursor BinfoCursor;
           87  +
           88  +/* A cursor for the sqlite_btreeinfo table */
           89  +struct BinfoCursor {
           90  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
           91  +  sqlite3_stmt *pStmt;            /* Query against sqlite_master */
           92  +  int rc;                         /* Result of previous sqlite_step() call */
           93  +  int hasRowid;                   /* hasRowid value.  Negative if unknown. */
           94  +  sqlite3_int64 nEntry;           /* nEntry value */
           95  +  int nPage;                      /* nPage value */
           96  +  int depth;                      /* depth value */
           97  +  int szPage;                     /* size of a btree page.  0 if unknown */
           98  +  char *zSchema;                  /* Schema being interrogated */
           99  +};
          100  +
          101  +/* The sqlite_btreeinfo table */
          102  +struct BinfoTable {
          103  +  sqlite3_vtab base;              /* Base class.  Must be first */
          104  +  sqlite3 *db;                    /* The databse connection */
          105  +};
          106  +
          107  +/*
          108  +** Connect to the sqlite_btreeinfo virtual table.
          109  +*/
          110  +static int binfoConnect(
          111  +  sqlite3 *db,
          112  +  void *pAux,
          113  +  int argc, const char *const*argv,
          114  +  sqlite3_vtab **ppVtab,
          115  +  char **pzErr
          116  +){
          117  +  BinfoTable *pTab = 0;
          118  +  int rc = SQLITE_OK;
          119  +  rc = sqlite3_declare_vtab(db, 
          120  +      "CREATE TABLE x(\n"
          121  +      " type TEXT,\n"
          122  +      " name TEXT,\n"
          123  +      " tbl_name TEXT,\n"
          124  +      " rootpage INT,\n"
          125  +      " sql TEXT,\n"
          126  +      " hasRowid BOOLEAN,\n"
          127  +      " nEntry INT,\n"
          128  +      " nPage INT,\n"
          129  +      " depth INT,\n"
          130  +      " szPage INT,\n"
          131  +      " zSchema TEXT HIDDEN\n"
          132  +      ")");
          133  +  if( rc==SQLITE_OK ){
          134  +    pTab = (BinfoTable *)sqlite3_malloc64(sizeof(BinfoTable));
          135  +    if( pTab==0 ) rc = SQLITE_NOMEM;
          136  +  }
          137  +  assert( rc==SQLITE_OK || pTab==0 );
          138  +  if( pTab ){
          139  +    pTab->db = db;
          140  +  }
          141  +  *ppVtab = (sqlite3_vtab*)pTab;
          142  +  return rc;
          143  +}
          144  +
          145  +/*
          146  +** Disconnect from or destroy a btreeinfo virtual table.
          147  +*/
          148  +static int binfoDisconnect(sqlite3_vtab *pVtab){
          149  +  sqlite3_free(pVtab);
          150  +  return SQLITE_OK;
          151  +}
          152  +
          153  +/*
          154  +** idxNum:
          155  +**
          156  +**     0     Use "main" for the schema
          157  +**     1     Schema identified by parameter ?1
          158  +*/
          159  +static int binfoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
          160  +  int i;
          161  +  pIdxInfo->estimatedCost = 10000.0;  /* Cost estimate */
          162  +  pIdxInfo->estimatedRows = 100;
          163  +  for(i=0; i<pIdxInfo->nConstraint; i++){
          164  +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
          165  +    if( p->usable
          166  +     && p->iColumn==BINFO_COLUMN_SCHEMA
          167  +     && p->op==SQLITE_INDEX_CONSTRAINT_EQ
          168  +    ){
          169  +      pIdxInfo->estimatedCost = 1000.0;
          170  +      pIdxInfo->idxNum = 1;
          171  +      pIdxInfo->aConstraintUsage[i].argvIndex = 1;
          172  +      pIdxInfo->aConstraintUsage[i].omit = 1;
          173  +      break;
          174  +    }
          175  +  }
          176  +  return SQLITE_OK;
          177  +}
          178  +
          179  +/*
          180  +** Open a new btreeinfo cursor.
          181  +*/
          182  +static int binfoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
          183  +  BinfoCursor *pCsr;
          184  +
          185  +  pCsr = (BinfoCursor *)sqlite3_malloc64(sizeof(BinfoCursor));
          186  +  if( pCsr==0 ){
          187  +    return SQLITE_NOMEM;
          188  +  }else{
          189  +    memset(pCsr, 0, sizeof(BinfoCursor));
          190  +    pCsr->base.pVtab = pVTab;
          191  +  }
          192  +
          193  +  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
          194  +  return SQLITE_OK;
          195  +}
          196  +
          197  +/*
          198  +** Close a btreeinfo cursor.
          199  +*/
          200  +static int binfoClose(sqlite3_vtab_cursor *pCursor){
          201  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          202  +  sqlite3_finalize(pCsr->pStmt);
          203  +  sqlite3_free(pCsr->zSchema);
          204  +  sqlite3_free(pCsr);
          205  +  return SQLITE_OK;
          206  +}
          207  +
          208  +/*
          209  +** Move a btreeinfo cursor to the next entry in the file.
          210  +*/
          211  +static int binfoNext(sqlite3_vtab_cursor *pCursor){
          212  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          213  +  pCsr->rc = sqlite3_step(pCsr->pStmt);
          214  +  pCsr->hasRowid = -1;
          215  +  return pCsr->rc==SQLITE_ERROR ? SQLITE_ERROR : SQLITE_OK;
          216  +}
          217  +
          218  +/* We have reached EOF if previous sqlite3_step() returned
          219  +** anything other than SQLITE_ROW;
          220  +*/
          221  +static int binfoEof(sqlite3_vtab_cursor *pCursor){
          222  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          223  +  return pCsr->rc!=SQLITE_ROW;
          224  +}
          225  +
          226  +/* Position a cursor back to the beginning.
          227  +*/
          228  +static int binfoFilter(
          229  +  sqlite3_vtab_cursor *pCursor, 
          230  +  int idxNum, const char *idxStr,
          231  +  int argc, sqlite3_value **argv
          232  +){
          233  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          234  +  BinfoTable *pTab = (BinfoTable *)pCursor->pVtab;
          235  +  char *zSql;
          236  +  int rc;
          237  +
          238  +  sqlite3_free(pCsr->zSchema);
          239  +  if( idxNum==1 && sqlite3_value_type(argv[0])!=SQLITE_NULL ){
          240  +    pCsr->zSchema = sqlite3_mprintf("%s", sqlite3_value_text(argv[0]));
          241  +  }else{
          242  +    pCsr->zSchema = sqlite3_mprintf("main");
          243  +  }
          244  +  zSql = sqlite3_mprintf(
          245  +      "SELECT 0, 'table','sqlite_master','sqlite_master',1,NULL "
          246  +      "UNION ALL "
          247  +      "SELECT rowid, type, name, tbl_name, rootpage, sql"
          248  +      " FROM \"%w\".sqlite_master WHERE rootpage>=1",
          249  +       pCsr->zSchema);
          250  +  sqlite3_finalize(pCsr->pStmt);
          251  +  pCsr->pStmt = 0;
          252  +  pCsr->hasRowid = -1;
          253  +  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
          254  +  sqlite3_free(zSql);
          255  +  if( rc==SQLITE_OK ){
          256  +    rc = binfoNext(pCursor);
          257  +  }
          258  +  return rc;
          259  +}
          260  +
          261  +/* Decode big-endian integers */
          262  +static unsigned int get_uint16(unsigned char *a){
          263  +  return (a[0]<<8)|a[1];
          264  +}
          265  +static unsigned int get_uint32(unsigned char *a){
          266  +  return (a[0]<<24)|(a[1]<<16)|(a[2]<<8)|a[3];
          267  +}
          268  +
          269  +/* Examine the b-tree rooted at pgno and estimate its size.
          270  +** Return non-zero if anything goes wrong.
          271  +*/
          272  +static int binfoCompute(sqlite3 *db, int pgno, BinfoCursor *pCsr){
          273  +  sqlite3_int64 nEntry = 1;
          274  +  int nPage = 1;
          275  +  unsigned char *aData;
          276  +  sqlite3_stmt *pStmt = 0;
          277  +  int rc = SQLITE_OK;
          278  +  int pgsz = 0;
          279  +  int nCell;
          280  +  int iCell;
          281  +
          282  +  rc = sqlite3_prepare_v2(db, 
          283  +           "SELECT data FROM sqlite_dbpage('main') WHERE pgno=?1", -1,
          284  +           &pStmt, 0);
          285  +  if( rc ) return rc;
          286  +  pCsr->depth = 1;
          287  +  while(1){
          288  +    sqlite3_bind_int(pStmt, 1, pgno);
          289  +    rc = sqlite3_step(pStmt);
          290  +    if( rc!=SQLITE_ROW ){
          291  +      rc = SQLITE_ERROR;
          292  +      break;
          293  +    }
          294  +    pCsr->szPage = pgsz = sqlite3_column_bytes(pStmt, 0);
          295  +    aData = (unsigned char*)sqlite3_column_blob(pStmt, 0);
          296  +    if( aData==0 ){    
          297  +      rc = SQLITE_NOMEM;
          298  +      break;
          299  +    }
          300  +    if( pgno==1 ){
          301  +      aData += 100;
          302  +      pgsz -= 100;
          303  +    }
          304  +    pCsr->hasRowid = aData[0]!=2 && aData[0]!=10;
          305  +    nCell = get_uint16(aData+3);
          306  +    nEntry *= (nCell+1);
          307  +    if( aData[0]==10 || aData[0]==13 ) break;
          308  +    nPage *= (nCell+1);
          309  +    if( nCell<=1 ){
          310  +      pgno = get_uint32(aData+8);
          311  +    }else{
          312  +      iCell = get_uint16(aData+12+2*(nCell/2));
          313  +      if( pgno==1 ) iCell -= 100;
          314  +      if( iCell<=12 || iCell>=pgsz-4 ){
          315  +        rc = SQLITE_CORRUPT;
          316  +        break;
          317  +      }
          318  +      pgno = get_uint32(aData+iCell);
          319  +    }
          320  +    pCsr->depth++;
          321  +    sqlite3_reset(pStmt);
          322  +  }
          323  +  sqlite3_finalize(pStmt);
          324  +  pCsr->nPage = nPage;
          325  +  pCsr->nEntry = nEntry;
          326  +  if( rc==SQLITE_ROW ) rc = SQLITE_OK;
          327  +  return rc;
          328  +}
          329  +
          330  +/* Return a column for the sqlite_btreeinfo table */
          331  +static int binfoColumn(
          332  +  sqlite3_vtab_cursor *pCursor, 
          333  +  sqlite3_context *ctx, 
          334  +  int i
          335  +){
          336  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          337  +  if( i>=BINFO_COLUMN_HASROWID && i<=BINFO_COLUMN_SZPAGE && pCsr->hasRowid<0 ){
          338  +    int pgno = sqlite3_column_int(pCsr->pStmt, BINFO_COLUMN_ROOTPAGE+1);
          339  +    sqlite3 *db = sqlite3_context_db_handle(ctx);
          340  +    int rc = binfoCompute(db, pgno, pCsr);
          341  +    if( rc ){
          342  +      return rc;
          343  +    }
          344  +  }
          345  +  switch( i ){
          346  +    case BINFO_COLUMN_NAME:
          347  +    case BINFO_COLUMN_TYPE:
          348  +    case BINFO_COLUMN_TBL_NAME:
          349  +    case BINFO_COLUMN_ROOTPAGE:
          350  +    case BINFO_COLUMN_SQL: {
          351  +      sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1));
          352  +      break;
          353  +    }
          354  +    case BINFO_COLUMN_HASROWID: {
          355  +      sqlite3_result_int(ctx, pCsr->hasRowid);
          356  +      break;
          357  +    }
          358  +    case BINFO_COLUMN_NENTRY: {
          359  +      sqlite3_result_int64(ctx, pCsr->nEntry);
          360  +      break;
          361  +    }
          362  +    case BINFO_COLUMN_NPAGE: {
          363  +      sqlite3_result_int(ctx, pCsr->nPage);
          364  +      break;
          365  +    }
          366  +    case BINFO_COLUMN_DEPTH: {
          367  +      sqlite3_result_int(ctx, pCsr->depth);
          368  +      break;
          369  +    }
          370  +    case BINFO_COLUMN_SCHEMA: {
          371  +      sqlite3_result_text(ctx, pCsr->zSchema, -1, SQLITE_STATIC);
          372  +      break;
          373  +    }
          374  +  }
          375  +  return SQLITE_OK;
          376  +}
          377  +
          378  +/* Return the ROWID for the sqlite_btreeinfo table */
          379  +static int binfoRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
          380  +  BinfoCursor *pCsr = (BinfoCursor *)pCursor;
          381  +  *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
          382  +  return SQLITE_OK;
          383  +}
          384  +
          385  +/*
          386  +** Invoke this routine to register the "sqlite_btreeinfo" virtual table module
          387  +*/
          388  +int sqlite3BinfoRegister(sqlite3 *db){
          389  +  static sqlite3_module binfo_module = {
          390  +    0,                           /* iVersion */
          391  +    0,                           /* xCreate */
          392  +    binfoConnect,                /* xConnect */
          393  +    binfoBestIndex,              /* xBestIndex */
          394  +    binfoDisconnect,             /* xDisconnect */
          395  +    0,                           /* xDestroy */
          396  +    binfoOpen,                   /* xOpen - open a cursor */
          397  +    binfoClose,                  /* xClose - close a cursor */
          398  +    binfoFilter,                 /* xFilter - configure scan constraints */
          399  +    binfoNext,                   /* xNext - advance a cursor */
          400  +    binfoEof,                    /* xEof - check for end of scan */
          401  +    binfoColumn,                 /* xColumn - read data */
          402  +    binfoRowid,                  /* xRowid - read data */
          403  +    0,                           /* xUpdate */
          404  +    0,                           /* xBegin */
          405  +    0,                           /* xSync */
          406  +    0,                           /* xCommit */
          407  +    0,                           /* xRollback */
          408  +    0,                           /* xFindMethod */
          409  +    0,                           /* xRename */
          410  +    0,                           /* xSavepoint */
          411  +    0,                           /* xRelease */
          412  +    0,                           /* xRollbackTo */
          413  +  };
          414  +  return sqlite3_create_module(db, "sqlite_btreeinfo", &binfo_module, 0);
          415  +}
          416  +
          417  +#ifdef _WIN32
          418  +__declspec(dllexport)
          419  +#endif
          420  +int sqlite3_btreeinfo_init(
          421  +  sqlite3 *db, 
          422  +  char **pzErrMsg, 
          423  +  const sqlite3_api_routines *pApi
          424  +){
          425  +  SQLITE_EXTENSION_INIT2(pApi);
          426  +  return sqlite3BinfoRegister(db);
          427  +}

Changes to ext/misc/spellfix.c.

  1118   1118   */
  1119   1119   static int editDist3Install(sqlite3 *db){
  1120   1120     int rc;
  1121   1121     EditDist3Config *pConfig = sqlite3_malloc64( sizeof(*pConfig) );
  1122   1122     if( pConfig==0 ) return SQLITE_NOMEM;
  1123   1123     memset(pConfig, 0, sizeof(*pConfig));
  1124   1124     rc = sqlite3_create_function_v2(db, "editdist3",
  1125         -              2, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0, 0);
         1125  +              2, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
         1126  +              editDist3SqlFunc, 0, 0, 0);
  1126   1127     if( rc==SQLITE_OK ){
  1127   1128       rc = sqlite3_create_function_v2(db, "editdist3",
  1128         -                3, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0, 0);
         1129  +                3, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
         1130  +                editDist3SqlFunc, 0, 0, 0);
  1129   1131     }
  1130   1132     if( rc==SQLITE_OK ){
  1131   1133       rc = sqlite3_create_function_v2(db, "editdist3",
  1132         -                1, SQLITE_UTF8, pConfig, editDist3SqlFunc, 0, 0,
  1133         -                editDist3ConfigDelete);
         1134  +                1, SQLITE_UTF8|SQLITE_DETERMINISTIC, pConfig,
         1135  +                editDist3SqlFunc, 0, 0, editDist3ConfigDelete);
  1134   1136     }else{
  1135   1137       sqlite3_free(pConfig);
  1136   1138     }
  1137   1139     return rc;
  1138   1140   }
  1139   1141   /* End configurable cost unicode edit distance routines
  1140   1142   ******************************************************************************
................................................................................
  2891   2893   
  2892   2894   /*
  2893   2895   ** Register the various functions and the virtual table.
  2894   2896   */
  2895   2897   static int spellfix1Register(sqlite3 *db){
  2896   2898     int rc = SQLITE_OK;
  2897   2899     int i;
  2898         -  rc = sqlite3_create_function(db, "spellfix1_translit", 1, SQLITE_UTF8, 0,
  2899         -                                  transliterateSqlFunc, 0, 0);
         2900  +  rc = sqlite3_create_function(db, "spellfix1_translit", 1,
         2901  +                               SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
         2902  +                                transliterateSqlFunc, 0, 0);
  2900   2903     if( rc==SQLITE_OK ){
  2901         -    rc = sqlite3_create_function(db, "spellfix1_editdist", 2, SQLITE_UTF8, 0,
         2904  +    rc = sqlite3_create_function(db, "spellfix1_editdist", 2,
         2905  +                                 SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
  2902   2906                                     editdistSqlFunc, 0, 0);
  2903   2907     }
  2904   2908     if( rc==SQLITE_OK ){
  2905         -    rc = sqlite3_create_function(db, "spellfix1_phonehash", 1, SQLITE_UTF8, 0,
         2909  +    rc = sqlite3_create_function(db, "spellfix1_phonehash", 1,
         2910  +                                 SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
  2906   2911                                     phoneticHashSqlFunc, 0, 0);
  2907   2912     }
  2908   2913     if( rc==SQLITE_OK ){
  2909         -    rc = sqlite3_create_function(db, "spellfix1_scriptcode", 1, SQLITE_UTF8, 0,
         2914  +    rc = sqlite3_create_function(db, "spellfix1_scriptcode", 1,
         2915  +                                  SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
  2910   2916                                     scriptCodeSqlFunc, 0, 0);
  2911   2917     }
  2912   2918     if( rc==SQLITE_OK ){
  2913   2919       rc = sqlite3_create_module(db, "spellfix1", &spellfix1Module, 0);
  2914   2920     }
  2915   2921     if( rc==SQLITE_OK ){
  2916   2922       rc = editDist3Install(db);

Changes to ext/rbu/sqlite3rbu.c.

    92     92   #if defined(_WIN32_WCE)
    93     93   #include "windows.h"
    94     94   #endif
    95     95   
    96     96   /* Maximum number of prepared UPDATE statements held by this module */
    97     97   #define SQLITE_RBU_UPDATE_CACHESIZE 16
    98     98   
           99  +/* Delta checksums disabled by default.  Compile with -DRBU_ENABLE_DELTA_CKSUM
          100  +** to enable checksum verification.
          101  +*/
          102  +#ifndef RBU_ENABLE_DELTA_CKSUM
          103  +# define RBU_ENABLE_DELTA_CKSUM 0
          104  +#endif
          105  +
    99    106   /*
   100    107   ** Swap two objects of type TYPE.
   101    108   */
   102    109   #if !defined(SQLITE_AMALGAMATION)
   103    110   # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
   104    111   #endif
   105    112   
................................................................................
   466    473     }
   467    474     z--;
   468    475     *pLen -= z - zStart;
   469    476     *pz = (char*)z;
   470    477     return v;
   471    478   }
   472    479   
          480  +#if RBU_ENABLE_DELTA_CKSUM
   473    481   /*
   474    482   ** Compute a 32-bit checksum on the N-byte buffer.  Return the result.
   475    483   */
   476    484   static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
   477    485     const unsigned char *z = (const unsigned char *)zIn;
   478    486     unsigned sum0 = 0;
   479    487     unsigned sum1 = 0;
................................................................................
   500    508       case 3:   sum3 += (z[2] << 8);
   501    509       case 2:   sum3 += (z[1] << 16);
   502    510       case 1:   sum3 += (z[0] << 24);
   503    511       default:  ;
   504    512     }
   505    513     return sum3;
   506    514   }
          515  +#endif
   507    516   
   508    517   /*
   509    518   ** Apply a delta.
   510    519   **
   511    520   ** The output buffer should be big enough to hold the whole output
   512    521   ** file and a NUL terminator at the end.  The delta_output_size()
   513    522   ** routine will determine this size for you.
................................................................................
   530    539     int lenSrc,            /* Length of the source file */
   531    540     const char *zDelta,    /* Delta to apply to the pattern */
   532    541     int lenDelta,          /* Length of the delta */
   533    542     char *zOut             /* Write the output into this preallocated buffer */
   534    543   ){
   535    544     unsigned int limit;
   536    545     unsigned int total = 0;
   537         -#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
          546  +#if RBU_ENABLE_DELTA_CKSUM
   538    547     char *zOrigOut = zOut;
   539    548   #endif
   540    549   
   541    550     limit = rbuDeltaGetInt(&zDelta, &lenDelta);
   542    551     if( *zDelta!='\n' ){
   543    552       /* ERROR: size integer not terminated by "\n" */
   544    553       return -1;
................................................................................
   585    594           zDelta += cnt;
   586    595           lenDelta -= cnt;
   587    596           break;
   588    597         }
   589    598         case ';': {
   590    599           zDelta++; lenDelta--;
   591    600           zOut[0] = 0;
   592         -#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
          601  +#if RBU_ENABLE_DELTA_CKSUM
   593    602           if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
   594    603             /* ERROR:  bad checksum */
   595    604             return -1;
   596    605           }
   597    606   #endif
   598    607           if( total!=limit ){
   599    608             /* ERROR: generated size does not match predicted size */

Added ext/repair/README.md.

            1  +This folder contains extensions and utility programs intended to analyze
            2  +live database files, detect problems, and possibly fix them.
            3  +
            4  +As SQLite is being used on larger and larger databases, database sizes
            5  +are growing into the terabyte range.  At that size, hardware malfunctions
            6  +and/or cosmic rays will occasionally corrupt a database file.  Detecting 
            7  +problems and fixing errors a terabyte-sized databases can take hours or days,
            8  +and it is undesirable to take applications that depend on the databases 
            9  +off-line for such a long time.
           10  +The utilities in the folder are intended to provide mechanisms for
           11  +detecting and fixing problems in large databases while those databases
           12  +are in active use.
           13  +
           14  +The utilities and extensions in this folder are experimental and under
           15  +active development at the time of this writing (2017-10-12).  If and when
           16  +they stabilize, this README will be updated to reflect that fact.

Added ext/repair/checkfreelist.c.

            1  +/*
            2  +** 2017 October 11
            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 module exports a single C function:
           14  +**
           15  +**   int sqlite3_check_freelist(sqlite3 *db, const char *zDb);
           16  +**
           17  +** This function checks the free-list in database zDb (one of "main", 
           18  +** "temp", etc.) and reports any errors by invoking the sqlite3_log()
           19  +** function. It returns SQLITE_OK if successful, or an SQLite error
           20  +** code otherwise. It is not an error if the free-list is corrupted but
           21  +** no IO or OOM errors occur.
           22  +**
           23  +** If this file is compiled and loaded as an SQLite loadable extension,
           24  +** it adds an SQL function "checkfreelist" to the database handle, to
           25  +** be invoked as follows:
           26  +**
           27  +**   SELECT checkfreelist(<database-name>);
           28  +**
           29  +** This function performs the same checks as sqlite3_check_freelist(),
           30  +** except that it returns all error messages as a single text value,
           31  +** separated by newline characters. If the freelist is not corrupted
           32  +** in any way, an empty string is returned.
           33  +**
           34  +** To compile this module for use as an SQLite loadable extension:
           35  +**
           36  +**   gcc -Os -fPIC -shared checkfreelist.c -o checkfreelist.so
           37  +*/
           38  +
           39  +#include "sqlite3ext.h"
           40  +SQLITE_EXTENSION_INIT1
           41  +
           42  +#ifndef SQLITE_AMALGAMATION
           43  +# include <string.h>
           44  +# include <stdio.h>
           45  +# include <stdlib.h>
           46  +# include <assert.h>
           47  +# define ALWAYS(X)  1
           48  +# define NEVER(X)   0
           49  +  typedef unsigned char u8;
           50  +  typedef unsigned short u16;
           51  +  typedef unsigned int u32;
           52  +#define get4byte(x) (        \
           53  +    ((u32)((x)[0])<<24) +    \
           54  +    ((u32)((x)[1])<<16) +    \
           55  +    ((u32)((x)[2])<<8) +     \
           56  +    ((u32)((x)[3]))          \
           57  +)
           58  +#endif
           59  +
           60  +/*
           61  +** Execute a single PRAGMA statement and return the integer value returned
           62  +** via output parameter (*pnOut).
           63  +**
           64  +** The SQL statement passed as the third argument should be a printf-style
           65  +** format string containing a single "%s" which will be replace by the
           66  +** value passed as the second argument. e.g.
           67  +**
           68  +**   sqlGetInteger(db, "main", "PRAGMA %s.page_count", pnOut)
           69  +**
           70  +** executes "PRAGMA main.page_count" and stores the results in (*pnOut).
           71  +*/
           72  +static int sqlGetInteger(
           73  +  sqlite3 *db,                    /* Database handle */
           74  +  const char *zDb,                /* Database name ("main", "temp" etc.) */
           75  +  const char *zFmt,               /* SQL statement format */
           76  +  u32 *pnOut                      /* OUT: Integer value */
           77  +){
           78  +  int rc, rc2;
           79  +  char *zSql;
           80  +  sqlite3_stmt *pStmt = 0;
           81  +  int bOk = 0;
           82  +
           83  +  zSql = sqlite3_mprintf(zFmt, zDb);
           84  +  if( zSql==0 ){
           85  +    rc = SQLITE_NOMEM;
           86  +  }else{
           87  +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
           88  +    sqlite3_free(zSql);
           89  +  }
           90  +
           91  +  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
           92  +    *pnOut = (u32)sqlite3_column_int(pStmt, 0);
           93  +    bOk = 1;
           94  +  }
           95  +
           96  +  rc2 = sqlite3_finalize(pStmt);
           97  +  if( rc==SQLITE_OK ) rc = rc2;
           98  +  if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_ERROR;
           99  +  return rc;
          100  +}
          101  +
          102  +/*
          103  +** Argument zFmt must be a printf-style format string and must be 
          104  +** followed by its required arguments. If argument pzOut is NULL, 
          105  +** then the results of printf()ing the format string are passed to
          106  +** sqlite3_log(). Otherwise, they are appended to the string
          107  +** at (*pzOut).
          108  +*/
          109  +static int checkFreelistError(char **pzOut, const char *zFmt, ...){
          110  +  int rc = SQLITE_OK;
          111  +  char *zErr = 0;
          112  +  va_list ap;
          113  +
          114  +  va_start(ap, zFmt);
          115  +  zErr = sqlite3_vmprintf(zFmt, ap);
          116  +  if( zErr==0 ){
          117  +    rc = SQLITE_NOMEM;
          118  +  }else{
          119  +    if( pzOut ){
          120  +      *pzOut = sqlite3_mprintf("%s%z%s", *pzOut?"\n":"", *pzOut, zErr);
          121  +      if( *pzOut==0 ) rc = SQLITE_NOMEM;
          122  +    }else{
          123  +      sqlite3_log(SQLITE_ERROR, "checkfreelist: %s", zErr);
          124  +    }
          125  +    sqlite3_free(zErr);
          126  +  }
          127  +  va_end(ap);
          128  +  return rc;
          129  +}
          130  +
          131  +static int checkFreelist(
          132  +  sqlite3 *db, 
          133  +  const char *zDb,
          134  +  char **pzOut
          135  +){
          136  +  /* This query returns one row for each page on the free list. Each row has
          137  +  ** two columns - the page number and page content.  */
          138  +  const char *zTrunk = 
          139  +    "WITH freelist_trunk(i, d, n) AS ("
          140  +      "SELECT 1, NULL, sqlite_readint32(data, 32) "
          141  +      "FROM sqlite_dbpage(:1) WHERE pgno=1 "
          142  +        "UNION ALL "
          143  +      "SELECT n, data, sqlite_readint32(data) "
          144  +      "FROM freelist_trunk, sqlite_dbpage(:1) WHERE pgno=n "
          145  +    ")"
          146  +    "SELECT i, d FROM freelist_trunk WHERE i!=1;";
          147  +
          148  +  int rc, rc2;                    /* Return code */
          149  +  sqlite3_stmt *pTrunk = 0;       /* Compilation of zTrunk */
          150  +  u32 nPage = 0;                  /* Number of pages in db */
          151  +  u32 nExpected = 0;              /* Expected number of free pages */
          152  +  u32 nFree = 0;                  /* Number of pages on free list */
          153  +
          154  +  if( zDb==0 ) zDb = "main";
          155  +
          156  +  if( (rc = sqlGetInteger(db, zDb, "PRAGMA %s.page_count", &nPage))
          157  +   || (rc = sqlGetInteger(db, zDb, "PRAGMA %s.freelist_count", &nExpected))
          158  +  ){
          159  +    return rc;
          160  +  }
          161  +
          162  +  rc = sqlite3_prepare_v2(db, zTrunk, -1, &pTrunk, 0);
          163  +  if( rc!=SQLITE_OK ) return rc;
          164  +  sqlite3_bind_text(pTrunk, 1, zDb, -1, SQLITE_STATIC);
          165  +  while( rc==SQLITE_OK && sqlite3_step(pTrunk)==SQLITE_ROW ){
          166  +    u32 i;
          167  +    u32 iTrunk = (u32)sqlite3_column_int(pTrunk, 0);
          168  +    const u8 *aData = (const u8*)sqlite3_column_blob(pTrunk, 1);
          169  +    u32 nData = (u32)sqlite3_column_bytes(pTrunk, 1);
          170  +    u32 iNext = get4byte(&aData[0]);
          171  +    u32 nLeaf = get4byte(&aData[4]);
          172  +
          173  +    if( nLeaf>((nData/4)-2-6) ){
          174  +      rc = checkFreelistError(pzOut, 
          175  +          "leaf count out of range (%d) on trunk page %d", 
          176  +          (int)nLeaf, (int)iTrunk
          177  +      );
          178  +      nLeaf = (nData/4) - 2 - 6;
          179  +    }
          180  +
          181  +    nFree += 1+nLeaf;
          182  +    if( iNext>nPage ){
          183  +      rc = checkFreelistError(pzOut, 
          184  +          "trunk page %d is out of range", (int)iNext
          185  +      );
          186  +    }
          187  +
          188  +    for(i=0; rc==SQLITE_OK && i<nLeaf; i++){
          189  +      u32 iLeaf = get4byte(&aData[8 + 4*i]);
          190  +      if( iLeaf==0 || iLeaf>nPage ){
          191  +        rc = checkFreelistError(pzOut,
          192  +            "leaf page %d is out of range (child %d of trunk page %d)", 
          193  +            (int)iLeaf, (int)i, (int)iTrunk
          194  +        );
          195  +      }
          196  +    }
          197  +  }
          198  +
          199  +  if( rc==SQLITE_OK && nFree!=nExpected ){
          200  +    rc = checkFreelistError(pzOut,
          201  +        "free-list count mismatch: actual=%d header=%d", 
          202  +        (int)nFree, (int)nExpected
          203  +    );
          204  +  }
          205  +
          206  +  rc2 = sqlite3_finalize(pTrunk);
          207  +  if( rc==SQLITE_OK ) rc = rc2;
          208  +  return rc;
          209  +}
          210  +
          211  +int sqlite3_check_freelist(sqlite3 *db, const char *zDb){
          212  +  return checkFreelist(db, zDb, 0);
          213  +}
          214  +
          215  +static void checkfreelist_function(
          216  +  sqlite3_context *pCtx,
          217  +  int nArg,
          218  +  sqlite3_value **apArg
          219  +){
          220  +  const char *zDb;
          221  +  int rc;
          222  +  char *zOut = 0;
          223  +  sqlite3 *db = sqlite3_context_db_handle(pCtx);
          224  +
          225  +  assert( nArg==1 );
          226  +  zDb = (const char*)sqlite3_value_text(apArg[0]);
          227  +  rc = checkFreelist(db, zDb, &zOut);
          228  +  if( rc==SQLITE_OK ){
          229  +    sqlite3_result_text(pCtx, zOut?zOut:"ok", -1, SQLITE_TRANSIENT);
          230  +  }else{
          231  +    sqlite3_result_error_code(pCtx, rc);
          232  +  }
          233  +
          234  +  sqlite3_free(zOut);
          235  +}
          236  +
          237  +/*
          238  +** An SQL function invoked as follows:
          239  +**
          240  +**   sqlite_readint32(BLOB)           -- Decode 32-bit integer from start of blob
          241  +*/
          242  +static void readint_function(
          243  +  sqlite3_context *pCtx,
          244  +  int nArg,
          245  +  sqlite3_value **apArg
          246  +){
          247  +  const u8 *zBlob;
          248  +  int nBlob;
          249  +  int iOff = 0;
          250  +  u32 iRet = 0;
          251  +
          252  +  if( nArg!=1 && nArg!=2 ){
          253  +    sqlite3_result_error(
          254  +        pCtx, "wrong number of arguments to function sqlite_readint32()", -1
          255  +    );
          256  +    return;
          257  +  }
          258  +  if( nArg==2 ){
          259  +    iOff = sqlite3_value_int(apArg[1]);
          260  +  }
          261  +
          262  +  zBlob = sqlite3_value_blob(apArg[0]);
          263  +  nBlob = sqlite3_value_bytes(apArg[0]);
          264  +
          265  +  if( nBlob>=(iOff+4) ){
          266  +    iRet = get4byte(&zBlob[iOff]);
          267  +  }
          268  +
          269  +  sqlite3_result_int64(pCtx, (sqlite3_int64)iRet);
          270  +}
          271  +
          272  +/*
          273  +** Register the SQL functions.
          274  +*/
          275  +static int cflRegister(sqlite3 *db){
          276  +  int rc = sqlite3_create_function(
          277  +      db, "sqlite_readint32", -1, SQLITE_UTF8, 0, readint_function, 0, 0
          278  +  );
          279  +  if( rc!=SQLITE_OK ) return rc;
          280  +  rc = sqlite3_create_function(
          281  +      db, "checkfreelist", 1, SQLITE_UTF8, 0, checkfreelist_function, 0, 0
          282  +  );
          283  +  return rc;
          284  +}
          285  +
          286  +/*
          287  +** Extension load function.
          288  +*/
          289  +#ifdef _WIN32
          290  +__declspec(dllexport)
          291  +#endif
          292  +int sqlite3_checkfreelist_init(
          293  +  sqlite3 *db, 
          294  +  char **pzErrMsg, 
          295  +  const sqlite3_api_routines *pApi
          296  +){
          297  +  SQLITE_EXTENSION_INIT2(pApi);
          298  +  return cflRegister(db);
          299  +}

Added ext/repair/checkindex.c.

            1  +/*
            2  +** 2017 October 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  +
           14  +#include "sqlite3ext.h"
           15  +SQLITE_EXTENSION_INIT1
           16  +
           17  +#ifndef SQLITE_AMALGAMATION
           18  +# include <string.h>
           19  +# include <stdio.h>
           20  +# include <stdlib.h>
           21  +# include <assert.h>
           22  +# define ALWAYS(X)  1
           23  +# define NEVER(X)   0
           24  +  typedef unsigned char u8;
           25  +  typedef unsigned short u16;
           26  +  typedef unsigned int u32;
           27  +#define get4byte(x) (        \
           28  +    ((u32)((x)[0])<<24) +    \
           29  +    ((u32)((x)[1])<<16) +    \
           30  +    ((u32)((x)[2])<<8) +     \
           31  +    ((u32)((x)[3]))          \
           32  +)
           33  +#endif
           34  +
           35  +typedef struct CidxTable CidxTable;
           36  +typedef struct CidxCursor CidxCursor;
           37  +
           38  +struct CidxTable {
           39  +  sqlite3_vtab base;              /* Base class.  Must be first */
           40  +  sqlite3 *db;
           41  +};
           42  +
           43  +struct CidxCursor {
           44  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
           45  +  sqlite3_int64 iRowid;
           46  +  sqlite3_stmt *pStmt;
           47  +};
           48  +
           49  +typedef struct CidxColumn CidxColumn;
           50  +struct CidxColumn {
           51  +  char *zExpr;                    /* Text for indexed expression */
           52  +  int bDesc;                      /* True for DESC columns, otherwise false */
           53  +  int bKey;                       /* Part of index, not PK */
           54  +};
           55  +
           56  +typedef struct CidxIndex CidxIndex;
           57  +struct CidxIndex {
           58  +  int nCol;                       /* Elements in aCol[] array */
           59  +  CidxColumn aCol[1];             /* Array of indexed columns */
           60  +};
           61  +
           62  +static void *cidxMalloc(int *pRc, int n){
           63  +  void *pRet = 0;
           64  +  assert( n!=0 );
           65  +  if( *pRc==SQLITE_OK ){
           66  +    pRet = sqlite3_malloc(n);
           67  +    if( pRet ){
           68  +      memset(pRet, 0, n);
           69  +    }else{
           70  +      *pRc = SQLITE_NOMEM;
           71  +    }
           72  +  }
           73  +  return pRet;
           74  +}
           75  +
           76  +static void cidxCursorError(CidxCursor *pCsr, const char *zFmt, ...){
           77  +  va_list ap;
           78  +  va_start(ap, zFmt);
           79  +  assert( pCsr->base.pVtab->zErrMsg==0 );
           80  +  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
           81  +  va_end(ap);
           82  +}
           83  +
           84  +/*
           85  +** Connect to then incremental_index_check virtual table.
           86  +*/
           87  +static int cidxConnect(
           88  +  sqlite3 *db,
           89  +  void *pAux,
           90  +  int argc, const char *const*argv,
           91  +  sqlite3_vtab **ppVtab,
           92  +  char **pzErr
           93  +){
           94  +  int rc = SQLITE_OK;
           95  +  CidxTable *pRet;
           96  +
           97  +#define IIC_ERRMSG        0
           98  +#define IIC_CURRENT_KEY   1
           99  +#define IIC_INDEX_NAME    2
          100  +#define IIC_AFTER_KEY     3
          101  +  rc = sqlite3_declare_vtab(db,
          102  +      "CREATE TABLE xyz("
          103  +      " errmsg TEXT, current_key TEXT,"
          104  +      " index_name HIDDEN, after_key HIDDEN"
          105  +      ")"
          106  +  );
          107  +  pRet = cidxMalloc(&rc, sizeof(CidxTable));
          108  +  if( pRet ){
          109  +    pRet->db = db;
          110  +  }
          111  +
          112  +  *ppVtab = (sqlite3_vtab*)pRet;
          113  +  return rc;
          114  +}
          115  +
          116  +/*
          117  +** Disconnect from or destroy an incremental_index_check virtual table.
          118  +*/
          119  +static int cidxDisconnect(sqlite3_vtab *pVtab){
          120  +  CidxTable *pTab = (CidxTable*)pVtab;
          121  +  sqlite3_free(pTab);
          122  +  return SQLITE_OK;
          123  +}
          124  +
          125  +/*
          126  +** xBestIndex method.
          127  +*/
          128  +static int cidxBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pInfo){
          129  +  int iIdxName = -1;
          130  +  int iAfterKey = -1;
          131  +  int i;
          132  +
          133  +  for(i=0; i<pInfo->nConstraint; i++){
          134  +    struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
          135  +    if( p->usable==0 ) continue;
          136  +    if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
          137  +
          138  +    if( p->iColumn==IIC_INDEX_NAME ){
          139  +      iIdxName = i;
          140  +    }
          141  +    if( p->iColumn==IIC_AFTER_KEY ){
          142  +      iAfterKey = i;
          143  +    }
          144  +  }
          145  +
          146  +  if( iIdxName<0 ){
          147  +    pInfo->estimatedCost = 1000000000.0;
          148  +  }else{
          149  +    pInfo->aConstraintUsage[iIdxName].argvIndex = 1;
          150  +    pInfo->aConstraintUsage[iIdxName].omit = 1;
          151  +    if( iAfterKey<0 ){
          152  +      pInfo->estimatedCost = 1000000.0;
          153  +    }else{
          154  +      pInfo->aConstraintUsage[iAfterKey].argvIndex = 2;
          155  +      pInfo->aConstraintUsage[iAfterKey].omit = 1;
          156  +      pInfo->estimatedCost = 1000.0;
          157  +    }
          158  +  }
          159  +
          160  +  return SQLITE_OK;
          161  +}
          162  +
          163  +/*
          164  +** Open a new btreeinfo cursor.
          165  +*/
          166  +static int cidxOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
          167  +  CidxCursor *pRet;
          168  +  int rc = SQLITE_OK;
          169  +
          170  +  pRet = cidxMalloc(&rc, sizeof(CidxCursor));
          171  +
          172  +  *ppCursor = (sqlite3_vtab_cursor*)pRet;
          173  +  return rc;
          174  +}
          175  +
          176  +/*
          177  +** Close a btreeinfo cursor.
          178  +*/
          179  +static int cidxClose(sqlite3_vtab_cursor *pCursor){
          180  +  CidxCursor *pCsr = (CidxCursor*)pCursor;
          181  +  sqlite3_finalize(pCsr->pStmt);
          182  +  pCsr->pStmt = 0;
          183  +  sqlite3_free(pCsr);
          184  +  return SQLITE_OK;
          185  +}
          186  +
          187  +/*
          188  +** Move a btreeinfo cursor to the next entry in the file.
          189  +*/
          190  +static int cidxNext(sqlite3_vtab_cursor *pCursor){
          191  +  CidxCursor *pCsr = (CidxCursor*)pCursor;
          192  +  int rc = sqlite3_step(pCsr->pStmt);
          193  +  if( rc!=SQLITE_ROW ){
          194  +    rc = sqlite3_finalize(pCsr->pStmt);
          195  +    pCsr->pStmt = 0;
          196  +    if( rc!=SQLITE_OK ){
          197  +      sqlite3 *db = ((CidxTable*)pCsr->base.pVtab)->db;
          198  +      cidxCursorError(pCsr, "Cursor error: %s", sqlite3_errmsg(db));
          199  +    }
          200  +  }else{
          201  +    pCsr->iRowid++;
          202  +    rc = SQLITE_OK;
          203  +  }
          204  +  return rc;
          205  +}
          206  +
          207  +/* We have reached EOF if previous sqlite3_step() returned
          208  +** anything other than SQLITE_ROW;
          209  +*/
          210  +static int cidxEof(sqlite3_vtab_cursor *pCursor){
          211  +  CidxCursor *pCsr = (CidxCursor*)pCursor;
          212  +  return pCsr->pStmt==0;
          213  +}
          214  +
          215  +static char *cidxMprintf(int *pRc, const char *zFmt, ...){
          216  +  char *zRet = 0;
          217  +  va_list ap;
          218  +  va_start(ap, zFmt);
          219  +  zRet = sqlite3_vmprintf(zFmt, ap);
          220  +  if( *pRc==SQLITE_OK ){
          221  +    if( zRet==0 ){
          222  +      *pRc = SQLITE_NOMEM;
          223  +    }
          224  +  }else{
          225  +    sqlite3_free(zRet);
          226  +    zRet = 0;
          227  +  }
          228  +  va_end(ap);
          229  +  return zRet;
          230  +}
          231  +
          232  +static sqlite3_stmt *cidxPrepare(
          233  +  int *pRc, CidxCursor *pCsr, const char *zFmt, ...
          234  +){
          235  +  sqlite3_stmt *pRet = 0;
          236  +  char *zSql;
          237  +  va_list ap;                     /* ... printf arguments */
          238  +  va_start(ap, zFmt);
          239  +
          240  +  zSql = sqlite3_vmprintf(zFmt, ap);
          241  +  if( *pRc==SQLITE_OK ){
          242  +    if( zSql==0 ){
          243  +      *pRc = SQLITE_NOMEM;
          244  +    }else{
          245  +      sqlite3 *db = ((CidxTable*)pCsr->base.pVtab)->db;
          246  +      *pRc = sqlite3_prepare_v2(db, zSql, -1, &pRet, 0);
          247  +      if( *pRc!=SQLITE_OK ){
          248  +        cidxCursorError(pCsr, "SQL error: %s", sqlite3_errmsg(db));
          249  +      }
          250  +    }
          251  +  }
          252  +  sqlite3_free(zSql);
          253  +  va_end(ap);
          254  +
          255  +  return pRet;
          256  +}
          257  +
          258  +static void cidxFinalize(int *pRc, sqlite3_stmt *pStmt){
          259  +  int rc = sqlite3_finalize(pStmt);
          260  +  if( *pRc==SQLITE_OK ) *pRc = rc;
          261  +}
          262  +
          263  +char *cidxStrdup(int *pRc, const char *zStr){
          264  +  char *zRet = 0;
          265  +  if( *pRc==SQLITE_OK ){
          266  +    int n = (int)strlen(zStr);
          267  +    zRet = cidxMalloc(pRc, n+1);
          268  +    if( zRet ) memcpy(zRet, zStr, n+1);
          269  +  }
          270  +  return zRet;
          271  +}
          272  +
          273  +static void cidxFreeIndex(CidxIndex *pIdx){
          274  +  if( pIdx ){
          275  +    int i;
          276  +    for(i=0; i<pIdx->nCol; i++){
          277  +      sqlite3_free(pIdx->aCol[i].zExpr);
          278  +    }
          279  +    sqlite3_free(pIdx);
          280  +  }
          281  +}
          282  +
          283  +static int cidx_isspace(char c){
          284  +  return c==' ' || c=='\t' || c=='\r' || c=='\n';
          285  +}
          286  +
          287  +static int cidx_isident(char c){
          288  +  return c<0 
          289  +    || (c>='0' && c<='9') || (c>='a' && c<='z') 
          290  +    || (c>='A' && c<='Z') || c=='_';
          291  +}
          292  +
          293  +#define CIDX_PARSE_EOF   0
          294  +#define CIDX_PARSE_COMMA 1      /*  "," */
          295  +#define CIDX_PARSE_OPEN  2      /*  "(" */
          296  +#define CIDX_PARSE_CLOSE 3      /*  ")" */
          297  +
          298  +static int cidxFindNext(
          299  +  const char *zIn, 
          300  +  const char **pzOut,
          301  +  int *pbDoNotTrim                /* OUT: True if prev is -- comment */
          302  +){
          303  +  const char *z = zIn;
          304  +
          305  +  while( 1 ){
          306  +    if( z[0]=='-' && z[1]=='-' ){
          307  +      z += 2;
          308  +      while( z[0]!='\n' ){
          309  +        if( z[0]=='\0' ) return CIDX_PARSE_EOF;
          310  +        z++;
          311  +      }
          312  +      while( cidx_isspace(*z) ) z++;
          313  +      *pbDoNotTrim = 1;
          314  +    }else{
          315  +      *pzOut = z;
          316  +      switch( *z ){
          317  +        case '\0':
          318  +          return CIDX_PARSE_EOF;
          319  +        case '(':
          320  +          return CIDX_PARSE_OPEN;
          321  +        case ')':
          322  +          return CIDX_PARSE_CLOSE;
          323  +        case ',':
          324  +          return CIDX_PARSE_COMMA;
          325  +  
          326  +        case '"': 
          327  +        case '\'': 
          328  +        case '`': {
          329  +          char q = *z;
          330  +          z++;
          331  +          while( *z ){
          332  +            if( *z==q ){
          333  +              z++;
          334  +              if( *z!=q ) break;
          335  +            }
          336  +            z++;
          337  +          }
          338  +          break;
          339  +        }
          340  +  
          341  +        case '[':
          342  +          while( *z++!=']' );
          343  +          break;
          344  +  
          345  +        case '/':
          346  +          if( z[1]=='*' ){
          347  +            z += 2;
          348  +            while( z[0]!='*' || z[1]!='/' ){
          349  +              if( z[1]=='\0' ) return CIDX_PARSE_EOF;
          350  +              z++;
          351  +            }
          352  +            z += 2;
          353  +            break;
          354  +          }
          355  +  
          356  +        default:
          357  +          z++;
          358  +          break;
          359  +      }
          360  +      *pbDoNotTrim = 0;
          361  +    }
          362  +  }
          363  +
          364  +  assert( 0 );
          365  +  return -1;
          366  +}
          367  +
          368  +static int cidxParseSQL(CidxCursor *pCsr, CidxIndex *pIdx, const char *zSql){
          369  +  const char *z = zSql;
          370  +  const char *z1;
          371  +  int e;
          372  +  int rc = SQLITE_OK;
          373  +  int nParen = 1;
          374  +  int bDoNotTrim = 0;
          375  +  CidxColumn *pCol = pIdx->aCol;
          376  +
          377  +  e = cidxFindNext(z, &z, &bDoNotTrim);
          378  +  if( e!=CIDX_PARSE_OPEN ) goto parse_error;
          379  +  z1 = z+1;
          380  +  z++;
          381  +  while( nParen>0 ){
          382  +    e = cidxFindNext(z, &z, &bDoNotTrim);
          383  +    if( e==CIDX_PARSE_EOF ) goto parse_error;
          384  +    if( (e==CIDX_PARSE_COMMA || e==CIDX_PARSE_CLOSE) && nParen==1 ){
          385  +      const char *z2 = z;
          386  +      if( pCol->zExpr ) goto parse_error;
          387  +
          388  +      if( bDoNotTrim==0 ){
          389  +        while( cidx_isspace(z[-1]) ) z--;
          390  +        if( !sqlite3_strnicmp(&z[-3], "asc", 3) && 0==cidx_isident(z[-4]) ){
          391  +          z -= 3;
          392  +          while( cidx_isspace(z[-1]) ) z--;
          393  +        }else
          394  +          if( !sqlite3_strnicmp(&z[-4], "desc", 4) && 0==cidx_isident(z[-5]) ){
          395  +            z -= 4;
          396  +            while( cidx_isspace(z[-1]) ) z--;
          397  +          }
          398  +        while( cidx_isspace(z1[0]) ) z1++;
          399  +      }
          400  +
          401  +      pCol->zExpr = cidxMprintf(&rc, "%.*s", z-z1, z1);
          402  +      pCol++;
          403  +      z = z1 = z2+1;
          404  +    }
          405  +    if( e==CIDX_PARSE_OPEN ) nParen++;
          406  +    if( e==CIDX_PARSE_CLOSE ) nParen--;
          407  +    z++;
          408  +  }
          409  +
          410  +  return rc;
          411  +
          412  + parse_error:
          413  +  cidxCursorError(pCsr, "Parse error in: %s", zSql);
          414  +  return SQLITE_ERROR;
          415  +}
          416  +
          417  +static int cidxLookupIndex(
          418  +  CidxCursor *pCsr,               /* Cursor object */
          419  +  const char *zIdx,               /* Name of index to look up */
          420  +  CidxIndex **ppIdx,              /* OUT: Description of columns */
          421  +  char **pzTab                    /* OUT: Table name */
          422  +){
          423  +  int rc = SQLITE_OK;
          424  +  char *zTab = 0;
          425  +  CidxIndex *pIdx = 0;
          426  +
          427  +  sqlite3_stmt *pFindTab = 0;
          428  +  sqlite3_stmt *pInfo = 0;
          429  +    
          430  +  /* Find the table for this index. */
          431  +  pFindTab = cidxPrepare(&rc, pCsr, 
          432  +      "SELECT tbl_name, sql FROM sqlite_master WHERE name=%Q AND type='index'",
          433  +      zIdx
          434  +  );
          435  +  if( rc==SQLITE_OK && sqlite3_step(pFindTab)==SQLITE_ROW ){
          436  +    const char *zSql = (const char*)sqlite3_column_text(pFindTab, 1);
          437  +    zTab = cidxStrdup(&rc, (const char*)sqlite3_column_text(pFindTab, 0));
          438  +
          439  +    pInfo = cidxPrepare(&rc, pCsr, "PRAGMA index_xinfo(%Q)", zIdx);
          440  +    if( rc==SQLITE_OK ){
          441  +      int nAlloc = 0;
          442  +      int iCol = 0;
          443  +
          444  +      while( sqlite3_step(pInfo)==SQLITE_ROW ){
          445  +        const char *zName = (const char*)sqlite3_column_text(pInfo, 2);
          446  +        const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
          447  +        CidxColumn *p;
          448  +        if( zName==0 ) zName = "rowid";
          449  +        if( iCol==nAlloc ){
          450  +          int nByte = sizeof(CidxIndex) + sizeof(CidxColumn)*(nAlloc+8);
          451  +          pIdx = (CidxIndex*)sqlite3_realloc(pIdx, nByte);
          452  +          nAlloc += 8;
          453  +        }
          454  +        p = &pIdx->aCol[iCol++];
          455  +        p->bDesc = sqlite3_column_int(pInfo, 3);
          456  +        p->bKey = sqlite3_column_int(pInfo, 5);
          457  +        if( zSql==0 || p->bKey==0 ){
          458  +          p->zExpr = cidxMprintf(&rc, "\"%w\" COLLATE %s",zName,zColl);
          459  +        }else{
          460  +          p->zExpr = 0;
          461  +        }
          462  +        pIdx->nCol = iCol;
          463  +      }
          464  +      cidxFinalize(&rc, pInfo);
          465  +    }
          466  +
          467  +    if( rc==SQLITE_OK && zSql ){
          468  +      rc = cidxParseSQL(pCsr, pIdx, zSql);
          469  +    }
          470  +  }
          471  +
          472  +  cidxFinalize(&rc, pFindTab);
          473  +  if( rc==SQLITE_OK && zTab==0 ){
          474  +    rc = SQLITE_ERROR;
          475  +  }
          476  +  
          477  +  if( rc!=SQLITE_OK ){
          478  +    sqlite3_free(zTab);
          479  +    cidxFreeIndex(pIdx);
          480  +  }else{
          481  +    *pzTab = zTab;
          482  +    *ppIdx = pIdx;
          483  +  }
          484  +
          485  +  return rc;
          486  +}
          487  +
          488  +static int cidxDecodeAfter(
          489  +  CidxCursor *pCsr, 
          490  +  int nCol, 
          491  +  const char *zAfterKey, 
          492  +  char ***pazAfter
          493  +){
          494  +  char **azAfter;
          495  +  int rc = SQLITE_OK;
          496  +  int nAfterKey = (int)strlen(zAfterKey);
          497  +
          498  +  azAfter = cidxMalloc(&rc, sizeof(char*)*nCol + nAfterKey+1);
          499  +  if( rc==SQLITE_OK ){
          500  +    int i;
          501  +    char *zCopy = (char*)&azAfter[nCol];
          502  +    char *p = zCopy;
          503  +    memcpy(zCopy, zAfterKey, nAfterKey+1);
          504  +    for(i=0; i<nCol; i++){
          505  +      while( *p==' ' ) p++;
          506  +
          507  +      /* Check NULL values */
          508  +      if( *p=='N' ){
          509  +        if( memcmp(p, "NULL", 4) ) goto parse_error;
          510  +        p += 4;
          511  +      }
          512  +
          513  +      /* Check strings and blob literals */
          514  +      else if( *p=='X' || *p=='\'' ){
          515  +        azAfter[i] = p;
          516  +        if( *p=='X' ) p++;
          517  +        if( *p!='\'' ) goto parse_error;
          518  +        p++;
          519  +        while( 1 ){
          520  +          if( *p=='\0' ) goto parse_error;
          521  +          if( *p=='\'' ){
          522  +            p++;
          523  +            if( *p!='\'' ) break;
          524  +          }
          525  +          p++;
          526  +        }
          527  +      }
          528  +
          529  +      /* Check numbers */
          530  +      else{
          531  +        azAfter[i] = p;
          532  +        while( (*p>='0' && *p<='9') 
          533  +            || *p=='.' || *p=='+' || *p=='-' || *p=='e' || *p=='E'
          534  +        ){
          535  +          p++;
          536  +        }
          537  +      }
          538  +
          539  +      while( *p==' ' ) p++;
          540  +      if( *p!=(i==(nCol-1) ? '\0' : ',') ){
          541  +        goto parse_error;
          542  +      }
          543  +      *p++ = '\0';
          544  +    }
          545  +  }
          546  +
          547  +  *pazAfter = azAfter;
          548  +  return rc;
          549  +
          550  + parse_error:
          551  +  sqlite3_free(azAfter);
          552  +  *pazAfter = 0;
          553  +  cidxCursorError(pCsr, "%s", "error parsing after value");
          554  +  return SQLITE_ERROR;
          555  +}
          556  +
          557  +static char *cidxWhere(
          558  +  int *pRc, CidxColumn *aCol, char **azAfter, int iGt, int bLastIsNull
          559  +){
          560  +  char *zRet = 0;
          561  +  const char *zSep = "";
          562  +  int i;
          563  +
          564  +  for(i=0; i<iGt; i++){
          565  +    zRet = cidxMprintf(pRc, "%z%s(%s) IS %s", zRet, 
          566  +        zSep, aCol[i].zExpr, (azAfter[i] ? azAfter[i] : "NULL")
          567  +    );
          568  +    zSep = " AND ";
          569  +  }
          570  +
          571  +  if( bLastIsNull ){
          572  +    zRet = cidxMprintf(pRc, "%z%s(%s) IS NULL", zRet, zSep, aCol[iGt].zExpr);
          573  +  }
          574  +  else if( azAfter[iGt] ){
          575  +    zRet = cidxMprintf(pRc, "%z%s(%s) %s %s", zRet, 
          576  +        zSep, aCol[iGt].zExpr, (aCol[iGt].bDesc ? "<" : ">"), 
          577  +        azAfter[iGt]
          578  +    );
          579  +  }else{
          580  +    zRet = cidxMprintf(pRc, "%z%s(%s) IS NOT NULL", zRet, zSep,aCol[iGt].zExpr);
          581  +  }
          582  +
          583  +  return zRet;
          584  +}
          585  +
          586  +#define CIDX_CLIST_ALL         0
          587  +#define CIDX_CLIST_ORDERBY     1
          588  +#define CIDX_CLIST_CURRENT_KEY 2
          589  +#define CIDX_CLIST_SUBWHERE    3
          590  +#define CIDX_CLIST_SUBEXPR     4
          591  +
          592  +/*
          593  +** This function returns various strings based on the contents of the
          594  +** CidxIndex structure and the eType parameter.
          595  +*/
          596  +static char *cidxColumnList(
          597  +  int *pRc,                       /* IN/OUT: Error code */
          598  +  const char *zIdx,
          599  +  CidxIndex *pIdx,                /* Indexed columns */
          600  +  int eType                       /* True to include ASC/DESC */
          601  +){
          602  +  char *zRet = 0;
          603  +  if( *pRc==SQLITE_OK ){
          604  +    const char *aDir[2] = {"", " DESC"};
          605  +    int i;
          606  +    const char *zSep = "";
          607  +
          608  +    for(i=0; i<pIdx->nCol; i++){
          609  +      CidxColumn *p = &pIdx->aCol[i];
          610  +      assert( pIdx->aCol[i].bDesc==0 || pIdx->aCol[i].bDesc==1 );
          611  +      switch( eType ){
          612  +
          613  +        case CIDX_CLIST_ORDERBY:
          614  +          zRet = cidxMprintf(pRc, "%z%s%d%s", zRet, zSep, i+1, aDir[p->bDesc]);
          615  +          zSep = ",";
          616  +          break;
          617  +
          618  +        case CIDX_CLIST_CURRENT_KEY:
          619  +          zRet = cidxMprintf(pRc, "%z%squote(i%d)", zRet, zSep, i);
          620  +          zSep = "||','||";
          621  +          break;
          622  +
          623  +        case CIDX_CLIST_SUBWHERE:
          624  +          if( p->bKey==0 ){
          625  +            zRet = cidxMprintf(pRc, "%z%s%s IS i.i%d", zRet, 
          626  +                zSep, p->zExpr, i
          627  +            );
          628  +            zSep = " AND ";
          629  +          }
          630  +          break;
          631  +
          632  +        case CIDX_CLIST_SUBEXPR:
          633  +          if( p->bKey==1 ){
          634  +            zRet = cidxMprintf(pRc, "%z%s%s IS i.i%d", zRet, 
          635  +                zSep, p->zExpr, i
          636  +            );
          637  +            zSep = " AND ";
          638  +          }
          639  +          break;
          640  +
          641  +        default:
          642  +          assert( eType==CIDX_CLIST_ALL );
          643  +          zRet = cidxMprintf(pRc, "%z%s(%s) AS i%d", zRet, zSep, p->zExpr, i);
          644  +          zSep = ", ";
          645  +          break;
          646  +      }
          647  +    }
          648  +  }
          649  +
          650  +  return zRet;
          651  +}
          652  +
          653  +/* 
          654  +** Position a cursor back to the beginning.
          655  +*/
          656  +static int cidxFilter(
          657  +  sqlite3_vtab_cursor *pCursor, 
          658  +  int idxNum, const char *idxStr,
          659  +  int argc, sqlite3_value **argv
          660  +){
          661  +  int rc = SQLITE_OK;
          662  +  CidxCursor *pCsr = (CidxCursor*)pCursor;
          663  +  const char *zIdxName = 0;
          664  +  const char *zAfterKey = 0;
          665  +
          666  +  if( argc>0 ){
          667  +    zIdxName = (const char*)sqlite3_value_text(argv[0]);
          668  +    if( argc>1 ){
          669  +      zAfterKey = (const char*)sqlite3_value_text(argv[1]);
          670  +    }
          671  +  }
          672  +
          673  +  if( zIdxName ){
          674  +    char *zTab = 0;
          675  +    char *zCurrentKey = 0;
          676  +    char *zOrderBy = 0;
          677  +    char *zSubWhere = 0;
          678  +    char *zSubExpr = 0;
          679  +    char *zSrcList = 0;
          680  +
          681  +    char **azAfter = 0;
          682  +    CidxIndex *pIdx = 0;
          683  +
          684  +    rc = cidxLookupIndex(pCsr, zIdxName, &pIdx, &zTab);
          685  +
          686  +    zOrderBy = cidxColumnList(&rc, zIdxName, pIdx, CIDX_CLIST_ORDERBY);
          687  +    zCurrentKey = cidxColumnList(&rc, zIdxName, pIdx, CIDX_CLIST_CURRENT_KEY);
          688  +    zSubWhere = cidxColumnList(&rc, zIdxName, pIdx, CIDX_CLIST_SUBWHERE);
          689  +    zSubExpr = cidxColumnList(&rc, zIdxName, pIdx, CIDX_CLIST_SUBEXPR);
          690  +    zSrcList = cidxColumnList(&rc, zIdxName, pIdx, CIDX_CLIST_ALL);
          691  +
          692  +    if( rc==SQLITE_OK && zAfterKey ){
          693  +      rc = cidxDecodeAfter(pCsr, pIdx->nCol, zAfterKey, &azAfter);
          694  +    }
          695  +
          696  +    if( rc || zAfterKey==0 ){
          697  +      pCsr->pStmt = cidxPrepare(&rc, pCsr, 
          698  +          "SELECT (SELECT %s FROM %Q AS t WHERE %s), %s "
          699  +          "FROM (SELECT %s FROM %Q ORDER BY %s) AS i",
          700  +          zSubExpr, zTab, zSubWhere, zCurrentKey, 
          701  +          zSrcList, zTab, zOrderBy
          702  +      );
          703  +      /* printf("SQL: %s\n", sqlite3_sql(pCsr->pStmt)); */
          704  +    }else{
          705  +      const char *zSep = "";
          706  +      char *zSql;
          707  +      int i;
          708  +
          709  +      zSql = cidxMprintf(&rc, 
          710  +          "SELECT (SELECT %s FROM %Q WHERE %s), %s FROM (",
          711  +          zSubExpr, zTab, zSubWhere, zCurrentKey
          712  +      );
          713  +      for(i=pIdx->nCol-1; i>=0; i--){
          714  +        int j;
          715  +        if( pIdx->aCol[i].bDesc && azAfter[i]==0 ) continue;
          716  +        for(j=0; j<2; j++){
          717  +          char *zWhere = cidxWhere(&rc, pIdx->aCol, azAfter, i, j);
          718  +          zSql = cidxMprintf(&rc, "%z"
          719  +              "%sSELECT * FROM (SELECT %s FROM %Q WHERE %z ORDER BY %s)",
          720  +              zSql, zSep, zSrcList, zTab, zWhere, zOrderBy
          721  +          );
          722  +          zSep = " UNION ALL ";
          723  +          if( pIdx->aCol[i].bDesc==0 ) break;
          724  +        }
          725  +      }
          726  +      zSql = cidxMprintf(&rc, "%z) AS i", zSql);
          727  +
          728  +      /* printf("SQL: %s\n", zSql); */
          729  +      pCsr->pStmt = cidxPrepare(&rc, pCsr, "%z", zSql);
          730  +    }
          731  +
          732  +    sqlite3_free(zTab);
          733  +    sqlite3_free(zCurrentKey);
          734  +    sqlite3_free(zOrderBy);
          735  +    sqlite3_free(zSubWhere);
          736  +    sqlite3_free(zSubExpr);
          737  +    sqlite3_free(zSrcList);
          738  +    cidxFreeIndex(pIdx);
          739  +    sqlite3_free(azAfter);
          740  +  }
          741  +
          742  +  if( pCsr->pStmt ){
          743  +    assert( rc==SQLITE_OK );
          744  +    rc = cidxNext(pCursor);
          745  +  }
          746  +  pCsr->iRowid = 1;
          747  +  return rc;
          748  +}
          749  +
          750  +/* 
          751  +** Return a column value.
          752  +*/
          753  +static int cidxColumn(
          754  +  sqlite3_vtab_cursor *pCursor, 
          755  +  sqlite3_context *ctx, 
          756  +  int iCol
          757  +){
          758  +  CidxCursor *pCsr = (CidxCursor*)pCursor;
          759  +  assert( iCol>=IIC_ERRMSG && iCol<=IIC_AFTER_KEY );
          760  +  if( iCol==IIC_ERRMSG ){
          761  +    const char *zVal = 0;
          762  +    if( sqlite3_column_type(pCsr->pStmt, 0)==SQLITE_INTEGER ){
          763  +      if( sqlite3_column_int(pCsr->pStmt, 0)==0 ){
          764  +        zVal = "row data mismatch";
          765  +      }
          766  +    }else{
          767  +      zVal = "row missing";
          768  +    }
          769  +    sqlite3_result_text(ctx, zVal, -1, SQLITE_STATIC);
          770  +  }else if( iCol==IIC_CURRENT_KEY ){
          771  +    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, 1));
          772  +  }
          773  +  return SQLITE_OK;
          774  +}
          775  +
          776  +/* Return the ROWID for the sqlite_btreeinfo table */
          777  +static int cidxRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
          778  +  *pRowid = 0;
          779  +  return SQLITE_OK;
          780  +}
          781  +
          782  +/*
          783  +** Register the virtual table modules with the database handle passed
          784  +** as the only argument.
          785  +*/
          786  +static int ciInit(sqlite3 *db){
          787  +  static sqlite3_module cidx_module = {
          788  +    0,                            /* iVersion */
          789  +    0,                            /* xCreate */
          790  +    cidxConnect,                  /* xConnect */
          791  +    cidxBestIndex,                /* xBestIndex */
          792  +    cidxDisconnect,               /* xDisconnect */
          793  +    0,                            /* xDestroy */
          794  +    cidxOpen,                     /* xOpen - open a cursor */
          795  +    cidxClose,                    /* xClose - close a cursor */
          796  +    cidxFilter,                   /* xFilter - configure scan constraints */
          797  +    cidxNext,                     /* xNext - advance a cursor */
          798  +    cidxEof,                      /* xEof - check for end of scan */
          799  +    cidxColumn,                   /* xColumn - read data */
          800  +    cidxRowid,                    /* xRowid - read data */
          801  +    0,                            /* xUpdate */
          802  +    0,                            /* xBegin */
          803  +    0,                            /* xSync */
          804  +    0,                            /* xCommit */
          805  +    0,                            /* xRollback */
          806  +    0,                            /* xFindMethod */
          807  +    0,                            /* xRename */
          808  +    0,                            /* xSavepoint */
          809  +    0,                            /* xRelease */
          810  +    0,                            /* xRollbackTo */
          811  +  };
          812  +  return sqlite3_create_module(db, "incremental_index_check", &cidx_module, 0);
          813  +}
          814  +
          815  +/*
          816  +** Extension load function.
          817  +*/
          818  +#ifdef _WIN32
          819  +__declspec(dllexport)
          820  +#endif
          821  +int sqlite3_checkindex_init(
          822  +  sqlite3 *db, 
          823  +  char **pzErrMsg, 
          824  +  const sqlite3_api_routines *pApi
          825  +){
          826  +  SQLITE_EXTENSION_INIT2(pApi);
          827  +  return ciInit(db);
          828  +}

Added ext/repair/sqlite3_checker.c.in.

            1  +/*
            2  +** Read an SQLite database file and analyze its space utilization.  Generate
            3  +** text on standard output.
            4  +*/
            5  +#define TCLSH_INIT_PROC sqlite3_checker_init_proc
            6  +#define SQLITE_ENABLE_DBPAGE_VTAB 1
            7  +#define SQLITE_ENABLE_JSON1 1
            8  +#undef SQLITE_THREADSAFE
            9  +#define SQLITE_THREADSAFE 0
           10  +#undef SQLITE_ENABLE_COLUMN_METADATA
           11  +#define SQLITE_OMIT_DECLTYPE 1
           12  +#define SQLITE_OMIT_DEPRECATED 1
           13  +#define SQLITE_OMIT_PROGRESS_CALLBACK 1
           14  +#define SQLITE_OMIT_SHARED_CACHE 1
           15  +#define SQLITE_DEFAULT_MEMSTATUS 0
           16  +#define SQLITE_MAX_EXPR_DEPTH 0
           17  +INCLUDE sqlite3.c
           18  +INCLUDE $ROOT/src/tclsqlite.c
           19  +INCLUDE $ROOT/ext/misc/btreeinfo.c
           20  +INCLUDE $ROOT/ext/repair/checkindex.c
           21  +INCLUDE $ROOT/ext/repair/checkfreelist.c
           22  +
           23  +/*
           24  +** Decode a pointer to an sqlite3 object.
           25  +*/
           26  +int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
           27  +  struct SqliteDb *p;
           28  +  Tcl_CmdInfo cmdInfo;
           29  +  if( Tcl_GetCommandInfo(interp, zA, &cmdInfo) ){
           30  +    p = (struct SqliteDb*)cmdInfo.objClientData;
           31  +    *ppDb = p->db;
           32  +    return TCL_OK;
           33  +  }else{
           34  +    *ppDb = 0;
           35  +    return TCL_ERROR;
           36  +  }
           37  +  return TCL_OK;
           38  +}
           39  +
           40  +/*
           41  +**   sqlite3_imposter db main rootpage {CREATE TABLE...}  ;# setup an imposter
           42  +**   sqlite3_imposter db main                             ;# rm all imposters
           43  +*/
           44  +static int sqlite3_imposter(
           45  +  void *clientData,
           46  +  Tcl_Interp *interp,
           47  +  int objc,
           48  +  Tcl_Obj *CONST objv[]
           49  +){
           50  +  sqlite3 *db;
           51  +  const char *zSchema;
           52  +  int iRoot;
           53  +  const char *zSql;
           54  +
           55  +  if( objc!=3 && objc!=5 ){
           56  +    Tcl_WrongNumArgs(interp, 1, objv, "DB SCHEMA [ROOTPAGE SQL]");
           57  +    return TCL_ERROR;
           58  +  }
           59  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
           60  +  zSchema = Tcl_GetString(objv[2]);
           61  +  if( objc==3 ){
           62  +    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zSchema, 0, 1);
           63  +  }else{
           64  +    if( Tcl_GetIntFromObj(interp, objv[3], &iRoot) ) return TCL_ERROR;
           65  +    zSql = Tcl_GetString(objv[4]);
           66  +    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zSchema, 1, iRoot);
           67  +    sqlite3_exec(db, zSql, 0, 0, 0);
           68  +    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, zSchema, 0, 0);
           69  +  }
           70  +  return TCL_OK;
           71  +}
           72  +
           73  +#include <stdio.h>
           74  +
           75  +const char *sqlite3_checker_init_proc(Tcl_Interp *interp){
           76  +  Tcl_CreateObjCommand(interp, "sqlite3_imposter", 
           77  +                       (Tcl_ObjCmdProc*)sqlite3_imposter, 0, 0);
           78  +  sqlite3_auto_extension((void(*)(void))sqlite3_btreeinfo_init);
           79  +  sqlite3_auto_extension((void(*)(void))sqlite3_checkindex_init);
           80  +  sqlite3_auto_extension((void(*)(void))sqlite3_checkfreelist_init);
           81  +  return
           82  +BEGIN_STRING
           83  +INCLUDE $ROOT/ext/repair/sqlite3_checker.tcl
           84  +END_STRING
           85  +;
           86  +}

Added ext/repair/sqlite3_checker.tcl.

            1  +# This TCL script is the main driver script for the sqlite3_checker utility
            2  +# program.
            3  +#
            4  +
            5  +# Special case:
            6  +#
            7  +#      sqlite3_checker --test FILENAME ARGS
            8  +#
            9  +# uses FILENAME in place of this script.
           10  +#
           11  +if {[lindex $argv 0]=="--test" && [llength $argv]>1} {
           12  +  set ::argv0 [lindex $argv 1]
           13  +  set argv [lrange $argv 2 end]
           14  +  source $argv0
           15  +  exit 0
           16  +}
           17  +
           18  +# Emulate a TCL shell
           19  +#
           20  +proc tclsh {} {
           21  +  set line {}
           22  +  while {![eof stdin]} {
           23  +    if {$line!=""} {
           24  +      puts -nonewline "> "
           25  +    } else {
           26  +      puts -nonewline "% "
           27  +    }
           28  +    flush stdout
           29  +    append line [gets stdin]
           30  +    if {[info complete $line]} {
           31  +      if {[catch {uplevel #0 $line} result]} {
           32  +        puts stderr "Error: $result"
           33  +      } elseif {$result!=""} {
           34  +        puts $result
           35  +      }
           36  +      set line {}
           37  +    } else {
           38  +      append line \n
           39  +    }
           40  +  }
           41  +}
           42  +
           43  +# Do an incremental integrity check of a single index
           44  +#
           45  +proc check_index {idxname batchsize} {
           46  +  set i 0
           47  +  set more 1
           48  +  set nerr 0
           49  +  set pct 00.0
           50  +  set max [db one {SELECT nEntry FROM sqlite_btreeinfo('main')
           51  +                    WHERE name=$idxname}]
           52  +  puts -nonewline "$idxname: $i of $max rows ($pct%)\r"
           53  +  flush stdout
           54  +  while {$more} {
           55  +    set more 0
           56  +    db eval {SELECT errmsg, current_key AS key
           57  +               FROM incremental_index_check($idxname)
           58  +              WHERE after_key=$key
           59  +              LIMIT $batchsize} {
           60  +      set more 1
           61  +      if {$errmsg!=""} {
           62  +        incr nerr
           63  +        puts "$idxname: key($key): $errmsg"
           64  +      }
           65  +      incr i
           66  +    }
           67  +    set x [format {%.1f} [expr {($i*100.0)/$max}]]
           68  +    if {$x!=$pct} {
           69  +      puts -nonewline "$idxname: $i of $max rows ($pct%)\r"
           70  +      flush stdout
           71  +      set pct $x
           72  +    }
           73  +  }
           74  +  puts "$idxname: $nerr errors out of $i entries"
           75  +}
           76  +
           77  +# Print a usage message on standard error, then quit.
           78  +#
           79  +proc usage {} {
           80  +  set argv0 [file rootname [file tail [info nameofexecutable]]]
           81  +  puts stderr "Usage: $argv0 OPTIONS database-filename"
           82  +  puts stderr {
           83  +Do sanity checking on a live SQLite3 database file specified by the
           84  +"database-filename" argument.
           85  +
           86  +Options:
           87  +
           88  +   --batchsize N     Number of rows to check per transaction
           89  +
           90  +   --freelist        Perform a freelist check
           91  +
           92  +   --index NAME      Run a check of the index NAME
           93  +
           94  +   --summary         Print summary information about the database
           95  +
           96  +   --table NAME      Run a check of all indexes for table NAME
           97  +
           98  +   --tclsh           Run the built-in TCL interpreter (for debugging)
           99  +
          100  +   --version         Show the version number of SQLite
          101  +}
          102  +  exit 1
          103  +}
          104  +
          105  +set file_to_analyze {}
          106  +append argv {}
          107  +set bFreelistCheck 0
          108  +set bSummary 0
          109  +set zIndex {}
          110  +set zTable {}
          111  +set batchsize 1000
          112  +set bAll 1
          113  +set argc [llength $argv]
          114  +for {set i 0} {$i<$argc} {incr i} {
          115  +  set arg [lindex $argv $i]
          116  +  if {[regexp {^-+tclsh$} $arg]} {
          117  +    tclsh
          118  +    exit 0
          119  +  }
          120  +  if {[regexp {^-+version$} $arg]} {
          121  +    sqlite3 mem :memory:
          122  +    puts [mem one {SELECT sqlite_version()||' '||sqlite_source_id()}]
          123  +    mem close
          124  +    exit 0
          125  +  }
          126  +  if {[regexp {^-+freelist$} $arg]} {
          127  +    set bFreelistCheck 1
          128  +    set bAll 0
          129  +    continue
          130  +  }
          131  +  if {[regexp {^-+summary$} $arg]} {
          132  +    set bSummary 1
          133  +    set bAll 0
          134  +    continue
          135  +  }
          136  +  if {[regexp {^-+batchsize$} $arg]} {
          137  +    incr i
          138  +    if {$i>=$argc} {
          139  +      puts stderr "missing argument on $arg"
          140  +      exit 1
          141  +    }
          142  +    set batchsize [lindex $argv $i]
          143  +    continue
          144  +  }
          145  +  if {[regexp {^-+index$} $arg]} {
          146  +    incr i
          147  +    if {$i>=$argc} {
          148  +      puts stderr "missing argument on $arg"
          149  +      exit 1
          150  +    }
          151  +    set zIndex [lindex $argv $i]
          152  +    set bAll 0
          153  +    continue
          154  +  }
          155  +  if {[regexp {^-+table$} $arg]} {
          156  +    incr i
          157  +    if {$i>=$argc} {
          158  +      puts stderr "missing argument on $arg"
          159  +      exit 1
          160  +    }
          161  +    set zTable [lindex $argv $i]
          162  +    set bAll 0
          163  +    continue
          164  +  }
          165  +  if {[regexp {^-} $arg]} {
          166  +    puts stderr "Unknown option: $arg"
          167  +    usage
          168  +  }
          169  +  if {$file_to_analyze!=""} {
          170  +    usage
          171  +  } else {
          172  +    set file_to_analyze $arg
          173  +  }
          174  +}
          175  +if {$file_to_analyze==""} usage
          176  +
          177  +# If a TCL script is specified on the command-line, then run that
          178  +# script.
          179  +#
          180  +if {[file extension $file_to_analyze]==".tcl"} {
          181  +  source $file_to_analyze
          182  +  exit 0
          183  +}
          184  +
          185  +set root_filename $file_to_analyze
          186  +regexp {^file:(//)?([^?]*)} $file_to_analyze all x1 root_filename
          187  +if {![file exists $root_filename]} {
          188  +  puts stderr "No such file: $root_filename"
          189  +  exit 1
          190  +}
          191  +if {![file readable $root_filename]} {
          192  +  puts stderr "File is not readable: $root_filename"
          193  +  exit 1
          194  +}
          195  +
          196  +if {[catch {sqlite3 db $file_to_analyze} res]} {
          197  +  puts stderr "Cannot open datababase $root_filename: $res"
          198  +  exit 1
          199  +}
          200  +
          201  +if {$bFreelistCheck || $bAll} {
          202  +  puts -nonewline "freelist-check: "
          203  +  flush stdout
          204  +  puts [db one {SELECT checkfreelist('main')}]
          205  +}
          206  +if {$bSummary} {
          207  +  set scale 0
          208  +  set pgsz [db one {PRAGMA page_size}]
          209  +  db eval {SELECT nPage*$pgsz AS sz, name, tbl_name
          210  +             FROM sqlite_btreeinfo
          211  +            WHERE type='index'
          212  +            ORDER BY 1 DESC, name} {
          213  +    if {$scale==0} {
          214  +      if {$sz>10000000} {
          215  +        set scale 1000000.0
          216  +        set unit MB
          217  +      } else {
          218  +        set scale 1000.0
          219  +        set unit KB
          220  +      }
          221  +    }
          222  +    puts [format {%7.1f %s index %s of table %s} \
          223  +            [expr {$sz/$scale}] $unit $name $tbl_name]
          224  +  }
          225  +}
          226  +if {$zIndex!=""} {
          227  +  check_index $zIndex $batchsize
          228  +}
          229  +if {$zTable!=""} {
          230  +  foreach idx [db eval {SELECT name FROM sqlite_master
          231  +                         WHERE type='index' AND rootpage>0
          232  +                           AND tbl_name=$zTable}] {
          233  +    check_index $idx $batchsize
          234  +  }
          235  +}
          236  +if {$bAll} {
          237  +  set allidx [db eval {SELECT name FROM sqlite_btreeinfo('main')
          238  +                        WHERE type='index' AND rootpage>0
          239  +                        ORDER BY nEntry}]
          240  +  foreach idx $allidx {
          241  +    check_index $idx $batchsize
          242  +  }
          243  +}

Added ext/repair/test/README.md.

            1  +To run these tests, first build sqlite3_checker:
            2  +
            3  +
            4  +>     make sqlite3_checker
            5  +
            6  +
            7  +Then run the "test.tcl" script using:
            8  +
            9  +
           10  +>     ./sqlite3_checker --test $path/test.tcl
           11  +
           12  +
           13  +Optionally add the full pathnames of individual *.test modules

Added ext/repair/test/checkfreelist01.test.

            1  +# 2017-10-11
            2  +
            3  +set testprefix checkfreelist
            4  +
            5  +do_execsql_test 1.0 {
            6  +  PRAGMA page_size=1024;
            7  +  CREATE TABLE t1(a, b);
            8  +}
            9  +
           10  +do_execsql_test 1.2 { SELECT checkfreelist('main') } {ok}
           11  +do_execsql_test 1.3 {
           12  +  WITH s(i) AS (
           13  +    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
           14  +  )
           15  +  INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
           16  +  DELETE FROM t1 WHERE rowid%3;
           17  +  PRAGMA freelist_count;
           18  +} {6726}
           19  +
           20  +do_execsql_test 1.4 { SELECT checkfreelist('main') } {ok}
           21  +do_execsql_test 1.5 {
           22  +  WITH freelist_trunk(i, d, n) AS (
           23  +    SELECT 1, NULL, sqlite_readint32(data, 32) FROM sqlite_dbpage WHERE pgno=1
           24  +      UNION ALL
           25  +    SELECT n, data, sqlite_readint32(data) 
           26  +    FROM freelist_trunk, sqlite_dbpage WHERE pgno=n
           27  +  )
           28  +  SELECT i FROM freelist_trunk WHERE i!=1;
           29  +} {
           30  +  10009 9715 9343 8969 8595 8222 7847 7474 7102 6727 6354 5982 5608 5234
           31  +  4860 4487 4112 3740 3367 2992 2619 2247 1872 1499 1125 752 377 5
           32  +}
           33  +
           34  +do_execsql_test 1.6 { SELECT checkfreelist('main') } {ok}
           35  +
           36  +proc set_int {blob idx newval} {
           37  +  binary scan $blob I* ints
           38  +  lset ints $idx $newval
           39  +  binary format I* $ints
           40  +}
           41  +db func set_int set_int
           42  +
           43  +proc get_int {blob idx} {
           44  +  binary scan $blob I* ints
           45  +  lindex $ints $idx
           46  +}
           47  +db func get_int get_int
           48  +
           49  +do_execsql_test 1.7 {
           50  +  BEGIN;
           51  +    UPDATE sqlite_dbpage 
           52  +      SET data = set_int(data, 1, get_int(data, 1)-1) 
           53  +      WHERE pgno=4860;
           54  +    SELECT checkfreelist('main');
           55  +  ROLLBACK;
           56  +} {{free-list count mismatch: actual=6725 header=6726}}
           57  +
           58  +do_execsql_test 1.8 {
           59  +  BEGIN;
           60  +    UPDATE sqlite_dbpage 
           61  +      SET data = set_int(data, 5, (SELECT * FROM pragma_page_count)+1)
           62  +      WHERE pgno=4860;
           63  +    SELECT checkfreelist('main');
           64  +  ROLLBACK;
           65  +} {{leaf page 10092 is out of range (child 3 of trunk page 4860)}}
           66  +
           67  +do_execsql_test 1.9 {
           68  +  BEGIN;
           69  +    UPDATE sqlite_dbpage 
           70  +      SET data = set_int(data, 5, 0)
           71  +      WHERE pgno=4860;
           72  +    SELECT checkfreelist('main');
           73  +  ROLLBACK;
           74  +} {{leaf page 0 is out of range (child 3 of trunk page 4860)}}
           75  +
           76  +do_execsql_test 1.10 {
           77  +  BEGIN;
           78  +    UPDATE sqlite_dbpage 
           79  +      SET data = set_int(data, get_int(data, 1)+1, 0)
           80  +      WHERE pgno=5;
           81  +    SELECT checkfreelist('main');
           82  +  ROLLBACK;
           83  +} {{leaf page 0 is out of range (child 247 of trunk page 5)}}
           84  +
           85  +do_execsql_test 1.11 {
           86  +  BEGIN;
           87  +    UPDATE sqlite_dbpage 
           88  +      SET data = set_int(data, 1, 249)
           89  +      WHERE pgno=5;
           90  +    SELECT checkfreelist('main');
           91  +  ROLLBACK;
           92  +} {{leaf count out of range (249) on trunk page 5}}

Added ext/repair/test/checkindex01.test.

            1  +# 2017-10-11
            2  +#
            3  +set testprefix checkindex
            4  +
            5  +do_execsql_test 1.0 {
            6  +  CREATE TABLE t1(a, b);
            7  +  CREATE INDEX i1 ON t1(a);
            8  +  INSERT INTO t1 VALUES('one', 2);
            9  +  INSERT INTO t1 VALUES('two', 4);
           10  +  INSERT INTO t1 VALUES('three', 6);
           11  +  INSERT INTO t1 VALUES('four', 8);
           12  +  INSERT INTO t1 VALUES('five', 10);
           13  +
           14  +  CREATE INDEX i2 ON t1(a DESC);
           15  +} {}
           16  +
           17  +proc incr_index_check {idx nStep} {
           18  +  set Q {
           19  +    SELECT errmsg, current_key FROM incremental_index_check($idx, $after)
           20  +    LIMIT $nStep
           21  +  }
           22  +
           23  +  set res [list]
           24  +  while {1} {
           25  +    unset -nocomplain current_key
           26  +    set res1 [db eval $Q]
           27  +    if {[llength $res1]==0} break
           28  +    set res [concat $res $res1]
           29  +    set after [lindex $res end]
           30  +  }
           31  +
           32  +  return $res
           33  +}
           34  +
           35  +proc do_index_check_test {tn idx res} {
           36  +  uplevel [list do_execsql_test $tn.1 "
           37  +    SELECT errmsg, current_key FROM incremental_index_check('$idx');
           38  +  " $res]
           39  +
           40  +  uplevel [list do_test $tn.2 "incr_index_check $idx 1" [list {*}$res]]
           41  +  uplevel [list do_test $tn.3 "incr_index_check $idx 2" [list {*}$res]]
           42  +  uplevel [list do_test $tn.4 "incr_index_check $idx 5" [list {*}$res]]
           43  +}
           44  +
           45  +
           46  +do_execsql_test 1.2 {
           47  +  SELECT errmsg IS NULL, current_key FROM incremental_index_check('i1');
           48  +} {
           49  +  1 'five',5
           50  +  1 'four',4
           51  +  1 'one',1
           52  +  1 'three',3
           53  +  1 'two',2
           54  +}
           55  +
           56  +do_index_check_test 1.3 i1 {
           57  +  {} 'five',5
           58  +  {} 'four',4
           59  +  {} 'one',1
           60  +  {} 'three',3
           61  +  {} 'two',2
           62  +}
           63  +
           64  +do_index_check_test 1.4 i2 {
           65  +  {} 'two',2
           66  +  {} 'three',3
           67  +  {} 'one',1
           68  +  {} 'four',4
           69  +  {} 'five',5
           70  +}
           71  +
           72  +do_test 1.5 {
           73  +  set tblroot [db one { SELECT rootpage FROM sqlite_master WHERE name='t1' }]
           74  +  sqlite3_imposter db main $tblroot {CREATE TABLE xt1(a,b)}
           75  +  db eval {
           76  +    UPDATE xt1 SET a='six' WHERE rowid=3;
           77  +    DELETE FROM xt1 WHERE rowid = 5;
           78  +  }
           79  +  sqlite3_imposter db main
           80  +} {}
           81  +
           82  +do_index_check_test 1.6 i1 {
           83  +  {row missing} 'five',5
           84  +  {} 'four',4
           85  +  {} 'one',1
           86  +  {row data mismatch} 'three',3
           87  +  {} 'two',2
           88  +}
           89  +
           90  +do_index_check_test 1.7 i2 {
           91  +  {} 'two',2
           92  +  {row data mismatch} 'three',3
           93  +  {} 'one',1
           94  +  {} 'four',4
           95  +  {row missing} 'five',5
           96  +}
           97  +
           98  +#--------------------------------------------------------------------------
           99  +do_execsql_test 2.0 {
          100  +
          101  +  CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c, d);
          102  +
          103  +  INSERT INTO t2 VALUES(1, NULL, 1, 1);
          104  +  INSERT INTO t2 VALUES(2, 1, NULL, 1);
          105  +  INSERT INTO t2 VALUES(3, 1, 1, NULL);
          106  +
          107  +  INSERT INTO t2 VALUES(4, 2, 2, 1);
          108  +  INSERT INTO t2 VALUES(5, 2, 2, 2);
          109  +  INSERT INTO t2 VALUES(6, 2, 2, 3);
          110  +
          111  +  INSERT INTO t2 VALUES(7, 2, 2, 1);
          112  +  INSERT INTO t2 VALUES(8, 2, 2, 2);
          113  +  INSERT INTO t2 VALUES(9, 2, 2, 3);
          114  +
          115  +  CREATE INDEX i3 ON t2(b, c, d);
          116  +  CREATE INDEX i4 ON t2(b DESC, c DESC, d DESC);
          117  +  CREATE INDEX i5 ON t2(d, c DESC, b);
          118  +} {}
          119  +
          120  +do_index_check_test 2.1 i3 {
          121  +  {} NULL,1,1,1 
          122  +  {} 1,NULL,1,2 
          123  +  {} 1,1,NULL,3 
          124  +  {} 2,2,1,4 
          125  +  {} 2,2,1,7 
          126  +  {} 2,2,2,5
          127  +  {} 2,2,2,8 
          128  +  {} 2,2,3,6 
          129  +  {} 2,2,3,9
          130  +}
          131  +
          132  +do_index_check_test 2.2 i4 {
          133  +  {} 2,2,3,6 
          134  +  {} 2,2,3,9
          135  +  {} 2,2,2,5
          136  +  {} 2,2,2,8 
          137  +  {} 2,2,1,4 
          138  +  {} 2,2,1,7 
          139  +  {} 1,1,NULL,3 
          140  +  {} 1,NULL,1,2 
          141  +  {} NULL,1,1,1 
          142  +}
          143  +
          144  +do_index_check_test 2.3 i5 {
          145  +  {} NULL,1,1,3 
          146  +  {} 1,2,2,4 
          147  +  {} 1,2,2,7 
          148  +  {} 1,1,NULL,1 
          149  +  {} 1,NULL,1,2 
          150  +  {} 2,2,2,5 
          151  +  {} 2,2,2,8 
          152  +  {} 3,2,2,6 
          153  +  {} 3,2,2,9
          154  +}
          155  +
          156  +#--------------------------------------------------------------------------
          157  +do_execsql_test 3.0 {
          158  +
          159  +  CREATE TABLE t3(w, x, y, z PRIMARY KEY) WITHOUT ROWID;
          160  +  CREATE INDEX t3wxy ON t3(w, x, y);
          161  +  CREATE INDEX t3wxy2 ON t3(w DESC, x DESC, y DESC);
          162  +
          163  +  INSERT INTO t3 VALUES(NULL, NULL, NULL, 1);
          164  +  INSERT INTO t3 VALUES(NULL, NULL, NULL, 2);
          165  +  INSERT INTO t3 VALUES(NULL, NULL, NULL, 3);
          166  +
          167  +  INSERT INTO t3 VALUES('a', NULL, NULL, 4);
          168  +  INSERT INTO t3 VALUES('a', NULL, NULL, 5);
          169  +  INSERT INTO t3 VALUES('a', NULL, NULL, 6);
          170  +
          171  +  INSERT INTO t3 VALUES('a', 'b', NULL, 7);
          172  +  INSERT INTO t3 VALUES('a', 'b', NULL, 8);
          173  +  INSERT INTO t3 VALUES('a', 'b', NULL, 9);
          174  +
          175  +} {}
          176  +
          177  +do_index_check_test 3.1 t3wxy {
          178  +  {} NULL,NULL,NULL,1 {} NULL,NULL,NULL,2 {} NULL,NULL,NULL,3 
          179  +  {} 'a',NULL,NULL,4  {} 'a',NULL,NULL,5  {} 'a',NULL,NULL,6 
          180  +  {} 'a','b',NULL,7   {} 'a','b',NULL,8   {} 'a','b',NULL,9 
          181  +}
          182  +do_index_check_test 3.2 t3wxy2 {
          183  +  {} 'a','b',NULL,7   {} 'a','b',NULL,8   {} 'a','b',NULL,9 
          184  +  {} 'a',NULL,NULL,4  {} 'a',NULL,NULL,5  {} 'a',NULL,NULL,6 
          185  +  {} NULL,NULL,NULL,1 {} NULL,NULL,NULL,2 {} NULL,NULL,NULL,3 
          186  +}
          187  +
          188  +#--------------------------------------------------------------------------
          189  +# Test with an index that uses non-default collation sequences.
          190  +#
          191  +do_execsql_test 4.0 {
          192  +  CREATE TABLE t4(a INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT);
          193  +  INSERT INTO t4 VALUES(1, 'aaa', 'bbb');
          194  +  INSERT INTO t4 VALUES(2, 'AAA', 'CCC');
          195  +  INSERT INTO t4 VALUES(3, 'aab', 'ddd');
          196  +  INSERT INTO t4 VALUES(4, 'AAB', 'EEE');
          197  +
          198  +  CREATE INDEX t4cc ON t4(c1 COLLATE nocase, c2 COLLATE nocase);
          199  +}
          200  +
          201  +do_index_check_test 4.1 t4cc {
          202  +  {} 'aaa','bbb',1 
          203  +  {} 'AAA','CCC',2 
          204  +  {} 'aab','ddd',3 
          205  +  {} 'AAB','EEE',4
          206  +}
          207  +
          208  +do_test 4.2 {
          209  +  set tblroot [db one { SELECT rootpage FROM sqlite_master WHERE name='t4' }]
          210  +  sqlite3_imposter db main $tblroot \
          211  +     {CREATE TABLE xt4(a INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT)}
          212  +
          213  +  db eval {
          214  +    UPDATE xt4 SET c1='hello' WHERE rowid=2;
          215  +    DELETE FROM xt4 WHERE rowid = 3;
          216  +  }
          217  +  sqlite3_imposter db main
          218  +} {}
          219  +
          220  +do_index_check_test 4.3 t4cc {
          221  +  {} 'aaa','bbb',1 
          222  +  {row data mismatch} 'AAA','CCC',2 
          223  +  {row missing} 'aab','ddd',3 
          224  +  {} 'AAB','EEE',4
          225  +}
          226  +
          227  +#--------------------------------------------------------------------------
          228  +# Test an index on an expression.
          229  +#
          230  +do_execsql_test 5.0 {
          231  +  CREATE TABLE t5(x INTEGER PRIMARY KEY, y TEXT, UNIQUE(y));
          232  +  INSERT INTO t5 VALUES(1, '{"x":1, "y":1}');
          233  +  INSERT INTO t5 VALUES(2, '{"x":2, "y":2}');
          234  +  INSERT INTO t5 VALUES(3, '{"x":3, "y":3}');
          235  +  INSERT INTO t5 VALUES(4, '{"w":4, "z":4}');
          236  +  INSERT INTO t5 VALUES(5, '{"x":5, "y":5}');
          237  +
          238  +  CREATE INDEX t5x ON t5( json_extract(y, '$.x') );
          239  +  CREATE INDEX t5y ON t5( json_extract(y, '$.y') DESC );
          240  +}
          241  +
          242  +do_index_check_test 5.1.1 t5x {
          243  +  {} NULL,4 {} 1,1 {} 2,2 {} 3,3 {} 5,5
          244  +}
          245  +
          246  +do_index_check_test 5.1.2 t5y {
          247  +  {} 5,5 {} 3,3 {} 2,2 {} 1,1 {} NULL,4
          248  +}
          249  +
          250  +do_index_check_test 5.1.3 sqlite_autoindex_t5_1 {
          251  +  {} {'{"w":4, "z":4}',4} 
          252  +  {} {'{"x":1, "y":1}',1} 
          253  +  {} {'{"x":2, "y":2}',2} 
          254  +  {} {'{"x":3, "y":3}',3} 
          255  +  {} {'{"x":5, "y":5}',5}
          256  +}
          257  +
          258  +do_test 5.2 {
          259  +  set tblroot [db one { SELECT rootpage FROM sqlite_master WHERE name='t5' }]
          260  +  sqlite3_imposter db main $tblroot \
          261  +      {CREATE TABLE xt5(a INTEGER PRIMARY KEY, c1 TEXT);}
          262  +  db eval {
          263  +    UPDATE xt5 SET c1='{"x":22, "y":11}' WHERE rowid=1;
          264  +    DELETE FROM xt5 WHERE rowid = 4;
          265  +  }
          266  +  sqlite3_imposter db main
          267  +} {}
          268  +
          269  +do_index_check_test 5.3.1 t5x {
          270  +  {row missing} NULL,4 
          271  +  {row data mismatch} 1,1 
          272  +  {} 2,2 
          273  +  {} 3,3 
          274  +  {} 5,5
          275  +}
          276  +
          277  +do_index_check_test 5.3.2 sqlite_autoindex_t5_1 {
          278  +  {row missing} {'{"w":4, "z":4}',4} 
          279  +  {row data mismatch} {'{"x":1, "y":1}',1} 
          280  +  {} {'{"x":2, "y":2}',2} 
          281  +  {} {'{"x":3, "y":3}',3} 
          282  +  {} {'{"x":5, "y":5}',5}
          283  +}
          284  +
          285  +#-------------------------------------------------------------------------
          286  +#
          287  +do_execsql_test 6.0 {
          288  +  CREATE TABLE t6(x INTEGER PRIMARY KEY, y, z);
          289  +  CREATE INDEX t6x1 ON t6(y, /* one,two,three */ z);
          290  +  CREATE INDEX t6x2 ON t6(z, -- hello,world,
          291  +  y);
          292  +
          293  +  CREATE INDEX t6x3 ON t6(z -- hello,world
          294  +  , y);
          295  +
          296  +  INSERT INTO t6 VALUES(1, 2, 3);
          297  +  INSERT INTO t6 VALUES(4, 5, 6);
          298  +}
          299  +
          300  +do_index_check_test 6.1 t6x1 {
          301  +  {} 2,3,1 
          302  +  {} 5,6,4
          303  +}
          304  +do_index_check_test 6.2 t6x2 {
          305  +  {} 3,2,1 
          306  +  {} 6,5,4
          307  +}
          308  +do_index_check_test 6.2 t6x3 {
          309  +  {} 3,2,1 
          310  +  {} 6,5,4
          311  +}

Added ext/repair/test/test.tcl.

            1  +# Run this script using
            2  +#
            3  +#       sqlite3_checker --test $thisscript $testscripts
            4  +#
            5  +# The $testscripts argument is optional.  If omitted, all *.test files
            6  +# in the same directory as $thisscript are run.
            7  +#
            8  +set NTEST 0
            9  +set NERR  0
           10  +
           11  +
           12  +# Invoke the do_test procedure to run a single test
           13  +#
           14  +# The $expected parameter is the expected result.  The result is the return
           15  +# value from the last TCL command in $cmd.
           16  +#
           17  +# Normally, $expected must match exactly.  But if $expected is of the form
           18  +# "/regexp/" then regular expression matching is used.  If $expected is
           19  +# "~/regexp/" then the regular expression must NOT match.  If $expected is
           20  +# of the form "#/value-list/" then each term in value-list must be numeric
           21  +# and must approximately match the corresponding numeric term in $result.
           22  +# Values must match within 10%.  Or if the $expected term is A..B then the
           23  +# $result term must be in between A and B.
           24  +#
           25  +proc do_test {name cmd expected} {
           26  +  if {[info exists ::testprefix]} {
           27  +    set name "$::testprefix$name"
           28  +  }
           29  +
           30  +  incr ::NTEST
           31  +  puts -nonewline $name...
           32  +  flush stdout
           33  +
           34  +  if {[catch {uplevel #0 "$cmd;\n"} result]} {
           35  +    puts -nonewline $name...
           36  +    puts "\nError: $result"
           37  +    incr ::NERR
           38  +  } else {
           39  +    set ok [expr {[string compare $result $expected]==0}]
           40  +    if {!$ok} {
           41  +      puts "\n!  $name expected: \[$expected\]\n! $name got:      \[$result\]"
           42  +      incr ::NERR
           43  +    } else {
           44  +      puts " Ok"
           45  +    }
           46  +  }
           47  +  flush stdout
           48  +}
           49  +
           50  +#
           51  +#   do_execsql_test TESTNAME SQL RES
           52  +#
           53  +proc do_execsql_test {testname sql {result {}}} {
           54  +  uplevel [list do_test $testname [list db eval $sql] [list {*}$result]]
           55  +}
           56  +
           57  +if {[llength $argv]==0} {
           58  +  set dir [file dirname $argv0]
           59  +  set argv [glob -nocomplain $dir/*.test]
           60  +}
           61  +foreach testfile $argv {
           62  +  file delete -force test.db
           63  +  sqlite3 db test.db
           64  +  source $testfile
           65  +  catch {db close}
           66  +}
           67  +puts "$NERR errors out of $NTEST tests"

Changes to ext/rtree/rtree.c.

   205    205   #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
   206    206   #define RTREE_MAXCELLS 51
   207    207   
   208    208   /*
   209    209   ** The smallest possible node-size is (512-64)==448 bytes. And the largest
   210    210   ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
   211    211   ** Therefore all non-root nodes must contain at least 3 entries. Since 
   212         -** 2^40 is greater than 2^64, an r-tree structure always has a depth of
          212  +** 3^40 is greater than 2^64, an r-tree structure always has a depth of
   213    213   ** 40 or less.
   214    214   */
   215    215   #define RTREE_MAX_DEPTH 40
   216    216   
   217    217   
   218    218   /*
   219    219   ** Number of entries in the cursor RtreeNode cache.  The first entry is
................................................................................
  3603   3603     ){
  3604   3604       sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
  3605   3605     }else{
  3606   3606       u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
  3607   3607       sqlite3_result_int(ctx, readInt16(zBlob));
  3608   3608     }
  3609   3609   }
         3610  +
         3611  +/*
         3612  +** Context object passed between the various routines that make up the
         3613  +** implementation of integrity-check function rtreecheck().
         3614  +*/
         3615  +typedef struct RtreeCheck RtreeCheck;
         3616  +struct RtreeCheck {
         3617  +  sqlite3 *db;                    /* Database handle */
         3618  +  const char *zDb;                /* Database containing rtree table */
         3619  +  const char *zTab;               /* Name of rtree table */
         3620  +  int bInt;                       /* True for rtree_i32 table */
         3621  +  int nDim;                       /* Number of dimensions for this rtree tbl */
         3622  +  sqlite3_stmt *pGetNode;         /* Statement used to retrieve nodes */
         3623  +  sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
         3624  +  int nLeaf;                      /* Number of leaf cells in table */
         3625  +  int nNonLeaf;                   /* Number of non-leaf cells in table */
         3626  +  int rc;                         /* Return code */
         3627  +  char *zReport;                  /* Message to report */
         3628  +  int nErr;                       /* Number of lines in zReport */
         3629  +};
         3630  +
         3631  +#define RTREE_CHECK_MAX_ERROR 100
         3632  +
         3633  +/*
         3634  +** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
         3635  +** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
         3636  +*/
         3637  +static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
         3638  +  int rc = sqlite3_reset(pStmt);
         3639  +  if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
         3640  +}
         3641  +
         3642  +/*
         3643  +** The second and subsequent arguments to this function are a format string
         3644  +** and printf style arguments. This function formats the string and attempts
         3645  +** to compile it as an SQL statement.
         3646  +**
         3647  +** If successful, a pointer to the new SQL statement is returned. Otherwise,
         3648  +** NULL is returned and an error code left in RtreeCheck.rc.
         3649  +*/
         3650  +static sqlite3_stmt *rtreeCheckPrepare(
         3651  +  RtreeCheck *pCheck,             /* RtreeCheck object */
         3652  +  const char *zFmt, ...           /* Format string and trailing args */
         3653  +){
         3654  +  va_list ap;
         3655  +  char *z;
         3656  +  sqlite3_stmt *pRet = 0;
         3657  +
         3658  +  va_start(ap, zFmt);
         3659  +  z = sqlite3_vmprintf(zFmt, ap);
         3660  +
         3661  +  if( pCheck->rc==SQLITE_OK ){
         3662  +    if( z==0 ){
         3663  +      pCheck->rc = SQLITE_NOMEM;
         3664  +    }else{
         3665  +      pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
         3666  +    }
         3667  +  }
         3668  +
         3669  +  sqlite3_free(z);
         3670  +  va_end(ap);
         3671  +  return pRet;
         3672  +}
         3673  +
         3674  +/*
         3675  +** The second and subsequent arguments to this function are a printf()
         3676  +** style format string and arguments. This function formats the string and
         3677  +** appends it to the report being accumuated in pCheck.
         3678  +*/
         3679  +static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
         3680  +  va_list ap;
         3681  +  va_start(ap, zFmt);
         3682  +  if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
         3683  +    char *z = sqlite3_vmprintf(zFmt, ap);
         3684  +    if( z==0 ){
         3685  +      pCheck->rc = SQLITE_NOMEM;
         3686  +    }else{
         3687  +      pCheck->zReport = sqlite3_mprintf("%z%s%z", 
         3688  +          pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
         3689  +      );
         3690  +      if( pCheck->zReport==0 ){
         3691  +        pCheck->rc = SQLITE_NOMEM;
         3692  +      }
         3693  +    }
         3694  +    pCheck->nErr++;
         3695  +  }
         3696  +  va_end(ap);
         3697  +}
         3698  +
         3699  +/*
         3700  +** This function is a no-op if there is already an error code stored
         3701  +** in the RtreeCheck object indicated by the first argument. NULL is
         3702  +** returned in this case.
         3703  +**
         3704  +** Otherwise, the contents of rtree table node iNode are loaded from
         3705  +** the database and copied into a buffer obtained from sqlite3_malloc().
         3706  +** If no error occurs, a pointer to the buffer is returned and (*pnNode)
         3707  +** is set to the size of the buffer in bytes.
         3708  +**
         3709  +** Or, if an error does occur, NULL is returned and an error code left
         3710  +** in the RtreeCheck object. The final value of *pnNode is undefined in
         3711  +** this case.
         3712  +*/
         3713  +static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
         3714  +  u8 *pRet = 0;                   /* Return value */
         3715  +
         3716  +  assert( pCheck->rc==SQLITE_OK );
         3717  +  if( pCheck->pGetNode==0 ){
         3718  +    pCheck->pGetNode = rtreeCheckPrepare(pCheck,
         3719  +        "SELECT data FROM %Q.'%q_node' WHERE nodeno=?", 
         3720  +        pCheck->zDb, pCheck->zTab
         3721  +    );
         3722  +  }
         3723  +
         3724  +  if( pCheck->rc==SQLITE_OK ){
         3725  +    sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
         3726  +    if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
         3727  +      int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
         3728  +      const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
         3729  +      pRet = sqlite3_malloc(nNode);
         3730  +      if( pRet==0 ){
         3731  +        pCheck->rc = SQLITE_NOMEM;
         3732  +      }else{
         3733  +        memcpy(pRet, pNode, nNode);
         3734  +        *pnNode = nNode;
         3735  +      }
         3736  +    }
         3737  +    rtreeCheckReset(pCheck, pCheck->pGetNode);
         3738  +    if( pCheck->rc==SQLITE_OK && pRet==0 ){
         3739  +      rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
         3740  +    }
         3741  +  }
         3742  +
         3743  +  return pRet;
         3744  +}
         3745  +
         3746  +/*
         3747  +** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
         3748  +** (if bLeaf==1) table contains a specified entry. The schemas of the
         3749  +** two tables are:
         3750  +**
         3751  +**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
         3752  +**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
         3753  +**
         3754  +** In both cases, this function checks that there exists an entry with
         3755  +** IPK value iKey and the second column set to iVal.
         3756  +**
         3757  +*/
         3758  +static void rtreeCheckMapping(
         3759  +  RtreeCheck *pCheck,             /* RtreeCheck object */
         3760  +  int bLeaf,                      /* True for a leaf cell, false for interior */
         3761  +  i64 iKey,                       /* Key for mapping */
         3762  +  i64 iVal                        /* Expected value for mapping */
         3763  +){
         3764  +  int rc;
         3765  +  sqlite3_stmt *pStmt;
         3766  +  const char *azSql[2] = {
         3767  +    "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?",
         3768  +    "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?"
         3769  +  };
         3770  +
         3771  +  assert( bLeaf==0 || bLeaf==1 );
         3772  +  if( pCheck->aCheckMapping[bLeaf]==0 ){
         3773  +    pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
         3774  +        azSql[bLeaf], pCheck->zDb, pCheck->zTab
         3775  +    );
         3776  +  }
         3777  +  if( pCheck->rc!=SQLITE_OK ) return;
         3778  +
         3779  +  pStmt = pCheck->aCheckMapping[bLeaf];
         3780  +  sqlite3_bind_int64(pStmt, 1, iKey);
         3781  +  rc = sqlite3_step(pStmt);
         3782  +  if( rc==SQLITE_DONE ){
         3783  +    rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
         3784  +        iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
         3785  +    );
         3786  +  }else if( rc==SQLITE_ROW ){
         3787  +    i64 ii = sqlite3_column_int64(pStmt, 0);
         3788  +    if( ii!=iVal ){
         3789  +      rtreeCheckAppendMsg(pCheck, 
         3790  +          "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
         3791  +          iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
         3792  +      );
         3793  +    }
         3794  +  }
         3795  +  rtreeCheckReset(pCheck, pStmt);
         3796  +}
         3797  +
         3798  +/*
         3799  +** Argument pCell points to an array of coordinates stored on an rtree page.
         3800  +** This function checks that the coordinates are internally consistent (no
         3801  +** x1>x2 conditions) and adds an error message to the RtreeCheck object
         3802  +** if they are not.
         3803  +**
         3804  +** Additionally, if pParent is not NULL, then it is assumed to point to
         3805  +** the array of coordinates on the parent page that bound the page 
         3806  +** containing pCell. In this case it is also verified that the two
         3807  +** sets of coordinates are mutually consistent and an error message added
         3808  +** to the RtreeCheck object if they are not.
         3809  +*/
         3810  +static void rtreeCheckCellCoord(
         3811  +  RtreeCheck *pCheck, 
         3812  +  i64 iNode,                      /* Node id to use in error messages */
         3813  +  int iCell,                      /* Cell number to use in error messages */
         3814  +  u8 *pCell,                      /* Pointer to cell coordinates */
         3815  +  u8 *pParent                     /* Pointer to parent coordinates */
         3816  +){
         3817  +  RtreeCoord c1, c2;
         3818  +  RtreeCoord p1, p2;
         3819  +  int i;
         3820  +
         3821  +  for(i=0; i<pCheck->nDim; i++){
         3822  +    readCoord(&pCell[4*2*i], &c1);
         3823  +    readCoord(&pCell[4*(2*i + 1)], &c2);
         3824  +
         3825  +    /* printf("%e, %e\n", c1.u.f, c2.u.f); */
         3826  +    if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
         3827  +      rtreeCheckAppendMsg(pCheck, 
         3828  +          "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
         3829  +      );
         3830  +    }
         3831  +
         3832  +    if( pParent ){
         3833  +      readCoord(&pParent[4*2*i], &p1);
         3834  +      readCoord(&pParent[4*(2*i + 1)], &p2);
         3835  +
         3836  +      if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f) 
         3837  +       || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
         3838  +      ){
         3839  +        rtreeCheckAppendMsg(pCheck, 
         3840  +            "Dimension %d of cell %d on node %lld is corrupt relative to parent"
         3841  +            , i, iCell, iNode
         3842  +        );
         3843  +      }
         3844  +    }
         3845  +  }
         3846  +}
         3847  +
         3848  +/*
         3849  +** Run rtreecheck() checks on node iNode, which is at depth iDepth within
         3850  +** the r-tree structure. Argument aParent points to the array of coordinates
         3851  +** that bound node iNode on the parent node.
         3852  +**
         3853  +** If any problems are discovered, an error message is appended to the
         3854  +** report accumulated in the RtreeCheck object.
         3855  +*/
         3856  +static void rtreeCheckNode(
         3857  +  RtreeCheck *pCheck,
         3858  +  int iDepth,                     /* Depth of iNode (0==leaf) */
         3859  +  u8 *aParent,                    /* Buffer containing parent coords */
         3860  +  i64 iNode                       /* Node to check */
         3861  +){
         3862  +  u8 *aNode = 0;
         3863  +  int nNode = 0;
         3864  +
         3865  +  assert( iNode==1 || aParent!=0 );
         3866  +  assert( pCheck->nDim>0 );
         3867  +
         3868  +  aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
         3869  +  if( aNode ){
         3870  +    if( nNode<4 ){
         3871  +      rtreeCheckAppendMsg(pCheck, 
         3872  +          "Node %lld is too small (%d bytes)", iNode, nNode
         3873  +      );
         3874  +    }else{
         3875  +      int nCell;                  /* Number of cells on page */
         3876  +      int i;                      /* Used to iterate through cells */
         3877  +      if( aParent==0 ){
         3878  +        iDepth = readInt16(aNode);
         3879  +        if( iDepth>RTREE_MAX_DEPTH ){
         3880  +          rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
         3881  +          sqlite3_free(aNode);
         3882  +          return;
         3883  +        }
         3884  +      }
         3885  +      nCell = readInt16(&aNode[2]);
         3886  +      if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
         3887  +        rtreeCheckAppendMsg(pCheck, 
         3888  +            "Node %lld is too small for cell count of %d (%d bytes)", 
         3889  +            iNode, nCell, nNode
         3890  +        );
         3891  +      }else{
         3892  +        for(i=0; i<nCell; i++){
         3893  +          u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
         3894  +          i64 iVal = readInt64(pCell);
         3895  +          rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
         3896  +
         3897  +          if( iDepth>0 ){
         3898  +            rtreeCheckMapping(pCheck, 0, iVal, iNode);
         3899  +            rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
         3900  +            pCheck->nNonLeaf++;
         3901  +          }else{
         3902  +            rtreeCheckMapping(pCheck, 1, iVal, iNode);
         3903  +            pCheck->nLeaf++;
         3904  +          }
         3905  +        }
         3906  +      }
         3907  +    }
         3908  +    sqlite3_free(aNode);
         3909  +  }
         3910  +}
         3911  +
         3912  +/*
         3913  +** The second argument to this function must be either "_rowid" or
         3914  +** "_parent". This function checks that the number of entries in the
         3915  +** %_rowid or %_parent table is exactly nExpect. If not, it adds
         3916  +** an error message to the report in the RtreeCheck object indicated
         3917  +** by the first argument.
         3918  +*/
         3919  +static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
         3920  +  if( pCheck->rc==SQLITE_OK ){
         3921  +    sqlite3_stmt *pCount;
         3922  +    pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
         3923  +        pCheck->zDb, pCheck->zTab, zTbl
         3924  +    );
         3925  +    if( pCount ){
         3926  +      if( sqlite3_step(pCount)==SQLITE_ROW ){
         3927  +        i64 nActual = sqlite3_column_int64(pCount, 0);
         3928  +        if( nActual!=nExpect ){
         3929  +          rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
         3930  +              " - expected %lld, actual %lld" , zTbl, nExpect, nActual
         3931  +          );
         3932  +        }
         3933  +      }
         3934  +      pCheck->rc = sqlite3_finalize(pCount);
         3935  +    }
         3936  +  }
         3937  +}
         3938  +
         3939  +/*
         3940  +** This function does the bulk of the work for the rtree integrity-check.
         3941  +** It is called by rtreecheck(), which is the SQL function implementation.
         3942  +*/
         3943  +static int rtreeCheckTable(
         3944  +  sqlite3 *db,                    /* Database handle to access db through */
         3945  +  const char *zDb,                /* Name of db ("main", "temp" etc.) */
         3946  +  const char *zTab,               /* Name of rtree table to check */
         3947  +  char **pzReport                 /* OUT: sqlite3_malloc'd report text */
         3948  +){
         3949  +  RtreeCheck check;               /* Common context for various routines */
         3950  +  sqlite3_stmt *pStmt = 0;        /* Used to find column count of rtree table */
         3951  +  int bEnd = 0;                   /* True if transaction should be closed */
         3952  +
         3953  +  /* Initialize the context object */
         3954  +  memset(&check, 0, sizeof(check));
         3955  +  check.db = db;
         3956  +  check.zDb = zDb;
         3957  +  check.zTab = zTab;
         3958  +
         3959  +  /* If there is not already an open transaction, open one now. This is
         3960  +  ** to ensure that the queries run as part of this integrity-check operate
         3961  +  ** on a consistent snapshot.  */
         3962  +  if( sqlite3_get_autocommit(db) ){
         3963  +    check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
         3964  +    bEnd = 1;
         3965  +  }
         3966  +
         3967  +  /* Find number of dimensions in the rtree table. */
         3968  +  pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
         3969  +  if( pStmt ){
         3970  +    int rc;
         3971  +    check.nDim = (sqlite3_column_count(pStmt) - 1) / 2;
         3972  +    if( check.nDim<1 ){
         3973  +      rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
         3974  +    }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
         3975  +      check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
         3976  +    }
         3977  +    rc = sqlite3_finalize(pStmt);
         3978  +    if( rc!=SQLITE_CORRUPT ) check.rc = rc;
         3979  +  }
         3980  +
         3981  +  /* Do the actual integrity-check */
         3982  +  if( check.nDim>=1 ){
         3983  +    if( check.rc==SQLITE_OK ){
         3984  +      rtreeCheckNode(&check, 0, 0, 1);
         3985  +    }
         3986  +    rtreeCheckCount(&check, "_rowid", check.nLeaf);
         3987  +    rtreeCheckCount(&check, "_parent", check.nNonLeaf);
         3988  +  }
         3989  +
         3990  +  /* Finalize SQL statements used by the integrity-check */
         3991  +  sqlite3_finalize(check.pGetNode);
         3992  +  sqlite3_finalize(check.aCheckMapping[0]);
         3993  +  sqlite3_finalize(check.aCheckMapping[1]);
         3994  +
         3995  +  /* If one was opened, close the transaction */
         3996  +  if( bEnd ){
         3997  +    int rc = sqlite3_exec(db, "END", 0, 0, 0);
         3998  +    if( check.rc==SQLITE_OK ) check.rc = rc;
         3999  +  }
         4000  +  *pzReport = check.zReport;
         4001  +  return check.rc;
         4002  +}
         4003  +
         4004  +/*
         4005  +** Usage:
         4006  +**
         4007  +**   rtreecheck(<rtree-table>);
         4008  +**   rtreecheck(<database>, <rtree-table>);
         4009  +**
         4010  +** Invoking this SQL function runs an integrity-check on the named rtree
         4011  +** table. The integrity-check verifies the following:
         4012  +**
         4013  +**   1. For each cell in the r-tree structure (%_node table), that:
         4014  +**
         4015  +**       a) for each dimension, (coord1 <= coord2).
         4016  +**
         4017  +**       b) unless the cell is on the root node, that the cell is bounded
         4018  +**          by the parent cell on the parent node.
         4019  +**
         4020  +**       c) for leaf nodes, that there is an entry in the %_rowid 
         4021  +**          table corresponding to the cell's rowid value that 
         4022  +**          points to the correct node.
         4023  +**
         4024  +**       d) for cells on non-leaf nodes, that there is an entry in the 
         4025  +**          %_parent table mapping from the cell's child node to the
         4026  +**          node that it resides on.
         4027  +**
         4028  +**   2. That there are the same number of entries in the %_rowid table
         4029  +**      as there are leaf cells in the r-tree structure, and that there
         4030  +**      is a leaf cell that corresponds to each entry in the %_rowid table.
         4031  +**
         4032  +**   3. That there are the same number of entries in the %_parent table
         4033  +**      as there are non-leaf cells in the r-tree structure, and that 
         4034  +**      there is a non-leaf cell that corresponds to each entry in the 
         4035  +**      %_parent table.
         4036  +*/
         4037  +static void rtreecheck(
         4038  +  sqlite3_context *ctx, 
         4039  +  int nArg, 
         4040  +  sqlite3_value **apArg
         4041  +){
         4042  +  if( nArg!=1 && nArg!=2 ){
         4043  +    sqlite3_result_error(ctx, 
         4044  +        "wrong number of arguments to function rtreecheck()", -1
         4045  +    );
         4046  +  }else{
         4047  +    int rc;
         4048  +    char *zReport = 0;
         4049  +    const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
         4050  +    const char *zTab;
         4051  +    if( nArg==1 ){
         4052  +      zTab = zDb;
         4053  +      zDb = "main";
         4054  +    }else{
         4055  +      zTab = (const char*)sqlite3_value_text(apArg[1]);
         4056  +    }
         4057  +    rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
         4058  +    if( rc==SQLITE_OK ){
         4059  +      sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
         4060  +    }else{
         4061  +      sqlite3_result_error_code(ctx, rc);
         4062  +    }
         4063  +    sqlite3_free(zReport);
         4064  +  }
         4065  +}
         4066  +
  3610   4067   
  3611   4068   /*
  3612   4069   ** Register the r-tree module with database handle db. This creates the
  3613   4070   ** virtual table module "rtree" and the debugging/analysis scalar 
  3614   4071   ** function "rtreenode".
  3615   4072   */
  3616   4073   int sqlite3RtreeInit(sqlite3 *db){
................................................................................
  3617   4074     const int utf8 = SQLITE_UTF8;
  3618   4075     int rc;
  3619   4076   
  3620   4077     rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
  3621   4078     if( rc==SQLITE_OK ){
  3622   4079       rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
  3623   4080     }
         4081  +  if( rc==SQLITE_OK ){
         4082  +    rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
         4083  +  }
  3624   4084     if( rc==SQLITE_OK ){
  3625   4085   #ifdef SQLITE_RTREE_INT_ONLY
  3626   4086       void *c = (void *)RTREE_COORD_INT32;
  3627   4087   #else
  3628   4088       void *c = (void *)RTREE_COORD_REAL32;
  3629   4089   #endif
  3630   4090       rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);

Changes to ext/rtree/rtree1.test.

   515    515       set res(1) {1 {UNIQUE constraint failed: t1.idx}}
   516    516       set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
   517    517   
   518    518       do_catchsql_test $testname.1 $sql $res($error)
   519    519       do_test $testname.2 [list sql_uses_stmt db $sql] $uses
   520    520       do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
   521    521   
   522         -    do_test $testname.4 { rtree_check db t1 } 0
          522  +    do_rtree_integrity_test $testname.4 t1
   523    523       db close
   524    524     }
   525    525   }
   526    526   
   527    527   #-------------------------------------------------------------------------
   528    528   # Test that bug [d2889096e7bdeac6d] has been fixed.
   529    529   #

Changes to ext/rtree/rtree2.test.

    77     77         if {$rc != 1} {
    78     78           puts $t1
    79     79           puts $t2
    80     80         }
    81     81         set rc
    82     82       } {1}
    83     83     
    84         -    do_test rtree2-$module.$nDim.3 {
    85         -      rtree_check db t1
    86         -    } 0
           84  +    do_rtree_integrity_test rtree2-$module.$nDim.3 t1
    87     85     
    88     86       set OPS [list < > <= >= =]
    89     87       for {set ii 0} {$ii < $::NSELECT} {incr ii} {
    90     88         do_test rtree2-$module.$nDim.4.$ii.1 {
    91     89           set where [list]
    92     90           foreach look_three_dots! {. . .} {
    93     91             set colidx [expr int(rand()*($nDim*2+1))-1]
................................................................................
   129    127           set rc [expr {$t1 eq $t2}]
   130    128           if {$rc != 1} {
   131    129             puts $t1
   132    130             puts $t2
   133    131           }
   134    132           set rc
   135    133         } {1}
   136         -      do_test rtree2-$module.$nDim.5.$ii.2 {
   137         -        rtree_check db t1
   138         -      } {0}
          134  +      do_rtree_integrity_test rtree2-$module.$nDim.5.$ii.2 t1
   139    135       }
   140    136     
   141    137       do_test rtree2-$module.$nDim.6 {
   142    138         execsql {
   143    139           DROP TABLE t1;
   144    140           DROP TABLE t2;
   145    141         }
   146    142       } {}
   147    143     }
   148    144   }
   149    145   
   150    146   finish_test

Changes to ext/rtree/rtree4.test.

    11     11   #
    12     12   # Randomized test cases for the rtree extension.
    13     13   #
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   
    20     21   ifcapable !rtree {
    21     22     finish_test
    22     23     return
    23     24   }
    24     25   
................................................................................
   242    243       }
   243    244       set where "WHERE [join [scramble $where] { AND }]"
   244    245       do_test rtree4-$nDim.2.$i.8 {
   245    246         list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
   246    247       } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
   247    248     }
   248    249   
          250  +  do_rtree_integrity_test rtree4-$nDim.3 rx
   249    251   }
   250    252   
   251    253   finish_test

Changes to ext/rtree/rtree5.test.

    12     12   # The focus of this file is testing the r-tree extension when it is
    13     13   # configured to store values as 32 bit integers.
    14     14   #
    15     15   
    16     16   if {![info exists testdir]} {
    17     17     set testdir [file join [file dirname [info script]] .. .. test]
    18     18   } 
           19  +source [file join [file dirname [info script]] rtree_util.tcl]
    19     20   source $testdir/tester.tcl
    20     21   
    21     22   ifcapable !rtree {
    22     23     finish_test
    23     24     return
    24     25   }
    25     26   
................................................................................
    72     73   do_test rtree5-1.13 { 
    73     74     execsql { 
    74     75       SELECT * FROM t1 WHERE 
    75     76           x1=2147483643 AND x2=2147483647 AND 
    76     77           y1=-2147483648 AND y2=-2147483643
    77     78     }
    78     79   } {2 2147483643 2147483647 -2147483648 -2147483643}
           80  +do_rtree_integrity_test rtree5-1.14 t1
    79     81   
    80     82   finish_test

Changes to ext/rtree/rtree7.test.

    13     13   # database page-size is modified. At one point (3.6.22), this was causing
    14     14   # malfunctions.
    15     15   #
    16     16   
    17     17   if {![info exists testdir]} {
    18     18     set testdir [file join [file dirname [info script]] .. .. test]
    19     19   } 
           20  +source [file join [file dirname [info script]] rtree_util.tcl]
    20     21   source $testdir/tester.tcl
    21     22   
    22     23   ifcapable !rtree||!vacuum {
    23     24     finish_test
    24     25     return
    25     26   }
    26     27   
................................................................................
    62     63   do_test rtree7-1.5 {
    63     64     execsql_intout { 
    64     65       PRAGMA page_size = 512;
    65     66       VACUUM;
    66     67       SELECT sum(x1), sum(x2), sum(y1), sum(y2) FROM rt
    67     68     }
    68     69   } {51 102 153 204}
           70  +
           71  +do_rtree_integrity_test rtree7-1.6 rt
    69     72   
    70     73   finish_test

Changes to ext/rtree/rtree8.test.

    10     10   #***********************************************************************
    11     11   # 
    12     12   #
    13     13   
    14     14   if {![info exists testdir]} {
    15     15     set testdir [file join [file dirname [info script]] .. .. test]
    16     16   } 
           17  +source [file join [file dirname [info script]] rtree_util.tcl]
    17     18   source $testdir/tester.tcl
    18     19   ifcapable !rtree { finish_test ; return }
    19     20   
    20     21   #-------------------------------------------------------------------------
    21     22   # The following block of tests - rtree8-1.* - feature reading and writing
    22     23   # an r-tree table while there exist open cursors on it.
    23     24   #
................................................................................
    60     61   do_test rtree8-1.2.2 { nested_select 1 } {51}
    61     62   
    62     63   # This test runs many SELECT queries simultaneously against a large 
    63     64   # table, causing a collision in the hash-table used to store r-tree 
    64     65   # nodes internally.
    65     66   #
    66     67   populate_t1 1500
           68  +do_rtree_integrity_test rtree8-1.3.0 t1
    67     69   do_execsql_test rtree8-1.3.1 { SELECT max(nodeno) FROM t1_node } {164}
    68     70   do_test rtree8-1.3.2 {
    69     71     set rowids [execsql {SELECT min(rowid) FROM t1_rowid GROUP BY nodeno}]
    70     72     set stmt_list [list]
    71     73     foreach row $rowids {
    72     74       set stmt [sqlite3_prepare db "SELECT * FROM t1 WHERE id = $row" -1 tail]
    73     75       sqlite3_step $stmt
................................................................................
   154    156       execsql { INSERT INTO t2 VALUES($i, 100, 101) }
   155    157     }
   156    158     for {set i 100} {$i < 200} {incr i} {
   157    159       execsql { INSERT INTO t2 VALUES($i, 1000, 1001) }
   158    160     }
   159    161     execsql COMMIT
   160    162   } {}
   161         -do_test rtree8-5.3 {
          163  +do_rtree_integrity_test rtree8-5.3 t2
          164  +do_test rtree8-5.4 {
   162    165     execsql BEGIN
   163    166     for {set i 0} {$i < 200} {incr i} {
   164    167       execsql { DELETE FROM t2 WHERE id = $i }
   165    168     }
   166    169     execsql COMMIT
   167    170   } {}
          171  +do_rtree_integrity_test rtree8-5.5 t2
   168    172   
   169    173   
   170    174   finish_test

Changes to ext/rtree/rtree9.test.

    11     11   # This file contains tests for the r-tree module. Specifically, it tests
    12     12   # that custom r-tree queries (geometry callbacks) work.
    13     13   # 
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   ifcapable !rtree { finish_test ; return }
    20     21   ifcapable rtree_int_only { finish_test; return }
    21     22   
    22     23   register_cube_geom db
    23     24   
    24     25   do_execsql_test rtree9-1.1 {
................................................................................
    38     39   
    39     40   for {set i 0} {$i < 1000} {incr i} {
    40     41     set x [expr $i%10]
    41     42     set y [expr ($i/10)%10]
    42     43     set z [expr ($i/100)%10]
    43     44     execsql { INSERT INTO rt VALUES($i, $x, $x+1, $y, $y+1, $z, $z+1) }
    44     45   }
           46  +do_rtree_integrity_test rtree9-2.0 rt
    45     47   do_execsql_test rtree9-2.1 {
    46     48     SELECT id FROM rt WHERE id MATCH cube(2.5, 2.5, 2.5, 1, 1, 1) ORDER BY id;
    47     49   } {222 223 232 233 322 323 332 333}
    48     50   do_execsql_test rtree9-2.2 {
    49     51     SELECT id FROM rt WHERE id MATCH cube(5.5, 5.5, 5.5, 1, 1, 1) ORDER BY id;
    50     52   } {555 556 565 566 655 656 665 666}
    51     53   
    52     54   
    53         -do_execsql_test rtree9-3.1 {
           55  +do_execsql_test rtree9-3.0 {
    54     56     CREATE VIRTUAL TABLE rt32 USING rtree_i32(id, x1, x2, y1, y2, z1, z2);
    55     57   } {} 
    56     58   for {set i 0} {$i < 1000} {incr i} {
    57     59     set x [expr $i%10]
    58     60     set y [expr ($i/10)%10]
    59     61     set z [expr ($i/100)%10]
    60     62     execsql { INSERT INTO rt32 VALUES($i, $x, $x+1, $y, $y+1, $z, $z+1) }
    61     63   }
           64  +do_rtree_integrity_test rtree9-3.1 rt32
    62     65   do_execsql_test rtree9-3.2 {
    63     66     SELECT id FROM rt32 WHERE id MATCH cube(3, 3, 3, 1, 1, 1) ORDER BY id;
    64     67   } {222 223 224 232 233 234 242 243 244 322 323 324 332 333 334 342 343 344 422 423 424 432 433 434 442 443 444}
    65     68   do_execsql_test rtree9-3.3 {
    66     69     SELECT id FROM rt32 WHERE id MATCH cube(5.5, 5.5, 5.5, 1, 1, 1) ORDER BY id;
    67     70   } {555 556 565 566 655 656 665 666}
    68     71   
................................................................................
   117    120     SELECT id FROM rt2 WHERE id MATCH circle(0.0, 0.0, 2.0);
   118    121   } {1 2 3 4 13 14 15 16 17}
   119    122   
   120    123   do_execsql_test rtree9-5.3 {
   121    124     UPDATE rt2 SET xmin=xmin+5, ymin=ymin+5, xmax=xmax+5, ymax=ymax+5;
   122    125     SELECT id FROM rt2 WHERE id MATCH circle(5.0, 5.0, 2.0);
   123    126   } {1 2 3 4 13 14 15 16 17}
          127  +do_rtree_integrity_test rtree9-5.4 rt2
   124    128   
   125    129   finish_test

Changes to ext/rtree/rtreeA.test.

   104    104   do_corruption_tests rtreeA-1.1 {
   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  +do_execsql_test rtreeA-1.1.1 {
          112  +  SELECT rtreecheck('main', 't1')
          113  +} {{Node 1 missing from database
          114  +Wrong number of entries in %_rowid table - expected 0, actual 500
          115  +Wrong number of entries in %_parent table - expected 0, actual 23}}
          116  +
   111    117   do_execsql_test  rtreeA-1.2.0 { DROP TABLE t1_node } {}
   112    118   do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" {
   113    119     1   "SELECT * FROM t1"
   114    120     2   "SELECT * FROM t1 WHERE rowid=5"
   115    121     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   116    122     4   "SELECT * FROM t1 WHERE x1<10 AND x2>12"
   117    123   }
................................................................................
   152    158   do_test rtreeA-3.1.0.1 { set_tree_depth t1 } {1}
   153    159   do_test rtreeA-3.1.0.2 { set_tree_depth t1 3 } {3}
   154    160   do_corruption_tests rtreeA-3.1 {
   155    161     1   "SELECT * FROM t1"
   156    162     2   "SELECT * FROM t1 WHERE rowid=5"
   157    163     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   158    164   }
          165  +
          166  +do_execsql_test rtreeA-3.1.0.3 {
          167  +  SELECT rtreecheck('main', 't1')!="ok"
          168  +} {1}
   159    169   
   160    170   do_test rtreeA-3.2.0 { set_tree_depth t1 1000 } {1000}
   161    171   do_corruption_tests rtreeA-3.2 {
   162    172     1   "SELECT * FROM t1"
   163    173     2   "SELECT * FROM t1 WHERE rowid=5"
   164    174     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   165    175   }
................................................................................
   172    182   } {65535}
   173    183   do_corruption_tests rtreeA-3.3 {
   174    184     1   "SELECT * FROM t1"
   175    185     2   "SELECT * FROM t1 WHERE rowid=5"
   176    186     3   "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"
   177    187   }
   178    188   
          189  +do_execsql_test rtreeA-3.3.3.4 {
          190  +  SELECT rtreecheck('main', 't1')
          191  +} {{Rtree depth out of range (65535)
          192  +Wrong number of entries in %_rowid table - expected 0, actual 499
          193  +Wrong number of entries in %_parent table - expected 0, actual 23}}
          194  +
   179    195   #-------------------------------------------------------------------------
   180    196   # Set the "number of entries" field on some nodes incorrectly.
   181    197   #
   182    198   create_t1
   183    199   populate_t1
   184    200   do_test rtreeA-4.1.0 { 
   185    201     set_entry_count t1 1 4000
................................................................................
   198    214   create_t1
   199    215   populate_t1
   200    216   do_execsql_test rtreeA-5.1.0 { DELETE FROM t1_parent } {}
   201    217   do_corruption_tests rtreeA-5.1 {
   202    218     1   "DELETE FROM t1 WHERE rowid = 5"
   203    219     2   "DELETE FROM t1"
   204    220   }
          221  +
          222  +do_execsql_test rtreeA-5.2 {
          223  +  SELECT rtreecheck('main', 't1')!="ok"
          224  +} {1}
   205    225   
   206    226   #-------------------------------------------------------------------------
   207    227   # Add some bad entries to the %_parent table.
   208    228   #
   209    229   create_t1
   210    230   populate_t1
   211    231   do_execsql_test rtreeA-6.1.0 { 
   212    232     UPDATE t1_parent set parentnode = parentnode+1
   213    233   } {}
   214    234   do_corruption_tests rtreeA-6.1 {
   215    235     1   "DELETE FROM t1 WHERE rowid = 5"
   216    236     2   "UPDATE t1 SET x1=x1+1, x2=x2+1"
   217    237   }
          238  +
          239  +do_execsql_test rtreeA-6.2 {
          240  +  SELECT rtreecheck('main', 't1')!="ok"
          241  +} {1}
   218    242   
   219    243   #-------------------------------------------------------------------------
   220    244   # Truncated blobs in the _node table.
   221    245   #
   222    246   create_t1
   223    247   populate_t1
   224    248   sqlite3 db test.db
................................................................................
   229    253     SELECT * FROM t1 WHERE x1>0 AND x1<100 AND x2>0 AND x2<100;
   230    254   } {1 {undersize RTree blobs in "t1_node"}}
   231    255   do_test rtreeA-7.120 {
   232    256     sqlite3_extended_errcode db
   233    257   } {SQLITE_CORRUPT_VTAB}
   234    258   
   235    259   
   236         -
   237    260   finish_test
          261  +

Changes to ext/rtree/rtreeB.test.

    11     11   # Make sure the rtreenode() testing function can handle entries with
    12     12   # 64-bit rowids.
    13     13   # 
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   ifcapable !rtree { finish_test ; return }
    20     21   
    21     22   ifcapable rtree_int_only {
    22     23     do_test rtreeB-1.1-intonly {
    23     24       db eval {
    24     25         CREATE VIRTUAL TABLE t1 USING rtree(ii, x0, y0, x1, y1);
................................................................................
    39     40         INSERT INTO t1 VALUES(4294967296, 0.0, 0.0, 300.0, 300.0);
    40     41         INSERT INTO t1 VALUES(8589934592, 20.0, 20.0, 150.0, 150.0);
    41     42         INSERT INTO t1 VALUES(9223372036854775807, 150, 150, 400, 400);
    42     43         SELECT rtreenode(2, data) FROM t1_node;
    43     44       }
    44     45     } {{{1073741824 0 0 100 100} {2147483646 0 0 200 200} {4294967296 0 0 300 300} {8589934592 20 20 150 150} {9223372036854775807 150 150 400 400}}}
    45     46   }
           47  +
           48  +do_rtree_integrity_test rtreeB-1.2 t1
    46     49   
    47     50   finish_test

Changes to ext/rtree/rtreeC.test.

    11     11   # Make sure the rtreenode() testing function can handle entries with
    12     12   # 64-bit rowids.
    13     13   # 
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   ifcapable !rtree { finish_test ; return }
    20     21   set testprefix rtreeC
    21     22   
    22     23   do_execsql_test 1.0 {
    23     24     CREATE VIRTUAL TABLE r_tree USING rtree(id, min_x, max_x, min_y, max_y);
    24     25     CREATE TABLE t(x, y);
................................................................................
   176    177     INSERT INTO t1(x) SELECT x+64 FROM t1;  -- 128
   177    178     INSERT INTO t1(x) SELECT x+128 FROM t1; -- 256
   178    179     INSERT INTO t1(x) SELECT x+256 FROM t1; -- 512
   179    180     INSERT INTO t1(x) SELECT x+512 FROM t1; --1024
   180    181   
   181    182     INSERT INTO rt SELECT x, x, x+1 FROM t1 WHERE x<=5;
   182    183   }
          184  +do_rtree_integrity_test 5.1.1 rt
   183    185   
   184    186   # First test a query with no ANALYZE data at all. The outer loop is
   185    187   # real table "t1".
   186    188   #
   187    189   do_eqp_test 5.2 {
   188    190     SELECT * FROM t1, rt WHERE x==id;
   189    191   } {

Changes to ext/rtree/rtreeE.test.

    11     11   # This file contains tests for the r-tree module. Specifically, it tests
    12     12   # that new-style custom r-tree queries (geometry callbacks) work.
    13     13   # 
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   ifcapable !rtree { finish_test ; return }
    20     21   ifcapable rtree_int_only { finish_test; return }
    21     22   
    22     23   
    23     24   #-------------------------------------------------------------------------
    24     25   # Test the example 2d "circle" geometry callback.
    25     26   #
    26     27   register_circle_geom db
    27     28   
    28         -do_execsql_test rtreeE-1.1 {
           29  +do_execsql_test rtreeE-1.0.0 {
    29     30     PRAGMA page_size=512;
    30     31     CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1);
    31     32     
    32     33     /* A tight pattern of small boxes near 0,0 */
    33     34     WITH RECURSIVE
    34     35       x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
    35     36       y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
................................................................................
    43     44   
    44     45     /* A looser pattern of larger boxes near 0, 200 */
    45     46     WITH RECURSIVE
    46     47       x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
    47     48       y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
    48     49     INSERT INTO rt1 SELECT 200+x+5*y, x*7, x*7+15, y*7+200, y*7+215 FROM x, y;
    49     50   } {}
           51  +do_rtree_integrity_test rtreeE-1.0.1 rt1
    50     52   
    51     53   # Queries against each of the three clusters */
    52     54   do_execsql_test rtreeE-1.1 {
    53     55     SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 0.0, 50.0, 3) ORDER BY id;
    54     56   } {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
    55     57   do_execsql_test rtreeE-1.1x {
    56     58     SELECT id FROM rt1 WHERE id MATCH Qcircle('x:0 y:0 r:50.0 e:3') ORDER BY id;
................................................................................
   107    109       db eval {INSERT INTO t2 VALUES($id,$x0,$x1,$y0,$y1)}
   108    110     }
   109    111     db eval {
   110    112       INSERT INTO rt2 SELECT * FROM t2;
   111    113       COMMIT;
   112    114     }
   113    115   } {}
          116  +do_rtree_integrity_test rtreeE-2.1.1 rt2
   114    117   
   115    118   for {set i 1} {$i<=200} {incr i} {
   116    119     set dx [expr {int(rand()*100)}]
   117    120     set dy [expr {int(rand()*100)}]
   118    121     set x0 [expr {int(rand()*(10000 - $dx))}]
   119    122     set x1 [expr {$x0+$dx}]
   120    123     set y0 [expr {int(rand()*(10000 - $dy))}]

Changes to ext/rtree/rtreeF.test.

    24     24   #     END;
    25     25   #     DELETE FROM t2 WHERE y=1;
    26     26   # 
    27     27   
    28     28   if {![info exists testdir]} {
    29     29     set testdir [file join [file dirname [info script]] .. .. test]
    30     30   } 
           31  +source [file join [file dirname [info script]] rtree_util.tcl]
    31     32   source $testdir/tester.tcl
    32     33   ifcapable !rtree { finish_test ; return }
    33     34   
    34     35   do_execsql_test rtreeF-1.1 {
    35     36     CREATE TABLE t1(x);
    36     37     CREATE TABLE t2(y);
    37     38     CREATE VIRTUAL TABLE t3 USING rtree(a,b,c);
................................................................................
    73     74   do_execsql_test rtreeF-1.5 {
    74     75     DELETE FROM t2 WHERE y=2;
    75     76   
    76     77     SELECT a FROM t3 ORDER BY a;
    77     78     SELECT '|';
    78     79     SELECT y FROM t2 ORDER BY y;
    79     80   } {1 4 5 | 1 4}
           81  +
           82  +do_rtree_integrity_test rtreeF-1.6 t3
    80     83   
    81     84   finish_test

Changes to ext/rtree/rtreeG.test.

    11     11   # This file contains tests for the r-tree module.
    12     12   #
    13     13   # Verify that no invalid SQL is run during initialization
    14     14   
    15     15   if {![info exists testdir]} {
    16     16     set testdir [file join [file dirname [info script]] .. .. test]
    17     17   } 
           18  +source [file join [file dirname [info script]] rtree_util.tcl]
    18     19   source $testdir/tester.tcl
    19     20   ifcapable !rtree { finish_test ; return }
    20     21   
    21     22   db close
    22     23   sqlite3_shutdown
    23     24   test_sqlite3_log [list lappend ::log]
    24     25   set ::log [list]
................................................................................
    33     34     set ::log
    34     35   } {}
    35     36   
    36     37   do_execsql_test rtreeG-1.2 {
    37     38     INSERT INTO t1 VALUES(1,10,15,5,23),(2,20,21,5,23),(3,10,15,20,30);
    38     39     SELECT id from t1 WHERE x0>8 AND x1<16 AND y0>2 AND y1<25;
    39     40   } {1}
           41  +do_rtree_integrity_test rtreeG-1.2.integrity t1
    40     42   do_test rtreeG-1.2log {
    41     43     set ::log
    42     44   } {}
    43     45   
    44     46   db close
    45     47   sqlite3 db test.db
    46     48   do_execsql_test rtreeG-1.3 {

Changes to ext/rtree/rtree_util.tcl.

   186    186     set ret
   187    187   }
   188    188   
   189    189   proc rtree_treedump {db zTab} {
   190    190     set d [rtree_depth $db $zTab]
   191    191     rtree_nodetreedump $db $zTab "" $d 1
   192    192   }
          193  +
          194  +proc do_rtree_integrity_test {tn tbl} {
          195  +  uplevel [list do_execsql_test $tn "SELECT rtreecheck('$tbl')" ok]
          196  +}
          197  +

Added ext/rtree/rtreecheck.test.

            1  +# 2017 August 17
            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  +
           14  +
           15  +if {![info exists testdir]} {
           16  +  set testdir [file join [file dirname [info script]] .. .. test]
           17  +} 
           18  +source $testdir/tester.tcl
           19  +set testprefix rtreecheck
           20  +
           21  +ifcapable !rtree {
           22  +  finish_test
           23  +  return
           24  +}
           25  +
           26  +proc swap_int32 {blob i0 i1} {
           27  +  binary scan $blob I* L
           28  +
           29  +  set a [lindex $L $i0]
           30  +  set b [lindex $L $i1]
           31  +
           32  +  lset L $i0 $b
           33  +  lset L $i1 $a
           34  +
           35  +  binary format I* $L
           36  +}
           37  +
           38  +proc set_int32 {blob idx val} {
           39  +  binary scan $blob I* L
           40  +  lset L $idx $val
           41  +  binary format I* $L
           42  +}
           43  +
           44  +do_catchsql_test 1.0 {
           45  +  SELECT rtreecheck();
           46  +} {1 {wrong number of arguments to function rtreecheck()}}
           47  +
           48  +do_catchsql_test 1.1 {
           49  +  SELECT rtreecheck(0,0,0);
           50  +} {1 {wrong number of arguments to function rtreecheck()}}
           51  +
           52  +
           53  +proc setup_simple_db {{module rtree}} {
           54  +  reset_db
           55  +  db func swap_int32 swap_int32
           56  +  execsql "
           57  +    CREATE VIRTUAL TABLE r1 USING $module (id, x1, x2, y1, y2);
           58  +    INSERT INTO r1 VALUES(1,  5, 5, 5, 5);  --  3
           59  +    INSERT INTO r1 VALUES(2,  6, 6, 6, 6);  --  9
           60  +    INSERT INTO r1 VALUES(3,  7, 7, 7, 7);  -- 15
           61  +    INSERT INTO r1 VALUES(4,  8, 8, 8, 8);  -- 21
           62  +    INSERT INTO r1 VALUES(5,  9, 9, 9, 9);  -- 27
           63  +  "
           64  +}
           65  +
           66  +setup_simple_db
           67  +do_execsql_test 2.1 { 
           68  +  SELECT rtreecheck('r1') 
           69  +} {ok}
           70  +
           71  +do_execsql_test 2.2 {
           72  +  UPDATE r1_node SET data = swap_int32(data, 3, 9);
           73  +  UPDATE r1_node SET data = swap_int32(data, 23, 29);
           74  +}
           75  +
           76  +do_execsql_test 2.3 { 
           77  +  SELECT rtreecheck('r1') 
           78  +} {{Dimension 0 of cell 0 on node 1 is corrupt
           79  +Dimension 1 of cell 3 on node 1 is corrupt}}
           80  +
           81  +setup_simple_db
           82  +do_execsql_test 2.4 {
           83  +  DELETE FROM r1_rowid WHERE rowid = 3;
           84  +  SELECT rtreecheck('r1') 
           85  +} {{Mapping (3 -> 1) missing from %_rowid table
           86  +Wrong number of entries in %_rowid table - expected 5, actual 4}}
           87  +
           88  +setup_simple_db
           89  +do_execsql_test 2.5 {
           90  +  UPDATE r1_rowid SET nodeno=2 WHERE rowid=3;
           91  +  SELECT rtreecheck('r1') 
           92  +} {{Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
           93  +
           94  +reset_db
           95  +do_execsql_test 3.0 { 
           96  +  CREATE VIRTUAL TABLE r1 USING rtree_i32(id, x1, x2);
           97  +  INSERT INTO r1 VALUES(1, 0x7FFFFFFF*-1, 0x7FFFFFFF);
           98  +  INSERT INTO r1 VALUES(2, 0x7FFFFFFF*-1, 5);
           99  +  INSERT INTO r1 VALUES(3, -5, 5);
          100  +  INSERT INTO r1 VALUES(4, 5, 0x11111111);
          101  +  INSERT INTO r1 VALUES(5, 5, 0x00800000);
          102  +  INSERT INTO r1 VALUES(6, 5, 0x00008000);
          103  +  INSERT INTO r1 VALUES(7, 5, 0x00000080);
          104  +  INSERT INTO r1 VALUES(8, 5, 0x40490fdb);
          105  +  INSERT INTO r1 VALUES(9, 0x7f800000, 0x7f900000);
          106  +  SELECT rtreecheck('r1') 
          107  +} {ok}
          108  +
          109  +do_execsql_test 3.1 { 
          110  +  CREATE VIRTUAL TABLE r2 USING rtree_i32(id, x1, x2);
          111  +  INSERT INTO r2 VALUES(2, -1*(1<<31), -1*(1<<31)+5);
          112  +  SELECT rtreecheck('r2') 
          113  +} {ok}
          114  +
          115  +do_execsql_test 3.2 {
          116  +  BEGIN;
          117  +    UPDATE r2_node SET data = X'123456';
          118  +    SELECT rtreecheck('r2')!="ok";
          119  +} {1}
          120  +
          121  +do_execsql_test 3.3 {
          122  +  ROLLBACK;
          123  +  UPDATE r2_node SET data = X'00001234';
          124  +  SELECT rtreecheck('r2')!="ok";
          125  +} {1}
          126  +
          127  +do_execsql_test 4.0 {
          128  +  CREATE TABLE notanrtree(i);
          129  +  SELECT rtreecheck('notanrtree');
          130  +} {{Schema corrupt or not an rtree}}
          131  +
          132  +#-------------------------------------------------------------------------
          133  +#
          134  +reset_db
          135  +db func set_int32 set_int32
          136  +do_execsql_test 5.0 {
          137  +  CREATE VIRTUAL TABLE r3 USING rtree_i32(id, x1, x2, y1, y2);
          138  +  WITH x(i) AS (
          139  +    SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000
          140  +  )
          141  +  INSERT INTO r3 SELECT i, i, i, i, i FROM x;
          142  +}
          143  +do_execsql_test 5.1 {
          144  +  BEGIN;
          145  +    UPDATE r3_node SET data = set_int32(data, 3, 5000);
          146  +    UPDATE r3_node SET data = set_int32(data, 4, 5000);
          147  +    SELECT rtreecheck('r3')=='ok'
          148  +} 0
          149  +do_execsql_test 5.2 {
          150  +  ROLLBACK;
          151  +  BEGIN;
          152  +    UPDATE r3_node SET data = set_int32(data, 3, 0);
          153  +    UPDATE r3_node SET data = set_int32(data, 4, 0);
          154  +    SELECT rtreecheck('r3')=='ok'
          155  +} 0
          156  +
          157  +finish_test
          158  +

Changes to ext/session/sqlite3session.h.

   384    384   ** Changes within a patchset are ordered in the same way as for changesets
   385    385   ** generated by the sqlite3session_changeset() function (i.e. all changes for
   386    386   ** a single table are grouped together, tables appear in the order in which
   387    387   ** they were attached to the session object).
   388    388   */
   389    389   int sqlite3session_patchset(
   390    390     sqlite3_session *pSession,      /* Session object */
   391         -  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
   392         -  void **ppPatchset               /* OUT: Buffer containing changeset */
          391  +  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
          392  +  void **ppPatchset               /* OUT: Buffer containing patchset */
   393    393   );
   394    394   
   395    395   /*
   396    396   ** CAPI3REF: Test if a changeset has recorded any changes.
   397    397   **
   398    398   ** Return non-zero if no changes to attached tables have been recorded by 
   399    399   ** the session object passed as the first argument. Otherwise, if one or 
................................................................................
  1152   1152   ** CAPI3REF: Streaming Versions of API functions.
  1153   1153   **
  1154   1154   ** The six streaming API xxx_strm() functions serve similar purposes to the 
  1155   1155   ** corresponding non-streaming API functions:
  1156   1156   **
  1157   1157   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
  1158   1158   **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
  1159         -**   <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 
  1160         -**   <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 
  1161         -**   <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 
  1162         -**   <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 
  1163         -**   <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 
  1164         -**   <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 
         1159  +**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
         1160  +**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
         1161  +**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
         1162  +**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
         1163  +**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
         1164  +**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
  1165   1165   ** </table>
  1166   1166   **
  1167   1167   ** Non-streaming functions that accept changesets (or patchsets) as input
  1168   1168   ** require that the entire changeset be stored in a single buffer in memory. 
  1169   1169   ** Similarly, those that return a changeset or patchset do so by returning 
  1170   1170   ** a pointer to a single large buffer allocated using sqlite3_malloc(). 
  1171   1171   ** Normally this is convenient. However, if an application running in a 

Changes to main.mk.

    51     51   THREADLIB += $(LIBS)
    52     52   
    53     53   # Object files for the SQLite library.
    54     54   #
    55     55   LIBOBJ+= vdbe.o parse.o \
    56     56            alter.o analyze.o attach.o auth.o \
    57     57            backup.o bitvec.o btmutex.o btree.o build.o \
    58         -         callback.o complete.o ctime.o date.o dbstat.o delete.o expr.o \
           58  +         callback.o complete.o ctime.o \
           59  +         date.o dbpage.o dbstat.o delete.o expr.o \
    59     60   	 fault.o fkey.o \
    60     61            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    61     62            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    62     63            fts3_tokenize_vtab.o \
    63     64   	 fts3_unicode.o fts3_unicode2.o \
    64     65            fts3_write.o fts5.o func.o global.o hash.o \
    65     66            icu.o insert.o json1.o legacy.o loadext.o \
................................................................................
    92     93     $(TOP)/src/btree.h \
    93     94     $(TOP)/src/btreeInt.h \
    94     95     $(TOP)/src/build.c \
    95     96     $(TOP)/src/callback.c \
    96     97     $(TOP)/src/complete.c \
    97     98     $(TOP)/src/ctime.c \
    98     99     $(TOP)/src/date.c \
          100  +  $(TOP)/src/dbpage.c \
    99    101     $(TOP)/src/dbstat.c \
   100    102     $(TOP)/src/delete.c \
   101    103     $(TOP)/src/expr.c \
   102    104     $(TOP)/src/fault.c \
   103    105     $(TOP)/src/fkey.c \
   104    106     $(TOP)/src/func.c \
   105    107     $(TOP)/src/global.c \
................................................................................
   142    144     $(TOP)/src/prepare.c \
   143    145     $(TOP)/src/printf.c \
   144    146     $(TOP)/src/random.c \
   145    147     $(TOP)/src/resolve.c \
   146    148     $(TOP)/src/rowset.c \
   147    149     $(TOP)/src/select.c \
   148    150     $(TOP)/src/status.c \
   149         -  $(TOP)/src/shell.c \
          151  +  $(TOP)/src/shell.c.in \
   150    152     $(TOP)/src/sqlite.h.in \
   151    153     $(TOP)/src/sqlite3ext.h \
   152    154     $(TOP)/src/sqliteInt.h \
   153    155     $(TOP)/src/sqliteLimit.h \
   154    156     $(TOP)/src/table.c \
   155    157     $(TOP)/src/tclsqlite.c \
   156    158     $(TOP)/src/threads.c \
................................................................................
   266    268   #
   267    269   SRC += \
   268    270     keywordhash.h \
   269    271     opcodes.c \
   270    272     opcodes.h \
   271    273     parse.c \
   272    274     parse.h \
          275  +  shell.c \
   273    276     sqlite3.h
   274    277   
   275    278   
   276    279   # Source code to the test files.
   277    280   #
   278    281   TESTSRC = \
   279    282     $(TOP)/ext/fts3/fts3_term.c \
................................................................................
   301    304     $(TOP)/src/test_fs.c \
   302    305     $(TOP)/src/test_func.c \
   303    306     $(TOP)/src/test_hexio.c \
   304    307     $(TOP)/src/test_init.c \
   305    308     $(TOP)/src/test_intarray.c \
   306    309     $(TOP)/src/test_journal.c \
   307    310     $(TOP)/src/test_malloc.c \
          311  +  $(TOP)/src/test_md5.c \
   308    312     $(TOP)/src/test_multiplex.c \
   309    313     $(TOP)/src/test_mutex.c \
   310    314     $(TOP)/src/test_onefile.c \
   311    315     $(TOP)/src/test_osinst.c \
   312    316     $(TOP)/src/test_pcache.c \
   313    317     $(TOP)/src/test_quota.c \
   314    318     $(TOP)/src/test_rtree.c \
   315    319     $(TOP)/src/test_schema.c \
   316    320     $(TOP)/src/test_server.c \
   317    321     $(TOP)/src/test_sqllog.c \
   318    322     $(TOP)/src/test_superlock.c \
   319    323     $(TOP)/src/test_syscall.c \
          324  +  $(TOP)/src/test_tclsh.c \
   320    325     $(TOP)/src/test_tclvar.c \
   321    326     $(TOP)/src/test_thread.c \
   322    327     $(TOP)/src/test_vfs.c \
   323    328     $(TOP)/src/test_windirent.c \
   324    329     $(TOP)/src/test_wsd.c
   325    330   
   326    331   # Extensions to be statically loaded.
................................................................................
   355    360   
   356    361   TESTSRC2 = \
   357    362     $(TOP)/src/attach.c \
   358    363     $(TOP)/src/backup.c \
   359    364     $(TOP)/src/btree.c \
   360    365     $(TOP)/src/build.c \
   361    366     $(TOP)/src/date.c \
          367  +  $(TOP)/src/dbpage.c \
   362    368     $(TOP)/src/dbstat.c \
   363    369     $(TOP)/src/expr.c \
   364    370     $(TOP)/src/func.c \
   365    371     $(TOP)/src/insert.c \
   366    372     $(TOP)/src/wal.c \
   367    373     $(TOP)/src/main.c \
   368    374     $(TOP)/src/mem5.c \
................................................................................
   455    461   
   456    462   # executables needed for testing
   457    463   #
   458    464   TESTPROGS = \
   459    465     testfixture$(EXE) \
   460    466     sqlite3$(EXE) \
   461    467     sqlite3_analyzer$(EXE) \
          468  +  sqlite3_checker$(EXE) \
   462    469     sqldiff$(EXE) \
   463    470     dbhash$(EXE)
   464    471   
   465    472   # Databases containing fuzzer test cases
   466    473   #
   467    474   FUZZDATA = \
   468    475     $(TOP)/test/fuzzdata1.db \
................................................................................
   477    484   
   478    485   # Extra compiler options for various shell tools
   479    486   #
   480    487   SHELL_OPT += -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5
   481    488   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   482    489   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   483    490   SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB
          491  +SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
          492  +SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   484    493   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   485    494   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
   486    495   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   487    496   DBFUZZ_OPT =
   488    497   KV_OPT = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ
   489    498   ST_OPT = -DSQLITE_THREADSAFE=0
   490    499   
................................................................................
   493    502   #
   494    503   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   495    504   
   496    505   libsqlite3.a:	$(LIBOBJ)
   497    506   	$(AR) libsqlite3.a $(LIBOBJ)
   498    507   	$(RANLIB) libsqlite3.a
   499    508   
   500         -sqlite3$(EXE):	$(TOP)/src/shell.c libsqlite3.a sqlite3.h
          509  +sqlite3$(EXE):	shell.c libsqlite3.a sqlite3.h
   501    510   	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) $(SHELL_OPT) \
   502         -		$(TOP)/src/shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
          511  +		shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
   503    512   
   504    513   sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
   505    514   	$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
   506    515   		$(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) $(THREADLIB)
   507    516   
   508    517   dbhash$(EXE):	$(TOP)/tool/dbhash.c sqlite3.c sqlite3.h
   509    518   	$(TCCX) -o dbhash$(EXE) -DSQLITE_THREADSAFE=0 \
................................................................................
   571    580   	tclsh $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   572    581   	mv vdbe.new tsrc/vdbe.c
   573    582   	cp fts5.c fts5.h tsrc
   574    583   	touch target_source
   575    584   
   576    585   sqlite3.c:	target_source $(TOP)/tool/mksqlite3c.tcl
   577    586   	tclsh $(TOP)/tool/mksqlite3c.tcl
   578         -	cp tsrc/shell.c tsrc/sqlite3ext.h .
          587  +	cp tsrc/sqlite3ext.h .
   579    588   	cp $(TOP)/ext/session/sqlite3session.h .
   580    589   	echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
   581    590   	cat sqlite3.c >>tclsqlite3.c
   582    591   	echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c
   583    592   	cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c
   584    593   
   585    594   sqlite3ext.h:	target_source
................................................................................
   654    663   
   655    664   sqlite3.h:	$(TOP)/src/sqlite.h.in $(TOP)/manifest mksourceid $(TOP)/VERSION $(TOP)/ext/rtree/sqlite3rtree.h
   656    665   	tclsh $(TOP)/tool/mksqlite3h.tcl $(TOP) >sqlite3.h
   657    666   
   658    667   keywordhash.h:	$(TOP)/tool/mkkeywordhash.c
   659    668   	$(BCC) -o mkkeywordhash $(OPTS) $(TOP)/tool/mkkeywordhash.c
   660    669   	./mkkeywordhash >keywordhash.h
          670  +
          671  +# Source files that go into making shell.c
          672  +SHELL_SRC = \
          673  +	$(TOP)/src/shell.c.in \
          674  +	$(TOP)/ext/misc/shathree.c \
          675  +	$(TOP)/ext/misc/fileio.c \
          676  +	$(TOP)/ext/misc/completion.c
          677  +
          678  +shell.c:	$(SHELL_SRC) $(TOP)/tool/mkshellc.tcl
          679  +	tclsh $(TOP)/tool/mkshellc.tcl >shell.c
   661    680   
   662    681   
   663    682   
   664    683   # Rules to build the extension objects.
   665    684   #
   666    685   icu.o:	$(TOP)/ext/icu/icu.c $(HDR) $(EXTHDR)
   667    686   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/icu/icu.c
................................................................................
   756    775   
   757    776   sqlite3rbu.o:	$(TOP)/ext/rbu/sqlite3rbu.c $(HDR) $(EXTHDR)
   758    777   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/rbu/sqlite3rbu.c
   759    778   
   760    779   # Rules for building test programs and for running tests
   761    780   #
   762    781   tclsqlite3:	$(TOP)/src/tclsqlite.c libsqlite3.a
   763         -	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \
          782  +	$(TCCX) $(TCL_FLAGS) -DTCLSH -o tclsqlite3 \
   764    783   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)
   765    784   
   766         -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   767         -	echo "#define TCLSH 2" > $@
   768         -	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
   769         -	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
   770         -	echo "static const char *tclsh_main_loop(void){" >> $@
   771         -	echo "static const char *zMainloop = " >> $@
   772         -	tclsh $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
   773         -	echo "; return zMainloop; }" >> $@
          785  +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl $(TOP)/tool/sqlite3_analyzer.c.in $(TOP)/tool/mkccode.tcl
          786  +	tclsh $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in >sqlite3_analyzer.c
   774    787   
   775    788   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   776    789   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
   777    790   
          791  +CHECKER_DEPS =\
          792  +  $(TOP)/tool/mkccode.tcl \
          793  +  sqlite3.c \
          794  +  $(TOP)/src/tclsqlite.c \
          795  +  $(TOP)/ext/repair/sqlite3_checker.tcl \
          796  +  $(TOP)/ext/repair/checkindex.c \
          797  +  $(TOP)/ext/repair/checkfreelist.c \
          798  +  $(TOP)/ext/misc/btreeinfo.c \
          799  +  $(TOP)/ext/repair/sqlite3_checker.c.in
          800  +
          801  +sqlite3_checker.c:	$(CHECKER_DEPS)
          802  +	tclsh $(TOP)/tool/mkccode.tcl $(TOP)/ext/repair/sqlite3_checker.c.in >$@
          803  +
          804  +sqlite3_checker$(TEXE):	sqlite3_checker.c
          805  +	$(TCCX) $(TCL_FLAGS) sqlite3_checker.c -o $@ $(LIBTCL) $(THREADLIB)
          806  +
   778    807   dbdump$(EXE):	$(TOP)/ext/misc/dbdump.c sqlite3.o
   779    808   	$(TCCX) -DDBDUMP_STANDALONE -o dbdump$(EXE) \
   780    809               $(TOP)/ext/misc/dbdump.c sqlite3.o $(THREADLIB)
   781    810   
   782    811   # Rules to build the 'testfixture' application.
   783    812   #
   784    813   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   785    814   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
   786    815   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
   787    816   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
   788    817   TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_STMTVTAB
          818  +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_DBPAGE_VTAB
          819  +TESTFIXTURE_FLAGS += -DTCLSH_INIT_PROC=sqlite3TestInit
   789    820   
   790    821   testfixture$(EXE): $(TESTSRC2) libsqlite3.a $(TESTSRC) $(TOP)/src/tclsqlite.c
   791         -	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
          822  +	$(TCCX) $(TCL_FLAGS) $(TESTFIXTURE_FLAGS)                            \
   792    823   		$(TESTSRC) $(TESTSRC2) $(TOP)/src/tclsqlite.c                \
   793    824   		-o testfixture$(EXE) $(LIBTCL) libsqlite3.a $(THREADLIB)
   794    825   
   795    826   amalgamation-testfixture$(EXE): sqlite3.c $(TESTSRC) $(TOP)/src/tclsqlite.c  \
   796    827   				$(TOP)/ext/session/test_session.c
   797         -	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
          828  +	$(TCCX) $(TCL_FLAGS) $(TESTFIXTURE_FLAGS)                            \
   798    829   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c                  \
   799    830   		$(TOP)/ext/session/test_session.c                            \
   800    831   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   801    832   
   802    833   fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c
   803         -	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
          834  +	$(TCCX) $(TCL_FLAGS) $(TESTFIXTURE_FLAGS)                            \
   804    835   	-DSQLITE_ENABLE_FTS3=1                                               \
   805    836   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c       \
   806    837   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   807    838   
   808    839   fulltest:	$(TESTPROGS) fuzztest
   809    840   	./testfixture$(EXE) $(TOP)/test/all.test $(TESTOPTS)
   810    841   
................................................................................
   894    925   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showjournal$(EXE) \
   895    926   		$(TOP)/tool/showjournal.c sqlite3.o $(THREADLIB)
   896    927   
   897    928   showwal$(EXE):	$(TOP)/tool/showwal.c sqlite3.o
   898    929   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showwal$(EXE) \
   899    930   		$(TOP)/tool/showwal.c sqlite3.o $(THREADLIB)
   900    931   
          932  +showshm$(EXE):	$(TOP)/tool/showshm.c
          933  +	$(TCC) -o showshm$(EXE) $(TOP)/tool/showshm.c
          934  +
   901    935   changeset$(EXE):	$(TOP)/ext/session/changeset.c sqlite3.o
   902    936   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o changeset$(EXE) \
   903    937   		$(TOP)/ext/session/changeset.c sqlite3.o $(THREADLIB)
   904    938   
   905    939   fts3view$(EXE):	$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o
   906    940   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \
   907    941   		$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB)

Changes to src/btree.c.

  1598   1598   
  1599   1599         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
  1600   1600           u8 *pEnd = &data[cellOffset + nCell*2];
  1601   1601           u8 *pAddr;
  1602   1602           int sz2 = 0;
  1603   1603           int sz = get2byte(&data[iFree+2]);
  1604   1604           int top = get2byte(&data[hdr+5]);
         1605  +        if( top>=iFree ){
         1606  +          return SQLITE_CORRUPT_PGNO(pPage->pgno);
         1607  +        }
  1605   1608           if( iFree2 ){
  1606   1609             assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
  1607   1610             sz2 = get2byte(&data[iFree2+2]);
  1608   1611             assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
  1609   1612             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
  1610   1613             sz += sz2;
  1611   1614           }
................................................................................
  2236   2239   ** error, return ((unsigned int)-1).
  2237   2240   */
  2238   2241   static Pgno btreePagecount(BtShared *pBt){
  2239   2242     return pBt->nPage;
  2240   2243   }
  2241   2244   u32 sqlite3BtreeLastPage(Btree *p){
  2242   2245     assert( sqlite3BtreeHoldsMutex(p) );
  2243         -  assert( ((p->pBt->nPage)&0x8000000)==0 );
         2246  +  assert( ((p->pBt->nPage)&0x80000000)==0 );
  2244   2247     return btreePagecount(p->pBt);
  2245   2248   }
  2246   2249   
  2247   2250   /*
  2248   2251   ** Get a page from the pager and initialize it.
  2249   2252   **
  2250   2253   ** If pCur!=0 then the page is being fetched as part of a moveToChild()
................................................................................
  3129   3132   
  3130   3133   /*
  3131   3134   ** If the user has not set the safety-level for this database connection
  3132   3135   ** using "PRAGMA synchronous", and if the safety-level is not already
  3133   3136   ** set to the value passed to this function as the second parameter,
  3134   3137   ** set it so.
  3135   3138   */
  3136         -#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS
         3139  +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
         3140  +    && !defined(SQLITE_OMIT_WAL)
  3137   3141   static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
  3138   3142     sqlite3 *db;
  3139   3143     Db *pDb;
  3140   3144     if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
  3141   3145       while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
  3142   3146       if( pDb->bSyncSet==0 
  3143   3147        && pDb->safety_level!=safety_level 
................................................................................
  5223   5227   ** page of the database.  The data might change or move the next time
  5224   5228   ** any btree routine is called.
  5225   5229   */
  5226   5230   static const void *fetchPayload(
  5227   5231     BtCursor *pCur,      /* Cursor pointing to entry to read from */
  5228   5232     u32 *pAmt            /* Write the number of available bytes here */
  5229   5233   ){
  5230         -  u32 amt;
         5234  +  int amt;
  5231   5235     assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
  5232   5236     assert( pCur->eState==CURSOR_VALID );
  5233   5237     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  5234   5238     assert( cursorOwnsBtShared(pCur) );
  5235   5239     assert( pCur->ix<pCur->pPage->nCell );
  5236   5240     assert( pCur->info.nSize>0 );
  5237   5241     assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
  5238   5242     assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
  5239         -  amt = (int)(pCur->pPage->aDataEnd - pCur->info.pPayload);
  5240         -  if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
  5241         -  *pAmt = amt;
         5243  +  amt = pCur->info.nLocal;
         5244  +  if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
         5245  +    /* There is too little space on the page for the expected amount
         5246  +    ** of local content. Database must be corrupt. */
         5247  +    assert( CORRUPT_DB );
         5248  +    amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
         5249  +  }
         5250  +  *pAmt = (u32)amt;
  5242   5251     return (void*)pCur->info.pPayload;
  5243   5252   }
  5244   5253   
  5245   5254   
  5246   5255   /*
  5247   5256   ** For the entry that cursor pCur is point to, return as
  5248   5257   ** many bytes of the key or data as are available on the local
................................................................................
  7719   7728     ** Allocate space for memory structures
  7720   7729     */
  7721   7730     szScratch =
  7722   7731          nMaxCells*sizeof(u8*)                       /* b.apCell */
  7723   7732        + nMaxCells*sizeof(u16)                       /* b.szCell */
  7724   7733        + pBt->pageSize;                              /* aSpace1 */
  7725   7734   
  7726         -  /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
  7727         -  ** that is more than 6 times the database page size. */
  7728   7735     assert( szScratch<=6*(int)pBt->pageSize );
  7729   7736     b.apCell = sqlite3StackAllocRaw(0, szScratch );
  7730   7737     if( b.apCell==0 ){
  7731   7738       rc = SQLITE_NOMEM_BKPT;
  7732   7739       goto balance_cleanup;
  7733   7740     }
  7734   7741     b.szCell = (u16*)&b.apCell[nMaxCells];

Changes to src/build.c.

  1061   1061     Table *p;
  1062   1062     int i;
  1063   1063     char *z;
  1064   1064     char *zType;
  1065   1065     Column *pCol;
  1066   1066     sqlite3 *db = pParse->db;
  1067   1067     if( (p = pParse->pNewTable)==0 ) return;
  1068         -#if SQLITE_MAX_COLUMN
  1069   1068     if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1070   1069       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
  1071   1070       return;
  1072   1071     }
  1073         -#endif
  1074   1072     z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
  1075   1073     if( z==0 ) return;
  1076   1074     memcpy(z, pName->z, pName->n);
  1077   1075     z[pName->n] = 0;
  1078   1076     sqlite3Dequote(z);
  1079   1077     for(i=0; i<p->nCol; i++){
  1080   1078       if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
................................................................................
  2364   2362   /*
  2365   2363   ** Write VDBE code to erase table pTab and all associated indices on disk.
  2366   2364   ** Code to update the sqlite_master tables and internal schema definitions
  2367   2365   ** in case a root-page belonging to another table is moved by the btree layer
  2368   2366   ** is also added (this can happen with an auto-vacuum database).
  2369   2367   */
  2370   2368   static void destroyTable(Parse *pParse, Table *pTab){
  2371         -#ifdef SQLITE_OMIT_AUTOVACUUM
  2372         -  Index *pIdx;
  2373         -  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  2374         -  destroyRootPage(pParse, pTab->tnum, iDb);
  2375         -  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  2376         -    destroyRootPage(pParse, pIdx->tnum, iDb);
  2377         -  }
  2378         -#else
  2379   2369     /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
  2380   2370     ** is not defined), then it is important to call OP_Destroy on the
  2381   2371     ** table and index root-pages in order, starting with the numerically 
  2382   2372     ** largest root-page number. This guarantees that none of the root-pages
  2383   2373     ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
  2384   2374     ** following were coded:
  2385   2375     **
................................................................................
  2414   2404       }else{
  2415   2405         int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  2416   2406         assert( iDb>=0 && iDb<pParse->db->nDb );
  2417   2407         destroyRootPage(pParse, iLargest, iDb);
  2418   2408         iDestroyed = iLargest;
  2419   2409       }
  2420   2410     }
  2421         -#endif
  2422   2411   }
  2423   2412   
  2424   2413   /*
  2425   2414   ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
  2426   2415   ** after a DROP INDEX or DROP TABLE command.
  2427   2416   */
  2428   2417   static void sqlite3ClearStatTables(
................................................................................
  3861   3850     if( !p && (pOn || pUsing) ){
  3862   3851       sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
  3863   3852         (pOn ? "ON" : "USING")
  3864   3853       );
  3865   3854       goto append_from_error;
  3866   3855     }
  3867   3856     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
  3868         -  if( p==0 || NEVER(p->nSrc==0) ){
         3857  +  if( p==0 ){
  3869   3858       goto append_from_error;
  3870   3859     }
         3860  +  assert( p->nSrc>0 );
  3871   3861     pItem = &p->a[p->nSrc-1];
  3872   3862     assert( pAlias!=0 );
  3873   3863     if( pAlias->n ){
  3874   3864       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
  3875   3865     }
  3876   3866     pItem->pSelect = pSubquery;
  3877   3867     pItem->pOn = pOn;

Added src/dbpage.c.

            1  +/*
            2  +** 2017-10-11
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains an implementation of the "sqlite_dbpage" virtual table.
           14  +**
           15  +** The sqlite_dbpage virtual table is used to read or write whole raw
           16  +** pages of the database file.  The pager interface is used so that 
           17  +** uncommitted changes and changes recorded in the WAL file are correctly
           18  +** retrieved.
           19  +**
           20  +** Usage example:
           21  +**
           22  +**    SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
           23  +**
           24  +** This is an eponymous virtual table so it does not need to be created before
           25  +** use.  The optional argument to the sqlite_dbpage() table name is the
           26  +** schema for the database file that is to be read.  The default schema is
           27  +** "main".
           28  +**
           29  +** The data field of sqlite_dbpage table can be updated.  The new
           30  +** value must be a BLOB which is the correct page size, otherwise the
           31  +** update fails.  Rows may not be deleted or inserted.
           32  +*/
           33  +
           34  +#include "sqliteInt.h"   /* Requires access to internal data structures */
           35  +#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
           36  +    && !defined(SQLITE_OMIT_VIRTUALTABLE)
           37  +
           38  +typedef struct DbpageTable DbpageTable;
           39  +typedef struct DbpageCursor DbpageCursor;
           40  +
           41  +struct DbpageCursor {
           42  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
           43  +  int pgno;                       /* Current page number */
           44  +  int mxPgno;                     /* Last page to visit on this scan */
           45  +  Pager *pPager;                  /* Pager being read/written */
           46  +  DbPage *pPage1;                 /* Page 1 of the database */
           47  +  int iDb;                        /* Index of database to analyze */
           48  +  int szPage;                     /* Size of each page in bytes */
           49  +};
           50  +
           51  +struct DbpageTable {
           52  +  sqlite3_vtab base;              /* Base class.  Must be first */
           53  +  sqlite3 *db;                    /* The database */
           54  +};
           55  +
           56  +/* Columns */
           57  +#define DBPAGE_COLUMN_PGNO    0
           58  +#define DBPAGE_COLUMN_DATA    1
           59  +#define DBPAGE_COLUMN_SCHEMA  2
           60  +
           61  +
           62  +
           63  +/*
           64  +** Connect to or create a dbpagevfs virtual table.
           65  +*/
           66  +static int dbpageConnect(
           67  +  sqlite3 *db,
           68  +  void *pAux,
           69  +  int argc, const char *const*argv,
           70  +  sqlite3_vtab **ppVtab,
           71  +  char **pzErr
           72  +){
           73  +  DbpageTable *pTab = 0;
           74  +  int rc = SQLITE_OK;
           75  +
           76  +  rc = sqlite3_declare_vtab(db, 
           77  +          "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
           78  +  if( rc==SQLITE_OK ){
           79  +    pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
           80  +    if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
           81  +  }
           82  +
           83  +  assert( rc==SQLITE_OK || pTab==0 );
           84  +  if( rc==SQLITE_OK ){
           85  +    memset(pTab, 0, sizeof(DbpageTable));
           86  +    pTab->db = db;
           87  +  }
           88  +
           89  +  *ppVtab = (sqlite3_vtab*)pTab;
           90  +  return rc;
           91  +}
           92  +
           93  +/*
           94  +** Disconnect from or destroy a dbpagevfs virtual table.
           95  +*/
           96  +static int dbpageDisconnect(sqlite3_vtab *pVtab){
           97  +  sqlite3_free(pVtab);
           98  +  return SQLITE_OK;
           99  +}
          100  +
          101  +/*
          102  +** idxNum:
          103  +**
          104  +**     0     schema=main, full table scan
          105  +**     1     schema=main, pgno=?1
          106  +**     2     schema=?1, full table scan
          107  +**     3     schema=?1, pgno=?2
          108  +*/
          109  +static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
          110  +  int i;
          111  +  int iPlan = 0;
          112  +
          113  +  /* If there is a schema= constraint, it must be honored.  Report a
          114  +  ** ridiculously large estimated cost if the schema= constraint is
          115  +  ** unavailable
          116  +  */
          117  +  for(i=0; i<pIdxInfo->nConstraint; i++){
          118  +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
          119  +    if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
          120  +    if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
          121  +    if( !p->usable ){
          122  +      /* No solution.  Use the default SQLITE_BIG_DBL cost */
          123  +      pIdxInfo->estimatedRows = 0x7fffffff;
          124  +      return SQLITE_OK;
          125  +    }
          126  +    iPlan = 2;
          127  +    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
          128  +    pIdxInfo->aConstraintUsage[i].omit = 1;
          129  +    break;
          130  +  }
          131  +
          132  +  /* If we reach this point, it means that either there is no schema=
          133  +  ** constraint (in which case we use the "main" schema) or else the
          134  +  ** schema constraint was accepted.  Lower the estimated cost accordingly
          135  +  */
          136  +  pIdxInfo->estimatedCost = 1.0e6;
          137  +
          138  +  /* Check for constraints against pgno */
          139  +  for(i=0; i<pIdxInfo->nConstraint; i++){
          140  +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
          141  +    if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
          142  +      pIdxInfo->estimatedRows = 1;
          143  +      pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
          144  +      pIdxInfo->estimatedCost = 1.0;
          145  +      pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
          146  +      pIdxInfo->aConstraintUsage[i].omit = 1;
          147  +      iPlan |= 1;
          148  +      break;
          149  +    }
          150  +  }
          151  +  pIdxInfo->idxNum = iPlan;
          152  +
          153  +  if( pIdxInfo->nOrderBy>=1
          154  +   && pIdxInfo->aOrderBy[0].iColumn<=0
          155  +   && pIdxInfo->aOrderBy[0].desc==0
          156  +  ){
          157  +    pIdxInfo->orderByConsumed = 1;
          158  +  }
          159  +  return SQLITE_OK;
          160  +}
          161  +
          162  +/*
          163  +** Open a new dbpagevfs cursor.
          164  +*/
          165  +static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
          166  +  DbpageCursor *pCsr;
          167  +
          168  +  pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
          169  +  if( pCsr==0 ){
          170  +    return SQLITE_NOMEM_BKPT;
          171  +  }else{
          172  +    memset(pCsr, 0, sizeof(DbpageCursor));
          173  +    pCsr->base.pVtab = pVTab;
          174  +    pCsr->pgno = -1;
          175  +  }
          176  +
          177  +  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
          178  +  return SQLITE_OK;
          179  +}
          180  +
          181  +/*
          182  +** Close a dbpagevfs cursor.
          183  +*/
          184  +static int dbpageClose(sqlite3_vtab_cursor *pCursor){
          185  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          186  +  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
          187  +  sqlite3_free(pCsr);
          188  +  return SQLITE_OK;
          189  +}
          190  +
          191  +/*
          192  +** Move a dbpagevfs cursor to the next entry in the file.
          193  +*/
          194  +static int dbpageNext(sqlite3_vtab_cursor *pCursor){
          195  +  int rc = SQLITE_OK;
          196  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          197  +  pCsr->pgno++;
          198  +  return rc;
          199  +}
          200  +
          201  +static int dbpageEof(sqlite3_vtab_cursor *pCursor){
          202  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          203  +  return pCsr->pgno > pCsr->mxPgno;
          204  +}
          205  +
          206  +/*
          207  +** idxNum:
          208  +**
          209  +**     0     schema=main, full table scan
          210  +**     1     schema=main, pgno=?1
          211  +**     2     schema=?1, full table scan
          212  +**     3     schema=?1, pgno=?2
          213  +**
          214  +** idxStr is not used
          215  +*/
          216  +static int dbpageFilter(
          217  +  sqlite3_vtab_cursor *pCursor, 
          218  +  int idxNum, const char *idxStr,
          219  +  int argc, sqlite3_value **argv
          220  +){
          221  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          222  +  DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
          223  +  int rc;
          224  +  sqlite3 *db = pTab->db;
          225  +  Btree *pBt;
          226  +
          227  +  /* Default setting is no rows of result */
          228  +  pCsr->pgno = 1; 
          229  +  pCsr->mxPgno = 0;
          230  +
          231  +  if( idxNum & 2 ){
          232  +    const char *zSchema;
          233  +    assert( argc>=1 );
          234  +    zSchema = (const char*)sqlite3_value_text(argv[0]);
          235  +    pCsr->iDb = sqlite3FindDbName(db, zSchema);
          236  +    if( pCsr->iDb<0 ) return SQLITE_OK;
          237  +  }else{
          238  +    pCsr->iDb = 0;
          239  +  }
          240  +  pBt = db->aDb[pCsr->iDb].pBt;
          241  +  if( pBt==0 ) return SQLITE_OK;
          242  +  pCsr->pPager = sqlite3BtreePager(pBt);
          243  +  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
          244  +  pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
          245  +  if( idxNum & 1 ){
          246  +    assert( argc>(idxNum>>1) );
          247  +    pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
          248  +    if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
          249  +      pCsr->pgno = 1;
          250  +      pCsr->mxPgno = 0;
          251  +    }else{
          252  +      pCsr->mxPgno = pCsr->pgno;
          253  +    }
          254  +  }else{
          255  +    assert( pCsr->pgno==1 );
          256  +  }
          257  +  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
          258  +  rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
          259  +  return rc;
          260  +}
          261  +
          262  +static int dbpageColumn(
          263  +  sqlite3_vtab_cursor *pCursor, 
          264  +  sqlite3_context *ctx, 
          265  +  int i
          266  +){
          267  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          268  +  int rc = SQLITE_OK;
          269  +  switch( i ){
          270  +    case 0: {           /* pgno */
          271  +      sqlite3_result_int(ctx, pCsr->pgno);
          272  +      break;
          273  +    }
          274  +    case 1: {           /* data */
          275  +      DbPage *pDbPage = 0;
          276  +      rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
          277  +      if( rc==SQLITE_OK ){
          278  +        sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
          279  +                            SQLITE_TRANSIENT);
          280  +      }
          281  +      sqlite3PagerUnref(pDbPage);
          282  +      break;
          283  +    }
          284  +    default: {          /* schema */
          285  +      sqlite3 *db = sqlite3_context_db_handle(ctx);
          286  +      sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
          287  +      break;
          288  +    }
          289  +  }
          290  +  return SQLITE_OK;
          291  +}
          292  +
          293  +static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
          294  +  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
          295  +  *pRowid = pCsr->pgno;
          296  +  return SQLITE_OK;
          297  +}
          298  +
          299  +static int dbpageUpdate(
          300  +  sqlite3_vtab *pVtab,
          301  +  int argc,
          302  +  sqlite3_value **argv,
          303  +  sqlite_int64 *pRowid
          304  +){
          305  +  DbpageTable *pTab = (DbpageTable *)pVtab;
          306  +  int pgno;
          307  +  DbPage *pDbPage = 0;
          308  +  int rc = SQLITE_OK;
          309  +  char *zErr = 0;
          310  +  const char *zSchema;
          311  +  int iDb;
          312  +  Btree *pBt;
          313  +  Pager *pPager;
          314  +  int szPage;
          315  +
          316  +  if( argc==1 ){
          317  +    zErr = "cannot delete";
          318  +    goto update_fail;
          319  +  }
          320  +  pgno = sqlite3_value_int(argv[0]);
          321  +  if( sqlite3_value_int(argv[1])!=pgno ){
          322  +    zErr = "cannot insert";
          323  +    goto update_fail;
          324  +  }
          325  +  zSchema = (const char*)sqlite3_value_text(argv[4]);
          326  +  iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
          327  +  if( iDb<0 ){
          328  +    zErr = "no such schema";
          329  +    goto update_fail;
          330  +  }
          331  +  pBt = pTab->db->aDb[iDb].pBt;
          332  +  if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
          333  +    zErr = "bad page number";
          334  +    goto update_fail;
          335  +  }
          336  +  szPage = sqlite3BtreeGetPageSize(pBt);
          337  +  if( sqlite3_value_type(argv[3])!=SQLITE_BLOB 
          338  +   || sqlite3_value_bytes(argv[3])!=szPage
          339  +  ){
          340  +    zErr = "bad page value";
          341  +    goto update_fail;
          342  +  }
          343  +  pPager = sqlite3BtreePager(pBt);
          344  +  rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
          345  +  if( rc==SQLITE_OK ){
          346  +    rc = sqlite3PagerWrite(pDbPage);
          347  +    if( rc==SQLITE_OK ){
          348  +      memcpy(sqlite3PagerGetData(pDbPage),
          349  +             sqlite3_value_blob(argv[3]),
          350  +             szPage);
          351  +    }
          352  +  }
          353  +  sqlite3PagerUnref(pDbPage);
          354  +  return rc;
          355  +
          356  +update_fail:
          357  +  sqlite3_free(pVtab->zErrMsg);
          358  +  pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
          359  +  return SQLITE_ERROR;
          360  +}
          361  +
          362  +/* Since we do not know in advance which database files will be
          363  +** written by the sqlite_dbpage virtual table, start a write transaction
          364  +** on them all.
          365  +*/
          366  +static int dbpageBegin(sqlite3_vtab *pVtab){
          367  +  DbpageTable *pTab = (DbpageTable *)pVtab;
          368  +  sqlite3 *db = pTab->db;
          369  +  int i;
          370  +  for(i=0; i<db->nDb; i++){
          371  +    Btree *pBt = db->aDb[i].pBt;
          372  +    if( pBt ) sqlite3BtreeBeginTrans(pBt, 1);
          373  +  }
          374  +  return SQLITE_OK;
          375  +}
          376  +
          377  +
          378  +/*
          379  +** Invoke this routine to register the "dbpage" virtual table module
          380  +*/
          381  +int sqlite3DbpageRegister(sqlite3 *db){
          382  +  static sqlite3_module dbpage_module = {
          383  +    0,                            /* iVersion */
          384  +    dbpageConnect,                /* xCreate */
          385  +    dbpageConnect,                /* xConnect */
          386  +    dbpageBestIndex,              /* xBestIndex */
          387  +    dbpageDisconnect,             /* xDisconnect */
          388  +    dbpageDisconnect,             /* xDestroy */
          389  +    dbpageOpen,                   /* xOpen - open a cursor */
          390  +    dbpageClose,                  /* xClose - close a cursor */
          391  +    dbpageFilter,                 /* xFilter - configure scan constraints */
          392  +    dbpageNext,                   /* xNext - advance a cursor */
          393  +    dbpageEof,                    /* xEof - check for end of scan */
          394  +    dbpageColumn,                 /* xColumn - read data */
          395  +    dbpageRowid,                  /* xRowid - read data */
          396  +    dbpageUpdate,                 /* xUpdate */
          397  +    dbpageBegin,                  /* xBegin */
          398  +    0,                            /* xSync */
          399  +    0,                            /* xCommit */
          400  +    0,                            /* xRollback */
          401  +    0,                            /* xFindMethod */
          402  +    0,                            /* xRename */
          403  +    0,                            /* xSavepoint */
          404  +    0,                            /* xRelease */
          405  +    0,                            /* xRollbackTo */
          406  +  };
          407  +  return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
          408  +}
          409  +#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
          410  +int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
          411  +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */

Changes to src/expr.c.

   120    120     return pExpr;
   121    121   }
   122    122   
   123    123   /*
   124    124   ** Return the collation sequence for the expression pExpr. If
   125    125   ** there is no defined collating sequence, return NULL.
   126    126   **
          127  +** See also: sqlite3ExprNNCollSeq()
          128  +**
          129  +** The sqlite3ExprNNCollSeq() works the same exact that it returns the
          130  +** default collation if pExpr has no defined collation.
          131  +**
   127    132   ** The collating sequence might be determined by a COLLATE operator
   128    133   ** or by the presence of a column with a defined collating sequence.
   129    134   ** COLLATE operators take first precedence.  Left operands take
   130    135   ** precedence over right operands.
   131    136   */
   132    137   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
   133    138     sqlite3 *db = pParse->db;
................................................................................
   183    188       }
   184    189     }
   185    190     if( sqlite3CheckCollSeq(pParse, pColl) ){ 
   186    191       pColl = 0;
   187    192     }
   188    193     return pColl;
   189    194   }
          195  +
          196  +/*
          197  +** Return the collation sequence for the expression pExpr. If
          198  +** there is no defined collating sequence, return a pointer to the
          199  +** defautl collation sequence.
          200  +**
          201  +** See also: sqlite3ExprCollSeq()
          202  +**
          203  +** The sqlite3ExprCollSeq() routine works the same except that it
          204  +** returns NULL if there is no defined collation.
          205  +*/
          206  +CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
          207  +  CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
          208  +  if( p==0 ) p = pParse->db->pDfltColl;
          209  +  assert( p!=0 );
          210  +  return p;
          211  +}
          212  +
          213  +/*
          214  +** Return TRUE if the two expressions have equivalent collating sequences.
          215  +*/
          216  +int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
          217  +  CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
          218  +  CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
          219  +  return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
          220  +}
   190    221   
   191    222   /*
   192    223   ** pExpr is an operand of a comparison operator.  aff2 is the
   193    224   ** type affinity of the other operand.  This routine returns the
   194    225   ** type affinity that should be used for the comparison operator.
   195    226   */
   196    227   char sqlite3CompareAffinity(Expr *pExpr, char aff2){
................................................................................
   917    948     assert( pToken );
   918    949     pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
   919    950     if( pNew==0 ){
   920    951       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   921    952       return 0;
   922    953     }
   923    954     pNew->x.pList = pList;
          955  +  ExprSetProperty(pNew, EP_HasFunc);
   924    956     assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   925    957     sqlite3ExprSetHeightAndFlags(pParse, pNew);
   926    958     return pNew;
   927    959   }
   928    960   
   929    961   /*
   930    962   ** Assign a variable number to an expression that encodes a wildcard
................................................................................
  1677   1709   /*
  1678   1710   ** Return the bitwise-OR of all Expr.flags fields in the given
  1679   1711   ** ExprList.
  1680   1712   */
  1681   1713   u32 sqlite3ExprListFlags(const ExprList *pList){
  1682   1714     int i;
  1683   1715     u32 m = 0;
  1684         -  if( pList ){
  1685         -    for(i=0; i<pList->nExpr; i++){
  1686         -       Expr *pExpr = pList->a[i].pExpr;
  1687         -       assert( pExpr!=0 );
  1688         -       m |= pExpr->flags;
  1689         -    }
         1716  +  assert( pList!=0 );
         1717  +  for(i=0; i<pList->nExpr; i++){
         1718  +     Expr *pExpr = pList->a[i].pExpr;
         1719  +     assert( pExpr!=0 );
         1720  +     m |= pExpr->flags;
  1690   1721     }
  1691   1722     return m;
  1692   1723   }
  1693   1724   
  1694   1725   /*
  1695   1726   ** This is a SELECT-node callback for the expression walker that
  1696   1727   ** always "fails".  By "fail" in this case, we mean set
................................................................................
  1839   1870     int i;
  1840   1871   
  1841   1872     /* Check if pExpr is identical to any GROUP BY term. If so, consider
  1842   1873     ** it constant.  */
  1843   1874     for(i=0; i<pGroupBy->nExpr; i++){
  1844   1875       Expr *p = pGroupBy->a[i].pExpr;
  1845   1876       if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
  1846         -      CollSeq *pColl = sqlite3ExprCollSeq(pWalker->pParse, p);
  1847         -      if( pColl==0 || sqlite3_stricmp("BINARY", pColl->zName)==0 ){
         1877  +      CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
         1878  +      if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){
  1848   1879           return WRC_Prune;
  1849   1880         }
  1850   1881       }
  1851   1882     }
  1852   1883   
  1853   1884     /* Check if pExpr is a sub-select. If so, consider it variable. */
  1854   1885     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  2644   2675             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  2645   2676             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  2646   2677           }
  2647   2678   
  2648   2679           /* Loop through each expression in <exprlist>. */
  2649   2680           r1 = sqlite3GetTempReg(pParse);
  2650   2681           r2 = sqlite3GetTempReg(pParse);
  2651         -        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
         2682  +        if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
  2652   2683           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  2653   2684             Expr *pE2 = pItem->pExpr;
  2654   2685             int iValToIns;
  2655   2686   
  2656   2687             /* If the expression is not constant then we will need to
  2657   2688             ** disable the test that was generated above that makes sure
  2658   2689             ** this code only executes once.  Because for a non-constant

Changes to src/func.c.

   694    694         ** that point.
   695    695         **
   696    696         ** For a case-insensitive search, set variable cx to be the same as
   697    697         ** c but in the other case and search the input string for either
   698    698         ** c or cx.
   699    699         */
   700    700         if( c<=0x80 ){
   701         -        u32 cx;
          701  +        char zStop[3];
   702    702           int bMatch;
   703    703           if( noCase ){
   704         -          cx = sqlite3Toupper(c);
   705         -          c = sqlite3Tolower(c);
          704  +          zStop[0] = sqlite3Toupper(c);
          705  +          zStop[1] = sqlite3Tolower(c);
          706  +          zStop[2] = 0;
   706    707           }else{
   707         -          cx = c;
          708  +          zStop[0] = c;
          709  +          zStop[1] = 0;
   708    710           }
   709         -        while( (c2 = *(zString++))!=0 ){
   710         -          if( c2!=c && c2!=cx ) continue;
          711  +        while(1){
          712  +          zString += strcspn((const char*)zString, zStop);
          713  +          if( zString[0]==0 ) break;
          714  +          zString++;
   711    715             bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
   712    716             if( bMatch!=SQLITE_NOMATCH ) return bMatch;
   713    717           }
   714    718         }else{
   715    719           int bMatch;
   716    720           while( (c2 = Utf8Read(zString))!=0 ){
   717    721             if( c2!=c ) continue;

Changes to src/insert.c.

   480    480     SrcList *pTabList,    /* Name of table into which we are inserting */
   481    481     Select *pSelect,      /* A SELECT statement to use as the data source */
   482    482     IdList *pColumn,      /* Column names corresponding to IDLIST. */
   483    483     int onError           /* How to handle constraint errors */
   484    484   ){
   485    485     sqlite3 *db;          /* The main database structure */
   486    486     Table *pTab;          /* The table to insert into.  aka TABLE */
   487         -  char *zTab;           /* Name of the table into which we are inserting */
   488    487     int i, j;             /* Loop counters */
   489    488     Vdbe *v;              /* Generate code into this virtual machine */
   490    489     Index *pIdx;          /* For looping over indices of the table */
   491    490     int nColumn;          /* Number of columns in the data */
   492    491     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
   493    492     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
   494    493     int iIdxCur = 0;      /* First index cursor */
................................................................................
   536    535       sqlite3SelectDelete(db, pSelect);
   537    536       pSelect = 0;
   538    537     }
   539    538   
   540    539     /* Locate the table into which we will be inserting new information.
   541    540     */
   542    541     assert( pTabList->nSrc==1 );
   543         -  zTab = pTabList->a[0].zName;
   544         -  if( NEVER(zTab==0) ) goto insert_cleanup;
   545    542     pTab = sqlite3SrcListLookup(pParse, pTabList);
   546    543     if( pTab==0 ){
   547    544       goto insert_cleanup;
   548    545     }
   549    546     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   550    547     assert( iDb<db->nDb );
   551    548     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
................................................................................
   908    905           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
   909    906         }else if( pSelect ){
   910    907           sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
   911    908         }else{
   912    909           VdbeOp *pOp;
   913    910           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
   914    911           pOp = sqlite3VdbeGetOp(v, -1);
   915         -        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
          912  +        assert( pOp!=0 );
          913  +        if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
   916    914             appendFlag = 1;
   917    915             pOp->opcode = OP_NewRowid;
   918    916             pOp->p1 = iDataCur;
   919    917             pOp->p2 = regRowid;
   920    918             pOp->p3 = regAutoinc;
   921    919           }
   922    920         }

Changes to src/main.c.

    43     43   #endif
    44     44   
    45     45   /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
    46     46   ** a pointer to the to the sqlite3_version[] string constant. 
    47     47   */
    48     48   const char *sqlite3_libversion(void){ return sqlite3_version; }
    49     49   
    50         -/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
           50  +/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
    51     51   ** pointer to a string constant whose value is the same as the
    52         -** SQLITE_SOURCE_ID C preprocessor macro. 
           52  +** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
           53  +** an edited copy of the amalgamation, then the last four characters of
           54  +** the hash might be different from SQLITE_SOURCE_ID.
    53     55   */
    54     56   const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
    55     57   
    56     58   /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
    57     59   ** returns an integer equal to SQLITE_VERSION_NUMBER.
    58     60   */
    59     61   int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
................................................................................
  3047   3049   #endif
  3048   3050   
  3049   3051   #ifdef SQLITE_ENABLE_RTREE
  3050   3052     if( !db->mallocFailed && rc==SQLITE_OK){
  3051   3053       rc = sqlite3RtreeInit(db);
  3052   3054     }
  3053   3055   #endif
         3056  +
         3057  +#ifdef SQLITE_ENABLE_DBPAGE_VTAB
         3058  +  if( !db->mallocFailed && rc==SQLITE_OK){
         3059  +    rc = sqlite3DbpageRegister(db);
         3060  +  }
         3061  +#endif
  3054   3062   
  3055   3063   #ifdef SQLITE_ENABLE_DBSTAT_VTAB
  3056   3064     if( !db->mallocFailed && rc==SQLITE_OK){
  3057   3065       rc = sqlite3DbstatRegister(db);
  3058   3066     }
  3059   3067   #endif
  3060   3068   
................................................................................
  3707   3715   
  3708   3716       /*
  3709   3717       **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
  3710   3718       **
  3711   3719       ** This action provides a run-time test to see how the ALWAYS and
  3712   3720       ** NEVER macros were defined at compile-time.
  3713   3721       **
  3714         -    ** The return value is ALWAYS(X).  
         3722  +    ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
  3715   3723       **
  3716   3724       ** The recommended test is X==2.  If the return value is 2, that means
  3717   3725       ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
  3718   3726       ** default setting.  If the return value is 1, then ALWAYS() is either
  3719   3727       ** hard-coded to true or else it asserts if its argument is false.
  3720   3728       ** The first behavior (hard-coded to true) is the case if
  3721   3729       ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
................................................................................
  3730   3738       **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
  3731   3739       **    }else{
  3732   3740       **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
  3733   3741       **    }
  3734   3742       */
  3735   3743       case SQLITE_TESTCTRL_ALWAYS: {
  3736   3744         int x = va_arg(ap,int);
  3737         -      rc = ALWAYS(x);
         3745  +      rc = x ? ALWAYS(x) : 0;
  3738   3746         break;
  3739   3747       }
  3740   3748   
  3741   3749       /*
  3742   3750       **   sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
  3743   3751       **
  3744   3752       ** The integer returned reveals the byte-order of the computer on which

Changes to src/os.c.

   149    149   int sqlite3OsSectorSize(sqlite3_file *id){
   150    150     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
   151    151     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
   152    152   }
   153    153   int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
   154    154     return id->pMethods->xDeviceCharacteristics(id);
   155    155   }
          156  +#ifndef SQLITE_OMIT_WAL
   156    157   int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
   157    158     return id->pMethods->xShmLock(id, offset, n, flags);
   158    159   }
   159    160   void sqlite3OsShmBarrier(sqlite3_file *id){
   160    161     id->pMethods->xShmBarrier(id);
   161    162   }
   162    163   int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
................................................................................
   168    169     int pgsz,
   169    170     int bExtend,                    /* True to extend file if necessary */
   170    171     void volatile **pp              /* OUT: Pointer to mapping */
   171    172   ){
   172    173     DO_OS_MALLOC_TEST(id);
   173    174     return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
   174    175   }
          176  +#endif /* SQLITE_OMIT_WAL */
   175    177   
   176    178   #if SQLITE_MAX_MMAP_SIZE>0
   177    179   /* The real implementation of xFetch and xUnfetch */
   178    180   int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
   179    181     DO_OS_MALLOC_TEST(id);
   180    182     return id->pMethods->xFetch(id, iOff, iAmt, pp);
   181    183   }

Changes to src/os.h.

   170    170   int sqlite3OsUnlock(sqlite3_file*, int);
   171    171   int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
   172    172   int sqlite3OsFileControl(sqlite3_file*,int,void*);
   173    173   void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
   174    174   #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
   175    175   int sqlite3OsSectorSize(sqlite3_file *id);
   176    176   int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
          177  +#ifndef SQLITE_OMIT_WAL
   177    178   int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
   178    179   int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
   179    180   void sqlite3OsShmBarrier(sqlite3_file *id);
   180    181   int sqlite3OsShmUnmap(sqlite3_file *id, int);
          182  +#endif /* SQLITE_OMIT_WAL */
   181    183   int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
   182    184   int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
   183    185   
   184    186   
   185    187   /* 
   186    188   ** Functions for accessing sqlite3_vfs methods 
   187    189   */

Changes to src/os_unix.c.

   509    509   #if defined(HAVE_LSTAT)
   510    510     { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
   511    511   #else
   512    512     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
   513    513   #endif
   514    514   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
   515    515   
          516  +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
   516    517     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
          518  +#else
          519  +  { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
          520  +#endif
   517    521   #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
   518    522   
   519    523   }; /* End of the overrideable system calls */
   520    524   
   521    525   
   522    526   /*
   523    527   ** On some systems, calls to fchown() will trigger a message in a security

Changes to src/os_win.c.

  5059   5059       extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
  5060   5060       extendedParameters.dwFileAttributes =
  5061   5061               dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
  5062   5062       extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
  5063   5063       extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
  5064   5064       extendedParameters.lpSecurityAttributes = NULL;
  5065   5065       extendedParameters.hTemplateFile = NULL;
  5066         -    while( (h = osCreateFile2((LPCWSTR)zConverted,
  5067         -                              dwDesiredAccess,
  5068         -                              dwShareMode,
  5069         -                              dwCreationDisposition,
  5070         -                              &extendedParameters))==INVALID_HANDLE_VALUE &&
  5071         -                              winRetryIoerr(&cnt, &lastErrno) ){
  5072         -               /* Noop */
  5073         -    }
         5066  +    do{
         5067  +      h = osCreateFile2((LPCWSTR)zConverted,
         5068  +                        dwDesiredAccess,
         5069  +                        dwShareMode,
         5070  +                        dwCreationDisposition,
         5071  +                        &extendedParameters);
         5072  +      if( h!=INVALID_HANDLE_VALUE ) break;
         5073  +      if( isReadWrite ){
         5074  +        int isRO = 0;
         5075  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
         5076  +        if( rc2==SQLITE_OK && isRO ) break;
         5077  +      }
         5078  +    }while( winRetryIoerr(&cnt, &lastErrno) );
  5074   5079   #else
  5075   5080       do{
  5076   5081         h = osCreateFileW((LPCWSTR)zConverted,
  5077   5082                           dwDesiredAccess,
  5078   5083                           dwShareMode, NULL,
  5079   5084                           dwCreationDisposition,
  5080   5085                           dwFlagsAndAttributes,
................................................................................
  5086   5091           if( rc2==SQLITE_OK && isRO ) break;
  5087   5092         }
  5088   5093       }while( winRetryIoerr(&cnt, &lastErrno) );
  5089   5094   #endif
  5090   5095     }
  5091   5096   #ifdef SQLITE_WIN32_HAS_ANSI
  5092   5097     else{
  5093         -    while( (h = osCreateFileA((LPCSTR)zConverted,
  5094         -                              dwDesiredAccess,
  5095         -                              dwShareMode, NULL,
  5096         -                              dwCreationDisposition,
  5097         -                              dwFlagsAndAttributes,
  5098         -                              NULL))==INVALID_HANDLE_VALUE &&
  5099         -                              winRetryIoerr(&cnt, &lastErrno) ){
  5100         -               /* Noop */
  5101         -    }
         5098  +    do{
         5099  +      h = osCreateFileA((LPCSTR)zConverted,
         5100  +                        dwDesiredAccess,
         5101  +                        dwShareMode, NULL,
         5102  +                        dwCreationDisposition,
         5103  +                        dwFlagsAndAttributes,
         5104  +                        NULL);
         5105  +      if( h!=INVALID_HANDLE_VALUE ) break;
         5106  +      if( isReadWrite ){
         5107  +        int isRO = 0;
         5108  +        int rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
         5109  +        if( rc2==SQLITE_OK && isRO ) break;
         5110  +      }
         5111  +    }while( winRetryIoerr(&cnt, &lastErrno) );
  5102   5112     }
  5103   5113   #endif
  5104   5114     winLogIoerr(cnt, __LINE__);
  5105   5115   
  5106   5116     OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
  5107   5117              dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
  5108   5118   

Changes to src/pager.c.

  1209   1209   
  1210   1210   #if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
  1211   1211    || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
  1212   1212     int dc;                           /* Device characteristics */
  1213   1213   
  1214   1214     assert( isOpen(pPager->fd) );
  1215   1215     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
         1216  +#else
         1217  +  UNUSED_PARAMETER(pPager);
  1216   1218   #endif
  1217   1219   
  1218   1220   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
  1219   1221     if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
  1220   1222       return -1;
  1221   1223     }
  1222   1224   #endif
................................................................................
  3058   3060   **
  3059   3061   ** If an IO error occurs, then the IO error is returned to the caller.
  3060   3062   ** Otherwise, SQLITE_OK is returned.
  3061   3063   */
  3062   3064   static int readDbPage(PgHdr *pPg){
  3063   3065     Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
  3064   3066     int rc = SQLITE_OK;          /* Return code */
         3067  +
         3068  +#ifndef SQLITE_OMIT_WAL
  3065   3069     u32 iFrame = 0;              /* Frame of WAL containing pgno */
  3066   3070   
  3067   3071     assert( pPager->eState>=PAGER_READER && !MEMDB );
  3068   3072     assert( isOpen(pPager->fd) );
  3069   3073   
  3070   3074     if( pagerUseWal(pPager) ){
  3071   3075       rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
  3072   3076       if( rc ) return rc;
  3073   3077     }
  3074   3078     if( iFrame ){
  3075   3079       rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
  3076         -  }else{
         3080  +  }else
         3081  +#endif
         3082  +  {
  3077   3083       i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
  3078   3084       rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
  3079   3085       if( rc==SQLITE_IOERR_SHORT_READ ){
  3080   3086         rc = SQLITE_OK;
  3081   3087       }
  3082   3088     }
  3083   3089   

Changes to src/pcache.c.

   562    562   
   563    563   /*
   564    564   ** Make sure the page is marked as clean. If it isn't clean already,
   565    565   ** make it so.
   566    566   */
   567    567   void sqlite3PcacheMakeClean(PgHdr *p){
   568    568     assert( sqlite3PcachePageSanity(p) );
   569         -  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
   570         -    assert( (p->flags & PGHDR_CLEAN)==0 );
   571         -    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   572         -    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
   573         -    p->flags |= PGHDR_CLEAN;
   574         -    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
   575         -    assert( sqlite3PcachePageSanity(p) );
   576         -    if( p->nRef==0 ){
   577         -      pcacheUnpin(p);
   578         -    }
          569  +  assert( (p->flags & PGHDR_DIRTY)!=0 );
          570  +  assert( (p->flags & PGHDR_CLEAN)==0 );
          571  +  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
          572  +  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
          573  +  p->flags |= PGHDR_CLEAN;
          574  +  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
          575  +  assert( sqlite3PcachePageSanity(p) );
          576  +  if( p->nRef==0 ){
          577  +    pcacheUnpin(p);
   579    578     }
   580    579   }
   581    580   
   582    581   /*
   583    582   ** Make every page in the cache clean.
   584    583   */
   585    584   void sqlite3PcacheCleanAll(PCache *pCache){

Changes to src/prepare.c.

   469    469     ** We return -1000000 instead of the more usual -1 simply because using
   470    470     ** -1000000 as the incorrect index into db->aDb[] is much 
   471    471     ** more likely to cause a segfault than -1 (of course there are assert()
   472    472     ** statements too, but it never hurts to play the odds).
   473    473     */
   474    474     assert( sqlite3_mutex_held(db->mutex) );
   475    475     if( pSchema ){
   476         -    for(i=0; ALWAYS(i<db->nDb); i++){
          476  +    for(i=0; 1; i++){
          477  +      assert( i<db->nDb );
   477    478         if( db->aDb[i].pSchema==pSchema ){
   478    479           break;
   479    480         }
   480    481       }
   481    482       assert( i>=0 && i<db->nDb );
   482    483     }
   483    484     return i;

Changes to src/resolve.c.

   955    955     ExprList *pEList;
   956    956     sqlite3 *db;
   957    957     int moreToDo = 1;
   958    958   
   959    959     pOrderBy = pSelect->pOrderBy;
   960    960     if( pOrderBy==0 ) return 0;
   961    961     db = pParse->db;
   962         -#if SQLITE_MAX_COLUMN
   963    962     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
   964    963       sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
   965    964       return 1;
   966    965     }
   967         -#endif
   968    966     for(i=0; i<pOrderBy->nExpr; i++){
   969    967       pOrderBy->a[i].done = 0;
   970    968     }
   971    969     pSelect->pNext = 0;
   972    970     while( pSelect->pPrior ){
   973    971       pSelect->pPrior->pNext = pSelect;
   974    972       pSelect = pSelect->pPrior;
................................................................................
  1052   1050   ){
  1053   1051     int i;
  1054   1052     sqlite3 *db = pParse->db;
  1055   1053     ExprList *pEList;
  1056   1054     struct ExprList_item *pItem;
  1057   1055   
  1058   1056     if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
  1059         -#if SQLITE_MAX_COLUMN
  1060   1057     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1061   1058       sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
  1062   1059       return 1;
  1063   1060     }
  1064         -#endif
  1065   1061     pEList = pSelect->pEList;
  1066   1062     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
  1067   1063     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
  1068   1064       if( pItem->u.x.iOrderByCol ){
  1069   1065         if( pItem->u.x.iOrderByCol>pEList->nExpr ){
  1070   1066           resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
  1071   1067           return 1;

Changes to src/select.c.

    71     71       sqlite3SrcListDelete(db, p->pSrc);
    72     72       sqlite3ExprDelete(db, p->pWhere);
    73     73       sqlite3ExprListDelete(db, p->pGroupBy);
    74     74       sqlite3ExprDelete(db, p->pHaving);
    75     75       sqlite3ExprListDelete(db, p->pOrderBy);
    76     76       sqlite3ExprDelete(db, p->pLimit);
    77     77       sqlite3ExprDelete(db, p->pOffset);
    78         -    if( p->pWith ) sqlite3WithDelete(db, p->pWith);
           78  +    if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
    79     79       if( bFree ) sqlite3DbFreeNN(db, p);
    80     80       p = pPrior;
    81     81       bFree = 1;
    82     82     }
    83     83   }
    84     84   
    85     85   /*
................................................................................
   114    114     Select standin;
   115    115     pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
   116    116     if( pNew==0 ){
   117    117       assert( pParse->db->mallocFailed );
   118    118       pNew = &standin;
   119    119     }
   120    120     if( pEList==0 ){
   121         -    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(pParse->db,TK_ASTERISK,0));
          121  +    pEList = sqlite3ExprListAppend(pParse, 0,
          122  +                                   sqlite3Expr(pParse->db,TK_ASTERISK,0));
   122    123     }
   123    124     pNew->pEList = pEList;
   124    125     pNew->op = TK_SELECT;
   125    126     pNew->selFlags = selFlags;
   126    127     pNew->iLimit = 0;
   127    128     pNew->iOffset = 0;
   128    129   #if SELECTTRACE_ENABLED
................................................................................
   138    139     pNew->pHaving = pHaving;
   139    140     pNew->pOrderBy = pOrderBy;
   140    141     pNew->pPrior = 0;
   141    142     pNew->pNext = 0;
   142    143     pNew->pLimit = pLimit;
   143    144     pNew->pOffset = pOffset;
   144    145     pNew->pWith = 0;
   145         -  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || pParse->db->mallocFailed!=0 );
          146  +  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0
          147  +                     || pParse->db->mallocFailed!=0 );
   146    148     if( pParse->db->mallocFailed ) {
   147    149       clearSelect(pParse->db, pNew, pNew!=&standin);
   148    150       pNew = 0;
   149    151     }else{
   150    152       assert( pNew->pSrc!=0 || pParse->nErr>0 );
   151    153     }
   152    154     assert( pNew!=&standin );
................................................................................
   165    167   #endif
   166    168   
   167    169   
   168    170   /*
   169    171   ** Delete the given Select structure and all of its substructures.
   170    172   */
   171    173   void sqlite3SelectDelete(sqlite3 *db, Select *p){
   172         -  if( p ) clearSelect(db, p, 1);
          174  +  if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
   173    175   }
   174    176   
   175    177   /*
   176    178   ** Return a pointer to the right-most SELECT statement in a compound.
   177    179   */
   178    180   static Select *findRightmost(Select *p){
   179    181     while( p->pNext ) p = p->pNext;
................................................................................
   406    408     struct SrcList_item *pLeft;     /* Left table being joined */
   407    409     struct SrcList_item *pRight;    /* Right table being joined */
   408    410   
   409    411     pSrc = p->pSrc;
   410    412     pLeft = &pSrc->a[0];
   411    413     pRight = &pLeft[1];
   412    414     for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
   413         -    Table *pLeftTab = pLeft->pTab;
   414    415       Table *pRightTab = pRight->pTab;
   415    416       int isOuter;
   416    417   
   417         -    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
          418  +    if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
   418    419       isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
   419    420   
   420    421       /* When the NATURAL keyword is present, add WHERE clause terms for
   421    422       ** every column that the two tables have in common.
   422    423       */
   423    424       if( pRight->fg.jointype & JT_NATURAL ){
   424    425         if( pRight->pOn || pRight->pUsing ){
................................................................................
   755    756             p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
   756    757           }
   757    758         }
   758    759         regOrig = 0;
   759    760         assert( eDest==SRT_Set || eDest==SRT_Mem 
   760    761              || eDest==SRT_Coroutine || eDest==SRT_Output );
   761    762       }
   762         -    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,0,ecelFlags);
          763  +    nResultCol = sqlite3ExprCodeExprList(pParse,p->pEList,regResult,
          764  +                                         0,ecelFlags);
   763    765     }
   764    766   
   765    767     /* If the DISTINCT keyword was present on the SELECT statement
   766    768     ** and this row has been seen before, then do not make this row
   767    769     ** part of the result.
   768    770     */
   769    771     if( hasDistinct ){
................................................................................
  1105   1107     int i;
  1106   1108   
  1107   1109     nExpr = pList->nExpr;
  1108   1110     pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
  1109   1111     if( pInfo ){
  1110   1112       assert( sqlite3KeyInfoIsWriteable(pInfo) );
  1111   1113       for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
  1112         -      CollSeq *pColl;
  1113         -      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
  1114         -      if( !pColl ) pColl = db->pDfltColl;
  1115         -      pInfo->aColl[i-iStart] = pColl;
         1114  +      pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
  1116   1115         pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
  1117   1116       }
  1118   1117     }
  1119   1118     return pInfo;
  1120   1119   }
  1121   1120   
  1122   1121   /*
................................................................................
  1358   1357   ** 
  1359   1358   ** The declaration type for any expression other than a column is NULL.
  1360   1359   **
  1361   1360   ** This routine has either 3 or 6 parameters depending on whether or not
  1362   1361   ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
  1363   1362   */
  1364   1363   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1365         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
         1364  +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
  1366   1365   #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1367         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
         1366  +# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
  1368   1367   #endif
  1369   1368   static const char *columnTypeImpl(
  1370   1369     NameContext *pNC, 
         1370  +#ifndef SQLITE_ENABLE_COLUMN_METADATA
         1371  +  Expr *pExpr
         1372  +#else
  1371   1373     Expr *pExpr,
  1372         -#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1373   1374     const char **pzOrigDb,
  1374   1375     const char **pzOrigTab,
  1375         -  const char **pzOrigCol,
         1376  +  const char **pzOrigCol
  1376   1377   #endif
  1377         -  u8 *pEstWidth
  1378   1378   ){
  1379   1379     char const *zType = 0;
  1380   1380     int j;
  1381         -  u8 estWidth = 1;
  1382   1381   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1383   1382     char const *zOrigDb = 0;
  1384   1383     char const *zOrigTab = 0;
  1385   1384     char const *zOrigCol = 0;
  1386   1385   #endif
  1387   1386   
  1388   1387     assert( pExpr!=0 );
................................................................................
  1443   1442             ** test case misc2.2.2) - it always evaluates to NULL.
  1444   1443             */
  1445   1444             NameContext sNC;
  1446   1445             Expr *p = pS->pEList->a[iCol].pExpr;
  1447   1446             sNC.pSrcList = pS->pSrc;
  1448   1447             sNC.pNext = pNC;
  1449   1448             sNC.pParse = pNC->pParse;
  1450         -          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
         1449  +          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); 
  1451   1450           }
  1452         -      }else if( pTab->pSchema ){
  1453         -        /* A real table */
         1451  +      }else{
         1452  +        /* A real table or a CTE table */
  1454   1453           assert( !pS );
         1454  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1455   1455           if( iCol<0 ) iCol = pTab->iPKey;
  1456         -        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
  1457         -#ifdef SQLITE_ENABLE_COLUMN_METADATA
         1456  +        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
  1458   1457           if( iCol<0 ){
  1459   1458             zType = "INTEGER";
  1460   1459             zOrigCol = "rowid";
  1461   1460           }else{
  1462   1461             zOrigCol = pTab->aCol[iCol].zName;
  1463   1462             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
  1464         -          estWidth = pTab->aCol[iCol].szEst;
  1465   1463           }
  1466   1464           zOrigTab = pTab->zName;
  1467         -        if( pNC->pParse ){
         1465  +        if( pNC->pParse && pTab->pSchema ){
  1468   1466             int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
  1469   1467             zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
  1470   1468           }
  1471   1469   #else
         1470  +        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
  1472   1471           if( iCol<0 ){
  1473   1472             zType = "INTEGER";
  1474   1473           }else{
  1475   1474             zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
  1476         -          estWidth = pTab->aCol[iCol].szEst;
  1477   1475           }
  1478   1476   #endif
  1479   1477         }
  1480   1478         break;
  1481   1479       }
  1482   1480   #ifndef SQLITE_OMIT_SUBQUERY
  1483   1481       case TK_SELECT: {
................................................................................
  1488   1486         NameContext sNC;
  1489   1487         Select *pS = pExpr->x.pSelect;
  1490   1488         Expr *p = pS->pEList->a[0].pExpr;
  1491   1489         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1492   1490         sNC.pSrcList = pS->pSrc;
  1493   1491         sNC.pNext = pNC;
  1494   1492         sNC.pParse = pNC->pParse;
  1495         -      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); 
         1493  +      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 
  1496   1494         break;
  1497   1495       }
  1498   1496   #endif
  1499   1497     }
  1500   1498   
  1501   1499   #ifdef SQLITE_ENABLE_COLUMN_METADATA  
  1502   1500     if( pzOrigDb ){
  1503   1501       assert( pzOrigTab && pzOrigCol );
  1504   1502       *pzOrigDb = zOrigDb;
  1505   1503       *pzOrigTab = zOrigTab;
  1506   1504       *pzOrigCol = zOrigCol;
  1507   1505     }
  1508   1506   #endif
  1509         -  if( pEstWidth ) *pEstWidth = estWidth;
  1510   1507     return zType;
  1511   1508   }
  1512   1509   
  1513   1510   /*
  1514   1511   ** Generate code that will tell the VDBE the declaration types of columns
  1515   1512   ** in the result set.
  1516   1513   */
................................................................................
  1529   1526     for(i=0; i<pEList->nExpr; i++){
  1530   1527       Expr *p = pEList->a[i].pExpr;
  1531   1528       const char *zType;
  1532   1529   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1533   1530       const char *zOrigDb = 0;
  1534   1531       const char *zOrigTab = 0;
  1535   1532       const char *zOrigCol = 0;
  1536         -    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
         1533  +    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
  1537   1534   
  1538   1535       /* The vdbe must make its own copy of the column-type and other 
  1539   1536       ** column specific strings, in case the schema is reset before this
  1540   1537       ** virtual machine is deleted.
  1541   1538       */
  1542   1539       sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
  1543   1540       sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
  1544   1541       sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
  1545   1542   #else
  1546         -    zType = columnType(&sNC, p, 0, 0, 0, 0);
         1543  +    zType = columnType(&sNC, p, 0, 0, 0);
  1547   1544   #endif
  1548   1545       sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
  1549   1546     }
  1550   1547   #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
  1551   1548   }
  1552   1549   
  1553   1550   
................................................................................
  1569   1566   ** other modes for legacy:
  1570   1567   **
  1571   1568   **    short=OFF, full=OFF:      Column name is the text of the expression has it
  1572   1569   **                              originally appears in the SELECT statement.  In
  1573   1570   **                              other words, the zSpan of the result expression.
  1574   1571   **
  1575   1572   **    short=ON, full=OFF:       (This is the default setting).  If the result
  1576         -**                              refers directly to a table column, then the result
  1577         -**                              column name is just the table column name: COLUMN. 
  1578         -**                              Otherwise use zSpan.
         1573  +**                              refers directly to a table column, then the
         1574  +**                              result column name is just the table column
         1575  +**                              name: COLUMN.  Otherwise use zSpan.
  1579   1576   **
  1580   1577   **    full=ON, short=ANY:       If the result refers directly to a table column,
  1581   1578   **                              then the result column name with the table name
  1582   1579   **                              prefix, ex: TABLE.COLUMN.  Otherwise use zSpan.
  1583   1580   */
  1584   1581   static void generateColumnNames(
  1585   1582     Parse *pParse,      /* Parser context */
................................................................................
  1613   1610     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
  1614   1611     sqlite3VdbeSetNumCols(v, pEList->nExpr);
  1615   1612     for(i=0; i<pEList->nExpr; i++){
  1616   1613       Expr *p = pEList->a[i].pExpr;
  1617   1614   
  1618   1615       assert( p!=0 );
  1619   1616       assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
  1620         -    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering indexes not yet coded */
         1617  +    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
  1621   1618       if( pEList->a[i].zName ){
  1622   1619         /* An AS clause always takes first priority */
  1623   1620         char *zName = pEList->a[i].zName;
  1624   1621         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
  1625   1622       }else if( srcName && p->op==TK_COLUMN ){
  1626   1623         char *zCol;
  1627   1624         int iCol = p->iColumn;
................................................................................
  1688   1685     Hash ht;                    /* Hash table of column names */
  1689   1686   
  1690   1687     sqlite3HashInit(&ht);
  1691   1688     if( pEList ){
  1692   1689       nCol = pEList->nExpr;
  1693   1690       aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
  1694   1691       testcase( aCol==0 );
         1692  +    if( nCol>32767 ) nCol = 32767;
  1695   1693     }else{
  1696   1694       nCol = 0;
  1697   1695       aCol = 0;
  1698   1696     }
  1699   1697     assert( nCol==(i16)nCol );
  1700   1698     *pnCol = nCol;
  1701   1699     *paCol = aCol;
................................................................................
  1784   1782     sqlite3 *db = pParse->db;
  1785   1783     NameContext sNC;
  1786   1784     Column *pCol;
  1787   1785     CollSeq *pColl;
  1788   1786     int i;
  1789   1787     Expr *p;
  1790   1788     struct ExprList_item *a;
  1791         -  u64 szAll = 0;
  1792   1789   
  1793   1790     assert( pSelect!=0 );
  1794   1791     assert( (pSelect->selFlags & SF_Resolved)!=0 );
  1795   1792     assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
  1796   1793     if( db->mallocFailed ) return;
  1797   1794     memset(&sNC, 0, sizeof(sNC));
  1798   1795     sNC.pSrcList = pSelect->pSrc;
  1799   1796     a = pSelect->pEList->a;
  1800   1797     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  1801   1798       const char *zType;
  1802   1799       int n, m;
  1803   1800       p = a[i].pExpr;
  1804         -    zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst);
  1805         -    szAll += pCol->szEst;
         1801  +    zType = columnType(&sNC, p, 0, 0, 0);
         1802  +    /* pCol->szEst = ... // Column size est for SELECT tables never used */
  1806   1803       pCol->affinity = sqlite3ExprAffinity(p);
  1807         -    if( zType && (m = sqlite3Strlen30(zType))>0 ){
         1804  +    if( zType ){
         1805  +      m = sqlite3Strlen30(zType);
  1808   1806         n = sqlite3Strlen30(pCol->zName);
  1809   1807         pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
  1810   1808         if( pCol->zName ){
  1811   1809           memcpy(&pCol->zName[n+1], zType, m+1);
  1812   1810           pCol->colFlags |= COLFLAG_HASTYPE;
  1813   1811         }
  1814   1812       }
  1815   1813       if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
  1816   1814       pColl = sqlite3ExprCollSeq(pParse, p);
  1817   1815       if( pColl && pCol->zColl==0 ){
  1818   1816         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
  1819   1817       }
  1820   1818     }
  1821         -  pTab->szTabRow = sqlite3LogEst(szAll*4);
         1819  +  pTab->szTabRow = 1; /* Any non-zero value works */
  1822   1820   }
  1823   1821   
  1824   1822   /*
  1825   1823   ** Given a SELECT statement, generate a Table structure that describes
  1826   1824   ** the result set of that SELECT.
  1827   1825   */
  1828   1826   Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
................................................................................
  3177   3175   ** of the subquery rather the result set of the subquery.
  3178   3176   */
  3179   3177   static Expr *substExpr(
  3180   3178     SubstContext *pSubst,  /* Description of the substitution */
  3181   3179     Expr *pExpr            /* Expr in which substitution occurs */
  3182   3180   ){
  3183   3181     if( pExpr==0 ) return 0;
  3184         -  if( ExprHasProperty(pExpr, EP_FromJoin) && pExpr->iRightJoinTable==pSubst->iTable ){
         3182  +  if( ExprHasProperty(pExpr, EP_FromJoin)
         3183  +   && pExpr->iRightJoinTable==pSubst->iTable
         3184  +  ){
  3185   3185       pExpr->iRightJoinTable = pSubst->iNewTable;
  3186   3186     }
  3187   3187     if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
  3188   3188       if( pExpr->iColumn<0 ){
  3189   3189         pExpr->op = TK_NULL;
  3190   3190       }else{
  3191   3191         Expr *pNew;
................................................................................
  3290   3290   **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
  3291   3291   **
  3292   3292   ** The code generated for this simplification gives the same result
  3293   3293   ** but only has to scan the data once.  And because indices might 
  3294   3294   ** exist on the table t1, a complete scan of the data might be
  3295   3295   ** avoided.
  3296   3296   **
  3297         -** Flattening is only attempted if all of the following are true:
         3297  +** Flattening is subject to the following constraints:
  3298   3298   **
  3299         -**   (1)  The subquery and the outer query do not both use aggregates.
         3299  +**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
         3300  +**        The subquery and the outer query cannot both be aggregates.
  3300   3301   **
  3301         -**   (2)  The subquery is not an aggregate or (2a) the outer query is not a join
  3302         -**        and (2b) the outer query does not use subqueries other than the one
  3303         -**        FROM-clause subquery that is a candidate for flattening.  (2b is
  3304         -**        due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
         3302  +**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
         3303  +**        (2) If the subquery is an aggregate then
         3304  +**        (2a) the outer query must not be a join and
         3305  +**        (2b) the outer query must not use subqueries
         3306  +**             other than the one FROM-clause subquery that is a candidate
         3307  +**             for flattening.  (This is due to ticket [2f7170d73bf9abf80]
         3308  +**             from 2015-02-09.)
  3305   3309   **
  3306         -**   (3)  The subquery is not the right operand of a LEFT JOIN
  3307         -**        or (a) the subquery is not itself a join and (b) the FROM clause
  3308         -**        of the subquery does not contain a virtual table and (c) the 
  3309         -**        outer query is not an aggregate.
         3310  +**   (3)  If the subquery is the right operand of a LEFT JOIN then
         3311  +**        (3a) the subquery may not be a join and
         3312  +**        (3b) the FROM clause of the subquery may not contain a virtual
         3313  +**             table and
         3314  +**        (3c) the outer query may not be an aggregate.
  3310   3315   **
  3311         -**   (4)  The subquery is not DISTINCT.
         3316  +**   (4)  The subquery can not be DISTINCT.
  3312   3317   **
  3313   3318   **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
  3314   3319   **        sub-queries that were excluded from this optimization. Restriction 
  3315   3320   **        (4) has since been expanded to exclude all DISTINCT subqueries.
  3316   3321   **
  3317         -**   (6)  The subquery does not use aggregates or the outer query is not
  3318         -**        DISTINCT.
         3322  +**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
         3323  +**        If the subquery is aggregate, the outer query may not be DISTINCT.
  3319   3324   **
  3320         -**   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
         3325  +**   (7)  The subquery must have a FROM clause.  TODO:  For subqueries without
  3321   3326   **        A FROM clause, consider adding a FROM clause with the special
  3322   3327   **        table sqlite_once that consists of a single row containing a
  3323   3328   **        single NULL.
  3324   3329   **
  3325         -**   (8)  The subquery does not use LIMIT or the outer query is not a join.
         3330  +**   (8)  If the subquery uses LIMIT then the outer query may not be a join.
  3326   3331   **
  3327         -**   (9)  The subquery does not use LIMIT or the outer query does not use
  3328         -**        aggregates.
         3332  +**   (9)  If the subquery uses LIMIT then the outer query may not be aggregate.
  3329   3333   **
  3330   3334   **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
  3331   3335   **        accidently carried the comment forward until 2014-09-15.  Original
  3332         -**        text: "The subquery does not use aggregates or the outer query 
  3333         -**        does not use LIMIT."
         3336  +**        constraint: "If the subquery is aggregate then the outer query 
         3337  +**        may not use LIMIT."
  3334   3338   **
  3335         -**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
         3339  +**  (11)  The subquery and the outer query may not both have ORDER BY clauses.
  3336   3340   **
  3337   3341   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
  3338   3342   **        a separate restriction deriving from ticket #350.
  3339   3343   **
  3340         -**  (13)  The subquery and outer query do not both use LIMIT.
         3344  +**  (13)  The subquery and outer query may not both use LIMIT.
  3341   3345   **
  3342         -**  (14)  The subquery does not use OFFSET.
         3346  +**  (14)  The subquery may not use OFFSET.
  3343   3347   **
  3344         -**  (15)  The outer query is not part of a compound select or the
  3345         -**        subquery does not have a LIMIT clause.
         3348  +**  (15)  If the outer query is part of a compound select, then the
         3349  +**        subquery may not use LIMIT.
  3346   3350   **        (See ticket #2339 and ticket [02a8e81d44]).
  3347   3351   **
  3348         -**  (16)  The outer query is not an aggregate or the subquery does
  3349         -**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
         3352  +**  (16)  If the outer query is aggregate, then the subquery may not
         3353  +**        use ORDER BY.  (Ticket #2942)  This used to not matter
  3350   3354   **        until we introduced the group_concat() function.  
  3351   3355   **
  3352         -**  (17)  The sub-query is not a compound select, or it is a UNION ALL 
  3353         -**        compound clause made up entirely of non-aggregate queries, and 
  3354         -**        the parent query:
  3355         -**
  3356         -**          * is not itself part of a compound select,
  3357         -**          * is not an aggregate or DISTINCT query, and
  3358         -**          * is not a join
         3356  +**  (17)  If the subquery is a compound select, then
         3357  +**        (17a) all compound operators must be a UNION ALL, and
         3358  +**        (17b) no terms within the subquery compound may be aggregate
         3359  +**              or DISTINCT, and
         3360  +**        (17c) every term within the subquery compound must have a FROM clause
         3361  +**        (17d) the outer query may not be
         3362  +**              (17d1) aggregate, or
         3363  +**              (17d2) DISTINCT, or
         3364  +**              (17d3) a join.
  3359   3365   **
  3360   3366   **        The parent and sub-query may contain WHERE clauses. Subject to
  3361   3367   **        rules (11), (13) and (14), they may also contain ORDER BY,
  3362   3368   **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
  3363   3369   **        operator other than UNION ALL because all the other compound
  3364   3370   **        operators have an implied DISTINCT which is disallowed by
  3365   3371   **        restriction (4).
................................................................................
  3367   3373   **        Also, each component of the sub-query must return the same number
  3368   3374   **        of result columns. This is actually a requirement for any compound
  3369   3375   **        SELECT statement, but all the code here does is make sure that no
  3370   3376   **        such (illegal) sub-query is flattened. The caller will detect the
  3371   3377   **        syntax error and return a detailed message.
  3372   3378   **
  3373   3379   **  (18)  If the sub-query is a compound select, then all terms of the
  3374         -**        ORDER by clause of the parent must be simple references to 
         3380  +**        ORDER BY clause of the parent must be simple references to 
  3375   3381   **        columns of the sub-query.
  3376   3382   **
  3377         -**  (19)  The subquery does not use LIMIT or the outer query does not
         3383  +**  (19)  If the subquery uses LIMIT then the outer query may not
  3378   3384   **        have a WHERE clause.
  3379   3385   **
  3380   3386   **  (20)  If the sub-query is a compound select, then it must not use
  3381   3387   **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
  3382   3388   **        somewhat by saying that the terms of the ORDER BY clause must
  3383   3389   **        appear as unmodified result columns in the outer query.  But we
  3384   3390   **        have other optimizations in mind to deal with that case.
  3385   3391   **
  3386         -**  (21)  The subquery does not use LIMIT or the outer query is not
         3392  +**  (21)  If the subquery uses LIMIT then the outer query may not be
  3387   3393   **        DISTINCT.  (See ticket [752e1646fc]).
  3388   3394   **
  3389         -**  (22)  The subquery is not a recursive CTE.
         3395  +**  (22)  The subquery may not be a recursive CTE.
  3390   3396   **
  3391         -**  (23)  The parent is not a recursive CTE, or the sub-query is not a
  3392         -**        compound query. This restriction is because transforming the
         3397  +**  (**)  Subsumed into restriction (17d3).  Was: If the outer query is
         3398  +**        a recursive CTE, then the sub-query may not be a compound query.
         3399  +**        This restriction is because transforming the
  3393   3400   **        parent to a compound query confuses the code that handles
  3394   3401   **        recursive queries in multiSelect().
  3395   3402   **
  3396         -**  (24)  The subquery is not an aggregate that uses the built-in min() or 
         3403  +**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
         3404  +**        The subquery may not be an aggregate that uses the built-in min() or 
  3397   3405   **        or max() functions.  (Without this restriction, a query like:
  3398   3406   **        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
  3399   3407   **        return the value X for which Y was maximal.)
  3400   3408   **
  3401   3409   **
  3402   3410   ** In this routine, the "p" parameter is a pointer to the outer query.
  3403   3411   ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
  3404         -** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
         3412  +** uses aggregates.
  3405   3413   **
  3406   3414   ** If flattening is not attempted, this routine is a no-op and returns 0.
  3407   3415   ** If flattening is attempted this routine returns 1.
  3408   3416   **
  3409   3417   ** All of the expression analysis must occur on both the outer query and
  3410   3418   ** the subquery before this routine runs.
  3411   3419   */
  3412   3420   static int flattenSubquery(
  3413   3421     Parse *pParse,       /* Parsing context */
  3414   3422     Select *p,           /* The parent or outer SELECT statement */
  3415   3423     int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
  3416         -  int isAgg,           /* True if outer SELECT uses aggregate functions */
  3417         -  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
         3424  +  int isAgg            /* True if outer SELECT uses aggregate functions */
  3418   3425   ){
  3419   3426     const char *zSavedAuthContext = pParse->zAuthContext;
  3420   3427     Select *pParent;    /* Current UNION ALL term of the other query */
  3421   3428     Select *pSub;       /* The inner query or "subquery" */
  3422   3429     Select *pSub1;      /* Pointer to the rightmost select in sub-query */
  3423   3430     SrcList *pSrc;      /* The FROM clause of the outer query */
  3424   3431     SrcList *pSubSrc;   /* The FROM clause of the subquery */
................................................................................
  3429   3436     Expr *pWhere;                    /* The WHERE clause */
  3430   3437     struct SrcList_item *pSubitem;   /* The subquery */
  3431   3438     sqlite3 *db = pParse->db;
  3432   3439   
  3433   3440     /* Check to see if flattening is permitted.  Return 0 if not.
  3434   3441     */
  3435   3442     assert( p!=0 );
  3436         -  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
         3443  +  assert( p->pPrior==0 );
  3437   3444     if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
  3438   3445     pSrc = p->pSrc;
  3439   3446     assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
  3440   3447     pSubitem = &pSrc->a[iFrom];
  3441   3448     iParent = pSubitem->iCursor;
  3442   3449     pSub = pSubitem->pSelect;
  3443   3450     assert( pSub!=0 );
  3444         -  if( subqueryIsAgg ){
  3445         -    if( isAgg ) return 0;                                /* Restriction (1)   */
  3446         -    if( pSrc->nSrc>1 ) return 0;                         /* Restriction (2a)  */
  3447         -    if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
  3448         -     || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
  3449         -     || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
  3450         -    ){
  3451         -      return 0;                                          /* Restriction (2b)  */
  3452         -    }
  3453         -  }
  3454   3451   
  3455   3452     pSubSrc = pSub->pSrc;
  3456   3453     assert( pSubSrc );
  3457   3454     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3458   3455     ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
  3459   3456     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
  3460   3457     ** became arbitrary expressions, we were forced to add restrictions (13)
................................................................................
  3461   3458     ** and (14). */
  3462   3459     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
  3463   3460     if( pSub->pOffset ) return 0;                          /* Restriction (14) */
  3464   3461     if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3465   3462       return 0;                                            /* Restriction (15) */
  3466   3463     }
  3467   3464     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
  3468         -  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
         3465  +  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
  3469   3466     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
  3470   3467        return 0;         /* Restrictions (8)(9) */
  3471   3468     }
  3472         -  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
  3473         -     return 0;         /* Restriction (6)  */
  3474         -  }
  3475   3469     if( p->pOrderBy && pSub->pOrderBy ){
  3476   3470        return 0;                                           /* Restriction (11) */
  3477   3471     }
  3478   3472     if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
  3479   3473     if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
  3480   3474     if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
  3481   3475        return 0;         /* Restriction (21) */
  3482   3476     }
  3483         -  testcase( pSub->selFlags & SF_Recursive );
  3484         -  testcase( pSub->selFlags & SF_MinMaxAgg );
  3485         -  if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
  3486         -    return 0; /* Restrictions (22) and (24) */
  3487         -  }
  3488         -  if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
  3489         -    return 0; /* Restriction (23) */
         3477  +  if( pSub->selFlags & (SF_Recursive) ){
         3478  +    return 0; /* Restrictions (22) */
  3490   3479     }
  3491   3480   
  3492   3481     /*
  3493   3482     ** If the subquery is the right operand of a LEFT JOIN, then the
  3494         -  ** subquery may not be a join itself.  Example of why this is not allowed:
         3483  +  ** subquery may not be a join itself (3a). Example of why this is not
         3484  +  ** allowed:
  3495   3485     **
  3496   3486     **         t1 LEFT OUTER JOIN (t2 JOIN t3)
  3497   3487     **
  3498   3488     ** If we flatten the above, we would get
  3499   3489     **
  3500   3490     **         (t1 LEFT OUTER JOIN t2) JOIN t3
  3501   3491     **
  3502   3492     ** which is not at all the same thing.
  3503   3493     **
  3504   3494     ** If the subquery is the right operand of a LEFT JOIN, then the outer
  3505         -  ** query cannot be an aggregate.  This is an artifact of the way aggregates
  3506         -  ** are processed - there is no mechanism to determine if the LEFT JOIN
  3507         -  ** table should be all-NULL.
         3495  +  ** query cannot be an aggregate. (3c)  This is an artifact of the way
         3496  +  ** aggregates are processed - there is no mechanism to determine if
         3497  +  ** the LEFT JOIN table should be all-NULL.
  3508   3498     **
  3509   3499     ** See also tickets #306, #350, and #3300.
  3510   3500     */
  3511   3501     if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
  3512   3502       isLeftJoin = 1;
  3513   3503       if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
  3514         -      return 0; /* Restriction (3) */
         3504  +      /*  (3a)             (3c)     (3b) */
         3505  +      return 0;
  3515   3506       }
  3516   3507     }
  3517   3508   #ifdef SQLITE_EXTRA_IFNULLROW
  3518   3509     else if( iFrom>0 && !isAgg ){
  3519   3510       /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
  3520         -    ** every reference to any result column from subquery in a join, even though
  3521         -    ** they are not necessary.  This will stress-test the OP_IfNullRow opcode. */
         3511  +    ** every reference to any result column from subquery in a join, even
         3512  +    ** though they are not necessary.  This will stress-test the OP_IfNullRow 
         3513  +    ** opcode. */
  3522   3514       isLeftJoin = -1;
  3523   3515     }
  3524   3516   #endif
  3525   3517   
  3526         -  /* Restriction 17: If the sub-query is a compound SELECT, then it must
         3518  +  /* Restriction (17): If the sub-query is a compound SELECT, then it must
  3527   3519     ** use only the UNION ALL operator. And none of the simple select queries
  3528   3520     ** that make up the compound SELECT are allowed to be aggregate or distinct
  3529   3521     ** queries.
  3530   3522     */
  3531   3523     if( pSub->pPrior ){
  3532   3524       if( pSub->pOrderBy ){
  3533         -      return 0;  /* Restriction 20 */
         3525  +      return 0;  /* Restriction (20) */
  3534   3526       }
  3535   3527       if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
  3536         -      return 0;
         3528  +      return 0; /* (17d1), (17d2), or (17d3) */
  3537   3529       }
  3538   3530       for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
  3539   3531         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
  3540   3532         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
  3541   3533         assert( pSub->pSrc!=0 );
  3542   3534         assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
  3543         -      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
  3544         -       || (pSub1->pPrior && pSub1->op!=TK_ALL) 
  3545         -       || pSub1->pSrc->nSrc<1
         3535  +      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0    /* (17b) */
         3536  +       || (pSub1->pPrior && pSub1->op!=TK_ALL)                 /* (17a) */
         3537  +       || pSub1->pSrc->nSrc<1                                  /* (17c) */
  3546   3538         ){
  3547   3539           return 0;
  3548   3540         }
  3549   3541         testcase( pSub1->pSrc->nSrc>1 );
  3550   3542       }
  3551   3543   
  3552         -    /* Restriction 18. */
         3544  +    /* Restriction (18). */
  3553   3545       if( p->pOrderBy ){
  3554   3546         int ii;
  3555   3547         for(ii=0; ii<p->pOrderBy->nExpr; ii++){
  3556   3548           if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
  3557   3549         }
  3558   3550       }
  3559   3551     }
  3560   3552   
         3553  +  /* Ex-restriction (23):
         3554  +  ** The only way that the recursive part of a CTE can contain a compound
         3555  +  ** subquery is for the subquery to be one term of a join.  But if the
         3556  +  ** subquery is a join, then the flattening has already been stopped by
         3557  +  ** restriction (17d3)
         3558  +  */
         3559  +  assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
         3560  +
  3561   3561     /***** If we reach this point, flattening is permitted. *****/
  3562   3562     SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
  3563   3563                      pSub->zSelName, pSub, iFrom));
  3564   3564   
  3565   3565     /* Authorize the subquery */
  3566   3566     pParse->zAuthContext = pSubitem->zName;
  3567   3567     TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
................................................................................
  3766   3766         pParent->pOrderBy = pOrderBy;
  3767   3767         pSub->pOrderBy = 0;
  3768   3768       }
  3769   3769       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
  3770   3770       if( isLeftJoin>0 ){
  3771   3771         setJoinExpr(pWhere, iNewParent);
  3772   3772       }
  3773         -    if( subqueryIsAgg ){
  3774         -      assert( pParent->pHaving==0 );
  3775         -      pParent->pHaving = pParent->pWhere;
  3776         -      pParent->pWhere = pWhere;
  3777         -      pParent->pHaving = sqlite3ExprAnd(db, 
  3778         -          sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
  3779         -      );
  3780         -      assert( pParent->pGroupBy==0 );
  3781         -      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
  3782         -    }else{
  3783         -      pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
  3784         -    }
         3773  +    pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
  3785   3774       if( db->mallocFailed==0 ){
  3786   3775         SubstContext x;
  3787   3776         x.pParse = pParse;
  3788   3777         x.iTable = iParent;
  3789   3778         x.iNewTable = iNewParent;
  3790   3779         x.isLeftJoin = isLeftJoin;
  3791   3780         x.pEList = pSub->pEList;
................................................................................
  3840   3829   **     WHERE x=5 AND y=10;
  3841   3830   **
  3842   3831   ** The hope is that the terms added to the inner query will make it more
  3843   3832   ** efficient.
  3844   3833   **
  3845   3834   ** Do not attempt this optimization if:
  3846   3835   **
  3847         -**   (1) The inner query is an aggregate.  (In that case, we'd really want
  3848         -**       to copy the outer WHERE-clause terms onto the HAVING clause of the
  3849         -**       inner query.  But they probably won't help there so do not bother.)
         3836  +**   (1) (** This restriction was removed on 2017-09-29.  We used to
         3837  +**           disallow this optimization for aggregate subqueries, but now
         3838  +**           it is allowed by putting the extra terms on the HAVING clause.
         3839  +**           The added HAVING clause is pointless if the subquery lacks
         3840  +**           a GROUP BY clause.  But such a HAVING clause is also harmless
         3841  +**           so there does not appear to be any reason to add extra logic
         3842  +**           to suppress it. **)
  3850   3843   **
  3851   3844   **   (2) The inner query is the recursive part of a common table expression.
  3852   3845   **
  3853   3846   **   (3) The inner query has a LIMIT clause (since the changes to the WHERE
  3854   3847   **       close would change the meaning of the LIMIT).
  3855   3848   **
  3856   3849   **   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
................................................................................
  3867   3860     Parse *pParse,        /* Parse context (for malloc() and error reporting) */
  3868   3861     Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
  3869   3862     Expr *pWhere,         /* The WHERE clause of the outer query */
  3870   3863     int iCursor           /* Cursor number of the subquery */
  3871   3864   ){
  3872   3865     Expr *pNew;
  3873   3866     int nChng = 0;
  3874         -  Select *pX;           /* For looping over compound SELECTs in pSubq */
  3875   3867     if( pWhere==0 ) return 0;
  3876         -  for(pX=pSubq; pX; pX=pX->pPrior){
  3877         -    if( (pX->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
  3878         -      testcase( pX->selFlags & SF_Aggregate );
  3879         -      testcase( pX->selFlags & SF_Recursive );
  3880         -      testcase( pX!=pSubq );
  3881         -      return 0; /* restrictions (1) and (2) */
         3868  +  if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
         3869  +
         3870  +#ifdef SQLITE_DEBUG
         3871  +  /* Only the first term of a compound can have a WITH clause.  But make
         3872  +  ** sure no other terms are marked SF_Recursive in case something changes
         3873  +  ** in the future.
         3874  +  */
         3875  +  {
         3876  +    Select *pX;  
         3877  +    for(pX=pSubq; pX; pX=pX->pPrior){
         3878  +      assert( (pX->selFlags & (SF_Recursive))==0 );
  3882   3879       }
  3883   3880     }
         3881  +#endif
         3882  +
  3884   3883     if( pSubq->pLimit!=0 ){
  3885   3884       return 0; /* restriction (3) */
  3886   3885     }
  3887   3886     while( pWhere->op==TK_AND ){
  3888   3887       nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
  3889   3888       pWhere = pWhere->pLeft;
  3890   3889     }
  3891         -  if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */
         3890  +  if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction (5) */
  3892   3891     if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
  3893   3892       nChng++;
  3894   3893       while( pSubq ){
  3895   3894         SubstContext x;
  3896   3895         pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
  3897   3896         x.pParse = pParse;
  3898   3897         x.iTable = iCursor;
  3899   3898         x.iNewTable = iCursor;
  3900   3899         x.isLeftJoin = 0;
  3901   3900         x.pEList = pSubq->pEList;
  3902   3901         pNew = substExpr(&x, pNew);
  3903         -      pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
         3902  +      if( pSubq->selFlags & SF_Aggregate ){
         3903  +        pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
         3904  +      }else{
         3905  +        pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
         3906  +      }
  3904   3907         pSubq = pSubq->pPrior;
  3905   3908       }
  3906   3909     }
  3907   3910     return nChng;
  3908   3911   }
  3909   3912   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3910   3913   
  3911   3914   /*
  3912         -** Based on the contents of the AggInfo structure indicated by the first
  3913         -** argument, this function checks if the following are true:
  3914         -**
  3915         -**    * the query contains just a single aggregate function,
  3916         -**    * the aggregate function is either min() or max(), and
  3917         -**    * the argument to the aggregate function is a column value.
  3918         -**
  3919         -** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
  3920         -** is returned as appropriate. Also, *ppMinMax is set to point to the 
  3921         -** list of arguments passed to the aggregate before returning.
  3922         -**
  3923         -** Or, if the conditions above are not met, *ppMinMax is set to 0 and
  3924         -** WHERE_ORDERBY_NORMAL is returned.
  3925         -*/
  3926         -static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
  3927         -  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
  3928         -
  3929         -  *ppMinMax = 0;
  3930         -  if( pAggInfo->nFunc==1 ){
  3931         -    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
  3932         -    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
  3933         -
  3934         -    assert( pExpr->op==TK_AGG_FUNCTION );
  3935         -    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
  3936         -      const char *zFunc = pExpr->u.zToken;
  3937         -      if( sqlite3StrICmp(zFunc, "min")==0 ){
  3938         -        eRet = WHERE_ORDERBY_MIN;
  3939         -        *ppMinMax = pEList;
  3940         -      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
  3941         -        eRet = WHERE_ORDERBY_MAX;
  3942         -        *ppMinMax = pEList;
  3943         -      }
  3944         -    }
  3945         -  }
  3946         -
  3947         -  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
         3915  +** The pFunc is the only aggregate function in the query.  Check to see
         3916  +** if the query is a candidate for the min/max optimization. 
         3917  +**
         3918  +** If the query is a candidate for the min/max optimization, then set
         3919  +** *ppMinMax to be an ORDER BY clause to be used for the optimization
         3920  +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
         3921  +** whether pFunc is a min() or max() function.
         3922  +**
         3923  +** If the query is not a candidate for the min/max optimization, return
         3924  +** WHERE_ORDERBY_NORMAL (which must be zero).
         3925  +**
         3926  +** This routine must be called after aggregate functions have been
         3927  +** located but before their arguments have been subjected to aggregate
         3928  +** analysis.
         3929  +*/
         3930  +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
         3931  +  int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
         3932  +  ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
         3933  +  const char *zFunc;                    /* Name of aggregate function pFunc */
         3934  +  ExprList *pOrderBy;
         3935  +  u8 sortOrder;
         3936  +
         3937  +  assert( *ppMinMax==0 );
         3938  +  assert( pFunc->op==TK_AGG_FUNCTION );
         3939  +  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
         3940  +  zFunc = pFunc->u.zToken;
         3941  +  if( sqlite3StrICmp(zFunc, "min")==0 ){
         3942  +    eRet = WHERE_ORDERBY_MIN;
         3943  +    sortOrder = SQLITE_SO_ASC;
         3944  +  }else if( sqlite3StrICmp(zFunc, "max")==0 ){
         3945  +    eRet = WHERE_ORDERBY_MAX;
         3946  +    sortOrder = SQLITE_SO_DESC;
         3947  +  }else{
         3948  +    return eRet;
         3949  +  }
         3950  +  *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
         3951  +  assert( pOrderBy!=0 || db->mallocFailed );
         3952  +  if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
  3948   3953     return eRet;
  3949   3954   }
  3950   3955   
  3951   3956   /*
  3952   3957   ** The select statement passed as the first argument is an aggregate query.
  3953   3958   ** The second argument is the associated aggregate-info object. This 
  3954   3959   ** function tests if the SELECT is of the form:
................................................................................
  4224   4229       /* Only one recursive reference is permitted. */ 
  4225   4230       if( pTab->nTabRef>2 ){
  4226   4231         sqlite3ErrorMsg(
  4227   4232             pParse, "multiple references to recursive table: %s", pCte->zName
  4228   4233         );
  4229   4234         return SQLITE_ERROR;
  4230   4235       }
  4231         -    assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
         4236  +    assert( pTab->nTabRef==1 || 
         4237  +            ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
  4232   4238   
  4233   4239       pCte->zCteErr = "circular reference: %s";
  4234   4240       pSavedWith = pParse->pWith;
  4235   4241       pParse->pWith = pWith;
  4236   4242       if( bMayRecursive ){
  4237   4243         Select *pPrior = pSel->pPrior;
  4238   4244         assert( pPrior->pWith==0 );
................................................................................
  4281   4287   **
  4282   4288   ** This function is used as the xSelectCallback2() callback by
  4283   4289   ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
  4284   4290   ** names and other FROM clause elements. 
  4285   4291   */
  4286   4292   static void selectPopWith(Walker *pWalker, Select *p){
  4287   4293     Parse *pParse = pWalker->pParse;
  4288         -  if( pParse->pWith && p->pPrior==0 ){
         4294  +  if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
  4289   4295       With *pWith = findRightmost(p)->pWith;
  4290   4296       if( pWith!=0 ){
  4291   4297         assert( pParse->pWith==pWith );
  4292   4298         pParse->pWith = pWith->pOuter;
  4293   4299       }
  4294   4300     }
  4295   4301   }
................................................................................
  4326   4332     int i, j, k;
  4327   4333     SrcList *pTabList;
  4328   4334     ExprList *pEList;
  4329   4335     struct SrcList_item *pFrom;
  4330   4336     sqlite3 *db = pParse->db;
  4331   4337     Expr *pE, *pRight, *pExpr;
  4332   4338     u16 selFlags = p->selFlags;
         4339  +  u32 elistFlags = 0;
  4333   4340   
  4334   4341     p->selFlags |= SF_Expanded;
  4335   4342     if( db->mallocFailed  ){
  4336   4343       return WRC_Abort;
  4337   4344     }
  4338         -  if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
         4345  +  assert( p->pSrc!=0 );
         4346  +  if( (selFlags & SF_Expanded)!=0 ){
  4339   4347       return WRC_Prune;
  4340   4348     }
  4341   4349     pTabList = p->pSrc;
  4342   4350     pEList = p->pEList;
  4343         -  if( p->pWith ){
         4351  +  if( OK_IF_ALWAYS_TRUE(p->pWith) ){
  4344   4352       sqlite3WithPush(pParse, p->pWith, 0);
  4345   4353     }
  4346   4354   
  4347   4355     /* Make sure cursor numbers have been assigned to all entries in
  4348   4356     ** the FROM clause of the SELECT statement.
  4349   4357     */
  4350   4358     sqlite3SrcListAssignCursors(pParse, pTabList);
................................................................................
  4368   4376         /* A sub-query in the FROM clause of a SELECT */
  4369   4377         assert( pSel!=0 );
  4370   4378         assert( pFrom->pTab==0 );
  4371   4379         if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
  4372   4380         pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
  4373   4381         if( pTab==0 ) return WRC_Abort;
  4374   4382         pTab->nTabRef = 1;
  4375         -      pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
         4383  +      if( pFrom->zAlias ){
         4384  +        pTab->zName = sqlite3DbStrDup(db, pFrom->zAlias);
         4385  +      }else{
         4386  +        pTab->zName = sqlite3MPrintf(db, "subquery_%p", (void*)pTab);
         4387  +      }
  4376   4388         while( pSel->pPrior ){ pSel = pSel->pPrior; }
  4377   4389         sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
  4378   4390         pTab->iPKey = -1;
  4379   4391         pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  4380   4392         pTab->tabFlags |= TF_Ephemeral;
  4381   4393   #endif
  4382   4394       }else{
................................................................................
  4434   4446     */
  4435   4447     for(k=0; k<pEList->nExpr; k++){
  4436   4448       pE = pEList->a[k].pExpr;
  4437   4449       if( pE->op==TK_ASTERISK ) break;
  4438   4450       assert( pE->op!=TK_DOT || pE->pRight!=0 );
  4439   4451       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
  4440   4452       if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
         4453  +    elistFlags |= pE->flags;
  4441   4454     }
  4442   4455     if( k<pEList->nExpr ){
  4443   4456       /*
  4444   4457       ** If we get here it means the result set contains one or more "*"
  4445   4458       ** operators that need to be expanded.  Loop through each expression
  4446   4459       ** in the result set and expand them one by one.
  4447   4460       */
................................................................................
  4449   4462       ExprList *pNew = 0;
  4450   4463       int flags = pParse->db->flags;
  4451   4464       int longNames = (flags & SQLITE_FullColNames)!=0
  4452   4465                         && (flags & SQLITE_ShortColNames)==0;
  4453   4466   
  4454   4467       for(k=0; k<pEList->nExpr; k++){
  4455   4468         pE = a[k].pExpr;
         4469  +      elistFlags |= pE->flags;
  4456   4470         pRight = pE->pRight;
  4457   4471         assert( pE->op!=TK_DOT || pRight!=0 );
  4458   4472         if( pE->op!=TK_ASTERISK
  4459   4473          && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
  4460   4474         ){
  4461   4475           /* This particular expression does not need to be expanded.
  4462   4476           */
................................................................................
  4578   4592             }
  4579   4593           }
  4580   4594         }
  4581   4595       }
  4582   4596       sqlite3ExprListDelete(db, pEList);
  4583   4597       p->pEList = pNew;
  4584   4598     }
  4585         -#if SQLITE_MAX_COLUMN
  4586         -  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  4587         -    sqlite3ErrorMsg(pParse, "too many columns in result set");
  4588         -    return WRC_Abort;
         4599  +  if( p->pEList ){
         4600  +    if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
         4601  +      sqlite3ErrorMsg(pParse, "too many columns in result set");
         4602  +      return WRC_Abort;
         4603  +    }
         4604  +    if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
         4605  +      p->selFlags |= SF_ComplexResult;
         4606  +    }
  4589   4607     }
  4590         -#endif
  4591   4608     return WRC_Continue;
  4592   4609   }
  4593   4610   
  4594   4611   /*
  4595   4612   ** No-op routine for the parse-tree walker.
  4596   4613   **
  4597   4614   ** When this routine is the Walker.xExprCallback then expression trees
................................................................................
  4637   4654   ** The calling function can detect the problem by looking at pParse->nErr
  4638   4655   ** and/or pParse->db->mallocFailed.
  4639   4656   */
  4640   4657   static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
  4641   4658     Walker w;
  4642   4659     w.xExprCallback = sqlite3ExprWalkNoop;
  4643   4660     w.pParse = pParse;
  4644         -  if( pParse->hasCompound ){
         4661  +  if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
  4645   4662       w.xSelectCallback = convertCompoundSelectToSubquery;
  4646   4663       w.xSelectCallback2 = 0;
  4647   4664       sqlite3WalkSelect(&w, pSelect);
  4648   4665     }
  4649   4666     w.xSelectCallback = selectExpander;
  4650   4667     w.xSelectCallback2 = selectPopWith;
  4651   4668     sqlite3WalkSelect(&w, pSelect);
................................................................................
  4725   4742   ** This routine acts recursively on all subqueries within the SELECT.
  4726   4743   */
  4727   4744   void sqlite3SelectPrep(
  4728   4745     Parse *pParse,         /* The parser context */
  4729   4746     Select *p,             /* The SELECT statement being coded. */
  4730   4747     NameContext *pOuterNC  /* Name context for container */
  4731   4748   ){
  4732         -  sqlite3 *db;
  4733         -  if( NEVER(p==0) ) return;
  4734         -  db = pParse->db;
  4735         -  if( db->mallocFailed ) return;
         4749  +  assert( p!=0 || pParse->db->mallocFailed );
         4750  +  if( pParse->db->mallocFailed ) return;
  4736   4751     if( p->selFlags & SF_HasTypeInfo ) return;
  4737   4752     sqlite3SelectExpand(pParse, p);
  4738         -  if( pParse->nErr || db->mallocFailed ) return;
         4753  +  if( pParse->nErr || pParse->db->mallocFailed ) return;
  4739   4754     sqlite3ResolveSelectNames(pParse, p, pOuterNC);
  4740         -  if( pParse->nErr || db->mallocFailed ) return;
         4755  +  if( pParse->nErr || pParse->db->mallocFailed ) return;
  4741   4756     sqlite3SelectAddTypeInfo(pParse, p);
  4742   4757   }
  4743   4758   
  4744   4759   /*
  4745   4760   ** Reset the aggregate accumulator.
  4746   4761   **
  4747   4762   ** The aggregate accumulator is a set of memory cells that hold
................................................................................
  5028   5043   ** Return TRUE if the optimization is undertaken.
  5029   5044   */
  5030   5045   static int countOfViewOptimization(Parse *pParse, Select *p){
  5031   5046     Select *pSub, *pPrior;
  5032   5047     Expr *pExpr;
  5033   5048     Expr *pCount;
  5034   5049     sqlite3 *db;
  5035         -  if( (p->selFlags & SF_Aggregate)==0 ) return 0;   /* This is an aggregate query */
         5050  +  if( (p->selFlags & SF_Aggregate)==0 ) return 0;   /* This is an aggregate */
  5036   5051     if( p->pEList->nExpr!=1 ) return 0;               /* Single result column */
  5037   5052     pExpr = p->pEList->a[0].pExpr;
  5038   5053     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;        /* Result is an aggregate */
  5039         -  if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0;  /* Must be count() */
         5054  +  if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0;  /* Is count() */
  5040   5055     if( pExpr->x.pList!=0 ) return 0;                 /* Must be count(*) */
  5041         -  if( p->pSrc->nSrc!=1 ) return 0;                  /* One table in the FROM clause */
         5056  +  if( p->pSrc->nSrc!=1 ) return 0;                  /* One table in FROM  */
  5042   5057     pSub = p->pSrc->a[0].pSelect;
  5043   5058     if( pSub==0 ) return 0;                           /* The FROM is a subquery */
  5044         -  if( pSub->pPrior==0 ) return 0;                   /* Must be a compound subquery */
         5059  +  if( pSub->pPrior==0 ) return 0;                   /* Must be a compound ry */
  5045   5060     do{
  5046   5061       if( pSub->op!=TK_ALL && pSub->pPrior ) return 0;  /* Must be UNION ALL */
  5047   5062       if( pSub->pWhere ) return 0;                      /* No WHERE clause */
  5048   5063       if( pSub->selFlags & SF_Aggregate ) return 0;     /* Not an aggregate */
  5049         -    pSub = pSub->pPrior;                              /* Repeat over compound terms */
         5064  +    pSub = pSub->pPrior;                              /* Repeat over compound */
  5050   5065     }while( pSub );
  5051   5066   
  5052         -  /* If we reach this point, that means it is OK to perform the transformation */
         5067  +  /* If we reach this point then it is OK to perform the transformation */
  5053   5068   
  5054   5069     db = pParse->db;
  5055   5070     pCount = pExpr;
  5056   5071     pExpr = 0;
  5057   5072     pSub = p->pSrc->a[0].pSelect;
  5058   5073     p->pSrc->a[0].pSelect = 0;
  5059   5074     sqlite3SrcListDelete(db, p->pSrc);
................................................................................
  5120   5135     Expr *pHaving;         /* The HAVING clause.  May be NULL */
  5121   5136     int rc = 1;            /* Value to return from this function */
  5122   5137     DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
  5123   5138     SortCtx sSort;         /* Info on how to code the ORDER BY clause */
  5124   5139     AggInfo sAggInfo;      /* Information used by aggregate queries */
  5125   5140     int iEnd;              /* Address of the end of the query */
  5126   5141     sqlite3 *db;           /* The database connection */
         5142  +  ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
         5143  +  u8 minMaxFlag;                 /* Flag for min/max queries */
  5127   5144   
  5128   5145   #ifndef SQLITE_OMIT_EXPLAIN
  5129   5146     int iRestoreSelectId = pParse->iSelectId;
  5130   5147     pParse->iSelectId = pParse->iNextSelectId++;
  5131   5148   #endif
  5132   5149   
  5133   5150     db = pParse->db;
................................................................................
  5185   5202   
  5186   5203     /* Try to flatten subqueries in the FROM clause up into the main query
  5187   5204     */
  5188   5205   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  5189   5206     for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
  5190   5207       struct SrcList_item *pItem = &pTabList->a[i];
  5191   5208       Select *pSub = pItem->pSelect;
  5192         -    int isAggSub;
  5193   5209       Table *pTab = pItem->pTab;
  5194   5210       if( pSub==0 ) continue;
  5195   5211   
  5196   5212       /* Catch mismatch in the declared columns of a view and the number of
  5197   5213       ** columns in the SELECT on the RHS */
  5198   5214       if( pTab->nCol!=pSub->pEList->nExpr ){
  5199   5215         sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
  5200   5216                         pTab->nCol, pTab->zName, pSub->pEList->nExpr);
  5201   5217         goto select_end;
  5202   5218       }
  5203   5219   
  5204         -    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
  5205         -    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
         5220  +    /* Do not try to flatten an aggregate subquery.
         5221  +    **
         5222  +    ** Flattening an aggregate subquery is only possible if the outer query
         5223  +    ** is not a join.  But if the outer query is not a join, then the subquery
         5224  +    ** will be implemented as a co-routine and there is no advantage to
         5225  +    ** flattening in that case.
         5226  +    */
         5227  +    if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
         5228  +    assert( pSub->pGroupBy==0 );
         5229  +
         5230  +    /* If the outer query contains a "complex" result set (that is,
         5231  +    ** if the result set of the outer query uses functions or subqueries)
         5232  +    ** and if the subquery contains an ORDER BY clause and if
         5233  +    ** it will be implemented as a co-routine, then do not flatten.  This
         5234  +    ** restriction allows SQL constructs like this:
         5235  +    **
         5236  +    **  SELECT expensive_function(x)
         5237  +    **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
         5238  +    **
         5239  +    ** The expensive_function() is only computed on the 10 rows that
         5240  +    ** are output, rather than every row of the table.
         5241  +    **
         5242  +    ** The requirement that the outer query have a complex result set
         5243  +    ** means that flattening does occur on simpler SQL constraints without
         5244  +    ** the expensive_function() like:
         5245  +    **
         5246  +    **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
         5247  +    */
         5248  +    if( pSub->pOrderBy!=0
         5249  +     && i==0
         5250  +     && (p->selFlags & SF_ComplexResult)!=0
         5251  +     && (pTabList->nSrc==1
         5252  +         || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
         5253  +    ){
         5254  +      continue;
         5255  +    }
         5256  +
         5257  +    if( flattenSubquery(pParse, p, i, isAgg) ){
  5206   5258         /* This subquery can be absorbed into its parent. */
  5207         -      if( isAggSub ){
  5208         -        isAgg = 1;
  5209         -        p->selFlags |= SF_Aggregate;
  5210         -      }
  5211   5259         i = -1;
  5212   5260       }
  5213   5261       pTabList = p->pSrc;
  5214   5262       if( db->mallocFailed ) goto select_end;
  5215   5263       if( !IgnorableOrderby(pDest) ){
  5216   5264         sSort.pOrderBy = p->pOrderBy;
  5217   5265       }
................................................................................
  5237   5285     ** (1) Authorized unreferenced tables
  5238   5286     ** (2) Generate code for all sub-queries
  5239   5287     */
  5240   5288     for(i=0; i<pTabList->nSrc; i++){
  5241   5289       struct SrcList_item *pItem = &pTabList->a[i];
  5242   5290       SelectDest dest;
  5243   5291       Select *pSub;
         5292  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
         5293  +    const char *zSavedAuthContext;
         5294  +#endif
  5244   5295   
  5245         -    /* Issue SQLITE_READ authorizations with a fake column name for any tables that
  5246         -    ** are referenced but from which no values are extracted. Examples of where these
  5247         -    ** kinds of null SQLITE_READ authorizations would occur:
         5296  +    /* Issue SQLITE_READ authorizations with a fake column name for any
         5297  +    ** tables that are referenced but from which no values are extracted.
         5298  +    ** Examples of where these kinds of null SQLITE_READ authorizations
         5299  +    ** would occur:
  5248   5300       **
  5249   5301       **     SELECT count(*) FROM t1;   -- SQLITE_READ t1.""
  5250   5302       **     SELECT t1.* FROM t1, t2;   -- SQLITE_READ t2.""
  5251   5303       **
  5252   5304       ** The fake column name is an empty string.  It is possible for a table to
  5253   5305       ** have a column named by the empty string, in which case there is no way to
  5254   5306       ** distinguish between an unreferenced table and an actual reference to the
  5255         -    ** "" column.  The original design was for the fake column name to be a NULL,
         5307  +    ** "" column. The original design was for the fake column name to be a NULL,
  5256   5308       ** which would be unambiguous.  But legacy authorization callbacks might
  5257         -    ** assume the column name is non-NULL and segfault.  The use of an empty string
  5258         -    ** for the fake column name seems safer.
         5309  +    ** assume the column name is non-NULL and segfault.  The use of an empty
         5310  +    ** string for the fake column name seems safer.
  5259   5311       */
  5260   5312       if( pItem->colUsed==0 ){
  5261   5313         sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
  5262   5314       }
  5263   5315   
  5264   5316   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  5265   5317       /* Generate code for all sub-queries in the FROM clause
................................................................................
  5302   5354   #if SELECTTRACE_ENABLED
  5303   5355         if( sqlite3SelectTrace & 0x100 ){
  5304   5356           SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
  5305   5357           sqlite3TreeViewSelect(0, p, 0);
  5306   5358         }
  5307   5359   #endif
  5308   5360       }
         5361  +
         5362  +    zSavedAuthContext = pParse->zAuthContext;
         5363  +    pParse->zAuthContext = pItem->zName;
  5309   5364   
  5310   5365       /* Generate code to implement the subquery
  5311   5366       **
  5312         -    ** The subquery is implemented as a co-routine if all of these are true:
  5313         -    **   (1)  The subquery is guaranteed to be the outer loop (so that it
  5314         -    **        does not need to be computed more than once)
  5315         -    **   (2)  The ALL keyword after SELECT is omitted.  (Applications are
  5316         -    **        allowed to say "SELECT ALL" instead of just "SELECT" to disable
  5317         -    **        the use of co-routines.)
  5318         -    **   (3)  Co-routines are not disabled using sqlite3_test_control()
  5319         -    **        with SQLITE_TESTCTRL_OPTIMIZATIONS.
         5367  +    ** The subquery is implemented as a co-routine if the subquery is
         5368  +    ** guaranteed to be the outer loop (so that it does not need to be
         5369  +    ** computed more than once)
  5320   5370       **
  5321   5371       ** TODO: Are there other reasons beside (1) to use a co-routine
  5322   5372       ** implementation?
  5323   5373       */
  5324   5374       if( i==0
  5325   5375        && (pTabList->nSrc==1
  5326   5376               || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)  /* (1) */
  5327         -     && (p->selFlags & SF_All)==0                                   /* (2) */
  5328         -     && OptimizationEnabled(db, SQLITE_SubqCoroutine)               /* (3) */
  5329   5377       ){
  5330   5378         /* Implement a co-routine that will return a single row of the result
  5331   5379         ** set on each invocation.
  5332   5380         */
  5333   5381         int addrTop = sqlite3VdbeCurrentAddr(v)+1;
         5382  +     
  5334   5383         pItem->regReturn = ++pParse->nMem;
  5335   5384         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
  5336   5385         VdbeComment((v, "%s", pItem->pTab->zName));
  5337   5386         pItem->addrFillSub = addrTop;
  5338   5387         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  5339   5388         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  5340   5389         sqlite3Select(pParse, pSub, &dest);
................................................................................
  5384   5433         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
  5385   5434         VdbeComment((v, "end %s", pItem->pTab->zName));
  5386   5435         sqlite3VdbeChangeP1(v, topAddr, retAddr);
  5387   5436         sqlite3ClearTempRegCache(pParse);
  5388   5437       }
  5389   5438       if( db->mallocFailed ) goto select_end;
  5390   5439       pParse->nHeight -= sqlite3SelectExprHeight(p);
         5440  +    pParse->zAuthContext = zSavedAuthContext;
  5391   5441   #endif
  5392   5442     }
  5393   5443   
  5394   5444     /* Various elements of the SELECT copied into local variables for
  5395   5445     ** convenience */
  5396   5446     pEList = p->pEList;
  5397   5447     pWhere = p->pWhere;
................................................................................
  5612   5662           assert( pWhere==p->pWhere );
  5613   5663           havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
  5614   5664           pWhere = p->pWhere;
  5615   5665         }
  5616   5666         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
  5617   5667       }
  5618   5668       sAggInfo.nAccumulator = sAggInfo.nColumn;
         5669  +    if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
         5670  +      minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
         5671  +    }else{
         5672  +      minMaxFlag = WHERE_ORDERBY_NORMAL;
         5673  +    }
  5619   5674       for(i=0; i<sAggInfo.nFunc; i++){
  5620   5675         assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
  5621   5676         sNC.ncFlags |= NC_InAggFunc;
  5622   5677         sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
  5623   5678         sNC.ncFlags &= ~NC_InAggFunc;
  5624   5679       }
  5625   5680       sAggInfo.mxReg = pParse->nMem;
  5626   5681       if( db->mallocFailed ) goto select_end;
         5682  +#if SELECTTRACE_ENABLED
         5683  +    if( sqlite3SelectTrace & 0x400 ){
         5684  +      int ii;
         5685  +      SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
         5686  +      sqlite3TreeViewSelect(0, p, 0);
         5687  +      for(ii=0; ii<sAggInfo.nColumn; ii++){
         5688  +        sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
         5689  +            ii, sAggInfo.aCol[ii].iMem);
         5690  +        sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
         5691  +      }
         5692  +      for(ii=0; ii<sAggInfo.nFunc; ii++){
         5693  +        sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
         5694  +            ii, sAggInfo.aFunc[ii].iMem);
         5695  +        sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
         5696  +      }
         5697  +    }
         5698  +#endif
         5699  +
  5627   5700   
  5628   5701       /* Processing for aggregates with GROUP BY is very different and
  5629   5702       ** much more complex than aggregates without a GROUP BY.
  5630   5703       */
  5631   5704       if( pGroupBy ){
  5632   5705         KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
  5633   5706         int addr1;          /* A-vs-B comparision jump */
................................................................................
  5849   5922         */
  5850   5923         sqlite3VdbeResolveLabel(v, addrReset);
  5851   5924         resetAccumulator(pParse, &sAggInfo);
  5852   5925         sqlite3VdbeAddOp1(v, OP_Return, regReset);
  5853   5926        
  5854   5927       } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
  5855   5928       else {
  5856         -      ExprList *pDel = 0;
  5857   5929   #ifndef SQLITE_OMIT_BTREECOUNT
  5858   5930         Table *pTab;
  5859   5931         if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
  5860   5932           /* If isSimpleCount() returns a pointer to a Table structure, then
  5861   5933           ** the SQL statement is of the form:
  5862   5934           **
  5863   5935           **   SELECT count(*) FROM <tbl>
................................................................................
  5911   5983           }
  5912   5984           sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
  5913   5985           sqlite3VdbeAddOp1(v, OP_Close, iCsr);
  5914   5986           explainSimpleCount(pParse, pTab, pBest);
  5915   5987         }else
  5916   5988   #endif /* SQLITE_OMIT_BTREECOUNT */
  5917   5989         {
  5918         -        /* Check if the query is of one of the following forms:
  5919         -        **
  5920         -        **   SELECT min(x) FROM ...
  5921         -        **   SELECT max(x) FROM ...
  5922         -        **
  5923         -        ** If it is, then ask the code in where.c to attempt to sort results
  5924         -        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
  5925         -        ** If where.c is able to produce results sorted in this order, then
  5926         -        ** add vdbe code to break out of the processing loop after the 
  5927         -        ** first iteration (since the first iteration of the loop is 
  5928         -        ** guaranteed to operate on the row with the minimum or maximum 
  5929         -        ** value of x, the only row required).
  5930         -        **
  5931         -        ** A special flag must be passed to sqlite3WhereBegin() to slightly
  5932         -        ** modify behavior as follows:
  5933         -        **
  5934         -        **   + If the query is a "SELECT min(x)", then the loop coded by
  5935         -        **     where.c should not iterate over any values with a NULL value
  5936         -        **     for x.
  5937         -        **
  5938         -        **   + The optimizer code in where.c (the thing that decides which
  5939         -        **     index or indices to use) should place a different priority on 
  5940         -        **     satisfying the 'ORDER BY' clause than it does in other cases.
  5941         -        **     Refer to code and comments in where.c for details.
  5942         -        */
  5943         -        ExprList *pMinMax = 0;
  5944         -        u8 flag = WHERE_ORDERBY_NORMAL;
  5945         -        
  5946         -        assert( p->pGroupBy==0 );
  5947         -        assert( flag==0 );
  5948         -        if( p->pHaving==0 ){
  5949         -          flag = minMaxQuery(&sAggInfo, &pMinMax);
  5950         -        }
  5951         -        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
  5952         -
  5953         -        if( flag ){
  5954         -          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
  5955         -          pDel = pMinMax;
  5956         -          assert( db->mallocFailed || pMinMax!=0 );
  5957         -          if( !db->mallocFailed ){
  5958         -            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
  5959         -            pMinMax->a[0].pExpr->op = TK_COLUMN;
  5960         -          }
  5961         -        }
  5962         -  
  5963   5990           /* This case runs if the aggregate has no GROUP BY clause.  The
  5964   5991           ** processing is much simpler since there is only a single row
  5965   5992           ** of output.
  5966   5993           */
         5994  +        assert( p->pGroupBy==0 );
  5967   5995           resetAccumulator(pParse, &sAggInfo);
  5968         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
         5996  +
         5997  +        /* If this query is a candidate for the min/max optimization, then
         5998  +        ** minMaxFlag will have been previously set to either
         5999  +        ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
         6000  +        ** be an appropriate ORDER BY expression for the optimization.
         6001  +        */
         6002  +        assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
         6003  +        assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
         6004  +
         6005  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
         6006  +                                   0, minMaxFlag, 0);
  5969   6007           if( pWInfo==0 ){
  5970         -          sqlite3ExprListDelete(db, pDel);
  5971   6008             goto select_end;
  5972   6009           }
  5973   6010           updateAccumulator(pParse, &sAggInfo);
  5974         -        assert( pMinMax==0 || pMinMax->nExpr==1 );
  5975   6011           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
  5976   6012             sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
  5977   6013             VdbeComment((v, "%s() by index",
  5978         -                (flag==WHERE_ORDERBY_MIN?"min":"max")));
         6014  +                (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
  5979   6015           }
  5980   6016           sqlite3WhereEnd(pWInfo);
  5981   6017           finalizeAggFunctions(pParse, &sAggInfo);
  5982   6018         }
  5983   6019   
  5984   6020         sSort.pOrderBy = 0;
  5985   6021         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  5986   6022         selectInnerLoop(pParse, p, -1, 0, 0, 
  5987   6023                         pDest, addrEnd, addrEnd);
  5988         -      sqlite3ExprListDelete(db, pDel);
  5989   6024       }
  5990   6025       sqlite3VdbeResolveLabel(v, addrEnd);
  5991   6026       
  5992   6027     } /* endif aggregate query */
  5993   6028   
  5994   6029     if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
  5995   6030       explainTempTable(pParse, "DISTINCT");
................................................................................
  6013   6048     rc = (pParse->nErr>0);
  6014   6049   
  6015   6050     /* Control jumps to here if an error is encountered above, or upon
  6016   6051     ** successful coding of the SELECT.
  6017   6052     */
  6018   6053   select_end:
  6019   6054     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  6020         -
         6055  +  sqlite3ExprListDelete(db, pMinMaxOrderBy);
  6021   6056     sqlite3DbFree(db, sAggInfo.aCol);
  6022   6057     sqlite3DbFree(db, sAggInfo.aFunc);
  6023   6058   #if SELECTTRACE_ENABLED
  6024   6059     SELECTTRACE(1,pParse,p,("end processing\n"));
  6025   6060     pParse->nSelectIndent--;
  6026   6061   #endif
  6027   6062     return rc;
  6028   6063   }

Deleted src/shell.c.

     1         -/* DO NOT EDIT!
     2         -** This file is automatically generated by the script in the canonical
     3         -** SQLite source tree at tool/mkshellc.tcl.  That script combines source
     4         -** code from various constituent source files of SQLite into this single
     5         -** "shell.c" file used to implement the SQLite command-line shell.
     6         -**
     7         -** Most of the code found below comes from the "src/shell.c.in" file in
     8         -** the canonical SQLite source tree.  That main file contains "INCLUDE"
     9         -** lines that specify other files in the canonical source tree that are
    10         -** inserted to getnerate this complete program source file.
    11         -**
    12         -** The code from multiple files is combined into this single "shell.c"
    13         -** source file to help make the command-line program easier to compile.
    14         -**
    15         -** To modify this program, get a copy of the canonical SQLite source tree,
    16         -** edit the src/shell.c.in" and/or some of the other files that are included
    17         -** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
    18         -*/
    19         -/*
    20         -** 2001 September 15
    21         -**
    22         -** The author disclaims copyright to this source code.  In place of
    23         -** a legal notice, here is a blessing:
    24         -**
    25         -**    May you do good and not evil.
    26         -**    May you find forgiveness for yourself and forgive others.
    27         -**    May you share freely, never taking more than you give.
    28         -**
    29         -*************************************************************************
    30         -** This file contains code to implement the "sqlite" command line
    31         -** utility for accessing SQLite databases.
    32         -*/
    33         -#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
    34         -/* This needs to come before any includes for MSVC compiler */
    35         -#define _CRT_SECURE_NO_WARNINGS
    36         -#endif
    37         -
    38         -/*
    39         -** Warning pragmas copied from msvc.h in the core.
    40         -*/
    41         -#if defined(_MSC_VER)
    42         -#pragma warning(disable : 4054)
    43         -#pragma warning(disable : 4055)
    44         -#pragma warning(disable : 4100)
    45         -#pragma warning(disable : 4127)
    46         -#pragma warning(disable : 4130)
    47         -#pragma warning(disable : 4152)
    48         -#pragma warning(disable : 4189)
    49         -#pragma warning(disable : 4206)
    50         -#pragma warning(disable : 4210)
    51         -#pragma warning(disable : 4232)
    52         -#pragma warning(disable : 4244)
    53         -#pragma warning(disable : 4305)
    54         -#pragma warning(disable : 4306)
    55         -#pragma warning(disable : 4702)
    56         -#pragma warning(disable : 4706)
    57         -#endif /* defined(_MSC_VER) */
    58         -
    59         -/*
    60         -** No support for loadable extensions in VxWorks.
    61         -*/
    62         -#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
    63         -# define SQLITE_OMIT_LOAD_EXTENSION 1
    64         -#endif
    65         -
    66         -/*
    67         -** Enable large-file support for fopen() and friends on unix.
    68         -*/
    69         -#ifndef SQLITE_DISABLE_LFS
    70         -# define _LARGE_FILE       1
    71         -# ifndef _FILE_OFFSET_BITS
    72         -#   define _FILE_OFFSET_BITS 64
    73         -# endif
    74         -# define _LARGEFILE_SOURCE 1
    75         -#endif
    76         -
    77         -#include <stdlib.h>
    78         -#include <string.h>
    79         -#include <stdio.h>
    80         -#include <assert.h>
    81         -#include "sqlite3.h"
    82         -#if SQLITE_USER_AUTHENTICATION
    83         -# include "sqlite3userauth.h"
    84         -#endif
    85         -#include <ctype.h>
    86         -#include <stdarg.h>
    87         -
    88         -#if !defined(_WIN32) && !defined(WIN32)
    89         -# include <signal.h>
    90         -# if !defined(__RTP__) && !defined(_WRS_KERNEL)
    91         -#  include <pwd.h>
    92         -# endif
    93         -# include <unistd.h>
    94         -# include <sys/types.h>
    95         -#endif
    96         -
    97         -#if HAVE_READLINE
    98         -# include <readline/readline.h>
    99         -# include <readline/history.h>
   100         -#endif
   101         -
   102         -#if HAVE_EDITLINE
   103         -# include <editline/readline.h>
   104         -#endif
   105         -
   106         -#if HAVE_EDITLINE || HAVE_READLINE
   107         -
   108         -# define shell_add_history(X) add_history(X)
   109         -# define shell_read_history(X) read_history(X)
   110         -# define shell_write_history(X) write_history(X)
   111         -# define shell_stifle_history(X) stifle_history(X)
   112         -# define shell_readline(X) readline(X)
   113         -
   114         -#elif HAVE_LINENOISE
   115         -
   116         -# include "linenoise.h"
   117         -# define shell_add_history(X) linenoiseHistoryAdd(X)
   118         -# define shell_read_history(X) linenoiseHistoryLoad(X)
   119         -# define shell_write_history(X) linenoiseHistorySave(X)
   120         -# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
   121         -# define shell_readline(X) linenoise(X)
   122         -
   123         -#else
   124         -
   125         -# define shell_read_history(X)
   126         -# define shell_write_history(X)
   127         -# define shell_stifle_history(X)
   128         -
   129         -# define SHELL_USE_LOCAL_GETLINE 1
   130         -#endif
   131         -
   132         -
   133         -#if defined(_WIN32) || defined(WIN32)
   134         -# include <io.h>
   135         -# include <fcntl.h>
   136         -# define isatty(h) _isatty(h)
   137         -# ifndef access
   138         -#  define access(f,m) _access((f),(m))
   139         -# endif
   140         -# undef popen
   141         -# define popen _popen
   142         -# undef pclose
   143         -# define pclose _pclose
   144         -#else
   145         - /* Make sure isatty() has a prototype. */
   146         - extern int isatty(int);
   147         -
   148         -# if !defined(__RTP__) && !defined(_WRS_KERNEL)
   149         -  /* popen and pclose are not C89 functions and so are
   150         -  ** sometimes omitted from the <stdio.h> header */
   151         -   extern FILE *popen(const char*,const char*);
   152         -   extern int pclose(FILE*);
   153         -# else
   154         -#  define SQLITE_OMIT_POPEN 1
   155         -# endif
   156         -#endif
   157         -
   158         -#if defined(_WIN32_WCE)
   159         -/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
   160         - * thus we always assume that we have a console. That can be
   161         - * overridden with the -batch command line option.
   162         - */
   163         -#define isatty(x) 1
   164         -#endif
   165         -
   166         -/* ctype macros that work with signed characters */
   167         -#define IsSpace(X)  isspace((unsigned char)X)
   168         -#define IsDigit(X)  isdigit((unsigned char)X)
   169         -#define ToLower(X)  (char)tolower((unsigned char)X)
   170         -
   171         -#if defined(_WIN32) || defined(WIN32)
   172         -#include <windows.h>
   173         -
   174         -/* string conversion routines only needed on Win32 */
   175         -extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
   176         -extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
   177         -extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
   178         -extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
   179         -#endif
   180         -
   181         -/* On Windows, we normally run with output mode of TEXT so that \n characters
   182         -** are automatically translated into \r\n.  However, this behavior needs
   183         -** to be disabled in some cases (ex: when generating CSV output and when
   184         -** rendering quoted strings that contain \n characters).  The following
   185         -** routines take care of that.
   186         -*/
   187         -#if defined(_WIN32) || defined(WIN32)
   188         -static void setBinaryMode(FILE *file, int isOutput){
   189         -  if( isOutput ) fflush(file);
   190         -  _setmode(_fileno(file), _O_BINARY);
   191         -}
   192         -static void setTextMode(FILE *file, int isOutput){
   193         -  if( isOutput ) fflush(file);
   194         -  _setmode(_fileno(file), _O_TEXT);
   195         -}
   196         -#else
   197         -# define setBinaryMode(X,Y)
   198         -# define setTextMode(X,Y)
   199         -#endif
   200         -
   201         -
   202         -/* True if the timer is enabled */
   203         -static int enableTimer = 0;
   204         -
   205         -/* Return the current wall-clock time */
   206         -static sqlite3_int64 timeOfDay(void){
   207         -  static sqlite3_vfs *clockVfs = 0;
   208         -  sqlite3_int64 t;
   209         -  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
   210         -  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
   211         -    clockVfs->xCurrentTimeInt64(clockVfs, &t);
   212         -  }else{
   213         -    double r;
   214         -    clockVfs->xCurrentTime(clockVfs, &r);
   215         -    t = (sqlite3_int64)(r*86400000.0);
   216         -  }
   217         -  return t;
   218         -}
   219         -
   220         -#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
   221         -#include <sys/time.h>
   222         -#include <sys/resource.h>
   223         -
   224         -/* VxWorks does not support getrusage() as far as we can determine */
   225         -#if defined(_WRS_KERNEL) || defined(__RTP__)
   226         -struct rusage {
   227         -  struct timeval ru_utime; /* user CPU time used */
   228         -  struct timeval ru_stime; /* system CPU time used */
   229         -};
   230         -#define getrusage(A,B) memset(B,0,sizeof(*B))
   231         -#endif
   232         -
   233         -/* Saved resource information for the beginning of an operation */
   234         -static struct rusage sBegin;  /* CPU time at start */
   235         -static sqlite3_int64 iBegin;  /* Wall-clock time at start */
   236         -
   237         -/*
   238         -** Begin timing an operation
   239         -*/
   240         -static void beginTimer(void){
   241         -  if( enableTimer ){
   242         -    getrusage(RUSAGE_SELF, &sBegin);
   243         -    iBegin = timeOfDay();
   244         -  }
   245         -}
   246         -
   247         -/* Return the difference of two time_structs in seconds */
   248         -static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
   249         -  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
   250         -         (double)(pEnd->tv_sec - pStart->tv_sec);
   251         -}
   252         -
   253         -/*
   254         -** Print the timing results.
   255         -*/
   256         -static void endTimer(void){
   257         -  if( enableTimer ){
   258         -    sqlite3_int64 iEnd = timeOfDay();
   259         -    struct rusage sEnd;
   260         -    getrusage(RUSAGE_SELF, &sEnd);
   261         -    printf("Run Time: real %.3f user %f sys %f\n",
   262         -       (iEnd - iBegin)*0.001,
   263         -       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
   264         -       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
   265         -  }
   266         -}
   267         -
   268         -#define BEGIN_TIMER beginTimer()
   269         -#define END_TIMER endTimer()
   270         -#define HAS_TIMER 1
   271         -
   272         -#elif (defined(_WIN32) || defined(WIN32))
   273         -
   274         -/* Saved resource information for the beginning of an operation */
   275         -static HANDLE hProcess;
   276         -static FILETIME ftKernelBegin;
   277         -static FILETIME ftUserBegin;
   278         -static sqlite3_int64 ftWallBegin;
   279         -typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
   280         -                                    LPFILETIME, LPFILETIME);
   281         -static GETPROCTIMES getProcessTimesAddr = NULL;
   282         -
   283         -/*
   284         -** Check to see if we have timer support.  Return 1 if necessary
   285         -** support found (or found previously).
   286         -*/
   287         -static int hasTimer(void){
   288         -  if( getProcessTimesAddr ){
   289         -    return 1;
   290         -  } else {
   291         -    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
   292         -    ** versions. See if the version we are running on has it, and if it
   293         -    ** does, save off a pointer to it and the current process handle.
   294         -    */
   295         -    hProcess = GetCurrentProcess();
   296         -    if( hProcess ){
   297         -      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
   298         -      if( NULL != hinstLib ){
   299         -        getProcessTimesAddr =
   300         -            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
   301         -        if( NULL != getProcessTimesAddr ){
   302         -          return 1;
   303         -        }
   304         -        FreeLibrary(hinstLib);
   305         -      }
   306         -    }
   307         -  }
   308         -  return 0;
   309         -}
   310         -
   311         -/*
   312         -** Begin timing an operation
   313         -*/
   314         -static void beginTimer(void){
   315         -  if( enableTimer && getProcessTimesAddr ){
   316         -    FILETIME ftCreation, ftExit;
   317         -    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
   318         -                        &ftKernelBegin,&ftUserBegin);
   319         -    ftWallBegin = timeOfDay();
   320         -  }
   321         -}
   322         -
   323         -/* Return the difference of two FILETIME structs in seconds */
   324         -static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
   325         -  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
   326         -  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
   327         -  return (double) ((i64End - i64Start) / 10000000.0);
   328         -}
   329         -
   330         -/*
   331         -** Print the timing results.
   332         -*/
   333         -static void endTimer(void){
   334         -  if( enableTimer && getProcessTimesAddr){
   335         -    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
   336         -    sqlite3_int64 ftWallEnd = timeOfDay();
   337         -    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
   338         -    printf("Run Time: real %.3f user %f sys %f\n",
   339         -       (ftWallEnd - ftWallBegin)*0.001,
   340         -       timeDiff(&ftUserBegin, &ftUserEnd),
   341         -       timeDiff(&ftKernelBegin, &ftKernelEnd));
   342         -  }
   343         -}
   344         -
   345         -#define BEGIN_TIMER beginTimer()
   346         -#define END_TIMER endTimer()
   347         -#define HAS_TIMER hasTimer()
   348         -
   349         -#else
   350         -#define BEGIN_TIMER
   351         -#define END_TIMER
   352         -#define HAS_TIMER 0
   353         -#endif
   354         -
   355         -/*
   356         -** Used to prevent warnings about unused parameters
   357         -*/
   358         -#define UNUSED_PARAMETER(x) (void)(x)
   359         -
   360         -/*
   361         -** If the following flag is set, then command execution stops
   362         -** at an error if we are not interactive.
   363         -*/
   364         -static int bail_on_error = 0;
   365         -
   366         -/*
   367         -** Threat stdin as an interactive input if the following variable
   368         -** is true.  Otherwise, assume stdin is connected to a file or pipe.
   369         -*/
   370         -static int stdin_is_interactive = 1;
   371         -
   372         -/*
   373         -** On Windows systems we have to know if standard output is a console
   374         -** in order to translate UTF-8 into MBCS.  The following variable is
   375         -** true if translation is required.
   376         -*/
   377         -static int stdout_is_console = 1;
   378         -
   379         -/*
   380         -** The following is the open SQLite database.  We make a pointer
   381         -** to this database a static variable so that it can be accessed
   382         -** by the SIGINT handler to interrupt database processing.
   383         -*/
   384         -static sqlite3 *globalDb = 0;
   385         -
   386         -/*
   387         -** True if an interrupt (Control-C) has been received.
   388         -*/
   389         -static volatile int seenInterrupt = 0;
   390         -
   391         -/*
   392         -** This is the name of our program. It is set in main(), used
   393         -** in a number of other places, mostly for error messages.
   394         -*/
   395         -static char *Argv0;
   396         -
   397         -/*
   398         -** Prompt strings. Initialized in main. Settable with
   399         -**   .prompt main continue
   400         -*/
   401         -static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
   402         -static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
   403         -
   404         -/*
   405         -** Render output like fprintf().  Except, if the output is going to the
   406         -** console and if this is running on a Windows machine, translate the
   407         -** output from UTF-8 into MBCS.
   408         -*/
   409         -#if defined(_WIN32) || defined(WIN32)
   410         -void utf8_printf(FILE *out, const char *zFormat, ...){
   411         -  va_list ap;
   412         -  va_start(ap, zFormat);
   413         -  if( stdout_is_console && (out==stdout || out==stderr) ){
   414         -    char *z1 = sqlite3_vmprintf(zFormat, ap);
   415         -    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
   416         -    sqlite3_free(z1);
   417         -    fputs(z2, out);
   418         -    sqlite3_free(z2);
   419         -  }else{
   420         -    vfprintf(out, zFormat, ap);
   421         -  }
   422         -  va_end(ap);
   423         -}
   424         -#elif !defined(utf8_printf)
   425         -# define utf8_printf fprintf
   426         -#endif
   427         -
   428         -/*
   429         -** Render output like fprintf().  This should not be used on anything that
   430         -** includes string formatting (e.g. "%s").
   431         -*/
   432         -#if !defined(raw_printf)
   433         -# define raw_printf fprintf
   434         -#endif
   435         -
   436         -/*
   437         -** Write I/O traces to the following stream.
   438         -*/
   439         -#ifdef SQLITE_ENABLE_IOTRACE
   440         -static FILE *iotrace = 0;
   441         -#endif
   442         -
   443         -/*
   444         -** This routine works like printf in that its first argument is a
   445         -** format string and subsequent arguments are values to be substituted
   446         -** in place of % fields.  The result of formatting this string
   447         -** is written to iotrace.
   448         -*/
   449         -#ifdef SQLITE_ENABLE_IOTRACE
   450         -static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
   451         -  va_list ap;
   452         -  char *z;
   453         -  if( iotrace==0 ) return;
   454         -  va_start(ap, zFormat);
   455         -  z = sqlite3_vmprintf(zFormat, ap);
   456         -  va_end(ap);
   457         -  utf8_printf(iotrace, "%s", z);
   458         -  sqlite3_free(z);
   459         -}
   460         -#endif
   461         -
   462         -/*
   463         -** Output string zUtf to stream pOut as w characters.  If w is negative,
   464         -** then right-justify the text.  W is the width in UTF-8 characters, not
   465         -** in bytes.  This is different from the %*.*s specification in printf
   466         -** since with %*.*s the width is measured in bytes, not characters.
   467         -*/
   468         -static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
   469         -  int i;
   470         -  int n;
   471         -  int aw = w<0 ? -w : w;
   472         -  char zBuf[1000];
   473         -  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
   474         -  for(i=n=0; zUtf[i]; i++){
   475         -    if( (zUtf[i]&0xc0)!=0x80 ){
   476         -      n++;
   477         -      if( n==aw ){
   478         -        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
   479         -        break;
   480         -      }
   481         -    }
   482         -  }
   483         -  if( n>=aw ){
   484         -    utf8_printf(pOut, "%.*s", i, zUtf);
   485         -  }else if( w<0 ){
   486         -    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
   487         -  }else{
   488         -    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
   489         -  }
   490         -}
   491         -
   492         -
   493         -/*
   494         -** Determines if a string is a number of not.
   495         -*/
   496         -static int isNumber(const char *z, int *realnum){
   497         -  if( *z=='-' || *z=='+' ) z++;
   498         -  if( !IsDigit(*z) ){
   499         -    return 0;
   500         -  }
   501         -  z++;
   502         -  if( realnum ) *realnum = 0;
   503         -  while( IsDigit(*z) ){ z++; }
   504         -  if( *z=='.' ){
   505         -    z++;
   506         -    if( !IsDigit(*z) ) return 0;
   507         -    while( IsDigit(*z) ){ z++; }
   508         -    if( realnum ) *realnum = 1;
   509         -  }
   510         -  if( *z=='e' || *z=='E' ){
   511         -    z++;
   512         -    if( *z=='+' || *z=='-' ) z++;
   513         -    if( !IsDigit(*z) ) return 0;
   514         -    while( IsDigit(*z) ){ z++; }
   515         -    if( realnum ) *realnum = 1;
   516         -  }
   517         -  return *z==0;
   518         -}
   519         -
   520         -/*
   521         -** Compute a string length that is limited to what can be stored in
   522         -** lower 30 bits of a 32-bit signed integer.
   523         -*/
   524         -static int strlen30(const char *z){
   525         -  const char *z2 = z;
   526         -  while( *z2 ){ z2++; }
   527         -  return 0x3fffffff & (int)(z2 - z);
   528         -}
   529         -
   530         -/*
   531         -** Return the length of a string in characters.  Multibyte UTF8 characters
   532         -** count as a single character.
   533         -*/
   534         -static int strlenChar(const char *z){
   535         -  int n = 0;
   536         -  while( *z ){
   537         -    if( (0xc0&*(z++))!=0x80 ) n++;
   538         -  }
   539         -  return n;
   540         -}
   541         -
   542         -/*
   543         -** This routine reads a line of text from FILE in, stores
   544         -** the text in memory obtained from malloc() and returns a pointer
   545         -** to the text.  NULL is returned at end of file, or if malloc()
   546         -** fails.
   547         -**
   548         -** If zLine is not NULL then it is a malloced buffer returned from
   549         -** a previous call to this routine that may be reused.
   550         -*/
   551         -static char *local_getline(char *zLine, FILE *in){
   552         -  int nLine = zLine==0 ? 0 : 100;
   553         -  int n = 0;
   554         -
   555         -  while( 1 ){
   556         -    if( n+100>nLine ){
   557         -      nLine = nLine*2 + 100;
   558         -      zLine = realloc(zLine, nLine);
   559         -      if( zLine==0 ) return 0;
   560         -    }
   561         -    if( fgets(&zLine[n], nLine - n, in)==0 ){
   562         -      if( n==0 ){
   563         -        free(zLine);
   564         -        return 0;
   565         -      }
   566         -      zLine[n] = 0;
   567         -      break;
   568         -    }
   569         -    while( zLine[n] ) n++;
   570         -    if( n>0 && zLine[n-1]=='\n' ){
   571         -      n--;
   572         -      if( n>0 && zLine[n-1]=='\r' ) n--;
   573         -      zLine[n] = 0;
   574         -      break;
   575         -    }
   576         -  }
   577         -#if defined(_WIN32) || defined(WIN32)
   578         -  /* For interactive input on Windows systems, translate the
   579         -  ** multi-byte characterset characters into UTF-8. */
   580         -  if( stdin_is_interactive && in==stdin ){
   581         -    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
   582         -    if( zTrans ){
   583         -      int nTrans = strlen30(zTrans)+1;
   584         -      if( nTrans>nLine ){
   585         -        zLine = realloc(zLine, nTrans);
   586         -        if( zLine==0 ){
   587         -          sqlite3_free(zTrans);
   588         -          return 0;
   589         -        }
   590         -      }
   591         -      memcpy(zLine, zTrans, nTrans);
   592         -      sqlite3_free(zTrans);
   593         -    }
   594         -  }
   595         -#endif /* defined(_WIN32) || defined(WIN32) */
   596         -  return zLine;
   597         -}
   598         -
   599         -/*
   600         -** Retrieve a single line of input text.
   601         -**
   602         -** If in==0 then read from standard input and prompt before each line.
   603         -** If isContinuation is true, then a continuation prompt is appropriate.
   604         -** If isContinuation is zero, then the main prompt should be used.
   605         -**
   606         -** If zPrior is not NULL then it is a buffer from a prior call to this
   607         -** routine that can be reused.
   608         -**
   609         -** The result is stored in space obtained from malloc() and must either
   610         -** be freed by the caller or else passed back into this routine via the
   611         -** zPrior argument for reuse.
   612         -*/
   613         -static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
   614         -  char *zPrompt;
   615         -  char *zResult;
   616         -  if( in!=0 ){
   617         -    zResult = local_getline(zPrior, in);
   618         -  }else{
   619         -    zPrompt = isContinuation ? continuePrompt : mainPrompt;
   620         -#if SHELL_USE_LOCAL_GETLINE
   621         -    printf("%s", zPrompt);
   622         -    fflush(stdout);
   623         -    zResult = local_getline(zPrior, stdin);
   624         -#else
   625         -    free(zPrior);
   626         -    zResult = shell_readline(zPrompt);
   627         -    if( zResult && *zResult ) shell_add_history(zResult);
   628         -#endif
   629         -  }
   630         -  return zResult;
   631         -}
   632         -/*
   633         -** A variable length string to which one can append text.
   634         -*/
   635         -typedef struct ShellText ShellText;
   636         -struct ShellText {
   637         -  char *z;
   638         -  int n;
   639         -  int nAlloc;
   640         -};
   641         -
   642         -/*
   643         -** Initialize and destroy a ShellText object
   644         -*/
   645         -static void initText(ShellText *p){
   646         -  memset(p, 0, sizeof(*p));
   647         -}
   648         -static void freeText(ShellText *p){
   649         -  free(p->z);
   650         -  initText(p);
   651         -}
   652         -
   653         -/* zIn is either a pointer to a NULL-terminated string in memory obtained
   654         -** from malloc(), or a NULL pointer. The string pointed to by zAppend is
   655         -** added to zIn, and the result returned in memory obtained from malloc().
   656         -** zIn, if it was not NULL, is freed.
   657         -**
   658         -** If the third argument, quote, is not '\0', then it is used as a
   659         -** quote character for zAppend.
   660         -*/
   661         -static void appendText(ShellText *p, char const *zAppend, char quote){
   662         -  int len;
   663         -  int i;
   664         -  int nAppend = strlen30(zAppend);
   665         -
   666         -  len = nAppend+p->n+1;
   667         -  if( quote ){
   668         -    len += 2;
   669         -    for(i=0; i<nAppend; i++){
   670         -      if( zAppend[i]==quote ) len++;
   671         -    }
   672         -  }
   673         -
   674         -  if( p->n+len>=p->nAlloc ){
   675         -    p->nAlloc = p->nAlloc*2 + len + 20;
   676         -    p->z = realloc(p->z, p->nAlloc);
   677         -    if( p->z==0 ){
   678         -      memset(p, 0, sizeof(*p));
   679         -      return;
   680         -    }
   681         -  }
   682         -
   683         -  if( quote ){
   684         -    char *zCsr = p->z+p->n;
   685         -    *zCsr++ = quote;
   686         -    for(i=0; i<nAppend; i++){
   687         -      *zCsr++ = zAppend[i];
   688         -      if( zAppend[i]==quote ) *zCsr++ = quote;
   689         -    }
   690         -    *zCsr++ = quote;
   691         -    p->n = (int)(zCsr - p->z);
   692         -    *zCsr = '\0';
   693         -  }else{
   694         -    memcpy(p->z+p->n, zAppend, nAppend);
   695         -    p->n += nAppend;
   696         -    p->z[p->n] = '\0';
   697         -  }
   698         -}
   699         -
   700         -/*
   701         -** Attempt to determine if identifier zName needs to be quoted, either
   702         -** because it contains non-alphanumeric characters, or because it is an
   703         -** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
   704         -** that quoting is required.
   705         -**
   706         -** Return '"' if quoting is required.  Return 0 if no quoting is required.
   707         -*/
   708         -static char quoteChar(const char *zName){
   709         -  /* All SQLite keywords, in alphabetical order */
   710         -  static const char *azKeywords[] = {
   711         -    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
   712         -    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
   713         -    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
   714         -    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
   715         -    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
   716         -    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
   717         -    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
   718         -    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
   719         -    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
   720         -    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
   721         -    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
   722         -    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
   723         -    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
   724         -    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
   725         -    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
   726         -    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
   727         -    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
   728         -    "WITH", "WITHOUT",
   729         -  };
   730         -  int i, lwr, upr, mid, c;
   731         -  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
   732         -  for(i=0; zName[i]; i++){
   733         -    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
   734         -  }
   735         -  lwr = 0;
   736         -  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
   737         -  while( lwr<=upr ){
   738         -    mid = (lwr+upr)/2;
   739         -    c = sqlite3_stricmp(azKeywords[mid], zName);
   740         -    if( c==0 ) return '"';
   741         -    if( c<0 ){
   742         -      lwr = mid+1;
   743         -    }else{
   744         -      upr = mid-1;
   745         -    }
   746         -  }
   747         -  return 0;
   748         -}
   749         -
   750         -/*
   751         -** SQL function:  shell_add_schema(S,X)
   752         -**
   753         -** Add the schema name X to the CREATE statement in S and return the result.
   754         -** Examples:
   755         -**
   756         -**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
   757         -**
   758         -** Also works on
   759         -**
   760         -**    CREATE INDEX
   761         -**    CREATE UNIQUE INDEX
   762         -**    CREATE VIEW
   763         -**    CREATE TRIGGER
   764         -**    CREATE VIRTUAL TABLE
   765         -**
   766         -** This UDF is used by the .schema command to insert the schema name of
   767         -** attached databases into the middle of the sqlite_master.sql field.
   768         -*/
   769         -static void shellAddSchemaName(
   770         -  sqlite3_context *pCtx,
   771         -  int nVal,
   772         -  sqlite3_value **apVal
   773         -){
   774         -  static const char *aPrefix[] = {
   775         -     "TABLE",
   776         -     "INDEX",
   777         -     "UNIQUE INDEX",
   778         -     "VIEW",
   779         -     "TRIGGER",
   780         -     "VIRTUAL TABLE"
   781         -  };
   782         -  int i = 0;
   783         -  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
   784         -  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
   785         -  assert( nVal==2 );
   786         -  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
   787         -    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
   788         -      int n = strlen30(aPrefix[i]);
   789         -      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
   790         -        char cQuote = quoteChar(zSchema);
   791         -        char *z;
   792         -        if( cQuote ){
   793         -         z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
   794         -        }else{
   795         -          z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
   796         -        }
   797         -        sqlite3_result_text(pCtx, z, -1, sqlite3_free);
   798         -        return;
   799         -      }
   800         -    }
   801         -  }
   802         -  sqlite3_result_value(pCtx, apVal[0]);
   803         -}
   804         -
   805         -/*
   806         -** The source code for several run-time loadable extensions is inserted
   807         -** below by the ../tool/mkshellc.tcl script.  Before processing that included
   808         -** code, we need to override some macros to make the included program code
   809         -** work here in the middle of this regular program.
   810         -*/
   811         -#define SQLITE_EXTENSION_INIT1
   812         -#define SQLITE_EXTENSION_INIT2(X) (void)(X)
   813         -
   814         -/************************* Begin ../ext/misc/shathree.c ******************/
   815         -/*
   816         -** 2017-03-08
   817         -**
   818         -** The author disclaims copyright to this source code.  In place of
   819         -** a legal notice, here is a blessing:
   820         -**
   821         -**    May you do good and not evil.
   822         -**    May you find forgiveness for yourself and forgive others.
   823         -**    May you share freely, never taking more than you give.
   824         -**
   825         -******************************************************************************
   826         -**
   827         -** This SQLite extension implements a functions that compute SHA1 hashes.
   828         -** Two SQL functions are implemented:
   829         -**
   830         -**     sha3(X,SIZE)
   831         -**     sha3_query(Y,SIZE)
   832         -**
   833         -** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
   834         -** X is NULL.
   835         -**
   836         -** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
   837         -** and returns a hash of their results.
   838         -**
   839         -** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
   840         -** is used.  If SIZE is included it must be one of the integers 224, 256,
   841         -** 384, or 512, to determine SHA3 hash variant that is computed.
   842         -*/
   843         -SQLITE_EXTENSION_INIT1
   844         -#include <assert.h>
   845         -#include <string.h>
   846         -#include <stdarg.h>
   847         -typedef sqlite3_uint64 u64;
   848         -
   849         -/******************************************************************************
   850         -** The Hash Engine
   851         -*/
   852         -/*
   853         -** Macros to determine whether the machine is big or little endian,
   854         -** and whether or not that determination is run-time or compile-time.
   855         -**
   856         -** For best performance, an attempt is made to guess at the byte-order
   857         -** using C-preprocessor macros.  If that is unsuccessful, or if
   858         -** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
   859         -** at run-time.
   860         -*/
   861         -#ifndef SHA3_BYTEORDER
   862         -# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
   863         -     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
   864         -     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
   865         -     defined(__arm__)
   866         -#   define SHA3_BYTEORDER    1234
   867         -# elif defined(sparc)    || defined(__ppc__)
   868         -#   define SHA3_BYTEORDER    4321
   869         -# else
   870         -#   define SHA3_BYTEORDER 0
   871         -# endif
   872         -#endif
   873         -
   874         -
   875         -/*
   876         -** State structure for a SHA3 hash in progress
   877         -*/
   878         -typedef struct SHA3Context SHA3Context;
   879         -struct SHA3Context {
   880         -  union {
   881         -    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
   882         -    unsigned char x[1600];    /* ... or 1600 bytes */
   883         -  } u;
   884         -  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
   885         -  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
   886         -  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
   887         -};
   888         -
   889         -/*
   890         -** A single step of the Keccak mixing function for a 1600-bit state
   891         -*/
   892         -static void KeccakF1600Step(SHA3Context *p){
   893         -  int i;
   894         -  u64 B0, B1, B2, B3, B4;
   895         -  u64 C0, C1, C2, C3, C4;
   896         -  u64 D0, D1, D2, D3, D4;
   897         -  static const u64 RC[] = {
   898         -    0x0000000000000001ULL,  0x0000000000008082ULL,
   899         -    0x800000000000808aULL,  0x8000000080008000ULL,
   900         -    0x000000000000808bULL,  0x0000000080000001ULL,
   901         -    0x8000000080008081ULL,  0x8000000000008009ULL,
   902         -    0x000000000000008aULL,  0x0000000000000088ULL,
   903         -    0x0000000080008009ULL,  0x000000008000000aULL,
   904         -    0x000000008000808bULL,  0x800000000000008bULL,
   905         -    0x8000000000008089ULL,  0x8000000000008003ULL,
   906         -    0x8000000000008002ULL,  0x8000000000000080ULL,
   907         -    0x000000000000800aULL,  0x800000008000000aULL,
   908         -    0x8000000080008081ULL,  0x8000000000008080ULL,
   909         -    0x0000000080000001ULL,  0x8000000080008008ULL
   910         -  };
   911         -# define A00 (p->u.s[0])
   912         -# define A01 (p->u.s[1])
   913         -# define A02 (p->u.s[2])
   914         -# define A03 (p->u.s[3])
   915         -# define A04 (p->u.s[4])
   916         -# define A10 (p->u.s[5])
   917         -# define A11 (p->u.s[6])
   918         -# define A12 (p->u.s[7])
   919         -# define A13 (p->u.s[8])
   920         -# define A14 (p->u.s[9])
   921         -# define A20 (p->u.s[10])
   922         -# define A21 (p->u.s[11])
   923         -# define A22 (p->u.s[12])
   924         -# define A23 (p->u.s[13])
   925         -# define A24 (p->u.s[14])
   926         -# define A30 (p->u.s[15])
   927         -# define A31 (p->u.s[16])
   928         -# define A32 (p->u.s[17])
   929         -# define A33 (p->u.s[18])
   930         -# define A34 (p->u.s[19])
   931         -# define A40 (p->u.s[20])
   932         -# define A41 (p->u.s[21])
   933         -# define A42 (p->u.s[22])
   934         -# define A43 (p->u.s[23])
   935         -# define A44 (p->u.s[24])
   936         -# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
   937         -
   938         -  for(i=0; i<24; i+=4){
   939         -    C0 = A00^A10^A20^A30^A40;
   940         -    C1 = A01^A11^A21^A31^A41;
   941         -    C2 = A02^A12^A22^A32^A42;
   942         -    C3 = A03^A13^A23^A33^A43;
   943         -    C4 = A04^A14^A24^A34^A44;
   944         -    D0 = C4^ROL64(C1, 1);
   945         -    D1 = C0^ROL64(C2, 1);
   946         -    D2 = C1^ROL64(C3, 1);
   947         -    D3 = C2^ROL64(C4, 1);
   948         -    D4 = C3^ROL64(C0, 1);
   949         -
   950         -    B0 = (A00^D0);
   951         -    B1 = ROL64((A11^D1), 44);
   952         -    B2 = ROL64((A22^D2), 43);
   953         -    B3 = ROL64((A33^D3), 21);
   954         -    B4 = ROL64((A44^D4), 14);
   955         -    A00 =   B0 ^((~B1)&  B2 );
   956         -    A00 ^= RC[i];
   957         -    A11 =   B1 ^((~B2)&  B3 );
   958         -    A22 =   B2 ^((~B3)&  B4 );
   959         -    A33 =   B3 ^((~B4)&  B0 );
   960         -    A44 =   B4 ^((~B0)&  B1 );
   961         -
   962         -    B2 = ROL64((A20^D0), 3);
   963         -    B3 = ROL64((A31^D1), 45);
   964         -    B4 = ROL64((A42^D2), 61);
   965         -    B0 = ROL64((A03^D3), 28);
   966         -    B1 = ROL64((A14^D4), 20);
   967         -    A20 =   B0 ^((~B1)&  B2 );
   968         -    A31 =   B1 ^((~B2)&  B3 );
   969         -    A42 =   B2 ^((~B3)&  B4 );
   970         -    A03 =   B3 ^((~B4)&  B0 );
   971         -    A14 =   B4 ^((~B0)&  B1 );
   972         -
   973         -    B4 = ROL64((A40^D0), 18);
   974         -    B0 = ROL64((A01^D1), 1);
   975         -    B1 = ROL64((A12^D2), 6);
   976         -    B2 = ROL64((A23^D3), 25);
   977         -    B3 = ROL64((A34^D4), 8);
   978         -    A40 =   B0 ^((~B1)&  B2 );
   979         -    A01 =   B1 ^((~B2)&  B3 );
   980         -    A12 =   B2 ^((~B3)&  B4 );
   981         -    A23 =   B3 ^((~B4)&  B0 );
   982         -    A34 =   B4 ^((~B0)&  B1 );
   983         -
   984         -    B1 = ROL64((A10^D0), 36);
   985         -    B2 = ROL64((A21^D1), 10);
   986         -    B3 = ROL64((A32^D2), 15);
   987         -    B4 = ROL64((A43^D3), 56);
   988         -    B0 = ROL64((A04^D4), 27);
   989         -    A10 =   B0 ^((~B1)&  B2 );
   990         -    A21 =   B1 ^((~B2)&  B3 );
   991         -    A32 =   B2 ^((~B3)&  B4 );
   992         -    A43 =   B3 ^((~B4)&  B0 );
   993         -    A04 =   B4 ^((~B0)&  B1 );
   994         -
   995         -    B3 = ROL64((A30^D0), 41);
   996         -    B4 = ROL64((A41^D1), 2);
   997         -    B0 = ROL64((A02^D2), 62);
   998         -    B1 = ROL64((A13^D3), 55);
   999         -    B2 = ROL64((A24^D4), 39);
  1000         -    A30 =   B0 ^((~B1)&  B2 );
  1001         -    A41 =   B1 ^((~B2)&  B3 );
  1002         -    A02 =   B2 ^((~B3)&  B4 );
  1003         -    A13 =   B3 ^((~B4)&  B0 );
  1004         -    A24 =   B4 ^((~B0)&  B1 );
  1005         -
  1006         -    C0 = A00^A20^A40^A10^A30;
  1007         -    C1 = A11^A31^A01^A21^A41;
  1008         -    C2 = A22^A42^A12^A32^A02;
  1009         -    C3 = A33^A03^A23^A43^A13;
  1010         -    C4 = A44^A14^A34^A04^A24;
  1011         -    D0 = C4^ROL64(C1, 1);
  1012         -    D1 = C0^ROL64(C2, 1);
  1013         -    D2 = C1^ROL64(C3, 1);
  1014         -    D3 = C2^ROL64(C4, 1);
  1015         -    D4 = C3^ROL64(C0, 1);
  1016         -
  1017         -    B0 = (A00^D0);
  1018         -    B1 = ROL64((A31^D1), 44);
  1019         -    B2 = ROL64((A12^D2), 43);
  1020         -    B3 = ROL64((A43^D3), 21);
  1021         -    B4 = ROL64((A24^D4), 14);
  1022         -    A00 =   B0 ^((~B1)&  B2 );
  1023         -    A00 ^= RC[i+1];
  1024         -    A31 =   B1 ^((~B2)&  B3 );
  1025         -    A12 =   B2 ^((~B3)&  B4 );
  1026         -    A43 =   B3 ^((~B4)&  B0 );
  1027         -    A24 =   B4 ^((~B0)&  B1 );
  1028         -
  1029         -    B2 = ROL64((A40^D0), 3);
  1030         -    B3 = ROL64((A21^D1), 45);
  1031         -    B4 = ROL64((A02^D2), 61);
  1032         -    B0 = ROL64((A33^D3), 28);
  1033         -    B1 = ROL64((A14^D4), 20);
  1034         -    A40 =   B0 ^((~B1)&  B2 );
  1035         -    A21 =   B1 ^((~B2)&  B3 );
  1036         -    A02 =   B2 ^((~B3)&  B4 );
  1037         -    A33 =   B3 ^((~B4)&  B0 );
  1038         -    A14 =   B4 ^((~B0)&  B1 );
  1039         -
  1040         -    B4 = ROL64((A30^D0), 18);
  1041         -    B0 = ROL64((A11^D1), 1);
  1042         -    B1 = ROL64((A42^D2), 6);
  1043         -    B2 = ROL64((A23^D3), 25);
  1044         -    B3 = ROL64((A04^D4), 8);
  1045         -    A30 =   B0 ^((~B1)&  B2 );
  1046         -    A11 =   B1 ^((~B2)&  B3 );
  1047         -    A42 =   B2 ^((~B3)&  B4 );
  1048         -    A23 =   B3 ^((~B4)&  B0 );
  1049         -    A04 =   B4 ^((~B0)&  B1 );
  1050         -
  1051         -    B1 = ROL64((A20^D0), 36);
  1052         -    B2 = ROL64((A01^D1), 10);
  1053         -    B3 = ROL64((A32^D2), 15);
  1054         -    B4 = ROL64((A13^D3), 56);
  1055         -    B0 = ROL64((A44^D4), 27);
  1056         -    A20 =   B0 ^((~B1)&  B2 );
  1057         -    A01 =   B1 ^((~B2)&  B3 );
  1058         -    A32 =   B2 ^((~B3)&  B4 );
  1059         -    A13 =   B3 ^((~B4)&  B0 );
  1060         -    A44 =   B4 ^((~B0)&  B1 );
  1061         -
  1062         -    B3 = ROL64((A10^D0), 41);
  1063         -    B4 = ROL64((A41^D1), 2);
  1064         -    B0 = ROL64((A22^D2), 62);
  1065         -    B1 = ROL64((A03^D3), 55);
  1066         -    B2 = ROL64((A34^D4), 39);
  1067         -    A10 =   B0 ^((~B1)&  B2 );
  1068         -    A41 =   B1 ^((~B2)&  B3 );
  1069         -    A22 =   B2 ^((~B3)&  B4 );
  1070         -    A03 =   B3 ^((~B4)&  B0 );
  1071         -    A34 =   B4 ^((~B0)&  B1 );
  1072         -
  1073         -    C0 = A00^A40^A30^A20^A10;
  1074         -    C1 = A31^A21^A11^A01^A41;
  1075         -    C2 = A12^A02^A42^A32^A22;
  1076         -    C3 = A43^A33^A23^A13^A03;
  1077         -    C4 = A24^A14^A04^A44^A34;
  1078         -    D0 = C4^ROL64(C1, 1);
  1079         -    D1 = C0^ROL64(C2, 1);
  1080         -    D2 = C1^ROL64(C3, 1);
  1081         -    D3 = C2^ROL64(C4, 1);
  1082         -    D4 = C3^ROL64(C0, 1);
  1083         -
  1084         -    B0 = (A00^D0);
  1085         -    B1 = ROL64((A21^D1), 44);
  1086         -    B2 = ROL64((A42^D2), 43);
  1087         -    B3 = ROL64((A13^D3), 21);
  1088         -    B4 = ROL64((A34^D4), 14);
  1089         -    A00 =   B0 ^((~B1)&  B2 );
  1090         -    A00 ^= RC[i+2];
  1091         -    A21 =   B1 ^((~B2)&  B3 );
  1092         -    A42 =   B2 ^((~B3)&  B4 );
  1093         -    A13 =   B3 ^((~B4)&  B0 );
  1094         -    A34 =   B4 ^((~B0)&  B1 );
  1095         -
  1096         -    B2 = ROL64((A30^D0), 3);
  1097         -    B3 = ROL64((A01^D1), 45);
  1098         -    B4 = ROL64((A22^D2), 61);
  1099         -    B0 = ROL64((A43^D3), 28);
  1100         -    B1 = ROL64((A14^D4), 20);
  1101         -    A30 =   B0 ^((~B1)&  B2 );
  1102         -    A01 =   B1 ^((~B2)&  B3 );
  1103         -    A22 =   B2 ^((~B3)&  B4 );
  1104         -    A43 =   B3 ^((~B4)&  B0 );
  1105         -    A14 =   B4 ^((~B0)&  B1 );
  1106         -
  1107         -    B4 = ROL64((A10^D0), 18);
  1108         -    B0 = ROL64((A31^D1), 1);
  1109         -    B1 = ROL64((A02^D2), 6);
  1110         -    B2 = ROL64((A23^D3), 25);
  1111         -    B3 = ROL64((A44^D4), 8);
  1112         -    A10 =   B0 ^((~B1)&  B2 );
  1113         -    A31 =   B1 ^((~B2)&  B3 );
  1114         -    A02 =   B2 ^((~B3)&  B4 );
  1115         -    A23 =   B3 ^((~B4)&  B0 );
  1116         -    A44 =   B4 ^((~B0)&  B1 );
  1117         -
  1118         -    B1 = ROL64((A40^D0), 36);
  1119         -    B2 = ROL64((A11^D1), 10);
  1120         -    B3 = ROL64((A32^D2), 15);
  1121         -    B4 = ROL64((A03^D3), 56);
  1122         -    B0 = ROL64((A24^D4), 27);
  1123         -    A40 =   B0 ^((~B1)&  B2 );
  1124         -    A11 =   B1 ^((~B2)&  B3 );
  1125         -    A32 =   B2 ^((~B3)&  B4 );
  1126         -    A03 =   B3 ^((~B4)&  B0 );
  1127         -    A24 =   B4 ^((~B0)&  B1 );
  1128         -
  1129         -    B3 = ROL64((A20^D0), 41);
  1130         -    B4 = ROL64((A41^D1), 2);
  1131         -    B0 = ROL64((A12^D2), 62);
  1132         -    B1 = ROL64((A33^D3), 55);
  1133         -    B2 = ROL64((A04^D4), 39);
  1134         -    A20 =   B0 ^((~B1)&  B2 );
  1135         -    A41 =   B1 ^((~B2)&  B3 );
  1136         -    A12 =   B2 ^((~B3)&  B4 );
  1137         -    A33 =   B3 ^((~B4)&  B0 );
  1138         -    A04 =   B4 ^((~B0)&  B1 );
  1139         -
  1140         -    C0 = A00^A30^A10^A40^A20;
  1141         -    C1 = A21^A01^A31^A11^A41;
  1142         -    C2 = A42^A22^A02^A32^A12;
  1143         -    C3 = A13^A43^A23^A03^A33;
  1144         -    C4 = A34^A14^A44^A24^A04;
  1145         -    D0 = C4^ROL64(C1, 1);
  1146         -    D1 = C0^ROL64(C2, 1);
  1147         -    D2 = C1^ROL64(C3, 1);
  1148         -    D3 = C2^ROL64(C4, 1);
  1149         -    D4 = C3^ROL64(C0, 1);
  1150         -
  1151         -    B0 = (A00^D0);
  1152         -    B1 = ROL64((A01^D1), 44);
  1153         -    B2 = ROL64((A02^D2), 43);
  1154         -    B3 = ROL64((A03^D3), 21);
  1155         -    B4 = ROL64((A04^D4), 14);
  1156         -    A00 =   B0 ^((~B1)&  B2 );
  1157         -    A00 ^= RC[i+3];
  1158         -    A01 =   B1 ^((~B2)&  B3 );
  1159         -    A02 =   B2 ^((~B3)&  B4 );
  1160         -    A03 =   B3 ^((~B4)&  B0 );
  1161         -    A04 =   B4 ^((~B0)&  B1 );
  1162         -
  1163         -    B2 = ROL64((A10^D0), 3);
  1164         -    B3 = ROL64((A11^D1), 45);
  1165         -    B4 = ROL64((A12^D2), 61);
  1166         -    B0 = ROL64((A13^D3), 28);
  1167         -    B1 = ROL64((A14^D4), 20);
  1168         -    A10 =   B0 ^((~B1)&  B2 );
  1169         -    A11 =   B1 ^((~B2)&  B3 );
  1170         -    A12 =   B2 ^((~B3)&  B4 );
  1171         -    A13 =   B3 ^((~B4)&  B0 );
  1172         -    A14 =   B4 ^((~B0)&  B1 );
  1173         -
  1174         -    B4 = ROL64((A20^D0), 18);
  1175         -    B0 = ROL64((A21^D1), 1);
  1176         -    B1 = ROL64((A22^D2), 6);
  1177         -    B2 = ROL64((A23^D3), 25);
  1178         -    B3 = ROL64((A24^D4), 8);
  1179         -    A20 =   B0 ^((~B1)&  B2 );
  1180         -    A21 =   B1 ^((~B2)&  B3 );
  1181         -    A22 =   B2 ^((~B3)&  B4 );
  1182         -    A23 =   B3 ^((~B4)&  B0 );
  1183         -    A24 =   B4 ^((~B0)&  B1 );
  1184         -
  1185         -    B1 = ROL64((A30^D0), 36);
  1186         -    B2 = ROL64((A31^D1), 10);
  1187         -    B3 = ROL64((A32^D2), 15);
  1188         -    B4 = ROL64((A33^D3), 56);
  1189         -    B0 = ROL64((A34^D4), 27);
  1190         -    A30 =   B0 ^((~B1)&  B2 );
  1191         -    A31 =   B1 ^((~B2)&  B3 );
  1192         -    A32 =   B2 ^((~B3)&  B4 );
  1193         -    A33 =   B3 ^((~B4)&  B0 );
  1194         -    A34 =   B4 ^((~B0)&  B1 );
  1195         -
  1196         -    B3 = ROL64((A40^D0), 41);
  1197         -    B4 = ROL64((A41^D1), 2);
  1198         -    B0 = ROL64((A42^D2), 62);
  1199         -    B1 = ROL64((A43^D3), 55);
  1200         -    B2 = ROL64((A44^D4), 39);
  1201         -    A40 =   B0 ^((~B1)&  B2 );
  1202         -    A41 =   B1 ^((~B2)&  B3 );
  1203         -    A42 =   B2 ^((~B3)&  B4 );
  1204         -    A43 =   B3 ^((~B4)&  B0 );
  1205         -    A44 =   B4 ^((~B0)&  B1 );
  1206         -  }
  1207         -}
  1208         -
  1209         -/*
  1210         -** Initialize a new hash.  iSize determines the size of the hash
  1211         -** in bits and should be one of 224, 256, 384, or 512.  Or iSize
  1212         -** can be zero to use the default hash size of 256 bits.
  1213         -*/
  1214         -static void SHA3Init(SHA3Context *p, int iSize){
  1215         -  memset(p, 0, sizeof(*p));
  1216         -  if( iSize>=128 && iSize<=512 ){
  1217         -    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
  1218         -  }else{
  1219         -    p->nRate = (1600 - 2*256)/8;
  1220         -  }
  1221         -#if SHA3_BYTEORDER==1234
  1222         -  /* Known to be little-endian at compile-time. No-op */
  1223         -#elif SHA3_BYTEORDER==4321
  1224         -  p->ixMask = 7;  /* Big-endian */
  1225         -#else
  1226         -  {
  1227         -    static unsigned int one = 1;
  1228         -    if( 1==*(unsigned char*)&one ){
  1229         -      /* Little endian.  No byte swapping. */
  1230         -      p->ixMask = 0;
  1231         -    }else{
  1232         -      /* Big endian.  Byte swap. */
  1233         -      p->ixMask = 7;
  1234         -    }
  1235         -  }
  1236         -#endif
  1237         -}
  1238         -
  1239         -/*
  1240         -** Make consecutive calls to the SHA3Update function to add new content
  1241         -** to the hash
  1242         -*/
  1243         -static void SHA3Update(
  1244         -  SHA3Context *p,
  1245         -  const unsigned char *aData,
  1246         -  unsigned int nData
  1247         -){
  1248         -  unsigned int i = 0;
  1249         -#if SHA3_BYTEORDER==1234
  1250         -  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
  1251         -    for(; i+7<nData; i+=8){
  1252         -      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
  1253         -      p->nLoaded += 8;
  1254         -      if( p->nLoaded>=p->nRate ){
  1255         -        KeccakF1600Step(p);
  1256         -        p->nLoaded = 0;
  1257         -      }
  1258         -    }
  1259         -  }
  1260         -#endif
  1261         -  for(; i<nData; i++){
  1262         -#if SHA3_BYTEORDER==1234
  1263         -    p->u.x[p->nLoaded] ^= aData[i];
  1264         -#elif SHA3_BYTEORDER==4321
  1265         -    p->u.x[p->nLoaded^0x07] ^= aData[i];
  1266         -#else
  1267         -    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
  1268         -#endif
  1269         -    p->nLoaded++;
  1270         -    if( p->nLoaded==p->nRate ){
  1271         -      KeccakF1600Step(p);
  1272         -      p->nLoaded = 0;
  1273         -    }
  1274         -  }
  1275         -}
  1276         -
  1277         -/*
  1278         -** After all content has been added, invoke SHA3Final() to compute
  1279         -** the final hash.  The function returns a pointer to the binary
  1280         -** hash value.
  1281         -*/
  1282         -static unsigned char *SHA3Final(SHA3Context *p){
  1283         -  unsigned int i;
  1284         -  if( p->nLoaded==p->nRate-1 ){
  1285         -    const unsigned char c1 = 0x86;
  1286         -    SHA3Update(p, &c1, 1);
  1287         -  }else{
  1288         -    const unsigned char c2 = 0x06;
  1289         -    const unsigned char c3 = 0x80;
  1290         -    SHA3Update(p, &c2, 1);
  1291         -    p->nLoaded = p->nRate - 1;
  1292         -    SHA3Update(p, &c3, 1);
  1293         -  }
  1294         -  for(i=0; i<p->nRate; i++){
  1295         -    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
  1296         -  }
  1297         -  return &p->u.x[p->nRate];
  1298         -}
  1299         -/* End of the hashing logic
  1300         -*****************************************************************************/
  1301         -
  1302         -/*
  1303         -** Implementation of the sha3(X,SIZE) function.
  1304         -**
  1305         -** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
  1306         -** size is 256.  If X is a BLOB, it is hashed as is.  
  1307         -** For all other non-NULL types of input, X is converted into a UTF-8 string
  1308         -** and the string is hashed without the trailing 0x00 terminator.  The hash
  1309         -** of a NULL value is NULL.
  1310         -*/
  1311         -static void sha3Func(
  1312         -  sqlite3_context *context,
  1313         -  int argc,
  1314         -  sqlite3_value **argv
  1315         -){
  1316         -  SHA3Context cx;
  1317         -  int eType = sqlite3_value_type(argv[0]);
  1318         -  int nByte = sqlite3_value_bytes(argv[0]);
  1319         -  int iSize;
  1320         -  if( argc==1 ){
  1321         -    iSize = 256;
  1322         -  }else{
  1323         -    iSize = sqlite3_value_int(argv[1]);
  1324         -    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
  1325         -      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
  1326         -                                    "384 512", -1);
  1327         -      return;
  1328         -    }
  1329         -  }
  1330         -  if( eType==SQLITE_NULL ) return;
  1331         -  SHA3Init(&cx, iSize);
  1332         -  if( eType==SQLITE_BLOB ){
  1333         -    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
  1334         -  }else{
  1335         -    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
  1336         -  }
  1337         -  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
  1338         -}
  1339         -
  1340         -/* Compute a string using sqlite3_vsnprintf() with a maximum length
  1341         -** of 50 bytes and add it to the hash.
  1342         -*/
  1343         -static void hash_step_vformat(
  1344         -  SHA3Context *p,                 /* Add content to this context */
  1345         -  const char *zFormat,
  1346         -  ...
  1347         -){
  1348         -  va_list ap;
  1349         -  int n;
  1350         -  char zBuf[50];
  1351         -  va_start(ap, zFormat);
  1352         -  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
  1353         -  va_end(ap);
  1354         -  n = (int)strlen(zBuf);
  1355         -  SHA3Update(p, (unsigned char*)zBuf, n);
  1356         -}
  1357         -
  1358         -/*
  1359         -** Implementation of the sha3_query(SQL,SIZE) function.
  1360         -**
  1361         -** This function compiles and runs the SQL statement(s) given in the
  1362         -** argument. The results are hashed using a SIZE-bit SHA3.  The default
  1363         -** size is 256.
  1364         -**
  1365         -** The format of the byte stream that is hashed is summarized as follows:
  1366         -**
  1367         -**       S<n>:<sql>
  1368         -**       R
  1369         -**       N
  1370         -**       I<int>
  1371         -**       F<ieee-float>
  1372         -**       B<size>:<bytes>
  1373         -**       T<size>:<text>
  1374         -**
  1375         -** <sql> is the original SQL text for each statement run and <n> is
  1376         -** the size of that text.  The SQL text is UTF-8.  A single R character
  1377         -** occurs before the start of each row.  N means a NULL value.
  1378         -** I mean an 8-byte little-endian integer <int>.  F is a floating point
  1379         -** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
  1380         -** B means blobs of <size> bytes.  T means text rendered as <size>
  1381         -** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
  1382         -** text integers.
  1383         -**
  1384         -** For each SQL statement in the X input, there is one S segment.  Each
  1385         -** S segment is followed by zero or more R segments, one for each row in the
  1386         -** result set.  After each R, there are one or more N, I, F, B, or T segments,
  1387         -** one for each column in the result set.  Segments are concatentated directly
  1388         -** with no delimiters of any kind.
  1389         -*/
  1390         -static void sha3QueryFunc(
  1391         -  sqlite3_context *context,
  1392         -  int argc,
  1393         -  sqlite3_value **argv
  1394         -){
  1395         -  sqlite3 *db = sqlite3_context_db_handle(context);
  1396         -  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
  1397         -  sqlite3_stmt *pStmt = 0;
  1398         -  int nCol;                   /* Number of columns in the result set */
  1399         -  int i;                      /* Loop counter */
  1400         -  int rc;
  1401         -  int n;
  1402         -  const char *z;
  1403         -  SHA3Context cx;
  1404         -  int iSize;
  1405         -
  1406         -  if( argc==1 ){
  1407         -    iSize = 256;
  1408         -  }else{
  1409         -    iSize = sqlite3_value_int(argv[1]);
  1410         -    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
  1411         -      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
  1412         -                                    "384 512", -1);
  1413         -      return;
  1414         -    }
  1415         -  }
  1416         -  if( zSql==0 ) return;
  1417         -  SHA3Init(&cx, iSize);
  1418         -  while( zSql[0] ){
  1419         -    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
  1420         -    if( rc ){
  1421         -      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
  1422         -                                   zSql, sqlite3_errmsg(db));
  1423         -      sqlite3_finalize(pStmt);
  1424         -      sqlite3_result_error(context, zMsg, -1);
  1425         -      sqlite3_free(zMsg);
  1426         -      return;
  1427         -    }
  1428         -    if( !sqlite3_stmt_readonly(pStmt) ){
  1429         -      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
  1430         -      sqlite3_finalize(pStmt);
  1431         -      sqlite3_result_error(context, zMsg, -1);
  1432         -      sqlite3_free(zMsg);
  1433         -      return;
  1434         -    }
  1435         -    nCol = sqlite3_column_count(pStmt);
  1436         -    z = sqlite3_sql(pStmt);
  1437         -    n = (int)strlen(z);
  1438         -    hash_step_vformat(&cx,"S%d:",n);
  1439         -    SHA3Update(&cx,(unsigned char*)z,n);
  1440         -
  1441         -    /* Compute a hash over the result of the query */
  1442         -    while( SQLITE_ROW==sqlite3_step(pStmt) ){
  1443         -      SHA3Update(&cx,(const unsigned char*)"R",1);
  1444         -      for(i=0; i<nCol; i++){
  1445         -        switch( sqlite3_column_type(pStmt,i) ){
  1446         -          case SQLITE_NULL: {
  1447         -            SHA3Update(&cx, (const unsigned char*)"N",1);
  1448         -            break;
  1449         -          }
  1450         -          case SQLITE_INTEGER: {
  1451         -            sqlite3_uint64 u;
  1452         -            int j;
  1453         -            unsigned char x[9];
  1454         -            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
  1455         -            memcpy(&u, &v, 8);
  1456         -            for(j=8; j>=1; j--){
  1457         -              x[j] = u & 0xff;
  1458         -              u >>= 8;
  1459         -            }
  1460         -            x[0] = 'I';
  1461         -            SHA3Update(&cx, x, 9);
  1462         -            break;
  1463         -          }
  1464         -          case SQLITE_FLOAT: {
  1465         -            sqlite3_uint64 u;
  1466         -            int j;
  1467         -            unsigned char x[9];
  1468         -            double r = sqlite3_column_double(pStmt,i);
  1469         -            memcpy(&u, &r, 8);
  1470         -            for(j=8; j>=1; j--){
  1471         -              x[j] = u & 0xff;
  1472         -              u >>= 8;
  1473         -            }
  1474         -            x[0] = 'F';
  1475         -            SHA3Update(&cx,x,9);
  1476         -            break;
  1477         -          }
  1478         -          case SQLITE_TEXT: {
  1479         -            int n2 = sqlite3_column_bytes(pStmt, i);
  1480         -            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
  1481         -            hash_step_vformat(&cx,"T%d:",n2);
  1482         -            SHA3Update(&cx, z2, n2);
  1483         -            break;
  1484         -          }
  1485         -          case SQLITE_BLOB: {
  1486         -            int n2 = sqlite3_column_bytes(pStmt, i);
  1487         -            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
  1488         -            hash_step_vformat(&cx,"B%d:",n2);
  1489         -            SHA3Update(&cx, z2, n2);
  1490         -            break;
  1491         -          }
  1492         -        }
  1493         -      }
  1494         -    }
  1495         -    sqlite3_finalize(pStmt);
  1496         -  }
  1497         -  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
  1498         -}
  1499         -
  1500         -
  1501         -#ifdef _WIN32
  1502         -
  1503         -#endif
  1504         -int sqlite3_shathree_init(
  1505         -  sqlite3 *db,
  1506         -  char **pzErrMsg,
  1507         -  const sqlite3_api_routines *pApi
  1508         -){
  1509         -  int rc = SQLITE_OK;
  1510         -  SQLITE_EXTENSION_INIT2(pApi);
  1511         -  (void)pzErrMsg;  /* Unused parameter */
  1512         -  rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
  1513         -                               sha3Func, 0, 0);
  1514         -  if( rc==SQLITE_OK ){
  1515         -    rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
  1516         -                                 sha3Func, 0, 0);
  1517         -  }
  1518         -  if( rc==SQLITE_OK ){
  1519         -    rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
  1520         -                                 sha3QueryFunc, 0, 0);
  1521         -  }
  1522         -  if( rc==SQLITE_OK ){
  1523         -    rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
  1524         -                                 sha3QueryFunc, 0, 0);
  1525         -  }
  1526         -  return rc;
  1527         -}
  1528         -
  1529         -/************************* End ../ext/misc/shathree.c ********************/
  1530         -/************************* Begin ../ext/misc/fileio.c ******************/
  1531         -/*
  1532         -** 2014-06-13
  1533         -**
  1534         -** The author disclaims copyright to this source code.  In place of
  1535         -** a legal notice, here is a blessing:
  1536         -**
  1537         -**    May you do good and not evil.
  1538         -**    May you find forgiveness for yourself and forgive others.
  1539         -**    May you share freely, never taking more than you give.
  1540         -**
  1541         -******************************************************************************
  1542         -**
  1543         -** This SQLite extension implements SQL functions readfile() and
  1544         -** writefile().
  1545         -*/
  1546         -SQLITE_EXTENSION_INIT1
  1547         -#include <stdio.h>
  1548         -
  1549         -/*
  1550         -** Implementation of the "readfile(X)" SQL function.  The entire content
  1551         -** of the file named X is read and returned as a BLOB.  NULL is returned
  1552         -** if the file does not exist or is unreadable.
  1553         -*/
  1554         -static void readfileFunc(
  1555         -  sqlite3_context *context,
  1556         -  int argc,
  1557         -  sqlite3_value **argv
  1558         -){
  1559         -  const char *zName;
  1560         -  FILE *in;
  1561         -  long nIn;
  1562         -  void *pBuf;
  1563         -
  1564         -  (void)(argc);  /* Unused parameter */
  1565         -  zName = (const char*)sqlite3_value_text(argv[0]);
  1566         -  if( zName==0 ) return;
  1567         -  in = fopen(zName, "rb");
  1568         -  if( in==0 ) return;
  1569         -  fseek(in, 0, SEEK_END);
  1570         -  nIn = ftell(in);
  1571         -  rewind(in);
  1572         -  pBuf = sqlite3_malloc( nIn );
  1573         -  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
  1574         -    sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
  1575         -  }else{
  1576         -    sqlite3_free(pBuf);
  1577         -  }
  1578         -  fclose(in);
  1579         -}
  1580         -
  1581         -/*
  1582         -** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
  1583         -** is written into file X.  The number of bytes written is returned.  Or
  1584         -** NULL is returned if something goes wrong, such as being unable to open
  1585         -** file X for writing.
  1586         -*/
  1587         -static void writefileFunc(
  1588         -  sqlite3_context *context,
  1589         -  int argc,
  1590         -  sqlite3_value **argv
  1591         -){
  1592         -  FILE *out;
  1593         -  const char *z;
  1594         -  sqlite3_int64 rc;
  1595         -  const char *zFile;
  1596         -
  1597         -  (void)(argc);  /* Unused parameter */
  1598         -  zFile = (const char*)sqlite3_value_text(argv[0]);
  1599         -  if( zFile==0 ) return;
  1600         -  out = fopen(zFile, "wb");
  1601         -  if( out==0 ) return;
  1602         -  z = (const char*)sqlite3_value_blob(argv[1]);
  1603         -  if( z==0 ){
  1604         -    rc = 0;
  1605         -  }else{
  1606         -    rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
  1607         -  }
  1608         -  fclose(out);
  1609         -  sqlite3_result_int64(context, rc);
  1610         -}
  1611         -
  1612         -
  1613         -#ifdef _WIN32
  1614         -
  1615         -#endif
  1616         -int sqlite3_fileio_init(
  1617         -  sqlite3 *db, 
  1618         -  char **pzErrMsg, 
  1619         -  const sqlite3_api_routines *pApi
  1620         -){
  1621         -  int rc = SQLITE_OK;
  1622         -  SQLITE_EXTENSION_INIT2(pApi);
  1623         -  (void)pzErrMsg;  /* Unused parameter */
  1624         -  rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
  1625         -                               readfileFunc, 0, 0);
  1626         -  if( rc==SQLITE_OK ){
  1627         -    rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
  1628         -                                 writefileFunc, 0, 0);
  1629         -  }
  1630         -  return rc;
  1631         -}
  1632         -
  1633         -/************************* End ../ext/misc/fileio.c ********************/
  1634         -/************************* Begin ../ext/misc/completion.c ******************/
  1635         -/*
  1636         -** 2017-07-10
  1637         -**
  1638         -** The author disclaims copyright to this source code.  In place of
  1639         -** a legal notice, here is a blessing:
  1640         -**
  1641         -**    May you do good and not evil.
  1642         -**    May you find forgiveness for yourself and forgive others.
  1643         -**    May you share freely, never taking more than you give.
  1644         -**
  1645         -*************************************************************************
  1646         -**
  1647         -** This file implements an eponymous virtual table that returns suggested
  1648         -** completions for a partial SQL input.
  1649         -**
  1650         -** Suggested usage:
  1651         -**
  1652         -**     SELECT DISTINCT candidate COLLATE nocase
  1653         -**       FROM completion($prefix,$wholeline)
  1654         -**      ORDER BY 1;
  1655         -**
  1656         -** The two query parameters are optional.  $prefix is the text of the
  1657         -** current word being typed and that is to be completed.  $wholeline is
  1658         -** the complete input line, used for context.
  1659         -**
  1660         -** The raw completion() table might return the same candidate multiple
  1661         -** times, for example if the same column name is used to two or more
  1662         -** tables.  And the candidates are returned in an arbitrary order.  Hence,
  1663         -** the DISTINCT and ORDER BY are recommended.
  1664         -**
  1665         -** This virtual table operates at the speed of human typing, and so there
  1666         -** is no attempt to make it fast.  Even a slow implementation will be much
  1667         -** faster than any human can type.
  1668         -**
  1669         -*/
  1670         -SQLITE_EXTENSION_INIT1
  1671         -#include <assert.h>
  1672         -#include <string.h>
  1673         -#include <ctype.h>
  1674         -
  1675         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1676         -
  1677         -/* completion_vtab is a subclass of sqlite3_vtab which will
  1678         -** serve as the underlying representation of a completion virtual table
  1679         -*/
  1680         -typedef struct completion_vtab completion_vtab;
  1681         -struct completion_vtab {
  1682         -  sqlite3_vtab base;  /* Base class - must be first */
  1683         -  sqlite3 *db;        /* Database connection for this completion vtab */
  1684         -};
  1685         -
  1686         -/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
  1687         -** serve as the underlying representation of a cursor that scans
  1688         -** over rows of the result
  1689         -*/
  1690         -typedef struct completion_cursor completion_cursor;
  1691         -struct completion_cursor {
  1692         -  sqlite3_vtab_cursor base;  /* Base class - must be first */
  1693         -  sqlite3 *db;               /* Database connection for this cursor */
  1694         -  int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
  1695         -  char *zPrefix;             /* The prefix for the word we want to complete */
  1696         -  char *zLine;               /* The whole that we want to complete */
  1697         -  const char *zCurrentRow;   /* Current output row */
  1698         -  sqlite3_stmt *pStmt;       /* Current statement */
  1699         -  sqlite3_int64 iRowid;      /* The rowid */
  1700         -  int ePhase;                /* Current phase */
  1701         -  int j;                     /* inter-phase counter */
  1702         -};
  1703         -
  1704         -/* Values for ePhase:
  1705         -*/
  1706         -#define COMPLETION_FIRST_PHASE   1
  1707         -#define COMPLETION_KEYWORDS      1
  1708         -#define COMPLETION_PRAGMAS       2
  1709         -#define COMPLETION_FUNCTIONS     3
  1710         -#define COMPLETION_COLLATIONS    4
  1711         -#define COMPLETION_INDEXES       5
  1712         -#define COMPLETION_TRIGGERS      6
  1713         -#define COMPLETION_DATABASES     7
  1714         -#define COMPLETION_TABLES        8
  1715         -#define COMPLETION_COLUMNS       9
  1716         -#define COMPLETION_MODULES       10
  1717         -#define COMPLETION_EOF           11
  1718         -
  1719         -/*
  1720         -** The completionConnect() method is invoked to create a new
  1721         -** completion_vtab that describes the completion virtual table.
  1722         -**
  1723         -** Think of this routine as the constructor for completion_vtab objects.
  1724         -**
  1725         -** All this routine needs to do is:
  1726         -**
  1727         -**    (1) Allocate the completion_vtab object and initialize all fields.
  1728         -**
  1729         -**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
  1730         -**        result set of queries against completion will look like.
  1731         -*/
  1732         -static int completionConnect(
  1733         -  sqlite3 *db,
  1734         -  void *pAux,
  1735         -  int argc, const char *const*argv,
  1736         -  sqlite3_vtab **ppVtab,
  1737         -  char **pzErr
  1738         -){
  1739         -  completion_vtab *pNew;
  1740         -  int rc;
  1741         -
  1742         -  (void)(pAux);    /* Unused parameter */
  1743         -  (void)(argc);    /* Unused parameter */
  1744         -  (void)(argv);    /* Unused parameter */
  1745         -  (void)(pzErr);   /* Unused parameter */
  1746         -
  1747         -/* Column numbers */
  1748         -#define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
  1749         -#define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
  1750         -#define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
  1751         -#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
  1752         -
  1753         -  rc = sqlite3_declare_vtab(db,
  1754         -      "CREATE TABLE x("
  1755         -      "  candidate TEXT,"
  1756         -      "  prefix TEXT HIDDEN,"
  1757         -      "  wholeline TEXT HIDDEN,"
  1758         -      "  phase INT HIDDEN"        /* Used for debugging only */
  1759         -      ")");
  1760         -  if( rc==SQLITE_OK ){
  1761         -    pNew = sqlite3_malloc( sizeof(*pNew) );
  1762         -    *ppVtab = (sqlite3_vtab*)pNew;
  1763         -    if( pNew==0 ) return SQLITE_NOMEM;
  1764         -    memset(pNew, 0, sizeof(*pNew));
  1765         -    pNew->db = db;
  1766         -  }
  1767         -  return rc;
  1768         -}
  1769         -
  1770         -/*
  1771         -** This method is the destructor for completion_cursor objects.
  1772         -*/
  1773         -static int completionDisconnect(sqlite3_vtab *pVtab){
  1774         -  sqlite3_free(pVtab);
  1775         -  return SQLITE_OK;
  1776         -}
  1777         -
  1778         -/*
  1779         -** Constructor for a new completion_cursor object.
  1780         -*/
  1781         -static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
  1782         -  completion_cursor *pCur;
  1783         -  pCur = sqlite3_malloc( sizeof(*pCur) );
  1784         -  if( pCur==0 ) return SQLITE_NOMEM;
  1785         -  memset(pCur, 0, sizeof(*pCur));
  1786         -  pCur->db = ((completion_vtab*)p)->db;
  1787         -  *ppCursor = &pCur->base;
  1788         -  return SQLITE_OK;
  1789         -}
  1790         -
  1791         -/*
  1792         -** Reset the completion_cursor.
  1793         -*/
  1794         -static void completionCursorReset(completion_cursor *pCur){
  1795         -  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
  1796         -  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
  1797         -  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
  1798         -  pCur->j = 0;
  1799         -}
  1800         -
  1801         -/*
  1802         -** Destructor for a completion_cursor.
  1803         -*/
  1804         -static int completionClose(sqlite3_vtab_cursor *cur){
  1805         -  completionCursorReset((completion_cursor*)cur);
  1806         -  sqlite3_free(cur);
  1807         -  return SQLITE_OK;
  1808         -}
  1809         -
  1810         -/*
  1811         -** All SQL keywords understood by SQLite
  1812         -*/
  1813         -static const char *completionKwrds[] = {
  1814         -  "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
  1815         -  "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
  1816         -  "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
  1817         -  "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
  1818         -  "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
  1819         -  "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
  1820         -  "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
  1821         -  "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
  1822         -  "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
  1823         -  "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
  1824         -  "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
  1825         -  "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
  1826         -  "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
  1827         -  "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
  1828         -  "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
  1829         -  "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
  1830         -  "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
  1831         -  "WITH", "WITHOUT",
  1832         -};
  1833         -#define completionKwCount \
  1834         -   (int)(sizeof(completionKwrds)/sizeof(completionKwrds[0]))
  1835         -
  1836         -/*
  1837         -** Advance a completion_cursor to its next row of output.
  1838         -**
  1839         -** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
  1840         -** record the current state of the scan.  This routine sets ->zCurrentRow
  1841         -** to the current row of output and then returns.  If no more rows remain,
  1842         -** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
  1843         -** table that has reached the end of its scan.
  1844         -**
  1845         -** The current implementation just lists potential identifiers and
  1846         -** keywords and filters them by zPrefix.  Future enhancements should
  1847         -** take zLine into account to try to restrict the set of identifiers and
  1848         -** keywords based on what would be legal at the current point of input.
  1849         -*/
  1850         -static int completionNext(sqlite3_vtab_cursor *cur){
  1851         -  completion_cursor *pCur = (completion_cursor*)cur;
  1852         -  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
  1853         -  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
  1854         -  pCur->iRowid++;
  1855         -  while( pCur->ePhase!=COMPLETION_EOF ){
  1856         -    switch( pCur->ePhase ){
  1857         -      case COMPLETION_KEYWORDS: {
  1858         -        if( pCur->j >= completionKwCount ){
  1859         -          pCur->zCurrentRow = 0;
  1860         -          pCur->ePhase = COMPLETION_DATABASES;
  1861         -        }else{
  1862         -          pCur->zCurrentRow = completionKwrds[pCur->j++];
  1863         -        }
  1864         -        iCol = -1;
  1865         -        break;
  1866         -      }
  1867         -      case COMPLETION_DATABASES: {
  1868         -        if( pCur->pStmt==0 ){
  1869         -          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
  1870         -                             &pCur->pStmt, 0);
  1871         -        }
  1872         -        iCol = 1;
  1873         -        eNextPhase = COMPLETION_TABLES;
  1874         -        break;
  1875         -      }
  1876         -      case COMPLETION_TABLES: {
  1877         -        if( pCur->pStmt==0 ){
  1878         -          sqlite3_stmt *pS2;
  1879         -          char *zSql = 0;
  1880         -          const char *zSep = "";
  1881         -          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
  1882         -          while( sqlite3_step(pS2)==SQLITE_ROW ){
  1883         -            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
  1884         -            zSql = sqlite3_mprintf(
  1885         -               "%z%s"
  1886         -               "SELECT name FROM \"%w\".sqlite_master"
  1887         -               " WHERE type='table'",
  1888         -               zSql, zSep, zDb
  1889         -            );
  1890         -            if( zSql==0 ) return SQLITE_NOMEM;
  1891         -            zSep = " UNION ";
  1892         -          }
  1893         -          sqlite3_finalize(pS2);
  1894         -          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
  1895         -          sqlite3_free(zSql);
  1896         -        }
  1897         -        iCol = 0;
  1898         -        eNextPhase = COMPLETION_COLUMNS;
  1899         -        break;
  1900         -      }
  1901         -      case COMPLETION_COLUMNS: {
  1902         -        if( pCur->pStmt==0 ){
  1903         -          sqlite3_stmt *pS2;
  1904         -          char *zSql = 0;
  1905         -          const char *zSep = "";
  1906         -          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
  1907         -          while( sqlite3_step(pS2)==SQLITE_ROW ){
  1908         -            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
  1909         -            zSql = sqlite3_mprintf(
  1910         -               "%z%s"
  1911         -               "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
  1912         -                       " JOIN pragma_table_info(sm.name,%Q) AS pti"
  1913         -               " WHERE sm.type='table'",
  1914         -               zSql, zSep, zDb, zDb
  1915         -            );
  1916         -            if( zSql==0 ) return SQLITE_NOMEM;
  1917         -            zSep = " UNION ";
  1918         -          }
  1919         -          sqlite3_finalize(pS2);
  1920         -          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
  1921         -          sqlite3_free(zSql);
  1922         -        }
  1923         -        iCol = 0;
  1924         -        eNextPhase = COMPLETION_EOF;
  1925         -        break;
  1926         -      }
  1927         -    }
  1928         -    if( iCol<0 ){
  1929         -      /* This case is when the phase presets zCurrentRow */
  1930         -      if( pCur->zCurrentRow==0 ) continue;
  1931         -    }else{
  1932         -      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
  1933         -        /* Extract the next row of content */
  1934         -        pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
  1935         -      }else{
  1936         -        /* When all rows are finished, advance to the next phase */
  1937         -        sqlite3_finalize(pCur->pStmt);
  1938         -        pCur->pStmt = 0;
  1939         -        pCur->ePhase = eNextPhase;
  1940         -        continue;
  1941         -      }
  1942         -    }
  1943         -    if( pCur->nPrefix==0 ) break;
  1944         -    if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
  1945         -      break;
  1946         -    }
  1947         -  }
  1948         -
  1949         -  return SQLITE_OK;
  1950         -}
  1951         -
  1952         -/*
  1953         -** Return values of columns for the row at which the completion_cursor
  1954         -** is currently pointing.
  1955         -*/
  1956         -static int completionColumn(
  1957         -  sqlite3_vtab_cursor *cur,   /* The cursor */
  1958         -  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  1959         -  int i                       /* Which column to return */
  1960         -){
  1961         -  completion_cursor *pCur = (completion_cursor*)cur;
  1962         -  switch( i ){
  1963         -    case COMPLETION_COLUMN_CANDIDATE: {
  1964         -      sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
  1965         -      break;
  1966         -    }
  1967         -    case COMPLETION_COLUMN_PREFIX: {
  1968         -      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
  1969         -      break;
  1970         -    }
  1971         -    case COMPLETION_COLUMN_WHOLELINE: {
  1972         -      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
  1973         -      break;
  1974         -    }
  1975         -    case COMPLETION_COLUMN_PHASE: {
  1976         -      sqlite3_result_int(ctx, pCur->ePhase);
  1977         -      break;
  1978         -    }
  1979         -  }
  1980         -  return SQLITE_OK;
  1981         -}
  1982         -
  1983         -/*
  1984         -** Return the rowid for the current row.  In this implementation, the
  1985         -** rowid is the same as the output value.
  1986         -*/
  1987         -static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  1988         -  completion_cursor *pCur = (completion_cursor*)cur;
  1989         -  *pRowid = pCur->iRowid;
  1990         -  return SQLITE_OK;
  1991         -}
  1992         -
  1993         -/*
  1994         -** Return TRUE if the cursor has been moved off of the last
  1995         -** row of output.
  1996         -*/
  1997         -static int completionEof(sqlite3_vtab_cursor *cur){
  1998         -  completion_cursor *pCur = (completion_cursor*)cur;
  1999         -  return pCur->ePhase >= COMPLETION_EOF;
  2000         -}
  2001         -
  2002         -/*
  2003         -** This method is called to "rewind" the completion_cursor object back
  2004         -** to the first row of output.  This method is always called at least
  2005         -** once prior to any call to completionColumn() or completionRowid() or 
  2006         -** completionEof().
  2007         -*/
  2008         -static int completionFilter(
  2009         -  sqlite3_vtab_cursor *pVtabCursor, 
  2010         -  int idxNum, const char *idxStr,
  2011         -  int argc, sqlite3_value **argv
  2012         -){
  2013         -  completion_cursor *pCur = (completion_cursor *)pVtabCursor;
  2014         -  int iArg = 0;
  2015         -  (void)(idxStr);   /* Unused parameter */
  2016         -  (void)(argc);     /* Unused parameter */
  2017         -  completionCursorReset(pCur);
  2018         -  if( idxNum & 1 ){
  2019         -    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
  2020         -    if( pCur->nPrefix>0 ){
  2021         -      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
  2022         -      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
  2023         -    }
  2024         -    iArg++;
  2025         -  }
  2026         -  if( idxNum & 2 ){
  2027         -    pCur->nLine = sqlite3_value_bytes(argv[iArg]);
  2028         -    if( pCur->nLine>0 ){
  2029         -      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
  2030         -      if( pCur->zLine==0 ) return SQLITE_NOMEM;
  2031         -    }
  2032         -    iArg++;
  2033         -  }
  2034         -  if( pCur->zLine!=0 && pCur->zPrefix==0 ){
  2035         -    int i = pCur->nLine;
  2036         -    while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
  2037         -      i--;
  2038         -    }
  2039         -    pCur->nPrefix = pCur->nLine - i;
  2040         -    if( pCur->nPrefix>0 ){
  2041         -      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
  2042         -      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
  2043         -    }
  2044         -  }
  2045         -  pCur->iRowid = 0;
  2046         -  pCur->ePhase = COMPLETION_FIRST_PHASE;
  2047         -  return completionNext(pVtabCursor);
  2048         -}
  2049         -
  2050         -/*
  2051         -** SQLite will invoke this method one or more times while planning a query
  2052         -** that uses the completion virtual table.  This routine needs to create
  2053         -** a query plan for each invocation and compute an estimated cost for that
  2054         -** plan.
  2055         -**
  2056         -** There are two hidden parameters that act as arguments to the table-valued
  2057         -** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
  2058         -** is available and bit 1 is set if "wholeline" is available.
  2059         -*/
  2060         -static int completionBestIndex(
  2061         -  sqlite3_vtab *tab,
  2062         -  sqlite3_index_info *pIdxInfo
  2063         -){
  2064         -  int i;                 /* Loop over constraints */
  2065         -  int idxNum = 0;        /* The query plan bitmask */
  2066         -  int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
  2067         -  int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
  2068         -  int nArg = 0;          /* Number of arguments that completeFilter() expects */
  2069         -  const struct sqlite3_index_constraint *pConstraint;
  2070         -
  2071         -  (void)(tab);    /* Unused parameter */
  2072         -  pConstraint = pIdxInfo->aConstraint;
  2073         -  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  2074         -    if( pConstraint->usable==0 ) continue;
  2075         -    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
  2076         -    switch( pConstraint->iColumn ){
  2077         -      case COMPLETION_COLUMN_PREFIX:
  2078         -        prefixIdx = i;
  2079         -        idxNum |= 1;
  2080         -        break;
  2081         -      case COMPLETION_COLUMN_WHOLELINE:
  2082         -        wholelineIdx = i;
  2083         -        idxNum |= 2;
  2084         -        break;
  2085         -    }
  2086         -  }
  2087         -  if( prefixIdx>=0 ){
  2088         -    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
  2089         -    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
  2090         -  }
  2091         -  if( wholelineIdx>=0 ){
  2092         -    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
  2093         -    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
  2094         -  }
  2095         -  pIdxInfo->idxNum = idxNum;
  2096         -  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
  2097         -  pIdxInfo->estimatedRows = 500 - 100*nArg;
  2098         -  return SQLITE_OK;
  2099         -}
  2100         -
  2101         -/*
  2102         -** This following structure defines all the methods for the 
  2103         -** completion virtual table.
  2104         -*/
  2105         -static sqlite3_module completionModule = {
  2106         -  0,                         /* iVersion */
  2107         -  0,                         /* xCreate */
  2108         -  completionConnect,         /* xConnect */
  2109         -  completionBestIndex,       /* xBestIndex */
  2110         -  completionDisconnect,      /* xDisconnect */
  2111         -  0,                         /* xDestroy */
  2112         -  completionOpen,            /* xOpen - open a cursor */
  2113         -  completionClose,           /* xClose - close a cursor */
  2114         -  completionFilter,          /* xFilter - configure scan constraints */
  2115         -  completionNext,            /* xNext - advance a cursor */
  2116         -  completionEof,             /* xEof - check for end of scan */
  2117         -  completionColumn,          /* xColumn - read data */
  2118         -  completionRowid,           /* xRowid - read data */
  2119         -  0,                         /* xUpdate */
  2120         -  0,                         /* xBegin */
  2121         -  0,                         /* xSync */
  2122         -  0,                         /* xCommit */
  2123         -  0,                         /* xRollback */
  2124         -  0,                         /* xFindMethod */
  2125         -  0,                         /* xRename */
  2126         -  0,                         /* xSavepoint */
  2127         -  0,                         /* xRelease */
  2128         -  0                          /* xRollbackTo */
  2129         -};
  2130         -
  2131         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
  2132         -
  2133         -int sqlite3CompletionVtabInit(sqlite3 *db){
  2134         -  int rc = SQLITE_OK;
  2135         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  2136         -  rc = sqlite3_create_module(db, "completion", &completionModule, 0);
  2137         -#endif
  2138         -  return rc;
  2139         -}
  2140         -
  2141         -#ifdef _WIN32
  2142         -
  2143         -#endif
  2144         -int sqlite3_completion_init(
  2145         -  sqlite3 *db, 
  2146         -  char **pzErrMsg, 
  2147         -  const sqlite3_api_routines *pApi
  2148         -){
  2149         -  int rc = SQLITE_OK;
  2150         -  SQLITE_EXTENSION_INIT2(pApi);
  2151         -  (void)(pzErrMsg);  /* Unused parameter */
  2152         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  2153         -  rc = sqlite3CompletionVtabInit(db);
  2154         -#endif
  2155         -  return rc;
  2156         -}
  2157         -
  2158         -/************************* End ../ext/misc/completion.c ********************/
  2159         -
  2160         -#if defined(SQLITE_ENABLE_SESSION)
  2161         -/*
  2162         -** State information for a single open session
  2163         -*/
  2164         -typedef struct OpenSession OpenSession;
  2165         -struct OpenSession {
  2166         -  char *zName;             /* Symbolic name for this session */
  2167         -  int nFilter;             /* Number of xFilter rejection GLOB patterns */
  2168         -  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
  2169         -  sqlite3_session *p;      /* The open session */
  2170         -};
  2171         -#endif
  2172         -
  2173         -/*
  2174         -** Shell output mode information from before ".explain on",
  2175         -** saved so that it can be restored by ".explain off"
  2176         -*/
  2177         -typedef struct SavedModeInfo SavedModeInfo;
  2178         -struct SavedModeInfo {
  2179         -  int valid;          /* Is there legit data in here? */
  2180         -  int mode;           /* Mode prior to ".explain on" */
  2181         -  int showHeader;     /* The ".header" setting prior to ".explain on" */
  2182         -  int colWidth[100];  /* Column widths prior to ".explain on" */
  2183         -};
  2184         -
  2185         -/*
  2186         -** State information about the database connection is contained in an
  2187         -** instance of the following structure.
  2188         -*/
  2189         -typedef struct ShellState ShellState;
  2190         -struct ShellState {
  2191         -  sqlite3 *db;           /* The database */
  2192         -  int autoExplain;       /* Automatically turn on .explain mode */
  2193         -  int autoEQP;           /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
  2194         -  int statsOn;           /* True to display memory stats before each finalize */
  2195         -  int scanstatsOn;       /* True to display scan stats before each finalize */
  2196         -  int outCount;          /* Revert to stdout when reaching zero */
  2197         -  int cnt;               /* Number of records displayed so far */
  2198         -  FILE *out;             /* Write results here */
  2199         -  FILE *traceOut;        /* Output for sqlite3_trace() */
  2200         -  int nErr;              /* Number of errors seen */
  2201         -  int mode;              /* An output mode setting */
  2202         -  int cMode;             /* temporary output mode for the current query */
  2203         -  int normalMode;        /* Output mode before ".explain on" */
  2204         -  int writableSchema;    /* True if PRAGMA writable_schema=ON */
  2205         -  int showHeader;        /* True to show column names in List or Column mode */
  2206         -  int nCheck;            /* Number of ".check" commands run */
  2207         -  unsigned shellFlgs;    /* Various flags */
  2208         -  char *zDestTable;      /* Name of destination table when MODE_Insert */
  2209         -  char zTestcase[30];    /* Name of current test case */
  2210         -  char colSeparator[20]; /* Column separator character for several modes */
  2211         -  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
  2212         -  int colWidth[100];     /* Requested width of each column when in column mode*/
  2213         -  int actualWidth[100];  /* Actual width of each column */
  2214         -  char nullValue[20];    /* The text to print when a NULL comes back from
  2215         -                         ** the database */
  2216         -  char outfile[FILENAME_MAX]; /* Filename for *out */
  2217         -  const char *zDbFilename;    /* name of the database file */
  2218         -  char *zFreeOnClose;         /* Filename to free when closing */
  2219         -  const char *zVfs;           /* Name of VFS to use */
  2220         -  sqlite3_stmt *pStmt;   /* Current statement if any. */
  2221         -  FILE *pLog;            /* Write log output here */
  2222         -  int *aiIndent;         /* Array of indents used in MODE_Explain */
  2223         -  int nIndent;           /* Size of array aiIndent[] */
  2224         -  int iIndent;           /* Index of current op in aiIndent[] */
  2225         -#if defined(SQLITE_ENABLE_SESSION)
  2226         -  int nSession;             /* Number of active sessions */
  2227         -  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
  2228         -#endif
  2229         -};
  2230         -
  2231         -/*
  2232         -** These are the allowed shellFlgs values
  2233         -*/
  2234         -#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
  2235         -#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
  2236         -#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
  2237         -#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
  2238         -#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
  2239         -#define SHFLG_CountChanges   0x00000020 /* .changes setting */
  2240         -#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
  2241         -
  2242         -/*
  2243         -** Macros for testing and setting shellFlgs
  2244         -*/
  2245         -#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
  2246         -#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
  2247         -#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
  2248         -
  2249         -/*
  2250         -** These are the allowed modes.
  2251         -*/
  2252         -#define MODE_Line     0  /* One column per line.  Blank line between records */
  2253         -#define MODE_Column   1  /* One record per line in neat columns */
  2254         -#define MODE_List     2  /* One record per line with a separator */
  2255         -#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
  2256         -#define MODE_Html     4  /* Generate an XHTML table */
  2257         -#define MODE_Insert   5  /* Generate SQL "insert" statements */
  2258         -#define MODE_Quote    6  /* Quote values as for SQL */
  2259         -#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
  2260         -#define MODE_Csv      8  /* Quote strings, numbers are plain */
  2261         -#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
  2262         -#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
  2263         -#define MODE_Pretty  11  /* Pretty-print schemas */
  2264         -
  2265         -static const char *modeDescr[] = {
  2266         -  "line",
  2267         -  "column",
  2268         -  "list",
  2269         -  "semi",
  2270         -  "html",
  2271         -  "insert",
  2272         -  "quote",
  2273         -  "tcl",
  2274         -  "csv",
  2275         -  "explain",
  2276         -  "ascii",
  2277         -  "prettyprint",
  2278         -};
  2279         -
  2280         -/*
  2281         -** These are the column/row/line separators used by the various
  2282         -** import/export modes.
  2283         -*/
  2284         -#define SEP_Column    "|"
  2285         -#define SEP_Row       "\n"
  2286         -#define SEP_Tab       "\t"
  2287         -#define SEP_Space     " "
  2288         -#define SEP_Comma     ","
  2289         -#define SEP_CrLf      "\r\n"
  2290         -#define SEP_Unit      "\x1F"
  2291         -#define SEP_Record    "\x1E"
  2292         -
  2293         -/*
  2294         -** Number of elements in an array
  2295         -*/
  2296         -#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
  2297         -
  2298         -/*
  2299         -** A callback for the sqlite3_log() interface.
  2300         -*/
  2301         -static void shellLog(void *pArg, int iErrCode, const char *zMsg){
  2302         -  ShellState *p = (ShellState*)pArg;
  2303         -  if( p->pLog==0 ) return;
  2304         -  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
  2305         -  fflush(p->pLog);
  2306         -}
  2307         -
  2308         -/*
  2309         -** Output the given string as a hex-encoded blob (eg. X'1234' )
  2310         -*/
  2311         -static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
  2312         -  int i;
  2313         -  char *zBlob = (char *)pBlob;
  2314         -  raw_printf(out,"X'");
  2315         -  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
  2316         -  raw_printf(out,"'");
  2317         -}
  2318         -
  2319         -/*
  2320         -** Find a string that is not found anywhere in z[].  Return a pointer
  2321         -** to that string.
  2322         -**
  2323         -** Try to use zA and zB first.  If both of those are already found in z[]
  2324         -** then make up some string and store it in the buffer zBuf.
  2325         -*/
  2326         -static const char *unused_string(
  2327         -  const char *z,                    /* Result must not appear anywhere in z */
  2328         -  const char *zA, const char *zB,   /* Try these first */
  2329         -  char *zBuf                        /* Space to store a generated string */
  2330         -){
  2331         -  unsigned i = 0;
  2332         -  if( strstr(z, zA)==0 ) return zA;
  2333         -  if( strstr(z, zB)==0 ) return zB;
  2334         -  do{
  2335         -    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
  2336         -  }while( strstr(z,zBuf)!=0 );
  2337         -  return zBuf;
  2338         -}
  2339         -
  2340         -/*
  2341         -** Output the given string as a quoted string using SQL quoting conventions.
  2342         -**
  2343         -** See also: output_quoted_escaped_string()
  2344         -*/
  2345         -static void output_quoted_string(FILE *out, const char *z){
  2346         -  int i;
  2347         -  char c;
  2348         -  setBinaryMode(out, 1);
  2349         -  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
  2350         -  if( c==0 ){
  2351         -    utf8_printf(out,"'%s'",z);
  2352         -  }else{
  2353         -    raw_printf(out, "'");
  2354         -    while( *z ){
  2355         -      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
  2356         -      if( c=='\'' ) i++;
  2357         -      if( i ){
  2358         -        utf8_printf(out, "%.*s", i, z);
  2359         -        z += i;
  2360         -      }
  2361         -      if( c=='\'' ){
  2362         -        raw_printf(out, "'");
  2363         -        continue;
  2364         -      }
  2365         -      if( c==0 ){
  2366         -        break;
  2367         -      }
  2368         -      z++;
  2369         -    }
  2370         -    raw_printf(out, "'");
  2371         -  }
  2372         -  setTextMode(out, 1);
  2373         -}
  2374         -
  2375         -/*
  2376         -** Output the given string as a quoted string using SQL quoting conventions.
  2377         -** Additionallly , escape the "\n" and "\r" characters so that they do not
  2378         -** get corrupted by end-of-line translation facilities in some operating
  2379         -** systems.
  2380         -**
  2381         -** This is like output_quoted_string() but with the addition of the \r\n
  2382         -** escape mechanism.
  2383         -*/
  2384         -static void output_quoted_escaped_string(FILE *out, const char *z){
  2385         -  int i;
  2386         -  char c;
  2387         -  setBinaryMode(out, 1);
  2388         -  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
  2389         -  if( c==0 ){
  2390         -    utf8_printf(out,"'%s'",z);
  2391         -  }else{
  2392         -    const char *zNL = 0;
  2393         -    const char *zCR = 0;
  2394         -    int nNL = 0;
  2395         -    int nCR = 0;
  2396         -    char zBuf1[20], zBuf2[20];
  2397         -    for(i=0; z[i]; i++){
  2398         -      if( z[i]=='\n' ) nNL++;
  2399         -      if( z[i]=='\r' ) nCR++;
  2400         -    }
  2401         -    if( nNL ){
  2402         -      raw_printf(out, "replace(");
  2403         -      zNL = unused_string(z, "\\n", "\\012", zBuf1);
  2404         -    }
  2405         -    if( nCR ){
  2406         -      raw_printf(out, "replace(");
  2407         -      zCR = unused_string(z, "\\r", "\\015", zBuf2);
  2408         -    }
  2409         -    raw_printf(out, "'");
  2410         -    while( *z ){
  2411         -      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
  2412         -      if( c=='\'' ) i++;
  2413         -      if( i ){
  2414         -        utf8_printf(out, "%.*s", i, z);
  2415         -        z += i;
  2416         -      }
  2417         -      if( c=='\'' ){
  2418         -        raw_printf(out, "'");
  2419         -        continue;
  2420         -      }
  2421         -      if( c==0 ){
  2422         -        break;
  2423         -      }
  2424         -      z++;
  2425         -      if( c=='\n' ){
  2426         -        raw_printf(out, "%s", zNL);
  2427         -        continue;
  2428         -      }
  2429         -      raw_printf(out, "%s", zCR);
  2430         -    }
  2431         -    raw_printf(out, "'");
  2432         -    if( nCR ){
  2433         -      raw_printf(out, ",'%s',char(13))", zCR);
  2434         -    }
  2435         -    if( nNL ){
  2436         -      raw_printf(out, ",'%s',char(10))", zNL);
  2437         -    }
  2438         -  }
  2439         -  setTextMode(out, 1);
  2440         -}
  2441         -
  2442         -/*
  2443         -** Output the given string as a quoted according to C or TCL quoting rules.
  2444         -*/
  2445         -static void output_c_string(FILE *out, const char *z){
  2446         -  unsigned int c;
  2447         -  fputc('"', out);
  2448         -  while( (c = *(z++))!=0 ){
  2449         -    if( c=='\\' ){
  2450         -      fputc(c, out);
  2451         -      fputc(c, out);
  2452         -    }else if( c=='"' ){
  2453         -      fputc('\\', out);
  2454         -      fputc('"', out);
  2455         -    }else if( c=='\t' ){
  2456         -      fputc('\\', out);
  2457         -      fputc('t', out);
  2458         -    }else if( c=='\n' ){
  2459         -      fputc('\\', out);
  2460         -      fputc('n', out);
  2461         -    }else if( c=='\r' ){
  2462         -      fputc('\\', out);
  2463         -      fputc('r', out);
  2464         -    }else if( !isprint(c&0xff) ){
  2465         -      raw_printf(out, "\\%03o", c&0xff);
  2466         -    }else{
  2467         -      fputc(c, out);
  2468         -    }
  2469         -  }
  2470         -  fputc('"', out);
  2471         -}
  2472         -
  2473         -/*
  2474         -** Output the given string with characters that are special to
  2475         -** HTML escaped.
  2476         -*/
  2477         -static void output_html_string(FILE *out, const char *z){
  2478         -  int i;
  2479         -  if( z==0 ) z = "";
  2480         -  while( *z ){
  2481         -    for(i=0;   z[i]
  2482         -            && z[i]!='<'
  2483         -            && z[i]!='&'
  2484         -            && z[i]!='>'
  2485         -            && z[i]!='\"'
  2486         -            && z[i]!='\'';
  2487         -        i++){}
  2488         -    if( i>0 ){
  2489         -      utf8_printf(out,"%.*s",i,z);
  2490         -    }
  2491         -    if( z[i]=='<' ){
  2492         -      raw_printf(out,"&lt;");
  2493         -    }else if( z[i]=='&' ){
  2494         -      raw_printf(out,"&amp;");
  2495         -    }else if( z[i]=='>' ){
  2496         -      raw_printf(out,"&gt;");
  2497         -    }else if( z[i]=='\"' ){
  2498         -      raw_printf(out,"&quot;");
  2499         -    }else if( z[i]=='\'' ){
  2500         -      raw_printf(out,"&#39;");
  2501         -    }else{
  2502         -      break;
  2503         -    }
  2504         -    z += i + 1;
  2505         -  }
  2506         -}
  2507         -
  2508         -/*
  2509         -** If a field contains any character identified by a 1 in the following
  2510         -** array, then the string must be quoted for CSV.
  2511         -*/
  2512         -static const char needCsvQuote[] = {
  2513         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2514         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2515         -  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
  2516         -  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
  2517         -  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
  2518         -  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
  2519         -  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
  2520         -  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
  2521         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2522         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2523         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2524         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2525         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2526         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2527         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2528         -  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
  2529         -};
  2530         -
  2531         -/*
  2532         -** Output a single term of CSV.  Actually, p->colSeparator is used for
  2533         -** the separator, which may or may not be a comma.  p->nullValue is
  2534         -** the null value.  Strings are quoted if necessary.  The separator
  2535         -** is only issued if bSep is true.
  2536         -*/
  2537         -static void output_csv(ShellState *p, const char *z, int bSep){
  2538         -  FILE *out = p->out;
  2539         -  if( z==0 ){
  2540         -    utf8_printf(out,"%s",p->nullValue);
  2541         -  }else{
  2542         -    int i;
  2543         -    int nSep = strlen30(p->colSeparator);
  2544         -    for(i=0; z[i]; i++){
  2545         -      if( needCsvQuote[((unsigned char*)z)[i]]
  2546         -         || (z[i]==p->colSeparator[0] &&
  2547         -             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
  2548         -        i = 0;
  2549         -        break;
  2550         -      }
  2551         -    }
  2552         -    if( i==0 ){
  2553         -      putc('"', out);
  2554         -      for(i=0; z[i]; i++){
  2555         -        if( z[i]=='"' ) putc('"', out);
  2556         -        putc(z[i], out);
  2557         -      }
  2558         -      putc('"', out);
  2559         -    }else{
  2560         -      utf8_printf(out, "%s", z);
  2561         -    }
  2562         -  }
  2563         -  if( bSep ){
  2564         -    utf8_printf(p->out, "%s", p->colSeparator);
  2565         -  }
  2566         -}
  2567         -
  2568         -#ifdef SIGINT
  2569         -/*
  2570         -** This routine runs when the user presses Ctrl-C
  2571         -*/
  2572         -static void interrupt_handler(int NotUsed){
  2573         -  UNUSED_PARAMETER(NotUsed);
  2574         -  seenInterrupt++;
  2575         -  if( seenInterrupt>2 ) exit(1);
  2576         -  if( globalDb ) sqlite3_interrupt(globalDb);
  2577         -}
  2578         -#endif
  2579         -
  2580         -#ifndef SQLITE_OMIT_AUTHORIZATION
  2581         -/*
  2582         -** When the ".auth ON" is set, the following authorizer callback is
  2583         -** invoked.  It always returns SQLITE_OK.
  2584         -*/
  2585         -static int shellAuth(
  2586         -  void *pClientData,
  2587         -  int op,
  2588         -  const char *zA1,
  2589         -  const char *zA2,
  2590         -  const char *zA3,
  2591         -  const char *zA4
  2592         -){
  2593         -  ShellState *p = (ShellState*)pClientData;
  2594         -  static const char *azAction[] = { 0,
  2595         -     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
  2596         -     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
  2597         -     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
  2598         -     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
  2599         -     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
  2600         -     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
  2601         -     "PRAGMA",               "READ",                 "SELECT",
  2602         -     "TRANSACTION",          "UPDATE",               "ATTACH",
  2603         -     "DETACH",               "ALTER_TABLE",          "REINDEX",
  2604         -     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
  2605         -     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
  2606         -  };
  2607         -  int i;
  2608         -  const char *az[4];
  2609         -  az[0] = zA1;
  2610         -  az[1] = zA2;
  2611         -  az[2] = zA3;
  2612         -  az[3] = zA4;
  2613         -  utf8_printf(p->out, "authorizer: %s", azAction[op]);
  2614         -  for(i=0; i<4; i++){
  2615         -    raw_printf(p->out, " ");
  2616         -    if( az[i] ){
  2617         -      output_c_string(p->out, az[i]);
  2618         -    }else{
  2619         -      raw_printf(p->out, "NULL");
  2620         -    }
  2621         -  }
  2622         -  raw_printf(p->out, "\n");
  2623         -  return SQLITE_OK;
  2624         -}
  2625         -#endif
  2626         -
  2627         -/*
  2628         -** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
  2629         -**
  2630         -** This routine converts some CREATE TABLE statements for shadow tables
  2631         -** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
  2632         -*/
  2633         -static void printSchemaLine(FILE *out, const char *z, const char *zTail){
  2634         -  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
  2635         -    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
  2636         -  }else{
  2637         -    utf8_printf(out, "%s%s", z, zTail);
  2638         -  }
  2639         -}
  2640         -static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
  2641         -  char c = z[n];
  2642         -  z[n] = 0;
  2643         -  printSchemaLine(out, z, zTail);
  2644         -  z[n] = c;
  2645         -}
  2646         -
  2647         -/*
  2648         -** This is the callback routine that the shell
  2649         -** invokes for each row of a query result.
  2650         -*/
  2651         -static int shell_callback(
  2652         -  void *pArg,
  2653         -  int nArg,        /* Number of result columns */
  2654         -  char **azArg,    /* Text of each result column */
  2655         -  char **azCol,    /* Column names */
  2656         -  int *aiType      /* Column types */
  2657         -){
  2658         -  int i;
  2659         -  ShellState *p = (ShellState*)pArg;
  2660         -
  2661         -  if( azArg==0 ) return 0;
  2662         -  switch( p->cMode ){
  2663         -    case MODE_Line: {
  2664         -      int w = 5;
  2665         -      if( azArg==0 ) break;
  2666         -      for(i=0; i<nArg; i++){
  2667         -        int len = strlen30(azCol[i] ? azCol[i] : "");
  2668         -        if( len>w ) w = len;
  2669         -      }
  2670         -      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
  2671         -      for(i=0; i<nArg; i++){
  2672         -        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
  2673         -                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
  2674         -      }
  2675         -      break;
  2676         -    }
  2677         -    case MODE_Explain:
  2678         -    case MODE_Column: {
  2679         -      static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
  2680         -      const int *colWidth;
  2681         -      int showHdr;
  2682         -      char *rowSep;
  2683         -      if( p->cMode==MODE_Column ){
  2684         -        colWidth = p->colWidth;
  2685         -        showHdr = p->showHeader;
  2686         -        rowSep = p->rowSeparator;
  2687         -      }else{
  2688         -        colWidth = aExplainWidths;
  2689         -        showHdr = 1;
  2690         -        rowSep = SEP_Row;
  2691         -      }
  2692         -      if( p->cnt++==0 ){
  2693         -        for(i=0; i<nArg; i++){
  2694         -          int w, n;
  2695         -          if( i<ArraySize(p->colWidth) ){
  2696         -            w = colWidth[i];
  2697         -          }else{
  2698         -            w = 0;
  2699         -          }
  2700         -          if( w==0 ){
  2701         -            w = strlenChar(azCol[i] ? azCol[i] : "");
  2702         -            if( w<10 ) w = 10;
  2703         -            n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
  2704         -            if( w<n ) w = n;
  2705         -          }
  2706         -          if( i<ArraySize(p->actualWidth) ){
  2707         -            p->actualWidth[i] = w;
  2708         -          }
  2709         -          if( showHdr ){
  2710         -            utf8_width_print(p->out, w, azCol[i]);
  2711         -            utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
  2712         -          }
  2713         -        }
  2714         -        if( showHdr ){
  2715         -          for(i=0; i<nArg; i++){
  2716         -            int w;
  2717         -            if( i<ArraySize(p->actualWidth) ){
  2718         -               w = p->actualWidth[i];
  2719         -               if( w<0 ) w = -w;
  2720         -            }else{
  2721         -               w = 10;
  2722         -            }
  2723         -            utf8_printf(p->out,"%-*.*s%s",w,w,
  2724         -                   "----------------------------------------------------------"
  2725         -                   "----------------------------------------------------------",
  2726         -                    i==nArg-1 ? rowSep : "  ");
  2727         -          }
  2728         -        }
  2729         -      }
  2730         -      if( azArg==0 ) break;
  2731         -      for(i=0; i<nArg; i++){
  2732         -        int w;
  2733         -        if( i<ArraySize(p->actualWidth) ){
  2734         -           w = p->actualWidth[i];
  2735         -        }else{
  2736         -           w = 10;
  2737         -        }
  2738         -        if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
  2739         -          w = strlenChar(azArg[i]);
  2740         -        }
  2741         -        if( i==1 && p->aiIndent && p->pStmt ){
  2742         -          if( p->iIndent<p->nIndent ){
  2743         -            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
  2744         -          }
  2745         -          p->iIndent++;
  2746         -        }
  2747         -        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
  2748         -        utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
  2749         -      }
  2750         -      break;
  2751         -    }
  2752         -    case MODE_Semi: {   /* .schema and .fullschema output */
  2753         -      printSchemaLine(p->out, azArg[0], ";\n");
  2754         -      break;
  2755         -    }
  2756         -    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
  2757         -      char *z;
  2758         -      int j;
  2759         -      int nParen = 0;
  2760         -      char cEnd = 0;
  2761         -      char c;
  2762         -      int nLine = 0;
  2763         -      assert( nArg==1 );
  2764         -      if( azArg[0]==0 ) break;
  2765         -      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
  2766         -       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
  2767         -      ){
  2768         -        utf8_printf(p->out, "%s;\n", azArg[0]);
  2769         -        break;
  2770         -      }
  2771         -      z = sqlite3_mprintf("%s", azArg[0]);
  2772         -      j = 0;
  2773         -      for(i=0; IsSpace(z[i]); i++){}
  2774         -      for(; (c = z[i])!=0; i++){
  2775         -        if( IsSpace(c) ){
  2776         -          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
  2777         -        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
  2778         -