/ Check-in [ddf6a54e]
Login

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

Overview
Comment:Merge changes for the 3.25.0 release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA3-256: ddf6a54ef3838e6f78d132da96a330efd541fe5401e1f849f36925f01bc28d45
User & Date: drh 2018-10-09 22:50:26
Context
2018-10-09
22:58
Merge enhancements from trunk. check-in: 1b60e7a9 user: drh tags: apple-osx
22:50
Merge changes for the 3.25.0 release. check-in: ddf6a54e user: drh tags: apple-osx
2018-09-15
04:01
Version 3.25.0 check-in: b63af6c3 user: drh tags: trunk, release, version-3.25.0
2018-06-04
14:10
Merge changes from trunk. check-in: 95fbac39 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   186    186            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   187    187            random.lo resolve.lo rowset.lo rtree.lo \
   188    188            sqlite3session.lo select.lo sqlite3rbu.lo status.lo stmt.lo \
   189    189            table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   190    190            update.lo upsert.lo util.lo vacuum.lo \
   191    191            vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   192    192            vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
   193         -         utf.lo vtab.lo
          193  +         window.lo utf.lo vtab.lo
   194    194   
   195    195   # Object files for the amalgamation.
   196    196   #
   197    197   LIBOBJS1 = sqlite3.lo
   198    198   
   199    199   # Determine the real value of LIBOBJ based on the 'configure' script
   200    200   #
................................................................................
   300    300     $(TOP)/src/vxworks.h \
   301    301     $(TOP)/src/wal.c \
   302    302     $(TOP)/src/wal.h \
   303    303     $(TOP)/src/walker.c \
   304    304     $(TOP)/src/where.c \
   305    305     $(TOP)/src/wherecode.c \
   306    306     $(TOP)/src/whereexpr.c \
   307         -  $(TOP)/src/whereInt.h
          307  +  $(TOP)/src/whereInt.h \
          308  +  $(TOP)/src/window.c
   308    309   
   309    310   # Source code for extensions
   310    311   #
   311    312   SRC += \
   312    313     $(TOP)/ext/fts1/fts1.c \
   313    314     $(TOP)/ext/fts1/fts1.h \
   314    315     $(TOP)/ext/fts1/fts1_hash.c \
................................................................................
   345    346     $(TOP)/ext/fts3/fts3_unicode2.c \
   346    347     $(TOP)/ext/fts3/fts3_write.c
   347    348   SRC += \
   348    349     $(TOP)/ext/icu/sqliteicu.h \
   349    350     $(TOP)/ext/icu/icu.c
   350    351   SRC += \
   351    352     $(TOP)/ext/rtree/rtree.h \
   352         -  $(TOP)/ext/rtree/rtree.c
          353  +  $(TOP)/ext/rtree/rtree.c \
          354  +  $(TOP)/ext/rtree/geopoly.c
   353    355   SRC += \
   354    356     $(TOP)/ext/sqlrr/sqlrr.h \
   355    357     $(TOP)/ext/sqlrr/sqlrr.c
   356    358   SRC += \
   357    359     $(TOP)/ext/session/sqlite3session.c \
   358    360     $(TOP)/ext/session/sqlite3session.h
   359    361   SRC += \
................................................................................
   417    419     $(TOP)/src/test_superlock.c \
   418    420     $(TOP)/src/test_syscall.c \
   419    421     $(TOP)/src/test_tclsh.c \
   420    422     $(TOP)/src/test_tclvar.c \
   421    423     $(TOP)/src/test_thread.c \
   422    424     $(TOP)/src/test_vfs.c \
   423    425     $(TOP)/src/test_windirent.c \
          426  +  $(TOP)/src/test_window.c \
   424    427     $(TOP)/src/test_wsd.c       \
   425    428     $(TOP)/ext/fts3/fts3_term.c \
   426    429     $(TOP)/ext/fts3/fts3_test.c  \
   427    430     $(TOP)/ext/session/test_session.c \
   428    431     $(TOP)/ext/rbu/test_rbu.c 
   429    432   
   430    433   # Statically linked extensions
................................................................................
   492    495     $(TOP)/src/vdbeaux.c \
   493    496     $(TOP)/src/vdbe.c \
   494    497     $(TOP)/src/vdbemem.c \
   495    498     $(TOP)/src/vdbetrace.c \
   496    499     $(TOP)/src/where.c \
   497    500     $(TOP)/src/wherecode.c \
   498    501     $(TOP)/src/whereexpr.c \
          502  +  $(TOP)/src/window.c \
   499    503     parse.c \
   500    504     $(TOP)/ext/fts3/fts3.c \
   501    505     $(TOP)/ext/fts3/fts3_aux.c \
   502    506     $(TOP)/ext/fts3/fts3_expr.c \
   503    507     $(TOP)/ext/fts3/fts3_term.c \
   504    508     $(TOP)/ext/fts3/fts3_tokenizer.c \
   505    509     $(TOP)/ext/fts3/fts3_write.c \
................................................................................
   548    552     $(TOP)/ext/fts2/fts2_tokenizer.h
   549    553   EXTHDR += \
   550    554     $(TOP)/ext/fts3/fts3.h \
   551    555     $(TOP)/ext/fts3/fts3Int.h \
   552    556     $(TOP)/ext/fts3/fts3_hash.h \
   553    557     $(TOP)/ext/fts3/fts3_tokenizer.h
   554    558   EXTHDR += \
   555         -  $(TOP)/ext/rtree/rtree.h
          559  +  $(TOP)/ext/rtree/rtree.h \
          560  +  $(TOP)/ext/rtree/geopoly.c
   556    561   EXTHDR += \
   557    562     $(TOP)/ext/icu/sqliteicu.h
   558    563   EXTHDR += \
   559    564     $(TOP)/ext/rtree/sqlite3rtree.h
   560    565   EXTHDR += \
   561    566     $(TOP)/ext/sqlrr/sqlrr.h
   562    567   
................................................................................
   976    981   
   977    982   wherecode.lo:	$(TOP)/src/wherecode.c $(HDR)
   978    983   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/wherecode.c
   979    984   
   980    985   whereexpr.lo:	$(TOP)/src/whereexpr.c $(HDR)
   981    986   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/whereexpr.c
   982    987   
          988  +window.lo:	$(TOP)/src/window.c $(HDR)
          989  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/window.c
          990  +
   983    991   tclsqlite.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   984    992   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -c $(TOP)/src/tclsqlite.c
   985    993   
   986    994   tclsqlite-shell.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   987    995   	$(LTCOMPILE) -DTCLSH -o $@ -c $(TOP)/src/tclsqlite.c
   988    996   
   989    997   tclsqlite-stubs.lo:	$(TOP)/src/tclsqlite.c $(HDR)
................................................................................
  1279   1287   
  1280   1288   changeset$(TEXE):	$(TOP)/ext/session/changeset.c sqlite3.lo
  1281   1289   	$(LTLINK) -o $@ $(TOP)/ext/session/changeset.c sqlite3.lo $(TLIBS)
  1282   1290   
  1283   1291   rollback-test$(TEXE):	$(TOP)/tool/rollback-test.c sqlite3.lo
  1284   1292   	$(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS)
  1285   1293   
         1294  +atrc$(TEXX): $(TOP)/test/atrc.c sqlite3.lo
         1295  +	$(LTLINK) -o $@ $(TOP)/test/atrc.c sqlite3.lo $(TLIBS)
         1296  +
  1286   1297   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h
  1287   1298   	$(LTLINK) -I. -o $@ $(TOP)/tool/logest.c
  1288   1299   
  1289   1300   wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.lo
  1290   1301   	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.lo $(TLIBS)
  1291   1302   
  1292   1303   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         -         utf.lo vtab.lo
         1217  +         window.lo utf.lo vtab.lo
  1200   1218   # <</mark>>
  1201   1219   
  1202   1220   # Object files for the amalgamation.
  1203   1221   #
  1204   1222   LIBOBJS1 = sqlite3.lo
  1205   1223   
  1206   1224   # Determine the real value of LIBOBJ based on the 'configure' script
................................................................................
  1303   1321     $(TOP)\src\vdbesort.c \
  1304   1322     $(TOP)\src\vdbetrace.c \
  1305   1323     $(TOP)\src\vtab.c \
  1306   1324     $(TOP)\src\wal.c \
  1307   1325     $(TOP)\src\walker.c \
  1308   1326     $(TOP)\src\where.c \
  1309   1327     $(TOP)\src\wherecode.c \
  1310         -  $(TOP)\src\whereexpr.c
         1328  +  $(TOP)\src\whereexpr.c \
         1329  +  $(TOP)\src\window.c
  1311   1330   
  1312   1331   # Core miscellaneous files.
  1313   1332   #
  1314   1333   SRC03 = \
  1315   1334     $(TOP)\src\parse.y
  1316   1335   
  1317   1336   # Core header files, part 1.
................................................................................
  1396   1415   SRC09 = \
  1397   1416     $(TOP)\ext\fts3\fts3.h \
  1398   1417     $(TOP)\ext\fts3\fts3Int.h \
  1399   1418     $(TOP)\ext\fts3\fts3_hash.h \
  1400   1419     $(TOP)\ext\fts3\fts3_tokenizer.h \
  1401   1420     $(TOP)\ext\icu\sqliteicu.h \
  1402   1421     $(TOP)\ext\rtree\rtree.h \
         1422  +  $(TOP)\ext\rtree\geopoly.c \
  1403   1423     $(TOP)\ext\rbu\sqlite3rbu.h \
  1404   1424     $(TOP)\ext\session\sqlite3session.h
  1405   1425   
  1406   1426   # Generated source code files
  1407   1427   #
  1408   1428   SRC10 = \
  1409   1429     opcodes.c \
................................................................................
  1474   1494     $(TOP)\src\test_superlock.c \
  1475   1495     $(TOP)\src\test_syscall.c \
  1476   1496     $(TOP)\src\test_tclsh.c \
  1477   1497     $(TOP)\src\test_tclvar.c \
  1478   1498     $(TOP)\src\test_thread.c \
  1479   1499     $(TOP)\src\test_vfs.c \
  1480   1500     $(TOP)\src\test_windirent.c \
         1501  +  $(TOP)\src\test_window.c \
  1481   1502     $(TOP)\src\test_wsd.c \
  1482   1503     $(TOP)\ext\fts3\fts3_term.c \
  1483   1504     $(TOP)\ext\fts3\fts3_test.c \
  1484   1505     $(TOP)\ext\rbu\test_rbu.c \
  1485   1506     $(TOP)\ext\session\test_session.c
  1486   1507   
  1487   1508   # Statically linked extensions.
................................................................................
  1569   1590     $(TOP)\ext\fts2\fts2_tokenizer.h
  1570   1591   EXTHDR = $(EXTHDR) \
  1571   1592     $(TOP)\ext\fts3\fts3.h \
  1572   1593     $(TOP)\ext\fts3\fts3Int.h \
  1573   1594     $(TOP)\ext\fts3\fts3_hash.h \
  1574   1595     $(TOP)\ext\fts3\fts3_tokenizer.h
  1575   1596   EXTHDR = $(EXTHDR) \
  1576         -  $(TOP)\ext\rtree\rtree.h
         1597  +  $(TOP)\ext\rtree\rtree.h \
         1598  +  $(TOP)\ext\rtree\geopoly.c
  1577   1599   EXTHDR = $(EXTHDR) \
  1578   1600     $(TOP)\ext\icu\sqliteicu.h
  1579   1601   EXTHDR = $(EXTHDR) \
  1580   1602     $(TOP)\ext\rtree\sqlite3rtree.h
  1581   1603   EXTHDR = $(EXTHDR) \
  1582   1604     $(TOP)\ext\session\sqlite3session.h
  1583   1605   
................................................................................
  1603   1625     $(TOP)\test\fuzzdata6.db
  1604   1626   # <</mark>>
  1605   1627   
  1606   1628   # Additional compiler options for the shell.  These are only effective
  1607   1629   # when the shell is not being dynamically linked.
  1608   1630   #
  1609   1631   !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0
  1610         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_STMTVTAB
  1611         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB
  1612         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
  1613         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_INTROSPECTION_PRAGMAS
  1614         -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_RTREE
         1632  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_FTS4=1
         1633  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1
         1634  +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1
  1615   1635   !ENDIF
  1616   1636   
  1617   1637   # <<mark>>
  1618   1638   # Extra compiler options for various test tools.
  1619   1639   #
  1620   1640   MPTESTER_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5
  1621   1641   FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1
................................................................................
  1692   1712   
  1693   1713   scrub.exe:	$(TOP)\ext\misc\scrub.c $(SQLITE3C) $(SQLITE3H)
  1694   1714   	$(LTLINK) $(NO_WARN) -DSCRUB_STANDALONE=1 $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1695   1715   
  1696   1716   srcck1.exe:	$(TOP)\tool\srcck1.c
  1697   1717   	$(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\srcck1.c
  1698   1718   
  1699         -sourcetest:	srcck1.exe sqlite3.c
  1700         -	srcck1.exe sqlite3.c
         1719  +sourcetest:	srcck1.exe $(SQLITE3C)
         1720  +	srcck1.exe $(SQLITE3C)
  1701   1721   
  1702   1722   fuzzershell.exe:	$(TOP)\tool\fuzzershell.c $(SQLITE3C) $(SQLITE3H)
  1703   1723   	$(LTLINK) $(NO_WARN) $(FUZZERSHELL_COMPILE_OPTS) $(TOP)\tool\fuzzershell.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1704   1724   
  1705   1725   dbfuzz.exe:	$(TOP)\test\dbfuzz.c $(SQLITE3C) $(SQLITE3H)
  1706   1726   	$(LTLINK) $(NO_WARN) $(DBFUZZ_COMPILE_OPTS) $(TOP)\test\dbfuzz.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  1707   1727   
................................................................................
  2043   2063   
  2044   2064   wherecode.lo:	$(TOP)\src\wherecode.c $(HDR)
  2045   2065   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\wherecode.c
  2046   2066   
  2047   2067   whereexpr.lo:	$(TOP)\src\whereexpr.c $(HDR)
  2048   2068   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\whereexpr.c
  2049   2069   
         2070  +window.lo:	$(TOP)\src\window.c $(HDR)
         2071  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\window.c
         2072  +
  2050   2073   tclsqlite.lo:	$(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP)
  2051   2074   	$(LTCOMPILE) $(NO_WARN) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  2052   2075   
  2053   2076   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP)
  2054   2077   	$(LTCOMPILE) $(NO_WARN) -DTCLSH -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  2055   2078   
  2056   2079   tclsqlite3.exe:	tclsqlite-shell.lo $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS)
................................................................................
  2176   2199   
  2177   2200   fts3_unicode2.lo:	$(TOP)\ext\fts3\fts3_unicode2.c $(HDR) $(EXTHDR)
  2178   2201   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_unicode2.c
  2179   2202   
  2180   2203   fts3_write.lo:	$(TOP)\ext\fts3\fts3_write.c $(HDR) $(EXTHDR)
  2181   2204   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_write.c
  2182   2205   
         2206  +json1.lo:	$(TOP)\ext\misc\json1.c $(HDR) $(EXTHDR)
         2207  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\misc\json1.c
         2208  +
         2209  +stmt.lo:	$(TOP)\ext\misc\stmt.c $(HDR) $(EXTHDR)
         2210  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\misc\stmt.c
         2211  +
  2183   2212   rtree.lo:	$(TOP)\ext\rtree\rtree.c $(HDR) $(EXTHDR)
  2184   2213   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\rtree\rtree.c
  2185   2214   
  2186   2215   sqlite3session.lo:	$(TOP)\ext\session\sqlite3session.c $(HDR) $(EXTHDR)
  2187   2216   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\session\sqlite3session.c
  2188   2217   
  2189   2218   # FTS5 things
................................................................................
  2258   2287   # hidden when the library is built via the amalgamation).
  2259   2288   #
  2260   2289   TESTFIXTURE_FLAGS = -DTCLSH_INIT_PROC=sqlite3TestInit -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
  2261   2290   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE=""
  2262   2291   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CORE $(NO_WARN)
  2263   2292   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  2264   2293   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_DEFAULT_PAGE_SIZE=1024
  2265         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB
  2266         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB
  2267         -TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_JSON1
         2294  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1
         2295  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1
         2296  +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_JSON1=1
  2268   2297   TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) $(TEST_CCONV_OPTS)
  2269   2298   
  2270   2299   TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2)
  2271   2300   TESTFIXTURE_SRC1 = $(TESTEXT) $(SQLITE3C)
  2272   2301   !IF $(USE_AMALGAMATION)==0
  2273   2302   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
  2274   2303   !ELSE
................................................................................
  2416   2445   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2417   2446   		$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2418   2447   
  2419   2448   rollback-test.exe:	$(TOP)\tool\rollback-test.c $(SQLITE3C) $(SQLITE3H)
  2420   2449   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2421   2450   		$(TOP)\tool\rollback-test.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2422   2451   
         2452  +atrc.exe:	$(TOP)\test\atrc.c $(SQLITE3C) $(SQLITE3H)
         2453  +	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
         2454  +		$(TOP)\test\atrc.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
         2455  +
  2423   2456   LogEst.exe:	$(TOP)\tool\logest.c $(SQLITE3H)
  2424   2457   	$(LTLINK) $(NO_WARN) $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS)
  2425   2458   
  2426   2459   wordcount.exe:	$(TOP)\test\wordcount.c $(SQLITE3C) $(SQLITE3H)
  2427   2460   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2428   2461   		$(TOP)\test\wordcount.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2429   2462   
................................................................................
  2447   2480   # <</mark>>
  2448   2481   
  2449   2482   clean:
  2450   2483   	del /Q *.exp *.lo *.ilk *.lib *.obj *.ncb *.pdb *.sdf *.suo 2>NUL
  2451   2484   	del /Q *.bsc *.def *.cod *.da *.bb *.bbg *.vc gmon.out 2>NUL
  2452   2485   	del /Q $(SQLITE3EXE) $(SQLITE3DLL) Replace.exe 2>NUL
  2453   2486   # <<mark>>
  2454         -	del /Q sqlite3.c sqlite3.h 2>NUL
  2455   2487   	del /Q opcodes.c opcodes.h 2>NUL
  2456   2488   	del /Q lemon.* lempar.c parse.* 2>NUL
  2457   2489   	del /Q mksourceid.* mkkeywordhash.* keywordhash.h 2>NUL
  2458   2490   	del /Q notasharedlib.* 2>NUL
  2459   2491   	-rmdir /Q/S .deps 2>NUL
  2460   2492   	-rmdir /Q/S .libs 2>NUL
  2461   2493   	-rmdir /Q/S tsrc 2>NUL
................................................................................
  2464   2496   	del /Q lsm.dll lsmtest.exe 2>NUL
  2465   2497   	del /Q testloadext.dll 2>NUL
  2466   2498   	del /Q testfixture.exe test.db 2>NUL
  2467   2499   	del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe dbdump.exe 2>NUL
  2468   2500   	del /Q changeset.exe 2>NUL
  2469   2501   	del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL
  2470   2502   	del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL
  2471         -	del /Q sqlite3.c sqlite3-*.c 2>NUL
         2503  +	del /Q sqlite3.c sqlite3-*.c sqlite3.h 2>NUL
  2472   2504   	del /Q sqlite3rc.h 2>NUL
  2473   2505   	del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL
  2474   2506   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL
  2475   2507   	del /Q sqlite-*-output.vsix 2>NUL
  2476   2508   	del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL
  2477   2509   	del /Q sqltclsh.* 2>NUL
  2478   2510   	del /Q dbfuzz.exe sessionfuzz.exe 2>NUL
  2479   2511   	del /Q kvtest.exe ossshell.exe scrub.exe 2>NUL
  2480   2512   	del /Q showshm.exe sqlite3_checker.* sqlite3_expert.exe 2>NUL
  2481   2513   	del /Q fts5.* fts5parse.* 2>NUL
  2482   2514   	del /Q lsm.h lsm1.c 2>NUL
  2483   2515   # <</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.24.0
            1  +3.25.0

Changes to autoconf/Makefile.am.

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

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.

    25     25   AC_PROG_MKDIR_P
    26     26   
    27     27   # Check for library functions that SQLite can optionally use.
    28     28   AC_CHECK_FUNCS([fdatasync usleep fullfsync localtime_r gmtime_r])
    29     29   AC_FUNC_STRERROR_R
    30     30   
    31     31   AC_CONFIG_FILES([Makefile sqlite3.pc])
           32  +BUILD_CFLAGS=
    32     33   AC_SUBST(BUILD_CFLAGS)
    33     34   
    34     35   #-------------------------------------------------------------------------
    35     36   # Two options to enable readline compatible libraries: 
    36     37   #
    37     38   #   --enable-editline
    38     39   #   --enable-readline
................................................................................
    82     83   
    83     84   #-----------------------------------------------------------------------
    84     85   #   --enable-threadsafe
    85     86   #
    86     87   AC_ARG_ENABLE(threadsafe, [AS_HELP_STRING(
    87     88     [--enable-threadsafe], [build a thread-safe library [default=yes]])], 
    88     89     [], [enable_threadsafe=yes])
    89         -THREADSAFE_FLAGS=-DSQLITE_THREADSAFE=0
    90     90   if test x"$enable_threadsafe" != "xno"; then
    91         -  THREADSAFE_FLAGS="-D_REENTRANT=1 -DSQLITE_THREADSAFE=1"
           91  +  BUILD_CFLAGS="$BUILD_CFLAGS -D_REENTRANT=1 -DSQLITE_THREADSAFE=1"
    92     92     AC_SEARCH_LIBS(pthread_create, pthread)
    93     93     AC_SEARCH_LIBS(pthread_mutexattr_init, pthread)
    94     94   fi
    95         -AC_SUBST(THREADSAFE_FLAGS)
    96     95   #-----------------------------------------------------------------------
    97     96   
    98     97   #-----------------------------------------------------------------------
    99     98   #   --enable-dynamic-extensions
   100     99   #
   101    100   AC_ARG_ENABLE(dynamic-extensions, [AS_HELP_STRING(
   102    101     [--enable-dynamic-extensions], [support loadable extensions [default=yes]])], 
   103    102     [], [enable_dynamic_extensions=yes])
   104    103   if test x"$enable_dynamic_extensions" != "xno"; then
   105    104     AC_SEARCH_LIBS(dlopen, dl)
   106    105   else
   107         -  DYNAMIC_EXTENSION_FLAGS=-DSQLITE_OMIT_LOAD_EXTENSION=1
          106  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_OMIT_LOAD_EXTENSION=1"
   108    107   fi
   109    108   AC_MSG_CHECKING([for whether to support dynamic extensions])
   110    109   AC_MSG_RESULT($enable_dynamic_extensions)
   111         -AC_SUBST(DYNAMIC_EXTENSION_FLAGS)
          110  +#-----------------------------------------------------------------------
          111  +
          112  +#-----------------------------------------------------------------------
          113  +#   --enable-fts4
          114  +#
          115  +AC_ARG_ENABLE(fts4, [AS_HELP_STRING(
          116  +  [--enable-fts4], [include fts4 support [default=yes]])], 
          117  +  [], [enable_fts4=yes])
          118  +if test x"$enable_fts4" = "xyes"; then
          119  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_FTS4"
          120  +fi
          121  +#-----------------------------------------------------------------------
          122  +
          123  +#-----------------------------------------------------------------------
          124  +#   --enable-fts3
          125  +#
          126  +AC_ARG_ENABLE(fts3, [AS_HELP_STRING(
          127  +  [--enable-fts3], [include fts3 support [default=no]])], 
          128  +  [], [])
          129  +if test x"$enable_fts3" = "xyes" -a x"$enable_fts4" = "xno"; then
          130  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_FTS3"
          131  +fi
   112    132   #-----------------------------------------------------------------------
   113    133   
   114    134   #-----------------------------------------------------------------------
   115    135   #   --enable-fts5
   116    136   #
   117    137   AC_ARG_ENABLE(fts5, [AS_HELP_STRING(
   118    138     [--enable-fts5], [include fts5 support [default=yes]])], 
   119    139     [], [enable_fts5=yes])
   120    140   if test x"$enable_fts5" = "xyes"; then
   121    141     AC_SEARCH_LIBS(log, m)
   122         -  FTS5_FLAGS=-DSQLITE_ENABLE_FTS5
          142  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_FTS5"
   123    143   fi
   124         -AC_SUBST(FTS5_FLAGS)
   125    144   #-----------------------------------------------------------------------
   126    145   
   127    146   #-----------------------------------------------------------------------
   128    147   #   --enable-json1
   129    148   #
   130    149   AC_ARG_ENABLE(json1, [AS_HELP_STRING(
   131    150     [--enable-json1], [include json1 support [default=yes]])], 
   132         -  [], [enable_json1=yes])
          151  +  [],[enable_json1=yes])
   133    152   if test x"$enable_json1" = "xyes"; then
   134         -  JSON1_FLAGS=-DSQLITE_ENABLE_JSON1
          153  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_JSON1"
   135    154   fi
   136         -AC_SUBST(JSON1_FLAGS)
          155  +#-----------------------------------------------------------------------
          156  +
          157  +#-----------------------------------------------------------------------
          158  +#   --enable-rtree
          159  +#
          160  +AC_ARG_ENABLE(rtree, [AS_HELP_STRING(
          161  +  [--enable-rtree], [include rtree support [default=yes]])], 
          162  +  [], [enable_rtree=yes])
          163  +if test x"$enable_rtree" = "xyes"; then
          164  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_ENABLE_RTREE"
          165  +fi
   137    166   #-----------------------------------------------------------------------
   138    167   
   139    168   #-----------------------------------------------------------------------
   140    169   #   --enable-session
   141    170   #
   142    171   AC_ARG_ENABLE(session, [AS_HELP_STRING(
   143    172     [--enable-session], [enable the session extension [default=no]])], 
   144         -  [], [enable_session=no])
          173  +  [], [])
   145    174   if test x"$enable_session" = "xyes"; then
   146         -  SESSION_FLAGS="-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK"
          175  +  BUILD_CFLAGS="$BUILD_CFLAGS-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK"
   147    176   fi
   148         -AC_SUBST(SESSION_FLAGS)
   149    177   #-----------------------------------------------------------------------
   150    178   
   151    179   #-----------------------------------------------------------------------
   152    180   #   --enable-debug
   153    181   #
   154    182   AC_ARG_ENABLE(debug, [AS_HELP_STRING(
   155    183     [--enable-debug], [build with debugging features enabled [default=no]])], 
   156         -  [], [enable_session=no])
          184  +  [], [])
   157    185   if test x"$enable_debug" = "xyes"; then
   158         -  DEBUG_FLAGS="-DSQLITE_DEBUG -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE"
          186  +  BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_DEBUG -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE"
          187  +  CFLAGS="-g -O0"
   159    188   fi
   160         -AC_SUBST(DEBUG_FLAGS)
   161    189   #-----------------------------------------------------------------------
   162    190   
   163    191   #-----------------------------------------------------------------------
   164    192   #   --enable-static-shell
   165    193   #
   166    194   AC_ARG_ENABLE(static-shell, [AS_HELP_STRING(
   167    195     [--enable-static-shell], 
................................................................................
   173    201     EXTRA_SHELL_OBJ=libsqlite3.la
   174    202   fi
   175    203   AC_SUBST(EXTRA_SHELL_OBJ)
   176    204   #-----------------------------------------------------------------------
   177    205   
   178    206   AC_CHECK_FUNCS(posix_fallocate)
   179    207   AC_CHECK_HEADERS(zlib.h,[
   180         -  AC_SEARCH_LIBS(deflate,z,[ZLIB_FLAGS="-DSQLITE_HAVE_ZLIB"])
          208  +  AC_SEARCH_LIBS(deflate,z,[BUILD_CFLAGS="$BUILD_CFLAGS -DSQLITE_HAVE_ZLIB"])
   181    209   ])
   182         -AC_SUBST(ZLIB_FLAGS)
   183    210   
   184    211   AC_SEARCH_LIBS(system,,,[SHELL_CFLAGS="-DSQLITE_NOHAVE_SYSTEM"])
   185    212   AC_SUBST(SHELL_CFLAGS)
   186    213   
   187    214   #-----------------------------------------------------------------------
   188    215   # UPDATE: Maybe it's better if users just set CFLAGS before invoking
   189    216   # configure. This option doesn't really add much...

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.24.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.25.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.24.0'
   730         -PACKAGE_STRING='sqlite 3.24.0'
          729  +PACKAGE_VERSION='3.25.0'
          730  +PACKAGE_STRING='sqlite 3.25.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.24.0 to adapt to many kinds of systems.
         1469  +\`configure' configures sqlite 3.25.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.24.0:";;
         1534  +     short | recursive ) echo "Configuration of sqlite 3.25.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.24.0
         1660  +sqlite configure 3.25.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.24.0, which was
         2079  +It was created by sqlite $as_me 3.25.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>
................................................................................
 10451  10453   
 10452  10454   ##########
 10453  10455   # Do we want to support multithreaded use of sqlite
 10454  10456   #
 10455  10457   # Check whether --enable-threadsafe was given.
 10456  10458   if test "${enable_threadsafe+set}" = set; then :
 10457  10459     enableval=$enable_threadsafe;
 10458         -else
 10459         -  enable_threadsafe=yes
 10460  10460   fi
 10461  10461   
 10462  10462   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support threadsafe operation" >&5
 10463  10463   $as_echo_n "checking whether to support threadsafe operation... " >&6; }
 10464  10464   if test "$enable_threadsafe" = "no"; then
 10465  10465     SQLITE_THREADSAFE=0
 10466  10466     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
................................................................................
 11245  11245   fi
 11246  11246   
 11247  11247   
 11248  11248   #########
 11249  11249   # check for debug enabled
 11250  11250   # Check whether --enable-debug was given.
 11251  11251   if test "${enable_debug+set}" = set; then :
 11252         -  enableval=$enable_debug; use_debug=$enableval
 11253         -else
 11254         -  use_debug=no
        11252  +  enableval=$enable_debug;
 11255  11253   fi
 11256  11254   
 11257         -if test "${use_debug}" = "yes" ; then
        11255  +if test "${enable_debug}" = "yes" ; then
 11258  11256     TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0"
 11259  11257   else
 11260  11258     TARGET_DEBUG="-DNDEBUG"
 11261  11259   fi
 11262  11260   
 11263  11261   
 11264  11262   #########
 11265  11263   # See whether we should use the amalgamation to build
 11266  11264   # Check whether --enable-amalgamation was given.
 11267  11265   if test "${enable_amalgamation+set}" = set; then :
 11268         -  enableval=$enable_amalgamation; use_amalgamation=$enableval
 11269         -else
 11270         -  use_amalgamation=yes
        11266  +  enableval=$enable_amalgamation;
 11271  11267   fi
 11272  11268   
 11273         -if test "${use_amalgamation}" != "yes" ; then
        11269  +if test "${enable_amalgamation}" == "no" ; then
 11274  11270     USE_AMALGAMATION=0
 11275  11271   fi
 11276  11272   
 11277  11273   
 11278  11274   #########
 11279  11275   # Look for zlib.  Only needed by extensions and by the sqlite3.exe shell
 11280  11276   for ac_header in zlib.h
................................................................................
 11349  11345   
 11350  11346   
 11351  11347   
 11352  11348   #########
 11353  11349   # See whether we should allow loadable extensions
 11354  11350   # Check whether --enable-load-extension was given.
 11355  11351   if test "${enable_load_extension+set}" = set; then :
 11356         -  enableval=$enable_load_extension; use_loadextension=$enableval
        11352  +  enableval=$enable_load_extension;
 11357  11353   else
 11358         -  use_loadextension=yes
        11354  +  enable_load_extension=yes
 11359  11355   fi
 11360  11356   
 11361         -if test "${use_loadextension}" = "yes" ; then
        11357  +if test "${enable_load_extension}" = "yes" ; then
 11362  11358     OPT_FEATURE_FLAGS=""
 11363  11359     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing dlopen" >&5
 11364  11360   $as_echo_n "checking for library containing dlopen... " >&6; }
 11365  11361   if ${ac_cv_search_dlopen+:} false; then :
 11366  11362     $as_echo_n "(cached) " >&6
 11367  11363   else
 11368  11364     ac_func_search_save_LIBS=$LIBS
................................................................................
 11421  11417   fi
 11422  11418   
 11423  11419   ##########
 11424  11420   # Do we want to support memsys3 and/or memsys5
 11425  11421   #
 11426  11422   # Check whether --enable-memsys5 was given.
 11427  11423   if test "${enable_memsys5+set}" = set; then :
 11428         -  enableval=$enable_memsys5; enable_memsys5=yes
 11429         -else
 11430         -  enable_memsys5=no
        11424  +  enableval=$enable_memsys5;
 11431  11425   fi
 11432  11426   
 11433  11427   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS5" >&5
 11434  11428   $as_echo_n "checking whether to support MEMSYS5... " >&6; }
 11435  11429   if test "${enable_memsys5}" = "yes"; then
 11436  11430     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_MEMSYS5"
 11437  11431     { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
................................................................................
 11438  11432   $as_echo "yes" >&6; }
 11439  11433   else
 11440  11434     { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 11441  11435   $as_echo "no" >&6; }
 11442  11436   fi
 11443  11437   # Check whether --enable-memsys3 was given.
 11444  11438   if test "${enable_memsys3+set}" = set; then :
 11445         -  enableval=$enable_memsys3; enable_memsys3=yes
 11446         -else
 11447         -  enable_memsys3=no
        11439  +  enableval=$enable_memsys3;
 11448  11440   fi
 11449  11441   
 11450  11442   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support MEMSYS3" >&5
 11451  11443   $as_echo_n "checking whether to support MEMSYS3... " >&6; }
 11452  11444   if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
 11453  11445     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_MEMSYS3"
 11454  11446     { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
................................................................................
 11458  11450   $as_echo "no" >&6; }
 11459  11451   fi
 11460  11452   
 11461  11453   #########
 11462  11454   # See whether we should enable Full Text Search extensions
 11463  11455   # Check whether --enable-fts3 was given.
 11464  11456   if test "${enable_fts3+set}" = set; then :
 11465         -  enableval=$enable_fts3; enable_fts3=yes
 11466         -else
 11467         -  enable_fts3=no
        11457  +  enableval=$enable_fts3;
 11468  11458   fi
 11469  11459   
 11470  11460   if test "${enable_fts3}" = "yes" ; then
 11471  11461     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS3"
 11472  11462   fi
 11473  11463   # Check whether --enable-fts4 was given.
 11474  11464   if test "${enable_fts4+set}" = set; then :
 11475         -  enableval=$enable_fts4; enable_fts4=yes
 11476         -else
 11477         -  enable_fts4=no
        11465  +  enableval=$enable_fts4;
 11478  11466   fi
 11479  11467   
 11480  11468   if test "${enable_fts4}" = "yes" ; then
 11481  11469     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS4"
 11482  11470     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5
 11483  11471   $as_echo_n "checking for library containing log... " >&6; }
 11484  11472   if ${ac_cv_search_log+:} false; then :
................................................................................
 11534  11522     test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
 11535  11523   
 11536  11524   fi
 11537  11525   
 11538  11526   fi
 11539  11527   # Check whether --enable-fts5 was given.
 11540  11528   if test "${enable_fts5+set}" = set; then :
 11541         -  enableval=$enable_fts5; enable_fts5=yes
 11542         -else
 11543         -  enable_fts5=no
        11529  +  enableval=$enable_fts5;
 11544  11530   fi
 11545  11531   
 11546  11532   if test "${enable_fts5}" = "yes" ; then
 11547  11533     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS5"
 11548  11534     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5
 11549  11535   $as_echo_n "checking for library containing log... " >&6; }
 11550  11536   if ${ac_cv_search_log+:} false; then :
................................................................................
 11603  11589   
 11604  11590   fi
 11605  11591   
 11606  11592   #########
 11607  11593   # See whether we should enable JSON1
 11608  11594   # Check whether --enable-json1 was given.
 11609  11595   if test "${enable_json1+set}" = set; then :
 11610         -  enableval=$enable_json1; enable_json1=yes
 11611         -else
 11612         -  enable_json1=no
        11596  +  enableval=$enable_json1;
 11613  11597   fi
 11614  11598   
 11615  11599   if test "${enable_json1}" = "yes" ; then
 11616  11600     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_JSON1"
 11617  11601   fi
 11618  11602   
 11619  11603   #########
 11620  11604   # See whether we should enable the LIMIT clause on UPDATE and DELETE
 11621  11605   # statements.
 11622  11606   # Check whether --enable-update-limit was given.
 11623  11607   if test "${enable_update_limit+set}" = set; then :
 11624         -  enableval=$enable_update_limit; enable_udlimit=yes
 11625         -else
 11626         -  enable_udlimit=no
        11608  +  enableval=$enable_update_limit;
 11627  11609   fi
 11628  11610   
 11629  11611   if test "${enable_udlimit}" = "yes" ; then
 11630  11612     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT"
 11631  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
 11632  11628   
 11633  11629   #########
 11634  11630   # See whether we should enable RTREE
 11635  11631   # Check whether --enable-rtree was given.
 11636  11632   if test "${enable_rtree+set}" = set; then :
 11637         -  enableval=$enable_rtree; enable_rtree=yes
 11638         -else
 11639         -  enable_rtree=no
        11633  +  enableval=$enable_rtree;
 11640  11634   fi
 11641  11635   
 11642  11636   if test "${enable_rtree}" = "yes" ; then
 11643  11637     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_RTREE"
 11644  11638   fi
 11645  11639   
 11646  11640   #########
 11647  11641   # See whether we should enable the SESSION extension
 11648  11642   # Check whether --enable-session was given.
 11649  11643   if test "${enable_session+set}" = set; then :
 11650         -  enableval=$enable_session; enable_session=yes
 11651         -else
 11652         -  enable_session=no
        11644  +  enableval=$enable_session;
 11653  11645   fi
 11654  11646   
 11655  11647   if test "${enable_session}" = "yes" ; then
 11656  11648     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_SESSION"
 11657  11649     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_PREUPDATE_HOOK"
 11658  11650   fi
 11659  11651   
................................................................................
 11708  11700   BUILD_CFLAGS=$ac_temp_BUILD_CFLAGS
 11709  11701   
 11710  11702   
 11711  11703   #########
 11712  11704   # See whether we should use GCOV
 11713  11705   # Check whether --enable-gcov was given.
 11714  11706   if test "${enable_gcov+set}" = set; then :
 11715         -  enableval=$enable_gcov; use_gcov=$enableval
 11716         -else
 11717         -  use_gcov=no
        11707  +  enableval=$enable_gcov;
 11718  11708   fi
 11719  11709   
 11720  11710   if test "${use_gcov}" = "yes" ; then
 11721  11711     USE_GCOV=1
 11722  11712   else
 11723  11713     USE_GCOV=0
 11724  11714   fi
................................................................................
 12238  12228   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12239  12229   
 12240  12230   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12241  12231   # Save the log message, to keep $0 and so on meaningful, and to
 12242  12232   # report actual input values of CONFIG_FILES etc. instead of their
 12243  12233   # values after options handling.
 12244  12234   ac_log="
 12245         -This file was extended by sqlite $as_me 3.24.0, which was
        12235  +This file was extended by sqlite $as_me 3.25.0, which was
 12246  12236   generated by GNU Autoconf 2.69.  Invocation command line was
 12247  12237   
 12248  12238     CONFIG_FILES    = $CONFIG_FILES
 12249  12239     CONFIG_HEADERS  = $CONFIG_HEADERS
 12250  12240     CONFIG_LINKS    = $CONFIG_LINKS
 12251  12241     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12252  12242     $ $0 $@
................................................................................
 12304  12294   
 12305  12295   Report bugs to the package provider."
 12306  12296   
 12307  12297   _ACEOF
 12308  12298   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12309  12299   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12310  12300   ac_cs_version="\\
 12311         -sqlite config.status 3.24.0
        12301  +sqlite config.status 3.25.0
 12312  12302   configured by $0, generated by GNU Autoconf 2.69,
 12313  12303     with options \\"\$ac_cs_config\\"
 12314  12304   
 12315  12305   Copyright (C) 2012 Free Software Foundation, Inc.
 12316  12306   This config.status script is free software; the Free Software Foundation
 12317  12307   gives unlimited permission to copy, distribute and modify it."
 12318  12308   

Changes to configure.ac.

   178    178   fi
   179    179   AC_SUBST(BUILD_CC)
   180    180   
   181    181   ##########
   182    182   # Do we want to support multithreaded use of sqlite
   183    183   #
   184    184   AC_ARG_ENABLE(threadsafe, 
   185         -AC_HELP_STRING([--disable-threadsafe],[Disable mutexing]),,enable_threadsafe=yes)
          185  +AC_HELP_STRING([--disable-threadsafe],[Disable mutexing]))
   186    186   AC_MSG_CHECKING([whether to support threadsafe operation])
   187    187   if test "$enable_threadsafe" = "no"; then
   188    188     SQLITE_THREADSAFE=0
   189    189     AC_MSG_RESULT([no])
   190    190   else
   191    191     SQLITE_THREADSAFE=1
   192    192     AC_MSG_RESULT([yes])
................................................................................
   553    553   # Figure out what C libraries are required to compile programs
   554    554   # that use "fdatasync()" function.
   555    555   #
   556    556   AC_SEARCH_LIBS(fdatasync, [rt])
   557    557   
   558    558   #########
   559    559   # check for debug enabled
   560         -AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],[enable debugging & verbose explain]),
   561         -      [use_debug=$enableval],[use_debug=no])
   562         -if test "${use_debug}" = "yes" ; then
          560  +AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],[enable debugging & verbose explain]))
          561  +if test "${enable_debug}" = "yes" ; then
   563    562     TARGET_DEBUG="-DSQLITE_DEBUG=1 -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE -O0"
   564    563   else
   565    564     TARGET_DEBUG="-DNDEBUG"
   566    565   fi
   567    566   AC_SUBST(TARGET_DEBUG)
   568    567   
   569    568   #########
   570    569   # See whether we should use the amalgamation to build
   571    570   AC_ARG_ENABLE(amalgamation, AC_HELP_STRING([--disable-amalgamation],
   572         -      [Disable the amalgamation and instead build all files separately]),
   573         -      [use_amalgamation=$enableval],[use_amalgamation=yes])
   574         -if test "${use_amalgamation}" != "yes" ; then
          571  +      [Disable the amalgamation and instead build all files separately]))
          572  +if test "${enable_amalgamation}" == "no" ; then
   575    573     USE_AMALGAMATION=0
   576    574   fi
   577    575   AC_SUBST(USE_AMALGAMATION)
   578    576   
   579    577   #########
   580    578   # Look for zlib.  Only needed by extensions and by the sqlite3.exe shell
   581    579   AC_CHECK_HEADERS(zlib.h)
   582    580   AC_SEARCH_LIBS(deflate, z, [HAVE_ZLIB="-DSQLITE_HAVE_ZLIB=1"], [HAVE_ZLIB=""])
   583    581   AC_SUBST(HAVE_ZLIB)
   584    582   
   585    583   #########
   586    584   # See whether we should allow loadable extensions
   587    585   AC_ARG_ENABLE(load-extension, AC_HELP_STRING([--disable-load-extension],
   588         -      [Disable loading of external extensions]),
   589         -      [use_loadextension=$enableval],[use_loadextension=yes])
   590         -if test "${use_loadextension}" = "yes" ; then
          586  +      [Disable loading of external extensions]),,[enable_load_extension=yes])
          587  +if test "${enable_load_extension}" = "yes" ; then
   591    588     OPT_FEATURE_FLAGS=""
   592    589     AC_SEARCH_LIBS(dlopen, dl)
   593    590   else
   594    591     OPT_FEATURE_FLAGS="-DSQLITE_OMIT_LOAD_EXTENSION=1"
   595    592   fi
   596    593   
   597    594   ##########
   598    595   # Do we want to support memsys3 and/or memsys5
   599    596   #
   600    597   AC_ARG_ENABLE(memsys5, 
   601         -  AC_HELP_STRING([--enable-memsys5],[Enable MEMSYS5]),
   602         -  [enable_memsys5=yes],[enable_memsys5=no])
          598  +  AC_HELP_STRING([--enable-memsys5],[Enable MEMSYS5]))
   603    599   AC_MSG_CHECKING([whether to support MEMSYS5])
   604    600   if test "${enable_memsys5}" = "yes"; then
   605    601     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_MEMSYS5"
   606    602     AC_MSG_RESULT([yes])
   607    603   else
   608    604     AC_MSG_RESULT([no])
   609    605   fi
   610    606   AC_ARG_ENABLE(memsys3, 
   611         -  AC_HELP_STRING([--enable-memsys3],[Enable MEMSYS3]),
   612         -  [enable_memsys3=yes],[enable_memsys3=no])
          607  +  AC_HELP_STRING([--enable-memsys3],[Enable MEMSYS3]))
   613    608   AC_MSG_CHECKING([whether to support MEMSYS3])
   614    609   if test "${enable_memsys3}" = "yes" -a "${enable_memsys5}" = "no"; then
   615    610     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_MEMSYS3"
   616    611     AC_MSG_RESULT([yes])
   617    612   else
   618    613     AC_MSG_RESULT([no])
   619    614   fi
   620    615   
   621    616   #########
   622    617   # See whether we should enable Full Text Search extensions
   623    618   AC_ARG_ENABLE(fts3, AC_HELP_STRING([--enable-fts3],
   624         -      [Enable the FTS3 extension]),
   625         -      [enable_fts3=yes],[enable_fts3=no])
          619  +      [Enable the FTS3 extension]))
   626    620   if test "${enable_fts3}" = "yes" ; then
   627    621     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS3"
   628    622   fi
   629    623   AC_ARG_ENABLE(fts4, AC_HELP_STRING([--enable-fts4],
   630         -      [Enable the FTS4 extension]),
   631         -      [enable_fts4=yes],[enable_fts4=no])
          624  +      [Enable the FTS4 extension]))
   632    625   if test "${enable_fts4}" = "yes" ; then
   633    626     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS4"
   634    627     AC_SEARCH_LIBS([log],[m])
   635    628   fi
   636    629   AC_ARG_ENABLE(fts5, AC_HELP_STRING([--enable-fts5],
   637         -      [Enable the FTS5 extension]),
   638         -      [enable_fts5=yes],[enable_fts5=no])
          630  +      [Enable the FTS5 extension]))
   639    631   if test "${enable_fts5}" = "yes" ; then
   640    632     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_FTS5"
   641    633     AC_SEARCH_LIBS([log],[m])
   642    634   fi
   643    635   
   644    636   #########
   645    637   # See whether we should enable JSON1
   646         -AC_ARG_ENABLE(json1, AC_HELP_STRING([--enable-json1],
   647         -      [Enable the JSON1 extension]),
   648         -      [enable_json1=yes],[enable_json1=no])
          638  +AC_ARG_ENABLE(json1, AC_HELP_STRING([--enable-json1],[Enable the JSON1 extension]))
   649    639   if test "${enable_json1}" = "yes" ; then
   650    640     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_JSON1"
   651    641   fi
   652    642   
   653    643   #########
   654    644   # See whether we should enable the LIMIT clause on UPDATE and DELETE
   655    645   # statements.
   656    646   AC_ARG_ENABLE(update-limit, AC_HELP_STRING([--enable-update-limit],
   657         -      [Enable the UPDATE/DELETE LIMIT clause]),
   658         -      [enable_udlimit=yes],[enable_udlimit=no])
          647  +      [Enable the UPDATE/DELETE LIMIT clause]))
   659    648   if test "${enable_udlimit}" = "yes" ; then
   660    649     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT"
   661    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
   662    661   
   663    662   #########
   664    663   # See whether we should enable RTREE
   665    664   AC_ARG_ENABLE(rtree, AC_HELP_STRING([--enable-rtree],
   666         -      [Enable the RTREE extension]),
   667         -      [enable_rtree=yes],[enable_rtree=no])
          665  +      [Enable the RTREE extension]))
   668    666   if test "${enable_rtree}" = "yes" ; then
   669    667     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_RTREE"
   670    668   fi
   671    669   
   672    670   #########
   673    671   # See whether we should enable the SESSION extension
   674    672   AC_ARG_ENABLE(session, AC_HELP_STRING([--enable-session],
   675         -      [Enable the SESSION extension]),
   676         -      [enable_session=yes],[enable_session=no])
          673  +      [Enable the SESSION extension]))
   677    674   if test "${enable_session}" = "yes" ; then
   678    675     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_SESSION"
   679    676     OPT_FEATURE_FLAGS="${OPT_FEATURE_FLAGS} -DSQLITE_ENABLE_PREUPDATE_HOOK"
   680    677   fi
   681    678   
   682    679   #########
   683    680   # attempt to duplicate any OMITS and ENABLES into the ${OPT_FEATURE_FLAGS} parameter
................................................................................
   729    726   done
   730    727   BUILD_CFLAGS=$ac_temp_BUILD_CFLAGS
   731    728   
   732    729   
   733    730   #########
   734    731   # See whether we should use GCOV
   735    732   AC_ARG_ENABLE(gcov, AC_HELP_STRING([--enable-gcov],
   736         -      [Enable coverage testing using gcov]),
   737         -      [use_gcov=$enableval],[use_gcov=no])
          733  +      [Enable coverage testing using gcov]))
   738    734   if test "${use_gcov}" = "yes" ; then
   739    735     USE_GCOV=1
   740    736   else
   741    737     USE_GCOV=0
   742    738   fi
   743    739   AC_SUBST(USE_GCOV)
   744    740   

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/expert/expert1.test.

   328    328     $expert destroy
   329    329   
   330    330     uplevel [list do_test $tn [list set {} $candidates] $res]
   331    331   }
   332    332   
   333    333   
   334    334   reset_db
   335         -do_execsql_test 3.0 {
          335  +do_execsql_test 4.0 {
   336    336     CREATE TABLE t1(a, b);
   337    337     CREATE TABLE t2(c, d);
   338    338   
   339    339     WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100)
   340    340     INSERT INTO t1 SELECT (i-1)/50, (i-1)/20 FROM s;
   341    341   
   342    342     WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100)
   343    343     INSERT INTO t2 SELECT (i-1)/20, (i-1)/5 FROM s;
   344    344   }
   345         -do_candidates_test 3.1 {
          345  +do_candidates_test 4.1 {
   346    346     SELECT * FROM t1,t2 WHERE (b=? OR a=?) AND (c=? OR d=?)
   347    347   } {
   348    348     CREATE INDEX t1_idx_00000062 ON t1(b); -- stat1: 100 20 
   349    349     CREATE INDEX t1_idx_00000061 ON t1(a); -- stat1: 100 50 
   350    350     CREATE INDEX t2_idx_00000063 ON t2(c); -- stat1: 100 20 
   351    351     CREATE INDEX t2_idx_00000064 ON t2(d); -- stat1: 100 5
   352    352   }
   353    353   
   354         -do_candidates_test 3.2 {
          354  +do_candidates_test 4.2 {
   355    355     SELECT * FROM t1,t2 WHERE a=? AND b=? AND c=? AND d=?
   356    356   } {
   357    357     CREATE INDEX t1_idx_000123a7 ON t1(a, b); -- stat1: 100 50 17
   358    358     CREATE INDEX t2_idx_0001295b ON t2(c, d); -- stat1: 100 20 5
   359    359   }
   360    360   
   361         -do_execsql_test 3.2 {
          361  +do_execsql_test 4.3 {
   362    362     CREATE INDEX t1_idx_00000061 ON t1(a); -- stat1: 100 50 
   363    363     CREATE INDEX t1_idx_00000062 ON t1(b); -- stat1: 100 20 
   364    364     CREATE INDEX t1_idx_000123a7 ON t1(a, b); -- stat1: 100 50 16
   365    365   
   366    366     CREATE INDEX t2_idx_00000063 ON t2(c); -- stat1: 100 20 
   367    367     CREATE INDEX t2_idx_00000064 ON t2(d); -- stat1: 100 5
   368    368     CREATE INDEX t2_idx_0001295b ON t2(c, d); -- stat1: 100 20 5

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.

    32     32   void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
    33     33   void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
    34     34   void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
    35     35   #ifndef NDEBUG
    36     36   #include <stdio.h>
    37     37   void sqlite3Fts5ParserTrace(FILE*, char*);
    38     38   #endif
           39  +int sqlite3Fts5ParserFallback(int);
    39     40   
    40     41   
    41     42   struct Fts5Expr {
    42     43     Fts5Index *pIndex;
    43     44     Fts5Config *pConfig;
    44     45     Fts5ExprNode *pRoot;
    45     46     int bDesc;                      /* Iterate in descending rowid order */
................................................................................
  2536   2537   */
  2537   2538   static void fts5ExprIsAlnum(
  2538   2539     sqlite3_context *pCtx,          /* Function call context */
  2539   2540     int nArg,                       /* Number of args */
  2540   2541     sqlite3_value **apVal           /* Function arguments */
  2541   2542   ){
  2542   2543     int iCode;
         2544  +  u8 aArr[32];
  2543   2545     if( nArg!=1 ){
  2544   2546       sqlite3_result_error(pCtx, 
  2545   2547           "wrong number of arguments to function fts5_isalnum", -1
  2546   2548       );
  2547   2549       return;
  2548   2550     }
         2551  +  memset(aArr, 0, sizeof(aArr));
         2552  +  sqlite3Fts5UnicodeCatParse("L*", aArr);
         2553  +  sqlite3Fts5UnicodeCatParse("N*", aArr);
         2554  +  sqlite3Fts5UnicodeCatParse("Co", aArr);
  2549   2555     iCode = sqlite3_value_int(apVal[0]);
  2550         -  sqlite3_result_int(pCtx, sqlite3Fts5UnicodeIsalnum(iCode));
         2556  +  sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory(iCode)]);
  2551   2557   }
  2552   2558   
  2553   2559   static void fts5ExprFold(
  2554   2560     sqlite3_context *pCtx,          /* Function call context */
  2555   2561     int nArg,                       /* Number of args */
  2556   2562     sqlite3_value **apVal           /* Function arguments */
  2557   2563   ){
................................................................................
  2587   2593     void *pCtx = (void*)pGlobal;
  2588   2594   
  2589   2595     for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
  2590   2596       struct Fts5ExprFunc *p = &aFunc[i];
  2591   2597       rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
  2592   2598     }
  2593   2599   
  2594         -  /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */
         2600  +  /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
         2601  +  ** sqlite3Fts5ParserFallback() are unused */
  2595   2602   #ifndef NDEBUG
  2596   2603     (void)sqlite3Fts5ParserTrace;
  2597   2604   #endif
         2605  +  (void)sqlite3Fts5ParserFallback;
  2598   2606   
  2599   2607     return rc;
  2600   2608   }
  2601   2609   
  2602   2610   /*
  2603   2611   ** Return the number of phrases in expression pExpr.
  2604   2612   */

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/lsm1/lsm-test/lsmtest1.c.

   270    270     int *pRc                        /* OUT: Error code */
   271    271   ){
   272    272     int i;
   273    273     int iDot;
   274    274     int rc = LSM_OK;
   275    275     Datasource *pData;
   276    276     TestDb *pDb;
          277  +  int iToggle = 0;
   277    278   
   278    279     /* Start the test case, open a database and allocate the datasource. */
   279    280     pDb = testOpen(zSystem, 1, &rc);
   280    281     pData = testDatasourceNew(&p->defn);
   281    282   
   282    283     i = 0;
   283    284     iDot = 0;
   284    285     while( rc==LSM_OK && i<p->nRow ){
   285    286   
   286    287       /* Insert some data */
   287    288       testWriteDatasourceRange(pDb, pData, i, p->nVerify, &rc);
   288    289       i += p->nVerify;
   289    290   
          291  +    if( iToggle ) testBegin(pDb, 1, &rc);
   290    292       /* Check that the db content is correct. */
   291    293       testDbContents(pDb, pData, p->nRow, 0, i-1, p->nTest, p->bTestScan, &rc);
          294  +    if( iToggle ) testCommit(pDb, 0, &rc);
          295  +    iToggle = (iToggle+1)%2;
   292    296   
   293    297       if( bRecover ){
   294    298         testReopenRecover(&pDb, &rc);
   295    299       }else{
   296    300         testReopen(&pDb, &rc);
   297    301       }
   298    302   

Changes to ext/lsm1/lsm-test/lsmtest_tdb3.c.

   613    613   ){
   614    614     int rc;
   615    615     LsmDb *pDb = (LsmDb *)pTestDb;
   616    616     lsm_cursor *csr;
   617    617   
   618    618     if( pKey==0 ) return LSM_OK;
   619    619   
   620         -  rc = lsm_csr_open(pDb->db, &csr);
   621         -  if( rc!=LSM_OK ) return rc;
          620  +  if( pDb->pCsr==0 ){
          621  +    rc = lsm_csr_open(pDb->db, &csr);
          622  +    if( rc!=LSM_OK ) return rc;
          623  +  }else{
          624  +    csr = pDb->pCsr;
          625  +  }
   622    626   
   623    627     rc = lsm_csr_seek(csr, pKey, nKey, LSM_SEEK_EQ);
   624    628     if( rc==LSM_OK ){
   625    629       if( lsm_csr_valid(csr) ){
   626    630         const void *pVal; int nVal;
   627    631         rc = lsm_csr_value(csr, &pVal, &nVal);
   628    632         if( nVal>pDb->nBuf ){
................................................................................
   634    638         *ppVal = pDb->pBuf;
   635    639         *pnVal = nVal;
   636    640       }else{
   637    641         *ppVal = 0;
   638    642         *pnVal = -1;
   639    643       }
   640    644     }
   641         -  lsm_csr_close(csr);
          645  +  if( pDb->pCsr==0 ){
          646  +    lsm_csr_close(csr);
          647  +  }
   642    648     return rc;
   643    649   }
   644    650   
   645    651   static int test_lsm_scan(
   646    652     TestDb *pTestDb,
   647    653     void *pCtx,
   648    654     int bReverse,
   649    655     void *pFirst, int nFirst,
   650    656     void *pLast, int nLast,
   651    657     void (*xCallback)(void *, void *, int , void *, int)
   652    658   ){
   653    659     LsmDb *pDb = (LsmDb *)pTestDb;
   654    660     lsm_cursor *csr;
          661  +  lsm_cursor *csr2 = 0;
   655    662     int rc;
   656    663   
   657         -  rc = lsm_csr_open(pDb->db, &csr);
   658         -  if( rc!=LSM_OK ) return rc;
          664  +  if( pDb->pCsr==0 ){
          665  +    rc = lsm_csr_open(pDb->db, &csr);
          666  +    if( rc!=LSM_OK ) return rc;
          667  +  }else{
          668  +    rc = LSM_OK;
          669  +    csr = pDb->pCsr;
          670  +  }
          671  +
          672  +  /* To enhance testing, if both pLast and pFirst are defined, seek the
          673  +  ** cursor to the "end" boundary here. Then the next block seeks it to
          674  +  ** the "start" ready for the scan. The point is to test that cursors
          675  +  ** can be reused.  */
          676  +  if( pLast && pFirst ){
          677  +    if( bReverse ){
          678  +      rc = lsm_csr_seek(csr, pFirst, nFirst, LSM_SEEK_LE);
          679  +    }else{
          680  +      rc = lsm_csr_seek(csr, pLast, nLast, LSM_SEEK_GE);
          681  +    }
          682  +  }
   659    683   
   660    684     if( bReverse ){
   661    685       if( pLast ){
   662    686         rc = lsm_csr_seek(csr, pLast, nLast, LSM_SEEK_LE);
   663    687       }else{
   664    688         rc = lsm_csr_last(csr);
   665    689       }
................................................................................
   692    716       if( bReverse ){
   693    717         rc = lsm_csr_prev(csr);
   694    718       }else{
   695    719         rc = lsm_csr_next(csr);
   696    720       }
   697    721     }
   698    722   
   699         -  lsm_csr_close(csr);
          723  +  if( pDb->pCsr==0 ){
          724  +    lsm_csr_close(csr);
          725  +  }
   700    726     return rc;
   701    727   }
   702    728   
   703    729   static int test_lsm_begin(TestDb *pTestDb, int iLevel){
   704    730     int rc = LSM_OK;
   705    731     LsmDb *pDb = (LsmDb *)pTestDb;
   706    732   
................................................................................
   757    783   
   758    784   #define TEST_NO_RECOVERY -1
   759    785   #define TEST_COMPRESSION -3
   760    786   
   761    787   #define TEST_MT_MODE     -2
   762    788   #define TEST_MT_MIN_CKPT -4
   763    789   #define TEST_MT_MAX_CKPT -5
          790  +
   764    791   
   765    792   int test_lsm_config_str(
   766    793     LsmDb *pLsm,
   767    794     lsm_db *db, 
   768    795     int bWorker,
   769    796     const char *zStr,
   770    797     int *pnThread

Changes to ext/lsm1/lsm_sorted.c.

  1918   1918       );
  1919   1919     }
  1920   1920   
  1921   1921     /* If (res<0), then key pKey/nKey is smaller than the split-key (or this
  1922   1922     ** is not a composite level and there is no split-key). Search the 
  1923   1923     ** left-hand-side of the level in this case.  */
  1924   1924     if( res<0 ){
         1925  +    int i;
  1925   1926       int iPtr = 0;
  1926   1927       if( nRhs==0 ) iPtr = (int)*piPgno;
  1927   1928   
  1928   1929       rc = seekInSegment(
  1929   1930           pCsr, &aPtr[0], iTopic, pKey, nKey, iPtr, eSeek, &iOut, &bStop
  1930   1931       );
  1931   1932       if( rc==LSM_OK && nRhs>0 && eSeek==LSM_SEEK_GE && aPtr[0].pPg==0 ){
  1932   1933         res = 0;
  1933   1934       }
         1935  +    for(i=1; i<=nRhs; i++){
         1936  +      segmentPtrReset(&aPtr[i], LSM_SEGMENTPTR_FREE_THRESHOLD);
         1937  +    }
  1934   1938     }
  1935   1939     
  1936   1940     if( res>=0 ){
  1937   1941       int bHit = 0;                 /* True if at least one rhs is not EOF */
  1938   1942       int iPtr = (int)*piPgno;
  1939   1943       int i;
         1944  +    segmentPtrReset(&aPtr[0], LSM_SEGMENTPTR_FREE_THRESHOLD);
  1940   1945       for(i=1; rc==LSM_OK && i<=nRhs && bStop==0; i++){
  1941   1946         SegmentPtr *pPtr = &aPtr[i];
  1942   1947         iOut = 0;
  1943   1948         rc = seekInSegment(
  1944   1949             pCsr, pPtr, iTopic, pKey, nKey, iPtr, eSeek, &iOut, &bStop
  1945   1950         );
  1946   1951         iPtr = iOut;
................................................................................
  2864   2869   }
  2865   2870   
  2866   2871   
  2867   2872   static int multiCursorEnd(MultiCursor *pCsr, int bLast){
  2868   2873     int rc = LSM_OK;
  2869   2874     int i;
  2870   2875   
  2871         -  pCsr->flags &= ~(CURSOR_NEXT_OK | CURSOR_PREV_OK);
         2876  +  pCsr->flags &= ~(CURSOR_NEXT_OK | CURSOR_PREV_OK | CURSOR_SEEK_EQ);
  2872   2877     pCsr->flags |= (bLast ? CURSOR_PREV_OK : CURSOR_NEXT_OK);
  2873   2878     pCsr->iFree = 0;
  2874   2879   
  2875   2880     /* Position the two in-memory tree cursors */
  2876   2881     for(i=0; rc==LSM_OK && i<2; i++){
  2877   2882       if( pCsr->apTreeCsr[i] ){
  2878   2883         rc = lsmTreeCursorEnd(pCsr->apTreeCsr[i], bLast);

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;

Changes to ext/misc/dbdump.c.

   481    481             switch( sqlite3_column_type(pStmt,i) ){
   482    482               case SQLITE_INTEGER: {
   483    483                 output_formatted(p, "%lld", sqlite3_column_int64(pStmt,i));
   484    484                 break;
   485    485               }
   486    486               case SQLITE_FLOAT: {
   487    487                 double r = sqlite3_column_double(pStmt,i);
   488         -              output_formatted(p, "%!.20g", r);
          488  +              sqlite3_uint64 ur;
          489  +              memcpy(&ur,&r,sizeof(r));
          490  +              if( ur==0x7ff0000000000000LL ){
          491  +                p->xCallback("1e999", p->pArg);
          492  +              }else if( ur==0xfff0000000000000LL ){
          493  +                p->xCallback("-1e999", p->pArg);
          494  +              }else{
          495  +                output_formatted(p, "%!.20g", r);
          496  +              }
   489    497                 break;
   490    498               }
   491    499               case SQLITE_NULL: {
   492    500                 p->xCallback("NULL", p->pArg);
   493    501                 break;
   494    502               }
   495    503               case SQLITE_TEXT: {

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   /*
................................................................................
  1798   1822       }else{
  1799   1823         jsonAppendChar(pStr, ',');
  1800   1824         pStr->pCtx = ctx;
  1801   1825       }
  1802   1826       jsonAppendValue(pStr, argv[0]);
  1803   1827     }
  1804   1828   }
  1805         -static void jsonArrayFinal(sqlite3_context *ctx){
         1829  +static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
  1806   1830     JsonString *pStr;
  1807   1831     pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
  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         -    }else{
  1815         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
         1838  +    }else if( isFinal ){
         1839  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
  1816   1840                             pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
  1817   1841         pStr->bStatic = 1;
         1842  +    }else{
         1843  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
         1844  +      pStr->nUsed--;
  1818   1845       }
  1819   1846     }else{
  1820   1847       sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
  1821   1848     }
  1822   1849     sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1823   1850   }
         1851  +static void jsonArrayValue(sqlite3_context *ctx){
         1852  +  jsonArrayCompute(ctx, 0);
         1853  +}
         1854  +static void jsonArrayFinal(sqlite3_context *ctx){
         1855  +  jsonArrayCompute(ctx, 1);
         1856  +}
         1857  +
         1858  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1859  +/*
         1860  +** This method works for both json_group_array() and json_group_object().
         1861  +** It works by removing the first element of the group by searching forward
         1862  +** to the first comma (",") that is not within a string and deleting all
         1863  +** text through that comma.
         1864  +*/
         1865  +static void jsonGroupInverse(
         1866  +  sqlite3_context *ctx,
         1867  +  int argc,
         1868  +  sqlite3_value **argv
         1869  +){
         1870  +  int i;
         1871  +  int inStr = 0;
         1872  +  char *z;
         1873  +  JsonString *pStr;
         1874  +  UNUSED_PARAM(argc);
         1875  +  UNUSED_PARAM(argv);
         1876  +  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
         1877  +#ifdef NEVER
         1878  +  /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
         1879  +  ** always have been called to initalize it */
         1880  +  if( NEVER(!pStr) ) return;
         1881  +#endif
         1882  +  z = pStr->zBuf;
         1883  +  for(i=1; z[i]!=',' || inStr; i++){
         1884  +    assert( i<pStr->nUsed );
         1885  +    if( z[i]=='"' ){
         1886  +      inStr = !inStr;
         1887  +    }else if( z[i]=='\\' ){
         1888  +      i++;
         1889  +    }
         1890  +  }
         1891  +  pStr->nUsed -= i;      
         1892  +  memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
         1893  +}
         1894  +#else
         1895  +# define jsonGroupInverse 0
         1896  +#endif
         1897  +
  1824   1898   
  1825   1899   /*
  1826   1900   ** json_group_obj(NAME,VALUE)
  1827   1901   **
  1828   1902   ** Return a JSON object composed of all names and values in the aggregate.
  1829   1903   */
  1830   1904   static void jsonObjectStep(
................................................................................
  1848   1922       z = (const char*)sqlite3_value_text(argv[0]);
  1849   1923       n = (u32)sqlite3_value_bytes(argv[0]);
  1850   1924       jsonAppendString(pStr, z, n);
  1851   1925       jsonAppendChar(pStr, ':');
  1852   1926       jsonAppendValue(pStr, argv[1]);
  1853   1927     }
  1854   1928   }
  1855         -static void jsonObjectFinal(sqlite3_context *ctx){
         1929  +static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
  1856   1930     JsonString *pStr;
  1857   1931     pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
  1858   1932     if( pStr ){
  1859   1933       jsonAppendChar(pStr, '}');
  1860   1934       if( pStr->bErr ){
  1861   1935         if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
  1862   1936         assert( pStr->bStatic );
  1863         -    }else{
  1864         -      sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
         1937  +    }else if( isFinal ){
         1938  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
  1865   1939                             pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
  1866   1940         pStr->bStatic = 1;
         1941  +    }else{
         1942  +      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
         1943  +      pStr->nUsed--;
  1867   1944       }
  1868   1945     }else{
  1869   1946       sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
  1870   1947     }
  1871   1948     sqlite3_result_subtype(ctx, JSON_SUBTYPE);
  1872   1949   }
         1950  +static void jsonObjectValue(sqlite3_context *ctx){
         1951  +  jsonObjectCompute(ctx, 0);
         1952  +}
         1953  +static void jsonObjectFinal(sqlite3_context *ctx){
         1954  +  jsonObjectCompute(ctx, 1);
         1955  +}
         1956  +
  1873   1957   
  1874   1958   
  1875   1959   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1876   1960   /****************************************************************************
  1877   1961   ** The json_each virtual table
  1878   1962   ****************************************************************************/
  1879   1963   typedef struct JsonEachCursor JsonEachCursor;
................................................................................
  2373   2457   #endif
  2374   2458     };
  2375   2459     static const struct {
  2376   2460        const char *zName;
  2377   2461        int nArg;
  2378   2462        void (*xStep)(sqlite3_context*,int,sqlite3_value**);
  2379   2463        void (*xFinal)(sqlite3_context*);
         2464  +     void (*xValue)(sqlite3_context*);
  2380   2465     } aAgg[] = {
  2381         -    { "json_group_array",     1,   jsonArrayStep,   jsonArrayFinal  },
  2382         -    { "json_group_object",    2,   jsonObjectStep,  jsonObjectFinal },
         2466  +    { "json_group_array",     1,
         2467  +      jsonArrayStep,   jsonArrayFinal,  jsonArrayValue  },
         2468  +    { "json_group_object",    2,
         2469  +      jsonObjectStep,  jsonObjectFinal, jsonObjectValue },
  2383   2470     };
  2384   2471   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2385   2472     static const struct {
  2386   2473        const char *zName;
  2387   2474        sqlite3_module *pModule;
  2388   2475     } aMod[] = {
  2389   2476       { "json_each",            &jsonEachModule               },
................................................................................
  2392   2479   #endif
  2393   2480     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
  2394   2481       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
  2395   2482                                    SQLITE_UTF8 | SQLITE_DETERMINISTIC, 
  2396   2483                                    (void*)&aFunc[i].flag,
  2397   2484                                    aFunc[i].xFunc, 0, 0);
  2398   2485     }
         2486  +#ifndef SQLITE_OMIT_WINDOWFUNC
  2399   2487     for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
  2400         -    rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
         2488  +    rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
  2401   2489                                    SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
  2402         -                                 0, aAgg[i].xStep, aAgg[i].xFinal);
         2490  +                                 aAgg[i].xStep, aAgg[i].xFinal,
         2491  +                                 aAgg[i].xValue, jsonGroupInverse, 0);
  2403   2492     }
         2493  +#endif
  2404   2494   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2405   2495     for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
  2406   2496       rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
  2407   2497     }
  2408   2498   #endif
  2409   2499     return rc;
  2410   2500   }

Changes to ext/misc/normalize.c.

   589    589             z[j++] = sqlite3Tolower(zSql[i+k]);
   590    590           }
   591    591           break;
   592    592         }
   593    593       }
   594    594     }
   595    595     while( j>0 && z[j-1]==' ' ){ j--; }
   596         -  if( i>0 && z[j-1]!=';' ){ z[j++] = ';'; }
          596  +  if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
   597    597     z[j] = 0;
   598    598   
   599    599     /* Make a second pass converting "in(...)" where the "..." is not a
   600    600     ** SELECT statement into "in(?,?,?)" */
   601    601     for(i=0; i<j; i=n){
   602    602       char *zIn = strstr(z+i, "in(");
   603    603       int nParen;

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

Changes to ext/rtree/rtree.c.

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

Changes to main.mk.

    71     71            pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    72     72            random.o resolve.o rowset.o rtree.o \
    73     73            select.o sqlite3rbu.o status.o stmt.o \
    74     74            table.o threads.o tokenize.o treeview.o trigger.o \
    75     75            update.o upsert.o userauth.o util.o vacuum.o \
    76     76            vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    77     77   	 vdbetrace.o wal.o walker.o where.o wherecode.o whereexpr.o \
    78         -         utf.o vtab.o
           78  +         utf.o vtab.o window.o
    79     79   
    80     80   LIBOBJ += sqlite3session.o
    81     81   
    82     82   # All of the source code files.
    83     83   #
    84     84   SRC = \
    85     85     $(TOP)/src/alter.c \
................................................................................
   178    178     $(TOP)/src/vxworks.h \
   179    179     $(TOP)/src/wal.c \
   180    180     $(TOP)/src/wal.h \
   181    181     $(TOP)/src/walker.c \
   182    182     $(TOP)/src/where.c \
   183    183     $(TOP)/src/wherecode.c \
   184    184     $(TOP)/src/whereexpr.c \
   185         -  $(TOP)/src/whereInt.h
          185  +  $(TOP)/src/whereInt.h \
          186  +  $(TOP)/src/window.c
   186    187   
   187    188   # Source code for extensions
   188    189   #
   189    190   SRC += \
   190    191     $(TOP)/ext/fts1/fts1.c \
   191    192     $(TOP)/ext/fts1/fts1.h \
   192    193     $(TOP)/ext/fts1/fts1_hash.c \
................................................................................
   224    225     $(TOP)/ext/fts3/fts3_write.c
   225    226   SRC += \
   226    227     $(TOP)/ext/icu/sqliteicu.h \
   227    228     $(TOP)/ext/icu/icu.c
   228    229   SRC += \
   229    230     $(TOP)/ext/rtree/sqlite3rtree.h \
   230    231     $(TOP)/ext/rtree/rtree.h \
   231         -  $(TOP)/ext/rtree/rtree.c
          232  +  $(TOP)/ext/rtree/rtree.c \
          233  +  $(TOP)/ext/rtree/geopoly.c
   232    234   SRC += \
   233    235     $(TOP)/ext/sqlrr/sqlrr.h \
   234    236     $(TOP)/ext/sqlrr/sqlrr.c
   235    237   SRC += \
   236    238     $(TOP)/ext/session/sqlite3session.c \
   237    239     $(TOP)/ext/session/sqlite3session.h
   238    240   SRC += \
................................................................................
   347    349     $(TOP)/src/test_superlock.c \
   348    350     $(TOP)/src/test_syscall.c \
   349    351     $(TOP)/src/test_tclsh.c \
   350    352     $(TOP)/src/test_tclvar.c \
   351    353     $(TOP)/src/test_thread.c \
   352    354     $(TOP)/src/test_vfs.c \
   353    355     $(TOP)/src/test_windirent.c \
          356  +  $(TOP)/src/test_window.c \
   354    357     $(TOP)/src/test_wsd.c
   355    358   
   356    359   # Extensions to be statically loaded.
   357    360   #
   358    361   TESTSRC += \
   359    362     $(TOP)/ext/misc/amatch.c \
   360    363     $(TOP)/ext/misc/carray.c \
................................................................................
   458    461      $(TOP)/src/vdbe.h \
   459    462      $(TOP)/src/vdbeInt.h \
   460    463      $(TOP)/src/vxworks.h \
   461    464      $(TOP)/src/whereInt.h
   462    465   
   463    466   # Header files used by extensions
   464    467   #
          468  +  $(TOP)/ext/fts1/fts1.h \
          469  +  $(TOP)/ext/fts1/fts1_hash.h \
          470  +  $(TOP)/ext/fts1/fts1_tokenizer.h
          471  +EXTHDR += \
          472  +  $(TOP)/ext/fts2/fts2.h \
          473  +  $(TOP)/ext/fts2/fts2_hash.h \
          474  +  $(TOP)/ext/fts2/fts2_tokenizer.h
          475  +EXTHDR += \
          476  +  $(TOP)/ext/fts3/fts3.h \
          477  +  $(TOP)/ext/fts3/fts3Int.h \
          478  +  $(TOP)/ext/fts3/fts3_hash.h \
          479  +  $(TOP)/ext/fts3/fts3_tokenizer.h
   465    480   EXTHDR += \
   466    481     $(TOP)/ext/sqlrr/sqlrr.h
          482  +EXTHDR += \
          483  +  $(TOP)/ext/rtree/rtree.h \
          484  +  $(TOP)/ext/rtree/geopoly.c
          485  +EXTHDR += \
          486  +  $(TOP)/ext/icu/sqliteicu.h
   467    487   EXTHDR += \
   468    488     $(TOP)/ext/fts5/fts5Int.h  \
   469    489     fts5parse.h                \
   470    490     $(TOP)/ext/fts5/fts5.h 
   471    491   EXTHDR += \
   472    492     $(TOP)/ext/userauth/sqlite3userauth.h
   473    493   
................................................................................
   980   1000   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \
   981   1001   		$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB)
   982   1002   
   983   1003   rollback-test$(EXE):	$(TOP)/tool/rollback-test.c sqlite3.o
   984   1004   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o rollback-test$(EXE) \
   985   1005   		$(TOP)/tool/rollback-test.c sqlite3.o $(THREADLIB)
   986   1006   
         1007  +atrc$(EXE):	$(TOP)/test/atrc.c sqlite3.o
         1008  +	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o atrc$(EXE) \
         1009  +		$(TOP)/test/atrc.c sqlite3.o $(THREADLIB)
         1010  +
   987   1011   LogEst$(EXE):	$(TOP)/tool/logest.c sqlite3.h
   988   1012   	$(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c
   989   1013   
   990   1014   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
   991   1015   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
   992   1016   		$(TOP)/test/wordcount.c sqlite3.c
   993   1017   

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 );
    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 );
   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);
   479    171   
   480    172     /* If this is a virtual table, invoke the xRename() function if
   481    173     ** one is defined. The xRename() callback will modify the names
   482    174     ** of any resources used by the v-table implementation (including other
   483    175     ** SQLite tables) that are identified by the name of the virtual table.
   484    176     */
   485    177   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
   491    183     }
   492    184   #endif
   493    185   
   494    186     /* figure out how many UTF-8 characters are in zName */
   495    187     zTabName = pTab->zName;
   496    188     nTabName = sqlite3Utf8CharLen(zTabName, -1);
   497    189   
   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
          190  +  /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
          191  +  ** the schema to use the new table name.  */
          192  +  sqlite3NestedParse(pParse, 
          193  +      "UPDATE \"%w\".%s SET "
          194  +      "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
          195  +      "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
          196  +      "AND   name NOT LIKE 'sqlite_%%'"
          197  +      , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
          198  +  );
   512    199   
   513         -  /* Modify the sqlite_master table to use the new table name. */
          200  +  /* Update the tbl_name and name columns of the sqlite_master table
          201  +  ** as required.  */
   514    202     sqlite3NestedParse(pParse,
   515    203         "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    204             "tbl_name = %Q, "
   524    205             "name = CASE "
   525    206               "WHEN type='table' THEN %Q "
   526    207               "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
   527    208                "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
   528    209               "ELSE name END "
   529    210         "WHERE tbl_name=%Q COLLATE nocase AND "
   530    211             "(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
          212  +      zDb, MASTER_NAME, 
          213  +      zName, zName, zName, 
          214  +      nTabName, zTabName
   536    215     );
   537    216   
   538    217   #ifndef SQLITE_OMIT_AUTOINCREMENT
   539    218     /* If the sqlite_sequence table exists in this database, then update 
   540    219     ** it with the new table name.
   541    220     */
   542    221     if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
   543    222       sqlite3NestedParse(pParse,
   544    223           "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
   545    224           zDb, zName, pTab->zName);
   546    225     }
   547    226   #endif
   548    227   
   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 ){
          228  +  /* If the table being renamed is not itself part of the temp database,
          229  +  ** edit view and trigger definitions within the temp database 
          230  +  ** as required.  */
          231  +  if( iDb!=1 ){
   555    232       sqlite3NestedParse(pParse, 
   556    233           "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);
          234  +            "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
          235  +            "tbl_name = "
          236  +              "CASE WHEN tbl_name=%Q COLLATE nocase AND "
          237  +              "          sqlite_rename_test(%Q, sql, type, name, 1) "
          238  +              "THEN %Q ELSE tbl_name END "
          239  +            "WHERE type IN ('view', 'trigger')"
          240  +        , zDb, zTabName, zName, zTabName, zDb, zName);
   561    241     }
   562         -#endif
   563    242   
   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->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{
         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->pTab ){
         1345  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->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  +      if( sParse.pNewTable ){
         1426  +        Table *pTab = sParse.pNewTable;
         1427  +
         1428  +        if( pTab->pSelect ){
         1429  +          NameContext sNC;
         1430  +          memset(&sNC, 0, sizeof(sNC));
         1431  +          sNC.pParse = &sParse;
         1432  +
         1433  +          sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
         1434  +          if( sParse.nErr ) rc = sParse.rc;
         1435  +          sqlite3WalkSelect(&sWalker, pTab->pSelect);
         1436  +        }else{
         1437  +          /* Modify any FK definitions to point to the new table. */
         1438  +#ifndef SQLITE_OMIT_FOREIGN_KEY
         1439  +          if( db->flags & SQLITE_ForeignKeys ){
         1440  +            FKey *pFKey;
         1441  +            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
         1442  +              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
         1443  +                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
         1444  +              }
         1445  +            }
         1446  +          }
         1447  +#endif
         1448  +
         1449  +          /* If this is the table being altered, fix any table refs in CHECK
         1450  +          ** expressions. Also update the name that appears right after the
         1451  +          ** "CREATE [VIRTUAL] TABLE" bit. */
         1452  +          if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
         1453  +            sCtx.pTab = pTab;
         1454  +            sqlite3WalkExprList(&sWalker, pTab->pCheck);
         1455  +            renameTokenFind(&sParse, &sCtx, pTab->zName);
         1456  +          }
         1457  +        }
         1458  +      }
         1459  +
         1460  +      else if( sParse.pNewIndex ){
         1461  +        renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
         1462  +        sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
         1463  +      }
         1464  +
         1465  +#ifndef SQLITE_OMIT_TRIGGER
         1466  +      else{
         1467  +        Trigger *pTrigger = sParse.pNewTrigger;
         1468  +        TriggerStep *pStep;
         1469  +        if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
         1470  +            && sCtx.pTab->pSchema==pTrigger->pTabSchema
         1471  +          ){
         1472  +          renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
         1473  +        }
         1474  +
         1475  +        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
         1476  +        if( rc==SQLITE_OK ){
         1477  +          renameWalkTrigger(&sWalker, pTrigger);
         1478  +          for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
         1479  +            if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
         1480  +              renameTokenFind(&sParse, &sCtx, pStep->zTarget);
         1481  +            }
         1482  +          }
         1483  +        }
         1484  +      }
         1485  +#endif
         1486  +    }
         1487  +
         1488  +    if( rc==SQLITE_OK ){
         1489  +      rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
         1490  +    }
         1491  +    if( rc!=SQLITE_OK ){
         1492  +      if( sParse.zErrMsg ){
         1493  +        renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
         1494  +      }else{
         1495  +        sqlite3_result_error_code(context, rc);
         1496  +      }
         1497  +    }
         1498  +
         1499  +    renameParseCleanup(&sParse);
         1500  +    renameTokenFree(db, sCtx.pList);
         1501  +    sqlite3BtreeLeaveAll(db);
         1502  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1503  +    db->xAuth = xAuth;
         1504  +#endif
         1505  +  }
         1506  +
         1507  +  return;
         1508  +}
         1509  +
         1510  +/*
         1511  +** An SQL user function that checks that there are no parse or symbol
         1512  +** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
         1513  +** After an ALTER TABLE .. RENAME operation is performed and the schema
         1514  +** reloaded, this function is called on each SQL statement in the schema
         1515  +** to ensure that it is still usable.
         1516  +**
         1517  +**   0: Database name ("main", "temp" etc.).
         1518  +**   1: SQL statement.
         1519  +**   2: Object type ("view", "table", "trigger" or "index").
         1520  +**   3: Object name.
         1521  +**   4: True if object is from temp schema.
         1522  +**
         1523  +** Unless it finds an error, this function normally returns NULL. However, it
         1524  +** returns integer value 1 if:
         1525  +**
         1526  +**   * the SQL argument creates a trigger, and
         1527  +**   * the table that the trigger is attached to is in database zDb.
         1528  +*/
         1529  +static void renameTableTest(
         1530  +  sqlite3_context *context,
         1531  +  int NotUsed,
         1532  +  sqlite3_value **argv
         1533  +){
         1534  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1535  +  char const *zDb = (const char*)sqlite3_value_text(argv[0]);
         1536  +  char const *zInput = (const char*)sqlite3_value_text(argv[1]);
         1537  +  int bTemp = sqlite3_value_int(argv[4]);
         1538  +
         1539  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1540  +  sqlite3_xauth xAuth = db->xAuth;
         1541  +  db->xAuth = 0;
         1542  +#endif
         1543  +
         1544  +  UNUSED_PARAMETER(NotUsed);
         1545  +  if( zDb && zInput ){
         1546  +    int rc;
         1547  +    Parse sParse;
         1548  +    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
         1549  +    if( rc==SQLITE_OK ){
         1550  +      if( sParse.pNewTable && sParse.pNewTable->pSelect ){
         1551  +        NameContext sNC;
         1552  +        memset(&sNC, 0, sizeof(sNC));
         1553  +        sNC.pParse = &sParse;
         1554  +        sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
         1555  +        if( sParse.nErr ) rc = sParse.rc;
         1556  +      }
         1557  +
         1558  +      else if( sParse.pNewTrigger ){
         1559  +        rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
         1560  +        if( rc==SQLITE_OK ){
         1561  +          int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
         1562  +          int i2 = sqlite3FindDbName(db, zDb);
         1563  +          if( i1==i2 ) sqlite3_result_int(context, 1);
         1564  +        }
         1565  +      }
         1566  +    }
         1567  +
         1568  +    if( rc!=SQLITE_OK ){
         1569  +      renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
         1570  +    }
         1571  +    renameParseCleanup(&sParse);
         1572  +  }
         1573  +
         1574  +#ifndef SQLITE_OMIT_AUTHORIZATION
         1575  +  db->xAuth = xAuth;
         1576  +#endif
         1577  +}
         1578  +
         1579  +/*
         1580  +** Register built-in functions used to help implement ALTER TABLE
         1581  +*/
         1582  +void sqlite3AlterFunctions(void){
         1583  +  static FuncDef aAlterTableFuncs[] = {
         1584  +    FUNCTION(sqlite_rename_column,  9, 0, 0, renameColumnFunc),
         1585  +    FUNCTION(sqlite_rename_table,  7, 0, 0, renameTableFunc),
         1586  +    FUNCTION(sqlite_rename_test,  5, 0, 0, renameTableTest),
         1587  +  };
         1588  +  sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
         1589  +}
   808   1590   #endif  /* SQLITE_ALTER_TABLE */

Changes to src/analyze.c.

   481    481   static const FuncDef statInitFuncdef = {
   482    482     2+IsStat34,      /* nArg */
   483    483     SQLITE_UTF8,     /* funcFlags */
   484    484     0,               /* pUserData */
   485    485     0,               /* pNext */
   486    486     statInit,        /* xSFunc */
   487    487     0,               /* xFinalize */
          488  +  0, 0,            /* xValue, xInverse */
   488    489     "stat_init",     /* zName */
   489    490     {0}
   490    491   };
   491    492   
   492    493   #ifdef SQLITE_ENABLE_STAT4
   493    494   /*
   494    495   ** pNew and pOld are both candidate non-periodic samples selected for 
................................................................................
   797    798   static const FuncDef statPushFuncdef = {
   798    799     2+IsStat34,      /* nArg */
   799    800     SQLITE_UTF8,     /* funcFlags */
   800    801     0,               /* pUserData */
   801    802     0,               /* pNext */
   802    803     statPush,        /* xSFunc */
   803    804     0,               /* xFinalize */
          805  +  0, 0,            /* xValue, xInverse */
   804    806     "stat_push",     /* zName */
   805    807     {0}
   806    808   };
   807    809   
   808    810   #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
   809    811   #define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
   810    812   #define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
................................................................................
   948    950   static const FuncDef statGetFuncdef = {
   949    951     1+IsStat34,      /* nArg */
   950    952     SQLITE_UTF8,     /* funcFlags */
   951    953     0,               /* pUserData */
   952    954     0,               /* pNext */
   953    955     statGet,         /* xSFunc */
   954    956     0,               /* xFinalize */
          957  +  0, 0,            /* xValue, xInverse */
   955    958     "stat_get",      /* zName */
   956    959     {0}
   957    960   };
   958    961   
   959    962   static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
   960    963     assert( regOut!=regStat4 && regOut!=regStat4+1 );
   961    964   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
................................................................................
  1267   1270         callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
  1268   1271         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
  1269   1272         VdbeCoverage(v);
  1270   1273         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1271   1274         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1272   1275         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1273   1276         sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
  1274         -      /* We know that the regSampleRowid row exists because it was read by
  1275         -      ** the previous loop.  Thus the not-found jump of seekOp will never
  1276         -      ** be taken */
  1277         -      VdbeCoverageNeverTaken(v);
         1277  +      VdbeCoverage(v);
  1278   1278   #ifdef SQLITE_ENABLE_STAT3
  1279   1279         sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
  1280   1280   #else
  1281   1281         for(i=0; i<nCol; i++){
  1282   1282           sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
  1283   1283         }
  1284   1284         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
................................................................................
  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/attach.c.

   410    410     static const FuncDef detach_func = {
   411    411       1,                /* nArg */
   412    412       SQLITE_UTF8,      /* funcFlags */
   413    413       0,                /* pUserData */
   414    414       0,                /* pNext */
   415    415       detachFunc,       /* xSFunc */
   416    416       0,                /* xFinalize */
          417  +    0, 0,             /* xValue, xInverse */
   417    418       "sqlite_detach",  /* zName */
   418    419       {0}
   419    420     };
   420    421     codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
   421    422   }
   422    423   
   423    424   /*
................................................................................
   429    430     static const FuncDef attach_func = {
   430    431       3,                /* nArg */
   431    432       SQLITE_UTF8,      /* funcFlags */
   432    433       0,                /* pUserData */
   433    434       0,                /* pNext */
   434    435       attachFunc,       /* xSFunc */
   435    436       0,                /* xFinalize */
          437  +    0, 0,             /* xValue, xInverse */
   436    438       "sqlite_attach",  /* zName */
   437    439       {0}
   438    440     };
   439    441     codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
   440    442   }
   441    443   #endif /* SQLITE_OMIT_ATTACH */
   442    444   

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/backup.c.

   378    378       }
   379    379   
   380    380       /* If there is no open read-transaction on the source database, open
   381    381       ** one now. If a transaction is opened here, then it will be closed
   382    382       ** before this function exits.
   383    383       */
   384    384       if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
   385         -      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
          385  +      rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
   386    386         bCloseTrans = 1;
   387    387       }
   388    388   
   389    389       /* If the destination database has not yet been locked (i.e. if this
   390    390       ** is the first call to backup_step() for the current backup operation),
   391    391       ** try to set its page size to the same as the source database. This
   392    392       ** is especially important on ZipVFS systems, as in that case it is
................................................................................
   394    394       ** writing to it with another.  */
   395    395       if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
   396    396         rc = SQLITE_NOMEM;
   397    397       }
   398    398   
   399    399       /* Lock the destination database, if it is not locked already. */
   400    400       if( SQLITE_OK==rc && p->bDestLocked==0
   401         -     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
          401  +     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
          402  +                                                (int*)&p->iDestSchema)) 
   402    403       ){
   403    404         p->bDestLocked = 1;
   404         -      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
   405    405       }
   406    406   
   407    407       /* Do not allow backup if the destination database is in WAL mode
   408    408       ** and the page sizes are different between source and destination */
   409    409       pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
   410    410       pgszDest = sqlite3BtreeGetPageSize(p->pDest);
   411    411       destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));

Changes to src/btree.c.

  3302   3302   ** a reserved lock.  B tries to promote to exclusive but is blocked because
  3303   3303   ** of A's read lock.  A tries to promote to reserved but is blocked by B.
  3304   3304   ** One or the other of the two processes must give way or there can be
  3305   3305   ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
  3306   3306   ** when A already has a read lock, we encourage A to give up and let B
  3307   3307   ** proceed.
  3308   3308   */
  3309         -int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
         3309  +int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
  3310   3310     BtShared *pBt = p->pBt;
  3311   3311     int rc = SQLITE_OK;
  3312   3312   
  3313   3313     sqlite3BtreeEnter(p);
  3314   3314     btreeIntegrity(p);
  3315   3315   
  3316   3316     /* If the btree is already in a write-transaction, or it
................................................................................
  3317   3317     ** is already in a read-transaction and a read-transaction
  3318   3318     ** is requested, this is a no-op.
  3319   3319     */
  3320   3320     if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
  3321   3321       goto trans_begun;
  3322   3322     }
  3323   3323     assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
         3324  +
         3325  +  if( (p->db->flags & SQLITE_ResetDatabase) 
         3326  +   && sqlite3PagerIsreadonly(pBt->pPager)==0 
         3327  +  ){
         3328  +    pBt->btsFlags &= ~BTS_READ_ONLY;
         3329  +  }
  3324   3330   
  3325   3331     /* Write transactions are not possible on a read-only database */
  3326   3332     if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
  3327   3333       rc = SQLITE_READONLY;
  3328   3334       goto trans_begun;
  3329   3335     }
  3330   3336   
................................................................................
  3377   3383       if( rc==SQLITE_OK && wrflag ){
  3378   3384         if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
  3379   3385           rc = SQLITE_READONLY;
  3380   3386         }else{
  3381   3387           rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
  3382   3388           if( rc==SQLITE_OK ){
  3383   3389             rc = newDatabase(pBt);
         3390  +        }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
         3391  +          /* if there was no transaction opened when this function was
         3392  +          ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
         3393  +          ** code to SQLITE_BUSY. */
         3394  +          rc = SQLITE_BUSY;
  3384   3395           }
  3385   3396         }
  3386   3397       }
  3387   3398     
  3388   3399       if( rc!=SQLITE_OK ){
  3389   3400         unlockBtreeIfUnused(pBt);
  3390   3401       }
................................................................................
  3428   3439           if( rc==SQLITE_OK ){
  3429   3440             put4byte(&pPage1->aData[28], pBt->nPage);
  3430   3441           }
  3431   3442         }
  3432   3443       }
  3433   3444     }
  3434   3445   
  3435         -
  3436   3446   trans_begun:
  3437         -  if( rc==SQLITE_OK && wrflag ){
  3438         -    /* This call makes sure that the pager has the correct number of
  3439         -    ** open savepoints. If the second parameter is greater than 0 and
  3440         -    ** the sub-journal is not already open, then it will be opened here.
  3441         -    */
  3442         -    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
         3447  +  if( rc==SQLITE_OK ){
         3448  +    if( pSchemaVersion ){
         3449  +      *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
         3450  +    }
         3451  +    if( wrflag ){
         3452  +      /* This call makes sure that the pager has the correct number of
         3453  +      ** open savepoints. If the second parameter is greater than 0 and
         3454  +      ** the sub-journal is not already open, then it will be opened here.
         3455  +      */
         3456  +      rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
         3457  +    }
  3443   3458     }
  3444   3459   
  3445   3460     btreeIntegrity(p);
  3446   3461     sqlite3BtreeLeave(p);
  3447   3462     return rc;
  3448   3463   }
  3449   3464   
................................................................................
  5182   5197       assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
  5183   5198       *pRes = 1;
  5184   5199       rc = SQLITE_OK;
  5185   5200     }
  5186   5201     return rc;
  5187   5202   }
  5188   5203   
         5204  +/*
         5205  +** This function is a no-op if cursor pCur does not point to a valid row.
         5206  +** Otherwise, if pCur is valid, configure it so that the next call to
         5207  +** sqlite3BtreeNext() is a no-op.
         5208  +*/
         5209  +#ifndef SQLITE_OMIT_WINDOWFUNC
         5210  +void sqlite3BtreeSkipNext(BtCursor *pCur){
         5211  +  /* We believe that the cursor must always be in the valid state when
         5212  +  ** this routine is called, but the proof is difficult, so we add an
         5213  +  ** ALWaYS() test just in case we are wrong. */
         5214  +  if( ALWAYS(pCur->eState==CURSOR_VALID) ){
         5215  +    pCur->eState = CURSOR_SKIPNEXT;
         5216  +    pCur->skipNext = 1;
         5217  +  }
         5218  +}
         5219  +#endif /* SQLITE_OMIT_WINDOWFUNC */
         5220  +
  5189   5221   /* Move the cursor to the last entry in the table.  Return SQLITE_OK
  5190   5222   ** on success.  Set *pRes to 0 if the cursor actually points to something
  5191   5223   ** or set *pRes to 1 if the table is empty.
  5192   5224   */
  5193   5225   int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
  5194   5226     int rc;
  5195   5227    
................................................................................
  5586   5618         }
  5587   5619         pCur->skipNext = 0;
  5588   5620       }
  5589   5621     }
  5590   5622   
  5591   5623     pPage = pCur->pPage;
  5592   5624     idx = ++pCur->ix;
  5593         -  assert( pPage->isInit );
         5625  +  if( !pPage->isInit ){
         5626  +    /* The only known way for this to happen is for there to be a
         5627  +    ** recursive SQL function that does a DELETE operation as part of a
         5628  +    ** SELECT which deletes content out from under an active cursor
         5629  +    ** in a corrupt database file where the table being DELETE-ed from
         5630  +    ** has pages in common with the table being queried.  See TH3
         5631  +    ** module cov1/btree78.test testcase 220 (2018-06-08) for an
         5632  +    ** example. */
         5633  +    return SQLITE_CORRUPT_BKPT;
         5634  +  }
  5594   5635   
  5595   5636     /* If the database file is corrupt, it is possible for the value of idx 
  5596   5637     ** to be invalid here. This can only occur if a second cursor modifies
  5597   5638     ** the page while cursor pCur is holding a reference to it. Which can
  5598   5639     ** only happen if the database is corrupt in such a way as to link the
  5599   5640     ** page into more than one b-tree structure. */
  5600   5641     testcase( idx>pPage->nCell );
................................................................................
  9298   9339   ** reference to the page, add an error message to pCheck->zErrMsg.
  9299   9340   ** Return 1 if there are 2 or more references to the page and 0 if
  9300   9341   ** if this is the first reference to the page.
  9301   9342   **
  9302   9343   ** Also check that the page number is in bounds.
  9303   9344   */
  9304   9345   static int checkRef(IntegrityCk *pCheck, Pgno iPage){
  9305         -  if( iPage==0 ) return 1;
  9306         -  if( iPage>pCheck->nPage ){
         9346  +  if( iPage>pCheck->nPage || iPage==0 ){
  9307   9347       checkAppendMsg(pCheck, "invalid page number %d", iPage);
  9308   9348       return 1;
  9309   9349     }
  9310   9350     if( getPageReferenced(pCheck, iPage) ){
  9311   9351       checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
  9312   9352       return 1;
  9313   9353     }
................................................................................
  9354   9394     IntegrityCk *pCheck,  /* Integrity checking context */
  9355   9395     int isFreeList,       /* True for a freelist.  False for overflow page list */
  9356   9396     int iPage,            /* Page number for first page in the list */
  9357   9397     int N                 /* Expected number of pages in the list */
  9358   9398   ){
  9359   9399     int i;
  9360   9400     int expected = N;
  9361         -  int iFirst = iPage;
  9362         -  while( N-- > 0 && pCheck->mxErr ){
         9401  +  int nErrAtStart = pCheck->nErr;
         9402  +  while( iPage!=0 && pCheck->mxErr ){
  9363   9403       DbPage *pOvflPage;
  9364   9404       unsigned char *pOvflData;
  9365         -    if( iPage<1 ){
  9366         -      checkAppendMsg(pCheck,
  9367         -         "%d of %d pages missing from overflow list starting at %d",
  9368         -          N+1, expected, iFirst);
  9369         -      break;
  9370         -    }
  9371   9405       if( checkRef(pCheck, iPage) ) break;
         9406  +    N--;
  9372   9407       if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
  9373   9408         checkAppendMsg(pCheck, "failed to get page %d", iPage);
  9374   9409         break;
  9375   9410       }
  9376   9411       pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
  9377   9412       if( isFreeList ){
  9378   9413         int n = get4byte(&pOvflData[4]);
................................................................................
  9408   9443           i = get4byte(pOvflData);
  9409   9444           checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
  9410   9445         }
  9411   9446       }
  9412   9447   #endif
  9413   9448       iPage = get4byte(pOvflData);
  9414   9449       sqlite3PagerUnref(pOvflPage);
  9415         -
  9416         -    if( isFreeList && N<(iPage!=0) ){
  9417         -      checkAppendMsg(pCheck, "free-page count in header is too small");
  9418         -    }
         9450  +  }
         9451  +  if( N && nErrAtStart==pCheck->nErr ){
         9452  +    checkAppendMsg(pCheck,
         9453  +      "%s is %d but should be %d",
         9454  +      isFreeList ? "size" : "overflow list length",
         9455  +      expected-N, expected);
  9419   9456     }
  9420   9457   }
  9421   9458   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  9422   9459   
  9423   9460   /*
  9424   9461   ** An implementation of a min-heap.
  9425   9462   **
................................................................................
  9805   9842     sCheck.zPfx = "Main freelist: ";
  9806   9843     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
  9807   9844               get4byte(&pBt->pPage1->aData[36]));
  9808   9845     sCheck.zPfx = 0;
  9809   9846   
  9810   9847     /* Check all the tables.
  9811   9848     */
         9849  +#ifndef SQLITE_OMIT_AUTOVACUUM
         9850  +  if( pBt->autoVacuum ){
         9851  +    int mx = 0;
         9852  +    int mxInHdr;
         9853  +    for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
         9854  +    mxInHdr = get4byte(&pBt->pPage1->aData[52]);
         9855  +    if( mx!=mxInHdr ){
         9856  +      checkAppendMsg(&sCheck,
         9857  +        "max rootpage (%d) disagrees with header (%d)",
         9858  +        mx, mxInHdr
         9859  +      );
         9860  +    }
         9861  +  }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
         9862  +    checkAppendMsg(&sCheck,
         9863  +      "incremental_vacuum enabled with a max rootpage of zero"
         9864  +    );
         9865  +  }
         9866  +#endif
  9812   9867     testcase( pBt->db->flags & SQLITE_CellSizeCk );
  9813   9868     pBt->db->flags &= ~SQLITE_CellSizeCk;
  9814   9869     for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
  9815   9870       i64 notUsed;
  9816   9871       if( aRoot[i]==0 ) continue;
  9817   9872   #ifndef SQLITE_OMIT_AUTOVACUUM
  9818   9873       if( pBt->autoVacuum && aRoot[i]>1 ){
................................................................................
 10086  10141   
 10087  10142     /* If setting the version fields to 1, do not automatically open the
 10088  10143     ** WAL connection, even if the version fields are currently set to 2.
 10089  10144     */
 10090  10145     pBt->btsFlags &= ~BTS_NO_WAL;
 10091  10146     if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
 10092  10147   
 10093         -  rc = sqlite3BtreeBeginTrans(pBtree, 0);
        10148  +  rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
 10094  10149     if( rc==SQLITE_OK ){
 10095  10150       u8 *aData = pBt->pPage1->aData;
 10096  10151       if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
 10097         -      rc = sqlite3BtreeBeginTrans(pBtree, 2);
        10152  +      rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
 10098  10153         if( rc==SQLITE_OK ){
 10099  10154           rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
 10100  10155           if( rc==SQLITE_OK ){
 10101  10156             aData[18] = (u8)iVersion;
 10102  10157             aData[19] = (u8)iVersion;
 10103  10158           }
 10104  10159         }

Changes to src/btree.h.

    74     74   int sqlite3BtreeMaxPageCount(Btree*,int);
    75     75   u32 sqlite3BtreeLastPage(Btree*);
    76     76   int sqlite3BtreeSecureDelete(Btree*,int);
    77     77   int sqlite3BtreeGetOptimalReserve(Btree*);
    78     78   int sqlite3BtreeGetReserveNoMutex(Btree *p);
    79     79   int sqlite3BtreeSetAutoVacuum(Btree *, int);
    80     80   int sqlite3BtreeGetAutoVacuum(Btree *);
    81         -int sqlite3BtreeBeginTrans(Btree*,int);
           81  +int sqlite3BtreeBeginTrans(Btree*,int,int*);
    82     82   int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
    83     83   int sqlite3BtreeCommitPhaseTwo(Btree*, int);
    84     84   int sqlite3BtreeCommit(Btree*);
    85     85   int sqlite3BtreeRollback(Btree*,int,int);
    86     86   int sqlite3BtreeBeginStmt(Btree*,int);
    87     87   int sqlite3BtreeCreateTable(Btree*, int*, int flags);
    88     88   int sqlite3BtreeIsInTrans(Btree*);
................................................................................
   297    297     int nData;              /* Size of pData.  0 if none. */
   298    298     int nZero;              /* Extra zero data appended after pData,nData */
   299    299   };
   300    300   
   301    301   int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
   302    302                          int flags, int seekResult);
   303    303   int sqlite3BtreeFirst(BtCursor*, int *pRes);
          304  +#ifndef SQLITE_OMIT_WINDOWFUNC
          305  +void sqlite3BtreeSkipNext(BtCursor*);
          306  +#endif
   304    307   int sqlite3BtreeLast(BtCursor*, int *pRes);
   305    308   int sqlite3BtreeNext(BtCursor*, int flags);
   306    309   int sqlite3BtreeEof(BtCursor*);
   307    310   int sqlite3BtreePrevious(BtCursor*, int flags);
   308    311   i64 sqlite3BtreeIntegerKey(BtCursor*);
   309    312   #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
   310    313   i64 sqlite3BtreeOffset(BtCursor*);

Changes to src/build.c.

   221    221       }
   222    222     }
   223    223   
   224    224   
   225    225     /* Get the VDBE program ready for execution
   226    226     */
   227    227     if( v && pParse->nErr==0 && !db->mallocFailed ){
   228         -    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   229    228       /* A minimum of one cursor is required if autoincrement is used
   230    229       *  See ticket [a696379c1f08866] */
   231    230       if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   232    231       sqlite3VdbeMakeReady(v, pParse);
   233    232       pParse->rc = SQLITE_DONE;
   234    233     }else{
   235    234       pParse->rc = SQLITE_ERROR;
................................................................................
   436    435     }
   437    436     return p;
   438    437   }
   439    438   
   440    439   /*
   441    440   ** Reclaim the memory used by an index
   442    441   */
   443         -static void freeIndex(sqlite3 *db, Index *p){
          442  +void sqlite3FreeIndex(sqlite3 *db, Index *p){
   444    443   #ifndef SQLITE_OMIT_ANALYZE
   445    444     sqlite3DeleteIndexSamples(db, p);
   446    445   #endif
   447    446     sqlite3ExprDelete(db, p->pPartIdxWhere);
   448    447     sqlite3ExprListDelete(db, p->aColExpr);
   449    448     sqlite3DbFree(db, p->zColAff);
   450    449     if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
................................................................................
   476    475         ** indices. */
   477    476         p = pIndex->pTable->pIndex;
   478    477         while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
   479    478         if( ALWAYS(p && p->pNext==pIndex) ){
   480    479           p->pNext = pIndex->pNext;
   481    480         }
   482    481       }
   483         -    freeIndex(db, pIndex);
          482  +    sqlite3FreeIndex(db, pIndex);
   484    483     }
   485    484     db->mDbFlags |= DBFLAG_SchemaChange;
   486    485   }
   487    486   
   488    487   /*
   489    488   ** Look through the list of open database files in db->aDb[] and if
   490    489   ** any have been closed, remove them from the list.  Reallocate the
................................................................................
   622    621         char *zName = pIndex->zName; 
   623    622         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   624    623            &pIndex->pSchema->idxHash, zName, 0
   625    624         );
   626    625         assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   627    626         assert( pOld==pIndex || pOld==0 );
   628    627       }
   629         -    freeIndex(db, pIndex);
          628  +    sqlite3FreeIndex(db, pIndex);
   630    629     }
   631    630   
   632    631     /* Delete any foreign keys attached to this table. */
   633    632     sqlite3FkDelete(db, pTable);
   634    633   
   635    634     /* Delete the Table structure itself.
   636    635     */
................................................................................
   780    779       *pUnqual = pName2;
   781    780       iDb = sqlite3FindDb(db, pName1);
   782    781       if( iDb<0 ){
   783    782         sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
   784    783         return -1;
   785    784       }
   786    785     }else{
   787         -    assert( db->init.iDb==0 || db->init.busy
          786  +    assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
   788    787                || (db->mDbFlags & DBFLAG_Vacuum)!=0);
   789    788       iDb = db->init.iDb;
   790    789       *pUnqual = pName1;
   791    790     }
   792    791     return iDb;
   793    792   }
   794    793   
................................................................................
   875    874         /* If creating a temp table, the name may not be qualified. Unless 
   876    875         ** the database name is "temp" anyway.  */
   877    876         sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
   878    877         return;
   879    878       }
   880    879       if( !OMIT_TEMPDB && isTemp ) iDb = 1;
   881    880       zName = sqlite3NameFromToken(db, pName);
          881  +    if( IN_RENAME_OBJECT ){
          882  +      sqlite3RenameTokenMap(pParse, (void*)zName, pName);
          883  +    }
   882    884     }
   883    885     pParse->sNameToken = *pName;
   884    886     if( zName==0 ) return;
   885    887     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   886    888       goto begin_table_error;
   887    889     }
   888    890     if( db->init.iDb==1 ) isTemp = 1;
................................................................................
   910    912     /* Make sure the new table name does not collide with an existing
   911    913     ** index or table name in the same database.  Issue an error message if
   912    914     ** it does. The exception is if the statement being parsed was passed
   913    915     ** to an sqlite3_declare_vtab() call. In that case only the column names
   914    916     ** and types will be used, so there is no need to test for namespace
   915    917     ** collisions.
   916    918     */
   917         -  if( !IN_DECLARE_VTAB ){
          919  +  if( !IN_SPECIAL_PARSE ){
   918    920       char *zDb = db->aDb[iDb].zDbSName;
   919    921       if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   920    922         goto begin_table_error;
   921    923       }
   922    924       pTable = sqlite3FindTable(db, zName, zDb);
   923    925       if( pTable ){
   924    926         if( !noErr ){
................................................................................
  1069   1071     if( (p = pParse->pNewTable)==0 ) return;
  1070   1072     if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1071   1073       sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
  1072   1074       return;
  1073   1075     }
  1074   1076     z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
  1075   1077     if( z==0 ) return;
         1078  +  if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
  1076   1079     memcpy(z, pName->z, pName->n);
  1077   1080     z[pName->n] = 0;
  1078   1081     sqlite3Dequote(z);
  1079   1082     for(i=0; i<p->nCol; i++){
  1080   1083       if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
  1081   1084         sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
  1082   1085         sqlite3DbFree(db, z);
................................................................................
  1275   1278         x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
  1276   1279         x.pLeft = pExpr;
  1277   1280         x.flags = EP_Skip;
  1278   1281         pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
  1279   1282         sqlite3DbFree(db, x.u.zToken);
  1280   1283       }
  1281   1284     }
         1285  +  if( IN_RENAME_OBJECT ){
         1286  +    sqlite3RenameExprUnmap(pParse, pExpr);
         1287  +  }
  1282   1288     sqlite3ExprDelete(db, pExpr);
  1283   1289   }
  1284   1290   
  1285   1291   /*
  1286   1292   ** Backwards Compatibility Hack:
  1287   1293   ** 
  1288   1294   ** Historical versions of SQLite accepted strings as column names in
................................................................................
  1366   1372       }
  1367   1373     }
  1368   1374     if( nTerm==1
  1369   1375      && pCol
  1370   1376      && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
  1371   1377      && sortOrder!=SQLITE_SO_DESC
  1372   1378     ){
         1379  +    if( IN_RENAME_OBJECT && pList ){
         1380  +      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
         1381  +    }
  1373   1382       pTab->iPKey = iCol;
  1374   1383       pTab->keyConf = (u8)onError;
  1375   1384       assert( autoInc==0 || autoInc==1 );
  1376   1385       pTab->tabFlags |= autoInc*TF_Autoincrement;
  1377   1386       if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
  1378   1387     }else if( autoInc ){
  1379   1388   #ifndef SQLITE_OMIT_AUTOINCREMENT
................................................................................
  1690   1699   
  1691   1700   /* Return true if value x is found any of the first nCol entries of aiCol[]
  1692   1701   */
  1693   1702   static int hasColumn(const i16 *aiCol, int nCol, int x){
  1694   1703     while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
  1695   1704     return 0;
  1696   1705   }
         1706  +
         1707  +/* Recompute the colNotIdxed field of the Index.
         1708  +**
         1709  +** colNotIdxed is a bitmask that has a 0 bit representing each indexed
         1710  +** columns that are within the first 63 columns of the table.  The
         1711  +** high-order bit of colNotIdxed is always 1.  All unindexed columns
         1712  +** of the table have a 1.
         1713  +**
         1714  +** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
         1715  +** to determine if the index is covering index.
         1716  +*/
         1717  +static void recomputeColumnsNotIndexed(Index *pIdx){
         1718  +  Bitmask m = 0;
         1719  +  int j;
         1720  +  for(j=pIdx->nColumn-1; j>=0; j--){
         1721  +    int x = pIdx->aiColumn[j];
         1722  +    if( x>=0 ){
         1723  +      testcase( x==BMS-1 );
         1724  +      testcase( x==BMS-2 );
         1725  +      if( x<BMS-1 ) m |= MASKBIT(x);
         1726  +    }
         1727  +  }
         1728  +  pIdx->colNotIdxed = ~m;
         1729  +  assert( (pIdx->colNotIdxed>>63)==1 );
         1730  +}
  1697   1731   
  1698   1732   /*
  1699   1733   ** This routine runs at the end of parsing a CREATE TABLE statement that
  1700   1734   ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
  1701   1735   ** internal schema data structures and the generated VDBE code so that they
  1702   1736   ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
  1703   1737   ** Changes include:
................................................................................
  1759   1793       pList = sqlite3ExprListAppend(pParse, 0, 
  1760   1794                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
  1761   1795       if( pList==0 ) return;
  1762   1796       pList->a[0].sortOrder = pParse->iPkSortOrder;
  1763   1797       assert( pParse->pNewTable==pTab );
  1764   1798       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
  1765   1799                          SQLITE_IDXTYPE_PRIMARYKEY);
  1766         -    if( db->mallocFailed ) return;
         1800  +    if( db->mallocFailed || pParse->nErr ) return;
  1767   1801       pPk = sqlite3PrimaryKeyIndex(pTab);
  1768   1802       pTab->iPKey = -1;
  1769   1803     }else{
  1770   1804       pPk = sqlite3PrimaryKeyIndex(pTab);
  1771   1805   
  1772   1806       /*
  1773   1807       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
................................................................................
  1839   1873         }
  1840   1874       }
  1841   1875       assert( pPk->nColumn==j );
  1842   1876       assert( pTab->nCol==j );
  1843   1877     }else{
  1844   1878       pPk->nColumn = pTab->nCol;
  1845   1879     }
         1880  +  recomputeColumnsNotIndexed(pPk);
  1846   1881   }
  1847   1882   
  1848   1883   /*
  1849   1884   ** This routine is called to report the final ")" that terminates
  1850   1885   ** a CREATE TABLE statement.
  1851   1886   **
  1852   1887   ** The table structure that other action routines have been building
................................................................................
  2142   2177     if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
  2143   2178   
  2144   2179     /* Make a copy of the entire SELECT statement that defines the view.
  2145   2180     ** This will force all the Expr.token.z values to be dynamically
  2146   2181     ** allocated rather than point to the input string - which means that
  2147   2182     ** they will persist after the current sqlite3_exec() call returns.
  2148   2183     */
  2149         -  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
         2184  +  if( IN_RENAME_OBJECT ){
         2185  +    p->pSelect = pSelect;
         2186  +    pSelect = 0;
         2187  +  }else{
         2188  +    p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
         2189  +  }
  2150   2190     p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
  2151   2191     if( db->mallocFailed ) goto create_view_fail;
  2152   2192   
  2153   2193     /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
  2154   2194     ** the end.
  2155   2195     */
  2156   2196     sEnd = pParse->sLastToken;
................................................................................
  2167   2207     sEnd.n = 1;
  2168   2208   
  2169   2209     /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
  2170   2210     sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
  2171   2211   
  2172   2212   create_view_fail:
  2173   2213     sqlite3SelectDelete(db, pSelect);
         2214  +  if( IN_RENAME_OBJECT ){
         2215  +    sqlite3RenameExprlistUnmap(pParse, pCNames);
         2216  +  }
  2174   2217     sqlite3ExprListDelete(db, pCNames);
  2175   2218     return;
  2176   2219   }
  2177   2220   #endif /* SQLITE_OMIT_VIEW */
  2178   2221   
  2179   2222   #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  2180   2223   /*
................................................................................
  2289   2332       sqlite3DeleteTable(db, pSelTab);
  2290   2333       sqlite3SelectDelete(db, pSel);
  2291   2334       db->lookaside.bDisable--;
  2292   2335     } else {
  2293   2336       nErr++;
  2294   2337     }
  2295   2338     pTable->pSchema->schemaFlags |= DB_UnresetViews;
         2339  +  if( db->mallocFailed ){
         2340  +    sqlite3DeleteColumnNames(db, pTable);
         2341  +    pTable->aCol = 0;
         2342  +    pTable->nCol = 0;
         2343  +  }
  2296   2344   #endif /* SQLITE_OMIT_VIEW */
  2297   2345     return nErr;  
  2298   2346   }
  2299   2347   #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
  2300   2348   
  2301   2349   #ifndef SQLITE_OMIT_VIEW
  2302   2350   /*
................................................................................
  2627   2675   
  2628   2676     /* Generate code to remove the table from the master table
  2629   2677     ** on disk.
  2630   2678     */
  2631   2679     v = sqlite3GetVdbe(pParse);
  2632   2680     if( v ){
  2633   2681       sqlite3BeginWriteOperation(pParse, 1, iDb);
  2634         -    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
  2635         -    sqlite3FkDropTable(pParse, pName, pTab);
         2682  +    if( !isView ){
         2683  +      sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
         2684  +      sqlite3FkDropTable(pParse, pName, pTab);
         2685  +    }
  2636   2686       sqlite3CodeDropTable(pParse, pTab, iDb, isView);
  2637   2687     }
  2638   2688   
  2639   2689   exit_drop_table:
  2640   2690     sqlite3SrcListDelete(db, pName);
  2641   2691   }
  2642   2692   
................................................................................
  2703   2753     if( pFKey==0 ){
  2704   2754       goto fk_end;
  2705   2755     }
  2706   2756     pFKey->pFrom = p;
  2707   2757     pFKey->pNextFrom = p->pFKey;
  2708   2758     z = (char*)&pFKey->aCol[nCol];
  2709   2759     pFKey->zTo = z;
         2760  +  if( IN_RENAME_OBJECT ){
         2761  +    sqlite3RenameTokenMap(pParse, (void*)z, pTo);
         2762  +  }
  2710   2763     memcpy(z, pTo->z, pTo->n);
  2711   2764     z[pTo->n] = 0;
  2712   2765     sqlite3Dequote(z);
  2713   2766     z += pTo->n+1;
  2714   2767     pFKey->nCol = nCol;
  2715   2768     if( pFromCol==0 ){
  2716   2769       pFKey->aCol[0].iFrom = p->nCol-1;
................................................................................
  2725   2778         }
  2726   2779         if( j>=p->nCol ){
  2727   2780           sqlite3ErrorMsg(pParse, 
  2728   2781             "unknown column \"%s\" in foreign key definition", 
  2729   2782             pFromCol->a[i].zName);
  2730   2783           goto fk_end;
  2731   2784         }
         2785  +      if( IN_RENAME_OBJECT ){
         2786  +        sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
         2787  +      }
  2732   2788       }
  2733   2789     }
  2734   2790     if( pToCol ){
  2735   2791       for(i=0; i<nCol; i++){
  2736   2792         int n = sqlite3Strlen30(pToCol->a[i].zName);
  2737   2793         pFKey->aCol[i].zCol = z;
         2794  +      if( IN_RENAME_OBJECT ){
         2795  +        sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
         2796  +      }
  2738   2797         memcpy(z, pToCol->a[i].zName, n);
  2739   2798         z[n] = 0;
  2740   2799         z += n+1;
  2741   2800       }
  2742   2801     }
  2743   2802     pFKey->isDeferred = 0;
  2744   2803     pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
................................................................................
  3063   3122     if( pName ){
  3064   3123       zName = sqlite3NameFromToken(db, pName);
  3065   3124       if( zName==0 ) goto exit_create_index;
  3066   3125       assert( pName->z!=0 );
  3067   3126       if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
  3068   3127         goto exit_create_index;
  3069   3128       }
  3070         -    if( !db->init.busy ){
  3071         -      if( sqlite3FindTable(db, zName, 0)!=0 ){
  3072         -        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
         3129  +    if( !IN_RENAME_OBJECT ){
         3130  +      if( !db->init.busy ){
         3131  +        if( sqlite3FindTable(db, zName, 0)!=0 ){
         3132  +          sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
         3133  +          goto exit_create_index;
         3134  +        }
         3135  +      }
         3136  +      if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
         3137  +        if( !ifNotExist ){
         3138  +          sqlite3ErrorMsg(pParse, "index %s already exists", zName);
         3139  +        }else{
         3140  +          assert( !db->init.busy );
         3141  +          sqlite3CodeVerifySchema(pParse, iDb);
         3142  +        }
  3073   3143           goto exit_create_index;
  3074   3144         }
  3075   3145       }
  3076         -    if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
  3077         -      if( !ifNotExist ){
  3078         -        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
  3079         -      }else{
  3080         -        assert( !db->init.busy );
  3081         -        sqlite3CodeVerifySchema(pParse, iDb);
  3082         -      }
  3083         -      goto exit_create_index;
  3084         -    }
  3085   3146     }else{
  3086   3147       int n;
  3087   3148       Index *pLoop;
  3088   3149       for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
  3089   3150       zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
  3090   3151       if( zName==0 ){
  3091   3152         goto exit_create_index;
................................................................................
  3092   3153       }
  3093   3154   
  3094   3155       /* Automatic index names generated from within sqlite3_declare_vtab()
  3095   3156       ** must have names that are distinct from normal automatic index names.
  3096   3157       ** The following statement converts "sqlite3_autoindex..." into
  3097   3158       ** "sqlite3_butoindex..." in order to make the names distinct.
  3098   3159       ** The "vtab_err.test" test demonstrates the need of this statement. */
  3099         -    if( IN_DECLARE_VTAB ) zName[7]++;
         3160  +    if( IN_SPECIAL_PARSE ) zName[7]++;
  3100   3161     }
  3101   3162   
  3102   3163     /* Check for authorization to create an index.
  3103   3164     */
  3104   3165   #ifndef SQLITE_OMIT_AUTHORIZATION
  3105         -  {
         3166  +  if( !IN_RENAME_OBJECT ){
  3106   3167       const char *zDb = pDb->zDbSName;
  3107   3168       if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
  3108   3169         goto exit_create_index;
  3109   3170       }
  3110   3171       i = SQLITE_CREATE_INDEX;
  3111   3172       if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
  3112   3173       if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
................................................................................
  3185   3246     ** a table column, store that column in aiColumn[].  For general expressions,
  3186   3247     ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
  3187   3248     **
  3188   3249     ** TODO: Issue a warning if two or more columns of the index are identical.
  3189   3250     ** TODO: Issue a warning if the table primary key is used as part of the
  3190   3251     ** index key.
  3191   3252     */
  3192         -  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
         3253  +  pListItem = pList->a;
         3254  +  if( IN_RENAME_OBJECT ){
         3255  +    pIndex->aColExpr = pList;
         3256  +    pList = 0;
         3257  +  }
         3258  +  for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
  3193   3259       Expr *pCExpr;                  /* The i-th index expression */
  3194   3260       int requestedSortOrder;        /* ASC or DESC on the i-th expression */
  3195   3261       const char *zColl;             /* Collation sequence name */
  3196   3262   
  3197   3263       sqlite3StringToId(pListItem->pExpr);
  3198   3264       sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
  3199   3265       if( pParse->nErr ) goto exit_create_index;
................................................................................
  3201   3267       if( pCExpr->op!=TK_COLUMN ){
  3202   3268         if( pTab==pParse->pNewTable ){
  3203   3269           sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
  3204   3270                                   "UNIQUE constraints");
  3205   3271           goto exit_create_index;
  3206   3272         }
  3207   3273         if( pIndex->aColExpr==0 ){
  3208         -        ExprList *pCopy = sqlite3ExprListDup(db, pList, 0);
  3209         -        pIndex->aColExpr = pCopy;
  3210         -        if( !db->mallocFailed ){
  3211         -          assert( pCopy!=0 );
  3212         -          pListItem = &pCopy->a[i];
  3213         -        }
         3274  +        pIndex->aColExpr = pList;
         3275  +        pList = 0;
  3214   3276         }
  3215   3277         j = XN_EXPR;
  3216   3278         pIndex->aiColumn[i] = XN_EXPR;
  3217   3279         pIndex->uniqNotNull = 0;
  3218   3280       }else{
  3219   3281         j = pCExpr->iColumn;
  3220   3282         assert( j<=0x7fff );
................................................................................
  3272   3334     sqlite3DefaultRowEst(pIndex);
  3273   3335     if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
  3274   3336   
  3275   3337     /* If this index contains every column of its table, then mark
  3276   3338     ** it as a covering index */
  3277   3339     assert( HasRowid(pTab) 
  3278   3340         || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
         3341  +  recomputeColumnsNotIndexed(pIndex);
  3279   3342     if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
  3280   3343       pIndex->isCovering = 1;
  3281   3344       for(j=0; j<pTab->nCol; j++){
  3282   3345         if( j==pTab->iPKey ) continue;
  3283   3346         if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
  3284   3347         pIndex->isCovering = 0;
  3285   3348         break;
................................................................................
  3344   3407           }
  3345   3408           if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
  3346   3409           goto exit_create_index;
  3347   3410         }
  3348   3411       }
  3349   3412     }
  3350   3413   
  3351         -  /* Link the new Index structure to its table and to the other
  3352         -  ** in-memory database structures. 
  3353         -  */
  3354         -  assert( pParse->nErr==0 );
  3355         -  if( db->init.busy ){
  3356         -    Index *p;
  3357         -    assert( !IN_DECLARE_VTAB );
  3358         -    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
  3359         -    p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
  3360         -                          pIndex->zName, pIndex);
  3361         -    if( p ){
  3362         -      assert( p==pIndex );  /* Malloc must have failed */
  3363         -      sqlite3OomFault(db);
  3364         -      goto exit_create_index;
  3365         -    }
  3366         -    db->mDbFlags |= DBFLAG_SchemaChange;
  3367         -    if( pTblName!=0 ){
  3368         -      pIndex->tnum = db->init.newTnum;
  3369         -    }
  3370         -  }
  3371         -
  3372         -  /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
  3373         -  ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
  3374         -  ** emit code to allocate the index rootpage on disk and make an entry for
  3375         -  ** the index in the sqlite_master table and populate the index with
  3376         -  ** content.  But, do not do this if we are simply reading the sqlite_master
  3377         -  ** table to parse the schema, or if this index is the PRIMARY KEY index
  3378         -  ** of a WITHOUT ROWID table.
  3379         -  **
  3380         -  ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
  3381         -  ** or UNIQUE index in a CREATE TABLE statement.  Since the table
  3382         -  ** has just been created, it contains no data and the index initialization
  3383         -  ** step can be skipped.
  3384         -  */
  3385         -  else if( HasRowid(pTab) || pTblName!=0 ){
  3386         -    Vdbe *v;
  3387         -    char *zStmt;
  3388         -    int iMem = ++pParse->nMem;
  3389         -
  3390         -    v = sqlite3GetVdbe(pParse);
  3391         -    if( v==0 ) goto exit_create_index;
  3392         -
  3393         -    sqlite3BeginWriteOperation(pParse, 1, iDb);
  3394         -
  3395         -    /* Create the rootpage for the index using CreateIndex. But before
  3396         -    ** doing so, code a Noop instruction and store its address in 
  3397         -    ** Index.tnum. This is required in case this index is actually a 
  3398         -    ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
  3399         -    ** that case the convertToWithoutRowidTable() routine will replace
  3400         -    ** the Noop with a Goto to jump over the VDBE code generated below. */
  3401         -    pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
  3402         -    sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
  3403         -
  3404         -    /* Gather the complete text of the CREATE INDEX statement into
  3405         -    ** the zStmt variable
         3414  +  if( !IN_RENAME_OBJECT ){
         3415  +
         3416  +    /* Link the new Index structure to its table and to the other
         3417  +    ** in-memory database structures. 
         3418  +    */
         3419  +    assert( pParse->nErr==0 );
         3420  +    if( db->init.busy ){
         3421  +      Index *p;
         3422  +      assert( !IN_SPECIAL_PARSE );
         3423  +      assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
         3424  +      p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
         3425  +          pIndex->zName, pIndex);
         3426  +      if( p ){
         3427  +        assert( p==pIndex );  /* Malloc must have failed */
         3428  +        sqlite3OomFault(db);
         3429  +        goto exit_create_index;
         3430  +      }
         3431  +      db->mDbFlags |= DBFLAG_SchemaChange;
         3432  +      if( pTblName!=0 ){
         3433  +        pIndex->tnum = db->init.newTnum;
         3434  +      }
         3435  +    }
         3436  +
         3437  +    /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
         3438  +    ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
         3439  +    ** emit code to allocate the index rootpage on disk and make an entry for
         3440  +    ** the index in the sqlite_master table and populate the index with
         3441  +    ** content.  But, do not do this if we are simply reading the sqlite_master
         3442  +    ** table to parse the schema, or if this index is the PRIMARY KEY index
         3443  +    ** of a WITHOUT ROWID table.
         3444  +    **
         3445  +    ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
         3446  +    ** or UNIQUE index in a CREATE TABLE statement.  Since the table
         3447  +    ** has just been created, it contains no data and the index initialization
         3448  +    ** step can be skipped.
  3406   3449       */
  3407         -    if( pStart ){
  3408         -      int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
  3409         -      if( pName->z[n-1]==';' ) n--;
  3410         -      /* A named index with an explicit CREATE INDEX statement */
  3411         -      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  3412         -        onError==OE_None ? "" : " UNIQUE", n, pName->z);
  3413         -    }else{
  3414         -      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
  3415         -      /* zStmt = sqlite3MPrintf(""); */
  3416         -      zStmt = 0;
  3417         -    }
  3418         -
  3419         -    /* Add an entry in sqlite_master for this index
  3420         -    */
  3421         -    sqlite3NestedParse(pParse, 
  3422         -        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
  3423         -        db->aDb[iDb].zDbSName, MASTER_NAME,
  3424         -        pIndex->zName,
  3425         -        pTab->zName,
  3426         -        iMem,
  3427         -        zStmt
  3428         -    );
  3429         -    sqlite3DbFree(db, zStmt);
  3430         -
  3431         -    /* Fill the index with data and reparse the schema. Code an OP_Expire
  3432         -    ** to invalidate all pre-compiled statements.
  3433         -    */
  3434         -    if( pTblName ){
  3435         -      sqlite3RefillIndex(pParse, pIndex, iMem);
  3436         -      sqlite3ChangeCookie(pParse, iDb);
  3437         -      sqlite3VdbeAddParseSchemaOp(v, iDb,
  3438         -         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
  3439         -      sqlite3VdbeAddOp0(v, OP_Expire);
  3440         -    }
  3441         -
  3442         -    sqlite3VdbeJumpHere(v, pIndex->tnum);
         3450  +    else if( HasRowid(pTab) || pTblName!=0 ){
         3451  +      Vdbe *v;
         3452  +      char *zStmt;
         3453  +      int iMem = ++pParse->nMem;
         3454  +
         3455  +      v = sqlite3GetVdbe(pParse);
         3456  +      if( v==0 ) goto exit_create_index;
         3457  +
         3458  +      sqlite3BeginWriteOperation(pParse, 1, iDb);
         3459  +
         3460  +      /* Create the rootpage for the index using CreateIndex. But before
         3461  +      ** doing so, code a Noop instruction and store its address in 
         3462  +      ** Index.tnum. This is required in case this index is actually a 
         3463  +      ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
         3464  +      ** that case the convertToWithoutRowidTable() routine will replace
         3465  +      ** the Noop with a Goto to jump over the VDBE code generated below. */
         3466  +      pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
         3467  +      sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
         3468  +
         3469  +      /* Gather the complete text of the CREATE INDEX statement into
         3470  +      ** the zStmt variable
         3471  +      */
         3472  +      if( pStart ){
         3473  +        int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
         3474  +        if( pName->z[n-1]==';' ) n--;
         3475  +        /* A named index with an explicit CREATE INDEX statement */
         3476  +        zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
         3477  +            onError==OE_None ? "" : " UNIQUE", n, pName->z);
         3478  +      }else{
         3479  +        /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
         3480  +        /* zStmt = sqlite3MPrintf(""); */
         3481  +        zStmt = 0;
         3482  +      }
         3483  +
         3484  +      /* Add an entry in sqlite_master for this index
         3485  +      */
         3486  +      sqlite3NestedParse(pParse, 
         3487  +          "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
         3488  +          db->aDb[iDb].zDbSName, MASTER_NAME,
         3489  +          pIndex->zName,
         3490  +          pTab->zName,
         3491  +          iMem,
         3492  +          zStmt
         3493  +          );
         3494  +      sqlite3DbFree(db, zStmt);
         3495  +
         3496  +      /* Fill the index with data and reparse the schema. Code an OP_Expire
         3497  +      ** to invalidate all pre-compiled statements.
         3498  +      */
         3499  +      if( pTblName ){
         3500  +        sqlite3RefillIndex(pParse, pIndex, iMem);
         3501  +        sqlite3ChangeCookie(pParse, iDb);
         3502  +        sqlite3VdbeAddParseSchemaOp(v, iDb,
         3503  +            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
         3504  +        sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
         3505  +      }
         3506  +
         3507  +      sqlite3VdbeJumpHere(v, pIndex->tnum);
         3508  +    }
  3443   3509     }
  3444   3510   
  3445   3511     /* When adding an index to the list of indices for a table, make
  3446   3512     ** sure all indices labeled OE_Replace come after all those labeled
  3447   3513     ** OE_Ignore.  This is necessary for the correct constraint check
  3448   3514     ** processing (in sqlite3GenerateConstraintChecks()) as part of
  3449   3515     ** UPDATE and INSERT statements.  
................................................................................
  3459   3525           pOther = pOther->pNext;
  3460   3526         }
  3461   3527         pIndex->pNext = pOther->pNext;
  3462   3528         pOther->pNext = pIndex;
  3463   3529       }
  3464   3530       pIndex = 0;
  3465   3531     }
         3532  +  else if( IN_RENAME_OBJECT ){
         3533  +    assert( pParse->pNewIndex==0 );
         3534  +    pParse->pNewIndex = pIndex;
         3535  +    pIndex = 0;
         3536  +  }
  3466   3537   
  3467   3538     /* Clean up before exiting */
  3468   3539   exit_create_index:
  3469         -  if( pIndex ) freeIndex(db, pIndex);
         3540  +  if( pIndex ) sqlite3FreeIndex(db, pIndex);
  3470   3541     sqlite3ExprDelete(db, pPIWhere);
  3471   3542     sqlite3ExprListDelete(db, pList);
  3472   3543     sqlite3SrcListDelete(db, pTblName);
  3473   3544     sqlite3DbFree(db, zName);
  3474   3545   }
  3475   3546   
  3476   3547   /*
................................................................................
  3631   3702   
  3632   3703   /*
  3633   3704   ** Append a new element to the given IdList.  Create a new IdList if
  3634   3705   ** need be.
  3635   3706   **
  3636   3707   ** A new IdList is returned, or NULL if malloc() fails.
  3637   3708   */
  3638         -IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
         3709  +IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
         3710  +  sqlite3 *db = pParse->db;
  3639   3711     int i;
  3640   3712     if( pList==0 ){
  3641   3713       pList = sqlite3DbMallocZero(db, sizeof(IdList) );
  3642   3714       if( pList==0 ) return 0;
  3643   3715     }
  3644   3716     pList->a = sqlite3ArrayAllocate(
  3645   3717         db,
................................................................................
  3649   3721         &i
  3650   3722     );
  3651   3723     if( i<0 ){
  3652   3724       sqlite3IdListDelete(db, pList);
  3653   3725       return 0;
  3654   3726     }
  3655   3727     pList->a[i].zName = sqlite3NameFromToken(db, pToken);
         3728  +  if( IN_RENAME_OBJECT && pList->a[i].zName ){
         3729  +    sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
         3730  +  }
  3656   3731     return pList;
  3657   3732   }
  3658   3733   
  3659   3734   /*
  3660   3735   ** Delete an IdList.
  3661   3736   */
  3662   3737   void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
................................................................................
  3895   3970     }
  3896   3971     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
  3897   3972     if( p==0 ){
  3898   3973       goto append_from_error;
  3899   3974     }
  3900   3975     assert( p->nSrc>0 );
  3901   3976     pItem = &p->a[p->nSrc-1];
         3977  +  assert( (pTable==0)==(pDatabase==0) );
         3978  +  assert( pItem->zName==0 || pDatabase!=0 );
         3979  +  if( IN_RENAME_OBJECT && pItem->zName ){
         3980  +    Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
         3981  +    sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
         3982  +  }
  3902   3983     assert( pAlias!=0 );
  3903   3984     if( pAlias->n ){
  3904   3985       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
  3905   3986     }
  3906   3987     pItem->pSelect = pSubquery;
  3907   3988     pItem->pOn = pOn;
  3908   3989     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/dbpage.c.

   365    365   */
   366    366   static int dbpageBegin(sqlite3_vtab *pVtab){
   367    367     DbpageTable *pTab = (DbpageTable *)pVtab;
   368    368     sqlite3 *db = pTab->db;
   369    369     int i;
   370    370     for(i=0; i<db->nDb; i++){
   371    371       Btree *pBt = db->aDb[i].pBt;
   372         -    if( pBt ) sqlite3BtreeBeginTrans(pBt, 1);
          372  +    if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
   373    373     }
   374    374     return SQLITE_OK;
   375    375   }
   376    376   
   377    377   
   378    378   /*
   379    379   ** Invoke this routine to register the "dbpage" virtual table module

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.

   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_CAST || op==TK_UPLUS ){
   145         -      p = p->pLeft;
   146         -      continue;
   147         -    }
   148         -    if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
   149         -      pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
   150         -      break;
   151         -    }
   152    144       if( (op==TK_AGG_COLUMN || op==TK_COLUMN
   153    145             || op==TK_REGISTER || op==TK_TRIGGER)
   154    146        && p->pTab!=0
   155    147       ){
   156    148         /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
   157    149         ** a TK_COLUMN but was previously evaluated and cached in a register */
   158    150         int j = p->iColumn;
   159    151         if( j>=0 ){
   160    152           const char *zColl = p->pTab->aCol[j].zColl;
   161    153           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   162    154         }
   163    155         break;
          156  +    }
          157  +    if( op==TK_CAST || op==TK_UPLUS ){
          158  +      p = p->pLeft;
          159  +      continue;
          160  +    }
          161  +    if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
          162  +      pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
          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;
   170    170           /* The Expr.x union is never used at the same time as Expr.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   **
................................................................................
  1059   1066       if( p->pRight ){
  1060   1067         sqlite3ExprDeleteNN(db, p->pRight);
  1061   1068       }else if( ExprHasProperty(p, EP_xIsSelect) ){
  1062   1069         sqlite3SelectDelete(db, p->x.pSelect);
  1063   1070       }else{
  1064   1071         sqlite3ExprListDelete(db, p->x.pList);
  1065   1072       }
         1073  +    if( !ExprHasProperty(p, EP_Reduced) ){
         1074  +      sqlite3WindowDelete(db, p->pWin);
         1075  +    }
  1066   1076     }
  1067   1077     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1068   1078     if( !ExprHasProperty(p, EP_Static) ){
  1069   1079       sqlite3DbFreeNN(db, p);
  1070   1080     }
  1071   1081   }
  1072   1082   void sqlite3ExprDelete(sqlite3 *db, Expr *p){
................................................................................
  1107   1117   ** The size of the structure can be found by masking the return value
  1108   1118   ** of this routine with 0xfff.  The flags can be found by masking the
  1109   1119   ** return value with EP_Reduced|EP_TokenOnly.
  1110   1120   **
  1111   1121   ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
  1112   1122   ** (unreduced) Expr objects as they or originally constructed by the parser.
  1113   1123   ** During expression analysis, extra information is computed and moved into
  1114         -** later parts of teh Expr object and that extra information might get chopped
         1124  +** later parts of the Expr object and that extra information might get chopped
  1115   1125   ** off if the expression is reduced.  Note also that it does not work to
  1116   1126   ** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
  1117   1127   ** to reduce a pristine expression tree from the parser.  The implementation
  1118   1128   ** of dupedExprStructSize() contain multiple assert() statements that attempt
  1119   1129   ** to enforce this constraint.
  1120   1130   */
  1121   1131   static int dupedExprStructSize(Expr *p, int flags){
  1122   1132     int nSize;
  1123   1133     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
  1124   1134     assert( EXPR_FULLSIZE<=0xfff );
  1125   1135     assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
  1126         -  if( 0==flags || p->op==TK_SELECT_COLUMN ){
         1136  +  if( 0==flags || p->op==TK_SELECT_COLUMN 
         1137  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1138  +   || p->pWin 
         1139  +#endif
         1140  +  ){
  1127   1141       nSize = EXPR_FULLSIZE;
  1128   1142     }else{
  1129   1143       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
  1130   1144       assert( !ExprHasProperty(p, EP_FromJoin) ); 
  1131   1145       assert( !ExprHasProperty(p, EP_MemToken) );
  1132   1146       assert( !ExprHasProperty(p, EP_NoReduce) );
  1133   1147       if( p->pLeft || p->x.pList ){
................................................................................
  1259   1273           pNew->pRight = p->pRight ?
  1260   1274                          exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
  1261   1275         }
  1262   1276         if( pzBuffer ){
  1263   1277           *pzBuffer = zAlloc;
  1264   1278         }
  1265   1279       }else{
         1280  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1281  +      if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
         1282  +        pNew->pWin = 0;
         1283  +      }else{
         1284  +        pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
         1285  +      }
         1286  +#endif /* SQLITE_OMIT_WINDOWFUNC */
  1266   1287         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
  1267   1288           if( pNew->op==TK_SELECT_COLUMN ){
  1268   1289             pNew->pLeft = p->pLeft;
  1269   1290             assert( p->iColumn==0 || p->pRight==0 );
  1270   1291             assert( p->pRight==0  || p->pRight==p->pLeft );
  1271   1292           }else{
  1272   1293             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
................................................................................
  1465   1486       pNew->iLimit = 0;
  1466   1487       pNew->iOffset = 0;
  1467   1488       pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1468   1489       pNew->addrOpenEphm[0] = -1;
  1469   1490       pNew->addrOpenEphm[1] = -1;
  1470   1491       pNew->nSelectRow = p->nSelectRow;
  1471   1492       pNew->pWith = withDup(db, p->pWith);
  1472         -    sqlite3SelectSetName(pNew, p->zSelName);
         1493  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1494  +    pNew->pWin = 0;
         1495  +    pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
         1496  +#endif
         1497  +    pNew->selId = p->selId;
  1473   1498       *pp = pNew;
  1474   1499       pp = &pNew->pPrior;
  1475   1500       pNext = pNew;
  1476   1501     }
  1477   1502   
  1478   1503     return pRet;
  1479   1504   }
................................................................................
  1637   1662     if( pList ){
  1638   1663       struct ExprList_item *pItem;
  1639   1664       assert( pList->nExpr>0 );
  1640   1665       pItem = &pList->a[pList->nExpr-1];
  1641   1666       assert( pItem->zName==0 );
  1642   1667       pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
  1643   1668       if( dequote ) sqlite3Dequote(pItem->zName);
         1669  +    if( IN_RENAME_OBJECT ){
         1670  +      sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
         1671  +    }
  1644   1672     }
  1645   1673   }
  1646   1674   
  1647   1675   /*
  1648   1676   ** Set the ExprList.a[].zSpan element of the most recently added item
  1649   1677   ** on the expression list.
  1650   1678   **
................................................................................
  1817   1845       case TK_COLUMN:
  1818   1846       case TK_AGG_FUNCTION:
  1819   1847       case TK_AGG_COLUMN:
  1820   1848         testcase( pExpr->op==TK_ID );
  1821   1849         testcase( pExpr->op==TK_COLUMN );
  1822   1850         testcase( pExpr->op==TK_AGG_FUNCTION );
  1823   1851         testcase( pExpr->op==TK_AGG_COLUMN );
         1852  +      if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
         1853  +        return WRC_Continue;
         1854  +      }
  1824   1855         if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
  1825   1856           return WRC_Continue;
  1826   1857         }
  1827   1858         /* Fall through */
  1828   1859       case TK_IF_NULL_ROW:
  1829   1860       case TK_REGISTER:
  1830   1861         testcase( pExpr->op==TK_REGISTER );
................................................................................
  1872   1903   ** a constant.
  1873   1904   */
  1874   1905   int sqlite3ExprIsConstant(Expr *p){
  1875   1906     return exprIsConst(p, 1, 0);
  1876   1907   }
  1877   1908   
  1878   1909   /*
  1879         -** Walk an expression tree.  Return non-zero if the expression is constant
  1880         -** that does no originate from the ON or USING clauses of a join.
  1881         -** Return 0 if it involves variables or function calls or terms from
  1882         -** an ON or USING clause.
         1910  +** Walk an expression tree.  Return non-zero if
         1911  +**
         1912  +**   (1) the expression is constant, and
         1913  +**   (2) the expression does originate in the ON or USING clause
         1914  +**       of a LEFT JOIN, and
         1915  +**   (3) the expression does not contain any EP_FixedCol TK_COLUMN
         1916  +**       operands created by the constant propagation optimization.
         1917  +**
         1918  +** When this routine returns true, it indicates that the expression
         1919  +** can be added to the pParse->pConstExpr list and evaluated once when
         1920  +** the prepared statement starts up.  See sqlite3ExprCodeAtInit().
  1883   1921   */
  1884   1922   int sqlite3ExprIsConstantNotJoin(Expr *p){
  1885   1923     return exprIsConst(p, 2, 0);
  1886   1924   }
  1887   1925   
  1888   1926   /*
  1889   1927   ** Walk an expression tree.  Return non-zero if the expression is constant
................................................................................
  1905   1943   
  1906   1944     /* Check if pExpr is identical to any GROUP BY term. If so, consider
  1907   1945     ** it constant.  */
  1908   1946     for(i=0; i<pGroupBy->nExpr; i++){
  1909   1947       Expr *p = pGroupBy->a[i].pExpr;
  1910   1948       if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
  1911   1949         CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
  1912         -      if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){
         1950  +      if( sqlite3IsBinary(pColl) ){
  1913   1951           return WRC_Prune;
  1914   1952         }
  1915   1953       }
  1916   1954     }
  1917   1955   
  1918   1956     /* Check if pExpr is a sub-select. If so, consider it variable. */
  1919   1957     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  2327   2365       if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
  2328   2366         /* The "x IN (SELECT rowid FROM table)" case */
  2329   2367         int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
  2330   2368         VdbeCoverage(v);
  2331   2369   
  2332   2370         sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2333   2371         eType = IN_INDEX_ROWID;
  2334         -
         2372  +      ExplainQueryPlan((pParse, 0,
         2373  +            "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
  2335   2374         sqlite3VdbeJumpHere(v, iAddr);
  2336   2375       }else{
  2337   2376         Index *pIdx;                         /* Iterator variable */
  2338   2377         int affinity_ok = 1;
  2339   2378         int i;
  2340   2379   
  2341   2380         /* Check that the affinity that will be used to perform each 
................................................................................
  2586   2625     int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
  2587   2626     int isRowid             /* If true, LHS of IN operator is a rowid */
  2588   2627   ){
  2589   2628     int jmpIfDynamic = -1;                      /* One-time test address */
  2590   2629     int rReg = 0;                           /* Register storing resulting */
  2591   2630     Vdbe *v = sqlite3GetVdbe(pParse);
  2592   2631     if( NEVER(v==0) ) return 0;
  2593         -  sqlite3ExprCachePush(pParse);
  2594   2632   
  2595   2633     /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
  2596   2634     ** is encountered if any of the following is true:
  2597   2635     **
  2598   2636     **    *  The right-hand side is a correlated subquery
  2599   2637     **    *  The right-hand side is an expression list containing variables
  2600   2638     **    *  We are inside a trigger
................................................................................
  2722   2760               if( isRowid ){
  2723   2761                 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
  2724   2762                                   sqlite3VdbeCurrentAddr(v)+2);
  2725   2763                 VdbeCoverage(v);
  2726   2764                 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
  2727   2765               }else{
  2728   2766                 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
  2729         -              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
  2730   2767                 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
  2731   2768               }
  2732   2769             }
  2733   2770           }
  2734   2771           sqlite3ReleaseTempReg(pParse, r1);
  2735   2772           sqlite3ReleaseTempReg(pParse, r2);
  2736   2773         }
................................................................................
  2803   2840     if( rHasNullFlag ){
  2804   2841       sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
  2805   2842     }
  2806   2843   
  2807   2844     if( jmpIfDynamic>=0 ){
  2808   2845       sqlite3VdbeJumpHere(v, jmpIfDynamic);
  2809   2846     }
  2810         -  sqlite3ExprCachePop(pParse);
  2811   2847   
  2812   2848     return rReg;
  2813   2849   }
  2814   2850   #endif /* SQLITE_OMIT_SUBQUERY */
  2815   2851   
  2816   2852   #ifndef SQLITE_OMIT_SUBQUERY
  2817   2853   /*
................................................................................
  2922   2958     ** at r1.
  2923   2959     **
  2924   2960     ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
  2925   2961     ** so that the fields are in the same order as an existing index.   The
  2926   2962     ** aiMap[] array contains a mapping from the original LHS field order to
  2927   2963     ** the field order that matches the RHS index.
  2928   2964     */
  2929         -  sqlite3ExprCachePush(pParse);
  2930   2965     rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
  2931   2966     for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
  2932   2967     if( i==nVector ){
  2933   2968       /* LHS fields are not reordered */
  2934   2969       rLhs = rLhsOrig;
  2935   2970     }else{
  2936   2971       /* Need to reorder the LHS fields according to aiMap */
................................................................................
  3081   3116     }
  3082   3117   
  3083   3118     /* Jumps here in order to return true. */
  3084   3119     sqlite3VdbeJumpHere(v, addrTruthOp);
  3085   3120   
  3086   3121   sqlite3ExprCodeIN_finished:
  3087   3122     if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
  3088         -  sqlite3ExprCachePop(pParse);
  3089   3123     VdbeComment((v, "end IN expr"));
  3090   3124   sqlite3ExprCodeIN_oom_error:
  3091   3125     sqlite3DbFree(pParse->db, aiMap);
  3092   3126     sqlite3DbFree(pParse->db, zAff);
  3093   3127   }
  3094   3128   #endif /* SQLITE_OMIT_SUBQUERY */
  3095   3129   
................................................................................
  3149   3183       }else{
  3150   3184         if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
  3151   3185         sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  3152   3186       }
  3153   3187     }
  3154   3188   }
  3155   3189   
  3156         -/*
  3157         -** Erase column-cache entry number i
  3158         -*/
  3159         -static void cacheEntryClear(Parse *pParse, int i){
  3160         -  if( pParse->aColCache[i].tempReg ){
  3161         -    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
  3162         -      pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3163         -    }
  3164         -  }
  3165         -  pParse->nColCache--;
  3166         -  if( i<pParse->nColCache ){
  3167         -    pParse->aColCache[i] = pParse->aColCache[pParse->nColCache];
  3168         -  }
  3169         -}
  3170         -
  3171         -
  3172         -/*
  3173         -** Record in the column cache that a particular column from a
  3174         -** particular table is stored in a particular register.
  3175         -*/
  3176         -void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
  3177         -  int i;
  3178         -  int minLru;
  3179         -  int idxLru;
  3180         -  struct yColCache *p;
  3181         -
  3182         -  /* Unless an error has occurred, register numbers are always positive. */
  3183         -  assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
  3184         -  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
  3185         -
  3186         -  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
  3187         -  ** for testing only - to verify that SQLite always gets the same answer
  3188         -  ** with and without the column cache.
  3189         -  */
  3190         -  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
  3191         -
  3192         -  /* First replace any existing entry.
  3193         -  **
  3194         -  ** Actually, the way the column cache is currently used, we are guaranteed
  3195         -  ** that the object will never already be in cache.  Verify this guarantee.
  3196         -  */
  3197         -#ifndef NDEBUG
  3198         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3199         -    assert( p->iTable!=iTab || p->iColumn!=iCol );
  3200         -  }
  3201         -#endif
  3202         -
  3203         -  /* If the cache is already full, delete the least recently used entry */
  3204         -  if( pParse->nColCache>=SQLITE_N_COLCACHE ){
  3205         -    minLru = 0x7fffffff;
  3206         -    idxLru = -1;
  3207         -    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
  3208         -      if( p->lru<minLru ){
  3209         -        idxLru = i;
  3210         -        minLru = p->lru;
  3211         -      }
  3212         -    }
  3213         -    p = &pParse->aColCache[idxLru];
  3214         -  }else{
  3215         -    p = &pParse->aColCache[pParse->nColCache++];
  3216         -  }
  3217         -
  3218         -  /* Add the new entry to the end of the cache */
  3219         -  p->iLevel = pParse->iCacheLevel;
  3220         -  p->iTable = iTab;
  3221         -  p->iColumn = iCol;
  3222         -  p->iReg = iReg;
  3223         -  p->tempReg = 0;
  3224         -  p->lru = pParse->iCacheCnt++;
  3225         -}
  3226         -
  3227         -/*
  3228         -** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
  3229         -** Purge the range of registers from the column cache.
  3230         -*/
  3231         -void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
  3232         -  int i = 0;
  3233         -  while( i<pParse->nColCache ){
  3234         -    struct yColCache *p = &pParse->aColCache[i];
  3235         -    if( p->iReg >= iReg && p->iReg < iReg+nReg ){
  3236         -      cacheEntryClear(pParse, i);
  3237         -    }else{
  3238         -      i++;
  3239         -    }
  3240         -  }
  3241         -}
  3242         -
  3243         -/*
  3244         -** Remember the current column cache context.  Any new entries added
  3245         -** added to the column cache after this call are removed when the
  3246         -** corresponding pop occurs.
  3247         -*/
  3248         -void sqlite3ExprCachePush(Parse *pParse){
  3249         -  pParse->iCacheLevel++;
  3250         -#ifdef SQLITE_DEBUG
  3251         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3252         -    printf("PUSH to %d\n", pParse->iCacheLevel);
  3253         -  }
  3254         -#endif
  3255         -}
  3256         -
  3257         -/*
  3258         -** Remove from the column cache any entries that were added since the
  3259         -** the previous sqlite3ExprCachePush operation.  In other words, restore
  3260         -** the cache to the state it was in prior the most recent Push.
  3261         -*/
  3262         -void sqlite3ExprCachePop(Parse *pParse){
  3263         -  int i = 0;
  3264         -  assert( pParse->iCacheLevel>=1 );
  3265         -  pParse->iCacheLevel--;
  3266         -#ifdef SQLITE_DEBUG
  3267         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3268         -    printf("POP  to %d\n", pParse->iCacheLevel);
  3269         -  }
  3270         -#endif
  3271         -  while( i<pParse->nColCache ){
  3272         -    if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){
  3273         -      cacheEntryClear(pParse, i);
  3274         -    }else{
  3275         -      i++;
  3276         -    }
  3277         -  }
  3278         -}
  3279         -
  3280         -/*
  3281         -** When a cached column is reused, make sure that its register is
  3282         -** no longer available as a temp register.  ticket #3879:  that same
  3283         -** register might be in the cache in multiple places, so be sure to
  3284         -** get them all.
  3285         -*/
  3286         -static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
  3287         -  int i;
  3288         -  struct yColCache *p;
  3289         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3290         -    if( p->iReg==iReg ){
  3291         -      p->tempReg = 0;
  3292         -    }
  3293         -  }
  3294         -}
  3295   3190   
  3296   3191   /* Generate code that will load into register regOut a value that is
  3297   3192   ** appropriate for the iIdxCol-th column of index pIdx.
  3298   3193   */
  3299   3194   void sqlite3ExprCodeLoadIndexColumn(
  3300   3195     Parse *pParse,  /* The parsing context */
  3301   3196     Index *pIdx,    /* The index whose column is to be loaded */
................................................................................
  3343   3238     if( iCol>=0 ){
  3344   3239       sqlite3ColumnDefault(v, pTab, iCol, regOut);
  3345   3240     }
  3346   3241   }
  3347   3242   
  3348   3243   /*
  3349   3244   ** Generate code that will extract the iColumn-th column from
  3350         -** table pTab and store the column value in a register. 
  3351         -**
  3352         -** An effort is made to store the column value in register iReg.  This
  3353         -** is not garanteeed for GetColumn() - the result can be stored in
  3354         -** any register.  But the result is guaranteed to land in register iReg
  3355         -** for GetColumnToReg().
         3245  +** table pTab and store the column value in register iReg. 
  3356   3246   **
  3357   3247   ** There must be an open cursor to pTab in iTable when this routine
  3358   3248   ** is called.  If iColumn<0 then code is generated that extracts the rowid.
  3359   3249   */
  3360   3250   int sqlite3ExprCodeGetColumn(
  3361   3251     Parse *pParse,   /* Parsing and code generating context */
  3362   3252     Table *pTab,     /* Description of the table we are reading from */
  3363   3253     int iColumn,     /* Index of the table column */
  3364   3254     int iTable,      /* The cursor pointing to the table */
  3365   3255     int iReg,        /* Store results here */
  3366   3256     u8 p5            /* P5 value for OP_Column + FLAGS */
  3367   3257   ){
  3368   3258     Vdbe *v = pParse->pVdbe;
  3369         -  int i;
  3370         -  struct yColCache *p;
  3371         -
  3372         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3373         -    if( p->iTable==iTable && p->iColumn==iColumn ){
  3374         -      p->lru = pParse->iCacheCnt++;
  3375         -      sqlite3ExprCachePinRegister(pParse, p->iReg);
  3376         -      return p->iReg;
  3377         -    }
  3378         -  }  
  3379   3259     assert( v!=0 );
  3380   3260     sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
  3381   3261     if( p5 ){
  3382   3262       sqlite3VdbeChangeP5(v, p5);
  3383         -  }else{   
  3384         -    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
  3385   3263     }
  3386   3264     return iReg;
  3387   3265   }
  3388         -void sqlite3ExprCodeGetColumnToReg(
  3389         -  Parse *pParse,   /* Parsing and code generating context */
  3390         -  Table *pTab,     /* Description of the table we are reading from */
  3391         -  int iColumn,     /* Index of the table column */
  3392         -  int iTable,      /* The cursor pointing to the table */
  3393         -  int iReg         /* Store results here */
  3394         -){
  3395         -  int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
  3396         -  if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
  3397         -}
  3398         -
  3399         -
  3400         -/*
  3401         -** Clear all column cache entries.
  3402         -*/
  3403         -void sqlite3ExprCacheClear(Parse *pParse){
  3404         -  int i;
  3405         -
  3406         -#ifdef SQLITE_DEBUG
  3407         -  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
  3408         -    printf("CLEAR\n");
  3409         -  }
  3410         -#endif
  3411         -  for(i=0; i<pParse->nColCache; i++){
  3412         -    if( pParse->aColCache[i].tempReg
  3413         -     && pParse->nTempReg<ArraySize(pParse->aTempReg)
  3414         -    ){
  3415         -       pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg;
  3416         -    }
  3417         -  }
  3418         -  pParse->nColCache = 0;
  3419         -}
  3420         -
  3421         -/*
  3422         -** Record the fact that an affinity change has occurred on iCount
  3423         -** registers starting with iStart.
  3424         -*/
  3425         -void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
  3426         -  sqlite3ExprCacheRemove(pParse, iStart, iCount);
  3427         -}
  3428   3266   
  3429   3267   /*
  3430   3268   ** Generate code to move content from registers iFrom...iFrom+nReg-1
  3431         -** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
         3269  +** over to iTo..iTo+nReg-1.
  3432   3270   */
  3433   3271   void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
  3434   3272     assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
  3435   3273     sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
  3436         -  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
  3437   3274   }
  3438         -
  3439         -#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  3440         -/*
  3441         -** Return true if any register in the range iFrom..iTo (inclusive)
  3442         -** is used as part of the column cache.
  3443         -**
  3444         -** This routine is used within assert() and testcase() macros only
  3445         -** and does not appear in a normal build.
  3446         -*/
  3447         -static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
  3448         -  int i;
  3449         -  struct yColCache *p;
  3450         -  for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
  3451         -    int r = p->iReg;
  3452         -    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
  3453         -  }
  3454         -  return 0;
  3455         -}
  3456         -#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
  3457         -
  3458   3275   
  3459   3276   /*
  3460   3277   ** Convert a scalar expression node to a TK_REGISTER referencing
  3461   3278   ** register iReg.  The caller must ensure that iReg already contains
  3462   3279   ** the correct value for the expression.
  3463   3280   */
  3464   3281   static void exprToRegister(Expr *p, int iReg){
................................................................................
  3549   3366                                 pCol->iSorterColumn, target);
  3550   3367           return target;
  3551   3368         }
  3552   3369         /* Otherwise, fall thru into the TK_COLUMN case */
  3553   3370       }
  3554   3371       case TK_COLUMN: {
  3555   3372         int iTab = pExpr->iTable;
         3373  +      if( ExprHasProperty(pExpr, EP_FixedCol) ){
         3374  +        /* This COLUMN expression is really a constant due to WHERE clause
         3375  +        ** constraints, and that constant is coded by the pExpr->pLeft
         3376  +        ** expresssion.  However, make sure the constant has the correct
         3377  +        ** datatype by applying the Affinity of the table column to the
         3378  +        ** constant.
         3379  +        */
         3380  +        int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
         3381  +        int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
         3382  +        if( aff!=SQLITE_AFF_BLOB ){
         3383  +          static const char zAff[] = "B\000C\000D\000E";
         3384  +          assert( SQLITE_AFF_BLOB=='A' );
         3385  +          assert( SQLITE_AFF_TEXT=='B' );
         3386  +          if( iReg!=target ){
         3387  +            sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
         3388  +            iReg = target;
         3389  +          }
         3390  +          sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
         3391  +                            &zAff[(aff-'B')*2], P4_STATIC);
         3392  +        }
         3393  +        return iReg;
         3394  +      }
  3556   3395         if( iTab<0 ){
  3557   3396           if( pParse->iSelfTab<0 ){
  3558   3397             /* Generating CHECK constraints or inserting into partial index */
  3559   3398             return pExpr->iColumn - pParse->iSelfTab;
  3560   3399           }else{
  3561   3400             /* Coding an expression that is part of an index where column names
  3562   3401             ** in the index refer to the table to which the index belongs */
................................................................................
  3629   3468         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  3630   3469         if( inReg!=target ){
  3631   3470           sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
  3632   3471           inReg = target;
  3633   3472         }
  3634   3473         sqlite3VdbeAddOp2(v, OP_Cast, target,
  3635   3474                           sqlite3AffinityType(pExpr->u.zToken, 0));
  3636         -      testcase( usedAsColumnCache(pParse, inReg, inReg) );
  3637         -      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
  3638   3475         return inReg;
  3639   3476       }
  3640   3477   #endif /* SQLITE_OMIT_CAST */
  3641   3478       case TK_IS:
  3642   3479       case TK_ISNOT:
  3643   3480         op = (op==TK_IS) ? TK_EQ : TK_NE;
  3644   3481         p5 = SQLITE_NULLEQ;
................................................................................
  3773   3610         FuncDef *pDef;         /* The function definition object */
  3774   3611         const char *zId;       /* The function name */
  3775   3612         u32 constMask = 0;     /* Mask of function arguments that are constant */
  3776   3613         int i;                 /* Loop counter */
  3777   3614         sqlite3 *db = pParse->db;  /* The database connection */
  3778   3615         u8 enc = ENC(db);      /* The text encoding used by this database */
  3779   3616         CollSeq *pColl = 0;    /* A collating sequence */
         3617  +
         3618  +#ifndef SQLITE_OMIT_WINDOWFUNC
         3619  +      if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
         3620  +        return pExpr->pWin->regResult;
         3621  +      }
         3622  +#endif
  3780   3623   
  3781   3624         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
  3782   3625           /* SQL functions can be expensive. So try to move constant functions
  3783   3626           ** out of the inner loop, even if that means an extra OP_Copy. */
  3784   3627           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
  3785   3628         }
  3786   3629         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
................................................................................
  3810   3653         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
  3811   3654           int endCoalesce = sqlite3VdbeMakeLabel(v);
  3812   3655           assert( nFarg>=2 );
  3813   3656           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  3814   3657           for(i=1; i<nFarg; i++){
  3815   3658             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
  3816   3659             VdbeCoverage(v);
  3817         -          sqlite3ExprCacheRemove(pParse, target, 1);
  3818         -          sqlite3ExprCachePush(pParse);
  3819   3660             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
  3820         -          sqlite3ExprCachePop(pParse);
  3821   3661           }
  3822   3662           sqlite3VdbeResolveLabel(v, endCoalesce);
  3823   3663           break;
  3824   3664         }
  3825   3665   
  3826   3666         /* The UNLIKELY() function is a no-op.  The result is the value
  3827   3667         ** of the first argument.
................................................................................
  3879   3719               assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
  3880   3720               testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
  3881   3721               pFarg->a[0].pExpr->op2 = 
  3882   3722                     pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
  3883   3723             }
  3884   3724           }
  3885   3725   
  3886         -        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
  3887   3726           sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
  3888   3727                                   SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
  3889         -        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
  3890   3728         }else{
  3891   3729           r1 = 0;
  3892   3730         }
  3893   3731   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3894   3732         /* Possibly overload the function if the first argument is
  3895   3733         ** a virtual table column.
  3896   3734         **
................................................................................
  3899   3737         ** see if it is a column in a virtual table.  This is done because
  3900   3738         ** the left operand of infix functions (the operand we want to
  3901   3739         ** control overloading) ends up as the second argument to the
  3902   3740         ** function.  The expression "A glob B" is equivalent to 
  3903   3741         ** "glob(B,A).  We want to use the A in "A glob B" to test
  3904   3742         ** for function overloading.  But we use the B term in "glob(B,A)".
  3905   3743         */
  3906         -      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
         3744  +      if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
  3907   3745           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
  3908   3746         }else if( nFarg>0 ){
  3909   3747           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
  3910   3748         }
  3911   3749   #endif
  3912   3750         if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  3913   3751           if( !pColl ) pColl = db->pDfltColl; 
................................................................................
  4055   3893         sqlite3ErrorMsg(pParse, "row value misused");
  4056   3894         break;
  4057   3895       }
  4058   3896   
  4059   3897       case TK_IF_NULL_ROW: {
  4060   3898         int addrINR;
  4061   3899         addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
  4062         -      sqlite3ExprCachePush(pParse);
  4063   3900         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  4064         -      sqlite3ExprCachePop(pParse);
  4065   3901         sqlite3VdbeJumpHere(v, addrINR);
  4066   3902         sqlite3VdbeChangeP3(v, addrINR, inReg);
  4067   3903         break;
  4068   3904       }
  4069   3905   
  4070   3906       /*
  4071   3907       ** Form A:
................................................................................
  4094   3930         int nExpr;                        /* 2x number of WHEN terms */
  4095   3931         int i;                            /* Loop counter */
  4096   3932         ExprList *pEList;                 /* List of WHEN terms */
  4097   3933         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  4098   3934         Expr opCompare;                   /* The X==Ei expression */
  4099   3935         Expr *pX;                         /* The X expression */
  4100   3936         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
  4101         -      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
  4102   3937   
  4103   3938         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  4104   3939         assert(pExpr->x.pList->nExpr > 0);
  4105   3940         pEList = pExpr->x.pList;
  4106   3941         aListelem = pEList->a;
  4107   3942         nExpr = pEList->nExpr;
  4108   3943         endLabel = sqlite3VdbeMakeLabel(v);
................................................................................
  4118   3953           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  4119   3954           ** The value in regFree1 might get SCopy-ed into the file result.
  4120   3955           ** So make sure that the regFree1 register is not reused for other
  4121   3956           ** purposes and possibly overwritten.  */
  4122   3957           regFree1 = 0;
  4123   3958         }
  4124   3959         for(i=0; i<nExpr-1; i=i+2){
  4125         -        sqlite3ExprCachePush(pParse);
  4126   3960           if( pX ){
  4127   3961             assert( pTest!=0 );
  4128   3962             opCompare.pRight = aListelem[i].pExpr;
  4129   3963           }else{
  4130   3964             pTest = aListelem[i].pExpr;
  4131   3965           }
  4132   3966           nextCase = sqlite3VdbeMakeLabel(v);
  4133   3967           testcase( pTest->op==TK_COLUMN );
  4134   3968           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
  4135   3969           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
  4136   3970           sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
  4137   3971           sqlite3VdbeGoto(v, endLabel);
  4138         -        sqlite3ExprCachePop(pParse);
  4139   3972           sqlite3VdbeResolveLabel(v, nextCase);
  4140   3973         }
  4141   3974         if( (nExpr&1)!=0 ){
  4142         -        sqlite3ExprCachePush(pParse);
  4143   3975           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
  4144         -        sqlite3ExprCachePop(pParse);
  4145   3976         }else{
  4146   3977           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  4147   3978         }
  4148         -      assert( pParse->db->mallocFailed || pParse->nErr>0 
  4149         -           || pParse->iCacheLevel==iCacheLevel );
  4150   3979         sqlite3VdbeResolveLabel(v, endLabel);
  4151   3980         break;
  4152   3981       }
  4153   3982   #ifndef SQLITE_OMIT_TRIGGER
  4154   3983       case TK_RAISE: {
  4155   3984         assert( pExpr->affinity==OE_Rollback 
  4156   3985              || pExpr->affinity==OE_Abort
................................................................................
  4292   4121   /*
  4293   4122   ** Generate code that will evaluate expression pExpr and store the
  4294   4123   ** results in register target.  The results are guaranteed to appear
  4295   4124   ** in register target.  If the expression is constant, then this routine
  4296   4125   ** might choose to code the expression at initialization time.
  4297   4126   */
  4298   4127   void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
  4299         -  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
         4128  +  if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
  4300   4129       sqlite3ExprCodeAtInit(pParse, pExpr, target);
  4301   4130     }else{
  4302   4131       sqlite3ExprCode(pParse, pExpr, target);
  4303   4132     }
  4304   4133   }
  4305   4134   
  4306   4135   /*
................................................................................
  4374   4203       if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
  4375   4204         if( flags & SQLITE_ECEL_OMITREF ){
  4376   4205           i--;
  4377   4206           n--;