/ Check-in [86750c92]
Login

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

Overview
Comment:Merge latest trunk into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent
Files: files | file ages | folders
SHA3-256: 86750c927cb5ba36acad33d5279629699c42b598e70fdc4887b40a1a16771ff6
User & Date: dan 2018-09-28 20:58:10
Wiki:begin-concurrent
Context
2018-11-26
07:21
Merge latest trunk changes into this branch. check-in: 28a615a2 user: dan tags: begin-concurrent
2018-09-28
21:11
Merge latest begin-concurrent changes into this branch. check-in: 6f3dd980 user: dan tags: begin-concurrent-pnu
20:58
Merge latest trunk into this branch. check-in: 86750c92 user: dan tags: begin-concurrent
20:46
Have sqlite3changegroup_output_strm() call its output function more regularly. check-in: 8467c31a user: dan tags: trunk
2018-07-12
19:28
Merge latest trunk changes into this branch. check-in: 6a00a34e user: dan tags: begin-concurrent
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   346    346     $(TOP)/ext/fts3/fts3_unicode2.c \
   347    347     $(TOP)/ext/fts3/fts3_write.c
   348    348   SRC += \
   349    349     $(TOP)/ext/icu/sqliteicu.h \
   350    350     $(TOP)/ext/icu/icu.c
   351    351   SRC += \
   352    352     $(TOP)/ext/rtree/rtree.h \
   353         -  $(TOP)/ext/rtree/rtree.c
          353  +  $(TOP)/ext/rtree/rtree.c \
          354  +  $(TOP)/ext/rtree/geopoly.c
   354    355   SRC += \
   355    356     $(TOP)/ext/session/sqlite3session.c \
   356    357     $(TOP)/ext/session/sqlite3session.h
   357    358   SRC += \
   358    359     $(TOP)/ext/rbu/sqlite3rbu.h \
   359    360     $(TOP)/ext/rbu/sqlite3rbu.c
   360    361   SRC += \
................................................................................
   432    433     $(TOP)/ext/expert/sqlite3expert.c \
   433    434     $(TOP)/ext/expert/test_expert.c \
   434    435     $(TOP)/ext/misc/amatch.c \
   435    436     $(TOP)/ext/misc/carray.c \
   436    437     $(TOP)/ext/misc/closure.c \
   437    438     $(TOP)/ext/misc/csv.c \
   438    439     $(TOP)/ext/misc/eval.c \
          440  +  $(TOP)/ext/misc/explain.c \
   439    441     $(TOP)/ext/misc/fileio.c \
   440    442     $(TOP)/ext/misc/fuzzer.c \
   441    443     $(TOP)/ext/fts5/fts5_tcl.c \
   442    444     $(TOP)/ext/fts5/fts5_test_mi.c \
   443    445     $(TOP)/ext/fts5/fts5_test_tok.c \
   444    446     $(TOP)/ext/misc/ieee754.c \
   445    447     $(TOP)/ext/misc/mmapwarm.c \
................................................................................
   548    550     $(TOP)/ext/fts2/fts2_tokenizer.h
   549    551   EXTHDR += \
   550    552     $(TOP)/ext/fts3/fts3.h \
   551    553     $(TOP)/ext/fts3/fts3Int.h \
   552    554     $(TOP)/ext/fts3/fts3_hash.h \
   553    555     $(TOP)/ext/fts3/fts3_tokenizer.h
   554    556   EXTHDR += \
   555         -  $(TOP)/ext/rtree/rtree.h
          557  +  $(TOP)/ext/rtree/rtree.h \
          558  +  $(TOP)/ext/rtree/geopoly.c
   556    559   EXTHDR += \
   557    560     $(TOP)/ext/icu/sqliteicu.h
   558    561   EXTHDR += \
   559    562     $(TOP)/ext/rtree/sqlite3rtree.h
   560    563   
   561    564   # executables needed for testing
   562    565   #
................................................................................
  1272   1275   
  1273   1276   changeset$(TEXE):	$(TOP)/ext/session/changeset.c sqlite3.lo
  1274   1277   	$(LTLINK) -o $@ $(TOP)/ext/session/changeset.c sqlite3.lo $(TLIBS)
  1275   1278   
  1276   1279   rollback-test$(TEXE):	$(TOP)/tool/rollback-test.c sqlite3.lo
  1277   1280   	$(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS)
  1278   1281   
         1282  +atrc$(TEXX): $(TOP)/test/atrc.c sqlite3.lo
         1283  +	$(LTLINK) -o $@ $(TOP)/test/atrc.c sqlite3.lo $(TLIBS)
         1284  +
  1279   1285   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h
  1280   1286   	$(LTLINK) -I. -o $@ $(TOP)/tool/logest.c
  1281   1287   
  1282   1288   wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.lo
  1283   1289   	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.lo $(TLIBS)
  1284   1290   
  1285   1291   speedtest1$(TEXE):	$(TOP)/test/speedtest1.c sqlite3.c

Changes to Makefile.msc.

   335    335   # These are the "standard" SQLite compilation options used when compiling for
   336    336   # the Windows platform.
   337    337   #
   338    338   !IFNDEF OPT_FEATURE_FLAGS
   339    339   !IF $(MINIMAL_AMALGAMATION)==0
   340    340   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1
   341    341   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1
          342  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_GEOPOLY=1
          343  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_JSON1=1
          344  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1
          345  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1
          346  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBSTAT_VTAB=1
          347  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_INTROSPECTION_PRAGMAS=1
   342    348   !ENDIF
   343    349   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1
   344    350   !ENDIF
   345    351   
   346    352   # Should the session extension be enabled?  If so, add compilation options
   347    353   # to enable it.
   348    354   #
................................................................................
   616    622   
   617    623   # This is the source code that the shell executable should be compiled
   618    624   # with.
   619    625   #
   620    626   !IFNDEF SHELL_CORE_SRC
   621    627   !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0
   622    628   SHELL_CORE_SRC =
          629  +# <<mark>>
          630  +!ELSEIF $(USE_AMALGAMATION)==0
          631  +SHELL_CORE_SRC =
          632  +# <</mark>>
   623    633   !ELSE
   624    634   SHELL_CORE_SRC = $(SQLITE3C)
   625    635   !ENDIF
   626    636   !ENDIF
   627    637   
   628    638   # This is the core library that the shell executable should depend on.
   629    639   #
   630    640   !IFNDEF SHELL_CORE_DEP
   631    641   !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0
   632    642   SHELL_CORE_DEP = $(SQLITE3DLL)
          643  +# <<mark>>
          644  +!ELSEIF $(USE_AMALGAMATION)==0
          645  +SHELL_CORE_DEP = libsqlite3.lib
          646  +# <</mark>>
   633    647   !ELSE
   634    648   SHELL_CORE_DEP =
   635    649   !ENDIF
   636    650   !ENDIF
   637    651   
   638    652   # <<mark>>
   639    653   # If zlib support is enabled, add the dependencies for it.
................................................................................
   645    659   # <</mark>>
   646    660   
   647    661   # This is the core library that the shell executable should link with.
   648    662   #
   649    663   !IFNDEF SHELL_CORE_LIB
   650    664   !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0
   651    665   SHELL_CORE_LIB = $(SQLITE3LIB)
          666  +# <<mark>>
          667  +!ELSEIF $(USE_AMALGAMATION)==0
          668  +SHELL_CORE_LIB = libsqlite3.lib
          669  +# <</mark>>
   652    670   !ELSE
   653    671   SHELL_CORE_LIB =
   654    672   !ENDIF
   655    673   !ENDIF
   656    674   
   657    675   # These are additional linker options used for the shell executable.
   658    676   #
................................................................................
  1180   1198            date.lo dbpage.lo dbstat.lo delete.lo \
  1181   1199            expr.lo fault.lo fkey.lo \
  1182   1200            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
  1183   1201            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
  1184   1202            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
  1185   1203            fts5.lo \
  1186   1204            func.lo global.lo hash.lo \
  1187         -         icu.lo insert.lo legacy.lo loadext.lo \
         1205  +         icu.lo insert.lo json1.lo legacy.lo loadext.lo \
  1188   1206            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
  1189   1207            memdb.lo memjournal.lo \
  1190   1208            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
  1191   1209            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
  1192   1210            pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
  1193   1211            random.lo resolve.lo rowset.lo rtree.lo \
  1194         -         sqlite3session.lo select.lo sqlite3rbu.lo status.lo \
         1212  +         sqlite3session.lo select.lo sqlite3rbu.lo status.lo stmt.lo \
  1195   1213            table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
  1196   1214            update.lo upsert.lo util.lo vacuum.lo \
  1197   1215            vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
  1198   1216            vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
  1199   1217            window.lo utf.lo vtab.lo
  1200   1218   # <</mark>>
  1201   1219   
................................................................................
  1397   1415   SRC09 = \
  1398   1416     $(TOP)\ext\fts3\fts3.h \
  1399   1417     $(TOP)\ext\fts3\fts3Int.h \
  1400   1418     $(TOP)\ext\fts3\fts3_hash.h \
  1401   1419     $(TOP)\ext\fts3\fts3_tokenizer.h \
  1402   1420     $(TOP)\ext\icu\sqliteicu.h \
  1403   1421     $(TOP)\ext\rtree\rtree.h \
         1422  +  $(TOP)\ext\rtree\geopoly.c \
  1404   1423     $(TOP)\ext\rbu\sqlite3rbu.h \
  1405   1424     $(TOP)\ext\session\sqlite3session.h
  1406   1425   
  1407   1426   # Generated source code files
  1408   1427   #
  1409   1428   SRC10 = \
  1410   1429     opcodes.c \
................................................................................
  1492   1511     $(TOP)\ext\expert\sqlite3expert.c \
  1493   1512     $(TOP)\ext\expert\test_expert.c \
  1494   1513     $(TOP)\ext\misc\amatch.c \
  1495   1514     $(TOP)\ext\misc\carray.c \
  1496   1515     $(TOP)\ext\misc\closure.c \
  1497   1516     $(TOP)\ext\misc\csv.c \
  1498   1517     $(TOP)\ext\misc\eval.c \
         1518  +  $(TOP)\ext\misc\explain.c \
  1499   1519     $(TOP)\ext\misc\fileio.c \
  1500   1520     $(TOP)\ext\misc\fuzzer.c \
  1501   1521     $(TOP)\ext\fts5\fts5_tcl.c \
  1502   1522     $(TOP)\ext\fts5\fts5_test_mi.c \
  1503   1523     $(TOP)\ext\fts5\fts5_test_tok.c \
  1504   1524     $(TOP)\ext\misc\ieee754.c \
  1505   1525     $(TOP)\ext\misc\mmapwarm.c \
................................................................................
  1571   1591     $(TOP)\ext\fts2\fts2_tokenizer.h
  1572   1592   EXTHDR = $(EXTHDR) \
  1573   1593     $(TOP)\ext\fts3\fts3.h \
  1574   1594     $(TOP)\ext\fts3\fts3Int.h \
  1575   1595     $(TOP)\ext\fts3\fts3_hash.h \
  1576   1596     $(TOP)\ext\fts3\fts3_tokenizer.h
  1577   1597   EXTHDR = $(EXTHDR) \
  1578         -  $(TOP)\ext\rtree\rtree.h
         1598  +  $(TOP)\ext\rtree\rtree.h \
         1599  +  $(TOP)\ext\rtree\geopoly.c
  1579   1600   EXTHDR = $(EXTHDR) \
  1580   1601     $(TOP)\ext\icu\sqliteicu.h
  1581   1602   EXTHDR = $(EXTHDR) \
  1582   1603     $(TOP)\ext\rtree\sqlite3rtree.h
  1583   1604   EXTHDR = $(EXTHDR) \
  1584   1605     $(TOP)\ext\session\sqlite3session.h
  1585   1606   
................................................................................
  1605   1626     $(TOP)\test\fuzzdata6.db
  1606   1627   # <</mark>>
  1607   1628   
  1608   1629   # Additional compiler options for the shell.  These are only effective
  1609   1630   # when the shell is not being dynamically linked.
  1610   1631   #
  1611   1632   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
  1612         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
  1613         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
  1614         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_INTROSPECTION_PRAGMAS
  1615         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_RTREE
         1633  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_FTS4=1
         1634  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1
         1635  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1
  1616   1636   !ENDIF
  1617   1637   
  1618   1638   # <<mark>>
  1619   1639   # Extra compiler options for various test tools.
  1620   1640   #
  1621   1641   MPTESTER_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5
  1622   1642   FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1
................................................................................
  1693   1713   
  1694   1714   scrub.exe:	$(TOP)\ext\misc\scrub.c $(SQLITE3C) $(SQLITE3H)
  1695   1715   	$(LTLINK) $(NO_WARN) -DSCRUB_STANDALONE=1 $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1696   1716   
  1697   1717   srcck1.exe:	$(TOP)\tool\srcck1.c
  1698   1718   	$(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\srcck1.c
  1699   1719   
  1700         -sourcetest:	srcck1.exe sqlite3.c
  1701         -	srcck1.exe sqlite3.c
         1720  +sourcetest:	srcck1.exe $(SQLITE3C)
         1721  +	srcck1.exe $(SQLITE3C)
  1702   1722   
  1703   1723   fuzzershell.exe:	$(TOP)\tool\fuzzershell.c $(SQLITE3C) $(SQLITE3H)
  1704   1724   	$(LTLINK) $(NO_WARN) $(FUZZERSHELL_COMPILE_OPTS) $(TOP)\tool\fuzzershell.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1705   1725   
  1706   1726   dbfuzz.exe:	$(TOP)\test\dbfuzz.c $(SQLITE3C) $(SQLITE3H)
  1707   1727   	$(LTLINK) $(NO_WARN) $(DBFUZZ_COMPILE_OPTS) $(TOP)\test\dbfuzz.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1708   1728   
................................................................................
  2180   2200   
  2181   2201   fts3_unicode2.lo:	$(TOP)\ext\fts3\fts3_unicode2.c $(HDR) $(EXTHDR)
  2182   2202   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_unicode2.c
  2183   2203   
  2184   2204   fts3_write.lo:	$(TOP)\ext\fts3\fts3_write.c $(HDR) $(EXTHDR)
  2185   2205   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_write.c
  2186   2206   
         2207  +json1.lo:	$(TOP)\ext\misc\json1.c $(HDR) $(EXTHDR)
         2208  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\misc\json1.c
         2209  +
         2210  +stmt.lo:	$(TOP)\ext\misc\stmt.c $(HDR) $(EXTHDR)
         2211  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\misc\stmt.c
         2212  +
  2187   2213   rtree.lo:	$(TOP)\ext\rtree\rtree.c $(HDR) $(EXTHDR)
  2188   2214   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\rtree\rtree.c
  2189   2215   
  2190   2216   sqlite3session.lo:	$(TOP)\ext\session\sqlite3session.c $(HDR) $(EXTHDR)
  2191   2217   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\session\sqlite3session.c
  2192   2218   
  2193   2219   # FTS5 things
................................................................................
  2262   2288   # hidden when the library is built via the amalgamation).
  2263   2289   #
  2264   2290   TESTFIXTURE_FLAGS = -DTCLSH_INIT_PROC=sqlite3TestInit -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
  2265   2291   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE=""
  2266   2292   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CORE $(NO_WARN)
  2267   2293   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  2268   2294   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_DEFAULT_PAGE_SIZE=1024
  2269         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB
  2270         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB
  2271         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_JSON1
         2295  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1
         2296  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1
         2297  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_JSON1=1
  2272   2298   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) $(TEST_CCONV_OPTS)
  2273   2299   
  2274   2300   TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2)
  2275   2301   TESTFIXTURE_SRC1 = $(TESTEXT) $(SQLITE3C)
  2276   2302   !IF $(USE_AMALGAMATION)==0
  2277   2303   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
  2278   2304   !ELSE
................................................................................
  2420   2446   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2421   2447   		$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2422   2448   
  2423   2449   rollback-test.exe:	$(TOP)\tool\rollback-test.c $(SQLITE3C) $(SQLITE3H)
  2424   2450   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2425   2451   		$(TOP)\tool\rollback-test.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2426   2452   
         2453  +atrc.exe:	$(TOP)\test\atrc.c $(SQLITE3C) $(SQLITE3H)
         2454  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
         2455  +		$(TOP)\test\atrc.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         2456  +
  2427   2457   LogEst.exe:	$(TOP)\tool\logest.c $(SQLITE3H)
  2428   2458   	$(LTLINK) $(NO_WARN) $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS)
  2429   2459   
  2430   2460   wordcount.exe:	$(TOP)\test\wordcount.c $(SQLITE3C) $(SQLITE3H)
  2431   2461   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2432   2462   		$(TOP)\test\wordcount.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2433   2463   
................................................................................
  2451   2481   # <</mark>>
  2452   2482   
  2453   2483   clean:
  2454   2484   	del /Q *.exp *.lo *.ilk *.lib *.obj *.ncb *.pdb *.sdf *.suo 2>NUL
  2455   2485   	del /Q *.bsc *.def *.cod *.da *.bb *.bbg *.vc gmon.out 2>NUL
  2456   2486   	del /Q $(SQLITE3EXE) $(SQLITE3DLL) Replace.exe 2>NUL
  2457   2487   # <<mark>>
  2458         -	del /Q sqlite3.c sqlite3.h 2>NUL
  2459   2488   	del /Q opcodes.c opcodes.h 2>NUL
  2460   2489   	del /Q lemon.* lempar.c parse.* 2>NUL
  2461   2490   	del /Q mksourceid.* mkkeywordhash.* keywordhash.h 2>NUL
  2462   2491   	del /Q notasharedlib.* 2>NUL
  2463   2492   	-rmdir /Q/S .deps 2>NUL
  2464   2493   	-rmdir /Q/S .libs 2>NUL
  2465   2494   	-rmdir /Q/S tsrc 2>NUL
................................................................................
  2468   2497   	del /Q lsm.dll lsmtest.exe 2>NUL
  2469   2498   	del /Q testloadext.dll 2>NUL
  2470   2499   	del /Q testfixture.exe test.db 2>NUL
  2471   2500   	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe dbdump.exe 2>NUL
  2472   2501   	del /Q changeset.exe 2>NUL
  2473   2502   	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL
  2474   2503   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2475         -	del /Q sqlite3.c sqlite3-*.c 2>NUL
         2504  +	del /Q sqlite3.c sqlite3-*.c sqlite3.h 2>NUL
  2476   2505   	del /Q sqlite3rc.h 2>NUL
  2477   2506   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2478   2507   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2479   2508   	del /Q sqlite-*-output.vsix 2>NUL
  2480   2509   	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2481   2510   	del /Q sqltclsh.* 2>NUL
  2482   2511   	del /Q dbfuzz.exe sessionfuzz.exe 2>NUL
  2483   2512   	del /Q kvtest.exe ossshell.exe scrub.exe 2>NUL
  2484   2513   	del /Q showshm.exe sqlite3_checker.* sqlite3_expert.exe 2>NUL
  2485   2514   	del /Q fts5.* fts5parse.* 2>NUL
  2486   2515   	del /Q lsm.h lsm1.c 2>NUL
  2487   2516   # <</mark>>

Changes to README.md.

     1      1   <h1 align="center">SQLite Source Repository</h1>
     2      2   
     3         -This repository contains the complete source code for the SQLite database
     4         -engine.  Some test scripts are also included.  However, many other test scripts
            3  +This repository contains the complete source code for the 
            4  +[SQLite database engine](https://sqlite.org/).  Some test scripts 
            5  +are also included.  However, many other test scripts
     5      6   and most of the documentation are managed separately.
     6      7   
     7      8   SQLite [does not use Git](https://sqlite.org/whynotgit.html).
     8      9   If you are reading this on GitHub, then you are looking at an
     9     10   unofficial mirror. See <https://sqlite.org/src> for the official
    10     11   repository.
    11     12   

Changes to VERSION.

     1         -3.25.0
            1  +3.26.0

Changes to autoconf/Makefile.msc.

   273    273   # These are the "standard" SQLite compilation options used when compiling for
   274    274   # the Windows platform.
   275    275   #
   276    276   !IFNDEF OPT_FEATURE_FLAGS
   277    277   !IF $(MINIMAL_AMALGAMATION)==0
   278    278   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1
   279    279   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1
          280  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_GEOPOLY=1
          281  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_JSON1=1
          282  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1
          283  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1
          284  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBSTAT_VTAB=1
          285  +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_INTROSPECTION_PRAGMAS=1
   280    286   !ENDIF
   281    287   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1
   282    288   !ENDIF
   283    289   
   284    290   # Should the session extension be enabled?  If so, add compilation options
   285    291   # to enable it.
   286    292   #
................................................................................
   924    930   !ENDIF
   925    931   
   926    932   
   927    933   # Additional compiler options for the shell.  These are only effective
   928    934   # when the shell is not being dynamically linked.
   929    935   #
   930    936   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
   931         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
   932         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
   933         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_INTROSPECTION_PRAGMAS
   934         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_RTREE
          937  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_FTS4=1
          938  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1
          939  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1
   935    940   !ENDIF
   936    941   
   937    942   
   938    943   # This is the default Makefile target.  The objects listed here
   939    944   # are what get build when you type just "make" with no arguments.
   940    945   #
   941    946   core:	dll shell

Changes to autoconf/configure.ac.

   168    168   #-----------------------------------------------------------------------
   169    169   #   --enable-session
   170    170   #
   171    171   AC_ARG_ENABLE(session, [AS_HELP_STRING(
   172    172     [--enable-session], [enable the session extension [default=no]])], 
   173    173     [], [])
   174    174   if test x"$enable_session" = "xyes"; then
   175         -  BUILD_CFLAGS="$BUILD_CFLAGS-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK"
          175  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK"
   176    176   fi
   177    177   #-----------------------------------------------------------------------
   178    178   
   179    179   #-----------------------------------------------------------------------
   180    180   #   --enable-debug
   181    181   #
   182    182   AC_ARG_ENABLE(debug, [AS_HELP_STRING(

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.25.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.26.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.25.0'
   730         -PACKAGE_STRING='sqlite 3.25.0'
          729  +PACKAGE_VERSION='3.26.0'
          730  +PACKAGE_STRING='sqlite 3.26.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
................................................................................
   907    907   enable_memsys5
   908    908   enable_memsys3
   909    909   enable_fts3
   910    910   enable_fts4
   911    911   enable_fts5
   912    912   enable_json1
   913    913   enable_update_limit
          914  +enable_geopoly
   914    915   enable_rtree
   915    916   enable_session
   916    917   enable_gcov
   917    918   '
   918    919         ac_precious_vars='build_alias
   919    920   host_alias
   920    921   target_alias
................................................................................
  1461   1462   #
  1462   1463   # Report the --help message.
  1463   1464   #
  1464   1465   if test "$ac_init_help" = "long"; then
  1465   1466     # Omit some internal or obsolete options to make the list less imposing.
  1466   1467     # This message is too long to be a string in the A/UX 3.1 sh.
  1467   1468     cat <<_ACEOF
  1468         -\`configure' configures sqlite 3.25.0 to adapt to many kinds of systems.
         1469  +\`configure' configures sqlite 3.26.0 to adapt to many kinds of systems.
  1469   1470   
  1470   1471   Usage: $0 [OPTION]... [VAR=VALUE]...
  1471   1472   
  1472   1473   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1473   1474   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1474   1475   
  1475   1476   Defaults for the options are specified in brackets.
................................................................................
  1526   1527     --build=BUILD     configure for building on BUILD [guessed]
  1527   1528     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1528   1529   _ACEOF
  1529   1530   fi
  1530   1531   
  1531   1532   if test -n "$ac_init_help"; then
  1532   1533     case $ac_init_help in
  1533         -     short | recursive ) echo "Configuration of sqlite 3.25.0:";;
         1534  +     short | recursive ) echo "Configuration of sqlite 3.26.0:";;
  1534   1535      esac
  1535   1536     cat <<\_ACEOF
  1536   1537   
  1537   1538   Optional Features:
  1538   1539     --disable-option-checking  ignore unrecognized --enable/--with options
  1539   1540     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1540   1541     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1559   1560     --enable-memsys5        Enable MEMSYS5
  1560   1561     --enable-memsys3        Enable MEMSYS3
  1561   1562     --enable-fts3           Enable the FTS3 extension
  1562   1563     --enable-fts4           Enable the FTS4 extension
  1563   1564     --enable-fts5           Enable the FTS5 extension
  1564   1565     --enable-json1          Enable the JSON1 extension
  1565   1566     --enable-update-limit   Enable the UPDATE/DELETE LIMIT clause
         1567  +  --enable-geopoly        Enable the GEOPOLY extension
  1566   1568     --enable-rtree          Enable the RTREE extension
  1567   1569     --enable-session        Enable the SESSION extension
  1568   1570     --enable-gcov           Enable coverage testing using gcov
  1569   1571   
  1570   1572   Optional Packages:
  1571   1573     --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  1572   1574     --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
................................................................................
  1651   1653       cd "$ac_pwd" || { ac_status=$?; break; }
  1652   1654     done
  1653   1655   fi
  1654   1656   
  1655   1657   test -n "$ac_init_help" && exit $ac_status
  1656   1658   if $ac_init_version; then
  1657   1659     cat <<\_ACEOF
  1658         -sqlite configure 3.25.0
         1660  +sqlite configure 3.26.0
  1659   1661   generated by GNU Autoconf 2.69
  1660   1662   
  1661   1663   Copyright (C) 2012 Free Software Foundation, Inc.
  1662   1664   This configure script is free software; the Free Software Foundation
  1663   1665   gives unlimited permission to copy, distribute and modify it.
  1664   1666   _ACEOF
  1665   1667     exit
................................................................................
  2070   2072     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2071   2073   
  2072   2074   } # ac_fn_c_check_header_mongrel
  2073   2075   cat >config.log <<_ACEOF
  2074   2076   This file contains any messages produced by compilers while
  2075   2077   running configure, to aid debugging if configure makes a mistake.
  2076   2078   
  2077         -It was created by sqlite $as_me 3.25.0, which was
         2079  +It was created by sqlite $as_me 3.26.0, which was
  2078   2080   generated by GNU Autoconf 2.69.  Invocation command line was
  2079   2081   
  2080   2082     $ $0 $@
  2081   2083   
  2082   2084   _ACEOF
  2083   2085   exec 5>>config.log
  2084   2086   {
................................................................................
  3928   3930   { $as_echo "$as_me:${as_lineno-$LINENO}: checking the name lister ($NM) interface" >&5
  3929   3931   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3930   3932   if ${lt_cv_nm_interface+:} false; then :
  3931   3933     $as_echo_n "(cached) " >&6
  3932   3934   else
  3933   3935     lt_cv_nm_interface="BSD nm"
  3934   3936     echo "int some_variable = 0;" > conftest.$ac_ext
  3935         -  (eval echo "\"\$as_me:3935: $ac_compile\"" >&5)
         3937  +  (eval echo "\"\$as_me:3937: $ac_compile\"" >&5)
  3936   3938     (eval "$ac_compile" 2>conftest.err)
  3937   3939     cat conftest.err >&5
  3938         -  (eval echo "\"\$as_me:3938: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3940  +  (eval echo "\"\$as_me:3940: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3939   3941     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3940   3942     cat conftest.err >&5
  3941         -  (eval echo "\"\$as_me:3941: output\"" >&5)
         3943  +  (eval echo "\"\$as_me:3943: output\"" >&5)
  3942   3944     cat conftest.out >&5
  3943   3945     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3944   3946       lt_cv_nm_interface="MS dumpbin"
  3945   3947     fi
  3946   3948     rm -f conftest*
  3947   3949   fi
  3948   3950   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_nm_interface" >&5
................................................................................
  5140   5142   	;;
  5141   5143       esac
  5142   5144     fi
  5143   5145     rm -rf conftest*
  5144   5146     ;;
  5145   5147   *-*-irix6*)
  5146   5148     # Find out which ABI we are using.
  5147         -  echo '#line 5147 "configure"' > conftest.$ac_ext
         5149  +  echo '#line 5149 "configure"' > conftest.$ac_ext
  5148   5150     if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
  5149   5151     (eval $ac_compile) 2>&5
  5150   5152     ac_status=$?
  5151   5153     $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
  5152   5154     test $ac_status = 0; }; then
  5153   5155       if test "$lt_cv_prog_gnu_ld" = yes; then
  5154   5156         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6665   6667      # Note that $ac_compile itself does not contain backslashes and begins
  6666   6668      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6667   6669      # The option is referenced via a variable to avoid confusing sed.
  6668   6670      lt_compile=`echo "$ac_compile" | $SED \
  6669   6671      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6670   6672      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6671   6673      -e 's:$: $lt_compiler_flag:'`
  6672         -   (eval echo "\"\$as_me:6672: $lt_compile\"" >&5)
         6674  +   (eval echo "\"\$as_me:6674: $lt_compile\"" >&5)
  6673   6675      (eval "$lt_compile" 2>conftest.err)
  6674   6676      ac_status=$?
  6675   6677      cat conftest.err >&5
  6676         -   echo "$as_me:6676: \$? = $ac_status" >&5
         6678  +   echo "$as_me:6678: \$? = $ac_status" >&5
  6677   6679      if (exit $ac_status) && test -s "$ac_outfile"; then
  6678   6680        # The compiler can only warn and ignore the option if not recognized
  6679   6681        # So say no if there are warnings other than the usual output.
  6680   6682        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6681   6683        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6682   6684        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6683   6685          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  7004   7006      # Note that $ac_compile itself does not contain backslashes and begins
  7005   7007      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7006   7008      # The option is referenced via a variable to avoid confusing sed.
  7007   7009      lt_compile=`echo "$ac_compile" | $SED \
  7008   7010      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7009   7011      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7010   7012      -e 's:$: $lt_compiler_flag:'`
  7011         -   (eval echo "\"\$as_me:7011: $lt_compile\"" >&5)
         7013  +   (eval echo "\"\$as_me:7013: $lt_compile\"" >&5)
  7012   7014      (eval "$lt_compile" 2>conftest.err)
  7013   7015      ac_status=$?
  7014   7016      cat conftest.err >&5
  7015         -   echo "$as_me:7015: \$? = $ac_status" >&5
         7017  +   echo "$as_me:7017: \$? = $ac_status" >&5
  7016   7018      if (exit $ac_status) && test -s "$ac_outfile"; then
  7017   7019        # The compiler can only warn and ignore the option if not recognized
  7018   7020        # So say no if there are warnings other than the usual output.
  7019   7021        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7020   7022        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7021   7023        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7022   7024          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7109   7111      # (2) before a word containing "conftest.", or (3) at the end.
  7110   7112      # Note that $ac_compile itself does not contain backslashes and begins
  7111   7113      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7112   7114      lt_compile=`echo "$ac_compile" | $SED \
  7113   7115      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7114   7116      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7115   7117      -e 's:$: $lt_compiler_flag:'`
  7116         -   (eval echo "\"\$as_me:7116: $lt_compile\"" >&5)
         7118  +   (eval echo "\"\$as_me:7118: $lt_compile\"" >&5)
  7117   7119      (eval "$lt_compile" 2>out/conftest.err)
  7118   7120      ac_status=$?
  7119   7121      cat out/conftest.err >&5
  7120         -   echo "$as_me:7120: \$? = $ac_status" >&5
         7122  +   echo "$as_me:7122: \$? = $ac_status" >&5
  7121   7123      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7122   7124      then
  7123   7125        # The compiler can only warn and ignore the option if not recognized
  7124   7126        # So say no if there are warnings
  7125   7127        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7126   7128        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7127   7129        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7164   7166      # (2) before a word containing "conftest.", or (3) at the end.
  7165   7167      # Note that $ac_compile itself does not contain backslashes and begins
  7166   7168      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7167   7169      lt_compile=`echo "$ac_compile" | $SED \
  7168   7170      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7169   7171      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7170   7172      -e 's:$: $lt_compiler_flag:'`
  7171         -   (eval echo "\"\$as_me:7171: $lt_compile\"" >&5)
         7173  +   (eval echo "\"\$as_me:7173: $lt_compile\"" >&5)
  7172   7174      (eval "$lt_compile" 2>out/conftest.err)
  7173   7175      ac_status=$?
  7174   7176      cat out/conftest.err >&5
  7175         -   echo "$as_me:7175: \$? = $ac_status" >&5
         7177  +   echo "$as_me:7177: \$? = $ac_status" >&5
  7176   7178      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7177   7179      then
  7178   7180        # The compiler can only warn and ignore the option if not recognized
  7179   7181        # So say no if there are warnings
  7180   7182        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7181   7183        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7182   7184        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  9544   9546   else
  9545   9547     	  if test "$cross_compiling" = yes; then :
  9546   9548     lt_cv_dlopen_self=cross
  9547   9549   else
  9548   9550     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9549   9551     lt_status=$lt_dlunknown
  9550   9552     cat > conftest.$ac_ext <<_LT_EOF
  9551         -#line 9551 "configure"
         9553  +#line 9553 "configure"
  9552   9554   #include "confdefs.h"
  9553   9555   
  9554   9556   #if HAVE_DLFCN_H
  9555   9557   #include <dlfcn.h>
  9556   9558   #endif
  9557   9559   
  9558   9560   #include <stdio.h>
................................................................................
  9640   9642   else
  9641   9643     	  if test "$cross_compiling" = yes; then :
  9642   9644     lt_cv_dlopen_self_static=cross
  9643   9645   else
  9644   9646     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9645   9647     lt_status=$lt_dlunknown
  9646   9648     cat > conftest.$ac_ext <<_LT_EOF
  9647         -#line 9647 "configure"
         9649  +#line 9649 "configure"
  9648   9650   #include "confdefs.h"
  9649   9651   
  9650   9652   #if HAVE_DLFCN_H
  9651   9653   #include <dlfcn.h>
  9652   9654   #endif
  9653   9655   
  9654   9656   #include <stdio.h>
................................................................................
 11605  11607   if test "${enable_update_limit+set}" = set; then :
 11606  11608     enableval=$enable_update_limit;
 11607  11609   fi
 11608  11610   
 11609  11611   if test "${enable_udlimit}" = "yes" ; then
 11610  11612     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT"
 11611  11613   fi
        11614  +
        11615  +#########
        11616  +# See whether we should enable GEOPOLY
        11617  +# Check whether --enable-geopoly was given.
        11618  +if test "${enable_geopoly+set}" = set; then :
        11619  +  enableval=$enable_geopoly; enable_geopoly=yes
        11620  +else
        11621  +  enable_geopoly=no
        11622  +fi
        11623  +
        11624  +if test "${enable_geopoly}" = "yes" ; then
        11625  +  OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_GEOPOLY"
        11626  +  enable_rtree=yes
        11627  +fi
 11612  11628   
 11613  11629   #########
 11614  11630   # See whether we should enable RTREE
 11615  11631   # Check whether --enable-rtree was given.
 11616  11632   if test "${enable_rtree+set}" = set; then :
 11617  11633     enableval=$enable_rtree;
 11618  11634   fi
................................................................................
 12212  12228   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12213  12229   
 12214  12230   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12215  12231   # Save the log message, to keep $0 and so on meaningful, and to
 12216  12232   # report actual input values of CONFIG_FILES etc. instead of their
 12217  12233   # values after options handling.
 12218  12234   ac_log="
 12219         -This file was extended by sqlite $as_me 3.25.0, which was
        12235  +This file was extended by sqlite $as_me 3.26.0, which was
 12220  12236   generated by GNU Autoconf 2.69.  Invocation command line was
 12221  12237   
 12222  12238     CONFIG_FILES    = $CONFIG_FILES
 12223  12239     CONFIG_HEADERS  = $CONFIG_HEADERS
 12224  12240     CONFIG_LINKS    = $CONFIG_LINKS
 12225  12241     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12226  12242     $ $0 $@
................................................................................
 12278  12294   
 12279  12295   Report bugs to the package provider."
 12280  12296   
 12281  12297   _ACEOF
 12282  12298   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12283  12299   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12284  12300   ac_cs_version="\\
 12285         -sqlite config.status 3.25.0
        12301  +sqlite config.status 3.26.0
 12286  12302   configured by $0, generated by GNU Autoconf 2.69,
 12287  12303     with options \\"\$ac_cs_config\\"
 12288  12304   
 12289  12305   Copyright (C) 2012 Free Software Foundation, Inc.
 12290  12306   This config.status script is free software; the Free Software Foundation
 12291  12307   gives unlimited permission to copy, distribute and modify it."
 12292  12308   

Changes to configure.ac.

   644    644   # See whether we should enable the LIMIT clause on UPDATE and DELETE
   645    645   # statements.
   646    646   AC_ARG_ENABLE(update-limit, AC_HELP_STRING([--enable-update-limit],
   647    647         [Enable the UPDATE/DELETE LIMIT clause]))
   648    648   if test "${enable_udlimit}" = "yes" ; then
   649    649     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT"
   650    650   fi
          651  +
          652  +#########
          653  +# See whether we should enable GEOPOLY
          654  +AC_ARG_ENABLE(geopoly, AC_HELP_STRING([--enable-geopoly],
          655  +      [Enable the GEOPOLY extension]),
          656  +      [enable_geopoly=yes],[enable_geopoly=no])
          657  +if test "${enable_geopoly}" = "yes" ; then
          658  +  OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_GEOPOLY"
          659  +  enable_rtree=yes
          660  +fi
   651    661   
   652    662   #########
   653    663   # See whether we should enable RTREE
   654    664   AC_ARG_ENABLE(rtree, AC_HELP_STRING([--enable-rtree],
   655    665         [Enable the RTREE extension]))
   656    666   if test "${enable_rtree}" = "yes" ; then
   657    667     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_RTREE"

Added doc/F2FS.txt.

            1  +
            2  +SQLite's OS layer contains the following definitions used in F2FS related
            3  +calls:
            4  +
            5  +#define F2FS_IOCTL_MAGIC        0xf5
            6  +#define F2FS_IOC_START_ATOMIC_WRITE     _IO(F2FS_IOCTL_MAGIC, 1)
            7  +#define F2FS_IOC_COMMIT_ATOMIC_WRITE    _IO(F2FS_IOCTL_MAGIC, 2)
            8  +#define F2FS_IOC_START_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 3)
            9  +#define F2FS_IOC_ABORT_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 5)
           10  +#define F2FS_IOC_GET_FEATURES           _IOR(F2FS_IOCTL_MAGIC, 12, u32)
           11  +#define F2FS_FEATURE_ATOMIC_WRITE       0x0004
           12  +
           13  +After opening a database file on Linux (including Android), SQLite determines
           14  +whether or not a file supports F2FS atomic commits as follows:
           15  +
           16  +  u32 flags = 0;
           17  +  rc = ioctl(fd, F2FS_IOC_GET_FEATURES, &flags);
           18  +  if( rc==0 && (flags & F2FS_FEATURE_ATOMIC_WRITE) ){
           19  +    /* File supports F2FS atomic commits */
           20  +  }else{
           21  +    /* File does NOT support F2FS atomic commits */
           22  +  }
           23  +
           24  +where "fd" is the file-descriptor open on the database file.
           25  +
           26  +Usually, when writing to a database file that supports atomic commits, SQLite
           27  +accumulates the entire transaction in heap memory, deferring all writes to the
           28  +db file until the transaction is committed.
           29  +
           30  +When it is time to commit a transaction on a file that supports atomic
           31  +commits, SQLite does:
           32  +
           33  +  /* Take an F_WRLCK lock on the database file. This prevents any other
           34  +  ** SQLite clients from reading or writing the file until the lock
           35  +  ** is released.  */
           36  +  rc = fcntl(fd, F_SETLK, ...);
           37  +  if( rc!=0 ) goto failed;
           38  +
           39  +  rc = ioctl(fd, F2FS_IOC_START_ATOMIC_WRITE);
           40  +  if( rc!=0 ) goto fallback_to_legacy_journal_commit;
           41  +
           42  +  foreach (dirty page){
           43  +    rc = write(fd, ...dirty page...);
           44  +    if( rc!=0 ){
           45  +      ioctl(fd, F2FS_IOC_ABORT_VOLATILE_WRITE);
           46  +      goto fallback_to_legacy_journal_commit;
           47  +    }
           48  +  }
           49  +
           50  +  rc = ioctl(fd, F2FS_IOC_COMMIT_ATOMIC_WRITE);
           51  +  if( rc!=0 ){
           52  +    ioctl(fd, F2FS_IOC_ABORT_VOLATILE_WRITE);
           53  +    goto fallback_to_legacy_journal_commit;
           54  +  }
           55  +
           56  +  /* If we get there, the transaction has been successfully 
           57  +  ** committed to persistent storage. The following call
           58  +  ** relinquishes the F_WRLCK lock.  */
           59  +  fcntl(fd, F_SETLK, ...);
           60  +
           61  +Assumptions:
           62  +
           63  +1. After either of the F2FS_IOC_ABORT_VOLATILE_WRITE calls return,
           64  +   the database file is in the state that it was in before
           65  +   F2FS_IOC_START_ATOMIC_WRITE was invoked. Even if the ioctl()
           66  +   fails - we're ignoring the return code.
           67  +
           68  +   This is true regardless of the type of error that occurred in
           69  +   ioctl() or write().
           70  +
           71  +2. If the system fails before the F2FS_IOC_COMMIT_ATOMIC_WRITE is
           72  +   completed, then following a reboot the database file is in the
           73  +   state that it was in before F2FS_IOC_START_ATOMIC_WRITE was invoked.
           74  +   Or, if the write was commited right before the system failed, in a 
           75  +   state indicating that all write() calls were successfully committed
           76  +   to persistent storage before the failure occurred.
           77  +
           78  +3. If the process crashes before the F2FS_IOC_COMMIT_ATOMIC_WRITE is
           79  +   completed then the file is automatically restored to the state that
           80  +   it was in before F2FS_IOC_START_ATOMIC_WRITE was called. This occurs
           81  +   before the posix advisory lock is automatically dropped - there is
           82  +   no chance that another client will be able to read the file in a
           83  +   half-committed state before the rollback operation occurs.
           84  +
           85  +
           86  +
           87  +

Changes to ext/fts3/fts3.c.

  3804   3804   **
  3805   3805   ** Flush the contents of the pending-terms table to disk.
  3806   3806   */
  3807   3807   static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3808   3808     int rc = SQLITE_OK;
  3809   3809     UNUSED_PARAMETER(iSavepoint);
  3810   3810     assert( ((Fts3Table *)pVtab)->inTransaction );
  3811         -  assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
         3811  +  assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
  3812   3812     TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
  3813   3813     if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
  3814   3814       rc = fts3SyncMethod(pVtab);
  3815   3815     }
  3816   3816     return rc;
  3817   3817   }
  3818   3818   

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

   524    524       tl_print_if_entry $entry
   525    525     }
   526    526   
   527    527     puts ""
   528    528     puts "  return ret;"
   529    529     puts "\}"
   530    530   }
          531  +
          532  +proc code {txt} {
          533  +  set txt [string trimright $txt]
          534  +  set txt [string trimleft $txt "\n"]
          535  +  set n [expr {[string length $txt] - [string length [string trim $txt]]}]
          536  +  set ret ""
          537  +  foreach L [split $txt "\n"] {
          538  +    append ret "[string range $L $n end]\n"
          539  +  }
          540  +  return [uplevel "subst -nocommands {$ret}"]
          541  +}
          542  +
          543  +proc intarray {lInt} {
          544  +  set ret ""
          545  +  set n [llength $lInt]
          546  +  for {set i 0} {$i < $n} {incr i 10} {
          547  +    append ret "\n    "
          548  +    foreach int [lrange $lInt $i [expr $i+9]] {
          549  +      append ret [format "%-7s" "$int, "]
          550  +    }
          551  +  }
          552  +  append ret "\n  "
          553  +  set ret
          554  +}
          555  +
          556  +proc categories_switch {Cvar first lSecond} {
          557  +  upvar $Cvar C
          558  +  set ret ""
          559  +  append ret "case '$first':\n"
          560  +  append ret "          switch( zCat\[1\] ){\n"
          561  +  foreach s $lSecond {
          562  +    append ret "            case '$s': aArray\[$C($first$s)\] = 1; break;\n"
          563  +  }
          564  +  append ret "            case '*': \n"
          565  +  foreach s $lSecond {
          566  +    append ret "              aArray\[$C($first$s)\] = 1;\n"
          567  +  }
          568  +  append ret "              break;\n"
          569  +  append ret "            default: return 1;"
          570  +  append ret "          }\n"
          571  +  append ret "          break;\n"
          572  +}
          573  +
          574  +# Argument is a list. Each element of which is itself a list of two elements:
          575  +#
          576  +#   * the codepoint
          577  +#   * the category
          578  +#
          579  +# List elements are sorted in order of codepoint.
          580  +#
          581  +proc print_categories {lMap} {
          582  +  set categories {
          583  +    Cc Cf Cn Cs
          584  +    Ll Lm Lo Lt Lu
          585  +    Mc Me Mn
          586  +    Nd Nl No
          587  +    Pc Pd Pe Pf Pi Po Ps
          588  +    Sc Sk Sm So
          589  +    Zl Zp Zs
          590  +
          591  +    LC Co
          592  +  }
          593  +
          594  +  for {set i 0} {$i < [llength $categories]} {incr i} {
          595  +    set C([lindex $categories $i]) [expr 1+$i]
          596  +  }
          597  +
          598  +  set caseC [categories_switch C C {c f n s o}]
          599  +  set caseL [categories_switch C L {l m o t u C}]
          600  +  set caseM [categories_switch C M {c e n}]
          601  +  set caseN [categories_switch C N {d l o}]
          602  +  set caseP [categories_switch C P {c d e f i o s}]
          603  +  set caseS [categories_switch C S {c k m o}]
          604  +  set caseZ [categories_switch C Z {l p s}]
          605  +
          606  +  set nCat [expr [llength [array names C]] + 1]
          607  +  puts [code {
          608  +    int sqlite3Fts5UnicodeNCat(void) { 
          609  +      return $nCat;
          610  +    }
          611  +
          612  +    int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){ 
          613  +      aArray[0] = 1;
          614  +      switch( zCat[0] ){
          615  +        $caseC
          616  +        $caseL
          617  +        $caseM
          618  +        $caseN
          619  +        $caseP
          620  +        $caseS
          621  +        $caseZ
          622  +      }
          623  +      return 0;
          624  +    }
          625  +  }]
          626  +
          627  +  set nRepeat 0
          628  +  set first   [lindex $lMap 0 0]
          629  +  set class   [lindex $lMap 0 1]
          630  +  set prev -1
          631  +
          632  +  set CASE(0) "Lu"
          633  +  set CASE(1) "Ll"
          634  +
          635  +  foreach m $lMap {
          636  +    foreach {codepoint cl} $m {}
          637  +    set codepoint [expr "0x$codepoint"]
          638  +    if {$codepoint>=(1<<20)} continue
          639  +
          640  +    set bNew 0
          641  +    if {$codepoint!=($prev+1)} {
          642  +      set bNew 1
          643  +    } elseif {
          644  +      $cl==$class || ($class=="LC" && $cl==$CASE([expr $nRepeat & 0x01]))
          645  +    } {
          646  +      incr nRepeat
          647  +    } elseif {$class=="Lu" && $nRepeat==1 && $cl=="Ll"} {
          648  +      set class LC
          649  +      incr nRepeat
          650  +    } else {
          651  +      set bNew 1
          652  +    }
          653  +    if {$bNew} {
          654  +      lappend lEntries [list $first $class $nRepeat]
          655  +      set nRepeat 1
          656  +      set first $codepoint
          657  +      set class $cl
          658  +    }
          659  +    set prev $codepoint
          660  +  }
          661  +  if {$nRepeat>0} {
          662  +    lappend lEntries [list $first $class $nRepeat]
          663  +  }
          664  +
          665  +  set aBlock [list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
          666  +  set aMap [list]
          667  +  foreach e $lEntries {
          668  +    foreach {cp class nRepeat} $e {}
          669  +    set block [expr ($cp>>16)]
          670  +    if {$block>0 && [lindex $aBlock $block]==0} {
          671  +      for {set i 1} {$i<=$block} {incr i} {
          672  +        if {[lindex $aBlock $i]==0} {
          673  +          lset aBlock $i [llength $aMap]
          674  +        }
          675  +      }
          676  +    }
          677  +    lappend aMap [expr {$cp & 0xFFFF}]
          678  +    lappend aData [expr {($nRepeat << 5) + $C($class)}]
          679  +  }
          680  +  for {set i 1} {$i<[llength $aBlock]} {incr i} {
          681  +    if {[lindex $aBlock $i]==0} {
          682  +      lset aBlock $i [llength $aMap]
          683  +    }
          684  +  }
          685  +
          686  +  set aBlockArray [intarray $aBlock]
          687  +  set aMapArray [intarray $aMap]
          688  +  set aDataArray [intarray $aData]
          689  +  puts [code {
          690  +    static u16 aFts5UnicodeBlock[] = {$aBlockArray};
          691  +    static u16 aFts5UnicodeMap[] = {$aMapArray};
          692  +    static u16 aFts5UnicodeData[] = {$aDataArray};
          693  +
          694  +    int sqlite3Fts5UnicodeCategory(int iCode) { 
          695  +      int iRes = -1;
          696  +      int iHi;
          697  +      int iLo;
          698  +      int ret;
          699  +      u16 iKey;
          700  +
          701  +      if( iCode>=(1<<20) ){
          702  +        return 0;
          703  +      }
          704  +      iLo = aFts5UnicodeBlock[(iCode>>16)];
          705  +      iHi = aFts5UnicodeBlock[1+(iCode>>16)];
          706  +      iKey = (iCode & 0xFFFF);
          707  +      while( iHi>iLo ){
          708  +        int iTest = (iHi + iLo) / 2;
          709  +        assert( iTest>=iLo && iTest<iHi );
          710  +        if( iKey>=aFts5UnicodeMap[iTest] ){
          711  +          iRes = iTest;
          712  +          iLo = iTest+1;
          713  +        }else{
          714  +          iHi = iTest;
          715  +        }
          716  +      }
          717  +
          718  +      if( iRes<0 ) return 0;
          719  +      if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
          720  +      ret = aFts5UnicodeData[iRes] & 0x1F;
          721  +      if( ret!=$C(LC) ) return ret;
          722  +      return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? $C(Ll) : $C(Lu);
          723  +    }
          724  +
          725  +    void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
          726  +      int i = 0;
          727  +      int iTbl = 0;
          728  +      while( i<128 ){
          729  +        int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
          730  +        int n = (aFts5UnicodeData[iTbl] >> 5) + i;
          731  +        for(; i<128 && i<n; i++){
          732  +          aAscii[i] = bToken;
          733  +        }
          734  +        iTbl++;
          735  +      }
          736  +    }
          737  +  }]
          738  +}
          739  +
          740  +proc print_test_categories {lMap} {
          741  +
          742  +  set lCP [list]
          743  +  foreach e $lMap {
          744  +    foreach {cp cat} $e {}
          745  +    if {[expr 0x$cp] < (1<<20)} {
          746  +      lappend lCP "{0x$cp, \"$cat\"}, "
          747  +    }
          748  +  }
          749  +
          750  +  set aCP "\n"
          751  +  for {set i 0} {$i < [llength $lCP]} {incr i 4} {
          752  +    append aCP "    [join [lrange $lCP $i $i+3]]\n"
          753  +  }
          754  +
          755  +
          756  +  puts [code {
          757  +    static int categories_test (int *piCode){
          758  +      struct Codepoint {
          759  +        int iCode;
          760  +        const char *zCat;
          761  +      } aCP[] = {$aCP};
          762  +      int i;
          763  +      int iCP = 0;
          764  +
          765  +      for(i=0; i<1000000; i++){
          766  +        u8 aArray[40];
          767  +        int cat = 0;
          768  +        int c = 0;
          769  +        memset(aArray, 0, sizeof(aArray));
          770  +        if( aCP[iCP].iCode==i ){
          771  +          sqlite3Fts5UnicodeCatParse(aCP[iCP].zCat, aArray);
          772  +          iCP++;
          773  +        }else{
          774  +          aArray[0] = 1;
          775  +        }
          776  +
          777  +        c = sqlite3Fts5UnicodeCategory(i);
          778  +        if( aArray[c]==0 ){
          779  +          *piCode = i;
          780  +          return 1;
          781  +        }
          782  +      }
          783  +
          784  +      return 0;
          785  +    }
          786  +  }]
          787  +}
   531    788   
   532    789   proc print_fold_test {zFunc mappings} {
   533    790     global tl_lookup_table
   534    791   
   535    792     foreach m $mappings {
   536    793       set c [lindex $m 1]
   537    794       if {$c == ""} {
................................................................................
   601    858   }
   602    859   
   603    860   proc print_test_main {} {
   604    861     puts ""
   605    862     puts "#include <stdio.h>"
   606    863     puts ""
   607    864     puts "int main(int argc, char **argv)\{"
   608         -  puts "  int r1, r2;"
          865  +  puts "  int r1, r2, r3;"
   609    866     puts "  int code;"
          867  +  puts "  r3 = 0;"
   610    868     puts "  r1 = isalnum_test(&code);"
   611    869     puts "  if( r1 ) printf(\"isalnum(): Problem with code %d\\n\",code);"
   612    870     puts "  else printf(\"isalnum(): test passed\\n\");"
   613    871     puts "  r2 = fold_test(&code);"
   614    872     puts "  if( r2 ) printf(\"fold(): Problem with code %d\\n\",code);"
   615    873     puts "  else printf(\"fold(): test passed\\n\");"
   616         -  puts "  return (r1 || r2);"
          874  +  if {$::generate_fts5_code} {
          875  +    puts "  r3 = categories_test(&code);"
          876  +    puts "  if( r3 ) printf(\"categories(): Problem with code %d\\n\",code);"
          877  +    puts "  else printf(\"categories(): test passed\\n\");"
          878  +  }
          879  +  puts "  return (r1 || r2 || r3);"
   617    880     puts "\}"
   618    881   }
   619    882   
   620    883   # Proces the command line arguments. Exit early if they are not to
   621    884   # our liking.
   622    885   #
   623    886   proc usage {} {
................................................................................
   646    909       default {
   647    910         usage
   648    911       }
   649    912     }
   650    913   }
   651    914   
   652    915   print_fileheader
          916  +
          917  +if {$::generate_test_code} {
          918  +  puts "typedef unsigned short int u16;"
          919  +  puts "typedef unsigned char u8;"
          920  +  puts "#include <string.h>"
          921  +}
   653    922   
   654    923   # Print the isalnum() function to stdout.
   655    924   #
   656    925   set lRange [an_load_separator_ranges]
   657         -print_isalnum ${function_prefix}UnicodeIsalnum $lRange
          926  +if {$generate_fts5_code==0} {
          927  +  print_isalnum ${function_prefix}UnicodeIsalnum $lRange
          928  +}
   658    929   
   659    930   # Leave a gap between the two generated C functions.
   660    931   #
   661    932   puts ""
   662    933   puts ""
   663    934   
   664    935   # Load the fold data. This is used by the [rd_XXX] commands
................................................................................
   672    943   print_isdiacritic ${function_prefix}UnicodeIsdiacritic $mappings
   673    944   puts ""
   674    945   puts ""
   675    946   
   676    947   # Print the fold() function to stdout.
   677    948   #
   678    949   print_fold ${function_prefix}UnicodeFold
          950  +
          951  +if {$generate_fts5_code} {
          952  +  puts ""
          953  +  puts ""
          954  +  print_categories [cc_load_unicodedata_text ${unicodedata.txt}]
          955  +}
   679    956   
   680    957   # Print the test routines and main() function to stdout, if -test 
   681    958   # was specified.
   682    959   #
   683    960   if {$::generate_test_code} {
   684         -  print_test_isalnum ${function_prefix}UnicodeIsalnum $lRange
          961  +  if {$generate_fts5_code==0} {
          962  +    print_test_isalnum ${function_prefix}UnicodeIsalnum $lRange
          963  +  }
   685    964     print_fold_test ${function_prefix}UnicodeFold $mappings
          965  +  print_test_categories [cc_load_unicodedata_text ${unicodedata.txt}]
   686    966     print_test_main 
   687    967   }
   688    968   
   689    969   if {$generate_fts5_code} {
   690    970     # no-op
   691    971   } else {
   692    972     puts "#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */"
   693    973     puts "#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */"
   694    974   }

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

   138    138       foreach elem $c { lappend c2 [expr "0x[string trim $elem]"] }
   139    139       set b [string trim $b]
   140    140       set d [string trim $d]
   141    141   
   142    142       if {$b=="C" || $b=="S"} { set tl_lookup_table($a2) $c2 }
   143    143     }
   144    144   }
          145  +
          146  +proc cc_load_unicodedata_text {zName} {
          147  +  set fd [open $zName]
          148  +  set lField {
          149  +    code
          150  +    character_name
          151  +    general_category
          152  +    canonical_combining_classes
          153  +    bidirectional_category
          154  +    character_decomposition_mapping
          155  +    decimal_digit_value
          156  +    digit_value
          157  +    numeric_value
          158  +    mirrored
          159  +    unicode_1_name
          160  +    iso10646_comment_field
          161  +    uppercase_mapping
          162  +    lowercase_mapping
          163  +    titlecase_mapping
          164  +  }
          165  +  set lRet [list]
          166  +
          167  +  while { ![eof $fd] } {
          168  +    set line [gets $fd]
          169  +    if {$line == ""} continue
          170  +
          171  +    set fields [split $line ";"]
          172  +    if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
          173  +    foreach $lField $fields {}
          174  +
          175  +    lappend lRet [list $code $general_category]
          176  +  }
          177  +
          178  +  close $fd
          179  +  set lRet
          180  +}
   145    181   
   146    182   

Changes to ext/fts5/fts5.h.

   440    440   **            document such as "I won first place" is tokenized, entries are
   441    441   **            added to the FTS index for "i", "won", "first", "1st" and
   442    442   **            "place".
   443    443   **
   444    444   **            This way, even if the tokenizer does not provide synonyms
   445    445   **            when tokenizing query text (it should not - to do would be
   446    446   **            inefficient), it doesn't matter if the user queries for 
   447         -**            'first + place' or '1st + place', as there are entires in the
          447  +**            'first + place' or '1st + place', as there are entries in the
   448    448   **            FTS index corresponding to both forms of the first token.
   449    449   **   </ol>
   450    450   **
   451    451   **   Whether it is parsing document or query text, any call to xToken that
   452    452   **   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
   453    453   **   is considered to supply a synonym for the previous token. For example,
   454    454   **   when parsing the document "I won first place", a tokenizer that supports
................................................................................
   468    468   **   There is no limit to the number of synonyms that may be provided for a
   469    469   **   single token.
   470    470   **
   471    471   **   In many cases, method (1) above is the best approach. It does not add 
   472    472   **   extra data to the FTS index or require FTS5 to query for multiple terms,
   473    473   **   so it is efficient in terms of disk space and query speed. However, it
   474    474   **   does not support prefix queries very well. If, as suggested above, the
   475         -**   token "first" is subsituted for "1st" by the tokenizer, then the query:
          475  +**   token "first" is substituted for "1st" by the tokenizer, then the query:
   476    476   **
   477    477   **   <codeblock>
   478    478   **     ... MATCH '1s*'</codeblock>
   479    479   **
   480    480   **   will not match documents that contain the token "1st" (as the tokenizer
   481    481   **   will probably not map "1s" to any prefix of "first").
   482    482   **

Changes to ext/fts5/fts5Int.h.

   780    780   ** End of interface to code in fts5_vocab.c.
   781    781   **************************************************************************/
   782    782   
   783    783   
   784    784   /**************************************************************************
   785    785   ** Interface to automatically generated code in fts5_unicode2.c. 
   786    786   */
   787         -int sqlite3Fts5UnicodeIsalnum(int c);
   788    787   int sqlite3Fts5UnicodeIsdiacritic(int c);
   789    788   int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
          789  +
          790  +int sqlite3Fts5UnicodeCatParse(const char*, u8*);
          791  +int sqlite3Fts5UnicodeCategory(int iCode);
          792  +void sqlite3Fts5UnicodeAscii(u8*, u8*);
   790    793   /*
   791    794   ** End of interface to code in fts5_unicode2.c.
   792    795   **************************************************************************/
   793    796   
   794    797   #endif

Changes to ext/fts5/fts5_expr.c.

  2537   2537   */
  2538   2538   static void fts5ExprIsAlnum(
  2539   2539     sqlite3_context *pCtx,          /* Function call context */
  2540   2540     int nArg,                       /* Number of args */
  2541   2541     sqlite3_value **apVal           /* Function arguments */
  2542   2542   ){
  2543   2543     int iCode;
         2544  +  u8 aArr[32];
  2544   2545     if( nArg!=1 ){
  2545   2546       sqlite3_result_error(pCtx, 
  2546   2547           "wrong number of arguments to function fts5_isalnum", -1
  2547   2548       );
  2548   2549       return;
  2549   2550     }
         2551  +  memset(aArr, 0, sizeof(aArr));
         2552  +  sqlite3Fts5UnicodeCatParse("L*", aArr);
         2553  +  sqlite3Fts5UnicodeCatParse("N*", aArr);
         2554  +  sqlite3Fts5UnicodeCatParse("Co", aArr);
  2550   2555     iCode = sqlite3_value_int(apVal[0]);
  2551         -  sqlite3_result_int(pCtx, sqlite3Fts5UnicodeIsalnum(iCode));
         2556  +  sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory(iCode)]);
  2552   2557   }
  2553   2558   
  2554   2559   static void fts5ExprFold(
  2555   2560     sqlite3_context *pCtx,          /* Function call context */
  2556   2561     int nArg,                       /* Number of args */
  2557   2562     sqlite3_value **apVal           /* Function arguments */
  2558   2563   ){

Changes to ext/fts5/fts5_index.c.

  5257   5257     int nChar
  5258   5258   ){
  5259   5259     int n = 0;
  5260   5260     int i;
  5261   5261     for(i=0; i<nChar; i++){
  5262   5262       if( n>=nByte ) return 0;      /* Input contains fewer than nChar chars */
  5263   5263       if( (unsigned char)p[n++]>=0xc0 ){
  5264         -      while( (p[n] & 0xc0)==0x80 ) n++;
         5264  +      while( (p[n] & 0xc0)==0x80 ){
         5265  +        n++;
         5266  +        if( n>=nByte ) break;
         5267  +      }
  5265   5268       }
  5266   5269     }
  5267   5270     return n;
  5268   5271   }
  5269   5272   
  5270   5273   /*
  5271   5274   ** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of

Changes to ext/fts5/fts5_main.c.

   276    276         assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
   277    277         p->ts.eState = 0;
   278    278         break;
   279    279   
   280    280       case FTS5_SAVEPOINT:
   281    281         assert( p->ts.eState==1 );
   282    282         assert( iSavepoint>=0 );
   283         -      assert( iSavepoint>p->ts.iSavepoint );
          283  +      assert( iSavepoint>=p->ts.iSavepoint );
   284    284         p->ts.iSavepoint = iSavepoint;
   285    285         break;
   286    286         
   287    287       case FTS5_RELEASE:
   288    288         assert( p->ts.eState==1 );
   289    289         assert( iSavepoint>=0 );
   290    290         assert( iSavepoint<=p->ts.iSavepoint );
................................................................................
  1201   1201       ** return results to the user for this query. The current cursor 
  1202   1202       ** (pCursor) is used to execute the query issued by function 
  1203   1203       ** fts5CursorFirstSorted() above.  */
  1204   1204       assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
  1205   1205       assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
  1206   1206       assert( pCsr->iLastRowid==LARGEST_INT64 );
  1207   1207       assert( pCsr->iFirstRowid==SMALLEST_INT64 );
         1208  +    if( pTab->pSortCsr->bDesc ){
         1209  +      pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
         1210  +      pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
         1211  +    }else{
         1212  +      pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
         1213  +      pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
         1214  +    }
  1208   1215       pCsr->ePlan = FTS5_PLAN_SOURCE;
  1209   1216       pCsr->pExpr = pTab->pSortCsr->pExpr;
  1210   1217       rc = fts5CursorFirst(pTab, pCsr, bDesc);
  1211   1218     }else if( pMatch ){
  1212   1219       const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
  1213   1220       if( zExpr==0 ) zExpr = "";
  1214   1221   

Changes to ext/fts5/fts5_tokenize.c.

   233    233   struct Unicode61Tokenizer {
   234    234     unsigned char aTokenChar[128];  /* ASCII range token characters */
   235    235     char *aFold;                    /* Buffer to fold text into */
   236    236     int nFold;                      /* Size of aFold[] in bytes */
   237    237     int bRemoveDiacritic;           /* True if remove_diacritics=1 is set */
   238    238     int nException;
   239    239     int *aiException;
          240  +
          241  +  unsigned char aCategory[32];    /* True for token char categories */
   240    242   };
   241    243   
   242    244   static int fts5UnicodeAddExceptions(
   243    245     Unicode61Tokenizer *p,          /* Tokenizer object */
   244    246     const char *z,                  /* Characters to treat as exceptions */
   245    247     int bTokenChars                 /* 1 for 'tokenchars', 0 for 'separators' */
   246    248   ){
................................................................................
   257    259         while( zCsr<zTerm ){
   258    260           int iCode;
   259    261           int bToken;
   260    262           READ_UTF8(zCsr, zTerm, iCode);
   261    263           if( iCode<128 ){
   262    264             p->aTokenChar[iCode] = (unsigned char)bTokenChars;
   263    265           }else{
   264         -          bToken = sqlite3Fts5UnicodeIsalnum(iCode);
          266  +          bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
   265    267             assert( (bToken==0 || bToken==1) ); 
   266    268             assert( (bTokenChars==0 || bTokenChars==1) );
   267    269             if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
   268    270               int i;
   269    271               for(i=0; i<nNew; i++){
   270    272                 if( aNew[i]>iCode ) break;
   271    273               }
................................................................................
   317    319       Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
   318    320       sqlite3_free(p->aiException);
   319    321       sqlite3_free(p->aFold);
   320    322       sqlite3_free(p);
   321    323     }
   322    324     return;
   323    325   }
          326  +
          327  +static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
          328  +  const char *z = zCat;
          329  +
          330  +  while( *z ){
          331  +    while( *z==' ' || *z=='\t' ) z++;
          332  +    if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
          333  +      return SQLITE_ERROR;
          334  +    }
          335  +    while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
          336  +  }
          337  +
          338  +  sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
          339  +  return SQLITE_OK;
          340  +}
   324    341   
   325    342   /*
   326    343   ** Create a "unicode61" tokenizer.
   327    344   */
   328    345   static int fts5UnicodeCreate(
   329    346     void *pUnused, 
   330    347     const char **azArg, int nArg,
................................................................................
   336    353     UNUSED_PARAM(pUnused);
   337    354   
   338    355     if( nArg%2 ){
   339    356       rc = SQLITE_ERROR;
   340    357     }else{
   341    358       p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
   342    359       if( p ){
          360  +      const char *zCat = "L* N* Co";
   343    361         int i;
   344    362         memset(p, 0, sizeof(Unicode61Tokenizer));
   345         -      memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
          363  +
   346    364         p->bRemoveDiacritic = 1;
   347    365         p->nFold = 64;
   348    366         p->aFold = sqlite3_malloc(p->nFold * sizeof(char));
   349    367         if( p->aFold==0 ){
   350    368           rc = SQLITE_NOMEM;
   351    369         }
          370  +
          371  +      /* Search for a "categories" argument */
          372  +      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
          373  +        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
          374  +          zCat = azArg[i+1];
          375  +        }
          376  +      }
          377  +
          378  +      if( rc==SQLITE_OK ){
          379  +        rc = unicodeSetCategories(p, zCat);
          380  +      }
          381  +
   352    382         for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
   353    383           const char *zArg = azArg[i+1];
   354    384           if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
   355    385             if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
   356    386               rc = SQLITE_ERROR;
   357    387             }
   358    388             p->bRemoveDiacritic = (zArg[0]=='1');
   359    389           }else
   360    390           if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
   361    391             rc = fts5UnicodeAddExceptions(p, zArg, 1);
   362    392           }else
   363    393           if( 0==sqlite3_stricmp(azArg[i], "separators") ){
   364    394             rc = fts5UnicodeAddExceptions(p, zArg, 0);
          395  +        }else
          396  +        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
          397  +          /* no-op */
   365    398           }else{
   366    399             rc = SQLITE_ERROR;
   367    400           }
   368    401         }
          402  +
   369    403       }else{
   370    404         rc = SQLITE_NOMEM;
   371    405       }
   372    406       if( rc!=SQLITE_OK ){
   373    407         fts5UnicodeDelete((Fts5Tokenizer*)p);
   374    408         p = 0;
   375    409       }
................................................................................
   380    414   
   381    415   /*
   382    416   ** Return true if, for the purposes of tokenizing with the tokenizer
   383    417   ** passed as the first argument, codepoint iCode is considered a token 
   384    418   ** character (not a separator).
   385    419   */
   386    420   static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
   387         -  assert( (sqlite3Fts5UnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
   388         -  return sqlite3Fts5UnicodeIsalnum(iCode) ^ fts5UnicodeIsException(p, iCode);
          421  +  return (
          422  +    p->aCategory[sqlite3Fts5UnicodeCategory(iCode)]
          423  +    ^ fts5UnicodeIsException(p, iCode)
          424  +  );
   389    425   }
   390    426   
   391    427   static int fts5UnicodeTokenize(
   392    428     Fts5Tokenizer *pTokenizer,
   393    429     void *pCtx,
   394    430     int iUnused,
   395    431     const char *pText, int nText,

Changes to ext/fts5/fts5_unicode2.c.

    14     14   /*
    15     15   ** DO NOT EDIT THIS MACHINE GENERATED FILE.
    16     16   */
    17     17   
    18     18   
    19     19   #include <assert.h>
    20     20   
    21         -/*
    22         -** Return true if the argument corresponds to a unicode codepoint
    23         -** classified as either a letter or a number. Otherwise false.
    24         -**
    25         -** The results are undefined if the value passed to this function
    26         -** is less than zero.
    27         -*/
    28         -int sqlite3Fts5UnicodeIsalnum(int c){
    29         -  /* Each unsigned integer in the following array corresponds to a contiguous
    30         -  ** range of unicode codepoints that are not either letters or numbers (i.e.
    31         -  ** codepoints for which this function should return 0).
    32         -  **
    33         -  ** The most significant 22 bits in each 32-bit value contain the first 
    34         -  ** codepoint in the range. The least significant 10 bits are used to store
    35         -  ** the size of the range (always at least 1). In other words, the value 
    36         -  ** ((C<<22) + N) represents a range of N codepoints starting with codepoint 
    37         -  ** C. It is not possible to represent a range larger than 1023 codepoints 
    38         -  ** using this format.
    39         -  */
    40         -  static const unsigned int aEntry[] = {
    41         -    0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
    42         -    0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
    43         -    0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
    44         -    0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
    45         -    0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
    46         -    0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
    47         -    0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
    48         -    0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
    49         -    0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
    50         -    0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
    51         -    0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
    52         -    0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
    53         -    0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
    54         -    0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
    55         -    0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
    56         -    0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
    57         -    0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
    58         -    0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
    59         -    0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
    60         -    0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
    61         -    0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
    62         -    0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
    63         -    0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
    64         -    0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
    65         -    0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
    66         -    0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
    67         -    0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
    68         -    0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
    69         -    0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
    70         -    0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
    71         -    0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
    72         -    0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
    73         -    0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
    74         -    0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
    75         -    0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
    76         -    0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
    77         -    0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
    78         -    0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
    79         -    0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
    80         -    0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
    81         -    0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
    82         -    0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
    83         -    0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
    84         -    0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
    85         -    0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
    86         -    0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
    87         -    0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
    88         -    0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
    89         -    0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
    90         -    0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
    91         -    0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
    92         -    0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
    93         -    0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
    94         -    0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
    95         -    0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
    96         -    0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
    97         -    0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
    98         -    0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
    99         -    0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
   100         -    0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
   101         -    0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
   102         -    0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
   103         -    0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
   104         -    0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
   105         -    0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
   106         -    0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
   107         -    0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
   108         -    0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
   109         -    0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
   110         -    0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
   111         -    0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
   112         -    0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
   113         -    0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
   114         -    0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
   115         -    0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
   116         -    0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
   117         -    0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
   118         -    0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
   119         -    0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
   120         -    0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
   121         -    0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
   122         -    0x380400F0,
   123         -  };
   124         -  static const unsigned int aAscii[4] = {
   125         -    0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
   126         -  };
   127         -
   128         -  if( (unsigned int)c<128 ){
   129         -    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
   130         -  }else if( (unsigned int)c<(1<<22) ){
   131         -    unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
   132         -    int iRes = 0;
   133         -    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
   134         -    int iLo = 0;
   135         -    while( iHi>=iLo ){
   136         -      int iTest = (iHi + iLo) / 2;
   137         -      if( key >= aEntry[iTest] ){
   138         -        iRes = iTest;
   139         -        iLo = iTest+1;
   140         -      }else{
   141         -        iHi = iTest-1;
   142         -      }
   143         -    }
   144         -    assert( aEntry[0]<key );
   145         -    assert( key>=aEntry[iRes] );
   146         -    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
   147         -  }
   148         -  return 1;
   149         -}
   150     21   
   151     22   
   152     23   /*
   153     24   ** If the argument is a codepoint corresponding to a lowercase letter
   154     25   ** in the ASCII range with a diacritic added, return the codepoint
   155     26   ** of the ASCII letter only. For example, if passed 235 - "LATIN
   156     27   ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
................................................................................
   354    225     
   355    226     else if( c>=66560 && c<66600 ){
   356    227       ret = c + 40;
   357    228     }
   358    229   
   359    230     return ret;
   360    231   }
          232  +
          233  +
          234  +#if 0
          235  +int sqlite3Fts5UnicodeNCat(void) { 
          236  +  return 32;
          237  +}
          238  +#endif
          239  +
          240  +int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){ 
          241  +  aArray[0] = 1;
          242  +  switch( zCat[0] ){
          243  +    case 'C':
          244  +          switch( zCat[1] ){
          245  +            case 'c': aArray[1] = 1; break;
          246  +            case 'f': aArray[2] = 1; break;
          247  +            case 'n': aArray[3] = 1; break;
          248  +            case 's': aArray[4] = 1; break;
          249  +            case 'o': aArray[31] = 1; break;
          250  +            case '*': 
          251  +              aArray[1] = 1;
          252  +              aArray[2] = 1;
          253  +              aArray[3] = 1;
          254  +              aArray[4] = 1;
          255  +              aArray[31] = 1;
          256  +              break;
          257  +            default: return 1;          }
          258  +          break;
          259  +
          260  +    case 'L':
          261  +          switch( zCat[1] ){
          262  +            case 'l': aArray[5] = 1; break;
          263  +            case 'm': aArray[6] = 1; break;
          264  +            case 'o': aArray[7] = 1; break;
          265  +            case 't': aArray[8] = 1; break;
          266  +            case 'u': aArray[9] = 1; break;
          267  +            case 'C': aArray[30] = 1; break;
          268  +            case '*': 
          269  +              aArray[5] = 1;
          270  +              aArray[6] = 1;
          271  +              aArray[7] = 1;
          272  +              aArray[8] = 1;
          273  +              aArray[9] = 1;
          274  +              aArray[30] = 1;
          275  +              break;
          276  +            default: return 1;          }
          277  +          break;
          278  +
          279  +    case 'M':
          280  +          switch( zCat[1] ){
          281  +            case 'c': aArray[10] = 1; break;
          282  +            case 'e': aArray[11] = 1; break;
          283  +            case 'n': aArray[12] = 1; break;
          284  +            case '*': 
          285  +              aArray[10] = 1;
          286  +              aArray[11] = 1;
          287  +              aArray[12] = 1;
          288  +              break;
          289  +            default: return 1;          }
          290  +          break;
          291  +
          292  +    case 'N':
          293  +          switch( zCat[1] ){
          294  +            case 'd': aArray[13] = 1; break;
          295  +            case 'l': aArray[14] = 1; break;
          296  +            case 'o': aArray[15] = 1; break;
          297  +            case '*': 
          298  +              aArray[13] = 1;
          299  +              aArray[14] = 1;
          300  +              aArray[15] = 1;
          301  +              break;
          302  +            default: return 1;          }
          303  +          break;
          304  +
          305  +    case 'P':
          306  +          switch( zCat[1] ){
          307  +            case 'c': aArray[16] = 1; break;
          308  +            case 'd': aArray[17] = 1; break;
          309  +            case 'e': aArray[18] = 1; break;
          310  +            case 'f': aArray[19] = 1; break;
          311  +            case 'i': aArray[20] = 1; break;
          312  +            case 'o': aArray[21] = 1; break;
          313  +            case 's': aArray[22] = 1; break;
          314  +            case '*': 
          315  +              aArray[16] = 1;
          316  +              aArray[17] = 1;
          317  +              aArray[18] = 1;
          318  +              aArray[19] = 1;
          319  +              aArray[20] = 1;
          320  +              aArray[21] = 1;
          321  +              aArray[22] = 1;
          322  +              break;
          323  +            default: return 1;          }
          324  +          break;
          325  +
          326  +    case 'S':
          327  +          switch( zCat[1] ){
          328  +            case 'c': aArray[23] = 1; break;
          329  +            case 'k': aArray[24] = 1; break;
          330  +            case 'm': aArray[25] = 1; break;
          331  +            case 'o': aArray[26] = 1; break;
          332  +            case '*': 
          333  +              aArray[23] = 1;
          334  +              aArray[24] = 1;
          335  +              aArray[25] = 1;
          336  +              aArray[26] = 1;
          337  +              break;
          338  +            default: return 1;          }
          339  +          break;
          340  +
          341  +    case 'Z':
          342  +          switch( zCat[1] ){
          343  +            case 'l': aArray[27] = 1; break;
          344  +            case 'p': aArray[28] = 1; break;
          345  +            case 's': aArray[29] = 1; break;
          346  +            case '*': 
          347  +              aArray[27] = 1;
          348  +              aArray[28] = 1;
          349  +              aArray[29] = 1;
          350  +              break;
          351  +            default: return 1;          }
          352  +          break;
          353  +
          354  +  }
          355  +  return 0;
          356  +}
          357  +
          358  +static u16 aFts5UnicodeBlock[] = {
          359  +    0,     1471,  1753,  1760,  1760,  1760,  1760,  1760,  1760,  1760,  
          360  +    1760,  1760,  1760,  1760,  1760,  1763,  1765,  
          361  +  };
          362  +static u16 aFts5UnicodeMap[] = {
          363  +    0,     32,    33,    36,    37,    40,    41,    42,    43,    44,    
          364  +    45,    46,    48,    58,    60,    63,    65,    91,    92,    93,    
          365  +    94,    95,    96,    97,    123,   124,   125,   126,   127,   160,   
          366  +    161,   162,   166,   167,   168,   169,   170,   171,   172,   173,   
          367  +    174,   175,   176,   177,   178,   180,   181,   182,   184,   185,   
          368  +    186,   187,   188,   191,   192,   215,   216,   223,   247,   248,   
          369  +    256,   312,   313,   329,   330,   377,   383,   385,   387,   388,   
          370  +    391,   394,   396,   398,   402,   403,   405,   406,   409,   412,   
          371  +    414,   415,   417,   418,   423,   427,   428,   431,   434,   436,   
          372  +    437,   440,   442,   443,   444,   446,   448,   452,   453,   454,   
          373  +    455,   456,   457,   458,   459,   460,   461,   477,   478,   496,   
          374  +    497,   498,   499,   500,   503,   505,   506,   564,   570,   572,   
          375  +    573,   575,   577,   580,   583,   584,   592,   660,   661,   688,   
          376  +    706,   710,   722,   736,   741,   748,   749,   750,   751,   768,   
          377  +    880,   884,   885,   886,   890,   891,   894,   900,   902,   903,   
          378  +    904,   908,   910,   912,   913,   931,   940,   975,   977,   978,   
          379  +    981,   984,   1008,  1012,  1014,  1015,  1018,  1020,  1021,  1072,  
          380  +    1120,  1154,  1155,  1160,  1162,  1217,  1231,  1232,  1329,  1369,  
          381  +    1370,  1377,  1417,  1418,  1423,  1425,  1470,  1471,  1472,  1473,  
          382  +    1475,  1476,  1478,  1479,  1488,  1520,  1523,  1536,  1542,  1545,  
          383  +    1547,  1548,  1550,  1552,  1563,  1566,  1568,  1600,  1601,  1611,  
          384  +    1632,  1642,  1646,  1648,  1649,  1748,  1749,  1750,  1757,  1758,  
          385  +    1759,  1765,  1767,  1769,  1770,  1774,  1776,  1786,  1789,  1791,  
          386  +    1792,  1807,  1808,  1809,  1810,  1840,  1869,  1958,  1969,  1984,  
          387  +    1994,  2027,  2036,  2038,  2039,  2042,  2048,  2070,  2074,  2075,  
          388  +    2084,  2085,  2088,  2089,  2096,  2112,  2137,  2142,  2208,  2210,  
          389  +    2276,  2304,  2307,  2308,  2362,  2363,  2364,  2365,  2366,  2369,  
          390  +    2377,  2381,  2382,  2384,  2385,  2392,  2402,  2404,  2406,  2416,  
          391  +    2417,  2418,  2425,  2433,  2434,  2437,  2447,  2451,  2474,  2482,  
          392  +    2486,  2492,  2493,  2494,  2497,  2503,  2507,  2509,  2510,  2519,  
          393  +    2524,  2527,  2530,  2534,  2544,  2546,  2548,  2554,  2555,  2561,  
          394  +    2563,  2565,  2575,  2579,  2602,  2610,  2613,  2616,  2620,  2622,  
          395  +    2625,  2631,  2635,  2641,  2649,  2654,  2662,  2672,  2674,  2677,  
          396  +    2689,  2691,  2693,  2703,  2707,  2730,  2738,  2741,  2748,  2749,  
          397  +    2750,  2753,  2759,  2761,  2763,  2765,  2768,  2784,  2786,  2790,  
          398  +    2800,  2801,  2817,  2818,  2821,  2831,  2835,  2858,  2866,  2869,  
          399  +    2876,  2877,  2878,  2879,  2880,  2881,  2887,  2891,  2893,  2902,  
          400  +    2903,  2908,  2911,  2914,  2918,  2928,  2929,  2930,  2946,  2947,  
          401  +    2949,  2958,  2962,  2969,  2972,  2974,  2979,  2984,  2990,  3006,  
          402  +    3008,  3009,  3014,  3018,  3021,  3024,  3031,  3046,  3056,  3059,  
          403  +    3065,  3066,  3073,  3077,  3086,  3090,  3114,  3125,  3133,  3134,  
          404  +    3137,  3142,  3146,  3157,  3160,  3168,  3170,  3174,  3192,  3199,  
          405  +    3202,  3205,  3214,  3218,  3242,  3253,  3260,  3261,  3262,  3263,  
          406  +    3264,  3270,  3271,  3274,  3276,  3285,  3294,  3296,  3298,  3302,  
          407  +    3313,  3330,  3333,  3342,  3346,  3389,  3390,  3393,  3398,  3402,  
          408  +    3405,  3406,  3415,  3424,  3426,  3430,  3440,  3449,  3450,  3458,  
          409  +    3461,  3482,  3507,  3517,  3520,  3530,  3535,  3538,  3542,  3544,  
          410  +    3570,  3572,  3585,  3633,  3634,  3636,  3647,  3648,  3654,  3655,  
          411  +    3663,  3664,  3674,  3713,  3716,  3719,  3722,  3725,  3732,  3737,  
          412  +    3745,  3749,  3751,  3754,  3757,  3761,  3762,  3764,  3771,  3773,  
          413  +    3776,  3782,  3784,  3792,  3804,  3840,  3841,  3844,  3859,  3860,  
          414  +    3861,  3864,  3866,  3872,  3882,  3892,  3893,  3894,  3895,  3896,  
          415  +    3897,  3898,  3899,  3900,  3901,  3902,  3904,  3913,  3953,  3967,  
          416  +    3968,  3973,  3974,  3976,  3981,  3993,  4030,  4038,  4039,  4046,  
          417  +    4048,  4053,  4057,  4096,  4139,  4141,  4145,  4146,  4152,  4153,  
          418  +    4155,  4157,  4159,  4160,  4170,  4176,  4182,  4184,  4186,  4190,  
          419  +    4193,  4194,  4197,  4199,  4206,  4209,  4213,  4226,  4227,  4229,  
          420  +    4231,  4237,  4238,  4239,  4240,  4250,  4253,  4254,  4256,  4295,  
          421  +    4301,  4304,  4347,  4348,  4349,  4682,  4688,  4696,  4698,  4704,  
          422  +    4746,  4752,  4786,  4792,  4800,  4802,  4808,  4824,  4882,  4888,  
          423  +    4957,  4960,  4969,  4992,  5008,  5024,  5120,  5121,  5741,  5743,  
          424  +    5760,  5761,  5787,  5788,  5792,  5867,  5870,  5888,  5902,  5906,  
          425  +    5920,  5938,  5941,  5952,  5970,  5984,  5998,  6002,  6016,  6068,  
          426  +    6070,  6071,  6078,  6086,  6087,  6089,  6100,  6103,  6104,  6107,  
          427  +    6108,  6109,  6112,  6128,  6144,  6150,  6151,  6155,  6158,  6160,  
          428  +    6176,  6211,  6212,  6272,  6313,  6314,  6320,  6400,  6432,  6435,  
          429  +    6439,  6441,  6448,  6450,  6451,  6457,  6464,  6468,  6470,  6480,  
          430  +    6512,  6528,  6576,  6593,  6600,  6608,  6618,  6622,  6656,  6679,  
          431  +    6681,  6686,  6688,  6741,  6742,  6743,  6744,  6752,  6753,  6754,  
          432  +    6755,  6757,  6765,  6771,  6783,  6784,  6800,  6816,  6823,  6824,  
          433  +    6912,  6916,  6917,  6964,  6965,  6966,  6971,  6972,  6973,  6978,  
          434  +    6979,  6981,  6992,  7002,  7009,  7019,  7028,  7040,  7042,  7043,  
          435  +    7073,  7074,  7078,  7080,  7082,  7083,  7084,  7086,  7088,  7098,  
          436  +    7142,  7143,  7144,  7146,  7149,  7150,  7151,  7154,  7164,  7168,  
          437  +    7204,  7212,  7220,  7222,  7227,  7232,  7245,  7248,  7258,  7288,  
          438  +    7294,  7360,  7376,  7379,  7380,  7393,  7394,  7401,  7405,  7406,  
          439  +    7410,  7412,  7413,  7424,  7468,  7531,  7544,  7545,  7579,  7616,  
          440  +    7676,  7680,  7830,  7838,  7936,  7944,  7952,  7960,  7968,  7976,  
          441  +    7984,  7992,  8000,  8008,  8016,  8025,  8027,  8029,  8031,  8033,  
          442  +    8040,  8048,  8064,  8072,  8080,  8088,  8096,  8104,  8112,  8118,  
          443  +    8120,  8124,  8125,  8126,  8127,  8130,  8134,  8136,  8140,  8141,  
          444  +    8144,  8150,  8152,  8157,  8160,  8168,  8173,  8178,  8182,  8184,  
          445  +    8188,  8189,  8192,  8203,  8208,  8214,  8216,  8217,  8218,  8219,  
          446  +    8221,  8222,  8223,  8224,  8232,  8233,  8234,  8239,  8240,  8249,  
          447  +    8250,  8251,  8255,  8257,  8260,  8261,  8262,  8263,  8274,  8275,  
          448  +    8276,  8277,  8287,  8288,  8298,  8304,  8305,  8308,  8314,  8317,  
          449  +    8318,  8319,  8320,  8330,  8333,  8334,  8336,  8352,  8400,  8413,  
          450  +    8417,  8418,  8421,  8448,  8450,  8451,  8455,  8456,  8458,  8459,  
          451  +    8462,  8464,  8467,  8468,  8469,  8470,  8472,  8473,  8478,  8484,  
          452  +    8485,  8486,  8487,  8488,  8489,  8490,  8494,  8495,  8496,  8500,  
          453  +    8501,  8505,  8506,  8508,  8510,  8512,  8517,  8519,  8522,  8523,  
          454  +    8524,  8526,  8527,  8528,  8544,  8579,  8581,  8585,  8592,  8597,  
          455  +    8602,  8604,  8608,  8609,  8611,  8612,  8614,  8615,  8622,  8623,  
          456  +    8654,  8656,  8658,  8659,  8660,  8661,  8692,  8960,  8968,  8972,  
          457  +    8992,  8994,  9001,  9002,  9003,  9084,  9085,  9115,  9140,  9180,  
          458  +    9186,  9216,  9280,  9312,  9372,  9450,  9472,  9655,  9656,  9665,  
          459  +    9666,  9720,  9728,  9839,  9840,  9985,  10088, 10089, 10090, 10091, 
          460  +    10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101, 
          461  +    10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217, 
          462  +    10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627, 
          463  +    10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637, 
          464  +    10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647, 
          465  +    10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750, 
          466  +    11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365, 
          467  +    11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393, 
          468  +    11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520, 
          469  +    11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696, 
          470  +    11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780, 
          471  +    11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800, 
          472  +    11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812, 
          473  +    11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904, 
          474  +    11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296, 
          475  +    12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306, 
          476  +    12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317, 
          477  +    12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347, 
          478  +    12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449, 
          479  +    12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736, 
          480  +    12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938, 
          481  +    12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981, 
          482  +    40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528, 
          483  +    42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624, 
          484  +    42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800, 
          485  +    42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912, 
          486  +    43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043, 
          487  +    43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136, 
          488  +    43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264, 
          489  +    43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395, 
          490  +    43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472, 
          491  +    43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588, 
          492  +    43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643, 
          493  +    43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713, 
          494  +    43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762, 
          495  +    43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003, 
          496  +    44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203, 
          497  +    55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112, 
          498  +    64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320, 
          499  +    64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020, 
          500  +    65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075, 
          501  +    65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086, 
          502  +    65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097, 
          503  +    65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118, 
          504  +    65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279, 
          505  +    65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294, 
          506  +    65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343, 
          507  +    65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378, 
          508  +    65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490, 
          509  +    65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529, 
          510  +    65532, 0,     13,    40,    60,    63,    80,    128,   256,   263,   
          511  +    311,   320,   373,   377,   394,   400,   464,   509,   640,   672,   
          512  +    768,   800,   816,   833,   834,   842,   896,   927,   928,   968,   
          513  +    976,   977,   1024,  1064,  1104,  1184,  2048,  2056,  2058,  2103,  
          514  +    2108,  2111,  2135,  2136,  2304,  2326,  2335,  2336,  2367,  2432,  
          515  +    2494,  2560,  2561,  2565,  2572,  2576,  2581,  2585,  2616,  2623,  
          516  +    2624,  2640,  2656,  2685,  2687,  2816,  2873,  2880,  2904,  2912,  
          517  +    2936,  3072,  3680,  4096,  4097,  4098,  4099,  4152,  4167,  4178,  
          518  +    4198,  4224,  4226,  4227,  4272,  4275,  4279,  4281,  4283,  4285,  
          519  +    4286,  4304,  4336,  4352,  4355,  4391,  4396,  4397,  4406,  4416,  
          520  +    4480,  4482,  4483,  4531,  4534,  4543,  4545,  4549,  4560,  5760,  
          521  +    5803,  5804,  5805,  5806,  5808,  5814,  5815,  5824,  8192,  9216,  
          522  +    9328,  12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248, 
          523  +    53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637, 
          524  +    53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298, 
          525  +    54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441, 
          526  +    54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541, 
          527  +    54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662, 
          528  +    54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922, 
          529  +    54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062, 
          530  +    55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178, 
          531  +    55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961, 
          532  +    60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003, 
          533  +    61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028, 
          534  +    61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099, 
          535  +    61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744, 
          536  +    61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368, 
          537  +    62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971, 
          538  +    63045, 63104, 63232, 0,     42710, 42752, 46900, 46912, 47133, 63488, 
          539  +    1,     32,    256,   0,     65533, 
          540  +  };
          541  +static u16 aFts5UnicodeData[] = {
          542  +    1025,  61,    117,   55,    117,   54,    50,    53,    57,    53,    
          543  +    49,    85,    333,   85,    121,   85,    841,   54,    53,    50,    
          544  +    56,    48,    56,    837,   54,    57,    50,    57,    1057,  61,    
          545  +    53,    151,   58,    53,    56,    58,    39,    52,    57,    34,    
          546  +    58,    56,    58,    57,    79,    56,    37,    85,    56,    47,    
          547  +    39,    51,    111,   53,    745,   57,    233,   773,   57,    261,   
          548  +    1822,  37,    542,   37,    1534,  222,   69,    73,    37,    126,   
          549  +    126,   73,    69,    137,   37,    73,    37,    105,   101,   73,    
          550  +    37,    73,    37,    190,   158,   37,    126,   126,   73,    37,    
          551  +    126,   94,    37,    39,    94,    69,    135,   41,    40,    37,    
          552  +    41,    40,    37,    41,    40,    37,    542,   37,    606,   37,    
          553  +    41,    40,    37,    126,   73,    37,    1886,  197,   73,    37,    
          554  +    73,    69,    126,   105,   37,    286,   2181,  39,    869,   582,   
          555  +    152,   390,   472,   166,   248,   38,    56,    38,    568,   3596,  
          556  +    158,   38,    56,    94,    38,    101,   53,    88,    41,    53,    
          557  +    105,   41,    73,    37,    553,   297,   1125,  94,    37,    105,   
          558  +    101,   798,   133,   94,    57,    126,   94,    37,    1641,  1541,  
          559  +    1118,  58,    172,   75,    1790,  478,   37,    2846,  1225,  38,    
          560  +    213,   1253,  53,    49,    55,    1452,  49,    44,    53,    76,    
          561  +    53,    76,    53,    44,    871,   103,   85,    162,   121,   85,    
          562  +    55,    85,    90,    364,   53,    85,    1031,  38,    327,   684,   
          563  +    333,   149,   71,    44,    3175,  53,    39,    236,   34,    58,    
          564  +    204,   70,    76,    58,    140,   71,    333,   103,   90,    39,    
          565  +    469,   34,    39,    44,    967,   876,   2855,  364,   39,    333,   
          566  +    1063,  300,   70,    58,    117,   38,    711,   140,   38,    300,   
          567  +    38,    108,   38,    172,   501,   807,   108,   53,    39,    359,   
          568  +    876,   108,   42,    1735,  44,    42,    44,    39,    106,   268,   
          569  +    138,   44,    74,    39,    236,   327,   76,    85,    333,   53,    
          570  +    38,    199,   231,   44,    74,    263,   71,    711,   231,   39,    
          571  +    135,   44,    39,    106,   140,   74,    74,    44,    39,    42,    
          572  +    71,    103,   76,    333,   71,    87,    207,   58,    55,    76,    
          573  +    42,    199,   71,    711,   231,   71,    71,    71,    44,    106,   
          574  +    76,    76,    108,   44,    135,   39,    333,   76,    103,   44,    
          575  +    76,    42,    295,   103,   711,   231,   71,    167,   44,    39,    
          576  +    106,   172,   76,    42,    74,    44,    39,    71,    76,    333,   
          577  +    53,    55,    44,    74,    263,   71,    711,   231,   71,    167,   
          578  +    44,    39,    42,    44,    42,    140,   74,    74,    44,    44,    
          579  +    42,    71,    103,   76,    333,   58,    39,    207,   44,    39,    
          580  +    199,   103,   135,   71,    39,    71,    71,    103,   391,   74,    
          581  +    44,    74,    106,   106,   44,    39,    42,    333,   111,   218,   
          582  +    55,    58,    106,   263,   103,   743,   327,   167,   39,    108,   
          583  +    138,   108,   140,   76,    71,    71,    76,    333,   239,   58,    
          584  +    74,    263,   103,   743,   327,   167,   44,    39,    42,    44,    
          585  +    170,   44,    74,    74,    76,    74,    39,    71,    76,    333,   
          586  +    71,    74,    263,   103,   1319,  39,    106,   140,   106,   106,   
          587  +    44,    39,    42,    71,    76,    333,   207,   58,    199,   74,    
          588  +    583,   775,   295,   39,    231,   44,    106,   108,   44,    266,   
          589  +    74,    53,    1543,  44,    71,    236,   55,    199,   38,    268,   
          590  +    53,    333,   85,    71,    39,    71,    39,    39,    135,   231,   
          591  +    103,   39,    39,    71,    135,   44,    71,    204,   76,    39,    
          592  +    167,   38,    204,   333,   135,   39,    122,   501,   58,    53,    
          593  +    122,   76,    218,   333,   335,   58,    44,    58,    44,    58,    
          594  +    44,    54,    50,    54,    50,    74,    263,   1159,  460,   42,    
          595  +    172,   53,    76,    167,   364,   1164,  282,   44,    218,   90,    
          596  +    181,   154,   85,    1383,  74,    140,   42,    204,   42,    76,    
          597  +    74,    76,    39,    333,   213,   199,   74,    76,    135,   108,   
          598  +    39,    106,   71,    234,   103,   140,   423,   44,    74,    76,    
          599  +    202,   44,    39,    42,    333,   106,   44,    90,    1225,  41,    
          600  +    41,    1383,  53,    38,    10631, 135,   231,   39,    135,   1319,  
          601  +    135,   1063,  135,   231,   39,    135,   487,   1831,  135,   2151,  
          602  +    108,   309,   655,   519,   346,   2727,  49,    19847, 85,    551,   
          603  +    61,    839,   54,    50,    2407,  117,   110,   423,   135,   108,   
          604  +    583,   108,   85,    583,   76,    423,   103,   76,    1671,  76,    
          605  +    42,    236,   266,   44,    74,    364,   117,   38,    117,   55,    
          606  +    39,    44,    333,   335,   213,   49,    149,   108,   61,    333,   
          607  +    1127,  38,    1671,  1319,  44,    39,    2247,  935,   108,   138,   
          608  +    76,    106,   74,    44,    202,   108,   58,    85,    333,   967,   
          609  +    167,   1415,  554,   231,   74,    333,   47,    1114,  743,   76,    
          610  +    106,   85,    1703,  42,    44,    42,    236,   44,    42,    44,    
          611  +    74,    268,   202,   332,   44,    333,   333,   245,   38,    213,   
          612  +    140,   42,    1511,  44,    42,    172,   42,    44,    170,   44,    
          613  +    74,    231,   333,   245,   346,   300,   314,   76,    42,    967,   
          614  +    42,    140,   74,    76,    42,    44,    74,    71,    333,   1415,  
          615  +    44,    42,    76,    106,   44,    42,    108,   74,    149,   1159,  
          616  +    266,   268,   74,    76,    181,   333,   103,   333,   967,   198,   
          617  +    85,    277,   108,   53,    428,   42,    236,   135,   44,    135,   
          618  +    74,    44,    71,    1413,  2022,  421,   38,    1093,  1190,  1260,  
          619  +    140,   4830,  261,   3166,  261,   265,   197,   201,   261,   265,   
          620  +    261,   265,   197,   201,   261,   41,    41,    41,    94,    229,   
          621  +    265,   453,   261,   264,   261,   264,   261,   264,   165,   69,    
          622  +    137,   40,    56,    37,    120,   101,   69,    137,   40,    120,   
          623  +    133,   69,    137,   120,   261,   169,   120,   101,   69,    137,   
          624  +    40,    88,    381,   162,   209,   85,    52,    51,    54,    84,    
          625  +    51,    54,    52,    277,   59,    60,    162,   61,    309,   52,    
          626  +    51,    149,   80,    117,   57,    54,    50,    373,   57,    53,    
          627  +    48,    341,   61,    162,   194,   47,    38,    207,   121,   54,    
          628  +    50,    38,    335,   121,   54,    50,    422,   855,   428,   139,   
          629  +    44,    107,   396,   90,    41,    154,   41,    90,    37,    105,   
          630  +    69,    105,   37,    58,    41,    90,    57,    169,   218,   41,    
          631  +    58,    41,    58,    41,    58,    137,   58,    37,    137,   37,    
          632  +    135,   37,    90,    69,    73,    185,   94,    101,   58,    57,    
          633  +    90,    37,    58,    527,   1134,  94,    142,   47,    185,   186,   
          634  +    89,    154,   57,    90,    57,    90,    57,    250,   57,    1018,  
          635  +    89,    90,    57,    58,    57,    1018,  8601,  282,   153,   666,   
          636  +    89,    250,   54,    50,    2618,  57,    986,   825,   1306,  217,   
          637  +    602,   1274,  378,   1935,  2522,  719,   5882,  57,    314,   57,    
          638  +    1754,  281,   3578,  57,    4634,  3322,  54,    50,    54,    50,    
          639  +    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,    
          640  +    975,   1434,  185,   54,    50,    1017,  54,    50,    54,    50,    
          641  +    54,    50,    54,    50,    54,    50,    537,   8218,  4217,  54,    
          642  +    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,    
          643  +    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,    
          644  +    50,    2041,  54,    50,    54,    50,    1049,  54,    50,    8281,  
          645  +    1562,  697,   90,    217,   346,   1513,  1509,  126,   73,    69,    
          646  +    254,   105,   37,    94,    37,    94,    165,   70,    105,   37,    
          647  +    3166,  37,    218,   158,   108,   94,    149,   47,    85,    1221,  
          648  +    37,    37,    1799,  38,    53,    44,    743,   231,   231,   231,   
          649  +    231,   231,   231,   231,   231,   1036,  85,    52,    51,    52,    
          650  +    51,    117,   52,    51,    53,    52,    51,    309,   49,    85,    
          651  +    49,    53,    52,    51,    85,    52,    51,    54,    50,    54,    
          652  +    50,    54,    50,    54,    50,    181,   38,    341,   81,    858,   
          653  +    2874,  6874,  410,   61,    117,   58,    38,    39,    46,    54,    
          654  +    50,    54,    50,    54,    50,    54,    50,    54,    50,    90,    
          655  +    54,    50,    54,    50,    54,    50,    54,    50,    49,    54,    
          656  +    82,    58,    302,   140,   74,    49,    166,   90,    110,   38,    
          657  +    39,    53,    90,    2759,  76,    88,    70,    39,    49,    2887,  
          658  +    53,    102,   39,    1319,  3015,  90,    143,   346,   871,   1178,  
          659  +    519,   1018,  335,   986,   271,   58,    495,   1050,  335,   1274,  
          660  +    495,   2042,  8218,  39,    39,    2074,  39,    39,    679,   38,    
          661  +    36583, 1786,  1287,  198,   85,    8583,  38,    117,   519,   333,   
          662  +    71,    1502,  39,    44,    107,   53,    332,   53,    38,    798,   
          663  +    44,    2247,  334,   76,    213,   760,   294,   88,    478,   69,    
          664  +    2014,  38,    261,   190,   350,   38,    88,    158,   158,   382,   
          665  +    70,    37,    231,   44,    103,   44,    135,   44,    743,   74,    
          666  +    76,    42,    154,   207,   90,    55,    58,    1671,  149,   74,    
          667  +    1607,  522,   44,    85,    333,   588,   199,   117,   39,    333,   
          668  +    903,   268,   85,    743,   364,   74,    53,    935,   108,   42,    
          669  +    1511,  44,    74,    140,   74,    44,    138,   437,   38,    333,   
          670  +    85,    1319,  204,   74,    76,    74,    76,    103,   44,    263,   
          671  +    44,    42,    333,   149,   519,   38,    199,   122,   39,    42,    
          672  +    1543,  44,    39,    108,   71,    76,    167,   76,    39,    44,    
          673  +    39,    71,    38,    85,    359,   42,    76,    74,    85,    39,    
          674  +    70,    42,    44,    199,   199,   199,   231,   231,   1127,  74,    
          675  +    44,    74,    44,    74,    53,    42,    44,    333,   39,    39,    
          676  +    743,   1575,  36,    68,    68,    36,    63,    63,    11719, 3399,  
          677  +    229,   165,   39,    44,    327,   57,    423,   167,   39,    71,    
          678  +    71,    3463,  536,   11623, 54,    50,    2055,  1735,  391,   55,    
          679  +    58,    524,   245,   54,    50,    53,    236,   53,    81,    80,    
          680  +    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,    
          681  +    54,    50,    54,    50,    54,    50,    85,    54,    50,    149,   
          682  +    112,   117,   149,   49,    54,    50,    54,    50,    54,    50,    
          683  +    117,   57,    49,    121,   53,    55,    85,    167,   4327,  34,    
          684  +    117,   55,    117,   54,    50,    53,    57,    53,    49,    85,    
          685  +    333,   85,    121,   85,    841,   54,    53,    50,    56,    48,    
          686  +    56,    837,   54,    57,    50,    57,    54,    50,    53,    54,    
          687  +    50,    85,    327,   38,    1447,  70,    999,   199,   199,   199,   
          688  +    103,   87,    57,    56,    58,    87,    58,    153,   90,    98,    
          689  +    90,    391,   839,   615,   71,    487,   455,   3943,  117,   1455,  
          690  +    314,   1710,  143,   570,   47,    410,   1466,  44,    935,   1575,  
          691  +    999,   143,   551,   46,    263,   46,    967,   53,    1159,  263,   
          692  +    53,    174,   1289,  1285,  2503,  333,   199,   39,    1415,  71,    
          693  +    39,    743,   53,    271,   711,   207,   53,    839,   53,    1799,  
          694  +    71,    39,    108,   76,    140,   135,   103,   871,   108,   44,    
          695  +    271,   309,   935,   79,    53,    1735,  245,   711,   271,   615,   
          696  +    271,   2343,  1007,  42,    44,    42,    1703,  492,   245,   655,   
          697  +    333,   76,    42,    1447,  106,   140,   74,    76,    85,    34,    
          698  +    149,   807,   333,   108,   1159,  172,   42,    268,   333,   149,   
          699  +    76,    42,    1543,  106,   300,   74,    135,   149,   333,   1383,  
          700  +    44,    42,    44,    74,    204,   42,    44,    333,   28135, 3182,  
          701  +    149,   34279, 18215, 2215,  39,    1482,  140,   422,   71,    7898,  
          702  +    1274,  1946,  74,    108,   122,   202,   258,   268,   90,    236,   
          703  +    986,   140,   1562,  2138,  108,   58,    2810,  591,   841,   837,   
          704  +    841,   229,   581,   841,   837,   41,    73,    41,    73,    137,   
          705  +    265,   133,   37,    229,   357,   841,   837,   73,    137,   265,   
          706  +    233,   837,   73,    137,   169,   41,    233,   837,   841,   837,   
          707  +    841,   837,   841,   837,   841,   837,   841,   837,   841,   901,   
          708  +    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,   
          709  +    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,   
          710  +    809,   57,    805,   57,    197,   94,    1613,  135,   871,   71,    
          711  +    39,    39,    327,   135,   39,    39,    39,    39,    39,    39,    
          712  +    103,   71,    39,    39,    39,    39,    39,    39,    71,    39,    
          713  +    135,   231,   135,   135,   39,    327,   551,   103,   167,   551,   
          714  +    89,    1434,  3226,  506,   474,   506,   506,   367,   1018,  1946,  
          715  +    1402,  954,   1402,  314,   90,    1082,  218,   2266,  666,   1210,  
          716  +    186,   570,   2042,  58,    5850,  154,   2010,  154,   794,   2266,  
          717  +    378,   2266,  3738,  39,    39,    39,    39,    39,    39,    17351, 
          718  +    34,    3074,  7692,  63,    63,    
          719  +  };
          720  +
          721  +int sqlite3Fts5UnicodeCategory(int iCode) { 
          722  +  int iRes = -1;
          723  +  int iHi;
          724  +  int iLo;
          725  +  int ret;
          726  +  u16 iKey;
          727  +
          728  +  if( iCode>=(1<<20) ){
          729  +    return 0;
          730  +  }
          731  +  iLo = aFts5UnicodeBlock[(iCode>>16)];
          732  +  iHi = aFts5UnicodeBlock[1+(iCode>>16)];
          733  +  iKey = (iCode & 0xFFFF);
          734  +  while( iHi>iLo ){
          735  +    int iTest = (iHi + iLo) / 2;
          736  +    assert( iTest>=iLo && iTest<iHi );
          737  +    if( iKey>=aFts5UnicodeMap[iTest] ){
          738  +      iRes = iTest;
          739  +      iLo = iTest+1;
          740  +    }else{
          741  +      iHi = iTest;
          742  +    }
          743  +  }
          744  +
          745  +  if( iRes<0 ) return 0;
          746  +  if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
          747  +  ret = aFts5UnicodeData[iRes] & 0x1F;
          748  +  if( ret!=30 ) return ret;
          749  +  return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
          750  +}
          751  +
          752  +void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
          753  +  int i = 0;
          754  +  int iTbl = 0;
          755  +  while( i<128 ){
          756  +    int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
          757  +    int n = (aFts5UnicodeData[iTbl] >> 5) + i;
          758  +    for(; i<128 && i<n; i++){
          759  +      aAscii[i] = (u8)bToken;
          760  +    }
          761  +    iTbl++;
          762  +  }
          763  +}
          764  +

Added ext/fts5/test/fts5cat.test.

            1  +# 2016 Jan 15
            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  +source [file join [file dirname [info script]] fts5_common.tcl]
           14  +ifcapable !fts5 { finish_test ; return }
           15  +set ::testprefix fts5cat
           16  +
           17  +
           18  +do_execsql_test 1.0 {
           19  +  CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize="unicode61 categories 'L*'");
           20  +  INSERT INTO t1 VALUES ('Unlike1option2values3and4column5names');
           21  +}
           22  +
           23  +do_execsql_test 1.1 {
           24  +  SELECT rowid FROM t1('option');
           25  +} {1}
           26  +
           27  +do_execsql_test 1.2 {
           28  +  CREATE VIRTUAL TABLE t2 USING fts5(x);
           29  +  CREATE VIRTUAL TABLE t2t USING fts5vocab(t2, row);
           30  +
           31  +  CREATE VIRTUAL TABLE t3 USING fts5(
           32  +    x, tokenize="unicode61 categories 'L* N* Co Mn'"
           33  +  );
           34  +  CREATE VIRTUAL TABLE t3t USING fts5vocab(t3, row);
           35  +
           36  +  CREATE VIRTUAL TABLE t4 USING fts5(
           37  +    x, tokenize="unicode61 categories 'L* N* Co M*'"
           38  +  );
           39  +  CREATE VIRTUAL TABLE t4t USING fts5vocab(t4, row);
           40  +
           41  +  INSERT INTO t2 VALUES ('สนามกีฬา');
           42  +  INSERT INTO t3 VALUES ('สนามกีฬา');
           43  +  INSERT INTO t4 VALUES ('สนามกีฬา');
           44  +}
           45  +
           46  +do_execsql_test 1.3 {
           47  +  SELECT * FROM t2t
           48  +} {สนามก 1 1 ฬา 1 1}
           49  +
           50  +do_execsql_test 1.4 {
           51  +  SELECT * FROM t3t
           52  +} {สนามกีฬา 1 1}
           53  +
           54  +do_execsql_test 1.5 {
           55  +  SELECT * FROM t4t
           56  +} {สนามกีฬา 1 1}
           57  +
           58  +
           59  +finish_test

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

   144    144     );
   145    145     INSERT INTO VTest (Title, Author) VALUES ('wrinkle in time', 'Bill Smith');
   146    146   
   147    147     SELECT * FROM VTest WHERE 
   148    148     VTest MATCH 'wrinkle in time OR a wrinkle in time' ORDER BY rank;
   149    149   } {{wrinkle in time} {Bill Smith}}
   150    150   
   151         -
          151  +#-------------------------------------------------------------------------
          152  +reset_db
          153  +do_execsql_test 5.0 {
          154  +  CREATE VIRTUAL TABLE ttt USING fts5(a);
          155  +  WITH s(i) AS (
          156  +    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
          157  +  )
          158  +  INSERT INTO ttt SELECT 'word ' || i FROM s;
          159  +}
   152    160   
          161  +do_execsql_test 5.1 {
          162  +  SELECT rowid FROM ttt('word') WHERE rowid BETWEEN 30 AND 40 ORDER BY rank;
          163  +} {30 31 32 33 34 35 36 37 38 39 40}
   153    164   
   154    165   finish_test
          166  +

Added ext/fts5/test/fts5unicode4.test.

            1  +# 2018 July 25
            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  +source [file join [file dirname [info script]] fts5_common.tcl]
           15  +set testprefix fts5unicode4
           16  +
           17  +# If SQLITE_ENABLE_FTS5 is defined, omit this file.
           18  +ifcapable !fts5 {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +do_execsql_test 1.0 {
           24  +  CREATE VIRTUAL TABLE sss USING fts5(a, prefix=3); 
           25  +}
           26  +
           27  +do_execsql_test 1.1 {
           28  +  INSERT INTO sss VALUES('まりや');
           29  +}
           30  +
           31  +finish_test

Changes to ext/misc/completion.c.

   361    361     completionCursorReset(pCur);
   362    362     if( idxNum & 1 ){
   363    363       pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
   364    364       if( pCur->nPrefix>0 ){
   365    365         pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
   366    366         if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
   367    367       }
   368         -    iArg++;
          368  +    iArg = 1;
   369    369     }
   370    370     if( idxNum & 2 ){
   371    371       pCur->nLine = sqlite3_value_bytes(argv[iArg]);
   372    372       if( pCur->nLine>0 ){
   373    373         pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
   374    374         if( pCur->zLine==0 ) return SQLITE_NOMEM;
   375    375       }
   376         -    iArg++;
   377    376     }
   378    377     if( pCur->zLine!=0 && pCur->zPrefix==0 ){
   379    378       int i = pCur->nLine;
   380    379       while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
   381    380         i--;
   382    381       }
   383    382       pCur->nPrefix = pCur->nLine - i;

Added ext/misc/explain.c.

            1  +/*
            2  +** 2018-09-16
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file demonstrates an eponymous virtual table that returns the
           14  +** EXPLAIN output from an SQL statement.
           15  +**
           16  +** Usage example:
           17  +**
           18  +**     .load ./explain
           19  +**     SELECT p2 FROM explain('SELECT * FROM sqlite_master')
           20  +**      WHERE opcode='OpenRead';
           21  +*/
           22  +#if !defined(SQLITEINT_H)
           23  +#include "sqlite3ext.h"
           24  +#endif
           25  +SQLITE_EXTENSION_INIT1
           26  +#include <assert.h>
           27  +#include <string.h>
           28  +
           29  +#ifndef SQLITE_OMIT_VIRTUALTABLE
           30  +
           31  +/* explain_vtab is a subclass of sqlite3_vtab which will
           32  +** serve as the underlying representation of a explain virtual table
           33  +*/
           34  +typedef struct explain_vtab explain_vtab;
           35  +struct explain_vtab {
           36  +  sqlite3_vtab base;  /* Base class - must be first */
           37  +  sqlite3 *db;        /* Database connection for this explain vtab */
           38  +};
           39  +
           40  +/* explain_cursor is a subclass of sqlite3_vtab_cursor which will
           41  +** serve as the underlying representation of a cursor that scans
           42  +** over rows of the result from an EXPLAIN operation.
           43  +*/
           44  +typedef struct explain_cursor explain_cursor;
           45  +struct explain_cursor {
           46  +  sqlite3_vtab_cursor base;  /* Base class - must be first */
           47  +  sqlite3 *db;               /* Database connection for this cursor */
           48  +  char *zSql;                /* Value for the EXPLN_COLUMN_SQL column */
           49  +  sqlite3_stmt *pExplain;    /* Statement being explained */
           50  +  int rc;                    /* Result of last sqlite3_step() on pExplain */
           51  +};
           52  +
           53  +/*
           54  +** The explainConnect() method is invoked to create a new
           55  +** explain_vtab that describes the explain virtual table.
           56  +**
           57  +** Think of this routine as the constructor for explain_vtab objects.
           58  +**
           59  +** All this routine needs to do is:
           60  +**
           61  +**    (1) Allocate the explain_vtab object and initialize all fields.
           62  +**
           63  +**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
           64  +**        result set of queries against explain will look like.
           65  +*/
           66  +static int explainConnect(
           67  +  sqlite3 *db,
           68  +  void *pAux,
           69  +  int argc, const char *const*argv,
           70  +  sqlite3_vtab **ppVtab,
           71  +  char **pzErr
           72  +){
           73  +  explain_vtab *pNew;
           74  +  int rc;
           75  +
           76  +/* Column numbers */
           77  +#define EXPLN_COLUMN_ADDR     0   /* Instruction address */
           78  +#define EXPLN_COLUMN_OPCODE   1   /* Opcode */
           79  +#define EXPLN_COLUMN_P1       2   /* Operand 1 */
           80  +#define EXPLN_COLUMN_P2       3   /* Operand 2 */
           81  +#define EXPLN_COLUMN_P3       4   /* Operand 3 */
           82  +#define EXPLN_COLUMN_P4       5   /* Operand 4 */
           83  +#define EXPLN_COLUMN_P5       6   /* Operand 5 */
           84  +#define EXPLN_COLUMN_COMMENT  7   /* Comment */
           85  +#define EXPLN_COLUMN_SQL      8   /* SQL that is being explained */
           86  +
           87  +
           88  +  rc = sqlite3_declare_vtab(db,
           89  +     "CREATE TABLE x(addr,opcode,p1,p2,p3,p4,p5,comment,sql HIDDEN)");
           90  +  if( rc==SQLITE_OK ){
           91  +    pNew = sqlite3_malloc( sizeof(*pNew) );
           92  +    *ppVtab = (sqlite3_vtab*)pNew;
           93  +    if( pNew==0 ) return SQLITE_NOMEM;
           94  +    memset(pNew, 0, sizeof(*pNew));
           95  +    pNew->db = db;
           96  +  }
           97  +  return rc;
           98  +}
           99  +
          100  +/*
          101  +** This method is the destructor for explain_cursor objects.
          102  +*/
          103  +static int explainDisconnect(sqlite3_vtab *pVtab){
          104  +  sqlite3_free(pVtab);
          105  +  return SQLITE_OK;
          106  +}
          107  +
          108  +/*
          109  +** Constructor for a new explain_cursor object.
          110  +*/
          111  +static int explainOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          112  +  explain_cursor *pCur;
          113  +  pCur = sqlite3_malloc( sizeof(*pCur) );
          114  +  if( pCur==0 ) return SQLITE_NOMEM;
          115  +  memset(pCur, 0, sizeof(*pCur));
          116  +  pCur->db = ((explain_vtab*)p)->db;
          117  +  *ppCursor = &pCur->base;
          118  +  return SQLITE_OK;
          119  +}
          120  +
          121  +/*
          122  +** Destructor for a explain_cursor.
          123  +*/
          124  +static int explainClose(sqlite3_vtab_cursor *cur){
          125  +  explain_cursor *pCur = (explain_cursor*)cur;
          126  +  sqlite3_finalize(pCur->pExplain);
          127  +  sqlite3_free(pCur->zSql);
          128  +  sqlite3_free(pCur);
          129  +  return SQLITE_OK;
          130  +}
          131  +
          132  +
          133  +/*
          134  +** Advance a explain_cursor to its next row of output.
          135  +*/
          136  +static int explainNext(sqlite3_vtab_cursor *cur){
          137  +  explain_cursor *pCur = (explain_cursor*)cur;
          138  +  pCur->rc = sqlite3_step(pCur->pExplain);
          139  +  if( pCur->rc!=SQLITE_DONE && pCur->rc!=SQLITE_ROW ) return pCur->rc;
          140  +  return SQLITE_OK;
          141  +}
          142  +
          143  +/*
          144  +** Return values of columns for the row at which the explain_cursor
          145  +** is currently pointing.
          146  +*/
          147  +static int explainColumn(
          148  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          149  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          150  +  int i                       /* Which column to return */
          151  +){
          152  +  explain_cursor *pCur = (explain_cursor*)cur;
          153  +  if( i==EXPLN_COLUMN_SQL ){
          154  +    sqlite3_result_text(ctx, pCur->zSql, -1, SQLITE_TRANSIENT);
          155  +  }else{
          156  +    sqlite3_result_value(ctx, sqlite3_column_value(pCur->pExplain, i));
          157  +  }
          158  +  return SQLITE_OK;
          159  +}
          160  +
          161  +/*
          162  +** Return the rowid for the current row.  In this implementation, the
          163  +** rowid is the same as the output value.
          164  +*/
          165  +static int explainRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          166  +  explain_cursor *pCur = (explain_cursor*)cur;
          167  +  *pRowid = sqlite3_column_int64(pCur->pExplain, 0);
          168  +  return SQLITE_OK;
          169  +}
          170  +
          171  +/*
          172  +** Return TRUE if the cursor has been moved off of the last
          173  +** row of output.
          174  +*/
          175  +static int explainEof(sqlite3_vtab_cursor *cur){
          176  +  explain_cursor *pCur = (explain_cursor*)cur;
          177  +  return pCur->rc!=SQLITE_ROW;
          178  +}
          179  +
          180  +/*
          181  +** This method is called to "rewind" the explain_cursor object back
          182  +** to the first row of output.  This method is always called at least
          183  +** once prior to any call to explainColumn() or explainRowid() or 
          184  +** explainEof().
          185  +**
          186  +** The argv[0] is the SQL statement that is to be explained.
          187  +*/
          188  +static int explainFilter(
          189  +  sqlite3_vtab_cursor *pVtabCursor, 
          190  +  int idxNum, const char *idxStr,
          191  +  int argc, sqlite3_value **argv
          192  +){
          193  +  explain_cursor *pCur = (explain_cursor *)pVtabCursor;
          194  +  char *zSql = 0;
          195  +  int rc;
          196  +  sqlite3_finalize(pCur->pExplain);
          197  +  pCur->pExplain = 0;
          198  +  if( sqlite3_value_type(argv[0])!=SQLITE_TEXT ){
          199  +    pCur->rc = SQLITE_DONE;
          200  +    return SQLITE_OK;
          201  +  }
          202  +  sqlite3_free(pCur->zSql);
          203  +  pCur->zSql = sqlite3_mprintf("%s", sqlite3_value_text(argv[0]));
          204  +  if( pCur->zSql ){
          205  +    zSql = sqlite3_mprintf("EXPLAIN %s", pCur->zSql);
          206  +  }
          207  +  if( zSql==0 ){
          208  +    rc = SQLITE_NOMEM;
          209  +  }else{
          210  +    rc = sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pExplain, 0);
          211  +    sqlite3_free(zSql);
          212  +  }
          213  +  if( rc ){
          214  +    sqlite3_finalize(pCur->pExplain);
          215  +    pCur->pExplain = 0;
          216  +    sqlite3_free(pCur->zSql);
          217  +    pCur->zSql = 0;
          218  +  }else{
          219  +    pCur->rc = sqlite3_step(pCur->pExplain);
          220  +    rc = (pCur->rc==SQLITE_DONE || pCur->rc==SQLITE_ROW) ? SQLITE_OK : pCur->rc;
          221  +  }
          222  +  return rc;
          223  +}
          224  +
          225  +/*
          226  +** SQLite will invoke this method one or more times while planning a query
          227  +** that uses the explain virtual table.  This routine needs to create
          228  +** a query plan for each invocation and compute an estimated cost for that
          229  +** plan.
          230  +*/
          231  +static int explainBestIndex(
          232  +  sqlite3_vtab *tab,
          233  +  sqlite3_index_info *pIdxInfo
          234  +){
          235  +  int i;
          236  +
          237  +  pIdxInfo->estimatedCost = (double)1000000;
          238  +  pIdxInfo->estimatedRows = 500;
          239  +  for(i=0; i<pIdxInfo->nConstraint; i++){
          240  +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
          241  +    if( p->usable
          242  +     && p->iColumn==EXPLN_COLUMN_SQL
          243  +     && p->op==SQLITE_INDEX_CONSTRAINT_EQ
          244  +    ){
          245  +      pIdxInfo->estimatedCost = 10.0;
          246  +      pIdxInfo->idxNum = 1;
          247  +      pIdxInfo->aConstraintUsage[i].argvIndex = 1;
          248  +      pIdxInfo->aConstraintUsage[i].omit = 1;
          249  +      break;
          250  +    }
          251  +  }
          252  +  return SQLITE_OK;
          253  +}
          254  +
          255  +/*
          256  +** This following structure defines all the methods for the 
          257  +** explain virtual table.
          258  +*/
          259  +static sqlite3_module explainModule = {
          260  +  0,                         /* iVersion */
          261  +  0,                         /* xCreate */
          262  +  explainConnect,            /* xConnect */
          263  +  explainBestIndex,          /* xBestIndex */
          264  +  explainDisconnect,         /* xDisconnect */
          265  +  0,                         /* xDestroy */
          266  +  explainOpen,               /* xOpen - open a cursor */
          267  +  explainClose,              /* xClose - close a cursor */
          268  +  explainFilter,             /* xFilter - configure scan constraints */
          269  +  explainNext,               /* xNext - advance a cursor */
          270  +  explainEof,                /* xEof - check for end of scan */
          271  +  explainColumn,             /* xColumn - read data */
          272  +  explainRowid,              /* xRowid - read data */
          273  +  0,                         /* xUpdate */
          274  +  0,                         /* xBegin */
          275  +  0,                         /* xSync */
          276  +  0,                         /* xCommit */
          277  +  0,                         /* xRollback */
          278  +  0,                         /* xFindMethod */
          279  +  0,                         /* xRename */
          280  +  0,                         /* xSavepoint */
          281  +  0,                         /* xRelease */
          282  +  0,                         /* xRollbackTo */
          283  +};
          284  +
          285  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          286  +
          287  +int sqlite3ExplainVtabInit(sqlite3 *db){
          288  +  int rc = SQLITE_OK;
          289  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          290  +  rc = sqlite3_create_module(db, "explain", &explainModule, 0);
          291  +#endif
          292  +  return rc;
          293  +}
          294  +
          295  +#ifdef _WIN32
          296  +__declspec(dllexport)
          297  +#endif
          298  +int sqlite3_explain_init(
          299  +  sqlite3 *db, 
          300  +  char **pzErrMsg, 
          301  +  const sqlite3_api_routines *pApi
          302  +){
          303  +  int rc = SQLITE_OK;
          304  +  SQLITE_EXTENSION_INIT2(pApi);
          305  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          306  +  rc = sqlite3ExplainVtabInit(db);
          307  +#endif
          308  +  return rc;
          309  +}

Changes to ext/misc/fileio.c.

   200    200   ){
   201    201     HANDLE hFindFile;
   202    202     WIN32_FIND_DATAW fd;
   203    203     LPWSTR zUnicodeName;
   204    204     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
   205    205     zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
   206    206     if( zUnicodeName ){
   207         -    memset(&fd, 0, sizeof(WIN32_FIND_DATA));
          207  +    memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
   208    208       hFindFile = FindFirstFileW(zUnicodeName, &fd);
   209    209       if( hFindFile!=NULL ){
   210    210         pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
   211    211         pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
   212    212         pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
   213    213         FindClose(hFindFile);
   214    214       }

Changes to ext/misc/json1.c.

   168    168     JsonNode *aNode;   /* Array of nodes containing the parse */
   169    169     const char *zJson; /* Original JSON string */
   170    170     u32 *aUp;          /* Index of parent of each node */
   171    171     u8 oom;            /* Set to true if out of memory */
   172    172     u8 nErr;           /* Number of errors seen */
   173    173     u16 iDepth;        /* Nesting depth */
   174    174     int nJson;         /* Length of the zJson string in bytes */
          175  +  u32 iHold;         /* Replace cache line with the lowest iHold value */
   175    176   };
   176    177   
   177    178   /*
   178    179   ** Maximum nesting depth of JSON for this implementation.
   179    180   **
   180    181   ** This limit is needed to avoid a stack overflow in the recursive
   181    182   ** descent parser.  A depth of 2000 is far deeper than any sane JSON
................................................................................
   972    973     jsonParseFillInParentage(pParse, 0, 0);
   973    974     return SQLITE_OK;
   974    975   }
   975    976   
   976    977   /*
   977    978   ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
   978    979   */
   979         -#define JSON_CACHE_ID  (-429938)
          980  +#define JSON_CACHE_ID  (-429938)  /* First cache entry */
          981  +#define JSON_CACHE_SZ  4          /* Max number of cache entries */
   980    982   
   981    983   /*
   982    984   ** Obtain a complete parse of the JSON found in the first argument
   983    985   ** of the argv array.  Use the sqlite3_get_auxdata() cache for this
   984    986   ** parse if it is available.  If the cache is not available or if it
   985    987   ** is no longer valid, parse the JSON again and return the new parse,
   986    988   ** and also register the new parse so that it will be available for
   987    989   ** future sqlite3_get_auxdata() calls.
   988    990   */
   989    991   static JsonParse *jsonParseCached(
   990    992     sqlite3_context *pCtx,
   991         -  sqlite3_value **argv
          993  +  sqlite3_value **argv,
          994  +  sqlite3_context *pErrCtx
   992    995   ){
   993    996     const char *zJson = (const char*)sqlite3_value_text(argv[0]);
   994    997     int nJson = sqlite3_value_bytes(argv[0]);
   995    998     JsonParse *p;
          999  +  JsonParse *pMatch = 0;
         1000  +  int iKey;
         1001  +  int iMinKey = 0;
         1002  +  u32 iMinHold = 0xffffffff;
         1003  +  u32 iMaxHold = 0;
   996   1004     if( zJson==0 ) return 0;
   997         -  p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID);
   998         -  if( p && p->nJson==nJson && memcmp(p->zJson,zJson,nJson)==0 ){
   999         -    p->nErr = 0;
  1000         -    return p; /* The cached entry matches, so return it */
         1005  +  for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
         1006  +    p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
         1007  +    if( p==0 ){
         1008  +      iMinKey = iKey;
         1009  +      break;
         1010  +    }
         1011  +    if( pMatch==0
         1012  +     && p->nJson==nJson
         1013  +     && memcmp(p->zJson,zJson,nJson)==0
         1014  +    ){
         1015  +      p->nErr = 0;
         1016  +      pMatch = p;
         1017  +    }else if( p->iHold<iMinHold ){
         1018  +      iMinHold = p->iHold;
         1019  +      iMinKey = iKey;
         1020  +    }
         1021  +    if( p->iHold>iMaxHold ){
         1022  +      iMaxHold = p->iHold;
         1023  +    }
         1024  +  }
         1025  +  if( pMatch ){
         1026  +    pMatch->nErr = 0;
         1027  +    pMatch->iHold = iMaxHold+1;
         1028  +    return pMatch;
  1001   1029     }
  1002   1030     p = sqlite3_malloc( sizeof(*p) + nJson + 1 );
  1003   1031     if( p==0 ){
  1004   1032       sqlite3_result_error_nomem(pCtx);
  1005   1033       return 0;
  1006   1034     }
  1007   1035     memset(p, 0, sizeof(*p));
  1008   1036     p->zJson = (char*)&p[1];
  1009   1037     memcpy((char*)p->zJson, zJson, nJson+1);
  1010         -  if( jsonParse(p, pCtx, p->zJson) ){
         1038  +  if( jsonParse(p, pErrCtx, p->zJson) ){
  1011   1039       sqlite3_free(p);
  1012   1040       return 0;
  1013   1041     }
  1014   1042     p->nJson = nJson;
  1015         -  sqlite3_set_auxdata(pCtx, JSON_CACHE_ID, p, (void(*)(void*))jsonParseFree);
  1016         -  return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID);
         1043  +  p->iHold = iMaxHold+1;
         1044  +  sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
         1045  +                      (void(*)(void*))jsonParseFree);
         1046  +  return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
  1017   1047   }
  1018   1048   
  1019   1049   /*
  1020   1050   ** Compare the OBJECT label at pNode against zKey,nKey.  Return true on
  1021   1051   ** a match.
  1022   1052   */
  1023   1053   static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
................................................................................
  1382   1412     sqlite3_value **argv
  1383   1413   ){
  1384   1414     JsonParse *p;          /* The parse */
  1385   1415     sqlite3_int64 n = 0;
  1386   1416     u32 i;
  1387   1417     JsonNode *pNode;
  1388   1418   
  1389         -  p = jsonParseCached(ctx, argv);
         1419  +  p = jsonParseCached(ctx, argv, ctx);
  1390   1420     if( p==0 ) return;
  1391   1421     assert( p->nNode );
  1392   1422     if( argc==2 ){
  1393   1423       const char *zPath = (const char*)sqlite3_value_text(argv[1]);
  1394   1424       pNode = jsonLookup(p, zPath, 0, ctx);
  1395   1425     }else{
  1396   1426       pNode = p->aNode;
................................................................................
  1423   1453     JsonParse *p;          /* The parse */
  1424   1454     JsonNode *pNode;
  1425   1455     const char *zPath;
  1426   1456     JsonString jx;
  1427   1457     int i;
  1428   1458   
  1429   1459     if( argc<2 ) return;
  1430         -  p = jsonParseCached(ctx, argv);
         1460  +  p = jsonParseCached(ctx, argv, ctx);
  1431   1461     if( p==0 ) return;
  1432   1462     jsonInit(&jx, ctx);
  1433   1463     jsonAppendChar(&jx, '[');
  1434   1464     for(i=1; i<argc; i++){
  1435   1465       zPath = (const char*)sqlite3_value_text(argv[i]);
  1436   1466       pNode = jsonLookup(p, zPath, 0, ctx);
  1437   1467       if( p->nErr ) break;
................................................................................
  1730   1760   ** either the JSON or PATH inputs are not well-formed.
  1731   1761   */
  1732   1762   static void jsonTypeFunc(
  1733   1763     sqlite3_context *ctx,
  1734   1764     int argc,
  1735   1765     sqlite3_value **argv
  1736   1766   ){
  1737         -  JsonParse x;          /* The parse */
         1767  +  JsonParse *p;          /* The parse */
  1738   1768     const char *zPath;
  1739   1769     JsonNode *pNode;
  1740   1770   
  1741         -  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
  1742         -  assert( x.nNode );
         1771  +  p = jsonParseCached(ctx, argv, ctx);
         1772  +  if( p==0 ) return;
  1743   1773     if( argc==2 ){
  1744   1774       zPath = (const char*)sqlite3_value_text(argv[1]);
  1745         -    pNode = jsonLookup(&x, zPath, 0, ctx);
         1775  +    pNode = jsonLookup(p, zPath, 0, ctx);
  1746   1776     }else{
  1747         -    pNode = x.aNode;
         1777  +    pNode = p->aNode;
  1748   1778     }
  1749   1779     if( pNode ){
  1750   1780       sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
  1751   1781     }
  1752         -  jsonParseReset(&x);
  1753   1782   }
  1754   1783   
  1755   1784   /*
  1756   1785   ** json_valid(JSON)
  1757   1786   **
  1758   1787   ** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
  1759   1788   ** Return 0 otherwise.
  1760   1789   */
  1761   1790   static void jsonValidFunc(
  1762   1791     sqlite3_context *ctx,
  1763   1792     int argc,
  1764   1793     sqlite3_value **argv
  1765   1794   ){
  1766         -  JsonParse x;          /* The parse */
  1767         -  int rc = 0;
  1768         -
         1795  +  JsonParse *p;          /* The parse */
  1769   1796     UNUSED_PARAM(argc);
  1770         -  if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
  1771         -    rc = 1;
  1772         -  }
  1773         -  jsonParseReset(&x);
  1774         -  sqlite3_result_int(ctx, rc);
         1797  +  p = jsonParseCached(ctx, argv, 0);
         1798  +  sqlite3_result_int(ctx, p!=0);
  1775   1799   }
  1776   1800   
  1777   1801   
  1778   1802   /****************************************************************************
  1779   1803   ** Aggregate SQL function implementations
  1780   1804   ****************************************************************************/
  1781   1805   /*
................................................................................
  1808   1832     if( pStr ){
  1809   1833       pStr->pCtx = ctx;
  1810   1834       jsonAppendChar(pStr, ']');
  1811   1835       if( pStr->bErr ){
  1812   1836         if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
  1813   1837         assert( pStr->bStatic );
  1814   1838       }else if( isFinal ){
  1815         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
         1839  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
  1816   1840                             pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
  1817   1841         pStr->bStatic = 1;
  1818   1842       }else{
  1819         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, SQLITE_TRANSIENT);
         1843  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
  1820   1844         pStr->nUsed--;
  1821   1845       }
  1822   1846     }else{
  1823   1847       sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
  1824   1848     }
  1825   1849     sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1826   1850   }
................................................................................
  1861   1885       if( z[i]=='"' ){
  1862   1886         inStr = !inStr;
  1863   1887       }else if( z[i]=='\\' ){
  1864   1888         i++;
  1865   1889       }
  1866   1890     }
  1867   1891     pStr->nUsed -= i;      
  1868         -  memmove(&z[1], &z[i+1], pStr->nUsed-1);
         1892  +  memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
  1869   1893   }
  1870   1894   #else
  1871   1895   # define jsonGroupInverse 0
  1872   1896   #endif
  1873   1897   
  1874   1898   
  1875   1899   /*
................................................................................
  1907   1931     pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
  1908   1932     if( pStr ){
  1909   1933       jsonAppendChar(pStr, '}');
  1910   1934       if( pStr->bErr ){
  1911   1935         if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
  1912   1936         assert( pStr->bStatic );
  1913   1937       }else if( isFinal ){
  1914         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
         1938  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
  1915   1939                             pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
  1916   1940         pStr->bStatic = 1;
  1917   1941       }else{
  1918         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, SQLITE_TRANSIENT);
         1942  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
  1919   1943         pStr->nUsed--;
  1920   1944       }
  1921   1945     }else{
  1922   1946       sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
  1923   1947     }
  1924   1948     sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1925   1949   }
................................................................................
  2455   2479   #endif
  2456   2480     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
  2457   2481       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
  2458   2482                                    SQLITE_UTF8 | SQLITE_DETERMINISTIC, 
  2459   2483                                    (void*)&aFunc[i].flag,
  2460   2484                                    aFunc[i].xFunc, 0, 0);
  2461   2485     }
         2486  +#ifndef SQLITE_OMIT_WINDOWFUNC
  2462   2487     for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
  2463   2488       rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
  2464   2489                                    SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
  2465   2490                                    aAgg[i].xStep, aAgg[i].xFinal,
  2466   2491                                    aAgg[i].xValue, jsonGroupInverse, 0);
  2467   2492     }
         2493  +#endif
  2468   2494   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2469   2495     for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
  2470   2496       rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
  2471   2497     }
  2472   2498   #endif
  2473   2499     return rc;
  2474   2500   }

Added ext/misc/memstat.c.

            1  +/*
            2  +** 2018-09-27
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file demonstrates an eponymous virtual table that returns information
           14  +** from sqlite3_status64() and sqlite3_db_status().
           15  +**
           16  +** Usage example:
           17  +**
           18  +**     .load ./memstat
           19  +**     .mode quote
           20  +**     .header on
           21  +**     SELECT * FROM memstat;
           22  +*/
           23  +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_MEMSTATVTAB)
           24  +#if !defined(SQLITEINT_H)
           25  +#include "sqlite3ext.h"
           26  +#endif
           27  +SQLITE_EXTENSION_INIT1
           28  +#include <assert.h>
           29  +#include <string.h>
           30  +
           31  +#ifndef SQLITE_OMIT_VIRTUALTABLE
           32  +
           33  +/* memstat_vtab is a subclass of sqlite3_vtab which will
           34  +** serve as the underlying representation of a memstat virtual table
           35  +*/
           36  +typedef struct memstat_vtab memstat_vtab;
           37  +struct memstat_vtab {
           38  +  sqlite3_vtab base;  /* Base class - must be first */
           39  +  sqlite3 *db;        /* Database connection for this memstat vtab */
           40  +};
           41  +
           42  +/* memstat_cursor is a subclass of sqlite3_vtab_cursor which will
           43  +** serve as the underlying representation of a cursor that scans
           44  +** over rows of the result
           45  +*/
           46  +typedef struct memstat_cursor memstat_cursor;
           47  +struct memstat_cursor {
           48  +  sqlite3_vtab_cursor base;  /* Base class - must be first */
           49  +  sqlite3 *db;               /* Database connection for this cursor */
           50  +  int iRowid;                /* Current row in aMemstatColumn[] */
           51  +  int iDb;                   /* Which schema we are looking at */
           52  +  int nDb;                   /* Number of schemas */
           53  +  char **azDb;               /* Names of all schemas */
           54  +  sqlite3_int64 aVal[2];     /* Result values */
           55  +};
           56  +
           57  +/*
           58  +** The memstatConnect() method is invoked to create a new
           59  +** memstat_vtab that describes the memstat virtual table.
           60  +**
           61  +** Think of this routine as the constructor for memstat_vtab objects.
           62  +**
           63  +** All this routine needs to do is:
           64  +**
           65  +**    (1) Allocate the memstat_vtab object and initialize all fields.
           66  +**
           67  +**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
           68  +**        result set of queries against memstat will look like.
           69  +*/
           70  +static int memstatConnect(
           71  +  sqlite3 *db,
           72  +  void *pAux,
           73  +  int argc, const char *const*argv,
           74  +  sqlite3_vtab **ppVtab,
           75  +  char **pzErr
           76  +){
           77  +  memstat_vtab *pNew;
           78  +  int rc;
           79  +
           80  +/* Column numbers */
           81  +#define MSV_COLUMN_NAME    0   /* Name of quantity being measured */
           82  +#define MSV_COLUMN_SCHEMA  1   /* schema name */
           83  +#define MSV_COLUMN_VALUE   2   /* Current value */
           84  +#define MSV_COLUMN_HIWTR   3   /* Highwater mark */
           85  +
           86  +  rc = sqlite3_declare_vtab(db,"CREATE TABLE x(name,schema,value,hiwtr)");
           87  +  if( rc==SQLITE_OK ){
           88  +    pNew = sqlite3_malloc( sizeof(*pNew) );
           89  +    *ppVtab = (sqlite3_vtab*)pNew;
           90  +    if( pNew==0 ) return SQLITE_NOMEM;
           91  +    memset(pNew, 0, sizeof(*pNew));
           92  +    pNew->db = db;
           93  +  }
           94  +  return rc;
           95  +}
           96  +
           97  +/*
           98  +** This method is the destructor for memstat_cursor objects.
           99  +*/
          100  +static int memstatDisconnect(sqlite3_vtab *pVtab){
          101  +  sqlite3_free(pVtab);
          102  +  return SQLITE_OK;
          103  +}
          104  +
          105  +/*
          106  +** Constructor for a new memstat_cursor object.
          107  +*/
          108  +static int memstatOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
          109  +  memstat_cursor *pCur;
          110  +  pCur = sqlite3_malloc( sizeof(*pCur) );
          111  +  if( pCur==0 ) return SQLITE_NOMEM;
          112  +  memset(pCur, 0, sizeof(*pCur));
          113  +  pCur->db = ((memstat_vtab*)p)->db;
          114  +  *ppCursor = &pCur->base;
          115  +  return SQLITE_OK;
          116  +}
          117  +
          118  +/*
          119  +** Clear all the schema names from a cursor
          120  +*/
          121  +static void memstatClearSchema(memstat_cursor *pCur){
          122  +  int i;
          123  +  if( pCur->azDb==0 ) return;
          124  +  for(i=0; i<pCur->nDb; i++){
          125  +    sqlite3_free(pCur->azDb[i]);
          126  +  }
          127  +  sqlite3_free(pCur->azDb);
          128  +  pCur->azDb = 0;
          129  +  pCur->nDb = 0;
          130  +}
          131  +
          132  +/*
          133  +** Fill in the azDb[] array for the cursor.
          134  +*/
          135  +static int memstatFindSchemas(memstat_cursor *pCur){
          136  +  sqlite3_stmt *pStmt = 0;
          137  +  int rc;
          138  +  if( pCur->nDb ) return SQLITE_OK;
          139  +  rc = sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pStmt, 0);
          140  +  if( rc ){
          141  +    sqlite3_finalize(pStmt);
          142  +    return rc;
          143  +  }
          144  +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
          145  +    char **az, *z;
          146  +    az = sqlite3_realloc(pCur->azDb, sizeof(char*)*(pCur->nDb+1));
          147  +    if( az==0 ){
          148  +      memstatClearSchema(pCur);
          149  +      return SQLITE_NOMEM;
          150  +    }
          151  +    pCur->azDb = az;
          152  +    z = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
          153  +    if( z==0 ){
          154  +      memstatClearSchema(pCur);
          155  +      return SQLITE_NOMEM;
          156  +    }
          157  +    pCur->azDb[pCur->nDb] = z;
          158  +    pCur->nDb++;
          159  +  }
          160  +  sqlite3_finalize(pStmt);
          161  +  return SQLITE_OK;
          162  +}
          163  +
          164  +
          165  +/*
          166  +** Destructor for a memstat_cursor.
          167  +*/
          168  +static int memstatClose(sqlite3_vtab_cursor *cur){
          169  +  memstat_cursor *pCur = (memstat_cursor*)cur;
          170  +  memstatClearSchema(pCur);
          171  +  sqlite3_free(cur);
          172  +  return SQLITE_OK;
          173  +}
          174  +
          175  +
          176  +/*
          177  +** Allowed values for aMemstatColumn[].eType
          178  +*/
          179  +#define MSV_GSTAT   0          /* sqlite3_status64() information */
          180  +#define MSV_DB      1          /* sqlite3_db_status() information */
          181  +#define MSV_ZIPVFS  2          /* ZIPVFS file-control with 64-bit return */
          182  +
          183  +/*
          184  +** An array of quantities that can be measured and reported by
          185  +** this virtual table
          186  +*/
          187  +static const struct MemstatColumns {
          188  +  const char *zName;    /* Symbolic name */
          189  +  unsigned char eType;  /* Type of interface */
          190  +  unsigned char mNull;  /* Bitmask of which columns are NULL */
          191  +                        /* 2: dbname,  4: current,  8: hiwtr */
          192  +  int eOp;              /* Opcode */
          193  +} aMemstatColumn[] = {
          194  + {"MEMORY_USED",            MSV_GSTAT,  2, SQLITE_STATUS_MEMORY_USED          },
          195  + {"MALLOC_SIZE",            MSV_GSTAT,  6, SQLITE_STATUS_MALLOC_SIZE          },
          196  + {"MALLOC_COUNT",           MSV_GSTAT,  2, SQLITE_STATUS_MALLOC_COUNT         },
          197  + {"PAGECACHE_USED",         MSV_GSTAT,  2, SQLITE_STATUS_PAGECACHE_USED       },
          198  + {"PAGECACHE_OVERFLOW",     MSV_GSTAT,  2, SQLITE_STATUS_PAGECACHE_OVERFLOW   },
          199  + {"PAGECACHE_SIZE",         MSV_GSTAT,  6, SQLITE_STATUS_PAGECACHE_SIZE       },
          200  + {"PARSER_STACK",           MSV_GSTAT,  6, SQLITE_STATUS_PARSER_STACK         },
          201  + {"DB_LOOKASIDE_USED",      MSV_DB,     2, SQLITE_DBSTATUS_LOOKASIDE_USED     },
          202  + {"DB_LOOKASIDE_HIT",       MSV_DB,     6, SQLITE_DBSTATUS_LOOKASIDE_HIT      },
          203  + {"DB_LOOKASIDE_MISS_SIZE", MSV_DB,     6, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE},
          204  + {"DB_LOOKASIDE_MISS_FULL", MSV_DB,     6, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL},
          205  + {"DB_CACHE_USED",          MSV_DB,    10, SQLITE_DBSTATUS_CACHE_USED         },
          206  +#if SQLITE_VERSION_NUMBER >= 3140000
          207  + {"DB_CACHE_USED_SHARED",   MSV_DB,    10, SQLITE_DBSTATUS_CACHE_USED_SHARED  },
          208  +#endif
          209  + {"DB_SCHEMA_USED",         MSV_DB,    10, SQLITE_DBSTATUS_SCHEMA_USED        },
          210  + {"DB_STMT_USED",           MSV_DB,    10, SQLITE_DBSTATUS_STMT_USED          },
          211  + {"DB_CACHE_HIT",           MSV_DB,    10, SQLITE_DBSTATUS_CACHE_HIT          },
          212  + {"DB_CACHE_MISS",          MSV_DB,    10, SQLITE_DBSTATUS_CACHE_MISS         },
          213  + {"DB_CACHE_WRITE",         MSV_DB,    10, SQLITE_DBSTATUS_CACHE_WRITE        },
          214  +#if SQLITE_VERSION_NUMBER >= 3230000
          215  + {"DB_CACHE_SPILL",         MSV_DB,    10, SQLITE_DBSTATUS_CACHE_SPILL        },
          216  +#endif
          217  + {"DB_DEFERRED_FKS",        MSV_DB,    10, SQLITE_DBSTATUS_DEFERRED_FKS       },
          218  +#ifdef SQLITE_ENABLE_ZIPVFS
          219  + {"ZIPVFS_CACHE_USED",      MSV_ZIPVFS, 8, 231454 },
          220  + {"ZIPVFS_CACHE_HIT",       MSV_ZIPVFS, 8, 231455 },
          221  + {"ZIPVFS_CACHE_MISS",      MSV_ZIPVFS, 8, 231456 },
          222  + {"ZIPVFS_CACHE_WRITE",     MSV_ZIPVFS, 8, 231457 },
          223  + {"ZIPVFS_DIRECT_READ",     MSV_ZIPVFS, 8, 231458 },
          224  + {"ZIPVFS_DIRECT_BYTES",    MSV_ZIPVFS, 8, 231459 },
          225  +#endif /* SQLITE_ENABLE_ZIPVFS */
          226  +};
          227  +#define MSV_NROW (sizeof(aMemstatColumn)/sizeof(aMemstatColumn[0]))
          228  +
          229  +/*
          230  +** Advance a memstat_cursor to its next row of output.
          231  +*/
          232  +static int memstatNext(sqlite3_vtab_cursor *cur){
          233  +  memstat_cursor *pCur = (memstat_cursor*)cur;
          234  +  int i;
          235  +  assert( pCur->iRowid<=MSV_NROW );
          236  +  while(1){
          237  +    i = (int)pCur->iRowid - 1;
          238  +    if( (aMemstatColumn[i].mNull & 2)!=0 || (++pCur->iDb)>=pCur->nDb ){
          239  +      pCur->iRowid++;
          240  +      if( pCur->iRowid>MSV_NROW ) return SQLITE_OK;  /* End of the table */
          241  +      pCur->iDb = 0;
          242  +      i++;
          243  +    }
          244  +    pCur->aVal[0] = 0;
          245  +    pCur->aVal[1] = 0;    
          246  +    switch( aMemstatColumn[i].eType ){
          247  +      case MSV_GSTAT: {
          248  +        if( sqlite3_libversion_number()>=3010000 ){
          249  +          sqlite3_status64(aMemstatColumn[i].eOp,
          250  +                           &pCur->aVal[0], &pCur->aVal[1],0);
          251  +        }else{
          252  +          int xCur, xHiwtr;
          253  +          sqlite3_status(aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0);
          254  +          pCur->aVal[0] = xCur;
          255  +          pCur->aVal[1] = xHiwtr;
          256  +        }
          257  +        break;
          258  +      }
          259  +      case MSV_DB: {
          260  +        int xCur, xHiwtr;
          261  +        sqlite3_db_status(pCur->db, aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0);
          262  +        pCur->aVal[0] = xCur;
          263  +        pCur->aVal[1] = xHiwtr;
          264  +        break;
          265  +      }
          266  +      case MSV_ZIPVFS: {
          267  +        int rc;
          268  +        rc = sqlite3_file_control(pCur->db, pCur->azDb[pCur->iDb],
          269  +                                  aMemstatColumn[i].eOp, (void*)&pCur->aVal[0]);
          270  +        if( rc!=SQLITE_OK ) continue;
          271  +        break;
          272  +      }
          273  +    }
          274  +    break;
          275  +  }
          276  +  return SQLITE_OK;
          277  +}
          278  +  
          279  +
          280  +/*
          281  +** Return values of columns for the row at which the memstat_cursor
          282  +** is currently pointing.
          283  +*/
          284  +static int memstatColumn(
          285  +  sqlite3_vtab_cursor *cur,   /* The cursor */
          286  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
          287  +  int iCol                    /* Which column to return */
          288  +){
          289  +  memstat_cursor *pCur = (memstat_cursor*)cur;
          290  +  int i;
          291  +  assert( pCur->iRowid>0 && pCur->iRowid<=MSV_NROW );
          292  +  i = (int)pCur->iRowid - 1;
          293  +  if( (aMemstatColumn[i].mNull & (1<<iCol))!=0 ){
          294  +    return SQLITE_OK;
          295  +  }
          296  +  switch( iCol ){
          297  +    case MSV_COLUMN_NAME: {
          298  +      sqlite3_result_text(ctx, aMemstatColumn[i].zName, -1, SQLITE_STATIC);
          299  +      break;
          300  +    }
          301  +    case MSV_COLUMN_SCHEMA: {
          302  +      sqlite3_result_text(ctx, pCur->azDb[pCur->iDb], -1, 0);
          303  +      break;
          304  +    }
          305  +    case MSV_COLUMN_VALUE: {
          306  +      sqlite3_result_int64(ctx, pCur->aVal[0]);
          307  +      break;
          308  +    }
          309  +    case MSV_COLUMN_HIWTR: {
          310  +      sqlite3_result_int64(ctx, pCur->aVal[1]);
          311  +      break;
          312  +    }
          313  +  }
          314  +  return SQLITE_OK;
          315  +}
          316  +
          317  +/*
          318  +** Return the rowid for the current row.  In this implementation, the
          319  +** rowid is the same as the output value.
          320  +*/
          321  +static int memstatRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
          322  +  memstat_cursor *pCur = (memstat_cursor*)cur;
          323  +  *pRowid = pCur->iRowid*1000 + pCur->iDb;
          324  +  return SQLITE_OK;
          325  +}
          326  +
          327  +/*
          328  +** Return TRUE if the cursor has been moved off of the last
          329  +** row of output.
          330  +*/
          331  +static int memstatEof(sqlite3_vtab_cursor *cur){
          332  +  memstat_cursor *pCur = (memstat_cursor*)cur;
          333  +  return pCur->iRowid>MSV_NROW;
          334  +}
          335  +
          336  +/*
          337  +** This method is called to "rewind" the memstat_cursor object back
          338  +** to the first row of output.  This method is always called at least
          339  +** once prior to any call to memstatColumn() or memstatRowid() or 
          340  +** memstatEof().
          341  +*/
          342  +static int memstatFilter(
          343  +  sqlite3_vtab_cursor *pVtabCursor, 
          344  +  int idxNum, const char *idxStr,
          345  +  int argc, sqlite3_value **argv
          346  +){
          347  +  memstat_cursor *pCur = (memstat_cursor *)pVtabCursor;
          348  +  int rc = memstatFindSchemas(pCur);
          349  +  if( rc ) return rc;
          350  +  pCur->iRowid = 0;
          351  +  pCur->iDb = 0;
          352  +  return memstatNext(pVtabCursor);
          353  +}
          354  +
          355  +/*
          356  +** SQLite will invoke this method one or more times while planning a query
          357  +** that uses the memstat virtual table.  This routine needs to create
          358  +** a query plan for each invocation and compute an estimated cost for that
          359  +** plan.
          360  +*/
          361  +static int memstatBestIndex(
          362  +  sqlite3_vtab *tab,
          363  +  sqlite3_index_info *pIdxInfo
          364  +){
          365  +  pIdxInfo->estimatedCost = (double)500;
          366  +  pIdxInfo->estimatedRows = 500;
          367  +  return SQLITE_OK;
          368  +}
          369  +
          370  +/*
          371  +** This following structure defines all the methods for the 
          372  +** memstat virtual table.
          373  +*/
          374  +static sqlite3_module memstatModule = {
          375  +  0,                         /* iVersion */
          376  +  0,                         /* xCreate */
          377  +  memstatConnect,            /* xConnect */
          378  +  memstatBestIndex,          /* xBestIndex */
          379  +  memstatDisconnect,         /* xDisconnect */
          380  +  0,                         /* xDestroy */
          381  +  memstatOpen,               /* xOpen - open a cursor */
          382  +  memstatClose,              /* xClose - close a cursor */
          383  +  memstatFilter,             /* xFilter - configure scan constraints */
          384  +  memstatNext,               /* xNext - advance a cursor */
          385  +  memstatEof,                /* xEof - check for end of scan */
          386  +  memstatColumn,             /* xColumn - read data */
          387  +  memstatRowid,              /* xRowid - read data */
          388  +  0,                         /* xUpdate */
          389  +  0,                         /* xBegin */
          390  +  0,                         /* xSync */
          391  +  0,                         /* xCommit */
          392  +  0,                         /* xRollback */
          393  +  0,                         /* xFindMethod */
          394  +  0,                         /* xRename */
          395  +  0,                         /* xSavepoint */
          396  +  0,                         /* xRelease */
          397  +  0,                         /* xRollbackTo */
          398  +};
          399  +
          400  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          401  +
          402  +int sqlite3MemstatVtabInit(sqlite3 *db){
          403  +  int rc = SQLITE_OK;
          404  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          405  +  rc = sqlite3_create_module(db, "sqlite_memstat", &memstatModule, 0);
          406  +#endif
          407  +  return rc;
          408  +}
          409  +
          410  +#ifndef SQLITE_CORE
          411  +#ifdef _WIN32
          412  +__declspec(dllexport)
          413  +#endif
          414  +int sqlite3_memstat_init(
          415  +  sqlite3 *db, 
          416  +  char **pzErrMsg, 
          417  +  const sqlite3_api_routines *pApi
          418  +){
          419  +  int rc = SQLITE_OK;
          420  +  SQLITE_EXTENSION_INIT2(pApi);
          421  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          422  +  rc = sqlite3MemstatVtabInit(db);
          423  +#endif
          424  +  return rc;
          425  +}
          426  +#endif /* SQLITE_CORE */
          427  +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_MEMSTATVTAB) */

Changes to ext/misc/sha1.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13         -** This SQLite extension implements a functions that compute SHA1 hashes.
           13  +** This SQLite extension implements functions that compute SHA1 hashes.
    14     14   ** Two SQL functions are implemented:
    15     15   **
    16     16   **     sha1(X)
    17     17   **     sha1_query(Y)
    18     18   **
    19     19   ** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
    20     20   ** X is NULL.

Changes to ext/misc/shathree.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   ******************************************************************************
    12     12   **
    13         -** This SQLite extension implements a functions that compute SHA1 hashes.
           13  +** This SQLite extension implements functions that compute SHA3 hashes.
    14     14   ** Two SQL functions are implemented:
    15     15   **
    16     16   **     sha3(X,SIZE)
    17     17   **     sha3_query(Y,SIZE)
    18     18   **
    19     19   ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
    20     20   ** X is NULL.

Changes to ext/misc/spellfix.c.

  1291   1291     return nChar;
  1292   1292   }
  1293   1293   
  1294   1294   typedef struct Transliteration Transliteration;
  1295   1295   struct Transliteration {
  1296   1296    unsigned short int cFrom;
  1297   1297    unsigned char cTo0, cTo1, cTo2, cTo3;
         1298  +#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
         1299  + unsigned char cTo4;
         1300  +#endif
  1298   1301   };
  1299   1302   
  1300   1303   /*
  1301   1304   ** Table of translations from unicode characters into ASCII.
  1302   1305   */
  1303   1306   static const Transliteration translit[] = {
  1304   1307     { 0x00A0,  0x20, 0x00, 0x00, 0x00 },  /*   to   */
................................................................................
  1704   1707   **
  1705   1708   ** The returned string might contain more characters than the input.
  1706   1709   **
  1707   1710   ** Space to hold the returned string comes from sqlite3_malloc() and
  1708   1711   ** should be freed by the caller.
  1709   1712   */
  1710   1713   static unsigned char *transliterate(const unsigned char *zIn, int nIn){
         1714  +#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
         1715  +  unsigned char *zOut = sqlite3_malloc64( nIn*5 + 1 );
         1716  +#else
  1711   1717     unsigned char *zOut = sqlite3_malloc64( nIn*4 + 1 );
         1718  +#endif
  1712   1719     int c, sz, nOut;
  1713   1720     if( zOut==0 ) return 0;
  1714   1721     nOut = 0;
  1715   1722     while( nIn>0 ){
  1716   1723       c = utf8Read(zIn, nIn, &sz);
  1717   1724       zIn += sz;
  1718   1725       nIn -= sz;
................................................................................
  1728   1735             zOut[nOut++] = tbl[x].cTo0;
  1729   1736             if( tbl[x].cTo1 ){
  1730   1737               zOut[nOut++] = tbl[x].cTo1;
  1731   1738               if( tbl[x].cTo2 ){
  1732   1739                 zOut[nOut++] = tbl[x].cTo2;
  1733   1740                 if( tbl[x].cTo3 ){
  1734   1741                   zOut[nOut++] = tbl[x].cTo3;
         1742  +#ifdef SQLITE_SPELLFIX_5BYTE_MAPPINGS
         1743  +                if( tbl[x].cTo4 ){
         1744  +                  zOut[nOut++] = tbl[x].cTo4;
         1745  +                }
         1746  +#endif /* SQLITE_SPELLFIX_5BYTE_MAPPINGS */
  1735   1747                 }
  1736   1748               }
  1737   1749             }
  1738   1750             c = 0;
  1739   1751             break;
  1740   1752           }else if( tbl[x].cFrom>c ){
  1741   1753             xTop = x-1;

Changes to ext/rbu/rbu.c.

    74     74   }
    75     75   
    76     76   int main(int argc, char **argv){
    77     77     int i;
    78     78     const char *zTarget;            /* Target database to apply RBU to */
    79     79     const char *zRbu;               /* Database containing RBU */
    80     80     char zBuf[200];                 /* Buffer for printf() */
    81         -  char *zErrmsg;                  /* Error message, if any */
           81  +  char *zErrmsg = 0;              /* Error message, if any */
    82     82     sqlite3rbu *pRbu;               /* RBU handle */
    83     83     int nStep = 0;                  /* Maximum number of step() calls */
    84     84     int nStatStep = 0;              /* Report stats after this many step calls */
    85     85     int bVacuum = 0;
    86     86     const char *zPreSql = 0;
    87         -  int rc;
           87  +  int rc = SQLITE_OK;
    88     88     sqlite3_int64 nProgress = 0;
    89     89     int nArgc = argc-2;
    90     90   
    91     91     if( argc<3 ) usage(argv[0]);
    92     92     for(i=1; i<nArgc; i++){
    93     93       const char *zArg = argv[i];
    94     94       int nArg = strlen(zArg);
................................................................................
   122    122       pRbu = sqlite3rbu_vacuum(zTarget, zRbu);
   123    123     }else{
   124    124       pRbu = sqlite3rbu_open(zTarget, zRbu, 0);
   125    125     }
   126    126     report_rbu_vfs(pRbu);
   127    127   
   128    128     if( zPreSql && pRbu ){
   129         -    sqlite3 *db = sqlite3rbu_db(pRbu, 0);
   130         -    rc = sqlite3_exec(db, zPreSql, 0, 0, 0);
          129  +    sqlite3 *dbMain = sqlite3rbu_db(pRbu, 0);
          130  +    rc = sqlite3_exec(dbMain, zPreSql, 0, 0, 0);
   131    131       if( rc==SQLITE_OK ){
   132         -      sqlite3 *db = sqlite3rbu_db(pRbu, 1);
   133         -      rc = sqlite3_exec(db, zPreSql, 0, 0, 0);
          132  +      sqlite3 *dbRbu = sqlite3rbu_db(pRbu, 1);
          133  +      rc = sqlite3_exec(dbRbu, zPreSql, 0, 0, 0);
   134    134       }
   135    135     }
   136    136   
   137    137     /* If nStep is less than or equal to zero, call
   138    138     ** sqlite3rbu_step() until either the RBU has been completely applied
   139    139     ** or an error occurs. Or, if nStep is greater than zero, call
   140    140     ** sqlite3rbu_step() a maximum of nStep times.  */

Added ext/rtree/geopoly.c.

            1  +/*
            2  +** 2018-05-25
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file implements an alternative R-Tree virtual table that
           14  +** uses polygons to express the boundaries of 2-dimensional objects.
           15  +**
           16  +** This file is #include-ed onto the end of "rtree.c" so that it has
           17  +** access to all of the R-Tree internals.
           18  +*/
           19  +#include <stdlib.h>
           20  +
           21  +/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
           22  +#ifdef GEOPOLY_ENABLE_DEBUG
           23  +  static int geo_debug = 0;
           24  +# define GEODEBUG(X) if(geo_debug)printf X
           25  +#else
           26  +# define GEODEBUG(X)
           27  +#endif
           28  +
           29  +#ifndef JSON_NULL   /* The following stuff repeats things found in json1 */
           30  +/*
           31  +** Versions of isspace(), isalnum() and isdigit() to which it is safe
           32  +** to pass signed char values.
           33  +*/
           34  +#ifdef sqlite3Isdigit
           35  +   /* Use the SQLite core versions if this routine is part of the
           36  +   ** SQLite amalgamation */
           37  +#  define safe_isdigit(x)  sqlite3Isdigit(x)
           38  +#  define safe_isalnum(x)  sqlite3Isalnum(x)
           39  +#  define safe_isxdigit(x) sqlite3Isxdigit(x)
           40  +#else
           41  +   /* Use the standard library for separate compilation */
           42  +#include <ctype.h>  /* amalgamator: keep */
           43  +#  define safe_isdigit(x)  isdigit((unsigned char)(x))
           44  +#  define safe_isalnum(x)  isalnum((unsigned char)(x))
           45  +#  define safe_isxdigit(x) isxdigit((unsigned char)(x))
           46  +#endif
           47  +
           48  +/*
           49  +** Growing our own isspace() routine this way is twice as fast as
           50  +** the library isspace() function.
           51  +*/
           52  +static const char geopolyIsSpace[] = {
           53  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 1, 1, 0, 0, 1, 0, 0,
           54  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           55  +  1, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           56  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           57  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           58  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           59  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           60  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           61  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           62  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           63  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           64  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           65  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           66  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           67  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           68  +  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
           69  +};
           70  +#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
           71  +#endif /* JSON NULL - back to original code */
           72  +
           73  +/* Compiler and version */
           74  +#ifndef GCC_VERSION
           75  +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
           76  +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
           77  +#else
           78  +# define GCC_VERSION 0
           79  +#endif
           80  +#endif
           81  +#ifndef MSVC_VERSION
           82  +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
           83  +# define MSVC_VERSION _MSC_VER
           84  +#else
           85  +# define MSVC_VERSION 0
           86  +#endif
           87  +#endif
           88  +
           89  +/* Datatype for coordinates
           90  +*/
           91  +typedef float GeoCoord;
           92  +
           93  +/*
           94  +** Internal representation of a polygon.
           95  +**
           96  +** The polygon consists of a sequence of vertexes.  There is a line
           97  +** segment between each pair of vertexes, and one final segment from
           98  +** the last vertex back to the first.  (This differs from the GeoJSON
           99  +** standard in which the final vertex is a repeat of the first.)
          100  +**
          101  +** The polygon follows the right-hand rule.  The area to the right of
          102  +** each segment is "outside" and the area to the left is "inside".
          103  +**
          104  +** The on-disk representation consists of a 4-byte header followed by
          105  +** the values.  The 4-byte header is:
          106  +**
          107  +**      encoding    (1 byte)   0=big-endian, 1=little-endian
          108  +**      nvertex     (3 bytes)  Number of vertexes as a big-endian integer
          109  +*/
          110  +typedef struct GeoPoly GeoPoly;
          111  +struct GeoPoly {
          112  +  int nVertex;          /* Number of vertexes */
          113  +  unsigned char hdr[4]; /* Header for on-disk representation */
          114  +  GeoCoord a[2];    /* 2*nVertex values. X (longitude) first, then Y */
          115  +};
          116  +
          117  +/*
          118  +** State of a parse of a GeoJSON input.
          119  +*/
          120  +typedef struct GeoParse GeoParse;
          121  +struct GeoParse {
          122  +  const unsigned char *z;   /* Unparsed input */
          123  +  int nVertex;              /* Number of vertexes in a[] */
          124  +  int nAlloc;               /* Space allocated to a[] */
          125  +  int nErr;                 /* Number of errors encountered */
          126  +  GeoCoord *a;          /* Array of vertexes.  From sqlite3_malloc64() */
          127  +};
          128  +
          129  +/* Do a 4-byte byte swap */
          130  +static void geopolySwab32(unsigned char *a){
          131  +  unsigned char t = a[0];
          132  +  a[0] = a[3];
          133  +  a[3] = t;
          134  +  t = a[1];
          135  +  a[1] = a[2];
          136  +  a[2] = t;
          137  +}
          138  +
          139  +/* Skip whitespace.  Return the next non-whitespace character. */
          140  +static char geopolySkipSpace(GeoParse *p){
          141  +  while( safe_isspace(p->z[0]) ) p->z++;
          142  +  return p->z[0];
          143  +}
          144  +
          145  +/* Parse out a number.  Write the value into *pVal if pVal!=0.
          146  +** return non-zero on success and zero if the next token is not a number.
          147  +*/
          148  +static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
          149  +  char c = geopolySkipSpace(p);
          150  +  const unsigned char *z = p->z;
          151  +  int j = 0;
          152  +  int seenDP = 0;
          153  +  int seenE = 0;
          154  +  if( c=='-' ){
          155  +    j = 1;
          156  +    c = z[j];
          157  +  }
          158  +  if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
          159  +  for(;; j++){
          160  +    c = z[j];
          161  +    if( safe_isdigit(c) ) continue;
          162  +    if( c=='.' ){
          163  +      if( z[j-1]=='-' ) return 0;
          164  +      if( seenDP ) return 0;
          165  +      seenDP = 1;
          166  +      continue;
          167  +    }
          168  +    if( c=='e' || c=='E' ){
          169  +      if( z[j-1]<'0' ) return 0;
          170  +      if( seenE ) return -1;
          171  +      seenDP = seenE = 1;
          172  +      c = z[j+1];
          173  +      if( c=='+' || c=='-' ){
          174  +        j++;
          175  +        c = z[j+1];
          176  +      }
          177  +      if( c<'0' || c>'9' ) return 0;
          178  +      continue;
          179  +    }
          180  +    break;
          181  +  }
          182  +  if( z[j-1]<'0' ) return 0;
          183  +  if( pVal ){
          184  +#ifdef SQLITE_AMALGAMATION
          185  +     /* The sqlite3AtoF() routine is much much faster than atof(), if it
          186  +     ** is available */
          187  +     double r;
          188  +     (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
          189  +     *pVal = r;
          190  +#else
          191  +     *pVal = (GeoCoord)atof((const char*)p->z);
          192  +#endif
          193  +  }
          194  +  p->z += j;
          195  +  return 1;
          196  +}
          197  +
          198  +/*
          199  +** If the input is a well-formed JSON array of coordinates with at least
          200  +** four coordinates and where each coordinate is itself a two-value array,
          201  +** then convert the JSON into a GeoPoly object and return a pointer to
          202  +** that object.
          203  +**
          204  +** If any error occurs, return NULL.
          205  +*/
          206  +static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
          207  +  GeoParse s;
          208  +  int rc = SQLITE_OK;
          209  +  memset(&s, 0, sizeof(s));
          210  +  s.z = z;
          211  +  if( geopolySkipSpace(&s)=='[' ){
          212  +    s.z++;
          213  +    while( geopolySkipSpace(&s)=='[' ){
          214  +      int ii = 0;
          215  +      char c;
          216  +      s.z++;
          217  +      if( s.nVertex>=s.nAlloc ){
          218  +        GeoCoord *aNew;
          219  +        s.nAlloc = s.nAlloc*2 + 16;
          220  +        aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
          221  +        if( aNew==0 ){
          222  +          rc = SQLITE_NOMEM;
          223  +          s.nErr++;
          224  +          break;
          225  +        }
          226  +        s.a = aNew;
          227  +      }
          228  +      while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
          229  +        ii++;
          230  +        if( ii==2 ) s.nVertex++;
          231  +        c = geopolySkipSpace(&s);
          232  +        s.z++;
          233  +        if( c==',' ) continue;
          234  +        if( c==']' && ii>=2 ) break;
          235  +        s.nErr++;
          236  +        rc = SQLITE_ERROR;
          237  +        goto parse_json_err;
          238  +      }
          239  +      if( geopolySkipSpace(&s)==',' ){
          240  +        s.z++;
          241  +        continue;
          242  +      }
          243  +      break;
          244  +    }
          245  +    if( geopolySkipSpace(&s)==']'
          246  +     && s.nVertex>=4
          247  +     && s.a[0]==s.a[s.nVertex*2-2]
          248  +     && s.a[1]==s.a[s.nVertex*2-1]
          249  +     && (s.z++, geopolySkipSpace(&s)==0)
          250  +    ){
          251  +      int nByte;
          252  +      GeoPoly *pOut;
          253  +      int x = 1;
          254  +      s.nVertex--;  /* Remove the redundant vertex at the end */
          255  +      nByte = sizeof(GeoPoly) * s.nVertex*2*sizeof(GeoCoord);
          256  +      pOut = sqlite3_malloc64( nByte );
          257  +      x = 1;
          258  +      if( pOut==0 ) goto parse_json_err;
          259  +      pOut->nVertex = s.nVertex;
          260  +      memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
          261  +      pOut->hdr[0] = *(unsigned char*)&x;
          262  +      pOut->hdr[1] = (s.nVertex>>16)&0xff;
          263  +      pOut->hdr[2] = (s.nVertex>>8)&0xff;
          264  +      pOut->hdr[3] = s.nVertex&0xff;
          265  +      sqlite3_free(s.a);
          266  +      if( pRc ) *pRc = SQLITE_OK;
          267  +      return pOut;
          268  +    }else{
          269  +      s.nErr++;
          270  +      rc = SQLITE_ERROR;
          271  +    }
          272  +  }
          273  +parse_json_err:
          274  +  if( pRc ) *pRc = rc;
          275  +  sqlite3_free(s.a);
          276  +  return 0;
          277  +}
          278  +
          279  +/*
          280  +** Given a function parameter, try to interpret it as a polygon, either
          281  +** in the binary format or JSON text.  Compute a GeoPoly object and
          282  +** return a pointer to that object.  Or if the input is not a well-formed
          283  +** polygon, put an error message in sqlite3_context and return NULL.
          284  +*/
          285  +static GeoPoly *geopolyFuncParam(
          286  +  sqlite3_context *pCtx,      /* Context for error messages */
          287  +  sqlite3_value *pVal,        /* The value to decode */
          288  +  int *pRc                    /* Write error here */
          289  +){
          290  +  GeoPoly *p = 0;
          291  +  int nByte;
          292  +  if( sqlite3_value_type(pVal)==SQLITE_BLOB
          293  +   && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
          294  +  ){
          295  +    const unsigned char *a = sqlite3_value_blob(pVal);
          296  +    int nVertex;
          297  +    nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
          298  +    if( (a[0]==0 || a[0]==1)
          299  +     && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
          300  +    ){
          301  +      p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
          302  +      if( p==0 ){
          303  +        if( pRc ) *pRc = SQLITE_NOMEM;
          304  +        if( pCtx ) sqlite3_result_error_nomem(pCtx);
          305  +      }else{
          306  +        int x = 1;
          307  +        p->nVertex = nVertex;
          308  +        memcpy(p->hdr, a, nByte);
          309  +        if( a[0] != *(unsigned char*)&x ){
          310  +          int ii;
          311  +          for(ii=0; ii<nVertex*2; ii++){
          312  +            geopolySwab32((unsigned char*)&p->a[ii]);
          313  +          }
          314  +          p->hdr[0] ^= 1;
          315  +        }
          316  +      }
          317  +    }
          318  +    if( pRc ) *pRc = SQLITE_OK;
          319  +    return p;
          320  +  }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
          321  +    const unsigned char *zJson = sqlite3_value_text(pVal);
          322  +    if( zJson==0 ){
          323  +      if( pRc ) *pRc = SQLITE_NOMEM;
          324  +      return 0;
          325  +    }
          326  +    return geopolyParseJson(zJson, pRc);
          327  +  }else{
          328  +    if( pRc ) *pRc = SQLITE_ERROR;
          329  +    return 0;
          330  +  }
          331  +}
          332  +
          333  +/*
          334  +** Implementation of the geopoly_blob(X) function.
          335  +**
          336  +** If the input is a well-formed Geopoly BLOB or JSON string
          337  +** then return the BLOB representation of the polygon.  Otherwise
          338  +** return NULL.
          339  +*/
          340  +static void geopolyBlobFunc(
          341  +  sqlite3_context *context,
          342  +  int argc,
          343  +  sqlite3_value **argv
          344  +){
          345  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
          346  +  if( p ){
          347  +    sqlite3_result_blob(context, p->hdr, 
          348  +       4+8*p->nVertex, SQLITE_TRANSIENT);
          349  +    sqlite3_free(p);
          350  +  }
          351  +}
          352  +
          353  +/*
          354  +** SQL function:     geopoly_json(X)
          355  +**
          356  +** Interpret X as a polygon and render it as a JSON array
          357  +** of coordinates.  Or, if X is not a valid polygon, return NULL.
          358  +*/
          359  +static void geopolyJsonFunc(
          360  +  sqlite3_context *context,
          361  +  int argc,
          362  +  sqlite3_value **argv
          363  +){
          364  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
          365  +  if( p ){
          366  +    sqlite3 *db = sqlite3_context_db_handle(context);
          367  +    sqlite3_str *x = sqlite3_str_new(db);
          368  +    int i;
          369  +    sqlite3_str_append(x, "[", 1);
          370  +    for(i=0; i<p->nVertex; i++){
          371  +      sqlite3_str_appendf(x, "[%!g,%!g],", p->a[i*2], p->a[i*2+1]);
          372  +    }
          373  +    sqlite3_str_appendf(x, "[%!g,%!g]]", p->a[0], p->a[1]);
          374  +    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
          375  +    sqlite3_free(p);
          376  +  }
          377  +}
          378  +
          379  +/*
          380  +** SQL function:     geopoly_svg(X, ....)
          381  +**
          382  +** Interpret X as a polygon and render it as a SVG <polyline>.
          383  +** Additional arguments are added as attributes to the <polyline>.
          384  +*/
          385  +static void geopolySvgFunc(
          386  +  sqlite3_context *context,
          387  +  int argc,
          388  +  sqlite3_value **argv
          389  +){
          390  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
          391  +  if( p ){
          392  +    sqlite3 *db = sqlite3_context_db_handle(context);
          393  +    sqlite3_str *x = sqlite3_str_new(db);
          394  +    int i;
          395  +    char cSep = '\'';
          396  +    sqlite3_str_appendf(x, "<polyline points=");
          397  +    for(i=0; i<p->nVertex; i++){
          398  +      sqlite3_str_appendf(x, "%c%g,%g", cSep, p->a[i*2], p->a[i*2+1]);
          399  +      cSep = ' ';
          400  +    }
          401  +    sqlite3_str_appendf(x, " %g,%g'", p->a[0], p->a[1]);
          402  +    for(i=1; i<argc; i++){
          403  +      const char *z = (const char*)sqlite3_value_text(argv[i]);
          404  +      if( z && z[0] ){
          405  +        sqlite3_str_appendf(x, " %s", z);
          406  +      }
          407  +    }
          408  +    sqlite3_str_appendf(x, "></polyline>");
          409  +    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
          410  +    sqlite3_free(p);
          411  +  }
          412  +}
          413  +
          414  +/*
          415  +** SQL Function:      geopoly_xform(poly, A, B, C, D, E, F)
          416  +**
          417  +** Transform and/or translate a polygon as follows:
          418  +**
          419  +**      x1 = A*x0 + B*y0 + E
          420  +**      y1 = C*x0 + D*y0 + F
          421  +**
          422  +** For a translation:
          423  +**
          424  +**      geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
          425  +**
          426  +** Rotate by R around the point (0,0):
          427  +**
          428  +**      geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
          429  +*/
          430  +static void geopolyXformFunc(
          431  +  sqlite3_context *context,
          432  +  int argc,
          433  +  sqlite3_value **argv
          434  +){
          435  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
          436  +  double A = sqlite3_value_double(argv[1]);
          437  +  double B = sqlite3_value_double(argv[2]);
          438  +  double C = sqlite3_value_double(argv[3]);
          439  +  double D = sqlite3_value_double(argv[4]);
          440  +  double E = sqlite3_value_double(argv[5]);
          441  +  double F = sqlite3_value_double(argv[6]);
          442  +  GeoCoord x1, y1, x0, y0;
          443  +  int ii;
          444  +  if( p ){
          445  +    for(ii=0; ii<p->nVertex; ii++){
          446  +      x0 = p->a[ii*2];
          447  +      y0 = p->a[ii*2+1];
          448  +      x1 = (GeoCoord)(A*x0 + B*y0 + E);
          449  +      y1 = (GeoCoord)(C*x0 + D*y0 + F);
          450  +      p->a[ii*2] = x1;
          451  +      p->a[ii*2+1] = y1;
          452  +    }
          453  +    sqlite3_result_blob(context, p->hdr, 
          454  +       4+8*p->nVertex, SQLITE_TRANSIENT);
          455  +    sqlite3_free(p);
          456  +  }
          457  +}
          458  +
          459  +/*
          460  +** Implementation of the geopoly_area(X) function.
          461  +**
          462  +** If the input is a well-formed Geopoly BLOB then return the area
          463  +** enclosed by the polygon.  If the polygon circulates clockwise instead
          464  +** of counterclockwise (as it should) then return the negative of the
          465  +** enclosed area.  Otherwise return NULL.
          466  +*/
          467  +static void geopolyAreaFunc(
          468  +  sqlite3_context *context,
          469  +  int argc,
          470  +  sqlite3_value **argv
          471  +){
          472  +  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
          473  +  if( p ){
          474  +    double rArea = 0.0;
          475  +    int ii;
          476  +    for(ii=0; ii<p->nVertex-1; ii++){
          477  +      rArea += (p->a[ii*2] - p->a[ii*2+2])           /* (x0 - x1) */
          478  +                * (p->a[ii*2+1] + p->a[ii*2+3])      /* (y0 + y1) */
          479  +                * 0.5;
          480  +    }
          481  +    rArea += (p->a[ii*2] - p->a[0])                  /* (xN - x0) */
          482  +             * (p->a[ii*2+1] + p->a[1])              /* (yN + y0) */
          483  +             * 0.5;
          484  +    sqlite3_result_double(context, rArea);
          485  +    sqlite3_free(p);
          486  +  }            
          487  +}
          488  +
          489  +#define GEOPOLY_PI 3.1415926535897932385
          490  +
          491  +/* Fast approximation for cosine(X) for X between -0.5*pi and 2*pi
          492  +*/
          493  +static double geopolyCosine(double r){
          494  +  assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
          495  +  if( r>=1.5*GEOPOLY_PI ){
          496  +    r -= 2.0*GEOPOLY_PI;
          497  +  }
          498  +  if( r>=0.5*GEOPOLY_PI ){
          499  +    return -geopolyCosine(r-GEOPOLY_PI);
          500  +  }else{
          501  +    double r2 = r*r;
          502  +    double r3 = r2*r;
          503  +    double r5 = r3*r2;
          504  +    return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
          505  +  }
          506  +}
          507  +
          508  +/*
          509  +** Function:   geopoly_regular(X,Y,R,N)
          510  +**
          511  +** Construct a simple, convex, regular polygon centered at X, Y
          512  +** with circumradius R and with N sides.
          513  +*/
          514  +static void geopolyRegularFunc(
          515  +  sqlite3_context *context,
          516  +  int argc,
          517  +  sqlite3_value **argv
          518  +){
          519  +  double x = sqlite3_value_double(argv[0]);
          520  +  double y = sqlite3_value_double(argv[1]);
          521  +  double r = sqlite3_value_double(argv[2]);
          522  +  int n = sqlite3_value_int(argv[3]);
          523  +  int i;
          524  +  GeoPoly *p;
          525  +
          526  +  if( n<3 || r<=0.0 ) return;
          527  +  if( n>1000 ) n = 1000;
          528  +  p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
          529  +  if( p==0 ){
          530  +    sqlite3_result_error_nomem(context);
          531  +    return;
          532  +  }
          533  +  i = 1;
          534  +  p->hdr[0] = *(unsigned char*)&i;
          535  +  p->hdr[1] = 0;
          536  +  p->hdr[2] = (n>>8)&0xff;
          537  +  p->hdr[3] = n&0xff;
          538  +  for(i=0; i<n; i++){
          539  +    double rAngle = 2.0*GEOPOLY_PI*i/n;
          540  +    p->a[i*2] = x - r*geopolyCosine(rAngle-0.5*GEOPOLY_PI);
          541  +    p->a[i*2+1] = y + r*geopolyCosine(rAngle);
          542  +  }
          543  +  sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
          544  +  sqlite3_free(p);
          545  +}
          546  +
          547  +/*
          548  +** If pPoly is a polygon, compute its bounding box. Then:
          549  +**
          550  +**    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
          551  +**    (2) otherwise, compute a GeoPoly for the bounding box and return the
          552  +**        new GeoPoly
          553  +**
          554  +** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
          555  +** the bounding box in aCoord and return a pointer to that GeoPoly.
          556  +*/
          557  +static GeoPoly *geopolyBBox(
          558  +  sqlite3_context *context,   /* For recording the error */
          559  +  sqlite3_value *pPoly,       /* The polygon */
          560  +  RtreeCoord *aCoord,         /* Results here */
          561  +  int *pRc                    /* Error code here */
          562  +){
          563  +  GeoPoly *pOut = 0;
          564  +  GeoPoly *p;
          565  +  float mnX, mxX, mnY, mxY;
          566  +  if( pPoly==0 && aCoord!=0 ){
          567  +    p = 0;
          568  +    mnX = aCoord[0].f;
          569  +    mxX = aCoord[1].f;
          570  +    mnY = aCoord[2].f;
          571  +    mxY = aCoord[3].f;
          572  +    goto geopolyBboxFill;
          573  +  }else{
          574  +    p = geopolyFuncParam(context, pPoly, pRc);
          575  +  }
          576  +  if( p ){
          577  +    int ii;
          578  +    mnX = mxX = p->a[0];
          579  +    mnY = mxY = p->a[1];
          580  +    for(ii=1; ii<p->nVertex; ii++){
          581  +      double r = p->a[ii*2];
          582  +      if( r<mnX ) mnX = (float)r;
          583  +      else if( r>mxX ) mxX = (float)r;
          584  +      r = p->a[ii*2+1];
          585  +      if( r<mnY ) mnY = (float)r;
          586  +      else if( r>mxY ) mxY = (float)r;
          587  +    }
          588  +    if( pRc ) *pRc = SQLITE_OK;
          589  +    if( aCoord==0 ){
          590  +      geopolyBboxFill:
          591  +      pOut = sqlite3_realloc(p, sizeof(GeoPoly)+sizeof(GeoCoord)*6);
          592  +      if( pOut==0 ){
          593  +        sqlite3_free(p);
          594  +        if( context ) sqlite3_result_error_nomem(context);
          595  +        if( pRc ) *pRc = SQLITE_NOMEM;
          596  +        return 0;
          597  +      }
          598  +      pOut->nVertex = 4;
          599  +      ii = 1;
          600  +      pOut->hdr[0] = *(unsigned char*)&ii;
          601  +      pOut->hdr[1] = 0;
          602  +      pOut->hdr[2] = 0;
          603  +      pOut->hdr[3] = 4;
          604  +      pOut->a[0] = mnX;
          605  +      pOut->a[1] = mnY;
          606  +      pOut->a[2] = mxX;
          607  +      pOut->a[3] = mnY;
          608  +      pOut->a[4] = mxX;
          609  +      pOut->a[5] = mxY;
          610  +      pOut->a[6] = mnX;
          611  +      pOut->a[7] = mxY;
          612  +    }else{
          613  +      sqlite3_free(p);
          614  +      aCoord[0].f = mnX;
          615  +      aCoord[1].f = mxX;
          616  +      aCoord[2].f = mnY;
          617  +      aCoord[3].f = mxY;
          618  +    }
          619  +  }
          620  +  return pOut;
          621  +}
          622  +
          623  +/*
          624  +** Implementation of the geopoly_bbox(X) SQL function.
          625  +*/
          626  +static void geopolyBBoxFunc(
          627  +  sqlite3_context *context,
          628  +  int argc,
          629  +  sqlite3_value **argv
          630  +){
          631  +  GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
          632  +  if( p ){
          633  +    sqlite3_result_blob(context, p->hdr, 
          634  +       4+8*p->nVertex, SQLITE_TRANSIENT);
          635  +    sqlite3_free(p);
          636  +  }
          637  +}
          638  +
          639  +/*
          640  +** State vector for the geopoly_group_bbox() aggregate function.
          641  +*/
          642  +typedef struct GeoBBox GeoBBox;
          643  +struct GeoBBox {
          644  +  int isInit;
          645  +  RtreeCoord a[4];
          646  +};
          647  +
          648  +
          649  +/*
          650  +** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
          651  +*/
          652  +static void geopolyBBoxStep(
          653  +  sqlite3_context *context,
          654  +  int argc,
          655  +  sqlite3_value **argv
          656  +){
          657  +  RtreeCoord a[4];
          658  +  int rc = SQLITE_OK;
          659  +  (void)geopolyBBox(context, argv[0], a, &rc);
          660  +  if( rc==SQLITE_OK ){
          661  +    GeoBBox *pBBox;
          662  +    pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
          663  +    if( pBBox==0 ) return;
          664  +    if( pBBox->isInit==0 ){
          665  +      pBBox->isInit = 1;
          666  +      memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
          667  +    }else{
          668  +      if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
          669  +      if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
          670  +      if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
          671  +      if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
          672  +    }
          673  +  }
          674  +}
          675  +static void geopolyBBoxFinal(
          676  +  sqlite3_context *context
          677  +){
          678  +  GeoPoly *p;
          679  +  GeoBBox *pBBox;
          680  +  pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
          681  +  if( pBBox==0 ) return;
          682  +  p = geopolyBBox(context, 0, pBBox->a, 0);
          683  +  if( p ){
          684  +    sqlite3_result_blob(context, p->hdr, 
          685  +       4+8*p->nVertex, SQLITE_TRANSIENT);
          686  +    sqlite3_free(p);
          687  +  }
          688  +}
          689  +
          690  +
          691  +/*
          692  +** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
          693  +** Returns:
          694  +**
          695  +**    +2  x0,y0 is on the line segement
          696  +**
          697  +**    +1  x0,y0 is beneath line segment
          698  +**
          699  +**    0   x0,y0 is not on or beneath the line segment or the line segment
          700  +**        is vertical and x0,y0 is not on the line segment
          701  +**
          702  +** The left-most coordinate min(x1,x2) is not considered to be part of
          703  +** the line segment for the purposes of this analysis.
          704  +*/
          705  +static int pointBeneathLine(
          706  +  double x0, double y0,
          707  +  double x1, double y1,
          708  +  double x2, double y2
          709  +){
          710  +  double y;
          711  +  if( x0==x1 && y0==y1 ) return 2;
          712  +  if( x1<x2 ){
          713  +    if( x0<=x1 || x0>x2 ) return 0;
          714  +  }else if( x1>x2 ){
          715  +    if( x0<=x2 || x0>x1 ) return 0;
          716  +  }else{
          717  +    /* Vertical line segment */
          718  +    if( x0!=x1 ) return 0;
          719  +    if( y0<y1 && y0<y2 ) return 0;
          720  +    if( y0>y1 && y0>y2 ) return 0;
          721  +    return 2;
          722  +  }
          723  +  y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
          724  +  if( y0==y ) return 2;
          725  +  if( y0<y ) return 1;
          726  +  return 0;
          727  +}
          728  +
          729  +/*
          730  +** SQL function:    geopoly_contains_point(P,X,Y)
          731  +**
          732  +** Return +2 if point X,Y is within polygon P.
          733  +** Return +1 if point X,Y is on the polygon boundary.
          734  +** Return 0 if point X,Y is outside the polygon
          735  +*/
          736  +static void geopolyContainsPointFunc(
          737  +  sqlite3_context *context,
          738  +  int argc,
          739  +  sqlite3_value **argv
          740  +){
          741  +  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
          742  +  double x0 = sqlite3_value_double(argv[1]);
          743  +  double y0 = sqlite3_value_double(argv[2]);
          744  +  int v = 0;
          745  +  int cnt = 0;
          746  +  int ii;
          747  +  if( p1==0 ) return;
          748  +  for(ii=0; ii<p1->nVertex-1; ii++){
          749  +    v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
          750  +                               p1->a[ii*2+2],p1->a[ii*2+3]);
          751  +    if( v==2 ) break;
          752  +    cnt += v;
          753  +  }
          754  +  if( v!=2 ){
          755  +    v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
          756  +                               p1->a[0],p1->a[1]);
          757  +  }
          758  +  if( v==2 ){
          759  +    sqlite3_result_int(context, 1);
          760  +  }else if( ((v+cnt)&1)==0 ){
          761  +    sqlite3_result_int(context, 0);
          762  +  }else{
          763  +    sqlite3_result_int(context, 2);
          764  +  }
          765  +  sqlite3_free(p1);
          766  +}
          767  +
          768  +/* Forward declaration */
          769  +static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
          770  +
          771  +/*
          772  +** SQL function:    geopoly_within(P1,P2)
          773  +**
          774  +** Return +2 if P1 and P2 are the same polygon
          775  +** Return +1 if P2 is contained within P1
          776  +** Return 0 if any part of P2 is on the outside of P1
          777  +**
          778  +*/
          779  +static void geopolyWithinFunc(
          780  +  sqlite3_context *context,
          781  +  int argc,
          782  +  sqlite3_value **argv
          783  +){
          784  +  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
          785  +  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
          786  +  if( p1 && p2 ){
          787  +    int x = geopolyOverlap(p1, p2);
          788  +    if( x<0 ){
          789  +      sqlite3_result_error_nomem(context);
          790  +    }else{
          791  +      sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
          792  +    }
          793  +  }
          794  +  sqlite3_free(p1);
          795  +  sqlite3_free(p2);
          796  +}
          797  +
          798  +/* Objects used by the overlap algorihm. */
          799  +typedef struct GeoEvent GeoEvent;
          800  +typedef struct GeoSegment GeoSegment;
          801  +typedef struct GeoOverlap GeoOverlap;
          802  +struct GeoEvent {
          803  +  double x;              /* X coordinate at which event occurs */
          804  +  int eType;             /* 0 for ADD, 1 for REMOVE */
          805  +  GeoSegment *pSeg;      /* The segment to be added or removed */
          806  +  GeoEvent *pNext;       /* Next event in the sorted list */
          807  +};
          808  +struct GeoSegment {
          809  +  double C, B;           /* y = C*x + B */
          810  +  double y;              /* Current y value */
          811  +  float y0;              /* Initial y value */
          812  +  unsigned char side;    /* 1 for p1, 2 for p2 */
          813  +  unsigned int idx;      /* Which segment within the side */
          814  +  GeoSegment *pNext;     /* Next segment in a list sorted by y */
          815  +};
          816  +struct GeoOverlap {
          817  +  GeoEvent *aEvent;          /* Array of all events */
          818  +  GeoSegment *aSegment;      /* Array of all segments */
          819  +  int nEvent;                /* Number of events */
          820  +  int nSegment;              /* Number of segments */
          821  +};
          822  +
          823  +/*
          824  +** Add a single segment and its associated events.
          825  +*/
          826  +static void geopolyAddOneSegment(
          827  +  GeoOverlap *p,
          828  +  GeoCoord x0,
          829  +  GeoCoord y0,
          830  +  GeoCoord x1,
          831  +  GeoCoord y1,
          832  +  unsigned char side,
          833  +  unsigned int idx
          834  +){
          835  +  GeoSegment *pSeg;
          836  +  GeoEvent *pEvent;
          837  +  if( x0==x1 ) return;  /* Ignore vertical segments */
          838  +  if( x0>x1 ){
          839  +    GeoCoord t = x0;
          840  +    x0 = x1;
          841  +    x1 = t;
          842  +    t = y0;
          843  +    y0 = y1;
          844  +    y1 = t;
          845  +  }
          846  +  pSeg = p->aSegment + p->nSegment;
          847  +  p->nSegment++;
          848  +  pSeg->C = (y1-y0)/(x1-x0);
          849  +  pSeg->B = y1 - x1*pSeg->C;
          850  +  pSeg->y0 = y0;
          851  +  pSeg->side = side;
          852  +  pSeg->idx = idx;
          853  +  pEvent = p->aEvent + p->nEvent;
          854  +  p->nEvent++;
          855  +  pEvent->x = x0;
          856  +  pEvent->eType = 0;
          857  +  pEvent->pSeg = pSeg;
          858  +  pEvent = p->aEvent + p->nEvent;
          859  +  p->nEvent++;
          860  +  pEvent->x = x1;
          861  +  pEvent->eType = 1;
          862  +  pEvent->pSeg = pSeg;
          863  +}
          864  +  
          865  +
          866  +
          867  +/*
          868  +** Insert all segments and events for polygon pPoly.
          869  +*/
          870  +static void geopolyAddSegments(
          871  +  GeoOverlap *p,          /* Add segments to this Overlap object */
          872  +  GeoPoly *pPoly,         /* Take all segments from this polygon */
          873  +  unsigned char side      /* The side of pPoly */
          874  +){
          875  +  unsigned int i;
          876  +  GeoCoord *x;
          877  +  for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
          878  +    x = pPoly->a + (i*2);
          879  +    geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
          880  +  }
          881  +  x = pPoly->a + (i*2);
          882  +  geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
          883  +}
          884  +
          885  +/*
          886  +** Merge two lists of sorted events by X coordinate
          887  +*/
          888  +static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
          889  +  GeoEvent head, *pLast;
          890  +  head.pNext = 0;
          891  +  pLast = &head;
          892  +  while( pRight && pLeft ){
          893  +    if( pRight->x <= pLeft->x ){
          894  +      pLast->pNext = pRight;
          895  +      pLast = pRight;
          896  +      pRight = pRight->pNext;
          897  +    }else{
          898  +      pLast->pNext = pLeft;
          899  +      pLast = pLeft;
          900  +      pLeft = pLeft->pNext;
          901  +    }
          902  +  }
          903  +  pLast->pNext = pRight ? pRight : pLeft;
          904  +  return head.pNext;  
          905  +}
          906  +
          907  +/*
          908  +** Sort an array of nEvent event objects into a list.
          909  +*/
          910  +static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
          911  +  int mx = 0;
          912  +  int i, j;
          913  +  GeoEvent *p;
          914  +  GeoEvent *a[50];
          915  +  for(i=0; i<nEvent; i++){
          916  +    p = &aEvent[i];
          917  +    p->pNext = 0;
          918  +    for(j=0; j<mx && a[j]; j++){
          919  +      p = geopolyEventMerge(a[j], p);
          920  +      a[j] = 0;
          921  +    }
          922  +    a[j] = p;
          923  +    if( j>=mx ) mx = j+1;
          924  +  }
          925  +  p = 0;
          926  +  for(i=0; i<mx; i++){
          927  +    p = geopolyEventMerge(a[i], p);
          928  +  }
          929  +  return p;
          930  +}
          931  +
          932  +/*
          933  +** Merge two lists of sorted segments by Y, and then by C.
          934  +*/
          935  +static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
          936  +  GeoSegment head, *pLast;
          937  +  head.pNext = 0;
          938  +  pLast = &head;
          939  +  while( pRight && pLeft ){
          940  +    double r = pRight->y - pLeft->y;
          941  +    if( r==0.0 ) r = pRight->C - pLeft->C;
          942  +    if( r<0.0 ){
          943  +      pLast->pNext = pRight;
          944  +      pLast = pRight;
          945  +      pRight = pRight->pNext;
          946  +    }else{
          947  +      pLast->pNext = pLeft;
          948  +      pLast = pLeft;
          949  +      pLeft = pLeft->pNext;
          950  +    }
          951  +  }
          952  +  pLast->pNext = pRight ? pRight : pLeft;
          953  +  return head.pNext;  
          954  +}
          955  +
          956  +/*
          957  +** Sort a list of GeoSegments in order of increasing Y and in the event of
          958  +** a tie, increasing C (slope).
          959  +*/
          960  +static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
          961  +  int mx = 0;
          962  +  int i;
          963  +  GeoSegment *p;
          964  +  GeoSegment *a[50];
          965  +  while( pList ){
          966  +    p = pList;
          967  +    pList = pList->pNext;
          968  +    p->pNext = 0;
          969  +    for(i=0; i<mx && a[i]; i++){
          970  +      p = geopolySegmentMerge(a[i], p);
          971  +      a[i] = 0;
          972  +    }
          973  +    a[i] = p;
          974  +    if( i>=mx ) mx = i+1;
          975  +  }
          976  +  p = 0;
          977  +  for(i=0; i<mx; i++){
          978  +    p = geopolySegmentMerge(a[i], p);
          979  +  }
          980  +  return p;
          981  +}
          982  +
          983  +/*
          984  +** Determine the overlap between two polygons
          985  +*/
          986  +static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
          987  +  int nVertex = p1->nVertex + p2->nVertex + 2;
          988  +  GeoOverlap *p;
          989  +  int nByte;
          990  +  GeoEvent *pThisEvent;
          991  +  double rX;
          992  +  int rc = 0;
          993  +  int needSort = 0;
          994  +  GeoSegment *pActive = 0;
          995  +  GeoSegment *pSeg;
          996  +  unsigned char aOverlap[4];
          997  +
          998  +  nByte = sizeof(GeoEvent)*nVertex*2 
          999  +           + sizeof(GeoSegment)*nVertex 
         1000  +           + sizeof(GeoOverlap);
         1001  +  p = sqlite3_malloc( nByte );
         1002  +  if( p==0 ) return -1;
         1003  +  p->aEvent = (GeoEvent*)&p[1];
         1004  +  p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
         1005  +  p->nEvent = p->nSegment = 0;
         1006  +  geopolyAddSegments(p, p1, 1);
         1007  +  geopolyAddSegments(p, p2, 2);
         1008  +  pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
         1009  +  rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
         1010  +  memset(aOverlap, 0, sizeof(aOverlap));
         1011  +  while( pThisEvent ){
         1012  +    if( pThisEvent->x!=rX ){
         1013  +      GeoSegment *pPrev = 0;
         1014  +      int iMask = 0;
         1015  +      GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
         1016  +      rX = pThisEvent->x;
         1017  +      if( needSort ){
         1018  +        GEODEBUG(("SORT\n"));
         1019  +        pActive = geopolySortSegmentsByYAndC(pActive);
         1020  +        needSort = 0;
         1021  +      }
         1022  +      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
         1023  +        if( pPrev ){
         1024  +          if( pPrev->y!=pSeg->y ){
         1025  +            GEODEBUG(("MASK: %d\n", iMask));
         1026  +            aOverlap[iMask] = 1;
         1027  +          }
         1028  +        }
         1029  +        iMask ^= pSeg->side;
         1030  +        pPrev = pSeg;
         1031  +      }
         1032  +      pPrev = 0;
         1033  +      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
         1034  +        double y = pSeg->C*rX + pSeg->B;
         1035  +        GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
         1036  +        pSeg->y = y;
         1037  +        if( pPrev ){
         1038  +          if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
         1039  +            rc = 1;
         1040  +            GEODEBUG(("Crossing: %d.%d and %d.%d\n",
         1041  +                    pPrev->side, pPrev->idx,
         1042  +                    pSeg->side, pSeg->idx));
         1043  +            goto geopolyOverlapDone;
         1044  +          }else if( pPrev->y!=pSeg->y ){
         1045  +            GEODEBUG(("MASK: %d\n", iMask));
         1046  +            aOverlap[iMask] = 1;
         1047  +          }
         1048  +        }
         1049  +        iMask ^= pSeg->side;
         1050  +        pPrev = pSeg;
         1051  +      }
         1052  +    }
         1053  +    GEODEBUG(("%s %d.%d C=%g B=%g\n",
         1054  +      pThisEvent->eType ? "RM " : "ADD",
         1055  +      pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
         1056  +      pThisEvent->pSeg->C,
         1057  +      pThisEvent->pSeg->B));
         1058  +    if( pThisEvent->eType==0 ){
         1059  +      /* Add a segment */
         1060  +      pSeg = pThisEvent->pSeg;
         1061  +      pSeg->y = pSeg->y0;
         1062  +      pSeg->pNext = pActive;
         1063  +      pActive = pSeg;
         1064  +      needSort = 1;
         1065  +    }else{
         1066  +      /* Remove a segment */
         1067  +      if( pActive==pThisEvent->pSeg ){
         1068  +        pActive = pActive->pNext;
         1069  +      }else{
         1070  +        for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
         1071  +          if( pSeg->pNext==pThisEvent->pSeg ){
         1072  +            pSeg->pNext = pSeg->pNext->pNext;
         1073  +            break;
         1074  +          }
         1075  +        }
         1076  +      }
         1077  +    }
         1078  +    pThisEvent = pThisEvent->pNext;
         1079  +  }
         1080  +  if( aOverlap[3]==0 ){
         1081  +    rc = 0;
         1082  +  }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
         1083  +    rc = 3;
         1084  +  }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
         1085  +    rc = 2;
         1086  +  }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
         1087  +    rc = 4;
         1088  +  }else{
         1089  +    rc = 1;
         1090  +  }
         1091  +
         1092  +geopolyOverlapDone:
         1093  +  sqlite3_free(p);
         1094  +  return rc;
         1095  +}
         1096  +
         1097  +/*
         1098  +** SQL function:    geopoly_overlap(P1,P2)
         1099  +**
         1100  +** Determine whether or not P1 and P2 overlap. Return value:
         1101  +**
         1102  +**   0     The two polygons are disjoint
         1103  +**   1     They overlap
         1104  +**   2     P1 is completely contained within P2
         1105  +**   3     P2 is completely contained within P1
         1106  +**   4     P1 and P2 are the same polygon
         1107  +**   NULL  Either P1 or P2 or both are not valid polygons
         1108  +*/
         1109  +static void geopolyOverlapFunc(
         1110  +  sqlite3_context *context,
         1111  +  int argc,
         1112  +  sqlite3_value **argv
         1113  +){
         1114  +  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
         1115  +  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
         1116  +  if( p1 && p2 ){
         1117  +    int x = geopolyOverlap(p1, p2);
         1118  +    if( x<0 ){
         1119  +      sqlite3_result_error_nomem(context);
         1120  +    }else{
         1121  +      sqlite3_result_int(context, x);
         1122  +    }
         1123  +  }
         1124  +  sqlite3_free(p1);
         1125  +  sqlite3_free(p2);
         1126  +}
         1127  +
         1128  +/*
         1129  +** Enable or disable debugging output
         1130  +*/
         1131  +static void geopolyDebugFunc(
         1132  +  sqlite3_context *context,
         1133  +  int argc,
         1134  +  sqlite3_value **argv
         1135  +){
         1136  +#ifdef GEOPOLY_ENABLE_DEBUG
         1137  +  geo_debug = sqlite3_value_int(argv[0]);
         1138  +#endif
         1139  +}
         1140  +
         1141  +/* 
         1142  +** This function is the implementation of both the xConnect and xCreate
         1143  +** methods of the geopoly virtual table.
         1144  +**
         1145  +**   argv[0]   -> module name
         1146  +**   argv[1]   -> database name
         1147  +**   argv[2]   -> table name
         1148  +**   argv[...] -> column names...
         1149  +*/
         1150  +static int geopolyInit(
         1151  +  sqlite3 *db,                        /* Database connection */
         1152  +  void *pAux,                         /* One of the RTREE_COORD_* constants */
         1153  +  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
         1154  +  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
         1155  +  char **pzErr,                       /* OUT: Error message, if any */
         1156  +  int isCreate                        /* True for xCreate, false for xConnect */
         1157  +){
         1158  +  int rc = SQLITE_OK;
         1159  +  Rtree *pRtree;
         1160  +  int nDb;              /* Length of string argv[1] */
         1161  +  int nName;            /* Length of string argv[2] */
         1162  +  sqlite3_str *pSql;
         1163  +  char *zSql;
         1164  +  int ii;
         1165  +
         1166  +  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
         1167  +
         1168  +  /* Allocate the sqlite3_vtab structure */
         1169  +  nDb = (int)strlen(argv[1]);
         1170  +  nName = (int)strlen(argv[2]);
         1171  +  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
         1172  +  if( !pRtree ){
         1173  +    return SQLITE_NOMEM;
         1174  +  }
         1175  +  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
         1176  +  pRtree->nBusy = 1;
         1177  +  pRtree->base.pModule = &rtreeModule;
         1178  +  pRtree->zDb = (char *)&pRtree[1];
         1179  +  pRtree->zName = &pRtree->zDb[nDb+1];
         1180  +  pRtree->eCoordType = RTREE_COORD_REAL32;
         1181  +  pRtree->nDim = 2;
         1182  +  pRtree->nDim2 = 4;
         1183  +  memcpy(pRtree->zDb, argv[1], nDb);
         1184  +  memcpy(pRtree->zName, argv[2], nName);
         1185  +
         1186  +
         1187  +  /* Create/Connect to the underlying relational database schema. If
         1188  +  ** that is successful, call sqlite3_declare_vtab() to configure
         1189  +  ** the r-tree table schema.
         1190  +  */
         1191  +  pSql = sqlite3_str_new(db);
         1192  +  sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
         1193  +  pRtree->nAux = 1;         /* Add one for _shape */
         1194  +  pRtree->nAuxNotNull = 1;  /* The _shape column is always not-null */
         1195  +  for(ii=3; ii<argc; ii++){
         1196  +    pRtree->nAux++;
         1197  +    sqlite3_str_appendf(pSql, ",%s", argv[ii]);
         1198  +  }
         1199  +  sqlite3_str_appendf(pSql, ");");
         1200  +  zSql = sqlite3_str_finish(pSql);
         1201  +  if( !zSql ){
         1202  +    rc = SQLITE_NOMEM;
         1203  +  }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
         1204  +    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
         1205  +  }
         1206  +  sqlite3_free(zSql);
         1207  +  if( rc ) goto geopolyInit_fail;
         1208  +  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
         1209  +
         1210  +  /* Figure out the node size to use. */
         1211  +  rc = getNodeSize(db, pRtree, isCreate, pzErr);
         1212  +  if( rc ) goto geopolyInit_fail;
         1213  +  rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
         1214  +  if( rc ){
         1215  +    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
         1216  +    goto geopolyInit_fail;
         1217  +  }
         1218  +
         1219  +  *ppVtab = (sqlite3_vtab *)pRtree;
         1220  +  return SQLITE_OK;
         1221  +
         1222  +geopolyInit_fail:
         1223  +  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
         1224  +  assert( *ppVtab==0 );
         1225  +  assert( pRtree->nBusy==1 );
         1226  +  rtreeRelease(pRtree);
         1227  +  return rc;
         1228  +}
         1229  +
         1230  +
         1231  +/* 
         1232  +** GEOPOLY virtual table module xCreate method.
         1233  +*/
         1234  +static int geopolyCreate(
         1235  +  sqlite3 *db,
         1236  +  void *pAux,
         1237  +  int argc, const char *const*argv,
         1238  +  sqlite3_vtab **ppVtab,
         1239  +  char **pzErr
         1240  +){
         1241  +  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
         1242  +}
         1243  +
         1244  +/* 
         1245  +** GEOPOLY virtual table module xConnect method.
         1246  +*/
         1247  +static int geopolyConnect(
         1248  +  sqlite3 *db,
         1249  +  void *pAux,
         1250  +  int argc, const char *const*argv,
         1251  +  sqlite3_vtab **ppVtab,
         1252  +  char **pzErr
         1253  +){
         1254  +  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
         1255  +}
         1256  +
         1257  +
         1258  +/* 
         1259  +** GEOPOLY virtual table module xFilter method.
         1260  +**
         1261  +** Query plans:
         1262  +**
         1263  +**      1         rowid lookup
         1264  +**      2         search for objects overlapping the same bounding box
         1265  +**                that contains polygon argv[0]
         1266  +**      3         search for objects overlapping the same bounding box
         1267  +**                that contains polygon argv[0]
         1268  +**      4         full table scan
         1269  +*/
         1270  +static int geopolyFilter(
         1271  +  sqlite3_vtab_cursor *pVtabCursor,     /* The cursor to initialize */
         1272  +  int idxNum,                           /* Query plan */
         1273  +  const char *idxStr,                   /* Not Used */
         1274  +  int argc, sqlite3_value **argv        /* Parameters to the query plan */
         1275  +){
         1276  +  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
         1277  +  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
         1278  +  RtreeNode *pRoot = 0;
         1279  +  int rc = SQLITE_OK;
         1280  +  int iCell = 0;
         1281  +  sqlite3_stmt *pStmt;
         1282  +
         1283  +  rtreeReference(pRtree);
         1284  +
         1285  +  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
         1286  +  freeCursorConstraints(pCsr);
         1287  +  sqlite3_free(pCsr->aPoint);
         1288  +  pStmt = pCsr->pReadAux;
         1289  +  memset(pCsr, 0, sizeof(RtreeCursor));
         1290  +  pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
         1291  +  pCsr->pReadAux = pStmt;
         1292  +
         1293  +  pCsr->iStrategy = idxNum;
         1294  +  if( idxNum==1 ){
         1295  +    /* Special case - lookup by rowid. */
         1296  +    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
         1297  +    RtreeSearchPoint *p;     /* Search point for the leaf */
         1298  +    i64 iRowid = sqlite3_value_int64(argv[0]);
         1299  +    i64 iNode = 0;
         1300  +    rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
         1301  +    if( rc==SQLITE_OK && pLeaf!=0 ){
         1302  +      p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
         1303  +      assert( p!=0 );  /* Always returns pCsr->sPoint */
         1304  +      pCsr->aNode[0] = pLeaf;
         1305  +      p->id = iNode;
         1306  +      p->eWithin = PARTLY_WITHIN;
         1307  +      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
         1308  +      p->iCell = (u8)iCell;
         1309  +      RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
         1310  +    }else{
         1311  +      pCsr->atEOF = 1;
         1312  +    }
         1313  +  }else{
         1314  +    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
         1315  +    ** with the configured constraints. 
         1316  +    */
         1317  +    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
         1318  +    if( rc==SQLITE_OK && idxNum<=3 ){
         1319  +      RtreeCoord bbox[4];
         1320  +      RtreeConstraint *p;
         1321  +      assert( argc==1 );
         1322  +      geopolyBBox(0, argv[0], bbox, &rc);
         1323  +      if( rc ){
         1324  +        goto geopoly_filter_end;
         1325  +      }
         1326  +      pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
         1327  +      pCsr->nConstraint = 4;
         1328  +      if( p==0 ){
         1329  +        rc = SQLITE_NOMEM;
         1330  +      }else{
         1331  +        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
         1332  +        memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
         1333  +        if( idxNum==2 ){
         1334  +          /* Overlap query */
         1335  +          p->op = 'B';
         1336  +          p->iCoord = 0;
         1337  +          p->u.rValue = bbox[1].f;
         1338  +          p++;
         1339  +          p->op = 'D';
         1340  +          p->iCoord = 1;
         1341  +          p->u.rValue = bbox[0].f;
         1342  +          p++;
         1343  +          p->op = 'B';
         1344  +          p->iCoord = 2;
         1345  +          p->u.rValue = bbox[3].f;
         1346  +          p++;
         1347  +          p->op = 'D';
         1348  +          p->iCoord = 3;
         1349  +          p->u.rValue = bbox[2].f;
         1350  +        }else{
         1351  +          /* Within query */
         1352  +          p->op = 'D';
         1353  +          p->iCoord = 0;
         1354  +          p->u.rValue = bbox[0].f;
         1355  +          p++;
         1356  +          p->op = 'B';
         1357  +          p->iCoord = 1;
         1358  +          p->u.rValue = bbox[1].f;
         1359  +          p++;
         1360  +          p->op = 'D';
         1361  +          p->iCoord = 2;
         1362  +          p->u.rValue = bbox[2].f;
         1363  +          p++;
         1364  +          p->op = 'B';
         1365  +          p->iCoord = 3;
         1366  +          p->u.rValue = bbox[3].f;
         1367  +        }
         1368  +      }
         1369  +    }
         1370  +    if( rc==SQLITE_OK ){
         1371  +      RtreeSearchPoint *pNew;
         1372  +      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
         1373  +      if( pNew==0 ){
         1374  +        rc = SQLITE_NOMEM;
         1375  +        goto geopoly_filter_end;
         1376  +      }
         1377  +      pNew->id = 1;
         1378  +      pNew->iCell = 0;
         1379  +      pNew->eWithin = PARTLY_WITHIN;
         1380  +      assert( pCsr->bPoint==1 );
         1381  +      pCsr->aNode[0] = pRoot;
         1382  +      pRoot = 0;
         1383  +      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
         1384  +      rc = rtreeStepToLeaf(pCsr);
         1385  +    }
         1386  +  }
         1387  +
         1388  +geopoly_filter_end:
         1389  +  nodeRelease(pRtree, pRoot);
         1390  +  rtreeRelease(pRtree);
         1391  +  return rc;
         1392  +}
         1393  +
         1394  +/*
         1395  +** Rtree virtual table module xBestIndex method. There are three
         1396  +** table scan strategies to choose from (in order from most to 
         1397  +** least desirable):
         1398  +**
         1399  +**   idxNum     idxStr        Strategy
         1400  +**   ------------------------------------------------
         1401  +**     1        "rowid"       Direct lookup by rowid.
         1402  +**     2        "rtree"       R-tree overlap query using geopoly_overlap()
         1403  +**     3        "rtree"       R-tree within query using geopoly_within()
         1404  +**     4        "fullscan"    full-table scan.
         1405  +**   ------------------------------------------------
         1406  +*/
         1407  +static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
         1408  +  int ii;
         1409  +  int iRowidTerm = -1;
         1410  +  int iFuncTerm = -1;
         1411  +  int idxNum = 0;
         1412  +
         1413  +  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
         1414  +    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
         1415  +    if( !p->usable ) continue;
         1416  +    if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ  ){
         1417  +      iRowidTerm = ii;
         1418  +      break;
         1419  +    }
         1420  +    if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
         1421  +      /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
         1422  +      ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
         1423  +      ** See geopolyFindFunction() */
         1424  +      iFuncTerm = ii;
         1425  +      idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
         1426  +    }
         1427  +  }
         1428  +
         1429  +  if( iRowidTerm>=0 ){
         1430  +    pIdxInfo->idxNum = 1;
         1431  +    pIdxInfo->idxStr = "rowid";
         1432  +    pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
         1433  +    pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
         1434  +    pIdxInfo->estimatedCost = 30.0;
         1435  +    pIdxInfo->estimatedRows = 1;
         1436  +    pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
         1437  +    return SQLITE_OK;
         1438  +  }
         1439  +  if( iFuncTerm>=0 ){
         1440  +    pIdxInfo->idxNum = idxNum;
         1441  +    pIdxInfo->idxStr = "rtree";
         1442  +    pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
         1443  +    pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
         1444  +    pIdxInfo->estimatedCost = 300.0;
         1445  +    pIdxInfo->estimatedRows = 10;
         1446  +    return SQLITE_OK;
         1447  +  }
         1448  +  pIdxInfo->idxNum = 4;
         1449  +  pIdxInfo->idxStr = "fullscan";
         1450  +  pIdxInfo->estimatedCost = 3000000.0;
         1451  +  pIdxInfo->estimatedRows = 100000;
         1452  +  return SQLITE_OK;
         1453  +}
         1454  +
         1455  +
         1456  +/* 
         1457  +** GEOPOLY virtual table module xColumn method.
         1458  +*/
         1459  +static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
         1460  +  Rtree *pRtree = (Rtree *)cur->pVtab;
         1461  +  RtreeCursor *pCsr = (RtreeCursor *)cur;
         1462  +  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
         1463  +  int rc = SQLITE_OK;
         1464  +  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
         1465  +
         1466  +  if( rc ) return rc;
         1467  +  if( p==0 ) return SQLITE_OK;
         1468  +  if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
         1469  +  if( i<=pRtree->nAux ){
         1470  +    if( !pCsr->bAuxValid ){
         1471  +      if( pCsr->pReadAux==0 ){
         1472  +        rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
         1473  +                                &pCsr->pReadAux, 0);
         1474  +        if( rc ) return rc;
         1475  +      }
         1476  +      sqlite3_bind_int64(pCsr->pReadAux, 1, 
         1477  +          nodeGetRowid(pRtree, pNode, p->iCell));
         1478  +      rc = sqlite3_step(pCsr->pReadAux);
         1479  +      if( rc==SQLITE_ROW ){
         1480  +        pCsr->bAuxValid = 1;
         1481  +      }else{
         1482  +        sqlite3_reset(pCsr->pReadAux);
         1483  +        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
         1484  +        return rc;
         1485  +      }
         1486  +    }
         1487  +    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
         1488  +  }
         1489  +  return SQLITE_OK;
         1490  +}
         1491  +
         1492  +
         1493  +/*
         1494  +** The xUpdate method for GEOPOLY module virtual tables.
         1495  +**
         1496  +** For DELETE:
         1497  +**
         1498  +**     argv[0] = the rowid to be deleted
         1499  +**
         1500  +** For INSERT:
         1501  +**
         1502  +**     argv[0] = SQL NULL
         1503  +**     argv[1] = rowid to insert, or an SQL NULL to select automatically
         1504  +**     argv[2] = _shape column
         1505  +**     argv[3] = first application-defined column....
         1506  +**
         1507  +** For UPDATE:
         1508  +**
         1509  +**     argv[0] = rowid to modify.  Never NULL
         1510  +**     argv[1] = rowid after the change.  Never NULL
         1511  +**     argv[2] = new value for _shape
         1512  +**     argv[3] = new value for first application-defined column....
         1513  +*/
         1514  +static int geopolyUpdate(
         1515  +  sqlite3_vtab *pVtab, 
         1516  +  int nData, 
         1517  +  sqlite3_value **aData, 
         1518  +  sqlite_int64 *pRowid
         1519  +){
         1520  +  Rtree *pRtree = (Rtree *)pVtab;
         1521  +  int rc = SQLITE_OK;
         1522  +  RtreeCell cell;                 /* New cell to insert if nData>1 */
         1523  +  i64 oldRowid;                   /* The old rowid */
         1524  +  int oldRowidValid;              /* True if oldRowid is valid */
         1525  +  i64 newRowid;                   /* The new rowid */
         1526  +  int newRowidValid;              /* True if newRowid is valid */
         1527  +  int coordChange = 0;            /* Change in coordinates */
         1528  +
         1529  +  if( pRtree->nNodeRef ){
         1530  +    /* Unable to write to the btree while another cursor is reading from it,
         1531  +    ** since the write might do a rebalance which would disrupt the read
         1532  +    ** cursor. */
         1533  +    return SQLITE_LOCKED_VTAB;
         1534  +  }
         1535  +  rtreeReference(pRtree);
         1536  +  assert(nData>=1);
         1537  +
         1538  +  oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
         1539  +  oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
         1540  +  newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
         1541  +  newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
         1542  +  cell.iRowid = newRowid;
         1543  +
         1544  +  if( nData>1                                 /* not a DELETE */
         1545  +   && (!oldRowidValid                         /* INSERT */
         1546  +        || !sqlite3_value_nochange(aData[2])  /* UPDATE _shape */
         1547  +        || oldRowid!=newRowid)                /* Rowid change */
         1548  +  ){
         1549  +    geopolyBBox(0, aData[2], cell.aCoord, &rc);
         1550  +    if( rc ){
         1551  +      if( rc==SQLITE_ERROR ){
         1552  +        pVtab->zErrMsg =
         1553  +          sqlite3_mprintf("_shape does not contain a valid polygon");
         1554  +      }
         1555  +      goto geopoly_update_end;
         1556  +    }
         1557  +    coordChange = 1;
         1558  +
         1559  +    /* If a rowid value was supplied, check if it is already present in 
         1560  +    ** the table. If so, the constraint has failed. */
         1561  +    if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
         1562  +      int steprc;
         1563  +      sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
         1564  +      steprc = sqlite3_step(pRtree->pReadRowid);
         1565  +      rc = sqlite3_reset(pRtree->pReadRowid);
         1566  +      if( SQLITE_ROW==steprc ){
         1567  +        if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
         1568  +          rc = rtreeDeleteRowid(pRtree, cell.iRowid);
         1569  +        }else{
         1570  +          rc = rtreeConstraintError(pRtree, 0);
         1571  +        }
         1572  +      }
         1573  +    }
         1574  +  }
         1575  +
         1576  +  /* If aData[0] is not an SQL NULL value, it is the rowid of a
         1577  +  ** record to delete from the r-tree table. The following block does
         1578  +  ** just that.
         1579  +  */
         1580  +  if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
         1581  +    rc = rtreeDeleteRowid(pRtree, oldRowid);
         1582  +  }
         1583  +
         1584  +  /* If the aData[] array contains more than one element, elements
         1585  +  ** (aData[2]..aData[argc-1]) contain a new record to insert into
         1586  +  ** the r-tree structure.
         1587  +  */
         1588  +  if( rc==SQLITE_OK && nData>1 && coordChange ){
         1589  +    /* Insert the new record into the r-tree */
         1590  +    RtreeNode *pLeaf = 0;
         1591  +    if( !newRowidValid ){
         1592  +      rc = rtreeNewRowid(pRtree, &cell.iRowid);
         1593  +    }
         1594  +    *pRowid = cell.iRowid;
         1595  +    if( rc==SQLITE_OK ){
         1596  +      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
         1597  +    }
         1598  +    if( rc==SQLITE_OK ){
         1599  +      int rc2;
         1600  +      pRtree->iReinsertHeight = -1;
         1601  +      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
         1602  +      rc2 = nodeRelease(pRtree, pLeaf);
         1603  +      if( rc==SQLITE_OK ){
         1604  +        rc = rc2;
         1605  +      }
         1606  +    }
         1607  +  }
         1608  +
         1609  +  /* Change the data */
         1610  +  if( rc==SQLITE_OK && nData>1 ){
         1611  +    sqlite3_stmt *pUp = pRtree->pWriteAux;
         1612  +    int jj;
         1613  +    int nChange = 0;
         1614  +    sqlite3_bind_int64(pUp, 1, cell.iRowid);
         1615  +    assert( pRtree->nAux>=1 );
         1616  +    if( sqlite3_value_nochange(aData[2]) ){
         1617  +      sqlite3_bind_null(pUp, 2);
         1618  +    }else{
         1619  +      GeoPoly *p = 0;
         1620  +      if( sqlite3_value_type(aData[2])==SQLITE_TEXT
         1621  +       && (p = geopolyFuncParam(0, aData[2], &rc))!=0
         1622  +       && rc==SQLITE_OK
         1623  +      ){
         1624  +        sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
         1625  +      }else{
         1626  +        sqlite3_bind_value(pUp, 2, aData[2]);
         1627  +      }
         1628  +      sqlite3_free(p);
         1629  +      nChange = 1;
         1630  +    }
         1631  +    for(jj=1; jj<pRtree->nAux; jj++){
         1632  +      nChange++;
         1633  +      sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
         1634  +    }
         1635  +    if( nChange ){
         1636  +      sqlite3_step(pUp);
         1637  +      rc = sqlite3_reset(pUp);
         1638  +    }
         1639  +  }
         1640  +
         1641  +geopoly_update_end:
         1642  +  rtreeRelease(pRtree);
         1643  +  return rc;
         1644  +}
         1645  +
         1646  +/*
         1647  +** Report that geopoly_overlap() is an overloaded function suitable
         1648  +** for use in xBestIndex.
         1649  +*/
         1650  +static int geopolyFindFunction(
         1651  +  sqlite3_vtab *pVtab,
         1652  +  int nArg,
         1653  +  const char *zName,
         1654  +  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
         1655  +  void **ppArg
         1656  +){
         1657  +  if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
         1658  +    *pxFunc = geopolyOverlapFunc;
         1659  +    *ppArg = 0;
         1660  +    return SQLITE_INDEX_CONSTRAINT_FUNCTION;
         1661  +  }
         1662  +  if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
         1663  +    *pxFunc = geopolyWithinFunc;
         1664  +    *ppArg = 0;
         1665  +    return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
         1666  +  }
         1667  +  return 0;
         1668  +}
         1669  +
         1670  +
         1671  +static sqlite3_module geopolyModule = {
         1672  +  2,                          /* iVersion */
         1673  +  geopolyCreate,              /* xCreate - create a table */
         1674  +  geopolyConnect,             /* xConnect - connect to an existing table */
         1675  +  geopolyBestIndex,           /* xBestIndex - Determine search strategy */
         1676  +  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
         1677  +  rtreeDestroy,               /* xDestroy - Drop a table */
         1678  +  rtreeOpen,                  /* xOpen - open a cursor */
         1679  +  rtreeClose,                 /* xClose - close a cursor */
         1680  +  geopolyFilter,              /* xFilter - configure scan constraints */
         1681  +  rtreeNext,                  /* xNext - advance a cursor */
         1682  +  rtreeEof,                   /* xEof */
         1683  +  geopolyColumn,              /* xColumn - read data */
         1684  +  rtreeRowid,                 /* xRowid - read data */
         1685  +  geopolyUpdate,              /* xUpdate - write data */
         1686  +  rtreeBeginTransaction,      /* xBegin - begin transaction */
         1687  +  rtreeEndTransaction,        /* xSync - sync transaction */
         1688  +  rtreeEndTransaction,        /* xCommit - commit transaction */
         1689  +  rtreeEndTransaction,        /* xRollback - rollback transaction */
         1690  +  geopolyFindFunction,        /* xFindFunction - function overloading */
         1691  +  rtreeRename,                /* xRename - rename the table */
         1692  +  rtreeSavepoint,             /* xSavepoint */
         1693  +  0,                          /* xRelease */
         1694  +  0,                          /* xRollbackTo */
         1695  +};
         1696  +
         1697  +static int sqlite3_geopoly_init(sqlite3 *db){
         1698  +  int rc = SQLITE_OK;
         1699  +  static const struct {
         1700  +    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
         1701  +    signed char nArg;
         1702  +    unsigned char bPure;
         1703  +    const char *zName;
         1704  +  } aFunc[] = {
         1705  +     { geopolyAreaFunc,          1, 1,    "geopoly_area"             },
         1706  +     { geopolyBlobFunc,          1, 1,    "geopoly_blob"             },
         1707  +     { geopolyJsonFunc,          1, 1,    "geopoly_json"             },
         1708  +     { geopolySvgFunc,          -1, 1,    "geopoly_svg"              },
         1709  +     { geopolyWithinFunc,        2, 1,    "geopoly_within"           },
         1710  +     { geopolyContainsPointFunc, 3, 1,    "geopoly_contains_point"   },
         1711  +     { geopolyOverlapFunc,       2, 1,    "geopoly_overlap"          },
         1712  +     { geopolyDebugFunc,         1, 0,    "geopoly_debug"            },
         1713  +     { geopolyBBoxFunc,          1, 1,    "geopoly_bbox"             },
         1714  +     { geopolyXformFunc,         7, 1,    "geopoly_xform"            },
         1715  +     { geopolyRegularFunc,       4, 1,    "geopoly_regular"          },
         1716  +  };
         1717  +  static const struct {
         1718  +    void (*xStep)(sqlite3_context*,int,sqlite3_value**);
         1719  +    void (*xFinal)(sqlite3_context*);
         1720  +    const char *zName;
         1721  +  } aAgg[] = {
         1722  +     { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox"    },
         1723  +  };
         1724  +  int i;
         1725  +  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
         1726  +    int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8;
         1727  +    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
         1728  +                                 enc, 0,
         1729  +                                 aFunc[i].xFunc, 0, 0);
         1730  +  }
         1731  +  for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
         1732  +    rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
         1733  +                                 0, aAgg[i].xStep, aAgg[i].xFinal);
         1734  +  }
         1735  +  if( rc==SQLITE_OK ){
         1736  +    rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
         1737  +  }
         1738  +  return rc;
         1739  +}

Changes to ext/rtree/rtree.c.

   123    123     int iNodeSize;              /* Size in bytes of each node in the node table */
   124    124     u8 nDim;                    /* Number of dimensions */
   125    125     u8 nDim2;                   /* Twice the number of dimensions */
   126    126     u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
   127    127     u8 nBytesPerCell;           /* Bytes consumed per cell */
   128    128     u8 inWrTrans;               /* True if inside write transaction */
   129    129     u8 nAux;                    /* # of auxiliary columns in %_rowid */
          130  +  u8 nAuxNotNull;             /* Number of initial not-null aux columns */
   130    131     int iDepth;                 /* Current depth of the r-tree structure */
   131    132     char *zDb;                  /* Name of database containing r-tree table */
   132    133     char *zName;                /* Name of r-tree table */ 
   133    134     u32 nBusy;                  /* Current number of users of this structure */
   134    135     i64 nRowEst;                /* Estimated number of rows in this table */
   135    136     u32 nCursor;                /* Number of open cursors */
   136    137     u32 nNodeRef;               /* Number RtreeNodes with positive nRef */
................................................................................
  2889   2890     }
  2890   2891     return rc;
  2891   2892   }
  2892   2893   
  2893   2894   /*
  2894   2895   ** Select a currently unused rowid for a new r-tree record.
  2895   2896   */
  2896         -static int newRowid(Rtree *pRtree, i64 *piRowid){
         2897  +static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
  2897   2898     int rc;
  2898   2899     sqlite3_bind_null(pRtree->pWriteRowid, 1);
  2899   2900     sqlite3_bind_null(pRtree->pWriteRowid, 2);
  2900   2901     sqlite3_step(pRtree->pWriteRowid);
  2901   2902     rc = sqlite3_reset(pRtree->pWriteRowid);
  2902   2903     *piRowid = sqlite3_last_insert_rowid(pRtree->db);
  2903   2904     return rc;
................................................................................
  3176   3177     */
  3177   3178     if( rc==SQLITE_OK && nData>1 ){
  3178   3179       /* Insert the new record into the r-tree */
  3179   3180       RtreeNode *pLeaf = 0;
  3180   3181   
  3181   3182       /* Figure out the rowid of the new row. */
  3182   3183       if( bHaveRowid==0 ){
  3183         -      rc = newRowid(pRtree, &cell.iRowid);
         3184  +      rc = rtreeNewRowid(pRtree, &cell.iRowid);
  3184   3185       }
  3185   3186       *pRowid = cell.iRowid;
  3186   3187   
  3187   3188       if( rc==SQLITE_OK ){
  3188   3189         rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
  3189   3190       }
  3190   3191       if( rc==SQLITE_OK ){
................................................................................
  3268   3269   **   BEGIN;
  3269   3270   **     INSERT INTO rtree...
  3270   3271   **     DROP TABLE <tablename>;    -- Would fail with SQLITE_LOCKED
  3271   3272   **   COMMIT;
  3272   3273   */
  3273   3274   static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
  3274   3275     Rtree *pRtree = (Rtree *)pVtab;
  3275         -  int iwt = pRtree->inWrTrans;
         3276  +  u8 iwt = pRtree->inWrTrans;
  3276   3277     UNUSED_PARAMETER(iSavepoint);
  3277   3278     pRtree->inWrTrans = 0;
  3278   3279     nodeBlobReset(pRtree);
  3279   3280     pRtree->inWrTrans = iwt;
  3280   3281     return SQLITE_OK;
  3281   3282   }
  3282   3283   
................................................................................
  3449   3450       }else{
  3450   3451         sqlite3_str *p = sqlite3_str_new(db);
  3451   3452         int ii;
  3452   3453         char *zSql;
  3453   3454         sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
  3454   3455         for(ii=0; ii<pRtree->nAux; ii++){
  3455   3456           if( ii ) sqlite3_str_append(p, ",", 1);
  3456         -        sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
         3457  +        if( ii<pRtree->nAuxNotNull ){
         3458  +          sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
         3459  +        }else{
         3460  +          sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
         3461  +        }
  3457   3462         }
  3458   3463         sqlite3_str_appendf(p, " WHERE rowid=?1");
  3459   3464         zSql = sqlite3_str_finish(p);
  3460   3465         if( zSql==0 ){
  3461   3466           rc = SQLITE_NOMEM;
  3462   3467         }else{
  3463   3468           rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
................................................................................
  4218   4223       }else{
  4219   4224         sqlite3_result_error_code(ctx, rc);
  4220   4225       }
  4221   4226       sqlite3_free(zReport);
  4222   4227     }
  4223   4228   }
  4224   4229   
         4230  +/* Conditionally include the geopoly code */
         4231  +#ifdef SQLITE_ENABLE_GEOPOLY
         4232  +# include "geopoly.c"
         4233  +#endif
  4225   4234   
  4226   4235   /*
  4227   4236   ** Register the r-tree module with database handle db. This creates the
  4228   4237   ** virtual table module "rtree" and the debugging/analysis scalar 
  4229   4238   ** function "rtreenode".
  4230   4239   */
  4231   4240   int sqlite3RtreeInit(sqlite3 *db){
................................................................................
  4247   4256   #endif
  4248   4257       rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
  4249   4258     }
  4250   4259     if( rc==SQLITE_OK ){
  4251   4260       void *c = (void *)RTREE_COORD_INT32;
  4252   4261       rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
  4253   4262     }
         4263  +#ifdef SQLITE_ENABLE_GEOPOLY
         4264  +  if( rc==SQLITE_OK ){
         4265  +    rc = sqlite3_geopoly_init(db);
         4266  +  }
         4267  +#endif
  4254   4268   
  4255   4269     return rc;
  4256   4270   }
  4257   4271   
  4258   4272   /*
  4259   4273   ** This routine deletes the RtreeGeomCallback object that was attached
  4260   4274   ** one of the SQL functions create by sqlite3_rtree_geometry_callback()

Added ext/rtree/util/randomshape.tcl.

            1  +#!/usr/bin/tclsh
            2  +#
            3  +# This script generates a cluster of random polygons that are useful
            4  +# for testing the geopoly extension.
            5  +#
            6  +# Usage:
            7  +#
            8  +#    tclsh randomshape.tcl | tee x.sql | sqlite3 >x.html
            9  +#
           10  +# The output files are x.sql and x.html.  Run the above multiple times
           11  +# until an interesting "x.html" file is found, then use the "x.sql" inputs
           12  +# to construct test cases.
           13  +#
           14  +proc randomenclosure {cx cy p1 p2 p3 p4} {
           15  +  set r 0
           16  +  set pi 3.145926
           17  +  set pi2 [expr {$pi*2}]
           18  +  set x0 [expr {$cx + rand()*$p3 + $p4}]
           19  +  set ans "\[\[$x0,$cy\]"
           20  +  while {1} {
           21  +    set r [expr {$r+$p1+$p2*rand()}]
           22  +    if {$r>=$pi2} break
           23  +    set m [expr {rand()*$p3 + $p4}]
           24  +    set x [expr {$cx+$m*cos($r)}]
           25  +    set y [expr {$cy+$m*sin($r)}]
           26  +    append ans ",\[$x,$y\]"
           27  +  }
           28  +  append ans ",\[$x0,$cy\]\]"
           29  +  return $ans
           30  +}
           31  +proc randomshape1 {} {
           32  +  set cx [expr {100+int(rand()*800)}]
           33  +  set cy [expr {100+int(rand()*600)}]
           34  +  set p1 [expr {rand()*0.1}]
           35  +  set p2 [expr {rand()*0.5+0.5}]
           36  +  set p3 [expr {rand()*100+25}]
           37  +  set p4 [expr {rand()*25}]
           38  +  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
           39  +}
           40  +proc randomshape1_sm {} {
           41  +  set cx [expr {100+int(rand()*800)}]
           42  +  set cy [expr {100+int(rand()*600)}]
           43  +  set p1 [expr {rand()*0.1}]
           44  +  set p2 [expr {rand()*0.5+0.5}]
           45  +  set p3 [expr {rand()*10+25}]
           46  +  set p4 [expr {rand()*5}]
           47  +  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
           48  +}
           49  +proc randomshape2 {} {
           50  +  set cx [expr {400+int(rand()*200)}]
           51  +  set cy [expr {300+int(rand()*200)}]
           52  +  set p1 [expr {rand()*0.05}]
           53  +  set p2 [expr {rand()*0.5+0.5}]
           54  +  set p3 [expr {rand()*50+200}]
           55  +  set p4 [expr {rand()*50+100}]
           56  +  return [randomenclosure $cx $cy $p1 $p2 $p3 $p4]
           57  +}
           58  +proc randomcolor {} {
           59  +  set n [expr {int(rand()*5)}]
           60  +  return [lindex {red orange green blue purple} $n]
           61  +}
           62  +
           63  +puts {.print '<html>'}
           64  +puts {.print '<svg width="1000" height="800" style="border:1px solid black">'}
           65  +puts {CREATE TABLE t1(poly,clr);}
           66  +puts {CREATE TABLE t2(poly,clr);}
           67  +for {set i 0} {$i<30} {incr i} {
           68  +  puts "INSERT INTO t1(rowid,poly,clr)"
           69  +  puts " VALUES($i,'[randomshape1]','[randomcolor]');"
           70  +}
           71  +for {set i 30} {$i<80} {incr i} {
           72  +  puts "INSERT INTO t1(rowid,poly,clr)"
           73  +  puts " VALUES($i,'[randomshape1_sm]','[randomcolor]');"
           74  +}
           75  +for {set i 100} {$i<105} {incr i} {
           76  +  puts "INSERT INTO t2(rowid,poly,clr)"
           77  +  puts " VALUES($i,'[randomshape2]','[randomcolor]');"
           78  +}
           79  +
           80  +puts {DELETE FROM t1 WHERE geopoly_json(poly) IS NULL;}
           81  +puts {SELECT geopoly_svg(poly,
           82  +   printf('style="fill:none;stroke:%s;stroke-width:1;"',clr))
           83  +  FROM t1;}
           84  +puts {SELECT geopoly_svg(poly,
           85  +   printf('style="fill:none;stroke:%s;stroke-width:2;"',clr))
           86  +  FROM t2;}
           87  +puts {.print '<svg>'}

Added ext/rtree/visual01.txt.

            1  +#!sqlite3
            2  +#
            3  +# This is a visual test case for the geopoly virtual table.
            4  +#
            5  +# Run this script in the sqlite3 CLI, and redirect output into an
            6  +# HTML file.  This display the HTML in a webbrowser.
            7  +#
            8  +
            9  +/* Test data.
           10  +** Lots of shapes to be displayed over a 1000x800 canvas.
           11  +*/
           12  +CREATE TEMP TABLE basis(name TEXT, jshape TEXT);
           13  +INSERT INTO basis(name,jshape) VALUES
           14  +  ('box-20','[[0,0],[20,0],[20,20],[0,20],[0,0]]'),
           15  +  ('house-70','[[0,0],[50,0],[50,50],[25,70],[0,50],[0,0]]'),
           16  +  ('line-40','[[0,0],[40,0],[40,5],[0,5],[0,0]]'),
           17  +  ('line-80','[[0,0],[80,0],[80,7],[0,7],[0,0]]'),
           18  +  ('arrow-50','[[0,0],[25,25],[0,50],[15,25],[0,0]]'),
           19  +  ('triangle-30','[[0,0],[30,0],[15,30],[0,0]]'),
           20  +  ('angle-30','[[0,0],[30,0],[30,30],[26,30],[26,4],[0,4],[0,0]]'),
           21  +  ('star-10','[[1,0],[5,2],[9,0],[7,4],[10,8],[7,7],[5,10],[3,7],[0,8],[3,4],[1,0]]');
           22  +CREATE TEMP TABLE xform(A,B,C,D,clr);
           23  +INSERT INTO xform(A,B,clr) VALUES
           24  +  (1,0,'black'),
           25  +  (0.707,0.707,'blue'),
           26  +  (0.5,0.866,'red'),
           27  +  (-0.866,0.5,'green');
           28  +CREATE TEMP TABLE xyoff(id1,id2,xoff,yoff,PRIMARY KEY(id1,id2,xoff,yoff))
           29  +  WITHOUT ROWID;
           30  +INSERT INTO xyoff VALUES(1,1,811,659);
           31  +INSERT INTO xyoff VALUES(1,1,235,550);
           32  +INSERT INTO xyoff VALUES(1,1,481,620);
           33  +INSERT INTO xyoff VALUES(1,1,106,494);
           34  +INSERT INTO xyoff VALUES(1,1,487,106);
           35  +INSERT INTO xyoff VALUES(1,1,817,595);
           36  +INSERT INTO xyoff VALUES(1,1,240,504);
           37  +INSERT INTO xyoff VALUES(1,1,806,457);
           38  +INSERT INTO xyoff VALUES(1,1,608,107);
           39  +INSERT INTO xyoff VALUES(1,1,768,662);
           40  +INSERT INTO xyoff VALUES(1,2,808,528);
           41  +INSERT INTO xyoff VALUES(1,2,768,528);
           42  +INSERT INTO xyoff VALUES(1,2,771,171);
           43  +INSERT INTO xyoff VALUES(1,2,275,671);
           44  +INSERT INTO xyoff VALUES(1,2,326,336);
           45  +INSERT INTO xyoff VALUES(1,2,690,688);
           46  +INSERT INTO xyoff VALUES(1,2,597,239);
           47  +INSERT INTO xyoff VALUES(1,2,317,528);
           48  +INSERT INTO xyoff VALUES(1,2,366,223);
           49  +INSERT INTO xyoff VALUES(1,2,621,154);
           50  +INSERT INTO xyoff VALUES(1,3,829,469);
           51  +INSERT INTO xyoff VALUES(1,3,794,322);
           52  +INSERT INTO xyoff VALUES(1,3,358,387);
           53  +INSERT INTO xyoff VALUES(1,3,184,444);
           54  +INSERT INTO xyoff VALUES(1,3,729,500);
           55  +INSERT INTO xyoff VALUES(1,3,333,523);
           56  +INSERT INTO xyoff VALUES(1,3,117,595);
           57  +INSERT INTO xyoff VALUES(1,3,496,201);
           58  +INSERT INTO xyoff VALUES(1,3,818,601);
           59  +INSERT INTO xyoff VALUES(1,3,541,343);
           60  +INSERT INTO xyoff VALUES(1,4,603,248);
           61  +INSERT INTO xyoff VALUES(1,4,761,649);
           62  +INSERT INTO xyoff VALUES(1,4,611,181);
           63  +INSERT INTO xyoff VALUES(1,4,607,233);
           64  +INSERT INTO xyoff VALUES(1,4,860,206);
           65  +INSERT INTO xyoff VALUES(1,4,310,231);
           66  +INSERT INTO xyoff VALUES(1,4,727,539);
           67  +INSERT INTO xyoff VALUES(1,4,660,661);
           68  +INSERT INTO xyoff VALUES(1,4,403,133);
           69  +INSERT INTO xyoff VALUES(1,4,619,331);
           70  +INSERT INTO xyoff VALUES(2,1,712,578);
           71  +INSERT INTO xyoff VALUES(2,1,567,313);
           72  +INSERT INTO xyoff VALUES(2,1,231,423);
           73  +INSERT INTO xyoff VALUES(2,1,490,175);
           74  +INSERT INTO xyoff VALUES(2,1,898,353);
           75  +INSERT INTO xyoff VALUES(2,1,589,483);
           76  +INSERT INTO xyoff VALUES(2,1,188,462);
           77  +INSERT INTO xyoff VALUES(2,1,720,106);
           78  +INSERT INTO xyoff VALUES(2,1,793,380);
           79  +INSERT INTO xyoff VALUES(2,1,154,396);
           80  +INSERT INTO xyoff VALUES(2,2,324,218);
           81  +INSERT INTO xyoff VALUES(2,2,120,327);
           82  +INSERT INTO xyoff VALUES(2,2,655,133);
           83  +INSERT INTO xyoff VALUES(2,2,516,603);
           84  +INSERT INTO xyoff VALUES(2,2,529,572);
           85  +INSERT INTO xyoff VALUES(2,2,481,212);
           86  +INSERT INTO xyoff VALUES(2,2,802,107);
           87  +INSERT INTO xyoff VALUES(2,2,234,509);
           88  +INSERT INTO xyoff VALUES(2,2,501,269);
           89  +INSERT INTO xyoff VALUES(2,2,349,553);
           90  +INSERT INTO xyoff VALUES(2,3,495,685);
           91  +INSERT INTO xyoff VALUES(2,3,897,372);
           92  +INSERT INTO xyoff VALUES(2,3,350,681);
           93  +INSERT INTO xyoff VALUES(2,3,832,257);
           94  +INSERT INTO xyoff VALUES(2,3,778,149);
           95  +INSERT INTO xyoff VALUES(2,3,683,426);
           96  +INSERT INTO xyoff VALUES(2,3,693,217);
           97  +INSERT INTO xyoff VALUES(2,3,746,317);
           98  +INSERT INTO xyoff VALUES(2,3,805,369);
           99  +INSERT INTO xyoff VALUES(2,3,336,585);
          100  +INSERT INTO xyoff VALUES(2,4,890,255);
          101  +INSERT INTO xyoff VALUES(2,4,556,565);
          102  +INSERT INTO xyoff VALUES(2,4,865,555);
          103  +INSERT INTO xyoff VALUES(2,4,230,293);
          104  +INSERT INTO xyoff VALUES(2,4,247,251);
          105  +INSERT INTO xyoff VALUES(2,4,730,563);
          106  +INSERT INTO xyoff VALUES(2,4,318,282);
          107  +INSERT INTO xyoff VALUES(2,4,220,431);
          108  +INSERT INTO xyoff VALUES(2,4,828,336);
          109  +INSERT INTO xyoff VALUES(2,4,278,525);
          110  +INSERT INTO xyoff VALUES(3,1,324,656);
          111  +INSERT INTO xyoff VALUES(3,1,625,362);
          112  +INSERT INTO xyoff VALUES(3,1,155,570);
          113  +INSERT INTO xyoff VALUES(3,1,267,433);
          114  +INSERT INTO xyoff VALUES(3,1,599,121);
          115  +INSERT INTO xyoff VALUES(3,1,873,498);
          116  +INSERT INTO xyoff VALUES(3,1,789,520);
          117  +INSERT INTO xyoff VALUES(3,1,656,378);
          118  +INSERT INTO xyoff VALUES(3,1,831,601);
          119  +INSERT INTO xyoff VALUES(3,1,256,471);
          120  +INSERT INTO xyoff VALUES(3,2,332,258);
          121  +INSERT INTO xyoff VALUES(3,2,305,463);
          122  +INSERT INTO xyoff VALUES(3,2,796,341);
          123  +INSERT INTO xyoff VALUES(3,2,830,229);
          124  +INSERT INTO xyoff VALUES(3,2,413,271);
          125  +INSERT INTO xyoff VALUES(3,2,269,140);
          126  +INSERT INTO xyoff VALUES(3,2,628,441);
          127  +INSERT INTO xyoff VALUES(3,2,747,643);
          128  +INSERT INTO xyoff VALUES(3,2,584,435);
          129  +INSERT INTO xyoff VALUES(3,2,784,314);
          130  +INSERT INTO xyoff VALUES(3,3,722,233);
          131  +INSERT INTO xyoff VALUES(3,3,815,421);
          132  +INSERT INTO xyoff VALUES(3,3,401,267);
          133  +INSERT INTO xyoff VALUES(3,3,451,650);
          134  +INSERT INTO xyoff VALUES(3,3,329,485);
          135  +INSERT INTO xyoff VALUES(3,3,878,370);
          136  +INSERT INTO xyoff VALUES(3,3,162,616);
          137  +INSERT INTO xyoff VALUES(3,3,844,183);
          138  +INSERT INTO xyoff VALUES(3,3,161,216);
          139  +INSERT INTO xyoff VALUES(3,3,176,676);
          140  +INSERT INTO xyoff VALUES(3,4,780,128);
          141  +INSERT INTO xyoff VALUES(3,4,566,121);
          142  +INSERT INTO xyoff VALUES(3,4,646,120);
          143  +INSERT INTO xyoff VALUES(3,4,223,557);
          144  +INSERT INTO xyoff VALUES(3,4,251,117);
          145  +INSERT INTO xyoff VALUES(3,4,139,209);
          146  +INSERT INTO xyoff VALUES(3,4,813,597);
          147  +INSERT INTO xyoff VALUES(3,4,454,538);
          148  +INSERT INTO xyoff VALUES(3,4,616,198);
          149  +INSERT INTO xyoff VALUES(3,4,210,159);
          150  +INSERT INTO xyoff VALUES(4,1,208,415);
          151  +INSERT INTO xyoff VALUES(4,1,326,665);
          152  +INSERT INTO xyoff VALUES(4,1,612,133);
          153  +INSERT INTO xyoff VALUES(4,1,537,513);
          154  +INSERT INTO xyoff VALUES(4,1,638,438);
          155  +INSERT INTO xyoff VALUES(4,1,808,269);
          156  +INSERT INTO xyoff VALUES(4,1,552,121);
          157  +INSERT INTO xyoff VALUES(4,1,100,189);
          158  +INSERT INTO xyoff VALUES(4,1,643,664);
          159  +INSERT INTO xyoff VALUES(4,1,726,378);
          160  +INSERT INTO xyoff VALUES(4,2,478,409);
          161  +INSERT INTO xyoff VALUES(4,2,497,507);
          162  +INSERT INTO xyoff VALUES(4,2,233,148);
          163  +INSERT INTO xyoff VALUES(4,2,587,237);
          164  +INSERT INTO xyoff VALUES(4,2,604,166);
          165  +INSERT INTO xyoff VALUES(4,2,165,455);
          166  +INSERT INTO xyoff VALUES(4,2,320,258);
          167  +INSERT INTO xyoff VALUES(4,2,353,496);
          168  +INSERT INTO xyoff VALUES(4,2,347,495);
          169  +INSERT INTO xyoff VALUES(4,2,166,622);
          170  +INSERT INTO xyoff VALUES(4,3,461,332);
          171  +INSERT INTO xyoff VALUES(4,3,685,278);
          172  +INSERT INTO xyoff VALUES(4,3,427,594);
          173  +INSERT INTO xyoff VALUES(4,3,467,346);
          174  +INSERT INTO xyoff VALUES(4,3,125,548);
          175  +INSERT INTO xyoff VALUES(4,3,597,680);
          176  +INSERT INTO xyoff VALUES(4,3,820,445);
          177  +INSERT INTO xyoff VALUES(4,3,144,330);
          178  +INSERT INTO xyoff VALUES(4,3,557,434);
          179  +INSERT INTO xyoff VALUES(4,3,254,315);
          180  +INSERT INTO xyoff VALUES(4,4,157,339);
          181  +INSERT INTO xyoff VALUES(4,4,249,220);
          182  +INSERT INTO xyoff VALUES(4,4,391,323);
          183  +INSERT INTO xyoff VALUES(4,4,589,429);
          184  +INSERT INTO xyoff VALUES(4,4,859,592);
          185  +INSERT INTO xyoff VALUES(4,4,337,680);
          186  +INSERT INTO xyoff VALUES(4,4,410,288);
          187  +INSERT INTO xyoff VALUES(4,4,636,596);
          188  +INSERT INTO xyoff VALUES(4,4,734,433);
          189  +INSERT INTO xyoff VALUES(4,4,559,549);
          190  +INSERT INTO xyoff VALUES(5,1,549,607);
          191  +INSERT INTO xyoff VALUES(5,1,584,498);
          192  +INSERT INTO xyoff VALUES(5,1,699,116);
          193  +INSERT INTO xyoff VALUES(5,1,525,524);
          194  +INSERT INTO xyoff VALUES(5,1,304,667);
          195  +INSERT INTO xyoff VALUES(5,1,302,232);
          196  +INSERT INTO xyoff VALUES(5,1,403,149);
          197  +INSERT INTO xyoff VALUES(5,1,824,403);
          198  +INSERT INTO xyoff VALUES(5,1,697,203);
          199  +INSERT INTO xyoff VALUES(5,1,293,689);
          200  +INSERT INTO xyoff VALUES(5,2,199,275);
          201  +INSERT INTO xyoff VALUES(5,2,395,393);
          202  +INSERT INTO xyoff VALUES(5,2,657,642);
          203  +INSERT INTO xyoff VALUES(5,2,200,655);
          204  +INSERT INTO xyoff VALUES(5,2,882,234);
          205  +INSERT INTO xyoff VALUES(5,2,483,565);
          206  +INSERT INTO xyoff VALUES(5,2,755,640);
          207  +INSERT INTO xyoff VALUES(5,2,810,305);
          208  +INSERT INTO xyoff VALUES(5,2,731,655);
          209  +INSERT INTO xyoff VALUES(5,2,466,690);
          210  +INSERT INTO xyoff VALUES(5,3,563,584);
          211  +INSERT INTO xyoff VALUES(5,3,491,117);
          212  +INSERT INTO xyoff VALUES(5,3,779,292);
          213  +INSERT INTO xyoff VALUES(5,3,375,637);
          214  +INSERT INTO xyoff VALUES(5,3,253,553);
          215  +INSERT INTO xyoff VALUES(5,3,797,514);
          216  +INSERT INTO xyoff VALUES(5,3,229,480);
          217  +INSERT INTO xyoff VALUES(5,3,257,194);
          218  +INSERT INTO xyoff VALUES(5,3,449,555);
          219  +INSERT INTO xyoff VALUES(5,3,849,630);
          220  +INSERT INTO xyoff VALUES(5,4,329,286);
          221  +INSERT INTO xyoff VALUES(5,4,640,197);
          222  +INSERT INTO xyoff VALUES(5,4,104,150);
          223  +INSERT INTO xyoff VALUES(5,4,438,272);
          224  +INSERT INTO xyoff VALUES(5,4,773,226);
          225  +INSERT INTO xyoff VALUES(5,4,441,650);
          226  +INSERT INTO xyoff VALUES(5,4,242,340);
          227  +INSERT INTO xyoff VALUES(5,4,301,435);
          228  +INSERT INTO xyoff VALUES(5,4,171,397);
          229  +INSERT INTO xyoff VALUES(5,4,541,619);
          230  +INSERT INTO xyoff VALUES(6,1,651,301);
          231  +INSERT INTO xyoff VALUES(6,1,637,137);
          232  +INSERT INTO xyoff VALUES(6,1,765,643);
          233  +INSERT INTO xyoff VALUES(6,1,173,296);
          234  +INSERT INTO xyoff VALUES(6,1,263,192);
          235  +INSERT INTO xyoff VALUES(6,1,791,302);
          236  +INSERT INTO xyoff VALUES(6,1,860,601);
          237  +INSERT INTO xyoff VALUES(6,1,780,445);
          238  +INSERT INTO xyoff VALUES(6,1,462,214);
          239  +INSERT INTO xyoff VALUES(6,1,802,207);
          240  +INSERT INTO xyoff VALUES(6,2,811,685);
          241  +INSERT INTO xyoff VALUES(6,2,533,531);
          242  +INSERT INTO xyoff VALUES(6,2,390,614);
          243  +INSERT INTO xyoff VALUES(6,2,260,580);
          244  +INSERT INTO xyoff VALUES(6,2,116,377);
          245  +INSERT INTO xyoff VALUES(6,2,860,458);
          246  +INSERT INTO xyoff VALUES(6,2,438,590);
          247  +INSERT INTO xyoff VALUES(6,2,604,562);
          248  +INSERT INTO xyoff VALUES(6,2,241,242);
          249  +INSERT INTO xyoff VALUES(6,2,667,298);
          250  +INSERT INTO xyoff VALUES(6,3,787,698);
          251  +INSERT INTO xyoff VALUES(6,3,868,521);
          252  +INSERT INTO xyoff VALUES(6,3,412,587);
          253  +INSERT INTO xyoff VALUES(6,3,640,131);
          254  +INSERT INTO xyoff VALUES(6,3,748,410);
          255  +INSERT INTO xyoff VALUES(6,3,257,244);
          256  +INSERT INTO xyoff VALUES(6,3,411,195);
          257  +INSERT INTO xyoff VALUES(6,3,464,356);
          258  +INSERT INTO xyoff VALUES(6,3,157,339);
          259  +INSERT INTO xyoff VALUES(6,3,434,505);
          260  +INSERT INTO xyoff VALUES(6,4,480,671);
          261  +INSERT INTO xyoff VALUES(6,4,519,228);
          262  +INSERT INTO xyoff VALUES(6,4,404,513);
          263  +INSERT INTO xyoff VALUES(6,4,120,538);
          264  +INSERT INTO xyoff VALUES(6,4,403,663);
          265  +INSERT INTO xyoff VALUES(6,4,477,677);
          266  +INSERT INTO xyoff VALUES(6,4,690,154);
          267  +INSERT INTO xyoff VALUES(6,4,606,498);
          268  +INSERT INTO xyoff VALUES(6,4,430,665);
          269  +INSERT INTO xyoff VALUES(6,4,499,273);
          270  +INSERT INTO xyoff VALUES(7,1,118,526);
          271  +INSERT INTO xyoff VALUES(7,1,817,522);
          272  +INSERT INTO xyoff VALUES(7,1,388,638);
          273  +INSERT INTO xyoff VALUES(7,1,181,265);
          274  +INSERT INTO xyoff VALUES(7,1,442,332);
          275  +INSERT INTO xyoff VALUES(7,1,475,282);
          276  +INSERT INTO xyoff VALUES(7,1,722,633);
          277  +INSERT INTO xyoff VALUES(7,1,104,394);
          278  +INSERT INTO xyoff VALUES(7,1,631,262);
          279  +INSERT INTO xyoff VALUES(7,1,372,392);
          280  +INSERT INTO xyoff VALUES(7,2,600,413);
          281  +INSERT INTO xyoff VALUES(7,2,386,223);
          282  +INSERT INTO xyoff VALUES(7,2,839,174);
          283  +INSERT INTO xyoff VALUES(7,2,293,410);
          284  +INSERT INTO xyoff VALUES(7,2,281,391);
          285  +INSERT INTO xyoff VALUES(7,2,859,387);
          286  +INSERT INTO xyoff VALUES(7,2,478,347);
          287  +INSERT INTO xyoff VALUES(7,2,646,690);
          288  +INSERT INTO xyoff VALUES(7,2,713,234);
          289  +INSERT INTO xyoff VALUES(7,2,199,588);
          290  +INSERT INTO xyoff VALUES(7,3,389,256);
          291  +INSERT INTO xyoff VALUES(7,3,349,542);
          292  +INSERT INTO xyoff VALUES(7,3,363,345);
          293  +INSERT INTO xyoff VALUES(7,3,751,302);
          294  +INSERT INTO xyoff VALUES(7,3,423,386);
          295  +INSERT INTO xyoff VALUES(7,3,267,444);
          296  +INSERT INTO xyoff VALUES(7,3,243,182);
          297  +INSERT INTO xyoff VALUES(7,3,453,658);
          298  +INSERT INTO xyoff VALUES(7,3,126,345);
          299  +INSERT INTO xyoff VALUES(7,3,120,472);
          300  +INSERT INTO xyoff VALUES(7,4,359,654);
          301  +INSERT INTO xyoff VALUES(7,4,339,516);
          302  +INSERT INTO xyoff VALUES(7,4,710,452);
          303  +INSERT INTO xyoff VALUES(7,4,810,560);
          304  +INSERT INTO xyoff VALUES(7,4,644,692);
          305  +INSERT INTO xyoff VALUES(7,4,826,327);
          306  +INSERT INTO xyoff VALUES(7,4,465,462);
          307  +INSERT INTO xyoff VALUES(7,4,310,456);
          308  +INSERT INTO xyoff VALUES(7,4,577,613);
          309  +INSERT INTO xyoff VALUES(7,4,502,555);
          310  +INSERT INTO xyoff VALUES(8,1,601,620);
          311  +INSERT INTO xyoff VALUES(8,1,372,683);
          312  +INSERT INTO xyoff VALUES(8,1,758,399);
          313  +INSERT INTO xyoff VALUES(8,1,485,552);
          314  +INSERT INTO xyoff VALUES(8,1,159,563);
          315  +INSERT INTO xyoff VALUES(8,1,536,303);
          316  +INSERT INTO xyoff VALUES(8,1,122,263);
          317  +INSERT INTO xyoff VALUES(8,1,836,435);
          318  +INSERT INTO xyoff VALUES(8,1,544,146);
          319  +INSERT INTO xyoff VALUES(8,1,270,277);
          320  +INSERT INTO xyoff VALUES(8,2,849,281);
          321  +INSERT INTO xyoff VALUES(8,2,563,242);
          322  +INSERT INTO xyoff VALUES(8,2,704,463);
          323  +INSERT INTO xyoff VALUES(8,2,102,165);
          324  +INSERT INTO xyoff VALUES(8,2,797,524);
          325  +INSERT INTO xyoff VALUES(8,2,612,426);
          326  +INSERT INTO xyoff VALUES(8,2,345,372);
          327  +INSERT INTO xyoff VALUES(8,2,820,376);
          328  +INSERT INTO xyoff VALUES(8,2,789,156);
          329  +INSERT INTO xyoff VALUES(8,2,321,466);
          330  +INSERT INTO xyoff VALUES(8,3,150,332);
          331  +INSERT INTO xyoff VALUES(8,3,136,152);
          332  +INSERT INTO xyoff VALUES(8,3,468,528);
          333  +INSERT INTO xyoff VALUES(8,3,409,192);
          334  +INSERT INTO xyoff VALUES(8,3,820,216);
          335  +INSERT INTO xyoff VALUES(8,3,847,249);
          336  +INSERT INTO xyoff VALUES(8,3,801,267);
          337  +INSERT INTO xyoff VALUES(8,3,181,670);
          338  +INSERT INTO xyoff VALUES(8,3,398,563);
          339  +INSERT INTO xyoff VALUES(8,3,439,576);
          340  +INSERT INTO xyoff VALUES(8,4,123,309);
          341  +INSERT INTO xyoff VALUES(8,4,190,496);
          342  +INSERT INTO xyoff VALUES(8,4,571,531);
          343  +INSERT INTO xyoff VALUES(8,4,290,255);
          344  +INSERT INTO xyoff VALUES(8,4,244,412);
          345  +INSERT INTO xyoff VALUES(8,4,264,596);
          346  +INSERT INTO xyoff VALUES(8,4,253,420);
          347  +INSERT INTO xyoff VALUES(8,4,847,536);
          348  +INSERT INTO xyoff VALUES(8,4,120,288);
          349  +INSERT INTO xyoff VALUES(8,4,331,639);
          350  +
          351  +/* Create the geopoly object from test data above */
          352  +CREATE VIRTUAL TABLE geo1 USING geopoly(type,clr);
          353  +INSERT INTO geo1(_shape,type,clr)
          354  +  SELECT geopoly_xform(jshape,A,B,-B,A,xoff,yoff), basis.name, xform.clr
          355  +    FROM basis, xform, xyoff
          356  +   WHERE xyoff.id1=basis.rowid AND xyoff.id2=xform.rowid;
          357  +
          358  +
          359  +/* Query polygon */
          360  +CREATE TEMP TABLE querypoly(poly JSON, clr TEXT);
          361  +INSERT INTO querypoly(clr, poly) VALUES
          362  +  ('orange', '[[300,300],[400,350],[500,250],[480,500],[400,480],[300,550],[280,450],[320,400],[280,350],[300,300]]');
          363  +
          364  +/* Generate the HTML */
          365  +.print '<html>'
          366  +.print '<h1>Everything</h1>'
          367  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          368  +SELECT geopoly_svg(_shape, 
          369  +         printf('style="fill:none;stroke:%s;stroke-width:1"',clr)
          370  +       )
          371  +  FROM geo1;
          372  +SELECT geopoly_svg(poly, 
          373  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          374  +       )
          375  +  FROM querypoly;
          376  +.print '</svg>'
          377  +
          378  +.print '<h1>Overlap Query</h1>'
          379  +.print '<pre>'
          380  +.print 'SELECT *'
          381  +.print '  FROM geo1, querypoly'
          382  +.print ' WHERE geopoly_overlap(_shape, poly);'
          383  +.print 
          384  +EXPLAIN QUERY PLAN
          385  +SELECT geopoly_svg(_shape,
          386  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          387  +       )
          388  +  FROM geo1, querypoly
          389  + WHERE geopoly_overlap(_shape, poly);
          390  +.print '</pre>'
          391  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          392  +SELECT geopoly_svg(_shape,
          393  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          394  +       )
          395  +  FROM geo1, querypoly
          396  + WHERE geopoly_overlap(_shape, poly);
          397  +SELECT geopoly_svg(poly, 
          398  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          399  +       )
          400  +  FROM querypoly;
          401  +.print '</svg>'
          402  +
          403  +.print '<h1>Overlap Query And Result Bounding Box</h1>'
          404  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          405  +SELECT geopoly_svg(_shape,
          406  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          407  +       )
          408  +  FROM geo1, querypoly
          409  + WHERE geopoly_overlap(_shape, poly);
          410  +SELECT geopoly_svg(geopoly_bbox(poly),
          411  +         'style="fill:none;stroke:black;stroke-width:3"'
          412  +       )
          413  +  FROM querypoly;
          414  +SELECT geopoly_svg(poly, 
          415  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          416  +       )
          417  +  FROM querypoly;
          418  +SELECT geopoly_svg(geopoly_group_bbox(_shape),
          419  +         'style="fill:none;stroke:red;stroke-width:3"'
          420  +       )
          421  +  FROM geo1, querypoly
          422  + WHERE geopoly_overlap(_shape, poly);
          423  +.print '</svg>'
          424  +
          425  +.print '<h1>Bounding-Box Overlap Query</h1>'
          426  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          427  +SELECT geopoly_svg(_shape,
          428  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          429  +       ),
          430  +       geopoly_svg(geopoly_bbox(_shape),
          431  +         'style="fill:none;stroke:black;stroke-width:1"'
          432  +       )
          433  +  FROM geo1, querypoly
          434  + WHERE geopoly_overlap(geopoly_bbox(_shape), geopoly_bbox(poly));
          435  +SELECT geopoly_svg(poly, 
          436  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          437  +       )
          438  +  FROM querypoly;
          439  +SELECT geopoly_svg(geopoly_bbox(poly),
          440  +         'style="fill:none;stroke:black;stroke-width:3"'
          441  +       )
          442  +  FROM querypoly;
          443  +.print '</svg>'
          444  +
          445  +.print '<h1>Within Query</h1>'
          446  +.print '<pre>'
          447  +.print 'SELECT *'
          448  +.print '  FROM geo1, querypoly'
          449  +.print ' WHERE geopoly_within(_shape, poly);'
          450  +.print 
          451  +EXPLAIN QUERY PLAN
          452  +SELECT geopoly_svg(_shape,
          453  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          454  +       )
          455  +  FROM geo1, querypoly
          456  + WHERE geopoly_within(_shape, poly);
          457  +.print '</pre>'
          458  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          459  +SELECT geopoly_svg(_shape,
          460  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          461  +       )
          462  +  FROM geo1, querypoly
          463  + WHERE geopoly_within(_shape, poly);
          464  +SELECT geopoly_svg(poly, 
          465  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          466  +       )
          467  +  FROM querypoly;
          468  +.print '</svg>'
          469  +
          470  +.print '<h1>Bounding-Box WITHIN Query</h1>'
          471  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          472  +SELECT geopoly_svg(_shape,
          473  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          474  +       ),
          475  +       geopoly_svg(geopoly_bbox(_shape),
          476  +         'style="fill:none;stroke:black;stroke-width:1"'
          477  +       )
          478  +  FROM geo1, querypoly
          479  + WHERE geopoly_within(geopoly_bbox(_shape), geopoly_bbox(poly));
          480  +SELECT geopoly_svg(poly, 
          481  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          482  +       )
          483  +  FROM querypoly;
          484  +SELECT geopoly_svg(geopoly_bbox(poly),
          485  +         'style="fill:none;stroke:black;stroke-width:3"'
          486  +       )
          487  +  FROM querypoly;
          488  +.print '</svg>'
          489  +
          490  +.print '<h1>Not Overlap Query</h1>'
          491  +.print '<pre>'
          492  +.print 'SELECT *'
          493  +.print '  FROM geo1, querypoly'
          494  +.print ' WHERE NOT geopoly_overlap(_shape, poly);'
          495  +.print 
          496  +EXPLAIN QUERY PLAN
          497  +SELECT geopoly_svg(_shape,
          498  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          499  +       )
          500  +  FROM geo1, querypoly
          501  + WHERE NOT geopoly_overlap(_shape, poly);
          502  +.print '</pre>'
          503  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          504  +SELECT geopoly_svg(_shape,
          505  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          506  +       )
          507  +  FROM geo1, querypoly
          508  + WHERE NOT geopoly_overlap(_shape, poly);
          509  +SELECT geopoly_svg(poly, 
          510  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          511  +       )
          512  +  FROM querypoly;
          513  +.print '</svg>'
          514  +
          515  +.print '<h1>Not Within Query</h1>'
          516  +.print '<pre>'
          517  +.print 'SELECT *'
          518  +.print '  FROM geo1, querypoly'
          519  +.print ' WHERE NOT geopoly_within(_shape, poly);'
          520  +.print 
          521  +EXPLAIN QUERY PLAN
          522  +SELECT geopoly_svg(_shape,
          523  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          524  +       )
          525  +  FROM geo1, querypoly
          526  + WHERE NOT geopoly_within(_shape, poly);
          527  +.print '</pre>'
          528  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          529  +SELECT geopoly_svg(_shape,
          530  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          531  +       )
          532  +  FROM geo1, querypoly
          533  + WHERE NOT geopoly_within(_shape, poly);
          534  +SELECT geopoly_svg(poly, 
          535  +         printf('style="fill:%s;fill-opacity:0.5;"',clr)
          536  +       )
          537  +  FROM querypoly;
          538  +.print '</svg>'
          539  +
          540  +.print '<h1>Color-Change For Overlapping Elements</h1>'
          541  +BEGIN;
          542  +UPDATE geo1
          543  +   SET clr=CASE WHEN rowid IN (SELECT geo1.rowid FROM geo1, querypoly
          544  +                                WHERE geopoly_overlap(_shape,poly))
          545  +           THEN 'red' ELSE 'blue' END;
          546  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          547  +SELECT geopoly_svg(_shape,
          548  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          549  +       )
          550  +  FROM geo1;
          551  +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"')
          552  +  FROM querypoly;
          553  +ROLLBACK;
          554  +.print '</svg>'
          555  +
          556  +.print '<h1>Color-Change And Move Overlapping Elements</h1>'
          557  +BEGIN;
          558  +UPDATE geo1
          559  +   SET clr=CASE WHEN rowid IN (SELECT geo1.rowid FROM geo1, querypoly
          560  +                                WHERE geopoly_overlap(_shape,poly))
          561  +           THEN 'red' ELSE '#76ccff' END;
          562  +UPDATE geo1
          563  +   SET _shape=geopoly_xform(_shape,1,0,0,1,300,0)
          564  + WHERE geopoly_overlap(_shape,(SELECT poly FROM querypoly));
          565  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          566  +SELECT geopoly_svg(_shape,
          567  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          568  +       )
          569  +  FROM geo1;
          570  +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"')
          571  +  FROM querypoly;
          572  +--ROLLBACK;
          573  +.print '</svg>'
          574  +
          575  +
          576  +.print '<h1>Overlap With Translated Query Polygon</h1>'
          577  +UPDATE querypoly SET poly=geopoly_xform(poly,1,0,0,1,300,0);
          578  +.print '<svg width="1000" height="800" style="border:1px solid black">'
          579  +SELECT geopoly_svg(_shape,
          580  +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
          581  +       )
          582  +  FROM geo1
          583  + WHERE geopoly_overlap(_shape,(SELECT poly FROM querypoly));
          584  +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"')
          585  +  FROM querypoly;
          586  +ROLLBACK;
          587  +.print '</svg>'
          588  +
          589  +.print '<h1>Regular Polygons</h1>'
          590  +.print '<svg width="1000" height="200" style="border:1px solid black">'
          591  +SELECT geopoly_svg(geopoly_regular(100,100,40,3),'style="fill:none;stroke:red;stroke-width:1"');
          592  +SELECT geopoly_svg(geopoly_regular(200,100,40,4),'style="fill:none;stroke:orange;stroke-width:1"');
          593  +SELECT geopoly_svg(geopoly_regular(300,100,40,5),'style="fill:none;stroke:green;stroke-width:1"');
          594  +SELECT geopoly_svg(geopoly_regular(400,100,40,6),'style="fill:none;stroke:blue;stroke-width:1"');
          595  +SELECT geopoly_svg(geopoly_regular(500,100,40,7),'style="fill:none;stroke:purple;stroke-width:1"');
          596  +SELECT geopoly_svg(geopoly_regular(600,100,40,8),'style="fill:none;stroke:red;stroke-width:1"');
          597  +SELECT geopoly_svg(geopoly_regular(700,100,40,10),'style="fill:none;stroke:orange;stroke-width:1"');
          598  +SELECT geopoly_svg(geopoly_regular(800,100,40,20),'style="fill:none;stroke:green;stroke-width:1"');
          599  +SELECT geopoly_svg(geopoly_regular(900,100,40,30),'style="fill:none;stroke:blue;stroke-width:1"');
          600  +.print '</svg>'
          601  +
          602  +.print '</html>'

Changes to ext/session/sqlite3session.c.

  4884   4884       sessionAppendTableHdr(&buf, eChangeset, pTab, &rc);
  4885   4885       for(i=0; i<pTab->nChange; i++){
  4886   4886         SessionChange *p;
  4887   4887         for(p=pTab->apChange[i]; p; p=p->pNext){
  4888   4888           sessionAppendByte(&buf, p->op, &rc);
  4889   4889           sessionAppendByte(&buf, p->bIndirect, &rc);
  4890   4890           sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
         4891  +        if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
         4892  +          rc = xOutput(pOut, buf.aBuf, buf.nBuf);
         4893  +          buf.nBuf = 0;
         4894  +        }
  4891   4895         }
  4892   4896       }
  4893         -
  4894         -    if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
  4895         -      rc = xOutput(pOut, buf.aBuf, buf.nBuf);
  4896         -      buf.nBuf = 0;
  4897         -    }
  4898   4897     }
  4899   4898   
  4900   4899     if( rc==SQLITE_OK ){
  4901   4900       if( xOutput ){
  4902   4901         if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
  4903   4902       }else{
  4904   4903         *ppOut = buf.aBuf;

Changes to ext/userauth/userauth.c.

   206    206     if( db->auth.zAuthUser==0 ) return SQLITE_NOMEM;
   207    207     db->auth.zAuthPW = sqlite3_malloc( nPW+1 );
   208    208     if( db->auth.zAuthPW==0 ) return SQLITE_NOMEM;
   209    209     memcpy(db->auth.zAuthPW,zPW,nPW);
   210    210     db->auth.nAuthPW = nPW;
   211    211     rc = sqlite3UserAuthCheckLogin(db, "main", &authLevel);
   212    212     db->auth.authLevel = authLevel;
   213         -  sqlite3ExpirePreparedStatements(db);
          213  +  sqlite3ExpirePreparedStatements(db, 0);
   214    214     if( rc ){
   215    215       return rc;           /* OOM error, I/O error, etc. */
   216    216     }
   217    217     if( authLevel<UAUTH_User ){
   218    218       return SQLITE_AUTH;  /* Incorrect username and/or password */
   219    219     }
   220    220     return SQLITE_OK;      /* Successful login */

Changes to main.mk.

   225    225     $(TOP)/ext/fts3/fts3_write.c
   226    226   SRC += \
   227    227     $(TOP)/ext/icu/sqliteicu.h \
   228    228     $(TOP)/ext/icu/icu.c
   229    229   SRC += \
   230    230     $(TOP)/ext/rtree/sqlite3rtree.h \
   231    231     $(TOP)/ext/rtree/rtree.h \
   232         -  $(TOP)/ext/rtree/rtree.c
          232  +  $(TOP)/ext/rtree/rtree.c \
          233  +  $(TOP)/ext/rtree/geopoly.c
   233    234   SRC += \
   234    235     $(TOP)/ext/session/sqlite3session.c \
   235    236     $(TOP)/ext/session/sqlite3session.h
   236    237   SRC += \
   237    238     $(TOP)/ext/userauth/userauth.c \
   238    239     $(TOP)/ext/userauth/sqlite3userauth.h 
   239    240   SRC += \
................................................................................
   356    357   #
   357    358   TESTSRC += \
   358    359     $(TOP)/ext/misc/amatch.c \
   359    360     $(TOP)/ext/misc/carray.c \
   360    361     $(TOP)/ext/misc/closure.c \
   361    362     $(TOP)/ext/misc/csv.c \
   362    363     $(TOP)/ext/misc/eval.c \
          364  +  $(TOP)/ext/misc/explain.c \
   363    365     $(TOP)/ext/misc/fileio.c \
   364    366     $(TOP)/ext/misc/fuzzer.c \
   365    367     $(TOP)/ext/misc/ieee754.c \
   366    368     $(TOP)/ext/misc/mmapwarm.c \
   367    369     $(TOP)/ext/misc/nextchar.c \
   368    370     $(TOP)/ext/misc/normalize.c \
   369    371     $(TOP)/ext/misc/percentile.c \
................................................................................
   472    474     $(TOP)/ext/fts2/fts2_tokenizer.h
   473    475   EXTHDR += \
   474    476     $(TOP)/ext/fts3/fts3.h \
   475    477     $(TOP)/ext/fts3/fts3Int.h \
   476    478     $(TOP)/ext/fts3/fts3_hash.h \
   477    479     $(TOP)/ext/fts3/fts3_tokenizer.h
   478    480   EXTHDR += \
   479         -  $(TOP)/ext/rtree/rtree.h
          481  +  $(TOP)/ext/rtree/rtree.h \
          482  +  $(TOP)/ext/rtree/geopoly.c
   480    483   EXTHDR += \
   481    484     $(TOP)/ext/icu/sqliteicu.h
   482    485   EXTHDR += \
   483    486     $(TOP)/ext/fts5/fts5Int.h  \
   484    487     fts5parse.h                \
   485    488     $(TOP)/ext/fts5/fts5.h 
   486    489   EXTHDR += \
................................................................................
   998   1001   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \
   999   1002   		$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB)
  1000   1003   
  1001   1004   rollback-test$(EXE):	$(TOP)/tool/rollback-test.c sqlite3.o
  1002   1005   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o rollback-test$(EXE) \
  1003   1006   		$(TOP)/tool/rollback-test.c sqlite3.o $(THREADLIB)
  1004   1007   
         1008  +atrc$(EXE):	$(TOP)/test/atrc.c sqlite3.o
         1009  +	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o atrc$(EXE) \
         1010  +		$(TOP)/test/atrc.c sqlite3.o $(THREADLIB)
         1011  +
  1005   1012   LogEst$(EXE):	$(TOP)/tool/logest.c sqlite3.h
  1006   1013   	$(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c
  1007   1014   
  1008   1015   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
  1009   1016   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
  1010   1017   		$(TOP)/test/wordcount.c sqlite3.c
  1011   1018   

Changes to src/alter.c.

    16     16   
    17     17   /*
    18     18   ** The code in this file only exists if we are not omitting the
    19     19   ** ALTER TABLE logic from the build.
    20     20   */
    21     21   #ifndef SQLITE_OMIT_ALTERTABLE
    22     22   
    23         -
    24         -/*
    25         -** This function is used by SQL generated to implement the 
    26         -** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
    27         -** CREATE INDEX command. The second is a table name. The table name in 
    28         -** the CREATE TABLE or CREATE INDEX statement is replaced with the third
    29         -** argument and the result returned. Examples:
    30         -**
    31         -** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
    32         -**     -> 'CREATE TABLE def(a, b, c)'
    33         -**
    34         -** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
    35         -**     -> 'CREATE INDEX i ON def(a, b, c)'
    36         -*/
    37         -static void renameTableFunc(
    38         -  sqlite3_context *context,
    39         -  int NotUsed,
    40         -  sqlite3_value **argv
    41         -){
    42         -  unsigned char const *zSql = sqlite3_value_text(argv[0]);
    43         -  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
    44         -
    45         -  int token;
    46         -  Token tname;
    47         -  unsigned char const *zCsr = zSql;
    48         -  int len = 0;
    49         -  char *zRet;
    50         -
    51         -  sqlite3 *db = sqlite3_context_db_handle(context);
    52         -
    53         -  UNUSED_PARAMETER(NotUsed);
    54         -
    55         -  /* The principle used to locate the table name in the CREATE TABLE 
    56         -  ** statement is that the table name is the first non-space token that
    57         -  ** is immediately followed by a TK_LP or TK_USING token.
    58         -  */
    59         -  if( zSql ){
    60         -    do {
    61         -      if( !*zCsr ){
    62         -        /* Ran out of input before finding an opening bracket. Return NULL. */
    63         -        return;
    64         -      }
    65         -
    66         -      /* Store the token that zCsr points to in tname. */
    67         -      tname.z = (char*)zCsr;
    68         -      tname.n = len;
    69         -
    70         -      /* Advance zCsr to the next token. Store that token type in 'token',
    71         -      ** and its length in 'len' (to be used next iteration of this loop).
    72         -      */
    73         -      do {
    74         -        zCsr += len;
    75         -        len = sqlite3GetToken(zCsr, &token);
    76         -      } while( token==TK_SPACE );
    77         -      assert( len>0 || !*zCsr );
    78         -    } while( token!=TK_LP && token!=TK_USING );
    79         -
    80         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
    81         -       zSql, zTableName, tname.z+tname.n);
    82         -    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
    83         -  }
    84         -}
    85         -
    86         -/*
    87         -** This C function implements an SQL user function that is used by SQL code
    88         -** generated by the ALTER TABLE ... RENAME command to modify the definition
    89         -** of any foreign key constraints that use the table being renamed as the 
    90         -** parent table. It is passed three arguments:
    91         -**
    92         -**   1) The complete text of the CREATE TABLE statement being modified,
    93         -**   2) The old name of the table being renamed, and
    94         -**   3) The new name of the table being renamed.
    95         -**
    96         -** It returns the new CREATE TABLE statement. For example:
    97         -**
    98         -**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
    99         -**       -> 'CREATE TABLE t1(a REFERENCES t3)'
   100         -*/
   101         -#ifndef SQLITE_OMIT_FOREIGN_KEY
   102         -static void renameParentFunc(
   103         -  sqlite3_context *context,
   104         -  int NotUsed,
   105         -  sqlite3_value **argv
   106         -){
   107         -  sqlite3 *db = sqlite3_context_db_handle(context);
   108         -  char *zOutput = 0;
   109         -  char *zResult;
   110         -  unsigned char const *zInput = sqlite3_value_text(argv[0]);
   111         -  unsigned char const *zOld = sqlite3_value_text(argv[1]);
   112         -  unsigned char const *zNew = sqlite3_value_text(argv[2]);
   113         -
   114         -  unsigned const char *z;         /* Pointer to token */
   115         -  int n;                          /* Length of token z */
   116         -  int token;                      /* Type of token */
   117         -
   118         -  UNUSED_PARAMETER(NotUsed);
   119         -  if( zInput==0 || zOld==0 ) return;
   120         -  for(z=zInput; *z; z=z+n){
   121         -    n = sqlite3GetToken(z, &token);
   122         -    if( token==TK_REFERENCES ){
   123         -      char *zParent;
   124         -      do {
   125         -        z += n;
   126         -        n = sqlite3GetToken(z, &token);
   127         -      }while( token==TK_SPACE );
   128         -
   129         -      if( token==TK_ILLEGAL ) break;
   130         -      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
   131         -      if( zParent==0 ) break;
   132         -      sqlite3Dequote(zParent);
   133         -      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
   134         -        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
   135         -            (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
   136         -        );
   137         -        sqlite3DbFree(db, zOutput);
   138         -        zOutput = zOut;
   139         -        zInput = &z[n];
   140         -      }
   141         -      sqlite3DbFree(db, zParent);
   142         -    }
   143         -  }
   144         -
   145         -  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput);
   146         -  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
   147         -  sqlite3DbFree(db, zOutput);
   148         -}
   149         -#endif
   150         -
   151         -#ifndef SQLITE_OMIT_TRIGGER
   152         -/* This function is used by SQL generated to implement the
   153         -** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
   154         -** statement. The second is a table name. The table name in the CREATE 
   155         -** TRIGGER statement is replaced with the third argument and the result 
   156         -** returned. This is analagous to renameTableFunc() above, except for CREATE
   157         -** TRIGGER, not CREATE INDEX and CREATE TABLE.
   158         -*/
   159         -static void renameTriggerFunc(
   160         -  sqlite3_context *context,
   161         -  int NotUsed,
   162         -  sqlite3_value **argv
   163         -){
   164         -  unsigned char const *zSql = sqlite3_value_text(argv[0]);
   165         -  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
   166         -
   167         -  int token;
   168         -  Token tname;
   169         -  int dist = 3;
   170         -  unsigned char const *zCsr = zSql;
   171         -  int len = 0;
   172         -  char *zRet;
   173         -  sqlite3 *db = sqlite3_context_db_handle(context);
   174         -
   175         -  UNUSED_PARAMETER(NotUsed);
   176         -
   177         -  /* The principle used to locate the table name in the CREATE TRIGGER 
   178         -  ** statement is that the table name is the first token that is immediately
   179         -  ** preceded by either TK_ON or TK_DOT and immediately followed by one
   180         -  ** of TK_WHEN, TK_BEGIN or TK_FOR.
   181         -  */
   182         -  if( zSql ){
   183         -    do {
   184         -
   185         -      if( !*zCsr ){
   186         -        /* Ran out of input before finding the table name. Return NULL. */
   187         -        return;
   188         -      }
   189         -
   190         -      /* Store the token that zCsr points to in tname. */
   191         -      tname.z = (char*)zCsr;
   192         -      tname.n = len;
   193         -
   194         -      /* Advance zCsr to the next token. Store that token type in 'token',
   195         -      ** and its length in 'len' (to be used next iteration of this loop).
   196         -      */
   197         -      do {
   198         -        zCsr += len;
   199         -        len = sqlite3GetToken(zCsr, &token);
   200         -      }while( token==TK_SPACE );
   201         -      assert( len>0 || !*zCsr );
   202         -
   203         -      /* Variable 'dist' stores the number of tokens read since the most
   204         -      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
   205         -      ** token is read and 'dist' equals 2, the condition stated above
   206         -      ** to be met.
   207         -      **
   208         -      ** Note that ON cannot be a database, table or column name, so
   209         -      ** there is no need to worry about syntax like 
   210         -      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
   211         -      */
   212         -      dist++;
   213         -      if( token==TK_DOT || token==TK_ON ){
   214         -        dist = 0;
   215         -      }
   216         -    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
   217         -
   218         -    /* Variable tname now contains the token that is the old table-name
   219         -    ** in the CREATE TRIGGER statement.
   220         -    */
   221         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
   222         -       zSql, zTableName, tname.z+tname.n);
   223         -    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
   224         -  }
   225         -}
   226         -#endif   /* !SQLITE_OMIT_TRIGGER */
   227         -
   228         -/*
   229         -** Register built-in functions used to help implement ALTER TABLE
   230         -*/
   231         -void sqlite3AlterFunctions(void){
   232         -  static FuncDef aAlterTableFuncs[] = {
   233         -    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
   234         -#ifndef SQLITE_OMIT_TRIGGER
   235         -    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
   236         -#endif
   237         -#ifndef SQLITE_OMIT_FOREIGN_KEY
   238         -    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
   239         -#endif
   240         -  };
   241         -  sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
   242         -}
   243         -
   244         -/*
   245         -** This function is used to create the text of expressions of the form:
   246         -**
   247         -**   name=<constant1> OR name=<constant2> OR ...
   248         -**
   249         -** If argument zWhere is NULL, then a pointer string containing the text 
   250         -** "name=<constant>" is returned, where <constant> is the quoted version
   251         -** of the string passed as argument zConstant. The returned buffer is
   252         -** allocated using sqlite3DbMalloc(). It is the responsibility of the
   253         -** caller to ensure that it is eventually freed.
   254         -**
   255         -** If argument zWhere is not NULL, then the string returned is 
   256         -** "<where> OR name=<constant>", where <where> is the contents of zWhere.
   257         -** In this case zWhere is passed to sqlite3DbFree() before returning.
   258         -** 
   259         -*/
   260         -static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
   261         -  char *zNew;
   262         -  if( !zWhere ){
   263         -    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
   264         -  }else{
   265         -    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
   266         -    sqlite3DbFree(db, zWhere);
   267         -  }
   268         -  return zNew;
   269         -}
   270         -
   271         -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   272         -/*
   273         -** Generate the text of a WHERE expression which can be used to select all
   274         -** tables that have foreign key constraints that refer to table pTab (i.e.
   275         -** constraints for which pTab is the parent table) from the sqlite_master
   276         -** table.
   277         -*/
   278         -static char *whereForeignKeys(Parse *pParse, Table *pTab){
   279         -  FKey *p;
   280         -  char *zWhere = 0;
   281         -  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   282         -    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
   283         -  }
   284         -  return zWhere;
   285         -}
   286         -#endif
   287         -
   288         -/*
   289         -** Generate the text of a WHERE expression which can be used to select all
   290         -** temporary triggers on table pTab from the sqlite_temp_master table. If
   291         -** table pTab has no temporary triggers, or is itself stored in the 
   292         -** temporary database, NULL is returned.
   293         -*/
   294         -static char *whereTempTriggers(Parse *pParse, Table *pTab){
   295         -  Trigger *pTrig;
   296         -  char *zWhere = 0;
   297         -  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
   298         -
   299         -  /* If the table is not located in the temp-db (in which case NULL is 
   300         -  ** returned, loop through the tables list of triggers. For each trigger
   301         -  ** that is not part of the temp-db schema, add a clause to the WHERE 
   302         -  ** expression being built up in zWhere.
   303         -  */
   304         -  if( pTab->pSchema!=pTempSchema ){
   305         -    sqlite3 *db = pParse->db;
   306         -    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   307         -      if( pTrig->pSchema==pTempSchema ){
   308         -        zWhere = whereOrName(db, zWhere, pTrig->zName);
   309         -      }
   310         -    }
   311         -  }
   312         -  if( zWhere ){
   313         -    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
   314         -    sqlite3DbFree(pParse->db, zWhere);
   315         -    zWhere = zNew;
   316         -  }
   317         -  return zWhere;
   318         -}
   319         -
   320         -/*
   321         -** Generate code to drop and reload the internal representation of table
   322         -** pTab from the database, including triggers and temporary triggers.
   323         -** Argument zName is the name of the table in the database schema at
   324         -** the time the generated code is executed. This can be different from
   325         -** pTab->zName if this function is being called to code part of an 
   326         -** "ALTER TABLE RENAME TO" statement.
   327         -*/
   328         -static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
   329         -  Vdbe *v;
   330         -  char *zWhere;
   331         -  int iDb;                   /* Index of database containing pTab */
   332         -#ifndef SQLITE_OMIT_TRIGGER
   333         -  Trigger *pTrig;
   334         -#endif
   335         -
   336         -  v = sqlite3GetVdbe(pParse);
   337         -  if( NEVER(v==0) ) return;
   338         -  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   339         -  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   340         -  assert( iDb>=0 );
   341         -
   342         -#ifndef SQLITE_OMIT_TRIGGER
   343         -  /* Drop any table triggers from the internal schema. */
   344         -  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   345         -    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   346         -    assert( iTrigDb==iDb || iTrigDb==1 );
   347         -    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
   348         -  }
   349         -#endif
   350         -
   351         -  /* Drop the table and index from the internal schema.  */
   352         -  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
   353         -
   354         -  /* Reload the table, index and permanent trigger schemas. */
   355         -  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   356         -  if( !zWhere ) return;
   357         -  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
   358         -
   359         -#ifndef SQLITE_OMIT_TRIGGER
   360         -  /* Now, if the table is not stored in the temp database, reload any temp 
   361         -  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
   362         -  */
   363         -  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   364         -    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
   365         -  }
   366         -#endif
   367         -}
   368         -
   369     23   /*
   370     24   ** Parameter zName is the name of a table that is about to be altered
   371     25   ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
   372     26   ** If the table is a system table, this function leaves an error message
   373     27   ** in pParse->zErr (system tables may not be altered) and returns non-zero.
   374     28   **
   375     29   ** Or, if zName is not a system table, zero is returned.
................................................................................
   377     31   static int isSystemTable(Parse *pParse, const char *zName){
   378     32     if( 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   379     33       sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
   380     34       return 1;
   381     35     }
   382     36     return 0;
   383     37   }
           38  +
           39  +/*
           40  +** Generate code to verify that the schemas of database zDb and, if
           41  +** bTemp is not true, database "temp", can still be parsed. This is
           42  +** called at the end of the generation of an ALTER TABLE ... RENAME ...
           43  +** statement to ensure that the operation has not rendered any schema
           44  +** objects unusable.
           45  +*/
           46  +static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
           47  +  sqlite3NestedParse(pParse, 
           48  +      "SELECT 1 "
           49  +      "FROM \"%w\".%s "
           50  +      "WHERE name NOT LIKE 'sqlite_%%'"
           51  +      " AND sql NOT LIKE 'create virtual%%'"
           52  +      " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
           53  +      zDb, MASTER_NAME, 
           54  +      zDb, bTemp
           55  +  );
           56  +
           57  +  if( bTemp==0 ){
           58  +    sqlite3NestedParse(pParse, 
           59  +        "SELECT 1 "
           60  +        "FROM temp.%s "
           61  +        "WHERE name NOT LIKE 'sqlite_%%'"
           62  +        " AND sql NOT LIKE 'create virtual%%'"
           63  +        " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
           64  +        MASTER_NAME, zDb 
           65  +    );
           66  +  }
           67  +}
           68  +
           69  +/*
           70  +** Generate code to reload the schema for database iDb. And, if iDb!=1, for
           71  +** the temp database as well.
           72  +*/
           73  +static void renameReloadSchema(Parse *pParse, int iDb){
           74  +  Vdbe *v = pParse->pVdbe;
           75  +  if( v ){
           76  +    sqlite3ChangeCookie(pParse, iDb);
           77  +    sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
           78  +    if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
           79  +  }
           80  +}
   384     81   
   385     82   /*
   386     83   ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
   387     84   ** command. 
   388     85   */
   389     86   void sqlite3AlterRenameTable(
   390     87     Parse *pParse,            /* Parser context. */
................................................................................
   395     92     char *zDb;                /* Name of database iDb */
   396     93     Table *pTab;              /* Table being renamed */
   397     94     char *zName = 0;          /* NULL-terminated version of pName */ 
   398     95     sqlite3 *db = pParse->db; /* Database connection */
   399     96     int nTabName;             /* Number of UTF-8 characters in zTabName */
   400     97     const char *zTabName;     /* Original name of the table */
   401     98     Vdbe *v;
   402         -#ifndef SQLITE_OMIT_TRIGGER
   403         -  char *zWhere = 0;         /* Where clause to locate temp triggers */
   404         -#endif
   405     99     VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
   406    100     u32 savedDbFlags;         /* Saved value of db->mDbFlags */
   407    101   
   408    102     savedDbFlags = db->mDbFlags;  
   409    103     if( NEVER(db->mallocFailed) ) goto exit_rename_table;
   410    104     assert( pSrc->nSrc==1 );
   411    105     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
................................................................................
   470    164     ** schema). Open a statement transaction if the table is a virtual
   471    165     ** table.
   472    166     */
   473    167     v = sqlite3GetVdbe(pParse);
   474    168     if( v==0 ){
   475    169       goto exit_rename_table;
   476    170     }
   477         -  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
   478         -  sqlite3ChangeCookie(pParse, iDb);
          171  +
          172  +  /* figure out how many UTF-8 characters are in zName */
          173  +  zTabName = pTab->zName;
          174  +  nTabName = sqlite3Utf8CharLen(zTabName, -1);
          175  +
          176  +  /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
          177  +  ** the schema to use the new table name.  */
          178  +  sqlite3NestedParse(pParse, 
          179  +      "UPDATE \"%w\".%s SET "
          180  +      "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
          181  +      "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
          182  +      "AND   name NOT LIKE 'sqlite_%%'"
          183  +      , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
          184  +  );
          185  +
          186  +  /* Update the tbl_name and name columns of the sqlite_master table
          187  +  ** as required.  */
          188  +  sqlite3NestedParse(pParse,
          189  +      "UPDATE %Q.%s SET "
          190  +          "tbl_name = %Q, "
          191  +          "name = CASE "
          192  +            "WHEN type='table' THEN %Q "
          193  +            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
          194  +             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
          195  +            "ELSE name END "
          196  +      "WHERE tbl_name=%Q COLLATE nocase AND "
          197  +          "(type='table' OR type='index' OR type='trigger');", 
          198  +      zDb, MASTER_NAME, 
          199  +      zName, zName, zName, 
          200  +      nTabName, zTabName
          201  +  );
          202  +
          203  +#ifndef SQLITE_OMIT_AUTOINCREMENT
          204  +  /* If the sqlite_sequence table exists in this database, then update 
          205  +  ** it with the new table name.
          206  +  */
          207  +  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
          208  +    sqlite3NestedParse(pParse,
          209  +        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
          210  +        zDb, zName, pTab->zName);
          211  +  }
          212  +#endif
          213  +
          214  +  /* If the table being renamed is not itself part of the temp database,
          215  +  ** edit view and trigger definitions within the temp database 
          216  +  ** as required.  */
          217  +  if( iDb!=1 ){
          218  +    sqlite3NestedParse(pParse, 
          219  +        "UPDATE sqlite_temp_master SET "
          220  +            "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
          221  +            "tbl_name = "
          222  +              "CASE WHEN tbl_name=%Q COLLATE nocase AND "
          223  +              "          sqlite_rename_test(%Q, sql, type, name, 1) "
          224  +              "THEN %Q ELSE tbl_name END "
          225  +            "WHERE type IN ('view', 'trigger')"
          226  +        , zDb, zTabName, zName, zTabName, zDb, zName);
          227  +  }
   479    228   
   480    229     /* If this is a virtual table, invoke the xRename() function if
   481    230     ** one is defined. The xRename() callback will modify the names
   482    231     ** of any resources used by the v-table implementation (including other
   483    232     ** SQLite tables) that are identified by the name of the virtual table.
   484    233     */
   485    234   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
   487    236       int i = ++pParse->nMem;
   488    237       sqlite3VdbeLoadString(v, i, zName);
   489    238       sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
   490    239       sqlite3MayAbort(pParse);
   491    240     }
   492    241   #endif
   493    242   
   494         -  /* figure out how many UTF-8 characters are in zName */
   495         -  zTabName = pTab->zName;
   496         -  nTabName = sqlite3Utf8CharLen(zTabName, -1);
   497         -
   498         -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   499         -  if( db->flags&SQLITE_ForeignKeys ){
   500         -    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
   501         -    ** statements corresponding to all child tables of foreign key constraints
   502         -    ** for which the renamed table is the parent table.  */
   503         -    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
   504         -      sqlite3NestedParse(pParse, 
   505         -          "UPDATE \"%w\".%s SET "
   506         -              "sql = sqlite_rename_parent(sql, %Q, %Q) "
   507         -              "WHERE %s;", zDb, MASTER_NAME, zTabName, zName, zWhere);
   508         -      sqlite3DbFree(db, zWhere);
   509         -    }
   510         -  }
   511         -#endif
   512         -
   513         -  /* Modify the sqlite_master table to use the new table name. */
   514         -  sqlite3NestedParse(pParse,
   515         -      "UPDATE %Q.%s SET "
   516         -#ifdef SQLITE_OMIT_TRIGGER
   517         -          "sql = sqlite_rename_table(sql, %Q), "
   518         -#else
   519         -          "sql = CASE "
   520         -            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
   521         -            "ELSE sqlite_rename_table(sql, %Q) END, "
   522         -#endif
   523         -          "tbl_name = %Q, "
   524         -          "name = CASE "
   525         -            "WHEN type='table' THEN %Q "
   526         -            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
   527         -             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
   528         -            "ELSE name END "
   529         -      "WHERE tbl_name=%Q COLLATE nocase AND "
   530         -          "(type='table' OR type='index' OR type='trigger');", 
   531         -      zDb, MASTER_NAME, zName, zName, zName, 
   532         -#ifndef SQLITE_OMIT_TRIGGER
   533         -      zName,
   534         -#endif
   535         -      zName, nTabName, zTabName
   536         -  );
   537         -
   538         -#ifndef SQLITE_OMIT_AUTOINCREMENT
   539         -  /* If the sqlite_sequence table exists in this database, then update 
   540         -  ** it with the new table name.
   541         -  */
   542         -  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
   543         -    sqlite3NestedParse(pParse,
   544         -        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
   545         -        zDb, zName, pTab->zName);
   546         -  }
   547         -#endif
   548         -
   549         -#ifndef SQLITE_OMIT_TRIGGER
   550         -  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
   551         -  ** table. Don't do this if the table being ALTERed is itself located in
   552         -  ** the temp database.
   553         -  */
   554         -  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   555         -    sqlite3NestedParse(pParse, 
   556         -        "UPDATE sqlite_temp_master SET "
   557         -            "sql = sqlite_rename_trigger(sql, %Q), "
   558         -            "tbl_name = %Q "
   559         -            "WHERE %s;", zName, zName, zWhere);
   560         -    sqlite3DbFree(db, zWhere);
   561         -  }
   562         -#endif
   563         -
   564         -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   565         -  if( db->flags&SQLITE_ForeignKeys ){
   566         -    FKey *p;
   567         -    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   568         -      Table *pFrom = p->pFrom;
   569         -      if( pFrom!=pTab ){
   570         -        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
   571         -      }
   572         -    }
   573         -  }
   574         -#endif
   575         -
   576         -  /* Drop and reload the internal table schema. */
   577         -  reloadTableSchema(pParse, pTab, zName);
          243  +  renameReloadSchema(pParse, iDb);
          244  +  renameTestSchema(pParse, zDb, iDb==1);
   578    245   
   579    246   exit_rename_table:
   580    247     sqlite3SrcListDelete(db, pSrc);
   581    248     sqlite3DbFree(db, zName);
   582    249     db->mDbFlags = savedDbFlags;
   583    250   }
   584    251   
................................................................................
   596    263     int iDb;                  /* Database number */
   597    264     const char *zDb;          /* Database name */
   598    265     const char *zTab;         /* Table name */
   599    266     char *zCol;               /* Null-terminated column definition */
   600    267     Column *pCol;             /* The new column */
   601    268     Expr *pDflt;              /* Default value for the new column */
   602    269     sqlite3 *db;              /* The database connection; */
   603         -  Vdbe *v = pParse->pVdbe;  /* The prepared statement under construction */
          270  +  Vdbe *v;                  /* The prepared statement under construction */
   604    271     int r1;                   /* Temporary registers */
   605    272   
   606    273     db = pParse->db;
   607    274     if( pParse->nErr || db->mallocFailed ) return;
   608         -  assert( v!=0 );
   609    275     pNew = pParse->pNewTable;
   610    276     assert( pNew );
   611    277   
   612    278     assert( sqlite3BtreeHoldsAllMutexes(db) );
   613    279     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   614    280     zDb = db->aDb[iDb].zDbSName;
   615    281     zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
................................................................................
   696    362       db->mDbFlags = savedDbFlags;
   697    363     }
   698    364   
   699    365     /* Make sure the schema version is at least 3.  But do not upgrade
   700    366     ** from less than 3 to 4, as that will corrupt any preexisting DESC
   701    367     ** index.
   702    368     */
   703         -  r1 = sqlite3GetTempReg(pParse);
   704         -  sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
   705         -  sqlite3VdbeUsesBtree(v, iDb);
   706         -  sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
   707         -  sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
   708         -  VdbeCoverage(v);
   709         -  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
   710         -  sqlite3ReleaseTempReg(pParse, r1);
          369  +  v = sqlite3GetVdbe(pParse);
          370  +  if( v ){
          371  +    r1 = sqlite3GetTempReg(pParse);
          372  +    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
          373  +    sqlite3VdbeUsesBtree(v, iDb);
          374  +    sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
          375  +    sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
          376  +    VdbeCoverage(v);
          377  +    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
          378  +    sqlite3ReleaseTempReg(pParse, r1);
          379  +  }
   711    380   
   712         -  /* Reload the schema of the modified table. */
   713         -  reloadTableSchema(pParse, pTab, pTab->zName);
          381  +  /* Reload the table definition */
          382  +  renameReloadSchema(pParse, iDb);
   714    383   }
   715    384   
   716    385   /*
   717    386   ** This function is called by the parser after the table-name in
   718    387   ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
   719    388   ** pSrc is the full-name of the table being altered.
   720    389   **
................................................................................
   727    396   **
   728    397   ** Routine sqlite3AlterFinishAddColumn() will be called to complete
   729    398   ** coding the "ALTER TABLE ... ADD" statement.
   730    399   */
   731    400   void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
   732    401     Table *pNew;
   733    402     Table *pTab;
   734         -  Vdbe *v;
   735    403     int iDb;
   736    404     int i;
   737    405     int nAlloc;
   738    406     sqlite3 *db = pParse->db;
   739    407   
   740    408     /* Look up the table being altered. */
   741    409     assert( pParse->pNewTable==0 );
................................................................................
   791    459       pCol->zColl = 0;
   792    460       pCol->pDflt = 0;
   793    461     }
   794    462     pNew->pSchema = db->aDb[iDb].pSchema;
   795    463     pNew->addColOffset = pTab->addColOffset;
   796    464     pNew->nTabRef = 1;
   797    465   
   798         -  /* Begin a transaction and increment the schema cookie.  */
   799         -  sqlite3BeginWriteOperation(pParse, 0, iDb);
   800         -  v = sqlite3GetVdbe(pParse);
   801         -  if( !v ) goto exit_begin_add_column;
   802         -  sqlite3ChangeCookie(pParse, iDb);
   803         -
   804    466   exit_begin_add_column:
   805    467     sqlite3SrcListDelete(db, pSrc);
   806    468     return;
   807    469   }
          470  +
          471  +/*
          472  +** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
          473  +** command. This function checks if the table is a view or virtual
          474  +** table (columns of views or virtual tables may not be renamed). If so,
          475  +** it loads an error message into pParse and returns non-zero.
          476  +**
          477  +** Or, if pTab is not a view or virtual table, zero is returned.
          478  +*/
          479  +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
          480  +static int isRealTable(Parse *pParse, Table *pTab){
          481  +  const char *zType = 0;
          482  +#ifndef SQLITE_OMIT_VIEW
          483  +  if( pTab->pSelect ){
          484  +    zType = "view";
          485  +  }
          486  +#endif
          487  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          488  +  if( IsVirtual(pTab) ){
          489  +    zType = "virtual table";
          490  +  }
          491  +#endif
          492  +  if( zType ){
          493  +    sqlite3ErrorMsg(
          494  +        pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
          495  +    );
          496  +    return 1;
          497  +  }
          498  +  return 0;
          499  +}
          500  +#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
          501  +# define isRealTable(x,y) (0)
          502  +#endif
          503  +
          504  +/*
          505  +** Handles the following parser reduction:
          506  +**
          507  +**  cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
          508  +*/
          509  +void sqlite3AlterRenameColumn(
          510  +  Parse *pParse,                  /* Parsing context */
          511  +  SrcList *pSrc,                  /* Table being altered.  pSrc->nSrc==1 */
          512  +  Token *pOld,                    /* Name of column being changed */
          513  +  Token *pNew                     /* New column name */
          514  +){
          515  +  sqlite3 *db = pParse->db;       /* Database connection */
          516  +  Table *pTab;                    /* Table being updated */
          517  +  int iCol;                       /* Index of column being renamed */
          518  +  char *zOld = 0;                 /* Old column name */
          519  +  char *zNew = 0;                 /* New column name */
          520  +  const char *zDb;                /* Name of schema containing the table */
          521  +  int iSchema;                    /* Index of the schema */
          522  +  int bQuote;                     /* True to quote the new name */
          523  +
          524  +  /* Locate the table to be altered */
          525  +  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
          526  +  if( !pTab ) goto exit_rename_column;
          527  +
          528  +  /* Cannot alter a system table */
          529  +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ) goto exit_rename_column;
          530  +  if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
          531  +
          532  +  /* Which schema holds the table to be altered */  
          533  +  iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
          534  +  assert( iSchema>=0 );
          535  +  zDb = db->aDb[iSchema].zDbSName;
          536  +
          537  +#ifndef SQLITE_OMIT_AUTHORIZATION
          538  +  /* Invoke the authorization callback. */
          539  +  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
          540  +    goto exit_rename_column;
          541  +  }
          542  +#endif
          543  +
          544  +  /* Make sure the old name really is a column name in the table to be
          545  +  ** altered.  Set iCol to be the index of the column being renamed */
          546  +  zOld = sqlite3NameFromToken(db, pOld);
          547  +  if( !zOld ) goto exit_rename_column;
          548  +  for(iCol=0; iCol<pTab->nCol; iCol++){
          549  +    if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
          550  +  }
          551  +  if( iCol==pTab->nCol ){
          552  +    sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
          553  +    goto exit_rename_column;
          554  +  }
          555  +
          556  +  /* Do the rename operation using a recursive UPDATE statement that
          557  +  ** uses the sqlite_rename_column() SQL function to compute the new
          558  +  ** CREATE statement text for the sqlite_master table.
          559  +  */
          560  +  zNew = sqlite3NameFromToken(db, pNew);
          561  +  if( !zNew ) goto exit_rename_column;
          562  +  assert( pNew->n>0 );
          563  +  bQuote = sqlite3Isquote(pNew->z[0]);
          564  +  sqlite3NestedParse(pParse, 
          565  +      "UPDATE \"%w\".%s SET "
          566  +      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
          567  +      "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)"
          568  +      " AND sql NOT LIKE 'create virtual%%'",
          569  +      zDb, MASTER_NAME, 
          570  +      zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
          571  +      pTab->zName
          572  +  );
          573  +
          574  +  sqlite3NestedParse(pParse, 
          575  +      "UPDATE temp.%s SET "
          576  +      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
          577  +      "WHERE type IN ('trigger', 'view')",
          578  +      MASTER_NAME, 
          579  +      zDb, pTab->zName, iCol, zNew, bQuote
          580  +  );
          581  +
          582  +  /* Drop and reload the database schema. */
          583  +  renameReloadSchema(pParse, iSchema);
          584  +  renameTestSchema(pParse, zDb, iSchema==1);
          585  +
          586  + exit_rename_column:
          587  +  sqlite3SrcListDelete(db, pSrc);
          588  +  sqlite3DbFree(db, zOld);
          589  +  sqlite3DbFree(db, zNew);
          590  +  return;
          591  +}
          592  +
          593  +/*
          594  +** Each RenameToken object maps an element of the parse tree into
          595  +** the token that generated that element.  The parse tree element
          596  +** might be one of:
          597  +**
          598  +**     *  A pointer to an Expr that represents an ID
          599  +**     *  The name of a table column in Column.zName
          600  +**
          601  +** A list of RenameToken objects can be constructed during parsing.
          602  +** Each new object is created by sqlite3RenameTokenMap().
          603  +** As the parse tree is transformed, the sqlite3RenameTokenRemap()
          604  +** routine is used to keep the mapping current.
          605  +**
          606  +** After the parse finishes, renameTokenFind() routine can be used
          607  +** to look up the actual token value that created some element in
          608  +** the parse tree.
          609  +*/
          610  +struct RenameToken {
          611  +  void *p;               /* Parse tree element created by token t */
          612  +  Token t;               /* The token that created parse tree element p */
          613  +  RenameToken *pNext;    /* Next is a list of all RenameToken objects */
          614  +};
          615  +
          616  +/*
          617  +** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
          618  +** down into the Walker.
          619  +*/
          620  +typedef struct RenameCtx RenameCtx;
          621  +struct RenameCtx {
          622  +  RenameToken *pList;             /* List of tokens to overwrite */
          623  +  int nList;                      /* Number of tokens in pList */
          624  +  int iCol;                       /* Index of column being renamed */
          625  +  Table *pTab;                    /* Table being ALTERed */ 
          626  +  const char *zOld;               /* Old column name */
          627  +};
          628  +
          629  +#ifdef SQLITE_DEBUG
          630  +/*
          631  +** This function is only for debugging. It performs two tasks:
          632  +**
          633  +**   1. Checks that pointer pPtr does not already appear in the 
          634  +**      rename-token list.
          635  +**
          636  +**   2. Dereferences each pointer in the rename-token list.
          637  +**
          638  +** The second is most effective when debugging under valgrind or
          639  +** address-sanitizer or similar. If any of these pointers no longer 
          640  +** point to valid objects, an exception is raised by the memory-checking 
          641  +** tool.
          642  +**
          643  +** The point of this is to prevent comparisons of invalid pointer values.
          644  +** Even though this always seems to work, it is undefined according to the
          645  +** C standard. Example of undefined comparison:
          646  +**
          647  +**     sqlite3_free(x);
          648  +**     if( x==y ) ...
          649  +**
          650  +** Technically, as x no longer points into a valid object or to the byte
          651  +** following a valid object, it may not be used in comparison operations.
          652  +*/
          653  +static void renameTokenCheckAll(Parse *pParse, void *pPtr){
          654  +  if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
          655  +    RenameToken *p;
          656  +    u8 i = 0;
          657  +    for(p=pParse->pRename; p; p=p->pNext){
          658  +      if( p->p ){
          659  +        assert( p->p!=pPtr );
          660  +        i += *(u8*)(p->p);
          661  +      }
          662  +    }
          663  +  }
          664  +}
          665  +#else
          666  +# define renameTokenCheckAll(x,y)
          667  +#endif
          668  +
          669  +/*
          670  +** Add a new RenameToken object mapping parse tree element pPtr into
          671  +** token *pToken to the Parse object currently under construction.
          672  +**
          673  +** Return a copy of pPtr.
          674  +*/
          675  +void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
          676  +  RenameToken *pNew;
          677  +  assert( pPtr || pParse->db->mallocFailed );
          678  +  renameTokenCheckAll(pParse, pPtr);
          679  +  pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
          680  +  if( pNew ){
          681  +    pNew->p = pPtr;
          682  +    pNew->t = *pToken;
          683  +    pNew->pNext = pParse->pRename;
          684  +    pParse->pRename = pNew;
          685  +  }
          686  +
          687  +  return pPtr;
          688  +}
          689  +
          690  +/*
          691  +** It is assumed that there is already a RenameToken object associated
          692  +** with parse tree element pFrom. This function remaps the associated token
          693  +** to parse tree element pTo.
          694  +*/
          695  +void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
          696  +  RenameToken *p;
          697  +  renameTokenCheckAll(pParse, pTo);
          698  +  for(p=pParse->pRename; p; p=p->pNext){
          699  +    if( p->p==pFrom ){
          700  +      p->p = pTo;
          701  +      break;
          702  +    }
          703  +  }
          704  +}
          705  +
          706  +/*
          707  +** Walker callback used by sqlite3RenameExprUnmap().
          708  +*/
          709  +static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
          710  +  Parse *pParse = pWalker->pParse;
          711  +  sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
          712  +  return WRC_Continue;
          713  +}
          714  +
          715  +/*
          716  +** Remove all nodes that are part of expression pExpr from the rename list.
          717  +*/
          718  +void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
          719  +  Walker sWalker;
          720  +  memset(&sWalker, 0, sizeof(Walker));
          721  +  sWalker.pParse = pParse;
          722  +  sWalker.xExprCallback = renameUnmapExprCb;
          723  +  sqlite3WalkExpr(&sWalker, pExpr);
          724  +}
          725  +
          726  +/*
          727  +** Remove all nodes that are part of expression-list pEList from the 
          728  +** rename list.
          729  +*/
          730  +void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
          731  +  if( pEList ){
          732  +    int i;
          733  +    Walker sWalker;
          734  +    memset(&sWalker, 0, sizeof(Walker));
          735  +    sWalker.pParse = pParse;
          736  +    sWalker.xExprCallback = renameUnmapExprCb;
          737  +    sqlite3WalkExprList(&sWalker, pEList);
          738  +    for(i=0; i<pEList->nExpr; i++){
          739  +      sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zName);
          740  +    }
          741  +  }
          742  +}
          743  +
          744  +/*
          745  +** Free the list of RenameToken objects given in the second argument
          746  +*/
          747  +static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
          748  +  RenameToken *pNext;
          749  +  RenameToken *p;
          750  +  for(p=pToken; p; p=pNext){
          751  +    pNext = p->pNext;
          752  +    sqlite3DbFree(db, p);
          753  +  }
          754  +}
          755  +
          756  +/*
          757  +** Search the Parse object passed as the first argument for a RenameToken
          758  +** object associated with parse tree element pPtr. If found, remove it
          759  +** from the Parse object and add it to the list maintained by the
          760  +** RenameCtx object passed as the second argument.
          761  +*/
          762  +static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
          763  +  RenameToken **pp;
          764  +  assert( pPtr!=0 );
          765  +  for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
          766  +    if( (*pp)->p==pPtr ){
          767  +      RenameToken *pToken = *pp;
          768  +      *pp = pToken->pNext;
          769  +      pToken->pNext = pCtx->pList;
          770  +      pCtx->pList = pToken;
          771  +      pCtx->nList++;
          772  +      break;
          773  +    }
          774  +  }
          775  +}
          776  +
          777  +/*
          778  +** This is a Walker select callback. It does nothing. It is only required
          779  +** because without a dummy callback, sqlite3WalkExpr() and similar do not
          780  +** descend into sub-select statements.
          781  +*/
          782  +static int renameColumnSelectCb(Walker *pWalker, Select *p){
          783  +  UNUSED_PARAMETER(pWalker);
          784  +  UNUSED_PARAMETER(p);
          785  +  return WRC_Continue;
          786  +}
          787  +
          788  +/*
          789  +** This is a Walker expression callback.
          790  +**
          791  +** For every TK_COLUMN node in the expression tree, search to see
          792  +** if the column being references is the column being renamed by an
          793  +** ALTER TABLE statement.  If it is, then attach its associated
          794  +** RenameToken object to the list of RenameToken objects being
          795  +** constructed in RenameCtx object at pWalker->u.pRename.
          796  +*/
          797  +static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
          798  +  RenameCtx *p = pWalker->u.pRename;
          799  +  if( pExpr->op==TK_TRIGGER 
          800  +   && pExpr->iColumn==p->iCol 
          801  +   && pWalker->pParse->pTriggerTab==p->pTab
          802  +  ){
          803  +    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
          804  +  }else if( pExpr->op==TK_COLUMN 
          805  +   && pExpr->iColumn==p->iCol 
          806  +   && p->pTab==pExpr->y.pTab
          807  +  ){
          808  +    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
          809  +  }
          810  +  return WRC_Continue;
          811  +}
          812  +
          813  +/*
          814  +** The RenameCtx contains a list of tokens that reference a column that
          815  +** is being renamed by an ALTER TABLE statement.  Return the "last"
          816  +** RenameToken in the RenameCtx and remove that RenameToken from the
          817  +** RenameContext.  "Last" means the last RenameToken encountered when
          818  +** the input SQL is parsed from left to right.  Repeated calls to this routine
          819  +** return all column name tokens in the order that they are encountered
          820  +** in the SQL statement.
          821  +*/
          822  +static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
          823  +  RenameToken *pBest = pCtx->pList;
          824  +  RenameToken *pToken;
          825  +  RenameToken **pp;
          826  +
          827  +  for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
          828  +    if( pToken->t.z>pBest->t.z ) pBest = pToken;
          829  +  }
          830  +  for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
          831  +  *pp = pBest->pNext;
          832  +
          833  +  return pBest;
          834  +}
          835  +
          836  +/*
          837  +** An error occured while parsing or otherwise processing a database
          838  +** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
          839  +** ALTER TABLE RENAME COLUMN program. The error message emitted by the
          840  +** sub-routine is currently stored in pParse->zErrMsg. This function
          841  +** adds context to the error message and then stores it in pCtx.
          842  +*/
          843  +static void renameColumnParseError(
          844  +  sqlite3_context *pCtx, 
          845  +  int bPost,
          846  +  sqlite3_value *pType,
          847  +  sqlite3_value *pObject,
          848  +  Parse *pParse
          849  +){
          850  +  const char *zT = (const char*)sqlite3_value_text(pType);
          851  +  const char *zN = (const char*)sqlite3_value_text(pObject);
          852  +  char *zErr;
          853  +
          854  +  zErr = sqlite3_mprintf("error in %s %s%s: %s", 
          855  +      zT, zN, (bPost ? " after rename" : ""),
          856  +      pParse->zErrMsg
          857  +  );
          858  +  sqlite3_result_error(pCtx, zErr, -1);
          859  +  sqlite3_free(zErr);
          860  +}
          861  +
          862  +/*
          863  +** For each name in the the expression-list pEList (i.e. each
          864  +** pEList->a[i].zName) that matches the string in zOld, extract the 
          865  +** corresponding rename-token from Parse object pParse and add it
          866  +** to the RenameCtx pCtx.
          867  +*/
          868  +static void renameColumnElistNames(
          869  +  Parse *pParse, 
          870  +  RenameCtx *pCtx, 
          871  +  ExprList *pEList, 
          872  +  const char *zOld
          873  +){
          874  +  if( pEList ){
          875  +    int i;
          876  +    for(i=0; i<pEList->nExpr; i++){
          877  +      char *zName = pEList->a[i].zName;
          878  +      if( 0==sqlite3_stricmp(zName, zOld) ){
          879  +        renameTokenFind(pParse, pCtx, (void*)zName);
          880  +      }
          881  +    }
          882  +  }
          883  +}
          884  +
          885  +/*
          886  +** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName) 
          887  +** that matches the string in zOld, extract the corresponding rename-token 
          888  +** from Parse object pParse and add it to the RenameCtx pCtx.
          889  +*/
          890  +static void renameColumnIdlistNames(
          891  +  Parse *pParse, 
          892  +  RenameCtx *pCtx, 
          893  +  IdList *pIdList, 
          894  +  const char *zOld
          895  +){
          896  +  if( pIdList ){
          897  +    int i;
          898  +    for(i=0; i<pIdList->nId; i++){
          899  +      char *zName = pIdList->a[i].zName;
          900  +      if( 0==sqlite3_stricmp(zName, zOld) ){
          901  +        renameTokenFind(pParse, pCtx, (void*)zName);
          902  +      }
          903  +    }
          904  +  }
          905  +}
          906  +
          907  +/*
          908  +** Parse the SQL statement zSql using Parse object (*p). The Parse object
          909  +** is initialized by this function before it is used.
          910  +*/
          911  +static int renameParseSql(
          912  +  Parse *p,                       /* Memory to use for Parse object */
          913  +  const char *zDb,                /* Name of schema SQL belongs to */
          914  +  int bTable,                     /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
          915  +  sqlite3 *db,                    /* Database handle */
          916  +  const char *zSql,               /* SQL to parse */
          917  +  int bTemp                       /* True if SQL is from temp schema */
          918  +){
          919  +  int rc;
          920  +  char *zErr = 0;
          921  +
          922  +  db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
          923  +
          924  +  /* Parse the SQL statement passed as the first argument. If no error
          925  +  ** occurs and the parse does not result in a new table, index or
          926  +  ** trigger object, the database must be corrupt. */
          927  +  memset(p, 0, sizeof(Parse));
          928  +  p->eParseMode = (bTable ? PARSE_MODE_RENAME_TABLE : PARSE_MODE_RENAME_COLUMN);
          929  +  p->db = db;
          930  +  p->nQueryLoop = 1;
          931  +  rc = sqlite3RunParser(p, zSql, &zErr);
          932  +  assert( p->zErrMsg==0 );
          933  +  assert( rc!=SQLITE_OK || zErr==0 );
          934  +  assert( (0!=p->pNewTable) + (0!=p->pNewIndex) + (0!=p->pNewTrigger)<2 );
          935  +  p->zErrMsg = zErr;
          936  +  if( db->mallocFailed ) rc = SQLITE_NOMEM;
          937  +  if( rc==SQLITE_OK 
          938  +   && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 
          939  +  ){
          940  +    rc = SQLITE_CORRUPT_BKPT;
          941  +  }
          942  +
          943  +#ifdef SQLITE_DEBUG
          944  +  /* Ensure that all mappings in the Parse.pRename list really do map to
          945  +  ** a part of the input string.  */
          946  +  if( rc==SQLITE_OK ){
          947  +    int nSql = sqlite3Strlen30(zSql);
          948  +    RenameToken *pToken;
          949  +    for(pToken=p->pRename; pToken; pToken=pToken->pNext){
          950  +      assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
          951  +    }
          952  +  }
          953  +#endif
          954  +
          955  +  db->init.iDb = 0;
          956  +  return rc;
          957  +}
          958  +
          959  +/*
          960  +** This function edits SQL statement zSql, replacing each token identified
          961  +** by the linked list pRename with the text of zNew. If argument bQuote is
          962  +** true, then zNew is always quoted first. If no error occurs, the result
          963  +** is loaded into context object pCtx as the result.
          964  +**
          965  +** Or, if an error occurs (i.e. an OOM condition), an error is left in
          966  +** pCtx and an SQLite error code returned.
          967  +*/
          968  +static int renameEditSql(
          969  +  sqlite3_context *pCtx,          /* Return result here */
          970  +  RenameCtx *pRename,             /* Rename context */
          971  +  const char *zSql,               /* SQL statement to edit */
          972  +  const char *zNew,               /* New token text */
          973  +  int bQuote                      /* True to always quote token */
          974  +){
          975  +  int nNew = sqlite3Strlen30(zNew);
          976  +  int nSql = sqlite3Strlen30(zSql);
          977  +  sqlite3 *db = sqlite3_context_db_handle(pCtx);
          978  +  int rc = SQLITE_OK;
          979  +  char *zQuot;
          980  +  char *zOut;
          981  +  int nQuot;
          982  +
          983  +  /* Set zQuot to point to a buffer containing a quoted copy of the 
          984  +  ** identifier zNew. If the corresponding identifier in the original 
          985  +  ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
          986  +  ** point to zQuot so that all substitutions are made using the
          987  +  ** quoted version of the new column name.  */
          988  +  zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
          989  +  if( zQuot==0 ){
          990  +    return SQLITE_NOMEM;
          991  +  }else{
          992  +    nQuot = sqlite3Strlen30(zQuot);
          993  +  }
          994  +  if( bQuote ){
          995  +    zNew = zQuot;
          996  +    nNew = nQuot;
          997  +  }
          998  +
          999  +  /* At this point pRename->pList contains a list of RenameToken objects
         1000  +  ** corresponding to all tokens in the input SQL that must be replaced
         1001  +  ** with the new column name. All that remains is to construct and
         1002  +  ** return the edited SQL string. */
         1003  +  assert( nQuot>=nNew );
         1004  +  zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
         1005  +  if( zOut ){
         1006  +    int nOut = nSql;
         1007  +    memcpy(zOut, zSql, nSql);
         1008  +    while( pRename->pList ){
         1009  +      int iOff;                   /* Offset of token to replace in zOut */
         1010  +      RenameToken *pBest = renameColumnTokenNext(pRename);
         1011  +
         1012  +      u32 nReplace;
         1013  +      const char *zReplace;
         1014  +      if( sqlite3IsIdChar(*pBest->t.z) ){
         1015  +        nReplace = nNew;
         1016  +        zReplace = zNew;
         1017  +      }else{
         1018  +        nReplace = nQuot;
         1019  +        zReplace = zQuot;
         1020  +      }
         1021  +
         1022  +      iOff = pBest->t.z - zSql;
         1023  +      if( pBest->t.n!=nReplace ){
         1024  +        memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n], 
         1025  +            nOut - (iOff + pBest->t.n)
         1026  +        );
         1027  +        nOut += nReplace - pBest->t.n;
         1028  +        zOut[nOut] = '\0';
         1029  +      }
         1030  +      memcpy(&zOut[iOff], zReplace, nReplace);
         1031  +      sqlite3DbFree(db, pBest);
         1032  +    }
         1033  +
         1034  +    sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
         1035  +    sqlite3DbFree(db, zOut);
         1036  +  }else{
         1037  +    rc = SQLITE_NOMEM;
         1038  +  }
         1039  +
         1040  +  sqlite3_free(zQuot);
         1041  +  return rc;
         1042  +}
         1043  +
         1044  +/*
         1045  +** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
         1046  +** it was read from the schema of database zDb. Return SQLITE_OK if 
         1047  +** successful. Otherwise, return an SQLite error code and leave an error
         1048  +** message in the Parse object.
         1049  +*/
         1050  +static int renameResolveTrigger(Parse *pParse, const char *zDb){
         1051  +  sqlite3 *db = pParse->db;
         1052  +  Trigger *pNew = pParse->pNewTrigger;
         1053  +  TriggerStep *pStep;
         1054  +  NameContext sNC;
         1055  +  int rc = SQLITE_OK;
         1056  +
         1057  +  memset(&sNC, 0, sizeof(sNC));
         1058  +  sNC.pParse = pParse;
         1059  +  assert( pNew->pTabSchema );
         1060  +  pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
         1061  +      db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
         1062  +  );
         1063  +  pParse->eTriggerOp = pNew->op;
         1064  +
         1065  +  /* Resolve symbols in WHEN clause */
         1066  +  if( pNew->pWhen ){
         1067  +    rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
         1068  +  }
         1069  +
         1070  +  for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
         1071  +    if( pStep->pSelect ){
         1072  +      sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
         1073  +      if( pParse->nErr ) rc = pParse->rc;
         1074  +    }
         1075  +    if( rc==SQLITE_OK && pStep->zTarget ){
         1076  +      Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
         1077  +      if( pTarget==0 ){
         1078  +        rc = SQLITE_ERROR;
         1079  +      }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
         1080  +        SrcList sSrc;
         1081  +        memset(&sSrc, 0, sizeof(sSrc));
         1082  +        sSrc.nSrc = 1;
         1083  +        sSrc.a[0].zName = pStep->zTarget;
         1084  +        sSrc.a[0].pTab = pTarget;
         1085  +        sNC.pSrcList = &sSrc;
         1086  +        if( pStep->pWhere ){
         1087  +          rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
         1088  +        }
         1089  +        if( rc==SQLITE_OK ){
         1090  +          rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
         1091  +        }
         1092  +        assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
         1093  +        if( pStep->pUpsert ){
         1094  +          Upsert *pUpsert = pStep->pUpsert;
         1095  +          assert( rc==SQLITE_OK );
         1096  +          pUpsert->pUpsertSrc = &sSrc;
         1097  +          sNC.uNC.pUpsert = pUpsert;
         1098  +          sNC.ncFlags = NC_UUpsert;
         1099  +          rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
         1100  +          if( rc==SQLITE_OK ){
         1101  +            ExprList *pUpsertSet = pUpsert->pUpsertSet;
         1102  +            rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
         1103  +          }
         1104  +          if( rc==SQLITE_OK ){
         1105  +            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
         1106  +          }
         1107  +          if( rc==SQLITE_OK ){
         1108  +            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
         1109  +          }
         1110  +          sNC.ncFlags = 0;
         1111  +        }
         1112  +      }
         1113  +    }
         1114  +  }
         1115  +  return rc;
         1116  +}
         1117  +
         1118  +/*
         1119  +** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
         1120  +** objects that are part of the trigger passed as the second argument.
         1121  +*/
         1122  +static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
         1123  +  TriggerStep *pStep;
         1124  +
         1125  +  /* Find tokens to edit in WHEN clause */
         1126  +  sqlite3WalkExpr(pWalker, pTrigger->pWhen);
         1127  +
         1128  +  /* Find tokens to edit in trigger steps */
         1129  +  for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
         1130  +    sqlite3WalkSelect(pWalker, pStep->pSelect);
         1131  +    sqlite3WalkExpr(pWalker, pStep->pWhere);
         1132  +    sqlite3WalkExprList(pWalker, pStep->pExprList);
         1133  +    if( pStep->pUpsert ){
         1134  +      Upsert *pUpsert = pStep->pUpsert;
         1135  +      sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
         1136  +      sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
         1137  +      sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
         1138  +      sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
         1139  +    }
         1140  +  }
         1141  +}
         1142  +
         1143  +/*
         1144  +** Free the contents of Parse object (*pParse). Do not free the memory
         1145  +** occupied by the Parse object itself.
         1146  +*/
         1147  +static void renameParseCleanup(Parse *pParse){
         1148  +  sqlite3 *db = pParse->db;
         1149  +  if( pParse->pVdbe ){
         1150  +    sqlite3VdbeFinalize(pParse->pVdbe);
         1151  +  }
         1152  +  sqlite3DeleteTable(db, pParse->pNewTable);
         1153  +  if( pParse->pNewIndex ) sqlite3FreeIndex(db, pParse->pNewIndex);
         1154  +  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
         1155  +  sqlite3DbFree(db, pParse->zErrMsg);
         1156  +  renameTokenFree(db, pParse->pRename);
         1157  +  sqlite3ParserReset(pParse);
         1158  +}
         1159  +
         1160  +/*
         1161  +** SQL function:
         1162  +**
         1163  +**     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
         1164  +**
         1165  +**   0. zSql:     SQL statement to rewrite
         1166  +**   1. type:     Type of object ("table", "view" etc.)
         1167  +**   2. object:   Name of object
         1168  +**   3. Database: Database name (e.g. "main")
         1169  +**   4. Table:    Table name
         1170  +**   5. iCol:     Index of column to rename
         1171  +**   6. zNew:     New column name
         1172  +**   7. bQuote:   Non-zero if the new column name should be quoted.
         1173  +**   8. bTemp:    True if zSql comes from temp schema
         1174  +**
         1175  +** Do a column rename operation on the CREATE statement given in zSql.
         1176  +** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
         1177  +** into zNew.  The name should be quoted if bQuote is true.
         1178  +**
         1179  +** This function is used internally by the ALTER TABLE RENAME COLUMN command.
         1180  +** Though accessible to application code, it is not intended for use by
         1181  +** applications.  The existance of this function, and the way it works,
         1182  +** is subject to change without notice.
         1183  +**
         1184  +** If any of the parameters are out-of-bounds, then simply return NULL.
         1185  +** An out-of-bounds parameter can only occur when the application calls
         1186  +** this function directly.  The parameters will always be well-formed when
         1187  +** this routine is invoked by the bytecode for a legitimate ALTER TABLE
         1188  +** statement.
         1189  +*/
         1190  +static void renameColumnFunc(
         1191  +  sqlite3_context *context,
         1192  +  int NotUsed,
         1193  +  sqlite3_value **argv
         1194  +){
         1195  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1196  +  RenameCtx sCtx;
         1197  +  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
         1198  +  const char *zDb = (const char*)sqlite3_value_text(argv[3]);
         1199  +  const char *zTable = (const char*)sqlite3_value_text(argv[4]);
         1200  +  int iCol = sqlite3_value_int(argv[5]);
         1201  +  const char *zNew = (const char*)sqlite3_value_text(argv[6]);
         1202  +  int bQuote = sqlite3_value_int(argv[7]);
         1203  +  int bTemp = sqlite3_value_int(argv[8]);
         1204  +  const char *zOld;
         1205  +  int rc;
         1206  +  Parse sParse;
         1207  +  Walker sWalker;
         1208  +  Index *pIdx;
         1209  +  int i;
         1210  +  Table *pTab;
         1211  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1212  +  sqlite3_xauth xAuth = db->xAuth;
         1213  +#endif
         1214  +
         1215  +  UNUSED_PARAMETER(NotUsed);
         1216  +  if( zSql==0 ) return;
         1217  +  if( zTable==0 ) return;
         1218  +  if( zNew==0 ) return;
         1219  +  if( iCol<0 ) return;
         1220  +  sqlite3BtreeEnterAll(db);
         1221  +  pTab = sqlite3FindTable(db, zTable, zDb);
         1222  +  if( pTab==0 || iCol>=pTab->nCol ){
         1223  +    sqlite3BtreeLeaveAll(db);
         1224  +    return;
         1225  +  }
         1226  +  zOld = pTab->aCol[iCol].zName;
         1227  +  memset(&sCtx, 0, sizeof(sCtx));
         1228  +  sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
         1229  +
         1230  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1231  +  db->xAuth = 0;
         1232  +#endif
         1233  +  rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
         1234  +
         1235  +  /* Find tokens that need to be replaced. */
         1236  +  memset(&sWalker, 0, sizeof(Walker));
         1237  +  sWalker.pParse = &sParse;
         1238  +  sWalker.xExprCallback = renameColumnExprCb;
         1239  +  sWalker.xSelectCallback = renameColumnSelectCb;
         1240  +  sWalker.u.pRename = &sCtx;
         1241  +
         1242  +  sCtx.pTab = pTab;
         1243  +  if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
         1244  +  if( sParse.pNewTable ){
         1245  +    Select *pSelect = sParse.pNewTable->pSelect;
         1246  +    if( pSelect ){
         1247  +      sParse.rc = SQLITE_OK;
         1248  +      sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0);
         1249  +      rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
         1250  +      if( rc==SQLITE_OK ){
         1251  +        sqlite3WalkSelect(&sWalker, pSelect);
         1252  +      }
         1253  +      if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
         1254  +    }else{
         1255  +      /* A regular table */
         1256  +      int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
         1257  +      FKey *pFKey;
         1258  +      assert( sParse.pNewTable->pSelect==0 );
         1259  +      sCtx.pTab = sParse.pNewTable;
         1260  +      if( bFKOnly==0 ){
         1261  +        renameTokenFind(
         1262  +            &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
         1263  +        );
         1264  +        if( sCtx.iCol<0 ){
         1265  +          renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
         1266  +        }
         1267  +        sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
         1268  +        for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
         1269  +          sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
         1270  +        }
         1271  +      }
         1272  +
         1273  +      for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
         1274  +        for(i=0; i<pFKey->nCol; i++){
         1275  +          if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
         1276  +            renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
         1277  +          }
         1278  +          if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
         1279  +           && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
         1280  +          ){
         1281  +            renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
         1282  +          }
         1283  +        }
         1284  +      }
         1285  +    }
         1286  +  }else if( sParse.pNewIndex ){
         1287  +    sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
         1288  +    sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
         1289  +  }else{
         1290  +    /* A trigger */
         1291  +    TriggerStep *pStep;
         1292  +    rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
         1293  +    if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
         1294  +
         1295  +    for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
         1296  +      if( pStep->zTarget ){ 
         1297  +        Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
         1298  +        if( pTarget==pTab ){
         1299  +          if( pStep->pUpsert ){
         1300  +            ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
         1301  +            renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
         1302  +          }
         1303  +          renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
         1304  +          renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
         1305  +        }
         1306  +      }
         1307  +    }
         1308  +
         1309  +
         1310  +    /* Find tokens to edit in UPDATE OF clause */
         1311  +    if( sParse.pTriggerTab==pTab ){
         1312  +      renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
         1313  +    }
         1314  +
         1315  +    /* Find tokens to edit in various expressions and selects */
         1316  +    renameWalkTrigger(&sWalker, sParse.pNewTrigger);
         1317  +  }
         1318  +
         1319  +  assert( rc==SQLITE_OK );
         1320  +  rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
         1321  +
         1322  +renameColumnFunc_done:
         1323  +  if( rc!=SQLITE_OK ){
         1324  +    if( sParse.zErrMsg ){
         1325  +      renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
         1326  +    }else{
         1327  +      sqlite3_result_error_code(context, rc);
         1328  +    }
         1329  +  }
         1330  +
         1331  +  renameParseCleanup(&sParse);
         1332  +  renameTokenFree(db, sCtx.pList);
         1333  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1334  +  db->xAuth = xAuth;
         1335  +#endif
         1336  +  sqlite3BtreeLeaveAll(db);
         1337  +}
         1338  +
         1339  +/*
         1340  +** Walker expression callback used by "RENAME TABLE". 
         1341  +*/
         1342  +static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
         1343  +  RenameCtx *p = pWalker->u.pRename;
         1344  +  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
         1345  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
         1346  +  }
         1347  +  return WRC_Continue;
         1348  +}
         1349  +
         1350  +/*
         1351  +** Walker select callback used by "RENAME TABLE". 
         1352  +*/
         1353  +static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
         1354  +  int i;
         1355  +  RenameCtx *p = pWalker->u.pRename;
         1356  +  SrcList *pSrc = pSelect->pSrc;
         1357  +  for(i=0; i<pSrc->nSrc; i++){
         1358  +    struct SrcList_item *pItem = &pSrc->a[i];
         1359  +    if( pItem->pTab==p->pTab ){
         1360  +      renameTokenFind(pWalker->pParse, p, pItem->zName);
         1361  +    }
         1362  +  }
         1363  +
         1364  +  return WRC_Continue;
         1365  +}
         1366  +
         1367  +
         1368  +/*
         1369  +** This C function implements an SQL user function that is used by SQL code
         1370  +** generated by the ALTER TABLE ... RENAME command to modify the definition
         1371  +** of any foreign key constraints that use the table being renamed as the 
         1372  +** parent table. It is passed three arguments:
         1373  +**
         1374  +**   0: The database containing the table being renamed.
         1375  +**   1. type:     Type of object ("table", "view" etc.)
         1376  +**   2. object:   Name of object
         1377  +**   3: The complete text of the schema statement being modified,
         1378  +**   4: The old name of the table being renamed, and
         1379  +**   5: The new name of the table being renamed.
         1380  +**   6: True if the schema statement comes from the temp db.
         1381  +**
         1382  +** It returns the new schema statement. For example:
         1383  +**
         1384  +** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
         1385  +**       -> 'CREATE TABLE t1(a REFERENCES t3)'
         1386  +*/
         1387  +static void renameTableFunc(
         1388  +  sqlite3_context *context,
         1389  +  int NotUsed,
         1390  +  sqlite3_value **argv
         1391  +){
         1392  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1393  +  const char *zDb = (const char*)sqlite3_value_text(argv[0]);
         1394  +  const char *zInput = (const char*)sqlite3_value_text(argv[3]);
         1395  +  const char *zOld = (const char*)sqlite3_value_text(argv[4]);
         1396  +  const char *zNew = (const char*)sqlite3_value_text(argv[5]);
         1397  +  int bTemp = sqlite3_value_int(argv[6]);
         1398  +  UNUSED_PARAMETER(NotUsed);
         1399  +
         1400  +  if( zInput && zOld && zNew ){
         1401  +    Parse sParse;
         1402  +    int rc;
         1403  +    int bQuote = 1;
         1404  +    RenameCtx sCtx;
         1405  +    Walker sWalker;
         1406  +
         1407  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1408  +    sqlite3_xauth xAuth = db->xAuth;
         1409  +    db->xAuth = 0;
         1410  +#endif
         1411  +
         1412  +    sqlite3BtreeEnterAll(db);
         1413  +
         1414  +    memset(&sCtx, 0, sizeof(RenameCtx));
         1415  +    sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
         1416  +    memset(&sWalker, 0, sizeof(Walker));
         1417  +    sWalker.pParse = &sParse;
         1418  +    sWalker.xExprCallback = renameTableExprCb;
         1419  +    sWalker.xSelectCallback = renameTableSelectCb;
         1420  +    sWalker.u.pRename = &sCtx;
         1421  +
         1422  +    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
         1423  +
         1424  +    if( rc==SQLITE_OK ){
         1425  +      int isLegacy = (db->flags & SQLITE_LegacyAlter);
         1426  +      if( sParse.pNewTable ){
         1427  +        Table *pTab = sParse.pNewTable;
         1428  +
         1429  +        if( pTab->pSelect ){
         1430  +          if( isLegacy==0 ){
         1431  +            NameContext sNC;
         1432  +            memset(&sNC, 0, sizeof(sNC));
         1433  +            sNC.pParse = &sParse;
         1434  +
         1435  +            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
         1436  +            if( sParse.nErr ) rc = sParse.rc;
         1437  +            sqlite3WalkSelect(&sWalker, pTab->pSelect);
         1438  +          }
         1439  +        }else{
         1440  +          /* Modify any FK definitions to point to the new table. */
         1441  +#ifndef SQLITE_OMIT_FOREIGN_KEY
         1442  +          if( db->flags & SQLITE_ForeignKeys ){
         1443  +            FKey *pFKey;
         1444  +            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
         1445  +              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
         1446  +                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
         1447  +              }
         1448  +            }
         1449  +          }
         1450  +#endif
         1451  +
         1452  +          /* If this is the table being altered, fix any table refs in CHECK
         1453  +          ** expressions. Also update the name that appears right after the
         1454  +          ** "CREATE [VIRTUAL] TABLE" bit. */
         1455  +          if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
         1456  +            sCtx.pTab = pTab;
         1457  +            if( isLegacy==0 ){
         1458  +              sqlite3WalkExprList(&sWalker, pTab->pCheck);
         1459  +            }
         1460  +            renameTokenFind(&sParse, &sCtx, pTab->zName);
         1461  +          }
         1462  +        }
         1463  +      }
         1464  +
         1465  +      else if( sParse.pNewIndex ){
         1466  +        renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
         1467  +        if( isLegacy==0 ){
         1468  +          sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
         1469  +        }
         1470  +      }
         1471  +
         1472  +#ifndef SQLITE_OMIT_TRIGGER
         1473  +      else{
         1474  +        Trigger *pTrigger = sParse.pNewTrigger;
         1475  +        TriggerStep *pStep;
         1476  +        if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
         1477  +            && sCtx.pTab->pSchema==pTrigger->pTabSchema
         1478  +          ){
         1479  +          renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
         1480  +        }
         1481  +
         1482  +        if( isLegacy==0 ){
         1483  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
         1484  +          if( rc==SQLITE_OK ){
         1485  +            renameWalkTrigger(&sWalker, pTrigger);
         1486  +            for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
         1487  +              if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
         1488  +                renameTokenFind(&sParse, &sCtx, pStep->zTarget);
         1489  +              }
         1490  +            }
         1491  +          }
         1492  +        }
         1493  +      }
         1494  +#endif
         1495  +    }
         1496  +
         1497  +    if( rc==SQLITE_OK ){
         1498  +      rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
         1499  +    }
         1500  +    if( rc!=SQLITE_OK ){
         1501  +      if( sParse.zErrMsg ){
         1502  +        renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
         1503  +      }else{
         1504  +        sqlite3_result_error_code(context, rc);
         1505  +      }
         1506  +    }
         1507  +
         1508  +    renameParseCleanup(&sParse);
         1509  +    renameTokenFree(db, sCtx.pList);
         1510  +    sqlite3BtreeLeaveAll(db);
         1511  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1512  +    db->xAuth = xAuth;
         1513  +#endif
         1514  +  }
         1515  +
         1516  +  return;
         1517  +}
         1518  +
         1519  +/*
         1520  +** An SQL user function that checks that there are no parse or symbol
         1521  +** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
         1522  +** After an ALTER TABLE .. RENAME operation is performed and the schema
         1523  +** reloaded, this function is called on each SQL statement in the schema
         1524  +** to ensure that it is still usable.
         1525  +**
         1526  +**   0: Database name ("main", "temp" etc.).
         1527  +**   1: SQL statement.
         1528  +**   2: Object type ("view", "table", "trigger" or "index").
         1529  +**   3: Object name.
         1530  +**   4: True if object is from temp schema.
         1531  +**
         1532  +** Unless it finds an error, this function normally returns NULL. However, it
         1533  +** returns integer value 1 if:
         1534  +**
         1535  +**   * the SQL argument creates a trigger, and
         1536  +**   * the table that the trigger is attached to is in database zDb.
         1537  +*/
         1538  +static void renameTableTest(
         1539  +  sqlite3_context *context,
         1540  +  int NotUsed,
         1541  +  sqlite3_value **argv
         1542  +){
         1543  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1544  +  char const *zDb = (const char*)sqlite3_value_text(argv[0]);
         1545  +  char const *zInput = (const char*)sqlite3_value_text(argv[1]);
         1546  +  int bTemp = sqlite3_value_int(argv[4]);
         1547  +  int isLegacy = (db->flags & SQLITE_LegacyAlter);
         1548  +
         1549  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1550  +  sqlite3_xauth xAuth = db->xAuth;
         1551  +  db->xAuth = 0;
         1552  +#endif
         1553  +
         1554  +  UNUSED_PARAMETER(NotUsed);
         1555  +  if( zDb && zInput ){
         1556  +    int rc;
         1557  +    Parse sParse;
         1558  +    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
         1559  +    if( rc==SQLITE_OK ){
         1560  +      if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
         1561  +        NameContext sNC;
         1562  +        memset(&sNC, 0, sizeof(sNC));
         1563  +        sNC.pParse = &sParse;
         1564  +        sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
         1565  +        if( sParse.nErr ) rc = sParse.rc;
         1566  +      }
         1567  +
         1568  +      else if( sParse.pNewTrigger ){
         1569  +        if( isLegacy==0 ){
         1570  +          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
         1571  +        }
         1572  +        if( rc==SQLITE_OK ){
         1573  +          int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
         1574  +          int i2 = sqlite3FindDbName(db, zDb);
         1575  +          if( i1==i2 ) sqlite3_result_int(context, 1);
         1576  +        }
         1577  +      }
         1578  +    }
         1579  +
         1580  +    if( rc!=SQLITE_OK ){
         1581  +      renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
         1582  +    }
         1583  +    renameParseCleanup(&sParse);
         1584  +  }
         1585  +
         1586  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1587  +  db->xAuth = xAuth;
         1588  +#endif
         1589  +}
         1590  +
         1591  +/*
         1592  +** Register built-in functions used to help implement ALTER TABLE
         1593  +*/
         1594  +void sqlite3AlterFunctions(void){
         1595  +  static FuncDef aAlterTableFuncs[] = {
         1596  +    FUNCTION(sqlite_rename_column,  9, 0, 0, renameColumnFunc),
         1597  +    FUNCTION(sqlite_rename_table,  7, 0, 0, renameTableFunc),
         1598  +    FUNCTION(sqlite_rename_test,  5, 0, 0, renameTableTest),
         1599  +  };
         1600  +  sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
         1601  +}
   808   1602   #endif  /* SQLITE_ALTER_TABLE */

Changes to src/analyze.c.

  1910   1910     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1911   1911       Index *pIdx = sqliteHashData(i);
  1912   1912       if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
  1913   1913     }
  1914   1914   
  1915   1915     /* Load the statistics from the sqlite_stat4 table. */
  1916   1916   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1917         -  if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
         1917  +  if( rc==SQLITE_OK ){
  1918   1918       db->lookaside.bDisable++;
  1919   1919       rc = loadStat4(db, sInfo.zDatabase);
  1920   1920       db->lookaside.bDisable--;
  1921   1921     }
  1922   1922     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1923   1923       Index *pIdx = sqliteHashData(i);
  1924   1924       sqlite3_free(pIdx->aiRowEst);

Changes to src/auth.c.

    74     74   ){
    75     75   #ifdef SQLITE_ENABLE_API_ARMOR
    76     76     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    77     77   #endif
    78     78     sqlite3_mutex_enter(db->mutex);
    79     79     db->xAuth = (sqlite3_xauth)xAuth;
    80     80     db->pAuthArg = pArg;
    81         -  sqlite3ExpirePreparedStatements(db);
           81  +  sqlite3ExpirePreparedStatements(db, 0);
    82     82     sqlite3_mutex_leave(db->mutex);
    83     83     return SQLITE_OK;
    84     84   }
    85     85   
    86     86   /*
    87     87   ** Write an error message into pParse->zErrMsg that explains that the
    88     88   ** user-supplied authorization function returned an illegal value.
................................................................................
   203    203   ){
   204    204     sqlite3 *db = pParse->db;
   205    205     int rc;
   206    206   
   207    207     /* Don't do any authorization checks if the database is initialising
   208    208     ** or if the parser is being invoked from within sqlite3_declare_vtab.
   209    209     */
   210         -  if( db->init.busy || IN_DECLARE_VTAB ){
          210  +  if( db->init.busy || IN_SPECIAL_PARSE ){
   211    211       return SQLITE_OK;
   212    212     }
   213    213   
   214    214     if( db->xAuth==0 ){
   215    215       return SQLITE_OK;
   216    216     }
   217    217   

Changes to src/btree.c.

  3526   3526     ** is already in a read-transaction and a read-transaction
  3527   3527     ** is requested, this is a no-op.
  3528   3528     */
  3529   3529     if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
  3530   3530       goto trans_begun;
  3531   3531     }
  3532   3532     assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
         3533  +
         3534  +  if( (p->db->flags & SQLITE_ResetDatabase) 
         3535  +   && sqlite3PagerIsreadonly(pBt->pPager)==0 
         3536  +  ){
         3537  +    pBt->btsFlags &= ~BTS_READ_ONLY;
         3538  +  }
  3533   3539   
  3534   3540     /* Write transactions are not possible on a read-only database */
  3535   3541     if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
  3536   3542       rc = SQLITE_READONLY;
  3537   3543       goto trans_begun;
  3538   3544     }
  3539   3545   
................................................................................
  9756   9762   ** reference to the page, add an error message to pCheck->zErrMsg.
  9757   9763   ** Return 1 if there are 2 or more references to the page and 0 if
  9758   9764   ** if this is the first reference to the page.
  9759   9765   **
  9760   9766   ** Also check that the page number is in bounds.
  9761   9767   */
  9762   9768   static int checkRef(IntegrityCk *pCheck, Pgno iPage){
  9763         -  if( iPage==0 ) return 1;
  9764         -  if( iPage>pCheck->nPage ){
         9769  +  if( iPage>pCheck->nPage || iPage==0 ){
  9765   9770       checkAppendMsg(pCheck, "invalid page number %d", iPage);
  9766   9771       return 1;
  9767   9772     }
  9768   9773     if( getPageReferenced(pCheck, iPage) ){
  9769   9774       checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
  9770   9775       return 1;
  9771   9776     }
................................................................................
  9812   9817     IntegrityCk *pCheck,  /* Integrity checking context */
  9813   9818     int isFreeList,       /* True for a freelist.  False for overflow page list */
  9814   9819     int iPage,            /* Page number for first page in the list */
  9815   9820     int N                 /* Expected number of pages in the list */
  9816   9821   ){
  9817   9822     int i;
  9818   9823     int expected = N;
  9819         -  int iFirst = iPage;
  9820         -  while( N-- > 0 && pCheck->mxErr ){
         9824  +  int nErrAtStart = pCheck->nErr;
         9825  +  while( iPage!=0 && pCheck->mxErr ){
  9821   9826       DbPage *pOvflPage;
  9822   9827       unsigned char *pOvflData;
  9823         -    if( iPage<1 ){
  9824         -      checkAppendMsg(pCheck,
  9825         -         "%d of %d pages missing from overflow list starting at %d",
  9826         -          N+1, expected, iFirst);
  9827         -      break;
  9828         -    }
  9829   9828       if( checkRef(pCheck, iPage) ) break;
         9829  +    N--;
  9830   9830       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
  9831   9831         checkAppendMsg(pCheck, "failed to get page %d", iPage);
  9832   9832         break;
  9833   9833       }
  9834   9834       pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
  9835   9835       if( isFreeList ){
  9836   9836         int n = get4byte(&pOvflData[4]);
................................................................................
  9866   9866           i = get4byte(pOvflData);
  9867   9867           checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
  9868   9868         }
  9869   9869       }
  9870   9870   #endif
  9871   9871       iPage = get4byte(pOvflData);
  9872   9872       sqlite3PagerUnref(pOvflPage);
  9873         -
  9874         -    if( isFreeList && N<(iPage!=0) ){
  9875         -      checkAppendMsg(pCheck, "free-page count in header is too small");
  9876         -    }
         9873  +  }
         9874  +  if( N && nErrAtStart==pCheck->nErr ){
         9875  +    checkAppendMsg(pCheck,
         9876  +      "%s is %d but should be %d",
         9877  +      isFreeList ? "size" : "overflow list length",
         9878  +      expected-N, expected);
  9877   9879     }
  9878   9880   }
  9879   9881   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  9880   9882   
  9881   9883   /*
  9882   9884   ** An implementation of a min-heap.
  9883   9885   **
................................................................................
 10263  10265     sCheck.zPfx = "Main freelist: ";
 10264  10266     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
 10265  10267               get4byte(&pBt->pPage1->aData[36]));
 10266  10268     sCheck.zPfx = 0;
 10267  10269   
 10268  10270     /* Check all the tables.
 10269  10271     */
        10272  +#ifndef SQLITE_OMIT_AUTOVACUUM
        10273  +  if( pBt->autoVacuum ){
        10274  +    int mx = 0;
        10275  +    int mxInHdr;
        10276  +    for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
        10277  +    mxInHdr = get4byte(&pBt->pPage1->aData[52]);
        10278  +    if( mx!=mxInHdr ){
        10279  +      checkAppendMsg(&sCheck,
        10280  +        "max rootpage (%d) disagrees with header (%d)",
        10281  +        mx, mxInHdr
        10282  +      );
        10283  +    }
        10284  +  }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
        10285  +    checkAppendMsg(&sCheck,
        10286  +      "incremental_vacuum enabled with a max rootpage of zero"
        10287  +    );
        10288  +  }
        10289  +#endif
 10270  10290     testcase( pBt->db->flags & SQLITE_CellSizeCk );
 10271  10291     pBt->db->flags &= ~SQLITE_CellSizeCk;
 10272  10292     for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
 10273  10293       i64 notUsed;
 10274  10294       if( aRoot[i]==0 ) continue;
 10275  10295   #ifndef SQLITE_OMIT_AUTOVACUUM
 10276  10296       if( pBt->autoVacuum && aRoot[i]>1 ){

Changes to src/build.c.

   223    223       }
   224    224     }
   225    225   
   226    226   
   227    227     /* Get the VDBE program ready for execution
   228    228     */
   229    229     if( v && pParse->nErr==0 && !db->mallocFailed ){
   230         -    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   231    230       /* A minimum of one cursor is required if autoincrement is used
   232    231       *  See ticket [a696379c1f08866] */
   233    232       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   234    233       sqlite3VdbeMakeReady(v, pParse);
   235    234       pParse->rc = SQLITE_DONE;
   236    235     }else{
   237    236       pParse->rc = SQLITE_ERROR;
................................................................................
   438    437     }
   439    438     return p;
   440    439   }
   441    440   
   442    441   /*
   443    442   ** Reclaim the memory used by an index
   444    443   */
   445         -static void freeIndex(sqlite3 *db, Index *p){
          444  +void sqlite3FreeIndex(sqlite3 *db, Index *p){
   446    445   #ifndef SQLITE_OMIT_ANALYZE
   447    446     sqlite3DeleteIndexSamples(db, p);
   448    447   #endif
   449    448     sqlite3ExprDelete(db, p->pPartIdxWhere);
   450    449     sqlite3ExprListDelete(db, p->aColExpr);
   451    450     sqlite3DbFree(db, p->zColAff);
   452    451     if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
................................................................................
   478    477         ** indices. */
   479    478         p = pIndex->pTable->pIndex;
   480    479         while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
   481    480         if( ALWAYS(p && p->pNext==pIndex) ){
   482    481           p->pNext = pIndex->pNext;
   483    482         }
   484    483       }
   485         -    freeIndex(db, pIndex);
          484  +    sqlite3FreeIndex(db, pIndex);
   486    485     }
   487    486     db->mDbFlags |= DBFLAG_SchemaChange;
   488    487   }
   489    488   
   490    489   /*
   491    490   ** Look through the list of open database files in db->aDb[] and if
   492    491   ** any have been closed, remove them from the list.  Reallocate the
................................................................................
   624    623         char *zName = pIndex->zName; 
   625    624         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   626    625            &pIndex->pSchema->idxHash, zName, 0
   627    626         );
   628    627         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   629    628         assert( pOld==pIndex || pOld==0 );
   630    629       }
   631         -    freeIndex(db, pIndex);
          630  +    sqlite3FreeIndex(db, pIndex);
   632    631     }
   633    632   
   634    633     /* Delete any foreign keys attached to this table. */
   635    634     sqlite3FkDelete(db, pTable);
   636    635   
   637    636     /* Delete the Table structure itself.
   638    637     */
................................................................................
   782    781       *pUnqual = pName2;
   783    782       iDb = sqlite3FindDb(db, pName1);
   784    783       if( iDb<0 ){
   785    784         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
   786    785         return -1;
   787    786       }
   788    787     }else{
   789         -    assert( db->init.iDb==0 || db->init.busy
          788  +    assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
   790    789                || (db->mDbFlags & DBFLAG_Vacuum)!=0);
   791    790       iDb = db->init.iDb;
   792    791       *pUnqual = pName1;
   793    792     }
   794    793     return iDb;
   795    794   }
   796    795   
................................................................................
   877    876         /* If creating a temp table, the name may not be qualified. Unless 
   878    877         ** the database name is "temp" anyway.  */
   879    878         sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
   880    879         return;
   881    880       }
   882    881       if( !OMIT_TEMPDB && isTemp ) iDb = 1;
   883    882       zName = sqlite3NameFromToken(db, pName);
          883  +    if( IN_RENAME_OBJECT ){
          884  +      sqlite3RenameTokenMap(pParse, (void*)zName, pName);
          885  +    }
   884    886     }
   885    887     pParse->sNameToken = *pName;
   886    888     if( zName==0 ) return;
   887    889     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   888    890       goto begin_table_error;
   889    891     }
   890    892     if( db->init.iDb==1 ) isTemp = 1;
................................................................................
   912    914     /* Make sure the new table name does not collide with an existing
   913    915     ** index or table name in the same database.  Issue an error message if
   914    916     ** it does. The exception is if the statement being parsed was passed
   915    917     ** to an sqlite3_declare_vtab() call. In that case only the column names
   916    918     ** and types will be used, so there is no need to test for namespace
   917    919     ** collisions.
   918    920     */
   919         -  if( !IN_DECLARE_VTAB ){
          921  +  if( !IN_SPECIAL_PARSE ){
   920    922       char *zDb = db->aDb[iDb].zDbSName;
   921    923       if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   922    924         goto begin_table_error;
   923    925       }
   924    926       pTable = sqlite3FindTable(db, zName, zDb);
   925    927       if( pTable ){
   926    928         if( !noErr ){
................................................................................
  1071   1073     if( (p = pParse->pNewTable)==0 ) return;
  1072   1074     if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1073   1075       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
  1074   1076       return;
  1075   1077     }
  1076   1078     z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
  1077   1079     if( z==0 ) return;
         1080  +  if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
  1078   1081     memcpy(z, pName->z, pName->n);
  1079   1082     z[pName->n] = 0;
  1080   1083     sqlite3Dequote(z);
  1081   1084     for(i=0; i<p->nCol; i++){
  1082   1085       if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
  1083   1086         sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
  1084   1087         sqlite3DbFree(db, z);
................................................................................
  1277   1280         x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
  1278   1281         x.pLeft = pExpr;
  1279   1282         x.flags = EP_Skip;
  1280   1283         pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
  1281   1284         sqlite3DbFree(db, x.u.zToken);
  1282   1285       }
  1283   1286     }
         1287  +  if( IN_RENAME_OBJECT ){
         1288  +    sqlite3RenameExprUnmap(pParse, pExpr);
         1289  +  }
  1284   1290     sqlite3ExprDelete(db, pExpr);
  1285   1291   }
  1286   1292   
  1287   1293   /*
  1288   1294   ** Backwards Compatibility Hack:
  1289   1295   ** 
  1290   1296   ** Historical versions of SQLite accepted strings as column names in
................................................................................
  1368   1374       }
  1369   1375     }
  1370   1376     if( nTerm==1
  1371   1377      && pCol
  1372   1378      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
  1373   1379      && sortOrder!=SQLITE_SO_DESC
  1374   1380     ){
         1381  +    if( IN_RENAME_OBJECT && pList ){
         1382  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
         1383  +    }
  1375   1384       pTab->iPKey = iCol;
  1376   1385       pTab->keyConf = (u8)onError;
  1377   1386       assert( autoInc==0 || autoInc==1 );
  1378   1387       pTab->tabFlags |= autoInc*TF_Autoincrement;
  1379   1388       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
  1380   1389     }else if( autoInc ){
  1381   1390   #ifndef SQLITE_OMIT_AUTOINCREMENT
................................................................................
  1760   1769       for(i=0; i<pTab->nCol; i++){
  1761   1770         if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
  1762   1771           pTab->aCol[i].notNull = OE_Abort;
  1763   1772         }
  1764   1773       }
  1765   1774     }
  1766   1775   
  1767         -  /* The remaining transformations only apply to b-tree tables, not to
  1768         -  ** virtual tables */
  1769         -  if( IN_DECLARE_VTAB ) return;
  1770         -
  1771   1776     /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
  1772   1777     ** into BTREE_BLOBKEY.
  1773   1778     */
  1774   1779     if( pParse->addrCrTab ){
  1775   1780       assert( v );
  1776   1781       sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
  1777   1782     }
................................................................................
  2170   2175     if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
  2171   2176   
  2172   2177     /* Make a copy of the entire SELECT statement that defines the view.
  2173   2178     ** This will force all the Expr.token.z values to be dynamically
  2174   2179     ** allocated rather than point to the input string - which means that
  2175   2180     ** they will persist after the current sqlite3_exec() call returns.
  2176   2181     */
  2177         -  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
         2182  +  if( IN_RENAME_OBJECT ){
         2183  +    p->pSelect = pSelect;
         2184  +    pSelect = 0;
         2185  +  }else{
         2186  +    p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
         2187  +  }
  2178   2188     p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
  2179   2189     if( db->mallocFailed ) goto create_view_fail;
  2180   2190   
  2181   2191     /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
  2182   2192     ** the end.
  2183   2193     */
  2184   2194     sEnd = pParse->sLastToken;
................................................................................
  2195   2205     sEnd.n = 1;
  2196   2206   
  2197   2207     /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
  2198   2208     sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
  2199   2209   
  2200   2210   create_view_fail:
  2201   2211     sqlite3SelectDelete(db, pSelect);
         2212  +  if( IN_RENAME_OBJECT ){
         2213  +    sqlite3RenameExprlistUnmap(pParse, pCNames);
         2214  +  }
  2202   2215     sqlite3ExprListDelete(db, pCNames);
  2203   2216     return;
  2204   2217   }
  2205   2218   #endif /* SQLITE_OMIT_VIEW */
  2206   2219   
  2207   2220   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  2208   2221   /*
................................................................................
  2268   2281     ** to the elements of the FROM clause.  But we do not want these changes
  2269   2282     ** to be permanent.  So the computation is done on a copy of the SELECT
  2270   2283     ** statement that defines the view.
  2271   2284     */
  2272   2285     assert( pTable->pSelect );
  2273   2286     pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
  2274   2287     if( pSel ){
         2288  +#ifndef SQLITE_OMIT_ALTERTABLE
         2289  +    u8 eParseMode = pParse->eParseMode;
         2290  +    pParse->eParseMode = PARSE_MODE_NORMAL;
         2291  +#endif
  2275   2292       n = pParse->nTab;
  2276   2293       sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
  2277   2294       pTable->nCol = -1;
  2278   2295       db->lookaside.bDisable++;
  2279   2296   #ifndef SQLITE_OMIT_AUTHORIZATION
  2280   2297       xAuth = db->xAuth;
  2281   2298       db->xAuth = 0;
................................................................................
  2313   2330       }else{
  2314   2331         pTable->nCol = 0;
  2315   2332         nErr++;
  2316   2333       }
  2317   2334       sqlite3DeleteTable(db, pSelTab);
  2318   2335       sqlite3SelectDelete(db, pSel);
  2319   2336       db->lookaside.bDisable--;
         2337  +#ifndef SQLITE_OMIT_ALTERTABLE
         2338  +    pParse->eParseMode = eParseMode;
         2339  +#endif
  2320   2340     } else {
  2321   2341       nErr++;
  2322   2342     }
  2323   2343     pTable->pSchema->schemaFlags |= DB_UnresetViews;
  2324   2344     if( db->mallocFailed ){
  2325   2345       sqlite3DeleteColumnNames(db, pTable);
  2326   2346       pTable->aCol = 0;
................................................................................
  2660   2680   
  2661   2681     /* Generate code to remove the table from the master table
  2662   2682     ** on disk.
  2663   2683     */
  2664   2684     v = sqlite3GetVdbe(pParse);
  2665   2685     if( v ){
  2666   2686       sqlite3BeginWriteOperation(pParse, 1, iDb);
  2667         -    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
  2668         -    sqlite3FkDropTable(pParse, pName, pTab);
         2687  +    if( !isView ){
         2688  +      sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
         2689  +      sqlite3FkDropTable(pParse, pName, pTab);
         2690  +    }
  2669   2691       sqlite3CodeDropTable(pParse, pTab, iDb, isView);
  2670   2692     }
  2671   2693   
  2672   2694   exit_drop_table:
  2673   2695     sqlite3SrcListDelete(db, pName);
  2674   2696   }
  2675   2697   
................................................................................
  2736   2758     if( pFKey==0 ){
  2737   2759       goto fk_end;
  2738   2760     }
  2739   2761     pFKey->pFrom = p;
  2740   2762     pFKey->pNextFrom = p->pFKey;
  2741   2763     z = (char*)&pFKey->aCol[nCol];
  2742   2764     pFKey->zTo = z;
         2765  +  if( IN_RENAME_OBJECT ){
         2766  +    sqlite3RenameTokenMap(pParse, (void*)z, pTo);
         2767  +  }
  2743   2768     memcpy(z, pTo->z, pTo->n);
  2744   2769     z[pTo->n] = 0;
  2745   2770     sqlite3Dequote(z);
  2746   2771     z += pTo->n+1;
  2747   2772     pFKey->nCol = nCol;
  2748   2773     if( pFromCol==0 ){
  2749   2774       pFKey->aCol[0].iFrom = p->nCol-1;
................................................................................
  2758   2783         }
  2759   2784         if( j>=p->nCol ){
  2760   2785           sqlite3ErrorMsg(pParse, 
  2761   2786             "unknown column \"%s\" in foreign key definition", 
  2762   2787             pFromCol->a[i].zName);
  2763   2788           goto fk_end;
  2764   2789         }
         2790  +      if( IN_RENAME_OBJECT ){
         2791  +        sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
         2792  +      }
  2765   2793       }
  2766   2794     }
  2767   2795     if( pToCol ){
  2768   2796       for(i=0; i<nCol; i++){
  2769   2797         int n = sqlite3Strlen30(pToCol->a[i].zName);
  2770   2798         pFKey->aCol[i].zCol = z;
         2799  +      if( IN_RENAME_OBJECT ){
         2800  +        sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
         2801  +      }
  2771   2802         memcpy(z, pToCol->a[i].zName, n);
  2772   2803         z[n] = 0;
  2773   2804         z += n+1;
  2774   2805       }
  2775   2806     }
  2776   2807     pFKey->isDeferred = 0;
  2777   2808     pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
................................................................................
  3096   3127     if( pName ){
  3097   3128       zName = sqlite3NameFromToken(db, pName);
  3098   3129       if( zName==0 ) goto exit_create_index;
  3099   3130       assert( pName->z!=0 );
  3100   3131       if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
  3101   3132         goto exit_create_index;
  3102   3133       }
  3103         -    if( !db->init.busy ){
  3104         -      if( sqlite3FindTable(db, zName, 0)!=0 ){
  3105         -        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
         3134  +    if( !IN_RENAME_OBJECT ){
         3135  +      if( !db->init.busy ){
         3136  +        if( sqlite3FindTable(db, zName, 0)!=0 ){
         3137  +          sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
         3138  +          goto exit_create_index;
         3139  +        }
         3140  +      }
         3141  +      if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
         3142  +        if( !ifNotExist ){
         3143  +          sqlite3ErrorMsg(pParse, "index %s already exists", zName);
         3144  +        }else{
         3145  +          assert( !db->init.busy );
         3146  +          sqlite3CodeVerifySchema(pParse, iDb);
         3147  +        }
  3106   3148           goto exit_create_index;
  3107   3149         }
  3108   3150       }
  3109         -    if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
  3110         -      if( !ifNotExist ){
  3111         -        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
  3112         -      }else{
  3113         -        assert( !db->init.busy );
  3114         -        sqlite3CodeVerifySchema(pParse, iDb);
  3115         -      }
  3116         -      goto exit_create_index;
  3117         -    }
  3118   3151     }else{
  3119   3152       int n;
  3120   3153       Index *pLoop;
  3121   3154       for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
  3122   3155       zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
  3123   3156       if( zName==0 ){
  3124   3157         goto exit_create_index;
................................................................................
  3125   3158       }
  3126   3159   
  3127   3160       /* Automatic index names generated from within sqlite3_declare_vtab()
  3128   3161       ** must have names that are distinct from normal automatic index names.
  3129   3162       ** The following statement converts "sqlite3_autoindex..." into
  3130   3163       ** "sqlite3_butoindex..." in order to make the names distinct.
  3131   3164       ** The "vtab_err.test" test demonstrates the need of this statement. */
  3132         -    if( IN_DECLARE_VTAB ) zName[7]++;
         3165  +    if( IN_SPECIAL_PARSE ) zName[7]++;
  3133   3166     }
  3134   3167   
  3135   3168     /* Check for authorization to create an index.
  3136   3169     */
  3137   3170   #ifndef SQLITE_OMIT_AUTHORIZATION
  3138         -  {
         3171  +  if( !IN_RENAME_OBJECT ){
  3139   3172       const char *zDb = pDb->zDbSName;
  3140   3173       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
  3141   3174         goto exit_create_index;
  3142   3175       }
  3143   3176       i = SQLITE_CREATE_INDEX;
  3144   3177       if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
  3145   3178       if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
................................................................................
  3218   3251     ** a table column, store that column in aiColumn[].  For general expressions,
  3219   3252     ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
  3220   3253     **
  3221   3254     ** TODO: Issue a warning if two or more columns of the index are identical.
  3222   3255     ** TODO: Issue a warning if the table primary key is used as part of the
  3223   3256     ** index key.
  3224   3257     */
  3225         -  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
         3258  +  pListItem = pList->a;
         3259  +  if( IN_RENAME_OBJECT ){
         3260  +    pIndex->aColExpr = pList;
         3261  +    pList = 0;
         3262  +  }
         3263  +  for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
  3226   3264       Expr *pCExpr;                  /* The i-th index expression */
  3227   3265       int requestedSortOrder;        /* ASC or DESC on the i-th expression */
  3228   3266       const char *zColl;             /* Collation sequence name */
  3229   3267   
  3230   3268       sqlite3StringToId(pListItem->pExpr);
  3231   3269       sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
  3232   3270       if( pParse->nErr ) goto exit_create_index;
................................................................................
  3234   3272       if( pCExpr->op!=TK_COLUMN ){
  3235   3273         if( pTab==pParse->pNewTable ){
  3236   3274           sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
  3237   3275                                   "UNIQUE constraints");
  3238   3276           goto exit_create_index;
  3239   3277         }
  3240   3278         if( pIndex->aColExpr==0 ){
  3241         -        ExprList *pCopy = sqlite3ExprListDup(db, pList, 0);
  3242         -        pIndex->aColExpr = pCopy;
  3243         -        if( !db->mallocFailed ){
  3244         -          assert( pCopy!=0 );
  3245         -          pListItem = &pCopy->a[i];
  3246         -        }
         3279  +        pIndex->aColExpr = pList;
         3280  +        pList = 0;
  3247   3281         }
  3248   3282         j = XN_EXPR;
  3249   3283         pIndex->aiColumn[i] = XN_EXPR;
  3250   3284         pIndex->uniqNotNull = 0;
  3251   3285       }else{
  3252   3286         j = pCExpr->iColumn;
  3253   3287         assert( j<=0x7fff );
................................................................................
  3378   3412           }
  3379   3413           if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
  3380   3414           goto exit_create_index;
  3381   3415         }
  3382   3416       }
  3383   3417     }
  3384   3418   
  3385         -  /* Link the new Index structure to its table and to the other
  3386         -  ** in-memory database structures. 
  3387         -  */
  3388         -  assert( pParse->nErr==0 );
  3389         -  if( db->init.busy ){
  3390         -    Index *p;
  3391         -    assert( !IN_DECLARE_VTAB );
  3392         -    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
  3393         -    p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
  3394         -                          pIndex->zName, pIndex);
  3395         -    if( p ){
  3396         -      assert( p==pIndex );  /* Malloc must have failed */
  3397         -      sqlite3OomFault(db);
  3398         -      goto exit_create_index;
  3399         -    }
  3400         -    db->mDbFlags |= DBFLAG_SchemaChange;
  3401         -    if( pTblName!=0 ){
  3402         -      pIndex->tnum = db->init.newTnum;
  3403         -    }
  3404         -  }
  3405         -
  3406         -  /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
  3407         -  ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
  3408         -  ** emit code to allocate the index rootpage on disk and make an entry for
  3409         -  ** the index in the sqlite_master table and populate the index with
  3410         -  ** content.  But, do not do this if we are simply reading the sqlite_master
  3411         -  ** table to parse the schema, or if this index is the PRIMARY KEY index
  3412         -  ** of a WITHOUT ROWID table.
  3413         -  **
  3414         -  ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
  3415         -  ** or UNIQUE index in a CREATE TABLE statement.  Since the table
  3416         -  ** has just been created, it contains no data and the index initialization
  3417         -  ** step can be skipped.
  3418         -  */
  3419         -  else if( HasRowid(pTab) || pTblName!=0 ){
  3420         -    Vdbe *v;
  3421         -    char *zStmt;
  3422         -    int iMem = ++pParse->nMem;
  3423         -
  3424         -    v = sqlite3GetVdbe(pParse);
  3425         -    if( v==0 ) goto exit_create_index;
  3426         -
  3427         -    sqlite3BeginWriteOperation(pParse, 1, iDb);
  3428         -
  3429         -    /* Create the rootpage for the index using CreateIndex. But before
  3430         -    ** doing so, code a Noop instruction and store its address in 
  3431         -    ** Index.tnum. This is required in case this index is actually a 
  3432         -    ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
  3433         -    ** that case the convertToWithoutRowidTable() routine will replace
  3434         -    ** the Noop with a Goto to jump over the VDBE code generated below. */
  3435         -    pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
  3436         -    sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
  3437         -
  3438         -    /* Gather the complete text of the CREATE INDEX statement into
  3439         -    ** the zStmt variable
         3419  +  if( !IN_RENAME_OBJECT ){
         3420  +
         3421  +    /* Link the new Index structure to its table and to the other
         3422  +    ** in-memory database structures. 
         3423  +    */
         3424  +    assert( pParse->nErr==0 );
         3425  +    if( db->init.busy ){
         3426  +      Index *p;
         3427  +      assert( !IN_SPECIAL_PARSE );
         3428  +      assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
         3429  +      p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
         3430  +          pIndex->zName, pIndex);
         3431  +      if( p ){
         3432  +        assert( p==pIndex );  /* Malloc must have failed */
         3433  +        sqlite3OomFault(db);
         3434  +        goto exit_create_index;
         3435  +      }
         3436  +      db->mDbFlags |= DBFLAG_SchemaChange;
         3437  +      if( pTblName!=0 ){
         3438  +        pIndex->tnum = db->init.newTnum;
         3439  +      }
         3440  +    }
         3441  +
         3442  +    /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
         3443  +    ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
         3444  +    ** emit code to allocate the index rootpage on disk and make an entry for
         3445  +    ** the index in the sqlite_master table and populate the index with
         3446  +    ** content.  But, do not do this if we are simply reading the sqlite_master
         3447  +    ** table to parse the schema, or if this index is the PRIMARY KEY index
         3448  +    ** of a WITHOUT ROWID table.
         3449  +    **
         3450  +    ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
         3451  +    ** or UNIQUE index in a CREATE TABLE statement.  Since the table
         3452  +    ** has just been created, it contains no data and the index initialization
         3453  +    ** step can be skipped.
  3440   3454       */
  3441         -    if( pStart ){
  3442         -      int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
  3443         -      if( pName->z[n-1]==';' ) n--;
  3444         -      /* A named index with an explicit CREATE INDEX statement */
  3445         -      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  3446         -        onError==OE_None ? "" : " UNIQUE", n, pName->z);
  3447         -    }else{
  3448         -      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
  3449         -      /* zStmt = sqlite3MPrintf(""); */
  3450         -      zStmt = 0;
  3451         -    }
  3452         -
  3453         -    /* Add an entry in sqlite_master for this index
  3454         -    */
  3455         -    sqlite3NestedParse(pParse, 
  3456         -        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
  3457         -        db->aDb[iDb].zDbSName, MASTER_NAME,
  3458         -        pIndex->zName,
  3459         -        pTab->zName,
  3460         -        iMem,
  3461         -        zStmt
  3462         -    );
  3463         -    sqlite3DbFree(db, zStmt);
  3464         -
  3465         -    /* Fill the index with data and reparse the schema. Code an OP_Expire
  3466         -    ** to invalidate all pre-compiled statements.
  3467         -    */
  3468         -    if( pTblName ){
  3469         -      sqlite3RefillIndex(pParse, pIndex, iMem);
  3470         -      sqlite3ChangeCookie(pParse, iDb);
  3471         -      sqlite3VdbeAddParseSchemaOp(v, iDb,
  3472         -         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
  3473         -      sqlite3VdbeAddOp0(v, OP_Expire);
  3474         -    }
  3475         -
  3476         -    sqlite3VdbeJumpHere(v, pIndex->tnum);
         3455  +    else if( HasRowid(pTab) || pTblName!=0 ){
         3456  +      Vdbe *v;
         3457  +      char *zStmt;
         3458  +      int iMem = ++pParse->nMem;
         3459  +
         3460  +      v = sqlite3GetVdbe(pParse);
         3461  +      if( v==0 ) goto exit_create_index;
         3462  +
         3463  +      sqlite3BeginWriteOperation(pParse, 1, iDb);
         3464  +
         3465  +      /* Create the rootpage for the index using CreateIndex. But before
         3466  +      ** doing so, code a Noop instruction and store its address in 
         3467  +      ** Index.tnum. This is required in case this index is actually a 
         3468  +      ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
         3469  +      ** that case the convertToWithoutRowidTable() routine will replace
         3470  +      ** the Noop with a Goto to jump over the VDBE code generated below. */
         3471  +      pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
         3472  +      sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
         3473  +
         3474  +      /* Gather the complete text of the CREATE INDEX statement into
         3475  +      ** the zStmt variable
         3476  +      */
         3477  +      if( pStart ){
         3478  +        int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
         3479  +        if( pName->z[n-1]==';' ) n--;
         3480  +        /* A named index with an explicit CREATE INDEX statement */
         3481  +        zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
         3482  +            onError==OE_None ? "" : " UNIQUE", n, pName->z);
         3483  +      }else{
         3484  +        /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
         3485  +        /* zStmt = sqlite3MPrintf(""); */
         3486  +        zStmt = 0;
         3487  +      }
         3488  +
         3489  +      /* Add an entry in sqlite_master for this index
         3490  +      */
         3491  +      sqlite3NestedParse(pParse, 
         3492  +          "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
         3493  +          db->aDb[iDb].zDbSName, MASTER_NAME,
         3494  +          pIndex->zName,
         3495  +          pTab->zName,
         3496  +          iMem,
         3497  +          zStmt
         3498  +          );
         3499  +      sqlite3DbFree(db, zStmt);
         3500  +
         3501  +      /* Fill the index with data and reparse the schema. Code an OP_Expire
         3502  +      ** to invalidate all pre-compiled statements.
         3503  +      */
         3504  +      if( pTblName ){
         3505  +        sqlite3RefillIndex(pParse, pIndex, iMem);
         3506  +        sqlite3ChangeCookie(pParse, iDb);
         3507  +        sqlite3VdbeAddParseSchemaOp(v, iDb,
         3508  +            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
         3509  +        sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
         3510  +      }
         3511  +
         3512  +      sqlite3VdbeJumpHere(v, pIndex->tnum);
         3513  +    }
  3477   3514     }
  3478   3515   
  3479   3516     /* When adding an index to the list of indices for a table, make
  3480   3517     ** sure all indices labeled OE_Replace come after all those labeled
  3481   3518     ** OE_Ignore.  This is necessary for the correct constraint check
  3482   3519     ** processing (in sqlite3GenerateConstraintChecks()) as part of
  3483   3520     ** UPDATE and INSERT statements.  
................................................................................
  3493   3530           pOther = pOther->pNext;
  3494   3531         }
  3495   3532         pIndex->pNext = pOther->pNext;
  3496   3533         pOther->pNext = pIndex;
  3497   3534       }
  3498   3535       pIndex = 0;
  3499   3536     }
         3537  +  else if( IN_RENAME_OBJECT ){
         3538  +    assert( pParse->pNewIndex==0 );
         3539  +    pParse->pNewIndex = pIndex;
         3540  +    pIndex = 0;
         3541  +  }
  3500   3542   
  3501   3543     /* Clean up before exiting */
  3502   3544   exit_create_index:
  3503         -  if( pIndex ) freeIndex(db, pIndex);
         3545  +  if( pIndex ) sqlite3FreeIndex(db, pIndex);
  3504   3546     sqlite3ExprDelete(db, pPIWhere);
  3505   3547     sqlite3ExprListDelete(db, pList);
  3506   3548     sqlite3SrcListDelete(db, pTblName);
  3507   3549     sqlite3DbFree(db, zName);
  3508   3550   }
  3509   3551   
  3510   3552   /*
................................................................................
  3665   3707   
  3666   3708   /*
  3667   3709   ** Append a new element to the given IdList.  Create a new IdList if
  3668   3710   ** need be.
  3669   3711   **
  3670   3712   ** A new IdList is returned, or NULL if malloc() fails.
  3671   3713   */
  3672         -IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
         3714  +IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
         3715  +  sqlite3 *db = pParse->db;
  3673   3716     int i;
  3674   3717     if( pList==0 ){
  3675   3718       pList = sqlite3DbMallocZero(db, sizeof(IdList) );
  3676   3719       if( pList==0 ) return 0;
  3677   3720     }
  3678   3721     pList->a = sqlite3ArrayAllocate(
  3679   3722         db,
................................................................................
  3683   3726         &i
  3684   3727     );
  3685   3728     if( i<0 ){
  3686   3729       sqlite3IdListDelete(db, pList);
  3687   3730       return 0;
  3688   3731     }
  3689   3732     pList->a[i].zName = sqlite3NameFromToken(db, pToken);
         3733  +  if( IN_RENAME_OBJECT && pList->a[i].zName ){
         3734  +    sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
         3735  +  }
  3690   3736     return pList;
  3691   3737   }
  3692   3738   
  3693   3739   /*
  3694   3740   ** Delete an IdList.
  3695   3741   */
  3696   3742   void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
................................................................................
  3929   3975     }
  3930   3976     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
  3931   3977     if( p==0 ){
  3932   3978       goto append_from_error;
  3933   3979     }
  3934   3980     assert( p->nSrc>0 );
  3935   3981     pItem = &p->a[p->nSrc-1];
         3982  +  assert( (pTable==0)==(pDatabase==0) );
         3983  +  assert( pItem->zName==0 || pDatabase!=0 );
         3984  +  if( IN_RENAME_OBJECT && pItem->zName ){
         3985  +    Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
         3986  +    sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
         3987  +  }
  3936   3988     assert( pAlias!=0 );
  3937   3989     if( pAlias->n ){
  3938   3990       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
  3939   3991     }
  3940   3992     pItem->pSelect = pSubquery;
  3941   3993     pItem->pOn = pOn;
  3942   3994     pItem->pUsing = pUsing;

Changes to src/ctime.c.

    26     26   #endif
    27     27   
    28     28   /* These macros are provided to "stringify" the value of the define
    29     29   ** for those options in which the value is meaningful. */
    30     30   #define CTIMEOPT_VAL_(opt) #opt
    31     31   #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
    32     32   
           33  +/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
           34  +** option requires a separate macro because legal values contain a single
           35  +** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
           36  +#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
           37  +#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
           38  +
    33     39   /*
    34     40   ** An array of names of all compile-time options.  This array should 
    35     41   ** be sorted A-Z.
    36     42   **
    37     43   ** This array looks large, but in a typical installation actually uses
    38     44   ** only a handful of compile-time options, so most times this array is usually
    39     45   ** rather short and uses little memory space.
................................................................................
   109    115   #ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
   110    116     "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
   111    117   #endif
   112    118   #ifdef SQLITE_DEFAULT_LOCKING_MODE
   113    119     "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
   114    120   #endif
   115    121   #ifdef SQLITE_DEFAULT_LOOKASIDE
   116         -  "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOOKASIDE),
          122  +  "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
   117    123   #endif
   118    124   #if SQLITE_DEFAULT_MEMSTATUS
   119    125     "DEFAULT_MEMSTATUS",
   120    126   #endif
   121    127   #ifdef SQLITE_DEFAULT_MMAP_SIZE
   122    128     "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
   123    129   #endif

Changes to src/delete.c.

   459    459         for(i=0; i<nPk; i++){
   460    460           assert( pPk->aiColumn[i]>=0 );
   461    461           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
   462    462                                           pPk->aiColumn[i], iPk+i);
   463    463         }
   464    464         iKey = iPk;
   465    465       }else{
   466         -      iKey = pParse->nMem + 1;
   467         -      iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
   468         -      if( iKey>pParse->nMem ) pParse->nMem = iKey;
          466  +      iKey = ++pParse->nMem;
          467  +      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
   469    468       }
   470    469     
   471    470       if( eOnePass!=ONEPASS_OFF ){
   472    471         /* For ONEPASS, no need to store the rowid/primary-key. There is only
   473    472         ** one, so just keep it in its register(s) and fall through to the
   474    473         ** delete code.  */
   475    474         nKey = nPk; /* OP_Found will use an unpacked key */
................................................................................
   894    893     int regBase;
   895    894     int nCol;
   896    895   
   897    896     if( piPartIdxLabel ){
   898    897       if( pIdx->pPartIdxWhere ){
   899    898         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
   900    899         pParse->iSelfTab = iDataCur + 1;
   901         -      sqlite3ExprCachePush(pParse);
   902    900         sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
   903    901                               SQLITE_JUMPIFNULL);
   904    902         pParse->iSelfTab = 0;
   905    903       }else{
   906    904         *piPartIdxLabel = 0;
   907    905       }
   908    906     }
................................................................................
   941    939   ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
   942    940   ** because it was a partial index, then this routine should be called to
   943    941   ** resolve that label.
   944    942   */
   945    943   void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
   946    944     if( iLabel ){
   947    945       sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
   948         -    sqlite3ExprCachePop(pParse);
   949    946     }
   950    947   }

Changes to src/expr.c.

    54     54     if( op==TK_REGISTER ) op = pExpr->op2;
    55     55   #ifndef SQLITE_OMIT_CAST
    56     56     if( op==TK_CAST ){
    57     57       assert( !ExprHasProperty(pExpr, EP_IntValue) );
    58     58       return sqlite3AffinityType(pExpr->u.zToken, 0);
    59     59     }
    60     60   #endif
    61         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
    62         -    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
           61  +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
           62  +    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
    63     63     }
    64     64     if( op==TK_SELECT_COLUMN ){
    65     65       assert( pExpr->pLeft->flags&EP_xIsSelect );
    66     66       return sqlite3ExprAffinity(
    67     67           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
    68     68       );
    69     69     }
................................................................................
   137    137   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
   138    138     sqlite3 *db = pParse->db;
   139    139     CollSeq *pColl = 0;
   140    140     Expr *p = pExpr;
   141    141     while( p ){
   142    142       int op = p->op;
   143    143       if( p->flags & EP_Generic ) break;
          144  +    if( (op==TK_AGG_COLUMN || op==TK_COLUMN
          145  +          || op==TK_REGISTER || op==TK_TRIGGER)
          146  +     && p->y.pTab!=0
          147  +    ){
          148  +      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
          149  +      ** a TK_COLUMN but was previously evaluated and cached in a register */
          150  +      int j = p->iColumn;
          151  +      if( j>=0 ){
          152  +        const char *zColl = p->y.pTab->aCol[j].zColl;
          153  +        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
          154  +      }
          155  +      break;
          156  +    }
   144    157       if( op==TK_CAST || op==TK_UPLUS ){
   145    158         p = p->pLeft;
   146    159         continue;
   147    160       }
   148    161       if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
   149    162         pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
   150         -      break;
   151         -    }
   152         -    if( (op==TK_AGG_COLUMN || op==TK_COLUMN
   153         -          || op==TK_REGISTER || op==TK_TRIGGER)
   154         -     && p->pTab!=0
   155         -    ){
   156         -      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
   157         -      ** a TK_COLUMN but was previously evaluated and cached in a register */
   158         -      int j = p->iColumn;
   159         -      if( j>=0 ){
   160         -        const char *zColl = p->pTab->aCol[j].zColl;
   161         -        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   162         -      }
   163    163         break;
   164    164       }
   165    165       if( p->flags & EP_Collate ){
   166    166         if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
   167    167           p = p->pLeft;
   168    168         }else{
   169    169           Expr *pNext  = p->pRight;
................................................................................
   577    577     regRight = exprCodeSubselect(pParse, pRight);
   578    578   
   579    579     for(i=0; 1 /*Loop exits by "break"*/; i++){
   580    580       int regFree1 = 0, regFree2 = 0;
   581    581       Expr *pL, *pR; 
   582    582       int r1, r2;
   583    583       assert( i>=0 && i<nLeft );
   584         -    if( i>0 ) sqlite3ExprCachePush(pParse);
   585    584       r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
   586    585       r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
   587    586       codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
   588    587       testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
   589    588       testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
   590    589       testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
   591    590       testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
   592    591       testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
   593    592       testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
   594    593       sqlite3ReleaseTempReg(pParse, regFree1);
   595    594       sqlite3ReleaseTempReg(pParse, regFree2);
   596         -    if( i>0 ) sqlite3ExprCachePop(pParse);
   597    595       if( i==nLeft-1 ){
   598    596         break;
   599    597       }
   600    598       if( opx==TK_EQ ){
   601    599         sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
   602    600         p5 |= SQLITE_KEEPNULL;
   603    601       }else if( opx==TK_NE ){
................................................................................
   937    935     }
   938    936   }
   939    937   
   940    938   /*
   941    939   ** Construct a new expression node for a function with multiple
   942    940   ** arguments.
   943    941   */
   944         -Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
          942  +Expr *sqlite3ExprFunction(
          943  +  Parse *pParse,        /* Parsing context */
          944  +  ExprList *pList,      /* Argument list */
          945  +  Token *pToken,        /* Name of the function */
          946  +  int eDistinct         /* SF_Distinct or SF_ALL or 0 */
          947  +){
   945    948     Expr *pNew;
   946    949     sqlite3 *db = pParse->db;
   947    950     assert( pToken );
   948    951     pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
   949    952     if( pNew==0 ){
   950    953       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   951    954       return 0;
   952    955     }
          956  +  if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
          957  +    sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
          958  +  }
   953    959     pNew->x.pList = pList;
   954    960     ExprSetProperty(pNew, EP_HasFunc);
   955    961     assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   956    962     sqlite3ExprSetHeightAndFlags(pParse, pNew);
          963  +  if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
   957    964     return pNew;
   958    965   }
   959    966   
   960    967   /*
   961    968   ** Assign a variable number to an expression that encodes a wildcard
   962    969   ** in the original SQL statement.  
   963    970   **
................................................................................
  1041   1048   /*
  1042   1049   ** Recursively delete an expression tree.
  1043   1050   */
  1044   1051   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
  1045   1052     assert( p!=0 );
  1046   1053     /* Sanity check: Assert that the IntValue is non-negative if it exists */
  1047   1054     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
         1055  +
         1056  +  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
         1057  +  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
         1058  +          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
  1048   1059   #ifdef SQLITE_DEBUG
  1049   1060     if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
  1050   1061       assert( p->pLeft==0 );
  1051   1062       assert( p->pRight==0 );
  1052   1063       assert( p->x.pSelect==0 );
  1053   1064     }
  1054   1065   #endif
................................................................................
  1059   1070       if( p->pRight ){
  1060   1071         sqlite3ExprDeleteNN(db, p->pRight);
  1061   1072       }else if( ExprHasProperty(p, EP_xIsSelect) ){
  1062   1073         sqlite3SelectDelete(db, p->x.pSelect);
  1063   1074       }else{
  1064   1075         sqlite3ExprListDelete(db, p->x.pList);
  1065   1076       }
  1066         -    if( !ExprHasProperty(p, EP_Reduced) ){
  1067         -      sqlite3WindowDelete(db, p->pWin);
         1077  +    if( ExprHasProperty(p, EP_WinFunc) ){
         1078  +      assert( p->op==TK_FUNCTION );
         1079  +      sqlite3WindowDelete(db, p->y.pWin);
  1068   1080       }
  1069   1081     }
  1070   1082     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1071   1083     if( !ExprHasProperty(p, EP_Static) ){
  1072   1084       sqlite3DbFreeNN(db, p);
  1073   1085     }
  1074   1086   }
................................................................................
  1124   1136   static int dupedExprStructSize(Expr *p, int flags){
  1125   1137     int nSize;
  1126   1138     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
  1127   1139     assert( EXPR_FULLSIZE<=0xfff );
  1128   1140     assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
  1129   1141     if( 0==flags || p->op==TK_SELECT_COLUMN 
  1130   1142   #ifndef SQLITE_OMIT_WINDOWFUNC
  1131         -   || p->pWin 
         1143  +   || ExprHasProperty(p, EP_WinFunc)
  1132   1144   #endif
  1133   1145     ){
  1134   1146       nSize = EXPR_FULLSIZE;
  1135   1147     }else{
  1136   1148       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
  1137   1149       assert( !ExprHasProperty(p, EP_FromJoin) ); 
  1138   1150       assert( !ExprHasProperty(p, EP_MemToken) );
................................................................................
  1254   1266           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
  1255   1267         }else{
  1256   1268           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
  1257   1269         }
  1258   1270       }
  1259   1271   
  1260   1272       /* Fill in pNew->pLeft and pNew->pRight. */
  1261         -    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
         1273  +    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
  1262   1274         zAlloc += dupedExprNodeSize(p, dupFlags);
  1263   1275         if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
  1264   1276           pNew->pLeft = p->pLeft ?
  1265   1277                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
  1266   1278           pNew->pRight = p->pRight ?
  1267   1279                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
  1268   1280         }
         1281  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1282  +      if( ExprHasProperty(p, EP_WinFunc) ){
         1283  +        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
         1284  +        assert( ExprHasProperty(pNew, EP_WinFunc) );
         1285  +      }
         1286  +#endif /* SQLITE_OMIT_WINDOWFUNC */
  1269   1287         if( pzBuffer ){
  1270   1288           *pzBuffer = zAlloc;
  1271   1289         }
  1272   1290       }else{
  1273         -#ifndef SQLITE_OMIT_WINDOWFUNC
  1274         -      if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
  1275         -        pNew->pWin = 0;
  1276         -      }else{
  1277         -        pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
  1278         -      }
  1279         -#endif /* SQLITE_OMIT_WINDOWFUNC */
  1280   1291         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
  1281   1292           if( pNew->op==TK_SELECT_COLUMN ){
  1282   1293             pNew->pLeft = p->pLeft;
  1283   1294             assert( p->iColumn==0 || p->pRight==0 );
  1284   1295             assert( p->pRight==0  || p->pRight==p->pLeft );
  1285   1296           }else{
  1286   1297             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
................................................................................
  1655   1666     if( pList ){
  1656   1667       struct ExprList_item *pItem;
  1657   1668       assert( pList->nExpr>0 );
  1658   1669       pItem = &pList->a[pList->nExpr-1];
  1659   1670       assert( pItem->zName==0 );
  1660   1671       pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
  1661   1672       if( dequote ) sqlite3Dequote(pItem->zName);
         1673  +    if( IN_RENAME_OBJECT ){
         1674  +      sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
         1675  +    }
  1662   1676     }
  1663   1677   }
  1664   1678   
  1665   1679   /*
  1666   1680   ** Set the ExprList.a[].zSpan element of the most recently added item
  1667   1681   ** on the expression list.
  1668   1682   **
................................................................................
  1835   1849       case TK_COLUMN:
  1836   1850       case TK_AGG_FUNCTION:
  1837   1851       case TK_AGG_COLUMN:
  1838   1852         testcase( pExpr->op==TK_ID );
  1839   1853         testcase( pExpr->op==TK_COLUMN );
  1840   1854         testcase( pExpr->op==TK_AGG_FUNCTION );
  1841   1855         testcase( pExpr->op==TK_AGG_COLUMN );
         1856  +      if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
         1857  +        return WRC_Continue;
         1858  +      }
  1842   1859         if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
  1843   1860           return WRC_Continue;
  1844   1861         }
  1845   1862         /* Fall through */
  1846   1863       case TK_IF_NULL_ROW:
  1847   1864       case TK_REGISTER:
  1848   1865         testcase( pExpr->op==TK_REGISTER );
................................................................................
  1890   1907   ** a constant.
  1891   1908   */
  1892   1909   int sqlite3ExprIsConstant(Expr *p){
  1893   1910     return exprIsConst(p, 1, 0);
  1894   1911   }
  1895   1912   
  1896   1913   /*
  1897         -** Walk an expression tree.  Return non-zero if the expression is constant
  1898         -** that does no originate from the ON or USING clauses of a join.
  1899         -** Return 0 if it involves variables or function calls or terms from
  1900         -** an ON or USING clause.
         1914  +** Walk an expression tree.  Return non-zero if
         1915  +**
         1916  +**   (1) the expression is constant, and
         1917  +**   (2) the expression does originate in the ON or USING clause
         1918  +**       of a LEFT JOIN, and
         1919  +**   (3) the expression does not contain any EP_FixedCol TK_COLUMN
         1920  +**       operands created by the constant propagation optimization.
         1921  +**
         1922  +** When this routine returns true, it indicates that the expression
         1923  +** can be added to the pParse->pConstExpr list and evaluated once when
         1924  +** the prepared statement starts up.  See sqlite3ExprCodeAtInit().
  1901   1925   */
  1902   1926   int sqlite3ExprIsConstantNotJoin(Expr *p){
  1903   1927     return exprIsConst(p, 2, 0);
  1904   1928   }
  1905   1929   
  1906   1930   /*
  1907   1931   ** Walk an expression tree.  Return non-zero if the expression is constant
................................................................................
  1923   1947   
  1924   1948     /* Check if pExpr is identical to any GROUP BY term. If so, consider
  1925   1949     ** it constant.  */
  1926   1950     for(i=0; i<pGroupBy->nExpr; i++){
  1927   1951       Expr *p = pGroupBy->a[i].pExpr;
  1928   1952       if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
  1929   1953         CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
  1930         -      if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){
         1954  +      if( sqlite3IsBinary(pColl) ){
  1931   1955           return WRC_Prune;
  1932   1956         }
  1933   1957       }
  1934   1958     }
  1935   1959   
  1936   1960     /* Check if pExpr is a sub-select. If so, consider it variable. */
  1937   1961     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  2065   2089       case TK_INTEGER:
  2066   2090       case TK_STRING:
  2067   2091       case TK_FLOAT:
  2068   2092       case TK_BLOB:
  2069   2093         return 0;
  2070   2094       case TK_COLUMN:
  2071   2095         return ExprHasProperty(p, EP_CanBeNull) ||
  2072         -             p->pTab==0 ||  /* Reference to column of index on expression */
  2073         -             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
         2096  +             p->y.pTab==0 ||  /* Reference to column of index on expression */
         2097  +             (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
  2074   2098       default:
  2075   2099         return 1;
  2076   2100     }
  2077   2101   }
  2078   2102   
  2079   2103   /*
  2080   2104   ** Return TRUE if the given expression is a constant which would be
................................................................................
  2345   2369       if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
  2346   2370         /* The "x IN (SELECT rowid FROM table)" case */
  2347   2371         int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
  2348   2372         VdbeCoverage(v);
  2349   2373   
  2350   2374         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2351   2375         eType = IN_INDEX_ROWID;
  2352         -
         2376  +      ExplainQueryPlan((pParse, 0,
         2377  +            "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
  2353   2378         sqlite3VdbeJumpHere(v, iAddr);
  2354   2379       }else{
  2355   2380         Index *pIdx;                         /* Iterator variable */
  2356   2381         int affinity_ok = 1;
  2357   2382         int i;
  2358   2383   
  2359   2384         /* Check that the affinity that will be used to perform each 
................................................................................
  2604   2629     int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
  2605   2630     int isRowid             /* If true, LHS of IN operator is a rowid */
  2606   2631   ){
  2607   2632     int jmpIfDynamic = -1;                      /* One-time test address */
  2608   2633     int rReg = 0;                           /* Register storing resulting */
  2609   2634     Vdbe *v = sqlite3GetVdbe(pParse);
  2610   2635     if( NEVER(v==0) ) return 0;
  2611         -  sqlite3ExprCachePush(pParse);
  2612   2636   
  2613   2637     /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
  2614   2638     ** is encountered if any of the following is true:
  2615   2639     **
  2616   2640     **    *  The right-hand side is a correlated subquery
  2617   2641     **    *  The right-hand side is an expression list containing variables
  2618   2642     **    *  We are inside a trigger
................................................................................
  2740   2764               if( isRowid ){
  2741   2765                 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
  2742   2766                                   sqlite3VdbeCurrentAddr(v)+2);
  2743   2767                 VdbeCoverage(v);
  2744   2768                 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
  2745   2769               }else{
  2746   2770                 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
  2747         -              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
  2748   2771                 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
  2749   2772               }
  2750   2773             }
  2751   2774           }
  2752   2775           sqlite3ReleaseTempReg(pParse, r1);
  2753   2776           sqlite3ReleaseTempReg(pParse, r2);
  2754   2777         }
................................................................................
  2821   2844     if( rHasNullFlag ){
  2822   2845       sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
  2823   2846     }
  2824   2847   
  2825   2848     if( jmpIfDynamic>=0 ){
  2826   2849       sqlite3VdbeJumpHere(v, jmpIfDynamic);
  2827   2850     }
  2828         -  sqlite3ExprCachePop(pParse);
  2829   2851   
  2830   2852     return rReg;
  2831   2853   }
  2832   2854   #endif /* SQLITE_OMIT_SUBQUERY */
  2833   2855   
  2834   2856   #ifndef SQLITE_OMIT_SUBQUERY
  2835   2857   /*
................................................................................
  2940   2962     ** at r1.
  2941   2963     **
  2942   2964     ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
  2943   2965     ** so that the fields are in the same order as an existing index.   The
  2944   2966     ** aiMap[] array contains a mapping from the original LHS field order to
  2945   2967     ** the field order that matches the RHS index.
  2946   2968     */
  2947         -  sqlite3ExprCachePush(pParse);
  2948   2969     rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
  2949   2970     for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
  2950   2971     if( i==nVector ){
  2951   2972       /* LHS fields are not reordered */
  2952   2973       rLhs = rLhsOrig;
  2953   2974     }else{
  2954   2975       /* Need to reorder the LHS fields according to aiMap */
................................................................................
  3099   3120     }
  3100   3121   
  3101   3122     /* Jumps here in order to return true. */
  3102   3123     sqlite3VdbeJumpHere(v, addrTruthOp);
  3103   3124   
  3104   3125   sqlite3ExprCodeIN_finished:
  3105   3126     if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
  3106         -  sqlite3ExprCachePop(pParse);
  3107   3127     VdbeComment((v, "end IN expr"));
  3108   3128   sqlite3ExprCodeIN_oom_error:
  3109   3129     sqlite3DbFree(pParse->db, aiMap);
  3110   3130     sqlite3DbFree(pParse->db, zAff);
  3111   3131   }
  3112   3132   #endif /* SQLITE_OMIT_SUBQUERY */
  3113   3133   
................................................................................
  3167   3187       }else{
  3168   3188         if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
  3169   3189         sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  3170   3190       }
  3171   3191     }
  3172   3192   }
  3173   3193   
  3174         -/*
  3175         -** Erase column-cache entry number i
  3176         -*/
  3177         -static void cacheEntryClear(Parse *pParse, int i){
  3178         -  if( pParse->aColCache[i].tempReg ){
  3179         -    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
  3180         -      pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3181         -    }
  3182         -  }
  3183         -  pParse->nColCache--;
  3184         -  if( i<pParse->nColCache ){
  3185         -    pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
  3186         -  }
  3187         -}
  3188         -
  3189         -
  3190         -/*
  3191         -** Record in the column cache that a particular column from a
  3192         -** particular table is stored in a particular register.
  3193         -*/
  3194         -void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
  3195         -  int i;
  3196         -  int minLru;
  3197         -  int idxLru;
  3198         -  struct yColCache *p;
  3199         -
  3200         -  /* Unless an error has occurred, register numbers are always positive. */
  3201         -  assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
  3202         -  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
  3203         -
  3204         -  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
  3205         -  ** for testing only - to verify that SQLite always gets the same answer
  3206         -  ** with and without the column cache.
  3207         -  */
  3208         -  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
  3209         -
  3210         -  /* First replace any existing entry.
  3211         -  **
  3212         -  ** Actually, the way the column cache is currently used, we are guaranteed
  3213         -  ** that the object will never already be in cache.  Verify this guarantee.
  3214         -  */
  3215         -#ifndef NDEBUG
  3216         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3217         -    assert( p->iTable!=iTab || p->iColumn!=iCol );
  3218         -  }
  3219         -#endif
  3220         -
  3221         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  3222         -  /* Add a SetTabCol opcode for run-time verification that the column
  3223         -  ** cache is working correctly.
  3224         -  */
  3225         -  sqlite3VdbeAddOp3(pParse->pVdbe, OP_SetTabCol, iTab, iCol, iReg);
  3226         -#endif
  3227         -
  3228         -  /* If the cache is already full, delete the least recently used entry */
  3229         -  if( pParse->nColCache>=SQLITE_N_COLCACHE ){
  3230         -    minLru = 0x7fffffff;
  3231         -    idxLru = -1;
  3232         -    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
  3233         -      if( p->lru<minLru ){
  3234         -        idxLru = i;
  3235         -        minLru = p->lru;
  3236         -      }
  3237         -    }
  3238         -    p = &pParse->aColCache[idxLru];
  3239         -  }else{
  3240         -    p = &pParse->aColCache[pParse->nColCache++];
  3241         -  }
  3242         -
  3243         -  /* Add the new entry to the end of the cache */
  3244         -  p->iLevel = pParse->iCacheLevel;
  3245         -  p->iTable = iTab;
  3246         -  p->iColumn = iCol;
  3247         -  p->iReg = iReg;
  3248         -  p->tempReg = 0;
  3249         -  p->lru = pParse->iCacheCnt++;
  3250         -}
  3251         -
  3252         -/*
  3253         -** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
  3254         -** Purge the range of registers from the column cache.
  3255         -*/
  3256         -void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
  3257         -  int i = 0;
  3258         -  while( i<pParse->nColCache ){
  3259         -    struct yColCache *p = &pParse->aColCache[i];
  3260         -    if( p->iReg >= iReg && p->iReg < iReg+nReg ){
  3261         -      cacheEntryClear(pParse, i);
  3262         -    }else{
  3263         -      i++;
  3264         -    }
  3265         -  }
  3266         -}
  3267         -
  3268         -/*
  3269         -** Remember the current column cache context.  Any new entries added
  3270         -** added to the column cache after this call are removed when the
  3271         -** corresponding pop occurs.
  3272         -*/
  3273         -void sqlite3ExprCachePush(Parse *pParse){
  3274         -  pParse->iCacheLevel++;
  3275         -#ifdef SQLITE_DEBUG
  3276         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3277         -    printf("PUSH to %d\n", pParse->iCacheLevel);
  3278         -  }
  3279         -#endif
  3280         -}
  3281         -
  3282         -/*
  3283         -** Remove from the column cache any entries that were added since the
  3284         -** the previous sqlite3ExprCachePush operation.  In other words, restore
  3285         -** the cache to the state it was in prior the most recent Push.
  3286         -*/
  3287         -void sqlite3ExprCachePop(Parse *pParse){
  3288         -  int i = 0;
  3289         -  assert( pParse->iCacheLevel>=1 );
  3290         -  pParse->iCacheLevel--;
  3291         -#ifdef SQLITE_DEBUG
  3292         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3293         -    printf("POP  to %d\n", pParse->iCacheLevel);
  3294         -  }
  3295         -#endif
  3296         -  while( i<pParse->nColCache ){
  3297         -    if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
  3298         -      cacheEntryClear(pParse, i);
  3299         -    }else{
  3300         -      i++;
  3301         -    }
  3302         -  }
  3303         -}
  3304         -
  3305         -/*
  3306         -** When a cached column is reused, make sure that its register is
  3307         -** no longer available as a temp register.  ticket #3879:  that same
  3308         -** register might be in the cache in multiple places, so be sure to
  3309         -** get them all.
  3310         -*/
  3311         -static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
  3312         -  int i;
  3313         -  struct yColCache *p;
  3314         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3315         -    if( p->iReg==iReg ){
  3316         -      p->tempReg = 0;
  3317         -    }
  3318         -  }
  3319         -}
  3320   3194   
  3321   3195   /* Generate code that will load into register regOut a value that is
  3322   3196   ** appropriate for the iIdxCol-th column of index pIdx.
  3323   3197   */
  3324   3198   void sqlite3ExprCodeLoadIndexColumn(
  3325   3199     Parse *pParse,  /* The parsing context */
  3326   3200     Index *pIdx,    /* The index whose column is to be loaded */
................................................................................
  3368   3242     if( iCol>=0 ){
  3369   3243       sqlite3ColumnDefault(v, pTab, iCol, regOut);
  3370   3244     }
  3371   3245   }
  3372   3246   
  3373   3247   /*
  3374   3248   ** Generate code that will extract the iColumn-th column from
  3375         -** table pTab and store the column value in a register. 
  3376         -**
  3377         -** An effort is made to store the column value in register iReg.  This
  3378         -** is not garanteeed for GetColumn() - the result can be stored in
  3379         -** any register.  But the result is guaranteed to land in register iReg
  3380         -** for GetColumnToReg().
         3249  +** table pTab and store the column value in register iReg. 
  3381   3250   **
  3382   3251   ** There must be an open cursor to pTab in iTable when this routine
  3383   3252   ** is called.  If iColumn<0 then code is generated that extracts the rowid.
  3384   3253   */
  3385   3254   int sqlite3ExprCodeGetColumn(
  3386   3255     Parse *pParse,   /* Parsing and code generating context */
  3387   3256     Table *pTab,     /* Description of the table we are reading from */
  3388   3257     int iColumn,     /* Index of the table column */
  3389   3258     int iTable,      /* The cursor pointing to the table */
  3390   3259     int iReg,        /* Store results here */
  3391   3260     u8 p5            /* P5 value for OP_Column + FLAGS */
  3392   3261   ){
  3393   3262     Vdbe *v = pParse->pVdbe;
  3394         -  int i;
  3395         -  struct yColCache *p;
  3396         -
  3397         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3398         -    if( p->iTable==iTable && p->iColumn==iColumn ){
  3399         -      p->lru = pParse->iCacheCnt++;
  3400         -      sqlite3ExprCachePinRegister(pParse, p->iReg);
  3401         -#ifdef SQLITE_DEBUG_COLUMNCACHE
  3402         -      sqlite3VdbeAddOp3(v, OP_VerifyTabCol, iTable, iColumn, p->iReg);
  3403         -#endif
  3404         -      return p->iReg;
  3405         -    }
  3406         -  }  
  3407   3263     assert( v!=0 );
  3408   3264     sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
  3409   3265     if( p5 ){
  3410   3266       sqlite3VdbeChangeP5(v, p5);
  3411         -  }else{   
  3412         -    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
  3413   3267     }
  3414   3268     return iReg;
  3415   3269   }
  3416         -void sqlite3ExprCodeGetColumnToReg(
  3417         -  Parse *pParse,   /* Parsing and code generating context */
  3418         -  Table *pTab,     /* Description of the table we are reading from */
  3419         -  int iColumn,     /* Index of the table column */
  3420         -  int iTable,      /* The cursor pointing to the table */
  3421         -  int iReg         /* Store results here */
  3422         -){
  3423         -  int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
  3424         -  if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
  3425         -}
  3426         -
  3427         -
  3428         -/*
  3429         -** Clear all column cache entries.
  3430         -*/
  3431         -void sqlite3ExprCacheClear(Parse *pParse){
  3432         -  int i;
  3433         -
  3434         -#ifdef SQLITE_DEBUG
  3435         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3436         -    printf("CLEAR\n");
  3437         -  }
  3438         -#endif
  3439         -  for(i=0; i<pParse->nColCache; i++){
  3440         -    if( pParse->aColCache[i].tempReg
  3441         -     && pParse->nTempReg<ArraySize(pParse->aTempReg)
  3442         -    ){
  3443         -       pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3444         -    }
  3445         -  }
  3446         -  pParse->nColCache = 0;
  3447         -}
  3448         -
  3449         -/*
  3450         -** Record the fact that an affinity change has occurred on iCount
  3451         -** registers starting with iStart.
  3452         -*/
  3453         -void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
  3454         -  sqlite3ExprCacheRemove(pParse, iStart, iCount);
  3455         -}
  3456   3270   
  3457   3271   /*
  3458   3272   ** Generate code to move content from registers iFrom...iFrom+nReg-1
  3459         -** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
         3273  +** over to iTo..iTo+nReg-1.
  3460   3274   */
  3461   3275   void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
  3462   3276     assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
  3463   3277     sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
  3464         -  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
  3465   3278   }
  3466         -
  3467         -#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  3468         -/*
  3469         -** Return true if any register in the range iFrom..iTo (inclusive)
  3470         -** is used as part of the column cache.
  3471         -**
  3472         -** This routine is used within assert() and testcase() macros only
  3473         -** and does not appear in a normal build.
  3474         -*/
  3475         -static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
  3476         -  int i;
  3477         -  struct yColCache *p;
  3478         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3479         -    int r = p->iReg;
  3480         -    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
  3481         -  }
  3482         -  return 0;
  3483         -}
  3484         -#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
  3485         -
  3486   3279   
  3487   3280   /*
  3488   3281   ** Convert a scalar expression node to a TK_REGISTER referencing
  3489   3282   ** register iReg.  The caller must ensure that iReg already contains
  3490   3283   ** the correct value for the expression.
  3491   3284   */
  3492   3285   static void exprToRegister(Expr *p, int iReg){
................................................................................
  3577   3370                                 pCol->iSorterColumn, target);
  3578   3371           return target;
  3579   3372         }
  3580   3373         /* Otherwise, fall thru into the TK_COLUMN case */
  3581   3374       }
  3582   3375       case TK_COLUMN: {
  3583   3376         int iTab = pExpr->iTable;
         3377  +      if( ExprHasProperty(pExpr, EP_FixedCol) ){
         3378  +        /* This COLUMN expression is really a constant due to WHERE clause
         3379  +        ** constraints, and that constant is coded by the pExpr->pLeft
         3380  +        ** expresssion.  However, make sure the constant has the correct
         3381  +        ** datatype by applying the Affinity of the table column to the
         3382  +        ** constant.
         3383  +        */
         3384  +        int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
         3385  +        int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
         3386  +        if( aff!=SQLITE_AFF_BLOB ){
         3387  +          static const char zAff[] = "B\000C\000D\000E";
         3388  +          assert( SQLITE_AFF_BLOB=='A' );
         3389  +          assert( SQLITE_AFF_TEXT=='B' );
         3390  +          if( iReg!=target ){
         3391  +            sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
         3392  +            iReg = target;
         3393  +          }
         3394  +          sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
         3395  +                            &zAff[(aff-'B')*2], P4_STATIC);
         3396  +        }
         3397  +        return iReg;
         3398  +      }
  3584   3399         if( iTab<0 ){
  3585   3400           if( pParse->iSelfTab<0 ){
  3586   3401             /* Generating CHECK constraints or inserting into partial index */
  3587   3402             return pExpr->iColumn - pParse->iSelfTab;
  3588   3403           }else{
  3589   3404             /* Coding an expression that is part of an index where column names
  3590   3405             ** in the index refer to the table to which the index belongs */
  3591   3406             iTab = pParse->iSelfTab - 1;
  3592   3407           }
  3593   3408         }
  3594         -      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
         3409  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
  3595   3410                                  pExpr->iColumn, iTab, target,
  3596   3411                                  pExpr->op2);
  3597   3412       }
  3598   3413       case TK_INTEGER: {
  3599   3414         codeInteger(pParse, pExpr, 0, target);
  3600   3415         return target;
  3601   3416       }
................................................................................
  3657   3472         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  3658   3473         if( inReg!=target ){
  3659   3474           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
  3660   3475           inReg = target;
  3661   3476         }
  3662   3477         sqlite3VdbeAddOp2(v, OP_Cast, target,
  3663   3478                           sqlite3AffinityType(pExpr->u.zToken, 0));
  3664         -      testcase( usedAsColumnCache(pParse, inReg, inReg) );
  3665         -      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
  3666   3479         return inReg;
  3667   3480       }
  3668   3481   #endif /* SQLITE_OMIT_CAST */
  3669   3482       case TK_IS:
  3670   3483       case TK_ISNOT:
  3671   3484         op = (op==TK_IS) ? TK_EQ : TK_NE;
  3672   3485         p5 = SQLITE_NULLEQ;
................................................................................
  3803   3616         u32 constMask = 0;     /* Mask of function arguments that are constant */
  3804   3617         int i;                 /* Loop counter */
  3805   3618         sqlite3 *db = pParse->db;  /* The database connection */
  3806   3619         u8 enc = ENC(db);      /* The text encoding used by this database */
  3807   3620         CollSeq *pColl = 0;    /* A collating sequence */
  3808   3621   
  3809   3622   #ifndef SQLITE_OMIT_WINDOWFUNC
  3810         -      if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
  3811         -        return pExpr->pWin->regResult;
         3623  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
         3624  +        return pExpr->y.pWin->regResult;
  3812   3625         }
  3813   3626   #endif
  3814   3627   
  3815   3628         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
  3816   3629           /* SQL functions can be expensive. So try to move constant functions
  3817   3630           ** out of the inner loop, even if that means an extra OP_Copy. */
  3818   3631           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
................................................................................
  3844   3657         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
  3845   3658           int endCoalesce = sqlite3VdbeMakeLabel(v);
  3846   3659           assert( nFarg>=2 );
  3847   3660           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  3848   3661           for(i=1; i<nFarg; i++){
  3849   3662             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
  3850   3663             VdbeCoverage(v);
  3851         -          sqlite3ExprCacheRemove(pParse, target, 1);
  3852         -          sqlite3ExprCachePush(pParse);
  3853   3664             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
  3854         -          sqlite3ExprCachePop(pParse);
  3855   3665           }
  3856   3666           sqlite3VdbeResolveLabel(v, endCoalesce);
  3857   3667           break;
  3858   3668         }
  3859   3669   
  3860   3670         /* The UNLIKELY() function is a no-op.  The result is the value
  3861   3671         ** of the first argument.
................................................................................
  3913   3723               assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
  3914   3724               testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
  3915   3725               pFarg->a[0].pExpr->op2 = 
  3916   3726                     pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
  3917   3727             }
  3918   3728           }
  3919   3729   
  3920         -        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
  3921   3730           sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
  3922   3731                                   SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
  3923         -        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
  3924   3732         }else{
  3925   3733           r1 = 0;
  3926   3734         }
  3927   3735   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3928   3736         /* Possibly overload the function if the first argument is
  3929   3737         ** a virtual table column.
  3930   3738         **
................................................................................
  3933   3741         ** see if it is a column in a virtual table.  This is done because
  3934   3742         ** the left operand of infix functions (the operand we want to
  3935   3743         ** control overloading) ends up as the second argument to the
  3936   3744         ** function.  The expression "A glob B" is equivalent to 
  3937   3745         ** "glob(B,A).  We want to use the A in "A glob B" to test
  3938   3746         ** for function overloading.  But we use the B term in "glob(B,A)".
  3939   3747         */
  3940         -      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
         3748  +      if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
  3941   3749           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
  3942   3750         }else if( nFarg>0 ){
  3943   3751           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
  3944   3752         }
  3945   3753   #endif
  3946   3754         if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  3947   3755           if( !pColl ) pColl = db->pDfltColl; 
................................................................................
  4052   3860         **
  4053   3861         ** Then p1 is interpreted as follows:
  4054   3862         **
  4055   3863         **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
  4056   3864         **   p1==1   ->    old.a         p1==4   ->    new.a
  4057   3865         **   p1==2   ->    old.b         p1==5   ->    new.b       
  4058   3866         */
  4059         -      Table *pTab = pExpr->pTab;
         3867  +      Table *pTab = pExpr->y.pTab;
  4060   3868         int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
  4061   3869   
  4062   3870         assert( pExpr->iTable==0 || pExpr->iTable==1 );
  4063   3871         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
  4064   3872         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
  4065   3873         assert( p1>=0 && p1<(pTab->nCol*2+2) );
  4066   3874   
  4067   3875         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
  4068   3876         VdbeComment((v, "r[%d]=%s.%s", target,
  4069   3877           (pExpr->iTable ? "new" : "old"),
  4070         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
         3878  +        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
  4071   3879         ));
  4072   3880   
  4073   3881   #ifndef SQLITE_OMIT_FLOATING_POINT
  4074   3882         /* If the column has REAL affinity, it may currently be stored as an
  4075   3883         ** integer. Use OP_RealAffinity to make sure it is really real.
  4076   3884         **
  4077   3885         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
  4089   3897         sqlite3ErrorMsg(pParse, "row value misused");
  4090   3898         break;
  4091   3899       }
  4092   3900   
  4093   3901       case TK_IF_NULL_ROW: {
  4094   3902         int addrINR;
  4095   3903         addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
  4096         -      sqlite3ExprCachePush(pParse);
  4097   3904         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  4098         -      sqlite3ExprCachePop(pParse);
  4099   3905         sqlite3VdbeJumpHere(v, addrINR);
  4100   3906         sqlite3VdbeChangeP3(v, addrINR, inReg);
  4101   3907         break;
  4102   3908       }
  4103   3909   
  4104   3910       /*
  4105   3911       ** Form A:
................................................................................
  4128   3934         int nExpr;                        /* 2x number of WHEN terms */
  4129   3935         int i;                            /* Loop counter */
  4130   3936         ExprList *pEList;                 /* List of WHEN terms */
  4131   3937         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  4132   3938         Expr opCompare;                   /* The X==Ei expression */
  4133   3939         Expr *pX;                         /* The X expression */
  4134   3940         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
  4135         -      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
  4136   3941   
  4137   3942         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  4138   3943         assert(pExpr->x.pList->nExpr > 0);
  4139   3944         pEList = pExpr->x.pList;
  4140   3945         aListelem = pEList->a;
  4141   3946         nExpr = pEList->nExpr;
  4142   3947         endLabel = sqlite3VdbeMakeLabel(v);
................................................................................
  4152   3957           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  4153   3958           ** The value in regFree1 might get SCopy-ed into the file result.
  4154   3959           ** So make sure that the regFree1 register is not reused for other
  4155   3960           ** purposes and possibly overwritten.  */
  4156   3961           regFree1 = 0;
  4157   3962         }
  4158   3963         for(i=0; i<nExpr-1; i=i+2){
  4159         -        sqlite3ExprCachePush(pParse);
  4160   3964           if( pX ){
  4161   3965             assert( pTest!=0 );
  4162   3966             opCompare.pRight = aListelem[i].pExpr;
  4163   3967           }else{
  4164   3968             pTest = aListelem[i].pExpr;
  4165   3969           }
  4166   3970           nextCase = sqlite3VdbeMakeLabel(v);
  4167   3971           testcase( pTest->op==TK_COLUMN );
  4168   3972           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
  4169   3973           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
  4170   3974           sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
  4171   3975           sqlite3VdbeGoto(v, endLabel);
  4172         -        sqlite3ExprCachePop(pParse);
  4173   3976           sqlite3VdbeResolveLabel(v, nextCase);
  4174   3977         }
  4175   3978         if( (nExpr&1)!=0 ){
  4176         -        sqlite3ExprCachePush(pParse);
  4177   3979           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
  4178         -        sqlite3ExprCachePop(pParse);
  4179   3980         }else{
  4180   3981           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  4181   3982         }
  4182         -      assert( pParse->db->mallocFailed || pParse->nErr>0 
  4183         -           || pParse->iCacheLevel==iCacheLevel );
  4184   3983         sqlite3VdbeResolveLabel(v, endLabel);
  4185   3984         break;
  4186   3985       }
  4187   3986   #ifndef SQLITE_OMIT_TRIGGER
  4188   3987       case TK_RAISE: {
  4189   3988         assert( pExpr->affinity==OE_Rollback 
  4190   3989              || pExpr->affinity==OE_Abort
................................................................................
  4326   4125   /*
  4327   4126   ** Generate code that will evaluate expression pExpr and store the
  4328   4127   ** results in register target.  The results are guaranteed to appear
  4329   4128   ** in register target.  If the expression is constant, then this routine
  4330   4129   ** might choose to code the expression at initialization time.
  4331   4130   */
  4332   4131   void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
  4333         -  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
         4132  +  if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
  4334   4133       sqlite3ExprCodeAtInit(pParse, pExpr, target);
  4335   4134     }else{
  4336   4135       sqlite3ExprCode(pParse, pExpr, target);
  4337   4136     }
  4338   4137   }
  4339   4138   
  4340   4139   /*
................................................................................
  4408   4207       if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
  4409   4208         if( flags & SQLITE_ECEL_OMITREF ){
  4410   4209           i--;
  4411   4210           n--;
  4412   4211         }else{
  4413   4212           sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
  4414   4213         }
  4415         -    }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
         4214  +    }else if( (flags & SQLITE_ECEL_FACTOR)!=0
         4215  +           && sqlite3ExprIsConstantNotJoin(pExpr)
         4216  +    ){
  4416   4217         sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
  4417   4218       }else{
  4418   4219         int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
  4419   4220         if( inReg!=target+i ){
  4420   4221           VdbeOp *pOp;
  4421   4222           if( copyOp==OP_Copy
  4422   4223            && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
................................................................................
  4534   4335     if( NEVER(pExpr==0) ) return;  /* No way this can happen */
  4535   4336     op = pExpr->op;
  4536   4337     switch( op ){
  4537   4338       case TK_AND: {
  4538   4339         int d2 = sqlite3VdbeMakeLabel(v);
  4539   4340         testcase( jumpIfNull==0 );
  4540   4341         sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
  4541         -      sqlite3ExprCachePush(pParse);
  4542   4342         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4543   4343         sqlite3VdbeResolveLabel(v, d2);
  4544         -      sqlite3ExprCachePop(pParse);
  4545   4344         break;
  4546   4345       }
  4547   4346       case TK_OR: {
  4548   4347         testcase( jumpIfNull==0 );
  4549   4348         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4550         -      sqlite3ExprCachePush(pParse);
  4551   4349         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4552         -      sqlite3ExprCachePop(pParse);
  4553   4350         break;
  4554   4351       }
  4555   4352       case TK_NOT: {
  4556   4353         testcase( jumpIfNull==0 );
  4557   4354         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4558   4355         break;
  4559   4356       }
................................................................................
  4704   4501     assert( pExpr->op!=TK_GT || op==OP_Le );
  4705   4502     assert( pExpr->op!=TK_GE || op==OP_Lt );
  4706   4503   
  4707   4504     switch( pExpr->op ){
  4708   4505       case TK_AND: {
  4709   4506         testcase( jumpIfNull==0 );
  4710   4507         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4711         -      sqlite3ExprCachePush(pParse);
  4712   4508         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4713         -      sqlite3ExprCachePop(pParse);
  4714   4509         break;
  4715   4510       }
  4716   4511       case TK_OR: {
  4717   4512         int d2 = sqlite3VdbeMakeLabel(v);
  4718   4513         testcase( jumpIfNull==0 );
  4719   4514         sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
  4720         -      sqlite3ExprCachePush(pParse);
  4721   4515         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4722   4516         sqlite3VdbeResolveLabel(v, d2);
  4723         -      sqlite3ExprCachePop(pParse);
  4724   4517         break;
  4725   4518       }
  4726   4519       case TK_NOT: {
  4727   4520         testcase( jumpIfNull==0 );
  4728   4521         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4729   4522         break;
  4730   4523       }
................................................................................
  4929   4722         return 1;
  4930   4723       }
  4931   4724       return 2;
  4932   4725     }
  4933   4726     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
  4934   4727       if( pA->op==TK_FUNCTION ){
  4935   4728         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
         4729  +#ifndef SQLITE_OMIT_WINDOWFUNC
         4730  +      /* Justification for the assert():
         4731  +      ** window functions have p->op==TK_FUNCTION but aggregate functions
         4732  +      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
         4733  +      ** function and a window function should have failed before reaching
         4734  +      ** this point.  And, it is not possible to have a window function and
         4735  +      ** a scalar function with the same name and number of arguments.  So
         4736  +      ** if we reach this point, either A and B both window functions or
         4737  +      ** neither are a window functions. */
         4738  +      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
         4739  +      if( ExprHasProperty(pA,EP_WinFunc) ){
         4740  +        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
         4741  +      }
         4742  +#endif
  4936   4743       }else if( pA->op==TK_COLLATE ){
  4937   4744         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4938   4745       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
  4939   4746         return 2;
  4940   4747       }
  4941   4748     }
  4942   4749     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
  4943   4750     if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
  4944   4751       if( combinedFlags & EP_xIsSelect ) return 2;
  4945         -    if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
         4752  +    if( (combinedFlags & EP_FixedCol)==0
         4753  +     && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
  4946   4754       if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
  4947   4755       if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
  4948   4756       assert( (combinedFlags & EP_Reduced)==0 );
  4949   4757       if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
  4950   4758         if( pA->iColumn!=pB->iColumn ) return 2;
  4951   4759         if( pA->iTable!=pB->iTable 
  4952   4760          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
  4953   4761       }
  4954         -#ifndef SQLITE_OMIT_WINDOWFUNC
  4955         -    /* Justification for the assert():
  4956         -    ** window functions have p->op==TK_FUNCTION but aggregate functions
  4957         -    ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
  4958         -    ** function and a window function should have failed before reaching
  4959         -    ** this point.  And, it is not possible to have a window function and
  4960         -    ** a scalar function with the same name and number of arguments.  So
  4961         -    ** if we reach this point, either A and B both window functions or
  4962         -    ** neither are a window functions. */
  4963         -    assert( (pA->pWin==0)==(pB->pWin==0) );
  4964         -
  4965         -    if( pA->pWin!=0 ){
  4966         -      if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2;
  4967         -    }
  4968         -#endif
  4969   4762     }
  4970   4763     return 0;
  4971   4764   }
  4972   4765   
  4973   4766   /*
  4974   4767   ** Compare two ExprList objects.  Return 0 if they are identical and 
  4975   4768   ** non-zero if they differ in any way.
................................................................................
  5052   4845     }
  5053   4846     return 0;
  5054   4847   }
  5055   4848   
  5056   4849   /*
  5057   4850   ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
  5058   4851   ** If the expression node requires that the table at pWalker->iCur
  5059         -** have a non-NULL column, then set pWalker->eCode to 1 and abort.
         4852  +** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
         4853  +**
         4854  +** This routine controls an optimization.  False positives (setting
         4855  +** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
         4856  +** (never setting pWalker->eCode) is a harmless missed optimization.
  5060   4857   */
  5061   4858   static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
  5062         -  /* This routine is only called for WHERE clause expressions and so it
  5063         -  ** cannot have any TK_AGG_COLUMN entries because those are only found
  5064         -  ** in HAVING clauses.  We can get a TK_AGG_FUNCTION in a WHERE clause,
  5065         -  ** but that is an illegal construct and the query will be rejected at
  5066         -  ** a later stage of processing, so the TK_AGG_FUNCTION case does not
  5067         -  ** need to be considered here. */
  5068         -  assert( pExpr->op!=TK_AGG_COLUMN );
         4859  +  testcase( pExpr->op==TK_AGG_COLUMN );
  5069   4860     testcase( pExpr->op==TK_AGG_FUNCTION );
  5070         -
  5071   4861     if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
  5072   4862     switch( pExpr->op ){
  5073   4863       case TK_ISNOT:
  5074   4864       case TK_NOT:
  5075   4865       case TK_ISNULL:
  5076   4866       case TK_IS:
  5077   4867       case TK_OR:
................................................................................
  5105   4895       case TK_GE:
  5106   4896         testcase( pExpr->op==TK_EQ );
  5107   4897         testcase( pExpr->op==TK_NE );
  5108   4898         testcase( pExpr->op==TK_LT );
  5109   4899         testcase( pExpr->op==TK_LE );
  5110   4900         testcase( pExpr->op==TK_GT );
  5111   4901         testcase( pExpr->op==TK_GE );
  5112         -      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
  5113         -       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
         4902  +      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
         4903  +       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
  5114   4904         ){
  5115   4905          return WRC_Prune;
  5116   4906         }
  5117   4907       default:
  5118   4908         return WRC_Continue;
  5119   4909     }
  5120   4910   }
................................................................................
  5337   5127                   break;
  5338   5128                 }
  5339   5129               }
  5340   5130               if( (k>=pAggInfo->nColumn)
  5341   5131                && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
  5342   5132               ){
  5343   5133                 pCol = &pAggInfo->aCol[k];
  5344         -              pCol->pTab = pExpr->pTab;
         5134  +              pCol->pTab = pExpr->y.pTab;
  5345   5135                 pCol->iTable = pExpr->iTable;
  5346   5136                 pCol->iColumn = pExpr->iColumn;
  5347   5137                 pCol->iMem = ++pParse->nMem;
  5348   5138                 pCol->iSorterColumn = -1;
  5349   5139                 pCol->pExpr = pExpr;
  5350   5140                 if( pAggInfo->pGroupBy ){
  5351   5141                   int j, n;
................................................................................
  5483   5273     }
  5484   5274     re